From ea48eb255bae656e7e429478abf8c5a3039ceed4 Mon Sep 17 00:00:00 2001 From: lkehresman Date: Fri, 23 Jun 2000 01:55:09 +0000 Subject: [PATCH] This MIME support now passes the torture test that Gustav sent to the mailing list. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@554 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- TODO | 2 + functions/mime.php | 101 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/TODO b/TODO index 1b7b0831..382ac6ac 100644 --- a/TODO +++ b/TODO @@ -14,6 +14,8 @@ initials = taken by that person a link (mcp) Spell checking (ssg) Search mailbox(es) for given criteria + - Remove subject and body requirements in Compose + - Select multiple Unsubscribe/Subscribe folders at one time Finished: diff --git a/functions/mime.php b/functions/mime.php index e46b0e92..c8399e46 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -25,7 +25,7 @@ var $type0, $type1, $boundary, $charset, $encoding; var $to, $from, $date, $cc, $bcc, $reply_to, $subject; - var $id, $mailbox; + var $id, $mailbox, $description; var $entity_id; } @@ -65,8 +65,13 @@ if ($debug_mime) echo "$read

"; // isolate the body structure and remove beginning and end parenthesis $read = trim(substr ($read, strpos($read, "bodystructure") + 13)); - $read = trim(substr ($read, 0, -2)); - $read = trim(substr ($read, 1)); + $read = trim(substr ($read, 0, -1)); + $end = mime_match_parenthesis(0, $read); + while ($end == strlen($read)-1) { + $read = trim(substr ($read, 0, -1)); + $read = trim(substr ($read, 1)); + $end = mime_match_parenthesis(0, $read); + } if ($debug_mime) echo "$read

"; @@ -82,22 +87,21 @@ if (substr($structure, 0, 1) == "(") { $ent_id = mime_new_element_level($ent_id); $start = $end = -1; + if ($debug_mime) echo "
$structure
"; do { if ($debug_mime) echo "Found entity...
"; $start = $end+1; $end = mime_match_parenthesis ($start, $structure); $element = substr($structure, $start+1, ($end - $start)-1); - $ent_id = mime_increment_id($ent_id); + $ent_id = mime_increment_id ($ent_id); $newmsg = mime_parse_structure ($element, $ent_id); $msg->addEntity ($newmsg); } while (substr($structure, $end+1, 1) == "("); } else { // parse the elements if ($debug_mime) echo "
$structure
"; - $msg->header = new msg_header(); - $msg->header = mime_get_element (&$structure, $header); - $msg->header->entity_id = $ent_id; + $msg = mime_get_element (&$structure, $msg, $ent_id); if ($debug_mime) echo "
"; } return $msg; @@ -108,14 +112,16 @@ // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments // the last number of the element id, changing 1.2 to 1.3. function mime_increment_id ($id) { + global $debug_mime; if (strpos($id, ".")) { $first = substr($id, 0, strrpos($id, ".")); - $last = substr($id, strlen($id) - strlen($first)); + $last = substr($id, strrpos($id, ".")+1); $last++; - $new = $first . $last; + $new = $first . "." .$last; } else { $new = $id + 1; } + if ($debug_mime) echo "INCREMENT: $new
"; return $new; } @@ -126,16 +132,17 @@ // to make a special case if it is the first entity_id. It // always increments it, and that works fine. function mime_new_element_level ($id) { - if (!$id) - $id = 0; - else - $id . ".0"; + if (!$id) $id = 0; + else $id = $id . ".0"; + return $id; } - function mime_get_element (&$structure, $header) { + function mime_get_element (&$structure, $msg, $ent_id) { global $debug_mime; $elem_num = 1; + $msg->header = new msg_header(); + $msg->header->entity_id = $ent_id; while (strlen($structure) > 0) { $structure = trim($structure); @@ -176,25 +183,51 @@ // This is where all the text parts get put into the header switch ($elem_num) { case 1: - $header->type0 = $text; + $msg->header->type0 = $text; if ($debug_mime) echo "type0 = $text
"; break; case 2: - $header->type1 = $text; + $msg->header->type1 = $text; if ($debug_mime) echo "type1 = $text
"; break; + case 5: + $msg->header->description = $text; + if ($debug_mime) echo "description = $text
"; + break; case 6: - $header->encoding = $text; + $msg->header->encoding = $text; if ($debug_mime) echo "encoding = $text
"; break; case 7: - $header->size = $text; + $msg->header->size = $text; if ($debug_mime) echo "size = $text
"; break; default: - if ($header->type0 == "text" && $elem_num == 8) { - $header->num_lines = $text; + if ($msg->header->type0 == "text" && $elem_num == 8) { + $msg->header->num_lines = $text; if ($debug_mime) echo "num_lines = $text
"; + } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) { + // This is an encapsulated message, so lets start all over again and + // parse this message adding it on to the existing one. + $structure = trim($structure); + if (substr($structure, 0, 1) == "(") { + $e = mime_match_parenthesis (0, $structure); + $structure = substr($structure, 0, $e); + $structure = substr($structure, 1); + $m = mime_parse_structure($structure, $msg->header->entity_id); + if (substr($structure, 1, 1) != "(") + $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id)); + if ($m->entities) { + for ($i=0; $i < count($m->entities); $i++) { + //echo "TYPE: $i - ".$m->entities[$i]->header->type0." - ".$m->entities[$i]->header->type1."
"; + $msg->addEntity($m->entities[$i]); + } + } else { + //echo "TYPE: ".$m->header->type0." - ".$m->header->type1."
"; + $msg->addEntity($m); + } + $structure = ""; + } } break; } @@ -202,11 +235,13 @@ $text = ""; } // loop through the additional properties and put those in the various headers + if ($msg->header->type0 != "message") { for ($i=0; $i < count($properties); $i++) { - $header->{$properties[$i]["name"]} = $properties[$i]["value"]; + $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"]; if ($debug_mime) echo "".$properties[$i]["name"]." = " . $properties[$i]["value"] . "
"; } - return $header; + } + return $msg; } // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't @@ -264,6 +299,7 @@ $sub = substr($structure, 1, $end-1); $props = mime_get_props($props, $sub); $structure = substr($structure, strlen($sub) + 2); + return $props; } else { return $props; } @@ -318,9 +354,21 @@ return mime_structure ($imap_stream, $header); } + function listEntities ($message) { + if ($message) { + if ($message->header->entity_id) + echo "" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "
"; + for ($i = 0; $message->entities[$i]; $i++) { + $msg = listEntities($message->entities[$i], $ent_id); + if ($msg) + return $msg; + } + } + } + function getEntity ($message, $ent_id) { if ($message) { - if ($message->header->entity_id == $ent_id) { + 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++) { @@ -402,14 +450,17 @@ if ($message->header->entity_id != $ent_id) { $filename = $message->header->filename; if (trim($filename) == "") { - $display_filename = "untitled-$ent_id"; + $display_filename = "untitled-".$message->header->entity_id; } else { $display_filename = $filename; } $urlMailbox = urlencode($mailbox); $ent = urlencode($message->header->entity_id); - $body .= "   " . $display_filename . "  (TYPE: $type0/$type1)
"; + $body .= "   " . $display_filename . "  (TYPE: $type0/$type1)"; + if ($message->header->description) + $body .= "  " . htmlspecialchars($message->header->description).""; + $body .= "
"; $num++; } return $body; -- 2.25.1