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