summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/validator/src/EmailAddressValidator.php
blob: 52c3697717c4c5bca1d0b00f471ec867e9e555e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php

namespace ipl\Validator;

use Exception;
use ipl\I18n\Translation;

/**
 * Validates an email address
 *
 * Email Address syntax: (<local part>@<domain-literal part>)
 *
 * We currently do not support dot-atom syntax (refer RFC 2822 [https://www.ietf.org/rfc/rfc2822.txt]
 * documentation for more details) for domain-literal part of an email address
 *
 */
class EmailAddressValidator extends BaseValidator
{
    use Translation;

    /**
     * If MX check should be enabled
     *
     * @var bool
     */
    protected $mx = false;

    /**
     * If a deep MX check should be enabled
     *
     * @var bool
     */
    protected $deep = false;

    /**
     * Create a new E-mail address validator with optional options
     *
     * Optional options:
     *
     * 'mx'   => If an MX check should be enabled, boolean
     * 'deep' => If a deep MX check should be enabled, boolean
     *
     * @param array $options
     *
     * @throws Exception
     */
    public function __construct(array $options = [])
    {
        if (array_key_exists('mx', $options)) {
            $this->setEnableMxCheck($options['mx']);
        }

        if (array_key_exists('deep', $options)) {
            $this->setEnableDeepMxCheck($options['deep']);
        }
    }

    /**
     * Set MX check
     *
     * To validate if the hostname is a DNS mail exchange (MX) record set it to true
     *
     * @param bool $mx if MX check should be enabled
     *
     * @return $this
     */
    public function setEnableMxCheck(bool $mx = true): self
    {
        $this->mx = $mx;

        return $this;
    }

    /**
     * Set Deep MX check
     *
     * To validate if the hostname is a DNS mail exchange (MX) record, and it points to an A record (for IPv4) or
     * an AAAA / A6 record (for IPv6) set it to true
     *
     * @param bool $deep if deep MX check should be enabled
     *
     * @return $this
     *
     * @throws Exception in case MX check has not been enabled
     */
    public function setEnableDeepMxCheck(bool $deep = true): self
    {
        if (! $this->mx) {
            throw new Exception("MX record check has to be enabled to enable deep MX record check");
        }

        $this->deep = $deep;

        return $this;
    }

    /**
     * Validate the local part (username / the part before '@') of the email address
     *
     * @param string $localPart
     * @param string $email
     *
     * @return bool
     */
    private function validateLocalPart(string $localPart, string $email): bool
    {
        // First try to match the local part on the common dot-atom format
        $result = false;

        // Dot-atom characters are: 1*atext *("." 1*atext)
        // atext: ALPHA / DIGIT / and "!", "#", "$", "%", "&", "'", "*",
        //        "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~"
        $atext = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d\x7e';
        if (preg_match('/^[' . $atext . ']+(\x2e+[' . $atext . ']+)*$/', $localPart)) {
            $result = true;
        } else {
            // Try quoted string format (RFC 5321 Chapter 4.1.2)

            // Quoted-string characters are: DQUOTE *(qtext/quoted-pair) DQUOTE
            $qtext = '\x20-\x21\x23-\x5b\x5d-\x7e'; // %d32-33 / %d35-91 / %d93-126
            $quotedPair = '\x20-\x7e'; // %d92 %d32-126
            if (preg_match('/^"([' . $qtext . ']|\x5c[' . $quotedPair . '])*"$/', $localPart)) {
                $result = true;
            } else {
                $this->addMessage(sprintf(
                    $this->translate(
                        "'%s' can not be matched against dot-atom format or quoted-string format"
                    ),
                    $localPart
                ));
                $this->addMessage(sprintf(
                    $this->translate("Hence '%s' is not a valid local part for email address '%s'"),
                    $localPart,
                    $email
                ));
            }
        }

        return $result;
    }

