From ba1ac8621f405ab842615ad06b745ac4571d8334 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 28 Dec 2020 07:57:58 +0100 Subject: [PATCH 01/14] Use empty array for foreach() when no group is to be set, always commit when editing domains, and report any errors while commiting. Signed-off-by: DL6ER --- scripts/pi-hole/php/groups.php | 96 ++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/scripts/pi-hole/php/groups.php b/scripts/pi-hole/php/groups.php index 6ae6ccfb..7da4f715 100644 --- a/scripts/pi-hole/php/groups.php +++ b/scripts/pi-hole/php/groups.php @@ -382,7 +382,10 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing DELETE statement: ' . $db->lastErrorMsg()); } - foreach ($_POST['groups'] as $gid) { + $groups = array(); + if(isset($_POST['groups'])) + $groups = $_POST['groups']; + foreach ($groups as $gid) { $stmt = $db->prepare('INSERT INTO client_by_group (client_id,group_id) VALUES(:id,:gid);'); if (!$stmt) { throw new Exception('While preparing INSERT INTO statement: ' . $db->lastErrorMsg()); @@ -400,7 +403,9 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing INSERT INTO statement: ' . $db->lastErrorMsg()); } } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -437,7 +442,9 @@ if ($_POST['action'] == 'get_groups') { if (!$stmt->execute()) { throw new Exception('While executing client statement: ' . $db->lastErrorMsg()); } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -677,7 +684,9 @@ if ($_POST['action'] == 'get_groups') { $added++; } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $after = intval($db->querySingle("SELECT COUNT(*) FROM domainlist;")); $difference = $after - $before; @@ -739,39 +748,43 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing: ' . $db->lastErrorMsg()); } - if (isset($_POST['groups'])) { - $stmt = $db->prepare('DELETE FROM domainlist_by_group WHERE domainlist_id = :id'); + $stmt = $db->prepare('DELETE FROM domainlist_by_group WHERE domainlist_id = :id'); + if (!$stmt) { + throw new Exception('While preparing DELETE statement: ' . $db->lastErrorMsg()); + } + + if (!$stmt->bindValue(':id', intval($_POST['id']), SQLITE3_INTEGER)) { + throw new Exception('While binding id: ' . $db->lastErrorMsg()); + } + + if (!$stmt->execute()) { + throw new Exception('While executing DELETE statement: ' . $db->lastErrorMsg()); + } + + $groups = array(); + if(isset($_POST['groups'])) + $groups = $_POST['groups']; + foreach ($groups as $gid) { + $stmt = $db->prepare('INSERT INTO domainlist_by_group (domainlist_id,group_id) VALUES(:id,:gid);'); if (!$stmt) { - throw new Exception('While preparing DELETE statement: ' . $db->lastErrorMsg()); + throw new Exception('While preparing INSERT INTO statement: ' . $db->lastErrorMsg()); } if (!$stmt->bindValue(':id', intval($_POST['id']), SQLITE3_INTEGER)) { throw new Exception('While binding id: ' . $db->lastErrorMsg()); } + if (!$stmt->bindValue(':gid', intval($gid), SQLITE3_INTEGER)) { + throw new Exception('While binding gid: ' . $db->lastErrorMsg()); + } + if (!$stmt->execute()) { - throw new Exception('While executing DELETE statement: ' . $db->lastErrorMsg()); + throw new Exception('While executing INSERT INTO statement: ' . $db->lastErrorMsg()); } + } - foreach ($_POST['groups'] as $gid) { - $stmt = $db->prepare('INSERT INTO domainlist_by_group (domainlist_id,group_id) VALUES(:id,:gid);'); - if (!$stmt) { - throw new Exception('While preparing INSERT INTO statement: ' . $db->lastErrorMsg()); - } - - if (!$stmt->bindValue(':id', intval($_POST['id']), SQLITE3_INTEGER)) { - throw new Exception('While binding id: ' . $db->lastErrorMsg()); - } - - if (!$stmt->bindValue(':gid', intval($gid), SQLITE3_INTEGER)) { - throw new Exception('While binding gid: ' . $db->lastErrorMsg()); - } - - if (!$stmt->execute()) { - throw new Exception('While executing INSERT INTO statement: ' . $db->lastErrorMsg()); - } - } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); } $reload = true; @@ -810,7 +823,9 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing domainlist statement: ' . $db->lastErrorMsg()); } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -856,7 +871,9 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing domainlist statement: ' . $db->lastErrorMsg()); } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -937,7 +954,9 @@ if ($_POST['action'] == 'get_groups') { $added++; } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -993,7 +1012,10 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing DELETE statement: ' . $db->lastErrorMsg()); } - foreach ($_POST['groups'] as $gid) { + $groups = array(); + if(isset($_POST['groups'])) + $groups = $_POST['groups']; + foreach ($groups as $gid) { $stmt = $db->prepare('INSERT INTO adlist_by_group (adlist_id,group_id) VALUES(:id,:gid);'); if (!$stmt) { throw new Exception('While preparing INSERT INTO statement: ' . $db->lastErrorMsg()); @@ -1012,7 +1034,9 @@ if ($_POST['action'] == 'get_groups') { } } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -1050,7 +1074,9 @@ if ($_POST['action'] == 'get_groups') { throw new Exception('While executing adlist statement: ' . $db->lastErrorMsg()); } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $reload = true; JSON_success(); @@ -1090,7 +1116,9 @@ if ($_POST['action'] == 'get_groups') { $added++; } - $db->query('COMMIT;'); + if(!$db->query('COMMIT;')) { + throw new Exception('While commiting changes to the database: ' . $db->lastErrorMsg()); + } $after = intval($db->querySingle("SELECT COUNT(*) FROM domain_audit;")); $difference = $after - $before; From fa2279d473d61c0bfee892e9ef6841b1188ecf15 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 31 Dec 2020 10:36:27 +0100 Subject: [PATCH 02/14] Add SVCB and HTTPS types and reduce code duplication Signed-off-by: DL6ER --- api_db.php | 36 +++++-------------------------- queries.php | 7 +----- scripts/pi-hole/php/func.php | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/api_db.php b/api_db.php index 76b0ca5f..51d59ba4 100644 --- a/api_db.php +++ b/api_db.php @@ -120,40 +120,14 @@ if (isset($_GET['getAllQueries']) && $auth) if(!is_bool($results)) while ($row = $results->fetchArray()) { + // Try to resolve host name of this client $c = resolveHostname($row[3],false); // Convert query type ID to name - // Names taken from FTL's query type names - switch($row[1]) { - case 1: - $query_type = "A"; - break; - case 2: - $query_type = "AAAA"; - break; - case 3: - $query_type = "ANY"; - break; - case 4: - $query_type = "SRV"; - break; - case 5: - $query_type = "SOA"; - break; - case 6: - $query_type = "PTR"; - break; - case 7: - $query_type = "TXT"; - break; - case 8: - $query_type = "NAPTR"; - break; - default: - $query_type = "UNKN"; - break; - } - // array: time type domain client status upstream destination + $query_type = getQueryTypeStr($row[1]); + + // Insert into array + // array: time type domain client status upstream destination $allQueries[] = [$row[0], $query_type, utf8_encode(str_replace("~"," ",$row[2])), utf8_encode($c), $row[4], utf8_encode($row[5])]; } } diff --git a/queries.php b/queries.php index 6044a244..b22c1954 100644 --- a/queries.php +++ b/queries.php @@ -59,12 +59,7 @@ else if(isset($_GET["forwarddest"])) } else if(isset($_GET["querytype"])) { - $qtypes = ["A (IPv4)", "AAAA (IPv6)", "ANY", "SRV", "SOA", "PTR", "TXT", "NAPTR", "MX", "DS", "RRSIG", "DNSKEY", "NS", "OTHER"]; - $qtype = intval($_GET["querytype"]); - if($qtype > 0 && $qtype <= count($qtypes)) - $showing .= " ".$qtypes[$qtype-1]." queries"; - else - $showing .= " type ".$qtype." queries"; + $showing .= " type ".getQueryTypeStr($_GET["querytype"])." queries"; } else if(isset($_GET["domain"])) { diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index 0db73b18..1960760d 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -405,4 +405,45 @@ function returnError($message = "", $json = true) } } +function getQueryTypeStr($querytype) +{ + $qtype = intval($querytype); + switch ($qtype) { + case 1: + return "A (IPv4)"; + case 2: + return "AAAA (IPv6)"; + case 3: + return "ANY"; + case 4: + return "SRV"; + case 5: + return "SOA"; + case 6: + return "PTR"; + case 7: + return "TXT"; + case 8: + return "NAPTR"; + case 9: + return "MX"; + case 10: + return "DS"; + case 11: + return "RRSIG"; + case 12: + return "DNSKEY"; + case 13: + return "NS"; + case 14: + return "OTHER"; + case 15: + return "SVCB"; + case 16: + return "HTTPS"; + default: + return "UNKN (".$qtype.")"; + } +} + ?> From db40cc31fe655b1cd3daad71adb85872bcd434e7 Mon Sep 17 00:00:00 2001 From: Timothy Stewart Date: Mon, 4 Jan 2021 17:34:25 -0600 Subject: [PATCH 03/14] fix(cname_records): Fixed gammar Signed-off-by: Timothy Stewart --- cname_records.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cname_records.php b/cname_records.php index 0e4e7e16..53dcdab5 100644 --- a/cname_records.php +++ b/cname_records.php @@ -40,7 +40,7 @@