c88b0d69f8d7b49a72ea4b0214d9bb79ab4f3140
[pharext/pharext] / src / pharext / Installer.php
1 <?php
2
3 namespace pharext;
4
5 use Phar;
6 use pharext\Cli\Args as CliArgs;
7 use pharext\Cli\Command as CliCommand;
8
9 /**
10 * The extension install command executed by the extension phar
11 */
12 class Installer implements Command
13 {
14 use CliCommand;
15
16 /**
17 * The temporary directory we should operate in
18 * @var string
19 */
20 private $tmp;
21
22 /**
23 * The directory we came from
24 * @var string
25 */
26 private $cwd;
27
28 /**
29 * Create the command
30 */
31 public function __construct() {
32 $this->args = new CliArgs([
33 ["h", "help", "Display help",
34 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
35 ["v", "verbose", "More output",
36 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
37 ["q", "quiet", "Less output",
38 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
39 ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
40 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
41 ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
42 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
43 "php"],
44 ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
45 CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
46 ["s", "sudo", "Installation might need increased privileges",
47 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
48 "sudo -S %s"],
49 ["i", "ini", "Activate in this php.ini instead of loaded default php.ini",
50 CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
51 ]);
52 }
53
54 /**
55 * Cleanup temp directory
56 */
57 public function __destruct() {
58 $this->cleanup();
59 }
60
61 /**
62 * @inheritdoc
63 * @see \pharext\Command::run()
64 */
65 public function run($argc, array $argv) {
66 $this->cwd = getcwd();
67 $this->tmp = $this->tempname(basename(Phar::running(false)));
68
69 $phar = new Phar(Phar::running(false));
70 foreach ($phar as $entry) {
71 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
72 $temp = $this->newtemp($entry->getBaseName());
73 $phar->extractTo($temp, $entry->getFilename(), true);
74 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
75 }
76 }
77 $phars[$this->tmp] = $phar;
78
79 foreach ($phars as $phar) {
80 if (isset($phar["pharext_install.php"])) {
81 $callable = include $phar["pharext_install.php"];
82 if (is_callable($callable)) {
83 $recv[] = $callable($this);
84 }
85 }
86 }
87
88 $errs = [];
89 $prog = array_shift($argv);
90 foreach ($this->args->parse(--$argc, $argv) as $error) {
91 $errs[] = $error;
92 }
93
94 if ($this->args["help"]) {
95 $this->header();
96 $this->help($prog);
97 exit;
98 }
99
100 foreach ($this->args->validate() as $error) {
101 $errs[] = $error;
102 }
103
104 if ($errs) {
105 if (!$this->args["quiet"]) {
106 $this->header();
107 }
108 foreach ($errs as $err) {
109 $this->error("%s\n", $err);
110 }
111 if (!$this->args["quiet"]) {
112 $this->help($prog);
113 }
114 exit(1);
115 }
116
117 if (isset($recv)) {
118 foreach ($recv as $r) {
119 $r($this);
120 }
121 }
122 foreach ($phars as $temp => $phar) {
123 $this->installPackage($phar, $temp);
124 }
125 }
126
127 /**
128 * Prepares, configures, builds and installs the extension
129 */
130 private function installPackage(Phar $phar, $temp) {
131 $this->info("Installing %s ... \n", basename($phar->getAlias()));
132 try {
133 $phar->extractTo($temp, null, true);
134 } catch (\Exception $e) {
135 $this->error("%s\n", $e->getMessage());
136 exit(3);
137 }
138
139 if (!chdir($temp)) {
140 $this->error(null);
141 exit(4);
142 }
143
144 // phpize
145 $this->exec("phpize", $this->php("ize"));
146
147 // configure
148 $args = ["--with-php-config=". $this->php("-config")];
149 if ($this->args->configure) {
150 $args = array_merge($args, $this->args->configure);
151 }
152 $this->exec("configure", "./configure", $args);
153
154 // make
155 if ($this->args->verbose) {
156 $this->exec("make", "make", ["-j3"]);
157 } else {
158 $this->exec("make", "make", ["-j3", "-s"]);
159 }
160
161 // install
162 if ($this->args->verbose) {
163 $this->exec("install", "make", ["install"], true);
164 } else {
165 $this->exec("install", "make", ["install", "-s"], true);
166 }
167
168 // activate
169 $this->activate();
170
171 // cleanup
172 $this->cleanup($temp);
173 }
174
175 /**
176 * Perform any cleanups
177 */
178 private function cleanup($temp = null) {
179 if (!isset($temp)) {
180 $temp = $this->tmp;
181 }
182 if (is_dir($temp)) {
183 chdir($this->cwd);
184 $this->info("Cleaning up %s ...\n", $temp);
185 $this->rm($temp);
186 }
187 }
188
189 /**
190 * Execute a program with escalated privileges handling interactive password prompt
191 * @param string $command
192 * @param string $output
193 * @return int
194 */
195 private function sudo($command, &$output) {
196 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
197 return -1;
198 }
199 $stdout = $pipes[1];
200 $passwd = 0;
201 while (!feof($stdout)) {
202 $R = [$stdout]; $W = []; $E = [];
203 if (!stream_select($R, $W, $E, null)) {
204 continue;
205 }
206 $data = fread($stdout, 0x1000);
207 /* only check a few times */
208 if ($passwd++ < 10) {
209 if (stristr($data, "password")) {
210 printf("\n%s", $data);
211 }
212 }
213 $output .= $data;
214 }
215 return proc_close($proc);
216 }
217 /**
218 * Execute a system command
219 * @param string $name pretty name
220 * @param string $command command
221 * @param array $args command arguments
222 * @param bool $sudo whether the command may need escalated privileges
223 */
224 private function exec($name, $command, array $args = null, $sudo = false) {
225 $exec = escapeshellcmd($command);
226 if ($args) {
227 $exec .= " ". implode(" ", array_map("escapeshellarg", (array) $args));
228 }
229
230 if ($this->args->verbose) {
231 $this->info("Running %s ...\n", $exec);
232 } else {
233 $this->info("Running %s ... ", $name);
234 }
235
236 if ($sudo && isset($this->args->sudo)) {
237 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $exec), $output);
238 } elseif ($this->args->verbose) {
239 passthru($exec ." 2>&1", $retval);
240 } else {
241 exec($exec ." 2>&1", $output, $retval);
242 $output = implode("\n", $output);
243 }
244
245 if ($retval) {
246 $this->error("Command %s failed with (%s)\n", $command, $retval);
247 if (isset($output) && !$this->args->quiet) {
248 printf("%s\n", $output);
249 }
250 exit(2);
251 }
252 if (!$this->args->verbose) {
253 // we already have a bunch of output
254 $this->info("OK\n");
255 }
256 }
257
258 /**
259 * Construct a command from prefix common-name and suffix
260 * @param type $suffix
261 * @return string
262 */
263 private function php($suffix) {
264 $cmd = $this->args["common-name"] . $suffix;
265 if (isset($this->args->prefix)) {
266 $cmd = $this->args->prefix . "/bin/" . $cmd;
267 }
268 return $cmd;
269 }
270
271 private function activate() {
272 if ($this->args->ini) {
273 $files = [realpath($this->args->ini)];
274 } else {
275 $files = array_filter(array_map("trim", explode(",", php_ini_scanned_files())));
276 $files[] = php_ini_loaded_file();
277 }
278
279 $extension = basename(current(glob("modules/*.so")));
280 $pattern = preg_quote($extension);
281
282 foreach ($files as $index => $file) {
283 $temp = new Tempfile("phpini");
284 foreach (file($file) as $line) {
285 if (preg_match("/^\s*extension\s*=\s*[\"']?{$pattern}[\"']?\s*(;.*)?\$/", $line)) {
286 // already there
287 $this->info("Extension already activated\n");
288 return;
289 }
290 fwrite($temp->getStream(), $line);
291 }
292 }
293
294 // not found, add extension line to the last process file
295 if (isset($temp, $file)) {
296 fprintf($temp->getStream(), "extension=%s\n", $extension);
297 $temp->closeStream();
298
299 $path = $temp->getPathname();
300 $stat = stat($file);
301
302 $ugid = sprintf("%d:%d", $stat["uid"], $stat["gid"]);
303 $this->exec("INI owner transfer", "chown", [$ugid, $path], true);
304
305 $perm = decoct($stat["mode"] & 0777);
306 $this->exec("INI permission transfer", "chmod", [$perm, $path], true);
307
308 $this->exec("INI activation", "mv", [$path, $file], true);
309 }
310 }
311 }