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