1 | <?php |
||
10 | class DNSCheckValidation implements EmailValidation |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $warnings = []; |
||
16 | |||
17 | /** |
||
18 | * @var InvalidEmail |
||
19 | */ |
||
20 | private $error; |
||
21 | |||
22 | 12 | public function isValid($email, EmailLexer $emailLexer) |
|
23 | { |
||
24 | // use the input to check DNS if we cannot extract something similar to a domain |
||
25 | 12 | $host = $email; |
|
26 | |||
27 | // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email |
||
28 | 12 | if (false !== $lastAtPos = strrpos($email, '@')) { |
|
29 | 11 | $host = substr($email, $lastAtPos + 1); |
|
30 | 11 | } |
|
31 | |||
32 | 12 | return $this->checkDNS($host); |
|
33 | } |
||
34 | |||
35 | 1 | public function getError() |
|
36 | { |
||
37 | 1 | return $this->error; |
|
38 | } |
||
39 | |||
40 | 1 | public function getWarnings() |
|
41 | { |
||
42 | 1 | return $this->warnings; |
|
43 | } |
||
44 | |||
45 | 12 | protected function checkDNS($host) |
|
46 | { |
||
47 | 12 | if ( defined('INTL_IDNA_VARIANT_UTS46') ) { |
|
48 | $variant = INTL_IDNA_VARIANT_UTS46 |
||
49 | 12 | } else { |
|
|
|||
50 | 12 | $variant = INTL_IDNA_VARIANT_2003; |
|
51 | } |
||
52 | 12 | ||
53 | 11 | $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.'; |
|
54 | 11 | ||
55 | 11 | $Aresult = true; |
|
56 | 3 | $MXresult = checkdnsrr($host, 'MX'); |
|
57 | 3 | ||
58 | 11 | if (!$MXresult) { |
|
59 | 12 | $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
|
60 | $Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'); |
||
61 | if (!$Aresult) { |
||
62 | $this->error = new NoDNSRecord(); |
||
68 |