3 * Functions for dealing with proxies
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';
13 // Subject to spoofing with headers like X_Forwarded_For
15 $index = 'HTTP_X_FORWARDED_FOR';
16 $index2 = 'CLIENT-IP';
18 #Try a couple of headers
19 if( isset( $set[$index] ) ) {
21 } else if( isset( $set[$index2] ) ) {
28 function wfGetLastIPfromXFF( $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);
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';
60 // Subject to spoofing with headers like X_Forwarded_For
62 $index = 'HTTP_USER_AGENT';
64 if( isset( $set[$index] ) ) {
71 /** Work out the IP address based on various globals */
75 # Return cached result
76 if ( !empty( $wgIP ) ) {
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'] ) );
86 $ipchain = array( '127.0.0.1' );
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 );
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];
111 wfDebug( "IP: $ip\n" );
116 function wfIsTrustedProxy( $ip ) {
117 global $wgSquidServers, $wgSquidServersNoPurge;
119 if ( in_array( $ip, $wgSquidServers ) ||
120 in_array( $ip, $wgSquidServersNoPurge ) ||
127 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
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
135 function wfProxyCheck() {
136 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
137 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
140 if ( !$wgBlockOpenProxies ) {
148 if ( $wgUseMemCached ) {
149 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
150 $mcValue = $wgMemc->get( $mcKey );
158 $title = SpecialPage
::getTitleFor( 'Blockme' );
159 $iphash = md5( $ip . $wgProxyKey );
160 $url = $title->getFullURL( 'ip='.$iphash );
162 foreach ( $wgProxyPorts as $port ) {
163 $params = implode( ' ', array(
164 escapeshellarg( $wgProxyScriptPath ),
165 escapeshellarg( $ip ),
166 escapeshellarg( $port ),
167 escapeshellarg( $url )
169 exec( "php $params &>/dev/null &" );
172 if ( $wgUseMemCached ) {
173 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
179 * Convert a network specification in CIDR notation to an integer network and a number of bits
181 function wfParseCIDR( $range ) {
182 return IP
::parseCIDR( $range );
186 * Check if an IP address is in the local proxy list
188 function wfIsLocallyBlockedProxy( $ip ) {
190 $fname = 'wfIsLocallyBlockedProxy';
192 if ( !$wgProxyList ) {
195 wfProfileIn( $fname );
197 if ( !is_array( $wgProxyList ) ) {
198 # Load from the specified file
199 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
202 if ( !is_array( $wgProxyList ) ) {
204 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
206 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
207 # Old-style flipped proxy list
212 wfProfileOut( $fname );
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.
220 function wfIsAOLProxy( $ip ) {
241 '205.188.146.144/30',
245 static $parsedRanges;
246 if ( is_null( $parsedRanges ) ) {
247 $parsedRanges = array();
248 foreach ( $ranges as $range ) {
249 $parsedRanges[] = IP
::parseRange( $range );
253 $hex = IP
::toHex( $ip );
254 foreach ( $parsedRanges as $range ) {
255 if ( $hex >= $range[0] && $hex <= $range[1] ) {