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