diff --git a/groups-adlists.php b/groups-adlists.php new file mode 100644 index 00000000..add8df83 --- /dev/null +++ b/groups-adlists.php @@ -0,0 +1,95 @@ + + + +
+
+
| Address | +Status | +Comment | +Group assignment | +Action | +
|---|
'+data["address"]+'' );
+
+ const disabled = data["enabled"] === 0;
+ $('td:eq(1)', row).html( '' );
+
+ $('td:eq(2)', row).html( '' );
+ $('#comment', row).val(data["comment"]);
+
+ $('td:eq(3)', row).empty();
+ $('td:eq(3)', row).append( '' );
+ var sel = $('#multiselect', row);
+ // Add all known groups
+ for (var i = 0; i < groups.length; i++) {
+ var extra = 'ID ' + groups[i].id;
+ if(!groups[i].enabled)
+ {
+ extra += ', disabled';
+ }
+ sel.append($('').val(groups[i].id).text(groups[i].name + ' (' + extra + ')'));
+ sel.redraw();
+ }
+ // Select assigned groups
+ sel.val(data.groups);
+ // Initialize multiselect
+ sel.multiselect({ includeSelectAllOption: true });
+
+ let button = "" +
+ " " +
+ "";
+ $('td:eq(4)', row).html( button );
+ },
+ "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
+ "stateSave": true,
+ stateSaveCallback: function(settings, data) {
+ // Store current state in client's local storage area
+ localStorage.setItem("groups-adlists-table", JSON.stringify(data));
+ },
+ stateLoadCallback: function(settings) {
+ // Receive previous state from client's local storage area
+ var data = localStorage.getItem("groups-adlists-table");
+ // Return if not available
+ if(data === null){ return null; }
+ data = JSON.parse(data);
+ // Always start on the first page to show most recent queries
+ data["start"] = 0;
+ // Always start with empty search field
+ data["search"]["search"] = "";
+ // Apply loaded state to table
+ return data;
+ }
+ });
+});
+
+function addAdlist()
+{
+ var address = $("#address").val();
+ var comment = $("#comment").val();
+
+ showAlert('info');
+ $.ajax({
+ url: "scripts/pi-hole/php/groups.php",
+ method: "post",
+ dataType: 'json',
+ data: {"action": "add_adlist", "address": address, "comment": comment},
+ success: function(response) {
+ if (response.success) {
+ showAlert('success');
+ $("#address").empty();
+ $("#comment").empty();
+ table.ajax.reload();
+ }
+ else
+ showAlert('error', response.message);
+ },
+ error: function(jqXHR, exception) {
+ showAlert('error', "Error while adding new adlist");
+ console.log(exception);
+ }
+ });
+}
+
+function editAdlist()
+{
+ var tr = $(this).closest("tr");
+ var id = tr.find("#id").val();
+ var status = tr.find("#status").val();
+ var comment = tr.find("#comment").val();
+ var groups = tr.find("#multiselect").val();
+
+ showAlert('info');
+ $.ajax({
+ url: "scripts/pi-hole/php/groups.php",
+ method: "post",
+ dataType: 'json',
+ data: {"action": "edit_adlist", "id": id, "comment": comment, "status": status, "groups": groups},
+ success: function(response) {
+ if (response.success) {
+ showAlert('success');
+ table.ajax.reload();
+ }
+ else
+ showAlert('error', response.message);
+ },
+ error: function(jqXHR, exception) {
+ showAlert('error', "Error while editing adlist with ID "+id);
+ console.log(exception);
+ }
+ });
+}
+
+function deleteAdlist()
+{
+ var id = $(this).attr("data-id");
+
+ showAlert('info');
+ $.ajax({
+ url: "scripts/pi-hole/php/groups.php",
+ method: "post",
+ dataType: 'json',
+ data: {"action": "delete_adlist", "id": id},
+ success: function(response) {
+ if (response.success) {
+ showAlert('success');
+ table.ajax.reload();
+ }
+ else
+ showAlert('error', response.message);
+ },
+ error: function(jqXHR, exception) {
+ showAlert('error', "Error while deleting adlist with ID "+id);
+ console.log(exception);
+ }
+ });
+}
diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js
index 174f4cac..979b94f5 100644
--- a/scripts/pi-hole/js/groups-clients.js
+++ b/scripts/pi-hole/js/groups-clients.js
@@ -149,7 +149,7 @@ function addClient()
showAlert('error', response.message);
},
error: function(jqXHR, exception) {
- showAlert('error', "Error while adding new group");
+ showAlert('error', "Error while adding new client");
console.log(exception);
}
});
diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js
index 082af2dc..5e53a914 100644
--- a/scripts/pi-hole/js/groups-domains.js
+++ b/scripts/pi-hole/js/groups-domains.js
@@ -152,7 +152,7 @@ function addDomain()
showAlert('error', response.message);
},
error: function(jqXHR, exception) {
- showAlert('error', "Error while adding new group");
+ showAlert('error', "Error while adding new domain");
console.log(exception);
}
});
diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php
index 33d93a20..2a4edbe4 100644
--- a/scripts/pi-hole/php/groups.php
+++ b/scripts/pi-hole/php/groups.php
@@ -617,6 +617,215 @@ elseif($_REQUEST['action'] == "delete_domain")
return JSON_error($ex->getMessage());
}
}
+elseif($_REQUEST['action'] == "get_adlists")
+{
+ // List all available groups
+ try
+ {
+ $query = $db->query("SELECT * FROM adlist;");
+ if(!$query)
+ {
+ throw new Exception("Error while querying gravity's adlist table: ".$db->lastErrorMsg());
+ }
+
+ $data = array();
+ while($res = $query->fetchArray(SQLITE3_ASSOC))
+ {
+ $group_query = $db->query("SELECT group_id FROM adlist_by_group WHERE adlist_id = ".$res["id"].";");
+ if(!$group_query)
+ {
+ throw new Exception("Error while querying gravity's adlist_by_group table: ".$db->lastErrorMsg());
+ }
+
+ $groups = array();
+ while($gres = $group_query->fetchArray(SQLITE3_ASSOC))
+ {
+ array_push($groups,$gres["group_id"]);
+ }
+ $res["groups"] = $groups;
+ array_push($data,$res);
+ }
+
+
+ echo json_encode(array("data" => $data));
+ }
+ catch (\Exception $ex)
+ {
+ return JSON_error($ex->getMessage());
+ }
+}
+elseif($_REQUEST['action'] == "add_adlist")
+{
+ // Add new adlist
+ try
+ {
+ $stmt = $db->prepare('INSERT INTO adlist (address,comment) VALUES (:address,:comment)');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':address', $_POST["address"], SQLITE3_TEXT))
+ {
+ throw new Exception("While binding address: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':comment', $_POST["comment"], SQLITE3_TEXT))
+ {
+ throw new Exception("While binding comment: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing: ".$db->lastErrorMsg());
+ }
+
+ $reload = true;
+ return JSON_success();
+ }
+ catch (\Exception $ex)
+ {
+ return JSON_error($ex->getMessage());
+ }
+}
+elseif($_REQUEST['action'] == "edit_adlist")
+{
+ // Edit adlist identified by ID
+ try
+ {
+ $stmt = $db->prepare('UPDATE adlist SET enabled=:enabled, comment=:comment WHERE id = :id');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing statement: ".$db->lastErrorMsg());
+ }
+
+ $status = intval($_POST["status"]);
+ if($status !== 0)
+ {
+ $status = 1;
+ }
+
+ if(!$stmt->bindValue(':enabled', $status, SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding enabled: ".$db->lastErrorMsg());
+ }
+
+ $comment = $_POST["comment"];
+ if(strlen($comment) == 0)
+ {
+ // Store NULL in database for empty comments
+ $comment = null;
+ }
+ if(!$stmt->bindValue(':comment', $comment, SQLITE3_TEXT))
+ {
+ throw new Exception("While binding comment: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding id: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing: ".$db->lastErrorMsg());
+ }
+
+ $stmt = $db->prepare('DELETE FROM adlist_by_group WHERE adlist_id = :id');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing DELETE statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding id: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing DELETE statement: ".$db->lastErrorMsg());
+ }
+
+ $db->query("BEGIN TRANSACTION;");
+ foreach ($_POST["groups"] as $gid)
+ {
+ $stmt = $db->prepare('INSERT INTO adlist_by_group (adlist_id,group_id) VALUES(:id,:gid);');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing INSERT INTO statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding id: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':gid', intval($gid), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding gid: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing INSERT INTO statement: ".$db->lastErrorMsg());
+ }
+ }
+ $db->query("COMMIT;");
+
+ $reload = true;
+ return JSON_success();
+ }
+ catch (\Exception $ex)
+ {
+ return JSON_error($ex->getMessage());
+ }
+}
+elseif($_REQUEST['action'] == "delete_adlist")
+{
+ // Delete adlist identified by ID
+ try
+ {
+ $stmt = $db->prepare('DELETE FROM adlist_by_group WHERE adlist_id=:id');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing adlist_by_group statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding id to adlist_by_group statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing adlist_by_group statement: ".$db->lastErrorMsg());
+ }
+
+ $stmt = $db->prepare('DELETE FROM adlist WHERE id=:id');
+ if(!$stmt)
+ {
+ throw new Exception("While preparing adlist statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->bindValue(':id', intval($_POST["id"]), SQLITE3_INTEGER))
+ {
+ throw new Exception("While binding id to adlist statement: ".$db->lastErrorMsg());
+ }
+
+ if(!$stmt->execute())
+ {
+ throw new Exception("While executing adlist statement: ".$db->lastErrorMsg());
+ }
+
+ $reload = true;
+ return JSON_success();
+ }
+ catch (\Exception $ex)
+ {
+ return JSON_error($ex->getMessage());
+ }
+}
else
{
log_and_die("Requested action not supported!");
diff --git a/scripts/pi-hole/php/header.php b/scripts/pi-hole/php/header.php
index 0fcde86e..a429cd3f 100644
--- a/scripts/pi-hole/php/header.php
+++ b/scripts/pi-hole/php/header.php
@@ -219,7 +219,7 @@
-
+
@@ -492,7 +492,7 @@ if($auth) {
-