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 )
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);
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';
58 // Subject to spoofing with headers like X_Forwarded_For
60 $index = 'HTTP_USER_AGENT';
62 if( isset( $set[$index] ) ) {
69 /** Work out the IP address based on various globals */
73 # Return cached result
74 if ( !empty( $wgIP ) ) {
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'] ) );
84 $ipchain = array( '127.0.0.1' );
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 );
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];
109 wfDebug( "IP: $ip\n" );
114 function wfIsTrustedProxy( $ip ) {
115 global $wgSquidServers, $wgSquidServersNoPurge;
117 if ( in_array( $ip, $wgSquidServers ) ||
118 in_array( $ip, $wgSquidServersNoPurge ) ||
125 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
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
133 function wfProxyCheck() {
134 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
135 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
138 if ( !$wgBlockOpenProxies ) {
146 if ( $wgUseMemCached ) {
147 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
148 $mcValue = $wgMemc->get( $mcKey );
156 $title = SpecialPage
::getTitleFor( 'Blockme' );
157 $iphash = md5( $ip . $wgProxyKey );
158 $url = $title->getFullURL( 'ip='.$iphash );
160 foreach ( $wgProxyPorts as $port ) {
161 $params = implode( ' ', array(
162 escapeshellarg( $wgProxyScriptPath ),
163 escapeshellarg( $ip ),
164 escapeshellarg( $port ),
165 escapeshellarg( $url )
167 exec( "php $params &>/dev/null &" );
170 if ( $wgUseMemCached ) {
171 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
177 * Convert a network specification in CIDR notation to an integer network and a number of bits
179 function wfParseCIDR( $range ) {
180 return IP
::parseCIDR( $range );
184 * Check if an IP address is in the local proxy list
186 function wfIsLocallyBlockedProxy( $ip ) {
188 $fname = 'wfIsLocallyBlockedProxy';
190 if ( !$wgProxyList ) {
193 wfProfileIn( $fname );
195 if ( !is_array( $wgProxyList ) ) {
196 # Load from the specified file
197 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
200 if ( !is_array( $wgProxyList ) ) {
202 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
204 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
205 # Old-style flipped proxy list
210 wfProfileOut( $fname );
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.
218 function wfIsAOLProxy( $ip ) {
239 '205.188.146.144/30',
243 static $parsedRanges;
244 if ( is_null( $parsedRanges ) ) {
245 $parsedRanges = array();
246 foreach ( $ranges as $range ) {
247 $parsedRanges[] = IP
::parseRange( $range );
251 $hex = IP
::toHex( $ip );
252 foreach ( $parsedRanges as $range ) {
253 if ( $hex >= $range[0] && $hex <= $range[1] ) {