4 ** This contains the functions necessary to detect and decode MIME messages.
8 function decodeMime($body, $bound, $type0, $type1) {
9 echo "<TT>decodeMime: $type0/$type1</TT><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) && (trim($body[$j]) != "")) {
24 $entity_header[$p] = $body[$j];
29 fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset);
31 if ($ent_type0 == "text") {
32 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
33 $entity_body[$p] = $body[$j];
38 if (trim($body[$j]) == "")
40 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
41 $entity_body .= $body[$j];
45 $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset);
47 $q = count($full_message);
48 $full_message[$q] = $entity[0];
53 $full_message = getEntity($body, $bound, $type0, $type1);
59 /** This gets one entity's properties **/
60 function getEntity($body, $bound, $type0, $type1, $encoding, $charset) {
61 echo "<TT>getEntity: $type0/$type1</TT><BR>";
62 $msg[0]["TYPE0"] = $type0;
63 $msg[0]["TYPE1"] = $type1;
64 $msg[0]["ENCODING"] = $encoding;
65 $msg[0]["CHARSET"] = $charset;
67 if ($type0 == "text") {
68 // error correcting if they didn't follow RFC standards
69 if (trim($type1) == "")
72 if ($type1 == "plain") {
73 $msg[0]["PRIORITY"] = 10;
74 for ($p = 0;$p < count($body);$p++
) {
75 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
77 } else if ($type1 == "html") {
78 $msg[0]["PRIORITY"] = 20;
79 $msg[0]["BODY"] = $body;
81 $msg[0]["PRIORITY"] = 1;
82 $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>";
83 for ($p = 1;$p < count($body);$p++
) {
85 $msg[0]["BODY"][$p] = $body[$q];
88 } else if ($type0 == "image") {
89 $msg[0]["PRIORITY"] == 5;
90 $msg[0]["BODY"][0] = $body;
92 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
98 function formatBody($message) {
99 for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
100 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
101 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
102 $priority = $message["ENTITIES"][$i]["PRIORITY"];
106 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
109 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
110 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
111 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
116 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
117 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
118 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
122 /** UNKNOWN...SEND WHAT WE GOT **/
123 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
124 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
126 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++
) {
128 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
136 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
138 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
139 $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
143 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
145 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
146 if ($message["ENTITIES"][$i]["TYPE0"] == "image") {
147 $body[$pos] = "<TT> Image: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
149 /* $file = fopen("../data/tmp.png", "w");
150 fwrite($file, base64_decode($message["ENTITIES"][$i]["BODY"][0]));
154 $body[$pos] = "<TT> Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
162 function decodeBody($body, $encoding) {
163 $encoding = strtolower($encoding);
164 if ($encoding == "us-ascii") {
165 $newbody = $body; // if only they all were this easy
166 } else if ($encoding == "quoted-printable") {
167 for ($q=0; $q < count($body); $q++
) {
168 if (substr(trim($body[$q]), -1) == "=") {
169 $body[$q] = trim($body[$q]);
170 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
171 } else if (substr(trim($body[$q]), -3) == "=20") {
172 $body[$q] = trim($body[$q]);
173 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
174 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
177 for ($q=0;$q < count($body);$q++
) {
178 $body[$q] = ereg_replace("=3D", "=", $body[$q]);