+ return $props;
+ } else if ($char == "(") {
+ $end = mime_match_parenthesis (0, $structure);
+ $sub = substr($structure, 1, $end-1);
+ $props = mime_get_props($props, $sub);
+ $structure = substr($structure, strlen($sub) + 2);
+ return $props;
+ } else {
+ return $props;
+ }
+ }
+ }
+
+ // Matches parenthesis. It will return the position of the matching
+ // parenthesis in $structure. For instance, if $structure was:
+ // ("text" "plain" ("val1name", "1") nil ... )
+ // x x
+ // then this would return 42 to match up those two.
+ function mime_match_parenthesis ($pos, $structure) {
+ $char = substr($structure, $pos, 1);
+
+ // ignore all extra characters
+ while ($pos < strlen($structure)) {
+ $pos++;
+ $char = substr($structure, $pos, 1);
+ if ($char == ")") {
+ return $pos;
+ } else if ($char == "(") {
+ $pos = mime_match_parenthesis ($pos, $structure);
+ }
+ }
+ }
+
+ function mime_fetch_body ($imap_stream, $id, $ent_id) {
+ // do a bit of error correction. If we couldn't find the entity id, just guess
+ // that it is the first one. That is usually the case anyway.
+ if (!$ent_id) $ent_id = 1;
+
+ fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
+ $topline = fgets ($imap_stream, 1024);
+ $size = substr ($topline, strpos($topline, "{")+1);
+ $size = substr ($size, 0, strpos($size, "}"));
+ $read = fread ($imap_stream, $size);
+ return $read;
+ }
+
+ /* -[ END MIME DECODING ]----------------------------------------------------------- */
+
+
+
+ /** 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 ($imap_stream, $body, $header) {
+ global $username, $key, $imapServerAddress, $imapPort;
+ return mime_structure ($imap_stream, $header);
+ }
+
+ // This is here for debugging purposese. It will print out a list
+ // of all the entity IDs that are in the $message object.
+ function listEntities ($message) {
+ if ($message) {
+ if ($message->header->entity_id)
+ echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
+ for ($i = 0; $message->entities[$i]; $i++) {
+ $msg = listEntities($message->entities[$i], $ent_id);
+ if ($msg)
+ return $msg;
+ }
+ }
+ }
+
+ // returns a $message object for a particular entity id
+ function getEntity ($message, $ent_id) {
+ if ($message) {
+ if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
+ return $message;
+ } else {
+ for ($i = 0; $message->entities[$i]; $i++) {
+ $msg = getEntity ($message->entities[$i], $ent_id);
+ if ($msg)
+ return $msg;
+ }
+ }
+ }
+ }
+
+ // figures out what entity to display and returns the $message object
+ // for that entity.
+ function findDisplayEntity ($message) {
+ if ($message) {
+ if ($message->header->type0 == "text") {
+ if ($message->header->type1 == "plain" ||
+ $message->header->type1 == "html") {
+ return $message->header->entity_id;
+ }
+ } else {
+ for ($i=0; $message->entities[$i]; $i++) {
+ return findDisplayEntity($message->entities[$i]);
+ }
+ }
+ }
+ }
+
+ /** 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($imap_stream, $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.
+ global $startMessage, $username, $key, $imapServerAddress, $imapPort;
+
+ $id = $message->header->id;
+ $urlmailbox = urlencode($message->header->mailbox);
+
+ // Get the right entity and redefine message to be this entity
+ $ent_num = findDisplayEntity ($message);
+ $body_message = getEntity($message, $ent_num);
+ if (($body_message->header->type0 == "text") ||
+ ($body_message->header->type0 == "rfc822")) {
+
+ $body = mime_fetch_body ($imap_stream, $id, $ent_num);
+ $body = decodeBody($body, $body_message->header->encoding);
+
+ // If there are other types that shouldn't be formatted, add
+ // them here
+ if ($message->header->type1 != "html") {
+ $body = translateText($body, $wrap_at, $body_message->header->charset);
+ }
+
+ $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>";
+
+ /** Display the ATTACHMENTS: message if there's more than one part **/
+ if ($message->entities) {
+ $body .= "</TD></TR></TABLE>";
+ $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
+ $body .= "<TT><B>ATTACHMENTS:</B></TT>";
+ $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
+ $num = 0;
+
+ $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
+ $body .= "</TD></TR></TABLE>";
+ } else {
+ $body .= "</TD></TR></TABLE>";
+ }
+ } else {
+ $body .= "</TD></TR></TABLE>";
+ $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
+ $body .= "<TT><B>ATTACHMENTS:</B></TT>";
+ $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
+ $num = 0;
+
+ $body .= formatAttachments ($message, 999999, $message->header->mailbox, $id);
+ $body .= "</TD></TR></TABLE>";
+ }
+ return $body;
+ }
+
+ // A recursive function that returns a list of attachments with links
+ // to where to download these attachments
+ function formatAttachments ($message, $ent_id, $mailbox, $id) {
+ global $where, $what;
+ global $startMessage;
+ if ($message) {
+ if (!$message->entities) {
+ $type0 = strtolower($message->header->type0);
+ $type1 = strtolower($message->header->type1);
+
+ if ($message->header->entity_id != $ent_id) {
+ $filename = decodeHeader($message->header->filename);
+ if (trim($filename) == "") {
+ $display_filename = "untitled-".$message->header->entity_id;
+ } else {
+ $display_filename = $filename;
+ }
+
+ $urlMailbox = urlencode($mailbox);
+ $ent = urlencode($message->header->entity_id);
+ if ($where && $what) {
+ // from a search
+ $body .= "<TT> <A HREF=\"../src/download.php?startMessage=$startMessage&where=".urlencode($where)."&what=".urlencode($what)."&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A> (TYPE: $type0/$type1)";
+ } else {
+ $body .= "<TT> <A HREF=\"../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A> (TYPE: $type0/$type1)";
+ }
+ if ($message->header->description)
+ $body .= " <b>" . htmlspecialchars($message->header->description)."</b>";
+ $body .= " (<a href=\"../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">"._("download")."</a>)\n";
+ do_hook("attachment $type0/$type1");
+ $body .= "</TT><BR>";
+ $num++;
+ }
+ return $body;
+ } else {
+ for ($i = 0; $i < count($message->entities); $i++) {
+ $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);