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