Skip to content

Commit

Permalink
[IP] fixes
Browse files Browse the repository at this point in the history
  inet_ptod IPv4 "short-cut" will properly strip /cidr notation, as needed.
  inet_ptob $bits will now be auto-detected (32 or 128) if not specified.
  • Loading branch information
lifo101 committed Jan 13, 2014
1 parent b918ec7 commit 1d24ce9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Lifo/IP/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static function inet_ptod($ip)
{
// shortcut for IPv4 addresses
if (strpos($ip, ':') === false && strpos($ip, '.') !== false) {
// remove any cidr block notation
if (($o = strpos($ip, '/')) !== false) {
$ip = substr($ip, 0, $o);
}
return sprintf('%u', ip2long($ip));
}

Expand Down Expand Up @@ -117,8 +121,11 @@ public static function inet_ptoh($ip)
/**
* Convert a human readable (presentational) IP address into a BINARY string.
*/
public static function inet_ptob($ip, $bits = 128)
public static function inet_ptob($ip, $bits = null)
{
if ($bits === null) {
$bits = self::isIPv4($ip) ? 32 : 128;
}
return BC::bcdecbin(self::inet_ptod($ip), $bits);
}

Expand Down

0 comments on commit 1d24ce9

Please sign in to comment.