* Hide the "back" buttons on the completion pages, they are potentially confusing...
[lhc/web/wiklou.git] / includes / installer / Installer.php
1 <?php
2 /**
3 * Base code for MediaWiki installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * This documentation group collects source code files with deployment functionality.
11 *
12 * @defgroup Deployment Deployment
13 */
14
15 /**
16 * Base installer class.
17 *
18 * This class provides the base for installation and update functionality
19 * for both MediaWiki core and extensions.
20 *
21 * @ingroup Deployment
22 * @since 1.17
23 */
24 abstract class Installer {
25
26 /**
27 * TODO: make protected?
28 *
29 * @var array
30 */
31 public $settings;
32
33 /**
34 * Cached DB installer instances, access using getDBInstaller().
35 *
36 * @var array
37 */
38 protected $dbInstallers = array();
39
40 /**
41 * Minimum memory size in MB.
42 *
43 * @var integer
44 */
45 protected $minMemorySize = 50;
46
47 /**
48 * Cached Title, used by parse().
49 *
50 * @var Title
51 */
52 protected $parserTitle;
53
54 /**
55 * Cached ParserOptions, used by parse().
56 *
57 * @var ParserOptions
58 */
59 protected $parserOptions;
60
61 /**
62 * Known database types. These correspond to the class names <type>Installer,
63 * and are also MediaWiki database types valid for $wgDBtype.
64 *
65 * To add a new type, create a <type>Installer class and a Database<type>
66 * class, and add a config-type-<type> message to MessagesEn.php.
67 *
68 * @var array
69 */
70 protected static $dbTypes = array(
71 'mysql',
72 'postgres',
73 'oracle',
74 'sqlite',
75 );
76
77 /**
78 * A list of environment check methods called by doEnvironmentChecks().
79 * These may output warnings using showMessage(), and/or abort the
80 * installation process by returning false.
81 *
82 * @var array
83 */
84 protected $envChecks = array(
85 'envCheckDB',
86 'envCheckRegisterGlobals',
87 'envCheckBrokenXML',
88 'envCheckPHP531',
89 'envCheckMagicQuotes',
90 'envCheckMagicSybase',
91 'envCheckMbstring',
92 'envCheckZE1',
93 'envCheckSafeMode',
94 'envCheckXML',
95 'envCheckPCRE',
96 'envCheckMemory',
97 'envCheckCache',
98 'envCheckDiff3',
99 'envCheckGraphics',
100 'envCheckPath',
101 'envCheckExtension',
102 'envCheckShellLocale',
103 'envCheckUploadsDirectory',
104 'envCheckLibicu'
105 );
106
107 /**
108 * UI interface for displaying a short message
109 * The parameters are like parameters to wfMsg().
110 * The messages will be in wikitext format, which will be converted to an
111 * output format such as HTML or text before being sent to the user.
112 */
113 public abstract function showMessage( $msg /*, ... */ );
114
115 /**
116 * Constructor, always call this from child classes.
117 */
118 public function __construct() {
119 // Disable the i18n cache and LoadBalancer
120 Language::getLocalisationCache()->disableBackend();
121 LBFactory::disableBackend();
122 }
123
124 /**
125 * Get a list of known DB types.
126 */
127 public static function getDBTypes() {
128 return self::$dbTypes;
129 }
130
131 /**
132 * Do initial checks of the PHP environment. Set variables according to
133 * the observed environment.
134 *
135 * It's possible that this may be called under the CLI SAPI, not the SAPI
136 * that the wiki will primarily run under. In that case, the subclass should
137 * initialise variables such as wgScriptPath, before calling this function.
138 *
139 * Under the web subclass, it can already be assumed that PHP 5+ is in use
140 * and that sessions are working.
141 *
142 * @return boolean
143 */
144 public function doEnvironmentChecks() {
145 $good = true;
146
147 foreach ( $this->envChecks as $check ) {
148 $status = $this->$check();
149 if ( $status === false ) {
150 $good = false;
151 }
152 }
153
154 $this->setVar( '_Environment', $good );
155
156 if ( $good ) {
157 $this->showMessage( 'config-env-good' );
158 } else {
159 $this->showMessage( 'config-env-bad' );
160 }
161
162 return $good;
163 }
164
165 /**
166 * Set a MW configuration variable, or internal installer configuration variable.
167 *
168 * @param $name String
169 * @param $value Mixed
170 */
171 public function setVar( $name, $value ) {
172 $this->settings[$name] = $value;
173 }
174
175 /**
176 * Get an MW configuration variable, or internal installer configuration variable.
177 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
178 * Installer variables are typically prefixed by an underscore.
179 *
180 * @param $name String
181 * @param $default Mixed
182 *
183 * @return mixed
184 */
185 public function getVar( $name, $default = null ) {
186 if ( !isset( $this->settings[$name] ) ) {
187 return $default;
188 } else {
189 return $this->settings[$name];
190 }
191 }
192
193 /**
194 * Get an instance of DatabaseInstaller for the specified DB type.
195 *
196 * @param $type Mixed: DB installer for which is needed, false to use default.
197 *
198 * @return DatabaseInstaller
199 */
200 public function getDBInstaller( $type = false ) {
201 if ( !$type ) {
202 $type = $this->getVar( 'wgDBtype' );
203 }
204
205 $type = strtolower( $type );
206
207 if ( !isset( $this->dbInstallers[$type] ) ) {
208 $class = ucfirst( $type ). 'Installer';
209 $this->dbInstallers[$type] = new $class( $this );
210 }
211
212 return $this->dbInstallers[$type];
213 }
214
215 /**
216 * Determine if LocalSettings.php exists. If it does, return its variables,
217 * merged with those from AdminSettings.php, as an array.
218 *
219 * @return Array
220 */
221 public function getExistingLocalSettings() {
222 global $IP;
223
224 wfSuppressWarnings();
225 $_lsExists = file_exists( "$IP/LocalSettings.php" );
226 wfRestoreWarnings();
227
228 if( $_lsExists ) {
229 require( "$IP/includes/DefaultSettings.php" );
230 require( "$IP/LocalSettings.php" );
231 if ( file_exists( "$IP/AdminSettings.php" ) ) {
232 require( "$IP/AdminSettings.php" );
233 }
234 $vars = get_defined_vars();
235 unset( $vars['_lsExists'] );
236 return $vars;
237 } else {
238 return false;
239 }
240 }
241
242 /**
243 * Get a fake password for sending back to the user in HTML.
244 * This is a security mechanism to avoid compromise of the password in the
245 * event of session ID compromise.
246 *
247 * @param $realPassword String
248 *
249 * @return string
250 */
251 public function getFakePassword( $realPassword ) {
252 return str_repeat( '*', strlen( $realPassword ) );
253 }
254
255 /**
256 * Set a variable which stores a password, except if the new value is a
257 * fake password in which case leave it as it is.
258 *
259 * @param $name String
260 * @param $value Mixed
261 */
262 public function setPassword( $name, $value ) {
263 if ( !preg_match( '/^\*+$/', $value ) ) {
264 $this->setVar( $name, $value );
265 }
266 }
267
268 /**
269 * On POSIX systems return the primary group of the webserver we're running under.
270 * On other systems just returns null.
271 *
272 * This is used to advice the user that he should chgrp his config/data/images directory as the
273 * webserver user before he can install.
274 *
275 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
276 *
277 * @return mixed
278 */
279 public static function maybeGetWebserverPrimaryGroup() {
280 if ( !function_exists( 'posix_getegid' ) || !function_exists( 'posix_getpwuid' ) ) {
281 # I don't know this, this isn't UNIX.
282 return null;
283 }
284
285 # posix_getegid() *not* getmygid() because we want the group of the webserver,
286 # not whoever owns the current script.
287 $gid = posix_getegid();
288 $getpwuid = posix_getpwuid( $gid );
289 $group = $getpwuid['name'];
290
291 return $group;
292 }
293
294 /**
295 * Convert wikitext $text to HTML.
296 *
297 * This is potentially error prone since many parser features require a complete
298 * installed MW database. The solution is to just not use those features when you
299 * write your messages. This appears to work well enough. Basic formatting and
300 * external links work just fine.
301 *
302 * But in case a translator decides to throw in a #ifexist or internal link or
303 * whatever, this function is guarded to catch the attempted DB access and to present
304 * some fallback text.
305 *
306 * @param $text String
307 * @param $lineStart Boolean
308 * @return String
309 */
310 public function parse( $text, $lineStart = false ) {
311 global $wgParser;
312
313 try {
314 $out = $wgParser->parse( $text, $this->parserTitle, $this->parserOptions, $lineStart );
315 $html = $out->getText();
316 } catch ( DBAccessError $e ) {
317 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
318
319 if ( !empty( $this->debug ) ) {
320 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
321 }
322 }
323
324 return $html;
325 }
326
327 public function getParserOptions() {
328 return $this->parserOptions;
329 }
330
331 public function disableLinkPopups() {
332 $this->parserOptions->setExternalLinkTarget( false );
333 }
334
335 public function restoreLinkPopups() {
336 global $wgExternalLinkTarget;
337 $this->parserOptions->setExternalLinkTarget( $wgExternalLinkTarget );
338 }
339
340 /**
341 * TODO: document
342 *
343 * @param $installer DatabaseInstaller
344 *
345 * @return Status
346 */
347 public function installTables( DatabaseInstaller &$installer ) {
348 $status = $installer->createTables();
349
350 if( $status->isOK() ) {
351 LBFactory::enableBackend();
352 }
353
354 return $status;
355 }
356
357 /**
358 * Exports all wg* variables stored by the installer into global scope.
359 */
360 public function exportVars() {
361 foreach ( $this->settings as $name => $value ) {
362 if ( substr( $name, 0, 2 ) == 'wg' ) {
363 $GLOBALS[$name] = $value;
364 }
365 }
366 }
367
368 /**
369 * Environment check for DB types.
370 */
371 protected function envCheckDB() {
372 global $wgLang;
373
374 $compiledDBs = array();
375 $goodNames = array();
376 $allNames = array();
377
378 foreach ( self::getDBTypes() as $name ) {
379 $db = $this->getDBInstaller( $name );
380 $readableName = wfMsg( 'config-type-' . $name );
381
382 if ( $db->isCompiled() ) {
383 $compiledDBs[] = $name;
384 $goodNames[] = $readableName;
385 }
386
387 $allNames[] = $readableName;
388 }
389
390 $this->setVar( '_CompiledDBs', $compiledDBs );
391
392 if ( !$compiledDBs ) {
393 $this->showMessage( 'config-no-db' );
394 // FIXME: this only works for the web installer!
395 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
396 return false;
397 }
398
399 // Check for FTS3 full-text search module
400 $sqlite = $this->getDBInstaller( 'sqlite' );
401 if ( $sqlite->isCompiled() ) {
402 $db = new DatabaseSqliteStandalone( ':memory:' );
403 if( $db->getFulltextSearchModule() != 'FTS3' ) {
404 $this->showMessage( 'config-no-fts3' );
405 }
406 }
407 }
408
409 /**
410 * Environment check for register_globals.
411 */
412 protected function envCheckRegisterGlobals() {
413 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
414 $this->showMessage( 'config-register-globals' );
415 }
416 }
417
418 /**
419 * Some versions of libxml+PHP break < and > encoding horribly
420 */
421 protected function envCheckBrokenXML() {
422 $test = new PhpXmlBugTester();
423 if ( !$test->ok ) {
424 $this->showMessage( 'config-brokenlibxml' );
425 return false;
426 }
427 }
428
429 /**
430 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
431 * reference parameters to __call() are not converted to null
432 */
433 protected function envCheckPHP531() {
434 $test = new PhpRefCallBugTester;
435 $test->execute();
436 if ( !$test->ok ) {
437 $this->showMessage( 'config-using531' );
438 return false;
439 }
440 }
441
442 /**
443 * Environment check for magic_quotes_runtime.
444 */
445 protected function envCheckMagicQuotes() {
446 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
447 $this->showMessage( 'config-magic-quotes-runtime' );
448 return false;
449 }
450 }
451
452 /**
453 * Environment check for magic_quotes_sybase.
454 */
455 protected function envCheckMagicSybase() {
456 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
457 $this->showMessage( 'config-magic-quotes-sybase' );
458 return false;
459 }
460 }
461
462 /**
463 * Environment check for mbstring.func_overload.
464 */
465 protected function envCheckMbstring() {
466 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
467 $this->showMessage( 'config-mbstring' );
468 return false;
469 }
470 }
471
472 /**
473 * Environment check for zend.ze1_compatibility_mode.
474 */
475 protected function envCheckZE1() {
476 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
477 $this->showMessage( 'config-ze1' );
478 return false;
479 }
480 }
481
482 /**
483 * Environment check for safe_mode.
484 */
485 protected function envCheckSafeMode() {
486 if ( wfIniGetBool( 'safe_mode' ) ) {
487 $this->setVar( '_SafeMode', true );
488 $this->showMessage( 'config-safe-mode' );
489 }
490 }
491
492 /**
493 * Environment check for the XML module.
494 */
495 protected function envCheckXML() {
496 if ( !function_exists( "utf8_encode" ) ) {
497 $this->showMessage( 'config-xml-bad' );
498 return false;
499 }
500 }
501
502 /**
503 * Environment check for the PCRE module.
504 */
505 protected function envCheckPCRE() {
506 if ( !function_exists( 'preg_match' ) ) {
507 $this->showMessage( 'config-pcre' );
508 return false;
509 }
510 wfSuppressWarnings();
511 $regexd = preg_replace( '/[\x{0400}-\x{04FF}]/u', '', '-АБВГД-' );
512 wfRestoreWarnings();
513 if ( $regexd != '--' ) {
514 $this->showMessage( 'config-pcre-no-utf8' );
515 return false;
516 }
517 }
518
519 /**
520 * Environment check for available memory.
521 */
522 protected function envCheckMemory() {
523 $limit = ini_get( 'memory_limit' );
524
525 if ( !$limit || $limit == -1 ) {
526 return true;
527 }
528
529 $n = intval( $limit );
530
531 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
532 $n = intval( $m[1] * ( 1024 * 1024 ) );
533 }
534
535 if( $n < $this->minMemorySize * 1024 * 1024 ) {
536 $newLimit = "{$this->minMemorySize}M";
537
538 if( ini_set( "memory_limit", $newLimit ) === false ) {
539 $this->showMessage( 'config-memory-bad', $limit );
540 } else {
541 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
542 $this->setVar( '_RaiseMemory', true );
543 }
544 } else {
545 return true;
546 }
547 }
548
549 /**
550 * Environment check for compiled object cache types.
551 */
552 protected function envCheckCache() {
553 $caches = array();
554 foreach ( $this->objectCaches as $name => $function ) {
555 if ( function_exists( $function ) ) {
556 $caches[$name] = true;
557 }
558 }
559
560 if ( !$caches ) {
561 $this->showMessage( 'config-no-cache' );
562 }
563
564 $this->setVar( '_Caches', $caches );
565 }
566
567 /**
568 * Search for GNU diff3.
569 */
570 protected function envCheckDiff3() {
571 $names = array( "gdiff3", "diff3", "diff3.exe" );
572 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
573
574 $diff3 = $this->locateExecutableInDefaultPaths( $names, $versionInfo );
575
576 if ( $diff3 ) {
577 $this->setVar( 'wgDiff3', $diff3 );
578 } else {
579 $this->setVar( 'wgDiff3', false );
580 $this->showMessage( 'config-diff3-bad' );
581 }
582 }
583
584 /**
585 * Environment check for ImageMagick and GD.
586 */
587 protected function envCheckGraphics() {
588 $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
589 $convert = $this->locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
590
591 if ( $convert ) {
592 $this->setVar( 'wgImageMagickConvertCommand', $convert );
593 $this->showMessage( 'config-imagemagick', $convert );
594 return true;
595 } elseif ( function_exists( 'imagejpeg' ) ) {
596 $this->showMessage( 'config-gd' );
597 return true;
598 } else {
599 $this->showMessage( 'no-scaling' );
600 }
601 }
602
603 /**
604 * Environment check for setting $IP and $wgScriptPath.
605 */
606 protected function envCheckPath() {
607 global $IP;
608 $IP = dirname( dirname( dirname( __FILE__ ) ) );
609
610 $this->setVar( 'IP', $IP );
611
612 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
613 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
614 // to get the path to the current script... hopefully it's reliable. SIGH
615 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
616 $path = $_SERVER['PHP_SELF'];
617 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
618 $path = $_SERVER['SCRIPT_NAME'];
619 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
620 // Some kind soul has set it for us already (e.g. debconf)
621 return true;
622 } else {
623 $this->showMessage( 'config-no-uri' );
624 return false;
625 }
626
627 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
628 $this->setVar( 'wgScriptPath', $uri );
629 }
630
631 /**
632 * Environment check for setting the preferred PHP file extension.
633 */
634 protected function envCheckExtension() {
635 // FIXME: detect this properly
636 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
637 $ext = 'php5';
638 } else {
639 $ext = 'php';
640 }
641 $this->setVar( 'wgScriptExtension', ".$ext" );
642 }
643
644 /**
645 * TODO: document
646 */
647 protected function envCheckShellLocale() {
648 $os = php_uname( 's' );
649 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
650
651 if ( !in_array( $os, $supported ) ) {
652 return true;
653 }
654
655 # Get a list of available locales.
656 $ret = false;
657 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
658
659 if ( $ret ) {
660 return true;
661 }
662
663 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
664 $candidatesByLocale = array();
665 $candidatesByLang = array();
666
667 foreach ( $lines as $line ) {
668 if ( $line === '' ) {
669 continue;
670 }
671
672 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
673 continue;
674 }
675
676 list( $all, $lang, $territory, $charset, $modifier ) = $m;
677
678 $candidatesByLocale[$m[0]] = $m;
679 $candidatesByLang[$lang][] = $m;
680 }
681
682 # Try the current value of LANG.
683 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
684 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
685 return true;
686 }
687
688 # Try the most common ones.
689 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
690 foreach ( $commonLocales as $commonLocale ) {
691 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
692 $this->setVar( 'wgShellLocale', $commonLocale );
693 return true;
694 }
695 }
696
697 # Is there an available locale in the Wiki's language?
698 $wikiLang = $this->getVar( 'wgLanguageCode' );
699
700 if ( isset( $candidatesByLang[$wikiLang] ) ) {
701 $m = reset( $candidatesByLang[$wikiLang] );
702 $this->setVar( 'wgShellLocale', $m[0] );
703 return true;
704 }
705
706 # Are there any at all?
707 if ( count( $candidatesByLocale ) ) {
708 $m = reset( $candidatesByLocale );
709 $this->setVar( 'wgShellLocale', $m[0] );
710 return true;
711 }
712
713 # Give up.
714 return true;
715 }
716
717 /**
718 * TODO: document
719 */
720 protected function envCheckUploadsDirectory() {
721 global $IP, $wgServer;
722
723 $dir = $IP . '/images/';
724 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
725 $safe = !$this->dirIsExecutable( $dir, $url );
726
727 if ( $safe ) {
728 return true;
729 } else {
730 $this->showMessage( 'config-uploads-not-safe', $dir );
731 }
732 }
733
734 /**
735 * Convert a hex string representing a Unicode code point to that code point.
736 * @param $c String
737 * @return string
738 */
739 protected function unicodeChar( $c ) {
740 $c = hexdec($c);
741 if ($c <= 0x7F) {
742 return chr($c);
743 } else if ($c <= 0x7FF) {
744 return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
745 } else if ($c <= 0xFFFF) {
746 return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
747 . chr(0x80 | $c & 0x3F);
748 } else if ($c <= 0x10FFFF) {
749 return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
750 . chr(0x80 | $c >> 6 & 0x3F)
751 . chr(0x80 | $c & 0x3F);
752 } else {
753 return false;
754 }
755 }
756
757
758 /**
759 * Check the libicu version
760 */
761 protected function envCheckLibicu() {
762 $utf8 = function_exists( 'utf8_normalize' );
763 $intl = function_exists( 'normalizer_normalize' );
764
765 /**
766 * This needs to be updated something that the latest libicu
767 * will properly normalize. This normalization was found at
768 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
769 * Note that we use the hex representation to create the code
770 * points in order to avoid any Unicode-destroying during transit.
771 */
772 $not_normal_c = $this->unicodeChar("FA6C");
773 $normal_c = $this->unicodeChar("242EE");
774
775 $useNormalizer = 'php';
776 $needsUpdate = false;
777
778 /**
779 * We're going to prefer the pecl extension here unless
780 * utf8_normalize is more up to date.
781 */
782 if( $utf8 ) {
783 $useNormalizer = 'utf8';
784 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC );
785 if ( $utf8 !== $normal_c ) $needsUpdate = true;
786 }
787 if( $intl ) {
788 $useNormalizer = 'intl';
789 $intl = normalizer_normalize( $not_normal_c, Normalizer::FORM_C );
790 if ( $intl !== $normal_c ) $needsUpdate = true;
791 }
792
793 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
794 if( $useNormalizer === 'php' ) {
795 $this->showMessage( 'config-unicode-pure-php-warning' );
796 } else {
797 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
798 if( $needsUpdate ) {
799 $this->showMessage( 'config-unicode-update-warning' );
800 }
801 }
802 }
803
804 /**
805 * Get an array of likely places we can find executables. Check a bunch
806 * of known Unix-like defaults, as well as the PATH environment variable
807 * (which should maybe make it work for Windows?)
808 *
809 * @return Array
810 */
811 protected static function getPossibleBinPaths() {
812 return array_merge(
813 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
814 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
815 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
816 );
817 }
818
819 /**
820 * Search a path for any of the given executable names. Returns the
821 * executable name if found. Also checks the version string returned
822 * by each executable.
823 *
824 * Used only by environment checks.
825 *
826 * @param $path String: path to search
827 * @param $names Array of executable names
828 * @param $versionInfo Boolean false or array with two members:
829 * 0 => Command to run for version check, with $1 for the full executable name
830 * 1 => String to compare the output with
831 *
832 * If $versionInfo is not false, only executables with a version
833 * matching $versionInfo[1] will be returned.
834 */
835 public static function locateExecutable( $path, $names, $versionInfo = false ) {
836 if ( !is_array( $names ) ) {
837 $names = array( $names );
838 }
839
840 foreach ( $names as $name ) {
841 $command = $path . DIRECTORY_SEPARATOR . $name;
842
843 wfSuppressWarnings();
844 $file_exists = file_exists( $command );
845 wfRestoreWarnings();
846
847 if ( $file_exists ) {
848 if ( !$versionInfo ) {
849 return $command;
850 }
851
852 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
853 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
854 return $command;
855 }
856 }
857 }
858 return false;
859 }
860
861 /**
862 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
863 * @see locateExecutable()
864 */
865 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
866 foreach( self::getPossibleBinPaths() as $path ) {
867 $exe = self::locateExecutable( $path, $names, $versionInfo );
868 if( $exe !== false ) {
869 return $exe;
870 }
871 }
872 return false;
873 }
874
875 /**
876 * Checks if scripts located in the given directory can be executed via the given URL.
877 *
878 * Used only by environment checks.
879 */
880 public function dirIsExecutable( $dir, $url ) {
881 $scriptTypes = array(
882 'php' => array(
883 "<?php echo 'ex' . 'ec';",
884 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
885 ),
886 );
887
888 // it would be good to check other popular languages here, but it'll be slow.
889
890 wfSuppressWarnings();
891
892 foreach ( $scriptTypes as $ext => $contents ) {
893 foreach ( $contents as $source ) {
894 $file = 'exectest.' . $ext;
895
896 if ( !file_put_contents( $dir . $file, $source ) ) {
897 break;
898 }
899
900 $text = Http::get( $url . $file );
901 unlink( $dir . $file );
902
903 if ( $text == 'exec' ) {
904 wfRestoreWarnings();
905 return $ext;
906 }
907 }
908 }
909
910 wfRestoreWarnings();
911
912 return false;
913 }
914
915 }