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