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