Commit acb242ef authored by Ad Schellevis's avatar Ad Schellevis

(mvc, model) add support for lists of networks/addresses in NetworkField,...

(mvc, model) add support for lists of networks/addresses in NetworkField, requirement for https://github.com/opnsense/core/issues/1226
parent f8645117
...@@ -52,6 +52,11 @@ class NetworkField extends BaseField ...@@ -52,6 +52,11 @@ class NetworkField extends BaseField
*/ */
protected $internalNetMaskRequired = false; protected $internalNetMaskRequired = false;
/**
* @var null when multiple values could be provided at once, specify the split character
*/
protected $internalFieldSeparator = null;
/** /**
* always lowercase / trim networks * always lowercase / trim networks
* @param string $value * @param string $value
...@@ -74,6 +79,15 @@ class NetworkField extends BaseField ...@@ -74,6 +79,15 @@ class NetworkField extends BaseField
} }
} }
/**
* if multiple addresses / networks maybe provided at once, set separator.
* @param $value separator
*/
public function setFieldSeparator($value)
{
$this->internalFieldSeparator = $value;
}
/** /**
* retrieve field validators for this field type * retrieve field validators for this field type
* @return array returns Text/regex validator * @return array returns Text/regex validator
...@@ -86,6 +100,7 @@ class NetworkField extends BaseField ...@@ -86,6 +100,7 @@ class NetworkField extends BaseField
// accept any as target // accept any as target
$validators[] = new NetworkValidator(array( $validators[] = new NetworkValidator(array(
'message' => $this->internalValidationMessage, 'message' => $this->internalValidationMessage,
'split' => $this->internalFieldSeparator,
'netMaskRequired' => $this->internalNetMaskRequired 'netMaskRequired' => $this->internalNetMaskRequired
)); ));
} }
......
...@@ -57,63 +57,69 @@ class NetworkValidator extends Validator implements ValidatorInterface ...@@ -57,63 +57,69 @@ class NetworkValidator extends Validator implements ValidatorInterface
public function validate(\Phalcon\Validation $validator, $attribute) public function validate(\Phalcon\Validation $validator, $attribute)
{ {
$result = true; $result = true;
$value = $validator->getValue($attribute);
$msg = $this->getOption('message'); $msg = $this->getOption('message');
$fieldSplit = $this->getOption('split', null);
// parse filter options if ($fieldSplit == null) {
$filterOpt = 0; $values = array($validator->getValue($attribute));
switch (strtolower($this->getOption('version'))) { } else {
case "ipv4": $values = explode($fieldSplit, $validator->getValue($attribute));
$filterOpt |= FILTER_FLAG_IPV4;
break;
case "ipv6":
$filterOpt |= FILTER_FLAG_IPV6;
break;
default:
$filterOpt |= FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6;
} }
foreach ($values as $value) {
// parse filter options
$filterOpt = 0;
switch (strtolower($this->getOption('version'))) {
case "ipv4":
$filterOpt |= FILTER_FLAG_IPV4;
break;
case "ipv6":
$filterOpt |= FILTER_FLAG_IPV6;
break;
default:
$filterOpt |= FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6;
}
if ($this->getOption('noReserved') === true) { if ($this->getOption('noReserved') === true) {
$filterOpt |= FILTER_FLAG_NO_RES_RANGE; $filterOpt |= FILTER_FLAG_NO_RES_RANGE;
} }
if ($this->getOption('noPrivate') === true) { if ($this->getOption('noPrivate') === true) {
$filterOpt |= FILTER_FLAG_NO_PRIV_RANGE; $filterOpt |= FILTER_FLAG_NO_PRIV_RANGE;
} }
// split network // split network
if (strpos($value, "/") !== false) { if (strpos($value, "/") !== false) {
$parts = explode("/", $value); $parts = explode("/", $value);
if (count($parts) > 2 || !ctype_digit($parts[1])) { if (count($parts) > 2 || !ctype_digit($parts[1])) {
// more parts then expected or second part is not numeric // more parts then expected or second part is not numeric
$result = false; $result = false;
} else {
$mask = $parts[1];
$value = $parts[0];
if (strpos($parts[0], ".")) {
// most likely ipv4 address, mask must be between 0..32
if ($mask < 0 || $mask > 32) {
$result = false;
}
} else { } else {
// probably ipv6, mask must be between 0..128 $mask = $parts[1];
if ($mask < 0 || $mask > 128) { $value = $parts[0];
$result = false; if (strpos($parts[0], ".")) {
// most likely ipv4 address, mask must be between 0..32
if ($mask < 0 || $mask > 32) {
$result = false;
}
} else {
// probably ipv6, mask must be between 0..128
if ($mask < 0 || $mask > 128) {
$result = false;
}
} }
} }
} elseif ($this->getOption('netMaskRequired') === true) {
$result = false;
} }
} elseif ($this->getOption('netMaskRequired') === true) {
$result = false;
}
if (filter_var($value, FILTER_VALIDATE_IP, $filterOpt) === false) { if (filter_var($value, FILTER_VALIDATE_IP, $filterOpt) === false) {
$result = false; $result = false;
} }
if (!$result) { if (!$result) {
// append validation message // append validation message
$validator->appendMessage(new Message($msg, $attribute, 'NetworkValidator')); $validator->appendMessage(new Message($msg, $attribute, 'NetworkValidator'));
}
} }
return $result; return $result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment