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