123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- <?php
- require_once 'PEAR.php';
- require_once 'System.php';
- define('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);
- define('SYSTEM_COMMAND_NONZERO_EXIT', -12);
- class System_Command {
-
-
- var $options = array();
-
- var $shells = array();
-
- var $controlOperators = array();
-
- var $systemCommand = null;
-
- var $previousElement = null;
-
- var $tmpDir = null;
-
- var $commandStatus = 0;
-
-
- var $_initError = null;
-
-
-
-
- function System_Command($in_shell = null)
- {
-
- $this->options = array(
- 'SEQUENCE' => true,
- 'SHUTDOWN' => false,
- 'SHELL' => $this->which($in_shell),
- 'OUTPUT' => true,
- 'NOHUP' => false,
- 'BACKGROUND' => false,
- 'STDERR' => false
- );
-
- $this->controlOperators = array(
- 'PIPE' => '|',
- 'AND' => '&&',
- 'OR' => '||',
- 'GROUP' => ';',
- 'LFIFO' => '<',
- 'RFIFO' => '>',
- );
-
-
- $this->shells = array(
- 'sh',
- 'bash',
- 'zsh',
- 'tcsh',
- 'csh',
- 'ash',
- 'sash',
- 'esh',
- 'ksh'
- );
-
-
- if (empty($this->options['SHELL'])) {
- foreach ($this->shells as $shell) {
- if ($this->options['SHELL'] = $this->which($shell)) {
- break;
- }
- }
-
- if (empty($this->options['SHELL'])) {
- $this->_initError =& PEAR::raiseError(null, SYSTEM_COMMAND_NO_SHELL, null, E_USER_WARNING, null, 'System_Command_Error', true);
- return;
- }
- }
-
- $this->tmpDir = System::tmpdir();
- if (!System::mkDir("-p {$this->tmpDir}")) {
- $this->_initError =& PEAR::raiseError(null, SYSTEM_COMMAND_TMPDIR_ERROR, null, E_USER_WARNING, null, 'System_Command_Error', true);
- return;
- }
- }
-
-
-
-
- function setOption($in_option, $in_setting)
- {
- if ($this->_initError) {
- return $this->_initError;
- }
- $option = strtoupper($in_option);
- if (!isset($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':
- case 'STDERR':
- $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;
- }
- }
-
-
-
-
- function pushCommand($in_command)
- {
- if ($this->_initError) {
- return $this->_initError;
- }
-
- if (!is_null($this->previousElement) && !in_array($this->previousElement, $this->controlOperators)) {
- $this->commandStatus = -1;
- $error = PEAR::raiseError(null, SYSTEM_COMMAND_COMMAND_PLACEMENT, null, E_USER_WARNING, null, 'System_Command_Error', true);
- }
-
- $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;
- }
-
-
-
- function pushOperator($in_operator)
- {
- if ($this->_initError) {
- return $this->_initError;
- }
- $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;
- }
-
-
-
- function execute()
- {
- if ($this->_initError) {
- return $this->_initError;
- }
-
- 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);
- }
-
- 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 (empty($this->options['OUTPUT'])) {
- $this->systemCommand .= ' >/dev/null';
- }
-
- $suffix = '';
-
- if (!empty($this->options['NOHUP'])) {
- $this->systemCommand = $this->options['NOHUP'] . $this->systemCommand;
- }
-
- elseif (!empty($this->options['BACKGROUND'])) {
- $suffix = ' &';
- }
-
-
- if (!empty($this->options['SHUTDOWN'])) {
- $line = "system(\"{$this->systemCommand}$suffix\");";
- $function = create_function('', $line);
- register_shutdown_function($function);
- return true;
- }
- else {
-
- $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) {
-
- $return = PEAR::raiseError(null, SYSTEM_COMMAND_NONZERO_EXIT, null, E_USER_WARNING, null, 'System_Command_Error', true);
- }
- else if (!$this->options['STDERR']) {
-
- $return = implode("\n", $result);
- }
- else {
-
- clearstatcache();
- if (filesize($tmpFile) > 0) {
-
- $stderr_output = file_get_contents($tmpFile);
- $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, $stderr_output, 'System_Command_Error', true);
- } else {
-
- $return = implode("\n", $result);
- }
- }
- unlink($tmpFile);
- return $return;
- }
- }
-
-
-
- function which($in_cmd)
- {
-
- if (!is_string($in_cmd) || '' === $in_cmd) {
- return(false);
- }
-
- return System::which($in_cmd, false);
- }
-
-
-
- function reset()
- {
- $this->previousElement = null;
- $this->systemCommand = null;
- $this->commandStatus = 0;
- }
-
-
-
- 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',
- SYSTEM_COMMAND_NONZERO_EXIT => 'non-zero exit value from command',
- );
- }
- if (System_Command::isError($in_value)) {
- $in_value = $in_value->getCode();
- }
- return isset($errorMessages[$in_value]) ? $errorMessages[$in_value] : $errorMessages[SYSTEM_COMMAND_ERROR];
- }
-
-
-
- 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 extends PEAR_Error
- {
-
-
- var $error_message_prefix = 'System_Command Error: ';
-
-
- 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);
- }
- }
-
-
- }
- ?>
|