6 * Execute system command
11 * Sudo command, if the cmd needs escalated privileges
17 * Executable of the cmd
23 * Passthrough cmd output
35 * Return code of cmd run
41 * @param string $command
44 public function __construct($command, $verbose = false) {
45 $this->command
= $command;
46 $this->verbose
= $verbose;
50 * (Re-)set sudo command
53 public function setSu($sudo = false) {
58 * Execute a program with escalated privileges handling interactive password prompt
59 * @param string $command
60 * @param string $output
63 private function suExec($command, &$output, &$status) {
64 if (!($proc = proc_open($command, [STDIN
,["pipe","w"],["pipe","w"]], $pipes))) {
66 throw new Exception("Failed to run {$command}");
70 while (!feof($stdout)) {
71 $R = [$stdout]; $W = []; $E = [];
72 if (!stream_select($R, $W, $E, null)) {
75 $data = fread($stdout, 0x1000);
76 /* only check a few times */
78 if (stristr($data, "password")) {
79 printf("\n%s", $data);
84 $status = proc_close($proc);
90 * @return \pharext\ExecCmd self
91 * @throws \pharext\Exception
93 public function run(array $args = null) {
94 $exec = escapeshellcmd($this->command
);
96 $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
100 $this->suExec(sprintf($this->sudo
." 2>&1", $exec), $this->output
, $this->status
);
101 } elseif ($this->verbose
) {
102 ob_start(function($s) {
106 passthru($exec, $this->status
);
109 exec($exec ." 2>&1", $output, $this->status
);
110 $this->output
= implode("\n", $output);
114 throw new Exception("Command {$exec} failed ({$this->status})");
121 * Retrieve exit code of cmd run
124 public function getStatus() {
125 return $this->status
;
129 * Retrieve output of cmd run
132 public function getOutput() {
133 return $this->output
;