1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Dns; |
4
|
|
|
|
5
|
|
|
use Spatie\Dns\Exceptions\CouldNotFetchDns; |
6
|
|
|
use Spatie\Dns\Exceptions\InvalidArgument; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
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
|
|
|
'+noidnout', |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$process = new Process($command); |
118
|
|
|
|
119
|
|
|
$process->run(); |
120
|
|
|
|
121
|
|
|
if (! $process->isSuccessful()) { |
122
|
|
|
throw CouldNotFetchDns::digReturnedWithError(trim($process->getErrorOutput())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $process->getOutput(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getSpecificNameserverPart(): ?string |
129
|
|
|
{ |
130
|
|
|
if ($this->nameserver === '') { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return '@'.$this->nameserver; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|