X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FIP.php;h=c20ebcda2dc21253e687b64e2c1c94a7b44f0d2b;hb=42fe290d16f66ba3d719bc6149cc79570e55a6b3;hp=355c7b68b36e7bb9059624240fcaf12d786afc1f;hpb=e7f76a6e773e46987e86dae95507309470bd886b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/IP.php b/includes/IP.php index 355c7b68b3..c20ebcda2d 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -18,16 +18,24 @@ define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' ); define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' ); // An IPv6 block is an IP address and a prefix (d1 to d128) define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)'); -// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used. This is lax! -define( 'RE_IPV6_ADD', '(:(:' . RE_IPV6_WORD . '){1,7}|' . RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7})' ); +// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used. +// This is lax! Number of octets/double colons validation not done. +define( 'RE_IPV6_ADD', + '(' . + ':(:' . RE_IPV6_WORD . '){1,7}' . // IPs that start with ":" + '|' . + RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7}' . // IPs that don't start with ":" + ')' +); define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX ); // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network define( 'IP_ADDRESS_STRING', '(?:' . - RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . + RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . // IPv4 '|' . - RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . - ')' ); + RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . // IPv6 + ')' +); /** * A collection of public static functions to play with IP address @@ -52,10 +60,12 @@ class IP { public static function isIPv6( $ip ) { if ( !$ip ) return false; if( is_array( $ip ) ) { - throw new MWException( "invalid value passed to " . __METHOD__ ); + throw new MWException( "invalid value passed to " . __METHOD__ ); } + $doubleColons = substr_count($ip, '::'); // IPv6 IPs with two "::" strings are ambiguous and thus invalid - return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) && ( substr_count($ip, '::') < 2); + return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) + && ( $doubleColons == 1 || substr_count($ip,':') == 7 ); } public static function isIPv4( $ip ) { @@ -123,11 +133,20 @@ class IP { // Remove any whitespaces, convert to upper case $ip = strtoupper( $ip ); // Expand zero abbreviations - if ( strpos( $ip, '::' ) !== false ) { - $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip); + $abbrevPos = strpos( $ip, '::' ); + if ( $abbrevPos !== false ) { + // If the '::' is at the beginning... + if( $abbrevPos == 0 ) { + $repeat = '0:'; $extra = ''; $pad = 9; // 7+2 (due to '::') + // If the '::' is at the end... + } else if( $abbrevPos == (strlen($ip)-2) ) { + $repeat = ':0'; $extra = ''; $pad = 9; // 7+2 (due to '::') + // If the '::' is at the end... + } else { + $repeat = ':0'; $extra = ':'; $pad = 8; // 6+2 (due to '::') + } + $ip = str_replace('::', str_repeat($repeat, $pad-substr_count($ip,':')).$extra, $ip); } - // For IPs that start with "::", correct the final IP so that it starts with '0' and not ':' - if ( $ip[0] == ':' ) $ip = "0$ip"; // Remove leading zereos from each bloc as needed $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip ); return $ip; @@ -502,10 +521,10 @@ class IP { */ public static function isInRange( $addr, $range ) { // Convert to IPv6 if needed - $unsignedIP = self::toUnsigned( $addr ); + $hexIP = self::toHex( $addr ); list( $start, $end ) = self::parseRange( $range ); - return (($unsignedIP >= base_convert($start, 16, 10)) && - ($unsignedIP <= base_convert($end, 16, 10))); + return (strcmp($hexIP, $start) >= 0 && + strcmp($hexIP, $end) <= 0); } /** @@ -522,11 +541,10 @@ class IP { if ( self::isValid( $addr ) ) return $addr; - // Annoying IPv6 representations like ::ffff:1.2.3.4 + // Turn mapped addresses from ::ce:ffff:1.2.3.4 to 1.2.3.4 if ( strpos($addr,':') !==false && strpos($addr,'.') !==false ) { - $addr = str_replace( '.', ':', $addr ); - if( IP::isIPv6( $addr ) ) - return $addr; + $addr = substr( $addr, strrpos($addr,':')+1 ); + if( self::isIPv4($addr) ) return $addr; } // IPv6 loopback address