From 8819825dc8a7b8b0326433a4fa55377eede62ced Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Thu, 4 Jul 2019 11:51:42 -0700 Subject: [PATCH] Upgrade the exporter/importer (teleporter) to use the gravity database Also fixed the content type of the tar.gz archive from zip to gzip. Signed-off-by: Mcat12 --- scripts/pi-hole/php/teleporter.php | 43 +++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 9cf280bd..9b03a788 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -8,12 +8,15 @@ require "password.php"; require "auth.php"; // Also imports func.php +require "database.php"; if (php_sapi_name() !== "cli") { if(!$auth) die("Not authorized"); check_csrf(isset($_POST["token"]) ? $_POST["token"] : ""); } +$db = SQLite3_connect(getGravityDBFilename()); + function archive_add_file($path,$name,$subdir="") { global $archive; @@ -21,6 +24,27 @@ function archive_add_file($path,$name,$subdir="") $archive[$subdir.$name] = file_get_contents($path.$name); } +/** + * Add the contents of a table to the archive + * + * @param $name string The name of the file in the archive to save the table to + * @param $table string The table to export + * @param $column string The column on the table to export + */ +function archive_add_table($name, $table, $column) +{ + global $archive, $db; + + $results = $db->query("SELECT $column FROM $table"); + $content = ""; + + while($row = $results->fetchArray()) { + $content .= $row[0]."\n"; + } + + $archive[$name] = $content; +} + function archive_add_directory($path,$subdir="") { if($dir = opendir($path)) @@ -125,11 +149,12 @@ if(isset($_POST["action"])) if(isset($_POST["regexlist"]) && $file->getFilename() === "regex.list") { - $regexraw = file_get_contents($file); - $regexlist = process_file($regexraw,false); + $regexlist = process_file(file_get_contents($file),false); echo "Processing regex.list (".count($regexlist)." entries)
\n"; - // NULL = overwrite (or create) the regex filter file - add_regex($regexraw, NULL,""); + + $escapedRegexlist = array_map("escapeshellcmd", $regexlist); + exec("sudo pihole --regex -nr --nuke"); + exec("sudo pihole --regex -q -nr ".implode(" ", $escapedRegexlist)); $importedsomething = true; } @@ -176,19 +201,19 @@ else exit("cannot open/create ".htmlentities($archive_file_name)."
\nPHP user: ".exec('whoami')."\n"); } - archive_add_file("/etc/pihole/","whitelist.txt"); - archive_add_file("/etc/pihole/","blacklist.txt"); - archive_add_file("/etc/pihole/","adlists.list"); + archive_add_table("whitelist.txt", "whitelist", "domain"); + archive_add_table("blacklist.txt", "blacklist", "domain"); + archive_add_table("regex.list", "regex", "domain"); + archive_add_table("adlists.list", "adlist", "address"); archive_add_file("/etc/pihole/","setupVars.conf"); archive_add_file("/etc/pihole/","auditlog.list"); - archive_add_file("/etc/pihole/","regex.list"); archive_add_directory("/etc/dnsmasq.d/","dnsmasq.d/"); $archive->compress(Phar::GZ); // Creates a gziped copy unlink($archive_file_name); // Unlink original tar file as it is not needed anymore $archive_file_name .= ".gz"; // Append ".gz" extension to ".tar" - header("Content-type: application/zip"); + header("Content-type: application/gzip"); header('Content-Transfer-Encoding: binary'); header("Content-Disposition: attachment; filename=".$filename); header("Content-length: " . filesize($archive_file_name));