From: moodler Date: Thu, 8 Jul 2004 07:52:01 +0000 (+0000) Subject: New fucntion to check address is in some subnets X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=47efbb0490487ac039fe182c7fee303b80abd52d;p=moodle.git New fucntion to check address is in some subnets address_in_subnet --- diff --git a/lib/moodlelib.php b/lib/moodlelib.php index b7cc39d912..1263e2070a 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2600,5 +2600,46 @@ function make_unique_id_code($extra="") { } +/** +* Function to check the passed address is within the passed subnet +* +* The parameter is a comma separated string of subnet definitions. +* Subnet strings can be in one of two formats: +* 1: xxx.xxx.xxx.xxx/xx +* 2: xxx.xxx +* Return boolean +* Code for type 1 modified from user posted comments by mediator at +* http://au.php.net/manual/en/function.ip2long.php +* +* @param addr the address you are checking +* @param subnetstr the string of subnet addresses +*/ + +function address_in_subnet($addr, $subnetstr) { + + $subnets = explode(",", $subnetstr); + $found = false; + $addr = trim($addr); + + foreach ($subnets as $subnet) { + $subnet = trim($subnet); + if (strpos($subnet, "/") !== false) { /// type 1 + + list($ip, $mask) = explode('/', $subnet); + $mask = 0xffffffff << (32 - $mask); + $found = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); + + } else { /// type 2 + $found = (strpos($addr, $subnet) === 0); + } + + if ($found) { + continue; + } + } + + return $found; +} + // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: ?>