diff --git a/scripts/pi-hole/js/db_queries.js b/scripts/pi-hole/js/db_queries.js
index 9c33bd18..0c3d2f21 100644
--- a/scripts/pi-hole/js/db_queries.js
+++ b/scripts/pi-hole/js/db_queries.js
@@ -99,7 +99,7 @@ function add(domain, list) {
domain: domain,
list: list,
token: token,
- action: "add_domain",
+ action: "replace_domain",
comment: "Added from Long-Term-Data Query Log"
},
success: function (response) {
diff --git a/scripts/pi-hole/js/queries.js b/scripts/pi-hole/js/queries.js
index 092dd610..681bf0fe 100644
--- a/scripts/pi-hole/js/queries.js
+++ b/scripts/pi-hole/js/queries.js
@@ -56,7 +56,7 @@ function add(domain, list) {
domain: domain,
list: list,
token: token,
- action: "add_domain",
+ action: "replace_domain",
comment: "Added from Query Log"
},
success: function (response) {
diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php
index 4c69c456..b594f565 100644
--- a/scripts/pi-hole/php/groups.php
+++ b/scripts/pi-hole/php/groups.php
@@ -497,7 +497,7 @@ if ($_POST['action'] == 'get_groups') {
} catch (\Exception $ex) {
JSON_error($ex->getMessage());
}
-} elseif ($_POST['action'] == 'add_domain') {
+} elseif ($_POST['action'] == 'add_domain' || $_POST['action'] == 'replace_domain') {
// Add new domain
try {
$domains = explode(' ', html_entity_decode(trim($_POST['domain'])));
@@ -509,6 +509,14 @@ if ($_POST['action'] == 'get_groups') {
throw new Exception('While preparing statement: ' . $db->lastErrorMsg());
}
+ $delstmt = null;
+ if($_POST['action'] == 'replace_domain') {
+ $delstmt = $db->prepare('DELETE FROM domainlist WHERE domain = :domain');
+ if (!$delstmt) {
+ throw new Exception('While preparing delete statement: ' . $db->lastErrorMsg());
+ }
+ }
+
if (isset($_POST['type'])) {
$type = intval($_POST['type']);
} else if (isset($_POST['list']) && $_POST['list'] === "white") {
@@ -571,6 +579,20 @@ if ($_POST['action'] == 'get_groups') {
}
}
+ // First try to delete any occurrences of this domain if we're in replace mode
+ if($_POST['action'] == 'replace_domain') {
+ if (!$delstmt->bindValue(':domain', $domain, SQLITE3_TEXT)) {
+ throw new Exception('While binding domain: ' . $db->lastErrorMsg() . '
'.
+ 'Added ' . $added . " out of ". $total . " domains");
+ }
+
+ if (!$delstmt->execute()) {
+ throw new Exception('While executing: ' . $db->lastErrorMsg() . '
'.
+ 'Added ' . $added . " out of ". $total . " domains");
+ }
+ }
+
+ // Add domain with specific type and comment (both were already bound above)
if (!$stmt->bindValue(':domain', $domain, SQLITE3_TEXT)) {
throw new Exception('While binding domain: ' . $db->lastErrorMsg() . '
'.
'Added ' . $added . " out of ". $total . " domains");