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

This commit is contained in:
johnnyq
2026-11-29 12:49:22 -05:00
parent e8c38c739e
commit 08f4c74253
5 changed files with 54 additions and 21 deletions

View File

@@ -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()];
}