EmailValidator.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use Yii;
  9. use yii\base\ErrorException;
  10. use yii\base\InvalidConfigException;
  11. use yii\helpers\Json;
  12. use yii\web\JsExpression;
  13. /**
  14. * EmailValidator validates that the attribute value is a valid email address.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class EmailValidator extends Validator
  20. {
  21. /**
  22. * @var string the regular expression used to validate the attribute value.
  23. * @see http://www.regular-expressions.info/email.html
  24. */
  25. public $pattern = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';
  26. /**
  27. * @var string the regular expression used to validate email addresses with the name part.
  28. * This property is used only when [[allowName]] is true.
  29. * @see allowName
  30. */
  31. public $fullPattern = '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';
  32. /**
  33. * @var bool whether to allow name in the email address (e.g. "John Smith <john.smith@example.com>"). Defaults to false.
  34. * @see fullPattern
  35. */
  36. public $allowName = false;
  37. /**
  38. * @var bool whether to check whether the email's domain exists and has either an A or MX record.
  39. * Be aware that this check can fail due to temporary DNS problems even if the email address is
  40. * valid and an email would be deliverable. Defaults to false.
  41. */
  42. public $checkDNS = false;
  43. /**
  44. * @var bool whether validation process should take into account IDN (internationalized domain
  45. * names). Defaults to false meaning that validation of emails containing IDN will always fail.
  46. * Note that in order to use IDN validation you have to install and enable `intl` PHP extension,
  47. * otherwise an exception would be thrown.
  48. */
  49. public $enableIDN = false;
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function init()
  54. {
  55. parent::init();
  56. if ($this->enableIDN && !function_exists('idn_to_ascii')) {
  57. throw new InvalidConfigException('In order to use IDN validation intl extension must be installed and enabled.');
  58. }
  59. if ($this->message === null) {
  60. $this->message = Yii::t('yii', '{attribute} is not a valid email address.');
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function validateValue($value)
  67. {
  68. if (!is_string($value)) {
  69. $valid = false;
  70. } elseif (!preg_match('/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?(?:(?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))$/i', $value, $matches)) {
  71. $valid = false;
  72. } else {
  73. if ($this->enableIDN) {
  74. $matches['local'] = $this->idnToAscii($matches['local']);
  75. $matches['domain'] = $this->idnToAscii($matches['domain']);
  76. $value = $matches['name'] . $matches['open'] . $matches['local'] . '@' . $matches['domain'] . $matches['close'];
  77. }
  78. if (strlen($matches['local']) > 64) {
  79. // The maximum total length of a user name or other local-part is 64 octets. RFC 5322 section 4.5.3.1.1
  80. // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
  81. $valid = false;
  82. } elseif (strlen($matches['local'] . '@' . $matches['domain']) > 254) {
  83. // There is a restriction in RFC 2821 on the length of an address in MAIL and RCPT commands
  84. // of 254 characters. Since addresses that do not fit in those fields are not normally useful, the
  85. // upper limit on address lengths should normally be considered to be 254.
  86. //
  87. // Dominic Sayers, RFC 3696 erratum 1690
  88. // http://www.rfc-editor.org/errata_search.php?eid=1690
  89. $valid = false;
  90. } else {
  91. $valid = preg_match($this->pattern, $value) || ($this->allowName && preg_match($this->fullPattern, $value));
  92. if ($valid && $this->checkDNS) {
  93. $valid = $this->isDNSValid($matches['domain']);
  94. }
  95. }
  96. }
  97. return $valid ? null : [$this->message, []];
  98. }
  99. /**
  100. * @param string $domain
  101. * @return bool if DNS records for domain are valid
  102. * @see https://github.com/yiisoft/yii2/issues/17083
  103. */
  104. protected function isDNSValid($domain)
  105. {
  106. return $this->hasDNSRecord($domain, true) || $this->hasDNSRecord($domain, false);
  107. }
  108. private function hasDNSRecord($domain, $isMX)
  109. {
  110. $normalizedDomain = $domain . '.';
  111. if (!checkdnsrr($normalizedDomain, ($isMX ? 'MX' : 'A'))) {
  112. return false;
  113. }
  114. try {
  115. // dns_get_record can return false and emit Warning that may or may not be converted to ErrorException
  116. $records = dns_get_record($normalizedDomain, ($isMX ? DNS_MX : DNS_A));
  117. } catch (ErrorException $exception) {
  118. return false;
  119. }
  120. return !empty($records);
  121. }
  122. private function idnToAscii($idn)
  123. {
  124. if (PHP_VERSION_ID < 50600) {
  125. // TODO: drop old PHP versions support
  126. return idn_to_ascii($idn);
  127. }
  128. return idn_to_ascii($idn, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function clientValidateAttribute($model, $attribute, $view)
  134. {
  135. ValidationAsset::register($view);
  136. if ($this->enableIDN) {
  137. PunycodeAsset::register($view);
  138. }
  139. $options = $this->getClientOptions($model, $attribute);
  140. return 'yii.validation.email(value, messages, ' . Json::htmlEncode($options) . ');';
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getClientOptions($model, $attribute)
  146. {
  147. $options = [
  148. 'pattern' => new JsExpression($this->pattern),
  149. 'fullPattern' => new JsExpression($this->fullPattern),
  150. 'allowName' => $this->allowName,
  151. 'message' => $this->formatMessage($this->message, [
  152. 'attribute' => $model->getAttributeLabel($attribute),
  153. ]),
  154. 'enableIDN' => (bool) $this->enableIDN,
  155. ];
  156. if ($this->skipOnEmpty) {
  157. $options['skipOnEmpty'] = 1;
  158. }
  159. return $options;
  160. }
  161. }