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;
00071
00072 # Return cached result
00073 if ( !empty( $wgIP ) ) {
00074 return $wgIP;
00075 }
00076
00077
00078 # Client connecting to this webserver
00079 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
00080 $ipchain = array( IP::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
00081 } else {
00082 # Running on CLI?
00083 $ipchain = array( '127.0.0.1' );
00084 }
00085 $ip = $ipchain[0];
00086
00087 # Append XFF on to $ipchain
00088 $forwardedFor = wfGetForwardedFor();
00089 if ( isset( $forwardedFor ) ) {
00090 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
00091 $xff = array_reverse( $xff );
00092 $ipchain = array_merge( $ipchain, $xff );
00093 }
00094
00095 # Step through XFF list and find the last address in the list which is a trusted server
00096 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
00097 foreach ( $ipchain as $i => $curIP ) {
00098 $curIP = IP::canonicalize( $curIP );
00099 if ( wfIsTrustedProxy( $curIP ) ) {
00100 if ( isset( $ipchain[$i + 1] ) ) {
00101 if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
00102 $ip = $ipchain[$i + 1];
00103 }
00104 }
00105 } else {
00106 break;
00107 }
00108 }
00109
00110 wfDebug( "IP: $ip\n" );
00111 $wgIP = $ip;
00112 return $ip;
00113 }
00114
00122 function wfIsTrustedProxy( $ip ) {
00123 global $wgSquidServers, $wgSquidServersNoPurge;
00124
00125 if ( in_array( $ip, $wgSquidServers ) ||
00126 in_array( $ip, $wgSquidServersNoPurge )
00127 ) {
00128 $trusted = true;
00129 } else {
00130 $trusted = false;
00131 }
00132 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
00133 return $trusted;
00134 }
00135
00140 function wfProxyCheck() {
00141 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
00142 global $wgMemc, $wgProxyMemcExpiry;
00143 global $wgProxyKey;
00144
00145 if ( !$wgBlockOpenProxies ) {
00146 return;
00147 }
00148
00149 $ip = wfGetIP();
00150
00151 # Get MemCached key
00152 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
00153 $mcValue = $wgMemc->get( $mcKey );
00154 $skip = (bool)$mcValue;
00155
00156 # Fork the processes
00157 if ( !$skip ) {
00158 $title = SpecialPage::getTitleFor( 'Blockme' );
00159 $iphash = md5( $ip . $wgProxyKey );
00160 $url = $title->getFullURL( 'ip='.$iphash );
00161
00162 foreach ( $wgProxyPorts as $port ) {
00163 $params = implode( ' ', array(
00164 escapeshellarg( $wgProxyScriptPath ),
00165 escapeshellarg( $ip ),
00166 escapeshellarg( $port ),
00167 escapeshellarg( $url )
00168 ));
00169 exec( "php $params &>/dev/null &" );
00170 }
00171 # Set MemCached key
00172 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
00173 }
00174 }
00175
00180 function wfParseCIDR( $range ) {
00181 return IP::parseCIDR( $range );
00182 }
00183
00188 function wfIsLocallyBlockedProxy( $ip ) {
00189 global $wgProxyList;
00190 $fname = 'wfIsLocallyBlockedProxy';
00191
00192 if ( !$wgProxyList ) {
00193 return false;
00194 }
00195 wfProfileIn( $fname );
00196
00197 if ( !is_array( $wgProxyList ) ) {
00198 # Load from the specified file
00199 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
00200 }
00201
00202 if ( !is_array( $wgProxyList ) ) {
00203 $ret = false;
00204 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
00205 $ret = true;
00206 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
00207 # Old-style flipped proxy list
00208 $ret = true;
00209 } else {
00210 $ret = false;
00211 }
00212 wfProfileOut( $fname );
00213 return $ret;
00214 }
00215