*Convert '.' to ':' for IPs that have both (like '::eff:1.4.0.3')
authorAaron Schulz <aaron@users.mediawiki.org>
Mon, 3 Sep 2007 16:36:25 +0000 (16:36 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Mon, 3 Sep 2007 16:36:25 +0000 (16:36 +0000)
*Expand IP dbkeys like "::fff:3e" rather than complain about the double colons

includes/IP.php
includes/Title.php

index 4c24ed9..db712c3 100644 (file)
@@ -468,21 +468,28 @@ class IP {
      * @return valid dotted quad IPv4 address or null
      */
     public static function canonicalize( $addr ) {
-       if ( self::isValid( $addr ) )
-           return $addr;
+               if ( self::isValid( $addr ) )
+                       return $addr;
 
-       // IPv6 loopback address
-       $m = array();
-       if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) )
-           return '127.0.0.1';
+               // Annoying IPv6 representations like ::ffff:1.2.3.4
+               if ( strpos($addr,':') !==false && strpos($addr,'.') !==false ) {
+                       $addr = str_replace( '.', ':', $addr );
+                       if( IP::isIPv6( $addr ) )
+                               return $addr;
+               }
+
+               // IPv6 loopback address
+               $m = array();
+               if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) )
+                       return '127.0.0.1';
 
-       // IPv4-mapped and IPv4-compatible IPv6 addresses
-       if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) )
-           return $m[1];
-       if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD . ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
-           return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
+               // IPv4-mapped and IPv4-compatible IPv6 addresses
+               if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) )
+                   return $m[1];
+               if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD . ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
+                   return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
 
-       return null;  // give up
+               return null;  // give up
     }
 }
 
index 0c6d819..b41122b 100644 (file)
@@ -1916,7 +1916,9 @@ class Title {
                        $this->mNamespace != NS_MAIN ) {
                        return false;
                }
-
+               // Allow IPv6 to start with '::' by expanding it.
+               // This trims all input, but that happens anyway.
+               $dbkey = IP::sanitizeIP( $dbkey );
                // Any remaining initial :s are illegal.
                if ( $dbkey !== '' && ':' == $dbkey{0} ) {
                        return false;