- /**
- * rm -r
- * @param string $dir
- */
- private function rm($dir) {
- foreach (scandir($dir) as $entry) {
- if ($entry === "." || $entry === "..") {
- continue;
- } elseif (is_dir("$dir/$entry")) {
- $this->rm("$dir/$entry");
- } elseif (!unlink("$dir/$entry")) {
- $this->error(null);
- }
- }
- if (!rmdir($dir)) {
- $this->error(null);
- }
- }
-
- /**
- * 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);
- }
- /**
- * Execute a system command
- * @param string $name pretty name
- * @param string $command full command
- * @param bool $sudo whether the command may need escalated privileges
- */
- private function exec($name, $command, $sudo = false) {
- $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
- if ($sudo && isset($this->args->sudo)) {
- $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
- } elseif ($this->args->verbose) {
- passthru($command ." 2>&1", $retval);