From: Ævar Arnfjörð Bjarmason Date: Sun, 31 Dec 2006 09:42:59 +0000 (+0000) Subject: * 001.001.001.001 and other addresses with leading zeros are valid X-Git-Tag: 1.31.0-rc.0~54719 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=dc80288f32970c7efa35f8db5c2afefadc9b3df2;p=lhc%2Fweb%2Fwiklou.git * 001.001.001.001 and other addresses with leading zeros are valid --- diff --git a/includes/IP.php b/includes/IP.php index 16fae9ca2e..edf4af7a6f 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -10,7 +10,7 @@ // Some regex definition to "play" with IP address and IP address blocks // An IP is made of 4 bytes from x00 to xFF which is d0 to d255 -define( 'RE_IP_BYTE', '(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)'); +define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])'); define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE ); // An IP block is an IP address and a prefix (d1 to d32) define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)'); @@ -78,7 +78,7 @@ class IP { /** * Split out an IP block as an array of 4 bytes and a mask, - * return false if it cant be determined + * return false if it can't be determined * * @parameter $ip string A quad dotted IP address * @return array @@ -223,8 +223,8 @@ class IP { $unsignedIP = IP::toUnsigned($addr); list( $start, $end ) = IP::parseRange($range); - $start = hexdec($start); - $end = hexdec($end); + $start = hexdec($start); + $end = hexdec($end); return (($unsignedIP >= $start) && ($unsignedIP <= $end)); } diff --git a/t/inc/IP.t b/t/inc/IP.t index 85fe8cb634..eb4978b932 100644 --- a/t/inc/IP.t +++ b/t/inc/IP.t @@ -3,7 +3,7 @@ require 'Test.php'; -plan( 'no_plan' ); +plan( 1120 ); require_ok( 'includes/IP.php' ); @@ -23,10 +23,15 @@ foreach ( range( 0, 255 ) as $i ) { } } -$valid = array( '216.17.184.1', '0.0.0.0', '000.000.000.000' ); - -foreach ( $valid as $v ) { - ok( IP::isValid( $v ), "$v is a valid IPv4 address" ); +# A bit excessive perhaps? meh.. +foreach ( range( 256, 999 ) as $i ) { + $a = sprintf( "%03d", $i ); + $b = sprintf( "%02d", $i ); + $c = sprintf( "%01d", $i ); + foreach ( array_unique( array( $a, $b, $c ) ) as $f ) { + $ip = "$f.$f.$f.$f"; + ok( ! IP::isValid( $ip ), "$ip is not a valid IPv4 address" ); + } } $invalid = array( @@ -52,9 +57,4 @@ foreach ( $private as $p ) { ok( ! IP::isPublic( $p ), "$p is not a public IP address" ); } -#is ('127.0.0.1', is_loopback_ipv4('127.0.0.1'), 'is_loopback_ipv4 127.0.0.1'); -#is ('192.0.2.9', is_testnet_ipv4('192.0.2.9'), 'is_testnet_ipv4 192.0.2.9'); -#is ('216.17.184.1', is_public_ipv4('216.17.184.1'), 'is_public_ipv4 216.17.184.1'); -#isnt ('192.168.0.1', is_public_ipv4('192.168.0.1'), 'is_public_ipv4 192.168.0.1'); -# -?> \ No newline at end of file +?>