1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
8
|
|
|
use Yiisoft\Validator\Rule; |
9
|
|
|
use Yiisoft\Validator\Result; |
10
|
|
|
use Yiisoft\Validator\DataSetInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* EmailValidator validates that the attribute value is a valid email address. |
14
|
|
|
*/ |
15
|
|
|
class Email extends Rule |
16
|
|
|
{ |
17
|
|
|
use HasValidationErrorMessage; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string the regular expression used to validateValue the attribute value. |
21
|
|
|
* @see http://www.regular-expressions.info/email.html |
22
|
|
|
*/ |
23
|
|
|
private string $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])?$/'; |
24
|
|
|
/** |
25
|
|
|
* @var string the regular expression used to validateValue email addresses with the name part. |
26
|
|
|
* This property is used only when [[allowName]] is true. |
27
|
|
|
* @see allowName |
28
|
|
|
*/ |
29
|
|
|
private string $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])?>$/'; |
30
|
|
|
/** |
31
|
|
|
* @var bool whether to allow name in the email address (e.g. "John Smith <[email protected]>"). Defaults to false. |
32
|
|
|
* @see fullPattern |
33
|
|
|
*/ |
34
|
|
|
private bool $allowName = false; |
35
|
|
|
/** |
36
|
|
|
* @var bool whether to check whether the email's domain exists and has either an A or MX record. |
37
|
|
|
* Be aware that this check can fail due to temporary DNS problems even if the email address is |
38
|
|
|
* valid and an email would be deliverable. Defaults to false. |
39
|
|
|
*/ |
40
|
|
|
private bool $checkDNS = false; |
41
|
|
|
/** |
42
|
|
|
* @var bool whether validation process should take into account IDN (internationalized domain |
43
|
|
|
* names). Defaults to false meaning that validation of emails containing IDN will always fail. |
44
|
|
|
* Note that in order to use IDN validation you have to install and enable `intl` PHP extension, |
45
|
|
|
* otherwise an exception would be thrown. |
46
|
|
|
*/ |
47
|
|
|
private bool $enableIDN = false; |
48
|
|
|
|
49
|
|
|
private string $message = 'This value is not a valid email address.'; |
50
|
|
|
|
51
|
36 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
52
|
|
|
{ |
53
|
36 |
|
$result = new Result(); |
54
|
|
|
|
55
|
36 |
|
if (!is_string($value)) { |
56
|
1 |
|
$valid = false; |
57
|
36 |
|
} elseif (!preg_match( |
58
|
36 |
|
'/^(?P<name>(?:"?([^"]*)"?\s)?)(?:\s+)?(?:(?P<open><?)((?P<local>.+)@(?P<domain>[^>]+))(?P<close>>?))$/i', |
59
|
|
|
$value, |
60
|
|
|
$matches |
61
|
|
|
)) { |
62
|
2 |
|
$valid = false; |
63
|
|
|
} else { |
64
|
36 |
|
if ($this->enableIDN) { |
65
|
35 |
|
$matches['local'] = $this->idnToAscii($matches['local']); |
66
|
35 |
|
$matches['domain'] = $this->idnToAscii($matches['domain']); |
67
|
35 |
|
$value = $matches['name'] . $matches['open'] . $matches['local'] . '@' . $matches['domain'] . $matches['close']; |
68
|
|
|
} |
69
|
|
|
|
70
|
36 |
|
if (is_string($matches['local']) && strlen($matches['local']) > 64) { |
71
|
|
|
// The maximum total length of a user name or other local-part is 64 octets. RFC 5322 section 4.5.3.1.1 |
72
|
|
|
// http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1 |
73
|
1 |
|
$valid = false; |
74
|
36 |
|
} elseif (is_string($matches['local']) && strlen($matches['local'] . '@' . $matches['domain']) > 254) { |
75
|
|
|
// There is a restriction in RFC 2821 on the length of an address in MAIL and RCPT commands |
76
|
|
|
// of 254 characters. Since addresses that do not fit in those fields are not normally useful, the |
77
|
|
|
// upper limit on address lengths should normally be considered to be 254. |
78
|
|
|
// |
79
|
|
|
// Dominic Sayers, RFC 3696 erratum 1690 |
80
|
|
|
// http://www.rfc-editor.org/errata_search.php?eid=1690 |
81
|
1 |
|
$valid = false; |
82
|
|
|
} else { |
83
|
36 |
|
$valid = preg_match($this->pattern, $value) || ($this->allowName && preg_match( |
84
|
36 |
|
$this->fullPattern, |
85
|
|
|
$value |
86
|
|
|
)); |
87
|
36 |
|
if ($valid && $this->checkDNS) { |
88
|
|
|
$valid = checkdnsrr($matches['domain'] . '.', 'MX') || checkdnsrr($matches['domain'] . '.', 'A'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
36 |
|
if ($valid === false) { |
94
|
36 |
|
$result->addError($this->translateMessage($this->message)); |
95
|
|
|
} |
96
|
|
|
|
97
|
36 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
35 |
|
private function idnToAscii($idn) |
101
|
|
|
{ |
102
|
35 |
|
return idn_to_ascii($idn, 0, INTL_IDNA_VARIANT_UTS46); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function allowName(bool $allowName): self |
106
|
|
|
{ |
107
|
2 |
|
$new = clone $this; |
108
|
2 |
|
$new->allowName = $allowName; |
109
|
2 |
|
return $new; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function checkDNS(bool $checkDNS): self |
113
|
|
|
{ |
114
|
|
|
$new = clone $this; |
115
|
|
|
$new->checkDNS = $checkDNS; |
116
|
|
|
return $new; |
117
|
|
|
} |
118
|
|
|
|
119
|
35 |
|
public function enableIDN(bool $enableIDN): self |
120
|
|
|
{ |
121
|
35 |
|
if ($enableIDN && !function_exists('idn_to_ascii')) { |
122
|
|
|
throw new \RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
35 |
|
$new = clone $this; |
126
|
35 |
|
$new->enableIDN = $enableIDN; |
127
|
35 |
|
return $new; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function getName(): string |
131
|
|
|
{ |
132
|
1 |
|
return 'email'; |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
public function getOptions(): array |
136
|
|
|
{ |
137
|
4 |
|
return array_merge( |
138
|
|
|
[ |
139
|
4 |
|
'allowName' => $this->allowName, |
140
|
4 |
|
'checkDNS' => $this->checkDNS, |
141
|
4 |
|
'enableIDN' => $this->enableIDN, |
142
|
4 |
|
'message' => $this->translateMessage($this->message) |
143
|
|
|
], |
144
|
4 |
|
parent::getOptions() |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|