* Add IPv6 support to XFF functions, improve IPv6 regexp
authorAaron Schulz <aaron@users.mediawiki.org>
Mon, 12 Mar 2007 18:15:58 +0000 (18:15 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Mon, 12 Mar 2007 18:15:58 +0000 (18:15 +0000)
includes/IP.php
includes/ProxyTools.php

index b0a1d18..540096e 100644 (file)
@@ -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 {
index cc0d340..fcd6a90 100644 (file)
@@ -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;
 }