PHPVersionCheck.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // phpcs:disable Squiz.Scope.MemberVarScope.Missing,Squiz.Scope.MethodScope.Missing
  22. // @phan-file-suppress PhanPluginDuplicateConditionalNullCoalescing
  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. *
  28. * @note Since we can't rely on anything external, the minimum PHP versions
  29. * and MW current version are hardcoded in this class.
  30. *
  31. * @note This class uses setter methods instead of a constructor so that
  32. * it can be compatible with PHP 4, PHP 5 and PHP 7 (without warnings).
  33. */
  34. class PHPVersionCheck {
  35. /* @var string The number of the MediaWiki version used. */
  36. var $mwVersion = '1.34';
  37. /* @var array A mapping of PHP functions to PHP extensions. */
  38. var $functionsExtensionsMapping = array(
  39. 'mb_substr' => 'mbstring',
  40. 'xml_parser_create' => 'xml',
  41. 'ctype_digit' => 'ctype',
  42. 'json_decode' => 'json',
  43. 'iconv' => 'iconv',
  44. 'mime_content_type' => 'fileinfo',
  45. );
  46. /**
  47. * @var string $format The format used for errors. One of "text" or "html"
  48. */
  49. var $format = 'text';
  50. /**
  51. * @var string $scriptPath
  52. */
  53. var $scriptPath = '/';
  54. /**
  55. * Set the format used for errors.
  56. *
  57. * @param string $format One of "text" or "html"
  58. */
  59. function setFormat( $format ) {
  60. $this->format = $format;
  61. }
  62. /**
  63. * Set the script path used for images in HTML-formatted errors.
  64. *
  65. * @param string $scriptPath
  66. */
  67. function setScriptPath( $scriptPath ) {
  68. $this->scriptPath = $scriptPath;
  69. }
  70. /**
  71. * Return the version of the installed PHP implementation.
  72. *
  73. * TODO: Deprecate/remove this workaround now that HHVM isn't supported.
  74. *
  75. * @return array An array of information about the PHP implementation, containing:
  76. * - 'version': The version of the PHP implementation (specific to the implementation, not
  77. * the version of the implemented PHP version)
  78. * - 'implementation': The name of the implementation used
  79. * - 'vendor': The development group, vendor or developer of the implementation.
  80. * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
  81. * - 'minSupported': The minimum version supported by MediaWiki
  82. * - 'upgradeURL': The URL to the website of the implementation that contains
  83. * upgrade/installation instructions.
  84. */
  85. function getPHPInfo() {
  86. return array(
  87. 'implementation' => 'PHP',
  88. 'version' => PHP_VERSION,
  89. 'vendor' => 'the PHP Group',
  90. 'upstreamSupported' => '7.1.0',
  91. 'minSupported' => '7.2.9',
  92. 'upgradeURL' => 'https://www.php.net/downloads.php',
  93. );
  94. }
  95. /**
  96. * Displays an error, if the installed PHP version does not meet the minimum requirement.
  97. */
  98. function checkRequiredPHPVersion() {
  99. $phpInfo = $this->getPHPInfo();
  100. $minimumVersion = $phpInfo['minSupported'];
  101. if ( version_compare( $phpInfo['version'], $minimumVersion ) < 0 ) {
  102. $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
  103. . " version $minimumVersion, you are using {$phpInfo['implementation']} "
  104. . "{$phpInfo['version']}.";
  105. $longText = "Error: You might be using an older {$phpInfo['implementation']} version "
  106. . "({$phpInfo['implementation']} {$phpInfo['version']}). \n"
  107. . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
  108. . " $minimumVersion or higher.\n\nCheck if you have a"
  109. . " newer PHP executable with a different name.\n\n";
  110. // phpcs:disable Generic.Files.LineLength
  111. $longHtml = <<<HTML
  112. Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
  113. {$phpInfo['implementation']}</a>.
  114. {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
  115. longer supported by {$phpInfo['vendor']} and will not receive
  116. security or bugfix updates.
  117. </p>
  118. <p>
  119. If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
  120. you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
  121. older version of MediaWiki from our website.
  122. See our <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
  123. for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
  124. HTML;
  125. // phpcs:enable Generic.Files.LineLength
  126. $this->triggerError(
  127. "Supported {$phpInfo['implementation']} versions",
  128. $shortText,
  129. $longText,
  130. $longHtml
  131. );
  132. }
  133. }
  134. /**
  135. * Displays an error, if the vendor/autoload.php file could not be found.
  136. */
  137. function checkVendorExistence() {
  138. if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
  139. $shortText = "Installing some external dependencies (e.g. via composer) is required.";
  140. $longText = "Error: You are missing some external dependencies. \n"
  141. . "MediaWiki now also has some external dependencies that need to be installed\n"
  142. . "via composer or from a separate git repo. Please see\n"
  143. . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
  144. . "for help on installing the required components.";
  145. // phpcs:disable Generic.Files.LineLength
  146. $longHtml = <<<HTML
  147. MediaWiki now also has some external dependencies that need to be installed via
  148. composer or from a separate git repo. Please see
  149. <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
  150. for help on installing the required components.
  151. HTML;
  152. // phpcs:enable Generic.Files.LineLength
  153. $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
  154. }
  155. }
  156. /**
  157. * Displays an error, if a PHP extension does not exist.
  158. */
  159. function checkExtensionExistence() {
  160. $missingExtensions = array();
  161. foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
  162. if ( !function_exists( $function ) ) {
  163. $missingExtensions[] = $extension;
  164. }
  165. }
  166. if ( $missingExtensions ) {
  167. $shortText = "Installing some PHP extensions is required.";
  168. $missingExtText = '';
  169. $missingExtHtml = '';
  170. $baseUrl = 'https://www.php.net';
  171. foreach ( $missingExtensions as $ext ) {
  172. $missingExtText .= " * $ext <$baseUrl/$ext>\n";
  173. $missingExtHtml .= "<li><b>$ext</b> "
  174. . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
  175. }
  176. $cliText = "Error: Missing one or more required components of PHP.\n"
  177. . "You are missing a required extension to PHP that MediaWiki needs.\n"
  178. . "Please install:\n" . $missingExtText;
  179. $longHtml = <<<HTML
  180. You are missing a required extension to PHP that MediaWiki
  181. requires to run. Please install:
  182. <ul>
  183. $missingExtHtml
  184. </ul>
  185. HTML;
  186. $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
  187. }
  188. }
  189. /**
  190. * Output headers that prevents error pages to be cached.
  191. */
  192. function outputHTMLHeader() {
  193. $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
  194. header( "$protocol 500 MediaWiki configuration Error" );
  195. // Don't cache error pages! They cause no end of trouble...
  196. header( 'Cache-control: none' );
  197. header( 'Pragma: no-cache' );
  198. }
  199. /**
  200. * Returns an error page, which is suitable for output to the end user via a web browser.
  201. *
  202. * @param string $title
  203. * @param string $longHtml
  204. * @param string $shortText
  205. * @return string
  206. */
  207. function getIndexErrorOutput( $title, $longHtml, $shortText ) {
  208. $encLogo =
  209. htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
  210. 'resources/assets/mediawiki.png' );
  211. $shortHtml = htmlspecialchars( $shortText );
  212. header( 'Content-type: text/html; charset=UTF-8' );
  213. $finalOutput = <<<HTML
  214. <!DOCTYPE html>
  215. <html lang="en" dir="ltr">
  216. <head>
  217. <meta charset="UTF-8" />
  218. <title>MediaWiki {$this->mwVersion}</title>
  219. <style media="screen">
  220. body {
  221. color: #000;
  222. background-color: #fff;
  223. font-family: sans-serif;
  224. padding: 2em;
  225. text-align: center;
  226. }
  227. p, img, h1, h2, ul {
  228. text-align: left;
  229. margin: 0.5em 0 1em;
  230. }
  231. h1 {
  232. font-size: 120%;
  233. }
  234. h2 {
  235. font-size: 110%;
  236. }
  237. </style>
  238. </head>
  239. <body>
  240. <img src="{$encLogo}" alt="The MediaWiki logo" />
  241. <h1>MediaWiki {$this->mwVersion} internal error</h1>
  242. <div class="error">
  243. <p>
  244. {$shortHtml}
  245. </p>
  246. <h2>{$title}</h2>
  247. <p>
  248. {$longHtml}
  249. </p>
  250. </div>
  251. </body>
  252. </html>
  253. HTML;
  254. return $finalOutput;
  255. }
  256. /**
  257. * Display something vaguely comprehensible in the event of a totally unrecoverable error.
  258. * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
  259. * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
  260. * no longer need to be).
  261. *
  262. * Calling this function kills execution immediately.
  263. *
  264. * @param string $title HTML code to be put within an <h2> tag
  265. * @param string $shortText
  266. * @param string $longText
  267. * @param string $longHtml
  268. */
  269. function triggerError( $title, $shortText, $longText, $longHtml ) {
  270. if ( $this->format === 'html' ) {
  271. // Used by index.php and mw-config/index.php
  272. $this->outputHTMLHeader();
  273. $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
  274. } else {
  275. // Used by Maintenance.php (CLI)
  276. $finalOutput = $longText;
  277. }
  278. echo "$finalOutput\n";
  279. die( 1 );
  280. }
  281. }
  282. /**
  283. * Check PHP version and that external dependencies are installed, and
  284. * display an informative error if either condition is not satisfied.
  285. *
  286. * @param string $format One of "text" or "html"
  287. * @param string $scriptPath Used when an error is formatted as HTML.
  288. */
  289. function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
  290. $phpVersionCheck = new PHPVersionCheck();
  291. $phpVersionCheck->setFormat( $format );
  292. $phpVersionCheck->setScriptPath( $scriptPath );
  293. $phpVersionCheck->checkRequiredPHPVersion();
  294. $phpVersionCheck->checkVendorExistence();
  295. $phpVersionCheck->checkExtensionExistence();
  296. }