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