diff options
author | Steve Beaver <sbeaver@netgate.com> | 2016-12-15 12:19:01 -0500 |
---|---|---|
committer | Steve Beaver <sbeaver@netgate.com> | 2016-12-15 12:23:48 -0500 |
commit | 77a8a7d6af748f633f35d9c9e65a2aa6df7d72ef (patch) | |
tree | 338f88b7e40a6afe2148ecbc8c9422a2fbb7fe6e /src/etc/inc/util.inc | |
parent | 0917101a0c0c4d611eeb41c2d3ee49062d52a2e9 (diff) | |
download | pfsense-77a8a7d6af748f633f35d9c9e65a2aa6df7d72ef.zip pfsense-77a8a7d6af748f633f35d9c9e65a2aa6df7d72ef.tar.gz |
Added a function validateipaddr() use as:
// Validate a network address
// $addr: the address to validate
// $type: IPV4|IPV6|IPV4V6
// $label: the label used by the GUI to display this value. Required to compose an error message
// $err_msg: pointer to the callers error message array so that error messages can be added to it here
// $alias: are aliases permitted for this address?
function validateipaddr(&$addr, $type, $label, &$err_msg, $alias=false)
This is indented to provide a single method of validating IP addresses of all types and composing a suitable error message
Diffstat (limited to 'src/etc/inc/util.inc')
-rw-r--r-- | src/etc/inc/util.inc | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/etc/inc/util.inc b/src/etc/inc/util.inc index 5081322..1b8ba36 100644 --- a/src/etc/inc/util.inc +++ b/src/etc/inc/util.inc @@ -2475,4 +2475,65 @@ function get_smart_drive_list() { return $disk_list; } +// Validate a network address +// $addr: the address to validate +// $type: IPV4|IPV6|IPV4V6 +// $label: the used by the GUI to display this value. Required to compose an error message +// $err_msg: pointer to the callers error message array so that error messages can be added to it here +// $alias: are aliases permitted for this address? +function validateipaddr(&$addr, $type, $label, &$err_msg, $alias=false) { + switch ($type) { + case IPV4: + if (is_ipaddrv4($addr)) { + return true; + } else if ($alias) { + if (is_alias($addr)) { + return true; + } else { + $err_msg[] = $label . gettext(" must be a valid IPv4 address or alias."); + return false; + } + } else { + $err_msg[] = $label . gettext(" must be a valid IPv4 address."); + return false; + } + break; + case IPV6: + if (is_ipaddrv6($addr)) { + $addr = strtolower($addr); + return true; + } else if($alias) { + if (is_alias($addr)) { + return true; + } else { + $err_msg[] = $label . gettext(" must be a valid IPv6 address or alias."); + return false; + } + } else { + $err_msg[] = $label . gettext(" must be a valid IPv6 address."); + return false; + } + break; + case IPV4V6: + if (is_ipaddrv6($addr)) { + $addr = strtolower($addr); + return true; + } else if (is_ipaddrv4($addr)) { + return true; + } else if($alias) { + if (is_alias($addr)) { + return true; + } else { + $err_msg[] = $label . gettext(" must be a valid IPv4 or IPv6 address or alias."); + return false; + } + } else { + $err_msg[] = $label . gettext(" must be a valid IPv4 or IPv6 address."); + return false; + } + break; + } + + return false; +} ?> |