Fix invalid ORDER BY
[mediawiki.git] / includes / ProxyTools.php
blob646f6b5ddabaf9dca357b21d960e5c781453185a
1 <?php
2 /**
3 * Functions for dealing with proxies
4 */
6 function wfGetForwardedFor() {
7 if( function_exists( 'apache_request_headers' ) ) {
8 // More reliable than $_SERVER due to case and -/_ folding
9 $set = apache_request_headers();
10 $index = 'X-Forwarded-For';
11 $index2 = 'Client-ip';
12 } else {
13 // Subject to spoofing with headers like X_Forwarded_For
14 $set = $_SERVER;
15 $index = 'HTTP_X_FORWARDED_FOR';
16 $index2 = 'CLIENT-IP';
18 #Try a couple of headers
19 if( isset( $set[$index] ) ) {
20 return $set[$index];
21 } else if( isset( $set[$index2] ) ) {
22 return $set[$index2];
23 } else {
24 return null;
28 function wfGetLastIPfromXFF( $xff ) {
29 if ( $xff ) {
30 // Avoid annoyingly long xff hacks
31 $xff = substr( $xff, 0, 511 );
32 // Look for the last IP, assuming they are separated by commas or spaces
33 $s = ( strrpos($xff, ',') ) ? strrpos($xff, ',') : strrpos($xff, ' ');
34 if ( $s !== false ) {
35 $last = trim( substr( $xff, $s + 1 ) );
36 // Make sure it is an IP
37 $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip4);
38 $n = preg_match('#:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}#', $last, $last_ip6);
39 if ( $m )
40 $xff_ip = $last_ip4;
41 else if ( $n )
42 $xff_ip = $last_ip6;
43 else
44 $xff_ip = null;
45 } else {
46 $xff_ip = null;
48 } else {
49 $xff_ip = null;
51 return $xff_ip;
54 function wfGetClientIPfromXFF( $xff ) {
55 if ( $xff ) {
56 // Avoid annoyingly long xff hacks
57 $xff = substr( $xff, 0, 511 );
58 // Look for the first IP
59 $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $xff, $first_ip);
60 $n = preg_match('#:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}#', $xff, $first_ip);
61 if ( $m )
62 $xff_ip = $first_ip4;
63 else if ( $n )
64 $xff_ip = $first_ip6;
65 else
66 $xff_ip = null;
67 } else {
68 $xff_ip = null;
70 return $xff_ip;
73 function wfGetAgent() {
74 if( function_exists( 'apache_request_headers' ) ) {
75 // More reliable than $_SERVER due to case and -/_ folding
76 $set = apache_request_headers();
77 $index = 'User-Agent';
78 } else {
79 // Subject to spoofing with headers like X_Forwarded_For
80 $set = $_SERVER;
81 $index = 'HTTP_USER_AGENT';
83 if( isset( $set[$index] ) ) {
84 return $set[$index];
85 } else {
86 return '';
90 /** Work out the IP address based on various globals */
91 function wfGetIP() {
92 global $wgIP;
94 # Return cached result
95 if ( !empty( $wgIP ) ) {
96 return $wgIP;
99 /* collect the originating ips */
100 # Client connecting to this webserver
101 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
102 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
103 } else {
104 # Running on CLI?
105 $ipchain = array( '127.0.0.1' );
107 $ip = $ipchain[0];
109 # Append XFF on to $ipchain
110 $forwardedFor = wfGetForwardedFor();
111 if ( isset( $forwardedFor ) ) {
112 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
113 $xff = array_reverse( $xff );
114 $ipchain = array_merge( $ipchain, $xff );
117 # Step through XFF list and find the last address in the list which is a trusted server
118 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
119 foreach ( $ipchain as $i => $curIP ) {
120 $curIP = IP::canonicalize( $curIP );
121 if ( wfIsTrustedProxy( $curIP ) ) {
122 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
123 $ip = $ipchain[$i + 1];
125 } else {
126 break;
130 wfDebug( "IP: $ip\n" );
131 $wgIP = $ip;
132 return $ip;
135 function wfIsTrustedProxy( $ip ) {
136 global $wgSquidServers, $wgSquidServersNoPurge;
138 if ( in_array( $ip, $wgSquidServers ) ||
139 in_array( $ip, $wgSquidServersNoPurge ) ||
140 wfIsAOLProxy( $ip )
142 $trusted = true;
143 } else {
144 $trusted = false;
146 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
147 return $trusted;
151 * Forks processes to scan the originating IP for an open proxy server
152 * MemCached can be used to skip IPs that have already been scanned
154 function wfProxyCheck() {
155 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
156 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
157 global $wgProxyKey;
159 if ( !$wgBlockOpenProxies ) {
160 return;
163 $ip = wfGetIP();
165 # Get MemCached key
166 $skip = false;
167 if ( $wgUseMemCached ) {
168 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
169 $mcValue = $wgMemc->get( $mcKey );
170 if ( $mcValue ) {
171 $skip = true;
175 # Fork the processes
176 if ( !$skip ) {
177 $title = SpecialPage::getTitleFor( 'Blockme' );
178 $iphash = md5( $ip . $wgProxyKey );
179 $url = $title->getFullURL( 'ip='.$iphash );
181 foreach ( $wgProxyPorts as $port ) {
182 $params = implode( ' ', array(
183 escapeshellarg( $wgProxyScriptPath ),
184 escapeshellarg( $ip ),
185 escapeshellarg( $port ),
186 escapeshellarg( $url )
188 exec( "php $params &>/dev/null &" );
190 # Set MemCached key
191 if ( $wgUseMemCached ) {
192 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
198 * Convert a network specification in CIDR notation to an integer network and a number of bits
200 function wfParseCIDR( $range ) {
201 return IP::parseCIDR( $range );
205 * Check if an IP address is in the local proxy list
207 function wfIsLocallyBlockedProxy( $ip ) {
208 global $wgProxyList;
209 $fname = 'wfIsLocallyBlockedProxy';
211 if ( !$wgProxyList ) {
212 return false;
214 wfProfileIn( $fname );
216 if ( !is_array( $wgProxyList ) ) {
217 # Load from the specified file
218 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
221 if ( !is_array( $wgProxyList ) ) {
222 $ret = false;
223 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
224 $ret = true;
225 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
226 # Old-style flipped proxy list
227 $ret = true;
228 } else {
229 $ret = false;
231 wfProfileOut( $fname );
232 return $ret;
236 * TODO: move this list to the database in a global IP info table incorporating
237 * trusted ISP proxies, blocked IP addresses and open proxies.
239 function wfIsAOLProxy( $ip ) {
240 $ranges = array(
241 '64.12.96.0/19',
242 '149.174.160.0/20',
243 '152.163.240.0/21',
244 '152.163.248.0/22',
245 '152.163.252.0/23',
246 '152.163.96.0/22',
247 '152.163.100.0/23',
248 '195.93.32.0/22',
249 '195.93.48.0/22',
250 '195.93.64.0/19',
251 '195.93.96.0/19',
252 '195.93.16.0/20',
253 '198.81.0.0/22',
254 '198.81.16.0/20',
255 '198.81.8.0/23',
256 '202.67.64.128/25',
257 '205.188.192.0/20',
258 '205.188.208.0/23',
259 '205.188.112.0/20',
260 '205.188.146.144/30',
261 '207.200.112.0/21',
264 static $parsedRanges;
265 if ( is_null( $parsedRanges ) ) {
266 $parsedRanges = array();
267 foreach ( $ranges as $range ) {
268 $parsedRanges[] = IP::parseRange( $range );
272 $hex = IP::toHex( $ip );
273 foreach ( $parsedRanges as $range ) {
274 if ( $hex >= $range[0] && $hex <= $range[1] ) {
275 return true;
278 return false;