Completed
Branch master (246348)
by
unknown
22:34
created

PHPVersionCheck.php ➔ wfEntryPointCheck()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 24
nop 1
dl 0
loc 40
rs 6.7272
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.28';
34
	$minimumVersionPHP = '5.5.9';
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
	// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
44
	if ( !file_exists( dirname( __FILE__ ) . '/../vendor/autoload.php' ) ) {
45
		// @codingStandardsIgnoreEnd
46
		wfMissingVendorError( $entryPoint, $mwVersion );
47
	}
48
49
	// List of functions and their associated PHP extension to check for
50
	// @codingStandardsIgnoreStart Generic.Arrays.DisallowLongArraySyntax
51
	$extensions = array(
52
		'mb_substr'   => 'mbstring',
53
		'utf8_encode' => 'xml',
54
		'ctype_digit' => 'ctype',
55
		'json_decode' => 'json',
56
		'iconv'       => 'iconv',
57
	);
58
	// List of extensions we're missing
59
	$missingExtensions = array();
60
	// @codingStandardsIgnoreEnd
61
62
	foreach ( $extensions as $function => $extension ) {
63
		if ( !function_exists( $function ) ) {
64
			$missingExtensions[] = $extension;
65
		}
66
	}
67
68
	if ( $missingExtensions ) {
69
		wfMissingExtensions( $entryPoint, $mwVersion, $missingExtensions );
70
	}
71
}
72
73
/**
74
 * Display something vaguely comprehensible in the event of a totally unrecoverable error.
75
 * Does not assume access to *anything*; no globals, no autoloader, no database, no localisation.
76
 * Safe for PHP4 (and putting this here means that WebStart.php and GlobalSettings.php
77
 * no longer need to be).
78
 *
79
 * Calling this function kills execution immediately.
80
 *
81
 * @param string $type Which entry point we are protecting. One of:
82
 *   - index.php
83
 *   - load.php
84
 *   - api.php
85
 *   - mw-config/index.php
86
 *   - cli
87
 * @param string $mwVersion The number of the MediaWiki version used
88
 * @param string $title HTML code to be put within an <h2> tag
89
 * @param string $shortText
90
 * @param string $longText
91
 * @param string $longHtml
92
 */
93
function wfGenericError( $type, $mwVersion, $title, $shortText, $longText, $longHtml ) {
0 ignored issues
show
Coding Style introduced by
wfGenericError uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
94
	$protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
95
96
	if ( $type == 'cli' ) {
97
		$finalOutput = $longText;
98
	} else {
99
		header( "$protocol 500 MediaWiki configuration Error" );
100
		// Don't cache error pages!  They cause no end of trouble...
101
		header( 'Cache-control: none' );
102
		header( 'Pragma: no-cache' );
103
104
		if ( $type == 'index.php' || $type == 'mw-config/index.php' ) {
105
			$pathinfo = pathinfo( $_SERVER['SCRIPT_NAME'] );
106
			if ( $type == 'mw-config/index.php' ) {
107
				$dirname = dirname( $pathinfo['dirname'] );
108
			} else {
109
				$dirname = $pathinfo['dirname'];
110
			}
111
			$encLogo = htmlspecialchars(
112
				str_replace( '//', '/', $dirname . '/' ) .
113
				'resources/assets/mediawiki.png'
114
			);
115
			$shortHtml = htmlspecialchars( $shortText );
116
117
			header( 'Content-type: text/html; charset=UTF-8' );
118
119
			$finalOutput = <<<HTML
120
<!DOCTYPE html>
121
<html lang="en" dir="ltr">
122
	<head>
123
		<meta charset="UTF-8" />
124
		<title>MediaWiki {$mwVersion}</title>
125
		<style media='screen'>
126
			body {
127
				color: #000;
128
				background-color: #fff;
129
				font-family: sans-serif;
130
				padding: 2em;
131
				text-align: center;
132
			}
133
			p, img, h1, h2, ul  {
134
				text-align: left;
135
				margin: 0.5em 0 1em;
136
			}
137
			h1 {
138
				font-size: 120%;
139
			}
140
			h2 {
141
				font-size: 110%;
142
			}
143
		</style>
144
	</head>
145
	<body>
146
		<img src="{$encLogo}" alt='The MediaWiki logo' />
147
		<h1>MediaWiki {$mwVersion} internal error</h1>
148
		<div class='error'>
149
		<p>
150
			{$shortHtml}
151
		</p>
152
		<h2>{$title}</h2>
153
		<p>
154
			{$longHtml}
155
		</p>
156
		</div>
157
	</body>
158
</html>
159
HTML;
160
		// Handle everything that's not index.php
161
		} else {
162
			// So nothing thinks this is JS or CSS
163
			$finalOutput = ( $type == 'load.php' ) ? "/* $shortText */" : $shortText;
164
		}
165
	}
166
	echo "$finalOutput\n";
167
	die( 1 );
0 ignored issues
show
Coding Style Compatibility introduced by
The function wfGenericError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
168
}
169
170
/**
171
 * Display an error for the minimum PHP version requirement not being satisfied.
172
 *
173
 * @param string $type See wfGenericError
174
 * @param string $mwVersion See wfGenericError
175
 * @param string $minimumVersionPHP The minimum PHP version supported by MediaWiki
176
 * @param string $phpVersion The current PHP version
177
 */
