/** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is a valid email address * according to RFC2822 * * @link http://www.ietf.org/rfc/rfc2822.txt RFC2822 * @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters * @param string $value * @return boolean */ public function isValid($value) { $valueString = (string) $value; $this->_setValue($valueString); // Split email address up if (!preg_match('/^(.+)@([^@]+)$/', $valueString, $matches)) { $this->_error(self::INVALID); return false; } $this->_localPart = $matches[1]; $this->_hostname = $matches[2]; // Match hostname part $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())->isValid($this->_hostname); if (!$hostnameResult) { $this->_error(self::INVALID_HOSTNAME); // Get messages and errors from hostnameValidator foreach ($this->hostnameValidator->getMessages() as $message) { $this->_messages[] = $message; } foreach ($this->hostnameValidator->getErrors() as $error) { $this->_errors[] = $error; } } // MX check on hostname via dns_get_record() if ($this->_validateMx) { if ($this->validateMxSupported()) { $result = dns_get_mx($this->_hostname, $mxHosts); if (count($mxHosts) < 1) { $hostnameResult = false; $this->_error(self::INVALID_MX_RECORD); } } else { /** * MX checks are not supported by this system * @see Zend_Validate_Exception */ require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception('Internal error: MX checking not available on this system'); } } // First try to match the local part on the common dot-atom format $localResult = false; // Dot-atom characters are: 1*atext *("." 1*atext) // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*", // "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~" $atext = 'a-zA-Z0-9\\x21\\x23\\x24\\x25\\x26\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b\\x7c\\x7d'; if (preg_match('/^[' . $atext . ']+(\\x2e+[' . $atext . ']+)*$/', $this->_localPart)) { $localResult = true; } else { // Try quoted string format // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE // qtext: Non white space controls, and the rest of the US-ASCII characters not // including "\" or the quote character $noWsCtl = '\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f'; $qtext = $noWsCtl . '\\x21\\x23-\\x5b\\x5d-\\x7e'; $ws = '\\x20\\x09'; if (preg_match('/^\\x22([' . $ws . $qtext . '])*[$ws]?\\x22$/', $this->_localPart)) { $localResult = true; } else { $this->_error(self::DOT_ATOM); $this->_error(self::QUOTED_STRING); $this->_error(self::INVALID_LOCAL_PART); } } // If both parts valid, return true if ($localResult && $hostnameResult) { return true; } else { return false; } }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is a valid email address * according to RFC2822 * * @link http://www.ietf.org/rfc/rfc2822.txt RFC2822 * @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters * @param string $value * @return boolean */ public function isValid($value) { $valueString = (string) $value; $matches = array(); $length = true; $this->_setValue($valueString); // Split email address up and disallow '..' if (strpos($valueString, '..') !== false or !preg_match('/^(.+)@([^@]+)$/', $valueString, $matches)) { $this->_error(self::INVALID); return false; } $this->_localPart = $matches[1]; $this->_hostname = $matches[2]; if (strlen($this->_localPart) > 64 || strlen($this->_hostname) > 255) { $length = false; $this->_error(self::LENGTH_EXCEEDED); } // Match hostname part $hostnameResult = $this->hostnameValidator->setTranslator($this->getTranslator())->isValid($this->_hostname); if (!$hostnameResult) { $this->_error(self::INVALID_HOSTNAME); // Get messages and errors from hostnameValidator foreach ($this->hostnameValidator->getMessages() as $code => $message) { $this->_messages[$code] = $message; } foreach ($this->hostnameValidator->getErrors() as $error) { $this->_errors[] = $error; } } else { if ($this->_validateMx) { // MX check on hostname via dns_get_record() if ($this->validateMxSupported()) { $result = dns_get_mx($this->_hostname, $mxHosts); if (count($mxHosts) < 1) { // the address may can accept email even if there's no MX record but A or A6 or AAAA record found if ($this->_deepMxCheck && $this->validateDnsSupported()) { if ($this->isReservedIpAddress($this->_hostname)) { $hostnameResult = false; $this->_error(self::INVALID_NETWORK_SEGMENT); } else { if (checkdnsrr($this->_hostname, "A") === false && checkdnsrr($this->_hostname, "AAAA") === false && checkdnsrr($this->_hostname, "A6") === false) { $hostnameResult = false; $this->_error(self::INVALID_MX_RECORD); } } } else { $hostnameResult = false; $this->_error(self::INVALID_MX_RECORD); } } else { // must check every MX record for at least one valid address if ($this->_deepMxCheck && $this->validateDnsSupported()) { $hasOneValidAddress = false; foreach ($mxHosts as $key => $hostname) { if (!$this->isReservedIpAddress($hostname) && (checkdnsrr($hostname, "A") !== false || checkdnsrr($hostname, "AAAA") !== false || checkdnsrr($hostname, "A6") !== false)) { $hasOneValidAddress = true; break; } } if ($hasOneValidAssress === false) { $hostnameResult = false; $this->_error(self::INVALID_MX_RECORD); } } } } else { /** * MX checks are not supported by this system * @see Zend_Validate_Exception */ require_once 'Zend/Validate/Exception.php'; throw new Zend_Validate_Exception('Internal error: MX checking not available on this system'); } } } // First try to match the local part on the common dot-atom format $localResult = false; // Dot-atom characters are: 1*atext *("." 1*atext) // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*", // "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~" $atext = 'a-zA-Z0-9\\x21\\x23\\x24\\x25\\x26\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b\\x7c\\x7d\\x7e'; if (preg_match('/^[' . $atext . ']+(\\x2e+[' . $atext . ']+)*$/', $this->_localPart)) { $localResult = true; } else { // Try quoted string format // Quoted-string characters are: DQUOTE *([FWS] qtext/quoted-pair) [FWS] DQUOTE // qtext: Non white space controls, and the rest of the US-ASCII characters not // including "\" or the quote character $noWsCtl = '\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f'; $qtext = $noWsCtl . '\\x21\\x23-\\x5b\\x5d-\\x7e'; $ws = '\\x20\\x09'; if (preg_match('/^\\x22([' . $ws . $qtext . '])*[$ws]?\\x22$/', $this->_localPart)) { $localResult = true; } else { $this->_error(self::DOT_ATOM); $this->_error(self::QUOTED_STRING); $this->_error(self::INVALID_LOCAL_PART); } } // If both parts valid, return true if ($localResult && $hostnameResult && $length) { return true; } else { return false; } }