Commit d7fb1c8d authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Fixes #4236 Improved disambiguation between ipv4 and ipv6

Showing with 26 additions and 38 deletions
+26 -38
...@@ -128,19 +128,24 @@ class tests { ...@@ -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 * \brief Test if the given string is an IPv4
* *
* \param string $ip The IPv4 to check * \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);
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);
}
} }
/*! /*!
...@@ -150,14 +155,7 @@ class tests { ...@@ -150,14 +155,7 @@ class tests {
*/ */
public static function is_ipv6($ip) public static function is_ipv6($ip)
{ {
if (function_exists('filter_var')) { return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
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);
}
} }
...@@ -315,17 +313,17 @@ class tests { ...@@ -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. * \return TRUE in case of a valid range, FALSE in case of an error.
*/ */
public static function is_ip_range($ip1, $ip2) 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; return FALSE;
} else { } else {
$ar1 = explode(".", $ip1); $ar1 = explode(".", $ip1);
......
...@@ -105,17 +105,20 @@ class PhoneNumberAttribute extends TestValidateAttribute ...@@ -105,17 +105,20 @@ class PhoneNumberAttribute extends TestValidateAttribute
protected $testFunc = 'is_phone_nr'; 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 /*! \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 () protected $testFunc = 'is_ipv4';
{
if (!tests::is_ip($this->value)) {
return msgPool::invalid($this->getLabel(), $this->value);
}
}
} }
/*! \brief This class allow to handle easily a String LDAP attribute that contains a IPv6 /*! \brief This class allow to handle easily a String LDAP attribute that contains a IPv6
...@@ -144,19 +147,6 @@ class MacAddressAttribute extends StringAttribute ...@@ -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 class CharSeparatedCompositeAttribute extends CompositeAttribute
{ {
private $sep; private $sep;
......
  • bmortier @bmortier

    mentioned in issue #1391

    By Côme Chilliet on 2017-09-02T15:25:10 (imported from GitLab)

    ·

    mentioned in issue #1391

    By Côme Chilliet on 2017-09-02T15:25:10 (imported from GitLab)

    Toggle commit list
Supports Markdown
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