7054b41fdebd7b6a920d780abc98ba87a81f9811
4 ** This contains the functions necessary to detect and decode MIME messages.
8 function decodeMime($body, $bound, $type0, $type1) {
9 if ($type0 == "multipart") {
15 $bound = trim($bound);
17 while ($i < count($body)) {
18 if (trim($body[$i]) == $bound) {
22 while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) {
23 $entity_header[$p] = $body[$j];
28 fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
30 if ($ent_type0 == "text") {
31 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
32 $entity_body[$p] = $body[$j];
39 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
40 $entity_body .= $body[$j];
44 $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
46 $q = count($full_message);
47 $full_message[$q] = $entity[0];
52 $full_message = getEntity($body, $bound, $type0, $type1);
58 /** This gets one entity's properties **/
59 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
60 $msg[0]["TYPE0"] = $type0;
61 $msg[0]["TYPE1"] = $type1;
62 $msg[0]["ENCODING"] = $encoding;
63 $msg[0]["CHARSET"] = $charset;
64 $msg[0]["FILENAME"] = $filename;
66 echo "$type0 / $type1<BR>";
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];
89 $msg[0]["PRIORITY"] == 5;
90 $msg[0]["BODY"][0] = $body;
96 function formatBody($message) {
97 for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
98 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
99 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
100 $priority = $message["ENTITIES"][$i]["PRIORITY"];
104 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
107 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
108 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
109 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
114 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
115 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
116 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
120 /** UNKNOWN...SEND WHAT WE GOT **/
121 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
122 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
124 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++
) {
126 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
134 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
136 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
137 $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
138 $i = count($message["ENTITIES"]);
142 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
144 if (($message["ENTITIES"][$i]["TYPE0"] == "image") ||
($message["ENTITIES"][$i]["TYPE0"] == "application")){
145 $filename = $message["ENTITIES"][$i]["FILENAME"];
146 $body[$pos] = "<TT> <A HREF=\"../data/$filename\">" . $filename . "</A></TT><BR>";
148 $file = fopen("../data/$filename", "w");
149 $image = base64_decode($message["ENTITIES"][$i]["BODY"][0]);
150 fwrite($file, $image);
158 function decodeBody($body, $encoding) {
159 $encoding = strtolower($encoding);
160 if ($encoding == "us-ascii") {
161 $newbody = $body; // if only they all were this easy
162 } else if ($encoding == "quoted-printable") {
163 for ($q=0; $q < count($body); $q++
) {
164 if (substr(trim($body[$q]), -1) == "=") {
165 $body[$q] = trim($body[$q]);
166 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
167 } else if (substr(trim($body[$q]), -3) == "=20") {
168 $body[$q] = trim($body[$q]);
169 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
170 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
173 for ($q=0;$q < count($body);$q++
) {
174 $body[$q] = ereg_replace("=3D", "=", $body[$q]);