Skip to content

Commit

Permalink
Adding option to show all results for queryads (#2487)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdwebdesign committed Jan 14, 2023
2 parents 56ab0bd + 839836d commit 854e7e9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
8 changes: 7 additions & 1 deletion queryads.php
Expand Up @@ -17,7 +17,7 @@
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-body">
<div class="box-header">
<!-- domain-search-block - Single search field mobile/desktop -->
<div id="domain-search-block" class="input-group">
<input id="domain" type="url" class="form-control" placeholder="Domain to look for (example.com or sub.example.com)" autocomplete="off" spellcheck="false" autocapitalize="none" autocorrect="off">
Expand All @@ -27,7 +27,13 @@
<button type="button" id="btnSearchExact" class="btn btn-default">Search exact match</button>
</span>
</div>
</div>
<div class="box-body">
<!-- /domain-search-block -->
<div id="limitbox-block">
<input type="checkbox" id="show-all">
<label for="show-all"><strong>Show unlimited results.</strong> <br class="hidden-md hidden-lg">This can be very slow if too many domains are returned. <span class="text-red">Use with caution</span>.</label></i>
</div>
</div>
</div>
</div>
Expand Down
48 changes: 14 additions & 34 deletions scripts/pi-hole/js/queryads.js
Expand Up @@ -6,56 +6,39 @@
* Please see LICENSE file for your rights under this license. */

var exact = "";

function quietfilter(ta, data) {
var lines = data.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i].indexOf("results") !== -1 && lines[i].indexOf("0 results") === -1) {
var shortstring = lines[i].replace("::: /etc/pihole/", "");
// Remove "(x results)"
shortstring = shortstring.replace(/\(.*/, "");
ta.append(shortstring + "\n");
}
}
}
var showAll = "";

function eventsource() {
var ta = $("#output");
// process with the current visible domain input field
var domain = $("input[id^='domain']:visible").val().trim();
var q = $("#quiet");
var domain = $("input[id^='domain']:visible").val().trim().toLowerCase();
var unlimited = $("#show-all").is(":checked");

if (domain.length === 0) {
return;
}

var quiet = false;
if (q.val() === "yes") {
quiet = true;
exact = "exact";
if (unlimited === true) {
showAll = "&showall";
}

var queryURL = "scripts/pi-hole/php/queryads.php?domain=" + domain + exact + showAll;

// IE does not support EventSource - load whole content at once
if (typeof EventSource !== "function") {
$.ajax({
method: "GET",
url: "scripts/pi-hole/php/queryads.php?domain=" + domain.toLowerCase() + "&" + exact + "&IE",
url: queryURL + "&IE",
async: false,
}).done(function (data) {
ta.show();
ta.empty();
if (!quiet) {
ta.append(data);
} else {
quietfilter(ta, data);
}
ta.append(data);
});
return;
}

var source = new EventSource(
"scripts/pi-hole/php/queryads.php?domain=" + domain.toLowerCase() + "&" + exact
);
var source = new EventSource(queryURL);

// Reset and show field
ta.empty();
Expand All @@ -64,11 +47,7 @@ function eventsource() {
source.addEventListener(
"message",
function (e) {
if (!quiet) {
ta.append(e.data);
} else {
quietfilter(ta, e.data);
}
ta.append(e.data);
},
false
);
Expand All @@ -82,8 +61,9 @@ function eventsource() {
false
);

// Reset exact variable
// Reset option variables
exact = "";
showAll = "";
}

// Handle enter key
Expand All @@ -100,7 +80,7 @@ $("button[id^='btnSearch']").on("click", function () {
exact = "";

if (this.id.match("^btnSearchExact")) {
exact = "exact";
exact = "&exact";
}

eventsource();
Expand Down
28 changes: 17 additions & 11 deletions scripts/pi-hole/php/queryads.php
Expand Up @@ -22,13 +22,18 @@
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

function echoEvent($datatext)
function echoEvent($datatext, $url = '')
{
if (!isset($_GET['IE'])) {
echo 'data:'.implode("\ndata:", explode("\n", $datatext))."\n\n";
$txt = 'data:'.implode("\ndata:", explode("\n", $datatext))."\n\n";
} else {
echo $datatext;
$txt = $datatext;
}

$txt = str_replace('This can be overridden using the -all option', 'Select the checkbox to remove the limitation', $txt);
$txt = str_replace($url, '<strong class="text-blue">'.$url.'</strong>', $txt);

echo $txt;
}

// Test if domain is set
Expand All @@ -37,7 +42,7 @@ function echoEvent($datatext)
// Convert domain name to IDNA ASCII form for international domains
$url = convertUnicodeToIDNA($_GET['domain']);
if (!validDomain($url)) {
echoEvent(htmlentities($url).' is an invalid domain!');
echoEvent(htmlentities($url).' is an invalid domain!', $url);

exit;
}
Expand All @@ -47,15 +52,16 @@ function echoEvent($datatext)
exit;
}

$options = '';
if (isset($_GET['exact'])) {
$exact = '-exact';
} elseif (isset($_GET['bp'])) {
$exact = '-bp';
} else {
$exact = '';
$options .= ' -exact';
}

if (isset($_GET['showall'])) {
$options .= ' -all';
}

$proc = popen('sudo pihole -q -adlist '.$url.' '.$exact, 'r');
$proc = popen('sudo pihole -q '.$url.$options, 'r');
while (!feof($proc)) {
echoEvent(fread($proc, 4096));
echoEvent(fread($proc, 4096), $url);
}
5 changes: 2 additions & 3 deletions style/pi-hole.css
Expand Up @@ -530,11 +530,10 @@ td.details-control {
margin: 5px 0;
}
#domain-search-block .input-group-btn {
margin: 5px 0;
text-align: center;
display: inline-block;
}
#domain-search-block .input-group-btn button {
margin: 0 2px;
margin: 0 5px 0 0;
border-radius: 3px;
}
}
Expand Down

0 comments on commit 854e7e9

Please sign in to comment.