diff --git a/cron_mail_queue.php b/cron_mail_queue.php index 19e499e0..c3642094 100644 --- a/cron_mail_queue.php +++ b/cron_mail_queue.php @@ -46,10 +46,16 @@ if (file_exists($lock_file_path)) { // If file is older than 10 minutes (600 seconds), delete and continue if ($file_age > 600) { + unlink($lock_file_path); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Delete', log_description = 'Cron Mail Queuer detected a lock file was present but was over 10 minutes old so it removed it.'"); + // Logging + logAction("Cron-Mail-Queue", "Delete", "Cron Mail Queuer detected a lock file was present but was over 10 minutes old so it removed it."); + } else { - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Locked', log_description = 'Cron Mail Queuer attempted to execute but was already executing so instead it terminated.'"); + + // Logging + logAction("Cron-Mail-Queue", "Locked", "Cron Mail Queuer attempted to execute but was already executing so instead it terminated."); + exit("Script is already running. Exiting."); } } @@ -120,15 +126,20 @@ if (mysqli_num_rows($sql_queue) > 0) { appNotify("Cron-Mail-Queue", "Failed to send email #$email_id to $email_recipient_logging"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Error', log_description = 'Failed to send email #$email_id to $email_recipient_logging regarding $email_subject_logging. $mail'"); + // Logging + logAction("Cron-Mail-Queue", "Error", "Failed to send email: $email_id to $email_recipient_logging regarding $email_subject_logging. $mail"); } else { // Update Message - Success mysqli_query($mysqli, "UPDATE email_queue SET email_status = 3, email_sent_at = NOW(), email_attempts = 1 WHERE email_id = $email_id"); } + } else { + // Recipient email isn't valid, mark as failed and log the error mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_attempts = 99 WHERE email_id = $email_id"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Error', log_description = 'Failed to send email #$email_id due to invalid recipient address. Email subject was: $email_subject_logging.'"); + + // Logging + logAction("Cron-Mail-Queue", "Error", "Failed to send email: $email_id due to invalid recipient address. Email subject was: $email_subject_logging"); } } else { @@ -136,7 +147,9 @@ if (mysqli_num_rows($sql_queue) > 0) { $email_from_logging = sanitizeInput($row['email_from']); mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_attempts = 99 WHERE email_id = $email_id"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Error', log_description = 'Failed to send email #$email_id due to invalid sender address: $email_from_logging - check configuration in settings.'"); + + // Logging + logAction("Cron-Mail-Queue", "Error", "Failed to send email #$email_id due to invalid sender address: $email_from_logging - check configuration in settings."); appNotify("Mail", "Failed to send email #$email_id due to invalid sender address"); @@ -197,10 +210,15 @@ if (mysqli_num_rows($sql_failed_queue) > 0) { if ($mail !== true) { // Update Message mysqli_query($mysqli, "UPDATE email_queue SET email_status = 2, email_failed_at = NOW(), email_attempts = $email_attempts WHERE email_id = $email_id"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Error', log_description = 'Failed to re-send email #$email_id to $email_recipient_logging regarding $email_subject_logging. $mail'"); + + // Logging + logAction("Cron-Mail-Queue", "Error", "Failed to re-send email #$email_id to $email_recipient_logging regarding $email_subject_logging. $mail"); + } else { + // Update Message mysqli_query($mysqli, "UPDATE email_queue SET email_status = 3, email_sent_at = NOW(), email_attempts = $email_attempts WHERE email_id = $email_id"); + } } } diff --git a/cron_ticket_email_parser.php b/cron_ticket_email_parser.php index cb2ea734..da89a315 100644 --- a/cron_ticket_email_parser.php +++ b/cron_ticket_email_parser.php @@ -55,9 +55,15 @@ if (file_exists($lock_file_path)) { // If file is older than 5 minutes (300 seconds), delete and continue if ($file_age > 300) { unlink($lock_file_path); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Delete', log_description = 'Cron Email Parser detected a lock file was present but was over 5 minutes old so it removed it'"); + + // Logging + logAction("Cron-Email-Parser", "Delete", "Cron Email Parser detected a lock file was present but was over 5 minutes old so it removed it."); + } else { - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Locked', log_description = 'Cron Email Parser attempted to execute but was already executing, so instead it terminated.'"); + + // Logging + logAction("Cron-Email-Parser", "Locked", "Cron Email Parser attempted to execute but was already executing, so instead it terminated."); + exit("Script is already running. Exiting."); } } @@ -108,7 +114,8 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date mysqli_query($mysqli, "INSERT INTO tickets SET ticket_prefix = '$ticket_prefix_esc', ticket_number = $ticket_number, ticket_subject = '$subject', ticket_details = '$message_esc', ticket_priority = 'Low', ticket_status = 1, ticket_created_by = 0, ticket_contact_id = $contact_id, ticket_url_key = '$url_key', ticket_client_id = $client_id"); $id = mysqli_insert_id($mysqli); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'Email parser: Client contact $contact_email_esc created ticket $ticket_prefix_esc$ticket_number ($subject) ($id)', log_client_id = $client_id"); + // Logging + logAction("Ticket", "Create", "Email parser: Client contact $contact_email_esc created ticket $ticket_prefix_esc$ticket_number ($subject) ($id)", $client_id, $id); mkdirMissing('uploads/tickets/'); $att_dir = "uploads/tickets/" . $id . "/"; @@ -136,7 +143,7 @@ function addTicket($contact_id, $contact_name, $contact_email, $client_id, $date mysqli_query($mysqli, "INSERT INTO ticket_attachments SET ticket_attachment_name = '$ticket_attachment_name_esc', ticket_attachment_reference_name = '$ticket_attachment_reference_name_esc', ticket_attachment_ticket_id = $id"); } else { $ticket_attachment_name_esc = mysqli_real_escape_string($mysqli, $att_name); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Blocked attachment $ticket_attachment_name_esc from Client contact $contact_email_esc for ticket $ticket_prefix_esc$ticket_number', log_client_id = $client_id"); + logAction("Ticket", "Edit", "Email parser: Blocked attachment $ticket_attachment_name_esc from Client contact $contact_email_esc for ticket $ticket_prefix_esc$ticket_number", $client_id, $id); } } @@ -277,7 +284,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac mysqli_query($mysqli, "INSERT INTO ticket_attachments SET ticket_attachment_name = '$ticket_attachment_name_esc', ticket_attachment_reference_name = '$ticket_attachment_reference_name_esc', ticket_attachment_reply_id = $reply_id, ticket_attachment_ticket_id = $ticket_id"); } else { $ticket_attachment_name_esc = mysqli_real_escape_string($mysqli, $att_name); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Blocked attachment $ticket_attachment_name_esc from Client contact $from_email_esc for ticket $config_ticket_prefix$ticket_number_esc', log_client_id = $client_id"); + logAction("Ticket", "Edit", "Email parser: Blocked attachment $ticket_attachment_name_esc from Client contact $from_email_esc for ticket $config_ticket_prefix$ticket_number_esc", $client_id, $ticket_id); } } @@ -313,7 +320,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 2, ticket_resolved_at = NULL WHERE ticket_id = $ticket_id AND ticket_client_id = $client_id LIMIT 1"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Update', log_description = 'Email parser: Client contact $from_email_esc updated ticket $config_ticket_prefix$ticket_number_esc ($subject)', log_client_id = $client_id"); + logAction("Ticket", "Edit", "Email parser: Client contact $from_email_esc updated ticket $config_ticket_prefix$ticket_number_esc ($subject)", $client_id, $ticket_id); customAction('ticket_reply_client', $ticket_id); @@ -473,7 +480,8 @@ if ($emails !== false) { mysqli_query($mysqli, "INSERT INTO contacts SET contact_name = '".mysqli_real_escape_string($mysqli, $contact_name)."', contact_email = '".mysqli_real_escape_string($mysqli, $contact_email)."', contact_notes = 'Added automatically via email parsing.', contact_client_id = $client_id"); $contact_id = mysqli_insert_id($mysqli); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Contact', log_action = 'Create', log_description = 'Email parser: created contact ".mysqli_real_escape_string($mysqli, $contact_name)."', log_client_id = $client_id"); + // Logging + logAction("Contact", "Create", "Email parser: created contact " . mysqli_real_escape_string($mysqli, $contact_name) . "", $client_id, $contact_id); customAction('contact_create', $contact_id); if (addTicket($contact_id, $contact_name, $contact_email, $client_id, $date, $subject, $message_body, $attachments, $original_message_file)) { @@ -528,7 +536,8 @@ $execution_time_formatted = number_format($execution_time, 2); // Insert a log entry into the logs table $processed_info = "Processed: $processed_count email(s), Unprocessed: $unprocessed_count email(s)"; // Remove Comment below for Troubleshooting -// mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Execution', log_description = 'Cron Email Parser executed in $execution_time_formatted seconds. $processed_info'"); + +//logAction("Cron-Email-Parser", "Execution", "Cron Email Parser executed in $execution_time_formatted seconds. $processed_info"); // END Calculate execution time diff --git a/login.php b/login.php index 9dad4246..41f9824d 100644 --- a/login.php +++ b/login.php @@ -113,6 +113,7 @@ if (isset($_POST['login'])) { // Set temporary user variables $user_name = sanitizeInput($row['user_name']); $user_id = intval($row['user_id']); + $session_user_id = $user_id; // to pass the user_id to logAction function $user_email = sanitizeInput($row['user_email']); $token = sanitizeInput($row['user_token']); $force_mfa = intval($row['user_config_force_mfa']); diff --git a/portal/login.php b/portal/login.php index 878ca6b4..10bc8259 100644 --- a/portal/login.php +++ b/portal/login.php @@ -61,6 +61,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) { $row = mysqli_fetch_array($sql); $client_id = intval($row['contact_client_id']); $user_id = intval($row['user_id']); + $session_user_id = $user_id; // to pass the user_id to logAction function $contact_id = intval($row['contact_id']); $user_email = sanitizeInput($row['user_email']); $user_auth_method = sanitizeInput($row['user_auth_method']); diff --git a/portal/login_microsoft.php b/portal/login_microsoft.php index 6b1b9797..783dfa0a 100644 --- a/portal/login_microsoft.php +++ b/portal/login_microsoft.php @@ -104,6 +104,7 @@ if (isset($_POST['code']) && $_POST['state'] == session_id()) { $row = mysqli_fetch_array($sql); $client_id = intval($row['contact_client_id']); $user_id = intval($row['user_id']); + $session_user_id = $user_id; // to pass the user_id to logAction function $contact_id = intval($row['contact_id']); $user_email = sanitizeInput($row['user_email']); $user_auth_method = sanitizeInput($row['user_auth_method']);