openssl signing
[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 $this->cwd = getcwd();
63 $this->tmp = $this->tempname(basename(Phar::running(false)));
64
65 $phar = new Phar(Phar::running(false));
66 foreach ($phar as $entry) {
67 if (fnmatch("*.ext.phar*", $entry->getBaseName())) {
68 $temp = $this->newtemp($entry->getBaseName());
69 $phar->extractTo($temp, $entry->getFilename(), true);
70 $phars[$temp] = new Phar($temp."/".$entry->getFilename());
71 }
72 }
73 $phars[$this->tmp] = $phar;
74
75 foreach ($phars as $phar) {
76 if (isset($phar["pharext_install.php"])) {
77 $callable = include $phar["pharext_install.php"];
78 if (is_callable($callable)) {
79 $recv[] = $callable($this);
80 }
81 }
82 }
83
84 $errs = [];
85 $prog = array_shift($argv);
86 foreach ($this->args->parse(--$argc, $argv) as $error) {
87 $errs[] = $error;
88 }
89
90 if ($this->args["help"]) {
91 $this->header();
92 $this->help($prog);
93 exit;
94 }
95
96 foreach ($this->args->validate() as $error) {
97 $errs[] = $error;
98 }
99
100 if ($errs) {
101 if (!$this->args["quiet"]) {
102 $this->header();
103 }
104 foreach ($errs as $err) {
105 $this->error("%s\n", $err);
106 }
107 if (!$this->args["quiet"]) {
108 $this->help($prog);
109 }
110 exit(1);
111 }
112
113 if (isset($recv)) {
114 foreach ($recv as $r) {
115 $r($this);
116 }
117 }
118 foreach ($phars as $temp => $phar) {
119 $this->installPackage($phar, $temp);
120 }
121 }
122
123 /**
124 * Prepares, configures, builds and installs the extension
125 */
126 private function installPackage(Phar $phar, $temp) {
127 $this->info("Installing %s ... \n", basename($phar->getAlias()));
128 try {
129 $phar->extractTo($temp, null, true);
130 } catch (\Exception $e) {
131 $this->error("%s\n", $e->getMessage());
132 exit(3);
133 }
134
135 if (!chdir($temp)) {
136 $this->error(null);
137 exit(4);
138 }
139
140 $this->exec("phpize", $this->php("ize"));
141 $this->exec("configure", "./configure --with-php-config=". $this->php("-config") . " ".
142 implode(" ", (array) $this->args->configure));
143 $this->exec("make", $this->args->verbose ? "make -j3" : "make -sj3");
144 $this->exec("install", $this->args->verbose ? "make install" : "make -s install", true);
145
146 $this->cleanup($temp);
147
148 $this->info("Don't forget to activiate the extension in your php.ini!\n\n");
149 }
150
151 /**
152 * Perform any cleanups
153 */
154 private function cleanup($temp = null) {
155 if (!isset($temp)) {
156 $temp = $this->tmp;
157 }
158 if (is_dir($temp)) {
159 chdir($this->cwd);
160 $this->info("Cleaning up %s ...\n", $temp);
161 $this->rm($temp);
162 }
163 }
164
165 /**
166 * Execute a program with escalated privileges handling interactive password prompt
167 * @param string $command
168 * @param string $output
169 * @return int
170 */
171 private function sudo($command, &$output) {
172 if (!($proc = proc_open($command, [STDIN,["pipe","w"],["pipe","w"]], $pipes))) {
173 return -1;
174 }
175 $stdout = $pipes[1];
176 $passwd = 0;
177 while (!feof($stdout)) {
178 $R = [$stdout]; $W = []; $E = [];
179 if (!stream_select($R, $W, $E, null)) {
180 continue;
181 }
182 $data = fread($stdout, 0x1000);
183 /* only check a few times */
184 if ($passwd++ < 10) {
185 if (stristr($data, "password")) {
186 printf("\n%s", $data);
187 }
188 }
189 $output .= $data;
190 }
191 return proc_close($proc);
192 }
193 /**
194 * Execute a system command
195 * @param string $name pretty name
196 * @param string $command full command
197 * @param bool $sudo whether the command may need escalated privileges
198 */
199 private function exec($name, $command, $sudo = false) {
200 $this->info("Running %s ...%s", $this->args->verbose ? $command : $name, $this->args->verbose ? "\n" : " ");
201 if ($sudo && isset($this->args->sudo)) {
202 $retval = $this->sudo(sprintf($this->args->sudo." 2>&1", $command), $output);
203 } elseif ($this->args->verbose) {
204 passthru($command ." 2>&1", $retval);
205 } else {
206 exec($command ." 2>&1", $output, $retval);
207 $output = implode("\n", $output);
208 }
209 if ($retval) {
210 $this->error("Command %s failed with (%s)\n", $command, $retval);
211 if (isset($output) && !$this->args->quiet) {
212 printf("%s\n", $output);
213 }
214 exit(2);
215 }
216 $this->info("OK\n");
217 }
218
219 /**
220 * Construct a command from prefix common-name and suffix
221 * @param type $suffix
222 * @return string
223 */
224 private function php($suffix) {
225 $cmd = $this->args["common-name"] . $suffix;
226 if (isset($this->args->prefix)) {
227 $cmd = $this->args->prefix . "/bin/" . $cmd;
228 }
229 return $cmd;
230 }
231 }