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