4347fd50880dd942c796d8d3d684313703649e3f
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 // error correcting if they didn't follow RFC standards
53 if (trim($type1) == "")
56 if ($type1 == "plain") {
57 $msg[0]["PRIORITY"] = 10;
58 for ($p = 0;$p < count($body);$p++
) {
59 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
61 } else if ($type1 == "html") {
62 $msg[0]["PRIORITY"] = 20;
63 $msg[0]["BODY"] = $body;
65 $msg[0]["PRIORITY"] = 1;
66 $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>";
67 for ($p = 1;$p < count($body);$p++
) {
69 $msg[0]["BODY"][$p] = $body[$q];
73 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
79 function formatBody($message) {
80 for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
81 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
82 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
83 $priority = $message["ENTITIES"][$i]["PRIORITY"];
87 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
90 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
91 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
92 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
97 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
98 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
99 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
103 /** UNKNOWN...SEND WHAT WE GOT **/
104 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++
) {
105 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
107 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++
) {
109 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
117 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
119 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
120 $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
124 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
126 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
127 if ($message["ENTITIES"][$i]["TYPE0"] == "image") {
128 $body[$pos] = "<TT> Image: " . strtoupper($message["ENTITIES"][$i]["TYPE1"]) . "</TT><BR>";
130 $body[$pos] = "<TT> Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
138 function decodeBody($body, $encoding) {
139 $encoding = strtolower($encoding);
140 if ($encoding == "us-ascii") {
141 $newbody = $body; // if only they all were this easy
142 } else if ($encoding == "quoted-printable") {
143 for ($q=0; $q < count($body); $q++
) {
144 if (substr(trim($body[$q]), -1) == "=") {
145 $body[$q] = trim($body[$q]);
146 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
147 } else if (substr(trim($body[$q]), -3) == "=20") {
148 $body[$q] = trim($body[$q]);
149 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
150 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
153 for ($q=0;$q < count($body);$q++
) {
154 $body[$q] = ereg_replace("=3D", "=", $body[$q]);