From 07af532b0910bf9d089b5b5ed036751a93ca7841 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 7 Aug 2019 21:32:49 +0200 Subject: [PATCH] Review comments Signed-off-by: DL6ER --- scripts/pi-hole/php/add.php | 2 +- scripts/pi-hole/php/database.php | 8 ++++---- scripts/pi-hole/php/teleporter.php | 32 ++++++++++-------------------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index 94a17345..ff306a68 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -68,6 +68,6 @@ switch($list) { die("Invalid list!"); } -// Reload lists in pihole-FTL after having removed something +// Reload lists in pihole-FTL after having added something echo shell_exec("sudo pihole restartdns reload"); ?> diff --git a/scripts/pi-hole/php/database.php b/scripts/pi-hole/php/database.php index 588aeed6..44b37b31 100644 --- a/scripts/pi-hole/php/database.php +++ b/scripts/pi-hole/php/database.php @@ -74,7 +74,7 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false) // Prepare SQLite statememt $stmt = $db->prepare("INSERT OR IGNORE INTO ".$table." (domain) VALUES (:domain);"); - // Return early if we prepare the SQLite statement + // Return early if we failed to prepare the SQLite statement if(!$stmt) { echo "Failed to prepare statement for ".$table." table."; @@ -103,7 +103,7 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false) } } - // Close database connection and return number of processed rows + // Close prepared statement and return number of processed rows $stmt->close(); return "Success, added: ".$num."\n"; } @@ -121,7 +121,7 @@ function remove_from_table($db, $table, $domains) // Prepare SQLite statememt $stmt = $db->prepare("DELETE FROM ".$table." WHERE domain = :domain;"); - // Return early if we prepare the SQLite statement + // Return early if we failed to prepare the SQLite statement if(!$stmt) { echo "Failed to prepare statement for ".$table." table."; @@ -147,7 +147,7 @@ function remove_from_table($db, $table, $domains) } } - // Close database connection and return number or processed rows + // Close prepared statement and return number or processed rows $stmt->close(); return "Success, removed: ".$num."\n"; } diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 13838d6f..f88f02db 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -87,18 +87,21 @@ function archive_restore_table($file, $table, $flush=false) $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"; } else { $sql = "INSERT OR IGNORE INTO ".$table; $sql .= " (id,domain,enabled,date_added,comment)"; $sql .= " VALUES (:id,:domain,:enabled,:date_added,:comment);"; + $field = "domain"; } // Prepare SQLite statememt @@ -112,22 +115,17 @@ function archive_restore_table($file, $table, $flush=false) return 0; } - // Loop over rows and inject the lines into the database + // Loop over rows and inject the entries into the database $num = 0; foreach($contents as $row) { - if($table === "adlist") - $line = "address"; - else - $line = "domain"; - // Limit max length for a domain entry to 253 chars - if(strlen($row[$line]) > 253) + if(strlen($row[$field]) > 253) continue; $stmt->bindValue(":id", $row["id"], SQLITE3_INTEGER); $stmt->bindValue(":date_added", $row["date_added"], SQLITE3_INTEGER); - $stmt->bindValue(":".$line, $row[$line], SQLITE3_TEXT); + $stmt->bindValue(":".$field, $row[$field], SQLITE3_TEXT); if($table !== "domain_audit") { @@ -180,17 +178,12 @@ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=f // Prepare field name for domain/address depending on the table we restore to if($table === "adlist") - { - $sql = "INSERT OR IGNORE INTO adlist"; - $sql .= " (address) VALUES (:address);"; - } + $field = "address"; else - { - $sql = "INSERT OR IGNORE INTO ".$table; - $sql .= " (domain) VALUES (:domain);"; - } + $field = "domain"; - // Prepare SQLite statememt + // Prepare SQLite statement + $sql = "INSERT OR IGNORE INTO ".$table." (".$field.") VALUES (:".$field.");"; $stmt = $db->prepare($sql); // Return early if we prepare the SQLite statement @@ -214,10 +207,7 @@ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=f if(strlen($line) > 253) continue; - if($table === "adlist") - $stmt->bindValue(":address", $line, SQLITE3_TEXT); - else - $stmt->bindValue(":domain", $line, SQLITE3_TEXT); + $stmt->bindValue(":".$field, $line, SQLITE3_TEXT); if($stmt->execute() && $stmt->reset() && $stmt->clear()) $num++;