4 ** This contains the functions necessary to detect and decode MIME messages.
8 /** This is the first function called. It decides if this is a multipart
9 message or if it should be handled as a single entity
11 function decodeMime($body, $bound, $type0, $type1, &$entities) {
12 if ($type0 == "multipart") {
13 $bound = trim($bound);
15 while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
16 if (trim($body[$i]) == "--$bound") {
20 /** Lets find the header for this entity **/
21 /** If the first line after the boundary is blank, we use default values **/
22 if (trim($body[$j]) == "") {
25 $charset = "us-ascii";
27 /** If the first line ISNT blank, read in the header for this entity **/
29 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
30 $entity_header[$p] = $body[$j];
34 /** All of these values are getting passed back to us **/
35 fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
39 /** OK, we have the header information, now lets decide what to do with it **/
40 if ($ent_type0 == "multipart") {
42 while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
43 $ent_body[$y] = $body[$j];
47 $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
52 while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
53 $entity_body .= $body[$j];
56 $count = count($entities);
57 $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
63 /** If this isn't a multipart message **/
66 while ($j < count($body)) {
67 $entity_body .= $body[$j];
71 $count = count($entities);
72 $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
78 /** This gets one entity's properties **/
79 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
80 $msg["TYPE0"] = $type0;
81 $msg["TYPE1"] = $type1;
82 $msg["ENCODING"] = $encoding;
83 $msg["CHARSET"] = $charset;
84 $msg["FILENAME"] = $filename;
91 /** This will check whether or not the message contains a certain type. It
92 searches through all the entities for a match.
94 function containsType($message, $type0, $type1, &$ent_num) {
95 $type0 = strtolower($type0);
96 $type1 = strtolower($type1);
97 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
98 /** Check only on type0 **/
99 if ( $type1 == "any_type" ) {
100 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) {
105 /** Check on type0 and type1 **/
107 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) {
116 /** This returns a parsed string called $body. That string can then be displayed
117 as the actual message in the HTML. It contains everything needed, including
118 HTML Tags, Attachments at the bottom, etc.
120 function formatBody($message, $color) {
122 /** this if statement checks for the entity to show as the primary message. To
123 add more of them, just put them in the order that is their priority.
125 $id = $message["INFO"]["ID"];
126 $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
129 if (containsType($message, "text", "html", $ent_num)) {
130 $body .= decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
131 } else if (containsType($message, "text", "plain", $ent_num)) {
132 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
133 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
135 // add other primary displaying message types here
137 // find any type that's displayable
138 if (containsType($message, "text", "any_type", $ent_num)) {
139 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
140 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
141 } else if (containsType($message, "message", "any_type", $ent_num)) {
142 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
143 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
147 /** If there are other types that shouldn't be formatted, add them here **/
148 if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html")
149 $body = translateText($body);
151 $body .= "<BR><SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">Download this as a file</A></CENTER><BR></SMALL>";
153 /** Display the ATTACHMENTS: message if there's more than one part **/
154 if (count($message["ENTITIES"]) > 1) {
155 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
156 $body .= "<TT><B>ATTACHMENTS:</B></TT>";
157 $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
160 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
161 /** If we've displayed this entity, go to the next one **/
165 $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
166 $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
169 $filename = $message["ENTITIES"][$i]["FILENAME"];
170 if (trim($filename) == "") {
171 $display_filename = "untitled$i";
173 $display_filename = $filename;
176 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
177 $id = $message["INFO"]["ID"];
178 $body .= "<TT> <A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A> <SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>";
180 $body .= "</TD></TR></TABLE>";
187 /** this function decodes the body depending on the encoding type. **/
188 function decodeBody($body, $encoding) {
189 $encoding = strtolower($encoding);
191 if ($encoding == "us-ascii") {
192 $newbody = $body; // if only they all were this easy
194 } else if ($encoding == "quoted-printable") {
195 $body_ary = explode("\n", $body);
197 for ($q=0; $q < count($body_ary); $q++
) {
198 if (substr(trim($body_ary[$q]), -1) == "=") {
199 $body_ary[$q] = trim($body_ary[$q]);
200 $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-1);
201 } else if (substr(trim($body_ary[$q]), -3) == "=20") {
202 $body_ary[$q] = trim($body_ary[$q]);
203 $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-3);
204 $body_ary[$q] = "$body_ary[$q]\n";
208 for ($q=0;$q < count($body_ary);$q++
) {
209 $body_ary[$q] = ereg_replace("=3D", "=", $body_ary[$q]);
213 for ($i = 0; $i < count($body_ary); $i++
) {
214 $body .= "$body_ary[$i]\n";
218 } else if ($encoding == "base64") {
219 $newbody = base64_decode($body);