Checks if value is an IPv4 address

@param mixed $value The value

@return bool
193608
public static function isIpV4Address($value): bool
    {
        if (!static::isStringCastable($value)) {
            return false;
        }

        return filter_var((string) $value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
    }
379250
public function isValueValid($value) {
		return $value instanceof Type\PropertyValue || $value instanceof DataType\TextType || $value instanceof DataType\URLType;
	}
1109195
protected function _validateIPv4($value) {
        $ip2long = ip2long($value);
        if($ip2long === false) {
            return false;
        }

        return $value == long2ip($ip2long);
    }
425589
public static function isUrn($value): bool
    {
        if (!static::isStringCastable($value)) {
            return false;
        }

        // http://tools.ietf.org/html/rfc2141#section-2.1
        // To avoid confusion with the "urn:" identifier, the NID "urn" is
        // reserved and MUST NOT be used
        if (substr((string) $value, 0, 8) === "urn:urn:") {
            return false;
        }

        // http://tools.ietf.org/html/rfc2141
        //
        // <URN> ::= "urn:" <NID> ":" <NSS>
        //
        // <NID> ::= <let-num> [ 1,31<let-num-hyp> ]
        // <let-num-hyp> ::= <upper> | <lower> | <number> | "-"
        // <let-num>     ::= <upper> | <lower> | <number>
        // <upper>       ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" |
        //                   "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" |
        //                   "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |
        //                   "Y" | "Z"
        // <lower>       ::= "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |
        //                   "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |
        //                   "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |
        //                   "y" | "z"
        // <number>      ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
        //                   "8" | "9"
        //
        // <NSS>         ::= 1*<URN chars>
        // <URN chars>   ::= <trans> | "%" <hex> <hex>
        // <trans>       ::= <upper> | <lower> | <number> | <other> | <reserved>
        // <hex>         ::= <number> | "A" | "B" | "C" | "D" | "E" | "F" |
        //                   "a" | "b" | "c" | "d" | "e" | "f"
        // <other>       ::= "(" | ")" | "+" | "," | "-" | "." |
        //                   ":" | "=" | "@" | ";" | "$" |
        //                   "_" | "!" | "*" | "'"
        //
        // Therefore, these characters are RESERVED for future developments.
        // Namespace developers SHOULD NOT use these characters in unencoded
        // form, but rather use the appropriate %-encoding for each character.
        //
        // In addition, octet 0 (0 hex) should NEVER be used, in either
        // unencoded or %-encoded form.
        $pattern = sprintf(
            '/\Aurn:%s:%s\z/i',
            '[a-z0-9](?:[a-z0-9\-]{1,31})?',
            '(?:[a-z0-9()+,\-.:=@;$_!*\']|%(?:0[a-f1-9]|[a-f1-9][a-f0-9]))+'
        );

        return !!preg_match($pattern, (string) $value);
    }
249839
public static function ipv6(string $value):bool
    {
        $value = (string)$value;
        $valid = filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
        return $valid !== false;
    }
1148872
public function isValid($value)
    {
        if (!is_string($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $this->_setValue($value);
        // Check input against IP address schema
        if (preg_match('/^[0-9a-f:.]*$/i', $value) &&
            $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
            if (!($this->_options['allow'] & self::ALLOW_IP)) {
                $this->_error(self::IP_ADDRESS_NOT_ALLOWED);
                return false;
            } else {
                return true;
            }
        }

        // RFC3986 3.2.2 states:
        // 
        //     The rightmost domain label of a fully qualified domain name
        //     in DNS may be followed by a single "." and should be if it is 
        //     necessary to distinguish between the complete domain name and
        //     some local domain.
        //     
        // (see ZF-6363)
        
        // Local hostnames are allowed to be partitial (ending '.')
        if ($this->_options['allow'] & self::ALLOW_LOCAL) {
            if (substr($value, -1) === '.') {
                $value = substr($value, 0, -1);
                if (substr($value, -1) === '.') {
                    // Empty hostnames (ending '..') are not allowed
                    $this->_error(self::INVALID_LOCAL_NAME);
                    return false;
                }
            }
        }

        $domainParts = explode('.', $value);

        // Prevent partitial IP V4 adresses (ending '.')
        if ((count($domainParts) == 4) && preg_match('/^[0-9.a-e:.]*$/i', $value) &&
            $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
            $this->_error(self::INVALID_LOCAL_NAME);
        }

        // Check input against DNS hostname schema
        if ((count($domainParts) > 1) && (strlen($value) >= 4) && (strlen($value) <= 254)) {
            $status = false;

            $origenc = iconv_get_encoding('internal_encoding');
            iconv_set_encoding('internal_encoding', 'UTF-8');
            do {
                // First check TLD
                $matches = array();
                if (preg_match('/([^.]{2,10})$/i', end($domainParts), $matches) ||
                    (end($domainParts) == 'ایران') || (end($domainParts) == '中国') ||
                    (end($domainParts) == '公司') || (end($domainParts) == '网络')) {

                    reset($domainParts);

                    // Hostname characters are: *(label dot)(label dot label); max 254 chars
                    // label: id-prefix [*ldh{61} id-prefix]; max 63 chars
                    // id-prefix: alpha / digit
                    // ldh: alpha / digit / dash

                    // Match TLD against known list
                    $this->_tld = strtolower($matches[1]);
                    if ($this->_options['tld']) {
                        if (!in_array($this->_tld, $this->_validTlds)) {
                            $this->_error(self::UNKNOWN_TLD);
                            $status = false;
                            break;
                        }
                    }

                    /**
                     * Match against IDN hostnames
                     * Note: Keep label regex short to avoid issues with long patterns when matching IDN hostnames
                     * @see Zend_Validate_Hostname_Interface
                     */
                    $regexChars = array(0 => '/^[a-z0-9\x2d]{1,63}$/i');
                    if ($this->_options['idn'] &&  isset($this->_validIdns[strtoupper($this->_tld)])) {
                        if (is_string($this->_validIdns[strtoupper($this->_tld)])) {
                            $regexChars += include(__DIR__.'/'.$this->_validIdns[strtoupper($this->_tld)]);
                        } else {
                            $regexChars += $this->_validIdns[strtoupper($this->_tld)];
                        }
                    }

                    // Check each hostname part
                    $check = 0;
                    foreach ($domainParts as $domainPart) {
                        // Decode Punycode domainnames to IDN
                        if (strpos($domainPart, 'xn--') === 0) {
                            $domainPart = $this->decodePunycode(substr($domainPart, 4));
                            if ($domainPart === false) {
                                return false;
                            }
                        }

                        // Check dash (-) does not start, end or appear in 3rd and 4th positions
                        if ((strpos($domainPart, '-') === 0)
                            || ((strlen($domainPart) > 2) && (strpos($domainPart, '-', 2) == 2) && (strpos($domainPart, '-', 3) == 3))
                            || (strpos($domainPart, '-') === (strlen($domainPart) - 1))) {
                                $this->_error(self::INVALID_DASH);
                            $status = false;
                            break 2;
                        }

                        // Check each domain part
                        $checked = false;
                        foreach($regexChars as $regexKey => $regexChar) {
                            $status = @preg_match($regexChar, $domainPart);
                            if ($status > 0) {
                                $length = 63;
                                if (array_key_exists(strtoupper($this->_tld), $this->_idnLength)
                                    && (array_key_exists($regexKey, $this->_idnLength[strtoupper($this->_tld)]))) {
                                    $length = $this->_idnLength[strtoupper($this->_tld)];
                                }

                                if (iconv_strlen($domainPart, 'UTF-8') > $length) {
                                    $this->_error(self::INVALID_HOSTNAME);
                                } else {
                                    $checked = true;
                                    break;
                                }
                            }
                        }

                        if ($checked) {
                            ++$check;
                        }
                    }

                    // If one of the labels doesn't match, the hostname is invalid
                    if ($check !== count($domainParts)) {
                        $this->_error(self::INVALID_HOSTNAME_SCHEMA);
                        $status = false;
                    }
                } else {
                    // Hostname not long enough
                    $this->_error(self::UNDECIPHERABLE_TLD);
                    $status = false;
                }
            } while (false);

            iconv_set_encoding('internal_encoding', $origenc);
            // If the input passes as an Internet domain name, and domain names are allowed, then the hostname
            // passes validation
            if ($status && ($this->_options['allow'] & self::ALLOW_DNS)) {
                return true;
            }
        } else if ($this->_options['allow'] & self::ALLOW_DNS) {
            $this->_error(self::INVALID_HOSTNAME);
        }

        // Check for URI Syntax (RFC3986)
        if ($this->_options['allow'] & self::ALLOW_URI) {
            if (preg_match("/^([a-zA-Z0-9-._~!$&\'()*+,;=]|%[[:xdigit:]]{2}){1,254}$/i", $value)) {
                return true;
            } else {
                $this->_error(self::INVALID_URI);
            }
        }

        // Check input against local network name schema; last chance to pass validation
        $regexLocal = '/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}[\x2e]{0,1}){1,254}$/';
        $status = @preg_match($regexLocal, $value);

        // If the input passes as a local network name, and local network names are allowed, then the
        // hostname passes validation
        $allowLocal = $this->_options['allow'] & self::ALLOW_LOCAL;
        if ($status && $allowLocal) {
            return true;
        }

        // If the input does not pass as a local network name, add a message
        if (!$status) {
            $this->_error(self::INVALID_LOCAL_NAME);
        }

        // If local network names are not allowed, add a message
        if ($status && !$allowLocal) {
            $this->_error(self::LOCAL_NAME_NOT_ALLOWED);
        }

        return false;
    }
1181509
public static function isHostnameIP($hostname)
  {
    // IP-literal: encased in square brackets
    // we match starting [ character followed by non ] characters followed by ] character as last character in hostname
    if(preg_match('/^\[[^\]]\]$/',$hostname)){
      // ok its an IP-literal, we do not parse the literal to see if its valid
      return true;
    }
    // IPv4 in decimal
    if(preg_match('/^[0-9]+$/',$hostname)){
      // ok its a number
      // lets see if its in the 32bit range
      $ip4_hex=base_convert($hostname, 10, 16);
      if(strlen($ip4_hex)<=8){
        // ok, hex representation is less than 9 chars, so thats max FFFFFFFF
        return true;
      }
    }
    $ip_parts=Array();
    if(!preg_match('/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/',$hostname,$ip_parts)){
      return false;
    }
    // check if each block is in byte range (hard to test with regex)
    for($j=1;$j<=4;$j++){
      if(($ip_parts[$j]>255) || ($ip_parts[$j]<0)){
        return false;
      }
    }
    return true;
  }
575846
public static function isIpAddress($value): bool
    {
        if (!static::isStringCastable($value)) {
            return false;
        }

        return filter_var((string) $value, FILTER_VALIDATE_IP) !== false;
    }
901271
protected function validateIp($ip)
    {
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_IPV6) === false) {
            return false;
        }

        return true;
    }
682557
private function validateIPNumber(): bool
    {
        $value = $this->value;

        if (\preg_match('/^([01]{8}\.){3}[01]{8}$/', $this->value)) {
            // binary format 00000000.00000000.00000000.00000000
            $value = \sprintf(
                '%s.%s.%s.%s',
                \bindec(\substr($this->value, 0, 8)),
                \bindec(\substr($this->value, 9, 8)),
                \bindec(\substr($this->value, 18, 8)),
                \bindec(\substr($this->value, 27, 8))
            );
        } elseif (\preg_match('/^([0-9a-f]{2}\.){3}[0-9a-f]{2}$/i', $this->value)) {
            // hex format ff.ff.ff.ff
            $value = \sprintf(
                '%s.%s.%s.%s',
                \hexdec(\substr($this->value, 0, 2)),
                \hexdec(\substr($this->value, 3, 2)),
                \hexdec(\substr($this->value, 6, 2)),
                \hexdec(\substr($this->value, 9, 2))
            );
        }
        $flags = 0;
        if ($this->options['allow-ipv4'] === true) {
            $flags |= FILTER_FLAG_IPV4;
        }
        if ($this->options['allow-ipv6'] === true) {
            $flags |= FILTER_FLAG_IPV6;
        }
        $isValid = \filter_var($value, FILTER_VALIDATE_IP, ['flags' => $flags]) !== false ? true : false;
        if (!$isValid) {
            $this->setError(self::VALIDATOR_ERROR_IP_INVALID_NUMBER);
        }
        return $isValid;
    }
296277
private function validatePrivateRange(string $value = ''): bool
    {
        $isValid = true;
        if ($this->options['allow-private-range'] === false) {
            $flags = FILTER_FLAG_NO_PRIV_RANGE;
            $isValid = \filter_var($value, FILTER_VALIDATE_IP, ['flags' => $flags]) !== false ? true : false;
        }
        if (!$isValid) {
            $this->setError(self::VALIDATOR_ERROR_IP_PRIVATE_RANGE_NOT_ALLOWED);
        }

        return $isValid;
    }
48533
public static function isIpInCidr(string $ip, string $cidr): bool
    {
        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            throw new \UnexpectedValueException(
                "invalid value for ip; expecting an IPv4 address"
            );
        }

        list($range, $netmask) = static::parseCidr($cidr);
        $netmask = ~((1 << (32 - $netmask)) - 1);
        return ((ip2long($ip) & $netmask) === ip2long($range));
    }
390819
public static function isUrl($value)
    {
        if (\strlen($value) > 0 && $value[0] == $value[1] && $value[0] == "/") {
            $value = 'http:'.$value;
        }

        return false !== \filter_var($value, FILTER_VALIDATE_URL, ['options' => ['flags' => FILTER_FLAG_PATH_REQUIRED]]);
    }
805068
public function check($value, IValidationData $data = null) {
        return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
    }
668574
public function isValid($value) {
		$intIp = ip2long($value);
		
		if ($intIp !== false && long2ip($intIp) === $value) {
			return true;
		}
		
		$this->addMessage('Invalid IP address');
		
		return false;
	}
537045
public static function macAddress(string $value): bool
    {
        $result = filter_var($value, FILTER_VALIDATE_MAC);
        return is_string($result) && $value === $result;
    }
38559
public function isValueValid($value) {
		return $value instanceof Type\Place || $value instanceof Type\PostalAddress || $value instanceof DataType\URLType;
	}