378edbd81b5838645d4776960bcc2c6b1373e1c3
[yaffs-website] / vendor / drush / drush / includes / startup.inc
1 <?php
2
3 /**
4  * @file
5  *   Functions used when Drush is starting up.
6  *
7  * This file is included and used early in the Drush
8  * startup process, before any other Drush APIs are
9  * available, and before the autoloader has been included.
10  */
11
12 /**
13  * Get the current enviornment.
14  */
15 function drush_env() {
16   // Fetch the current environment.  To ensure that
17   // $_ENV is correctly populated, make sure that
18   // the value of 'variables-order' in your php.ini
19   // contains "E" ("Environment").  See:
20   // http://us.php.net/manual/en/ini.core.php#ini.variables-order
21   $env = $_ENV;
22
23   // If PHP is not configured correctly, $_ENV will be
24   // empty.  Drush counts on the fact that environment
25   // variables will always be available, though, so we
26   // need to repair this situation.  We can always access
27   // individual environmnet values via getenv(); however,
28   // there is no PHP API that will tell us all of the
29   // available values, so we will get the environment
30   // variable values using 'printenv'.
31   if (empty($env)) {
32     exec('printenv', $env_items);
33     foreach ($env_items as $item) {
34       // Each $item is 'key=value' or just 'key'.
35       // If $item has no value, then explode will return
36       // a single array, [0 => 'key'].  We add a default
37       // value of [1 => 'value'] to cover this case.  If
38       // explode returns two items, the default value is ignored.
39       list($key, $value) = explode('=', $item, 2) + array(1 => '');
40       $env[$key] = $value;
41     }
42   }
43
44   return $env;
45 }
46
47 /**
48  * Checks the provided location and return the appropriate
49  * Drush wrapper or Drush launcher script, if found.
50  *
51  * If the provided location looks like it might be a web
52  * root (i.e., it contains an index.php), then we will search
53  * in a number of locations in the general vicinity of the
54  * web root for a Drush executable.
55  *
56  * For other locations, we will look only in that specific
57  * directory, or in vendor/bin.
58  */
59 function find_wrapper_or_launcher($location) {
60   if (file_exists($location. DIRECTORY_SEPARATOR. 'index.php')) {
61     return find_wrapper_or_launcher_in_vicinity($location);
62   }
63   return find_wrapper_or_launcher_in_specific_locations($location, ["", 'vendor'. DIRECTORY_SEPARATOR. 'bin']);
64 }
65
66 /**
67  * We look for a "Drush wrapper" script that might
68  * be stored in the root of a site.  If there is
69  * no wrapper script, then we look for the
70  * drush.launcher script in vendor/bin.  We try just a
71  * few of the most common locations; if the user relocates
72  * their vendor directory anywhere else, then they must
73  * use a wrapper script to locate it.  See the comment in
74  * 'examples/drush' for details.
75  */
76 function find_wrapper_or_launcher_in_vicinity($location) {
77   $sep = DIRECTORY_SEPARATOR;
78   $drush_locations = [
79     "",
80     "vendor{$sep}bin/",
81     "..{$sep}vendor{$sep}bin{$sep}",
82     "sites{$sep}all{$sep}vendor{$sep}bin{$sep}",
83     "sites{$sep}all{$sep}vendor{$sep}drush{$sep}drush{$sep}",
84     "sites{$sep}all{$sep}drush{$sep}drush{$sep}",
85     "drush{$sep}drush{$sep}",
86   ];
87
88   return find_wrapper_or_launcher_in_specific_locations($location, $drush_locations);
89 }
90
91 function find_wrapper_or_launcher_in_specific_locations($location, $drush_locations) {
92   $sep = DIRECTORY_SEPARATOR;
93   foreach ($drush_locations as $d) {
94     $found_script = find_wrapper_or_launcher_at_location("$location$sep$d");
95     if (!empty($found_script)) {
96       return $found_script;
97     }
98   }
99   return "";
100 }
101
102 /**
103  * We are somewhat "loose" about whether we are looking
104  * for "drush" or "drush.launcher", because in old versions
105  * of Drush, the "drush launcher" was named "drush".
106  * Otherwise, there wouldn't be any point in looking for
107  * "drush.launcher" at the root, or "drush" in a vendor directory.
108  * We also allow users to rename their drush wrapper to
109  * 'drush.wrapper' to avoid conflicting with a directory named
110  * 'drush' at the site root.
111  */
112 function find_wrapper_or_launcher_at_location($location) {
113   $sep = DIRECTORY_SEPARATOR;
114   // Sanity-check: empty $location means that we should search
115   // at the cwd, not at the root of the filesystem.
116   if (empty($location)) {
117     $location = ".";
118   }
119   foreach (array('.launcher', '.wrapper', '') as $suffix) {
120     $check_location = "$location{$sep}drush$suffix";
121     if (is_file($check_location)) {
122       return $check_location;
123     }
124   }
125   return "";
126 }
127
128 /**
129  * Determine whether current OS is a Windows variant.
130  */
131 function drush_is_windows($os = NULL) {
132   return strtoupper(substr($os ?: PHP_OS, 0, 3)) === 'WIN';
133 }
134
135 function drush_escapeshellarg($arg, $os = NULL, $raw = FALSE) {
136   // Short-circuit escaping for simple params (keep stuff readable)
137   if (preg_match('|^[a-zA-Z0-9.:/_-]*$|', $arg)) {
138     return $arg;
139   }
140   elseif (drush_is_windows($os)) {
141     return _drush_escapeshellarg_windows($arg, $raw);
142   }
143   else {
144     return _drush_escapeshellarg_linux($arg, $raw);
145   }
146 }
147
148 /**
149  * Linux version of escapeshellarg().
150  *
151  * This is intended to work the same way that escapeshellarg() does on
152  * Linux.  If we need to escape a string that will be used remotely on
153  * a Linux system, then we need our own implementation of escapeshellarg,
154  * because the Windows version behaves differently.
155  */
156 function _drush_escapeshellarg_linux($arg, $raw = FALSE) {
157   // For single quotes existing in the string, we will "exit"
158   // single-quote mode, add a \' and then "re-enter"
159   // single-quote mode.  The result of this is that
160   // 'quote' becomes '\''quote'\''
161   $arg = preg_replace('/\'/', '\'\\\'\'', $arg);
162
163   // Replace "\t", "\n", "\r", "\0", "\x0B" with a whitespace.
164   // Note that this replacement makes Drush's escapeshellarg work differently
165   // than the built-in escapeshellarg in PHP on Linux, as these characters
166   // usually are NOT replaced. However, this was done deliberately to be more
167   // conservative when running _drush_escapeshellarg_linux on Windows
168   // (this can happen when generating a command to run on a remote Linux server.)
169   $arg = str_replace(array("\t", "\n", "\r", "\0", "\x0B"), ' ', $arg);
170
171   // Only wrap with quotes when needed.
172   if(!$raw) {
173     // Add surrounding quotes.
174     $arg = "'" . $arg . "'";
175   }
176
177   return $arg;
178 }
179
180 /**
181  * Windows version of escapeshellarg().
182  */
183 function _drush_escapeshellarg_windows($arg, $raw = FALSE) {
184   // Double up existing backslashes
185   $arg = preg_replace('/\\\/', '\\\\\\\\', $arg);
186
187   // Double up double quotes
188   $arg = preg_replace('/"/', '""', $arg);
189
190   // Double up percents.
191   // $arg = preg_replace('/%/', '%%', $arg);
192
193   // Only wrap with quotes when needed.
194   if(!$raw) {
195     // Add surrounding quotes.
196     $arg = '"' . $arg . '"';
197   }
198
199   return $arg;
200 }
201
202 /**
203  * drush_startup is called once, by the Drush "finder"
204  * script -- the "drush" script at the Drush root.
205  * It finds the correct Drush "wrapper" or "launcher"
206  * script to use, and executes it with process replacement.
207  */
208 function drush_startup($argv) {
209   $sep = DIRECTORY_SEPARATOR;
210   $found_script = "";
211   $cwd = getcwd();
212   $home = getenv("HOME");
213   $use_dir = "$home{$sep}.drush{$sep}use";
214
215   // Get the arguments for the command.  Shift off argv[0],
216   // which contains the name of this script.
217   $arguments = $argv;
218   array_shift($arguments);
219
220   // We need to do at least a partial parsing of the options,
221   // so that we can find --root / -r and so on.
222   $VERBOSE=FALSE;
223   $DEBUG=FALSE;
224   $ROOT=FALSE;
225   $COMMAND=FALSE;
226   $ALIAS=FALSE;
227   $VAR=FALSE;
228
229   foreach ($arguments as $arg) {
230     // If a variable to set was indicated on the
231     // previous iteration, then set the value of
232     // the named variable (e.g. "ROOT") to "$arg".
233     if ($VAR) {
234       $$VAR = "$arg";
235       $VAR = FALSE;
236     }
237     else {
238       switch ($arg) {
239         case "-r":
240           $VAR = "ROOT";
241           break;
242
243         case "-dv":
244         case "-vd":
245         case "--debug":
246         case "-d":
247           $DEBUG = TRUE;
248           break;
249
250         case "-dv":
251         case "-vd":
252         case "--verbose":
253         case "-v":
254           $VERBOSE = TRUE;
255           break;
256       }
257       if (!$COMMAND && !$ALIAS && ($arg[0] == '@')) {
258         $ALIAS = $arg;
259       }
260       elseif (!$COMMAND && ($arg[0] != '-')) {
261         $COMMAND = $arg;
262       }
263       if (substr($arg, 0, 7) == "--root=") {
264         $ROOT = substr($arg, 7);
265       }
266     }
267   }
268
269   $NONE=($ALIAS == "@none");
270
271   // If we have found the site-local Drush script, then
272   // do not search for it again; use the environment value
273   // we set last time.
274   $found_script = getenv('DRUSH_FINDER_SCRIPT');
275
276   // If the @none alias is used, then we skip the Drush wrapper,
277   // and call the Drush launcher directly.
278   //
279   // In this instance, we are assuming that the 'drush' that is being
280   // called is:
281   //
282   //  a) The global 'drush', or
283   //  b) A site-local 'drush' in a vendor/bin directory.
284   //
285   // In either event, the appropriate 'drush.launcher' should be right next
286   // to this script (stored in the same directory).
287   if (empty($found_script) && $NONE) {
288     if (is_file(dirname(__DIR__) . "{$sep}drush.launcher")) {
289       $found_script = dirname(__DIR__) . "{$sep}drush.launcher";
290     }
291     else {
292       fwrite(STDERR, "Could not find drush.launcher in " . dirname(__DIR__) . ". Check your installation.\n");
293       exit(1);
294     }
295   }
296
297   // Check for a root option:
298   //
299   //   drush --root=/path
300   //
301   // If the site root is specified via a commandline option, then we
302   // should always use the Drush stored at this root, if there is one.
303   // We will first check for a "wrapper" script at the root, and then
304   // we will look for a "launcher" script in vendor/bin.
305   if (empty($found_script) && !empty($ROOT)) {
306     $found_script = find_wrapper_or_launcher($ROOT);
307     if (!empty($found_script)) {
308       chdir($ROOT);
309     }
310   }
311
312   // If there is a .drush-use file, then its contents will
313   // contain the path to the Drush to use.
314   if (empty($found_script)) {
315     if (is_file(".drush-use")) {
316       $found_script = trim(file_get_contents(".drush-use"));
317     }
318   }
319
320   // Look for a 'drush' wrapper or launcher at the cwd,
321   // and in each of the directories above the cwd.  If
322   // we find one, use it.
323   if (empty($found_script)) {
324     $c = getcwd();
325     // Windows can give us lots of different strings to represent the root
326     // directory as it often includes the drive letter. If we get the same
327     // result from dirname() twice in a row, then we know we're at the root.
328     $last = '';
329     while (!empty($c) && ($c != $last)) {
330       $found_script = find_wrapper_or_launcher($c);
331       if ($found_script) {
332         chdir($c);
333         break;
334       }
335       $last = $c;
336       $c = dirname($c);
337     }
338   }
339
340   if (!empty($found_script)) {
341     $found_script = realpath($found_script);
342
343     // Guard against errors:  if we have found a "drush" script
344     // (that is, theoretically a drush wrapper script), and
345     // there is a "drush.launcher" script in the same directory,
346     // then we will skip the "drush" script and use the drush launcher
347     // instead.  This is because drush "wrapper" scripts should
348     // only ever exist at the root of a site, and there should
349     // never be a drush "launcher" at the root of a site.
350     // Therefore, if we find a "drush.launcher" next to a script
351     // called "drush", we have probably found a Drush install directory,
352     // not a site root.  Adjust appropriately.  Note that this
353     // also catches the case where a drush "finder" script finds itself.
354     if (is_file(dirname($found_script) . "{$sep}drush.launcher")) {
355       $found_script = dirname($found_script) . "{$sep}drush.launcher";
356     }
357   }
358
359   // Didn't find any site-local Drush, or @use'd Drush.
360   // Skip the Bash niceties of the launcher and proceed to drush_main() in either case:
361   // - No script was found and we are running a Phar
362   // - The found script *is* the Phar https://github.com/drush-ops/drush/pull/2246.
363   $phar_path = class_exists('Phar') ? Phar::running(FALSE) : '';
364   if ((empty($found_script) && $phar_path) || !empty($found_script) && $found_script == $phar_path) {
365     drush_run_main($DEBUG, $sep, "Phar detected. Proceeding to drush_main().");
366   }
367
368   // Didn't find any site-local Drush, or @use'd Drush, or Phar.
369   // There should be a drush.launcher in same directory as this script.
370   if (empty($found_script)) {
371     $found_script = dirname(__DIR__) . "{$sep}drush.launcher";
372   }
373
374   if (drush_is_windows()) {
375     // Sometimes we found launcher in /bin, and sometimes not. Adjust accordingly.
376     if (strpos($found_script, 'bin')) {
377       $found_script = dirname($found_script). $sep. 'drush.php.bat';
378     }
379     else {
380       array_unshift($arguments, dirname($found_script). $sep. 'drush.php');
381       $found_script = 'php';
382     }
383   }
384
385   // Always use pcntl_exec if it exists.
386   $use_pcntl_exec = function_exists("pcntl_exec");
387
388   // If we have posix_getppid, then pass in the shell pid so
389   // that 'site-set' et. al. can work correctly.
390   if (function_exists('posix_getppid')) {
391     putenv("DRUSH_SHELL_PID=" . posix_getppid());
392   }
393
394   // Set an environment variable indicating which script
395   // the Drush finder found. If we end up re-entrantly calling
396   // another Drush finder, then we will skip searching for
397   // a site-local Drush, and always use the drush.launcher
398   // found previously.  This environment variable typically should
399   // not be set by clients.
400   putenv("DRUSH_FINDER_SCRIPT=$found_script");
401
402   // Emit a message in debug mode advertising the location of the
403   // script we found.
404   if ($DEBUG) {
405     $launch_method = $use_pcntl_exec ? 'pcntl_exec' : 'proc_open';
406     fwrite(STDERR, "Using the Drush script found at $found_script using $launch_method\n");
407   }
408
409   if ($use_pcntl_exec) {
410     // Get the current environment for pnctl_exec.
411     $env = drush_env();
412
413     // Launch the new script in the same process.
414     // If the launch succeeds, then it will not return.
415     $error = pcntl_exec($found_script, $arguments, $env);
416     if (!$error) {
417       $errno = pcntl_get_last_error();
418       $strerror = pcntl_strerror($errno);
419       fwrite(STDERR, "Error has occurred executing the Drush script found at $found_script\n");
420       fwrite(STDERR, "(errno {$errno}) $strerror\n");
421     }
422     exit(1);
423   }
424   else {
425     $escaped_args = array_map(function($item) { return drush_escapeshellarg($item); }, $arguments);
426     // Double quotes around $found_script as it can contain spaces.
427     $cmd = drush_escapeshellarg($found_script). ' '. implode(' ', $escaped_args);
428     if (drush_is_windows()) {
429       // Windows requires double quotes around whole command.
430       // @see https://bugs.php.net/bug.php?id=49139
431       // @see https://bugs.php.net/bug.php?id=60181
432       $cmd = '"'. $cmd. '"';
433     }
434     $process = proc_open($cmd, array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes, $cwd);
435     $proc_status = proc_get_status($process);
436     $exit_code = proc_close($process);
437     exit($proc_status["running"] ? $exit_code : $proc_status["exitcode"] );
438   }
439 }
440
441 /**
442  * Run drush_main() and then exit. Used when we cannot hand over execution to
443  * the launcher.
444  *
445  * @param bool $DEBUG
446  *   Are we in debug mode
447  * @param string $sep
448  *   Directory separator
449  * @param string $msg
450  *   Debug message to log before running drush_main()
451  */
452 function drush_run_main($DEBUG, $sep, $msg) {
453 // Emit a message in debug mode advertising how we proceeded.
454   if ($DEBUG) {
455     fwrite(STDERR, $msg. "\n");
456   }
457   require __DIR__ . "{$sep}preflight.inc";
458   exit(drush_main());
459 }