Move add_to_table() to database.php.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-08-06 20:45:56 +02:00
parent 5b9ccecd11
commit 98e5f21373
2 changed files with 59 additions and 53 deletions

View File

@@ -8,12 +8,12 @@
require_once('auth.php');
$type = $_POST['list'];
$list = $_POST['list'];
// Perform all of the authentication for list editing
// when NOT invoked and authenticated from API
if (empty($api)) {
list_verify($type);
list_verify($list);
}
// Only check domains we add to the exact lists.
@@ -23,84 +23,45 @@ if(in_array($list, $check_lists)) {
check_domain();
}
// Escape shell metacharacters
$domains = explode(",",$_POST['domain']);
// Split individual domains into array
$domains = explode(" ",$_POST['domain']);
require_once("func.php");
require_once("database.php");
$GRAVITYDB = getGravityDBFilename();
$db = SQLite3_connect($GRAVITYDB, SQLITE3_OPEN_READWRITE);
function add_to_table($table, $domains, $wildcardstyle=false)
{
global $db;
// Prepare SQLite statememt
$stmt = $db->prepare("INSERT OR IGNORE INTO ".$table." (domain) VALUES (:domain);");
// Return early if we prepare the SQLite statement
if(!$stmt)
{
echo "Failed to prepare statement for ".$table." table.";
echo $sql;
return 0;
}
// Loop over domains and inject the lines into the database
$num = 0;
foreach($domains as $row)
{
if($wildcardstyle)
$line = "(\\.|^)".str_replace(".","\\.",$row)."$";
else
$line = $row;
$stmt->bindValue(":domain", $line, SQLITE3_TEXT);
if($stmt->execute() && $stmt->reset() && $stmt->clear())
$num++;
else
{
$stmt->close();
return "Error, added: ".$num."\n";
}
}
// Close database connection and return number or processed rows
$stmt->close();
return "Success, added: ".$num."\n";
}
switch($type) {
switch($list) {
case "white":
if(isset($_POST["auditlog"]))
echo add_to_table("domain_audit", $domains);
echo add_to_table("whitelist", $domains);
echo add_to_table($db, "domain_audit", $domains);
echo add_to_table($db, "whitelist", $domains);
break;
case "black":
if(isset($_POST["auditlog"]))
echo add_to_table("domain_audit", $domains);
echo add_to_table("blacklist", $domains);
echo add_to_table($db, "domain_audit", $domains);
echo add_to_table($db, "blacklist", $domains);
break;
case "black_regex":
echo add_to_table("regex_blacklist", $domains);
echo add_to_table($db, "regex_blacklist", $domains);
break;
case "white_regex":
echo add_to_table("regex_whitelist", $domains);
echo add_to_table($db, "regex_whitelist", $domains);
break;
case "black_wild":
echo add_to_table("regex_blacklist", $domains, true);
echo add_to_table($db, "regex_blacklist", $domains, true);
break;
case "white_wild":
echo add_to_table("regex_whitelist", $domains, true);
echo add_to_table($db, "regex_whitelist", $domains, true);
break;
case "audit":
echo add_to_table("domain_audit", $domains);
echo add_to_table($db, "domain_audit", $domains);
break;
}
?>