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