(bug 13453) Fix rebuildrecentchanges for DB's with FK constraints
[lhc/web/wiklou.git] / includes / ProxyTools.php
index 759857d..f76e24f 100644 (file)
@@ -67,22 +67,26 @@ function wfGetAgent() {
  * @return string
  */
 function wfGetIP() {
-       global $wgIP;
+       global $wgIP, $wgUsePrivateIPs, $wgCommandLineMode;
 
        # Return cached result
        if ( !empty( $wgIP ) ) {
                return $wgIP;
        }
 
+       $ipchain = array();
+       $ip = false;
+
        /* collect the originating ips */
        # Client connecting to this webserver
        if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
-               $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
-       } else {
-               # Running on CLI?
-               $ipchain = array( '127.0.0.1' );
+               $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
+       } elseif( $wgCommandLineMode ) {
+               $ip = '127.0.0.1';
+       }
+       if( $ip ) {
+               $ipchain[] = $ip;
        }
-       $ip = $ipchain[0];
 
        # Append XFF on to $ipchain
        $forwardedFor = wfGetForwardedFor();
@@ -97,14 +101,20 @@ function wfGetIP() {
        foreach ( $ipchain as $i => $curIP ) {
                $curIP = IP::canonicalize( $curIP );
                if ( wfIsTrustedProxy( $curIP ) ) {
-                       if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
-                               $ip = $ipchain[$i + 1];
+                       if ( isset( $ipchain[$i + 1] ) ) {
+                               if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
+                                       $ip = $ipchain[$i + 1];
+                               }
                        }
                } else {
                        break;
                }
        }
 
+       if( !$ip ) {
+               throw new MWException( "Unable to determine IP" );
+       }
+
        wfDebug( "IP: $ip\n" );
        $wgIP = $ip;
        return $ip;
@@ -164,7 +174,7 @@ function wfProxyCheck() {
                                                escapeshellarg( $port ),
                                                escapeshellarg( $url )
                                                ));
-                       exec( "php $params &>/dev/null &" );
+                       exec( "php $params >" . wfGetNull() . " 2>&1 &" );
                }
                # Set MemCached key
                $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );