Fix (MediaWiki)Site::normalizePageName return type
[lhc/web/wiklou.git] / includes / PHPVersionCheck.php
1 <?php
2 // phpcs:ignoreFile Generic.Arrays.DisallowLongArraySyntax,MediaWiki.Usage.DirUsage.FunctionFound
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
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 * @return $this
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 MediWiki
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' => '5.5.9',
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 * @return $this
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. \n"
122 . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
123 . " $minimumVersion or higher or {$otherInfo['implementation']} version "
124 . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
125 . " newer php executable with a different name, such as php5.\n\n";
126
127 // phpcs:ignore Generic.Files.LineLength
128 $longHtml = <<<HTML
129 Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
130 {$phpInfo['implementation']}</a>.
131 {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
132 longer supported by {$phpInfo['vendor']} and will not receive
133 security or bugfix updates.
134 </p>
135 <p>
136 If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
137 you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
138 older version of MediaWiki from our website.
139 See our<a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
140 for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
141 HTML;
142 // phpcs:enable
143 $this->triggerError(
144 "Supported {$phpInfo['implementation']} versions",
145 $shortText,
146 $longText,
147 $longHtml
148 );
149 }
150 }
151
152 /**
153 * Displays an error, if the vendor/autoload.php file could not be found.
154 *
155 * @return $this
156 */
157 function checkVendorExistence() {
158 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
159 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
160
161 $longText = "Error: You are missing some external dependencies. \n"
162 . "MediaWiki now also has some external dependencies that need to be installed\n"
163 . "via composer or from a separate git repo. Please see\n"
164 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
165 . "for help on installing the required components.";
166
167 // phpcs:ignore Generic.Files.LineLength
168 $longHtml = <<<HTML
169 MediaWiki now also has some external dependencies that need to be installed via
170 composer or from a separate git repo. Please see
171 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
172 for help on installing the required components.
173 HTML;
174 // phpcs:enable
175
176 $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
177 }
178 }
179
180 /**
181 * Displays an error, if a PHP extension does not exist.
182 *
183 * @return $this
184 */
185 function checkExtensionExistence() {
186 $missingExtensions = array();
187 foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
188 if ( !function_exists( $function ) ) {
189 $missingExtensions[] = $extension;
190 }
191 }
192
193 if ( $missingExtensions ) {
194 $shortText = "Installing some PHP extensions is required.";
195
196 $missingExtText = '';
197 $missingExtHtml = '';
198 $baseUrl = 'https://secure.php.net';
199 foreach ( $missingExtensions as $ext ) {
200 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
201 $missingExtHtml .= "<li><b>$ext</b> "
202 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
203 }
204
205 $cliText = "Error: Missing one or more required components of PHP.\n"
206 . "You are missing a required extension to PHP that MediaWiki needs.\n"
207 . "Please install:\n" . $missingExtText;
208
209 $longHtml = <<<HTML
210 You are missing a required extension to PHP that MediaWiki
211 requires to run. Please install:
212 <ul>
213 $missingExtHtml
214 </ul>
215 HTML;
216
217 $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
218 }
219 }
220
221 /**
222 * Output headers that prevents error pages to be cached.
223 */
224 function outputHTMLHeader() {
225 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
226
227 header( "$protocol 500 MediaWiki configuration Error" );
228 // Don't cache error pages! They cause no end of trouble...
229 header( 'Cache-control: none' );
230 header( 'Pragma: no-cache' );
231 }
232
233 /**
234 * Returns an error page, which is suitable for output to the end user via a web browser.
235 *
236 * @param string $title
237 * @param string $longHtml
238 * @param string $shortText
239 * @return string
240 */
241 function getIndexErrorOutput( $title, $longHtml, $shortText ) {
242 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
243 if ( $this->entryPoint == 'mw-config/index.php' ) {
244 $dirname = dirname( $pathinfo['dirname'] );
245 } else {
246 $dirname = $pathinfo['dirname'];
247 }
248 $encLogo =
249 htmlspecialchars( str_replace( '//', '/', $dirname . '/' ) .
250 'resources/assets/mediawiki.png' );
251 $shortHtml = htmlspecialchars( $shortText );
252
253 header( 'Content-type: text/html; charset=UTF-8' );
254
255 $finalOutput = <<<HTML
256 <!DOCTYPE html>
257 <html lang="en" dir="ltr">
258 <head>
259 <meta charset="UTF-8" />
260 <title>MediaWiki {$this->mwVersion}</title>
261 <style media='screen'>
262 body {
263 color: #000;
264 background-color: #fff;
265 font-family: sans-serif;
266 padding: 2em;
267 text-align: center;
268 }
269 p, img, h1, h2, ul {
270 text-align: left;
271 margin: 0.5em 0 1em;
272 }
273 h1 {
274 font-size: 120%;
275 }
276 h2 {
277 font-size: 110%;
278 }
279 </style>
280 </head>
281 <body>
282 <img src="{$encLogo}" alt='The MediaWiki logo' />
283 <h1>MediaWiki {$this->mwVersion} internal error</h1>
284 <div class='error'>
285 <p>
286 {$shortHtml}
287 </p>
288 <h2>{$title}</h2>
289 <p>
290 {$longHtml}
291 </p>
292 </div>
293 </body>
294 </html>
295 HTML;
296
297 return $finalOutput;
298 }
299
300 /**
301 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
302 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
303 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
304 * no longer need to be).
305 *
306 * Calling this function kills execution immediately.
307 *
308 * @param string $title HTML code to be put within an <h2> tag
309 * @param string $shortText
310 * @param string $longText
311 * @param string $longHtml
312 */
313 function triggerError( $title, $shortText, $longText, $longHtml ) {
314 switch ( $this->entryPoint ) {
315 case 'cli':
316 $finalOutput = $longText;
317 break;
318 case 'index.php':
319 case 'mw-config/index.php':
320 $this->outputHTMLHeader();
321 $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
322 break;
323 case 'load.php':
324 $this->outputHTMLHeader();
325 $finalOutput = "/* $shortText */";
326 break;
327 default:
328 $this->outputHTMLHeader();
329 // Handle everything that's not index.php
330 $finalOutput = $shortText;
331 }
332
333 echo "$finalOutput\n";
334 die( 1 );
335 }
336 }
337
338 /**
339 * Check php version and that external dependencies are installed, and
340 * display an informative error if either condition is not satisfied.
341 *
342 * @note Since we can't rely on anything, the minimum PHP versions and MW current
343 * version are hardcoded here
344 */
345 function wfEntryPointCheck( $entryPoint ) {
346 $phpVersionCheck = new PHPVersionCheck();
347 $phpVersionCheck->setEntryPoint( $entryPoint );
348 $phpVersionCheck->checkRequiredPHPVersion();
349 $phpVersionCheck->checkVendorExistence();
350 $phpVersionCheck->checkExtensionExistence();
351 }