Skip to content

Commit

Permalink
fix(encoding): better character encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Larch <[email protected]>
  • Loading branch information
miaulalala committed Oct 9, 2023
1 parent 1ec5caa commit b557369
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/IMAP/ImapMessageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ private function loadBodyData(Horde_Mime_Part $p, string $partNo, bool $isFetche
}

$data = $fetch->getBodyPart($partNo);

$p->setContents($data);
}

Expand All @@ -470,11 +469,30 @@ private function loadBodyData(Horde_Mime_Part $p, string $partNo, bool $isFetche

// Only convert encoding if it is explicitly specified in the header because text/calendar
// data is utf-8 by default.
$charset = $p->getContentTypeParameter('charset');
if ($charset !== null && strtoupper($charset) !== 'UTF-8') {
$data = mb_convert_encoding($data, 'UTF-8', $charset);
$charset = mb_detect_encoding($data, 'UTF-8', true);
if (!$charset) {
$charset = $p->getCharset();
}

// Try the email encoding first
$converted = @mb_convert_encoding($data, 'UTF-8', $p->getCharset());
if ($converted) {

Check failure on line 479 in lib/IMAP/ImapMessageFetcher.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnStatement

lib/IMAP/ImapMessageFetcher.php:479:11: InvalidReturnStatement: The inferred type 'non-empty-array<array-key, mixed|string>|non-falsy-string' does not match the declared return type 'string' for OCA\Mail\IMAP\ImapMessageFetcher::loadBodyData (see https://psalm.dev/128)

Check failure on line 479 in lib/IMAP/ImapMessageFetcher.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

InvalidReturnStatement

lib/IMAP/ImapMessageFetcher.php:479:11: InvalidReturnStatement: The inferred type 'non-empty-array<array-key, mixed|string>|non-falsy-string' does not match the declared return type 'string' for OCA\Mail\IMAP\ImapMessageFetcher::loadBodyData (see https://psalm.dev/128)

Check failure on line 479 in lib/IMAP/ImapMessageFetcher.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

InvalidReturnStatement

lib/IMAP/ImapMessageFetcher.php:479:11: InvalidReturnStatement: The inferred type 'non-empty-array<array-key, mixed|string>|non-falsy-string' does not match the declared return type 'string' for OCA\Mail\IMAP\ImapMessageFetcher::loadBodyData (see https://psalm.dev/128)
return $converted;
}

// Fallback
$charset = mb_detect_encoding($data, null, true);
$converted = @mb_convert_encoding($data, 'UTF-8', $charset);
if ($converted) {
return $converted;
}

// Might be a charset that PHP mb doesn't know how to handle, fall back to iconv
$converted = iconv($charset, 'UTF-8', $data);
if ($converted === false) {
throw new ServiceException('Could not detect message charset');
}
return (string)$data;
return $converted;
}

private function hasAttachments(Horde_Mime_Part $part): bool {
Expand Down

0 comments on commit b557369

Please sign in to comment.