1
|
|
|
<?php |
2
|
|
|
namespace RemotelyLiving\PHPDNS\Mappers; |
3
|
|
|
|
4
|
|
|
use RemotelyLiving\PHPDNS\Entities\DNSRecord; |
5
|
|
|
use RemotelyLiving\PHPDNS\Entities\DNSRecordType; |
6
|
|
|
|
7
|
|
|
class LocalSystem extends MapperAbstract |
8
|
|
|
{ |
9
|
|
|
private const PHP_CODE_TYPE_MAP = [ |
10
|
|
|
\DNS_A => DNSRecordType::TYPE_A, |
11
|
|
|
\DNS_CNAME => DNSRecordType::TYPE_CNAME, |
12
|
|
|
\DNS_HINFO => DNSRecordType::TYPE_HINFO, |
13
|
|
|
\DNS_CAA => DNSRecordType::TYPE_CAA, |
14
|
|
|
\DNS_MX => DNSRecordType::TYPE_MX, |
15
|
|
|
\DNS_NS => DNSRecordType::TYPE_NS, |
16
|
|
|
\DNS_PTR => DNSRecordType::TYPE_PTR, |
17
|
|
|
\DNS_SOA => DNSRecordType::TYPE_SOA, |
18
|
|
|
\DNS_TXT => DNSRecordType::TYPE_TXT, |
19
|
|
|
\DNS_AAAA => DNSRecordType::TYPE_AAAA, |
20
|
|
|
\DNS_SRV => DNSRecordType::TYPE_SRV, |
21
|
|
|
\DNS_NAPTR => DNSRecordType::TYPE_NAPTR, |
22
|
|
|
\DNS_A6 => DNSRecordType::TYPE_A6, |
23
|
|
|
\DNS_ANY => DNSRecordType::TYPE_ANY, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public function toDNSRecord(): DNSRecord |
27
|
|
|
{ |
28
|
|
|
$IPAddress = null; |
29
|
|
|
|
30
|
|
|
if (isset($this->fields['ipv6'])) { |
31
|
|
|
$IPAddress = $this->fields['ipv6']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (isset($this->fields['ip'])) { |
35
|
|
|
$IPAddress = $this->fields['ip']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return DNSRecord::createFromPrimitives( |
39
|
|
|
$this->fields['type'], |
40
|
|
|
$this->fields['host'], |
41
|
|
|
$this->fields['ttl'], |
42
|
|
|
$IPAddress, |
43
|
|
|
$this->fields['class'], |
44
|
|
|
$this->formatData($this->fields) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getTypeCodeFromType(DNSRecordType $type): int |
49
|
|
|
{ |
50
|
|
|
return array_flip(self::PHP_CODE_TYPE_MAP)[(string)$type] ?? \DNS_ANY; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function formatData(array $fields): ?string |
54
|
|
|
{ |
55
|
|
|
if (isset($this->fields['flags'], $fields['tag'], $fields['value'])) { |
56
|
|
|
return "{$fields['flags']} {$fields['tag']} \"{$fields['value']}\""; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($fields['mname'])) { |
60
|
|
|
$template = '%s %s %s %s %s %s %s'; |
61
|
|
|
return sprintf( |
62
|
|
|
$template, |
63
|
|
|
$fields['mname'], |
64
|
|
|
$fields['rname'], |
65
|
|
|
$fields['serial'], |
66
|
|
|
$fields['refresh'], |
67
|
|
|
$fields['retry'], |
68
|
|
|
$fields['expire'], |
69
|
|
|
$fields['minimum-ttl'] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (isset($fields['target'], $fields['pri'])) { |
74
|
|
|
return "{$fields['pri']} {$fields['target']}"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($fields['target'])) { |
78
|
|
|
return $fields['target']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (isset($fields['txt'])) { |
82
|
|
|
return $fields['txt']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|