Localisation updates for core and extension messages from translatewiki.net (2010...
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 *
5 * @file
6 */
7
8 /**
9 * Extracts the XFF string from the request header
10 * Checks first for "X-Forwarded-For", then "Client-ip"
11 * Note: headers are spoofable
12 * @return string
13 */
14 function wfGetForwardedFor() {
15 if( function_exists( 'apache_request_headers' ) ) {
16 // More reliable than $_SERVER due to case and -/_ folding
17 $set = array ();
18 foreach ( apache_request_headers() as $tempName => $tempValue ) {
19 $set[ strtoupper( $tempName ) ] = $tempValue;
20 }
21 $index = strtoupper ( 'X-Forwarded-For' );
22 $index2 = strtoupper ( 'Client-ip' );
23 } else {
24 // Subject to spoofing with headers like X_Forwarded_For
25 $set = $_SERVER;
26 $index = 'HTTP_X_FORWARDED_FOR';
27 $index2 = 'CLIENT-IP';
28 }
29
30 #Try a couple of headers
31 if( isset( $set[$index] ) ) {
32 return $set[$index];
33 } else if( isset( $set[$index2] ) ) {
34 return $set[$index2];
35 } else {
36 return null;
37 }
38 }
39
40 /**
41 * Returns the browser/OS data from the request header
42 * Note: headers are spoofable
43 * @return string
44 */
45 function wfGetAgent() {
46 if( function_exists( 'apache_request_headers' ) ) {
47 // More reliable than $_SERVER due to case and -/_ folding
48 $set = array ();
49 foreach ( apache_request_headers() as $tempName => $tempValue ) {
50 $set[ strtoupper( $tempName ) ] = $tempValue;
51 }
52 $index = strtoupper ( 'User-Agent' );
53 } else {
54 // Subject to spoofing with headers like X_Forwarded_For
55 $set = $_SERVER;
56 $index = 'HTTP_USER_AGENT';
57 }
58 if( isset( $set[$index] ) ) {
59 return $set[$index];
60 } else {
61 return '';
62 }
63 }
64
65 /**
66 * Work out the IP address based on various globals
67 * For trusted proxies, use the XFF client IP (first of the chain)
68 * @return string
69 */
70 function wfGetIP() {
71 global $wgIP, $wgUsePrivateIPs, $wgCommandLineMode;
72
73 # Return cached result
74 if ( !empty( $wgIP ) ) {
75 return $wgIP;
76 }
77
78 $ipchain = array();
79 $ip = false;
80
81 /* collect the originating ips */
82 # Client connecting to this webserver
83 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
84 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
85 } elseif( $wgCommandLineMode ) {
86 $ip = '127.0.0.1';
87 }
88 if( $ip ) {
89 $ipchain[] = $ip;
90 }
91
92 # Append XFF on to $ipchain
93 $forwardedFor = wfGetForwardedFor();
94 if ( isset( $forwardedFor ) ) {
95 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
96 $xff = array_reverse( $xff );
97 $ipchain = array_merge( $ipchain, $xff );
98 }
99
100 # Step through XFF list and find the last address in the list which is a trusted server
101 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
102 foreach ( $ipchain as $i => $curIP ) {
103 $curIP = IP::canonicalize( $curIP );
104 if ( wfIsTrustedProxy( $curIP ) ) {
105 if ( isset( $ipchain[$i + 1] ) ) {
106 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
107 $ip = $ipchain[$i + 1];
108 }
109 }
110 } else {
111 break;
112 }
113 }
114
115 if( !$ip ) {
116 throw new MWException( "Unable to determine IP" );
117 }
118
119 wfDebug( "IP: $ip\n" );
120 $wgIP = $ip;
121 return $ip;
122 }
123
124 /**
125 * Checks if an IP is a trusted proxy providor
126 * Useful to tell if X-Fowarded-For data is possibly bogus
127 * Squid cache servers for the site and AOL are whitelisted
128 * @param $ip String
129 * @return bool
130 */
131 function wfIsTrustedProxy( $ip ) {
132 global $wgSquidServers, $wgSquidServersNoPurge;
133
134 if ( in_array( $ip, $wgSquidServers ) ||
135 in_array( $ip, $wgSquidServersNoPurge )
136 ) {
137 $trusted = true;
138 } else {
139 $trusted = false;
140 }
141 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
142 return $trusted;
143 }
144
145 /**
146 * Forks processes to scan the originating IP for an open proxy server
147 * MemCached can be used to skip IPs that have already been scanned
148 */
149 function wfProxyCheck() {
150 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
151 global $wgMemc, $wgProxyMemcExpiry;
152 global $wgProxyKey;
153
154 if ( !$wgBlockOpenProxies ) {
155 return;
156 }
157
158 $ip = wfGetIP();
159
160 # Get MemCached key
161 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
162 $mcValue = $wgMemc->get( $mcKey );
163 $skip = (bool)$mcValue;
164
165 # Fork the processes
166 if ( !$skip ) {
167 $title = SpecialPage::getTitleFor( 'Blockme' );
168 $iphash = md5( $ip . $wgProxyKey );
169 $url = $title->getFullURL( 'ip='.$iphash );
170
171 foreach ( $wgProxyPorts as $port ) {
172 $params = implode( ' ', array(
173 escapeshellarg( $wgProxyScriptPath ),
174 escapeshellarg( $ip ),
175 escapeshellarg( $port ),
176 escapeshellarg( $url )
177 ));
178 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
179 }
180 # Set MemCached key
181 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
182 }
183 }
184
185 /**
186 * Convert a network specification in CIDR notation to an integer network and a number of bits
187 * @return array(string, int)
188 */
189 function wfParseCIDR( $range ) {
190 return IP::parseCIDR( $range );
191 }
192
193 /**
194 * Check if an IP address is in the local proxy list
195 * @return bool
196 */
197 function wfIsLocallyBlockedProxy( $ip ) {
198 global $wgProxyList;
199
200 if ( !$wgProxyList ) {
201 return false;
202 }
203 wfProfileIn( __METHOD__ );
204
205 if ( !is_array( $wgProxyList ) ) {
206 # Load from the specified file
207 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
208 }
209
210 if ( !is_array( $wgProxyList ) ) {
211 $ret = false;
212 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
213 $ret = true;
214 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
215 # Old-style flipped proxy list
216 $ret = true;
217 } else {
218 $ret = false;
219 }
220 wfProfileOut( __METHOD__ );
221 return $ret;
222 }
223