3 * Functions for dealing with proxies
9 * Extracts the XFF string from the request header
10 * Checks first for "X-Forwarded-For", then "Client-ip"
11 * Note: headers are spoofable
14 function wfGetForwardedFor() {
15 if( function_exists( 'apache_request_headers' ) ) {
16 // More reliable than $_SERVER due to case and -/_ folding
18 foreach ( apache_request_headers() as $tempName => $tempValue ) {
19 $set[ strtoupper( $tempName ) ] = $tempValue;
21 $index = strtoupper ( 'X-Forwarded-For' );
22 $index2 = strtoupper ( 'Client-ip' );
24 // Subject to spoofing with headers like X_Forwarded_For
26 $index = 'HTTP_X_FORWARDED_FOR';
27 $index2 = 'CLIENT-IP';
30 #Try a couple of headers
31 if( isset( $set[$index] ) ) {
33 } else if( isset( $set[$index2] ) ) {
41 * Returns the browser/OS data from the request header
42 * Note: headers are spoofable
45 function wfGetAgent() {
46 if( function_exists( 'apache_request_headers' ) ) {
47 // More reliable than $_SERVER due to case and -/_ folding
49 foreach ( apache_request_headers() as $tempName => $tempValue ) {
50 $set[ strtoupper( $tempName ) ] = $tempValue;
52 $index = strtoupper ( 'User-Agent' );
54 // Subject to spoofing with headers like X_Forwarded_For
56 $index = 'HTTP_USER_AGENT';
58 if( isset( $set[$index] ) ) {
66 * Work out the IP address based on various globals
67 * For trusted proxies, use the XFF client IP (first of the chain)
71 global $wgUsePrivateIPs, $wgCommandLineMode;
74 # Return cached result
75 if ( !empty( $ip ) ) {
81 /* collect the originating ips */
82 # Client connecting to this webserver
83 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
84 $ip = IP
::canonicalize( $_SERVER['REMOTE_ADDR'] );
85 } elseif( $wgCommandLineMode ) {
92 # Append XFF on to $ipchain
93 $forwardedFor = wfGetForwardedFor();
94 if ( isset( $forwardedFor ) ) {
95 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
96 $xff = array_reverse( $xff );
97 $ipchain = array_merge( $ipchain, $xff );
100 # Step through XFF list and find the last address in the list which is a trusted server
101 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
102 foreach ( $ipchain as $i => $curIP ) {
103 $curIP = IP
::canonicalize( $curIP );
104 if ( wfIsTrustedProxy( $curIP ) ) {
105 if ( isset( $ipchain[$i +
1] ) ) {
106 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
107 $ip = $ipchain[$i +
1];
115 # Allow extensions to improve our guess
116 wfRunHooks( 'GetIP', array( &$ip ) );
119 throw new MWException( "Unable to determine IP" );
122 wfDebug( "IP: $ip\n" );
127 * Checks if an IP is a trusted proxy providor
128 * Useful to tell if X-Fowarded-For data is possibly bogus
129 * Squid cache servers for the site and AOL are whitelisted
133 function wfIsTrustedProxy( $ip ) {
134 global $wgSquidServers, $wgSquidServersNoPurge;
136 if ( in_array( $ip, $wgSquidServers ) ||
137 in_array( $ip, $wgSquidServersNoPurge )
143 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
148 * Forks processes to scan the originating IP for an open proxy server
149 * MemCached can be used to skip IPs that have already been scanned
151 function wfProxyCheck() {
152 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
153 global $wgMemc, $wgProxyMemcExpiry;
156 if ( !$wgBlockOpenProxies ) {
163 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
164 $mcValue = $wgMemc->get( $mcKey );
165 $skip = (bool)$mcValue;
169 $title = SpecialPage
::getTitleFor( 'Blockme' );
170 $iphash = md5( $ip . $wgProxyKey );
171 $url = $title->getFullURL( 'ip='.$iphash );
173 foreach ( $wgProxyPorts as $port ) {
174 $params = implode( ' ', array(
175 escapeshellarg( $wgProxyScriptPath ),
176 escapeshellarg( $ip ),
177 escapeshellarg( $port ),
178 escapeshellarg( $url )
180 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
183 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
188 * Convert a network specification in CIDR notation to an integer network and a number of bits
190 * @deprecated Call IP::parseCIDR() directly, will be removed in 1.19
191 * @return array(string, int)
193 function wfParseCIDR( $range ) {
194 wfDeprecated( __FUNCTION__
);
195 return IP
::parseCIDR( $range );
199 * Check if an IP address is in the local proxy list
202 function wfIsLocallyBlockedProxy( $ip ) {
205 if ( !$wgProxyList ) {
208 wfProfileIn( __METHOD__
);
210 if ( !is_array( $wgProxyList ) ) {
211 # Load from the specified file
212 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
215 if ( !is_array( $wgProxyList ) ) {
217 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
219 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
220 # Old-style flipped proxy list
225 wfProfileOut( __METHOD__
);