From 41cfd8d27b2faecfc9447017f6dd72fbda5cfa09 Mon Sep 17 00:00:00 2001 From: Marcus Hill Date: Sat, 18 Nov 2023 11:53:24 +0000 Subject: [PATCH] Scheduled tickets - Notify agent DL when scheduled tickets are raised (if configured) - Bugfix: Ticket numbering if two or more scheduled tickets are raised in the same script execution - Bugfix: Email processing if a contact replied to a scheduled ticket email (still showing original #--itflow# line rather than ##- Please type your reply above this line -##) --- cron.php | 97 ++++++++++++++++++++---------------- cron_ticket_email_parser.php | 2 +- post/ticket.php | 8 +-- settings_mail_queue.php | 10 ++-- 4 files changed, 63 insertions(+), 54 deletions(-) diff --git a/cron.php b/cron.php index 58e86857..8973ee8d 100644 --- a/cron.php +++ b/cron.php @@ -45,12 +45,12 @@ $config_recurring_auto_send_invoice = intval($row['config_recurring_auto_send_in // Tickets $config_ticket_prefix = $row['config_ticket_prefix']; -$config_ticket_next_number = intval($row['config_ticket_next_number']); -$config_ticket_from_name = $row['config_ticket_from_name']; +$config_ticket_from_name = $row['config_ticket_from_name']; // TODO: Sanitize from_name and from_email on assignment, once everything is moved to the database queue $config_ticket_from_email = $row['config_ticket_from_email']; $config_ticket_client_general_notifications = intval($row['config_ticket_client_general_notifications']); $config_ticket_autoclose = intval($row['config_ticket_autoclose']); $config_ticket_autoclose_hours = intval($row['config_ticket_autoclose_hours']); +$config_ticket_new_ticket_notification_email = $row['config_ticket_new_ticket_notification_email']; // Get Config for Telemetry $config_theme = $row['config_theme']; @@ -239,6 +239,7 @@ $sql_scheduled_tickets = mysqli_query($mysqli, "SELECT * FROM scheduled_tickets if (mysqli_num_rows($sql_scheduled_tickets) > 0) { while ($row = mysqli_fetch_array($sql_scheduled_tickets)) { + $schedule_id = intval($row['scheduled_ticket_id']); $subject = sanitizeInput($row['scheduled_ticket_subject']); $details_escaped = mysqli_real_escape_string($mysqli, $row['scheduled_ticket_details']); @@ -250,9 +251,12 @@ if (mysqli_num_rows($sql_scheduled_tickets) > 0) { $contact_id = intval($row['scheduled_ticket_contact_id']); $asset_id = intval($row['scheduled_ticket_asset_id']); - //Get the next Ticket Number and add 1 for the new ticket number - $ticket_number = $config_ticket_next_number; - $new_config_ticket_next_number = $config_ticket_next_number + 1; + // Assign this new ticket the next ticket number + $ticket_number_sql = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_ticket_next_number FROM settings WHERE company_id = 1")); + $ticket_number = intval($ticket_number_sql['config_ticket_next_number']); + + // Increment config_ticket_next_number by 1 (for the next ticket) + $new_config_ticket_next_number = $ticket_number + 1; mysqli_query($mysqli, "UPDATE settings SET config_ticket_next_number = $new_config_ticket_next_number WHERE company_id = 1"); // Raise the ticket @@ -262,53 +266,58 @@ if (mysqli_num_rows($sql_scheduled_tickets) > 0) { // Logging mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Create', log_description = 'System created scheduled $frequency ticket - $subject', log_client_id = $client_id, log_user_id = $created_id"); - // E-mail client - if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1) { + // Notifications - // Get contact/ticket/company details - $sql = mysqli_query( - $mysqli, - "SELECT contact_name, contact_email, ticket_prefix, ticket_number, ticket_subject FROM tickets + + // Get client/contact/ticket details + $sql = mysqli_query( + $mysqli, + "SELECT client_name, contact_name, contact_email, ticket_prefix, ticket_number, ticket_priority, ticket_subject, ticket_details FROM tickets LEFT JOIN clients ON ticket_client_id = client_id LEFT JOIN contacts ON ticket_contact_id = contact_id WHERE ticket_id = $id" - ); - $row = mysqli_fetch_array($sql); + ); + $row = mysqli_fetch_array($sql); - $contact_name = $row['contact_name']; - $contact_email = $row['contact_email']; - $ticket_prefix = $row['ticket_prefix']; - $ticket_number = intval($row['ticket_number']); - $ticket_subject = $row['ticket_subject']; - // Verify contact email is valid - if (filter_var($contact_email, FILTER_VALIDATE_EMAIL)) { - $subject = "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)"; - $body = "#--itflow--#

Hello, $contact_name

A ticket regarding \"$ticket_subject\" has been automatically created for you.

--------------------------------
$details--------------------------------

Ticket: $ticket_prefix$ticket_number
Subject: $ticket_subject
Status: Open
Portal: https://$config_base_url/portal/ticket.php?id=$id

~
$company_name
Support Department
$config_ticket_from_email
$company_phone"; + // Escaped - Do not re-escape in the general subject/body escaping as this would re-escape + $config_ticket_from_name_escaped = sanitizeInput($config_ticket_from_name); // TODO: Move this sanitization to the start of cron, once everything uses the queue + $config_ticket_from_email_escaped = sanitizeInput($config_ticket_from_email); // TODO: Move this sanitization to the start of cron, once everything uses the queue + $contact_name_escaped = sanitizeInput($row['contact_name']); + $contact_email_escaped = sanitizeInput($row['contact_email']); - $mail = sendSingleEmail( - $config_smtp_host, - $config_smtp_username, - $config_smtp_password, - $config_smtp_encryption, - $config_smtp_port, - $config_ticket_from_email, - $config_ticket_from_name, - $contact_email, - $contact_name, - $subject, - $body - ); + // Unescaped - DANGEROUS unless escaped within the general subject/body escape queries + $client_name = $row['client_name']; + $contact_name = $row['contact_name']; + $contact_email = $row['contact_email']; + $ticket_prefix = $row['ticket_prefix']; + $ticket_number = intval($row['ticket_number']); + $ticket_priority = $row['ticket_priority']; + $ticket_subject = $row['ticket_subject']; + $ticket_details = $row['ticket_details']; // Output on settings_mail_queue.php is sanitized through HTML Purifier - if ($mail !== true) { - mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Mail', notification = 'Failed to send email to $contact_email'"); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Failed to send email to $contact_email regarding $subject. $mail'"); - } - } + // Notify client by email their ticket has been raised, if general notifications are turned on & there is a valid contact email + if (!empty($config_smtp_host) && $config_ticket_client_general_notifications == 1 && filter_var($contact_email, FILTER_VALIDATE_EMAIL)) { + + $email_subject = mysqli_real_escape_string($mysqli, "Ticket created - [$ticket_prefix$ticket_number] - $ticket_subject (scheduled)"); + $email_body = mysqli_real_escape_string($mysqli, "##- Please type your reply above this line -##

