From cb48efff195964fed6e64612f885e60e42ee68c9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 12 May 2020 20:01:57 +0200 Subject: [PATCH 1/8] Add export for missing tables: group, client + the group linking tables. Signed-off-by: DL6ER --- scripts/pi-hole/php/teleporter.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index e746e6c0..81adb3d3 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -40,11 +40,11 @@ function archive_add_table($name, $table, $type=-1) if($type > -1) { - $querystr = "SELECT * FROM $table WHERE type = $type;"; + $querystr = "SELECT * FROM \"$table\" WHERE type = $type;"; } else { - $querystr = "SELECT * FROM $table;"; + $querystr = "SELECT * FROM \"$table\";"; } $results = $db->query($querystr); @@ -248,10 +248,10 @@ function flush_table($table, $type=null) if(!in_array($table, $flushed_tables)) { if($type !== null) { - $sql = "DELETE FROM ".$table." WHERE type = ".$type; + $sql = "DELETE FROM \"".$table."\" WHERE type = ".$type; array_push($flushed_tables, $table.$type); } else { - $sql = "DELETE FROM ".$table; + $sql = "DELETE FROM \"".$table."\""; array_push($flushed_tables, $table); } $db->exec($sql); @@ -467,6 +467,14 @@ else archive_add_table("blacklist.regex.json", "domainlist", ListType::regex_blacklist); archive_add_table("adlist.json", "adlist"); archive_add_table("domain_audit.json", "domain_audit"); + archive_add_table("group.json", "group"); + archive_add_table("client.json", "client"); + + // Group linking tables + archive_add_table("domainlist_by_group.json", "domainlist_by_group"); + archive_add_table("adlist_by_group.json", "adlist_by_group"); + archive_add_table("client_by_group.json", "client_by_group"); + archive_add_file("/etc/pihole/","setupVars.conf"); archive_add_file("/etc/pihole/","dhcp.leases"); archive_add_file("/etc/","hosts","etc/"); From ad4a10c01e0d00dece87a39ced2f7b0d24601270 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 12 May 2020 20:09:59 +0200 Subject: [PATCH 2/8] Add import for missing tables: group, client + the group linking tables. Signed-off-by: DL6ER --- scripts/pi-hole/php/teleporter.php | 109 +++++++++++++++++++++++------ settings.php | 12 +++- 2 files changed, 98 insertions(+), 23 deletions(-) diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 81adb3d3..606d3a57 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -87,31 +87,58 @@ function archive_restore_table($file, $table, $flush=false) // Flush table if requested, only flush each table once if($flush && !in_array($table, $flushed_tables)) { - $db->exec("DELETE FROM ".$table); + $db->exec("DELETE FROM \"".$table."\""); array_push($flushed_tables, $table); } - // Prepare field name for domain/address depending on the table we restore to + // Prepare fields depending on the table we restore to if($table === "adlist") { $sql = "INSERT OR IGNORE INTO adlist"; $sql .= " (id,address,enabled,date_added,comment)"; $sql .= " VALUES (:id,:address,:enabled,:date_added,:comment);"; - $field = "address"; } elseif($table === "domain_audit") { $sql = "INSERT OR IGNORE INTO domain_audit"; $sql .= " (id,domain,date_added)"; $sql .= " VALUES (:id,:domain,:date_added);"; - $field = "domain"; } elseif($table === "domainlist") { $sql = "INSERT OR IGNORE INTO domainlist"; $sql .= " (id,domain,enabled,date_added,comment,type)"; $sql .= " VALUES (:id,:domain,:enabled,:date_added,:comment,:type);"; - $field = "domain"; + } + elseif($table === "group") + { + $sql = "INSERT OR IGNORE INTO \"group\""; + $sql .= " (id,name,date_added,description)"; + $sql .= " VALUES (:id,:name,:date_added,:description);"; + } + elseif($table === "client") + { + $sql = "INSERT OR IGNORE INTO client"; + $sql .= " (id,ip,date_added,comment)"; + $sql .= " VALUES (:id,:ip,:date_added,:comment);"; + } + elseif($table === "domainlist_by_group") + { + $sql = "INSERT OR IGNORE INTO domainlist_by_group"; + $sql .= " (domainlist_id,group_id)"; + $sql .= " VALUES (:domainlist_id,:group_id);"; + } + elseif($table === "client_by_group") + { + $sql = "INSERT OR IGNORE INTO client_by_group"; + $sql .= " (client_id,group_id)"; + $sql .= " VALUES (:client_id,:group_id);"; + } + elseif($table === "adlist_by_group") + { + $sql = "INSERT OR IGNORE INTO adlist_by_group"; + $sql .= " (adlist_id,group_id)"; + $sql .= " VALUES (:adlist_id,:group_id);"; } else { @@ -149,23 +176,27 @@ function archive_restore_table($file, $table, $flush=false) if(strlen($row[$field]) > 253) continue; - $stmt->bindValue(":id", $row["id"], SQLITE3_INTEGER); - $stmt->bindValue(":date_added", $row["date_added"], SQLITE3_INTEGER); - $stmt->bindValue(":".$field, $row[$field], SQLITE3_TEXT); - - if($table !== "domain_audit") - { - $stmt->bindValue(":enabled", $row["enabled"], SQLITE3_INTEGER); - if(is_null($row["comment"])) - $type = SQLITE3_NULL; - else - $type = SQLITE3_TEXT; - $stmt->bindValue(":comment", $row["comment"], $type); - } - - if($table === "domainlist") - { - $stmt->bindValue(":type", $row["type"], SQLITE3_INTEGER); + // Bind properties from JSON data + // Note that only defined above are actually used + // so even maliciously modified Teleporter files + // cannot be dangerous in any way + foreach($row as $key => $value) { + $type = gettype($value); + $sqltype=NULL; + switch($type) { + case "integer": + $sqltype = SQLITE3_INTEGER; + break; + case "string": + $sqltype = SQLITE3_TEXT; + break; + case "NULL": + $sqltype = SQLITE3_NULL; + break; + default: + $sqltype = "UNK"; + } + $stmt->bindValue(":".$key, $value, $sqltype); } if($stmt->execute() && $stmt->reset() && $stmt->clear()) @@ -411,6 +442,40 @@ if(isset($_POST["action"])) $importedsomething = true; } + if(isset($_POST["group"]) && $file->getFilename() === "group.json") + { + $num = archive_restore_table($file, "group", $flushtables); + echo "Processed group (".$num." entries)
\n"; + $importedsomething = true; + } + + if(isset($_POST["client"]) && $file->getFilename() === "client.json") + { + $num = archive_restore_table($file, "client", $flushtables); + echo "Processed client (".$num." entries)
\n"; + $importedsomething = true; + } + + if(isset($_POST["client"]) && $file->getFilename() === "client_by_group.json") + { + $num = archive_restore_table($file, "client_by_group", $flushtables); + $importedsomething = true; + } + + if((isset($_POST["whitelist"]) || isset($_POST["regex_whitelist"]) || + isset($_POST["blacklist"]) || isset($_POST["regex_blacklist"])) && + $file->getFilename() === "domainlist_by_group.json") + { + $num = archive_restore_table($file, "domainlist_by_group", $flushtables); + $importedsomething = true; + } + + if(isset($_POST["adlist"]) && $file->getFilename() === "adlist_by_group.json") + { + $num = archive_restore_table($file, "adlist_by_group", $flushtables); + $importedsomething = true; + } + if(isset($_POST["staticdhcpleases"]) && $file->getFilename() === "04-pihole-static-dhcp.conf") { if($flushtables) { diff --git a/settings.php b/settings.php index a6e69056..5b34acc7 100644 --- a/settings.php +++ b/settings.php @@ -1118,7 +1118,17 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array("sysadmin", "blocklists"
+ Adlists +
+
+ +
+
+