9e0d32d6c912f432ce87a5d6b00f190e7ef2d2c1
[squirrelmail.git] / functions / mime.php
1 <?php
2
3 /**
4 * mime.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains the functions necessary to detect and decode MIME
10 * messages.
11 *
12 * $Id$
13 */
14
15 require_once('../functions/imap.php');
16 require_once('../functions/attachment_common.php');
17
18 /** Setting up the objects that have the structure for the message **/
19 class msg_header {
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
22
23 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
24 $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
25 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
26 $id = 0, $mailbox = '', $description = '', $filename = '',
27 $entity_id = 0, $message_id = 0, $name = '', $priority = 3;
28 }
29
30 class message {
31 /** message is the object that contains messages. It is a recursive
32 object in that through the $entities variable, it can contain
33 more objects of type message. See documentation in mime.txt for
34 a better description of how this works.
35 **/
36 var $header = '', $entities = array();
37
38 function addEntity ($msg) {
39 $this->entities[] = $msg;
40 }
41 }
42
43 /* --------------------------------------------------------------------------------- */
44 /* MIME DECODING */
45 /* --------------------------------------------------------------------------------- */
46
47 /* This function gets the structure of a message and stores it in the "message" class.
48 * It will return this object for use with all relevant header information and
49 * fully parsed into the standard "message" object format.
50 */
51 function mime_structure ($imap_stream, $header) {
52
53 sqimap_messages_flag ($imap_stream, $header->id, $header->id, 'Seen');
54 $ssid = sqimap_session_id();
55 $lsid = strlen( $ssid );
56 $id = $header->id;
57 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
58 //
59 // This should use sqimap_read_data instead of reading it itself
60 //
61 $read = fgets ($imap_stream, 9216);
62 $bodystructure = '';
63 while ( substr($read, 0, $lsid) <> $ssid &&
64 !feof( $imap_stream ) ) {
65 $bodystructure .= $read;
66 $read = fgets ($imap_stream, 9216);
67 }
68 $read = $bodystructure;
69
70 // isolate the body structure and remove beginning and end parenthesis
71 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13));
72 $read = trim(substr ($read, 0, -1));
73 $end = mime_match_parenthesis(0, $read);
74 while ($end == strlen($read)-1) {
75 $read = trim(substr ($read, 0, -1));
76 $read = trim(substr ($read, 1));
77 $end = mime_match_parenthesis(0, $read);
78 }
79
80 $msg = mime_parse_structure ($read, 0);
81 $msg->header = $header;
82
83 return( $msg );
84 }
85
86 /* this starts the parsing of a particular structure. It is called recursively,
87 * so it can be passed different structures. It returns an object of type
88 * $message.
89 * First, it checks to see if it is a multipart message. If it is, then it
90 * handles that as it sees is necessary. If it is just a regular entity,
91 * then it parses it and adds the necessary header information (by calling out
92 * to mime_get_elements()
93 */
94 function mime_parse_structure ($structure, $ent_id) {
95
96 $msg = new message();
97 if ($structure{0} == '(') {
98 $ent_id = mime_new_element_level($ent_id);
99 $start = $end = -1;
100 do {
101 $start = $end+1;
102 $end = mime_match_parenthesis ($start, $structure);
103
104 $element = substr($structure, $start+1, ($end - $start)-1);
105 $ent_id = mime_increment_id ($ent_id);
106 $newmsg = mime_parse_structure ($element, $ent_id);
107 $msg->addEntity ($newmsg);
108 } while ($structure{$end+1} == '(');
109 } else {
110 // parse the elements
111 $msg = mime_get_element ($structure, $msg, $ent_id);
112 }
113 return $msg;
114 }
115
116 /* Increments the element ID. An element id can look like any of
117 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
118 * the last number of the element id, changing 1.2 to 1.3.
119 */
120 function mime_increment_id ($id) {
121
122 if (strpos($id, '.')) {
123 $first = substr($id, 0, strrpos($id, '.'));
124 $last = substr($id, strrpos($id, '.')+1);
125 $last++;
126 $new = $first . '.' .$last;
127 } else {
128 $new = $id + 1;
129 }
130
131 return $new;
132 }
133
134 /*
135 * See comment for mime_increment_id().
136 * This adds another level on to the entity_id changing 1.3 to 1.3.0
137 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
138 * before it can be used. I left it this way so as not to have
139 * to make a special case if it is the first entity_id. It
140 * always increments it, and that works fine.
141 */
142 function mime_new_element_level ($id) {
143
144 if (!$id) {
145 $id = 0;
146 } else {
147 $id = $id . '.0';
148 }
149
150 return( $id );
151 }
152
153 function mime_get_element (&$structure, $msg, $ent_id) {
154
155 $elem_num = 1;
156 $msg->header = new msg_header();
157 $msg->header->entity_id = $ent_id;
158 $properties = array();
159
160 while (strlen($structure) > 0) {
161 $structure = trim($structure);
162 $char = $structure{0};
163
164 if (strtolower(substr($structure, 0, 3)) == 'nil') {
165 $text = '';
166 $structure = substr($structure, 3);
167 } else if ($char == '"') {
168 // loop through until we find the matching quote, and return that as a string
169 $pos = 1;
170 $text = '';
171 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
172 $text .= $char;
173 $pos++;
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 = $structure{$pos};
186 $text = '';
187 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
188 $text .= $char;
189 $pos++;
190 $char = $structure{$pos};
191 }
192 $structure = substr($structure, strlen($text));
193 }
194
195 // This is where all the text parts get put into the header
196 switch ($elem_num) {
197 case 1:
198 $msg->header->type0 = strtolower($text);
199 break;
200 case 2:
201 $msg->header->type1 = strtolower($text);
202 break;
203 case 4: // Id
204 // Invisimail enclose images with <>
205 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
206 break;
207 case 5:
208 $msg->header->description = $text;
209 break;
210 case 6:
211 $msg->header->encoding = strtolower($text);
212 break;
213 case 7:
214 $msg->header->size = $text;
215 break;
216 default:
217 if ($msg->header->type0 == 'text' && $elem_num == 8) {
218 // This is a plain text message, so lets get the number of lines
219 // that it contains.
220 $msg->header->num_lines = $text;
221
222 } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
223 // This is an encapsulated message, so lets start all over again and
224 // parse this message adding it on to the existing one.
225 $structure = trim($structure);
226 if ( $structure{0} == '(' ) {
227 $e = mime_match_parenthesis (0, $structure);
228 $structure = substr($structure, 0, $e);
229 $structure = substr($structure, 1);
230 $m = mime_parse_structure($structure, $msg->header->entity_id);
231
232 // the following conditional is there to correct a bug that wasn't
233 // incrementing the entity IDs correctly because of the special case
234 // that message/rfc822 is. This fixes it fine.
235 if (substr($structure, 1, 1) != '(')
236 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
237
238 // Now we'll go through and reformat the results.
239 if ($m->entities) {
240 for ($i=0; $i < count($m->entities); $i++) {
241 $msg->addEntity($m->entities[$i]);
242 }
243 } else {
244 $msg->addEntity($m);
245 }
246 $structure = "";
247 }
248 }
249 break;
250 }
251 $elem_num++;
252 $text = "";
253 }
254 // loop through the additional properties and put those in the various headers
255 if ($msg->header->type0 != 'message') {
256 for ($i=0; $i < count($properties); $i++) {
257 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
258 }
259 }
260
261 return $msg;
262 }
263
264 /*
265 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
266 * figure out how to do this part, so I decided to go to bed. I woke up
267 * in the morning and had a flash of insight. I went to the white-board
268 * and scribbled it out, then spent a bit programming it, and this is the
269 * result. Nothing complicated, but I think my brain was fried yesterday.
270 * Funny how that happens some times.
271 *
272 * This gets properties in a nested parenthesisized list. For example,
273 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
274 * This returns an array called $props with all paired up properties.
275 * It ignores the "attachment" for now, maybe that should change later
276 * down the road. In this case, what is returned is:
277 * $props[0]["name"] = "filename";
278 * $props[0]["value"] = "luke.tar.gz";
279 */
280 function mime_get_props ($props, $structure) {
281
282 while (strlen($structure) > 0) {
283 $structure = trim($structure);
284 $char = $structure{0};
285
286 if ($char == '"') {
287 $pos = 1;
288 $tmp = '';
289 while ( ( $char = $structure{$pos} ) != '"' &&
290 $pos < strlen($structure)) {
291 $tmp .= $char;
292 $pos++;
293 }
294 $structure = trim(substr($structure, strlen($tmp) + 2));
295 $char = $structure{0};
296
297 if ($char == '"') {
298 $pos = 1;
299 $value = '';
300 while ( ( $char = $structure{$pos} ) != '"' &&
301 $pos < strlen($structure) ) {
302 $value .= $char;
303 $pos++;
304 }
305 $structure = trim(substr($structure, strlen($tmp) + 2));
306
307 $k = count($props);
308 $props[$k]['name'] = strtolower($tmp);
309 $props[$k]['value'] = $value;
310 } else if ($char == '(') {
311 $end = mime_match_parenthesis (0, $structure);
312 $sub = substr($structure, 1, $end-1);
313 if (! isset($props))
314 $props = array();
315 $props = mime_get_props($props, $sub);
316 $structure = substr($structure, strlen($sub) + 2);
317 }
318 return $props;
319 } else if ($char == '(') {
320 $end = mime_match_parenthesis (0, $structure);
321 $sub = substr($structure, 1, $end-1);
322 $props = mime_get_props($props, $sub);
323 $structure = substr($structure, strlen($sub) + 2);
324 return $props;
325 } else {
326 return $props;
327 }
328 }
329 }
330
331 /*
332 * Matches parenthesis. It will return the position of the matching
333 * parenthesis in $structure. For instance, if $structure was:
334 * ("text" "plain" ("val1name", "1") nil ... )
335 * x x
336 * then this would return 42 to match up those two.
337 */
338 function mime_match_parenthesis ($pos, $structure) {
339
340 $j = strlen( $structure );
341
342 // ignore all extra characters
343 // If inside of a string, skip string -- Boundary IDs and other
344 // things can have ) in them.
345 if ( $structure{$pos} != '(' ) {
346 return( $j );
347 }
348
349 while ( $pos < $j ) {
350 $pos++;
351 if ($structure{$pos} == ')') {
352 return $pos;
353 } elseif ($structure{$pos} == '"') {
354 $pos++;
355 while ( $structure{$pos} != '"' &&
356 $pos < $j ) {
357 if (substr($structure, $pos, 2) == '\\"') {
358 $pos++;
359 } elseif (substr($structure, $pos, 2) == '\\\\') {
360 $pos++;
361 }
362 $pos++;
363 }
364 } elseif ( $structure{$pos} == '(' ) {
365 $pos = mime_match_parenthesis ($pos, $structure);
366 }
367 }
368 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
369 return( $pos );
370 }
371
372 function mime_fetch_body($imap_stream, $id, $ent_id ) {
373
374 /*
375 * do a bit of error correction. If we couldn't find the entity id, just guess
376 * that it is the first one. That is usually the case anyway.
377 */
378 if (!$ent_id) {
379 $ent_id = 1;
380 }
381
382 $cmd = "FETCH $id BODY[$ent_id]";
383 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
384
385 do {
386 $topline = array_shift( $data );
387 } while( $topline && $topline == '*' && !preg_match( '/\\* [0-9] FETCH.*/i', $topline )) ;
388 $wholemessage = implode('', $data);
389 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
390
391 $ret = substr( $wholemessage, 0, $regs[1] );
392 /*
393 There is some information in the content info header that could be important
394 in order to parse html messages. Let's get them here.
395 */
396 if ( $ret{0} == '<' ) {
397 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
398 /* BASE within HTML documents is illegal (see w3 spec)
399 * $base = '';
400 * $k = 10;
401 * foreach( $data as $d ) {
402 * if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
403 * $j = strlen( $d );
404 * $i = 13;
405 * $base = '';
406 * while ( $i < $j &&
407 * ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
408 * $i++;
409 * while ( $i < $j ) {
410 * if ( isNoSep( $d{$i} ) )
411 * $base .= $d{$i};
412 * $i++;
413 * }
414 * $k = 0;
415 * } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
416 * $base .= substr( $d, 1 );
417 * }
418 * $k++;
419 * }
420 * if ( $base <> '' ) {
421 * $ret = "<base href=\"$base\">" . $ret;
422 * }
423 * */
424 }
425 } else if (ereg('"([^"]*)"', $topline, $regs)) {
426 $ret = $regs[1];
427 } else {
428 global $where, $what, $mailbox, $passed_id, $startMessage;
429 $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
430 if (isset($where) && isset($what)) {
431 $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
432 } else {
433 $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
434 }
435 $par .= '&amp;response=' . urlencode($response) .
436 '&amp;message=' . urlencode($message).
437 '&amp;topline=' . urlencode($topline);
438
439 echo '<tt><br>' .
440 '<table width="80%"><tr>' .
441 '<tr><td colspan=2>' .
442 _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
443 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
444 _("Submit message") . '</A><BR>&nbsp;' .
445 '</td></tr>' .
446 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
447 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
448 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
449 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
450 "</table><BR></tt></font><hr>";
451
452 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
453 array_shift($data);
454 $wholemessage = implode('', $data);
455
456 $ret = $wholemessage;
457 }
458 return( $ret );
459 }
460
461 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
462 // do a bit of error correction. If we couldn't find the entity id, just guess
463 // that it is the first one. That is usually the case anyway.
464 if (!$ent_id) {
465 $ent_id = 1;
466 }
467 $sid = sqimap_session_id();
468 // Don't kill the connection if the browser is over a dialup
469 // and it would take over 30 seconds to download it.
470
471 // don´t call set_time_limit in safe mode.
472 if (!ini_get("safe_mode")) {
473 set_time_limit(0);
474 }
475
476 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
477 $cnt = 0;
478 $continue = true;
479 $read = fgets ($imap_stream,4096);
480 // This could be bad -- if the section has sqimap_session_id() . ' OK'
481 // or similar, it will kill the download.
482 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
483 if (trim($read) == ')==') {
484 $read1 = $read;
485 $read = fgets ($imap_stream,4096);
486 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
487 return;
488 } else {
489 echo decodeBody($read1, $encoding) .
490 decodeBody($read, $encoding);
491 }
492 } else if ($cnt) {
493 echo decodeBody($read, $encoding);
494 }
495 $read = fgets ($imap_stream,4096);
496 $cnt++;
497 }
498 }
499
500 /* -[ END MIME DECODING ]----------------------------------------------------------- */
501
502
503
504 /* This is the first function called. It decides if this is a multipart
505 message or if it should be handled as a single entity
506 */
507 function decodeMime ($imap_stream, &$header) {
508 global $username, $key, $imapServerAddress, $imapPort;
509 return mime_structure ($imap_stream, $header);
510 }
511
512 // This is here for debugging purposese. It will print out a list
513 // of all the entity IDs that are in the $message object.
514 /*
515 function listEntities ($message) {
516 if ($message) {
517 if ($message->header->entity_id)
518 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
519 for ($i = 0; $message->entities[$i]; $i++) {
520 $msg = listEntities($message->entities[$i], $ent_id);
521 if ($msg)
522 return $msg;
523 }
524 }
525 }
526 */
527
528 /* returns a $message object for a particular entity id */
529 function getEntity ($message, $ent_id) {
530 if ($message) {
531 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
532 return $message;
533 } else {
534 for ($i = 0; isset($message->entities[$i]); $i++) {
535 $msg = getEntity ($message->entities[$i], $ent_id);
536 if ($msg) {
537 return $msg;
538 }
539 }
540 }
541 }
542 }
543
544 /*
545 * figures out what entity to display and returns the $message object
546 * for that entity.
547 */
548 function findDisplayEntity ($message, $textOnly = 1) {
549 global $show_html_default;
550
551 $entity = 0;
552
553 if ($message) {
554 if ( $message->header->type0 == 'multipart' &&
555 ( $message->header->type1 == 'alternative' ||
556 $message->header->type1 == 'related' ) &&
557 $show_html_default && ! $textOnly ) {
558 $entity = findDisplayEntityHTML($message);
559 }
560
561 // Show text/plain or text/html -- the first one we find.
562 if ( $entity == 0 &&
563 $message->header->type0 == 'text' &&
564 ( $message->header->type1 == 'plain' ||
565 $message->header->type1 == 'html' ) &&
566 isset($message->header->entity_id) ) {
567 $entity = $message->header->entity_id;
568 }
569
570 $i = 0;
571 while ($entity == 0 && isset($message->entities[$i]) ) {
572 $entity = findDisplayEntity($message->entities[$i], $textOnly);
573 $i++;
574 }
575 }
576
577 return( $entity );
578 }
579
580 /* Shows the HTML version */
581 function findDisplayEntityHTML ($message) {
582
583 if ( $message->header->type0 == 'text' &&
584 $message->header->type1 == 'html' &&
585 isset($message->header->entity_id)) {
586 return $message->header->entity_id;
587 }
588 for ($i = 0; isset($message->entities[$i]); $i ++) {
589 $entity = findDisplayEntityHTML($message->entities[$i]);
590 if ($entity != 0) {
591 return $entity;
592 }
593 }
594
595 return 0;
596 }
597
598 /* This returns a parsed string called $body. That string can then
599 be displayed as the actual message in the HTML. It contains
600 everything needed, including HTML Tags, Attachments at the
601 bottom, etc.
602 */
603 function formatBody($imap_stream, $message, $color, $wrap_at) {
604 // this if statement checks for the entity to show as the
605 // primary message. To add more of them, just put them in the
606 // order that is their priority.
607 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
608 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
609
610 $has_unsafe_images = 0;
611
612 $id = $message->header->id;
613 $urlmailbox = urlencode($message->header->mailbox);
614
615 // Get the right entity and redefine message to be this entity
616 // Pass the 0 to mean that we want the 'best' viewable one
617 $ent_num = findDisplayEntity ($message, 0);
618 $body_message = getEntity($message, $ent_num);
619 if (($body_message->header->type0 == 'text') ||
620 ($body_message->header->type0 == 'rfc822')) {
621
622 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
623 $body = decodeBody($body, $body_message->header->encoding);
624 $hookResults = do_hook("message_body", $body);
625 $body = $hookResults[1];
626
627 // If there are other types that shouldn't be formatted, add
628 // them here
629 if ($body_message->header->type1 == 'html') {
630 if ( $show_html_default <> 1 ) {
631 $body = strip_tags( $body );
632 translateText($body, $wrap_at, $body_message->header->charset);
633 } else {
634 $body = MagicHTML( $body, $id );
635 }
636 } else {
637 translateText($body, $wrap_at, $body_message->header->charset);
638 }
639
640 $body .= "<CENTER><SMALL><A HREF=\"../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;passed_ent_id=$ent_num&amp;mailbox=$urlmailbox&amp;showHeaders=1\">". _("Download this as a file") ."</A></SMALL></CENTER><BR>";
641 if ($has_unsafe_images) {
642 if ($view_unsafe_images) {
643 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0\">". _("Hide Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
644 } else {
645 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0&amp;view_unsafe_images=1\">". _("View Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
646 }
647 }
648
649 /** Display the ATTACHMENTS: message if there's more than one part **/
650 if (isset($message->entities[0])) {
651 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
652 }
653 } else {
654 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
655 }
656 return ($body);
657 }
658
659 /*
660 * A recursive function that returns a list of attachments with links
661 * to where to download these attachments
662 */
663 function formatAttachments($message, $ent_id, $mailbox, $id) {
664 global $where, $what;
665 global $startMessage, $color;
666 static $ShownHTML = 0;
667
668 $body = '';
669 if ($ShownHTML == 0) {
670
671 $ShownHTML = 1;
672 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
673 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
674 _("Attachments") . ':' .
675 "</B></TH></TR><TR><TD>\n" .
676 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
677 formatAttachments($message, $ent_id, $mailbox, $id) .
678 "</TABLE></TD></TR></TABLE>";
679
680 } else if ($message) {
681
682 if (!$message->entities) {
683
684 $type0 = strtolower($message->header->type0);
685 $type1 = strtolower($message->header->type1);
686 $name = decodeHeader($message->header->name);
687
688 if ($message->header->entity_id != $ent_id) {
689 $filename = decodeHeader($message->header->filename);
690 if (trim($filename) == '') {
691 if (trim($name) == '') {
692 if ( trim( $message->header->id ) == '' )
693 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
694 else
695 $display_filename = 'cid: ' . $message->header->id;
696 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
697 } else {
698 $display_filename = $name;
699 $filename = $name;
700 }
701 } else {
702 $display_filename = $filename;
703 }
704
705 $urlMailbox = urlencode($mailbox);
706 $ent = urlencode($message->header->entity_id);
707
708 $DefaultLink =
709 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
710 if ($where && $what) {
711 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
712 }
713 $Links['download link']['text'] = _("download");
714 $Links['download link']['href'] =
715 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
716 $ImageURL = '';
717
718 /* this executes the attachment hook with a specific MIME-type.
719 * if that doens't have results, it tries if there's a rule
720 * for a more generic type. */
721 $HookResults = do_hook("attachment $type0/$type1", $Links,
722 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
723 $display_filename, $where, $what);
724 if(count($HookResults[1]) <= 1) {
725 $HookResults = do_hook("attachment $type0/*", $Links,
726 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
727 $display_filename, $where, $what);
728 }
729
730 $Links = $HookResults[1];
731 $DefaultLink = $HookResults[6];
732
733 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
734 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
735 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
736 '</b>&nbsp;&nbsp;</small></TD>' .
737 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
738 '<TD><SMALL>';
739 if ($message->header->description) {
740 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
741 }
742 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
743
744
745 $SkipSpaces = 1;
746 foreach ($Links as $Val) {
747 if ($SkipSpaces) {
748 $SkipSpaces = 0;
749 } else {
750 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
751 }
752 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
753 }
754
755 unset($Links);
756
757 $body .= "</SMALL></TD></TR>\n";
758 }
759 } else {
760 for ($i = 0; $i < count($message->entities); $i++) {
761 $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
762 }
763 }
764 }
765 return( $body );
766 }
767
768
769 /** this function decodes the body depending on the encoding type. **/
770 function decodeBody($body, $encoding) {
771 $body = str_replace("\r\n", "\n", $body);
772 $encoding = strtolower($encoding);
773
774 global $show_html_default;
775
776 if ($encoding == 'quoted-printable') {
777 $body = quoted_printable_decode($body);
778
779
780 while (ereg("=\n", $body))
781 $body = ereg_replace ("=\n", "", $body);
782
783 } else if ($encoding == 'base64') {
784 $body = base64_decode($body);
785 }
786
787 // All other encodings are returned raw.
788 return $body;
789 }
790
791 /*
792 * This functions decode strings that is encoded according to
793 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
794 */
795 function decodeHeader ($string, $utfencode=true) {
796
797 if ( is_array( $string ) ) {
798 $string = implode("\n", $string );
799 }
800
801 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
802 $string, $res)) {
803 if (ucfirst($res[2]) == 'B') {
804 $replace = base64_decode($res[3]);
805 } else {
806 $replace = str_replace('_', ' ', $res[3]);
807 // Convert lowercase Quoted Printable to uppercase for
808 // quoted_printable_decode to understand it.
809 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))",
810 $replace, $res)) {
811 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
812 }
813 $replace = quoted_printable_decode($replace);
814 }
815 /* Only encode into entities by default. Some places
816 don't need the encoding, like the compose form. */
817 if ($utfencode){
818 $replace = charset_decode ($res[1], $replace);
819 }
820
821 // Remove the name of the character set.
822 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
823 $replace, $string);
824
825 // In case there should be more encoding in the string: recurse
826 $string = decodeHeader($string);
827 }
828
829 return ($string);
830 }
831
832 /*
833 * Encode a string according to RFC 1522 for use in headers if it
834 * contains 8-bit characters or anything that looks like it should
835 * be encoded.
836 */
837 function encodeHeader ($string) {
838 global $default_charset;
839
840 // Encode only if the string contains 8-bit characters or =?
841 $j = strlen( $string );
842 $l = strstr($string, '=?'); // Must be encoded ?
843 $ret = '';
844 for( $i=0; $i < $j; ++$i) {
845 switch( $string{$i} ) {
846 case '=':
847 $ret .= '=3D';
848 break;
849 case '?':
850 $ret .= '=3F';
851 break;
852 case '_':
853 $ret .= '=5F';
854 break;
855 case ' ':
856 $ret .= '_';
857 break;
858 default:
859 $k = ord( $string{$i} );
860 if ( $k > 126 ) {
861 $ret .= sprintf("=%02X", $k);
862 $l = TRUE;
863 } else
864 $ret .= $string{$i};
865 }
866 }
867
868 if ( $l ) {
869 $string = "=?$default_charset?Q?$ret?=";
870 }
871
872 return( $string );
873 }
874
875 /*
876 Strips dangerous tags from html messages.
877 */
878 function MagicHTML( $body, $id ) {
879
880 global $message, $HTTP_SERVER_VARS,
881 $attachment_common_show_images;
882
883 $attachment_common_show_images =
884 FALSE; // Don't display attached images in HTML mode
885 $j = strlen( $body ); // Legnth of the HTML
886 $ret = ''; // Returned string
887 $bgcolor = '#ffffff'; // Background style color (defaults to white)
888 $textcolor = '#000000'; // Foreground style color (defaults to black)
889 $leftmargin = ''; // Left margin style
890 $title = ''; // HTML title if any
891
892 $i = 0;
893 while ( $i < $j ) {
894 if ( $body{$i} == '<' ) {
895 $pos = $i + 1;
896 $tag = '';
897 while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
898 $body{$pos} == "\n" ) {
899 $pos ++;
900 }
901 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
902 $body{$pos} != "\t" && $body{$pos} != "\n" &&
903 $pos < $j ) {
904 $tag .= $body{$pos};
905 $pos ++;
906 }
907 switch( strtoupper( $tag ) ) {
908 // Strips the entire tag and contents
909 case 'APPL':
910 case 'EMBB':
911 case 'FRAM':
912 case 'SCRI':
913 case 'OBJE':
914 $etg = '/' . $tag;
915 while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
916 $i < $j ) $i++;
917 while ( $i < $j && $body{++$i} <> '>' );
918 // $ret .= "<!-- $tag removed -->";
919 break;
920 // Substitute Title
921 case 'TITL':
922 $i += 5;
923 while ( $body{$i} <> '>' && // </title>
924 $i < $j )
925 $i++;
926 $i++;
927 $title = '';
928 while ( $body{$i} <> '<' && // </title>
929 $i < $j ) {
930 $title .= $body{$i};
931 $i++;
932 }
933 $i += 7;
934 break;
935 // Destroy these tags
936 case 'HTML':
937 case 'HEAD':
938 case '/HTM':
939 case '/HEA':
940 case '!DOC':
941 case 'META':
942 //case 'DIV ':
943 //case '/DIV':
944 case '!-- ':
945 $i += 4;
946 while ( $body{$i} <> '>' &&
947 $i < $j )
948 $i++;
949 // $i++;
950 break;
951 case 'STYL':
952 $i += 5;
953 while ( $body{$i} <> '>' && // </title>
954 $i < $j )
955 $i++;
956 $i++;
957 // We parse the style to look for interesting stuff
958 $styleblk = '';
959 while ( $body{$i} <> '>' &&
960 $i < $j ) {
961 // First we get the name of the style
962 $style = '';
963 while ( $body{$i} <> '>' &&
964 $body{$i} <> '<' &&
965 $body{$i} <> '{' &&
966 $i < $j ) {
967 if ( isnoSep( $body{$i} ) )
968 $style .= $body{$i};
969 $i++;
970 }
971 stripComments( $i, $j, $body );
972 $style = strtoupper( trim( $style ) );
973 if ( $style == 'BODY' ) {
974 // Next we look into the definitions of the body style
975 while ( $body{$i} <> '>' &&
976 $body{$i} <> '}' &&
977 $i < $j ) {
978 // We look for the background color if any.
979 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
980 $i += 17;
981 $bgcolor = getStyleData( $i, $j, $body );
982 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
983 $i += 12;
984 $leftmargin = getStyleData( $i, $j, $body );
985 }
986 $i++;
987 }
988 } else {
989 // Other style are mantained
990 $styleblk .= "$style ";
991 while ( $body{$i} <> '>' &&
992 $body{$i} <> '<' &&
993 $body{$i} <> '}' &&
994 $i < $j ) {
995 $styleblk .= $body{$i};
996 $i++;
997 }
998 $styleblk .= $body{$i};
999 }
1000 stripComments( $i, $j, $body );
1001 if ( $body{$i} <> '>' )
1002 $i++;
1003 }
1004 if ( $styleblk <> '' )
1005 $ret .= "<style>$styleblk";
1006 break;
1007 case 'BODY':
1008 if ( $title <> '' )
1009 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
1010 $ret .= "<TABLE";
1011 $i += 5;
1012 if (! isset($base)) {
1013 $base = '';
1014 }
1015 $ret .= stripEvent( $i, $j, $body, $id, $base );
1016 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
1017 if ( $leftmargin <> '' )
1018 $ret .= "<td width=$leftmargin>&nbsp;</td>";
1019 $ret .= '<td>';
1020 if (strtolower($bgcolor) == 'ffffff' ||
1021 strtolower($bgcolor) == '#ffffff')
1022 $ret .= '<font color=#000000>';
1023 break;
1024 case 'BASE':
1025 $i += 5;
1026 $base = '';
1027 while ( !isNoSep( $body{$i} ) &&
1028 $i < $j ) {
1029 $i++;
1030 }
1031 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
1032 $i += 5;
1033 while ( !isNoSep( $body{$i} ) &&
1034 $i < $j ) {
1035 $i++;
1036 }
1037 while ( $body{$i} <> '>' &&
1038 $i < $j ) {
1039 if ( $body{$i} <> '"' ) {
1040 $base .= $body{$i};
1041 }
1042 $i++;
1043 }
1044 // Debuging $ret .= "<!-- base == $base -->";
1045 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1046 $ret .= "\n<BASE HREF=\"$base\">\n";
1047 }
1048 }
1049 break;
1050 case '/BOD':
1051 $ret .= '</font></td></tr></TABLE>';
1052 $i += 6;
1053 break;
1054 default:
1055 // Following tags can contain some event handler, lets search it
1056 stripComments( $i, $j, $body );
1057 if (! isset($base)) {
1058 $base = '';
1059 }
1060 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1061 // $ret .= "<!-- $tag detected -->";
1062 }
1063 } else {
1064 $ret .= $body{$i};
1065 }
1066 $i++;
1067 }
1068
1069 return( "\n\n<!-- HTML Output ahead -->\n" .
1070 $ret .
1071 /* Base is illegal within HTML
1072 "\n<!-- END of HTML Output --><base href=\"".
1073 get_location() . '/'.
1074 "\">\n\n" );
1075 */
1076 "\n<!-- END of HTML Output -->\n\n" );
1077 }
1078
1079 function isNoSep( $char ) {
1080
1081 switch( $char ) {
1082 case ' ':
1083 case "\n":
1084 case "\t":
1085 case "\r":
1086 case '>':
1087 case '"':
1088 return( FALSE );
1089 break;
1090 default:
1091 return( TRUE );
1092 }
1093
1094 }
1095
1096 /*
1097 The following function is usefull to remove extra data that can cause
1098 html not to display properly. Especialy with MS stuff.
1099 */
1100
1101 function stripComments( &$i, $j, &$body ) {
1102
1103 while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
1104 $i < $j ) {
1105 $i += 5;
1106 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1107 $i < $j )
1108 $i++;
1109 $i++;
1110 }
1111
1112 return;
1113
1114 }
1115
1116 /* Gets the style data of a specific style */
1117
1118 function getStyleData( &$i, $j, &$body ) {
1119
1120 // We skip spaces
1121 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1122 $i < $j ) {
1123 $i++;
1124 }
1125 // And get the color
1126 $ret = '';
1127 while ( isNoSep( $body{$i} ) &&
1128 $i < $j ) {
1129 $ret .= $body{$i};
1130 $i++;
1131 }
1132
1133 return( $ret );
1134 }
1135
1136 /*
1137 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1138 change on with no (onload -> noload)
1139 */
1140
1141 function stripEvent( &$i, $j, &$body, $id, $base ) {
1142
1143 global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
1144
1145 $ret = '';
1146
1147 while ( $body{$i} <> '>' &&
1148 $i < $j ) {
1149 $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
1150 switch( $etg ) {
1151 case 'src':
1152 // This is probably a src specification
1153 $k = $i + 3;
1154 while( !isNoSep( $body{$k} )) {
1155 $k++;
1156 }
1157 if ( $body{$k} == '=' ) {
1158 /* It is indeed */
1159 $k++;
1160 while( !isNoSep( $body{$k} ) &&
1161 $k < $j ) {
1162 $k++;
1163 }
1164 $src = '';
1165 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1166 $k < $j ) {
1167 $src .= $body{$k};
1168 $k++;
1169 }
1170 while( !isNoSep( $body{$k} ) &&
1171 $k < $j ) {
1172 $k++;
1173 }
1174 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1175 $src = substr( $src, 4 );
1176 $src = "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
1177 urlencode( $message->header->mailbox ) .
1178 "&amp;passed_ent_id=" . find_ent_id( $src, $message );
1179 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
1180 stristr( $src, $base_uri ) ) {
1181 /* Javascript and local urls goes out */
1182 if (!$view_unsafe_images) {
1183 $src = '../images/' . _("sec_remove_eng.png");
1184 }
1185 $has_unsafe_images = 1;
1186 }
1187 $ret .= 'src="' . $src . '" ';
1188 $i = $k - 2;
1189 } else {
1190 $ret .= 'src';
1191 $i = $i + 3;
1192 }
1193
1194 break;
1195 case '../':
1196 // Retrolinks are not allowed without a base because they mess with SM security
1197 if ( $base == '' ) {
1198 $i += 2;
1199 } else {
1200 $ret .= '.';
1201 }
1202 break;
1203 case 'cid':
1204 // Internal link
1205 $k = $i-1;
1206 if ( $body{$i+3} == ':') {
1207 $i +=4;
1208 $name = '';
1209 while ( isNoSep( $body{$i} ) &&
1210 $i < $j ) {
1211 $name .= $body{$i++};
1212 }
1213 if ( $name <> '' ) {
1214 $ret .= "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
1215 urlencode( $message->header->mailbox ) .
1216 "&amp;passed_ent_id=" . find_ent_id( $name, $message );
1217 if ( $body{$k} == '"' )
1218 $ret .= '" ';
1219 else
1220 $ret .= ' ';
1221 }
1222 if ( $body{$i} == '>' )
1223 $i -= 1;
1224 }
1225 break;
1226 case ' on':
1227 case "\non":
1228 case "\ron":
1229 case "\ton":
1230 $ret .= ' no';
1231 $i += 2;
1232 break;
1233 case 'pt:':
1234 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
1235 $ret .= '_no/';
1236 } else {
1237 $ret .= $etg;
1238 }
1239 $i += 2;
1240 break;
1241 default:
1242 $ret .= $body{$i};
1243 }
1244 $i++;
1245 }
1246 return( $ret );
1247 }
1248
1249
1250 /* This function trys to locate the entity_id of a specific mime element */
1251
1252 function find_ent_id( $id, $message ) {
1253
1254 $ret = '';
1255 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1256
1257 if ( $message->entities[$i]->header->entity_id == '' ) {
1258 $ret = find_ent_id( $id, $message->entities[$i] );
1259 } else {
1260 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1261 $ret = $message->entities[$i]->header->entity_id;
1262 }
1263
1264 }
1265
1266 return( $ret );
1267
1268 }
1269 ?>