X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fmime.php;h=ef00957c74b2519bb77c3eeb645d7466fde66b63;hb=47ca0924695dcc2d8841786b56bedf847362357c;hp=6bd52f036129422d86b51c937c5365afe9a0e800;hpb=f7835374d25ce9749a22c896694fcd2328475a53;p=squirrelmail.git diff --git a/functions/mime.php b/functions/mime.php index 6bd52f03..ef00957c 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -1,14 +1,20 @@ -" . nl2br(trim($tmpbody)) . ""; + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; } // add other primary displaying message types here else { // find any type that's displayable if (containsType($message, "text", "any_type", $ent_num)) { - $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); - $body .= "" . nl2br(trim($tmpbody)) . ""; + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; } else if (containsType($message, "message", "any_type", $ent_num)) { - $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); - $body .= "" . nl2br(trim($tmpbody)) . ""; + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; } } - /** If there are other types that shouldn't be formatted, add them here **/ + /** If there are other types that shouldn't be formatted, add + them here **/ if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html") - $body = translateText($body); + $body = translateText($body, $wrap_at, $charset); - $body .= "
Download this as a file

"; + + $body .= "
". _("Download this as a file") ."

"; /** Display the ATTACHMENTS: message if there's more than one part **/ if (count($message["ENTITIES"]) > 1) { @@ -188,39 +200,65 @@ function decodeBody($body, $encoding) { $encoding = strtolower($encoding); - if ($encoding == "us-ascii") { - $newbody = $body; // if only they all were this easy + if ($encoding == "quoted-printable") { + $body = quoted_printable_decode($body); - } else if ($encoding == "quoted-printable") { - $body_ary = explode("\n", $body); + while (ereg("=\n", $body)) + $body = ereg_replace ("=\n", "", $body); + } else if ($encoding == "base64") { + $body = base64_decode($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"; - } - } + // All other encodings are returned raw. + return $body; + } - for ($q=0;$q < count($body_ary);$q++) { - $body_ary[$q] = ereg_replace("=3D", "=", $body_ary[$q]); + + // This functions decode strings that is encoded according to + // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). + function decodeHeader ($string) { + if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=', + $string, $res)) { + if (ucfirst($res[2]) == "B") { + $replace = base64_decode($res[3]); + } else { + $replace = ereg_replace("_", " ", $res[3]); + $replace = quoted_printable_decode($replace); } - $body = ""; - for ($i = 0; $i < count($body_ary); $i++) { - $body .= "$body_ary[$i]\n"; + $replace = charset_decode ($res[1], $replace); + + $string = eregi_replace + ('=\?([^?]+)\?(q|b)\?([^?]+)\?=', + $replace, $string); + // In case there should be more encoding in the string: recurse + return (decodeHeader($string)); + } else + return ($string); + } + + // Encode a string according to RFC 1522 for use in headers if it + // contains 8-bit characters + function encodeHeader ($string) { + global $default_charset; + + // Encode only if the string contains 8-bit characters + if (ereg("[\200-\377]", $string)) { + $newstring = "=?$default_charset?Q?"; + $newstring .= str_replace(" ", "_", $string); + + while (ereg("([\200-\377])", $newstring, $regs)) { + $replace = $regs[1]; + $insert = "=" . bin2hex($replace); + $newstring = str_replace($replace, $insert, $newstring); } - $newbody = $body; - } else if ($encoding == "base64") { - $newbody = base64_decode($body); + $newstring .= "?="; - } else { - $newbody = $body; + return $newstring; } - return $newbody; + + return $string; } -?> \ No newline at end of file + +?>