00001 <?php
00013 function wfGetForwardedFor() {
00014 if( function_exists( 'apache_request_headers' ) ) {
00015
00016 $set = array ();
00017 foreach ( apache_request_headers() as $tempName => $tempValue ) {
00018 $set[ strtoupper( $tempName ) ] = $tempValue;
00019 }
00020 $index = strtoupper ( 'X-Forwarded-For' );
00021 $index2 = strtoupper ( 'Client-ip' );
00022 } else {
00023
00024 $set = $_SERVER;
00025 $index = 'HTTP_X_FORWARDED_FOR';
00026 $index2 = 'CLIENT-IP';
00027 }
00028
00029 #Try a couple of headers
00030 if( isset( $set[$index] ) ) {
00031 return $set[$index];
00032 } else if( isset( $set[$index2] ) ) {
00033 return $set[$index2];
00034 } else {
00035 return null;
00036 }
00037 }
00038
00044 function wfGetAgent() {
00045 if( function_exists( 'apache_request_headers' ) ) {
00046
00047 $set = array ();
00048 foreach ( apache_request_headers() as $tempName => $tempValue ) {
00049 $set[ strtoupper( $tempName ) ] = $tempValue;
00050 }
00051 $index = strtoupper ( 'User-Agent' );
00052 } else {
00053
00054 $set = $_SERVER;
00055 $index = 'HTTP_USER_AGENT';
00056 }
00057 if( isset( $set[$index] ) ) {
00058 return $set[$index];
00059 } else {
00060 return '';
00061 }
00062 }
00063
00069 function wfGetIP() {
00070 global $wgIP, $wgUsePrivateIPs, $wgCommandLineMode;
00071
00072 # Return cached result
00073 if ( !empty( $wgIP ) ) {
00074 return $wgIP;
00075 }
00076
00077 $ipchain = array();
00078 $ip = false;
00079
00080
00081 # Client connecting to this webserver
00082 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
00083 $ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
00084 } elseif( $wgCommandLineMode ) {
00085 $ip = '127.0.0.1';
00086 }
00087 if( $ip ) {
00088 $ipchain[] = $ip;
00089 }
00090
00091 # Append XFF on to $ipchain
00092 $forwardedFor = wfGetForwardedFor();
00093 if ( isset( $forwardedFor ) ) {
00094 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
00095 $xff = array_reverse( $xff );
00096 $ipchain = array_merge( $ipchain, $xff );
00097 }
00098
00099 # Step through XFF list and find the last address in the list which is a trusted server
00100 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
00101 foreach ( $ipchain as $i => $curIP ) {
00102 $curIP = IP::canonicalize( $curIP );
00103 if ( wfIsTrustedProxy( $curIP ) ) {
00104 if ( isset( $ipchain[$i + 1] ) ) {
00105 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
00106 $ip = $ipchain[$i + 1];
00107 }
00108 }
00109 } else {
00110 break;
00111 }
00112 }
00113
00114 if( !$ip ) {
00115 throw new MWException( "Unable to determine IP" );
00116 }
00117
00118 wfDebug( "IP: $ip\n" );
00119 $wgIP = $ip;
00120 return $ip;
00121 }
00122
00130 function wfIsTrustedProxy( $ip ) {
00131 global $wgSquidServers, $wgSquidServersNoPurge;
00132
00133 if ( in_array( $ip, $wgSquidServers ) ||
00134 in_array( $ip, $wgSquidServersNoPurge )
00135 ) {
00136 $trusted = true;
00137 } else {
00138 $trusted = false;
00139 }
00140 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
00141 return $trusted;
00142 }
00143
00148 function wfProxyCheck() {
00149 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
00150 global $wgMemc, $wgProxyMemcExpiry;
00151 global $wgProxyKey;
00152
00153 if ( !$wgBlockOpenProxies ) {
00154 return;
00155 }
00156
00157 $ip = wfGetIP();
00158
00159 # Get MemCached key
00160 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
00161 $mcValue = $wgMemc->get( $mcKey );
00162 $skip = (bool)$mcValue;
00163
00164 # Fork the processes
00165 if ( !$skip ) {
00166 $title = SpecialPage::getTitleFor( 'Blockme' );
00167 $iphash = md5( $ip . $wgProxyKey );
00168 $url = $title->getFullURL( 'ip='.$iphash );
00169
00170 foreach ( $wgProxyPorts as $port ) {
00171 $params = implode( ' ', array(
00172 escapeshellarg( $wgProxyScriptPath ),
00173 escapeshellarg( $ip ),
00174 escapeshellarg( $port ),
00175 escapeshellarg( $url )
00176 ));
00177 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
00178 }
00179 # Set MemCached key
00180 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
00181 }
00182 }
00183
00188 function wfParseCIDR( $range ) {
00189 return IP::parseCIDR( $range );
00190 }
00191
00196 function wfIsLocallyBlockedProxy( $ip ) {
00197 global $wgProxyList;
00198
00199 if ( !$wgProxyList ) {
00200 return false;
00201 }
00202 wfProfileIn( __METHOD__ );
00203
00204 if ( !is_array( $wgProxyList ) ) {
00205 # Load from the specified file
00206 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
00207 }
00208
00209 if ( !is_array( $wgProxyList ) ) {
00210 $ret = false;
00211 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
00212 $ret = true;
00213 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
00214 # Old-style flipped proxy list
00215 $ret = true;
00216 } else {
00217 $ret = false;
00218 }
00219 wfProfileOut( __METHOD__ );
00220 return $ret;
00221 }
00222