Hello, $contact_name

A ticket regarding \"$ticket_subject\" has been automatically created for you.

--------------------------------
$details--------------------------------

Ticket: $ticket_prefix$ticket_number
Subject: $ticket_subject
Status: Open
Portal: https://$config_base_url/portal/ticket.php?id=$id

~
$company_name
Support Department
$config_ticket_from_email
$company_phone"); + + mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$contact_email_escaped', email_recipient_name = '$contact_name_escaped', email_from = '$config_ticket_from_email_escaped', email_from_name = '$config_ticket_from_name_escaped', email_subject = '$email_subject', email_content = '$email_body'"); } + + // Notify agent's via the DL address of the new ticket, if it's populated with a valid email + if (filter_var($config_ticket_new_ticket_notification_email, FILTER_VALIDATE_EMAIL)) { + + $email_subject = mysqli_real_escape_string($mysqli, "ITFlow - New Scheduled Ticket - $client_name: $ticket_subject"); + $email_body = mysqli_real_escape_string($mysqli, "Hello,

This is a notification that a new scheduled ticket has been raised in ITFlow.
Ticket: $ticket_prefix$ticket_number
Client: $client_name
Priority: $priority
Link: https://$config_base_url/ticket.php?ticket_id=$id

--------------------------------

$ticket_subject
$ticket_details"); + + mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$config_ticket_new_ticket_notification_email', email_recipient_name = 'ITFlow Agents', email_from = '$config_ticket_from_email', email_from_name = '$config_ticket_from_name', email_subject = '$email_subject', email_content = '$email_body'"); + } + + // Set the next run date if ($frequency == "weekly") { // Note: We seemingly have to initialize a new datetime for each loop to avoid stacking the dates @@ -451,7 +460,7 @@ if ($config_send_invoice_reminders == 1) { // Late Charges if ($config_invoice_late_fee_enable == 1) { - + $todays_date = date('Y-m-d'); $late_fee_amount = ($invoice_amount * $config_invoice_late_fee_percent) / 100; $new_invoice_amount = $invoice_amount + $late_fee_amount; @@ -464,7 +473,7 @@ if ($config_send_invoice_reminders == 1) { mysqli_query($mysqli, "INSERT INTO history SET history_status = 'Sent', history_description = 'Cron applied a late charge', history_invoice_id = $invoice_id"); mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Invoice Late Charge', notification = 'Invoice $invoice_prefix$invoice_number for $client_name in the amount of $invoice_amount was charged a late fee of $late_fee_amount', notification_action = 'invoice.php?invoice_id=$invoice_id', notification_client_id = $client_id, notification_entity_id = $invoice_id"); - + } mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Invoice Overdue', notification = 'Invoice $invoice_prefix$invoice_number for $client_name in the amount of $invoice_amount is overdue by $day days', notification_action = 'invoice.php?invoice_id=$invoice_id', notification_client_id = $client_id, notification_entity_id = $invoice_id"); @@ -673,7 +682,7 @@ while ($row = mysqli_fetch_array($sql_recurring_expenses)) { mysqli_query($mysqli,"INSERT INTO expenses SET expense_date = CURDATE(), expense_amount = $recurring_expense_amount, expense_currency_code = '$recurring_expense_currency_code', expense_account_id = $recurring_expense_account_id, expense_vendor_id = $recurring_expense_vendor_id, expense_client_id = $recurring_expense_client_id, expense_category_id = $recurring_expense_category_id, expense_description = '$recurring_expense_description', expense_reference = '$recurring_expense_reference'"); - $expense_id = mysqli_insert_id($mysqli); + $expense_id = mysqli_insert_id($mysqli); mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Expense Created', notification = 'Expense $recurring_expense_description created from recurring expenses', notification_action = 'expenses.php', notification_client_id = $recurring_expense_client_id, notification_entity_id = $expense_id"); @@ -681,7 +690,7 @@ while ($row = mysqli_fetch_array($sql_recurring_expenses)) { mysqli_query($mysqli, "UPDATE recurring_expenses SET recurring_expense_last_sent = CURDATE(), recurring_expense_next_date = $next_date_query WHERE recurring_expense_id = $recurring_expense_id"); - + } //End Recurring Invoices Loop // Logging //mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron', log_action = 'Task', log_description = 'Cron created expenses from recurring expenses'"); diff --git a/cron_ticket_email_parser.php b/cron_ticket_email_parser.php index f36be614..cc7d78ab 100644 --- a/cron_ticket_email_parser.php +++ b/cron_ticket_email_parser.php @@ -205,7 +205,7 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac $ticket_reply_type = 'Client'; // Capture just the latest/most recent email reply content - // based off the "#--itflow#" line that we prepend the outgoing emails with (similar to the old school --reply above this line--) + // based off the "##- Please type your reply above this line -##" line that we prepend the outgoing emails with $message = explode("##- Please type your reply above this line -##", $message); $message = nl2br($message[0]); $message = "Email from: $from_email at $date:-

