-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small fix for trailing dot on domains
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace yswery\DNS; | ||
|
||
use \Exception; | ||
|
||
class JsonStorageProvider extends AbstractStorageProvider { | ||
|
||
private $dns_records; | ||
private $DS_TTL; | ||
|
||
public function __construct($record_file, $default_ttl = 300) | ||
{ | ||
$handle = @fopen($record_file, "r"); | ||
if(!$handle) { | ||
throw new Exception('Unable to open dns record file.'); | ||
} | ||
|
||
$dns_json = fread($handle, filesize($record_file)); | ||
fclose($handle); | ||
|
||
$dns_records = json_decode($dns_json, true); | ||
if(!$dns_records) { | ||
throw new Exception('Unable to parse dns record file.'); | ||
} | ||
|
||
if(!is_int($default_ttl)) { | ||
throw new Exception('Default TTL must be an integer.'); | ||
} | ||
$this->DS_TTL = $default_ttl; | ||
|
||
$this->dns_records = $dns_records; | ||
} | ||
|
||
public function get_answer($question) | ||
{ | ||
$answer = array(); | ||
$domain = trim($question[0]['qname'], '.'); | ||
$type = RecordTypeEnum::get_name($question[0]['qtype']); | ||
|
||
if(isset($this->dns_records[$domain]) &&isset($this->dns_records[$domain][$type])) { | ||
if(is_array($this->dns_records[$domain][$type])) { | ||
foreach($this->dns_records[$domain][$type] as $ip) { | ||
$answer[] = array('name' => $question[0]['qname'], 'class' => $question[0]['qclass'], 'ttl' => $this->DS_TTL, 'data' => array('type' => $question[0]['qtype'], 'value' => $ip)); | ||
} | ||
} else { | ||
$answer[] = array('name' => $question[0]['qname'], 'class' => $question[0]['qclass'], 'ttl' => $this->DS_TTL, 'data' => array('type' => $question[0]['qtype'], 'value' => $this->dns_records[$domain][$type])); | ||
} | ||
} | ||
|
||
return $answer; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters