mirror of
https://github.com/pi-hole/web.git
synced 2025-12-24 20:55:28 +00:00
Implement necessary changes due to switching from individual to a common domainlist.
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
@@ -34,27 +34,27 @@ $db = SQLite3_connect($GRAVITYDB, SQLITE3_OPEN_READWRITE);
|
||||
|
||||
switch($list) {
|
||||
case "white":
|
||||
echo add_to_table($db, "whitelist", $domains, $comment);
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, false, false, 0);
|
||||
break;
|
||||
|
||||
case "black":
|
||||
echo add_to_table($db, "blacklist", $domains, $comment);
|
||||
break;
|
||||
|
||||
case "black_regex":
|
||||
echo add_to_table($db, "regex_blacklist", $domains, $comment);
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, false, false, 1);
|
||||
break;
|
||||
|
||||
case "white_regex":
|
||||
echo add_to_table($db, "regex_whitelist", $domains, $comment);
|
||||
break;
|
||||
|
||||
case "black_wild":
|
||||
echo add_to_table($db, "regex_blacklist", $domains, $comment, true);
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, false, false, 2);
|
||||
break;
|
||||
|
||||
case "white_wild":
|
||||
echo add_to_table($db, "regex_whitelist", $domains, $comment, true);
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, true, false, 2);
|
||||
break;
|
||||
|
||||
case "black_regex":
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, false, false, 3);
|
||||
break;
|
||||
|
||||
case "black_wild":
|
||||
echo add_to_table($db, "domainlist", $domains, $comment, true, false, 3);
|
||||
break;
|
||||
|
||||
case "audit":
|
||||
|
||||
@@ -74,22 +74,48 @@ function SQLite3_connect($filename, $mode=SQLITE3_OPEN_READONLY)
|
||||
* @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
|
||||
* @param $type integer The target type (0 = exact whitelist, 1 = exact blacklist, 2 = regex whitelist, 3 = regex blacklist)
|
||||
* @return string Success/error and number of processed domains
|
||||
*/
|
||||
function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $returnnum=false)
|
||||
function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $returnnum=false, $type=-1)
|
||||
{
|
||||
if(!is_int($type))
|
||||
{
|
||||
return "Error: Argument type has to be of type integer (is ".gettype($type).")";
|
||||
}
|
||||
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for ".$table." table.";
|
||||
return "Error: Unable to begin transaction for $table table.";
|
||||
}
|
||||
$initialcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
|
||||
// Prepare SQLite statememt
|
||||
$stmt = $db->prepare("INSERT OR IGNORE INTO ".$table." (domain,comment) VALUES (:domain, :comment);");
|
||||
// Get initial count of domains in this table
|
||||
if($type === -1)
|
||||
{
|
||||
$countquery = "SELECT COUNT(*) FROM $table;";
|
||||
}
|
||||
else
|
||||
{
|
||||
$countquery = "SELECT COUNT(*) FROM $table WHERE type = $type;";
|
||||
}
|
||||
error_log($countquery);
|
||||
$initialcount = intval($db->querySingle($countquery));
|
||||
|
||||
// Prepare INSERT SQLite statememt
|
||||
if($type === -1)
|
||||
{
|
||||
$querystr = "INSERT OR IGNORE INTO $table (domain,comment) VALUES (:domain, :comment);";
|
||||
}
|
||||
else
|
||||
{
|
||||
$querystr = "INSERT OR IGNORE INTO $table (domain,comment,type) VALUES (:domain, :comment, $type);";
|
||||
}
|
||||
error_log($querystr);
|
||||
$stmt = $db->prepare($querystr);
|
||||
|
||||
// Return early if we failed to prepare the SQLite statement
|
||||
if(!$stmt)
|
||||
@@ -97,7 +123,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.";
|
||||
return "Error: Failed to prepare statement for $table table (type = $type).";
|
||||
}
|
||||
|
||||
// Loop over domains and inject the lines into the database
|
||||
@@ -140,7 +166,7 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
$finalcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
$finalcount = intval($countquery);
|
||||
$modified = $finalcount - $initialcount;
|
||||
|
||||
// If we add less domains than the user specified, then they wanted to add duplicates
|
||||
@@ -169,22 +195,46 @@ function add_to_table($db, $table, $domains, $comment, $wildcardstyle=false, $re
|
||||
* @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
|
||||
* @param $type integer The target type (0 = exact whitelist, 1 = exact blacklist, 2 = regex whitelist, 3 = regex blacklist)
|
||||
* @return string Success/error and number of processed domains
|
||||
*/
|
||||
function remove_from_table($db, $table, $domains, $returnnum=false)
|
||||
function remove_from_table($db, $table, $domains, $returnnum=false, $type=-1)
|
||||
{
|
||||
if(!is_int($type))
|
||||
{
|
||||
return "Error: Argument type has to be of type integer (is ".gettype($type).")";
|
||||
}
|
||||
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for ".$table." table.";
|
||||
return "Error: Unable to begin transaction for domainlist table.";
|
||||
}
|
||||
$initialcount = intval($db->querySingle("SELECT COUNT(*) FROM ".$table.";"));
|
||||
|
||||
// Get initial count of domains in this table
|
||||
if($type === -1)
|
||||
{
|
||||
$countquery = "SELECT COUNT(*) FROM $table;";
|
||||
}
|
||||
else
|
||||
{
|
||||
$countquery = "SELECT COUNT(*) FROM $table WHERE type = $type;";
|
||||
}
|
||||
$initialcount = intval($db->querySingle($countquery));
|
||||
|
||||
// Prepare SQLite statememt
|
||||
$stmt = $db->prepare("DELETE FROM ".$table." WHERE domain = :domain;");
|
||||
if($type === -1)
|
||||
{
|
||||
$querystr = "DELETE FROM $table WHERE domain = :domain AND type = $type;";
|
||||
}
|
||||
else
|
||||
{
|
||||
$querystr = "DELETE FROM $table WHERE domain = :domain;";
|
||||
}
|
||||
$stmt = $db->prepare($querystr);
|
||||
|
||||
// Return early if we failed to prepare the SQLite statement
|
||||
if(!$stmt)
|
||||
@@ -192,7 +242,7 @@ function remove_from_table($db, $table, $domains, $returnnum=false)
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Failed to prepare statement for ".$table." table.";
|
||||
return "Error: Failed to prepare statement for ".$table." table (type = ".$type.").";
|
||||
}
|
||||
|
||||
// Loop over domains and remove the lines from the database
|
||||
|
||||
@@ -17,13 +17,14 @@ require("database.php");
|
||||
$GRAVITYDB = getGravityDBFilename();
|
||||
$db = SQLite3_connect($GRAVITYDB);
|
||||
|
||||
function getTableContent($listname) {
|
||||
function getTableContent($type) {
|
||||
global $db;
|
||||
$entries = array();
|
||||
$querystr = implode(" ",array("SELECT ${listname}.*,\"group\".enabled as group_enabled",
|
||||
"FROM ${listname}",
|
||||
"LEFT JOIN ${listname}_by_group ON ${listname}_by_group.${listname}_id = ${listname}.id",
|
||||
"LEFT JOIN \"group\" ON \"group\".id = ${listname}_by_group.group_id",
|
||||
$querystr = implode(" ",array("SELECT domainlist.*,\"group\".enabled as group_enabled",
|
||||
"FROM domainlist",
|
||||
"LEFT JOIN domainlist_by_group ON domainlist_by_group.domainlist_id = domainlist.id",
|
||||
"LEFT JOIN \"group\" ON \"group\".id = domainlist_by_group.group_id",
|
||||
"WHERE type = $type",
|
||||
"GROUP BY domain;"));
|
||||
$results = $db->query($querystr);
|
||||
|
||||
@@ -32,7 +33,7 @@ function getTableContent($listname) {
|
||||
array_push($entries, $res);
|
||||
}
|
||||
|
||||
return array($listname => $entries);
|
||||
return $entries;
|
||||
}
|
||||
|
||||
function filterArray(&$inArray) {
|
||||
@@ -54,14 +55,14 @@ function filterArray(&$inArray) {
|
||||
switch ($listtype)
|
||||
{
|
||||
case "white":
|
||||
$exact = getTableContent("whitelist");
|
||||
$regex = getTableContent("regex_whitelist");
|
||||
$exact = array("whitelist" => getTableContent(0));
|
||||
$regex = array("regex_whitelist" => getTableContent(2));
|
||||
$list = array_merge($exact, $regex);
|
||||
break;
|
||||
|
||||
case "black":
|
||||
$exact = getTableContent("blacklist");
|
||||
$regex = getTableContent("regex_blacklist");
|
||||
$exact = array("blacklist" => getTableContent(1));
|
||||
$regex = array("regex_blacklist" => getTableContent(3));
|
||||
$list = array_merge($exact, $regex);
|
||||
break;
|
||||
|
||||
|
||||
@@ -27,19 +27,19 @@ $db = SQLite3_connect($GRAVITYDB, SQLITE3_OPEN_READWRITE);
|
||||
|
||||
switch($type) {
|
||||
case "white":
|
||||
echo remove_from_table($db, "whitelist", $domains);
|
||||
echo remove_from_table($db, "domainlist", $domains, false, 0);
|
||||
break;
|
||||
|
||||
case "black":
|
||||
echo remove_from_table($db, "blacklist", $domains);
|
||||
break;
|
||||
|
||||
case "black_regex":
|
||||
echo remove_from_table($db, "regex_blacklist", $domains);
|
||||
echo remove_from_table($db, "domainlist", $domains, false, 1);
|
||||
break;
|
||||
|
||||
case "white_regex":
|
||||
echo remove_from_table($db, "regex_whitelist", $domains);
|
||||
echo remove_from_table($db, "domainlist", $domains, false, 2);
|
||||
break;
|
||||
|
||||
case "black_regex":
|
||||
echo remove_from_table($db, "domainlist", $domains, false, 3);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -31,12 +31,21 @@ function archive_add_file($path,$name,$subdir="")
|
||||
*
|
||||
* @param $name string The name of the file in the archive to save the table to
|
||||
* @param $table string The table to export
|
||||
* @param $type integer Type of domains to store
|
||||
*/
|
||||
function archive_add_table($name, $table)
|
||||
function archive_add_table($name, $table, $type=-1)
|
||||
{
|
||||
global $archive, $db;
|
||||
|
||||
$results = $db->query("SELECT * FROM $table");
|
||||
if($type > -1)
|
||||
{
|
||||
$querystr = "SELECT * FROM $table WHERE type = $type;";
|
||||
}
|
||||
else
|
||||
{
|
||||
$querystr = "SELECT * FROM $table;";
|
||||
}
|
||||
$results = $db->query($querystr);
|
||||
|
||||
// Return early without creating a file if the
|
||||
// requested table cannot be accessed
|
||||
@@ -96,11 +105,27 @@ function archive_restore_table($file, $table, $flush=false)
|
||||
$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";
|
||||
}
|
||||
else
|
||||
{
|
||||
$sql = "INSERT OR IGNORE INTO ".$table;
|
||||
$sql .= " (id,domain,enabled,date_added,comment)";
|
||||
$sql .= " VALUES (:id,:domain,:enabled,:date_added,:comment);";
|
||||
if($table === "whitelist")
|
||||
$type = 0;
|
||||
elseif($table === "blacklist")
|
||||
$type = 1;
|
||||
elseif($table === "regex_whitelist")
|
||||
$type = 2;
|
||||
elseif($table === "regex_blacklist")
|
||||
$type = 3;
|
||||
|
||||
$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";
|
||||
}
|
||||
|
||||
@@ -137,6 +162,11 @@ function archive_restore_table($file, $table, $flush=false)
|
||||
$stmt->bindValue(":comment", $row["comment"], $type);
|
||||
}
|
||||
|
||||
if($table === "domainlist")
|
||||
{
|
||||
$stmt->bindValue(":type", $row["type"], SQLITE3_INTEGER);
|
||||
}
|
||||
|
||||
if($stmt->execute() && $stmt->reset() && $stmt->clear())
|
||||
$num++;
|
||||
else
|
||||
@@ -335,10 +365,10 @@ else
|
||||
exit("cannot open/create ".htmlentities($archive_file_name)."<br>\nPHP user: ".exec('whoami')."\n");
|
||||
}
|
||||
|
||||
archive_add_table("whitelist.exact.json", "whitelist");
|
||||
archive_add_table("whitelist.regex.json", "regex_whitelist");
|
||||
archive_add_table("blacklist.exact.json", "blacklist");
|
||||
archive_add_table("blacklist.regex.json", "regex_blacklist");
|
||||
archive_add_table("whitelist.exact.json", "domainlist");
|
||||
archive_add_table("whitelist.regex.json", "domainlist");
|
||||
archive_add_table("blacklist.exact.json", "domainlist");
|
||||
archive_add_table("blacklist.regex.json", "domainlist");
|
||||
archive_add_table("adlist.json", "adlist");
|
||||
archive_add_table("domain_audit.json", "domain_audit");
|
||||
archive_add_file("/etc/pihole/","setupVars.conf");
|
||||
|
||||
Reference in New Issue
Block a user