cli.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 795 行 · 第 1/2 页

PHP
795
字号
<?php/** * PEAR_Frontend_CLI * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt.  If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category   pear * @package    PEAR * @author     Stig Bakken <ssb@php.net> * @author     Greg Beaver <cellog@php.net> * @copyright  1997-2006 The PHP Group * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 * @version    CVS: $Id: CLI.php,v 1.67 2007/03/14 13:42:24 timj Exp $ * @link       http://pear.php.net/package/PEAR * @since      File available since Release 0.1 *//** * base class */require_once 'PEAR/Frontend.php';/** * Command-line Frontend for the PEAR Installer * @category   pear * @package    PEAR * @author     Stig Bakken <ssb@php.net> * @author     Greg Beaver <cellog@php.net> * @copyright  1997-2006 The PHP Group * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 * @version    Release: 1.6.1 * @link       http://pear.php.net/package/PEAR * @since      Class available since Release 0.1 */class PEAR_Frontend_CLI extends PEAR_Frontend{    // {{{ properties    /**     * What type of user interface this frontend is for.     * @var string     * @access public     */    var $type = 'CLI';    var $lp = ''; // line prefix    var $params = array();    var $term = array(        'bold' => '',        'normal' => '',        );    // }}}    // {{{ constructor    function PEAR_Frontend_CLI()    {        parent::PEAR();        $term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1        if (function_exists('posix_isatty') && !posix_isatty(1)) {            // output is being redirected to a file or through a pipe        } elseif ($term) {            // XXX can use ncurses extension here, if available            if (preg_match('/^(xterm|vt220|linux)/', $term)) {                $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109);                $this->term['normal']=sprintf("%c%c%c", 27, 91, 109);            } elseif (preg_match('/^vt100/', $term)) {                $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0);                $this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0);            }        } elseif (OS_WINDOWS) {            // XXX add ANSI codes here        }    }    // }}}    // {{{ displayLine(text)    function displayLine($text)    {        trigger_error("PEAR_Frontend_CLI::displayLine deprecated", E_USER_ERROR);    }    function _displayLine($text)    {        print "$this->lp$text\n";    }    // }}}    // {{{ display(text)    function display($text)    {        trigger_error("PEAR_Frontend_CLI::display deprecated", E_USER_ERROR);    }    function _display($text)    {        print $text;    }    // }}}    // {{{ displayError(eobj)    /**     * @param object PEAR_Error object     */    function displayError($eobj)    {        return $this->_displayLine($eobj->getMessage());    }    // }}}    // {{{ displayFatalError(eobj)    /**     * @param object PEAR_Error object     */    function displayFatalError($eobj)    {        $this->displayError($eobj);        if (class_exists('PEAR_Config')) {            $config = &PEAR_Config::singleton();            if ($config->get('verbose') > 5) {                if (function_exists('debug_print_backtrace')) {                    debug_print_backtrace();                } elseif (function_exists('debug_backtrace')) {                    $trace = debug_backtrace();                    $raised = false;                    foreach ($trace as $i => $frame) {                        if (!$raised) {                            if (isset($frame['class']) && strtolower($frame['class']) ==                                  'pear' && strtolower($frame['function']) == 'raiseerror') {                                $raised = true;                            } else {                                continue;                            }                        }                        if (!isset($frame['class'])) {                            $frame['class'] = '';                        }                        if (!isset($frame['type'])) {                            $frame['type'] = '';                        }                        if (!isset($frame['function'])) {                            $frame['function'] = '';                        }                        if (!isset($frame['line'])) {                            $frame['line'] = '';                        }                        $this->_displayLine("#$i: $frame[class]$frame[type]$frame[function] $frame[line]");                    }                }            }        }        exit(1);    }    // }}}    // {{{ displayHeading(title)    function displayHeading($title)    {        trigger_error("PEAR_Frontend_CLI::displayHeading deprecated", E_USER_ERROR);    }    function _displayHeading($title)    {        print $this->lp.$this->bold($title)."\n";        print $this->lp.str_repeat("=", strlen($title))."\n";    }    // }}}    /**     * Instruct the runInstallScript method to skip a paramgroup that matches the     * id value passed in.     *     * This method is useful for dynamically configuring which sections of a post-install script     * will be run based on the user's setup, which is very useful for making flexible     * post-install scripts without losing the cross-Frontend ability to retrieve user input     * @param string     */    function skipParamgroup($id)    {        $this->_skipSections[$id] = true;    }    function runPostinstallScripts(&$scripts)    {        foreach ($scripts as $i => $script) {            $this->runInstallScript($scripts[$i]->_params, $scripts[$i]->_obj);        }    }    /**     * @param array $xml contents of postinstallscript tag     * @param object $script post-installation script     * @param string install|upgrade     */    function runInstallScript($xml, &$script)    {        $this->_skipSections = array();        if (!is_array($xml) || !isset($xml['paramgroup'])) {            $script->run(array(), '_default');        } else {            $completedPhases = array();            if (!isset($xml['paramgroup'][0])) {                $xml['paramgroup'] = array($xml['paramgroup']);            }            foreach ($xml['paramgroup'] as $group) {                if (isset($this->_skipSections[$group['id']])) {                    // the post-install script chose to skip this section dynamically                    continue;                }                if (isset($group['name'])) {                    $paramname = explode('::', $group['name']);                    if ($lastgroup['id'] != $paramname[0]) {                        continue;                    }                    $group['name'] = $paramname[1];                    if (isset($answers)) {                        if (isset($answers[$group['name']])) {                            switch ($group['conditiontype']) {                                case '=' :                                    if ($answers[$group['name']] != $group['value']) {                                        continue 2;                                    }                                break;                                case '!=' :                                    if ($answers[$group['name']] == $group['value']) {                                        continue 2;                                    }                                break;                                case 'preg_match' :                                    if (!@preg_match('/' . $group['value'] . '/',                                          $answers[$group['name']])) {                                        continue 2;                                    }                                break;                                default :                                return;                            }                        }                    } else {                        return;                    }                }                $lastgroup = $group;                if (isset($group['instructions'])) {                    $this->_display($group['instructions']);                }                if (!isset($group['param'][0])) {                    $group['param'] = array($group['param']);                }                if (isset($group['param'])) {                    if (method_exists($script, 'postProcessPrompts')) {                        $prompts = $script->postProcessPrompts($group['param'], $group['id']);                        if (!is_array($prompts) || count($prompts) != count($group['param'])) {                            $this->outputData('postinstall', 'Error: post-install script did not ' .                                'return proper post-processed prompts');                            $prompts = $group['param'];                        } else {                            foreach ($prompts as $i => $var) {                                if (!is_array($var) || !isset($var['prompt']) ||                                      !isset($var['name']) ||                                      ($var['name'] != $group['param'][$i]['name']) ||                                      ($var['type'] != $group['param'][$i]['type'])) {                                    $this->outputData('postinstall', 'Error: post-install script ' .                                        'modified the variables or prompts, severe security risk. ' .                                        'Will instead use the defaults from the package.xml');                                    $prompts = $group['param'];                                }                            }                        }                        $answers = $this->confirmDialog($prompts);                    } else {                        $answers = $this->confirmDialog($group['param']);                    }                }                if ((isset($answers) && $answers) || !isset($group['param'])) {                    if (!isset($answers)) {                        $answers = array();                    }                    array_unshift($completedPhases, $group['id']);                    if (!$script->run($answers, $group['id'])) {                        $script->run($completedPhases, '_undoOnError');                        return;                    }                } else {                    $script->run($completedPhases, '_undoOnError');                    return;                }            }        }    }    /**     * Ask for user input, confirm the answers and continue until the user is satisfied     * @param array an array of arrays, format array('name' => 'paramname', 'prompt' =>     *              'text to display', 'type' => 'string'[, default => 'default value'])     * @return array     */    function confirmDialog($params)    {        $answers = array();        $prompts = $types = array();        foreach ($params as $param) {            $prompts[$param['name']] = $param['prompt'];            $types[$param['name']] = $param['type'];            if (isset($param['default'])) {                $answers[$param['name']] = $param['default'];            } else {                $answers[$param['name']] = '';            }        }        $tried = false;        do {            if ($tried) {                $i = 1;                foreach ($answers as $var => $value) {                    if (!strlen($value)) {                        echo $this->bold("* Enter an answer for #" . $i . ": ({$prompts[$var]})\n");                    }                    $i++;                }            }            $answers = $this->userDialog('', $prompts, $types, $answers);            $tried = true;        } while (is_array($answers) && count(array_filter($answers)) != count($prompts));        return $answers;    }    // {{{ userDialog(prompt, [type], [default])    function userDialog($command, $prompts, $types = array(), $defaults = array(),                        $screensize = 20)    {        if (!is_array($prompts)) {            return array();        }        $testprompts = array_keys($prompts);        $result = $defaults;        if (!defined('STDIN')) {            $fp = fopen('php://stdin', 'r');        } else {            $fp = STDIN;        }        reset($prompts);        if (count($prompts) == 1 && $types[key($prompts)] == 'yesno') {            foreach ($prompts as $key => $prompt) {                $type = $types[$key];                $default = @$defaults[$key];                print "$prompt ";                if ($default) {                    print "[$default] ";                }                print ": ";                if (version_compare(phpversion(), '5.0.0', '<')) {                    $line = fgets($fp, 2048);                } else {                    if (!defined('STDIN')) {                        define('STDIN', fopen('php://stdin', 'r'));                    }                    $line = fgets(STDIN, 2048);                }                if ($default && trim($line) == "") {                    $result[$key] = $default;                } else {                    $result[$key] = trim($line);                }            }            return $result;        }        while (true) {            $descLength = max(array_map('strlen', $prompts));            $descFormat = "%-{$descLength}s";            $last = count($prompts);            $i = 0;            foreach ($prompts as $n => $var) {                printf("%2d. $descFormat : %s\n", ++$i, $prompts[$n], isset($result[$n]) ?                    $result[$n] : null);            }            print "\n1-$last, 'all', 'abort', or Enter to continue: ";            $tmp = trim(fgets($fp, 1024));            if (empty($tmp)) {                break;            }            if ($tmp == 'abort') {                return false;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?