Http::getProxy() method to get proxy configuration
[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 * @note Since we can't rely on anything, the minimum PHP versions and MW current
30 * version are hardcoded here
31 */
32 function wfEntryPointCheck( $entryPoint ) {
33 $mwVersion = '1.27';
34 $minimumVersionPHP = '5.3.3';
35 $phpVersion = PHP_VERSION;
36
37 if ( !function_exists( 'version_compare' )
38 || version_compare( $phpVersion, $minimumVersionPHP ) < 0
39 ) {
40 wfPHPVersionError( $entryPoint, $mwVersion, $minimumVersionPHP, $phpVersion );
41 }
42
43 if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
44 wfMissingVendorError( $entryPoint, $mwVersion );
45 }
46 }
47
48 /**
49 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
50 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
51 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
52 * no longer need to be).
53 *
54 * Calling this function kills execution immediately.
55 *
56 * @param string $type Which entry point we are protecting. One of:
57 * - index.php
58 * - load.php
59 * - api.php
60 * - mw-config/index.php
61 * - cli
62 * @param string $mwVersion The number of the MediaWiki version used
63 * @param string $title HTML code to be put within an <h2> tag
64 * @param string $shortText
65 * @param string $longText
66 * @param string $longHtml
67 */
68 function wfGenericError( $type, $mwVersion, $title, $shortText, $longText, $longHtml ) {
69 $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
70
71 if ( $type == 'cli' ) {
72 $finalOutput = $longText;
73 } else {
74 header( "$protocol 500 MediaWiki configuration Error" );
75 // Don't cache error pages! They cause no end of trouble...
76 header( 'Cache-control: none' );
77 header( 'Pragma: no-cache' );
78
79 if ( $type == 'index.php' || $type == 'mw-config/index.php' ) {
80 $pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
81 if ( $type == 'mw-config/index.php' ) {
82 $dirname = dirname( $pathinfo['dirname'] );
83 } else {
84 $dirname = $pathinfo['dirname'];
85 }
86 $encLogo = htmlspecialchars(
87 str_replace( '//', '/', $dirname . '/' ) .
88 'resources/assets/mediawiki.png'
89 );
90 $shortHtml = htmlspecialchars( $shortText );
91
92 header( 'Content-type: text/html; charset=UTF-8' );
93
94 $finalOutput = <<<HTML
95 <!DOCTYPE html>
96 <html lang="en" dir="ltr">
97 <head>
98 <meta charset="UTF-8" />
99 <title>MediaWiki {$mwVersion}</title>
100 <style media='screen'>
101 body {
102 color: #000;
103 background-color: #fff;
104 font-family: sans-serif;
105 padding: 2em;
106 text-align: center;
107 }
108 p, img, h1, h2 {
109 text-align: left;
110 margin: 0.5em 0 1em;
111 }
112 h1 {
113 font-size: 120%;
114 }
115 h2 {
116 font-size: 110%;
117 }
118 </style>
119 </head>
120 <body>
121 <img src="{$encLogo}" alt='The MediaWiki logo' />
122 <h1>MediaWiki {$mwVersion} internal error</h1>
123 <div class='error'>
124 <p>
125 {$shortHtml}
126 </p>
127 <h2>{$title}</h2>
128 <p>
129 {$longHtml}
130 </p>
131 </div>
132 </body>
133 </html>
134 HTML;
135 // Handle everything that's not index.php
136 } else {
137 // So nothing thinks this is JS or CSS
138 $finalOutput = ( $type == 'load.php' ) ? "/* $shortText */" : $shortText;
139 }
140 }
141 echo "$finalOutput\n";
142 die( 1 );
143 }
144
145 /**
146 * Display an error for the minimum PHP version requirement not being satisfied.
147 *
148 * @param string $type See wfGenericError
149 * @param string $mwVersion See wfGenericError
150 * @param string $minimumVersionPHP The minimum PHP version supported by MediaWiki
151 * @param string $phpVersion The current PHP version
152 */
153 function wfPHPVersionError( $type, $mwVersion, $minimumVersionPHP, $phpVersion ) {
154 $shortText = "MediaWiki $mwVersion requires at least "
155 . "PHP version $minimumVersionPHP, you are using PHP $phpVersion.";
156
157 $longText = "Error: You might be using on older PHP version. \n"
158 . "MediaWiki $mwVersion needs PHP $minimumVersionPHP or higher.\n\n"
159 . "Check if you have a newer php executable with a different name, such as php5.\n\n";
160
161 $longHtml = <<<HTML
162 Please consider <a href="http://www.php.net/downloads.php">upgrading your copy of PHP</a>.
163 PHP versions less than 5.3.0 are no longer supported by the PHP Group and will not receive
164 security or bugfix updates.
165 </p>
166 <p>
167 If for some reason you are unable to upgrade your PHP version, you will need to
168 <a href="https://www.mediawiki.org/wiki/Download">download</a> an older version
169 of MediaWiki from our website. See our
170 <a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
171 for details of which versions are compatible with prior versions of PHP.
172 HTML;
173 wfGenericError( $type, $mwVersion, 'Supported PHP versions', $shortText, $longText, $longHtml );
174 }
175
176 /**
177 * Display an error for the vendor/autoload.php file not being found.
178 *
179 * @param string $type See wfGenericError
180 * @param string $mwVersion See wfGenericError
181 */
182 function wfMissingVendorError( $type, $mwVersion ) {
183 $shortText = "Installing some external dependencies (e.g. via composer) is required.";
184
185 $longText = "Error: You are missing some external dependencies. \n"
186 . "MediaWiki now also has some external dependencies that need to be installed\n"
187 . "via composer or from a separate git repo. Please see\n"
188 . "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
189 . "for help on installing the required components.";
190
191 // @codingStandardsIgnoreStart Generic.Files.LineLength
192 $longHtml = <<<HTML
193 MediaWiki now also has some external dependencies that need to be installed via
194 composer or from a separate git repo. Please see
195 <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
196 for help on installing the required components.
197 HTML;
198 // @codingStandardsIgnoreEnd
199
200 wfGenericError( $type, $mwVersion, 'External dependencies', $shortText, $longText, $longHtml );
201 }