Add IP::isInRanges()
authorReedy <reedy@wikimedia.org>
Tue, 11 Nov 2014 16:16:06 +0000 (16:16 +0000)
committerUmherirrender <umherirrender_de.wp@web.de>
Fri, 28 Nov 2014 16:51:33 +0000 (16:51 +0000)
cf https://github.com/wikimedia/operations-mediawiki-config/blob/master/wmf-config/throttle.php#L43-L58

Change-Id: I8ae145035de9d349710971f17551da637e4d2242

includes/utils/IP.php

index 0e2db8c..77f9cd1 100644 (file)
@@ -628,6 +628,25 @@ class IP {
                        strcmp( $hexIP, $end ) <= 0 );
        }
 
+       /**
+        * Determines if an IP address is a list of CIDR a.b.c.d/n ranges.
+        *
+        * @since 1.25
+        *
+        * @param string $ip the IP to check
+        * @param array $ranges the IP ranges, each element a range
+        *
+        * @return bool true if the specified adress belongs to the specified range; otherwise, false.
+        */
+       public static function isInRanges( $ip, $ranges ) {
+               foreach ( $ranges as $range ) {
+                       if ( self::isInRange( $ip, $range ) ) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
        /**
         * Convert some unusual representations of IPv4 addresses to their
         * canonical dotted quad representation.