Revert r21158 and r21159 -- clearly broken code.
[lhc/web/wiklou.git] / includes / ProxyTools.php
1 <?php
2 /**
3 * Functions for dealing with proxies
4 */
5
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';
17 }
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;
25 }
26 }
27
28 function wfGetLastIPfromXFF( $xff ) {
29 if ( $xff ) {
30 // Avoid annoyingly long xff hacks
31 $xff = substr( $xff, 0, 255 );
32 // Look for the last IP, assuming they are separated by commas or spaces
33 $n = ( strrpos($xff, ',') ) ? strrpos($xff, ',') : strrpos($xff, ' ');
34 if ( strrpos !== false ) {
35 $last = trim( substr( $xff, $n + 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 > 0 )
40 $xff_ip = $last_ip4;
41 else if ( $n > 0 )
42 $xff_ip = $last_ip6;
43 else
44 $xff_ip = null;
45 } else {
46 $xff_ip = null;
47 }
48 } else {
49 $xff_ip = null;
50 }
51 return $xff_ip;
52 }
53
54 function wfGetAgent() {
55 if( function_exists( 'apache_request_headers' ) ) {
56 // More reliable than $_SERVER due to case and -/_ folding
57 $set = apache_request_headers();
58 $index = 'User-Agent';
59 } else {
60 // Subject to spoofing with headers like X_Forwarded_For
61 $set = $_SERVER;
62 $index = 'HTTP_USER_AGENT';
63 }
64 if( isset( $set[$index] ) ) {
65 return $set[$index];
66 } else {
67 return '';
68 }
69 }
70
71 /** Work out the IP address based on various globals */
72 function wfGetIP() {
73 global $wgIP;
74
75 # Return cached result
76 if ( !empty( $wgIP ) ) {
77 return $wgIP;
78 }
79
80 /* collect the originating ips */
81 # Client connecting to this webserver
82 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
83 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
84 } else {
85 # Running on CLI?
86 $ipchain = array( '127.0.0.1' );
87 }
88 $ip = $ipchain[0];
89
90 # Append XFF on to $ipchain
91 $forwardedFor = wfGetForwardedFor();
92 if ( isset( $forwardedFor ) ) {
93 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
94 $xff = array_reverse( $xff );
95 $ipchain = array_merge( $ipchain, $xff );
96 }
97
98 # Step through XFF list and find the last address in the list which is a trusted server
99 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
100 foreach ( $ipchain as $i => $curIP ) {
101 $curIP = IP::canonicalize( $curIP );
102 if ( wfIsTrustedProxy( $curIP ) ) {
103 if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
104 $ip = $ipchain[$i + 1];
105 }
106 } else {
107 break;
108 }
109 }
110
111 wfDebug( "IP: $ip\n" );
112 $wgIP = $ip;
113 return $ip;
114 }
115
116 function wfIsTrustedProxy( $ip ) {
117 global $wgSquidServers, $wgSquidServersNoPurge;
118
119 if ( in_array( $ip, $wgSquidServers ) ||
120 in_array( $ip, $wgSquidServersNoPurge ) ||
121 wfIsAOLProxy( $ip )
122 ) {
123 $trusted = true;
124 } else {
125 $trusted = false;
126 }
127 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
128 return $trusted;
129 }
130
131 /**
132 * Forks processes to scan the originating IP for an open proxy server
133 * MemCached can be used to skip IPs that have already been scanned
134 */
135 function wfProxyCheck() {
136 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
137 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
138 global $wgProxyKey;
139
140 if ( !$wgBlockOpenProxies ) {
141 return;
142 }
143
144 $ip = wfGetIP();
145
146 # Get MemCached key
147 $skip = false;
148 if ( $wgUseMemCached ) {
149 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
150 $mcValue = $wgMemc->get( $mcKey );
151 if ( $mcValue ) {
152 $skip = true;
153 }
154 }
155
156 # Fork the processes
157 if ( !$skip ) {
158 $title = SpecialPage::getTitleFor( 'Blockme' );
159 $iphash = md5( $ip . $wgProxyKey );
160 $url = $title->getFullURL( 'ip='.$iphash );
161
162 foreach ( $wgProxyPorts as $port ) {
163 $params = implode( ' ', array(
164 escapeshellarg( $wgProxyScriptPath ),
165 escapeshellarg( $ip ),
166 escapeshellarg( $port ),
167 escapeshellarg( $url )
168 ));
169 exec( "php $params &>/dev/null &" );
170 }
171 # Set MemCached key
172 if ( $wgUseMemCached ) {
173 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
174 }
175 }
176 }
177
178 /**
179 * Convert a network specification in CIDR notation to an integer network and a number of bits
180 */
181 function wfParseCIDR( $range ) {
182 return IP::parseCIDR( $range );
183 }
184
185 /**
186 * Check if an IP address is in the local proxy list
187 */
188 function wfIsLocallyBlockedProxy( $ip ) {
189 global $wgProxyList;
190 $fname = 'wfIsLocallyBlockedProxy';
191
192 if ( !$wgProxyList ) {
193 return false;
194 }
195 wfProfileIn( $fname );
196
197 if ( !is_array( $wgProxyList ) ) {
198 # Load from the specified file
199 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
200 }
201
202 if ( !is_array( $wgProxyList ) ) {
203 $ret = false;
204 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
205 $ret = true;
206 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
207 # Old-style flipped proxy list
208 $ret = true;
209 } else {
210 $ret = false;
211 }
212 wfProfileOut( $fname );
213 return $ret;
214 }
215
216 /**
217 * TODO: move this list to the database in a global IP info table incorporating
218 * trusted ISP proxies, blocked IP addresses and open proxies.
219 */
220 function wfIsAOLProxy( $ip ) {
221 $ranges = array(
222 '64.12.96.0/19',
223 '149.174.160.0/20',
224 '152.163.240.0/21',
225 '152.163.248.0/22',
226 '152.163.252.0/23',
227 '152.163.96.0/22',
228 '152.163.100.0/23',
229 '195.93.32.0/22',
230 '195.93.48.0/22',
231 '195.93.64.0/19',
232 '195.93.96.0/19',
233 '195.93.16.0/20',
234 '198.81.0.0/22',
235 '198.81.16.0/20',
236 '198.81.8.0/23',
237 '202.67.64.128/25',
238 '205.188.192.0/20',
239 '205.188.208.0/23',
240 '205.188.112.0/20',
241 '205.188.146.144/30',
242 '207.200.112.0/21',
243 );
244
245 static $parsedRanges;
246 if ( is_null( $parsedRanges ) ) {
247 $parsedRanges = array();
248 foreach ( $ranges as $range ) {
249 $parsedRanges[] = IP::parseRange( $range );
250 }
251 }
252
253 $hex = IP::toHex( $ip );
254 foreach ( $parsedRanges as $range ) {
255 if ( $hex >= $range[0] && $hex <= $range[1] ) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262
263
264 ?>