diff --git a/api.php b/api.php index 6259590d..3c3ad7d1 100644 --- a/api.php +++ b/api.php @@ -80,16 +80,16 @@ $data = array_merge($data, getGravityDomains($gravity)); } - function filterArray(&$a) { - $sanArray = array(); - foreach ($a as $k=>$v) { - if (is_array($v)) { - $sanArray[htmlspecialchars($k)] = filterArray($v); + function filterArray(&$inArray) { + $outArray = array(); + foreach ($inArray as $key=>$value) { + if (is_array($value)) { + $outArray[htmlspecialchars($key)] = filterArray($value); } else { - $sanArray[htmlspecialchars($k)] = htmlspecialchars($v); + $outArray[htmlspecialchars($key)] = htmlspecialchars($value); } } - return $sanArray; + return $outArray; } $data = filterArray($data); diff --git a/js/pihole/footer.js b/js/pihole/footer.js index f1166433..57659dbc 100644 --- a/js/pihole/footer.js +++ b/js/pihole/footer.js @@ -79,14 +79,14 @@ function versionCompare(left, right) { $.getJSON("https://api.github.com/repos/pi-hole/pi-hole/releases/latest", function(json) { if(versionCompare(piholeVersion, json.tag_name.slice(1)) < 0) { // Alert user - $("#piholeVersion").html($("#piholeVersion").text() + '(Update available!)'); + $("#piholeVersion").html($("#piholeVersion").text() + "(Update available!)"); $("#alPiholeUpdate").show(); } }); $.getJSON("https://api.github.com/repos/pi-hole/AdminLTE/releases/latest", function(json) { if(versionCompare(webVersion, json.tag_name.slice(1)) < 0) { // Alert user - $("#webVersion").html($("#webVersion").text() + '(Update available!)'); + $("#webVersion").html($("#webVersion").text() + "(Update available!)"); $("#alWebUpdate").show(); } }); diff --git a/js/pihole/index.js b/js/pihole/index.js index ec53d861..5d7dba0e 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -42,7 +42,7 @@ function updateSummaryData(runOnce) { var textData = idx === 3 ? data[header] + "%" : data[header]; $("h3#" + header).text(textData); }); - $("h3.statistic.glow").removeClass("glow") + $("h3.statistic.glow").removeClass("glow"); }, 500); updateSessionTimer(); @@ -76,7 +76,7 @@ function updateQueriesOverTime() { timeLineChart.data.datasets[1].data.push(data.ads_over_time[1][hour]); } } - $('#queries-over-time .overlay').remove(); + $("#queries-over-time .overlay").remove(); timeLineChart.update(); }).done(function() { // Reload graph after 10 minutes @@ -109,7 +109,7 @@ function updateQueryTypes() { var dd = {data: v, backgroundColor: c}; // and push it at once queryTypeChart.data.datasets.push(dd); - $('#query-types .overlay').remove(); + $("#query-types .overlay").remove(); queryTypeChart.update(); queryTypeChart.chart.config.options.cutoutPercentage=30; queryTypeChart.update(); @@ -131,20 +131,24 @@ function escapeHtml(text) { function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { - var clienttable = $('#client-frequency').find('tbody:last'); + var clienttable = $("#client-frequency").find("tbody:last"); var domain, percentage; for (domain in data.top_sources) { - // Sanitize domain - domain = escapeHtml(domain); - var url = ""+domain+""; - percentage = data.top_sources[domain] / data.dns_queries_today * 100; - clienttable.append(" " + url + - " " + data.top_sources[domain] + "
"); + if ({}.hasOwnProperty.call(data.top_sources, domain)){ + // Sanitize domain + domain = escapeHtml(domain); + + var url = ""+domain+""; + percentage = data.top_sources[domain] / data.dns_queries_today * 100; + clienttable.append(" " + url + + " " + data.top_sources[domain] + "
"); + } + } - $('#client-frequency .overlay').remove(); + $("#client-frequency .overlay").remove(); }); } @@ -164,7 +168,7 @@ function updateForwardDestinations() { var dd = {data: v, backgroundColor: c}; // and push it at once forwardDestinationChart.data.datasets.push(dd); - $('#forward-destinations .overlay').remove(); + $("#forward-destinations .overlay").remove(); forwardDestinationChart.update(); forwardDestinationChart.chart.config.options.cutoutPercentage=30; forwardDestinationChart.update(); @@ -173,34 +177,39 @@ function updateForwardDestinations() { function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { - var domaintable = $('#domain-frequency').find('tbody:last'); - var adtable = $('#ad-frequency').find('tbody:last'); + var domaintable = $("#domain-frequency").find("tbody:last"); + var adtable = $("#ad-frequency").find("tbody:last"); var url, domain, percentage; for (domain in data.top_queries) { - // Sanitize domain - domain = escapeHtml(domain); - if(domain !== "pi.hole") - { - url = ""+domain+""; + if ({}.hasOwnProperty.call(data.top_queries,domain)){ + // Sanitize domain + domain = escapeHtml(domain); + if(domain !== "pi.hole") + { + url = ""+domain+""; + } + else + { + url = domain; + } + percentage = data.top_queries[domain] / data.dns_queries_today * 100; + domaintable.append(" " + url + + " " + data.top_queries[domain] + "
"); } - else - { - url = domain; - } - percentage = data.top_queries[domain] / data.dns_queries_today * 100; - domaintable.append(" " + url + - " " + data.top_queries[domain] + "
"); + } for (domain in data.top_ads) { - // Sanitize domain - domain = escapeHtml(domain); - url = ""+domain+""; - percentage = data.top_ads[domain] / data.ads_blocked_today * 100; - adtable.append(" " + url + - " " + data.top_ads[domain] + "
"); + if ({}.hasOwnProperty.call(data.top_ads,domain)){ + // Sanitize domain + domain = escapeHtml(domain); + url = ""+domain+""; + percentage = data.top_ads[domain] / data.ads_blocked_today * 100; + adtable.append(" " + url + + " " + data.top_ads[domain] + "
"); + } } $("#domain-frequency .overlay").remove(); diff --git a/js/pihole/list.js b/js/pihole/list.js index 3f2e2d74..ff24f94e 100644 --- a/js/pihole/list.js +++ b/js/pihole/list.js @@ -1,35 +1,29 @@ // IE likes to cache too much :P $.ajaxSetup({cache: false}); -// Handle enter button for adding domains -$(document).keypress(function(e) { - if(e.which === 13 && $("#domain").is(":focus")) { - // Enter was pressed, and the input has focus - add(); - } -}); - -// Handle buttons -$("#btnAdd").on("click", function() { - add(); -}); -$("#btnRefresh").on("click", function() { - refresh(true); -}); - -// Handle hiding of alerts -$(function(){ - $("[data-hide]").on("click", function(){ - $(this).closest("." + $(this).attr("data-hide")).hide(); - }); -}); - // Get PHP info var token = $("#token").html(); -var list_type = $("#list-type").html(); -var fullName = list_type === "white" ? "Whitelist" : "Blacklist"; +var listType = $("#list-type").html(); +var fullName = listType === "white" ? "Whitelist" : "Blacklist"; -window.onload = refresh(false); +function sub(index, entry) { + var domain = $("#"+index); + domain.hide("highlight"); + $.ajax({ + url: "php/sub.php", + method: "post", + data: {"domain":entry, "list":listType, "token":token}, + success: function(response) { + if(response.length !== 0){ + return; + } + domain.remove(); + }, + error: function(jqXHR, exception) { + alert("Failed to remove the domain!"); + } + }); +} function refresh(fade) { var list = $("#list"); @@ -39,25 +33,25 @@ function refresh(fade) { $.ajax({ url: "php/get.php", method: "get", - data: {"list":list_type}, + data: {"list":listType}, success: function(response) { list.html(""); var data = JSON.parse(response); if(data.length === 0) { - list.html(''); + list.html("
Your " + fullName + " is empty!
"); } else { data.forEach(function (entry, index) { list.append( - '
  • ' + entry + - '
  • ' + "
  • " + entry + + "
  • " ); // Handle button $("#list #"+index+"").on("click", "button", function() { - sub(index, entry) + sub(index, entry); }); }); } @@ -69,10 +63,13 @@ function refresh(fade) { }); } +window.onload = refresh(false); + function add() { var domain = $("#domain"); - if(domain.val().length === 0) + if(domain.val().length === 0){ return; + } var alInfo = $("#alInfo"); var alSuccess = $("#alSuccess"); @@ -83,7 +80,7 @@ function add() { $.ajax({ url: "php/add.php", method: "post", - data: {"domain":domain.val(), "list":list_type, "token":token}, + data: {"domain":domain.val(), "list":listType, "token":token}, success: function(response) { if (response.indexOf("not a valid argument") >= 0 || response.indexOf("is not a valid domain") >= 0) { @@ -118,20 +115,27 @@ function add() { }); } -function sub(index, entry) { - var domain = $("#"+index); - domain.hide("highlight"); - $.ajax({ - url: "php/sub.php", - method: "post", - data: {"domain":entry, "list":list_type, "token":token}, - success: function(response) { - if(response.length !== 0) - return; - domain.remove(); - }, - error: function(jqXHR, exception) { - alert("Failed to remove the domain!"); - } + + +// Handle enter button for adding domains +$(document).keypress(function(e) { + if(e.which === 13 && $("#domain").is(":focus")) { + // Enter was pressed, and the input has focus + add(); + } +}); + +// Handle buttons +$("#btnAdd").on("click", function() { + add(); +}); +$("#btnRefresh").on("click", function() { + refresh(true); +}); + +// Handle hiding of alerts +$(function(){ + $("[data-hide]").on("click", function(){ + $(this).closest("." + $(this).attr("data-hide")).hide(); }); -} +}); diff --git a/js/pihole/queries.js b/js/pihole/queries.js index 4a52c990..0029ea3e 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -9,16 +9,77 @@ function escapeRegex(text) { return text.replace(/[().]/g, function(m) { return map[m]; }); } +function refreshData() { + tableApi.ajax.url("api.php?getAllQueries").load(); +// updateSessionTimer(); +} + +function add(domain,list) { + var token = $("#token").html(); + var alInfo = $("#alInfo"); + var alList = $("#alList"); + var alDomain = $("#alDomain"); + alDomain.html(domain); + var alSuccess = $("#alSuccess"); + var alFailure = $("#alFailure"); + + if(list === "white") + { + alList.html("Whitelist"); + } + else + { + alList.html("Blacklist"); + } + + alInfo.show(); + alSuccess.hide(); + alFailure.hide(); + $.ajax({ + url: "php/add.php", + method: "post", + data: {"domain":domain, "list":list, "token":token}, + success: function(response) { + if (response.indexOf("not a valid argument") >= 0 || response.indexOf("is not a valid domain") >= 0) + { + alFailure.show(); + alFailure.delay(1000).fadeOut(2000, function() { alFailure.hide(); }); + } + else + { + alSuccess.show(); + alSuccess.delay(1000).fadeOut(2000, function() { alSuccess.hide(); }); + } + alInfo.delay(1000).fadeOut(2000, function() { + alInfo.hide(); + alList.html(""); + alDomain.html(""); + }); + }, + error: function(jqXHR, exception) { + alFailure.show(); + alFailure.delay(1000).fadeOut(2000, function() { + alFailure.hide(); + }); + alInfo.delay(1000).fadeOut(2000, function() { + alInfo.hide(); + alList.html(""); + alDomain.html(""); + }); + } + }); +} + $(document).ready(function() { - tableApi = $('#all-queries').DataTable( { + tableApi = $("#all-queries").DataTable( { "rowCallback": function( row, data, index ){ - if (data[4] == "Pi-holed") { - $(row).css('color','red'); - $('td:eq(5)', row).html( '' ); + if (data[4] === "Pi-holed") { + $(row).css("color","red"); + $("td:eq(5)", row).html( "" ); } else{ - $(row).css('color','green'); - $('td:eq(5)', row).html( '' ); + $(row).css("color","green"); + $("td:eq(5)", row).html( "" ); } }, @@ -41,12 +102,12 @@ $(document).ready(function() { "columnDefs": [ { "targets": -1, "data": null, - "defaultContent": '' + "defaultContent": "" } ] }); - $('#all-queries tbody').on( 'click', 'button', function () { - var data = tableApi.row( $(this).parents('tr') ).data(); - if (data[4] == "Pi-holed") + $("#all-queries tbody").on( "click", "button", function () { + var data = tableApi.row( $(this).parents("tr") ).data(); + if (data[4] === "Pi-holed") { add(data[2],"white"); } @@ -74,63 +135,4 @@ $(document).ready(function() { } } ); -function refreshData() { - tableApi.ajax.url("api.php?getAllQueries").load(); -// updateSessionTimer(); -} -function add(domain,list) { - var token = $("#token").html(); - var alInfo = $("#alInfo"); - var alList = $("#alList"); - var alDomain = $("#alDomain"); - alDomain.html(domain); - var alSuccess = $("#alSuccess"); - var alFailure = $("#alFailure"); - - if(list == "white") - { - alList.html("Whitelist"); - } - else - { - alList.html("Blacklist"); - } - - alInfo.show(); - alSuccess.hide(); - alFailure.hide(); - $.ajax({ - url: "php/add.php", - method: "post", - data: {"domain":domain, "list":list, "token":token}, - success: function(response) { - if (response.indexOf("not a valid argument") >= 0 || response.indexOf("is not a valid domain") >= 0) - { - alFailure.show(); - alFailure.delay(1000).fadeOut(2000, function() { alFailure.hide(); }); - } - else - { - alSuccess.show(); - alSuccess.delay(1000).fadeOut(2000, function() { alSuccess.hide(); }); - } - alInfo.delay(1000).fadeOut(2000, function() { - alInfo.hide(); - alList.html(""); - alDomain.html(""); - }); - }, - error: function(jqXHR, exception) { - alFailure.show(); - alFailure.delay(1000).fadeOut(2000, function() { - alFailure.hide(); - }); - alInfo.delay(1000).fadeOut(2000, function() { - alInfo.hide(); - alList.html(""); - alDomain.html(""); - }); - } - }); -} diff --git a/php/get.php b/php/get.php index 12f0a321..eeae056c 100644 --- a/php/get.php +++ b/php/get.php @@ -16,16 +16,16 @@ for($i = sizeof($list)-1; $i >= 0; $i--) { unset($list[$i]); } -function filterArray(&$a) { - $sanArray = array(); - foreach ($a as $k=>$v) { - if (is_array($v)) { - $sanArray[htmlspecialchars($k)] = filterArray($v); +function filterArray(&$inArray) { + $outArray = array(); + foreach ($inArray as $key=>$value) { + if (is_array($value)) { + $outArray[htmlspecialchars($key)] = filterArray($value); } else { - $sanArray[htmlspecialchars($k)] = htmlspecialchars($v); + $outArray[htmlspecialchars($key)] = htmlspecialchars($value); } } - return $sanArray; + return $outArray; } // Protect against XSS attacks