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