From 04a0d93878523b5163cb4661723350b20cc71ab4 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 22 Mar 2007 22:08:31 +0000 Subject: [PATCH] *Fix comment typo, remove some redundant conversions --- includes/IP.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/IP.php b/includes/IP.php index 03a3724f7c..9567f1effa 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -36,13 +36,13 @@ class IP { */ public static function isIPAddress( $ip ) { if ( !$ip ) return false; - // IPv6 IPs with two "::" strings are ambiguous and this invalid + // IPv6 IPs with two "::" strings are ambiguous and thus invalid return preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip) && ( substr_count($ip, '::') < 2 ); } public static function isIPv6( $ip ) { if ( !$ip ) return false; - // IPv6 IPs with two "::" strings are ambiguous and this invalid + // IPv6 IPs with two "::" strings are ambiguous and thus invalid return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) && ( substr_count($ip, '::') < 2); } @@ -101,6 +101,8 @@ class IP { */ public static function sanitizeIP( $ip ) { if ( !$ip ) return null; + // Trim and return IPv4 addresses + if ( self::isIPv4($ip) ) return trim($ip); // Only IPv6 addresses can be expanded if ( !self::isIPv6($ip) ) return $ip; // Remove any whitespaces, convert to upper case @@ -131,7 +133,7 @@ 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, with NO leading zeroes + // Convert to uppercase hex, and add ":" marks, with NO leading zeroes $ip_oct .= ':' . wfBaseConvert( substr($ip_int, 16*$n, 16), 2, 16, 1, false ); } return $ip_oct; @@ -424,15 +426,15 @@ class IP { } /** - * Determine if a given IPv4 address is in a given CIDR network + * Determine if a given IPv4/IPv6 address is in a given CIDR network * @param $addr The address to check against the given range. * @param $range The range to check the given address against. * @return bool Whether or not the given address is in the given range. */ public static function isInRange( $addr, $range ) { // Convert to IPv6 if needed - $unsignedIP = self::toUnsigned6( self::IPv4toIPv6($addr) ); - list( $start, $end ) = self::parseRange6( self::IPv4toIPv6($range) ); + $unsignedIP = self::toUnsigned( $addr ); + list( $start, $end ) = self::parseRange($range ); return (($unsignedIP >= $start) && ($unsignedIP <= $end)); } -- 2.20.1