PHPVersionCheck.php 11 KB

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