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] ) ) {
29 * @todo FUCKING DOCUMENT THIS FUCKING FUNCTION
31 function wfGetLastIPfromXFF( $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, ' ');
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);
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';
63 // Subject to spoofing with headers like X_Forwarded_For
65 $index = 'HTTP_USER_AGENT';
67 if( isset( $set[$index] ) ) {
74 /** Work out the IP address based on various globals */
78 # Return cached result
79 if ( !empty( $wgIP ) ) {
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'] ) );
89 $ipchain = array( '127.0.0.1' );
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 );
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];
114 wfDebug( "IP: $ip\n" );
119 function wfIsTrustedProxy( $ip ) {
120 global $wgSquidServers, $wgSquidServersNoPurge;
122 if ( in_array( $ip, $wgSquidServers ) ||
123 in_array( $ip, $wgSquidServersNoPurge ) ||
130 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
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
138 function wfProxyCheck() {
139 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
140 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
143 if ( !$wgBlockOpenProxies ) {
151 if ( $wgUseMemCached ) {
152 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
153 $mcValue = $wgMemc->get( $mcKey );
161 $title = SpecialPage
::getTitleFor( 'Blockme' );
162 $iphash = md5( $ip . $wgProxyKey );
163 $url = $title->getFullURL( 'ip='.$iphash );
165 foreach ( $wgProxyPorts as $port ) {
166 $params = implode( ' ', array(
167 escapeshellarg( $wgProxyScriptPath ),
168 escapeshellarg( $ip ),
169 escapeshellarg( $port ),
170 escapeshellarg( $url )
172 exec( "php $params &>/dev/null &" );
175 if ( $wgUseMemCached ) {
176 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
182 * Convert a network specification in CIDR notation to an integer network and a number of bits
184 function wfParseCIDR( $range ) {
185 return IP
::parseCIDR( $range );
189 * Check if an IP address is in the local proxy list
191 function wfIsLocallyBlockedProxy( $ip ) {
193 $fname = 'wfIsLocallyBlockedProxy';
195 if ( !$wgProxyList ) {
198 wfProfileIn( $fname );
200 if ( !is_array( $wgProxyList ) ) {
201 # Load from the specified file
202 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
205 if ( !is_array( $wgProxyList ) ) {
207 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
209 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
210 # Old-style flipped proxy list
215 wfProfileOut( $fname );
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.
223 function wfIsAOLProxy( $ip ) {
244 '205.188.146.144/30',
248 static $parsedRanges;
249 if ( is_null( $parsedRanges ) ) {
250 $parsedRanges = array();
251 foreach ( $ranges as $range ) {
252 $parsedRanges[] = IP
::parseRange( $range );
256 $hex = IP
::toHex( $ip );
257 foreach ( $parsedRanges as $range ) {
258 if ( $hex >= $range[0] && $hex <= $range[1] ) {