mirror of
https://github.com/getgrav/grav.git
synced 2026-03-03 19:11:35 +01:00
Add getSubnet util to Grav\Common\Utils (#2465)
This commit is contained in:
committed by
Andy Miller
parent
7e39755154
commit
6e9af3bb29
@@ -1469,4 +1469,44 @@ abstract class Utils
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the subnet of an ip with CIDR prefix size
|
||||
*
|
||||
* @param string $ip
|
||||
* @param int $prefix
|
||||
*
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException if provided an invalid IP
|
||||
*/
|
||||
public static function getSubnet($ip, $prefix = 64)
|
||||
{
|
||||
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
throw new \InvalidArgumentException('Invalid IP: ' . $ip);
|
||||
}
|
||||
|
||||
// Packed representation of IP
|
||||
$ip = inet_pton($ip);
|
||||
|
||||
// Maximum netmask length = same as packed address
|
||||
$len = 8*strlen($ip);
|
||||
if ($prefix > $len) $prefix = $len;
|
||||
|
||||
$mask = str_repeat('f', $prefix>>2);
|
||||
|
||||
switch($prefix & 3)
|
||||
{
|
||||
case 3: $mask .= 'e'; break;
|
||||
case 2: $mask .= 'c'; break;
|
||||
case 1: $mask .= '8'; break;
|
||||
}
|
||||
$mask = str_pad($mask, $len>>2, '0');
|
||||
|
||||
// Packed representation of netmask
|
||||
$mask = pack('H*', $mask);
|
||||
// Bitwise - Take all bits that are both 1 to generate subnet
|
||||
$subnet = inet_ntop($ip & $mask);
|
||||
|
||||
return $subnet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user