From 8b0ee8f4fa35491e69f2b48bb7cacaa5f53a85eb Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 26 Apr 2019 17:58:27 +0200 Subject: [PATCH] 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;