mirror of
https://github.com/pi-hole/web.git
synced 2026-04-23 02:09:58 +01:00
Replacing tabs with 4 spaces
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
This commit is contained in:
@@ -9,75 +9,75 @@
|
||||
|
||||
function getGravityDBFilename()
|
||||
{
|
||||
// Get possible non-standard location of FTL's database
|
||||
$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf");
|
||||
if(isset($FTLsettings["GRAVITYDB"]))
|
||||
{
|
||||
return $FTLsettings["GRAVITYDB"];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/etc/pihole/gravity.db";
|
||||
}
|
||||
// Get possible non-standard location of FTL's database
|
||||
$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf");
|
||||
if(isset($FTLsettings["GRAVITYDB"]))
|
||||
{
|
||||
return $FTLsettings["GRAVITYDB"];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/etc/pihole/gravity.db";
|
||||
}
|
||||
}
|
||||
|
||||
function getQueriesDBFilename()
|
||||
{
|
||||
// Get possible non-standard location of FTL's database
|
||||
$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf");
|
||||
if(isset($FTLsettings["DBFILE"]))
|
||||
{
|
||||
return $FTLsettings["DBFILE"];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/etc/pihole/pihole-FTL.db";
|
||||
}
|
||||
// Get possible non-standard location of FTL's database
|
||||
$FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf");
|
||||
if(isset($FTLsettings["DBFILE"]))
|
||||
{
|
||||
return $FTLsettings["DBFILE"];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/etc/pihole/pihole-FTL.db";
|
||||
}
|
||||
}
|
||||
|
||||
function SQLite3_connect_try($filename, $mode, $trytoreconnect)
|
||||
{
|
||||
try
|
||||
{
|
||||
// connect to database
|
||||
return new SQLite3($filename, $mode);
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
// sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds
|
||||
if($trytoreconnect)
|
||||
{
|
||||
sleep(3);
|
||||
return SQLite3_connect_try($filename, $mode, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we should not try again (or are already trying again!), we return the exception string
|
||||
// so the user gets it on the dashboard
|
||||
return $filename.": ".$exception->getMessage();
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
// connect to database
|
||||
return new SQLite3($filename, $mode);
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
// sqlite3 throws an exception when it is unable to connect, try to reconnect after 3 seconds
|
||||
if($trytoreconnect)
|
||||
{
|
||||
sleep(3);
|
||||
return SQLite3_connect_try($filename, $mode, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we should not try again (or are already trying again!), we return the exception string
|
||||
// so the user gets it on the dashboard
|
||||
return $filename.": ".$exception->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function SQLite3_connect($filename, $mode=SQLITE3_OPEN_READONLY)
|
||||
{
|
||||
if(strlen($filename) > 0)
|
||||
{
|
||||
$db = SQLite3_connect_try($filename, $mode, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
die("No database available");
|
||||
}
|
||||
if(is_string($db))
|
||||
{
|
||||
die("Error connecting to database\n".$db);
|
||||
}
|
||||
if(strlen($filename) > 0)
|
||||
{
|
||||
$db = SQLite3_connect_try($filename, $mode, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
die("No database available");
|
||||
}
|
||||
if(is_string($db))
|
||||
{
|
||||
die("Error connecting to database\n".$db);
|
||||
}
|
||||
|
||||
// Add busy timeout so methods don't fail immediately when, e.g., FTL is currently reading from the DB
|
||||
$db->busyTimeout(5000);
|
||||
// Add busy timeout so methods don't fail immediately when, e.g., FTL is currently reading from the DB
|
||||
$db->busyTimeout(5000);
|
||||
|
||||
return $db;
|
||||
return $db;
|
||||
}
|
||||
|
||||
|
||||
@@ -94,125 +94,125 @@ function SQLite3_connect($filename, $mode=SQLITE3_OPEN_READONLY)
|
||||
*/
|
||||
function add_to_table($db, $table, $domains, $comment=null, $wildcardstyle=false, $returnnum=false, $type=-1)
|
||||
{
|
||||
if(!is_int($type))
|
||||
{
|
||||
return "Error: Argument type has to be of type integer (is ".gettype($type).")";
|
||||
}
|
||||
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.";
|
||||
}
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for $table table.";
|
||||
}
|
||||
|
||||
// To which column should the record be added to?
|
||||
if ($table === "adlist")
|
||||
{
|
||||
$field = "address";
|
||||
}
|
||||
else
|
||||
{
|
||||
$field = "domain";
|
||||
}
|
||||
// To which column should the record be added to?
|
||||
if ($table === "adlist")
|
||||
{
|
||||
$field = "address";
|
||||
}
|
||||
else
|
||||
{
|
||||
$field = "domain";
|
||||
}
|
||||
|
||||
// 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));
|
||||
// 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 INSERT SQLite statement
|
||||
$bindcomment = false;
|
||||
if($table === "domain_audit") {
|
||||
$querystr = "INSERT OR IGNORE INTO $table ($field) VALUES (:$field);";
|
||||
} elseif($type === -1) {
|
||||
$querystr = "INSERT OR IGNORE INTO $table ($field,comment) VALUES (:$field, :comment);";
|
||||
$bindcomment = true;
|
||||
} else {
|
||||
$querystr = "REPLACE INTO $table ($field,comment,type) VALUES (:$field, :comment, $type);";
|
||||
$bindcomment = true;
|
||||
}
|
||||
$stmt = $db->prepare($querystr);
|
||||
// Prepare INSERT SQLite statement
|
||||
$bindcomment = false;
|
||||
if($table === "domain_audit") {
|
||||
$querystr = "INSERT OR IGNORE INTO $table ($field) VALUES (:$field);";
|
||||
} elseif($type === -1) {
|
||||
$querystr = "INSERT OR IGNORE INTO $table ($field,comment) VALUES (:$field, :comment);";
|
||||
$bindcomment = true;
|
||||
} else {
|
||||
$querystr = "REPLACE INTO $table ($field,comment,type) VALUES (:$field, :comment, $type);";
|
||||
$bindcomment = true;
|
||||
}
|
||||
$stmt = $db->prepare($querystr);
|
||||
|
||||
// Return early if we failed to prepare the SQLite statement
|
||||
if(!$stmt)
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Failed to prepare statement for $table table (type = $type, field = $field).";
|
||||
}
|
||||
// Return early if we failed to prepare the SQLite statement
|
||||
if(!$stmt)
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Failed to prepare statement for $table table (type = $type, field = $field).";
|
||||
}
|
||||
|
||||
// Loop over domains and inject the lines into the database
|
||||
$num = 0;
|
||||
foreach($domains as $domain)
|
||||
{
|
||||
// Limit max length for a domain entry to 253 chars
|
||||
if(strlen($domain) > 253)
|
||||
continue;
|
||||
// Loop over domains and inject the lines into the database
|
||||
$num = 0;
|
||||
foreach($domains as $domain)
|
||||
{
|
||||
// Limit max length for a domain entry to 253 chars
|
||||
if(strlen($domain) > 253)
|
||||
continue;
|
||||
|
||||
if($wildcardstyle)
|
||||
$domain = "(\\.|^)".str_replace(".","\\.",$domain)."$";
|
||||
if($wildcardstyle)
|
||||
$domain = "(\\.|^)".str_replace(".","\\.",$domain)."$";
|
||||
|
||||
$stmt->bindValue(":$field", htmlentities($domain), SQLITE3_TEXT);
|
||||
if($bindcomment) {
|
||||
$stmt->bindValue(":comment", htmlentities($comment), SQLITE3_TEXT);
|
||||
}
|
||||
$stmt->bindValue(":$field", htmlentities($domain), SQLITE3_TEXT);
|
||||
if($bindcomment) {
|
||||
$stmt->bindValue(":comment", htmlentities($comment), SQLITE3_TEXT);
|
||||
}
|
||||
|
||||
if($stmt->execute() && $stmt->reset())
|
||||
$num++;
|
||||
else
|
||||
{
|
||||
$stmt->close();
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", added ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($stmt->execute() && $stmt->reset())
|
||||
$num++;
|
||||
else
|
||||
{
|
||||
$stmt->close();
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", added ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close prepared statement and return number of processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
// Close prepared statement and return number of processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
$finalcount = intval($db->querySingle($countquery));
|
||||
$modified = $finalcount - $initialcount;
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
$finalcount = intval($db->querySingle($countquery));
|
||||
$modified = $finalcount - $initialcount;
|
||||
|
||||
// If we add less domains than the user specified, then they wanted to add duplicates
|
||||
if($modified !== $num)
|
||||
{
|
||||
$delta = $num - $modified;
|
||||
$extra = " (skipped ".$delta." duplicates)";
|
||||
}
|
||||
else
|
||||
{
|
||||
$extra = "";
|
||||
}
|
||||
// If we add less domains than the user specified, then they wanted to add duplicates
|
||||
if($modified !== $num)
|
||||
{
|
||||
$delta = $num - $modified;
|
||||
$extra = " (skipped ".$delta." duplicates)";
|
||||
}
|
||||
else
|
||||
{
|
||||
$extra = "";
|
||||
}
|
||||
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, added ".$modified." of ".$num." domain".$plural.$extra;
|
||||
}
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, added ".$modified." of ".$num." domain".$plural.$extra;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,96 +227,96 @@ function add_to_table($db, $table, $domains, $comment=null, $wildcardstyle=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).")";
|
||||
}
|
||||
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 domainlist table.";
|
||||
}
|
||||
// Begin transaction
|
||||
if(!$db->exec("BEGIN TRANSACTION;"))
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Unable to begin transaction for domainlist 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));
|
||||
// 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 statement
|
||||
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);
|
||||
// Prepare SQLite statement
|
||||
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)
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Failed to prepare statement for ".$table." table (type = ".$type.").";
|
||||
}
|
||||
// Return early if we failed to prepare the SQLite statement
|
||||
if(!$stmt)
|
||||
{
|
||||
if($returnnum)
|
||||
return 0;
|
||||
else
|
||||
return "Error: Failed to prepare statement for ".$table." table (type = ".$type.").";
|
||||
}
|
||||
|
||||
// Loop over domains and remove the lines from the database
|
||||
$num = 0;
|
||||
foreach($domains as $domain)
|
||||
{
|
||||
$stmt->bindValue(":domain", $domain, SQLITE3_TEXT);
|
||||
// Loop over domains and remove the lines from the database
|
||||
$num = 0;
|
||||
foreach($domains as $domain)
|
||||
{
|
||||
$stmt->bindValue(":domain", $domain, SQLITE3_TEXT);
|
||||
|
||||
if($stmt->execute() && $stmt->reset())
|
||||
$num++;
|
||||
else
|
||||
{
|
||||
$stmt->close();
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", removed ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($stmt->execute() && $stmt->reset())
|
||||
$num++;
|
||||
else
|
||||
{
|
||||
$stmt->close();
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Error: ".$db->lastErrorMsg().", removed ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close prepared statement and return number or processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
// Close prepared statement and return number or processed rows
|
||||
$stmt->close();
|
||||
$db->exec("COMMIT;");
|
||||
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, removed ".$num." domain".$plural;
|
||||
}
|
||||
if($returnnum)
|
||||
return $num;
|
||||
else
|
||||
{
|
||||
if($num === 1)
|
||||
$plural = "";
|
||||
else
|
||||
$plural = "s";
|
||||
return "Success, removed ".$num." domain".$plural;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists("ListType")) {
|
||||
class ListType{
|
||||
const whitelist = 0;
|
||||
const blacklist = 1;
|
||||
const regex_whitelist = 2;
|
||||
const regex_blacklist = 3;
|
||||
}
|
||||
class ListType{
|
||||
const whitelist = 0;
|
||||
const blacklist = 1;
|
||||
const regex_whitelist = 2;
|
||||
const regex_blacklist = 3;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user