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