diff options
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -315,6 +315,24 @@ 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) + * @return true if $ip is within $cidr + */ +function cidr_match($ip, $cidr) +{ + list($subnet, $mask) = explode('/', $cidr); + $ip_bin = inet_pton($ip); + $subnet_bin = inet_pton($subnet); + $valid_bytes = $mask >> 3; + $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; +} + + +/** * Convert regex for preg (adds and escapes delimiter, adds modifiers) * @param $pattern Regex to convert * @param $p['casesensitive'] Regex is case sensitive (omit modifier i) |