From eab6485edb0ce0bbb4805790a9ee006716b30c2d Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 25 May 2020 12:32:43 +0300 Subject: [PATCH] Move `escapeHtml` to utils. Signed-off-by: XhmikosR --- auditlog.php | 1 + db_lists.php | 1 + index.php | 1 + scripts/pi-hole/js/auditlog.js | 21 ++++------------- scripts/pi-hole/js/db_lists.js | 35 ++++++++-------------------- scripts/pi-hole/js/index.js | 42 ++++++++++++---------------------- scripts/pi-hole/js/utils.js | 16 +++++++++++++ 7 files changed, 47 insertions(+), 70 deletions(-) diff --git a/auditlog.php b/auditlog.php index 069c3ac5..9e5d4464 100644 --- a/auditlog.php +++ b/auditlog.php @@ -73,6 +73,7 @@ + + + ": ">", - '"': """, - "'": "'" - }; - - return text.replace(/[&<>"']/g, function (m) { - return map[m]; - }); -} - function updateTopLists() { $.getJSON("api.php?topItems=audit", function (data) { if ("FTLnotrunning" in data) { @@ -38,7 +25,7 @@ function updateTopLists() { for (domain in data.top_queries) { if (Object.prototype.hasOwnProperty.call(data.top_queries, domain)) { // Sanitize domain - domain = escapeHtml(domain); + domain = utils.escapeHtml(domain); url = '' + domain + ""; domaintable.append( "" + @@ -57,7 +44,7 @@ function updateTopLists() { if (Object.prototype.hasOwnProperty.call(data.top_ads, domain)) { var input = domain.split(" "); // Sanitize domain - var printdomain = escapeHtml(input[0]); + var printdomain = utils.escapeHtml(input[0]); if (input.length > 1) { url = '": ">", - '"': """, - "'": "'" - }; - - return text.replace(/[&<>"']/g, function (m) { - return map[m]; - }); -} - function updateTopClientsChart() { $("#client-frequency .overlay").show(); $.getJSON("api_db.php?topClients&from=" + from + "&until=" + until, function (data) { @@ -84,10 +69,10 @@ function updateTopClientsChart() { for (client in data.top_sources) { if (Object.prototype.hasOwnProperty.call(data.top_sources, client)) { // Sanitize client - client = escapeHtml(client); - if (escapeHtml(client) !== client) { + client = utils.escapeHtml(client); + if (utils.escapeHtml(client) !== client) { // Make a copy with the escaped index if necessary - data.top_sources[escapeHtml(client)] = data.top_sources[client]; + data.top_sources[utils.escapeHtml(client)] = data.top_sources[client]; } if (client.indexOf("|") !== -1) { @@ -138,10 +123,10 @@ function updateTopDomainsChart() { for (domain in data.top_domains) { if (Object.prototype.hasOwnProperty.call(data.top_domains, domain)) { // Sanitize domain - domain = escapeHtml(domain); - if (escapeHtml(domain) !== domain) { + domain = utils.escapeHtml(domain); + if (utils.escapeHtml(domain) !== domain) { // Make a copy with the escaped index if necessary - data.top_domains[escapeHtml(domain)] = data.top_domains[domain]; + data.top_domains[utils.escapeHtml(domain)] = data.top_domains[domain]; } percentage = (data.top_domains[domain] / sum) * 100; @@ -185,10 +170,10 @@ function updateTopAdsChart() { for (ad in data.top_ads) { if (Object.prototype.hasOwnProperty.call(data.top_ads, ad)) { // Sanitize ad - ad = escapeHtml(ad); - if (escapeHtml(ad) !== ad) { + ad = utils.escapeHtml(ad); + if (utils.escapeHtml(ad) !== ad) { // Make a copy with the escaped index if necessary - data.top_ads[escapeHtml(ad)] = data.top_ads[ad]; + data.top_ads[utils.escapeHtml(ad)] = data.top_ads[ad]; } percentage = (data.top_ads[ad] / sum) * 100; diff --git a/scripts/pi-hole/js/index.js b/scripts/pi-hole/js/index.js index f2a51c83..75d01dc4 100644 --- a/scripts/pi-hole/js/index.js +++ b/scripts/pi-hole/js/index.js @@ -5,8 +5,9 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ +/* global utils:false, Chart:false, updateSessionTimer:false */ + // Define global variables -/* global Chart:false, updateSessionTimer:false */ var timeLineChart, clientsChart; var queryTypePieChart, forwardDestinationPieChart; @@ -528,21 +529,6 @@ function updateForwardDestinationsPie() { }); } -// Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 -function escapeHtml(text) { - var map = { - "&": "&", - "<": "<", - ">": ">", - '"': """, - "'": "'" - }; - - return text.replace(/[&<>"']/g, function (m) { - return map[m]; - }); -} - function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources&topClientsBlocked", function (data) { if ("FTLnotrunning" in data) { @@ -556,12 +542,12 @@ function updateTopClientsChart() { for (client in data.top_sources) { if (Object.prototype.hasOwnProperty.call(data.top_sources, client)) { // Sanitize client - if (escapeHtml(client) !== client) { + if (utils.escapeHtml(client) !== client) { // Make a copy with the escaped index if necessary - data.top_sources[escapeHtml(client)] = data.top_sources[client]; + data.top_sources[utils.escapeHtml(client)] = data.top_sources[client]; } - client = escapeHtml(client); + client = utils.escapeHtml(client); if (client.indexOf("|") !== -1) { idx = client.indexOf("|"); clientname = client.substr(0, idx); @@ -602,12 +588,12 @@ function updateTopClientsChart() { for (client in data.top_sources_blocked) { if (Object.prototype.hasOwnProperty.call(data.top_sources_blocked, client)) { // Sanitize client - if (escapeHtml(client) !== client) { + if (utils.escapeHtml(client) !== client) { // Make a copy with the escaped index if necessary - data.top_sources_blocked[escapeHtml(client)] = data.top_sources_blocked[client]; + data.top_sources_blocked[utils.escapeHtml(client)] = data.top_sources_blocked[client]; } - client = escapeHtml(client); + client = utils.escapeHtml(client); if (client.indexOf("|") !== -1) { idx = client.indexOf("|"); clientname = client.substr(0, idx); @@ -674,12 +660,12 @@ function updateTopLists() { for (domain in data.top_queries) { if (Object.prototype.hasOwnProperty.call(data.top_queries, domain)) { // Sanitize domain - if (escapeHtml(domain) !== domain) { + if (utils.escapeHtml(domain) !== domain) { // Make a copy with the escaped index if necessary - data.top_queries[escapeHtml(domain)] = data.top_queries[domain]; + data.top_queries[utils.escapeHtml(domain)] = data.top_queries[domain]; } - domain = escapeHtml(domain); + domain = utils.escapeHtml(domain); urlText = domain === "" ? "." : domain; url = '' + urlText + ""; percentage = (data.top_queries[domain] / data.dns_queries_today) * 100; @@ -707,12 +693,12 @@ function updateTopLists() { for (domain in data.top_ads) { if (Object.prototype.hasOwnProperty.call(data.top_ads, domain)) { // Sanitize domain - if (escapeHtml(domain) !== domain) { + if (utils.escapeHtml(domain) !== domain) { // Make a copy with the escaped index if necessary - data.top_ads[escapeHtml(domain)] = data.top_ads[domain]; + data.top_ads[utils.escapeHtml(domain)] = data.top_ads[domain]; } - domain = escapeHtml(domain); + domain = utils.escapeHtml(domain); urlText = domain === "" ? "." : domain; url = '' + urlText + ""; percentage = (data.top_ads[domain] / data.ads_blocked_today) * 100; diff --git a/scripts/pi-hole/js/utils.js b/scripts/pi-hole/js/utils.js index 5d09cee5..810ab5df 100644 --- a/scripts/pi-hole/js/utils.js +++ b/scripts/pi-hole/js/utils.js @@ -7,6 +7,21 @@ /* global moment:false */ +// Credit: https://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 +function escapeHtml(text) { + var map = { + "&": "&", + "<": "<", + ">": ">", + '"': """, + "'": "'" + }; + + return text.replace(/[&<>"']/g, function (m) { + return map[m]; + }); +} + var info = null; function showAlert(type, icon, title, message) { var opts = {}; @@ -172,6 +187,7 @@ function stateLoadCallback(itemName) { window.utils = (function () { return { + escapeHtml: escapeHtml, showAlert: showAlert, datetime: datetime, disableAll: disableAll,