setAllow($allow); // Set validation options $this->_validateIdn = $validateIdn; $this->_validateTld = $validateTld; } /** * Returns the allow option * * @return integer */ public function getAllow() { return $this->_allow; } /** * Sets the allow option * * @param integer $allow * @return Zend_Validate_Hostname Provides a fluent interface */ public function setAllow($allow) { $this->_allow = $allow; return $this; } /** * Set whether IDN domains are validated * * This only applies when DNS hostnames are validated * * @param boolean $allowed Set allowed to true to validate IDNs, and false to not validate them */ public function setValidateIdn ($allowed) { $this->_validateIdn = (bool) $allowed; } /** * Set whether the TLD element of a hostname is validated * * This only applies when DNS hostnames are validated * * @param boolean $allowed Set allowed to true to validate TLDs, and false to not validate them */ public function setValidateTld ($allowed) { $this->_validateTld = (bool) $allowed; } /** * Sets the check option * * @param integer $check * @return Zend_Validate_Hostname Provides a fluent interface */ public function setCheck($check) { $this->_check = $check; return $this; } /** * Defined by Zend_Validate_Interface * * Returns true if and only if the $value is a valid hostname with respect to the current allow option * * @param string $value * @throws Zend_Validate_Exception if a fatal error occurs for validation process * @return boolean */ public function isValid($value) { $this->_messages = array(); $valueString = (string) $value; /** * Check input against IP address schema * @see Zend_Validate_Ip */ require_once 'Zend/Validate/Ip.php'; $ip = new Zend_Validate_Ip(); if ($ip->isValid($valueString)) { if (!($this->_allow & self::ALLOW_IP)) { $this->_messages[] = "'$valueString' appears to be an IP address, but IP addresses are not allowed"; return false; } else{ return true; } } // Check input against DNS hostname schema $domainParts = explode('.', $valueString); if ((count($domainParts) > 1) && (strlen($valueString) >= 4) && (strlen($valueString) <= 254)) { $status = false; do { // First check TLD if (preg_match('/([a-z]{2,10})$/i', end($domainParts), $matches)) { 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 $valueTld = strtolower($matches[1]); if ($this->_validateTld) { if (!in_array($valueTld, $this->_validTlds)) { $this->_messages[] = "'$valueString' appears to be a DNS hostname but cannot match TLD" . " against known list"; $status = false; break; } } /** * Match against IDN hostnames * @see Zend_Validate_Hostname_Interface */ $labelChars = 'a-z0-9'; $utf8 = false; $classFile = 'Zend/Validate/Hostname/' . ucfirst($valueTld) . '.php'; if ($this->_validateIdn) { if (Zend_Loader::isReadable($classFile)) { // Load additional characters $className = 'Zend_Validate_Hostname_' . ucfirst($valueTld); Zend_Loader::loadClass($className); $labelChars .= call_user_func(array($className, 'getCharacters')); $utf8 = true; } } // Keep label regex short to avoid issues with long patterns when matching IDN hostnames $regexLabel = '/^[' . $labelChars . '\x2d]{1,63}$/i'; if ($utf8) { $regexLabel .= 'u'; } // Check each hostname part $valid = true; foreach ($domainParts as $domainPart) { // 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) || strrpos($domainPart, '-') === strlen($domainPart) - 1) { $this->_messages[] = "'$valueString' appears to be a DNS hostname but contains a dash (-)" . " in an invalid position"; $status = false; break 2; } // Check each domain part $status = @preg_match($regexLabel, $domainPart); if ($status === false) { /** * Regex error * @see Zend_Validate_Exception */ require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception('Internal error: DNS validation failed'); } elseif ($status === 0) { $valid = false; } } // If all labels didn't match, the hostname is invalid if (!$valid) { $this->_messages[] = "'$valueString' appears to be a DNS hostname but cannot match against" . " hostname schema for TLD '$valueTld'"; $status = false; } } else { // Hostname not long enough $this->_messages[] = "'$valueString' appears to be a DNS hostname but cannot extract TLD part"; $status = false; } } while (false); // If the input passes as an Internet domain name, and domain names are allowed, then the hostname // passes validation if ($status && ($this->_allow & self::ALLOW_DNS)) { return true; } } else { $this->_messages[] = "'$valueString' does not match the expected structure for a DNS hostname"; } // 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}){1,254}$/"; $status = @preg_match($regexLocal, $valueString); if (false === $status) { /** * Regex error * @see Zend_Validate_Exception */ require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception('Internal error: local network name validation failed'); } // If the input passes as a local network name, and local network names are allowed, then the // hostname passes validation $allowLocal = $this->_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->_messages[] = "'$valueString' does not appear to be a valid local network name"; } // If local network names are not allowed, add a message if (!$allowLocal) { $this->_messages[] = "'$valueString' appears to be a local network name but but local network names are" . " not allowed"; } return false; } /** * Defined by Zend_Validate_Interface * * Returns array of validation failure messages * * @return array */ public function getMessages() { return $this->_messages; } /** * Throws an exception if a regex for $type does not exist * * @param string $type * @throws Zend_Validate_Exception * @return Zend_Validate_Hostname Provides a fluent interface */ protected function _checkRegexType($type) { if (!isset($this->_regex[$type])) { /** * @see Zend_Validate_Exception */ require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception("'$type' must be one of ('" . implode(', ', array_keys($this->_regex)) . "')"); } return $this; } }