diff --git a/include/class_tests.inc b/include/class_tests.inc index 3658e6fda3c15ed99f2744f7b8ba712a6fa9ccd3..59f054db229e586cd98ac64290a589fda497641a 100644 --- a/include/class_tests.inc +++ b/include/class_tests.inc @@ -128,19 +128,24 @@ class tests { } } + /*! + * \brief Test if the given string is an IP (v4 or v6) + * + * \param string $ip The IP to check + */ + public static function is_ip($ip) + { + return filter_var($ip, FILTER_VALIDATE_IP); + } /*! * \brief Test if the given string is an IPv4 * * \param string $ip The IPv4 to check */ - public static function is_ip($ip) + public static function is_ipv4($ip) { - if (function_exists('filter_var')) { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); - } else { - return preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $ip); - } + return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); } /*! @@ -150,14 +155,7 @@ class tests { */ public static function is_ipv6($ip) { - if (function_exists('filter_var')) { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); - } else { - $ipv4 = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'; - $g = '([0-9a-f]{1,4})'; //IPv6 group - return preg_match("/^$g:$g:$g:$g:$g:$g:$g:$g$/", $ip) || - preg_match("/^$g:$g:$g:$g:$g:$g:$ipv4$/", $ip); - } + return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); } @@ -315,17 +313,17 @@ class tests { /* - * \brief Check if $ip1 and $ip2 represents a valid IP range + * \brief Check if $ip1 and $ip2 represents a valid IPv4 range * - * \param string $ip1 The first IP + * \param string $ip1 The first IPv4 * - * \param string $ip2 The second IP + * \param string $ip2 The second IPv4 * * \return TRUE in case of a valid range, FALSE in case of an error. */ public static function is_ip_range($ip1, $ip2) { - if (!tests::is_ip($ip1) || !tests::is_ip($ip2)) { + if (!tests::is_ipv4($ip1) || !tests::is_ipv4($ip2)) { return FALSE; } else { $ar1 = explode(".", $ip1); diff --git a/include/simpleplugin/class_helpersAttribute.inc b/include/simpleplugin/class_helpersAttribute.inc index 7a2ad46cf27f1316ba4f2c0db041a099832cc6c1..a430bc7fbded19f0d9c6752aaa5d9f7955ecddbf 100644 --- a/include/simpleplugin/class_helpersAttribute.inc +++ b/include/simpleplugin/class_helpersAttribute.inc @@ -105,17 +105,20 @@ class PhoneNumberAttribute extends TestValidateAttribute protected $testFunc = 'is_phone_nr'; } +/*! \brief This class allow to handle easily a String LDAP attribute that contains an IP (v4 or v6) + * + */ +class IPAttribute extends TestValidateAttribute +{ + protected $testFunc = 'is_ip'; +} + /*! \brief This class allow to handle easily a String LDAP attribute that contains an IPv4 * */ -class IPv4Attribute extends StringAttribute +class IPv4Attribute extends TestValidateAttribute { - function validate () - { - if (!tests::is_ip($this->value)) { - return msgPool::invalid($this->getLabel(), $this->value); - } - } + protected $testFunc = 'is_ipv4'; } /*! \brief This class allow to handle easily a String LDAP attribute that contains a IPv6 @@ -144,19 +147,6 @@ class MacAddressAttribute extends StringAttribute } } -/*! \brief This class allow to handle easily a String LDAP attribute that contains an IP (v4 or v6) - * - */ -class IPAttribute extends StringAttribute -{ - function validate () - { - if (!tests::is_ip($this->value) && !tests::is_ipv6($this->value)) { - return msgPool::invalid($this->getLabel(), $this->value); - } - } -} - class CharSeparatedCompositeAttribute extends CompositeAttribute { private $sep;