Skip to content

Commit

Permalink
Merge pull request #10647 from nextcloud/fix/preview-text-encoding
Browse files Browse the repository at this point in the history
fix: decoding preview texts
  • Loading branch information
st3iny authored Feb 4, 2025
2 parents 559d5dd + 945b4a2 commit 584ef81
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');
/** @var Horde_Mime_Headers_ContentTransferEncoding $transferEncoding */
$transferEncoding = $mimeHeaders->getHeader('content-transfer-encoding');

if (!$contentType && !$transferEncoding) {
// Nothing to convert here ...
return $body;
}

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

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

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


$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);
$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);
return new MessageStructureData(
$hasAttachments,
$textBody,
Expand Down

0 comments on commit 584ef81

Please sign in to comment.