Skip to content

Commit a140635

Browse files
committed
Issue #12: Better 7BIT message decoding.
1 parent fa73757 commit a140635

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Imap.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,47 @@ public function decode8Bit($text) {
364364
/**
365365
* Decodes 7-Bit text.
366366
*
367+
* PHP seems to think that most emails are 7BIT-encoded, therefore this
368+
* decoding method assumes that text passed through may actually be base64-
369+
* encoded, quoted-printable encoded, or just plain text. Instead of passing
370+
* the email directly through a particular decoding function, this method
371+
* runs through a bunch of common encoding schemes to try to decode everything
372+
* and simply end up with something *resembling* plain text.
373+
*
374+
* Results are not guaranteed, but it's pretty good at what it does.
375+
*
367376
* @param $text (string)
368377
* 7-Bit text to convert.
369378
*
370379
* @return (string)
371380
* Decoded text.
372-
*
373-
* @todo - Decode the text somehow.
374381
*/
375382
public function decode7Bit($text) {
383+
// If there are no spaces on the first line, assume that the body is
384+
// actually base64-encoded, and decode it.
385+
$lines = explode("\r\n", $text);
386+
$first_line_words = explode(' ', $lines[0]);
387+
if ($first_line_words[0] == $lines[0]) {
388+
$text = base64_decode($text);
389+
}
390+
391+
// Manually convert common encoded characters into their UTF-8 equivalents.
392+
$characters = array(
393+
'=20' => ' ', // space.
394+
'=E2=80=99' => "'", // single quote.
395+
'=0A' => "\r\n", // line break.
396+
'=A0' => ' ', // non-breaking space.
397+
'=C2=A0' => ' ', // non-breaking space.
398+
"=\r\n" => '', // joined line.
399+
'=E2=80=A6' => '', // ellipsis.
400+
'=E2=80=A2' => '', // bullet.
401+
);
402+
403+
// Loop through the encoded characters and replace any that are found.
404+
foreach ($characters as $key => $value) {
405+
$text = str_replace($key, $value, $text);
406+
}
407+
376408
return $text;
377409
}
378410

0 commit comments

Comments
 (0)