From d4467150fe5126007ae625edb14ae757dbd687a0 Mon Sep 17 00:00:00 2001 From: lkehresman Date: Tue, 14 Dec 1999 18:32:35 +0000 Subject: [PATCH] Added: - decoding of quoted-printable messages - reorganized the message array git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@76 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/mailbox.php | 104 ++++++++++++++++++++++++++++++++------- functions/mime.php | 108 ++++++++++++++++++++++++++++++++++++----- src/delete_message.php | 18 +++++++ src/read_body.php | 29 ++++++++--- 4 files changed, 220 insertions(+), 39 deletions(-) create mode 100644 src/delete_message.php diff --git a/functions/mailbox.php b/functions/mailbox.php index ea674fd4..8f9c92ab 100644 --- a/functions/mailbox.php +++ b/functions/mailbox.php @@ -238,7 +238,7 @@ /** This function gets all the information about a message. Including Header and body **/ function fetchMessage($imapConnection, $id) { $message["HEADER"] = fetchHeader($imapConnection, $id); - $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE"][0], $message["HEADER"]["TYPE"][1]); + $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]); return $message; } @@ -248,29 +248,66 @@ $read = fgets($imapConnection, 1024); /** defaults... if the don't get overwritten, it will display text **/ - $header["TYPE"][0] = "text"; - $header["TYPE"][1] = "plain"; + $header["TYPE0"] = "text"; + $header["TYPE1"] = "plain"; + $header["ENCODING"] = "us-ascii"; while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { /** MIME-VERSION **/ if (substr($read, 0, 17) == "MIME-Version: 1.0") { $header["MIME"] = true; $read = fgets($imapConnection, 1024); } + + /** ENCODING TYPE **/ + else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { + $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); + } + /** CONTENT-TYPE **/ else if (substr($read, 0, 13) == "Content-Type:") { - $cont = trim(substr($read, 13)); - $cont = substr($cont, 0, strpos($cont, ";")); - $header["TYPE"][0] = substr($cont, 0, strpos($cont, "/")); - $header["TYPE"][1] = substr($cont, strpos($cont, "/")+1); + $cont = strtolower(trim(substr($read, 13))); + if (strpos($cont, ";")) + $cont = substr($cont, 0, strpos($cont, ";")); + $header["TYPE0"] = substr($cont, 0, strpos($cont, "/")); + $header["TYPE1"] = substr($cont, strpos($cont, "/")+1); + $line = $read; $read = fgets($imapConnection, 1024); - if (substr(strtolower(trim($read)), 0, 9) == "boundary=") { - $bound = trim($read); - $bound = substr($bound, 9); + while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) { + str_replace("\n", "", $line); + str_replace("\n", "", $read); + $line = "$line $read"; + $read = fgets($imapConnection, 1024); + } + + /** Detect the boundary of a multipart message **/ + if (strpos(strtolower(trim($line)), "boundary=")) { + $pos = strpos($line, "boundary=") + 9; + $bound = trim($line); + if (strpos($line, " ", $pos) > 0) { + $bound = substr($bound, $pos, strpos($line, " ", $pos)); + } else { + $bound = substr($bound, $pos); + } $bound = str_replace("\"", "", $bound); $header["BOUNDARY"] = $bound; - $read = fgets($imapConnection, 1024); } + + /** Detect the charset **/ + if (strpos(strtolower(trim($line)), "charset=")) { + $pos = strpos($line, "charset=") + 8; + $charset = trim($line); + if (strpos($line, " ", $pos) > 0) { + $charset = substr($charset, $pos, strpos($line, " ", $pos)); + } else { + $charset = substr($charset, $pos); + } + $charset = str_replace("\"", "", $charset); + $header["CHARSET"] = $charset; + } else { + $header["CHARSET"] = "us-ascii"; + } + } /** FROM **/ else if (substr($read, 0, 5) == "From:") { @@ -365,23 +402,54 @@ return decodeMime($body, $bound, $type0, $type1); } - function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound) { + function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset) { /** defaults... if the don't get overwritten, it will display text **/ $type0 = "text"; $type1 = "plain"; + $encoding = "us-ascii"; $i = 0; while (trim($read[$i]) != "") { - if (substr($read[$i], 0, 13) == "Content-Type:") { - $cont = trim(substr($read[$i], 13)); - $cont = substr($cont, 0, strpos($cont, ";")); + if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { + $encoding = strtolower(trim(substr($read[$i], 26))); + + } else if (substr($read[$i], 0, 13) == "Content-Type:") { + $cont = strtolower(trim(substr($read[$i], 13))); + if (strpos($cont, ";")) + $cont = substr($cont, 0, strpos($cont, ";")); $type0 = substr($cont, 0, strpos($cont, "/")); $type1 = substr($cont, strpos($cont, "/")+1); - if (substr(strtolower(trim($read[$i])), 0, 9) == "boundary=") { - $bound = trim($read[$i]); - $bound = substr($bound, 9); + $line = $read[$i]; + while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { + str_replace("\n", "", $line); + str_replace("\n", "", $read[$i]); + $line = "$line $read[$i]"; + $i++; + } + + /** Detect the boundary of a multipart message **/ + if (strpos(strtolower(trim($line)), "boundary=")) { + $pos = strpos($line, "boundary=") + 9; + $bound = trim($line); + if (strpos($line, " ", $pos) > 0) { + $bound = substr($bound, $pos, strpos($line, " ", $pos)); + } else { + $bound = substr($bound, $pos); + } $bound = str_replace("\"", "", $bound); } + + /** Detect the charset **/ + if (strpos(strtolower(trim($line)), "charset=")) { + $pos = strpos($line, "charset=") + 8; + $charset = trim($line); + if (strpos($line, " ", $pos) > 0) { + $charset = substr($charset, $pos, strpos($line, " ", $pos)); + } else { + $charset = substr($charset, $pos); + } + $charset = str_replace("\"", "", $charset); + } } $i++; } diff --git a/functions/mime.php b/functions/mime.php index f89cdfc8..b1a9293e 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -6,6 +6,7 @@ function decodeMime($body, $bound, $type0, $type1) { + echo "$type0/$type1
"; if ($type0 == "multipart") { if ($body[0] == "") $i = 1; @@ -20,33 +21,114 @@ $p = 0; while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { - $entity[$p] = $body[$j]; + $entity_body[$p] = $body[$j]; $j++; $p++; } - fetchEntityHeader($imapConnection, $entity, $ent_type0, $ent_type1, $ent_bound); - $entity = decodeMime($entity, $ent_bound, $ent_type0, $ent_type1); + fetchEntityHeader($imapConnection, $entity_body, $ent_type0, $ent_type1, $ent_bound, &$encoding, &$charset); + $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset); $q = count($full_message); - $full_message[$q] = $entity; + $full_message[$q] = $entity[0]; } $i++; } - } else if ($type0 == "text") { - $entity_msg["TYPE0"] = "text"; + } else { + $full_message = getEntity($body, $bound, $type0, $type1); + } + + return $full_message; + } + + /** This gets one entity's properties **/ + function getEntity($body, $bound, $type0, $type1, $encoding, $charset) { + echo "$type0/$type1
"; + $msg[0]["TYPE0"] = $type0; + $msg[0]["TYPE1"] = $type1; + $msg[0]["ENCODING"] = $encoding; + $msg[0]["CHARSET"] = $charset; + + if ($type0 == "text") { if ($type1 == "plain") { - $entity_msg["TYPE1"] = "plain"; + $msg[0]["PRIORITY"] = 10; for ($p = 0;$p < count($body);$p++) { - $entity_msg["BODY"][$p] = parsePlainTextMessage($body[$p]); + $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); } - } else if ($type1 == "html") { - $entity_msg["TYPE1"] = "html"; - $entity_msg["BODY"] = $body; + $msg[0]["PRIORITY"] = 20; + $msg[0]["BODY"] = $body; + } else { + $msg[0]["PRIORITY"] = 1; + $msg[0]["BODY"][0] = "This attachment is an unknown text format: $type0/$type1"; } - $full_message[0] = $entity_msg; + } else if ($type0 == "image") { + $msg[0]["BODY"][0] = "This is an image. Squirrelmail currently cannot decode images."; + } else { + $msg[0]["BODY"][0] = "This attachment is of an unknown format: $type0/$type1"; } - return $full_message; + return $msg; + } + + 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"]; + } + } + + 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; + } + } + return $body; + } + + 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 + } + } + $newbody = $body; + } else { + $newbody = $body; + } + return $newbody; } ?> \ No newline at end of file diff --git a/src/delete_message.php b/src/delete_message.php new file mode 100644 index 00000000..b6232f74 --- /dev/null +++ b/src/delete_message.php @@ -0,0 +1,18 @@ + + + \ No newline at end of file diff --git a/src/read_body.php b/src/read_body.php index 4c751686..7e98d1a5 100644 --- a/src/read_body.php +++ b/src/read_body.php @@ -10,13 +10,15 @@ $imapConnection = loginToImapServer($username, $key, $imapServerAddress); selectMailbox($imapConnection, $mailbox, $numMessages); - echo "\n"; - displayPageHeader($mailbox); - // $message contains all information about the message // including header and body $message = fetchMessage($imapConnection, $passed_id); + echo ""; + echo ""; + echo "\n"; + displayPageHeader($mailbox); + /** translate the subject and mailbox into url-able text **/ $url_subj = urlencode(trim($message["HEADER"]["SUBJECT"])); $urlMailbox = urlencode($mailbox); @@ -141,23 +143,34 @@ } echo " \n"; - echo "
\n"; - if (count($message["ENTITIES"]) > 1) { - echo "This is a multipart MIME encoded message.
"; + echo " \n"; + $body = formatBody($message); + for ($i = 0; $i < count($body); $i++) { + echo "$body[$i]"; + } + +/* if (count($message["ENTITIES"]) > 1) { + echo "
This is a multipart MIME encoded message.
"; + echo ""; + $i = 0; + $q = 0; + $entity[0] = $i; while ($i < count($message["ENTITIES"])) { - echo "--(PART $i)--
"; + $b = $i + 1; + echo "
Part $b
"; for ($p = 0; $p < count($message["ENTITIES"][$i][0]["BODY"]); $p++) { echo $message["ENTITIES"][$i][0]["BODY"][$p]; } $i++; } } else { - echo "This is a single part mime encoded message
"; + echo "
This is a single part MIME encoded message.
"; for ($p = 0; $p < count($message["ENTITIES"][0]["BODY"]); $p++) { echo $message["ENTITIES"][0]["BODY"][$p]; } } +*/ echo "
\n"; echo "  "; echo "\n"; -- 2.25.1