Update IpRange.php

~ Fix up some naming issues

~ Add a netmask check

~ Add an example
This commit is contained in:
Nikita Petko 2021-11-25 15:00:19 +00:00 committed by Github Enterprise
parent 4ce172a9ee
commit cce95c2c42
1 changed files with 80 additions and 41 deletions

View File

@ -4,74 +4,113 @@
* Copyright 2015-2021 Nikita Petko (http://two-time-corp.mfdlabs.local/ui/?petko/)
*/
/*
Example:
<?php
use Alphaland\Web\IpHelper;
require_once './globals/Dependencies/Web/IpRange.php';
$baseAddress = '127.0.0.1';
$cidrNotation = '127.0.0.0/8'; // 127.0.0.0-127.255.255.255 or 127.0.0.0/255.255.255.0
$ipRange = '127.0.0.0-127.255.255.255'; // 127.0.0.0/255.255.255.0 or 127.0.0.0/8
$netmask = '127.0.0.0/255.255.255.0'; // 127.0.0.0-127.255.255.255 or 127.0.0.0/8
assert(IpHelper::IsIpInCidrRange($baseAddress, $cidrNotation));
assert(IpHelper::IsIpInRange($baseAddress, $ipRange));
assert(IpHelper::IsIpInNetmask($baseAddress, $netmask));
*/
namespace Alphaland\Web {
/**
* A class to help in the aid of IP address identification.
*/
class IpHelper
{
/**
* @param string $ip IP address
* @return bool true if IP is an IPv4 address
*/
public static function IsIpv4(string $ip)
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
/**
* @param string $ip IP address
* @return bool true if IP is an IPv6 address
*/
public static function IsIpv6(string $ip)
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
/**
* @param string $ip IP address
* @return bool true if IP is an IPv4 or IPv6 address
*/
public static function IsIp(string $ip)
{
return self::IsIpv4($ip) || self::IsIpv6($ip);
}
public static function IpIsInRange(string $ip, string $range)
/**
* @param string $ip IP address
* @param string $range IP range
* @return bool true if IP is in the range
*/
public static function IsIpInRange(string $ip, string $range)
{
if (strpos($range, '/') !== false) {
// $range is in IP/NETMASK format
list($range, $netmask) = explode('/', $range, 2);
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
if (strpos($range, '*') !== false) { // a.b.*.* format
// Just convert to A-B format by setting * to 0 for A and 255 for B
$lower = str_replace('*', '0', $range);
$upper = str_replace('*', '255', $range);
$range = "$lower-$upper";
}
if (strpos($range, '-') !== false) { // A-B format
list($lower, $upper) = explode('-', $range, 2);
$lower_dec = (float)sprintf("%u", ip2long($lower));
$upper_dec = (float)sprintf("%u", ip2long($upper));
$ip_dec = (float)sprintf("%u", ip2long($ip));
return (($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec));
}
return false;
}
/**
* @param string $ip IP address
* @param string $netmask Netmask
* @return bool true if IP is in the netmask
*/
public static function IsIpInNetmask(string $ip, string $netmask)
{
if (strpos($netmask, '/') !== false) {
list($range, $netmask) = explode('/', $netmask, 2);
if (strpos($netmask, '.') !== false) {
// $netmask is a 255.255.0.0 format
// $netmask is a
$netmask = str_replace('*', '0', $netmask);
$netmask_dec = ip2long($netmask);
return ((ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec));
} else {
// $netmask is a CIDR size block
// fix the range argument
$x = explode('.', $range);
while (count($x) < 4) $x[] = '0';
list($a, $b, $c, $d) = $x;
$range = sprintf("%u.%u.%u.%u", empty($a) ? '0' : $a, empty($b) ? '0' : $b, empty($c) ? '0' : $c, empty($d) ? '0' : $d);
$range_dec = ip2long($range);
$ip_dec = ip2long($ip);
# Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
#$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
# Strategy 2 - Use math to create it
$wildcard_dec = pow(2, (32 - $netmask)) - 1;
$netmask_dec = ~$wildcard_dec;
return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
}
} else {
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
if (strpos($range, '*') !== false) { // a.b.*.* format
// Just convert to A-B format by setting * to 0 for A and 255 for B
$lower = str_replace('*', '0', $range);
$upper = str_replace('*', '255', $range);
$range = "$lower-$upper";
}
if (strpos($range, '-') !== false) { // A-B format
list($lower, $upper) = explode('-', $range, 2);
$lower_dec = (float)sprintf("%u", ip2long($lower));
$upper_dec = (float)sprintf("%u", ip2long($upper));
$ip_dec = (float)sprintf("%u", ip2long($ip));
return (($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec));
}
return false;
}
return false;
}
/**
* @param string $ip IP address
* @param string $cidr CIDR
* @return bool true if IP is in the CIDR
*/
public static function IsIpInCidrRange(string $ip, string $cidr)
{
list($subnet, $bits) = explode('/', $cidr);