mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-18 18:44:44 +00:00
MVC: NetworkField type, add "NetMaskAllowed" option to validate on single addresses
This commit is contained in:
parent
a2aec6c76e
commit
2afb3777b8
@ -51,6 +51,11 @@ class NetworkField extends BaseField
|
||||
*/
|
||||
protected $internalNetMaskRequired = false;
|
||||
|
||||
/**
|
||||
* @var bool marks if net mask is (dis)allowed
|
||||
*/
|
||||
protected $internalNetMaskAllowed = true;
|
||||
|
||||
/**
|
||||
* @var null when multiple values could be provided at once, specify the split character
|
||||
*/
|
||||
@ -93,6 +98,15 @@ class NetworkField extends BaseField
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for net mask required
|
||||
* @param integer $value
|
||||
*/
|
||||
public function setNetMaskAllowed($value)
|
||||
{
|
||||
$this->internalNetMaskAllowed = (trim(strtoupper($value)) == "Y");
|
||||
}
|
||||
|
||||
/**
|
||||
* setter for address family
|
||||
* @param $value address family [ipv4, ipv6, empty for all]
|
||||
@ -170,6 +184,7 @@ class NetworkField extends BaseField
|
||||
'message' => $this->internalValidationMessage,
|
||||
'split' => $this->internalFieldSeparator,
|
||||
'netMaskRequired' => $this->internalNetMaskRequired,
|
||||
'netMaskAllowed' => $this->internalNetMaskAllowed,
|
||||
'version' => $this->internalAddressFamily
|
||||
));
|
||||
}
|
||||
|
||||
@ -86,22 +86,26 @@ class NetworkValidator extends Validator implements ValidatorInterface
|
||||
|
||||
// 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
|
||||
if ($this->getOption('netMaskAllowed') === 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;
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user