From 8050dffbb81bf2eda64f4b79043a56fcc6362dda Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sun, 24 Nov 2024 15:04:23 -0500 Subject: [PATCH] Enhancement: DB Structure Update: Seperate Logs into 3 seperate log tables error_logs, auth_logs, audit_logs. This will provide huge performance benefits and organization --- database_updates.php | 30 +++++++++++++++++++++++++++--- database_version.php | 2 +- db.sql | 37 ++++++++++++++++++++++++++++++++++++- functions.php | 16 ++++++++++++++++ 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/database_updates.php b/database_updates.php index bceb122e..cf7ff1ed 100644 --- a/database_updates.php +++ b/database_updates.php @@ -2279,10 +2279,34 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) { mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.7'"); } - // if (CURRENT_DATABASE_VERSION == '1.6.7') { - // // Insert queries here required to update to DB version 1.6.8 + if (CURRENT_DATABASE_VERSION == '1.6.7') { + + mysqli_query($mysqli, "CREATE TABLE `error_logs` ( + `error_log_id` INT(11) NOT NULL AUTO_INCREMENT, + `error_log_type` VARCHAR(200) NOT NULL, + `error_log_details` VARCHAR(1000) NULL DEFAULT NULL, + `error_log_created_at` DATETIME NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`error_log_id`) + )"); + + mysqli_query($mysqli, "CREATE TABLE `auth_logs` ( + `auth_log_id` INT(11) NOT NULL AUTO_INCREMENT, + `auth_log_status` TINYINT(1) NOT NULL, + `auth_log_details` VARCHAR(200) NULL DEFAULT NULL, + `auth_log_ip` VARCHAR(200) NULL DEFAULT NULL, + `auth_log_user_agent` VARCHAR(250) NULL DEFAULT NULL, + `auth_log_user_id` INT(11) NOT NULL DEFAULT 0, + `auth_log_created_at` DATETIME NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`auth_log_id`) + )"); + + mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.8'"); + } + + // if (CURRENT_DATABASE_VERSION == '1.6.8') { + // // Insert queries here required to update to DB version 1.6.9 // // Then, update the database to the next sequential version - // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.8'"); + // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.9'"); // } } else { diff --git a/database_version.php b/database_version.php index 30e5d0c9..c41d689f 100644 --- a/database_version.php +++ b/database_version.php @@ -5,4 +5,4 @@ * It is used in conjunction with database_updates.php */ -DEFINE("LATEST_DATABASE_VERSION", "1.6.7"); +DEFINE("LATEST_DATABASE_VERSION", "1.6.8"); diff --git a/db.sql b/db.sql index 2edc929d..4651600e 100644 --- a/db.sql +++ b/db.sql @@ -201,6 +201,25 @@ CREATE TABLE `assets` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `auth_logs` +-- + +DROP TABLE IF EXISTS `auth_logs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `auth_logs` ( + `auth_log_id` int(11) NOT NULL AUTO_INCREMENT, + `auth_log_status` tinyint(1) NOT NULL, + `auth_log_details` varchar(200) DEFAULT NULL, + `auth_log_ip` varchar(200) DEFAULT NULL, + `auth_log_user_agent` varchar(250) DEFAULT NULL, + `auth_log_user_id` int(11) NOT NULL DEFAULT 0, + `auth_log_created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`auth_log_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `budget` -- @@ -656,6 +675,22 @@ CREATE TABLE `email_queue` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `error_logs` +-- + +DROP TABLE IF EXISTS `error_logs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `error_logs` ( + `error_log_id` int(11) NOT NULL AUTO_INCREMENT, + `error_log_type` varchar(200) NOT NULL, + `error_log_details` varchar(1000) DEFAULT NULL, + `error_log_created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`error_log_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `event_attendees` -- @@ -2227,4 +2262,4 @@ CREATE TABLE `vendors` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2024-11-23 15:34:58 +-- Dump completed on 2024-11-24 15:03:10 diff --git a/functions.php b/functions.php index b92fc97f..1e29ff1a 100644 --- a/functions.php +++ b/functions.php @@ -1346,4 +1346,20 @@ function logAction($type, $action, $description, $client_id = 0, $entity_id = 0) } mysqli_query($mysqli, "INSERT INTO logs SET log_type = '$type', log_action = '$action', log_description = '$description', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $entity_id"); +} + +function logError($type, $details) { + global $mysqli; + + mysqli_query($mysqli, "INSERT INTO error_logs SET error_log_type = '$type', error_log_details = '$details'"); +} + +function logAuth($status, $details) { + global $mysqli, $session_user_agent, $session_ip, $session_user_id; + + if (empty($session_user_id)) { + $session_user_id = 0; + } + + mysqli_query($mysqli, "INSERT INTO auth_logs SET auth_log_status = $status, auth_log_details = '$details', auth_log_ip = '$session_ip', auth_log_user_agent = '$session_user_agent', auth_log_user_id = $session_user_id"); } \ No newline at end of file