better handling of sudo output
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6
7 /**
8 * The extension install command executed by the extension phar
9 */
10 class Installer implements Command
11 {
12 use CliCommand;
13
14 /**
15 * The temporary directory we should operate in
16 * @var string
17 */
18 private $tmp;
19
20 /**
21 * The directory we came from
22 * @var string
23 */
24 private $cwd;
25
26 /**
27 * Create the command
28 */
29 public function __construct() {
30 $this->args = new CliArgs([
31 ["h", "help", "Display help",
32 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
33 ["v", "verbose", "More output",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
35 ["q", "quiet", "Less output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
39 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
41 "php"],
42 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
43 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
44 ["s", "sudo", "Installation might need increased privileges",
45 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
46 "sudo -S %s"]
47 ]);
48 }
49
50 /**
51 * Cleanup temp directory
52 */
53 public function __destruct() {
54 $this->cleanup();
55 }
56
57 /**
58 * @inheritdoc
59 * @see \pharext\Command::run()
60 */
61 public function run($argc, array $argv) {
62 if (($hook = stream_resolve_include_path("pharext_install.php"))) {
63 $callable = include $hook;
64 if (is_callable($callable)) {
65 $recv = $callable($this);
66 }
67 }
68
69 $errs = [];
70 $prog = array_shift($argv);
71 foreach ($this->args->parse(--$argc, $argv) as $error) {
72 $errs[] = $error;
73 }
74
75 if ($this->args["help"]) {
76 $this->header();
77 $this->help($prog);
78 exit;
79 }
80
81 foreach ($this->args->validate() as $error) {
82 $errs[] = $error;
83 }
84
85 if ($errs) {
86 if (!$this->args["quiet"]) {
87 $this->header();
88 }
89 foreach ($errs as $err) {
90 $this->error("%s\n", $err);
91 }
92 if (!$this->args["quiet"]) {
93 $this->help($prog);
94 }
95 exit(1);
96 }
97
98 if (isset($recv)) {
99 $recv($this);
100 }
101
102 $this->installPackage();
103 }
104
105 /**
106 * Prepares, configures, builds and installs the extension
107 */
108 private function installPackage() {
109 $this->extract();
110
111 if (!chdir($this->tmp)) {
112 $this->error(null);
113 exit(4);
114 }
115
116 $this->exec("phpize", $this->php("ize"));
117 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
118 implode(" ", (array) $this->args->configure));
119 $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
120 $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
121
122 $this->cleanup();
123
124 $this->info("\nDon't forget to activiate the extension in your php.ini!\n");
125 }
126
127 /**
128 * Perform any cleanups
129 */
130 private function cleanup() {
131 if (is_dir($this->tmp)) {
132 chdir($this->cwd);
133 $this->info("Cleaning up %s ...\n", $this->tmp);
134 $this->rm($this->tmp);
135 }
136 }
137
138 /**
139 * Extract the phar to a temporary directory
140 */
141 private function extract() {
142 if (!$file = Phar::running(false)) {
143 $this->error("Did your run the ext.phar?\n");
144 exit(3);
145 }
146
147 $temp = $this->tempname(basename($file));
148 if (!is_dir($temp)) {
149 if (!mkdir($temp, 0750, true)) {
150 $this->error(null);
151 exit(3);
152 }
153 }
154 $this->tmp = $temp;
155 $this->cwd = getcwd();
156
157 try {
158 $phar = new Phar($file);
159 $phar->extractTo($temp, null, true);
160 } catch (\Exception $e) {
161 $this->error("%s\n", $e->getMessage());
162 exit(3);
163 }
164 }
165
166 /**
167 * rm -r
168 * @param string $dir
169 */
170 private function rm($dir) {
171 foreach (scandir($dir) as $entry) {
172 if ($entry === "." || $entry === "..") {
173 continue;
174 } elseif (is_dir("$dir/$entry")) {
175 $this->rm("$dir/$entry");
176 } elseif (!unlink("$dir/$entry")) {
177 $this->error(null);
178 }
179 }
180 if (!rmdir($dir)) {
181 $this->error(null);
182 }
183 }
184
185 /**
186 * Execute a program with escalated privileges handling interactive password prompt
187 * @param string $command
188 * @param string $output
189 * @return int
190 */
191 private function sudo($command, &$output) {
192 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
193 return -1;
194 }
195 $stdout = $pipes[1];
196 $passwd = 0;
197 while (!feof($stdout)) {
198 $R = [$stdout]; $W = []; $E = [];
199 if (!stream_select($R, $W, $E, null)) {
200 continue;
201 }
202 $data = fread($stdout, 0x1000);
203 /* only check a few times */
204 if ($passwd++ < 10) {
205 if (stristr($data, "password")) {
206 printf("\n%s", $data);
207 }
208 }
209 $output .= $data;
210 }
211 return proc_close($proc);
212 }
213 /**
214 * Execute a system command
215 * @param string $name pretty name
216 * @param string $command full command
217 * @param bool $sudo whether the command may need escalated privileges
218 */
219 private function exec($name, $command, $sudo = false) {
220 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
221 if ($sudo && isset($this->args->sudo)) {
222 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
223 } elseif ($this->args->verbose) {
224 passthru($command ." 2>&1", $retval);
225 } else {
226 exec($command ." 2>&1", $output, $retval);
227 $output = implode("\n", $output);
228 }
229 if ($retval) {
230 $this->error("Command %s failed with (%s)\n", $command, $retval);
231 if (isset($output) && !$this->args->quiet) {
232 printf("%s\n", $output);
233 }
234 exit(2);
235 }
236 $this->info("OK\n");
237 }
238
239 /**
240 * Construct a command from prefix common-name and suffix
241 * @param type $suffix
242 * @return string
243 */
244 private function php($suffix) {
245 $cmd = $this->args["common-name"] . $suffix;
246 if (isset($this->args->prefix)) {
247 $cmd = $this->args->prefix . "/bin/" . $cmd;
248 }
249 return $cmd;
250 }
251 }