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