Skip to content

Commit

Permalink
Patch Valid::ip to work with PHP v7.0.10+ AND v5.6.25+
Browse files Browse the repository at this point in the history
  • Loading branch information
enov committed Aug 29, 2016
1 parent bdbe81a commit 88adc21
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions classes/Kohana/Valid.php
Expand Up @@ -224,16 +224,27 @@ public static function url($url)
*/
public static function ip($ip, $allow_private = TRUE)
{
// Do not allow reserved addresses
$flags = FILTER_FLAG_NO_RES_RANGE;

if ($allow_private === FALSE)
// In PHP v7.0.10+ AND v5.6.25+ FILTER_FLAG_NO_RES_RANGE ∈ FILTER_FLAG_NO_PRIV_RANGE
// However we have to combine both flags to support older versions
$flags = FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE;

$is_valid_public_ip = (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);

if ( ! $allow_private)
{
// Do not allow private or reserved addresses
$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
return $is_valid_public_ip;
}

return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
// at this point we are allowing private IPs as well
return
(
$is_valid_public_ip OR
(
(bool) filter_var($ip, FILTER_VALIDATE_IP) AND
! (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)
)
);
}

/**
Expand Down

0 comments on commit 88adc21

Please sign in to comment.