From: Aaron Schulz Date: Mon, 12 Mar 2007 18:15:58 +0000 (+0000) Subject: * Add IPv6 support to XFF functions, improve IPv6 regexp X-Git-Tag: 1.31.0-rc.0~53792 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=ac03a462ff7e35afea55a4cafe7408ea8d22b43d;p=lhc%2Fweb%2Fwiklou.git * Add IPv6 support to XFF functions, improve IPv6 regexp --- diff --git a/includes/IP.php b/includes/IP.php index b0a1d187e6..540096eb77 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -22,7 +22,7 @@ define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' ); // An IPv6 block is an IP address and a prefix (d1 to d128) define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)'); // An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used. This is lax! -define( 'RE_IPV6_ADD', RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7}' ); +define( 'RE_IPV6_ADD', '(:(:' . RE_IPV6_WORD . '){1,7}|' . RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7})' ); define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX ); // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network define( 'IP_ADDRESS_STRING', RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)|' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)'); @@ -178,7 +178,7 @@ class IP { } elseif ( strpos( $range, '-' ) !== false ) { # Explicit range list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) ); - $start = self::toUnsigned6( $start ); $end = self::toUnsigned6( $end ); + $start = self::toUnsigned6( $start ); $end = self::toUnsigned6( $end ); if ( $start > $end ) { $start = $end = false; } else { diff --git a/includes/ProxyTools.php b/includes/ProxyTools.php index cc0d340bca..fcd6a90186 100644 --- a/includes/ProxyTools.php +++ b/includes/ProxyTools.php @@ -25,26 +25,28 @@ function wfGetForwardedFor() { } } -function wfGetLastIPfromXFF( $xff ) -{ +function wfGetLastIPfromXFF( $xff ) { if ( $xff ) { // Avoid annoyingly long xff hacks - $xff = substr( $xff, 0, 255 ); - // Look for the last IP, assuming they are separated by commas - $n = strrpos( $xff, ',' ); - if ( strrpos !== false ) { - $last = substr( $xff, $n + 1 ); - // Make sure it is an IP - $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip); - if ( $m > 0 ) - $xff_ip = $last_ip; - else - $xff_ip = null; + $xff = substr( $xff, 0, 255 ); + // Look for the last IP, assuming they are separated by commas or spaces + $n = ( strrpos($xff, ',') ) ? strrpos($xff, ',') : strrpos($xff, ' '); + if ( strrpos !== false ) { + $last = trim( substr( $xff, $n + 1 ) ); + // Make sure it is an IP + $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip4); + $n = preg_match('#:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}#', $last, $last_ip6); + if ( $m > 0 ) + $xff_ip = $last_ip4; + else if ( $n > 0 ) + $xff_ip = $last_ip6; + else + $xff_ip = null; } else { - $xff_ip = null; + $xff_ip = null; } } else { - $xff_ip = null; + $xff_ip = null; } return $xff_ip; }