Final Class Yiisoft\NetworkUtilities\IpRanges
| Inheritance | Yiisoft\NetworkUtilities\IpRanges |
|---|
IpRanges represents a set of IP ranges that are either allowed or forbidden.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\NetworkUtilities\IpRanges | |
| getNetworks() | Get network aliases, that can be used in $ranges. | Yiisoft\NetworkUtilities\IpRanges |
| getRanges() | Get the IPv4 or IPv6 ranges that are either allowed or forbidden. | Yiisoft\NetworkUtilities\IpRanges |
| isAllowed() | Whether the IP address with specified CIDR is allowed according to the $ranges list. | Yiisoft\NetworkUtilities\IpRanges |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| ANY | 'any' | Yiisoft\NetworkUtilities\IpRanges | |
| DEFAULT_NETWORKS | [ '*' => [ self::ANY, ], self::ANY => [ '0.0.0.0/0', '::/0', ], self::PRIVATE => [ '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8', ], self::MULTICAST => [ '224.0.0.0/4', 'ff00::/8', ], self::LINK_LOCAL => [ '169.254.0.0/16', 'fe80::/10', ], self::LOCALHOST => [ '127.0.0.0/8', '::1', ], self::DOCUMENTATION => [ '192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', '2001:db8::/32', ], self::SYSTEM => [ self::MULTICAST, self::LINK_LOCAL, self::LOCALHOST, self::DOCUMENTATION, ], ] | Default network aliases. | Yiisoft\NetworkUtilities\IpRanges |
| DOCUMENTATION | 'documentation' | Yiisoft\NetworkUtilities\IpRanges | |
| LINK_LOCAL | 'linklocal' | Yiisoft\NetworkUtilities\IpRanges | |
| LOCALHOST | 'localhost' | Yiisoft\NetworkUtilities\IpRanges | |
| MULTICAST | 'multicast' | Yiisoft\NetworkUtilities\IpRanges | |
| NEGATION_CHARACTER | '!' | Negation character used to negate ranges | Yiisoft\NetworkUtilities\IpRanges |
| PRIVATE | 'private' | Yiisoft\NetworkUtilities\IpRanges | |
| SYSTEM | 'system' | Yiisoft\NetworkUtilities\IpRanges |
Method Details
| public __construct( string[] $ranges = [], array $networks = [] ): mixed | ||
| $ranges | string[] |
The IPv4 or IPv6 ranges that are either allowed or forbidden. The following preparation tasks are performed:
When the array is empty or the option is not set, all IP addresses are allowed. Otherwise, the rules are checked sequentially until the first match is found. An IP address is forbidden when it hasn't matched any of the rules. Example:
In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the |
| $networks | array |
Custom network aliases, that can be used in $ranges:
|
public function __construct(array $ranges = [], array $networks = [])
{
foreach ($networks as $key => $_values) {
if (array_key_exists($key, self::DEFAULT_NETWORKS)) {
throw new InvalidArgumentException("Network alias \"{$key}\" already set as default.");
}
}
$this->networks = array_merge(self::DEFAULT_NETWORKS, $networks);
$this->ranges = $this->prepareRanges($ranges);
}
Get network aliases, that can be used in $ranges.
| public getNetworks( ): array | ||
| return | array |
Network aliases. |
|---|---|---|
public function getNetworks(): array
{
return $this->networks;
}
Get the IPv4 or IPv6 ranges that are either allowed or forbidden.
| public getRanges( ): string[] | ||
| return | string[] |
The IPv4 or IPv6 ranges that are either allowed or forbidden. |
|---|---|---|
public function getRanges(): array
{
return $this->ranges;
}
Whether the IP address with specified CIDR is allowed according to the $ranges list.
| public isAllowed( string $ip ): boolean | ||
| $ip | string | |
public function isAllowed(string $ip): bool
{
if (empty($this->ranges)) {
return true;
}
foreach ($this->ranges as $string) {
[$isNegated, $range] = $this->parseNegatedRange($string);
if (IpHelper::inRange($ip, $range)) {
return !$isNegated;
}
}
return false;
}
Signup or Login in order to comment.