| 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 ExecWithFallbackNoMercy |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* Execute. - A substitute for exec() |
| 15 |
* |
| 16 |
* Same signature and results as exec(): https://www.php.net/manual/en/function.exec.php |
| 17 |
* |
| 18 |
* This is our hardcore version of our exec(). It does not merely throw an Exception, if |
| 19 |
* no methods are available. It calls exec(). |
| 20 |
* This ensures exactly same behavior as normal exec() - the same error is thrown. |
| 21 |
* You might want that. But do you really? |
| 22 |
* DANGER: On some systems, calling a disabled exec() results in a fatal (non-catchable) error. |
| 23 |
* |
| 24 |
* @param string $command The command to execute |
| 25 |
* @param string &$output (optional) |
| 26 |
* @param int &$result_code (optional) |
| 27 |
* |
| 28 |
* @return string | false The last line of output or false in case of failure |
| 29 |
* @throws \Exception|\Error If no methods are available. Note: On some systems, it is FATAL! |
| 30 |
*/ |
| 31 |
public static function exec($command, &$output = null, &$result_code = null) |
| 32 |
{ |
| 33 |
foreach (self::$methods as $method) { |
| 34 |
if (self::functionEnabled($method)) { |
| 35 |
if (func_num_args() >= 3) { |
| 36 |
if ($method == 'shell_exec') { |
| 37 |
continue; |
| 38 |
} |
| 39 |
$result = self::runExec($method, $command, $output, $result_code); |
| 40 |
} else { |
| 41 |
$result = self::runExec($method, $command, $output); |
| 42 |
} |
| 43 |
if ($result !== false) { |
| 44 |
return $result; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
if (isset($result) && ($result === false)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
// MIGHT THROW FATAL! |
| 52 |
return exec($command, $output, $result_code); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
} |
| 57 |
|