Merge "Consistency tweaks: xml -> XML, Href -> href"
[lhc/web/wiklou.git] / includes / PHPVersionCheck.php
1 <?php
2 /**
3 * Check PHP Version, as well as for composer dependencies in entry points,
4 * and display something vaguely comprehensible in the event of a totally
5 * unrecoverable error.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /**
26 * Check php version and that external dependencies are installed, and
27 * display an informative error if either condition is not satisfied.
28 */
29 function wfEntryPointCheck( $entryPoint ) {
30 if ( !function_exists( 'version_compare' )
31 || version_compare( PHP_VERSION, '5.3.3' ) < 0
32 || !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' )
33 ) {
34 wfPHPVersionError( $entryPoint );
35 }
36 }
37
38 /**
39 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
40 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
41 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
42 * no longer need to be).
43 *
44 * Calling this function kills execution immediately.
45 *
46 * @param string $type Which entry point we are protecting. One of:
47 * - index.php
48 * - load.php
49 * - api.php
50 * - mw-config/index.php
51 * - cli
52 *
53 * @note Since we can't rely on anything, the minimum PHP versions and MW current
54 * version are hardcoded here
55 */
56 function wfPHPVersionError( $type ) {
57 $mwVersion = '1.26';
58 $minimumVersionPHP = '5.3.3';
59
60 $phpVersion = PHP_VERSION;
61 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
62 $message = "MediaWiki $mwVersion requires at least "
63 . "PHP version $minimumVersionPHP, you are using PHP $phpVersion. Installing some "
64 . " external dependencies (e.g. via composer) is also required.";
65
66 if ( $type == 'cli' ) {
67 $finalOutput = "Error: You are missing some external dependencies or are using on older PHP version. \n"
68 . "MediaWiki $mwVersion needs PHP $minimumVersionPHP or higher.\n\n"
69 . "Check if you have a newer php executable with a different name, such as php5.\n\n"
70 . "MediaWiki now also has some external dependencies that need to be installed\n"
71 . "via composer or from a separate git repo. Please see\n"
72 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
73 . "for help on installing the required components.";
74 } elseif ( $type == 'index.php' || $type == 'mw-config/index.php' ) {
75 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
76 if ( $type == 'mw-config/index.php' ) {
77 $dirname = dirname( $pathinfo['dirname'] );
78 } else {
79 $dirname = $pathinfo['dirname'];
80 }
81 $encLogo = htmlspecialchars(
82 str_replace( '//', '/', $dirname . '/' ) .
83 'resources/assets/mediawiki.png'
84 );
85
86 header( "$protocol 500 MediaWiki configuration Error" );
87 header( 'Content-type: text/html; charset=UTF-8' );
88 // Don't cache error pages! They cause no end of trouble...
89 header( 'Cache-control: none' );
90 header( 'Pragma: no-cache' );
91
92 $finalOutput = <<<HTML
93 <!DOCTYPE html>
94 <html lang="en" dir="ltr">
95 <head>
96 <meta charset="UTF-8" />
97 <title>MediaWiki {$mwVersion}</title>
98 <style media='screen'>
99 body {
100 color: #000;
101 background-color: #fff;
102 font-family: sans-serif;
103 padding: 2em;
104 text-align: center;
105 }
106 p, img, h1, h2 {
107 text-align: left;
108 margin: 0.5em 0 1em;
109 }
110 h1 {
111 font-size: 120%;
112 }
113 h2 {
114 font-size: 110%;
115 }
116 </style>
117 </head>
118 <body>
119 <img src="{$encLogo}" alt='The MediaWiki logo' />
120 <h1>MediaWiki {$mwVersion} internal error</h1>
121 <div class='error'>
122 <p>
123 {$message}
124 </p>
125 <h2>Supported PHP versions</h2>
126 <p>
127 Please consider <a href="http://www.php.net/downloads.php">upgrading your copy of PHP</a>.
128 PHP versions less than 5.3.0 are no longer supported by the PHP Group and will not receive
129 security or bugfix updates.
130 </p>
131 <p>
132 If for some reason you are unable to upgrade your PHP version, you will need to
133 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version
134 of MediaWiki from our website. See our
135 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
136 for details of which versions are compatible with prior versions of PHP.
137 </p>
138 <h2>External dependencies</h2>
139 <p>
140 MediaWiki now also has some external dependencies that need to be installed via
141 composer or from a separate git repo. Please see
142 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
143 for help on installing the required components.
144 </p>
145 </div>
146 </body>
147 </html>
148 HTML;
149 // Handle everything that's not index.php
150 } else {
151 // So nothing thinks this is JS or CSS
152 $finalOutput = ( $type == 'load.php' ) ? "/* $message */" : $message;
153 header( "$protocol 500 MediaWiki configuration Error" );
154 }
155 echo "$finalOutput\n";
156 die( 1 );
157 }