summaryrefslogtreecommitdiff
path: root/it.class
diff options
context:
space:
mode:
authorChristian A. Weber2016-02-01 15:42:04 +0100
committerChristian A. Weber2016-02-01 15:45:58 +0100
commit7b076048688ddd20fc698d19e1921155de8c48e5 (patch)
treebdceef3bf214c27130dec0f438e74a2295f0ac39 /it.class
parent2ba8c034b134be1af855e1bfcb0b5acadc58cc2d (diff)
downloaditools-7b076048688ddd20fc698d19e1921155de8c48e5.tar.gz
itools-7b076048688ddd20fc698d19e1921155de8c48e5.tar.bz2
itools-7b076048688ddd20fc698d19e1921155de8c48e5.zip
add support for arrays of cidrs in cidr_match()
Diffstat (limited to 'it.class')
-rw-r--r--it.class25
1 files changed, 16 insertions, 9 deletions
diff --git a/it.class b/it.class
index cfad675..b533c3f 100644
--- a/it.class
+++ b/it.class
@@ -317,18 +317,25 @@ static function toascii($text)
/**
* Check whether an IP adress lies within a given range. Supports IPv4 and IPv6
* @param $ip IP address (192.168.42.123)
- * @param $cidr IP range in CIDR notation (192.168.42.64/26)
+ * @param $cidrs IP range in CIDR notation (192.168.42.64/26) or array of ranges
* @return true if $ip is within $cidr
*/
-function cidr_match($ip, $cidr)
+function cidr_match($ip, $cidrs)
{
- list($subnet, $mask) = explode('/', $cidr);
- $ip_bin = inet_pton($ip);
- $subnet_bin = inet_pton($subnet);
- $valid_bytes = $mask ? $mask >> 3 : 42;
- $bitmask = 256 - (1 << (8 - ($mask & 7)));
- $lastbyte_matched = $bitmask ? (ord($ip_bin{$valid_bytes}) & $bitmask) == (ord($subnet_bin{$valid_bytes}) & $bitmask) : true;
- return substr($ip_bin, 0, $valid_bytes) == substr($subnet_bin, 0, $valid_bytes) && $lastbyte_matched;
+ foreach ((array)$cidrs as $cidr)
+ {
+ list($subnet, $mask) = explode('/', $cidr);
+ $ip_bin = inet_pton($ip);
+ $subnet_bin = inet_pton($subnet);
+ $valid_bytes = $mask ? $mask >> 3 : 42;
+ $bitmask = 256 - (1 << (8 - ($mask & 7)));
+ $lastbyte_matched = $bitmask ? (ord($ip_bin{$valid_bytes}) & $bitmask) == (ord($subnet_bin{$valid_bytes}) & $bitmask) : true;
+
+ if (substr($ip_bin, 0, $valid_bytes) == substr($subnet_bin, 0, $valid_bytes) && $lastbyte_matched)
+ return true;
+ }
+
+ return false;
}