Merge "Make getLagFromPtHeartbeat() always use the LB cluster master entry"
[lhc/web/wiklou.git] / includes / WebStart.php
1 <?php
2 /**
3 * This does the initial set up for a web request.
4 * It does some security checks, starts the profiler and loads the
5 * configuration, and optionally loads Setup.php depending on whether
6 * MW_NO_SETUP is defined.
7 *
8 * Setup.php (if loaded) then sets up GlobalFunctions, the AutoLoader,
9 * and the configuration globals.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 # Die if register_globals is enabled (PHP <=5.3)
30 # This must be done before any globals are set by the code
31 if ( ini_get( 'register_globals' ) ) {
32 die( 'MediaWiki does not support installations where register_globals is enabled. Please see '
33 . '<a href="https://www.mediawiki.org/wiki/register_globals">mediawiki.org</a> '
34 . 'for help on how to disable it.' );
35 }
36
37 if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
38 die( 'MediaWiki does not function when magic quotes are enabled. Please see the '
39 . '<a href="https://php.net/manual/security.magicquotes.disabling.php">PHP Manual</a> '
40 . 'for help on how to disable magic quotes.' );
41 }
42
43
44 # bug 15461: Make IE8 turn off content sniffing. Everybody else should ignore this
45 # We're adding it here so that it's *always* set, even for alternate entry
46 # points and when $wgOut gets disabled or overridden.
47 header( 'X-Content-Type-Options: nosniff' );
48
49 # Approximate $_SERVER['REQUEST_TIME_FLOAT'] for PHP<5.4
50 if ( !isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ) {
51 $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
52 }
53
54 /**
55 * @var float Request start time as fractional seconds since epoch
56 * @deprecated since 1.25; use $_SERVER['REQUEST_TIME_FLOAT'] or
57 * WebRequest::getElapsedTime() instead.
58 */
59 $wgRequestTime = $_SERVER['REQUEST_TIME_FLOAT'];
60
61 unset( $IP );
62
63 # Valid web server entry point, enable includes.
64 # Please don't move this line to includes/Defines.php. This line essentially
65 # defines a valid entry point. If you put it in includes/Defines.php, then
66 # any script that includes it becomes an entry point, thereby defeating
67 # its purpose.
68 define( 'MEDIAWIKI', true );
69
70 # Full path to working directory.
71 # Makes it possible to for example to have effective exclude path in apc.
72 # __DIR__ breaks symlinked includes, but realpath() returns false
73 # if we don't have permissions on parent directories.
74 $IP = getenv( 'MW_INSTALL_PATH' );
75 if ( $IP === false ) {
76 $IP = realpath( '.' ) ?: dirname( __DIR__ );
77 }
78
79 # Grab profiling functions
80 require_once "$IP/includes/profiler/ProfilerFunctions.php";
81
82 # Start the autoloader, so that extensions can derive classes from core files
83 require_once "$IP/includes/AutoLoader.php";
84
85 # Load up some global defines.
86 require_once "$IP/includes/Defines.php";
87
88 # Start the profiler
89 $wgProfiler = array();
90 if ( file_exists( "$IP/StartProfiler.php" ) ) {
91 require "$IP/StartProfiler.php";
92 }
93
94
95 # Load default settings
96 require_once "$IP/includes/DefaultSettings.php";
97
98 # Load global functions
99 require_once "$IP/includes/GlobalFunctions.php";
100
101 # Load composer's autoloader if present
102 if ( is_readable( "$IP/vendor/autoload.php" ) ) {
103 require_once "$IP/vendor/autoload.php";
104 }
105
106 # Assert that composer dependencies were successfully loaded
107 # Purposely no leading \ due to it breaking HHVM RepoAuthorative mode
108 # PHP works fine with both versions
109 # See https://github.com/facebook/hhvm/issues/5833
110 if ( !interface_exists( 'Psr\Log\LoggerInterface' ) ) {
111 $message = (
112 'MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging ' .
113 "library</a> to be present. This library is not embedded directly in MediaWiki's " .
114 "git repository and must be installed separately by the end user.\n\n" .
115 'Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git' .
116 '#Fetch_external_libraries">mediawiki.org</a> for help on installing ' .
117 'the required components.'
118 );
119 echo $message;
120 trigger_error( $message, E_USER_ERROR );
121 die( 1 );
122 }
123
124 if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
125 # Use a callback function to configure MediaWiki
126 call_user_func( MW_CONFIG_CALLBACK );
127 } else {
128 if ( !defined( 'MW_CONFIG_FILE' ) ) {
129 define( 'MW_CONFIG_FILE', "$IP/LocalSettings.php" );
130 }
131
132 # LocalSettings.php is the per site customization file. If it does not exist
133 # the wiki installer needs to be launched or the generated file uploaded to
134 # the root wiki directory. Give a hint, if it is not readable by the server.
135 if ( !is_readable( MW_CONFIG_FILE ) ) {
136 require_once "$IP/includes/NoLocalSettings.php";
137 die();
138 }
139
140 # Include site settings. $IP may be changed (hopefully before the AutoLoader is invoked)
141 require_once MW_CONFIG_FILE;
142 }
143
144
145 # Initialise output buffering
146 # Check that there is no previous output or previously set up buffers, because
147 # that would cause us to potentially mix gzip and non-gzip output, creating a
148 # big mess.
149 if ( ob_get_level() == 0 ) {
150 require_once "$IP/includes/OutputHandler.php";
151 ob_start( 'wfOutputHandler' );
152 }
153
154 if ( !defined( 'MW_NO_SETUP' ) ) {
155 require_once "$IP/includes/Setup.php";
156 }
157
158 # Multiple DBs or commits might be used; keep the request as transactional as possible
159 if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) {
160 ignore_user_abort( true );
161 }
162
163 if ( !defined( 'MW_API' ) &&
164 RequestContext::getMain()->getRequest()->getHeader( 'Promise-Non-Write-API-Action' )
165 ) {
166 header( 'Cache-Control: no-cache' );
167 header( 'Content-Type: text/html; charset=utf-8' );
168 HttpStatus::header( 400 );
169 $error = wfMessage( 'nonwrite-api-promise-error' )->escaped();
170 $content = <<<EOT
171 <!DOCTYPE html>
172 <html>
173 <head><meta charset="UTF-8" /></head>
174 <body>
175 $error
176 </body>
177 </html>
178
179 EOT;
180 header( 'Content-Length: ' . strlen( $content ) );
181 echo $content;
182 die();
183 }