1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Validation; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Exception\InvalidEmail; |
7
|
|
|
use Egulias\EmailValidator\Warning\NoDNSMXRecord; |
8
|
|
|
use Egulias\EmailValidator\Exception\NoDNSRecord; |
9
|
|
|
|
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 |
|
$variant = INTL_IDNA_VARIANT_2003; |
48
|
12 |
|
if ( defined('INTL_IDNA_VARIANT_UTS46') ) { |
49
|
12 |
|
$variant = INTL_IDNA_VARIANT_UTS46; |
50
|
12 |
|
} |
51
|
12 |
|
$host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.'; |
52
|
|
|
|
53
|
12 |
|
$Aresult = true; |
54
|
12 |
|
$MXresult = checkdnsrr($host, 'MX'); |
55
|
|
|
|
56
|
12 |
|
if (!$MXresult) { |
57
|
3 |
|
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
58
|
3 |
|
$Aresult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'); |
59
|
3 |
|
if (!$Aresult) { |
60
|
3 |
|
$this->error = new NoDNSRecord(); |
61
|
3 |
|
} |
62
|
3 |
|
} |
63
|
12 |
|
return $MXresult || $Aresult; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|