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", then "X-Real-IP"
11 * Note: headers are spoofable
14 function wfGetForwardedFor() {
15 $apacheHeaders = function_exists( 'apache_request_headers' ) ?
apache_request_headers() : null;
16 if( is_array( $apacheHeaders ) ) {
17 // More reliable than $_SERVER due to case and -/_ folding
19 foreach ( $apacheHeaders as $tempName => $tempValue ) {
20 $set[ strtoupper( $tempName ) ] = $tempValue;
22 $index = strtoupper ( 'X-Forwarded-For' );
23 $index2 = strtoupper ( 'Client-ip' );
24 $index3 = strtoupper ( 'X-Real-IP' );
26 // Subject to spoofing with headers like X_Forwarded_For
28 $index = 'HTTP_X_FORWARDED_FOR';
29 $index2 = 'CLIENT-IP';
30 $index3 = 'HTTP_X_REAL_IP';
33 #Try a couple of headers
34 if( isset( $set[$index] ) ) {
36 } elseif( isset( $set[$index2] ) ) {
38 } elseif( isset( $set[$index3] ) ) {
46 * Returns the browser/OS data from the request header
47 * Note: headers are spoofable
49 * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
52 function wfGetAgent() {
53 wfDeprecated( __FUNCTION__
);
54 if( function_exists( 'apache_request_headers' ) ) {
55 // More reliable than $_SERVER due to case and -/_ folding
57 foreach ( apache_request_headers() as $tempName => $tempValue ) {
58 $set[ strtoupper( $tempName ) ] = $tempValue;
60 $index = strtoupper ( 'User-Agent' );
62 // Subject to spoofing with headers like X_Forwarded_For
64 $index = 'HTTP_USER_AGENT';
66 if( isset( $set[$index] ) ) {
74 * Work out the IP address based on various globals
75 * For trusted proxies, use the XFF client IP (first of the chain)
79 global $wgUsePrivateIPs, $wgCommandLineMode;
82 # Return cached result
83 if ( !empty( $ip ) ) {
87 /* collect the originating ips */
88 # Client connecting to this webserver
89 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
90 $ip = IP
::canonicalize( $_SERVER['REMOTE_ADDR'] );
91 } elseif( $wgCommandLineMode ) {
96 $forwardedFor = wfGetForwardedFor();
97 if ( $forwardedFor !== null ) {
98 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
99 $ipchain = array_reverse( $ipchain );
101 array_unshift( $ipchain, $ip );
104 # Step through XFF list and find the last address in the list which is a trusted server
105 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
106 foreach ( $ipchain as $i => $curIP ) {
107 $curIP = IP
::canonicalize( $curIP );
108 if ( wfIsTrustedProxy( $curIP ) ) {
109 if ( isset( $ipchain[$i +
1] ) ) {
110 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
111 $ip = $ipchain[$i +
1];
120 # Allow extensions to improve our guess
121 wfRunHooks( 'GetIP', array( &$ip ) );
124 throw new MWException( "Unable to determine IP" );
127 wfDebug( "IP: $ip\n" );
132 * Checks if an IP is a trusted proxy providor
133 * Useful to tell if X-Fowarded-For data is possibly bogus
134 * Squid cache servers for the site and AOL are whitelisted
138 function wfIsTrustedProxy( $ip ) {
139 global $wgSquidServers, $wgSquidServersNoPurge;
141 $trusted = in_array( $ip, $wgSquidServers ) ||
142 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 );