diff --git a/CHANGELOG.md b/CHANGELOG.md
index f54c3f0aa..0ed697000 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -66,6 +66,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
### Bug Fixes
+- Ticket emails: a picture of about 750 KB or more pasted into a reply, or into a ticket's details, left the email without its message - the picture went missing, and the words with it. The email has all of it now, whatever the picture's size. The same picture made a document come out empty in a client export, and a document template come out empty when it was exported or imported.
- 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.
@@ -113,6 +114,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- `apiKeyFromRequest()` (`functions/api_keys.php`) reads the key an API call came with for `api/v1/validate_api_key.php` - the `X-API-Key` header (`HTTP_X_API_KEY`), else the JSON body's `api_key`, else the query string's; one that's empty or not a string counts as none, a 401 with an empty body. The key was also escaped twice on its way in; it's escaped once now.
- Database 3.7.0: `api_keys.api_key_address_at`, when a key last came in a call's address (`apiKeyNoteAddress()`, at most once a minute), and `settings.config_api_key_in_address` - on where a key that hadn't expired was there at the update, off otherwise and in `db.sql`. `apiKeyInAddress()` says whether a call's query string has an `api_key`. With `apiKeyInAddressAccepted()` false (read guarded, so true before the update), `api/v1/validate_api_key.php` answers a working key in the address with a 401 and a message, and logs it as an `API` `Failed` against the key without counting it toward `apiKeyWatch()` - even when a header sends the key too, as the address is logged all the same. Admin > API Keys reads `api_key_address_at` for its flag and note, and Admin > Settings > Security saves the switch.
- Database 3.7.1: `email_queue.email_sensitive`, set by `emailQueue()`'s new `sensitive` option (`addToMailQueue()` writes it once the column is there, `mailQueueHasSensitive()`). It is passed by `shareEmailLink()`, `shareEmailCode()`, `portalInviteMail()`, `portalMailInvite()` and `portalMailSignupExists()` when the account is local, `portalMailPasswordReset()`, `userInviteMail()`, `contractSignatureRequestEmail()` and `ticketMailTaskApproval()`. `cron/mail_queue.php` marks an email sent with `mailQueueSentSql()`, which clears `email_content`, `email_cal_str` and `email_attachments` only where `email_sensitive` is 1 - every email before the update, as before. `mailQueueClearSensitive()` (nightly) clears a failed or cancelled one a day after it was queued. Retry (`mailQueueCan()`, `mailQueueAct()`) needs `email_content`, and Admin > Mail Queue reads `email_content = '' AS email_cleared` for it. The update marks the emails waiting or being sent as sensitive, as they would have been cleared.
+- `emailMessageHtml()` styled images and paragraphs with a lookahead across each tag (`(?![^>]*\bstyle=)`). On a data: image of 750 KB or more - its base64 is a million characters - that ran out of `pcre.backtrack_limit`, `preg_replace()` returned `null` and the message came out ''. It now works a tag at a time (`/<(img|p)\b([^>]*)>/`, which never backtracks into a tag) and keeps its input should a pattern fail. The same kind of scan through a tag to an attribute is replaced in `documentTemplateInlineImages()` and in the template import's alt and uploads clean-up (`functions/document_template_transfer.php`), and `clientExportLocalImages()` stops at the first `src=` and keeps its input on failure.
## [26.11.8]
diff --git a/functions/client_export.php b/functions/client_export.php
index 4eadea789..0e47529bc 100644
--- a/functions/client_export.php
+++ b/functions/client_export.php
@@ -1591,7 +1591,8 @@ function clientExportHtmlPage($title, $body, $up = '') {
* anything else is left pointing where it did.
*/
function clientExportLocalImages($html, array $roots, $image_folder, $up, callable $add_file) {
- return preg_replace_callback('/(]*\bsrc\s*=\s*")([^"]*)(")/i', function ($m) use ($roots, $image_folder, $up, $add_file) {
+ // To the first src= - not the last, which ran out of PCRE's backtracking past a large picture's data
+ return preg_replace_callback('/(
]*?\bsrc\s*=\s*")([^"]*)(")/i', function ($m) use ($roots, $image_folder, $up, $add_file) {
$src = html_entity_decode($m[2], ENT_QUOTES, 'UTF-8');
foreach ($roots as $web => $dir) {
$web_relative = ltrim($web, '/');
@@ -1609,7 +1610,7 @@ function clientExportLocalImages($html, array $roots, $image_folder, $up, callab
return $m[1] . escapeHtml($up . $target) . $m[3];
}
return $m[0];
- }, $html);
+ }, $html) ?? $html;
}
/**
diff --git a/functions/document_template_transfer.php b/functions/document_template_transfer.php
index 28cb7264c..8307f368a 100644
--- a/functions/document_template_transfer.php
+++ b/functions/document_template_transfer.php
@@ -24,15 +24,20 @@ const DOCUMENT_TEMPLATE_TRANSFER_IMAGE_MAX = 5 * 1024 * 1024;
function documentTemplateInlineImages($document_template_id, $html, &$left_out = 0) {
$document_template_id = intval($document_template_id);
$dir = documentTemplateDir($document_template_id);
- return preg_replace_callback('/(
]*?\bsrc=)(["\'])\/?uploads\/document_templates\/' . $document_template_id . '\/([^"\'\/]+)\2/i', function ($match) use ($dir, &$left_out) {
+ // A tag at a time - a lazy scan to its src= ran out of PCRE's backtracking past a large picture
+ // already in it as data, and the template went out empty
+ return preg_replace_callback('/
]*>/i', function ($tag) use ($dir, $document_template_id, &$left_out) {
+ if (!preg_match('/(\ssrc=)(["\'])\/?uploads\/document_templates\/' . $document_template_id . '\/([^"\'\/]+)\2/i', $tag[0], $match)) {
+ return $tag[0];
+ }
$path = $dir . '/' . basename($match[3]);
$info = is_file($path) && filesize($path) <= DOCUMENT_TEMPLATE_TRANSFER_IMAGE_MAX ? @getimagesize($path) : false;
if (!$info || !in_array($info['mime'], ['image/png', 'image/jpeg', 'image/gif', 'image/webp'], true)) {
$left_out++;
- return $match[0];
+ return $tag[0];
}
- return $match[1] . $match[2] . 'data:' . $info['mime'] . ';base64,' . base64_encode((string) file_get_contents($path)) . $match[2];
- }, (string) $html);
+ return str_replace($match[0], $match[1] . $match[2] . 'data:' . $info['mime'] . ';base64,' . base64_encode((string) file_get_contents($path)) . $match[2], $tag[0]);
+ }, (string) $html) ?? (string) $html;
}
/*
@@ -112,12 +117,22 @@ function documentTemplatesImportRead($contents, &$error = '') {
if ($name === '') {
continue;
}
- // An image carried as data with no alt would get one made of its data
- $content = is_scalar($t['content'] ?? null) ? preg_replace('/
]*\balt=)(?=[^>]*\bsrc=["\']data:)/i', '
]*)>/i', function ($m) {
+ return preg_match('/\salt\s*=/i', $m[1]) || !preg_match('/\ssrc\s*=\s*["\']data:/i', $m[1]) ? $m[0] : '
';
+ }, $content) ?? $content;
$content = trim($content) !== '' ? documentHtmlPurify($content) : '';
// An image still pointing at the uploads of the install it came from is no image here
$dropped = 0;
- $content = preg_replace('/
]*?\bsrc=(["\'])\/?uploads\/[^"\']*\1[^>]*>/i', '', $content, -1, $dropped);
+ $content = preg_replace_callback('/
]*>/i', function ($tag) use (&$dropped) {
+ if (preg_match('/\ssrc=(["\'])\/?uploads\//i', $tag[0])) {
+ $dropped++;
+ return '';
+ }
+ return $tag[0];
+ }, $content) ?? $content;
$fill_ins = [];
foreach (array_slice(is_array($t['fill_ins'] ?? null) ? $t['fill_ins'] : [], 0, 100) as $f) {
diff --git a/functions/email.php b/functions/email.php
index 61b9dc757..b84f1bdec 100644
--- a/functions/email.php
+++ b/functions/email.php
@@ -295,11 +295,18 @@ function emailMessageHtml($html) {
$html = documentHtmlPurify($html);
if (!empty($config_base_url)) {
$base = 'https://' . $config_base_url;
- $html = preg_replace('/\b(src|href)="\/(?!\/)/i', '$1="' . $base . '/', $html);
+ $html = preg_replace('/\b(src|href)="\/(?!\/)/i', '$1="' . $base . '/', $html) ?? $html;
}
- // Images no wider than the card, paragraphs closer together than a mail program's default
- $html = preg_replace('/
]*\bstyle=)/i', '
])(?![^>]*\bstyle=)/i', '
]*)>/i', function ($m) { + if (preg_match('/\sstyle\s*=/i', $m[2])) { + return $m[0]; + } + return '<' . $m[1] . ' style="' . (strtolower($m[1]) === 'img' ? 'max-width:100%;height:auto;' : 'margin:0 0 10px;') . '"' . $m[2] . '>'; + }, $html) ?? $html; } /*