From df90796155338021cf502da8c8f54047c00d2d75 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 16 Oct 2006 13:38:36 +0000 Subject: [PATCH] MDL-6249 - easier way for human beings (as opposed to network engineers) to specify IP ranges. Merged from MOODLE_17_STABLE. --- lang/en_utf8/help/quiz/requiresubnet.html | 11 +++++++---- lib/moodlelib.php | 15 +++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lang/en_utf8/help/quiz/requiresubnet.html b/lang/en_utf8/help/quiz/requiresubnet.html index 68bb109392..f989d87cfc 100644 --- a/lang/en_utf8/help/quiz/requiresubnet.html +++ b/lang/en_utf8/help/quiz/requiresubnet.html @@ -10,10 +10,11 @@ to be sure that only people in a certain room are able to access the quiz.

-

For example: 192.168. , 231.54.211.0/20, 231.3.56.211

+

For example: 192.168. , 231.54.211.0/20, 231.3.56.211, 231.3.56.10-20

-

There are three types of numbers you can use (you can not use - text based domain names like example.com): +

There are four types of numbers you can use (you can not use + text based domain names like example.com):

+
  1. Full IP addresses, such as 192.168.10.1 which will match a single computer (or proxy).
  2. @@ -21,7 +22,9 @@ starting with those numbers.
  3. CIDR notation, such as 231.54.211.0/20 which allows you to specify more detailed subnets.
  4. +
  5. A range of IP addresses 231.3.56.10-20 The range applies to the last + part of the address, so this means all the IP addresses from 231.3.56.10 + to 231.3.56.20.
-

Spaces are ignored.

diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 283483ebbc..b78fd10214 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5950,9 +5950,10 @@ 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: + * Subnet strings can be in one of three formats: * 1: xxx.xxx.xxx.xxx/xx * 2: xxx.xxx + * 3: xxx.xxx.xxx.xxx-xxx //a range of IP addresses in the last group. * Code for type 1 modified from user posted comments by mediator at * {@link http://au.php.net/manual/en/function.ip2long.php} * @@ -5969,11 +5970,18 @@ function address_in_subnet($addr, $subnetstr) { 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 if (strpos($subnet, '-') !== false) {/// type 3 + $subnetparts = explode('.', $subnet); + $addrparts = explode('.', $addr); + $subnetrange = explode('-', array_pop($subnetparts)); + if (count($subnetrange) == 2) { + $lastaddrpart = array_pop($addrparts); + $found = ($subnetparts == $addrparts && + $subnetrange[0] <= $lastaddrpart && $lastaddrpart <= $subnetrange[1]); + } } else { /// type 2 $found = (strpos($addr, $subnet) === 0); } @@ -5982,7 +5990,6 @@ function address_in_subnet($addr, $subnetstr) { break; } } - return $found; } -- 2.39.5