From 0e924fd91042aec53d34b2786de82e78a76bade8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 8 Nov 2020 18:51:49 +0100 Subject: [PATCH] Add new replace_domain action in groups.php to allow adding domain(s) exclusively to a specific list. In this mode, any occurrences of said domain are first removed from the list before adding the new ones. Signed-off-by: DL6ER --- scripts/pi-hole/js/db_queries.js | 2 +- scripts/pi-hole/js/queries.js | 2 +- scripts/pi-hole/php/groups.php | 24 +++++++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) 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");