3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator
;
12 class EmailAddress
extends AbstractValidator
14 const INVALID
= 'emailAddressInvalid';
15 const INVALID_FORMAT
= 'emailAddressInvalidFormat';
16 const INVALID_HOSTNAME
= 'emailAddressInvalidHostname';
17 const INVALID_MX_RECORD
= 'emailAddressInvalidMxRecord';
18 const INVALID_SEGMENT
= 'emailAddressInvalidSegment';
19 const DOT_ATOM
= 'emailAddressDotAtom';
20 const QUOTED_STRING
= 'emailAddressQuotedString';
21 const INVALID_LOCAL_PART
= 'emailAddressInvalidLocalPart';
22 const LENGTH_EXCEEDED
= 'emailAddressLengthExceeded';
27 protected $messageTemplates = array(
28 self
::INVALID
=> "Invalid type given. String expected",
29 self
::INVALID_FORMAT
=> "The input is not a valid email address. Use the basic format local-part@hostname",
30 self
::INVALID_HOSTNAME
=> "'%hostname%' is not a valid hostname for the email address",
31 self
::INVALID_MX_RECORD
=> "'%hostname%' does not appear to have any valid MX or A records for the email address",
32 self
::INVALID_SEGMENT
=> "'%hostname%' is not in a routable network segment. The email address should not be resolved from public network",
33 self
::DOT_ATOM
=> "'%localPart%' can not be matched against dot-atom format",
34 self
::QUOTED_STRING
=> "'%localPart%' can not be matched against quoted-string format",
35 self
::INVALID_LOCAL_PART
=> "'%localPart%' is not a valid local part for the email address",
36 self
::LENGTH_EXCEEDED
=> "The input exceeds the allowed length",
42 protected $messageVariables = array(
43 'hostname' => 'hostname',
44 'localPart' => 'localPart'
58 * Returns the found mx record informations
65 * Internal options array
67 protected $options = array(
68 'useMxCheck' => false,
69 'useDeepMxCheck' => false,
70 'useDomainCheck' => true,
71 'allow' => Hostname
::ALLOW_DNS
,
72 'hostnameValidator' => null,
76 * Instantiates hostname validator for local use
78 * The following additional option keys are supported:
79 * 'hostnameValidator' => A hostname validator, see Zend\Validator\Hostname
80 * 'allow' => Options for the hostname validator, see Zend\Validator\Hostname::ALLOW_*
81 * 'useMxCheck' => If MX check should be enabled, boolean
82 * 'useDeepMxCheck' => If a deep MX check should be done, boolean
84 * @param array|\Traversable $options OPTIONAL
86 public function __construct($options = array())
88 if (!is_array($options)) {
89 $options = func_get_args();
90 $temp['allow'] = array_shift($options);
91 if (!empty($options)) {
92 $temp['useMxCheck'] = array_shift($options);
95 if (!empty($options)) {
96 $temp['hostnameValidator'] = array_shift($options);
102 parent
::__construct($options);
106 * Sets the validation failure message template for a particular key
107 * Adds the ability to set messages to the attached hostname validator
109 * @param string $messageString
110 * @param string $messageKey OPTIONAL
111 * @return AbstractValidator Provides a fluent interface
113 public function setMessage($messageString, $messageKey = null)
115 if ($messageKey === null) {
116 $this->getHostnameValidator()->setMessage($messageString);
117 parent
::setMessage($messageString);
121 if (!isset($this->messageTemplates
[$messageKey])) {
122 $this->getHostnameValidator()->setMessage($messageString, $messageKey);
124 parent
::setMessage($messageString, $messageKey);
131 * Returns the set hostname validator
133 * If was not previously set then lazy load a new one
137 public function getHostnameValidator()
139 if (!isset($this->options
['hostnameValidator'])) {
140 $this->options
['hostnameValidator'] = new Hostname($this->getAllow());
143 return $this->options
['hostnameValidator'];
147 * @param Hostname $hostnameValidator OPTIONAL
148 * @return EmailAddress Provides a fluent interface
150 public function setHostnameValidator(Hostname
$hostnameValidator = null)
152 $this->options
['hostnameValidator'] = $hostnameValidator;
158 * Returns the allow option of the attached hostname validator
162 public function getAllow()
164 return $this->options
['allow'];
168 * Sets the allow option of the hostname validator to use
171 * @return EmailAddress Provides a fluent interface
173 public function setAllow($allow)
175 $this->options
['allow'] = $allow;
176 if (isset($this->options
['hostnameValidator'])) {
177 $this->options
['hostnameValidator']->setAllow($allow);
184 * Whether MX checking via getmxrr is supported or not
188 public function isMxSupported()
190 return function_exists('getmxrr');
194 * Returns the set validateMx option
198 public function getMxCheck()
200 return $this->options
['useMxCheck'];
204 * Set whether we check for a valid MX record via DNS
206 * This only applies when DNS hostnames are validated
208 * @param bool $mx Set allowed to true to validate for MX records, and false to not validate them
209 * @return EmailAddress Fluid Interface
211 public function useMxCheck($mx)
213 $this->options
['useMxCheck'] = (bool) $mx;
218 * Returns the set deepMxCheck option
222 public function getDeepMxCheck()
224 return $this->options
['useDeepMxCheck'];
228 * Use deep validation for MX records
230 * @param bool $deep Set deep to true to perform a deep validation process for MX records
231 * @return EmailAddress Fluid Interface
233 public function useDeepMxCheck($deep)
235 $this->options
['useDeepMxCheck'] = (bool) $deep;
240 * Returns the set domainCheck option
244 public function getDomainCheck()
246 return $this->options
['useDomainCheck'];
250 * Sets if the domain should also be checked
251 * or only the local part of the email address
253 * @param bool $domain
254 * @return EmailAddress Fluid Interface
256 public function useDomainCheck($domain = true)
258 $this->options
['useDomainCheck'] = (bool) $domain;
263 * Returns if the given host is reserved
265 * The following addresses are seen as reserved
266 * '0.0.0.0/8', '10.0.0.0/8', '127.0.0.0/8'
270 * '169.254.0.0/16', '192.168.0.0/16'
271 * '192.0.2.0/24', '192.88.99.0/24', '198.51.100.0/24', '203.0.113.0/24'
272 * '224.0.0.0/4', '240.0.0.0/4'
273 * @see http://en.wikipedia.org/wiki/Reserved_IP_addresses
275 * As of RFC5753 (JAN 2010), the following blocks are no longer reserved:
279 * @see http://tools.ietf.org/html/rfc5735#page-6
281 * As of RFC6598 (APR 2012), the following blocks are now reserved:
283 * @see http://tools.ietf.org/html/rfc6598#section-7
285 * @param string $host
286 * @return bool Returns false when minimal one of the given addresses is not reserved
288 protected function isReserved($host)
290 if (!preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $host)) {
291 $host = gethostbynamel($host);
293 $host = array($host);
300 foreach ($host as $server) {
301 // Search for 0.0.0.0/8, 10.0.0.0/8, 127.0.0.0/8
302 if (!preg_match('/^(0|10|127)(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){3}$/', $server) &&
303 // Search for 100.64.0.0/10
304 !preg_match('/^100\.(6[0-4]|[7-9][0-9]|1[0-1][0-9]|12[0-7])(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){2}$/', $server) &&
305 // Search for 172.16.0.0/12
306 !preg_match('/^172\.(1[6-9]|2[0-9]|3[0-1])(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){2}$/', $server) &&
307 // Search for 198.18.0.0/15
308 !preg_match('/^198\.(1[8-9])(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){2}$/', $server) &&
309 // Search for 169.254.0.0/16, 192.168.0.0/16
310 !preg_match('/^(169\.254|192\.168)(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){2}$/', $server) &&
311 // Search for 192.0.2.0/24, 192.88.99.0/24, 198.51.100.0/24, 203.0.113.0/24
312 !preg_match('/^(192\.0\.2|192\.88\.99|198\.51\.100|203\.0\.113)\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$/', $server) &&
313 // Search for 224.0.0.0/4, 240.0.0.0/4
314 !preg_match('/^(2(2[4-9]|[3-4][0-9]|5[0-5]))(\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))){3}$/', $server)
324 * Internal method to validate the local part of the email address
328 protected function validateLocalPart()
330 // First try to match the local part on the common dot-atom format
333 // Dot-atom characters are: 1*atext *("." 1*atext)
334 // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
335 // "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
336 $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
337 if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $this->localPart
)) {
340 // Try quoted string format (RFC 5321 Chapter 4.1.2)
342 // Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE
343 $qtext = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126
344 $quotedPair = '\x20-\x7e'; // %d92 %d32-126
345 if (preg_match('/^"(['. $qtext .']|\x5c[' . $quotedPair . '])*"$/', $this->localPart
)) {
348 $this->error(self
::DOT_ATOM
);
349 $this->error(self
::QUOTED_STRING
);
350 $this->error(self
::INVALID_LOCAL_PART
);
358 * Returns the found MX Record information after validation including weight for further processing
362 public function getMXRecord()
364 return $this->mxRecord
;
368 * Internal method to validate the servers MX records
372 protected function validateMXRecords()
376 $result = getmxrr($this->hostname
, $mxHosts, $weight);
377 if (!empty($mxHosts) && !empty($weight)) {
378 $this->mxRecord
= array_combine($mxHosts, $weight);
380 $this->mxRecord
= $mxHosts;
383 arsort($this->mxRecord
);
385 // Fallback to IPv4 hosts if no MX record found (RFC 2821 SS 5).
387 $result = gethostbynamel($this->hostname
);
388 if (is_array($result)) {
389 $this->mxRecord
= array_flip($result);
394 $this->error(self
::INVALID_MX_RECORD
);
398 if (!$this->options
['useDeepMxCheck']) {
402 $validAddress = false;
404 foreach ($this->mxRecord
as $hostname => $weight) {
405 $res = $this->isReserved($hostname);
411 && (checkdnsrr($hostname, "A")
412 ||
checkdnsrr($hostname, "AAAA")
413 ||
checkdnsrr($hostname, "A6"))
415 $validAddress = true;
420 if (!$validAddress) {
422 $error = ($reserved) ? self
::INVALID_SEGMENT
: self
::INVALID_MX_RECORD
;
423 $this->error($error);
430 * Internal method to validate the hostname part of the email address
434 protected function validateHostnamePart()
436 $hostname = $this->getHostnameValidator()->setTranslator($this->getTranslator())
437 ->isValid($this->hostname
);
439 $this->error(self
::INVALID_HOSTNAME
);
440 // Get messages and errors from hostnameValidator
441 foreach ($this->getHostnameValidator()->getMessages() as $code => $message) {
442 $this->abstractOptions
['messages'][$code] = $message;
444 } elseif ($this->options
['useMxCheck']) {
445 // MX check on hostname
446 $hostname = $this->validateMXRecords();
453 * Splits the given value in hostname and local part of the email address
455 * @param string $value Email address to be split
456 * @return bool Returns false when the email can not be split
458 protected function splitEmailParts($value)
460 // Split email address up and disallow '..'
461 if ((strpos($value, '..') !== false) or
462 (!preg_match('/^(.+)@([^@]+)$/', $value, $matches))) {
466 $this->localPart
= $matches[1];
467 $this->hostname
= $matches[2];
473 * Defined by Zend\Validator\ValidatorInterface
475 * Returns true if and only if $value is a valid email address
476 * according to RFC2822
478 * @link http://www.ietf.org/rfc/rfc2822.txt RFC2822
479 * @link http://www.columbia.edu/kermit/ascii.html US-ASCII characters
480 * @param string $value
483 public function isValid($value)
485 if (!is_string($value)) {
486 $this->error(self
::INVALID
);
491 $this->setValue($value);
493 // Split email address up and disallow '..'
494 if (!$this->splitEmailParts($value)) {
495 $this->error(self
::INVALID_FORMAT
);
499 if ((strlen($this->localPart
) > 64) ||
(strlen($this->hostname
) > 255)) {
501 $this->error(self
::LENGTH_EXCEEDED
);
504 // Match hostname part
505 if ($this->options
['useDomainCheck']) {
506 $hostname = $this->validateHostnamePart();
509 $local = $this->validateLocalPart();
511 // If both parts valid, return true
512 if ($local && $length) {
513 if (($this->options
['useDomainCheck'] && $hostname) ||
!$this->options
['useDomainCheck']) {