From 5f28a3816d724c53828f7ccd703c03c0ab9ded26 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 16 Jun 2019 20:08:24 +0200 Subject: [PATCH 1/5] Pass group_enabled boolean to web interface and show if entry was disabled due to group setting. Although the current web interface does not support group-based management, this may aid in debugging if users interact the database directly. Signed-off-by: DL6ER --- scripts/pi-hole/js/list.js | 5 +++-- scripts/pi-hole/php/get.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index b21ba955..bd6b4247 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -14,7 +14,8 @@ var fullName = listType === "white" ? "Whitelist" : "Blacklist"; function addListEntry(entry, index, list, button, type) { - var used = entry.enabled === "1" ? "used" : "not-used"; + var used = (entry.enabled === "1" && entry.group_enabled === "1") ? "used" : "not-used"; + var groupmessage = entry.group_enabled === "1" ? "" : "(disabled due to group)"; var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; var date_added = new Date(parseInt(entry.date_added)*1000); var date_modified = new Date(parseInt(entry.date_modified)*1000); @@ -23,7 +24,7 @@ function addListEntry(entry, index, list, button, type) list.append( "
  • " + "" + - entry.domain + comment + "" + + entry.domain + comment + " " + groupmessage + "" + "
  • " ); diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index d2b75b95..5260dd44 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -20,7 +20,7 @@ $db = SQLite3_connect($GRAVITYDB); function getTableContent($listname) { global $db; $entries = array(); - $results = $db->query("SELECT * FROM $listname"); + $results = $db->query("SELECT a.*,b.enabled AS group_enabled FROM $listname a INNER JOIN domain_groups b ON b.id = a.group_id"); while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC)) { From 4cc54ee407a3d1a8fbd207715bf9117877c68865 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 5 Jul 2019 11:24:22 +0200 Subject: [PATCH 2/5] Group assignment has changed in the database. Adapt the code accordignly. Signed-off-by: DL6ER --- scripts/pi-hole/js/list.js | 14 +++++++++++--- scripts/pi-hole/php/get.php | 8 ++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index bd6b4247..53d6b7e5 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -14,9 +14,17 @@ var fullName = listType === "white" ? "Whitelist" : "Blacklist"; function addListEntry(entry, index, list, button, type) { - var used = (entry.enabled === "1" && entry.group_enabled === "1") ? "used" : "not-used"; - var groupmessage = entry.group_enabled === "1" ? "" : "(disabled due to group)"; + var disabled = []; + if(entry.enabled === "0") + disabled.push("individual"); + // For entry.group_enabled we either get "0" (= disabled by a group), + // "1" (= enabled by a group), or "" (= not managed by a group) + if(entry.group_enabled === "0") + disabled.push("group"); + + var used = disabled.length === 0 ? "used" : "not-used"; var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; + var disabled_message = disabled.length > 0 ? " - disabled due to " + disabled.join(" + ") + " setting" : ""; var date_added = new Date(parseInt(entry.date_added)*1000); var date_modified = new Date(parseInt(entry.date_modified)*1000); var tooltip = "Added: " + date_added.toLocaleString() + @@ -24,7 +32,7 @@ function addListEntry(entry, index, list, button, type) list.append( "
  • " + "" + - entry.domain + comment + " " + groupmessage + "" + + entry.domain + comment + disabled_message + "" + "
  • " ); diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 5260dd44..3941938d 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -20,14 +20,18 @@ $db = SQLite3_connect($GRAVITYDB); function getTableContent($listname) { global $db; $entries = array(); - $results = $db->query("SELECT a.*,b.enabled AS group_enabled FROM $listname a INNER JOIN domain_groups b ON b.id = a.group_id"); + $querystr = implode(" ",array("SELECT DISTINCT ${listname}.*,\"group\".enabled as group_enabled", + "FROM $listname", + "LEFT JOIN ${listname}_by_group ON ${listname}_by_group.whitelist_id = ${listname}.id", + "LEFT JOIN \"group\" ON \"group\".id = ${listname}_by_group.group_id;")); + $results = $db->query($querystr); while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC)) { array_push($entries, $res); } - return array($listname => $entries); + return array($listname => $entries, "querystring" => $querystr); } function filterArray(&$inArray) { From e3f76a46d2dd3b72da169a9ebc3de6c73964a629 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 5 Jul 2019 11:49:44 +0200 Subject: [PATCH 3/5] Use GROUP BY instead of DISTINCT as we pairs or(domain,1) and (domain,0) are distinct as well where we actually want to only GROUP BY domain. Signed-off-by: DL6ER --- scripts/pi-hole/php/get.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 3941938d..ac6a5e65 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -20,10 +20,11 @@ $db = SQLite3_connect($GRAVITYDB); function getTableContent($listname) { global $db; $entries = array(); - $querystr = implode(" ",array("SELECT DISTINCT ${listname}.*,\"group\".enabled as group_enabled", + $querystr = implode(" ",array("SELECT ${listname}.*,\"group\".enabled as group_enabled", "FROM $listname", "LEFT JOIN ${listname}_by_group ON ${listname}_by_group.whitelist_id = ${listname}.id", - "LEFT JOIN \"group\" ON \"group\".id = ${listname}_by_group.group_id;")); + "LEFT JOIN \"group\" ON \"group\".id = ${listname}_by_group.group_id", + "GROUP BY domain;")); $results = $db->query($querystr); while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC)) From 8698ce1b9cf9ed939b86c6fc23d5a23838c8aa2e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 7 Jul 2019 10:19:20 +0200 Subject: [PATCH 4/5] Do not return query string in get.php. Signed-off-by: DL6ER --- scripts/pi-hole/php/get.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index ac6a5e65..57a4bf70 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -32,7 +32,7 @@ function getTableContent($listname) { array_push($entries, $res); } - return array($listname => $entries, "querystring" => $querystr); + return array($listname => $entries); } function filterArray(&$inArray) { From eeb459892569d6265bc48369d1761bb184834953 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 7 Jul 2019 22:40:34 +0200 Subject: [PATCH 5/5] Fix hardcoded table name. Signed-off-by: DL6ER --- scripts/pi-hole/php/get.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 57a4bf70..c84b991a 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -22,7 +22,7 @@ function getTableContent($listname) { $entries = array(); $querystr = implode(" ",array("SELECT ${listname}.*,\"group\".enabled as group_enabled", "FROM $listname", - "LEFT JOIN ${listname}_by_group ON ${listname}_by_group.whitelist_id = ${listname}.id", + "LEFT JOIN ${listname}_by_group ON ${listname}_by_group.${listname}_id = ${listname}.id", "LEFT JOIN \"group\" ON \"group\".id = ${listname}_by_group.group_id", "GROUP BY domain;")); $results = $db->query($querystr);