Merge branch 'release/v5.0' into devel

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-04-08 12:19:09 +02:00
45 changed files with 1476 additions and 1416 deletions

View File

@@ -5,71 +5,13 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
/* global moment:false */
/* global utils:false */
var table;
var groups = [];
var token = $("#token").text();
var info = null;
function showAlert(type, icon, title, message) {
var opts = {};
title = "&nbsp;<strong>" + title + "</strong><br>";
switch (type) {
case "info":
opts = {
type: "info",
icon: "far fa-clock",
title: title,
message: message
};
info = $.notify(opts);
break;
case "success":
opts = {
type: "success",
icon: icon,
title: title,
message: message
};
if (info) {
info.update(opts);
} else {
$.notify(opts);
}
break;
case "warning":
opts = {
type: "warning",
icon: "fas fa-exclamation-triangle",
title: title,
message: message
};
if (info) {
info.update(opts);
} else {
$.notify(opts);
}
break;
case "error":
opts = {
type: "danger",
icon: "fas fa-times",
title: "&nbsp;<strong>Error, something went wrong!</strong><br>",
message: message
};
if (info) {
info.update(opts);
} else {
$.notify(opts);
}
break;
default:
}
}
var token = $("#token").html();
var GETDict = {};
var showtype = "all";
function get_groups() {
$.post(
@@ -83,18 +25,32 @@ function get_groups() {
);
}
function datetime(date) {
return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z");
}
$(document).ready(function() {
$("#btnAdd").on("click", addDomain);
window.location.search
.substr(1)
.split("&")
.forEach(function(item) {
GETDict[item.split("=")[0]] = item.split("=")[1];
});
get_groups();
if ("type" in GETDict && (GETDict.type === "white" || GETDict.type === "black")) {
showtype = GETDict.type;
}
$("#select").on("change", function() {
$("#ip-custom").val("");
$("#ip-custom").prop("disabled", $("#select option:selected").val() !== "custom");
// sync description fields, reset inactive inputs on tab change
$('a[data-toggle="tab"]').on("shown.bs.tab", function() {
var tabHref = $(this).attr("href");
var val;
if (tabHref === "#tab_domain") {
val = $("#new_regex_comment").val();
$("#new_domain_comment").val(val);
$("#new_regex").val("");
} else if (tabHref === "#tab_regex") {
val = $("#new_domain_comment").val();
$("#new_regex_comment").val(val);
$("#new_domain").val("");
$("#wildcard_checkbox").prop("checked", false);
}
});
// Disable autocorrect in the search box
var input = document.querySelector("input[type=search]");
@@ -102,13 +58,17 @@ $(document).ready(function() {
input.setAttribute("autocorrect", "off");
input.setAttribute("autocapitalize", "off");
input.setAttribute("spellcheck", false);
$("#add2black, #add2white").on("click", addDomain);
get_groups();
});
function initTable() {
table = $("#domainsTable").DataTable({
ajax: {
url: "scripts/pi-hole/php/groups.php",
data: { action: "get_domains", token: token },
data: { action: "get_domains", showtype: showtype, token: token },
type: "POST"
},
order: [[0, "asc"]],
@@ -122,90 +82,163 @@ function initTable() {
{ data: null, width: "80px", orderable: false }
],
drawCallback: function() {
$(".deleteDomain").on("click", deleteDomain);
$('button[id^="deleteDomain_"]').on("click", deleteDomain);
},
rowCallback: function(row, data) {
$(row).attr("data-id", data.id);
var tooltip =
"Added: " +
datetime(data.date_added) +
utils.datetime(data.date_added) +
"\nLast modified: " +
datetime(data.date_modified) +
utils.datetime(data.date_modified) +
"\nDatabase ID: " +
data.id;
$("td:eq(0)", row).html(
'<code id="domain" title="' + tooltip + '">' + data.domain + "</code>"
'<code id="domain_' +
data.id +
'" title="' +
tooltip +
'" class="breakall">' +
data.domain +
"</code>"
);
$("td:eq(1)", row).html(
'<select id="type" class="form-control">' +
var whitelist_options = "";
if (showtype === "all" || showtype === "white") {
whitelist_options =
'<option value="0"' +
(data.type === 0 ? " selected" : "") +
">Exact whitelist</option>" +
'<option value="1"' +
(data.type === 1 ? " selected" : "") +
">Exact blacklist</option>" +
'<option value="2"' +
(data.type === 2 ? " selected" : "") +
">Regex whitelist</option>" +
">Regex whitelist</option>";
}
var blacklist_options = "";
if (showtype === "all" || showtype === "black") {
blacklist_options =
'<option value="1"' +
(data.type === 1 ? " selected " : " ") +
">Exact blacklist</option>" +
'<option value="3"' +
(data.type === 3 ? " selected" : "") +
">Regex blacklist</option>" +
">Regex blacklist</option>";
}
$("td:eq(1)", row).html(
'<select id="type_' +
data.id +
'" class="form-control">' +
whitelist_options +
blacklist_options +
"</select>"
);
$("#type", row).on("change", editDomain);
var typeEl = $("#type_" + data.id, row);
typeEl.on("change", editDomain);
var disabled = data.enabled === 0;
$("td:eq(2)", row).html(
'<input type="checkbox" id="status"' + (disabled ? "" : " checked") + ">"
'<input type="checkbox" id="status_' + data.id + '"' + (disabled ? "" : " checked") + ">"
);
$("#status", row).bootstrapToggle({
var statusEl = $("#status_" + data.id, row);
statusEl.bootstrapToggle({
on: "Enabled",
off: "Disabled",
size: "small",
onstyle: "success",
width: "80px"
});
$("#status", row).on("change", editDomain);
statusEl.on("change", editDomain);
$("td:eq(3)", row).html(
'<input id="comment" class="form-control"><input id="id" type="hidden" value="' +
data.id +
'">'
);
$("#comment", row).val(data.comment);
$("#comment", row).on("change", editDomain);
$("td:eq(3)", row).html('<input id="comment_' + data.id + '" class="form-control">');
var commentEl = $("#comment_" + data.id, row);
commentEl.val(data.comment);
commentEl.on("change", editDomain);
$("td:eq(4)", row).empty();
$("td:eq(4)", row).append('<select id="multiselect" multiple="multiple"></select>');
var sel = $("#multiselect", row);
// Add all known groups
for (var i = 0; i < groups.length; i++) {
var extra = "";
if (!groups[i].enabled) {
extra = " (disabled)";
// Show group assignment field only if in full domain management mode
if (table.column(5).visible()) {
$("td:eq(4)", row).empty();
$("td:eq(4)", row).append(
'<div id="selectHome_' +
data.id +
'">' +
'<select id="multiselect_' +
data.id +
'" multiple="multiple"></select></div>'
);
var selectEl = $("#multiselect_" + data.id, row);
// Add all known groups
for (var i = 0; i < groups.length; i++) {
var extra = "";
if (!groups[i].enabled) {
extra = " (disabled)";
}
selectEl.append(
$("<option />")
.val(groups[i].id)
.text(groups[i].name + extra)
);
}
sel.append(
$("<option />")
.val(groups[i].id)
.text(groups[i].name + extra)
);
// Select assigned groups
selectEl.val(data.groups);
// Initialize multiselect
selectEl.multiselect({
includeSelectAllOption: true,
buttonContainer: '<div id="container_' + data.id + '" class="btn-group"/>',
maxHeight: 200,
onDropdownShown: function() {
var el = $("#container_" + data.id);
var top = el[0].getBoundingClientRect().top;
var bottom = $(window).height() - top - el.height();
if (bottom < 200) {
el.addClass("dropup");
}
if (bottom > 200) {
el.removeClass("dropup");
}
var offset = el.offset();
$("body").append(el);
el.css("position", "absolute");
el.css("top", offset.top + "px");
el.css("left", offset.left + "px");
},
onDropdownHide: function() {
var el = $("#container_" + data.id);
var home = $("#selectHome_" + data.id);
home.append(el);
el.removeAttr("style");
}
});
selectEl.on("change", editDomain);
}
// Select assigned groups
sel.val(data.groups);
// Initialize multiselect
sel.multiselect({ includeSelectAllOption: true });
sel.on("change", editDomain);
// Highlight row (if url parameter "domainid=" is used)
if ("domainid" in GETDict && data.id === parseInt(GETDict.domainid)) {
$(row)
.find("td")
.addClass("highlight");
}
var button =
'<button class="btn btn-danger btn-xs deleteDomain" type="button" data-id="' +
'<button class="btn btn-danger btn-xs" type="button" id="deleteDomain_' +
data.id +
'">' +
'<span class="far fa-trash-alt"></span>' +
"</button>";
$("td:eq(5)", row).html(button);
if (table.column(5).visible()) {
$("td:eq(5)", row).html(button);
} else {
$("td:eq(4)", row).html(button);
}
},
dom:
"<'row'<'col-sm-4'l><'col-sm-8'f>>" +
"<'row'<'col-sm-12'<'table-responsive'tr>>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>",
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "All"]
@@ -230,8 +263,22 @@ function initTable() {
data.search.search = "";
// Reset visibility of ID column
data.columns[0].visible = false;
// Show group assignment column only on full page
data.columns[5].visible = showtype === "all";
// Apply loaded state to table
return data;
},
initComplete: function() {
if ("domainid" in GETDict) {
var pos = table
.column(0, { order: "current" })
.data()
.indexOf(parseInt(GETDict.domainid));
if (pos >= 0) {
var page = Math.floor(pos / table.page.info().length);
table.page(page).draw(false);
}
}
}
});
@@ -250,14 +297,53 @@ function initTable() {
}
function addDomain() {
var domain = $("#new_domain").val();
var type = $("#new_type").val();
var comment = $("#new_comment").val();
var action = this.id;
var tabHref = $('a[data-toggle="tab"][aria-expanded="true"]').attr("href");
var wildcardEl = $("#wildcard_checkbox");
var wildcard_checked = wildcardEl.prop("checked");
var type;
showAlert("info", "", "Adding domain...", domain);
// current tab's inputs
var domain_regex, domainEl, commentEl;
if (tabHref === "#tab_domain") {
domain_regex = "domain";
domainEl = $("#new_domain");
commentEl = $("#new_domain_comment");
} else if (tabHref === "#tab_regex") {
domain_regex = "regex";
domainEl = $("#new_regex");
commentEl = $("#new_regex_comment");
}
if (domain.length === 0) {
showAlert("warning", "", "Warning", "Please specify a domain");
var domain = domainEl.val();
var comment = commentEl.val();
utils.disableAll();
utils.showAlert("info", "", "Adding " + domain_regex + "...", domain);
if (domain.length > 0) {
// strip "*." if specified by user in wildcard mode
if (domain_regex === "domain" && wildcard_checked && domain.startsWith("*.")) {
domain = domain.substr(2);
}
// determine list type
if (domain_regex === "domain" && action === "add2black" && wildcard_checked) {
type = "3W";
} else if (domain_regex === "domain" && action === "add2black" && !wildcard_checked) {
type = "1";
} else if (domain_regex === "domain" && action === "add2white" && wildcard_checked) {
type = "2W";
} else if (domain_regex === "domain" && action === "add2white" && !wildcard_checked) {
type = "0";
} else if (domain_regex === "regex" && action === "add2black") {
type = "3";
} else if (domain_regex === "regex" && action === "add2white") {
type = "2";
}
} else {
utils.enableAll();
utils.showAlert("warning", "", "Warning", "Please specify a " + domain_regex);
return;
}
@@ -273,15 +359,20 @@ function addDomain() {
token: token
},
success: function(response) {
utils.enableAll();
if (response.success) {
showAlert("success", "fas fa-plus", "Successfully added domain", domain);
$("#new_domain").val("");
$("#new_comment").val("");
utils.showAlert("success", "fas fa-plus", "Successfully added " + domain_regex, domain);
domainEl.val("");
commentEl.val("");
wildcardEl.prop("checked", false);
table.ajax.reload();
} else showAlert("error", "", "Error while adding new domain", response.message);
} else {
utils.showAlert("error", "", "Error while adding new " + domain_regex, response.message);
}
},
error: function(jqXHR, exception) {
showAlert("error", "", "Error while adding new domain", jqXHR.responseText);
utils.enableAll();
utils.showAlert("error", "", "Error while adding new " + domain_regex, jqXHR.responseText);
console.log(exception);
}
});
@@ -290,36 +381,60 @@ function addDomain() {
function editDomain() {
var elem = $(this).attr("id");
var tr = $(this).closest("tr");
var domain = tr.find("#domain").text();
var id = tr.find("#id").val();
var type = tr.find("#type").val();
var status = tr.find("#status").is(":checked") ? 1 : 0;
var comment = tr.find("#comment").val();
var groups = tr.find("#multiselect").val();
var id = tr.attr("data-id");
var domain = tr.find("#domain_" + id).text();
var type = tr.find("#type_" + id).val();
var status = tr.find("#status_" + id).is(":checked") ? 1 : 0;
var comment = tr.find("#comment_" + id).val();
// Show group assignment field only if in full domain management mode
// if not included, just use the row data.
var rowData = table.row(tr).data();
var groups = table.column(5).visible() ? tr.find("#multiselect_" + id).val() : rowData.groups;
var domain_regex;
if (type === "0" || type === "1") {
domain_regex = "domain";
} else if (type === "2" || type === "3") {
domain_regex = "regex";
}
var done = "edited";
var not_done = "editing";
if (elem === "status" && status === 1) {
done = "enabled";
not_done = "enabling";
} else if (elem === "status" && status === 0) {
done = "disabled";
not_done = "disabling";
} else if (elem === "name") {
done = "edited name of";
not_done = "editing name of";
} else if (elem === "comment") {
done = "edited comment of";
not_done = "editing comment of";
} else if (elem === "type") {
done = "edited type of";
not_done = "editing type of";
} else if (elem === "multiselect") {
done = "edited groups of";
not_done = "editing groups of";
switch (elem) {
case "status_" + id:
if (status === 0) {
done = "disabled";
not_done = "disabling";
} else if (status === 1) {
done = "enabled";
not_done = "enabling";
}
break;
case "name_" + id:
done = "edited name of";
not_done = "editing name of";
break;
case "comment_" + id:
done = "edited comment of";
not_done = "editing comment of";
break;
case "type_" + id:
done = "edited type of";
not_done = "editing type of";
break;
case "multiselect_" + id:
done = "edited groups of";
not_done = "editing groups of";
break;
default:
alert("bad element or invalid data-id!");
return;
}
showAlert("info", "", "Editing domain...", name);
utils.disableAll();
utils.showAlert("info", "", "Editing " + domain_regex + "...", name);
$.ajax({
url: "scripts/pi-hole/php/groups.php",
method: "post",
@@ -334,21 +449,28 @@ function editDomain() {
token: token
},
success: function(response) {
utils.enableAll();
if (response.success) {
showAlert("success", "fas fa-pencil-alt", "Successfully " + done + " domain", domain);
utils.showAlert(
"success",
"fas fa-pencil-alt",
"Successfully " + done + " " + domain_regex,
domain
);
} else
showAlert(
utils.showAlert(
"error",
"",
"Error while " + not_done + " domain with ID " + id,
"Error while " + not_done + " " + domain_regex + " with ID " + id,
response.message
);
},
error: function(jqXHR, exception) {
showAlert(
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while " + not_done + " domain with ID " + id,
"Error while " + not_done + " " + domain_regex + " with ID " + id,
jqXHR.responseText
);
console.log(exception);
@@ -357,27 +479,55 @@ function editDomain() {
}
function deleteDomain() {
var id = $(this).attr("data-id");
var tr = $(this).closest("tr");
var domain = tr.find("#domain").text();
var id = tr.attr("data-id");
var domain = tr.find("#domain_" + id).text();
var type = tr.find("#type_" + id).val();
showAlert("info", "", "Deleting domain...", domain);
var domain_regex;
if (type === "0" || type === "1") {
domain_regex = "domain";
} else if (type === "2" || type === "3") {
domain_regex = "regex";
}
utils.disableAll();
utils.showAlert("info", "", "Deleting " + domain_regex + "...", domain);
$.ajax({
url: "scripts/pi-hole/php/groups.php",
method: "post",
dataType: "json",
data: { action: "delete_domain", id: id, token: token },
success: function(response) {
utils.enableAll();
if (response.success) {
showAlert("success", "far fa-trash-alt", "Successfully deleted domain", domain);
utils.showAlert(
"success",
"far fa-trash-alt",
"Successfully deleted " + domain_regex,
domain
);
table
.row(tr)
.remove()
.draw(false);
} else showAlert("error", "", "Error while deleting domain with ID " + id, response.message);
} else {
utils.showAlert(
"error",
"",
"Error while deleting " + domain_regex + " with ID " + id,
response.message
);
}
},
error: function(jqXHR, exception) {
showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText);
utils.enableAll();
utils.showAlert(
"error",
"",
"Error while deleting " + domain_regex + " with ID " + id,
jqXHR.responseText
);
console.log(exception);
}
});