**/
+ if (!isset($i18n_php))
+ include "../functions/i18n.php";
+
/** This is the first function called. It decides if this is a multipart
message or if it should be handled as a single entity
**/
- function decodeMime($body, $bound, $type0, $type1, &$entities) {
+ function decodeMime($body, $bound, $type0, $type1, $encoding, &$entities) {
if ($type0 == "multipart") {
$bound = trim($bound);
$i = 0;
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]);
- }
+ if ($encoding == "quoted-printable") {
+ $body = quoted_printable_decode($body);
- $body = "";
- for ($i = 0; $i < count($body_ary); $i++) {
- $body .= "$body_ary[$i]\n";
- }
-
- $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) {
- // 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)\?([^?]+)\?=',
+ function decodeHeader ($string) {
+ if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
$string, $res)) {
- if (ucfirst($res[3]) == "B") {
- $replace = base64_decode($res[4]);
+ if (ucfirst($res[2]) == "B") {
+ $replace = base64_decode($res[3]);
} else {
- $replace = ereg_replace("_", " ", $res[4]);
+ $replace = ereg_replace("_", " ", $res[3]);
$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
- $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".
- "\270\271\272\273\274\275\276\277".
- "\300\301\302\303\304\305\306\307".
- "\310\311\312\313\314\315\316\317".
- "\320\321\322\323\324\325\326\327".
- "\330\331\332\333\334\335\336\337".
- "\340\341\342\343\344\345\346\347".
- "\350\351\352\353\354\355\356\357".
- "\360\361\362\363\364\365\366\367".
- "\370\371\372\373\374\375\376\377",
- "????????????????????????????????????????".
- "????????????????????????????????????????".
- "????????????????????????????????????????".
- "????????");
- }
+ $replace = charset_decode ($res[1], $replace);
$string = eregi_replace
- ('=\?(us-ascii|iso-8859-([0-9])+)\?(q|b)\?([^?]+)\?=',
+ ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
$replace, $string);
-
- return (rfc1522Decode($string));
+ // In case there should be more encoding in the string: recurse
+ return (decodeHeader($string));
} else
return ($string);
}