Skip to content

Commit

Permalink
fix: decoding preview texts
Browse files Browse the repository at this point in the history
Respect the content type charset when decoding bodies to use when
generating preview texts.

Signed-off-by: Richard Steinmetz <[email protected]>
  • Loading branch information
st3iny authored and backportbot[bot] committed Feb 4, 2025
1 parent a746e9e commit bbfc4eb
Showing 1 changed file with 32 additions and 10 deletions.
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 @@ -934,15 +936,39 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,
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 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
/** @var Horde_Mime_Headers_ContentTransferEncoding $transferEncoding */
$transferEncoding = $mimeHeaders->getHeader('content-transfer-encoding');

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

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

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
// Nothing to convert here ...
return $body;

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

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L949

Added line #L949 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L952-L953

Added lines #L952 - L953 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L956-L959

Added lines #L956 - L959 were not covered by tests
}
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L963-L964

Added lines #L963 - L964 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 971 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L971

Added line #L971 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 @@ -957,11 +983,7 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,

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 986 in lib/IMAP/MessageMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/IMAP/MessageMapper.php#L986

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

0 comments on commit bbfc4eb

Please sign in to comment.