Only check X-Forwarded-For if we're in squid mode. It's easy to forge an IP address...
[lhc/web/wiklou.git] / includes / Setup.php
1 <?
2 # The main wiki script and things like database
3 # conversion and maintenance scripts all share a
4 # common setup of including lots of classes and
5 # setting up a few globals.
6 #
7
8 if( !isset( $wgProfiling ) )
9 $wgProfiling = false;
10
11 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
12 include_once( "Profiling.php" );
13 } else {
14 function wfProfileIn( $fn ) {}
15 function wfProfileOut( $fn = "" ) {}
16 function wfGetProfilingOutput( $s, $e ) {}
17 function wfProfileClose() {}
18 }
19
20
21
22 /* collect the originating ips */
23 $wgIP = getenv("REMOTE_ADDR");
24 if( $wgUseSquid && isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
25 # If the web server is behind a reverse proxy, we need to find
26 # out where our requests are really coming from.
27 $wgIP = trim( preg_replace( "/^(.*, )?([^,]+)$/", "$2",
28 $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
29 }
30
31 $fname = "Setup.php";
32 wfProfileIn( $fname );
33 global $wgUseDynamicDates;
34 wfProfileIn( "$fname-includes" );
35
36 include_once( "GlobalFunctions.php" );
37 include_once( "Namespace.php" );
38 include_once( "RecentChange.php" );
39 include_once( "Skin.php" );
40 include_once( "OutputPage.php" );
41 include_once( "User.php" );
42 include_once( "LinkCache.php" );
43 include_once( "Title.php" );
44 include_once( "Article.php" );
45 include_once( "MagicWord.php" );
46 include_once( "memcached-client.php" );
47 include_once( "Block.php" );
48 include_once( "SearchEngine.php" );
49 include_once( "DifferenceEngine.php" );
50 include_once( "MessageCache.php" );
51
52 wfProfileOut( "$fname-includes" );
53 wfProfileIn( "$fname-memcached" );
54 global $wgUser, $wgLang, $wgOut, $wgTitle;
55 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
56 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
57 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
58 global $wgMsgCacheExpiry, $wgDBname, $wgCommandLineMode;
59
60 class MemCachedClientforWiki extends memcached {
61 function _debugprint( $text ) {
62 wfDebug( "memcached: $text\n" );
63 }
64 }
65
66 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
67 # It acts as a memcached server with no RAM, that is, all objects are
68 # cleared the moment they are set. All set operations succeed and all
69 # get operations return null.
70
71 class FakeMemCachedClient {
72 function add ($key, $val, $exp = 0) { return true; }
73 function decr ($key, $amt=1) { return null; }
74 function delete ($key, $time = 0) { return false; }
75 function disconnect_all () { }
76 function enable_compress ($enable) { }
77 function forget_dead_hosts () { }
78 function get ($key) { return null; }
79 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
80 function incr ($key, $amt=1) { return null; }
81 function replace ($key, $value, $exp=0) { return false; }
82 function run_command ($sock, $cmd) { return null; }
83 function set ($key, $value, $exp=0){ return true; }
84 function set_compress_threshold ($thresh){ }
85 function set_debug ($dbg) { }
86 function set_servers ($list) { }
87 }
88
89 if( $wgUseMemCached ) {
90 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
91 $wgMemc->set_servers( $wgMemCachedServers );
92 $wgMemc->set_debug( $wgMemCachedDebug );
93
94 # Test it to see if it's working
95 # This is necessary because otherwise wfMsg would be extremely inefficient
96 if ( !$wgMemc->set( "test", "", 0 ) ) {
97 wfDebug( "Memcached failed setup test - connection error?\n" );
98 $wgUseMemCached = false;
99 $wgMemc = new FakeMemCachedClient();
100 }
101 } else {
102 $wgMemc = new FakeMemCachedClient();
103 }
104
105 wfProfileOut( "$fname-memcached" );
106 wfProfileIn( "$fname-misc" );
107
108 include_once( "Language.php" );
109
110 $wgMessageCache = new MessageCache;
111
112 $wgOut = new OutputPage();
113 wfDebug( "\n\n" );
114
115 $wgLangClass = "Language" . ucfirst( $wgLanguageCode );
116 if( ! class_exists( $wgLangClass ) ) {
117 include_once( "LanguageUtf8.php" );
118 $wgLangClass = "LanguageUtf8";
119 }
120
121 $wgLang = new $wgLangClass();
122 if ( !is_object($wgLang) ) {
123 print "No language class ($wgLang)\N";
124 }
125 $wgMessageCache->initialise( $wgUseMemCached, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
126
127 if ( $wgUseDynamicDates ) {
128 include_once( "DateFormatter.php" );
129 global $wgDateFormatter;
130 $wgDateFormatter = new DateFormatter;
131 }
132
133 if( !$wgCommandLineMode && isset( $_COOKIE[ini_get("session.name")] ) ) {
134 User::SetupSession();
135 }
136
137 $wgUser = User::loadFromSession();
138 $wgDeferredUpdateList = array();
139 $wgLinkCache = new LinkCache();
140 $wgMagicWords = array();
141 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
142
143 wfProfileOut( "$fname-misc" );
144 wfProfileOut( $fname );
145
146 ?>