diff --git a/scripts/pi-hole/php/tailLog.php b/scripts/pi-hole/php/tailLog.php index e7eef3d2..cdd2b301 100644 --- a/scripts/pi-hole/php/tailLog.php +++ b/scripts/pi-hole/php/tailLog.php @@ -9,40 +9,48 @@ require "password.php"; if(!$auth) die("Not authorized"); +function formatLine($line){ + if (strpos($line, "blacklisted") || strpos($line, "gravity blocked")) { + $txt = ''.htmlspecialchars($line).''; + } elseif (strpos($line, "query[A") || strpos($line, "query[DHCP")) { + $txt = ''.htmlspecialchars($line).''; + } else { + $txt = ''.htmlspecialchars($line).''; + } + return $txt; +} + // Not using SplFileObject here, since direct // usage of f-streams will be much faster for // files as large as the pihole.log -if(isset($_GET["FTL"])) -{ - $file = fopen("/var/log/pihole/FTL.log","r"); -} -else -{ - $file = fopen("/var/log/pihole/pihole.log","r"); +if (isset($_GET["FTL"])) { + $file = fopen("/var/log/pihole/FTL.log","r"); +} else { + $file = fopen("/var/log/pihole/pihole.log","r"); } -if(!$file) -{ - die(json_encode(array("offset" => 0, "lines" => array("Failed to open log file. Check permissions!\n")))); +if (!$file) { + die(json_encode(array("offset" => 0, "lines" => array("Failed to open log file. Check permissions!\n")))); } -if(isset($_GET["offset"])) -{ - $offset = intval($_GET['offset']); - 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, htmlspecialchars(fgets($file))); - die(json_encode(array("offset" => ftell($file), "lines" => $lines))); - } +if (isset($_GET["offset"])) { + $offset = intval($_GET['offset']); + 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, formatLine(fgets($file))); + } + + die(json_encode(array("offset" => ftell($file), "lines" => $lines))); + } } // 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 die(json_encode(array("offset" => ftell($file)+1))); - ?>