PHPVersionCheck.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. // @codingStandardsIgnoreFile Generic.Arrays.DisallowLongArraySyntax
  3. // @codingStandardsIgnoreFile Generic.Files.LineLength
  4. // @codingStandardsIgnoreFile MediaWiki.Usage.DirUsage.FunctionFound
  5. /**
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. */
  23. /**
  24. * Check PHP Version, as well as for composer dependencies in entry points,
  25. * and display something vaguely comprehensible in the event of a totally
  26. * unrecoverable error.
  27. * @class
  28. */
  29. class PHPVersionCheck {
  30. /* @var string The number of the MediaWiki version used */
  31. var $mwVersion = '1.30';
  32. var $functionsExtensionsMapping = array(
  33. 'mb_substr' => 'mbstring',
  34. 'utf8_encode' => 'xml',
  35. 'ctype_digit' => 'ctype',
  36. 'json_decode' => 'json',
  37. 'iconv' => 'iconv',
  38. 'mime_content_type' => 'fileinfo',
  39. );
  40. /**
  41. * @var string Which entry point we are protecting. One of:
  42. * - index.php
  43. * - load.php
  44. * - api.php
  45. * - mw-config/index.php
  46. * - cli
  47. */
  48. var $entryPoint = null;
  49. /**
  50. * @param string $entryPoint Which entry point we are protecting. One of:
  51. * - index.php
  52. * - load.php
  53. * - api.php
  54. * - mw-config/index.php
  55. * - cli
  56. * @return $this
  57. */
  58. function setEntryPoint( $entryPoint ) {
  59. $this->entryPoint = $entryPoint;
  60. }
  61. /**
  62. * Returns the version of the installed php implementation.
  63. *
  64. * @param string $impl By default, the function returns the info of the currently installed PHP
  65. * implementation. Using this parameter the caller can decide, what version info will be
  66. * returned. Valid values: HHVM, PHP
  67. * @return array An array of information about the php implementation, containing:
  68. * - 'version': The version of the php implementation (specific to the implementation, not
  69. * the version of the implemented php version)
  70. * - 'implementation': The name of the implementation used
  71. * - 'vendor': The development group, vendor or developer of the implementation.
  72. * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
  73. * - 'minSupported': The minimum version supported by MediWiki
  74. * - 'upgradeURL': The URL to the website of the implementation that contains
  75. * upgrade/installation instructions.
  76. */
  77. function getPHPInfo( $impl = false ) {
  78. if (
  79. ( defined( 'HHVM_VERSION' ) && $impl !== 'PHP' ) ||
  80. $impl === 'HHVM'
  81. ) {
  82. return array(
  83. 'implementation' => 'HHVM',
  84. 'version' => defined( 'HHVM_VERSION' ) ? HHVM_VERSION : 'undefined',
  85. 'vendor' => 'Facebook',
  86. 'upstreamSupported' => '3.6.5',
  87. 'minSupported' => '3.6.5',
  88. 'upgradeURL' => 'https://docs.hhvm.com/hhvm/installation/introduction',
  89. );
  90. }
  91. return array(
  92. 'implementation' => 'PHP',
  93. 'version' => PHP_VERSION,
  94. 'vendor' => 'the PHP Group',
  95. 'upstreamSupported' => '5.5.0',
  96. 'minSupported' => '5.5.9',
  97. 'upgradeURL' => 'https://secure.php.net/downloads.php',
  98. );
  99. }
  100. /**
  101. * Displays an error, if the installed php version does not meet the minimum requirement.
  102. *
  103. * @return $this
  104. */
  105. function checkRequiredPHPVersion() {
  106. $phpInfo = $this->getPHPInfo();
  107. $minimumVersion = $phpInfo['minSupported'];
  108. $otherInfo = $this->getPHPInfo( $phpInfo['implementation'] === 'HHVM' ? 'PHP' : 'HHVM' );
  109. if (
  110. !function_exists( 'version_compare' )
  111. || version_compare( $phpInfo['version'], $minimumVersion ) < 0
  112. ) {
  113. $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
  114. . " version $minimumVersion or {$otherInfo['implementation']} version "
  115. . "{$otherInfo['minSupported']}, you are using {$phpInfo['implementation']} "
  116. . "{$phpInfo['version']}.";
  117. $longText = "Error: You might be using an older {$phpInfo['implementation']} version. \n"
  118. . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
  119. . " $minimumVersion or higher or {$otherInfo['implementation']} version "
  120. . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
  121. . " newer php executable with a different name, such as php5.\n\n";
  122. $longHtml = <<<HTML
  123. Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
  124. {$phpInfo['implementation']}</a>.
  125. {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
  126. longer supported by {$phpInfo['vendor']} and will not receive
  127. security or bugfix updates.
  128. </p>
  129. <p>
  130. If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
  131. you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
  132. older version of MediaWiki from our website.
  133. See our <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
  134. for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
  135. HTML;
  136. $this->triggerError(
  137. "Supported {$phpInfo['implementation']} versions",
  138. $shortText,
  139. $longText,
  140. $longHtml
  141. );
  142. }
  143. }
  144. /**
  145. * Displays an error, if the vendor/autoload.php file could not be found.
  146. *
  147. * @return $this
  148. */
  149. function checkVendorExistence() {
  150. if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
  151. $shortText = "Installing some external dependencies (e.g. via composer) is required.";
  152. $longText = "Error: You are missing some external dependencies. \n"
  153. . "MediaWiki now also has some external dependencies that need to be installed\n"
  154. . "via composer or from a separate git repo. Please see\n"
  155. . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
  156. . "for help on installing the required components.";
  157. $longHtml = <<<HTML
  158. MediaWiki now also has some external dependencies that need to be installed via
  159. composer or from a separate git repo. Please see
  160. <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
  161. for help on installing the required components.
  162. HTML;
  163. $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
  164. }
  165. }
  166. /**
  167. * Displays an error, if a PHP extension does not exist.
  168. *
  169. * @return $this
  170. */
  171. function checkExtensionExistence() {
  172. $missingExtensions = array();
  173. foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
  174. if ( !function_exists( $function ) ) {
  175. $missingExtensions[] = $extension;
  176. }
  177. }
  178. if ( $missingExtensions ) {
  179. $shortText = "Installing some PHP extensions is required.";
  180. $missingExtText = '';
  181. $missingExtHtml = '';
  182. $baseUrl = 'https://secure.php.net';
  183. foreach ( $missingExtensions as $ext ) {
  184. $missingExtText .= " * $ext <$baseUrl/$ext>\n";
  185. $missingExtHtml .= "<li><b>$ext</b> "
  186. . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
  187. }
  188. $cliText = "Error: Missing one or more required components of PHP.\n"
  189. . "You are missing a required extension to PHP that MediaWiki needs.\n"
  190. . "Please install:\n" . $missingExtText;
  191. $longHtml = <<<HTML
  192. You are missing a required extension to PHP that MediaWiki
  193. requires to run. Please install:
  194. <ul>
  195. $missingExtHtml
  196. </ul>
  197. HTML;
  198. $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
  199. }
  200. }
  201. /**
  202. * Output headers that prevents error pages to be cached.
  203. */
  204. function outputHTMLHeader() {
  205. $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
  206. header( "$protocol 500 MediaWiki configuration Error" );
  207. // Don't cache error pages! They cause no end of trouble...
  208. header( 'Cache-control: none' );
  209. header( 'Pragma: no-cache' );
  210. }
  211. /**
  212. * Returns an error page, which is suitable for output to the end user via a web browser.
  213. *
  214. * @param string $title
  215. * @param string $longHtml
  216. * @param string $shortText
  217. * @return string
  218. */
  219. function getIndexErrorOutput( $title, $longHtml, $shortText ) {
  220. $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
  221. if ( $this->entryPoint == 'mw-config/index.php' ) {
  222. $dirname = dirname( $pathinfo['dirname'] );
  223. } else {
  224. $dirname = $pathinfo['dirname'];
  225. }
  226. $encLogo =
  227. htmlspecialchars( str_replace( '//', '/', $dirname . '/' ) .
  228. 'resources/assets/mediawiki.png' );
  229. $shortHtml = htmlspecialchars( $shortText );
  230. header( 'Content-type: text/html; charset=UTF-8' );
  231. $finalOutput = <<<HTML
  232. <!DOCTYPE html>
  233. <html lang="en" dir="ltr">
  234. <head>
  235. <meta charset="UTF-8" />
  236. <title>MediaWiki {$this->mwVersion}</title>
  237. <style media='screen'>
  238. body {
  239. color: #000;
  240. background-color: #fff;
  241. font-family: sans-serif;
  242. padding: 2em;
  243. text-align: center;
  244. }
  245. p, img, h1, h2, ul {
  246. text-align: left;
  247. margin: 0.5em 0 1em;
  248. }
  249. h1 {
  250. font-size: 120%;
  251. }
  252. h2 {
  253. font-size: 110%;
  254. }
  255. </style>
  256. </head>
  257. <body>
  258. <img src="{$encLogo}" alt='The MediaWiki logo' />
  259. <h1>MediaWiki {$this->mwVersion} internal error</h1>
  260. <div class='error'>
  261. <p>
  262. {$shortHtml}
  263. </p>
  264. <h2>{$title}</h2>
  265. <p>
  266. {$longHtml}
  267. </p>
  268. </div>
  269. </body>
  270. </html>
  271. HTML;
  272. return $finalOutput;
  273. }
  274. /**
  275. * Display something vaguely comprehensible in the event of a totally unrecoverable error.
  276. * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
  277. * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
  278. * no longer need to be).
  279. *
  280. * Calling this function kills execution immediately.
  281. *
  282. * @param string $title HTML code to be put within an <h2> tag
  283. * @param string $shortText
  284. * @param string $longText
  285. * @param string $longHtml
  286. */
  287. function triggerError( $title, $shortText, $longText, $longHtml ) {
  288. switch ( $this->entryPoint ) {
  289. case 'cli':
  290. $finalOutput = $longText;
  291. break;
  292. case 'index.php':
  293. case 'mw-config/index.php':
  294. $this->outputHTMLHeader();
  295. $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
  296. break;
  297. case 'load.php':
  298. $this->outputHTMLHeader();
  299. $finalOutput = "/* $shortText */";
  300. break;
  301. default:
  302. $this->outputHTMLHeader();
  303. // Handle everything that's not index.php
  304. $finalOutput = $shortText;
  305. }
  306. echo "$finalOutput\n";
  307. die( 1 );
  308. }
  309. }
  310. /**
  311. * Check php version and that external dependencies are installed, and
  312. * display an informative error if either condition is not satisfied.
  313. *
  314. * @note Since we can't rely on anything, the minimum PHP versions and MW current
  315. * version are hardcoded here
  316. */
  317. function wfEntryPointCheck( $entryPoint ) {
  318. $phpVersionCheck = new PHPVersionCheck();
  319. $phpVersionCheck->setEntryPoint( $entryPoint );
  320. $phpVersionCheck->checkRequiredPHPVersion();
  321. $phpVersionCheck->checkVendorExistence();
  322. $phpVersionCheck->checkExtensionExistence();
  323. }