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