4 ** This contains the functions necessary to detect and decode MIME messages.
8 function decodeMime($body, $bound, $type0, $type1) {
9 echo "$type0/$type1<BR>";
10 if ($type0 == "multipart") {
16 $bound = trim($bound);
18 while ($i < count($body)) {
19 if (trim($body[$i]) == $bound) {
23 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
24 $entity_body[$p] = $body[$j];
28 fetchEntityHeader($imapConnection, $entity_body, $ent_type0, $ent_type1, $ent_bound, &$encoding, &$charset);
29 $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset);
31 $q = count($full_message);
32 $full_message[$q] = $entity[0];
37 $full_message = getEntity($body, $bound, $type0, $type1);
43 /** This gets one entity's properties **/
44 function getEntity($body, $bound, $type0, $type1, $encoding, $charset) {
45 echo "$type0/$type1<BR>";
46 $msg[0]["TYPE0"] = $type0;
47 $msg[0]["TYPE1"] = $type1;
48 $msg[0]["ENCODING"] = $encoding;
49 $msg[0]["CHARSET"] = $charset;
51 if ($type0 == "text") {
52 if ($type1 == "plain") {
53 $msg[0]["PRIORITY"] = 10;
54 for ($p = 0;$p < count($body);$p++
) {
55 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
57 } else if ($type1 == "html") {
58 $msg[0]["PRIORITY"] = 20;
59 $msg[0]["BODY"] = $body;
61 $msg[0]["PRIORITY"] = 1;
62 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is an unknown text format: $type0/$type1</FONT></B>";
64 } else if ($type0 == "image") {
65 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This is an image. Squirrelmail currently cannot decode images.</FONT></B>";
67 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
73 function formatBody($message) {
74 for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
75 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
76 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
77 $priority = $message["ENTITIES"][$i]["PRIORITY"];
81 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
84 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
85 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
86 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
91 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
92 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
93 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
97 /** UNKNOWN...SEND WHAT WE GOT **/
98 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
99 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
101 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++
) {
103 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
113 function decodeBody($body, $encoding) {
114 $encoding = strtolower($encoding);
115 if ($encoding == "us-ascii") {
116 $newbody = $body; // if only they all were this easy
117 } else if ($encoding == "quoted-printable") {
118 for ($q=0; $q < count($body); $q++
) {
119 if (substr(trim($body[$q]), -1) == "=") {
120 $body[$q] = trim($body[$q]);
121 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
122 } else if (substr(trim($body[$q]), -3) == "=20") {
123 $body[$q] = trim($body[$q]);
124 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
125 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
128 for ($q=0;$q < count($body);$q++
) {
129 $body[$q] = ereg_replace("=3D", "=", $body[$q]);