1 | <?php |
||
12 | class DNSCheckValidation implements EmailValidation |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $warnings = []; |
||
18 | |||
19 | /** |
||
20 | * @var InvalidEmail|null |
||
21 | */ |
||
22 | private $error; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $mxRecords = []; |
||
28 | |||
29 | |||
30 | 23 | public function __construct() |
|
36 | |||
37 | 23 | public function isValid($email, EmailLexer $emailLexer) |
|
82 | |||
83 | 13 | public function getError() |
|
87 | |||
88 | public function getWarnings() |
||
92 | |||
93 | /** |
||
94 | * @param string $host |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | 12 | protected function checkDns($host) |
|
106 | |||
107 | |||
108 | /** |
||
109 | * Validate the DNS records for given host. |
||
110 | * |
||
111 | * @param string $host A set of DNS records in the format returned by dns_get_record. |
||
112 | * |
||
113 | * @return bool True on success. |
||
114 | */ |
||
115 | 12 | private function validateDnsRecords($host) |
|
116 | { |
||
117 | // Get all MX, A and AAAA DNS records for host |
||
118 | // Using @ as workaround to fix https://bugs.php.net/bug.php?id=73149 |
||
119 | 12 | $dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA); |
|
120 | |||
121 | |||
122 | // No MX, A or AAAA DNS records |
||
123 | 12 | if (empty($dnsRecords)) { |
|
124 | 2 | $this->error = new NoDNSRecord(); |
|
125 | 2 | return false; |
|
126 | } |
||
127 | |||
128 | // For each DNS record |
||
129 | 10 | foreach ($dnsRecords as $dnsRecord) { |
|
130 | 10 | if (!$this->validateMXRecord($dnsRecord)) { |
|
131 | 1 | return false; |
|
132 | } |
||
133 | 10 | } |
|
134 | |||
135 | // No MX records (fallback to A or AAAA records) |
||
136 | 9 | if (empty($this->mxRecords)) { |
|
137 | $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
||
138 | } |
||
139 | |||
140 | 9 | return true; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Validate an MX record |
||
145 | * |
||
146 | * @param array $dnsRecord Given DNS record. |
||
147 | * |
||
148 | * @return bool True if valid. |
||
149 | */ |
||
150 | 10 | private function validateMxRecord($dnsRecord) |
|
166 | } |
||
167 |