diff --git a/lib/Froxlor/Validate/Check.php b/lib/Froxlor/Validate/Check.php index 14f30fe3..041381f1 100644 --- a/lib/Froxlor/Validate/Check.php +++ b/lib/Froxlor/Validate/Check.php @@ -77,8 +77,7 @@ class Check $mysql_access_host_array = array_map('trim', explode(',', $newfieldvalue)); foreach ($mysql_access_host_array as $host_entry) { - - if (Validate::validate_ip2($host_entry, true, 'invalidip', true, true, true) == false && Validate::validateDomain($host_entry) == false && Validate::validateLocalHostname($host_entry) == false && $host_entry != '%') { + if (Validate::validate_ip2($host_entry, true, 'invalidip', true, true, true, false, true) == false && Validate::validateDomain($host_entry) == false && Validate::validateLocalHostname($host_entry) == false && $host_entry != '%') { return array( self::FORMFIELDS_PLAUSIBILITY_CHECK_ERROR, 'invalidmysqlhost', diff --git a/lib/Froxlor/Validate/Validate.php b/lib/Froxlor/Validate/Validate.php index 0e23ad1c..edc2a860 100644 --- a/lib/Froxlor/Validate/Validate.php +++ b/lib/Froxlor/Validate/Validate.php @@ -96,25 +96,27 @@ class Validate ($ipv4_mapped_ipv6 === FALSE || $ipv4_mapped_ipv6 != 0); } - /** - * Checks whether it is a valid ip - * - * @param string $ip - * ip-address to check - * @param bool $return_bool - * whether to return bool or call \Froxlor\UI\Response::standard_error() - * @param string $lng - * index for error-message (if $return_bool is false) - * @param bool $allow_localhost - * whether to allow 127.0.0.1 - * @param bool $allow_priv - * whether to allow private network addresses - * @param bool $allow_cidr - * whether to allow CIDR values e.g. 10.10.10.10/16 - * - * @return string|bool ip address on success, false on failure - */ - public static function validate_ip2($ip, $return_bool = false, $lng = 'invalidip', $allow_localhost = false, $allow_priv = false, $allow_cidr = false, $throw_exception = false) + /** + * Checks whether it is a valid ip + * + * @param string $ip + * ip-address to check + * @param bool $return_bool + * whether to return bool or call \Froxlor\UI\Response::standard_error() + * @param string $lng + * index for error-message (if $return_bool is false) + * @param bool $allow_localhost + * whether to allow 127.0.0.1 + * @param bool $allow_priv + * whether to allow private network addresses + * @param bool $allow_cidr + * whether to allow CIDR values e.g. 10.10.10.10/16 + * @param bool $cidr_as_netmask + * whether to format CIDR nodation to netmask notation + * + * @return string|bool ip address on success, false on failure + */ + public static function validate_ip2($ip, $return_bool = false, $lng = 'invalidip', $allow_localhost = false, $allow_priv = false, $allow_cidr = false, $throw_exception = false, $cidr_as_netmask = false) { $cidr = ""; if ($allow_cidr) { @@ -128,7 +130,7 @@ class Validate } } $ip = $ip_cidr[0]; - if (strlen($ip_cidr[1]) <= 2) { + if ($cidr_as_netmask && strlen($ip_cidr[1]) <= 2) { $ip_cidr[1] = self::cidr2NetmaskAddr($org_ip); } $cidr = "/" . $ip_cidr[1]; diff --git a/tests/Froxlor/ValidateTest.php b/tests/Froxlor/ValidateTest.php index 6600d5f1..0222b991 100644 --- a/tests/Froxlor/ValidateTest.php +++ b/tests/Froxlor/ValidateTest.php @@ -99,6 +99,16 @@ class ValidateTest extends TestCase $this->assertEquals("127.0.0.1/32", $result); } + public function testValidateCidrNoationToNetmaskNotationIPv4() + { + $result = Validate::validate_ip2("1.1.1.1/4", false, 'invalidip', true, false, true, true, true); + $this->assertEquals("1.1.1.1/240.0.0.0", $result); + $result = Validate::validate_ip2("8.8.8.8/18", false, 'invalidip', true, false, true, true, true); + $this->assertEquals("8.8.8.8/255.255.192.0", $result); + $result = Validate::validate_ip2("8.8.8.8/1", false, 'invalidip', true, false, true, true, true); + $this->assertEquals("8.8.8.8/128.0.0.0", $result); + } + public function testValidateIpLocalhostAllowedWrongIp() { $this->expectException("Exception");