From 45239a493a822d0f61287cf044d1b8697d26601e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 13 Mar 2007 17:15:02 +0000 Subject: [PATCH] *sanitizeIP() now adds leading zeroes to each bloc for better consistency --- includes/IP.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/includes/IP.php b/includes/IP.php index 1f13821388..599482c83f 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -99,12 +99,18 @@ class IP { if ( !$ip ) return null; // Only IPv6 addresses can be expanded if ( !self::isIPv6( $ip ) ) return $ip; - // Convert to upper case - $ip = strtoupper( $ip ); + // Remove any whitespaces, convert to upper case + $ip = strtoupper( trim($ip) ); // Expand zero abbreviations if ( substr_count($ip, '::') ) { $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip); } + // Add leading zereos to each bloc as needed + $ip = explode( ':', $ip ); + foreach ( $ip as $v ) { + $v = str_pad( $v, 4, 0, STR_PAD_LEFT ); + } + $ip = implode( ':', $ip ); return $ip; } @@ -119,8 +125,8 @@ class IP { // Seperate into 8 octets $ip_oct = wfBaseConvert( substr( $ip_int, 0, 16 ), 2, 16, 1, false ); for ($n=1; $n < 8; $n++) { - // Convert to hex, and add ":" marks - $ip_oct .= ':' . wfBaseConvert( substr($ip_int, 16*$n, 16), 2, 16, 1, false ); + // Convert to hex, and add ":" marks, with leading zeroes + $ip_oct .= ':' . wfBaseConvert( substr($ip_int, 16*$n, 16), 2, 16, 4, false ); } return $ip_oct; } -- 2.20.1