PHP Класс yii\validators\IpValidator

It also may change attribute's value if normalization of IPv6 expansion is enabled. The following are examples of validation rules using this validator: php ['ip_address', 'ip'], // IPv4 or IPv6 address ['ip_address', 'ip', 'ipv6' => false], // IPv4 address (IPv6 is disabled) ['ip_address', 'ip', 'subnet' => true], // requires a CIDR prefix (like 10.0.0.1/24) for the IP address ['ip_address', 'ip', 'subnet' => null], // CIDR prefix is optional ['ip_address', 'ip', 'subnet' => null, 'normalize' => true], // CIDR prefix is optional and will be added when missing ['ip_address', 'ip', 'ranges' => ['192.168.0.0/24']], // only IP addresses from the specified subnet are allowed ['ip_address', 'ip', 'ranges' => ['!192.168.0.0/24', 'any']], // any IP is allowed except IP in the specified subnet ['ip_address', 'ip', 'expandIPv6' => true], // expands IPv6 address to a full notation format
С версии: 2.0.7
Автор: Dmitry Naumenko ([email protected])
Наследование: extends Validator
Показать файл Открыть проект

Открытые свойства

Свойство Тип Описание
$expandIPv6 whether to expand an IPv6 address to the full notation format. Defaults to false.
$hasSubnet user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4 whether the validating value can be an IPv4 address. Defaults to true.
$ipv4NotAllowed user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4Pattern Regexp-pattern to validate IPv4 address
$ipv6 whether the validating value can be an IPv6 address. Defaults to true.
$ipv6NotAllowed user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv6Pattern Regexp-pattern to validate IPv6 address
$message user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$negation whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
$networks The 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 [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
$noSubnet user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$normalize whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
$notInRange user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$subnet whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
$wrongCidr user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated

Открытые методы

Метод Описание
clientValidateAttribute ( $model, $attribute, $view )
getRanges ( ) : array
init ( )
setRanges ( array $ranges ) Set the IPv4 or IPv6 ranges that are allowed or forbidden.
validateAttribute ( $model, $attribute )

Защищенные методы

Метод Описание
validateIPv4 ( string $value ) : boolean Validates IPv4 address
validateIPv6 ( string $value ) : boolean Validates IPv6 address
validateValue ( $value )

Приватные методы

Метод Описание
expandIPv6 ( string $ip ) : string Expands an IPv6 address to it's full notation. For example 2001:db8::1 will be expanded to 2001:0db8:0000:0000:0000:0000:0000:0001
getIpParsePattern ( ) : string Used to get the Regexp pattern for initial IP address parsing
getIpVersion ( string $ip ) : integer Gets the IP version
inRange ( string $ip, integer $cidr, string $range ) : boolean Checks whether the IP is in subnet range
ip2bin ( string $ip ) : string Converts IP address to bits representation
isAllowed ( string $ip, integer $cidr ) : boolean The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list.
parseNegatedRange ( $string ) : array Parses IP address/range for the negation with [[NEGATION_CHAR]].
prepareRanges ( $ranges ) : array Prepares array to fill in [[ranges]]: - Recursively substitutes aliases, described in [[networks]] with their values - Removes duplicates
validateSubnet ( $ip ) : string | array Validates an IPv4/IPv6 address or subnet

Описание методов

clientValidateAttribute() публичный Метод

public clientValidateAttribute ( $model, $attribute, $view )

getRanges() публичный Метод

public getRanges ( ) : array
Результат array The IPv4 or IPv6 ranges that are allowed or forbidden.

init() публичный Метод

public init ( )

setRanges() публичный Метод

The following preparation tasks are performed: - Recursively substitutes aliases (described in [[networks]]) with their values. - Removes duplicates
public setRanges ( array $ranges )
$ranges array the IPv4 or IPv6 ranges that are allowed or forbidden. When the array is empty, or the option 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 has not matched any of the rules. Example: ```php [ '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.

validateAttribute() публичный Метод

public validateAttribute ( $model, $attribute )

validateIPv4() защищенный Метод

Validates IPv4 address
protected validateIPv4 ( string $value ) : boolean
$value string
Результат boolean

validateIPv6() защищенный Метод

Validates IPv6 address
protected validateIPv6 ( string $value ) : boolean
$value string
Результат boolean

validateValue() защищенный Метод

protected validateValue ( $value )

Описание свойств

$expandIPv6 публичное свойство

whether to expand an IPv6 address to the full notation format. Defaults to false.
public $expandIPv6

$hasSubnet публичное свойство

user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: subnet
public $hasSubnet

$ipv4 публичное свойство

whether the validating value can be an IPv4 address. Defaults to true.
public $ipv4

$ipv4NotAllowed публичное свойство

user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: ipv4
public $ipv4NotAllowed

$ipv4Pattern публичное свойство

Regexp-pattern to validate IPv4 address
public $ipv4Pattern

$ipv6 публичное свойство

whether the validating value can be an IPv6 address. Defaults to true.
public $ipv6

$ipv6NotAllowed публичное свойство

user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: ipv6
public $ipv6NotAllowed

$ipv6Pattern публичное свойство

Regexp-pattern to validate IPv6 address
public $ipv6Pattern

$message публичное свойство

user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
public $message

$negation публичное свойство

whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
public $negation

$networks публичное свойство

The 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 [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
public $networks

$noSubnet публичное свойство

user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: subnet
public $noSubnet

$normalize публичное свойство

whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
См. также: subnet
public $normalize

$notInRange публичное свойство

user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: ranges
public $notInRange

$subnet публичное свойство

whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
public $subnet

$wrongCidr публичное свойство

user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
См. также: subnet
public $wrongCidr