fixed potential XSS vulnerability
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 /**
3 * Include most things that's need to customize the site
4 * @package MediaWiki
5 */
6
7 /**
8 * This file is not a valid entry point, perform no further processing unless
9 * MEDIAWIKI is defined
10 */
11 if( defined( 'MEDIAWIKI' ) ) {
12
13 # The main wiki script and things like database
14 # conversion and maintenance scripts all share a
15 # common setup of including lots of classes and
16 # setting up a few globals.
17 #
18
19 // Check to see if we are at the file scope
20 if ( !isset( $wgVersion ) ) {
21 die( "Error, Setup.php must be included from the file scope, after DefaultSettings.php\n" );
22 }
23
24 if( !isset( $wgProfiling ) )
25 $wgProfiling = false;
26
27 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
28 require_once( 'Profiling.php' );
29 } else {
30 function wfProfileIn( $fn = '' ) {
31 global $hackwhere, $wgDBname;
32 $hackwhere[] = $fn;
33 if (function_exists("setproctitle"))
34 setproctitle($fn . " [$wgDBname]");
35 }
36 function wfProfileOut( $fn = '' ) {
37 global $hackwhere, $wgDBname;
38 if (count($hackwhere))
39 array_pop($hackwhere);
40 if (function_exists("setproctitle") && count($hackwhere))
41 setproctitle($hackwhere[count($hackwhere)-1] . " [$wgDBname]");
42 }
43 function wfGetProfilingOutput( $s, $e ) {}
44 function wfProfileClose() {}
45 }
46
47 $fname = 'Setup.php';
48 wfProfileIn( $fname );
49 wfProfileIn( $fname.'-includes' );
50
51 require_once( 'GlobalFunctions.php' );
52 require_once( 'Hooks.php' );
53 require_once( 'Namespace.php' );
54 require_once( 'RecentChange.php' );
55 require_once( 'User.php' );
56 require_once( 'Skin.php' );
57 require_once( 'OutputPage.php' );
58 require_once( 'LinkCache.php' );
59 require_once( 'Title.php' );
60 require_once( 'Article.php' );
61 require_once( 'MagicWord.php' );
62 require_once( 'Block.php' );
63 require_once( 'MessageCache.php' );
64 require_once( 'BlockCache.php' );
65 require_once( 'Parser.php' );
66 require_once( 'ParserCache.php' );
67 require_once( 'WebRequest.php' );
68 require_once( 'LoadBalancer.php' );
69 require_once( 'HistoryBlob.php' );
70 require_once( 'ProxyTools.php' );
71 require_once( 'ObjectCache.php' );
72 require_once( 'WikiError.php' );
73
74 if ( $wgUseDynamicDates ) {
75 require_once( 'DateFormatter.php' );
76 }
77
78 wfProfileOut( $fname.'-includes' );
79 wfProfileIn( $fname.'-misc1' );
80
81 $wgIP = wfGetIP();
82 $wgRequest = new WebRequest();
83
84 # Useful debug output
85 if ( $wgCommandLineMode ) {
86 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
87 } elseif ( function_exists( 'getallheaders' ) ) {
88 wfDebug( "\n\nStart request\n" );
89 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
90 $headers = getallheaders();
91 foreach ($headers as $name => $value) {
92 wfDebug( "$name: $value\n" );
93 }
94 wfDebug( "\n" );
95 } elseif( isset( $_SERVER['REQUEST_URI'] ) ) {
96 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
97 }
98
99 # Disable linkscc except if the old existence check method is enabled
100 if (!$wgUseOldExistenceCheck) {
101 $wgEnablePersistentLC = false;
102 }
103
104 wfProfileOut( $fname.'-misc1' );
105 wfProfileIn( $fname.'-memcached' );
106
107 $wgMemc =& wfGetMainCache();
108 $messageMemc =& wfGetMessageCacheStorage();
109 $parserMemc =& wfGetParserCacheStorage();
110
111 wfDebug( 'Main cache: ' . get_class( $wgMemc ) .
112 "\nMessage cache: " . get_class( $messageMemc ) .
113 "\nParser cache: " . get_class( $parserMemc ) . "\n" );
114
115 wfProfileOut( $fname.'-memcached' );
116 wfProfileIn( $fname.'-SetupSession' );
117
118 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Token'] ) ) ) {
119 User::SetupSession();
120 $wgSessionStarted = true;
121 } else {
122 $wgSessionStarted = false;
123 }
124
125 wfProfileOut( $fname.'-SetupSession' );
126 wfProfileIn( $fname.'-database' );
127
128 if ( !$wgDBservers ) {
129 $wgDBservers = array(array(
130 'host' => $wgDBserver,
131 'user' => $wgDBuser,
132 'password' => $wgDBpassword,
133 'dbname' => $wgDBname,
134 'type' => $wgDBtype,
135 'load' => 1,
136 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
137 ));
138 }
139 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
140 $wgLoadBalancer->loadMasterPos();
141
142 wfProfileOut( $fname.'-database' );
143 wfProfileIn( $fname.'-language1' );
144
145 require_once( "$IP/languages/Language.php" );
146
147 wfProfileOut( $fname.'-language1' );
148 wfProfileIn( $fname.'-User' );
149
150 # Skin setup functions
151 # Entries can be added to this variable during the inclusion
152 # of the extension file. Skins can then perform any necessary initialisation.
153 foreach ( $wgSkinExtensionFunctions as $func ) {
154 $func();
155 }
156
157 if( !is_object( $wgAuth ) ) {
158 require_once( 'AuthPlugin.php' );
159 $wgAuth = new AuthPlugin();
160 }
161
162 if( $wgCommandLineMode ) {
163 # Used for some maintenance scripts; user session cookies can screw things up
164 # when the database is in an in-between state.
165 $wgUser = new User();
166 # Prevent loading User settings from the DB.
167 $wgUser->setLoaded( true );
168 } else {
169 $wgUser = User::loadFromSession();
170 }
171
172 wfProfileOut( $fname.'-User' );
173 wfProfileIn( $fname.'-language2' );
174
175 function setupLangObj(&$langclass) {
176 global $IP;
177
178 if( ! class_exists( $langclass ) ) {
179 # Default to English/UTF-8
180 $baseclass = 'LanguageUtf8';
181 require_once( "$IP/languages/$baseclass.php" );
182 $lc = strtolower(substr($langclass, 8));
183 $snip = "
184 class $langclass extends $baseclass {
185 function getVariants() {
186 return array(\"$lc\");
187 }
188
189 }";
190 eval($snip);
191 }
192
193 $lang = new $langclass();
194
195 return $lang;
196 }
197
198 # $wgLanguageCode may be changed later to fit with user preference.
199 # The content language will remain fixed as per the configuration,
200 # so let's keep it.
201 $wgContLanguageCode = $wgLanguageCode;
202 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
203
204 $wgContLang = setupLangObj( $wgContLangClass );
205 $wgContLang->initEncoding();
206
207 // set default user option from content language
208 if( !$wgUser->mDataLoaded ) {
209 $wgUser->loadDefaultFromLanguage();
210 }
211
212 // wgLanguageCode now specifically means the UI language
213 $wgLanguageCode = $wgUser->getOption('language');
214 # Validate $wgLanguageCode, which will soon be sent to an eval()
215 if( empty( $wgLanguageCode ) || !preg_match( '/^[a-z\-]*$/', $wgLanguageCode ) ) {
216 $wgLanguageCode = $wgContLanguageCode;
217 }
218
219 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
220
221 if( $wgLangClass == $wgContLangClass ) {
222 $wgLang = &$wgContLang;
223 } else {
224 wfSuppressWarnings();
225 include_once("$IP/languages/$wgLangClass.php");
226 wfRestoreWarnings();
227
228 $wgLang = setupLangObj( $wgLangClass );
229 }
230
231 wfProfileOut( $fname.'-language' );
232 wfProfileIn( $fname.'-MessageCache' );
233
234 $wgMessageCache = new MessageCache;
235 $wgMessageCache->initialise( $parserMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
236
237 wfProfileOut( $fname.'-MessageCache' );
238
239 #
240 # I guess the warning about UI switching might still apply...
241 #
242 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
243 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
244 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
245 #
246 # To disable it, the easiest thing could be to uncomment the
247 # following; they should effectively disable the UI switch functionality
248 #
249 # $wgLangClass = $wgContLangClass;
250 # $wgLanguageCode = $wgContLanguageCode;
251 # $wgLang = $wgContLang;
252 #
253 # TODO: Need to change reference to $wgLang to $wgContLang at proper
254 # places, including namespaces, dates in signatures, magic words,
255 # and links
256 #
257 # TODO: Need to look at the issue of input/output encoding
258 #
259
260
261 wfProfileIn( $fname.'-OutputPage' );
262
263 $wgOut = new OutputPage();
264
265 wfProfileOut( $fname.'-OutputPage' );
266 wfProfileIn( $fname.'-BlockCache' );
267
268 $wgBlockCache = new BlockCache( true );
269
270 wfProfileOut( $fname.'-BlockCache' );
271 wfProfileIn( $fname.'-misc2' );
272
273 $wgDeferredUpdateList = array();
274 $wgPostCommitUpdateList = array();
275
276 $wgLinkCache = new LinkCache();
277 $wgMagicWords = array();
278 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
279 $wgParserCache = new ParserCache( $messageMemc );
280
281 if ( $wgUseXMLparser ) {
282 require_once( 'ParserXML.php' );
283 $wgParser = new ParserXML();
284 } else {
285 $wgParser = new Parser();
286 }
287 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
288 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
289 wfSeedRandom();
290
291 # Placeholders in case of DB error
292 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
293 $wgArticle = new Article($wgTitle);
294
295 wfProfileOut( $fname.'-misc2' );
296 wfProfileIn( $fname.'-extensions' );
297
298 # Extension setup functions for extensions other than skins
299 # Entries should be added to this variable during the inclusion
300 # of the extension file. This allows the extension to perform
301 # any necessary initialisation in the fully initialised environment
302 foreach ( $wgExtensionFunctions as $func ) {
303 $func();
304 }
305
306 wfDebug( "\n" );
307 $wgFullyInitialised = true;
308 wfProfileOut( $fname.'-extensions' );
309 wfProfileOut( $fname );
310
311 }
312 ?>