From 08f4c74253947d888cb2b07698bb7beaa24e30a3 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sun, 29 Nov 2026 12:49:22 -0500 Subject: [PATCH] Ticket pictures review fixes: Redact takes a picture selected on its own as well as text, a picture that can't be saved stays in the text instead of being lost and the Ticket Pictures job logs a warning and carries on past a ticket that fails, new tickets for several clients keep their pictures as files straight away, and deleting a client deletes its tickets with their attachments, pictures, tasks and history instead of leaving them behind; 26.12.3 changelog entries --- CHANGELOG.md | 4 ++- agent/post/client.php | 10 ++++---- cron/ticket_pictures.php | 4 +++ functions/ticket_pictures.php | 47 +++++++++++++++++++++++++++-------- js/app.js | 10 +++++--- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 421781b6e..0dceed799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,12 +22,13 @@ Update from Maintenance > Update as usual - this release has three database chan - A document's page is easier to read: headings in proportion to the text with room between sections, and tables, code and quotes that look part of it. Under its name are its folder, its template, Markdown when it's written in it, and its review - due, or when it's next due - whose menu has Mark Reviewed and how often. The buttons are Edit, Share and Export (PDF, Markdown or Print), with the rest in the ⋮ menu, now including Template. Beside it, Contents marks the section you're reading, and Linked, Versions and History are tabs of one card: Linked shows each kind of record with its count, opening to the list. - Pictures in tickets are kept as files: a screenshot pasted or dropped into a ticket, a reply or a note - by your team, in the client portal or through the API - and the pictures in a client's email are kept with the ticket as files instead of inside its text. A ticket full of screenshots opens quickly, and so do the tickets list and the client portal. Pictures show where they were put and stay out of the ticket's list of files, and emails carry them as before. - Pictures already inside tickets move to files in the background - the new Ticket Pictures job in Maintenance > Cron does a batch every minute until every ticket is done, then keeps an eye on new ones. -- Taking a picture out of a reply or the ticket's details when editing it, or redacting it, deletes it - it no longer opens from an old link. +- Taking a picture out of a reply or the ticket's details when editing it, or redacting it, deletes it - it no longer opens from an old link. Redact now takes a picture selected on its own, as well as text. - A picture from an internal note put in a reply the client sees, or from a ticket merged into another, is copied, so it shows for the client and stays when the other is deleted. ### Bug Fixes - Documents created or edited through the API no longer get extra backslashes added before quotes and line breaks, and changing only a document's name through the API no longer changes what it says. +- Deleting a client deletes its tickets' attachments and pictures, files and all, and their tasks, watchers and history, instead of leaving them behind. ### Developer Updates @@ -46,6 +47,7 @@ Update from Maintenance > Update as usual - this release has three database chan - functions/ticket_pictures.php: ticketPicturesSaveText() (ticketPicturesSaveTicket() / ticketPicturesSaveReply()) makes data: pictures in a ticket's details or a reply its files under uploads/tickets//, as ticket_attachments rows with ticket_attachment_inline = 1, and points the at /agent/ticket_attachment.php?attachment_id=N&action=view - with 'adopt' it copies pictures of another reply or ticket of the same client (merges: any), with 'prune' it deletes the text's pictures it no longer shows. It keeps the row's updated time. Called after the saves in agent/post/ticket.php (new ticket, edit, reply, edit reply, redact, bulk reply, bulk asset tickets, split), ticketMergeInto(), client/post.php (new ticket, reply), cron/ticket_email_parser.php and the API's tickets and ticket_replies create and update; anything else is picked up by the sweep. - ticketPicturesHtml() points the pictures at /client/ticket_attachment.php in the portal, /guest/guest_ticket_attachment.php on the guest page and at the files in a client export; ticketPicturesAsData() puts a ticket's pictures back as data for an email and the task approval page. emailQueue() takes opts ticket_id (the pictures the client can see), and ticketAgentEmailContent() sets $email['ticket_pictures'] for agents' emails (internal notes' too), which emailInlineImages() turns into inline cid: pictures as before. ticketAttachmentsListedSql() keeps inline pictures out of the attachment lists; opening one isn't audit logged. - New cron job ticket_pictures (cron/ticket_pictures.php, every minute): ticketPicturesSweep() goes through tickets then replies whose text has data:image/, 500 ids at a time, for up to 25 seconds a run. +- A picture that can't be saved - the folder not writable, the attachment row not going in - stays as data in the text; ticketPicturesFailed() counts them, ticketPicturesSweep() carries on past a ticket or reply that fails, and the Ticket Pictures job logs a warning (Cron-Ticket-Pictures) when there were any. bulk_add_client_ticket keeps its pictures as files straight away; deleting a client deletes its tickets with ticketDeleteForGood(). ## [26.12.2] diff --git a/agent/post/client.php b/agent/post/client.php index 3e0987054..16e0642c8 100644 --- a/agent/post/client.php +++ b/agent/post/client.php @@ -444,14 +444,12 @@ if (isset($_GET['delete_client'])) { // Delete Software mysqli_query($mysqli, "DELETE FROM software WHERE software_client_id = $client_id"); - // Delete tickets and related data + // Delete tickets and everything that is only theirs - attachments and the pictures in their text, files and all, + // tasks, watchers and history too (functions/ticket.php) $sql = mysqli_query($mysqli, "SELECT ticket_id FROM tickets WHERE ticket_client_id = $client_id"); while($row = mysqli_fetch_assoc($sql)) { - $ticket_id = $row['ticket_id']; - mysqli_query($mysqli, "DELETE FROM ticket_replies WHERE ticket_reply_ticket_id = $ticket_id"); - mysqli_query($mysqli, "DELETE FROM ticket_views WHERE view_ticket_id = $ticket_id"); + ticketDeleteForGood(intval($row['ticket_id'])); } - mysqli_query($mysqli, "DELETE FROM tickets WHERE ticket_client_id = $client_id"); mysqli_query($mysqli, "DELETE FROM trips WHERE trip_client_id = $client_id"); // Their calls and phones go, and their phone system domains are no longer linked (functions/phone.php) if (phoneTablesReady()) { @@ -690,6 +688,8 @@ if (isset($_POST['bulk_add_client_ticket'])) { if ($template) { ticketTemplateFinish($ticket_id, $ticket_template_id, true, true, $fill_in_answers); } + // Pictures pasted into it kept as its files (functions/ticket_pictures.php) + ticketPicturesSaveTicket($ticket_id); logAudit("Ticket", "Create", "$session_name created ticket $ticket_prefix$ticket_number - $subject", $client_id, $ticket_id); triggerCustomAction('ticket_create', $ticket_id); diff --git a/cron/ticket_pictures.php b/cron/ticket_pictures.php index 75e113d42..6a39e8402 100644 --- a/cron/ticket_pictures.php +++ b/cron/ticket_pictures.php @@ -38,3 +38,7 @@ if ($swept === null) { if ($swept['texts']) { echo "Ticket pictures: {$swept['moved']} moved into files, in {$swept['texts']} tickets and replies" . ($swept['behind'] ? ' - more next run' : '') . "\n"; } +// Pictures it couldn't save stay as they were, in the text - said once a run, for whoever looks into why +if ($swept['failed']) { + logApp("Cron-Ticket-Pictures", "warning", "{$swept['failed']} picture(s) in tickets couldn't be saved as files and were left in the text - check that uploads/tickets is writable by the user cron runs as."); +} diff --git a/functions/ticket_pictures.php b/functions/ticket_pictures.php index 7643d5f49..15b794bf0 100644 --- a/functions/ticket_pictures.php +++ b/functions/ticket_pictures.php @@ -52,6 +52,13 @@ function ticketAttachmentIsPicture(array $row) { return ticketPicturesReady() && intval($row['ticket_attachment_inline'] ?? 0) === 1; } +/* How many pictures this run couldn't keep as files - left as data - with $add counted on (the Ticket Pictures cron job says so) */ +function ticketPicturesFailed($add = 0) { + static $failed = 0; + $failed += intval($add); + return $failed; +} + /* A picture's address in a ticket's text, as it goes in the HTML */ function ticketPictureUrl($attachment_id) { return '/agent/ticket_attachment.php?attachment_id=' . intval($attachment_id) . '&action=view'; @@ -96,18 +103,29 @@ function ticketPicturesFromData($html, $ticket_id, $reply_id = null) { $data = base64_decode(preg_replace('/\s+/', '', $m[2]), true); $info = $data !== false ? @getimagesizefromstring($data) : false; $ext = $info ? (TICKET_PICTURE_TYPES[$info[2]] ?? '') : ''; - if ($ext === '' || (!is_dir($dir) && !@mkdir($dir, 0775, true))) { + if ($ext === '') { return $tag[0]; } + // Left as data when it can't be saved - the folder not writable by the user PHP runs as, say $reference = randomString(32) . ".$ext"; - if (@file_put_contents($dir . $reference, $data) === false) { + if ((!is_dir($dir) && !@mkdir($dir, 0775, true)) || @file_put_contents($dir . $reference, $data) === false) { + ticketPicturesFailed(1); return $tag[0]; } $name = 'picture-' . substr(hash('sha256', $data), 0, 8) . ".$ext"; - mysqli_query($mysqli, "INSERT INTO ticket_attachments SET ticket_attachment_name = '$name', ticket_attachment_reference_name = '$reference', - ticket_attachment_inline = 1, ticket_attachment_reply_id = $reply_sql, ticket_attachment_ticket_id = $ticket_id"); + try { + $attachment_id = mysqli_query($mysqli, "INSERT INTO ticket_attachments SET ticket_attachment_name = '$name', ticket_attachment_reference_name = '$reference', + ticket_attachment_inline = 1, ticket_attachment_reply_id = $reply_sql, ticket_attachment_ticket_id = $ticket_id") ? intval(mysqli_insert_id($mysqli)) : 0; + } catch (mysqli_sql_exception $e) { + $attachment_id = 0; + } + if (!$attachment_id) { + @unlink($dir . $reference); + ticketPicturesFailed(1); + return $tag[0]; + } $saved++; - return str_replace($m[0], ' src=' . $m[1] . ticketPictureUrl(mysqli_insert_id($mysqli)) . $m[1], $tag[0]); + return str_replace($m[0], ' src=' . $m[1] . ticketPictureUrl($attachment_id) . $m[1], $tag[0]); }, $html); return $out === null ? [$html, 0] : [$out, $saved]; } @@ -334,7 +352,8 @@ function ticketPicturesAsData($html, $ticket_id, $internal = false) { * replies, in order, from where the last run got to (settings.config_ticket_pictures_ticket_id and * config_ticket_pictures_reply_id), for up to $seconds. Once through, each run looks at what is * newer, so a picture something kept as data - a recurring ticket's, say - is a file a minute - * later. ['moved' => pictures, 'texts' => details and replies changed, 'behind' => more to do]. + * later. ['moved' => pictures, 'texts' => details and replies changed, 'behind' => more to do, + * 'failed' => pictures left as data, as they couldn't be saved]. */ function ticketPicturesSweep($seconds = 25) { global $mysqli; @@ -357,14 +376,20 @@ function ticketPicturesSweep($seconds = 25) { } while ($from < $last && microtime(true) < $deadline) { $to = min($from + 500, $last); - $sql = mysqli_query($mysqli, "SELECT $key FROM $table WHERE $key > $from AND $key <= $to AND $column LIKE '%data:image/%' ORDER BY $key"); - while ($row = mysqli_fetch_row($sql)) { + // The ids read up front - once a query has failed and been caught, fetching on from a result still open throws it again + $ids = array_map('intval', array_column(mysqli_fetch_all(mysqli_query($mysqli, "SELECT $key FROM $table WHERE $key > $from AND $key <= $to AND $column LIKE '%data:image/%' ORDER BY $key")), 0)); + foreach ($ids as $id) { if (microtime(true) >= $deadline) { // Back to just before the one there was no time for - $to = intval($row[0]) - 1; + $to = $id - 1; break; } - $moved += ticketPicturesSaveText($kind, intval($row[0])); + // One that fails is left as it is, not tried every run ahead of all the others + try { + $moved += ticketPicturesSaveText($kind, $id); + } catch (Throwable $e) { + ticketPicturesFailed(1); + } $texts++; } $from = $to; @@ -374,5 +399,5 @@ function ticketPicturesSweep($seconds = 25) { $behind = true; } } - return ['moved' => $moved, 'texts' => $texts, 'behind' => $behind]; + return ['moved' => $moved, 'texts' => $texts, 'behind' => $behind, 'failed' => ticketPicturesFailed()]; } diff --git a/js/app.js b/js/app.js index a8076152d..3e885bad4 100644 --- a/js/app.js +++ b/js/app.js @@ -716,12 +716,13 @@ function itflowInit() { icon: 'permanent-pen', tooltip: 'Redact Text', onAction: function() { + // Text, or a picture on its own - a saved one's file goes when the reply is saved (functions/ticket_pictures.php) var selectedText = editor.selection.getContent({ format: 'text' }); - if (selectedText) { + if (selectedText.trim() || /