diff --git a/CHANGELOG.md b/CHANGELOG.md index afe7d7f36..a0c8fef0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,11 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - App Logs: the email parser wrote an entry every minute that mail came in or was left in the inbox ("Cron Email Parser executed in 0.16 seconds. Processed: 0 email(s), Unprocessed: 2 email(s)"), and the mail queue and email parser wrote one every minute while no mail server was set up. Neither does now. Each email is still a ticket or a reply, one that can't be is still left flagged and unread in the inbox, and an email that fails is still logged, once. With no mail server set up, Maintenance > Cron says so instead. The Mail Queue, Ticket SLA Monitor, Domain Refresher and Certificate Refresher also no longer log an error each time they run while cron is switched off - Maintenance > Cron says why they stopped, as the email parser already did. - Assets: a virtual machine can have a vendor - the provider a hosted one or VPS is rented from - with its order reference and date, on the Purchase tab like other assets. Before, only one added from a vendor's page kept its vendor. - Hosting panels: on a server where Apache hands PHP to PHP-FPM - as HestiaCP, cPanel, Plesk and most hosting control panels set it up - your logo, the favicon, avatars and the photos on contacts, locations, assets and racks didn't load, and the server answered them with an error. They load now. The folders kept private, such as backups and receipts, stay private. Installs made with the install script, which runs PHP inside Apache, were not affected. +- Tickets: a resolved ticket reopened by a reply - on the ticket page, in a bulk reply, from the client portal or through the API - kept its resolved time, and its resolution SLA stayed met, so however long it then stayed open, it was never warned or breached. Each of these now clears the resolved time and starts the resolution SLA again, as a client's emailed reply already did. Found by Mountaineer IT. +- Ticket emails: every email from the client on a ticket that was already open sent the resolution SLA warning again once the ticket was past its warning point. Only an email that reopens a resolved ticket starts the resolution SLA over now. Found by Mountaineer IT. +- Client portal: opening a ticket's Resolve or Close link directly could resolve a ticket that was already resolved, giving it a new resolved time, resolve one whose tasks weren't all done, or close one that wasn't resolved. Resolve now works only on a ticket that isn't resolved yet and has its tasks done, and Close only on a resolved one, as the ticket's page offers them. Found by Mountaineer IT. +- API: a reply could be added to a closed ticket, and a reply could set a ticket to New, Closed or a status that doesn't exist, or resolve it with tasks still open. These are now refused with a message saying why. A status set with a reply also pauses or restarts the SLA clock as it does on the ticket page, and changing a ticket's priority through the API picks its SLA again. Found by Mountaineer IT. +- Merging tickets: with Also move this ticket's replies onto it ticked, the replies moved but their attachments stayed behind on the merged ticket, so the replies showed without their files. The attachments go with the replies now. Found by Mountaineer IT. ### Developer Updates @@ -155,6 +160,8 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - `client/includes/check_login.php` ends a portal session whose contact or client is archived or when `config_client_portal_enable` is off (not View as contact). `client/login_microsoft.php` checks the switch and `contact_archived_at`, with curl timeouts. `create_stripe_checkout` keeps the session id in `$_SESSION['stripe_checkout_session_id']` and `stripe_save_card` takes only that one, and only for the client's own Stripe customer. `client_upload_document` takes the type from `finfo`. From Mountaineer IT's build of ITFlow. - `cron/ticket_email_parser.php` `addReply()`: a sender who is no contact of the client, watcher or active agent (user_type 1) is a stranger - internal note, no status change, no attachments, the assignee mail as text, no `ticket_reply_client` action. An agent's mail is an internal note by their user id. No `ticketMailClosedReply()` to an address matching the new-ticket bounce pattern. The subject is cut to 500 before escaping, and attachment names to 255 and escaped once. From Mountaineer IT's build of ITFlow. - `enable_mfa` (`agent/user/post/profile.php`) refuses while `user_token` is set, and `mfa_enforcement.php` sends a user who has one on. `login.php` tells a typed code from none with `$code_given`. `send_bulk_mail_now` and `bulk_send_client_email` need `module_client` at 2. `bulk_archive_contacts` archives a contact's user only when it archives the contact. `complete_task` and `complete_all_tasks` leave tasks with a pending or declined `task_approvals` row open. From Mountaineer IT's build of ITFlow. +- Every way out of Resolved into a working status - `add_ticket_reply` and `bulk_ticket_reply` (`agent/post/ticket.php`), `add_ticket_comment` (`client/post.php`) and `ticket_replies/create` - sets `ticket_resolved_at` to NULL and calls `resetTicketResolutionSla()` after `syncTicketSlaClock()`. `addReply()` in `cron/ticket_email_parser.php` calls `resetTicketResolutionSla()` only when the ticket was Resolved, and `syncTicketSlaClock()` otherwise. The portal's `resolve_ticket` refuses a ticket with `ticket_resolved_at` set or `ticketOpenTaskCount()` above 0, and `close_ticket` one with no `ticket_resolved_at`. From Mountaineer IT's build of ITFlow. +- `ticket_replies/create` answers 400 (`apiFormRefuse()`) for a closed ticket, a `ticket_status` not in `ticketReplyStatuses()`, or Resolved while `ticketOpenTaskCount()` is above 0, and calls `syncTicketSlaClock()` after a status change. `tickets/update` calls `applyTicketSla()` when `ticket_priority` changes. `ticketMergeInto()` with `$move_replies` moves the replies' `ticket_attachments` rows to the parent and their files from `uploads/tickets//` to `uploads/tickets//`, giving one whose name is taken there a new `randomString(32)` name. From Mountaineer IT's build of ITFlow. ## [26.11.8] diff --git a/agent/post/ticket.php b/agent/post/ticket.php index 31ed25972..1bf564454 100644 --- a/agent/post/ticket.php +++ b/agent/post/ticket.php @@ -38,6 +38,28 @@ function ticketMergeInto($child, $parent, $comment, $move_replies = false) { $parent_url = escapeSql(ticketUrl($parent_id, $parent['ticket_client_id'])); if ($move_replies) { + // The replies' attachments go with them - rows and files (uploads/tickets//) - or + // they would be left on a ticket the replies are no longer on + $sql_attachments = mysqli_query($mysqli, "SELECT ticket_attachment_id, ticket_attachment_reference_name FROM ticket_attachments + INNER JOIN ticket_replies ON ticket_reply_id = ticket_attachment_reply_id AND ticket_reply_ticket_id = $ticket_id + WHERE ticket_attachment_ticket_id = $ticket_id"); + if (mysqli_num_rows($sql_attachments)) { + $from_dir = __DIR__ . "/../../uploads/tickets/$ticket_id/"; + $to_dir = __DIR__ . "/../../uploads/tickets/$parent_id/"; + mkdirMissing(__DIR__ . "/../../uploads/tickets/"); + mkdirMissing($to_dir); + while ($attachment = mysqli_fetch_assoc($sql_attachments)) { + $attachment_id = intval($attachment['ticket_attachment_id']); + $from_name = basename((string) $attachment['ticket_attachment_reference_name']); + // Never over a file already there - it gets a new name + $to_name = file_exists($to_dir . $from_name) ? randomString(32) . '.' . strtolower(pathinfo($from_name, PATHINFO_EXTENSION)) : $from_name; + if ($from_name !== '' && is_file($from_dir . $from_name)) { + rename($from_dir . $from_name, $to_dir . $to_name); + } + $to_name_sql = escapeSql($to_name); + mysqli_query($mysqli, "UPDATE ticket_attachments SET ticket_attachment_ticket_id = $parent_id, ticket_attachment_reference_name = '$to_name_sql' WHERE ticket_attachment_id = $attachment_id"); + } + } mysqli_query($mysqli, "UPDATE ticket_replies SET ticket_reply_ticket_id = $parent_id WHERE ticket_reply_ticket_id = $ticket_id"); // The work goes over, and the driving for it with it mysqli_query($mysqli, "UPDATE trips SET trip_ticket_id = $parent_id WHERE trip_ticket_id = $ticket_id"); @@ -888,8 +910,13 @@ if (isset($_POST['bulk_ticket_reply'])) { // The files of the canned responses that went into it, on each ticket and in its email $bulk_attachments = filterEmailableAttachments(cannedResponseAttachFiles($canned_response_file_ids, $ticket_id, $bulk_reply_id, $session_user_id)); - mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $ticket_status, ticket_updated_at = NOW() WHERE ticket_id = $ticket_id"); + // Out of Resolved into a working status is a reopen - the resolved time goes and the resolution clock runs again + $reopen = $original_status === TICKET_STATUS_RESOLVED && $ticket_status !== TICKET_STATUS_RESOLVED && $ticket_status !== TICKET_STATUS_CLOSED; + mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $ticket_status, ticket_updated_at = NOW()" . ($reopen ? ", ticket_resolved_at = NULL" : "") . " WHERE ticket_id = $ticket_id"); syncTicketSlaClock($ticket_id); + if ($reopen) { + resetTicketResolutionSla($ticket_id); + } if ($ticket_status !== $original_status && $ticket_status !== TICKET_STATUS_RESOLVED) { logTicketHistory($ticket_id, "$session_name set the status to " . escapeSql(getTicketStatusName($ticket_status))); @@ -1098,9 +1125,14 @@ if (isset($_POST['add_ticket_reply'])) { } $ticket_reply = mysqli_real_escape_string($mysqli, $ticket_reply); - // Update Ticket Status & updated at (in case status didn't change) - mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $ticket_status, ticket_updated_at = NOW() WHERE ticket_id = $ticket_id"); + // Update Ticket Status & updated at (in case status didn't change). Out of Resolved into a working + // status is a reopen - the resolved time goes and the resolution clock runs again + $reopen = $original_ticket_status === TICKET_STATUS_RESOLVED && $ticket_status !== TICKET_STATUS_RESOLVED && $ticket_status !== TICKET_STATUS_CLOSED; + mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $ticket_status, ticket_updated_at = NOW()" . ($reopen ? ", ticket_resolved_at = NULL" : "") . " WHERE ticket_id = $ticket_id"); syncTicketSlaClock($ticket_id); + if ($reopen) { + resetTicketResolutionSla($ticket_id); + } // Resolve the ticket, if it is actually moving into Resolved - replying on an // already-resolved ticket must not restamp resolved_at or re-log the resolve diff --git a/api/v1/ticket_replies/create.php b/api/v1/ticket_replies/create.php index 1e42c4d3d..d30ad5335 100644 --- a/api/v1/ticket_replies/create.php +++ b/api/v1/ticket_replies/create.php @@ -34,6 +34,8 @@ require_once '../validate_api_key.php'; require_once '../require_post_method.php'; +require_once '../form_input.php'; + // Ticket/mail settings for public reply notifications require_once "../../../includes/load_global_settings.php"; @@ -62,6 +64,18 @@ if (!empty($ticket_id) && !empty($reply)) { $ticket_row = null; } + // As on the ticket page: a closed ticket takes no replies, a reply sets only a status a reply may (not New or + // Closed, which ITFlow sets, nor one that doesn't exist), and resolves only once its tasks are done + if ($ticket_row && ($ticket_row['ticket_closed_at'] !== null || intval($ticket_row['ticket_status']) === TICKET_STATUS_CLOSED)) { + apiFormRefuse('That ticket is closed - it takes no replies'); + } + if ($ticket_row && !empty($reply_ticket_status) && !in_array($reply_ticket_status, ticketReplyStatuses($ticket_row['ticket_status']), true)) { + apiFormRefuse('ticket_status must be the id of an active status a reply may set - not New or Closed'); + } + if ($ticket_row && $reply_ticket_status === TICKET_STATUS_RESOLVED && intval($ticket_row['ticket_status']) !== TICKET_STATUS_RESOLVED && ticketOpenTaskCount($ticket_id)) { + apiFormRefuse("The ticket's tasks aren't all done - it can't be resolved yet"); + } + if ($ticket_row) { $ticket_prefix = escapeSql($ticket_row['ticket_prefix']); @@ -86,7 +100,13 @@ if (!empty($ticket_id) && !empty($reply)) { // Optional status change alongside the reply if (!empty($reply_ticket_status)) { - mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $reply_ticket_status WHERE ticket_id = $ticket_id LIMIT 1"); + // Out of Resolved into a working status is a reopen - the resolved time goes and the resolution clock runs again + $reopen = $original_ticket_status === TICKET_STATUS_RESOLVED && $reply_ticket_status !== TICKET_STATUS_RESOLVED; + mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $reply_ticket_status" . ($reopen ? ", ticket_resolved_at = NULL" : "") . " WHERE ticket_id = $ticket_id LIMIT 1"); + syncTicketSlaClock($ticket_id); + if ($reopen) { + resetTicketResolutionSla($ticket_id); + } // Only record a status change when the status actually changed - // Resolved is left out because the resolve block below logs it diff --git a/api/v1/tickets/update.php b/api/v1/tickets/update.php index 8704aa51a..3cf59f852 100644 --- a/api/v1/tickets/update.php +++ b/api/v1/tickets/update.php @@ -30,6 +30,11 @@ if (!empty($ticket_id)) { logTicketHistory($ticket_id, "Edited via the API ($api_key_name)"); + // A new priority can mean another SLA - as the priority picker on the ticket page + if (stripslashes($priority) !== (string) $ticket_row['ticket_priority']) { + applyTicketSla($ticket_id); + } + // On or off a project if ($ticket_project_id !== intval($ticket_row['ticket_project_id'])) { $project = $ticket_project_id ? projectRow($ticket_project_id) : null; diff --git a/client/post.php b/client/post.php index 7e60728e1..249d4769e 100644 --- a/client/post.php +++ b/client/post.php @@ -181,10 +181,15 @@ if (isset($_POST['add_ticket_comment'])) { $original_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT ticket_status FROM tickets WHERE ticket_id = $ticket_id LIMIT 1")); $original_ticket_status = intval($original_row['ticket_status'] ?? 0); - // Back to Open - unless it is in a status of your own set to stay put when the client replies (Scheduled, say) + // Back to Open - unless it is in a status of your own set to stay put when the client replies (Scheduled, say). + // A resolved ticket is reopened - its resolved time goes and the resolution clock runs again, as by email. $new_ticket_status = ticketStatusAfterClientReply($original_ticket_status); - mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $new_ticket_status WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id LIMIT 1"); + $reopen = $original_ticket_status === TICKET_STATUS_RESOLVED; + mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $new_ticket_status" . ($reopen ? ", ticket_resolved_at = NULL" : "") . " WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id LIMIT 1"); syncTicketSlaClock($ticket_id); + if ($reopen) { + resetTicketResolutionSla($ticket_id); + } // Only record the reopen when the ticket was not already open if ($original_ticket_status !== $new_ticket_status) { @@ -400,6 +405,13 @@ if (isset($_GET['resolve_ticket'])) { // Verify the contact has access to the provided ticket ID if (verifyContactTicketAccess($ticket_id, "Open")) { + // Only a ticket that isn't resolved yet, with its tasks done - as its page offers + $resolvable = mysqli_fetch_row(mysqli_query($mysqli, "SELECT ticket_resolved_at FROM tickets WHERE ticket_id = $ticket_id LIMIT 1")); + if (!$resolvable || $resolvable[0] !== null || ticketOpenTaskCount($ticket_id)) { + flashAlert("This ticket can't be resolved yet", 'error'); + redirect("ticket.php?id=" . $ticket_id); + } + // Resolve the ticket mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 4, ticket_resolved_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id"); setTicketResolutionSlaMet($ticket_id); @@ -476,6 +488,13 @@ if (isset($_GET['close_ticket'])) { // Verify the contact has access to the provided ticket ID if (verifyContactTicketAccess($ticket_id, "Open")) { + // Only a resolved ticket closes - as its page offers + $closable = mysqli_fetch_row(mysqli_query($mysqli, "SELECT ticket_resolved_at FROM tickets WHERE ticket_id = $ticket_id LIMIT 1")); + if (!$closable || $closable[0] === null) { + flashAlert("Only a resolved ticket can be closed", 'error'); + redirect("ticket.php?id=" . $ticket_id); + } + // Fully close ticket mysqli_query($mysqli, "UPDATE tickets SET ticket_status = 5, ticket_closed_at = NOW() WHERE ticket_id = $ticket_id AND ticket_client_id = $session_client_id"); syncTicketSlaClock($ticket_id); diff --git a/cron/ticket_email_parser.php b/cron/ticket_email_parser.php index 606c9bd83..f20a47ebc 100644 --- a/cron/ticket_email_parser.php +++ b/cron/ticket_email_parser.php @@ -329,7 +329,12 @@ function addReply($from_email, $date, $subject, $ticket_number, $message, $attac // Back to Open - unless it is in a status of your own set to stay put when the client replies (Scheduled, say) $new_ticket_status = ticketStatusAfterClientReply($ticket_status); mysqli_query($mysqli, "UPDATE tickets SET ticket_status = $new_ticket_status, ticket_resolved_at = NULL WHERE ticket_id = $ticket_id AND ticket_client_id = $client_id LIMIT 1"); - resetTicketResolutionSla($ticket_id); + // A resolved ticket's resolution clock runs again - an open one keeps its alerts, so a warning isn't sent twice + if (intval($ticket_status) === TICKET_STATUS_RESOLVED) { + resetTicketResolutionSla($ticket_id); + } else { + syncTicketSlaClock($ticket_id); + } // Only record the reopen when the ticket was not already open if (intval($ticket_status) !== $new_ticket_status) {