X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FIP.php;h=3bfb531f5ec1aa484fa6527586eec5fd47d46e6b;hb=237d3271fd313ebe09858a5c442a91216a7b61cf;hp=e8b0e6a77084b45d28a20d53c3a4d605e28d1090;hpb=d56b46598d58f16eb8d7c8252083bbf3844e5430;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/IP.php b/includes/libs/IP.php index e8b0e6a770..3bfb531f5e 100644 --- a/includes/libs/IP.php +++ b/includes/libs/IP.php @@ -23,14 +23,14 @@ use IPSet\IPSet; -// Some regex definition to "play" with IP address and IP address blocks +// Some regex definition to "play" with IP address and IP address ranges // An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255 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 IPv4 block is an IP address and a prefix (d1 to d32) +// An IPv4 range is an IP address and a prefix (d1 to d32) define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' ); -define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX ); +define( 'RE_IP_RANGE', RE_IP_ADD . '\/' . RE_IP_PREFIX ); // An IPv6 address is made up of 8 words (each x0000 to xFFFF). // However, the "::" abbreviation can be used on consecutive x0000 words. @@ -47,8 +47,8 @@ define( 'RE_IPV6_ADD', RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' . ')' ); -// An IPv6 block is an IP address and a prefix (d1 to d128) -define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX ); +// An IPv6 range is an IP address and a prefix (d1 to d128) +define( 'RE_IPV6_RANGE', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX ); // For IPv6 canonicalization (NOT for strict validation; these are quite lax!) define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' ); define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' ); @@ -64,11 +64,9 @@ define( 'IP_ADDRESS_STRING', /** * A collection of public static functions to play with IP address - * and IP blocks. + * and IP ranges. */ class IP { - /** @var IPSet */ - private static $proxyIpSet = null; /** * Determine if a string is as valid IP address or network (CIDR prefix). @@ -118,16 +116,30 @@ class IP { } /** - * Validate an IP Block (valid address WITH a valid prefix). + * Validate an IP range (valid address with a valid CIDR prefix). * SIIT IPv4-translated addresses are rejected. * @note canonicalize() tries to convert translated addresses to IPv4. * - * @param string $ipblock + * @deprecated since 1.30. Use the equivalent IP::isValidRange(). + * @param string $ipRange * @return bool True if it is valid */ - public static function isValidBlock( $ipblock ) { - return ( preg_match( '/^' . RE_IPV6_BLOCK . '$/', $ipblock ) - || preg_match( '/^' . RE_IP_BLOCK . '$/', $ipblock ) ); + public static function isValidBlock( $ipRange ) { + return self::isValidRange( $ipRange ); + } + + /** + * Validate an IP range (valid address with a valid CIDR prefix). + * SIIT IPv4-translated addresses are rejected. + * @note canonicalize() tries to convert translated addresses to IPv4. + * + * @param string $ipRange + * @return bool True if it is valid + * @since 1.30 + */ + public static function isValidRange( $ipRange ) { + return ( preg_match( '/^' . RE_IPV6_RANGE . '$/', $ipRange ) + || preg_match( '/^' . RE_IP_RANGE . '$/', $ipRange ) ); } /** @@ -549,7 +561,7 @@ class IP { */ private static function parseCIDR6( $range ) { # Explode into - $parts = explode( '/', IP::sanitizeIP( $range ), 2 ); + $parts = explode( '/', self::sanitizeIP( $range ), 2 ); if ( count( $parts ) != 2 ) { return [ false, false ]; } @@ -590,7 +602,7 @@ class IP { */ private static function parseRange6( $range ) { # Expand any IPv6 IP - $range = IP::sanitizeIP( $range ); + $range = self::sanitizeIP( $range ); // CIDR notation... if ( strpos( $range, '/' ) !== false ) { list( $network, $bits ) = self::parseCIDR6( $range ); @@ -732,8 +744,8 @@ class IP { public static function getSubnet( $ip ) { $matches = []; $subnet = false; - if ( IP::isIPv6( $ip ) ) { - $parts = IP::parseRange( "$ip/64" ); + if ( self::isIPv6( $ip ) ) { + $parts = self::parseRange( "$ip/64" ); $subnet = $parts[0]; } elseif ( preg_match( '/^(\d+\.\d+\.\d+)\.\d+$/', $ip, $matches ) ) { // IPv4