From 7222e505ba0e2132fd6225fbf7fbd9613ab813e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Fri, 21 Oct 2022 10:15:31 +0200 Subject: [PATCH] Allow addding more than one local DNS/CNAME record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- cname_records.php | 2 +- dns_records.php | 2 +- scripts/pi-hole/php/func.php | 46 ++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/cname_records.php b/cname_records.php index 4804eb68..017fa014 100644 --- a/cname_records.php +++ b/cname_records.php @@ -32,7 +32,7 @@ require 'scripts/pi-hole/php/header_authenticated.php';
- +
diff --git a/dns_records.php b/dns_records.php index b0d3f52c..9e3c55b6 100644 --- a/dns_records.php +++ b/dns_records.php @@ -32,7 +32,7 @@ require 'scripts/pi-hole/php/header_authenticated.php';
- +
diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index 3d8c450b..ef1a514b 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -196,6 +196,7 @@ function getCustomDNSEntries() $data = new \stdClass(); $data->ip = $explodedLine[0]; $data->domain = $explodedLine[1]; + $data->domains = array_slice($explodedLine, 0, -1); $entries[] = $data; } @@ -234,22 +235,39 @@ function addCustomDNSEntry($ip = '', $domain = '', $reload = '', $json = true) return returnError('Domain must be set', $json); } - if (!validDomain($domain)) { - return returnError('Domain must be valid', $json); + $num = 0; + // Check if each submitted domain is valid + $domains = array_map('trim', explode(',', $domain)); + foreach ($domains as $d) { + if (!validDomain($d)) { + return returnError("Domain '{$d}' is not valid", $json); + } + ++$num; } // Only check for duplicates if adding new records from the web UI (not through Teleporter) if (isset($_REQUEST['ip']) || isset($_REQUEST['domain'])) { $existingEntries = getCustomDNSEntries(); foreach ($existingEntries as $entry) { - if ($entry->domain == $domain && get_ip_type($entry->ip) == $ipType) { - return returnError('This domain already has a custom DNS entry for an IPv'.$ipType, $json); + foreach ($domains as $d) { + if ($entry->domain == $d && get_ip_type($entry->ip) == $ipType) { + return returnError("The domain {$d} already has a custom DNS entry for an IPv".$ipType, $json); + } } } } - // Add record - pihole_execute('-a addcustomdns '.$ip.' '.$domain.' '.$reload); + // if $reload is not set and more then one domain is supplied, restart FTL only once after all entries were added + if (($num > 0) && (empty($reload))) { + $reload = 'false'; + } + // Add records + foreach ($domains as $domain) { + pihole_execute('-a addcustomdns '.$ip.' '.$domain.' '.$reload); + } + if ($num > 0) { + pihole_execute('restartdns'); + } return returnSuccess('', $json); } catch (\Exception $ex) { @@ -392,6 +410,7 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t return returnError('Target must be valid', $json); } + $num = 0; $domains = array_map('trim', explode(',', $domain)); foreach ($domains as $d) { // Check if each submitted domain is valid @@ -403,6 +422,8 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t if (strtolower($d) === strtolower($target)) { return returnError('Domain and target cannot be the same', $json); } + + ++$num; } $existingEntries = getCustomCNAMEEntries(); @@ -416,7 +437,18 @@ function addCustomCNAMEEntry($domain = '', $target = '', $reload = '', $json = t } } - pihole_execute('-a addcustomcname '.$domain.' '.$target.' '.$reload); + // if $reload is not set and more then one domain is supplied, restart FTL only once after all entries were added + if (($num > 0) && (empty($reload))) { + $reload = 'false'; + } + // add records + foreach ($domains as $d) { + pihole_execute('-a addcustomcname '.$d.' '.$target.' '.$reload); + } + + if ($num > 0) { + pihole_execute('restartdns'); + } return returnSuccess('', $json); } catch (\Exception $ex) {