From 0656526d434bd6e16a01e0e2c1918e00e6cb21b2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 23 Dec 2016 12:49:54 +0100 Subject: [PATCH 01/15] Add blocking version of pihole.log tailing --- header.php | 6 ++++++ js/pihole/taillog.js | 30 ++++++++++++++++++++++++++++++ php/taillog.php | 23 +++++++++++++++++++++++ taillog.php | 16 ++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 js/pihole/taillog.js create mode 100644 php/taillog.php create mode 100644 taillog.php diff --git a/header.php b/header.php index 39146967..eb5a0940 100644 --- a/header.php +++ b/header.php @@ -353,6 +353,12 @@ Query adlists + +
  • + + Tail pihole.log + +
  • diff --git a/taillog.php b/taillog.php new file mode 100644 index 00000000..fd328837 --- /dev/null +++ b/taillog.php @@ -0,0 +1,16 @@ + + + + + + + + + + From 1009dba240148c46ccf69bcac97bde3e3b5bfa42 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 23 Dec 2016 13:39:29 +0100 Subject: [PATCH 02/15] 100% non-blocking using our awesome API --- api.php | 4 ++++ data.php | 27 ++++++++++++++++++++++++++ js/pihole/taillog.js | 45 ++++++++++++++++++++++---------------------- php/taillog.php | 23 ---------------------- taillog.php | 2 +- 5 files changed, 54 insertions(+), 47 deletions(-) delete mode 100644 php/taillog.php diff --git a/api.php b/api.php index 150dc9e2..7d2ee8e9 100644 --- a/api.php +++ b/api.php @@ -80,6 +80,10 @@ $data = array_merge($data, getGravity()); } + if (isset($_GET['tailLog'])) { + $data = array_merge($data, tailPiholeLog($_GET['tailLog'])); + } + function filterArray(&$inArray) { $outArray = array(); foreach ($inArray as $key=>$value) { diff --git a/data.php b/data.php index 5cc0fdfd..2e7ec397 100644 --- a/data.php +++ b/data.php @@ -293,6 +293,33 @@ return $allQueries; } + function tailPiholeLog($param) { + // Not using SplFileObject here, since direct + // usage of f-streams will be much faster for + // files as large as the pihole.log + global $logListName; + $file = fopen($logListName,"r"); + $offset = intval($param); + if($offset > 0) + { + // Seeks on the file pointer where we want to continue reading is known + fseek($file, $offset); + $lines = []; + while (!feof($file)) { + array_push($lines,fgets($file)); + } + return ["offset" => ftell($file), "lines" => $lines]; + } + else + { + // Locate the current position of the file read/write pointer + fseek($file, -1, SEEK_END); + // Add one to skip the very last "\n" in the log file + return ["offset" => ftell($file)+1]; + } + fclose($file); + } + /******** Private Members ********/ function gravityCount() { global $gravityListName,$blackListFile; diff --git a/js/pihole/taillog.js b/js/pihole/taillog.js index 0ae63f41..e6682761 100644 --- a/js/pihole/taillog.js +++ b/js/pihole/taillog.js @@ -1,30 +1,29 @@ -var source; -function eventsource() { - var ta = $("#output"); - source = new EventSource("php/taillog.php"); +var offset, timer, pre; - ta.html(""); - ta.show(); +// Check every 200msec for fresh data +var interval = 200; - source.addEventListener("message", function(e) { - if(e.data.indexOf("Ctrl-C") === -1) - { - // Hide the "Press Ctrl-C to exit" message - ta.append(e.data); - } - - }, false); - - // Will be called when script has finished - source.addEventListener("error", function(e) { - source.close(); - }, false); +// Function that asks the API for new data +function reloadData(){ + clearTimeout(timer); + $.getJSON("api.php?tailLog="+offset, function (data) + { + offset = data["offset"]; + pre.append(data["lines"]); + console.log(offset); + }); + timer = setTimeout(reloadData, interval); } $(function(){ - eventsource(); + // Get offset at first loading of page + $.getJSON("api.php?tailLog", function (data) + { + offset = data["offset"]; + console.log(offset); + }); + pre = $("#output"); + // Trigger function that looks for new data + reloadData(); }); -$(window).unload(function(){ - source.close(); -}); diff --git a/php/taillog.php b/php/taillog.php deleted file mode 100644 index 210397d4..00000000 --- a/php/taillog.php +++ /dev/null @@ -1,23 +0,0 @@ - diff --git a/taillog.php b/taillog.php index fd328837..10800fc1 100644 --- a/taillog.php +++ b/taillog.php @@ -6,7 +6,7 @@

    Output the last lines of the pihole.log file (live)

    - +
    
     
     
    Date: Fri, 23 Dec 2016 13:53:18 +0100
    Subject: [PATCH 03/15] Add auto scrolling feature
    
    ---
     js/pihole/taillog.js | 39 +++++++++++++++++++++++++--------------
     taillog.php          |  2 ++
     2 files changed, 27 insertions(+), 14 deletions(-)
    
    diff --git a/js/pihole/taillog.js b/js/pihole/taillog.js
    index e6682761..0324c395 100644
    --- a/js/pihole/taillog.js
    +++ b/js/pihole/taillog.js
    @@ -1,29 +1,40 @@
    -var offset, timer, pre;
    +var offset, timer, pre, scrolling = true;
     
     // Check every 200msec for fresh data
     var interval = 200;
     
     // Function that asks the API for new data
     function reloadData(){
    -  clearTimeout(timer);
    -  $.getJSON("api.php?tailLog="+offset, function (data)
    +    clearTimeout(timer);
    +    $.getJSON("api.php?tailLog="+offset, function (data)
         {
    -      offset = data["offset"];
    -      pre.append(data["lines"]);
    -      console.log(offset);
    +        offset = data["offset"];
    +        pre.append(data["lines"]);
         });
    -  timer = setTimeout(reloadData, interval);
    +
    +    if(scrolling)
    +    {
    +        window.scrollTo(0,document.body.scrollHeight);
    +    }
    +    timer = setTimeout(reloadData, interval);
     }
     
     $(function(){
    -  // Get offset at first loading of page
    -  $.getJSON("api.php?tailLog", function (data)
    +    // Get offset at first loading of page
    +    $.getJSON("api.php?tailLog", function (data)
         {
    -      offset = data["offset"];
    -      console.log(offset);
    +        offset = data["offset"];
         });
    -  pre = $("#output");
    -  // Trigger function that looks for new data
    -  reloadData();
    +    pre = $("#output");
    +    // Trigger function that looks for new data
    +    reloadData();
     });
     
    +$('#chk1').click(function() {
    +    $("#chk2").prop("checked",this.checked);
    +    scrolling = this.checked;
    +});
    +$('#chk2').click(function() {
    +    $("#chk1").prop("checked",this.checked);
    +    scrolling = this.checked;
    +});
    diff --git a/taillog.php b/taillog.php
    index 10800fc1..6ce2c4be 100644
    --- a/taillog.php
    +++ b/taillog.php
    @@ -6,7 +6,9 @@
         

    Output the last lines of the pihole.log file (live)

    +
    
    +
    Date: Fri, 23 Dec 2016 13:55:29 +0100 Subject: [PATCH 04/15] Adjusted wording slightly --- taillog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/taillog.php b/taillog.php index 6ce2c4be..16b46801 100644 --- a/taillog.php +++ b/taillog.php @@ -6,9 +6,9 @@

    Output the last lines of the pihole.log file (live)

    -
    +
    
    -
    +
    Date: Fri, 23 Dec 2016 13:56:24 +0100 Subject: [PATCH 05/15] Strings must use doublequote. --- js/pihole/taillog.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/pihole/taillog.js b/js/pihole/taillog.js index 0324c395..00cbfb15 100644 --- a/js/pihole/taillog.js +++ b/js/pihole/taillog.js @@ -30,11 +30,11 @@ $(function(){ reloadData(); }); -$('#chk1').click(function() { +$("#chk1").click(function() { $("#chk2").prop("checked",this.checked); scrolling = this.checked; }); -$('#chk2').click(function() { +$("#chk2").click(function() { $("#chk1").prop("checked",this.checked); scrolling = this.checked; }); From f3cc33a140ced7e3ee6831ddbb2a5019eab7b520 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 23 Dec 2016 14:03:09 +0100 Subject: [PATCH 06/15] Fixed one comment --- header.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/header.php b/header.php index eb5a0940..1526ce24 100644 --- a/header.php +++ b/header.php @@ -353,7 +353,7 @@ Query adlists - +
  • Tail pihole.log From dc95f8db56a74c315dfb844c52f492a9ec6dfeb9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Fri, 23 Dec 2016 14:12:40 +0100 Subject: [PATCH 07/15] Add Tools submenu --- header.php | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/header.php b/header.php index 39146967..beb265b0 100644 --- a/header.php +++ b/header.php @@ -341,17 +341,27 @@ Blacklist
  • - -
  • - - Update Lists - -
  • - -
  • - - Query adlists - +
  • + + Tools + + + + +
  • Date: Fri, 23 Dec 2016 14:17:30 +0100 Subject: [PATCH 08/15] Add active class to currently active main nav entry --- header.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/header.php b/header.php index beb265b0..3b7b9e8c 100644 --- a/header.php +++ b/header.php @@ -314,34 +314,48 @@ +