Bump RL filter version to 4 to keep it in sync with the cluster. WMF-centrism, I...
[lhc/web/wiklou.git] / includes / IP.php
index 0883992..9b55ad2 100644 (file)
@@ -39,17 +39,18 @@ define( 'RE_IPV6_ADD',
                ':(?::|(?::' . RE_IPV6_WORD . '){1,7})' .
        '|' . // ends with "::" (except "::")
                RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){0,6}::' .
-       '|' . // contains no "::"
-               RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' .
-       '|' . // contains one "::" in the middle and 2 words
-               RE_IPV6_WORD . '::' . RE_IPV6_WORD .
-       '|' . // contains one "::" in the middle and 3+ words (awkward regex for PCRE 4.0+)
+       '|' . // contains one "::" in the middle, ending in "::WORD"
+               RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){0,5}' . '::' . RE_IPV6_WORD .
+       '|' . // contains one "::" in the middle, not ending in "::WORD" (regex for PCRE 4.0+)
                RE_IPV6_WORD . '(?::(?P<abn>:(?P<iabn>))?' . RE_IPV6_WORD . '(?!:(?P=abn))){1,5}' .
                        ':' . RE_IPV6_WORD . '(?P=iabn)' .
                // NOTE: (?!(?P=abn)) fails iff "::" used twice; (?P=iabn) passes iff a "::" was found.
-               // RegExp (PCRE 7.2+ only) for last 2 cases that allows easy regex concatenation:
-               #RE_IPV6_WORD . '(?::((?(-1)|:))?' . RE_IPV6_WORD . '){1,6}(?(-2)|^)' .
+       '|' . // contains no "::"
+               RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' .
        ')'
+       // NOTE: With PCRE 7.2+, we can combine the two '"::" in the middle' cases into:
+       //              RE_IPV6_WORD . '(?::((?(-1)|:))?' . RE_IPV6_WORD . '){1,6}(?(-2)|^)'
+       // This also improves regex concatenation by using relative references.
 );
 // An IPv6 block is an IP address and a prefix (d1 to d128)
 define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
@@ -613,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";
+       }
 }