HosipLan

Untitled

Mar 24th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. /**
  2.  * @author Filip Procházka <[email protected]>
  3.  */
  4. class Process
  5. {
  6.     /**
  7.      * @return string
  8.      * @throws ProcessException
  9.      */
  10.     public static function execute($cmd)
  11.     {
  12.         $output = NULL;
  13.         $spec = array(
  14.             0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  15.             1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  16.             2 => array("pipe", "w"), // errors
  17.         );
  18.  
  19.         if (is_resource($process = proc_open($cmd, $spec, $pipes))) {
  20.             fclose($pipes[0]);
  21.             stream_set_blocking($pipes[1], 1);
  22.             $output = stream_get_contents($pipes[1]);
  23.             fclose($pipes[1]);
  24.             @fclose($pipes[2]);
  25.  
  26.             if (0 !== proc_close($process)) {
  27.                 throw new ProcessException("Error occured while executing: `$cmd`\n\n" . $output);
  28.             }
  29.  
  30.         } else {
  31.             throw new ProcessException("Could not execute: `$cmd`");
  32.         }
  33.  
  34.         return $output;
  35.     }
  36.  
  37. }
  38.  
  39.  
  40.  
  41. /**
  42.  * @author Filip Procházka <[email protected]>
  43.  */
  44. class ProcessException extends \RuntimeException
  45. {
  46.  
  47. }
  48.  
  49.  
  50. $output = Process::execute('plistgenerator.py --url ' . escapeshellarg($url) . ' --path ' . escapeshellarg($_GET['plist']));
Advertisement
Add Comment
Please, Sign In to add comment