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