made "quoted-printable" work
[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 ($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, $color) {
120
121 /** this if statement checks for the entity to show as the primary message. To
122 add more of them, just put them in the order that is their priority.
123 **/
124 $id = $message["INFO"]["ID"];
125 $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
126 $body = "";
127
128 if (containsType($message, "text", "html", $ent_num)) {
129 $body .= decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
130 } else if (containsType($message, "text", "plain", $ent_num)) {
131 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
132 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
133 }
134 // add other primary displaying message types here
135 else {
136 // find any type that's displayable
137 if (containsType($message, "text", "any_type", $ent_num)) {
138 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
139 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
140 } else if (containsType($message, "message", "any_type", $ent_num)) {
141 $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
142 $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>";
143 }
144 }
145
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);
149
150 $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>";
151
152 /** Display the ATTACHMENTS: message if there's more than one part **/
153 if (count($message["ENTITIES"]) > 1) {
154 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
155 $body .= "<TT><B>ATTACHMENTS:</B></TT>";
156 $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
157 $num = 0;
158
159 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
160 /** If we've displayed this entity, go to the next one **/
161 if ($ent_num == $i)
162 continue;
163
164 $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
165 $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
166
167 $num++;
168 $filename = $message["ENTITIES"][$i]["FILENAME"];
169 if (trim($filename) == "") {
170 $display_filename = "untitled$i";
171 } else {
172 $display_filename = $filename;
173 }
174
175 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
176 $id = $message["INFO"]["ID"];
177 $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>";
178 }
179 $body .= "</TD></TR></TABLE>";
180 }
181 return $body;
182 }
183
184
185
186 /** this function decodes the body depending on the encoding type. **/
187 function decodeBody($body, $encoding) {
188 $encoding = strtolower($encoding);
189
190 if ($encoding == "us-ascii") {
191 $newbody = $body; // if only they all were this easy
192
193 } else if ($encoding == "quoted-printable") {
194 $body_ary = explode("\n", $body);
195
196 for ($q=0; $q < count($body_ary); $q++) {
197 if (substr(trim($body_ary[$q]), -1) == "=") {
198 $body_ary[$q] = trim($body_ary[$q]);
199 $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-1);
200 } else if (substr(trim($body_ary[$q]), -3) == "=20") {
201 $body_ary[$q] = trim($body_ary[$q]);
202 $body_ary[$q] = substr($body_ary[$q], 0, strlen($body_ary[$q])-3);
203 $body_ary[$q] = "$body_ary[$q]\n";
204 }
205 }
206
207 for ($q=0;$q < count($body_ary);$q++) {
208 $body_ary[$q] = ereg_replace("=3D", "=", $body_ary[$q]);
209 }
210
211 $body = "";
212 for ($i = 0; $i < count($body_ary); $i++) {
213 $body .= "$body_ary[$i]\n";
214 }
215
216 $newbody = $body;
217 } else if ($encoding == "base64") {
218 $newbody = base64_decode($body);
219
220 } else {
221 $newbody = $body;
222 }
223 return $newbody;
224 }
225 ?>