From 71b3ec988e5b898e9268af7dd6927cb64897ef16 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 3 Sep 2007 16:36:25 +0000 Subject: [PATCH] *Convert '.' to ':' for IPs that have both (like '::eff:1.4.0.3') *Expand IP dbkeys like "::fff:3e" rather than complain about the double colons --- includes/IP.php | 31 +++++++++++++++++++------------ includes/Title.php | 4 +++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/includes/IP.php b/includes/IP.php index 4c24ed9b72..db712c3bcd 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -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 } } diff --git a/includes/Title.php b/includes/Title.php index 0c6d81987a..b41122ba64 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -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; -- 2.20.1