Reduce code duplication

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-04-26 17:58:27 +02:00
parent cedc59ba78
commit 8b0ee8f4fa
4 changed files with 29 additions and 99 deletions

View File

@@ -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)
{

View File

@@ -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 ? "&nbsp;-&nbsp;" + entry.comment : "";
var time = new Date(parseInt(entry.date_added)*1000);
var added = "Added: " + time.toLocaleString();
list.append(
"<li id=\"" + index + "\" class=\"list-group-item " + used + " clearfix\">" +
"<span title=\"" + added + "\" data-toggle=\"tooltip\" data-placement=\"right\">" +
entry.domain + comment + "</span>" +
"<button class=\"btn btn-danger btn-xs pull-right\" type=\"button\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>"
);
// 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 ? "&nbsp;-&nbsp;" + 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(
"<li id=\"" + index + "\" class=\"list-group-item " + used + " clearfix\">" +
"<span title=\"" + added + "\" data-toggle=\"tooltip\" data-placement=\"right\">" +
entry.domain + comment + "</span>" +
"<button class=\"btn btn-danger btn-xs pull-right\" type=\"button\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>");
// 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 ? "&nbsp;-&nbsp;" + entry.comment : "";
var time = new Date(parseInt(entry.date_added)*1000);
var added = "Added: " + time.toLocaleString();
// Regex entry
listw.append(
"<li id=\"" + index + "\" class=\"list-group-item " + used + " clearfix\">" +
"<span title=\"" + added + "\" data-toggle=\"tooltip\" data-placement=\"right\">" +
entry.filter + comment + "</span>" +
"<button class=\"btn btn-danger btn-xs pull-right\" type=\"button\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>");
// 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() {

View File

@@ -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;

View File

@@ -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;