| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace RemotelyLiving\PHPDNS\Resolvers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use RemotelyLiving\PHPDNS\Entities\DNSRecordCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use RemotelyLiving\PHPDNS\Entities\DNSRecordType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use RemotelyLiving\PHPDNS\Entities\Hostname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use RemotelyLiving\PHPDNS\Factories\SpatieDNS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use RemotelyLiving\PHPDNS\Mappers\Dig as DigMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use RemotelyLiving\PHPDNS\Resolvers\Exceptions\QueryFailure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Dig extends ResolverAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private const SUPPORTED_QUERY_TYPES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         DNSRecordType::TYPE_A, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         DNSRecordType::TYPE_AAAA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         DNSRecordType::TYPE_CNAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         DNSRecordType::TYPE_NS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         DNSRecordType::TYPE_SOA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         DNSRecordType::TYPE_MX, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         DNSRecordType::TYPE_SRV, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         DNSRecordType::TYPE_TXT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         DNSRecordType::TYPE_CAA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         DNSRecordType::TYPE_NAPTR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var \RemotelyLiving\PHPDNS\Factories\SpatieDNS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $spatieDNSFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var \RemotelyLiving\PHPDNS\Mappers\Dig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var \RemotelyLiving\PHPDNS\Entities\Hostname|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $nameserver; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         SpatieDNS $spatieDNSFactory = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         DigMapper $mapper = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         Hostname $nameserver = null | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->spatieDNSFactory = $spatieDNSFactory ?? new SpatieDNS(); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->mapper = $mapper ?? new DigMapper(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->nameserver = $nameserver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function doQuery(Hostname $hostname, DNSRecordType $type): DNSRecordCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (!self::isSupportedQueryType($type)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return new DNSRecordCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $dig = $this->spatieDNSFactory->createResolver($hostname, $this->nameserver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $response = ($type->equals(DNSRecordType::createANY())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ? $dig->getRecords(...self::SUPPORTED_QUERY_TYPES) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 : $dig->getRecords((string) $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new QueryFailure($e->getMessage(), 0, $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this->mapResults($this->mapper, self::parseDigResponseToRows($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private static function parseDigResponseToRows(string $digResponse): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $rows = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach (explode(PHP_EOL, self::normalizeColumns($digResponse)) as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if (!trim($line)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $columns = explode(' ', $line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $rows[] = [$columns[0], $columns[1], $columns[2], $columns[3], implode(' ', array_slice($columns, 4))]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $rows; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     private static function normalizeColumns(string $response): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $keysRemoved = preg_replace('/;(.*)/m', ' ', trim($response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $tabsRemoved = preg_replace('/(\t)/i', ' ', (string) $keysRemoved); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $breaksRemoved = preg_replace('/\s\s/m', '', (string) $tabsRemoved); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return (string) preg_replace('/(\(\s|(\s\)))/m', '', (string) $breaksRemoved); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     private static function isSupportedQueryType(DNSRecordType $type): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if ($type->isA(DNSRecordType::TYPE_ANY)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         foreach (self::SUPPORTED_QUERY_TYPES as $queryType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if ($type->isA($queryType)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |