/** 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;
}
$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:") {
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++;
}
function decodeMime($body, $bound, $type0, $type1) {
+ echo "$type0/$type1<BR>";
if ($type0 == "multipart") {
if ($body[0] == "")
$i = 1;
$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<BR>";
+ $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] = "<B><FONT COLOR=DD0000>This attachment is an unknown text format: $type0/$type1</FONT></B>";
}
- $full_message[0] = $entity_msg;
+ } else if ($type0 == "image") {
+ $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This is an image. Squirrelmail currently cannot decode images.</FONT></B>";
+ } else {
+ $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
}
- 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
$imapConnection = loginToImapServer($username, $key, $imapServerAddress);
selectMailbox($imapConnection, $mailbox, $numMessages);
- echo "<HTML><BODY TEXT=\"#000000\" BGCOLOR=\"#FFFFFF\" LINK=\"#0000EE\" VLINK=\"#0000EE\" ALINK=\"#0000EE\">\n";
- displayPageHeader($mailbox);
-
// $message contains all information about the message
// including header and body
$message = fetchMessage($imapConnection, $passed_id);
+ echo "<HTML>";
+ echo "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"" . $message["HEADER"]["TYPE"][0] . "/" . $message["HEADER"]["TYPE"][1] . "; charset=" . $message["HEADER"]["CHARSET"] . "\">";
+ echo "<BODY TEXT=\"#000000\" BGCOLOR=\"#FFFFFF\" LINK=\"#0000EE\" VLINK=\"#0000EE\" ALINK=\"#0000EE\">\n";
+ displayPageHeader($mailbox);
+
/** translate the subject and mailbox into url-able text **/
$url_subj = urlencode(trim($message["HEADER"]["SUBJECT"]));
$urlMailbox = urlencode($mailbox);
}
echo " </TABLE></TD></TR>\n";
- echo " <TR><TD BGCOLOR=FFFFFF WIDTH=100%><BR>\n";
- if (count($message["ENTITIES"]) > 1) {
- echo "<B>This is a multipart MIME encoded message.<BR></B>";
+ echo " <TR><TD BGCOLOR=FFFFFF WIDTH=100%>\n";
+ $body = formatBody($message);
+ for ($i = 0; $i < count($body); $i++) {
+ echo "$body[$i]";
+ }
+
+/* if (count($message["ENTITIES"]) > 1) {
+ echo "</TD></TR><TR><TD BGCOLOR=DCDCDC><CENTER><B><FONT COLOR=DD0000>This is a multipart MIME encoded message.</FONT></B></CENTER></TD></TR><TR><TD BGCOLOR=FFFFFF WIDTH=100%>";
+ echo "";
+
$i = 0;
+ $q = 0;
+ $entity[0] = $i;
while ($i < count($message["ENTITIES"])) {
- echo "<B>--(PART $i)--</B><BR>";
+ $b = $i + 1;
+ echo "</TD></TR><TR><TD BGCOLOR=DCDCDC><CENTER>Part $b</CENTER></TD></TR><TR><TD BGCOLOR=FFFFFF WIDTH=100%>";
for ($p = 0; $p < count($message["ENTITIES"][$i][0]["BODY"]); $p++) {
echo $message["ENTITIES"][$i][0]["BODY"][$p];
}
$i++;
}
} else {
- echo "<FONT COLOR=DD0000><B>This is a single part mime encoded message</B></FONT><BR>";
+ echo "</TD></TR><TR><TD BGCOLOR=DCDCDC><CENTER><B><FONT COLOR=DD0000>This is a single part MIME encoded message.</FONT></B></CENTER></TD></TR><TR><TD BGCOLOR=FFFFFF WIDTH=100%>";
for ($p = 0; $p < count($message["ENTITIES"][0]["BODY"]); $p++) {
echo $message["ENTITIES"][0]["BODY"][$p];
}
}
+*/
echo " <BR></TD></TR>\n";
echo " <TR><TD BGCOLOR=DCDCDC> </TD></TR>";
echo "</TABLE>\n";