Skip to content

Commit

Permalink
Update bind.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneMcC authored Nov 28, 2017
1 parent 37e0dea commit 51c5c54
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function isDebug() {
* @param $file (optional) File to load domain info from
*/
function __construct($domain, $zonedirectory, $file = '') {
$domain = idn_to_ascii($domain);
$this->domain = $domain;
$this->zonedirectory = $zonedirectory;
if ($file == '' || !file_exists($file) || !is_file($file) || !is_readable($file)) {
Expand Down Expand Up @@ -161,7 +162,7 @@ function parseZoneFile() {
}
}

$type = strtoupper($bits[$pos]);
$type = strtoupper(isset($bits[$pos]) ? $bits[$pos] : '');
$pos++;
$this->debug('parseZoneFile', 'Got Line of Type: '.$type.' ('.$line.')');

Expand All @@ -177,7 +178,11 @@ function parseZoneFile() {
$len = strlen($this->domain)+1;
$end = substr($name, strlen($name) - $len);

if ($type != 'SOA') {
if ($type == 'SOA') {
if ($name == $origin) {
$name = $this->domain . '.';
}
} else {
if ($end == $this->domain.'.') {
if ($name != $end) {
$name = substr($name, 0, strlen($name) - $len - 1);
Expand All @@ -194,7 +199,6 @@ function parseZoneFile() {

// Add params to this bit first, we add it to domainInfo afterwards
$info = array();

switch ($type) {
case 'SOA':
// SOAs can span multiple lines.
Expand All @@ -216,7 +220,7 @@ function parseZoneFile() {
$line = trim($file[++$i]);
$bits = preg_split('/\s+/', $line);
foreach ($bits as $bit) {
if ($bit{0} == ';') { break; }
if (trim($bit) == '' || $bit{0} == ';') { break; }
$soabits[] = $bit;
}
} else {
Expand All @@ -227,7 +231,7 @@ function parseZoneFile() {
$info['Refresh'] = $soabits[1];
$info['Retry'] = $soabits[2];
$info['Expire'] = $soabits[3];
$info['MinTTL'] = $soabits[4];
$info['MinTTL'] = rtrim($soabits[4], ')');
break;
case 'MX':
case 'SRV':
Expand Down Expand Up @@ -310,6 +314,8 @@ function getSOA() {
* @param $soa The SOA record for this domain.
*/
function setSOA($soa) {
$soa['Nameserver'] = idn_to_ascii(substr($soa['Nameserver'], -1) == '.' ? substr($soa['Nameserver'], 0, -1) : $soa['Nameserver']) . '.';
$soa['Email'] = idn_to_ascii(substr($soa['Email'], -1) == '.' ? substr($soa['Email'], 0, -1) : $soa['Email']) . '.';
$this->domainInfo['SOA'][$this->domain.'.'][0] = $soa;
}

Expand Down Expand Up @@ -342,6 +348,7 @@ function setMETA($meta) {
* @param $priority (optional) Priority of the record (for mx)
*/
function setRecord($name, $type, $data, $ttl = '', $priority = '') {
$name = idn_to_ascii($name);
$domainInfo = $this->domainInfo;
if ($ttl == '') { $ttl = $domainInfo[' META ']['TTL']; }

Expand All @@ -351,6 +358,10 @@ function setRecord($name, $type, $data, $ttl = '', $priority = '') {
$info['Priority'] = $priority;
}

if ($type == 'MX' || $type == 'CNAME' || $type == 'PTR' || $type == 'NS') {
$info['Address'] = idn_to_ascii($info['Address']);
}

if (!isset($domainInfo[$type][$name])) { $domainInfo[$type][$name] = array(); };
$domainInfo[$type][$name][] = $info;

Expand Down Expand Up @@ -390,6 +401,22 @@ function getRecords($name, $type) {
}
}

private function ksortr(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
$this->ksortr($value);
}
}

return ksort($array);
}

function getZoneHash() {
$hashData = $this->domainInfo;
$this->ksortr($hashData);
return base_convert(crc32(json_encode($hashData)), 10, 36);
}

/**
* Clear all records (does not clear SOA or META)
*/
Expand All @@ -413,6 +440,7 @@ function getParsedZoneFile() {
$lines = array();

$lines[] = '; Written at '.date('r');
$lines[] = '; Zone Hash: '.$this->getZoneHash();

// TTL and ORIGIN First
if (isset($domainInfo[' META ']['TTL'])) {
Expand Down

0 comments on commit 51c5c54

Please sign in to comment.