diff options
author | Michael Tänzer <neo@nhng.de> | 2011-10-21 20:20:27 +0200 |
---|---|---|
committer | Michael Tänzer <neo@nhng.de> | 2011-10-21 20:20:27 +0200 |
commit | 8d2e661d78cc1fb095e3a37d80cb4e0d37ac1e9e (patch) | |
tree | b59f918e159fc6fae64d735e20530eea352f3d5d /includes/lib/general.php | |
parent | 99d0ec582fb1f76479424c0300005284c58e67b0 (diff) | |
download | cacert-devel-8d2e661d78cc1fb095e3a37d80cb4e0d37ac1e9e.tar.gz cacert-devel-8d2e661d78cc1fb095e3a37d80cb4e0d37ac1e9e.tar.xz cacert-devel-8d2e661d78cc1fb095e3a37d80cb4e0d37ac1e9e.zip |
bug 978: New helper function runCommand() -> reduce boilerplate code
Signed-off-by: Michael Tänzer <neo@nhng.de>
Diffstat (limited to 'includes/lib/general.php')
-rw-r--r-- | includes/lib/general.php | 80 |
1 files changed, 71 insertions, 9 deletions
diff --git a/includes/lib/general.php b/includes/lib/general.php index 6cfbd10..d91b24e 100644 --- a/includes/lib/general.php +++ b/includes/lib/general.php @@ -48,15 +48,15 @@ function get_user_id_from_cert($serial, $issuer_cn) } /** -* Produces a log entry with the error message with log level E_USER_WARN -* and a random ID an returns a message that can be displayed to the user -* including the generated ID -* -* @param $errormessage string -* The error message that should be logged -* @return string containing the generated ID that can be displayed to the -* user -*/ + * Produces a log entry with the error message with log level E_USER_WARN + * and a random ID an returns a message that can be displayed to the user + * including the generated ID + * + * @param $errormessage string + * The error message that should be logged + * @return string containing the generated ID that can be displayed to the + * user + */ function failWithId($errormessage) { $errorId = rand(); trigger_error("$errormessage. ID: $errorId", E_USER_WARNING); @@ -68,3 +68,65 @@ function failWithId($errormessage) { $errorId); } + +/** + * Runs a command on the shell and return it's exit code and output + * + * @param string $command + * The command to run. Make sure that you escapeshellarg() any non-constant + * parts as this is executed on a shell! + * @param string|bool $input + * The input that is passed to the command via STDIN, if true the real + * STDIN is passed through + * @param string|bool $output + * The output the command wrote to STDOUT (this is passed as reference), + * if true the output will be written to the real STDOUT. Output is ignored + * by default + * @param string|bool $errors + * The output the command wrote to STDERR (this is passed as reference), + * if true (default) the output will be written to the real STDERR + * + * @return int|bool + * The exit code of the command, true if the execution of the command + * failed (true because then + * <code>if (runCommand('echo "foo"')) handle_error();</code> will work) + */ +function runCommand($command, $input = "", &$output = null, &$errors = true) { + $descriptorspec = array(); + + if ($input !== true) { + $descriptorspec[0] = array("pipe", "r"); // STDIN for child + } + + if ($output !== true) { + $descriptorspec[1] = array("pipe", "w"); // STDOUT for child + } + + if ($errors !== true) { + $descriptorspec[2] = array("pipe", "w"); // STDERR for child + } + + $proc = proc_open($command, $descriptorspec, $pipes); + + if (is_resource($proc)) + { + if ($input !== true) { + fwrite($pipes[0], $input); + fclose($pipes[0]); + } + + if ($output !== true) { + $output = stream_get_contents($pipes[1]); + } + + if ($errors !== true) { + $errors = stream_get_contents($pipes[2]); + } + + return proc_close($proc); + + } else { + return true; + } +} + |