X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fmime.php;h=ecbc6abcf217047c0cfd5e3986afa34e64b77222;hb=81a897dced6deb1e5fc9a332dfe572818f1da0f0;hp=f00754a9811eca5dfd50dac4ead53f17dd03c82f;hpb=bcb432a37b8baaf9de42ffa0732580859cee3d56;p=squirrelmail.git diff --git a/functions/mime.php b/functions/mime.php index f00754a9..ecbc6abc 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -1,186 +1,241 @@ decodeMime: $type0/$type1
"; - if ($type0 == "multipart") { - if ($body[0] == "") - $i = 1; - else - $i = 0; + 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, $encoding, $charset, &$entities) { + if ($type0 == "multipart") { $bound = trim($bound); - $bound = "--$bound"; - while ($i < count($body)) { - if (trim($body[$i]) == $bound) { - $j = $i + 1; + $i = 0; + while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) { + if (trim($body[$i]) == "--$bound") { + $j = $i+1; $p = 0; - while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) { - $entity_header[$p] = $body[$j]; + /** Lets find the header for this entity **/ + /** If the first line after the boundary is blank, we + use default values **/ + if (trim($body[$j]) == "") { + $ent_type0 = "text"; + $ent_type1 = "plain"; + $charset = "us-ascii"; $j++; - $p++; + /** If the first line ISNT blank, read in the header + for this entity **/ + } else { + while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) { + $entity_header[$p] = $body[$j]; + $j++; + $p++; + } + /** All of these values are getting passed back to us **/ + sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename); } - fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset); - if ($ent_type0 == "text") { - while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { - $entity_body[$p] = $body[$j]; + /** OK, we have the header information, now lets decide + what to do with it **/ + if ($ent_type0 == "multipart") { + $y = 0; + while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") { + $ent_body[$y] = $body[$j]; + $y++; $j++; - $p++; } + $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $charset, $entities); + $entities = $ent; } else { - if (trim($body[$j]) == "") - $j++; - while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { + $j++; + $entity_body = ""; + while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") { $entity_body .= $body[$j]; $j++; } + $count = count($entities); + $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename); } - $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset); - - $q = count($full_message); - $full_message[$q] = $entity[0]; } $i++; } } else { - $full_message = getEntity($body, $bound, $type0, $type1); + /** If this isn't a multipart message **/ + $j = 0; + $entity_body = ""; + while ($j < count($body)) { + $entity_body .= $body[$j]; + $j++; + } + + $count = count($entities); + $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename); } - return $full_message; + return $entities; } /** This gets one entity's properties **/ - function getEntity($body, $bound, $type0, $type1, $encoding, $charset) { - echo "getEntity: $type0/$type1
"; - $msg[0]["TYPE0"] = $type0; - $msg[0]["TYPE1"] = $type1; - $msg[0]["ENCODING"] = $encoding; - $msg[0]["CHARSET"] = $charset; - - if ($type0 == "text") { - // error correcting if they didn't follow RFC standards - if (trim($type1) == "") - $type1 = "plain"; - - if ($type1 == "plain") { - $msg[0]["PRIORITY"] = 10; - for ($p = 0;$p < count($body);$p++) { - $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); + function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) { + $msg["TYPE0"] = $type0; + $msg["TYPE1"] = $type1; + $msg["ENCODING"] = $encoding; + $msg["CHARSET"] = $charset; + $msg["FILENAME"] = $filename; + + $msg["BODY"] = $body; + + return $msg; + } + + /** This will check whether or not the message contains a certain type. It + searches through all the entities for a match. + **/ + function containsType($message, $type0, $type1, &$ent_num) { + $type0 = strtolower($type0); + $type1 = strtolower($type1); + for ($i = 0; $i < count($message["ENTITIES"]); $i++) { + /** Check only on type0 **/ + if ( $type1 == "any_type" ) { + if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) { + $ent_num = $i; + return true; } - } else if ($type1 == "html") { - $msg[0]["PRIORITY"] = 20; - $msg[0]["BODY"] = $body; + + /** Check on type0 and type1 **/ } else { - $msg[0]["PRIORITY"] = 1; - $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...

"; - for ($p = 1;$p < count($body);$p++) { - $q = $p - 1; - $msg[0]["BODY"][$p] = $body[$q]; + if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) { + $ent_num = $i; + return true; } } - } else if ($type0 == "image") { - $msg[0]["PRIORITY"] == 5; - $msg[0]["BODY"][0] = $body; - } else { - $msg[0]["BODY"][0] = "This attachment is of an unknown format: $type0/$type1"; } - - return $msg; + return false; } - function formatBody($message) { - for ($i=0; $i < count($message["ENTITIES"]); $i++) { - if ($message["ENTITIES"][$i]["TYPE0"] == "text") { - if ($message["ENTITIES"][$i]["PRIORITY"] > $priority) - $priority = $message["ENTITIES"][$i]["PRIORITY"]; - } + /** This returns a parsed string called $body. That string can then + be displayed as the actual message in the HTML. It contains + everything needed, including HTML Tags, Attachments at the + bottom, etc. + **/ + function formatBody($message, $color, $wrap_at) { + + /** this if statement checks for the entity to show as the + primary message. To add more of them, just put them in the + order that is their priority. + **/ + $id = $message["INFO"]["ID"]; + $urlmailbox = urlencode($message["INFO"]["MAILBOX"]); + + if (containsType($message, "text", "html", $ent_num)) { + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; + } else if (containsType($message, "text", "plain", $ent_num)) { + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; } - - for ($i = 0; $i < count($message["ENTITIES"]); $i++) { - switch ($priority) { - /** HTML **/ - case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) { - if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) { - $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); - } - } - break; - /** PLAIN **/ - case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) { - if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) { - $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); - } - } - break; - /** UNKNOWN...SEND WHAT WE GOT **/ - case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) { - if (($message["ENTITIES"][$i]["TYPE0"] == "text")) { - $pos = count($body); - for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) { - $pos = $pos + $b; - $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b]; - } - } - } - break; + // add other primary displaying message types here + else { + // find any type that's displayable + if (containsType($message, "text", "any_type", $ent_num)) { + $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)) { + $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $charset = $message["ENTITIES"][$ent_num]["CHARSET"]; } } - for ($i = 0; $i < count($message["ENTITIES"]); $i++) { - $pos = count($body); - if ($message["ENTITIES"][$i]["TYPE0"] != "text") { - $body[$pos] = "
ATTACHMENTS:
"; - } - } + /** If there are other types that shouldn't be formatted, add + them here **/ + if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html") + $body = translateText($body, $wrap_at, $charset); - for ($i = 0; $i < count($message["ENTITIES"]); $i++) { - $pos = count($body); - if ($message["ENTITIES"][$i]["TYPE0"] != "text") { - if ($message["ENTITIES"][$i]["TYPE0"] == "image") { - $body[$pos] = "   Image: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "
"; - -/* $file = fopen("../data/tmp.png", "w"); - fwrite($file, base64_decode($message["ENTITIES"][$i]["BODY"][0])); - fclose($file); -*/ + + $body .= "
". _("Download this as a file") ."

"; + + /** Display the ATTACHMENTS: message if there's more than one part **/ + if (count($message["ENTITIES"]) > 1) { + $body .= "
"; + $body .= "ATTACHMENTS:"; + $body .= "
"; + $num = 0; + + for ($i = 0; $i < count($message["ENTITIES"]); $i++) { + /** If we've displayed this entity, go to the next one **/ + if ($ent_num == $i) + continue; + + $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]); + $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]); + + $num++; + $filename = $message["ENTITIES"][$i]["FILENAME"]; + if (trim($filename) == "") { + $display_filename = "untitled$i"; } else { - $body[$pos] = "   Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "
"; + $display_filename = $filename; } + + $urlMailbox = urlencode($message["INFO"]["MAILBOX"]); + $id = $message["INFO"]["ID"]; + $body .= "   " . $display_filename . "  (TYPE: $type0/$type1)
"; } + $body .= "
"; } - return $body; } + + + /** this function decodes the body depending on the encoding type. **/ 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") { - for ($q=0; $q < count($body); $q++) { - if (substr(trim($body[$q]), -1) == "=") { - $body[$q] = trim($body[$q]); - $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); - } else if (substr(trim($body[$q]), -3) == "=20") { - $body[$q] = trim($body[$q]); - $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); - $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno - } - } - for ($q=0;$q < count($body);$q++) { - $body[$q] = ereg_replace("=3D", "=", $body[$q]); - } - $newbody = $body; - } else { - $newbody = $body; + + if ($encoding == "quoted-printable") { + $body = quoted_printable_decode($body); + + while (ereg("=\n", $body)) + $body = ereg_replace ("=\n", "", $body); + } else if ($encoding == "base64") { + $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 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); + } + + $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); } -?> \ No newline at end of file + +?>