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

fix: decoding preview texts #10647

Merged
merged 1 commit into from
Feb 4, 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 @@ -934,15 +936,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 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 @@

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