1 | <?php |
||
9 | class Dns |
||
10 | { |
||
11 | protected string $domain = ''; |
||
|
|||
12 | |||
13 | protected string $nameserver = ''; |
||
14 | |||
15 | protected array $recordTypes = [ |
||
16 | 'A', |
||
17 | 'AAAA', |
||
18 | 'CNAME', |
||
19 | 'NS', |
||
20 | 'SOA', |
||
21 | 'MX', |
||
22 | 'SRV', |
||
23 | 'TXT', |
||
24 | 'DNSKEY', |
||
25 | 'CAA', |
||
26 | 'NAPTR', |
||
27 | ]; |
||
28 | |||
29 | public function __construct(string $domain, string $nameserver = '') |
||
30 | { |
||
31 | if (empty($domain)) { |
||
32 | throw InvalidArgument::domainIsMissing(); |
||
33 | } |
||
34 | |||
35 | $this->nameserver = $nameserver; |
||
36 | |||
37 | $this->domain = $this->sanitizeDomainName($domain); |
||
38 | } |
||
39 | |||
40 | public function useNameserver(string $nameserver) |
||
41 | { |
||
42 | $this->nameserver = $nameserver; |
||
43 | |||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | public function getDomain(): string |
||
48 | { |
||
49 | return $this->domain; |
||
50 | } |
||
51 | |||
52 | public function getNameserver(): string |
||
53 | { |
||
54 | return $this->nameserver; |
||
55 | } |
||
56 | |||
57 | |||
58 | public function getRecords(...$types): string |
||
59 | { |
||
60 | $types = $this->determineTypes($types); |
||
61 | |||
62 | $types = count($types) |
||
63 | ? $types |
||
64 | : $this->recordTypes; |
||
65 | |||
66 | $dnsRecords = array_map([$this, 'getRecordsOfType'], $types); |
||
67 | |||
68 | return implode('', array_filter($dnsRecords)); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @throws InvalidArgument |
||
73 | */ |
||
74 | protected function determineTypes(array $types): array |
||
75 | { |
||
76 | $types = is_array($types[0] ?? null) |
||
77 | ? $types[0] |
||
78 | : $types; |
||
79 | |||
80 | $types = array_map('strtoupper', $types); |
||
81 | |||
82 | if ($invalidTypes = array_diff($types, $this->recordTypes)) { |
||
83 | throw InvalidArgument::filterIsNotAValidRecordType(reset($invalidTypes), $this->recordTypes); |
||
84 | } |
||
85 | |||
86 | return $types; |
||
87 | } |
||
88 | |||
89 | protected function sanitizeDomainName(string $domain): string |
||
90 | { |
||
91 | $domain = str_replace(['http://', 'https://'], '', $domain); |
||
92 | |||
93 | $domain = strtok($domain, '/'); |
||
94 | |||
95 | return strtolower($domain); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @throws CouldNotFetchDns |
||
100 | */ |
||
101 | protected function getRecordsOfType(string $type): string |
||
102 | { |
||
103 | $nameserverPart = $this->getSpecificNameserverPart(); |
||
104 | |||
105 | $command = array_filter([ |
||
106 | 'dig', |
||
107 | '+nocmd', |
||
108 | $nameserverPart, |
||
109 | $this->domain, |
||
110 | $type, |
||
111 | '+multiline', |
||
112 | '+noall', |
||
113 | '+answer', |
||
114 | ]); |
||
115 | |||
116 | $process = new Process($command); |
||
117 | |||
118 | $process->run(); |
||
119 | |||
120 | if (! $process->isSuccessful()) { |
||
121 | throw CouldNotFetchDns::digReturnedWithError(trim($process->getErrorOutput())); |
||
122 | } |
||
123 | |||
124 | return $process->getOutput(); |
||
125 | } |
||
126 | |||
127 | protected function getSpecificNameserverPart(): ?string |
||
128 | { |
||
129 | if ($this->nameserver === '') { |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | return '@'.$this->nameserver; |
||
134 | } |
||
135 | } |
||
136 |