4 ** This contains the functions necessary to detect and decode MIME messages.
9 include "../functions/i18n.php";
11 /** This is the first function called. It decides if this is a multipart
12 message or if it should be handled as a single entity
14 function decodeMime($body, $bound, $type0, $type1, $encoding, &$entities) {
15 if ($type0 == "multipart") {
16 $bound = trim($bound);
18 while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
19 if (trim($body[$i]) == "--$bound") {
23 /** Lets find the header for this entity **/
24 /** If the first line after the boundary is blank, we use default values **/
25 if (trim($body[$j]) == "") {
28 $charset = "us-ascii";
30 /** If the first line ISNT blank, read in the header for this entity **/
32 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
33 $entity_header[$p] = $body[$j];
37 /** All of these values are getting passed back to us **/
38 sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
42 /** OK, we have the header information, now lets decide what to do with it **/
43 if ($ent_type0 == "multipart") {
45 while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
46 $ent_body[$y] = $body[$j];
50 $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
55 while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
56 $entity_body .= $body[$j];
59 $count = count($entities);
60 $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
66 /** If this isn't a multipart message **/
69 while ($j < count($body)) {
70 $entity_body .= $body[$j];
74 $count = count($entities);
75 $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
81 /** This gets one entity's properties **/
82 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
83 $msg["TYPE0"] = $type0;
84 $msg["TYPE1"] = $type1;
85 $msg["ENCODING"] = $encoding;
86 $msg["CHARSET"] = $charset;
87 $msg["FILENAME"] = $filename;
94 /** This will check whether or not the message contains a certain type. It
95 searches through all the entities for a match.
97 function containsType($message, $type0, $type1, &$ent_num) {
98 $type0 = strtolower($type0);
99 $type1 = strtolower($type1);
100 for ($i = 0; $i < count($message["ENTITIES"]); $i++
) {
101 /** Check only on type0 **/
102 if ( $type1 == "any_type" ) {
103 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) {
108 /** Check on type0 and type1 **/
110 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) {
119 /** This returns a parsed string called $body. That string can then be displayed
120 as the actual message in the HTML. It contains everything needed, including
121 HTML Tags, Attachments at the bottom, etc.
123 function formatBody($message, $color, $wrap_at) {
125 /** this if statement checks for the entity to show as the primary message. To
126 add more of them, just put them in the order that is their priority.
128 $id = $message["INFO"]["ID"];
129 $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
131 if (containsType($message, "text", "html", $ent_num)) {
132 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
133 } else if (containsType($message, "text", "plain", $ent_num)) {
134 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
136 // add other primary displaying message types here
138 // find any type that's displayable
139 if (containsType($message, "text", "any_type", $ent_num)) {
140 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
141 } else if (containsType($message, "message", "any_type", $ent_num)) {
142 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
146 /** If there are other types that shouldn't be formatted, add them here **/
147 if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html")
148 $body = translateText($body, $wrap_at);
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 == "quoted-printable") {
192 $body = quoted_printable_decode($body);
194 while (ereg("=\n", $body))
195 $body = ereg_replace ("=\n", "", $body);
196 } else if ($encoding == "base64") {
197 $body = base64_decode($body);
200 // All other encodings are returned raw.
205 // This functions decode strings that is encoded according to
206 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
207 function decodeHeader ($string) {
208 if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
210 if (ucfirst($res[2]) == "B") {
211 $replace = base64_decode($res[3]);
213 $replace = ereg_replace("_", " ", $res[3]);
214 $replace = quoted_printable_decode($replace);
217 $replace = charset_decode ($res[1], $replace);
219 $string = eregi_replace
220 ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
222 // In case there should be more encoding in the string: recurse
223 return (decodeHeader($string));