1 | <?php |
||
29 | class EmailValidator implements ValidatorInterface |
||
30 | { |
||
31 | |||
32 | use AllowEmpty, |
||
33 | Messages, |
||
34 | OnScenario, |
||
35 | Safe; |
||
36 | |||
37 | const EmailPattern = '/^[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?\.)+[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?$/u'; |
||
38 | const FullEmailPattern = '/^[^@]*<[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\\.[\p{L}0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?\\.)+[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?>$/u'; |
||
39 | |||
40 | /** |
||
41 | * @var string the regular expression used to validate the attribute value. |
||
42 | * @see http://www.regular-expressions.info/email.html |
||
43 | */ |
||
44 | public $pattern = self::EmailPattern; |
||
45 | |||
46 | /** |
||
47 | * @var boolean whether to check the MX record for the email address. |
||
48 | * Defaults to false. To enable it, you need to make sure the PHP function 'checkdnsrr' |
||
49 | * exists in your PHP installation. |
||
50 | * Please note that this check may fail due to temporary problems even if email is deliverable. |
||
51 | */ |
||
52 | public $checkMX = false; |
||
53 | |||
54 | /** |
||
55 | * @var boolean whether to check port 25 for the email address. |
||
56 | * Defaults to false. To enable it, ensure that the PHP functions 'dns_get_record' and |
||
57 | * 'fsockopen' are available in your PHP installation. |
||
58 | * Please note that this check may fail due to temporary problems even if email is deliverable. |
||
59 | */ |
||
60 | public $checkPort = false; |
||
61 | |||
62 | /** |
||
63 | * @Label('{attribute} must be valid email address') |
||
64 | * @var string |
||
65 | */ |
||
66 | public $msgValid = ''; |
||
67 | |||
68 | /** |
||
69 | * @Label('Email domain "{domain}" does not exists') |
||
70 | * @var string |
||
71 | */ |
||
72 | public $msgDomain = ''; |
||
73 | |||
74 | /** |
||
75 | * @Label('Email service does not seem to be running at "{domain}"') |
||
76 | * @var string |
||
77 | */ |
||
78 | public $msgPort = ''; |
||
79 | |||
80 | 6 | public function isValid(AnnotatedInterface $model, $attribute) |
|
81 | { |
||
82 | 6 | if ($this->allowEmpty && empty($model->$attribute)) |
|
83 | { |
||
84 | 2 | return true; |
|
85 | } |
||
86 | 4 | $label = ManganMeta::create($model)->field($attribute)->label; |
|
87 | |||
88 | 4 | if (!is_scalar($model->$attribute)) |
|
89 | { |
||
90 | $this->addError('msgValid', ['{attribute}' => $label]); |
||
91 | return false; |
||
92 | } |
||
93 | 4 | if (!preg_match($this->pattern, $model->$attribute)) |
|
94 | { |
||
95 | 1 | $this->addError('msgValid', ['{attribute}' => $label]); |
|
96 | 1 | return false; |
|
97 | } |
||
98 | 4 | $domain = rtrim(substr($model->$attribute, strpos($model->$attribute, '@') + 1), '>'); |
|
99 | 4 | if ($this->checkMX) |
|
100 | { |
||
101 | 1 | if (function_exists('checkdnsrr')) |
|
102 | { |
||
103 | 1 | if (!checkdnsrr($domain, 'MX')) |
|
104 | { |
||
105 | 1 | $this->addError('msgDomain', ['{domain}' => $domain]); |
|
106 | 1 | return false; |
|
107 | } |
||
108 | } |
||
109 | } |
||
110 | 4 | if ($this->checkPort) |
|
111 | { |
||
112 | if ($this->checkMxPorts($domain)) |
||
113 | { |
||
114 | $this->addError('msgPort', ['{domain}' => $domain]); |
||
115 | return false; |
||
116 | } |
||
117 | } |
||
118 | 4 | return true; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Retrieves the list of MX records for $domain and checks if port 25 |
||
123 | * is opened on any of these. |
||
124 | * @param string $domain domain to be checked |
||
125 | * @return boolean true if a reachable MX server has been found |
||
126 | */ |
||
127 | protected function checkMxPorts($domain) |
||
152 | |||
153 | } |
||
154 |