command.php

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

PHP
546
字号
<?php// {{{ license// +----------------------------------------------------------------------+// | PHP Version 4.0                                                      |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group                                |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 of the PHP license,      |// | that is bundled with this package in the file LICENSE, and is        |// | available at through the world-wide-web at                           |// | http://www.php.net/license/2_02.txt.                                 |// | If you did not receive a copy of the PHP license and are unable to   |// | obtain it through the world-wide-web, please send a note to          |// | license@php.net so we can mail you a copy immediately.               |// +----------------------------------------------------------------------+// | Author: Anders Johannsen <anders@johannsen.com>                      |// | Author: Dan Allen <dan@mojavelinux.com>// +----------------------------------------------------------------------+// $Id: Command.php,v 1.4 2004/03/02 05:21:41 dallen Exp $// }}}// {{{ includesrequire_once 'PEAR.php';require_once 'System.php';// }}}// {{{ constantsdefine('SYSTEM_COMMAND_OK',                 1);define('SYSTEM_COMMAND_ERROR',             -1);define('SYSTEM_COMMAND_NO_SHELL',          -2);define('SYSTEM_COMMAND_INVALID_SHELL',     -3);define('SYSTEM_COMMAND_TMPDIR_ERROR',      -4);define('SYSTEM_COMMAND_INVALID_OPERATOR',  -5);define('SYSTEM_COMMAND_INVALID_COMMAND',   -6);define('SYSTEM_COMMAND_OPERATOR_PLACEMENT',-7);define('SYSTEM_COMMAND_COMMAND_PLACEMENT', -8);define('SYSTEM_COMMAND_NOHUP_MISSING',     -9);define('SYSTEM_COMMAND_NO_OUTPUT',        -10);define('SYSTEM_COMMAND_STDERR',           -11);// }}}// {{{ class System_Command/** * The System_Command:: class implements an abstraction for various ways  * of executing commands (directly using the backtick operator, * as a background task after the script has terminated using * register_shutdown_function() or as a detached process using nohup). * * @author  Anders Johannsen <anders@johannsen.com> * @author  Dan Allen <dan@mojavelinux.com> * @version $Revision: 1.4 $ */// }}}class System_Command extends PEAR {    // {{{ properties    /**     * Array of settings used when creating the shell command     *     * @var array     * @access private     */    var $options = array();    /**     * Array of available shells to use to execute the command     *     * @var array     * @access private     */    var $shells = array();    /**     * Array of available control operators used between commands     *     * @var array     * @access private     */    var $controlOperators = array();    /**     * The system command to be executed     *     * @var string     * @access private     */    var $systemCommand = null;    /**     * Previously added part to the command string     *     * @var string     * @access private     */    var $previousElement = null;    /**     * Directory for writing stderr output     *     * @var string     * @access private     */    var $tmpDir = null;    /**     * To allow the pear error object to accumulate when building     * the command, we use the command status to keep track when     * a pear error is raised     *     * @var int     * @access private     */    var $commandStatus = 0;            // }}}    // {{{ constructor    /**     * Class constructor     *      * Defines all necessary constants and sets defaults     *      * @access public     */    function System_Command($in_shell = null)    {        parent::PEAR();        // Defining constants        $this->options = array(            'SEQUENCE'   => true,            'SHUTDOWN'   => false,            'SHELL'      => $this->which($in_shell),            'OUTPUT'     => true,            'NOHUP'      => false,            'BACKGROUND' => false,        );        // prepare the available control operators        $this->controlOperators = array(            'PIPE'  => '|',            'AND'   => '&&',            'OR'    => '||',            'GROUP' => ';',            'LFIFO' => '<',            'RFIFO' => '>',        );                        // List of allowed/available shells        $this->shells = array(            'sh',            'bash',            'zsh',            'tcsh',            'csh',            'ash',            'sash',            'esh',            'ksh'        );                                           // Find the first available shell        if (empty($this->options['SHELL'])) {            foreach ($this->shells as $shell) {                if ($this->options['SHELL'] = $this->which($shell)) {                    break;                }            }            // see if we still have no shell            if (empty($this->options['SHELL'])) {                $this = PEAR::raiseError(null, SYSTEM_COMMAND_NO_SHELL, null, E_USER_WARNING, null, 'System_Command_Error', true);                return;            }        }        // Caputre a temporary directory for capturing stderr from commands        $this->tmpdir = System::tmpdir();        if (!System::mkDir("-p {$this->tmpdir}")) {            $this = PEAR::raiseError(null, SYSTEM_COMMAND_TMPDIR_ERROR, null, E_USER_WARNING, null, 'System_Command_Error', true);            return;        }    }            // }}}    // {{{ setOption()    /**     * Sets any option     *      * The options are currently:     * SEQUENCE : Allow a sequence command or not (right now this is always on)     * SHUTDOWN : Execute commands via a shutdown function      * SHELL    : Path to shell     * OUTPUT   : Output stdout from process     * NOHUP    : Use nohup to detach process     * BACKGROUND : Run as a background process with &     *     * @param $in_option is a constant, which corresponds to the     *                option that should be changed     * @param $in_setting is the value of the option currently     *                 being toggled.     * @access public     * @return bool true if succes, else false     */    function setOption($in_option, $in_setting)    {        $option = strtoupper($in_option);        if (empty($this->options[$option])) {            PEAR::raiseError(null, SYSTEM_COMMAND_ERROR, null, E_USER_NOTICE, null, 'System_Command_Error', true);            return false;        }                        switch ($option) {            case 'OUTPUT':            case 'SHUTDOWN':            case 'SEQUENCE':            case 'BACKGROUND':                $this->options[$option] = !empty($in_setting);                return true;            break;                            case 'SHELL':                if (($shell = $this->which($in_setting)) !== false) {                    $this->options[$option] = $shell;                    return true;                }                 else {                    PEAR::raiseError(null, SYSTEM_COMMAND_NO_SHELL, null, E_USER_NOTICE, $in_setting, 'System_Command_Error', true);                    return false;                }            break;                                    case 'NOHUP':                if (empty($in_setting)) {                    $this->options[$option] = false;                }                 else if ($location = $this->which('nohup')) {                    $this->options[$option] = $location;                }                 else {                    PEAR::raiseError(null, SYSTEM_COMMAND_NOHUP_MISSING, null, E_USER_NOTICE, null, 'System_Command_Error', true);                    return false;                }            break;        }    }        // }}}    // {{{ pushCommand()    /**     * Used to push a command onto the running command to be executed     *     * @param  string $in_command binary to be run     * @param  string $in_argument either an option or argument value, to be handled appropriately     * @param  string $in_argument     * @param  ...     *     * @access public     * @return boolean true on success {or System_Command_Error Exception}     */    function pushCommand($in_command)    {        if (!is_null($this->previousElement) && !in_array($this->previousElement, $this->controlOperators)) {

⌨️ 快捷键说明

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