Test Setup Failed
Push — 3.0.0-dev ( 35c9db...729ecb )
by Eduardo Gulias
03:26
created

DNSCheckValidation::validateMxRecord()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace Egulias\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Result\InvalidEmail;
7
use Egulias\EmailValidator\Result\Reason\DomainAcceptsNoMail;
8
use Egulias\EmailValidator\Warning\NoDNSMXRecord;
9
use Egulias\EmailValidator\Result\Reason\LocalOrReservedDomain;
10
use Egulias\EmailValidator\Result\Reason\NoDNSRecord as ReasonNoDNSRecord;
11
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
    public function __construct()
31
    {
32
        if (!function_exists('idn_to_ascii')) {
33
            throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__));
34
        }
35
    }
36
37
    public function isValid($email, EmailLexer $emailLexer) : bool
38
    {
39
        // use the input to check DNS if we cannot extract something similar to a domain
40
        $host = $email;
41
42
        // Arguable pattern to extract the domain. Not aiming to validate the domain nor the email
43
        if (false !== $lastAtPos = strrpos($email, '@')) {
44
            $host = substr($email, $lastAtPos + 1);
45
        }
46
47
        // Get the domain parts
48
        $hostParts = explode('.', $host);
49
50
        // Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2),
51
        // mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G)
52
        $reservedTopLevelDnsNames = [
53
            // Reserved Top Level DNS Names
54
            'test',
55
            'example',
56
            'invalid',
57
            'localhost',
58
59
            // mDNS
60
            'local',
61
62
            // Private DNS Namespaces
63
            'intranet',
64
            'internal',
65
            'private',
66
            'corp',
67
            'home',
68
            'lan',
69
        ];
70
71
        $isLocalDomain = count($hostParts) <= 1;
72
        $isReservedTopLevel = in_array($hostParts[(count($hostParts) - 1)], $reservedTopLevelDnsNames, true);
73
74
        // Exclude reserved top level DNS names
75
        if ($isLocalDomain || $isReservedTopLevel) {
76
            $this->error = new InvalidEmail(new LocalOrReservedDomain(), $host);
77
            return false;
78
        }
79
80
        return $this->checkDns($host);
81
    }
82
83
    public function getError() : ?InvalidEmail
84
    {
85
        return $this->error;
86
    }
87
88
    public function getWarnings() : array
89
    {
90
        return $this->warnings;
91
    }
92
93
    /**
94
     * @param string $host
95
     *
96
     * @return bool
97
     */
98
    protected function checkDns($host)
99
    {
100
        $variant = INTL_IDNA_VARIANT_UTS46;
101
102
        $host = rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
103
104
        return $this->validateDnsRecords($host);
105
    }
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
    private function validateDnsRecords($host) : bool
116
    {
117
        // Get all MX, A and AAAA DNS records for host
118
        $dnsRecords = @dns_get_record($host, DNS_MX + DNS_A + DNS_AAAA);
119
120
121
        // No MX, A or AAAA DNS records
122
        if (empty($dnsRecords)) {
123
            $this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
124
            return false;
125
        }
126
127
        // For each DNS record
128
        foreach ($dnsRecords as $dnsRecord) {
129
            if (!$this->validateMXRecord($dnsRecord)) {
130
                // No MX records (fallback to A or AAAA records)
131
                if (empty($this->mxRecords)) {
132
                    $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
133
                }
134
                return false;
135
            }
136
        }
137
        return true;
138
    }
139
140
    /**
141
     * Validate an MX record
142
     *
143
     * @param array $dnsRecord Given DNS record.
144
     *
145
     * @return bool True if valid.
146
     */
147
    private function validateMxRecord($dnsRecord) : bool
148
    {
149
        if ($dnsRecord['type'] !== 'MX') {
150
            return true;
151
        }
152
153
        // "Null MX" record indicates the domain accepts no mail (https://tools.ietf.org/html/rfc7505)
154
        if (empty($dnsRecord['target']) || $dnsRecord['target'] === '.') {
155
            $this->error = new InvalidEmail(new DomainAcceptsNoMail(), "");
156
            return false;
157
        }
158
159
        $this->mxRecords[] = $dnsRecord;
160
161
        return true;
162
    }
163
}