1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Dns; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Spatie\Dns\Exceptions\InvalidArgument; |
8
|
|
|
|
9
|
|
|
class Dns |
10
|
|
|
{ |
11
|
|
|
protected $domain = ''; |
12
|
|
|
|
13
|
|
|
protected $nameserver = ''; |
14
|
|
|
|
15
|
|
|
protected $recordTypes = [ |
16
|
|
|
'A', |
17
|
|
|
'AAAA', |
18
|
|
|
'NS', |
19
|
|
|
'SOA', |
20
|
|
|
'MX', |
21
|
|
|
'TXT', |
22
|
|
|
'DNSKEY', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function __construct(string $domain, string $nameserver = '') |
26
|
|
|
{ |
27
|
|
|
if (empty($domain)) { |
28
|
|
|
throw InvalidArgument::domainIsMissing(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->nameserver = $nameserver; |
32
|
|
|
|
33
|
|
|
$this->domain = $this->sanitizeDomainName($domain); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function useNameserver(string $nameserver) |
37
|
|
|
{ |
38
|
|
|
$this->nameserver = $nameserver; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getDomain(): string |
44
|
|
|
{ |
45
|
|
|
return $this->domain; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getNameserver(): string |
49
|
|
|
{ |
50
|
|
|
return $this->nameserver; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getRecords(...$types): string |
54
|
|
|
{ |
55
|
|
|
$types = $this->determineTypes($types); |
56
|
|
|
|
57
|
|
|
$types = count($types) |
58
|
|
|
? $types |
59
|
|
|
: $this->recordTypes; |
60
|
|
|
|
61
|
|
|
$dnsRecords = array_map([$this, 'getRecordsOfType'], $types); |
62
|
|
|
|
63
|
|
|
return implode('', array_filter($dnsRecords)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function determineTypes(array $types): array |
67
|
|
|
{ |
68
|
|
|
$types = is_array($types[0] ?? null) |
69
|
|
|
? $types[0] |
70
|
|
|
: $types; |
71
|
|
|
|
72
|
|
|
$types = array_map('strtoupper', $types); |
73
|
|
|
|
74
|
|
|
foreach ($types as $type) { |
75
|
|
|
if (! in_array($type, $this->recordTypes)) { |
76
|
|
|
throw InvalidArgument::filterIsNotAValidRecordType($type, $this->recordTypes); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $types; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function sanitizeDomainName(string $domain): string |
84
|
|
|
{ |
85
|
|
|
$domain = str_replace(['http://', 'https://'], '', $domain); |
86
|
|
|
|
87
|
|
|
$domain = strtok($domain, '/'); |
88
|
|
|
|
89
|
|
|
return strtolower($domain); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getRecordsOfType(string $type): string |
93
|
|
|
{ |
94
|
|
|
$nameserverPart = $this->getSpecificNameserverPart(); |
95
|
|
|
|
96
|
|
|
$command = 'dig +nocmd '.$nameserverPart.' '.escapeshellarg($this->domain)." {$type} +multiline +noall +answer"; |
97
|
|
|
|
98
|
|
|
$process = new Process($command); |
99
|
|
|
|
100
|
|
|
$process->run(); |
101
|
|
|
|
102
|
|
|
if (! $process->isSuccessful()) { |
103
|
|
|
throw new Exception('Dns records could not be fetched'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $process->getOutput(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getSpecificNameserverPart() |
110
|
|
|
{ |
111
|
|
|
if ($this->nameserver === '') { |
112
|
|
|
return ''; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return '@'.escapeshellarg($this->nameserver); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|