1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yswery\DNS\Resolver; |
4
|
|
|
|
5
|
|
|
use yswery\DNS\ClassEnum; |
6
|
|
|
use yswery\DNS\ResourceRecord; |
7
|
|
|
use yswery\DNS\RecordTypeEnum; |
8
|
|
|
use yswery\DNS\UnsupportedTypeException; |
9
|
|
|
|
10
|
|
|
class XmlResolver extends AbstractResolver |
11
|
|
|
{ |
12
|
|
|
private $defaultClass = ClassEnum::INTERNET; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* XmlResolver constructor. |
16
|
|
|
* |
17
|
|
|
* @param array $files |
18
|
|
|
* |
19
|
|
|
* @throws UnsupportedTypeException |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $files) |
22
|
|
|
{ |
23
|
|
|
$this->isAuthoritative = true; |
24
|
|
|
$this->allowRecursion = false; |
25
|
|
|
|
26
|
|
|
foreach ($files as $file) { |
27
|
|
|
$xml = new \SimpleXMLElement(file_get_contents($file)); |
28
|
|
|
$this->addZone($this->process($xml)); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param object $xml |
34
|
|
|
* |
35
|
|
|
* @return ResourceRecord[] |
36
|
|
|
* |
37
|
|
|
* @throws UnsupportedTypeException |
38
|
|
|
*/ |
39
|
|
|
private function process($xml): array |
40
|
|
|
{ |
41
|
|
|
$parent = (string) $xml->{'name'}; |
42
|
|
|
$defaultTtl = (int) $xml->{'default-ttl'}; |
43
|
|
|
$resourceRecords = []; |
44
|
|
|
|
45
|
|
|
foreach ($xml->{'resource-records'}->{'resource-record'} as $rr) { |
46
|
|
|
$name = (string) $rr->{'name'} ?? $parent; |
47
|
|
|
$class = isset($rr->{'class'}) ? ClassEnum::getClassFromName($rr->{'class'}) : $this->defaultClass; |
48
|
|
|
$ttl = isset($rr->{'ttl'}) ? (int) $rr->{'ttl'} : $defaultTtl; |
49
|
|
|
|
50
|
|
|
$resourceRecords[] = (new ResourceRecord()) |
51
|
|
|
->setName($this->handleName($name, $parent)) |
52
|
|
|
->setClass($class) |
53
|
|
|
->setType($type = RecordTypeEnum::getTypeIndex($rr->{'type'})) |
|
|
|
|
54
|
|
|
->setTtl($ttl) |
55
|
|
|
->setRdata($this->extractRdata($this->simpleXmlToArray($rr->{'rdata'}), $type, $parent)); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $resourceRecords; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convert a SimpleXML object to an associative array. |
63
|
|
|
* |
64
|
|
|
* @param \SimpleXMLElement $xmlObject |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
private function simpleXmlToArray(\SimpleXMLElement $xmlObject): array |
69
|
|
|
{ |
70
|
|
|
$array = []; |
71
|
|
|
foreach ($xmlObject->children() as $node) { |
72
|
|
|
$array[$node->getName()] = is_array($node) ? $this->simpleXmlToArray($node) : (string) $node; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $array; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|