0 follower

Final Class Yiisoft\NetworkUtilities\IpRanges

InheritanceYiisoft\NetworkUtilities\IpRanges

IpRanges represents a set of IP ranges that are either allowed or forbidden.

Public Methods

Hide inherited 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

Hide inherited 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
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

Hide inherited methods

__construct() public method

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:

  • recursively substitute aliases (described in $networks) with their values;
  • remove duplicates.

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:

new Ip(ranges: [
    '192.168.10.128'
    '!192.168.10.0/24',
    'any' // allows any other IP addresses
]);

In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the 192.168.10.0/24 subnet. IPv4 address 192.168.10.128 is also allowed, because it is listed before the restriction.

$networks array

Custom network aliases, that can be used in $ranges:

  • key - alias name;
  • value - array of strings. String can be an IP range, IP address or another alias. String can be negated with ! character. The default aliases are defined in \Yiisoft\NetworkUtilities\self::DEFAULT_NETWORKS and will be merged with custom ones.

                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);
}

            
getNetworks() public method

Get network aliases, that can be used in $ranges.

public getNetworks( ): array
return array

Network aliases.

                public function getNetworks(): array
{
    return $this->networks;
}

            
getRanges() public method

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;
}

            
isAllowed() public method

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;
}