| 1 |
<?php |
| 2 |
namespace ExecWithFallback; |
| 3 |
|
| 4 |
/** |
| 5 |
* Execute command with exec(), open_proc() or whatever available |
| 6 |
* |
| 7 |
* @package ExecWithFallback |
| 8 |
* @author Bjørn Rosell <[email protected]> |
| 9 |
*/ |
| 10 |
class ExecWithFallback |
| 11 |
{ |
| 12 |
|
| 13 |
protected static $methods = ['exec', 'passthru', 'popen', 'proc_open', 'shell_exec']; |
| 14 |
|
| 15 |
/** |
| 16 |
* Check if any of the methods are available on the system. |
| 17 |
* |
| 18 |
* @param boolean $needResultCode Whether the code using this library is going to supply $result_code to the exec |
| 19 |
* call. This matters because shell_exec is only available when not. |
| 20 |
*/ |
| 21 |
public static function anyAvailable($needResultCode = true) |
| 22 |
{ |
| 23 |
return Availability::anyAvailable($needResultCode); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check if a function is enabled (function_exists as well as ini is tested) |
| 28 |
* |
| 29 |
* @param string $functionName The name of the function |
| 30 |
* |
| 31 |
* @return boolean If the function is enabled |
| 32 |
*/ |
| 33 |
public static function functionEnabled($functionName) |
| 34 |
{ |
| 35 |
if (!function_exists($functionName)) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
if (function_exists('ini_get')) { |
| 39 |
if (ini_get('safe_mode')) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
$d = ini_get('disable_functions') . ',' . ini_get('suhosin.executor.func.blacklist'); |
| 43 |
if ($d === false) { |
| 44 |
$d = ''; |
| 45 |
} |
| 46 |
$d = preg_replace('/,\s*/', ',', $d); |
| 47 |
if (strpos(',' . $d . ',', ',' . $functionName . ',') !== false) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
return is_callable($functionName); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Execute. - A substitute for exec() |
| 57 |
* |
| 58 |
* Same signature and results as exec(): https://www.php.net/manual/en/function.exec.php |
| 59 |
* In case neither exec(), nor emulations are available, it throws an Exception. |
| 60 |
* This is more gentle than real exec(), which on some systems throws a FATAL when exec() is disabled |
| 61 |
* If you want the more acurate substitute, which might halt execution, use execNoMercy() instead. |
| 62 |
* |
| 63 |
* @param string $command The command to execute |
| 64 |
* @param string &$output (optional) |
| 65 |
* @param int &$result_code (optional) |
| 66 |
* |
| 67 |
* @return string | false The last line of output or false in case of failure |
| 68 |
* @throws \Exception If no methods are available |
| 69 |
*/ |
| 70 |
public static function exec($command, &$output = null, &$result_code = null) |
| 71 |
{ |
| 72 |
foreach (self::$methods as $method) { |
| 73 |
if (self::functionEnabled($method)) { |
| 74 |
if (func_num_args() >= 3) { |
| 75 |
if ($method == 'shell_exec') { |
| 76 |
continue; |
| 77 |
} |
| 78 |
$result = self::runExec($method, $command, $output, $result_code); |
| 79 |
} else { |
| 80 |
$result = self::runExec($method, $command, $output); |
| 81 |
} |
| 82 |
if ($result !== false) { |
| 83 |
return $result; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
if (isset($result) && ($result === false)) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
throw new \Exception('exec() is not available'); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Execute. - A substitute for exec(), with exact same errors thrown if exec() is missing. |
| 95 |
* |
| 96 |
* Danger: On some systems, this results in a fatal (non-catchable) error. |
| 97 |
*/ |
| 98 |
public static function execNoMercy($command, &$output = null, &$result_code = null) |
| 99 |
{ |
| 100 |
if (func_num_args() == 3) { |
| 101 |
return ExecWithFallbackNoMercy::exec($command, $output, $result_code); |
| 102 |
} else { |
| 103 |
return ExecWithFallbackNoMercy::exec($command, $output); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static function runExec($method, $command, &$output = null, &$result_code = null) |
| 108 |
{ |
| 109 |
switch ($method) { |
| 110 |
case 'exec': |
| 111 |
return exec($command, $output, $result_code); |
| 112 |
case 'passthru': |
| 113 |
return Passthru::exec($command, $output, $result_code); |
| 114 |
case 'popen': |
| 115 |
return POpen::exec($command, $output, $result_code); |
| 116 |
case 'proc_open': |
| 117 |
return ProcOpen::exec($command, $output, $result_code); |
| 118 |
case 'shell_exec': |
| 119 |
if (func_num_args() == 4) { |
| 120 |
return ShellExec::exec($command, $output, $result_code); |
| 121 |
} else { |
| 122 |
return ShellExec::exec($command, $output); |
| 123 |
} |
| 124 |
} |
| 125 |
return false; |
| 126 |
} |
| 127 |
} |
| 128 |
|