introduce new parameter to allow automatic convert cidr notation to netmask notation
This commit is contained in:
@@ -77,8 +77,7 @@ class Check
|
|||||||
$mysql_access_host_array = array_map('trim', explode(',', $newfieldvalue));
|
$mysql_access_host_array = array_map('trim', explode(',', $newfieldvalue));
|
||||||
|
|
||||||
foreach ($mysql_access_host_array as $host_entry) {
|
foreach ($mysql_access_host_array as $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 != '%') {
|
||||||
if (Validate::validate_ip2($host_entry, true, 'invalidip', true, true, true) == false && Validate::validateDomain($host_entry) == false && Validate::validateLocalHostname($host_entry) == false && $host_entry != '%') {
|
|
||||||
return array(
|
return array(
|
||||||
self::FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
self::FORMFIELDS_PLAUSIBILITY_CHECK_ERROR,
|
||||||
'invalidmysqlhost',
|
'invalidmysqlhost',
|
||||||
|
|||||||
@@ -111,10 +111,12 @@ class Validate
|
|||||||
* whether to allow private network addresses
|
* whether to allow private network addresses
|
||||||
* @param bool $allow_cidr
|
* @param bool $allow_cidr
|
||||||
* whether to allow CIDR values e.g. 10.10.10.10/16
|
* 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
|
* @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)
|
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 = "";
|
$cidr = "";
|
||||||
if ($allow_cidr) {
|
if ($allow_cidr) {
|
||||||
@@ -128,7 +130,7 @@ class Validate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$ip = $ip_cidr[0];
|
$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);
|
$ip_cidr[1] = self::cidr2NetmaskAddr($org_ip);
|
||||||
}
|
}
|
||||||
$cidr = "/" . $ip_cidr[1];
|
$cidr = "/" . $ip_cidr[1];
|
||||||
|
|||||||
@@ -99,6 +99,16 @@ class ValidateTest extends TestCase
|
|||||||
$this->assertEquals("127.0.0.1/32", $result);
|
$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()
|
public function testValidateIpLocalhostAllowedWrongIp()
|
||||||
{
|
{
|
||||||
$this->expectException("Exception");
|
$this->expectException("Exception");
|
||||||
|
|||||||
Reference in New Issue
Block a user