Skip to content

Commit 1b615f1

Browse files
committed
ImapDriver: getHeaders fix headers parser bug, now properly handles whitespaces, normalizes UTF-8 strings and trims headers content
1 parent 8ab921d commit 1b615f1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

MailLibrary/Drivers/ImapDriver.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use greeny\MailLibrary\Mailbox;
1111
use greeny\MailLibrary\Structures\IStructure;
1212
use greeny\MailLibrary\Structures\ImapStructure;
13+
use Nette\Utils\Strings;
1314
use greeny\MailLibrary\Mail;
1415
use DateTime;
1516

@@ -227,18 +228,21 @@ public function checkFilter($key, $value = NULL) {
227228
public function getHeaders($mailId)
228229
{
229230
$raw = imap_fetchheader($this->resource, $mailId, FT_UID);
230-
$lines = explode("\n", $raw);
231+
$lines = explode("\n", Strings::fixEncoding($raw));
231232
$headers = array();
232233
$lastHeader = NULL;
234+
235+
// normalize headers
233236
foreach($lines as $line) {
234-
if(mb_substr($line, 0, 1, 'UTF-8') === " ") {
235-
$headers[$lastHeader] .= $line;
237+
$firstCharacter = mb_substr($line, 0, 1, 'UTF-8'); // todo: correct assumption that string must be UTF-8 encoded?
238+
if(preg_match('/[\pZ\pC]/u', $firstCharacter) === 1) { // search for UTF-8 whitespaces
239+
$headers[$lastHeader] .= " " . Strings::trim($line);
236240
} else {
237241
$parts = explode(':', $line);
238-
$name = $parts[0];
242+
$name = Strings::trim($parts[0]);
239243
unset($parts[0]);
240244

241-
$headers[$name] = implode(':', $parts);
245+
$headers[$name] = Strings::trim(implode(':', $parts));
242246
$lastHeader = $name;
243247
}
244248
}
@@ -254,7 +258,7 @@ public function getHeaders($mailId)
254258
$text = '';
255259
foreach($decoded as $part) {
256260
if($part->charset !== 'UTF-8' && $part->charset !== 'default') {
257-
$text .= mb_convert_encoding($part->text, 'UTF-8', $part->charset);
261+
$text .= @mb_convert_encoding($part->text, 'UTF-8', $part->charset); // todo: handle this more properly
258262
} else {
259263
$text .= $part->text;
260264
}

0 commit comments

Comments
 (0)