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);
14 while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
15 if (trim($body[$i]) == "--$bound") {
19 /** Lets find the header for this entity **/
20 /** If the first line after the boundary is blank, we use default values **/
21 if (trim($body[$j]) == "") {
24 $charset = "us-ascii";
26 /** If the first line ISNT blank, read in the header for this entity **/
28 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
29 $entity_header[$p] = $body[$j];
33 /** All of these values are getting passed back to us **/
34 fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
38 /** OK, we have the header information, now lets decide what to do with it **/
39 if ($ent_type0 == "multipart") {
41 while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
42 $ent_body[$y] = $body[$j];
46 $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
51 while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
52 $entity_body .= $body[$j];
55 $count = count($entities);
56 $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
62 /** If this isn't a multipart message **/
65 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && ($j < count($body))) {
66 $entity_body .= $body[$j];
70 $count = count($entities);
71 $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
77 /** This gets one entity's properties **/
78 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
79 $msg["TYPE0"] = $type0;
80 $msg["TYPE1"] = $type1;
81 $msg["ENCODING"] = $encoding;
82 $msg["CHARSET"] = $charset;
83 $msg["FILENAME"] = $filename;
90 /** This will check whether or not the message contains a certain type. It
91 searches through all the entities for a match.
93 function containsType($message, $type0, $type1, &$ent_num) {
94 $type0 = strtolower($type0);
95 $type1 = strtolower($type1);
96 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
97 /** Check only on type0 **/
98 if ( $type1 == "any_type" ) {
99 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) {
104 /** Check on type0 and type1 **/
106 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) {
115 /** This returns a parsed string called $body. That string can then be displayed
116 as the actual message in the HTML. It contains everything needed, including
117 HTML Tags, Attachments at the bottom, etc.
119 function formatBody($message) {
120 include ("../config/config.php");
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($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($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($tmpbody) . "</TT>";
147 $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>";
149 /** Display the ATTACHMENTS: message if there's more than one part **/
150 if (count($message["ENTITIES"]) > 1) {
151 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
152 $body .= "<TT><B>ATTACHMENTS:</B></TT>";
153 $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
156 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
157 /** If we've displayed this entity, go to the next one **/
161 $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
162 $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
165 $filename = $message["ENTITIES"][$i]["FILENAME"];
166 if (trim($filename) == "") {
167 $display_filename = "untitled$i";
169 $display_filename = $filename;
172 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
173 $id = $message["INFO"]["ID"];
174 $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>";
176 $body .= "</TD></TR></TABLE>";
183 /** this function decodes the body depending on the encoding type. **/
184 function decodeBody($body, $encoding) {
185 $encoding = strtolower($encoding);
187 if ($encoding == "us-ascii") {
188 $newbody = $body; // if only they all were this easy
190 } else if ($encoding == "quoted-printable") {
192 $body = ereg_replace("=3D", "=", $body);
193 $body = ereg_replace("=\n", "", $body);
194 $body = ereg_replace("=20", "\n", $body);
197 } else if ($encoding == "base64") {
198 $newbody = base64_decode($body);