+ /**
+ * Execute a program with escalated privileges handling interactive password prompt
+ * @param string $command
+ * @param string $output
+ * @return int
+ */
+ private function sudo($command, &$output) {
+ if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
+ return -1;
+ }
+ $stdout = $pipes[1];
+ $passwd = 0;
+ while (!feof($stdout)) {
+ $R = [$stdout]; $W = []; $E = [];
+ if (!stream_select($R, $W, $E, null)) {
+ continue;
+ }
+ $data = fread($stdout, 0x1000);
+ /* only check a few times */
+ if ($passwd++ < 10) {
+ if (stristr($data, "password")) {
+ printf("\n%s", $data);
+ }
+ }
+ $output .= $data;
+ }
+ return proc_close($proc);
+ }