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