From d7fb1c8dbc121b2e9bb906b49039d5ed9b409503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.bernigaud@opensides.be> Date: Tue, 20 Oct 2015 14:14:29 +0200 Subject: [PATCH] Fixes #4236 Improved disambiguation between ipv4 and ipv6 --- include/class_tests.inc | 34 +++++++++---------- .../simpleplugin/class_helpersAttribute.inc | 30 ++++++---------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/include/class_tests.inc b/include/class_tests.inc index 3658e6fda..59f054db2 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 7a2ad46cf..a430bc7fb 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; -- GitLab