From e0e748a0bc98a3c675198bc6048c7564d9527697 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Sun, 15 May 2016 08:46:23 +0200 Subject: [PATCH] outsource record-generation and zone-generation to classes for better handling Signed-off-by: Michael Kaufmann (d00p) --- lib/classes/dns/class.DnsEntry.php | 47 +++++++++++++++++++ lib/classes/dns/class.DnsZone.php | 44 +++++++++++++++++ .../dns/function.createDomainZone.php | 32 +++++-------- lng/english.lng.php | 1 + templates/Sparkle/dns_editor/index.tpl | 7 +++ 5 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 lib/classes/dns/class.DnsEntry.php create mode 100644 lib/classes/dns/class.DnsZone.php diff --git a/lib/classes/dns/class.DnsEntry.php b/lib/classes/dns/class.DnsEntry.php new file mode 100644 index 00000000..f1e36ded --- /dev/null +++ b/lib/classes/dns/class.DnsEntry.php @@ -0,0 +1,47 @@ + (2016-) + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Classes + * + */ +class DnsEntry +{ + + public $record; + + public $ttl; + + public $class = 'IN'; + + public $type; + + public $priority; + + public $content; + + public function __construct($record = '', $type = 'A', $content = null, $prio = 0, $ttl = 18000, $class = 'IN') + { + $this->record = $record; + $this->type = $type; + $this->content = $content; + $this->priority = $prio; + $this->ttl = $ttl; + $this->class = $class; + } + + public function __toString() + { + $result = $this->record . "\t" . $this->ttl . "\t" . $this->class . "\t" . $this->type . "\t" . (($this->prio >= 0 && ($this->type == 'MX' || $this->type == 'SRV')) ? $this->prio . "\t" : "") . $this->content . PHP_EOL; + return $result; + } +} diff --git a/lib/classes/dns/class.DnsZone.php b/lib/classes/dns/class.DnsZone.php new file mode 100644 index 00000000..b1b04eb5 --- /dev/null +++ b/lib/classes/dns/class.DnsZone.php @@ -0,0 +1,44 @@ + (2016-) + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package Classes + * + */ +class DnsZone +{ + + public $ttl; + + public $origin; + + public $records; + + public function __construct($ttl = 18000, $origin = '', $records = null) + { + $this->ttl = $ttl; + $this->origin = $origin; + $this->records = $records; + } + + public function __toString() + { + $_zonefile = "\$TTL " . $this->ttl . PHP_EOL; + $_zonefile .= "\$ORIGIN " . $this->origin . "." . PHP_EOL; + if (! empty($this->records)) { + foreach ($this->records as $record) { + $_zonefile .= (string) $record; + } + } + return $_zonefile; + } +} diff --git a/lib/functions/dns/function.createDomainZone.php b/lib/functions/dns/function.createDomainZone.php index e3d72a62..4b990127 100644 --- a/lib/functions/dns/function.createDomainZone.php +++ b/lib/functions/dns/function.createDomainZone.php @@ -106,7 +106,7 @@ function createDomainZone($domain_id, $froxlorhostname = false) } $primary_ns = null; - $zonefile = ""; + $zonerecords = array(); // now generate all records and unset the required entries we have foreach ($dom_entries as $entry) { @@ -121,7 +121,7 @@ function createDomainZone($domain_id, $froxlorhostname = false) // use the first NS entry as primary ns $primary_ns = $entry['content']; } - $zonefile .= formatEntry($entry['record'], $entry['type'], $entry['content'], $entry['prio'], $entry['ttl']); + $zonerecords[] = new DnsEntry($entry['record'], $entry['type'], $entry['content'], $entry['prio'], $entry['ttl']); } // add missing required entries @@ -151,9 +151,9 @@ function createDomainZone($domain_id, $froxlorhostname = false) foreach ($required_entries as $type => $records) { foreach ($records as $record) { if ($type == 'A' && filter_var($ip['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) { - $zonefile .= formatEntry($record, 'A', $ip['ip']); + $zonerecords[] = new DnsEntry($record, 'A', $ip['ip']); } elseif ($type == 'AAAA' && filter_var($ip['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) { - $zonefile .= formatEntry($record, 'AAAA', $ip['ip']); + $zonerecords[] = new DnsEntry($record, 'AAAA', $ip['ip']); } } } @@ -179,7 +179,7 @@ function createDomainZone($domain_id, $froxlorhostname = false) // use the first NS entry as primary ns $primary_ns = $nameserver; } - $zonefile .= formatEntry($record, 'NS', $nameserver); + $zonerecords[] = new DnsEntry($record, 'NS', $nameserver); } } } @@ -205,7 +205,7 @@ function createDomainZone($domain_id, $froxlorhostname = false) foreach ($required_entries as $type => $records) { if ($type == 'MX') { foreach ($records as $record) { - $zonefile .= formatEntry($record, 'MX', $mx_details[1], $mx_details[0]); + $zonerecords[] = new DnsEntry($record, 'MX', $mx_details[1], $mx_details[0]); } } } @@ -226,16 +226,16 @@ function createDomainZone($domain_id, $froxlorhostname = false) foreach ($records as $record) { if ($record == '@SPF@') { $txt_content = Settings::Get('spf.spf_entry'); - $zonefile .= formatEntry('@', 'TXT', encloseTXTContent($txt_content)); + $zonerecords[] = new DnsEntry('@', 'TXT', encloseTXTContent($txt_content)); } elseif ($record == 'dkim_' . $domain['dkim_id'] . '._domainkey' && ! empty($dkim_entries)) { // check for multiline entry $multiline = false; if (substr($dkim_entries[0], 0, 1) == '(') { $multiline = true; } - $zonefile .= formatEntry($record, 'TXT', encloseTXTContent($dkim_entries[0], $multiline)); + $zonerecords[] = new DnsEntry($record, 'TXT', encloseTXTContent($dkim_entries[0], $multiline)); } elseif ($record == '_adsp._domainkey' && ! empty($dkim_entries) && isset($dkim_entries[1])) { - $zonefile .= formatEntry($record, 'TXT', encloseTXTContent($dkim_entries[1])); + $zonerecords[] = new DnsEntry($record, 'TXT', encloseTXTContent($dkim_entries[1])); } } } @@ -256,18 +256,12 @@ function createDomainZone($domain_id, $froxlorhostname = false) $soa_content .= "604800\t; expire (7 days)" . PHP_EOL; $soa_content .= "1200\t)\t; minimum (20 mins)"; - $_zonefile = "\$TTL " . (int) Settings::Get('system.defaultttl') . PHP_EOL; - $_zonefile .= "\$ORIGIN " . $domain['domain'] . "." . PHP_EOL; - $_zonefile .= formatEntry('@', 'SOA', $soa_content); - $zonefile = $_zonefile . $zonefile; + $soa_record = new DnsEntry('@', 'SOA', $soa_content); + array_unshift($zonerecords, $soa_record); - return $zonefile; -} + $zone = new DnsZone((int) Settings::Get('system.defaultttl'), $domain['domain'], $zonerecords); -function formatEntry($record = '@', $type = 'A', $content = null, $prio = 0, $ttl = 18000, $class = 'IN') -{ - $result = $record . "\t" . $ttl . "\t" . $class . "\t" . $type . "\t" . (($prio >= 0 && ($type == 'MX' || $type == 'SRV')) ? $prio . "\t" : "") . $content . PHP_EOL; - return $result; + return (string)$zone; } function addRequiredEntry($record = '@', $type = 'A', &$required) diff --git a/lng/english.lng.php b/lng/english.lng.php index 47e8d9f3..43538921 100644 --- a/lng/english.lng.php +++ b/lng/english.lng.php @@ -2012,3 +2012,4 @@ $lng['dnseditor']['records'] = 'records'; $lng['error']['dns_notfoundorallowed'] = 'Domain not found or no permission'; $lng['serversettings']['dnseditorenable']['title'] = 'Enable DNS editor'; $lng['serversettings']['dnseditorenable']['description'] = 'Allows admins and customer to manage domain dns entries'; +$lng['dns']['howitworks'] = 'Here you can manage DNS entries for your domain. Note that froxlor will automatically generate NS/MX/A/AAAA records for you. The custom entries are prefered, only missing entries will be automatically generated.'; diff --git a/templates/Sparkle/dns_editor/index.tpl b/templates/Sparkle/dns_editor/index.tpl index bc18f76f..732c3e90 100644 --- a/templates/Sparkle/dns_editor/index.tpl +++ b/templates/Sparkle/dns_editor/index.tpl @@ -7,6 +7,13 @@ $header +
+
+
{$lng['admin']['warning']}
+
{$lng['dns']['howitworks']}
+
+
+
{$lng['error']['error']}