From da0b7d272082cfd97e729010c2fe02cb423bc840 Mon Sep 17 00:00:00 2001 From: OpenXE <> Date: Sun, 29 Jan 2023 19:06:08 +0100 Subject: [PATCH] Ticket system bugfix character encoding is now read from parts and used to convert to UTF-8 --- .../MailClient/Data/MailMessagePartData.php | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/classes/Components/MailClient/Data/MailMessagePartData.php b/classes/Components/MailClient/Data/MailMessagePartData.php index 0899b25e..8210ff71 100644 --- a/classes/Components/MailClient/Data/MailMessagePartData.php +++ b/classes/Components/MailClient/Data/MailMessagePartData.php @@ -97,6 +97,33 @@ final class MailMessagePartData implements MailMessagePartInterface, JsonSeriali return $split[0]; } + /** + * @inheritDoc + */ + public function getCharset(): ?string + { + $header = $this->getHeader('content-type'); + if ($header === null) { + return ''; + } + $pattern = "/([a-zA-Z]*[\/]*[a-zA-Z]*);[a-zA-Z\n\t\r0-9 ]*charset=\"([a-zA-Z-0-9]+)\"/i"; + $matches = array(); + if (preg_match( + $pattern, + $header->getValue(), + $matches + )) { + if (count($matches) >= 3) { + return($matches[2]); + } else { + return(null); + } + } + else { + return(null); + } + } + /** * @inheritDoc */ @@ -116,17 +143,29 @@ final class MailMessagePartData implements MailMessagePartInterface, JsonSeriali /** * @return string|null */ - public function getDecodedContent(): ?string + public function getDecodedContent(string $to_charset = 'UTF-8'): ?string { + $result = ''; if ($this->content === null) { return null; } $encodingHeader = $this->getHeader('content-transfer-encoding'); if ($encodingHeader === null ) { - return $this->content; + $result = $this->content; } - return $this->decode($this->content, $encodingHeader->getValue()); + $result = $this->decode($this->content, $encodingHeader->getValue()); + + $charset = $this->getCharset(); + +// throw new InvalidArgumentException('Charset is '.$charset." Text is: ".$result); + + $converted = mb_convert_encoding( + $result, + $to_charset, + $charset + ); + return($converted); } /**