Use database.php:add_to_table() in teleporter.php:archive_insert_into_table().

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-08-07 21:49:51 +02:00
parent 838516e558
commit 3770556732
2 changed files with 32 additions and 55 deletions
+28 -8
View File
@@ -67,9 +67,10 @@ function SQLite3_connect($filename, $mode=SQLITE3_OPEN_READONLY)
* @param $table string The target table
* @param $domains array Array of domains (strings) to be added to the table
* @param $wildcardstyle boolean Whether to format the input domains in legacy wildcard notation
* @param $returnnum boolean Whether to return an integer or a string
* @return string Success/error and number of processed domains
*/
function add_to_table($db, $table, $domains, $wildcardstyle=false)
function add_to_table($db, $table, $domains, $wildcardstyle=false, $returnnum=false)
{
// Prepare SQLite statememt
$stmt = $db->prepare("INSERT OR IGNORE INTO ".$table." (domain) VALUES (:domain);");
@@ -78,7 +79,10 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false)
if(!$stmt)
{
echo "Failed to prepare statement for ".$table." table.";
return "Error, added: 0\n";
if($returnnum)
return 0;
else
return "Error, added: 0\n";
}
// Loop over domains and inject the lines into the database
@@ -95,13 +99,19 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false)
else
{
$stmt->close();
return "Error, added: ".$num."\n";
if($returnnum)
return $num;
else
return "Error, added: ".$num."\n";
}
}
// Close prepared statement and return number of processed rows
$stmt->close();
return "Success, added: ".$num."\n";
if($returnnum)
return $num;
else
return "Success, added: ".$num."\n";
}
/**
@@ -110,9 +120,10 @@ function add_to_table($db, $table, $domains, $wildcardstyle=false)
* @param $db object The SQLite3 database connection object
* @param $table string The target table
* @param $domains array Array of domains (strings) to be removed from the table
* @param $returnnum boolean Whether to return an integer or a string
* @return string Success/error and number of processed domains
*/
function remove_from_table($db, $table, $domains)
function remove_from_table($db, $table, $domains, $returnnum=false)
{
// Prepare SQLite statememt
$stmt = $db->prepare("DELETE FROM ".$table." WHERE domain = :domain;");
@@ -121,7 +132,10 @@ function remove_from_table($db, $table, $domains)
if(!$stmt)
{
echo "Failed to prepare statement for ".$table." table.";
return "Error, added: 0\n";
if($returnnum)
return 0;
else
return "Error, added: 0\n";
}
// Loop over domains and remove the lines from the database
@@ -135,13 +149,19 @@ function remove_from_table($db, $table, $domains)
else
{
$stmt->close();
return "Error, removed: ".$num."\n";
if($returnnum)
return $num;
else
return "Error, removed: ".$num."\n";
}
}
// Close prepared statement and return number or processed rows
$stmt->close();
return "Success, removed: ".$num."\n";
if($returnnum)
return $num;
else
return "Success, removed: ".$num."\n";
}
?>
+4 -47
View File
@@ -164,9 +164,9 @@ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=f
{
global $db, $flushed_tables;
$rows = array_filter(explode("\n",file_get_contents($file)));
$domains = array_filter(explode("\n",file_get_contents($file)));
// Return early if we cannot extract the lines in the file
if(is_null($rows))
if(is_null($domains))
return 0;
// Flush table if requested, only flush each table once
@@ -176,51 +176,8 @@ function archive_insert_into_table($file, $table, $flush=false, $wildcardstyle=f
array_push($flushed_tables, $table);
}
// Prepare field name for domain/address depending on the table we restore to
if($table === "adlist")
$field = "address";
else
$field = "domain";
// Prepare SQLite statement
$sql = "INSERT OR IGNORE INTO ".$table." (".$field.") VALUES (:".$field.");";
$stmt = $db->prepare($sql);
// Return early if we prepare the SQLite statement
if(!$stmt)
{
echo "Failed to prepare statement for ".$table." table.";
echo $sql;
return 0;
}
// Loop over rows and inject the lines into the database
$num = 0;
foreach($rows as $row)
{
if($wildcardstyle)
$line = "(\\.|^)".str_replace(".","\\.",$row)."$";
else
$line = $row;
// Limit max length for a domain entry to 253 chars
if(strlen($line) > 253)
continue;
$stmt->bindValue(":".$field, $line, SQLITE3_TEXT);
if($stmt->execute() && $stmt->reset() && $stmt->clear())
$num++;
else
{
$stmt->close();
return $num;
}
}
// Close database connection and return number or processed rows
$stmt->close();
return $num;
// Add domains to requested table
return add_to_table($db, $table, $domains, $wildcardstyle, true);
}
function archive_add_directory($path,$subdir="")