    /**
     * Validate the hostname part of the email address
     *
     * @param string $hostname
     * @param string $email
     *
     * @return bool
     */
    private function validateHostnamePart(string $hostname, string $email): bool
    {
        $hostValidator = new HostnameValidator();

        if ($this->validateIp($hostname)) {
            return true;
        }

        if (preg_match('/^\[([^\]]*)\]$/i', $hostname, $matches)) {
            $validHostname = $matches[1];
            if (! $this->validateIp($validHostname)) {
                $this->addMessage(sprintf(
                    $this->translate("host name %s is a domain literal and is invalid"),
                    $hostname
                ));

                return false;
            }

            return true;
        }

        if (! $hostValidator->isValid($hostname)) {
            $this->addMessage(sprintf(
                $this->translate('%s is not a valid domain name for email address %s.'),
                $hostname,
                $email
            ));

            return false;
        } elseif ($this->mx) {
            // MX check on hostname
            return $this->validateMXRecords($hostname, $email);
        }

        return true;
    }

    /**
     * Check if the given IP address is valid
     *
     * @param string $value
     *
     * @return bool
     */
    private function validateIp(string $value): bool
    {
        if (! filter_var($value, FILTER_VALIDATE_IP)) {
            return false;
        }

        return true;
    }

    /**
     * Returns true if and only if $value is a valid email address
     * according to RFC2822
     *
     * @param string $value
     *
     * @return bool
     */
    public function isValid($value): bool
    {
        $this->clearMessages();

        $matches = [];
        $length = true;

        // Split email address up and disallow '..'
        if (
            (strpos($value, '..') !== false)
            || (! preg_match('/^(.+)@([^@]+)$/', $value, $matches))
        ) {
            $this->addMessage(sprintf(
                $this->translate("'%s' is not a valid email address in the basic format local-part@hostname"),
                $value
            ));
            return false;
        }

        $localPart = $matches[1];
        $hostname = $matches[2];

        if ((strlen($localPart) > 64) || (strlen($hostname) > 255)) {
            $length = false;
            $this->addMessage(sprintf(
                $this->translate("'%s' exceeds the allowed length"),
                $value
            ));
        }

        $local = $this->validateLocalPart($localPart, $value);

        // If both parts valid, return true
        if (($local && $this->validateHostnamePart($hostname, $value)) && $length) {
            return true;
        }

        return false;
    }

    /**
     * Perform deep MX record validation
     *
     * Check if the hostname is a valid DNS mail exchange (MX) record in case deep MX record check is enabled,
     * also checks if the corresponding MX record points to an A record (for IPv4) or an AAAA / A6 record (for IPv6)
     *
     * @param string $hostname
     * @param string $email
     *
     * @return bool
     */
    private function validateMXRecords(string $hostname, string $email): bool
    {
        $mxHosts = [];
        //decode IDN domain name
        $decodedHostname = idn_to_ascii($hostname, 0, INTL_IDNA_VARIANT_UTS46);

        $result = getmxrr($decodedHostname, $mxHosts);
        if (! $result) {
            $this->addMessage(sprintf(
                $this->translate("'%s' does not appear to have a valid MX record for the email address '%s'"),
                $hostname,
                $email
            ));
        } elseif ($this->deep) {
            $validAddress = false;
            $reserved     = true;
            foreach ($mxHosts as $decodedHostname) {
                $res = $this->isReserved($decodedHostname);
                if (! $res) {
                    $reserved = false;
                }

                if (
                    ! $res
                    && (
                        checkdnsrr($decodedHostname, "A")
                        || checkdnsrr($decodedHostname, "AAAA")
                        || checkdnsrr($decodedHostname, "A6")
                    )
                ) {
                    $validAddress = true;
                    break;
                }
            }

            if (! $validAddress) {
                $result = false;
                if ($reserved) {
                    $this->addMessage(sprintf(
                        $this->translate(
                            "'%s' is not in a routable network segment." .
                            " The email address '%s' should not be resolved from public network"
                        ),
                        $hostname,
                        $email
                    ));
                } else {
                    $this->addMessage(sprintf(
                        $this->translate("'%s' does not appear to have a valid MX record for the email address '%s'"),
                        $hostname,
                        $email
                    ));
                }
            }
        }

        return $result;
    }

    /**
     * Validate whether the given host is reserved
     *
     * @param string $host host name or ip address
     *
     * @return bool
     */
    private function isReserved(string $host): bool
    {
        if (! preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $host)) {
            $host = gethostbyname($host);
        }

        if (! filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)) {
            return true;
        }

        return false;
    }
}