X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FProxyTools.php;h=bdab3be24fe07d4687e68642ebe20275a7f8c878;hb=68cad93a447be3e7824c79c0663967ee727a6244;hp=991ec368748da342b201e38d9d23ff8ba64ccfe5;hpb=55671d7040e820552344dab96caf40c25085259a;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ProxyTools.php b/includes/ProxyTools.php index 991ec36874..bdab3be24f 100644 --- a/includes/ProxyTools.php +++ b/includes/ProxyTools.php @@ -1,114 +1,64 @@ $curIP ) { - if ( array_key_exists( $curIP, $trustedProxies ) ) { - if ( isset( $ipchain[$i + 1] ) && wfIsIPPublic( $ipchain[$i + 1] ) ) { - $ip = $ipchain[$i + 1]; - } - } else { - break; - } - } - } - - wfDebug( "IP: $ip\n" ); - $wgIP = $ip; - return $ip; +/** + * Extracts the XFF string from the request header + * Note: headers are spoofable + * + * @deprecated in 1.19; use $wgRequest->getHeader( 'X-Forwarded-For' ) instead. + * @return string + */ +function wfGetForwardedFor() { + wfDeprecated( __METHOD__, '1.19' ); + global $wgRequest; + return $wgRequest->getHeader( 'X-Forwarded-For' ); } -/** - * Given an IP address in dotted-quad notation, returns an unsigned integer. - * Like ip2long() except that it actually works and has a consistent error return value. +/** + * Returns the browser/OS data from the request header + * Note: headers are spoofable + * + * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead. + * @return string */ -function wfIP2Unsigned( $ip ) { - $n = ip2long( $ip ); - if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version - $n = false; - } elseif ( $n < 0 ) { - $n += pow( 2, 32 ); - } - return $n; +function wfGetAgent() { + wfDeprecated( __METHOD__, '1.18' ); + global $wgRequest; + return $wgRequest->getHeader( 'User-Agent' ); } /** - * Return a zero-padded hexadecimal representation of an IP address + * Work out the IP address based on various globals + * For trusted proxies, use the XFF client IP (first of the chain) + * + * @deprecated in 1.19; call $wgRequest->getIP() directly. + * @return string */ -function wfIP2Hex( $ip ) { - $n = wfIP2Unsigned( $ip ); - if ( $n !== false ) { - $n = sprintf( '%08X', $n ); - } - return $n; +function wfGetIP() { + wfDeprecated( __METHOD__, '1.19' ); + global $wgRequest; + return $wgRequest->getIP(); } /** - * Determine if an IP address really is an IP address, and if it is public, - * i.e. not RFC 1918 or similar + * Checks if an IP is a trusted proxy providor. + * Useful to tell if X-Fowarded-For data is possibly bogus. + * Squid cache servers for the site are whitelisted. + * + * @param $ip String + * @return bool */ -function wfIsIPPublic( $ip ) { - $n = wfIP2Unsigned( $ip ); - if ( !$n ) { - return false; - } +function wfIsTrustedProxy( $ip ) { + global $wgSquidServers, $wgSquidServersNoPurge; - static $privateRanges = false; - if ( !$privateRanges ) { - $privateRanges = array( - array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private) - array( '172.16.0.0', '172.31.255.255' ), # " - array( '192.168.0.0', '192.168.255.255' ), # " - array( '0.0.0.0', '0.255.255.255' ), # this network - array( '127.0.0.0', '127.255.255.255' ), # loopback - ); - } - - foreach ( $privateRanges as $r ) { - $start = wfIP2Unsigned( $r[0] ); - $end = wfIP2Unsigned( $r[1] ); - if ( $n >= $start && $n <= $end ) { - return false; - } - } - return true; + $trusted = in_array( $ip, $wgSquidServers ) || + in_array( $ip, $wgSquidServersNoPurge ); + wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) ); + return $trusted; } /** @@ -117,29 +67,25 @@ function wfIsIPPublic( $ip ) { */ function wfProxyCheck() { global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath; - global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry; + global $wgMemc, $wgProxyMemcExpiry, $wgRequest; + global $wgProxyKey; if ( !$wgBlockOpenProxies ) { return; } - $ip = wfGetIP(); - + $ip = $wgRequest->getIP(); + # Get MemCached key - $skip = false; - if ( $wgUseMemCached ) { - $mcKey = "$wgDBname:proxy:ip:$ip"; - $mcValue = $wgMemc->get( $mcKey ); - if ( $mcValue ) { - $skip = true; - } - } + $mcKey = wfMemcKey( 'proxy', 'ip', $ip ); + $mcValue = $wgMemc->get( $mcKey ); + $skip = (bool)$mcValue; # Fork the processes if ( !$skip ) { - $title = Title::makeTitle( NS_SPECIAL, 'Blockme' ); + $title = SpecialPage::getTitleFor( 'Blockme' ); $iphash = md5( $ip . $wgProxyKey ); - $url = $title->getFullURL( 'ip='.$iphash ); + $url = wfExpandUrl( $title->getFullURL( 'ip='.$iphash ), PROTO_HTTP ); foreach ( $wgProxyPorts as $port ) { $params = implode( ' ', array( @@ -148,31 +94,9 @@ function wfProxyCheck() { escapeshellarg( $port ), escapeshellarg( $url ) )); - exec( "php $params &>/dev/null &" ); + exec( "php $params >" . wfGetNull() . " 2>&1 &" ); } # Set MemCached key - if ( $wgUseMemCached ) { - $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry ); - } - } -} - -/** - * Convert a network specification in CIDR notation to an integer network and a number of bits - */ -function wfParseCIDR( $range ) { - $parts = explode( '/', $range, 2 ); - if ( count( $parts ) != 2 ) { - return array( false, false ); + $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry ); } - $network = wfIP2Unsigned( $parts[0] ); - if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) { - $bits = $parts[1]; - } else { - $network = false; - $bits = false; - } - return array( $network, $bits ); } - -?>