Merge "database: Add extra sanity check to selectFieldValues()"
[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 # bug 15461: Make IE8 turn off content sniffing. Everybody else should ignore this
30 # We're adding it here so that it's *always* set, even for alternate entry
31 # points and when $wgOut gets disabled or overridden.
32 header( 'X-Content-Type-Options: nosniff' );
33
34 /**
35 * @var float Request start time as fractional seconds since epoch
36 * @deprecated since 1.25; use $_SERVER['REQUEST_TIME_FLOAT'] or
37 * WebRequest::getElapsedTime() instead.
38 */
39 $wgRequestTime = $_SERVER['REQUEST_TIME_FLOAT'];
40
41 unset( $IP );
42
43 # Valid web server entry point, enable includes.
44 # Please don't move this line to includes/Defines.php. This line essentially
45 # defines a valid entry point. If you put it in includes/Defines.php, then
46 # any script that includes it becomes an entry point, thereby defeating
47 # its purpose.
48 define( 'MEDIAWIKI', true );
49
50 # Full path to working directory.
51 # Makes it possible to for example to have effective exclude path in apc.
52 # __DIR__ breaks symlinked includes, but realpath() returns false
53 # if we don't have permissions on parent directories.
54 $IP = getenv( 'MW_INSTALL_PATH' );
55 if ( $IP === false ) {
56 $IP = realpath( '.' ) ?: dirname( __DIR__ );
57 }
58
59 # Grab profiling functions
60 require_once "$IP/includes/profiler/ProfilerFunctions.php";
61
62 # Start the autoloader, so that extensions can derive classes from core files
63 require_once "$IP/includes/AutoLoader.php";
64
65 # Load up some global defines.
66 require_once "$IP/includes/Defines.php";
67
68 # Start the profiler
69 $wgProfiler = [];
70 if ( file_exists( "$IP/StartProfiler.php" ) ) {
71 require "$IP/StartProfiler.php";
72 }
73
74 # Load default settings
75 require_once "$IP/includes/DefaultSettings.php";
76
77 # Load global functions
78 require_once "$IP/includes/GlobalFunctions.php";
79
80 # Load composer's autoloader if present
81 if ( is_readable( "$IP/vendor/autoload.php" ) ) {
82 require_once "$IP/vendor/autoload.php";
83 }
84
85 # Assert that composer dependencies were successfully loaded
86 # Purposely no leading \ due to it breaking HHVM RepoAuthorative mode
87 # PHP works fine with both versions
88 # See https://github.com/facebook/hhvm/issues/5833
89 if ( !interface_exists( 'Psr\Log\LoggerInterface' ) ) {
90 $message = (
91 'MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging ' .
92 "library</a> to be present. This library is not embedded directly in MediaWiki's " .
93 "git repository and must be installed separately by the end user.\n\n" .
94 'Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git' .
95 '#Fetch_external_libraries">mediawiki.org</a> for help on installing ' .
96 'the required components.'
97 );
98 echo $message;
99 trigger_error( $message, E_USER_ERROR );
100 die( 1 );
101 }
102
103 if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
104 # Use a callback function to configure MediaWiki
105 call_user_func( MW_CONFIG_CALLBACK );
106 } else {
107 if ( !defined( 'MW_CONFIG_FILE' ) ) {
108 define( 'MW_CONFIG_FILE', "$IP/LocalSettings.php" );
109 }
110
111 # LocalSettings.php is the per site customization file. If it does not exist
112 # the wiki installer needs to be launched or the generated file uploaded to
113 # the root wiki directory. Give a hint, if it is not readable by the server.
114 if ( !is_readable( MW_CONFIG_FILE ) ) {
115 require_once "$IP/includes/NoLocalSettings.php";
116 die();
117 }
118
119 # Include site settings. $IP may be changed (hopefully before the AutoLoader is invoked)
120 require_once MW_CONFIG_FILE;
121 }
122
123 # Initialise output buffering
124 # Check that there is no previous output or previously set up buffers, because
125 # that would cause us to potentially mix gzip and non-gzip output, creating a
126 # big mess.
127 if ( ob_get_level() == 0 ) {
128 require_once "$IP/includes/OutputHandler.php";
129 ob_start( 'wfOutputHandler' );
130 }
131
132 if ( !defined( 'MW_NO_SETUP' ) ) {
133 require_once "$IP/includes/Setup.php";
134 }
135
136 # Multiple DBs or commits might be used; keep the request as transactional as possible
137 if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) {
138 ignore_user_abort( true );
139 }
140
141 if ( !defined( 'MW_API' ) &&
142 RequestContext::getMain()->getRequest()->getHeader( 'Promise-Non-Write-API-Action' )
143 ) {
144 header( 'Cache-Control: no-cache' );
145 header( 'Content-Type: text/html; charset=utf-8' );
146 HttpStatus::header( 400 );
147 $error = wfMessage( 'nonwrite-api-promise-error' )->escaped();
148 $content = <<<EOT
149 <!DOCTYPE html>
150 <html>
151 <head><meta charset="UTF-8" /></head>
152 <body>
153 $error
154 </body>
155 </html>
156
157 EOT;
158 header( 'Content-Length: ' . strlen( $content ) );
159 echo $content;
160 die();
161 }