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