Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable4.2] fix: decoding preview texts #10740

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Horde_Imap_Client_Socket;
use Horde_Mime_Exception;
use Horde_Mime_Headers;
use Horde_Mime_Headers_ContentParam_ContentType;
use Horde_Mime_Headers_ContentTransferEncoding;
use Horde_Mime_Part;
use Html2Text\Html2Text;
use OCA\Mail\Attachment;
Expand Down Expand Up @@ -932,15 +934,39 @@
return new MessageStructureData($hasAttachments, $text, $isImipMessage, $isEncrypted, false);
}

// Convert a given binary body to utf-8 according to the transfer encoding and content
// type headers of the underlying MIME part
$convertBody = function (string $body, Horde_Mime_Headers $mimeHeaders) use ($structure): string {
/** @var Horde_Mime_Headers_ContentParam_ContentType $contentType */
$contentType = $mimeHeaders->getHeader('content-type');

Check warning on line 941 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L941

Added line #L941 was not covered by tests
/** @var Horde_Mime_Headers_ContentTransferEncoding $transferEncoding */
$transferEncoding = $mimeHeaders->getHeader('content-transfer-encoding');

Check warning on line 943 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L943

Added line #L943 was not covered by tests

if (!$contentType && !$transferEncoding) {

Check warning on line 945 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L945

Added line #L945 was not covered by tests
// Nothing to convert here ...
return $body;

Check warning on line 947 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L947

Added line #L947 was not covered by tests
}

if ($transferEncoding) {
$structure->setTransferEncoding($transferEncoding->value_single);

Check warning on line 951 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L950-L951

Added lines #L950 - L951 were not covered by tests
}

if ($contentType) {
$structure->setType($contentType->value_single);
if (isset($contentType['charset'])) {
$structure->setCharset($contentType['charset']);

Check warning on line 957 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L954-L957

Added lines #L954 - L957 were not covered by tests
}
}

$structure->setContents($body);
return $this->converter->convert($structure);

Check warning on line 962 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L961-L962

Added lines #L961 - L962 were not covered by tests
};


$htmlBody = ($htmlBodyId !== null) ? $part->getBodyPart($htmlBodyId) : null;
if (!empty($htmlBody)) {
$mimeHeaders = $part->getMimeHeader($htmlBodyId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
$structure->setTransferEncoding($enc);
$structure->setContents($htmlBody);
$htmlBody = $this->converter->convert($structure);
}
$htmlBody = $convertBody($htmlBody, $mimeHeaders);

Check warning on line 969 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L969

Added line #L969 was not covered by tests
$mentionsUser = $this->checkLinks($htmlBody, $emailAddress);
$html = new Html2Text($htmlBody, ['do_links' => 'none','alt_image' => 'hide']);
return new MessageStructureData(
Expand All @@ -955,11 +981,7 @@

if (!empty($textBody)) {
$mimeHeaders = $part->getMimeHeader($textBodyId, Horde_Imap_Client_Data_Fetch::HEADER_PARSE);
if ($enc = $mimeHeaders->getValue('content-transfer-encoding')) {
$structure->setTransferEncoding($enc);
$structure->setContents($textBody);
$textBody = $this->converter->convert($structure);
}
$textBody = $convertBody($textBody, $mimeHeaders);

Check warning on line 984 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L984

Added line #L984 was not covered by tests
return new MessageStructureData(
$hasAttachments,
$textBody,
Expand Down