]> git.mjollnir.org Git - moodle.git/commitdiff
New fucntion to check address is in some subnets
authormoodler <moodler>
Thu, 8 Jul 2004 07:52:01 +0000 (07:52 +0000)
committermoodler <moodler>
Thu, 8 Jul 2004 07:52:01 +0000 (07:52 +0000)
   address_in_subnet

lib/moodlelib.php

index b7cc39d9125d6570dc168d123bddf80f788e9a00..1263e2070a50428a2a87dc70ab9f29c018763813 100644 (file)
@@ -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:
 ?>