function decodeBody($body, $encoding) {
$encoding = strtolower($encoding);
- if ($encoding == "us-ascii") {
- $newbody = $body; // if only they all were this easy
-
- } else if ($encoding == "quoted-printable") {
- $body_ary = explode("\n", $body);
-
- for ($q=0; $q < count($body_ary); $q++) {
- if (substr(trim($body_ary[$q]), -1) == "=") {
- $body_ary[$q] = trim($body_ary[$q]);
- $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-1);
- } else if (substr(trim($body_ary[$q]), -3) == "=20") {
- $body_ary[$q] = trim($body_ary[$q]);
- $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-3);
- $body_ary[$q] = "$body_ary[$q]\n";
- }
- }
-
- for ($q=0;$q < count($body_ary);$q++) {
- $body_ary[$q] = ereg_replace("=3D", "=", $body_ary[$q]);
- }
-
- $body = "";
- for ($i = 0; $i < count($body_ary); $i++) {
- $body .= "$body_ary[$i]\n";
- }
+ if ($encoding == "quoted-printable") {
+ $body = quoted_printable_decode($body);
- $newbody = $body;
+ while (ereg("=\n", $body))
+ $body = ereg_replace ("=\n", "", $body);
} else if ($encoding == "base64") {
- $newbody = base64_decode($body);
-
- } else {
- $newbody = $body;
+ $body = base64_decode($body);
}
- return $newbody;
+
+ // All other encodings are returned raw.
+ return $body;
}
// This functions decode strings that is encoded according to
// RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
- function rfc1522Decode ($string) {
+ function decodeHeader ($string) {
// Recognizing only US-ASCII and ISO-8859. Other charsets should
// probably be recognized as well.
if (eregi('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=',
$replace = quoted_printable_decode($replace);
}
- // Only US-ASCII and ISO-8859-1 can be displayed without further ado
- if ($res[2] != "" && $res[2] != "1") {
- // This get rid of all characters with over 0x9F
+ // All HTML characters are in the 7-bit ASCII range and can
+ // be replaced before doing anything with the 8-bi
+ // characters.
+ $replace = htmlspecialchars($replace);
+
+ if ($res[2] == 1) {
+ // This if clause is debug code. -- gustavf
+ // Latin small letter o with stroke
+ while (ereg("\370", $replace))
+ $replace = ereg_replace ("\370", "ø", $replace);
+ } else if ($res[2] == "15") {
+ // Euro sign
+ while (ereg("\244", $replace))
+ $replace = ereg_replace ("\244", "€", $replace);
+ // Latin capital letter S with caron
+ while (ereg("\246", $replace))
+ $replace = ereg_replace ("\244", "Š", $replace);
+ // Latin small letter s with caron
+ while (ereg("\250", $replace))
+ $replace = ereg_replace ("\250", "š", $replace);
+ // Latin capital letter Z with caron
+ while (ereg("\264", $replace))
+ $replace = ereg_replace ("\264", "Ž", $replace);
+ // Latin small letter z with caron
+ while (ereg("\270", $replace))
+ $replace = ereg_replace ("\270", "ž", $replace);
+ // Latin capital ligature OE
+ while (ereg("\274", $replace))
+ $replace = ereg_replace ("\274", "Œ", $replace);
+ // Latin small ligature oe
+ while (ereg("\275", $replace))
+ $replace = ereg_replace ("\275", "œ", $replace);
+ // Latin capital letter Y with diaeresis
+ while (ereg("\276", $replace))
+ $replace = ereg_replace ("\276", "Ÿ", $replace);
+ } else if ($res[2] != "") {
+ // This gets rid of all characters over 0x9F for other
+ // iso-8859 charsets.
$replace = strtr($replace, "\240\241\242\243\244\245\246\247".
"\250\251\252\253\254\255\256\257".
"\260\261\262\263\264\265\266\267".
('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=',
$replace, $string);
- return (rfc1522Decode($string));
+ // In case there should be more encoding in the string: recurse
+ return (decodeHeader($string));
} else
return ($string);
}