59177427 |
1 | <?php |
aceb0d5c |
2 | /** mime.php |
3 | ** |
d068c0ec |
4 | ** This contains the functions necessary to detect and decode MIME |
5 | ** messages. |
6 | ** |
aceb0d5c |
7 | **/ |
8 | |
e79bed1b |
9 | $debug_mime = false; |
d068c0ec |
10 | $mime_php = true; |
aceb0d5c |
11 | |
1fd97780 |
12 | if (!isset($i18n_php)) |
13 | include "../functions/i18n.php"; |
8beafbbc |
14 | if (!isset($imap_php)) |
15 | include "../functions/imap.php"; |
16 | if (!isset($config_php)) |
17 | include "../config/config.php"; |
18 | |
19 | |
cbcf32f6 |
20 | /** Setting up the objects that have the structure for the message **/ |
8beafbbc |
21 | |
22 | class msg_header { |
23 | /** msg_header contains generic variables for values that **/ |
24 | /** could be in a header. **/ |
25 | |
26 | var $type0, $type1, $boundary, $charset, $encoding; |
27 | var $to, $from, $date, $cc, $bcc, $reply_to, $subject; |
ea48eb25 |
28 | var $id, $mailbox, $description; |
e4a256af |
29 | var $entity_id, $message_id, $charset; |
8beafbbc |
30 | } |
31 | |
32 | class message { |
33 | /** message is the object that contains messages. It is a recursive |
34 | object in that through the $entities variable, it can contain |
35 | more objects of type message. See documentation in mime.txt for |
36 | a better description of how this works. |
37 | **/ |
38 | var $header; |
39 | var $entities; |
40 | |
41 | function addEntity ($msg) { |
42 | $this->entities[count($this->entities)] = $msg; |
43 | } |
44 | } |
1fd97780 |
45 | |
bcb432a3 |
46 | |
bcb432a3 |
47 | |
8beafbbc |
48 | /* --------------------------------------------------------------------------------- */ |
49 | /* MIME DECODING */ |
50 | /* --------------------------------------------------------------------------------- */ |
51 | |
cbcf32f6 |
52 | // This function gets the structure of a message and stores it in the "message" class. |
53 | // It will return this object for use with all relevant header information and |
54 | // fully parsed into the standard "message" object format. |
8beafbbc |
55 | function mime_structure ($imap_stream, $header) { |
e79bed1b |
56 | global $debug_mime; |
8beafbbc |
57 | sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen"); |
58 | |
59 | $id = $header->id; |
60 | fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n"); |
e79bed1b |
61 | $read = fgets ($imap_stream, 10000); |
254925d1 |
62 | $response = substr($read, 0, 4); |
63 | while ($response != "a001") { |
64 | $bodystructure = $read; |
65 | $read = fgets ($imap_stream, 10000); |
66 | $response = substr($read, 0, 4); |
67 | } |
68 | $read = strtolower($bodystructure); |
8beafbbc |
69 | |
e79bed1b |
70 | if ($debug_mime) echo "<tt>$read</tt><br><br>"; |
8beafbbc |
71 | // isolate the body structure and remove beginning and end parenthesis |
72 | $read = trim(substr ($read, strpos($read, "bodystructure") + 13)); |
ea48eb25 |
73 | $read = trim(substr ($read, 0, -1)); |
74 | $end = mime_match_parenthesis(0, $read); |
75 | while ($end == strlen($read)-1) { |
76 | $read = trim(substr ($read, 0, -1)); |
77 | $read = trim(substr ($read, 1)); |
78 | $end = mime_match_parenthesis(0, $read); |
79 | } |
8beafbbc |
80 | |
e79bed1b |
81 | if ($debug_mime) echo "<tt>$read</tt><br><br>"; |
82 | |
85daa3ad |
83 | $msg = mime_parse_structure ($read, 0); |
8beafbbc |
84 | $msg->header = $header; |
85 | return $msg; |
86 | } |
87 | |
cbcf32f6 |
88 | // this starts the parsing of a particular structure. It is called recursively, |
89 | // so it can be passed different structures. It returns an object of type |
90 | // $message. |
91 | // First, it checks to see if it is a multipart message. If it is, then it |
92 | // handles that as it sees is necessary. If it is just a regular entity, |
93 | // then it parses it and adds the necessary header information (by calling out |
94 | // to mime_get_elements() |
8beafbbc |
95 | function mime_parse_structure ($structure, $ent_id) { |
e79bed1b |
96 | global $debug_mime; |
97 | if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>"; |
8beafbbc |
98 | $msg = new message(); |
99 | if (substr($structure, 0, 1) == "(") { |
100 | $ent_id = mime_new_element_level($ent_id); |
101 | $start = $end = -1; |
ea48eb25 |
102 | if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>"; |
8beafbbc |
103 | do { |
e79bed1b |
104 | if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>"; |
8beafbbc |
105 | $start = $end+1; |
106 | $end = mime_match_parenthesis ($start, $structure); |
107 | |
108 | $element = substr($structure, $start+1, ($end - $start)-1); |
ea48eb25 |
109 | $ent_id = mime_increment_id ($ent_id); |
8beafbbc |
110 | $newmsg = mime_parse_structure ($element, $ent_id); |
111 | $msg->addEntity ($newmsg); |
112 | } while (substr($structure, $end+1, 1) == "("); |
113 | } else { |
114 | // parse the elements |
e79bed1b |
115 | if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>"; |
ea48eb25 |
116 | $msg = mime_get_element (&$structure, $msg, $ent_id); |
e79bed1b |
117 | if ($debug_mime) echo "<br>"; |
8beafbbc |
118 | } |
119 | return $msg; |
e79bed1b |
120 | if ($debug_mime) echo "<font color=008800><tt> END: mime_parse_structure()</tt></font><br>"; |
8beafbbc |
121 | } |
122 | |
123 | // Increments the element ID. An element id can look like any of |
124 | // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments |
125 | // the last number of the element id, changing 1.2 to 1.3. |
126 | function mime_increment_id ($id) { |
ea48eb25 |
127 | global $debug_mime; |
8beafbbc |
128 | if (strpos($id, ".")) { |
129 | $first = substr($id, 0, strrpos($id, ".")); |
ea48eb25 |
130 | $last = substr($id, strrpos($id, ".")+1); |
8beafbbc |
131 | $last++; |
ea48eb25 |
132 | $new = $first . "." .$last; |
8beafbbc |
133 | } else { |
134 | $new = $id + 1; |
135 | } |
ea48eb25 |
136 | if ($debug_mime) echo "<b>INCREMENT: $new</b><br>"; |
8beafbbc |
137 | return $new; |
138 | } |
139 | |
140 | // See comment for mime_increment_id(). |
141 | // This adds another level on to the entity_id changing 1.3 to 1.3.0 |
142 | // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented |
143 | // before it can be used. I left it this way so as not to have |
144 | // to make a special case if it is the first entity_id. It |
145 | // always increments it, and that works fine. |
146 | function mime_new_element_level ($id) { |
ea48eb25 |
147 | if (!$id) $id = 0; |
148 | else $id = $id . ".0"; |
149 | |
8beafbbc |
150 | return $id; |
151 | } |
152 | |
ea48eb25 |
153 | function mime_get_element (&$structure, $msg, $ent_id) { |
e79bed1b |
154 | global $debug_mime; |
8beafbbc |
155 | $elem_num = 1; |
ea48eb25 |
156 | $msg->header = new msg_header(); |
157 | $msg->header->entity_id = $ent_id; |
8beafbbc |
158 | |
159 | while (strlen($structure) > 0) { |
160 | $structure = trim($structure); |
161 | $char = substr($structure, 0, 1); |
162 | |
163 | if (substr($structure, 0, 3) == "nil") { |
164 | $text = ""; |
165 | $structure = substr($structure, 3); |
166 | } else if ($char == "\"") { |
167 | // loop through until we find the matching quote, and return that as a string |
168 | $pos = 1; |
169 | $char = substr($structure, $pos, 1); |
170 | while ($char != "\"" && $pos < strlen($structure)) { |
171 | $text .= $char; |
172 | $pos++; |
173 | $char = substr($structure, $pos, 1); |
174 | } |
175 | $structure = substr($structure, strlen($text) + 2); |
176 | } else if ($char == "(") { |
177 | // comment me |
178 | $end = mime_match_parenthesis (0, $structure); |
179 | $sub = substr($structure, 1, $end-1); |
180 | $properties = mime_get_props($properties, $sub); |
181 | $structure = substr($structure, strlen($sub) + 2); |
182 | } else { |
183 | // loop through until we find a space or an end parenthesis |
184 | $pos = 0; |
185 | $char = substr($structure, $pos, 1); |
186 | while ($char != " " && $char != ")" && $pos < strlen($structure)) { |
187 | $text .= $char; |
188 | $pos++; |
189 | $char = substr($structure, $pos, 1); |
aceb0d5c |
190 | } |
8beafbbc |
191 | $structure = substr($structure, strlen($text)); |
aceb0d5c |
192 | } |
e79bed1b |
193 | if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>"; |
8beafbbc |
194 | |
195 | // This is where all the text parts get put into the header |
196 | switch ($elem_num) { |
197 | case 1: |
ea48eb25 |
198 | $msg->header->type0 = $text; |
e79bed1b |
199 | if ($debug_mime) echo "<tt>type0 = $text</tt><br>"; |
8beafbbc |
200 | break; |
201 | case 2: |
ea48eb25 |
202 | $msg->header->type1 = $text; |
e79bed1b |
203 | if ($debug_mime) echo "<tt>type1 = $text</tt><br>"; |
8beafbbc |
204 | break; |
ea48eb25 |
205 | case 5: |
206 | $msg->header->description = $text; |
207 | if ($debug_mime) echo "<tt>description = $text</tt><br>"; |
208 | break; |
8beafbbc |
209 | case 6: |
ea48eb25 |
210 | $msg->header->encoding = $text; |
e79bed1b |
211 | if ($debug_mime) echo "<tt>encoding = $text</tt><br>"; |
8beafbbc |
212 | break; |
213 | case 7: |
ea48eb25 |
214 | $msg->header->size = $text; |
e79bed1b |
215 | if ($debug_mime) echo "<tt>size = $text</tt><br>"; |
8beafbbc |
216 | break; |
217 | default: |
ea48eb25 |
218 | if ($msg->header->type0 == "text" && $elem_num == 8) { |
cbcf32f6 |
219 | // This is a plain text message, so lets get the number of lines |
220 | // that it contains. |
ea48eb25 |
221 | $msg->header->num_lines = $text; |
e79bed1b |
222 | if ($debug_mime) echo "<tt>num_lines = $text</tt><br>"; |
cbcf32f6 |
223 | |
ea48eb25 |
224 | } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) { |
225 | // This is an encapsulated message, so lets start all over again and |
226 | // parse this message adding it on to the existing one. |
227 | $structure = trim($structure); |
228 | if (substr($structure, 0, 1) == "(") { |
229 | $e = mime_match_parenthesis (0, $structure); |
230 | $structure = substr($structure, 0, $e); |
231 | $structure = substr($structure, 1); |
232 | $m = mime_parse_structure($structure, $msg->header->entity_id); |
cbcf32f6 |
233 | |
234 | // the following conditional is there to correct a bug that wasn't |
235 | // incrementing the entity IDs correctly because of the special case |
236 | // that message/rfc822 is. This fixes it fine. |
ea48eb25 |
237 | if (substr($structure, 1, 1) != "(") |
238 | $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id)); |
cbcf32f6 |
239 | |
240 | // Now we'll go through and reformat the results. |
ea48eb25 |
241 | if ($m->entities) { |
242 | for ($i=0; $i < count($m->entities); $i++) { |
ea48eb25 |
243 | $msg->addEntity($m->entities[$i]); |
244 | } |
245 | } else { |
ea48eb25 |
246 | $msg->addEntity($m); |
247 | } |
248 | $structure = ""; |
249 | } |
8beafbbc |
250 | } |
251 | break; |
252 | } |
253 | $elem_num++; |
254 | $text = ""; |
255 | } |
256 | // loop through the additional properties and put those in the various headers |
ea48eb25 |
257 | if ($msg->header->type0 != "message") { |
cbcf32f6 |
258 | for ($i=0; $i < count($properties); $i++) { |
259 | $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"]; |
260 | if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>"; |
261 | } |
ea48eb25 |
262 | } |
e4a256af |
263 | |
ea48eb25 |
264 | return $msg; |
8beafbbc |
265 | } |
266 | |
267 | // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't |
268 | // figure out how to do this part, so I decided to go to bed. I woke up |
269 | // in the morning and had a flash of insight. I went to the white-board |
270 | // and scribbled it out, then spent a bit programming it, and this is the |
271 | // result. Nothing complicated, but I think my brain was fried yesterday. |
cbcf32f6 |
272 | // Funny how that happens some times. |
8beafbbc |
273 | // |
274 | // This gets properties in a nested parenthesisized list. For example, |
275 | // this would get passed something like: ("attachment" ("filename" "luke.tar.gz")) |
276 | // This returns an array called $props with all paired up properties. |
277 | // It ignores the "attachment" for now, maybe that should change later |
278 | // down the road. In this case, what is returned is: |
279 | // $props[0]["name"] = "filename"; |
280 | // $props[0]["value"] = "luke.tar.gz"; |
281 | function mime_get_props ($props, $structure) { |
e79bed1b |
282 | global $debug_mime; |
8beafbbc |
283 | while (strlen($structure) > 0) { |
284 | $structure = trim($structure); |
285 | $char = substr($structure, 0, 1); |
286 | |
287 | if ($char == "\"") { |
288 | $pos = 1; |
289 | $char = substr($structure, $pos, 1); |
290 | while ($char != "\"" && $pos < strlen($structure)) { |
291 | $tmp .= $char; |
292 | $pos++; |
293 | $char = substr($structure, $pos, 1); |
294 | } |
295 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
296 | $char = substr($structure, 0, 1); |
297 | |
298 | if ($char == "\"") { |
299 | $pos = 1; |
300 | $char = substr($structure, $pos, 1); |
301 | while ($char != "\"" && $pos < strlen($structure)) { |
302 | $value .= $char; |
303 | $pos++; |
304 | $char = substr($structure, $pos, 1); |
305 | } |
306 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
307 | |
308 | $k = count($props); |
309 | $props[$k]["name"] = $tmp; |
310 | $props[$k]["value"] = $value; |
311 | } else if ($char == "(") { |
312 | $end = mime_match_parenthesis (0, $structure); |
313 | $sub = substr($structure, 1, $end-1); |
314 | $props = mime_get_props($props, $sub); |
315 | $structure = substr($structure, strlen($sub) + 2); |
316 | } |
317 | return $props; |
318 | } else if ($char == "(") { |
319 | $end = mime_match_parenthesis (0, $structure); |
320 | $sub = substr($structure, 1, $end-1); |
321 | $props = mime_get_props($props, $sub); |
322 | $structure = substr($structure, strlen($sub) + 2); |
ea48eb25 |
323 | return $props; |
8beafbbc |
324 | } else { |
325 | return $props; |
7831268e |
326 | } |
8beafbbc |
327 | } |
328 | } |
7831268e |
329 | |
8beafbbc |
330 | // Matches parenthesis. It will return the position of the matching |
331 | // parenthesis in $structure. For instance, if $structure was: |
332 | // ("text" "plain" ("val1name", "1") nil ... ) |
333 | // x x |
334 | // then this would return 42 to match up those two. |
335 | function mime_match_parenthesis ($pos, $structure) { |
336 | $char = substr($structure, $pos, 1); |
337 | |
338 | // ignore all extra characters |
339 | while ($pos < strlen($structure)) { |
340 | $pos++; |
341 | $char = substr($structure, $pos, 1); |
342 | if ($char == ")") { |
343 | return $pos; |
344 | } else if ($char == "(") { |
345 | $pos = mime_match_parenthesis ($pos, $structure); |
346 | } |
d4467150 |
347 | } |
8beafbbc |
348 | } |
d4467150 |
349 | |
8beafbbc |
350 | function mime_fetch_body ($imap_stream, $id, $ent_id) { |
351 | // do a bit of error correction. If we couldn't find the entity id, just guess |
352 | // that it is the first one. That is usually the case anyway. |
353 | if (!$ent_id) $ent_id = 1; |
354 | |
355 | fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n"); |
e79bed1b |
356 | $topline = fgets ($imap_stream, 1024); |
357 | $size = substr ($topline, strpos($topline, "{")+1); |
358 | $size = substr ($size, 0, strpos($size, "}")); |
359 | $read = fread ($imap_stream, $size); |
360 | return $read; |
d4467150 |
361 | } |
362 | |
8beafbbc |
363 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
d4467150 |
364 | |
aceb0d5c |
365 | |
d4467150 |
366 | |
8beafbbc |
367 | /** This is the first function called. It decides if this is a multipart |
368 | message or if it should be handled as a single entity |
4809f489 |
369 | **/ |
8d8ab69a |
370 | function decodeMime ($imap_stream, $body, $header) { |
8beafbbc |
371 | global $username, $key, $imapServerAddress, $imapPort; |
8d8ab69a |
372 | return mime_structure ($imap_stream, $header); |
8beafbbc |
373 | } |
b1dadc61 |
374 | |
cbcf32f6 |
375 | // This is here for debugging purposese. It will print out a list |
376 | // of all the entity IDs that are in the $message object. |
ea48eb25 |
377 | function listEntities ($message) { |
378 | if ($message) { |
cbcf32f6 |
379 | if ($message->header->entity_id) |
380 | echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>"; |
381 | for ($i = 0; $message->entities[$i]; $i++) { |
382 | $msg = listEntities($message->entities[$i], $ent_id); |
383 | if ($msg) |
384 | return $msg; |
385 | } |
ea48eb25 |
386 | } |
387 | } |
388 | |
cbcf32f6 |
389 | // returns a $message object for a particular entity id |
8beafbbc |
390 | function getEntity ($message, $ent_id) { |
391 | if ($message) { |
ea48eb25 |
392 | if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) { |
8beafbbc |
393 | return $message; |
b1dadc61 |
394 | } else { |
8beafbbc |
395 | for ($i = 0; $message->entities[$i]; $i++) { |
396 | $msg = getEntity ($message->entities[$i], $ent_id); |
397 | if ($msg) |
398 | return $msg; |
b1dadc61 |
399 | } |
8beafbbc |
400 | } |
401 | } |
402 | } |
403 | |
cbcf32f6 |
404 | // figures out what entity to display and returns the $message object |
405 | // for that entity. |
8beafbbc |
406 | function findDisplayEntity ($message) { |
407 | if ($message) { |
408 | if ($message->header->type0 == "text") { |
409 | if ($message->header->type1 == "plain" || |
410 | $message->header->type1 == "html") { |
411 | return $message->header->entity_id; |
412 | } |
413 | } else { |
414 | for ($i=0; $message->entities[$i]; $i++) { |
415 | return findDisplayEntity($message->entities[$i]); |
416 | } |
417 | } |
d4467150 |
418 | } |
b1dadc61 |
419 | } |
8405ee35 |
420 | |
d068c0ec |
421 | /** This returns a parsed string called $body. That string can then |
422 | be displayed as the actual message in the HTML. It contains |
423 | everything needed, including HTML Tags, Attachments at the |
424 | bottom, etc. |
4809f489 |
425 | **/ |
8d8ab69a |
426 | function formatBody($imap_stream, $message, $color, $wrap_at) { |
cbcf32f6 |
427 | // this if statement checks for the entity to show as the |
428 | // primary message. To add more of them, just put them in the |
429 | // order that is their priority. |
8beafbbc |
430 | global $username, $key, $imapServerAddress, $imapPort; |
431 | |
8beafbbc |
432 | $id = $message->header->id; |
433 | $urlmailbox = urlencode($message->header->mailbox); |
434 | |
e4a256af |
435 | // Get the right entity and redefine message to be this entity |
8beafbbc |
436 | $ent_num = findDisplayEntity ($message); |
2c252f5a |
437 | $body_message = getEntity($message, $ent_num); |
e4a256af |
438 | |
8beafbbc |
439 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
2c252f5a |
440 | $body = decodeBody($body, $body_message->header->encoding); |
8405ee35 |
441 | |
cbcf32f6 |
442 | // If there are other types that shouldn't be formatted, add |
443 | // them here |
444 | if ($message->header->type1 != "html") { |
2c252f5a |
445 | $body = translateText($body, $wrap_at, $body_message->header->charset); |
cbcf32f6 |
446 | } |
78509c54 |
447 | |
c36ed9cf |
448 | $body .= "<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>"; |
7831268e |
449 | |
b1dadc61 |
450 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
8beafbbc |
451 | if ($message->entities) { |
7831268e |
452 | $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">"; |
453 | $body .= "<TT><B>ATTACHMENTS:</B></TT>"; |
454 | $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">"; |
b1dadc61 |
455 | $num = 0; |
456 | |
8beafbbc |
457 | $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id); |
7831268e |
458 | $body .= "</TD></TR></TABLE>"; |
8405ee35 |
459 | } |
d4467150 |
460 | return $body; |
461 | } |
462 | |
8beafbbc |
463 | // A recursive function that returns a list of attachments with links |
464 | // to where to download these attachments |
465 | function formatAttachments ($message, $ent_id, $mailbox, $id) { |
f4991a86 |
466 | global $where, $what; |
8beafbbc |
467 | if ($message) { |
468 | if (!$message->entities) { |
469 | $type0 = strtolower($message->header->type0); |
470 | $type1 = strtolower($message->header->type1); |
471 | |
472 | if ($message->header->entity_id != $ent_id) { |
888c82e2 |
473 | $filename = decodeHeader($message->header->filename); |
8beafbbc |
474 | if (trim($filename) == "") { |
ea48eb25 |
475 | $display_filename = "untitled-".$message->header->entity_id; |
8beafbbc |
476 | } else { |
477 | $display_filename = $filename; |
478 | } |
479 | |
480 | $urlMailbox = urlencode($mailbox); |
481 | $ent = urlencode($message->header->entity_id); |
f4991a86 |
482 | if ($where && $what) { |
483 | // from a search |
484 | $body .= "<TT> <A HREF=\"../src/download.php?where=".urlencode($where)."&what=".urlencode($what)."&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A> (TYPE: $type0/$type1)"; |
485 | } else { |
486 | $body .= "<TT> <A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">" . $display_filename . "</A> (TYPE: $type0/$type1)"; |
487 | } |
ea48eb25 |
488 | if ($message->header->description) |
489 | $body .= " <b>" . htmlspecialchars($message->header->description)."</b>"; |
ca1a555e |
490 | $body .= " (<a href=\"../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent\">"._("download")."</a>)\n"; |
ea48eb25 |
491 | $body .= "</TT><BR>"; |
8beafbbc |
492 | $num++; |
493 | } |
494 | return $body; |
495 | } else { |
496 | for ($i = 0; $i < count($message->entities); $i++) { |
497 | $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id); |
498 | } |
499 | return $body; |
500 | } |
501 | } |
502 | } |
4809f489 |
503 | |
504 | |
505 | /** this function decodes the body depending on the encoding type. **/ |
d4467150 |
506 | function decodeBody($body, $encoding) { |
623332f3 |
507 | $body = str_replace("\r\n", "\n", $body); |
d4467150 |
508 | $encoding = strtolower($encoding); |
7831268e |
509 | |
ef3f274f |
510 | if ($encoding == "quoted-printable") { |
511 | $body = quoted_printable_decode($body); |
db87f79c |
512 | |
ef3f274f |
513 | while (ereg("=\n", $body)) |
514 | $body = ereg_replace ("=\n", "", $body); |
97be2168 |
515 | } else if ($encoding == "base64") { |
ef3f274f |
516 | $body = base64_decode($body); |
d4467150 |
517 | } |
ef3f274f |
518 | |
519 | // All other encodings are returned raw. |
520 | return $body; |
aceb0d5c |
521 | } |
a4c2cd49 |
522 | |
523 | |
524 | // This functions decode strings that is encoded according to |
525 | // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
2e434774 |
526 | function decodeHeader ($string) { |
1fd97780 |
527 | if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=', |
a4c2cd49 |
528 | $string, $res)) { |
1fd97780 |
529 | if (ucfirst($res[2]) == "B") { |
530 | $replace = base64_decode($res[3]); |
a4c2cd49 |
531 | } else { |
1fd97780 |
532 | $replace = ereg_replace("_", " ", $res[3]); |
5155cfcb |
533 | // Convert lowercase Quoted Printable to uppercase for |
534 | // quoted_printable_decode to understand it. |
535 | while (ereg("(=([0-9][a-f])|([a-f][0-9]))", $replace, $res)) { |
536 | $replace = str_replace($res[1], strtoupper($res[1]), $replace); |
537 | } |
a4c2cd49 |
538 | $replace = quoted_printable_decode($replace); |
539 | } |
540 | |
1fd97780 |
541 | $replace = charset_decode ($res[1], $replace); |
a4c2cd49 |
542 | |
543 | $string = eregi_replace |
1fd97780 |
544 | ('=\?([^?]+)\?(q|b)\?([^?]+)\?=', |
a4c2cd49 |
545 | $replace, $string); |
2e434774 |
546 | // In case there should be more encoding in the string: recurse |
547 | return (decodeHeader($string)); |
a4c2cd49 |
548 | } else |
549 | return ($string); |
550 | } |
551 | |
c3084273 |
552 | // Encode a string according to RFC 1522 for use in headers if it |
bb60fa3f |
553 | // contains 8-bit characters or anything that looks like it should |
554 | // be encoded. |
c3084273 |
555 | function encodeHeader ($string) { |
556 | global $default_charset; |
557 | |
bb60fa3f |
558 | // Encode only if the string contains 8-bit characters or =? |
559 | if (ereg("([\200-\377])|=\\?", $string)) { |
c3084273 |
560 | $newstring = "=?$default_charset?Q?"; |
c3084273 |
561 | |
bb60fa3f |
562 | // First the special characters |
563 | $string = str_replace("=", "=3D", $string); |
564 | $string = str_replace("?", "=3F", $string); |
565 | $string = str_replace("_", "=5F", $string); |
566 | $string = str_replace(" ", "_", $string); |
567 | |
568 | |
569 | while (ereg("([\200-\377])", $string, $regs)) { |
c3084273 |
570 | $replace = $regs[1]; |
bb60fa3f |
571 | $insert = "=" . strtoupper(bin2hex($replace)); |
572 | $string = str_replace($replace, $insert, $string); |
c3084273 |
573 | } |
574 | |
bb60fa3f |
575 | $newstring = "=?$default_charset?Q?".$string."?="; |
576 | |
c3084273 |
577 | return $newstring; |
578 | } |
579 | |
580 | return $string; |
581 | } |
582 | |
9f9d7d28 |
583 | ?> |