From 53007faad1b1d4e806031f63c7fe5df2236ae35a Mon Sep 17 00:00:00 2001 From: Evan Giles Date: Thu, 3 May 2018 08:59:48 +1000 Subject: [PATCH] MDL-57404 admin_settings: Allow comments in ip block This adds the ability to to add comments to ip lists in moodle. Currently, the configiplist textentry box does not allow the user to annotate the IP addresses defined. For example, I'd like to be able to define: 192.168.1.1 # London office 10.1.1.1 # New york office 118.209.246.240 # My home IP This would allow me to revisit this list after a few months, and remove IP addresses that are no longer required - without having to manually confirm each IP address --- lang/en/admin.php | 2 +- lib/adminlib.php | 7 ++++--- lib/moodlelib.php | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lang/en/admin.php b/lang/en/admin.php index aa79e8e6f78..0103646de7e 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -631,7 +631,7 @@ $string['invalidsection'] = 'Invalid section.'; $string['invaliduserchangeme'] = 'Username "changeme" is reserved -- you cannot create an account with it.'; $string['ipblocked'] = 'This site is not available currently.'; $string['ipblocker'] = 'IP blocker'; -$string['ipblockersyntax'] = 'Put every entry on one line. Valid entries are either full IP address (such as 192.168.10.1) which matches a single host; or partial address (such as 192.168) which matches any address starting with those numbers; or CIDR notation (such as 231.54.211.0/20); or a range of IP addresses (such as 231.3.56.10-20) where the range applies to the last part of the address. Text domain names (like \'example.com\') are not supported. Blank lines are ignored.'; +$string['ipblockersyntax'] = 'Put every entry on one line. Valid entries are either full IP address (such as 192.168.10.1) which matches a single host; or partial address (such as 192.168) which matches any address starting with those numbers; or CIDR notation (such as 231.54.211.0/20); or a range of IP addresses (such as 231.3.56.10-20) where the range applies to the last part of the address. Text domain names (like \'example.com\') are not supported. Blank lines, and text following a "#" character are ignored.'; $string['iplookup'] = 'IP address lookup'; $string['iplookupgeoplugin'] = 'geoPlugin service is currently being used to look up geographical information. For more accurate results we recommend installing a local copy of the MaxMind GeoLite database.'; $string['iplookupinfo'] = 'By default Moodle uses the free online NetGeo (The Internet Geographic Database) server to lookup location of IP addresses, unfortunately this database is not maintained anymore and may return wildly incorrect data. diff --git a/lib/adminlib.php b/lib/adminlib.php index b14741ef7a4..c3e39cd1e1f 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3614,14 +3614,15 @@ class admin_setting_configiplist extends admin_setting_configtextarea { */ public function validate($data) { if(!empty($data)) { - $ips = explode("\n", $data); + $lines = explode("\n", $data); } else { return true; } $result = true; $badips = array(); - foreach($ips as $ip) { - $ip = trim($ip); + foreach ($lines as $line) { + $tokens = explode('#', $line); + $ip = trim($tokens[0]); if (empty($ip)) { continue; } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 446807ef262..34fe56bb108 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8888,7 +8888,7 @@ function cleardoubleslashes ($path) { } /** - * Is current ip in give list? + * Is the current ip in a given list? * * @param string $list * @return bool @@ -8903,8 +8903,9 @@ function remoteip_in_list($list) { } $list = explode("\n", $list); - foreach ($list as $subnet) { - $subnet = trim($subnet); + foreach ($list as $line) { + $tokens = explode('#', $line); + $subnet = trim($tokens[0]); if (address_in_subnet($clientip, $subnet)) { $inlist = true; break;