From 1c4e8056c29a387e3ba719254acf584f96b7fc70 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 12 Jan 2020 13:16:55 +0100 Subject: [PATCH 1/7] Allow underscores in the middle of TLDs. Fixes #1036 Signed-off-by: DL6ER --- scripts/pi-hole/php/savesettings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/php/savesettings.php b/scripts/pi-hole/php/savesettings.php index a7524b77..8ab0922a 100644 --- a/scripts/pi-hole/php/savesettings.php +++ b/scripts/pi-hole/php/savesettings.php @@ -35,7 +35,7 @@ function istrue(&$argument) { // Credit: http://stackoverflow.com/a/4694816/2087442 function validDomain($domain_name) { - $validChars = preg_match("/^([_a-z\d](-*[_a-z\d])*)(\.([_a-z\d](-*[a-z\d])*))*(\.([a-z\d])*)*$/i", $domain_name); + $validChars = preg_match("/^([_a-z\d](-*[_a-z\d])*)(\.([_a-z\d](-*[a-z\d])*))*(\.([_a-z\d])*)*$/i", $domain_name); $lengthCheck = preg_match("/^.{1,253}$/", $domain_name); $labelLengthCheck = preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name); return ( $validChars && $lengthCheck && $labelLengthCheck ); //length of each label @@ -44,7 +44,7 @@ function validDomain($domain_name) function validDomainWildcard($domain_name) { // There has to be either no or at most one "*" at the beginning of a line - $validChars = preg_match("/^((\*.)?[_a-z\d](-*[_a-z\d])*)(\.([_a-z\d](-*[a-z\d])*))*(\.([a-z\d])*)*$/i", $domain_name); + $validChars = preg_match("/^((\*.)?[_a-z\d](-*[_a-z\d])*)(\.([_a-z\d](-*[a-z\d])*))*(\.([_a-z\d])*)*$/i", $domain_name); $lengthCheck = preg_match("/^.{1,253}$/", $domain_name); $labelLengthCheck = preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name); return ( $validChars && $lengthCheck && $labelLengthCheck ); //length of each label From fa6033ab56dca49c2b2cb32d29d48c019e264bb9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 20 Jan 2020 19:03:00 +0100 Subject: [PATCH 2/7] Sort by first IP if multiple IPs are available for a given device. Signed-off-by: DL6ER --- scripts/pi-hole/js/ip-address-sorting.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/pi-hole/js/ip-address-sorting.js b/scripts/pi-hole/js/ip-address-sorting.js index 3055b3c1..21343fa2 100644 --- a/scripts/pi-hole/js/ip-address-sorting.js +++ b/scripts/pi-hole/js/ip-address-sorting.js @@ -14,6 +14,12 @@ jQuery.extend(jQuery.fn.dataTableExt.oSort, { } var i, item; + // Use the first IP in case there is a list of IPs + // for a given device + if (Array.isArray(a)) { + a = a[0]; + } + var m = a.split("."), n = a.split(":"), x = "", From 43a861a6a90f6dd9ee1dad7f6a2f59e9b48ce5f1 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 22 Jan 2020 19:10:21 +0100 Subject: [PATCH 3/7] Disable form elements while database operation is pending. Signed-off-by: DL6ER --- groups-adlists.php | 1 + groups-clients.php | 1 + groups-domains.php | 1 + groups.php | 1 + scripts/pi-hole/js/groups-adlists.js | 78 ++++-------------------- scripts/pi-hole/js/groups-clients.js | 74 +++++------------------ scripts/pi-hole/js/groups-common.js | 90 ++++++++++++++++++++++++++++ scripts/pi-hole/js/groups-domains.js | 79 +++++------------------- scripts/pi-hole/js/groups.js | 78 ++++-------------------- 9 files changed, 148 insertions(+), 255 deletions(-) create mode 100644 scripts/pi-hole/js/groups-common.js diff --git a/groups-adlists.php b/groups-adlists.php index 9b806967..49ddb58b 100644 --- a/groups-adlists.php +++ b/groups-adlists.php @@ -72,6 +72,7 @@ + + + + " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - 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: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function get_groups() { $.post( @@ -83,10 +26,6 @@ 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", addAdlist); @@ -230,6 +169,7 @@ function addAdlist() { var address = $("#new_address").val(); var comment = $("#new_comment").val(); + disableAll(); showAlert("info", "", "Adding adlist...", address); if (address.length === 0) { @@ -248,6 +188,7 @@ function addAdlist() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added adlist", address); $("#new_address").val(""); @@ -258,6 +199,7 @@ function addAdlist() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); console.log(exception); } @@ -289,6 +231,7 @@ function editAdlist() { not_done = "editing groups of"; } + disableAll(); showAlert("info", "", "Editing adlist...", address); $.ajax({ @@ -304,6 +247,7 @@ function editAdlist() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -321,6 +265,7 @@ function editAdlist() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -337,6 +282,7 @@ function deleteAdlist() { var tr = $(this).closest("tr"); var address = tr.find("#address").text(); + disableAll(); showAlert("info", "", "Deleting adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -344,6 +290,7 @@ function deleteAdlist() { dataType: "json", data: { action: "delete_adlist", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted adlist ", address); table @@ -353,6 +300,7 @@ function deleteAdlist() { } else showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index 68fbed9e..0edb1a50 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -5,69 +5,13 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ +/* global showAlert:false */ +/* global disableAll:false */ +/* global enableAll:false */ + var table; var groups = []; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - 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: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function reload_client_suggestions() { $.post( @@ -238,9 +182,11 @@ function addClient() { ip = $("#ip-custom").val(); } + disableAll(); showAlert("info", "", "Adding client...", ip); if (ip.length === 0) { + enableAll(); showAlert("warning", "", "Warning", "Please specify a client IP address"); return; } @@ -251,6 +197,7 @@ function addClient() { dataType: "json", data: { action: "add_client", ip: ip, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); reload_client_suggestions(); @@ -260,6 +207,7 @@ function addClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new client", jqXHR.responseText); console.log(exception); } @@ -286,6 +234,7 @@ function editClient() { ip_name += " (" + name + ")"; } + disableAll(); showAlert("info", "", "Editing client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -293,6 +242,7 @@ function editClient() { dataType: "json", data: { action: "edit_client", id: id, groups: groups, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -305,6 +255,7 @@ function editClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -327,6 +278,7 @@ function deleteClient() { ip_name += " (" + name + ")"; } + disableAll(); showAlert("info", "", "Deleting client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -334,6 +286,7 @@ function deleteClient() { dataType: "json", data: { action: "delete_client", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted client ", ip_name); table @@ -346,6 +299,7 @@ function deleteClient() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js new file mode 100644 index 00000000..dce071f8 --- /dev/null +++ b/scripts/pi-hole/js/groups-common.js @@ -0,0 +1,90 @@ +/* Pi-hole: A black hole for Internet advertisements + * (c) 2020 Pi-hole, LLC (https://pi-hole.net) + * Network-wide ad blocking via your own hardware. + * + * 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 showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ + +var info = null; +function showAlert(type, icon, title, message) { + var opts = {}; + title = " " + title + "
"; + switch (type) { + case "info": + opts = { + type: "info", + icon: "glyphicon glyphicon-time", + 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: "glyphicon glyphicon-warning-sign", + title: title, + message: message + }; + if (info) { + info.update(opts); + } else { + $.notify(opts); + } + + break; + case "error": + opts = { + type: "danger", + icon: "glyphicon glyphicon-remove", + title: " Error, something went wrong!
", + message: message + }; + if (info) { + info.update(opts); + } else { + $.notify(opts); + } + + break; + default: + } +} + +function datetime(date) { + return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); +} + +function disableAll() { + $("input").attr("disabled", true); + $("select").attr("disabled", true); + $("button").attr("disabled", true); + $("textarea").attr("disabled", true); +} + +function enableAll() { + $("input").attr("disabled", false); + $("select").attr("disabled", false); + $("button").attr("disabled", false); + $("textarea").attr("disabled", false); +} diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js index b83c5296..ecdf696b 100644 --- a/scripts/pi-hole/js/groups-domains.js +++ b/scripts/pi-hole/js/groups-domains.js @@ -5,71 +5,14 @@ * 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 showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ var table; var groups = []; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - 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: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} function get_groups() { $.post( @@ -83,10 +26,6 @@ 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); @@ -248,9 +187,11 @@ function addDomain() { var type = $("#new_type").val(); var comment = $("#new_comment").val(); + disableAll(); showAlert("info", "", "Adding domain...", domain); if (domain.length === 0) { + enableAll(); showAlert("warning", "", "Warning", "Please specify a domain"); return; } @@ -267,6 +208,7 @@ function addDomain() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); $("#new_domain").val(""); @@ -275,6 +217,7 @@ function addDomain() { } else showAlert("error", "", "Error while adding new domain", response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new domain", jqXHR.responseText); console.log(exception); } @@ -313,6 +256,7 @@ function editDomain() { not_done = "editing groups of"; } + disableAll(); showAlert("info", "", "Editing domain...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -328,6 +272,7 @@ function editDomain() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert( "success", @@ -344,6 +289,7 @@ function editDomain() { ); }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -360,6 +306,7 @@ function deleteDomain() { var tr = $(this).closest("tr"); var domain = tr.find("#domain").text(); + disableAll(); showAlert("info", "", "Deleting domain...", domain); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -367,6 +314,7 @@ function deleteDomain() { dataType: "json", data: { action: "delete_domain", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted domain", domain); table @@ -376,6 +324,7 @@ function deleteDomain() { } else showAlert("error", "", "Error while deleting domain with ID " + id, response.message); }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); console.log(exception); } diff --git a/scripts/pi-hole/js/groups.js b/scripts/pi-hole/js/groups.js index b7ec372c..48798451 100644 --- a/scripts/pi-hole/js/groups.js +++ b/scripts/pi-hole/js/groups.js @@ -5,74 +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 showAlert:false */ +/* global datetime:false */ +/* global disableAll:false */ +/* global enableAll:false */ var table; var token = $("#token").html(); -var info = null; - -function showAlert(type, icon, title, message) { - var opts = {}; - title = " " + title + "
"; - switch (type) { - case "info": - opts = { - type: "info", - icon: "glyphicon glyphicon-time", - 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: "glyphicon glyphicon-warning-sign", - title: title, - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - case "error": - opts = { - type: "danger", - icon: "glyphicon glyphicon-remove", - title: " Error, something went wrong!
", - message: message - }; - if (info) { - info.update(opts); - } else { - $.notify(opts); - } - - break; - default: - } -} - -function datetime(date) { - return moment.unix(Math.floor(date)).format("Y-MM-DD HH:mm:ss z"); -} $(document).ready(function() { $("#btnAdd").on("click", addGroup); @@ -191,6 +130,7 @@ function addGroup() { var name = $("#new_name").val(); var desc = $("#new_desc").val(); + disableAll(); showAlert("info", "", "Adding group...", name); if (name.length === 0) { @@ -204,6 +144,7 @@ function addGroup() { dataType: "json", data: { action: "add_group", name: name, desc: desc, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name); $("#new_name").val(""); @@ -214,6 +155,7 @@ function addGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while adding new group", jqXHR.responseText); console.log(exception); } @@ -244,6 +186,7 @@ function editGroup() { not_done = "editing description of"; } + disableAll(); showAlert("info", "", "Editing group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -258,6 +201,7 @@ function editGroup() { token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-pencil", "Successfully " + done + " group", name); } else { @@ -270,6 +214,7 @@ function editGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert( "error", "", @@ -286,6 +231,7 @@ function deleteGroup() { var tr = $(this).closest("tr"); var name = tr.find("#name").val(); + disableAll(); showAlert("info", "", "Deleting group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -293,6 +239,7 @@ function deleteGroup() { dataType: "json", data: { action: "delete_group", id: id, token: token }, success: function(response) { + enableAll(); if (response.success) { showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted group ", name); table @@ -304,6 +251,7 @@ function deleteGroup() { } }, error: function(jqXHR, exception) { + enableAll(); showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText); console.log(exception); } From 516c9e946a2f9c9c62065d31c09317e678ec9c29 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 27 Jan 2020 11:01:48 +0000 Subject: [PATCH 4/7] Add utils object for common code. Signed-off-by: DL6ER --- scripts/pi-hole/js/groups-adlists.js | 65 ++++++++++++++++------------ scripts/pi-hole/js/groups-clients.js | 59 ++++++++++++++----------- scripts/pi-hole/js/groups-common.js | 13 ++++-- scripts/pi-hole/js/groups-domains.js | 62 +++++++++++++------------- scripts/pi-hole/js/groups.js | 63 +++++++++++++++------------ 5 files changed, 147 insertions(+), 115 deletions(-) diff --git a/scripts/pi-hole/js/groups-adlists.js b/scripts/pi-hole/js/groups-adlists.js index 5c571724..f16e1bc5 100644 --- a/scripts/pi-hole/js/groups-adlists.js +++ b/scripts/pi-hole/js/groups-adlists.js @@ -5,10 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global showAlert:false */ -/* global datetime:false */ -/* global disableAll:false */ -/* global enableAll:false */ +/* global utils:false */ var table; var groups = []; @@ -59,9 +56,9 @@ function initTable() { rowCallback: function(row, data) { 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( @@ -173,11 +170,11 @@ function addAdlist() { var address = $("#new_address").val(); var comment = $("#new_comment").val(); - disableAll(); - showAlert("info", "", "Adding adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Adding adlist...", address); if (address.length === 0) { - showAlert("warning", "", "Warning", "Please specify an adlist address"); + utils.showAlert("warning", "", "Warning", "Please specify an adlist address"); return; } @@ -192,19 +189,24 @@ function addAdlist() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added adlist", address); + utils.showAlert( + "success", + "glyphicon glyphicon-plus", + "Successfully added adlist", + address + ); $("#new_address").val(""); $("#new_comment").val(""); table.ajax.reload(); } else { - showAlert("error", "", "Error while adding new adlist: ", response.message); + utils.showAlert("error", "", "Error while adding new adlist: ", response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new adlist: ", jqXHR.responseText); console.log(exception); } }); @@ -235,8 +237,8 @@ function editAdlist() { not_done = "editing groups of"; } - disableAll(); - showAlert("info", "", "Editing adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Editing adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", @@ -251,16 +253,16 @@ function editAdlist() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-pencil", "Successfully " + done + " adlist ", address ); } else { - showAlert( + utils.showAlert( "error", "", "Error while " + not_done + " adlist with ID " + id, @@ -269,8 +271,8 @@ function editAdlist() { } }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " adlist with ID " + id, @@ -286,26 +288,33 @@ function deleteAdlist() { var tr = $(this).closest("tr"); var address = tr.find("#address").text(); - disableAll(); - showAlert("info", "", "Deleting adlist...", address); + utils.disableAll(); + utils.showAlert("info", "", "Deleting adlist...", address); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_adlist", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted adlist ", address); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted adlist ", + address + ); table .row(tr) .remove() .draw(false); - } else showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); + } else { + utils.showAlert("error", "", "Error while deleting adlist with ID " + id, response.message); + } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting adlist with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index 3a4cae4e..71e0e585 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -5,9 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global showAlert:false */ -/* global disableAll:false */ -/* global enableAll:false */ +/* global utils:false */ var table; var groups = []; @@ -186,12 +184,12 @@ function addClient() { ip = $("#ip-custom").val(); } - disableAll(); - showAlert("info", "", "Adding client...", ip); + utils.disableAll(); + utils.showAlert("info", "", "Adding client...", ip); if (ip.length === 0) { - enableAll(); - showAlert("warning", "", "Warning", "Please specify a client IP address"); + utils.enableAll(); + utils.showAlert("warning", "", "Warning", "Please specify a client IP address"); return; } @@ -201,18 +199,18 @@ function addClient() { dataType: "json", data: { action: "add_client", ip: ip, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); + utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added client", ip); reload_client_suggestions(); table.ajax.reload(); } else { - showAlert("error", "", "Error while adding new client", response.message); + utils.showAlert("error", "", "Error while adding new client", response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new client", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new client", jqXHR.responseText); console.log(exception); } }); @@ -238,29 +236,33 @@ function editClient() { ip_name += " (" + name + ")"; } - disableAll(); - showAlert("info", "", "Editing client...", ip_name); + utils.disableAll(); + utils.showAlert("info", "", "Editing client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "edit_client", id: id, groups: groups, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-plus", "Successfully " + done + " client", ip_name ); } else { - showAlert("error", "Error while " + not_done + " client with ID " + id, response.message); + utils.showAlert( + "error", + "Error while " + not_done + " client with ID " + id, + response.message + ); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " client with ID " + id, @@ -282,29 +284,34 @@ function deleteClient() { ip_name += " (" + name + ")"; } - disableAll(); - showAlert("info", "", "Deleting client...", ip_name); + utils.disableAll(); + utils.showAlert("info", "", "Deleting client...", ip_name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_client", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted client ", ip_name); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted client ", + ip_name + ); table .row(tr) .remove() .draw(false); reload_client_suggestions(); } else { - showAlert("error", "", "Error while deleting client with ID " + id, response.message); + utils.showAlert("error", "", "Error while deleting client with ID " + id, response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting client with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js index dce071f8..8cff4727 100644 --- a/scripts/pi-hole/js/groups-common.js +++ b/scripts/pi-hole/js/groups-common.js @@ -6,10 +6,6 @@ * Please see LICENSE file for your rights under this license. */ /* global moment:false */ -/* global showAlert:false */ -/* global datetime:false */ -/* global disableAll:false */ -/* global enableAll:false */ var info = null; function showAlert(type, icon, title, message) { @@ -88,3 +84,12 @@ function enableAll() { $("button").attr("disabled", false); $("textarea").attr("disabled", false); } + +window.utils = (function() { + return { + showAlert: showAlert, + datetime: datetime, + disableAll: disableAll, + enableAll: enableAll + }; +})(); diff --git a/scripts/pi-hole/js/groups-domains.js b/scripts/pi-hole/js/groups-domains.js index 36748b40..85ae0647 100644 --- a/scripts/pi-hole/js/groups-domains.js +++ b/scripts/pi-hole/js/groups-domains.js @@ -5,10 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global showAlert:false */ -/* global datetime:false */ -/* global disableAll:false */ -/* global enableAll:false */ +/* global utils:false */ var table; var groups = []; @@ -60,9 +57,9 @@ function initTable() { rowCallback: function(row, data) { 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( @@ -191,12 +188,12 @@ function addDomain() { var type = $("#new_type").val(); var comment = $("#new_comment").val(); - disableAll(); - showAlert("info", "", "Adding domain...", domain); + utils.disableAll(); + utils.showAlert("info", "", "Adding domain...", domain); if (domain.length === 0) { - enableAll(); - showAlert("warning", "", "Warning", "Please specify a domain"); + utils.enableAll(); + utils.showAlert("warning", "", "Warning", "Please specify a domain"); return; } @@ -212,17 +209,17 @@ function addDomain() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); + utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added domain", domain); $("#new_domain").val(""); $("#new_comment").val(""); table.ajax.reload(); - } else showAlert("error", "", "Error while adding new domain", response.message); + } else utils.showAlert("error", "", "Error while adding new domain", response.message); }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new domain", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new domain", jqXHR.responseText); console.log(exception); } }); @@ -260,8 +257,8 @@ function editDomain() { not_done = "editing groups of"; } - disableAll(); - showAlert("info", "", "Editing domain...", name); + utils.disableAll(); + utils.showAlert("info", "", "Editing domain...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", @@ -276,16 +273,16 @@ function editDomain() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert( + utils.showAlert( "success", "glyphicon glyphicon-pencil", "Successfully " + done + " domain", domain ); } else - showAlert( + utils.showAlert( "error", "", "Error while " + not_done + " domain with ID " + id, @@ -293,8 +290,8 @@ function editDomain() { ); }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " domain with ID " + id, @@ -310,26 +307,33 @@ function deleteDomain() { var tr = $(this).closest("tr"); var domain = tr.find("#domain").text(); - disableAll(); - showAlert("info", "", "Deleting domain...", domain); + utils.disableAll(); + utils.showAlert("info", "", "Deleting domain...", domain); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_domain", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted domain", domain); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted domain", + 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 with ID " + id, response.message); + } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting domain with ID " + id, jqXHR.responseText); console.log(exception); } }); diff --git a/scripts/pi-hole/js/groups.js b/scripts/pi-hole/js/groups.js index d4a6bbc7..0ffa3dcc 100644 --- a/scripts/pi-hole/js/groups.js +++ b/scripts/pi-hole/js/groups.js @@ -5,10 +5,7 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -/* global showAlert:false */ -/* global datetime:false */ -/* global disableAll:false */ -/* global enableAll:false */ +/* global utils:false */ var table; var token = $("#token").html(); @@ -36,9 +33,9 @@ $(document).ready(function() { rowCallback: function(row, data) { 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( @@ -134,11 +131,11 @@ function addGroup() { var name = $("#new_name").val(); var desc = $("#new_desc").val(); - disableAll(); - showAlert("info", "", "Adding group...", name); + utils.disableAll(); + utils.showAlert("info", "", "Adding group...", name); if (name.length === 0) { - showAlert("warning", "", "Warning", "Please specify a group name"); + utils.showAlert("warning", "", "Warning", "Please specify a group name"); return; } @@ -148,19 +145,19 @@ function addGroup() { dataType: "json", data: { action: "add_group", name: name, desc: desc, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name); + utils.showAlert("success", "glyphicon glyphicon-plus", "Successfully added group", name); $("#new_name").val(""); $("#new_desc").val(""); table.ajax.reload(); } else { - showAlert("error", "", "Error while adding new group", response.message); + utils.showAlert("error", "", "Error while adding new group", response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while adding new group", jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while adding new group", jqXHR.responseText); console.log(exception); } }); @@ -190,8 +187,8 @@ function editGroup() { not_done = "editing description of"; } - disableAll(); - showAlert("info", "", "Editing group...", name); + utils.disableAll(); + utils.showAlert("info", "", "Editing group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", @@ -205,11 +202,16 @@ function editGroup() { token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-pencil", "Successfully " + done + " group", name); + utils.showAlert( + "success", + "glyphicon glyphicon-pencil", + "Successfully " + done + " group", + name + ); } else { - showAlert( + utils.showAlert( "error", "", "Error while " + not_done + " group with ID " + id, @@ -218,8 +220,8 @@ function editGroup() { } }, error: function(jqXHR, exception) { - enableAll(); - showAlert( + utils.enableAll(); + utils.showAlert( "error", "", "Error while " + not_done + " group with ID " + id, @@ -235,28 +237,33 @@ function deleteGroup() { var tr = $(this).closest("tr"); var name = tr.find("#name").val(); - disableAll(); - showAlert("info", "", "Deleting group...", name); + utils.disableAll(); + utils.showAlert("info", "", "Deleting group...", name); $.ajax({ url: "scripts/pi-hole/php/groups.php", method: "post", dataType: "json", data: { action: "delete_group", id: id, token: token }, success: function(response) { - enableAll(); + utils.enableAll(); if (response.success) { - showAlert("success", "glyphicon glyphicon-trash", "Successfully deleted group ", name); + utils.showAlert( + "success", + "glyphicon glyphicon-trash", + "Successfully deleted group ", + name + ); table .row(tr) .remove() .draw(false); } else { - showAlert("error", "", "Error while deleting group with ID " + id, response.message); + utils.showAlert("error", "", "Error while deleting group with ID " + id, response.message); } }, error: function(jqXHR, exception) { - enableAll(); - showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText); + utils.enableAll(); + utils.showAlert("error", "", "Error while deleting group with ID " + id, jqXHR.responseText); console.log(exception); } }); From abe0b760bdecc34f02bdb0755faf8ac7943bdd8d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 27 Jan 2020 12:40:45 +0000 Subject: [PATCH 5/7] Ensure we reach the end of groups.php to send SIGRTMIN to FTL. Signed-off-by: DL6ER --- scripts/pi-hole/php/groups.php | 58 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php index 252e52cd..81a9c31b 100644 --- a/scripts/pi-hole/php/groups.php +++ b/scripts/pi-hole/php/groups.php @@ -49,7 +49,7 @@ if ($_POST['action'] == 'get_groups') { } echo json_encode(array('data' => $data)); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'add_group') { // Add new group @@ -72,9 +72,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'edit_group') { // Edit group identified by ID @@ -111,9 +111,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'delete_group') { // Delete group identified by ID @@ -142,9 +142,9 @@ if ($_POST['action'] == 'get_groups') { } } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'get_clients') { // List all available groups @@ -193,7 +193,7 @@ if ($_POST['action'] == 'get_groups') { echo json_encode(array('data' => $data)); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'get_unconfigured_clients') { // List all available clients WITHOUT already configured clients @@ -227,7 +227,7 @@ if ($_POST['action'] == 'get_groups') { echo json_encode($ips); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'add_client') { // Add new client @@ -246,9 +246,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'edit_client') { // Edit client identified by ID @@ -288,9 +288,9 @@ if ($_POST['action'] == 'get_groups') { $db->query('COMMIT;'); $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'delete_client') { // Delete client identified by ID @@ -322,9 +322,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'get_domains') { // List all available groups @@ -352,7 +352,7 @@ if ($_POST['action'] == 'get_groups') { echo json_encode(array('data' => $data)); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'add_domain') { // Add new domain @@ -392,9 +392,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'edit_domain') { // Edit domain identified by ID @@ -469,9 +469,9 @@ if ($_POST['action'] == 'get_groups') { $db->query('COMMIT;'); $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'delete_domain') { // Delete domain identified by ID @@ -503,9 +503,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'get_adlists') { // List all available groups @@ -533,7 +533,7 @@ if ($_POST['action'] == 'get_groups') { echo json_encode(array('data' => $data)); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'add_adlist') { // Add new adlist @@ -556,9 +556,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'edit_adlist') { // Edit adlist identified by ID @@ -629,9 +629,9 @@ if ($_POST['action'] == 'get_groups') { $db->query('COMMIT;'); $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } elseif ($_POST['action'] == 'delete_adlist') { // Delete adlist identified by ID @@ -663,9 +663,9 @@ if ($_POST['action'] == 'get_groups') { } $reload = true; - return JSON_success(); + JSON_success(); } catch (\Exception $ex) { - return JSON_error($ex->getMessage()); + JSON_error($ex->getMessage()); } } else { log_and_die('Requested action not supported!'); From 86f56ee5df302effd424b93d17490140fb3afd7f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 20 Jan 2020 19:35:43 +0100 Subject: [PATCH 6/7] Make IP addresses links that send to the Query Log. Signed-off-by: DL6ER --- scripts/pi-hole/js/network.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/pi-hole/js/network.js b/scripts/pi-hole/js/network.js index 2320dfcd..f8e6f181 100644 --- a/scripts/pi-hole/js/network.js +++ b/scripts/pi-hole/js/network.js @@ -101,16 +101,21 @@ $(document).ready(function() { // Set number of queries to localized string (add thousand separators) $("td:eq(6)", row).html(data.numQueries.toLocaleString()); - var ips = data.ip; - var shortips = ips; - if (ips.length > MAXIPDISPLAY) { - shortips = ips.slice(0, MAXIPDISPLAY - 1); - shortips.push("..."); + var ips = []; + var maxiter = Math.min(data.ip.length, MAXIPDISPLAY); + for (let index = 0; index < maxiter; index++) { + var ip = data.ip[index]; + ips.push('' + ip + ''); + } + if (data.ip.length > MAXIPDISPLAY) { + // We hit the maximum above, add "..." to symbolize we would + // have more to show here + ips.push("..."); } - $("td:eq(0)", row).html(shortips.join("
")); + $("td:eq(0)", row).html(ips.join("
")); $("td:eq(0)", row).hover(function() { - this.title = ips.join("\n"); + this.title = data.ip.join("\n"); }); // MAC + Vendor field if available From 4228d883365aa8ed0de8f65b2cccb202390f02a0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 3 Feb 2020 23:15:25 +0100 Subject: [PATCH 7/7] xo does neither like const nor let. Use var everywhere. Signed-off-by: DL6ER --- scripts/pi-hole/js/network.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/js/network.js b/scripts/pi-hole/js/network.js index f8e6f181..6203d81d 100644 --- a/scripts/pi-hole/js/network.js +++ b/scripts/pi-hole/js/network.js @@ -103,10 +103,11 @@ $(document).ready(function() { var ips = []; var maxiter = Math.min(data.ip.length, MAXIPDISPLAY); - for (let index = 0; index < maxiter; index++) { + for (var index = 0; index < maxiter; index++) { var ip = data.ip[index]; - ips.push('' + ip + ''); + ips.push('' + ip + ""); } + if (data.ip.length > MAXIPDISPLAY) { // We hit the maximum above, add "..." to symbolize we would // have more to show here