From 506644b67120f985af7d03e5083065a6df686f0d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 25 Apr 2019 15:02:39 +0200 Subject: [PATCH 01/11] Rewrite web interface to allow interaction with database-based lists Signed-off-by: DL6ER --- api_db.php | 2 +- scripts/pi-hole/js/list.js | 178 ++++++++++++++++++------------------ scripts/pi-hole/php/add.php | 28 +++--- scripts/pi-hole/php/get.php | 123 +++++++++++++++++-------- scripts/pi-hole/php/sub.php | 35 ++----- style/pi-hole.css | 17 ++++ 6 files changed, 214 insertions(+), 169 deletions(-) diff --git a/api_db.php b/api_db.php index 7687fb6e..57a52d62 100644 --- a/api_db.php +++ b/api_db.php @@ -48,7 +48,7 @@ function resolveHostname($clientip, $printIP) return $clientname; } -// Get posible non-standard location of FTL's database +// Get possible non-standard location of FTL's database $FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); if(isset($FTLsettings["DBFILE"])) { diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index 530a3b3b..26eaa5c5 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -12,6 +12,96 @@ var token = $("#token").html(); var listType = $("#list-type").html(); var fullName = listType === "white" ? "Whitelist" : "Blacklist"; +function refresh(fade) { + var listw; + var list = $("#list"); + if(listType === "black") + { + listw = $("#list-regex"); + } + if(fade) { + list.fadeOut(100); + if(listw) + { + listw.fadeOut(100); + } + } + $.ajax({ + url: "scripts/pi-hole/php/get.php", + method: "get", + data: {"list":listType}, + success: function(response) { + list.html(""); + if(listw) + { + listw.html(""); + } + + if((listType === "black" && + response.blacklist.length === 0 && + response.regex.length === 0) || + (listType === "white" && + response.whitelist.length === 0)) + { + $("h3").hide(); + list.html("
Your " + fullName + " is empty!
"); + } + else + { + $("h3").show(); + if(listType === "white") + { + data = response.whitelist.sort(); + data2 = []; // No regex data, use empty array + } + else if(listType === "black") + { + data = response.blacklist.sort(); + data2 = response.regex.sort(); + } + data.forEach(function (entry, index) + { + var used = entry.enabled === "1" ? "used" : "not-used"; + // Whitelist entry or Blacklist (exact entry) + list.append( + "
  • " + entry.domain + + "
  • "); + // Handle button + $("#list #"+index+"").on("click", "button", function() { + sub(index, entry.domain, "exact"); + }); + }); + + // Add regex domains if present in returned list data + data2.forEach(function (entry, index) + { + var used = entry.enabled === "1" ? "used" : "not-used"; + // Regex entry + listw.append( + "
  • " + entry.filter + + "
  • "); + // Handle button + $("#list-regex #"+index+"").on("click", "button", function() { + sub(index, entry.filter, "regex"); + }); + }); + } + list.fadeIn(100); + if(listw) + { + listw.fadeIn(100); + } + }, + error: function(jqXHR, exception) { + $("#alFailure").show(); + } + }); +} + +window.onload = refresh(false); + function sub(index, entry, arg) { var domain = $("#list #"+index); var locallistType = listType; @@ -38,92 +128,6 @@ function sub(index, entry, arg) { }); } -function refresh(fade) { - var listw; - var list = $("#list"); - if(listType === "black") - { - listw = $("#list-regex"); - } - if(fade) { - list.fadeOut(100); - if(listw) - { - listw.fadeOut(100); - } - } - $.ajax({ - url: "scripts/pi-hole/php/get.php", - method: "get", - data: {"list":listType}, - success: function(response) { - list.html(""); - if(listw) - { - listw.html(""); - } - var data = JSON.parse(response); - - if(data.length === 0) { - $("h3").hide(); - if(listw) - { - listw.html("
    Your " + fullName + " is empty!
    "); - } - else - { - list.html("
    Your " + fullName + " is empty!
    "); - } - } - else - { - $("h3").show(); - data[0] = data[0].sort(); - data[0].forEach(function (entry, index) { - // Whitelist entry or Blacklist (exact entry) are in the zero-th - // array returned by get.php - list.append( - "
  • " + entry + - "
  • "); - // Handle button - $("#list #"+index+"").on("click", "button", function() { - sub(index, entry, "exact"); - }); - }); - - // Add regex domains if present in returned list data - if(data.length === 2) - { - data[1] = data[1].sort(); - data[1].forEach(function (entry, index) { - // Whitelist entry or Blacklist (exact entry) are in the zero-th - // array returned by get.php - listw.append( - "
  • " + entry + - "
  • "); - // Handle button - $("#list-regex #"+index+"").on("click", "button", function() { - sub(index, entry, "regex"); - }); - }); - } - } - list.fadeIn(100); - if(listw) - { - listw.fadeIn(100); - } - }, - error: function(jqXHR, exception) { - $("#alFailure").show(); - } - }); -} - -window.onload = refresh(false); - function add(arg) { var locallistType = listType; var domain = $("#domain"); @@ -161,7 +165,7 @@ function add(arg) { alInfo.delay(8000).fadeOut(2000, function() { alInfo.hide(); }); - } else if (!wild && response.indexOf("] Pi-hole blocking is ") === -1 || + } else if (!wild && response.indexOf("DONE") === -1 || wild && response.length > 1) { alFailure.show(); err.html(response); diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index 762ea2cf..476b2410 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -22,36 +22,36 @@ if($type !== "regex") { check_domain(); } +// Escape shell metacharacters +$domains = escapeshellcmd($_POST['domain']); + switch($type) { case "white": if(!isset($_POST["auditlog"])) - echo shell_exec("sudo pihole -w ${_POST['domain']}"); + echo shell_exec("sudo pihole -w --web ".$domains); else { - echo shell_exec("sudo pihole -w -n ${_POST['domain']}"); - echo shell_exec("sudo pihole -a audit ${_POST['domain']}"); + echo shell_exec("sudo pihole -w --web -n ".$domains); + echo shell_exec("sudo pihole -a audit ".$domains); } break; case "black": if(!isset($_POST["auditlog"])) - echo shell_exec("sudo pihole -b ${_POST['domain']}"); + echo shell_exec("sudo pihole -b --web ".$domains); else { - echo shell_exec("sudo pihole -b -n ${_POST['domain']}"); - echo shell_exec("sudo pihole -a audit ${_POST['domain']}"); + echo shell_exec("sudo pihole -b --web -n ".$domains); + echo shell_exec("sudo pihole -a audit ".$domains); } break; - case "wild": - // Escape "." so it won't be interpreted as the wildcard character - $domain = str_replace(".","\.",$_POST['domain']); - // Add regex filter for legacy wildcard behavior - add_regex("(^|\.)".$domain."$"); - break; case "regex": - add_regex($_POST['domain']); + echo shell_exec("sudo pihole --regex --web ".$domains); + break; + case "wild": + echo shell_exec("sudo pihole --wild --web ".$domains); break; case "audit": - echo exec("sudo pihole -a audit ${_POST['domain']}"); + echo exec("sudo pihole -a audit ".$domain); break; } diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 156622c7..5a707b49 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -11,57 +11,100 @@ if(!isset($_GET['list'])) $listtype = $_GET['list']; -$basedir = "/etc/pihole/"; +require_once("func.php"); -require_once "func.php"; - -switch ($listtype) { - case "white": - $list = array(getListContent("whitelist.txt")); - break; - - case "black": - $exact = getListContent("blacklist.txt"); - $regex = getListContent("regex.list"); - $list = array($exact, $regex); - break; - - default: - die("Invalid list parameter"); - break; +// Get possible non-standard location of FTL's database +$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); +if(isset($FTLsettings["GRAVITYDB"])) +{ + $GRAVITYDB = $FTLsettings["GRAVITYDB"]; +} +else +{ + $GRAVITYDB = "/etc/pihole/gravity.db"; } +function SQLite3_connect($dbfile, $trytoreconnect) +{ + try + { + // connect to database + return new SQLite3($dbfile, SQLITE3_OPEN_READONLY); + } + catch (Exception $exception) + { + // sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds + if($trytoreconnect) + { + sleep(3); + $db = SQLite3_connect($dbfile, false); + } + } +} -function getListContent($listname) { - global $basedir; - $rawList = file_get_contents(checkfile($basedir.$listname)); - $list = explode("\n", $rawList); +if(strlen($GRAVITYDB) > 0) +{ + $db = SQLite3_connect($GRAVITYDB, true); - // Get rid of empty lines and comments - for($i = sizeof($list)-1; $i >= 0; $i--) { - if(strlen($list[$i]) < 1 || $list[$i][0] === '#') - unset($list[$i]); - } + // Check if we successfully opened the database + if(!$db) + { + die("Error connecting to database"); + } +} +else +{ + die("No database available"); +} - // Re-index list after possible unset() activity - $newlist = array_values($list); +function getTableContent($listname) { + global $db; + $entries = array(); + $results = $db->query("SELECT * FROM $listname"); - return $newlist; + while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC)) + { + array_push($entries, $res); + } + return array($listname => $entries); } function filterArray(&$inArray) { - $outArray = array(); - foreach ($inArray as $key=>$value) { - if (is_array($value)) { - $outArray[htmlspecialchars($key)] = filterArray($value); - } else { - $outArray[htmlspecialchars($key)] = htmlspecialchars($value); - } - } - return $outArray; + $outArray = array(); + foreach ($inArray as $key => $value) + { + if (is_array($value)) + { + $outArray[htmlspecialchars($key)] = filterArray($value); + } + else + { + $outArray[htmlspecialchars($key)] = htmlspecialchars($value); + } + } + return $outArray; } +switch ($listtype) +{ + case "white": + $list = getTableContent("whitelist"); + break; + + case "black": + $exact = getTableContent("blacklist"); + $regex = getTableContent("regex"); + $list = array_merge($exact, $regex); + break; + + default: + die("Invalid list parameter"); + break; +} // Protect against XSS attacks -$list = filterArray($list); -echo json_encode(array_values($list)); +$output = filterArray($list); + +// Return results +header('Content-type: application/json'); +echo json_encode($output); diff --git a/scripts/pi-hole/php/sub.php b/scripts/pi-hole/php/sub.php index 1c9bc8fe..97c8f84b 100644 --- a/scripts/pi-hole/php/sub.php +++ b/scripts/pi-hole/php/sub.php @@ -16,43 +16,24 @@ if (empty($api)) { list_verify($type); } -// Don't check if the added item is a valid domain for regex expressions. Regex -// filters are validated by FTL on import and skipped if invalid +// Don't check if the added item is a valid domain for regex expressions. +// Regex filters are validated by FTL on import and skipped if invalid if($type !== "regex") { check_domain(); } +// Escape shell metacharacters +$domain = escapeshellcmd($_POST['domain']); + switch($type) { case "white": - exec("sudo pihole -w -q -d ${_POST['domain']}"); + exec("sudo pihole -w -q -d ".$domain); break; case "black": - exec("sudo pihole -b -q -d ${_POST['domain']}"); + exec("sudo pihole -b -q -d ".$domain); break; case "regex": - if(($list = file_get_contents($regexfile)) === FALSE) - { - $err = error_get_last()["message"]; - echo "Unable to read ${regexfile}
    Error message: $err"; - } - - // Remove the regex and any empty lines from the list - $list = explode("\n", $list); - $list = array_diff($list, array($_POST['domain'], "")); - $list = implode("\n", $list); - - if(file_put_contents($regexfile, $list."\n") === FALSE) - { - $err = error_get_last()["message"]; - echo "Unable to remove regex \"".htmlspecialchars($_POST['domain'])."\" from ${regexfile}
    Error message: $err"; - } - else - { - // Send SIGHUP to pihole-FTL using a frontend command - // to force reloading of the regex domains - // This will also wipe the resolver's cache - echo exec("sudo pihole restartdns reload"); - } + exec("sudo pihole --regex -q -d ".$domain); break; } diff --git a/style/pi-hole.css b/style/pi-hole.css index eec770c6..311731c5 100644 --- a/style/pi-hole.css +++ b/style/pi-hole.css @@ -24,6 +24,23 @@ .skin-blue .list-group-item:hover { background: #ddd; } + +.skin-blue .not-used { + background: #eee; +} + +.skin-blue .not-used:hover { + background: #c5c5c95; +} + +.skin-blue .used { + background: #fff; +} + +.skin-blue .used:hover { + background: #ddd; +} + @-webkit-keyframes Pulse{ from {color:#630030;-webkit-text-shadow:0 0 2px transparent;} 50% {color:#e33100;-webkit-text-shadow:0 0 5px #e33100;} From d8613aa53b0e4088c3c8c9ac98fd32409bcf08d3 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 25 Apr 2019 15:48:26 +0200 Subject: [PATCH 02/11] Show comments when available Signed-off-by: DL6ER --- scripts/pi-hole/js/list.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index 26eaa5c5..db1c62b3 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -62,9 +62,10 @@ function refresh(fade) { data.forEach(function (entry, index) { var used = entry.enabled === "1" ? "used" : "not-used"; + var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; // Whitelist entry or Blacklist (exact entry) list.append( - "
  • " + entry.domain + + "
  • " + entry.domain + comment + "
  • "); // Handle button @@ -77,9 +78,10 @@ function refresh(fade) { data2.forEach(function (entry, index) { var used = entry.enabled === "1" ? "used" : "not-used"; + var comment = entry.comment.length > 0 ? "(" + entry.comment + ")" : ""; // Regex entry listw.append( - "
  • " + entry.filter + + "
  • " + entry.filter + comment + "
  • "); // Handle button From cedc59ba78757c51cfda4939319db80a3039fc9a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 25 Apr 2019 16:06:24 +0200 Subject: [PATCH 03/11] Show date added Signed-off-by: DL6ER --- scripts/pi-hole/js/list.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index db1c62b3..71ee3532 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -63,9 +63,14 @@ function refresh(fade) { { var used = entry.enabled === "1" ? "used" : "not-used"; var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; + var time = new Date(parseInt(entry.date_added)*1000); + var added = "Added: " + time.toLocaleString(); + console.log(time); // Whitelist entry or Blacklist (exact entry) list.append( - "
  • " + entry.domain + comment + + "
  • " + + "" + + entry.domain + comment + "" + "
  • "); // Handle button @@ -78,10 +83,14 @@ function refresh(fade) { data2.forEach(function (entry, index) { var used = entry.enabled === "1" ? "used" : "not-used"; - var comment = entry.comment.length > 0 ? "(" + entry.comment + ")" : ""; + var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; + var time = new Date(parseInt(entry.date_added)*1000); + var added = "Added: " + time.toLocaleString(); // Regex entry listw.append( - "
  • " + entry.filter + comment + + "
  • " + + "" + + entry.filter + comment + "" + "
  • "); // Handle button From 8b0ee8f4fa35491e69f2b48bb7cacaa5f53a85eb Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 26 Apr 2019 17:58:27 +0200 Subject: [PATCH 04/11] Reduce code duplication Signed-off-by: DL6ER --- api_db.php | 33 ++------------------- scripts/pi-hole/js/list.js | 57 +++++++++++++++---------------------- scripts/pi-hole/php/add.php | 4 +-- scripts/pi-hole/php/get.php | 34 ++-------------------- 4 files changed, 29 insertions(+), 99 deletions(-) diff --git a/api_db.php b/api_db.php index 57a52d62..00c6ba2e 100644 --- a/api_db.php +++ b/api_db.php @@ -8,6 +8,7 @@ $api = true; header('Content-type: application/json'); +require("scripts/pi-hole/php/database.php"); require("scripts/pi-hole/php/password.php"); require("scripts/pi-hole/php/auth.php"); check_cors(); @@ -62,37 +63,7 @@ else // Needs package php5-sqlite, e.g. // sudo apt-get install php5-sqlite -function SQLite3_connect($trytoreconnect) -{ - global $DBFILE; - try - { - // connect to database - return new SQLite3($DBFILE, SQLITE3_OPEN_READONLY); - } - catch (Exception $exception) - { - // sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds - if($trytoreconnect) - { - sleep(3); - $db = SQLite3_connect(false); - } - } -} - -if(strlen($DBFILE) > 0) -{ - $db = SQLite3_connect(true); -} -else -{ - die("No database available"); -} -if(!$db) -{ - die("Error connecting to database"); -} +$db = SQLite3_connect($DBFILE); if(isset($_GET["network"]) && $auth) { diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index 71ee3532..0dd24fb8 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -12,6 +12,25 @@ var token = $("#token").html(); var listType = $("#list-type").html(); var fullName = listType === "white" ? "Whitelist" : "Blacklist"; +function addListEntry(entry, index, list, button, type) +{ + var used = entry.enabled === "1" ? "used" : "not-used"; + var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; + var time = new Date(parseInt(entry.date_added)*1000); + var added = "Added: " + time.toLocaleString(); + list.append( + "
  • " + + "" + + entry.domain + comment + "" + + "
  • " + ); + // Handle button + $(button+" #"+index).on("click", "button", function() { + sub(index, entry.domain, type); + }); +} + function refresh(fade) { var listw; var list = $("#list"); @@ -61,42 +80,13 @@ function refresh(fade) { } data.forEach(function (entry, index) { - var used = entry.enabled === "1" ? "used" : "not-used"; - var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; - var time = new Date(parseInt(entry.date_added)*1000); - var added = "Added: " + time.toLocaleString(); - console.log(time); - // Whitelist entry or Blacklist (exact entry) - list.append( - "
  • " + - "" + - entry.domain + comment + "" + - "
  • "); - // Handle button - $("#list #"+index+"").on("click", "button", function() { - sub(index, entry.domain, "exact"); - }); + addListEntry(entry, index, list, "#list", "exact"); }); // Add regex domains if present in returned list data data2.forEach(function (entry, index) { - var used = entry.enabled === "1" ? "used" : "not-used"; - var comment = entry.comment.length > 0 ? " - " + entry.comment : ""; - var time = new Date(parseInt(entry.date_added)*1000); - var added = "Added: " + time.toLocaleString(); - // Regex entry - listw.append( - "
  • " + - "" + - entry.filter + comment + "" + - "
  • "); - // Handle button - $("#list-regex #"+index+"").on("click", "button", function() { - sub(index, entry.filter, "regex"); - }); + addListEntry(entry, index, listw, "#list-regex", "regex"); }); } list.fadeIn(100); @@ -167,7 +157,7 @@ function add(arg) { method: "post", data: {"domain":domain.val().trim(), "list":locallistType, "token":token}, success: function(response) { - if (!wild && response.indexOf(" already exists in ") !== -1) { + if (response.indexOf(" already exists in ") !== -1) { alWarning.show(); warn.html(response); alWarning.delay(8000).fadeOut(2000, function() { @@ -176,8 +166,7 @@ function add(arg) { alInfo.delay(8000).fadeOut(2000, function() { alInfo.hide(); }); - } else if (!wild && response.indexOf("DONE") === -1 || - wild && response.length > 1) { + } else if (response.indexOf("DONE") === -1) { alFailure.show(); err.html(response); alFailure.delay(8000).fadeOut(2000, function() { diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index 476b2410..dbb9df2e 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -31,7 +31,7 @@ switch($type) { echo shell_exec("sudo pihole -w --web ".$domains); else { - echo shell_exec("sudo pihole -w --web -n ".$domains); + echo shell_exec("sudo pihole -w --web ".$domains); echo shell_exec("sudo pihole -a audit ".$domains); } break; @@ -40,7 +40,7 @@ switch($type) { echo shell_exec("sudo pihole -b --web ".$domains); else { - echo shell_exec("sudo pihole -b --web -n ".$domains); + echo shell_exec("sudo pihole -b --web ".$domains); echo shell_exec("sudo pihole -a audit ".$domains); } break; diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 5a707b49..cbf77bae 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -24,38 +24,8 @@ else $GRAVITYDB = "/etc/pihole/gravity.db"; } -function SQLite3_connect($dbfile, $trytoreconnect) -{ - try - { - // connect to database - return new SQLite3($dbfile, SQLITE3_OPEN_READONLY); - } - catch (Exception $exception) - { - // sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds - if($trytoreconnect) - { - sleep(3); - $db = SQLite3_connect($dbfile, false); - } - } -} - -if(strlen($GRAVITYDB) > 0) -{ - $db = SQLite3_connect($GRAVITYDB, true); - - // Check if we successfully opened the database - if(!$db) - { - die("Error connecting to database"); - } -} -else -{ - die("No database available"); -} +require("database.php"); +$db = SQLite3_connect($GRAVITYDB); function getTableContent($listname) { global $db; From 406a946b24202517a7b2988f914fb1d4a39b1641 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 26 Apr 2019 18:32:33 +0200 Subject: [PATCH 05/11] Add new file scripts/pi-hole/php/database.php Signed-off-by: DL6ER --- scripts/pi-hole/php/database.php | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 scripts/pi-hole/php/database.php diff --git a/scripts/pi-hole/php/database.php b/scripts/pi-hole/php/database.php new file mode 100644 index 00000000..72d06322 --- /dev/null +++ b/scripts/pi-hole/php/database.php @@ -0,0 +1,42 @@ + 0) + { + $db = SQLite3_connect_try($filename, $mode, true); + } + else + { + die("No database available"); + } + if(!$db) + { + die("Error connecting to database"); + } + return $db; +} +?> From ced7174c37ce3e420d34a46277796b462dd50813 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 28 Apr 2019 20:42:50 +0200 Subject: [PATCH 06/11] Modify adlists subpage of the settings page to sources the lists from the gravity database. Signed-off-by: DL6ER --- scripts/pi-hole/php/database.php | 17 +++++++++++- scripts/pi-hole/php/get.php | 12 +------- scripts/pi-hole/php/savesettings.php | 41 +++++++++------------------- settings.php | 4 +-- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/scripts/pi-hole/php/database.php b/scripts/pi-hole/php/database.php index 72d06322..3a453677 100644 --- a/scripts/pi-hole/php/database.php +++ b/scripts/pi-hole/php/database.php @@ -6,8 +6,23 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license */ +function getGravityDBFilename() +{ + // Get possible non-standard location of FTL's database + $FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); + if(isset($FTLsettings["GRAVITYDB"])) + { + return $FTLsettings["GRAVITYDB"]; + } + else + { + return "/etc/pihole/gravity.db"; + } +} + function SQLite3_connect_try($filename, $mode, $trytoreconnect) -{ try +{ + try { // connect to database return new SQLite3($filename, $mode); diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index cbf77bae..d2b75b95 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -13,18 +13,8 @@ $listtype = $_GET['list']; require_once("func.php"); -// Get possible non-standard location of FTL's database -$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); -if(isset($FTLsettings["GRAVITYDB"])) -{ - $GRAVITYDB = $FTLsettings["GRAVITYDB"]; -} -else -{ - $GRAVITYDB = "/etc/pihole/gravity.db"; -} - require("database.php"); +$GRAVITYDB = getGravityDBFilename(); $db = SQLite3_connect($GRAVITYDB); function getTableContent($listname) { diff --git a/scripts/pi-hole/php/savesettings.php b/scripts/pi-hole/php/savesettings.php index fc358d66..ed7f2556 100644 --- a/scripts/pi-hole/php/savesettings.php +++ b/scripts/pi-hole/php/savesettings.php @@ -155,37 +155,22 @@ function readDNSserversList() return $list; } +require_once("database.php"); $adlist = []; function readAdlists() { // Reset list $list = []; - $handle = @fopen("/etc/pihole/adlists.list", "r"); - if ($handle) + $db = SQLite3_connect(getGravityDBFilename()); + if ($db) { - while (($line = fgets($handle)) !== false) + $results = $db->query("SELECT * FROM adlists"); + + while($results !== false && $res = $results->fetchArray(SQLITE3_ASSOC)) { - if(strlen($line) < 3) - { - continue; - } - elseif($line[0] === "#") - { - // Comments start either with "##" or "# " - if($line[1] !== "#" && - $line[1] !== " ") - { - // Commented list - array_push($list, [false,rtrim(substr($line, 1))]); - } - } - else - { - // Active list - array_push($list, [true,rtrim($line)]); - } + array_push($list, $res); } - fclose($handle); + $db->close(); } return $list; } @@ -689,18 +674,18 @@ function readAdlists() if(isset($_POST["adlist-del-".$key])) { // Delete list - exec("sudo pihole -a adlist del ".escapeshellcmd($value[1])); + exec("sudo pihole -a adlist del ".escapeshellcmd($value["address"])); } - elseif(isset($_POST["adlist-enable-".$key]) && !$value[0]) + elseif(isset($_POST["adlist-enable-".$key]) && $value["enabled"] !== 1) { // Is not enabled, but should be - exec("sudo pihole -a adlist enable ".escapeshellcmd($value[1])); + exec("sudo pihole -a adlist enable ".escapeshellcmd($value["address"])); } - elseif(!isset($_POST["adlist-enable-".$key]) && $value[0]) + elseif(!isset($_POST["adlist-enable-".$key]) && $value["enabled"] === 1) { // Is enabled, but shouldn't be - exec("sudo pihole -a adlist disable ".escapeshellcmd($value[1])); + exec("sudo pihole -a adlist disable ".escapeshellcmd($value["address"])); } } diff --git a/settings.php b/settings.php index 3a4f8ffe..44133542 100644 --- a/settings.php +++ b/settings.php @@ -266,10 +266,10 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array("sysadmin", "blocklists" $value) { ?> - checked> + checked> - + " target="_new" id="adlist-text-"> "