From: Happy-melon Date: Fri, 18 Mar 2011 23:28:23 +0000 (+0000) Subject: Move Block::normaliseRange() to IP.php, then reduce it to 4 lines by using the existi... X-Git-Tag: 1.31.0-rc.0~31329 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=99f39a47798bc47222064acfc0cc670ee5c70e54;p=lhc%2Fweb%2Fwiklou.git Move Block::normaliseRange() to IP.php, then reduce it to 4 lines by using the existing functionality in that class. --- diff --git a/includes/Block.php b/includes/Block.php index 62ede18fa1..8a26da4b09 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -813,35 +813,10 @@ class Block { * For example, 127.111.113.151/24 -> 127.111.113.0/24 * @param $range String: IP address to normalize * @return string + * @deprecated since 1.18, call IP::sanitizeRange() directly */ public static function normaliseRange( $range ) { - $parts = explode( '/', $range ); - if ( count( $parts ) == 2 ) { - // IPv6 - if ( IP::isIPv6( $range ) && $parts[1] >= 64 && $parts[1] <= 128 ) { - $bits = $parts[1]; - $ipint = IP::toUnsigned( $parts[0] ); - # Native 32 bit functions WON'T work here!!! - # Convert to a padded binary number - $network = wfBaseConvert( $ipint, 10, 2, 128 ); - # Truncate the last (128-$bits) bits and replace them with zeros - $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT ); - # Convert back to an integer - $network = wfBaseConvert( $network, 2, 10 ); - # Reform octet address - $newip = IP::toOctet( $network ); - $range = "$newip/{$parts[1]}"; - } // IPv4 - elseif ( IP::isIPv4( $range ) && $parts[1] >= 16 && $parts[1] <= 32 ) { - $shift = 32 - $parts[1]; - $ipint = IP::toUnsigned( $parts[0] ); - $ipint = $ipint >> $shift << $shift; - $newip = long2ip( $ipint ); - $range = "$newip/{$parts[1]}"; - } - } - - return $range; + return IP::sanitizeRange( $range ); } /** diff --git a/includes/IP.php b/includes/IP.php index 5f492c66b4..9b55ad22fc 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -614,4 +614,17 @@ class IP { return null; // give up } + + /** + * Gets rid of uneeded numbers in quad-dotted/octet IP strings + * For example, 127.111.113.151/24 -> 127.111.113.0/24 + * @param $range String: IP address to normalize + * @return string + */ + public static function sanitizeRange( $range ){ + list( /*...*/, $bits ) = self::parseCIDR( $range ); + list( $start, /*...*/ ) = self::parseRange( $range ); + $start = self::formatHex( $start ); + return "$start/$bits"; + } }