whitespace
[lhc/web/wiklou.git] / includes / ProxyTools.php
index dd7daa8..7b8e144 100644 (file)
@@ -1,11 +1,26 @@
 <?php
-if ( ! defined( 'MEDIAWIKI' ) )
-       die();
 /**
  * Functions for dealing with proxies
  * @package MediaWiki
  */
 
+function wfGetForwardedFor() {
+       if( function_exists( 'apache_request_headers' ) ) {
+               // More reliable than $_SERVER due to case and -/_ folding
+               $set = apache_request_headers();
+               $index = 'X-Forwarded-For';
+       } else {
+               // Subject to spoofing with headers like X_Forwarded_For
+               $set = $_SERVER;
+               $index = 'HTTP_X_FORWARDED_FOR';
+       }
+       if( isset( $set[$index] ) ) {
+               return $set[$index];
+       } else {
+               return null;
+       }
+}
+
 /** Work out the IP address based on various globals */
 function wfGetIP() {
        global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
@@ -30,8 +45,9 @@ function wfGetIP() {
        $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
        if ( count( $trustedProxies ) ) {
                # Append XFF on to $ipchain
-               if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
-                       $xff = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
+               $forwardedFor = wfGetForwardedFor();
+               if ( isset( $forwardedFor ) ) {
+                       $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
                        $xff = array_reverse( $xff );
                        $ipchain = array_merge( $ipchain, $xff );
                }
@@ -116,13 +132,14 @@ function wfIsIPPublic( $ip ) {
 function wfProxyCheck() {
        global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
        global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
+       global $wgProxyKey;
 
        if ( !$wgBlockOpenProxies ) {
                return;
        }
 
        $ip = wfGetIP();
-       
+
        # Get MemCached key
        $skip = false;
        if ( $wgUseMemCached ) {
@@ -179,17 +196,17 @@ function wfParseCIDR( $range ) {
 function wfIsLocallyBlockedProxy( $ip ) {
        global $wgProxyList;
        $fname = 'wfIsLocallyBlockedProxy';
-       
+
        if ( !$wgProxyList ) {
                return false;
        }
        wfProfileIn( $fname );
-       
+
        if ( !is_array( $wgProxyList ) ) {
                # Load from the specified file
                $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
        }
-       
+
        if ( !is_array( $wgProxyList ) ) {
                $ret = false;
        } elseif ( array_search( $ip, $wgProxyList ) !== false ) {