Spaces:
Sleeping
Sleeping
File size: 8,663 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | <?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | 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> |
// +----------------------------------------------------------------------+
//
define('CMD_RCSID', '$Id: CMD.php,v 1.5 2002/12/31 16:18:22 sebastian Exp $');
/**
* The Cmd:: 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>
* @version $Revision: 1.5 $
**/
require_once 'PEAR.php';
class Cmd extends PEAR
{
var $arrSetting = array();
var $arrConstant = array();
var $arrCommand = array();
/**
* Class constructor
*
* Defines all necessary constants and sets defaults
*
* @author Anders Johannsen <anders@johannsen.com>
*
* @access public
*
**/
function Cmd ()
{
// Defining constants
$this->arrConstant = array ("CMD_SEQUENCE",
"CMD_SHUTDOWN",
"CMD_SHELL",
"CMD_OUTPUT",
"CMD_NOHUP",
"CMD_VERBOSE"
);
foreach ($this->arrConstant as $key => $value) {
if (!defined($value)) {
define($value, $key);
}
}
// Setting default values
$this->arrSetting[CMD_SEQUENCE] = true;
$this->arrSetting[CMD_SHUTDOWN] = false;
$this->arrSetting[CMD_OUTPUT] = false;
$this->arrSetting[CMD_NOHUP] = false;
$this->arrSetting[CMD_VERBOSE] = false;
$arrShell = array ("sh", "bash", "zsh", "tcsh", "csh", "ash", "sash", "esh", "ksh");
foreach ($arrShell as $shell) {
if ($this->arrSetting[CMD_SHELL] = $this->which($shell)) {
break;
}
}
if (empty($this->arrSetting[CMD_SHELL])) {
$this->raiseError("No shell found");
}
}
/**
* Sets any option
*
* The options are currently:
* CMD_SHUTDOWN : Execute commands via a shutdown function
* CMD_SHELL : Path to shell
* CMD_OUTPUT : Output stdout from process
* CMD_NOHUP : Use nohup to detach process
* CMD_VERBOSE : Print errors to stdout
*
* @param $option is a constant, which corresponds to the
* option that should be changed
*
* @param $setting is the value of the option currently
* being toggled.
*
* @return bool true if succes, else false
*
* @access public
*
* @author Anders Johannsen <anders@johannsen.com>
*
**/
function setOption ($option, $setting)
{
if (empty($this->arrConstant[$option])) {
$this->raiseError("No such option: $option");
return false;
}
switch ($option) {
case CMD_OUTPUT:
case CMD_SHUTDOWN:
case CMD_VERBOSE:
case CMD_SEQUENCE:
$this->arrSetting[$option] = $setting;
return true;
break;
case CMD_SHELL:
if (is_executable($setting)) {
$this->arrSetting[$option] = $setting;
return true;
} else {
$this->raiseError("No such shell: $setting");
return false;
}
break;
case CMD_NOHUP:
if (empty($setting)) {
$this->arrSetting[$option] = false;
} else if ($location = $this->which("nohup")) {
$this->arrSetting[$option] = true;
} else {
$this->raiseError("Nohup was not found on your system");
return false;
}
break;
}
}
/**
* Add command for execution
*
* @param $command accepts both arrays and regular strings
*
* @return bool true if succes, else false
*
* @access public
*
* @author Anders Johannsen <anders@johannsen.com>
*
**/
function command($command)
{
if (is_array($command)) {
foreach ($command as $key => $value) {
$this->arrCommand[] = $value;
}
return true;
} else if (is_string($command)) {
$this->arrCommand[] = $command;
return true;
}
$this->raiseError("Argument not valid");
return false;
}
/**
* Executes the code according to given options
*
* @return bool true if succes, else false
*
* @access public
*
* @author Anders Johannsen <anders@johannsen.com>
*
**/
function exec()
{
// Warning about impossible mix of options
if (!empty($this->arrSetting[CMD_OUTPUT])) {
if (!empty($this->arrSetting[CMD_SHUTDOWN]) || !empty($this->arrSetting[CMD_NOHUP])) {
$this->raiseError("Error: Commands executed via shutdown functions or nohup cannot return output");
return false;
}
}
// Building command
$strCommand = implode(";", $this->arrCommand);
$strExec = "echo '$strCommand' | ".$this->arrSetting[CMD_SHELL];
if (empty($this->arrSetting[CMD_OUTPUT])) {
$strExec = $strExec . ' > /dev/null';
}
if (!empty($this->arrSetting[CMD_NOHUP])) {
$strExec = 'nohup ' . $strExec;
}
// Executing
if (!empty($this->arrSetting[CMD_SHUTDOWN])) {
$line = "system(\"$strExec\");";
$function = create_function('', $line);
register_shutdown_function($function);
return true;
} else {
return `$strExec`;
}
}
/**
* Errorhandler. If option CMD_VERBOSE is true,
* the error is printed to stdout, otherwise it
* is avaliable in lastError
*
* @return bool always returns true
*
* @access private
*
* @author Anders Johannsen <anders@johannsen.com>
**/
function raiseError($strError)
{
if (!empty($this->arrSetting[CMD_VERBOSE])) {
echo $strError;
} else {
$this->lastError = $strError;
}
return true;
}
/**
* Functionality similiar to unix 'which'. Searches the path
* for the specified program.
*
* @param $cmd name of the executable to search for
*
* @return string returns the full path if found,
* false if not
*
* @access private
*
* @author Anders Johannsen <anders@johannsen.com>
**/
function which($cmd)
{
global $HTTP_ENV_VARS;
$arrPath = explode(":", $HTTP_ENV_VARS['PATH']);
foreach ($arrPath as $path) {
$location = $path . "/" . $cmd;
if (is_executable($location)) {
return $location;
}
}
return false;
}
}
?>
|