samuelwilliams /
PHP-DNS-SERVER
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of PHP DNS Server. |
||
| 5 | * |
||
| 6 | * (c) Yif Swery <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace yswery\DNS\Resolver; |
||
| 13 | |||
| 14 | use yswery\DNS\ClassEnum; |
||
| 15 | use yswery\DNS\RecordTypeEnum; |
||
| 16 | use yswery\DNS\ResourceRecord; |
||
| 17 | use yswery\DNS\UnsupportedTypeException; |
||
| 18 | |||
| 19 | class XmlResolver extends AbstractResolver |
||
| 20 | { |
||
| 21 | private $defaultClass = ClassEnum::INTERNET; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * XmlResolver constructor. |
||
| 25 | * |
||
| 26 | * @param array $files |
||
| 27 | * |
||
| 28 | * @throws UnsupportedTypeException |
||
| 29 | */ |
||
| 30 | 23 | public function __construct(array $files) |
|
| 31 | { |
||
| 32 | 23 | $this->isAuthoritative = true; |
|
| 33 | 23 | $this->allowRecursion = false; |
|
| 34 | |||
| 35 | 23 | foreach ($files as $file) { |
|
| 36 | 23 | $xml = new \SimpleXMLElement(file_get_contents($file)); |
|
| 37 | 23 | $this->addZone($this->process($xml)); |
|
| 38 | } |
||
| 39 | 23 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param object $xml |
||
| 43 | * |
||
| 44 | * @return ResourceRecord[] |
||
| 45 | * |
||
| 46 | * @throws UnsupportedTypeException |
||
| 47 | */ |
||
| 48 | 23 | private function process($xml): array |
|
| 49 | { |
||
| 50 | 23 | $parent = (string) $xml->{'name'}; |
|
| 51 | 23 | $defaultTtl = (int) $xml->{'default-ttl'}; |
|
| 52 | 23 | $resourceRecords = []; |
|
| 53 | |||
| 54 | 23 | foreach ($xml->{'resource-records'}->{'resource-record'} as $rr) { |
|
| 55 | 23 | $name = (string) $rr->{'name'} ?? $parent; |
|
| 56 | 23 | $class = isset($rr->{'class'}) ? ClassEnum::getClassFromName($rr->{'class'}) : $this->defaultClass; |
|
| 57 | 23 | $ttl = isset($rr->{'ttl'}) ? (int) $rr->{'ttl'} : $defaultTtl; |
|
| 58 | |||
| 59 | 23 | $resourceRecords[] = (new ResourceRecord()) |
|
| 60 | 23 | ->setName($this->handleName($name, $parent)) |
|
| 61 | 23 | ->setClass($class) |
|
| 62 | 23 | ->setType($type = RecordTypeEnum::getTypeFromName($rr->{'type'})) |
|
| 63 | 23 | ->setTtl($ttl) |
|
| 64 | 23 | ->setRdata($this->extractRdata($this->simpleXmlToArray($rr->{'rdata'}), $type, $parent)); |
|
| 65 | } |
||
| 66 | |||
| 67 | 23 | return $resourceRecords; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Convert a SimpleXML object to an associative array. |
||
| 72 | * |
||
| 73 | * @param \SimpleXMLElement $xmlObject |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 23 | private function simpleXmlToArray(\SimpleXMLElement $xmlObject): array |
|
| 78 | { |
||
| 79 | 23 | $array = []; |
|
| 80 | 23 | foreach ($xmlObject->children() as $node) { |
|
| 81 | 23 | $array[$node->getName()] = is_array($node) ? $this->simpleXmlToArray($node) : (string) $node; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 82 | } |
||
| 83 | |||
| 84 | 23 | return $array; |
|
| 85 | } |
||
| 86 | } |
||
| 87 |