Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions classes/Kohana/Valid.php
Original file line number Diff line number Diff line change
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_PRIV_RANGE ∈ FILTER_FLAG_NO_RES_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)
Copy link
Copy Markdown

@rjd22 rjd22 Sep 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this if ($allow_private === FALSE) for backwards compatibility. What if someone does:

Valid::ip('ip', null);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I understand.

{
// 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