From 870ab035fcd346e9c3e8ca6e83f5b869e31bd6d6 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 28 Jan 2020 22:26:19 +0100 Subject: [PATCH 1/2] Fix teleporter's ability to import v4.x archives. Signed-off-by: DL6ER --- scripts/pi-hole/php/teleporter.php | 60 ++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 7c34c47f..d43ee28b 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -185,7 +185,7 @@ function archive_restore_table($file, $table, $flush=false) /** * Create table rows from an uploaded archive file * - * @param $file object The file of the file in the archive to import + * @param $file object The file in the archive to import * @param $table string The target table * @param $flush boolean Whether to flush the table before importing the archived data * @param $wildcardstyle boolean Whether to format the input domains in legacy wildcard notation @@ -193,22 +193,66 @@ function archive_restore_table($file, $table, $flush=false) */ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=false) { - global $db, $flushed_tables; + global $db; $domains = array_filter(explode("\n",file_get_contents($file))); // Return early if we cannot extract the lines in the file if(is_null($domains)) return 0; - // Flush table if requested, only flush each table once - if($flush && !in_array($table, $flushed_tables)) - { - $db->exec("DELETE FROM ".$table); - array_push($flushed_tables, $table); + // Generate comment + $prefix = "phar:///tmp/"; + if (substr($file, 0, strlen($prefix)) == $prefix) { + $file = substr($file, strlen($prefix)); + } + $comment = "Imported from ".$file; + + // Determine table and type to import to + $type = null; + if($table === "whitelist") { + $table = "domainlist"; + $type = ListType::whitelist; + } else if($table === "blacklist") { + $table = "domainlist"; + $type = ListType::blacklist; + } else if($table === "regex_blacklist") { + $table = "domainlist"; + $type = ListType::regex_blacklist; + } else if($table === "domain_audit") { + $table = "domain_audit"; + } + + // Flush table if requested + if($flush) { + flush_table($table, $type); } // Add domains to requested table - return add_to_table($db, $table, $domains, $wildcardstyle, true); + return add_to_table($db, $table, $domains, $comment, $wildcardstyle, true, $type); +} + +/** + * Flush table if requested. This subroutine flushes each table only once + * + * @param $table string The target table + * @param $type integer Type of item to flush in table (applies only to domainlist table) + */ +function flush_table($table, $type=null) +{ + global $db, $flushed_tables; + + if(!in_array($table, $flushed_tables)) + { + if($type !== null) { + $sql = "DELETE FROM ".$table." WHERE type = ".$type; + array_push($flushed_tables, $table.$type); + } else { + $sql = "DELETE FROM ".$table; + array_push($flushed_tables, $table); + } + echo $sql."
"; + $db->exec($sql); + } } function archive_add_directory($path,$subdir="") From 6ad2430f9580c8019de695b2a49bb7b52b105518 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 2 Feb 2020 19:10:26 +0100 Subject: [PATCH 2/2] Import legacy adlist.list and fix/remove debugging output Signed-off-by: DL6ER --- scripts/pi-hole/php/database.php | 18 ++++++++++++++---- scripts/pi-hole/php/teleporter.php | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/scripts/pi-hole/php/database.php b/scripts/pi-hole/php/database.php index 2bad73db..cf250d13 100644 --- a/scripts/pi-hole/php/database.php +++ b/scripts/pi-hole/php/database.php @@ -107,6 +107,16 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re return "Error: Unable to begin transaction for $table table."; } + // To which column should the record be added to? + if ($table === "adlist") + { + $field = "address"; + } + else + { + $field = "domain"; + } + // Get initial count of domains in this table if($type === -1) { @@ -121,11 +131,11 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re // Prepare INSERT SQLite statememt if($type === -1) { - $querystr = "INSERT OR IGNORE INTO $table (domain,comment) VALUES (:domain, :comment);"; + $querystr = "INSERT OR IGNORE INTO $table ($field,comment) VALUES (:$field, :comment);"; } else { - $querystr = "INSERT OR IGNORE INTO $table (domain,comment,type) VALUES (:domain, :comment, $type);"; + $querystr = "INSERT OR IGNORE INTO $table ($field,comment,type) VALUES (:$field, :comment, $type);"; } $stmt = $db->prepare($querystr); @@ -135,7 +145,7 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re if($returnnum) return 0; else - return "Error: Failed to prepare statement for $table table (type = $type)."; + return "Error: Failed to prepare statement for $table table (type = $type, field = $field)."; } // Loop over domains and inject the lines into the database @@ -149,7 +159,7 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re if($wildcardstyle) $domain = "(\\.|^)".str_replace(".","\\.",$domain)."$"; - $stmt->bindValue(":domain", $domain, SQLITE3_TEXT); + $stmt->bindValue(":$field", $domain, SQLITE3_TEXT); $stmt->bindValue(":comment", $comment, SQLITE3_TEXT); if($stmt->execute() && $stmt->reset()) diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index d43ee28b..84f96bc8 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -220,6 +220,10 @@ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=f $type = ListType::regex_blacklist; } else if($table === "domain_audit") { $table = "domain_audit"; + $type = -1; // -1 -> not used inside add_to_table() + } else if($table === "adlist") { + $table = "adlist"; + $type = -1; // -1 -> not used inside add_to_table() } // Flush table if requested @@ -250,7 +254,6 @@ function flush_table($table, $type=null) $sql = "DELETE FROM ".$table; array_push($flushed_tables, $table); } - echo $sql."
"; $db->exec($sql); } } @@ -355,7 +358,14 @@ if(isset($_POST["action"])) if(isset($_POST["auditlog"]) && $file->getFilename() === "auditlog.list") { $num = archive_insert_into_table($file, "domain_audit", $flushtables); - echo "Processed blacklist (regex) (".$num." entries)
\n"; + echo "Processed audit log (".$num." entries)
\n"; + $importedsomething = true; + } + + if(isset($_POST["adlist"]) && $file->getFilename() === "adlists.list") + { + $num = archive_insert_into_table($file, "adlist", $flushtables); + echo "Processed adlists (".$num." entries)
\n"; $importedsomething = true; }