1: <?php
2:
3: class CommandLineUtil {
4:
5: public static function execute($command, $showOutput = false) {
6: $command = self::_sanitizeCommand($command);
7: if ($showOutput) {
8: system($command, $result);
9: } else {
10: exec($command, $lines, $result);
11: }
12: if ($result) {
13: throw new Exception("Command \"$command\" returned error code \"$result\"");
14: }
15: }
16:
17: private static function _sanitizeCommand($command) {
18: if (is_array($command)) {
19: return self::_buildCommandFromArray($command);
20: } else {
21: return escapeshellcmd($command);
22: }
23: }
24:
25: private static function _buildCommandFromArray($args) {
26: $cmd = '';
27: foreach ($args as $arg) {
28: $cmd .= escapeshellarg($arg) . ' ';
29: }
30: return trim($cmd);
31: }
32:
33: }
34: