Add config for serving main Page from the domain root
[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 PHPVersionCheck {
36 /* @var string The number of the MediaWiki version used. */
37 var $mwVersion = '1.34';
38
39 /* @var array A mapping of PHP functions to PHP extensions. */
40 var $functionsExtensionsMapping = array(
41 'mb_substr' => 'mbstring',
42 'xml_parser_create' => 'xml',
43 'ctype_digit' => 'ctype',
44 'json_decode' => 'json',
45 'iconv' => 'iconv',
46 'mime_content_type' => 'fileinfo',
47 );
48
49 /**
50 * @var string $format The format used for errors. One of "text" or "html"
51 */
52 var $format = 'text';
53
54 /**
55 * @var string $scriptPath
56 */
57 var $scriptPath = '/';
58
59 /**
60 * Set the format used for errors.
61 *
62 * @param string $format One of "text" or "html"
63 */
64 function setFormat( $format ) {
65 $this->format = $format;
66 }
67
68 /**
69 * Set the script path used for images in HTML-formatted errors.
70 *
71 * @param string $scriptPath
72 */
73 function setScriptPath( $scriptPath ) {
74 $this->scriptPath = $scriptPath;
75 }
76
77 /**
78 * Return the version of the installed PHP implementation.
79 *
80 * @param string $impl By default, the function returns the info of the currently installed PHP
81 * implementation. Using this parameter the caller can decide, what version info will be
82 * returned. Valid values: HHVM, PHP
83 * @return array An array of information about the PHP implementation, containing:
84 * - 'version': The version of the PHP implementation (specific to the implementation, not
85 * the version of the implemented PHP version)
86 * - 'implementation': The name of the implementation used
87 * - 'vendor': The development group, vendor or developer of the implementation.
88 * - 'upstreamSupported': The minimum version of the implementation supported by the named vendor.
89 * - 'minSupported': The minimum version supported by MediaWiki
90 * - 'upgradeURL': The URL to the website of the implementation that contains
91 * upgrade/installation instructions.
92 */
93 function getPHPInfo( $impl = false ) {
94 if (
95 ( defined( 'HHVM_VERSION' ) && $impl !== 'PHP' ) ||
96 $impl === 'HHVM'
97 ) {
98 return array(
99 'implementation' => 'HHVM',
100 'version' => defined( 'HHVM_VERSION' ) ? HHVM_VERSION : 'undefined',
101 'vendor' => 'Facebook',
102 'upstreamSupported' => '3.18.5',
103 'minSupported' => '3.18.5',
104 'upgradeURL' => 'https://docs.hhvm.com/hhvm/installation/introduction',
105 );
106 }
107 return array(
108 'implementation' => 'PHP',
109 'version' => PHP_VERSION,
110 'vendor' => 'the PHP Group',
111 'upstreamSupported' => '5.6.0',
112 'minSupported' => '7.0.13',
113 'upgradeURL' => 'https://www.php.net/downloads.php',
114 );
115 }
116
117 /**
118 * Displays an error, if the installed PHP version does not meet the minimum requirement.
119 */
120 function checkRequiredPHPVersion() {
121 $phpInfo = $this->getPHPInfo();
122 $minimumVersion = $phpInfo['minSupported'];
123 $otherInfo = $this->getPHPInfo( $phpInfo['implementation'] === 'HHVM' ? 'PHP' : 'HHVM' );
124 if ( version_compare( $phpInfo['version'], $minimumVersion ) < 0 ) {
125 $shortText = "MediaWiki $this->mwVersion requires at least {$phpInfo['implementation']}"
126 . " version $minimumVersion or {$otherInfo['implementation']} version "
127 . "{$otherInfo['minSupported']}, you are using {$phpInfo['implementation']} "
128 . "{$phpInfo['version']}.";
129
130 $longText = "Error: You might be using an older {$phpInfo['implementation']} version "
131 . "({$phpInfo['implementation']} {$phpInfo['version']}). \n"
132 . "MediaWiki $this->mwVersion needs {$phpInfo['implementation']}"
133 . " $minimumVersion or higher or {$otherInfo['implementation']} version "
134 . "{$otherInfo['minSupported']}.\n\nCheck if you have a"
135 . " newer PHP executable with a different name.\n\n";
136
137 // phpcs:disable Generic.Files.LineLength
138 $longHtml = <<<HTML
139 Please consider <a href="{$phpInfo['upgradeURL']}">upgrading your copy of
140 {$phpInfo['implementation']}</a>.
141 {$phpInfo['implementation']} versions less than {$phpInfo['upstreamSupported']} are no
142 longer supported by {$phpInfo['vendor']} and will not receive
143 security or bugfix updates.
144 </p>
145 <p>
146 If for some reason you are unable to upgrade your {$phpInfo['implementation']} version,
147 you will need to <a href="https://www.mediawiki.org/wiki/Download">download</a> an
148 older version of MediaWiki from our website.
149 See our <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
150 for details of which versions are compatible with prior versions of {$phpInfo['implementation']}.
151 HTML;
152 // phpcs:enable Generic.Files.LineLength
153 $this->triggerError(
154 "Supported {$phpInfo['implementation']} versions",
155 $shortText,
156 $longText,
157 $longHtml
158 );
159 }
160 }
161
162 /**
163 * Displays an error, if the vendor/autoload.php file could not be found.
164 */
165 function checkVendorExistence() {
166 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
167 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
168
169 $longText = "Error: You are missing some external dependencies. \n"
170 . "MediaWiki now also has some external dependencies that need to be installed\n"
171 . "via composer or from a separate git repo. Please see\n"
172 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
173 . "for help on installing the required components.";
174
175 // phpcs:disable Generic.Files.LineLength
176 $longHtml = <<<HTML
177 MediaWiki now also has some external dependencies that need to be installed via
178 composer or from a separate git repo. Please see
179 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
180 for help on installing the required components.
181 HTML;
182 // phpcs:enable Generic.Files.LineLength
183
184 $this->triggerError( 'External dependencies', $shortText, $longText, $longHtml );
185 }
186 }
187
188 /**
189 * Displays an error, if a PHP extension does not exist.
190 */
191 function checkExtensionExistence() {
192 $missingExtensions = array();
193 foreach ( $this->functionsExtensionsMapping as $function => $extension ) {
194 if ( !function_exists( $function ) ) {
195 $missingExtensions[] = $extension;
196 }
197 }
198
199 if ( $missingExtensions ) {
200 $shortText = "Installing some PHP extensions is required.";
201
202 $missingExtText = '';
203 $missingExtHtml = '';
204 $baseUrl = 'https://www.php.net';
205 foreach ( $missingExtensions as $ext ) {
206 $missingExtText .= " * $ext <$baseUrl/$ext>\n";
207 $missingExtHtml .= "<li><b>$ext</b> "
208 . "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
209 }
210
211 $cliText = "Error: Missing one or more required components of PHP.\n"
212 . "You are missing a required extension to PHP that MediaWiki needs.\n"
213 . "Please install:\n" . $missingExtText;
214
215 $longHtml = <<<HTML
216 You are missing a required extension to PHP that MediaWiki
217 requires to run. Please install:
218 <ul>
219 $missingExtHtml
220 </ul>
221 HTML;
222
223 $this->triggerError( 'Required components', $shortText, $cliText, $longHtml );
224 }
225 }
226
227 /**
228 * Output headers that prevents error pages to be cached.
229 */
230 function outputHTMLHeader() {
231 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
232
233 header( "$protocol 500 MediaWiki configuration Error" );
234 // Don't cache error pages! They cause no end of trouble...
235 header( 'Cache-control: none' );
236 header( 'Pragma: no-cache' );
237 }
238
239 /**
240 * Returns an error page, which is suitable for output to the end user via a web browser.
241 *
242 * @param string $title
243 * @param string $longHtml
244 * @param string $shortText
245 * @return string
246 */
247 function getIndexErrorOutput( $title, $longHtml, $shortText ) {
248 $encLogo =
249 htmlspecialchars( str_replace( '//', '/', $this->scriptPath . '/' ) .
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 if ( $this->format === 'html' ) {
315 // Used by index.php and mw-config/index.php
316 $this->outputHTMLHeader();
317 $finalOutput = $this->getIndexErrorOutput( $title, $longHtml, $shortText );
318 } else {
319 // Used by Maintenance.php (CLI)
320 $finalOutput = $longText;
321 }
322
323 echo "$finalOutput\n";
324 die( 1 );
325 }
326 }
327
328 /**
329 * Check PHP version and that external dependencies are installed, and
330 * display an informative error if either condition is not satisfied.
331 *
332 * @param string $format One of "text" or "html"
333 * @param string $scriptPath Used when an error is formatted as HTML.
334 */
335 function wfEntryPointCheck( $format = 'text', $scriptPath = '/' ) {
336 $phpVersionCheck = new PHPVersionCheck();
337 $phpVersionCheck->setFormat( $format );
338 $phpVersionCheck->setScriptPath( $scriptPath );
339 $phpVersionCheck->checkRequiredPHPVersion();
340 $phpVersionCheck->checkVendorExistence();
341 $phpVersionCheck->checkExtensionExistence();
342 }