178
function wfPHPVersionError( $type, $mwVersion, $minimumVersionPHP, $phpVersion ) {
179
	$shortText = "MediaWiki $mwVersion requires at least "
180
		. "PHP version $minimumVersionPHP, you are using PHP $phpVersion.";
181
182
	$longText = "Error: You might be using on older PHP version. \n"
183
		. "MediaWiki $mwVersion needs PHP $minimumVersionPHP or higher.\n\n"
184
		. "Check if you have a newer php executable with a different name, such as php5.\n\n";
185
186
	$longHtml = <<<HTML
187
			Please consider <a href="http://www.php.net/downloads.php">upgrading your copy of PHP</a>.
188
			PHP versions less than 5.5.0 are no longer supported by the PHP Group and will not receive
189
			security or bugfix updates.
190
		</p>
191
		<p>
192
			If for some reason you are unable to upgrade your PHP version, you will need to
193
			<a href="https://www.mediawiki.org/wiki/Download">download</a> an older version
194
			of MediaWiki from our website.  See our
195
			<a href="https://www.mediawiki.org/wiki/Compatibility#PHP">compatibility page</a>
196
			for details of which versions are compatible with prior versions of PHP.
197
HTML;
198
	wfGenericError( $type, $mwVersion, 'Supported PHP versions', $shortText, $longText, $longHtml );
199
}
200
201
/**
202
 * Display an error for the vendor/autoload.php file not being found.
203
 *
204
 * @param string $type See wfGenericError
205
 * @param string $mwVersion See wfGenericError
206
 */
207
function wfMissingVendorError( $type, $mwVersion ) {
208
	$shortText = "Installing some external dependencies (e.g. via composer) is required.";
209
210
	$longText = "Error: You are missing some external dependencies. \n"
211
		. "MediaWiki now also has some external dependencies that need to be installed\n"
212
		. "via composer or from a separate git repo. Please see\n"
213
		. "https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries\n"
214
		. "for help on installing the required components.";
215
216
	// @codingStandardsIgnoreStart Generic.Files.LineLength
217
	$longHtml = <<<HTML
218
		MediaWiki now also has some external dependencies that need to be installed via
219
		composer or from a separate git repo. Please see
220
		<a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a>
221
		for help on installing the required components.
222
HTML;
223
	// @codingStandardsIgnoreEnd
224
225
	wfGenericError( $type, $mwVersion, 'External dependencies', $shortText, $longText, $longHtml );
226
}
227
228
/**
229
 * Display an error for a PHP extension not existing.
230
 *
231
 * @param string $type See wfGenericError
232
 * @param string $mwVersion See wfGenericError
233
 * @param array $missingExts The extensions we're missing
234
 */
235
function wfMissingExtensions( $type, $mwVersion, $missingExts ) {
236
	$shortText = "Installing some PHP extensions is required.";
237
238
	$missingExtText = '';
239
	$missingExtHtml = '';
240
	$baseUrl = 'https://secure.php.net';
241
	foreach ( $missingExts as $ext ) {
242
		$missingExtText .= " * $ext <$baseUrl/$ext>\n";
243
		$missingExtHtml .= "<li><b>$ext</b> "
244
			. "(<a href=\"$baseUrl/$ext\">more information</a>)</li>";
245
	}
246
247
	$cliText = "Error: Missing one or more required components of PHP.\n"
248
		. "You are missing a required extension to PHP that MediaWiki needs.\n"
249
		. "Please install:\n" . $missingExtText;
250
251
	$longHtml = <<<HTML
252
		You are missing a required extension to PHP that MediaWiki
253
		requires to run. Please install:
254
		<ul>
255
		$missingExtHtml
256
		</ul>
257
HTML;
258
259
	wfGenericError( $type, $mwVersion, 'Required components', $shortText,
260
		$cliText, $longHtml );
261
}
262