$message"; diff --git a/post/ticket.php b/post/ticket.php index 72edd2ef..039ee5f1 100644 --- a/post/ticket.php +++ b/post/ticket.php @@ -10,9 +10,9 @@ if (isset($_POST['add_ticket'])) { $client_id = intval($_POST['client']); $assigned_to = intval($_POST['assigned_to']); - if($assigned_to == 0){ + if ($assigned_to == 0) { $ticket_status = 'Pending-Assignment'; - }else{ + } else { $ticket_status = 'Assigned'; } $contact = intval($_POST['contact']); @@ -24,7 +24,7 @@ if (isset($_POST['add_ticket'])) { $asset_id = intval($_POST['asset']); $use_primary_contact = intval($_POST['use_primary_contact']); - // Add the primary contact as the ticket contact if use primary contact is checked + // Add the primary contact as the ticket contact if "Use primary contact" is checked if ($use_primary_contact == 1) { $sql = mysqli_query($mysqli,"SELECT contact_id FROM contacts WHERE contact_client_id = $client_id AND contact_primary = 1"); $row = mysqli_fetch_array($sql); @@ -851,7 +851,7 @@ if (isset($_POST['add_invoice_from_ticket'])) { $row = mysqli_fetch_array($sql); $tax_percent = floatval($row['tax_percent']); $tax_amount = $subtotal * $tax_percent / 100; - }else{ + } else { $tax_amount = 0; } diff --git a/settings_mail_queue.php b/settings_mail_queue.php index ae25e68f..0791c6c7 100644 --- a/settings_mail_queue.php +++ b/settings_mail_queue.php @@ -110,13 +110,13 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $email_failed_at = nullable_htmlentities($row['email_failed_at']); $email_sent_at = nullable_htmlentities($row['email_sent_at']); $email_status = intval($row['email_status']); - if($email_status == 0){ + if ($email_status == 0) { $email_status_display = "
Queued
"; - }elseif($email_status == 1){ + } elseif($email_status == 1) { $email_status_display = "
Sending
"; - }elseif($email_status == 2){ + } elseif($email_status == 2) { $email_status_display = "
Failed
$email_failed_at"; - }else{ + } else { $email_status_display = "
Sent
$email_sent_at"; } @@ -132,7 +132,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); - + 3 && $email_status == 2) { ?>