MDL-67861 libraries: Refactor is_ip_in_subnet_list in ip_utils

This commit is contained in:
Brendan Heywood
2020-03-05 01:27:48 +01:00
committed by Eloy Lafuente (stronk7)
parent c70d7d0386
commit c4665bcf95
3 changed files with 50 additions and 12 deletions
+21
View File
@@ -224,4 +224,25 @@ final class ip_utils {
}
return false;
}
/**
* Is an ip in a given list of subnets?
*
* @param string $ip - the IP to test against the list
* @param string $list - the list of IP subnets
* @param string $delim a delimiter of the list
* @return bool
*/
public static function is_ip_in_subnet_list($ip, $list, $delim = "\n") {
$list = explode($delim, $list);
foreach ($list as $line) {
$tokens = explode('#', $line);
$subnet = trim($tokens[0]);
if (address_in_subnet($ip, $subnet)) {
return true;
}
}
return false;
}
}