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