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
*/
protected $internalNetMaskRequired = false;
/**
* @var null when multiple values could be provided at once, specify the split character
*/
protected $internalFieldSeparator = null;
/**
* always lowercase / trim networks
* @param string $value
......@@ -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
* @return array returns Text/regex validator
......@@ -86,6 +100,7 @@ class NetworkField extends BaseField
// accept any as target
$validators[] = new NetworkValidator(array(
'message' => $this->internalValidationMessage,
'split' => $this->internalFieldSeparator,
'netMaskRequired' => $this->internalNetMaskRequired
));
}
......
......@@ -57,63 +57,69 @@ class NetworkValidator extends Validator implements ValidatorInterface
public function validate(\Phalcon\Validation $validator, $attribute)
{
$result = true;
$value = $validator->getValue($attribute);
$msg = $this->getOption('message');
// 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;
$fieldSplit = $this->getOption('split', null);
if ($fieldSplit == null) {
$values = array($validator->getValue($attribute));
} else {
$values = explode($fieldSplit, $validator->getValue($attribute));
}
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) {
$filterOpt |= FILTER_FLAG_NO_RES_RANGE;
}
if ($this->getOption('noReserved') === true) {
$filterOpt |= FILTER_FLAG_NO_RES_RANGE;
}
if ($this->getOption('noPrivate') === true) {
$filterOpt |= FILTER_FLAG_NO_PRIV_RANGE;
}
if ($this->getOption('noPrivate') === true) {
$filterOpt |= FILTER_FLAG_NO_PRIV_RANGE;
}
// split network
if (strpos($value, "/") !== false) {
$parts = explode("/", $value);
if (count($parts) > 2 || !ctype_digit($parts[1])) {
// more parts then expected or second part is not numeric
$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;
}
// split network
if (strpos($value, "/") !== false) {
$parts = explode("/", $value);
if (count($parts) > 2 || !ctype_digit($parts[1])) {
// more parts then expected or second part is not numeric
$result = false;
} else {
// probably ipv6, mask must be between 0..128
if ($mask < 0 || $mask > 128) {
$result = false;
$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 {
// 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) {
$result = false;
}
if (filter_var($value, FILTER_VALIDATE_IP, $filterOpt) === false) {
$result = false;
}
if (!$result) {
// append validation message
$validator->appendMessage(new Message($msg, $attribute, 'NetworkValidator'));
if (!$result) {
// append validation message
$validator->appendMessage(new Message($msg, $attribute, 'NetworkValidator'));
}
}
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