3 * Functions for dealing with proxies
9 * Extracts the XFF string from the request header
10 * Note: headers are spoofable
13 function wfGetForwardedFor() {
14 $apacheHeaders = function_exists( 'apache_request_headers' ) ?
apache_request_headers() : null;
15 if( is_array( $apacheHeaders ) ) {
16 // More reliable than $_SERVER due to case and -/_ folding
18 foreach ( $apacheHeaders as $tempName => $tempValue ) {
19 $set[ strtoupper( $tempName ) ] = $tempValue;
21 $index = strtoupper ( 'X-Forwarded-For' );
23 // Subject to spoofing with headers like X_Forwarded_For
25 $index = 'HTTP_X_FORWARDED_FOR';
28 #Try to see if XFF is set
29 if( isset( $set[$index] ) ) {
37 * Returns the browser/OS data from the request header
38 * Note: headers are spoofable
40 * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
43 function wfGetAgent() {
44 wfDeprecated( __FUNCTION__
);
45 if( function_exists( 'apache_request_headers' ) ) {
46 // More reliable than $_SERVER due to case and -/_ folding
48 foreach ( apache_request_headers() as $tempName => $tempValue ) {
49 $set[ strtoupper( $tempName ) ] = $tempValue;
51 $index = strtoupper ( 'User-Agent' );
53 // Subject to spoofing with headers like X_Forwarded_For
55 $index = 'HTTP_USER_AGENT';
57 if( isset( $set[$index] ) ) {
65 * Work out the IP address based on various globals
66 * For trusted proxies, use the XFF client IP (first of the chain)
67 * @param $reset boolean Used to reset the internal static variable
68 * tracking the IP address. Set to anything non empty to reset it, for
69 * example: wfGetIP( 'reset' ); (default: false).
72 function wfGetIP( $reset = false ) {
73 global $wgUsePrivateIPs, $wgCommandLineMode;
80 # Return cached result
81 if ( !empty( $ip ) ) {
85 /* collect the originating ips */
86 # Client connecting to this webserver
87 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
88 $ip = IP
::canonicalize( $_SERVER['REMOTE_ADDR'] );
89 } elseif( $wgCommandLineMode ) {
94 $forwardedFor = wfGetForwardedFor();
95 if ( $forwardedFor !== null ) {
96 $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
97 $ipchain = array_reverse( $ipchain );
99 array_unshift( $ipchain, $ip );
102 # Step through XFF list and find the last address in the list which is a trusted server
103 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
104 foreach ( $ipchain as $i => $curIP ) {
105 $curIP = IP
::canonicalize( $curIP );
106 if ( wfIsTrustedProxy( $curIP ) ) {
107 if ( isset( $ipchain[$i +
1] ) ) {
108 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
109 $ip = $ipchain[$i +
1];
118 # Allow extensions to improve our guess
119 wfRunHooks( 'GetIP', array( &$ip ) );
122 throw new MWException( "Unable to determine IP" );
125 wfDebug( "IP: $ip\n" );
130 * Checks if an IP is a trusted proxy providor
131 * Useful to tell if X-Fowarded-For data is possibly bogus
132 * Squid cache servers for the site and AOL are whitelisted
136 function wfIsTrustedProxy( $ip ) {
137 global $wgSquidServers, $wgSquidServersNoPurge;
139 $trusted = in_array( $ip, $wgSquidServers ) ||
140 in_array( $ip, $wgSquidServersNoPurge );
141 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
146 * Forks processes to scan the originating IP for an open proxy server
147 * MemCached can be used to skip IPs that have already been scanned
149 function wfProxyCheck() {
150 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
151 global $wgMemc, $wgProxyMemcExpiry;
154 if ( !$wgBlockOpenProxies ) {
161 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
162 $mcValue = $wgMemc->get( $mcKey );
163 $skip = (bool)$mcValue;
167 $title = SpecialPage
::getTitleFor( 'Blockme' );
168 $iphash = md5( $ip . $wgProxyKey );
169 $url = $title->getFullURL( 'ip='.$iphash );
171 foreach ( $wgProxyPorts as $port ) {
172 $params = implode( ' ', array(
173 escapeshellarg( $wgProxyScriptPath ),
174 escapeshellarg( $ip ),
175 escapeshellarg( $port ),
176 escapeshellarg( $url )
178 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
181 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );