command.php

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

PHP
546
字号
            $this->commandStatus = -1;            $error = PEAR::raiseError(null, SYSTEM_COMMAND_COMMAND_PLACEMENT, null, E_USER_WARNING, null, 'System_Command_Error', true);        }        // check for error here        $command = escapeshellcmd($this->which($in_command));        if ($command === false) {            $error = PEAR::raiseError(null, SYSTEM_COMMAND_INVALID_COMMAND, null, E_USER_WARNING, null, 'System_Command_Error', true);        }        $argv = func_get_args();        array_shift($argv);        foreach($argv as $arg) {            if (strpos($arg, '-') === 0) {                $command .= ' ' . $arg;             }            elseif ($arg != '') {                $command .= ' ' . escapeshellarg($arg);            }        }        $this->previousElement = $command;        $this->systemCommand .= $command;        return isset($error) ? $error : true;    }    // }}}    // {{{ pushOperator()    /**     * Used to push an operator onto the running command to be executed     *     * @param  string $in_operator Either string reprentation of operator or system character     *     * @access public     * @return boolean true on success {or System_Command_Error Exception}     */    function pushOperator($in_operator)    {        $operator = isset($this->controlOperators[$in_operator]) ? $this->controlOperators[$in_operator] : $in_operator;        if (is_null($this->previousElement) || in_array($this->previousElement, $this->controlOperators)) {            $this->commandStatus = -1;            $error = PEAR::raiseError(null, SYSTEM_COMMAND_OPERATOR_PLACEMENT, null, E_USER_WARNING, null, 'System_Command_Error', true);        }        elseif (!in_array($operator, $this->controlOperators)) {            $this->commandStatus = -1;            $error = PEAR::raiseError(null, SYSTEM_COMMAND_INVALID_OPERATOR, null, E_USER_WARNING, $operator, 'System_Command_Error', true);        }        $this->previousElement = $operator;        $this->systemCommand .= ' ' . $operator . ' ';        return isset($error) ? $error : true;    }    // }}}    // {{{ execute()    /**     * Executes the code according to given options     *     * @return bool true if success {or System_Command_Exception}     *     * @access public     */    function execute()     {        // if the command is empty or if the last element was a control operator, we can't continue        if (is_null($this->previousElement) || $this->commandStatus == -1 || in_array($this->previousElement, $this->controlOperators)) {            return PEAR::raiseError(null, SYSTEM_COMMAND_INVALID_COMMAND, null, E_USER_WARNING, $this->systemCommand, 'System_Command_Error', true);        }        // Warning about impossible mix of options        if (!empty($this->options['OUTPUT'])) {            if (!empty($this->options['SHUTDOWN']) || !empty($this->options['NOHUP'])) {                return PEAR::raiseError(null, SYSTEM_COMMAND_NO_OUTPUT, null, E_USER_WARNING, null, 'System_Command_Error', true);            }        }                        // if this is not going to stdout, then redirect to /dev/null        if (empty($this->options['OUTPUT'])) {            $this->systemCommand .= ' >/dev/null';        }                        $suffix = '';        // run a command immune to hangups, with output to a non-tty        if (!empty($this->options['NOHUP'])) {            $this->systemCommand = $this->options['NOHUP'] . $this->systemCommand;        }        // run a background process (only if not nohup)        elseif (!empty($this->options['BACKGROUND'])) {            $suffix = ' &';        }                        // Register to be run on shutdown        if (!empty($this->options['SHUTDOWN'])) {            $line = "system(\"{$this->systemCommand}$suffix\");";            $function = create_function('', $line);            register_shutdown_function($function);            return true;        }         else {            // send stderr to a file so that we can reap the error message            $tmpFile = tempnam($this->tmpDir, 'System_Command-');            $this->systemCommand .= ' 2>' . $tmpFile . $suffix;            $shellPipe = $this->which('echo') . ' ' . escapeshellarg($this->systemCommand) . ' | ' . $this->options['SHELL'];            exec($shellPipe, $result, $returnVal);            if ($returnVal !== 0) {                $error = implode('', file($tmpFile));                $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, null, 'System_Command_Error', true);            }            else {                $return = implode("\n", $result);            }            unlink($tmpFile);            return $return;        }    }    // }}}    // {{{ which()    /**     * Functionality similiar to unix 'which'. Searches the path     * for the specified program.      *     * @param $cmd name of the executable to search for      *     * @access private     * @return string returns the full path if found, false if not     */    function which($in_cmd)    {        // if we already have a binary, then return it        if (is_executable($in_cmd)) {            return $in_cmd;        }        $paths = explode(':', $_ENV['PATH']);                    foreach ($paths as $path) {            $location = $path . '/' . $in_cmd;                                        if (is_executable($location)) {                return $location;            }        }        return false;    }       // }}}    // {{{ reset()    /**     * Prepare for a new command to be built     *     * @access public     * @return void     */    function reset()    {        $this->previousElement = null;        $this->systemCommand = null;        $this->commandStatus = 0;    }    // }}}    // {{{ errorMessage()    /**     * Return a textual error message for a System_Command error code     *     * @param integer error code     *     * @return string error message, or false if the error code was     * not recognized     */    function errorMessage($in_value)    {        static $errorMessages;        if (!isset($errorMessages)) {            $errorMessages = array(                SYSTEM_COMMAND_OK                     => 'no error',                SYSTEM_COMMAND_ERROR                  => 'unknown error',                SYSTEM_COMMAND_NO_SHELL               => 'no shell found',                SYSTEM_COMMAND_INVALID_SHELL          => 'invalid shell',                SYSTEM_COMMAND_TMPDIR_ERROR           => 'could not create temporary directory',                SYSTEM_COMMAND_INVALID_OPERATOR       => 'control operator invalid',                SYSTEM_COMMAND_INVALID_COMMAND        => 'invalid system command',                SYSTEM_COMMAND_OPERATOR_PLACEMENT     => 'invalid placement of control operator',                SYSTEM_COMMAND_COMMAND_PLACEMENT      => 'invalid placement of command',                SYSTEM_COMMAND_NOHUP_MISSING          => 'nohup not found on system',                SYSTEM_COMMAND_NO_OUTPUT              => 'output not allowed',                SYSTEM_COMMAND_STDERR                 => 'command wrote to stderr',            );        }        if (System_Command::isError($in_value)) {            $in_value = $in_value->getCode();        }        return isset($errorMessages[$in_value]) ? $errorMessages[$in_value] : $errorMessages[SYSTEM_COMMAND_ERROR];    }    // }}}    // {{{ isError()    /**     * Tell whether a result code from a System_Command method is an error     *     * @param int result code     *     * @return bool whether $in_value is an error     *     * @access public     */    function isError($in_value)    {        return (is_object($in_value) &&                (strtolower(get_class($in_value)) == 'system_command_error' ||                 is_subclass_of($in_value, 'system_command_error')));    }        // }}}}// {{{ class System_Command_Error/** * System_Command_Error constructor. * * @param mixed      System_Command error code, or string with error message. * @param integer    what "error mode" to operate in * @param integer    what error level to use for $mode & PEAR_ERROR_TRIGGER * @param mixed      additional debug info, such as the last query * * @access public * * @see PEAR_Error */// }}}class System_Command_Error extends PEAR_Error{    // {{{ properties    /**     * Message in front of the error message     * @var string $error_message_prefix     */    var $error_message_prefix = 'System_Command Error: ';    // }}}    // {{{ constructor    function System_Command_Error($code = SYSTEM_COMMAND_ERROR, $mode = PEAR_ERROR_RETURN,              $level = E_USER_NOTICE, $debuginfo = null)    {        if (is_int($code)) {            $this->PEAR_Error(System_Command::errorMessage($code), $code, $mode, $level, $debuginfo);        } else {            $this->PEAR_Error("Invalid error code: $code", SYSTEM_COMMAND_ERROR, $mode, $level, $debuginfo);        }    }        // }}}}?>

⌨️ 快捷键说明

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