- global $wgUsePrivateIPs, $wgCommandLineMode;
- static $ip = false;
-
- # Return cached result
- if ( !empty( $ip ) ) {
- return $ip;
- }
-
- /* collect the originating ips */
- # Client connecting to this webserver
- if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
- $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
- } elseif( $wgCommandLineMode ) {
- $ip = '127.0.0.1';
- }
-
- # Append XFF
- $forwardedFor = wfGetForwardedFor();
- if ( $forwardedFor !== null ) {
- $ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
- $ipchain = array_reverse( $ipchain );
- if ( $ip ) {
- array_unshift( $ipchain, $ip );
- }
-
- # Step through XFF list and find the last address in the list which is a trusted server
- # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
- foreach ( $ipchain as $i => $curIP ) {
- $curIP = IP::canonicalize( $curIP );
- if ( wfIsTrustedProxy( $curIP ) ) {
- if ( isset( $ipchain[$i + 1] ) ) {
- if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
- $ip = $ipchain[$i + 1];
- }
- }
- } else {
- break;
- }
- }
- }
-
- # Allow extensions to improve our guess
- wfRunHooks( 'GetIP', array( &$ip ) );
-
- if( !$ip ) {
- throw new MWException( "Unable to determine IP" );
- }
-
- wfDebug( "IP: $ip\n" );
- return $ip;