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