From 60c81030b35c5503cef34521a2c86360d4d2d366 Mon Sep 17 00:00:00 2001 From: biodigitalfish <11165243+biodigitalfish@users.noreply.github.com> Date: Thu, 2 Nov 2023 17:17:35 +1300 Subject: [PATCH] Add validSubnetMask function This function is used to validate a subnetmask Signed-off-by: biodigitalfish <11165243+biodigitalfish@users.noreply.github.com> --- scripts/pi-hole/php/func.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index b345ca87d7..cac5dd7bec 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -67,6 +67,28 @@ function validIP($address) return !filter_var($address, FILTER_VALIDATE_IP) === false; } +function validSubnetMask($subnetMask) +{ + $octets = explode('.', $subnetMask); + if (count($octets) != 4) { + return false; + } + + $subnetBinary = ''; + foreach ($octets as $octet) { + if (!is_numeric($octet) || $octet < 0 || $octet > 255) { + return false; + } + $subnetBinary .= str_pad(decbin($octet), 8, '0', STR_PAD_LEFT); + } + + if (strpos($subnetBinary, '01') !== false) { + return false; + } + + return true; +} + function validCIDRIP($address) { // This validation strategy has been taken from ../js/groups-common.js