@@ -364,15 +364,47 @@ public function decode8Bit($text) {
364
364
/**
365
365
* Decodes 7-Bit text.
366
366
*
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
+ *
367
376
* @param $text (string)
368
377
* 7-Bit text to convert.
369
378
*
370
379
* @return (string)
371
380
* Decoded text.
372
- *
373
- * @todo - Decode the text somehow.
374
381
*/
375
382
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
+
376
408
return $ text ;
377
409
}
378
410
0 commit comments