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