Bugfixes in MIME stuff
[squirrelmail.git] / functions / mime.php
1 <?
2 /** mime.php
3 **
4 ** This contains the functions necessary to detect and decode MIME messages.
5 **/
6
7
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
10 **/
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") {
16 $j = $i+1;
17 $p = 0;
18
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]) == "") {
22 $ent_type0 = "text";
23 $ent_type1 = "plain";
24 $charset = "us-ascii";
25 $j++;
26 /** If the first line ISNT blank, read in the header for this entity **/
27 } else {
28 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
29 $entity_header[$p] = $body[$j];
30 $j++;
31 $p++;
32 }
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);
35 }
36
37
38 /** OK, we have the header information, now lets decide what to do with it **/
39 if ($ent_type0 == "multipart") {
40 $y = 0;
41 while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
42 $ent_body[$y] = $body[$j];
43 $y++;
44 $j++;
45 }
46 $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
47 $entities = $ent;
48 } else {
49 $j++;
50 $entity_body = "";
51 while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
52 $entity_body .= $body[$j];
53 $j++;
54 }
55 $count = count($entities);
56 $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
57 }
58 }
59 $i++;
60 }
61 } else {
62 /** If this isn't a multipart message **/
63 $j = 0;
64 $entity_body = "";
65 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && ($j < count($body))) {
66 $entity_body .= $body[$j];
67 $j++;
68 }
69
70 $count = count($entities);
71 $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
72 }
73
74 return $entities;
75 }
76
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;
84
85 $msg["BODY"] = $body;
86
87 return $msg;
88 }
89
90 /** This will check whether or not the message contains a certain type. It
91 searches through all the entities for a match.
92 **/
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) ) {
100 $ent_num = $i;
101 return true;
102 }
103
104 /** Check on type0 and type1 **/
105 } else {
106 if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) {
107 $ent_num = $i;
108 return true;
109 }
110 }
111 }
112 return false;
113 }
114
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.
118 **/
119 function formatBody($message) {
120 include ("../config/config.php");
121
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.
124 **/
125 $id = $message["INFO"]["ID"];
126 $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
127 $body = "";
128
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>";
134 }
135 // add other primary displaying message types here
136 else {
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>";
144 }
145 }
146
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>";
148
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]\">";
154 $num = 0;
155
156 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
157 /** If we've displayed this entity, go to the next one **/
158 if ($ent_num == $i)
159 continue;
160
161 $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
162 $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
163
164 $num++;
165 $filename = $message["ENTITIES"][$i]["FILENAME"];
166 if (trim($filename) == "") {
167 $display_filename = "untitled$i";
168 } else {
169 $display_filename = $filename;
170 }
171
172 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
173 $id = $message["INFO"]["ID"];
174 $body .= "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A>&nbsp;&nbsp;<SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>";
175 }
176 $body .= "</TD></TR></TABLE>";
177 }
178 return $body;
179 }
180
181
182
183 /** this function decodes the body depending on the encoding type. **/
184 function decodeBody($body, $encoding) {
185 $encoding = strtolower($encoding);
186
187 if ($encoding == "us-ascii") {
188 $newbody = $body; // if only they all were this easy
189
190 } else if ($encoding == "quoted-printable") {
191 echo "$body";
192 $body = ereg_replace("=3D", "=", $body);
193 $body = ereg_replace("=\n", "", $body);
194 $body = ereg_replace("=20", "\n", $body);
195 $newbody= $body;
196
197 } else if ($encoding == "base64") {
198 $newbody = base64_decode($body);
199
200 } else {
201 $newbody = $body;
202 }
203 return $newbody;
204 }
205 ?>