fixes sort order when using server-side and $sort=6
[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 = '';
805 if (isset($header->name)) {
806 $name = decodeHeader($header->name);
807 }
808 if ($type0 =='message' && $type1 == 'rfc822') {
809
810 $filename = decodeHeader($message->header->filename);
811 if (trim($filename) == '') {
812 if (trim($name) == '') {
813 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
814 } else {
815 $display_filename = $name;
816 $filename = $name;
817 }
818 } else {
819 $display_filename = $filename;
820 }
821
822 $urlMailbox = urlencode($mailbox);
823 $ent = urlencode($message->header->entity_id);
824
825 $DefaultLink =
826 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
827 if ($where && $what) {
828 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
829 }
830 $Links['download link']['text'] = _("download");
831 $Links['download link']['href'] =
832 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
833 $ImageURL = '';
834
835 /* this executes the attachment hook with a specific MIME-type.
836 * if that doens't have results, it tries if there's a rule
837 * for a more generic type. */
838 $HookResults = do_hook("attachment $type0/$type1", $Links,
839 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
840 if(count($HookResults[1]) <= 1) {
841 $HookResults = do_hook("attachment $type0/*", $Links,
842 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
843 $display_filename, $where, $what);
844 }
845
846 $Links = $HookResults[1];
847 $DefaultLink = $HookResults[6];
848
849 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
850 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
851 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
852 '</b>&nbsp;&nbsp;</small></TD>' .
853 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
854 '<TD><SMALL>';
855 if ($message->header->description) {
856 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
857 }
858 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
859
860
861 $SkipSpaces = 1;
862 foreach ($Links as $Val) {
863 if ($SkipSpaces) {
864 $SkipSpaces = 0;
865 } else {
866 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
867 }
868 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
869 }
870
871 unset($Links);
872
873 $body .= "</SMALL></TD></TR>\n";
874
875 return( $body );
876
877 } elseif (!$message->entities) {
878
879 $type0 = strtolower($message->header->type0);
880 $type1 = strtolower($message->header->type1);
881 $name = decodeHeader($message->header->name);
882
883 if ($message->header->entity_id != $ent_id) {
884 $filename = decodeHeader($message->header->filename);
885 if (trim($filename) == '') {
886 if (trim($name) == '') {
887 if ( trim( $message->header->id ) == '' )
888 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
889 else
890 $display_filename = 'cid: ' . $message->header->id;
891 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
892 } else {
893 $display_filename = $name;
894 $filename = $name;
895 }
896 } else {
897 $display_filename = $filename;
898 }
899
900 $urlMailbox = urlencode($mailbox);
901 $ent = urlencode($message->header->entity_id);
902
903 $DefaultLink =
904 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
905 if ($where && $what) {
906 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
907 }
908 $Links['download link']['text'] = _("download");
909 $Links['download link']['href'] =
910 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
911 $ImageURL = '';
912
913 /* this executes the attachment hook with a specific MIME-type.
914 * if that doens't have results, it tries if there's a rule
915 * for a more generic type. */
916 $HookResults = do_hook("attachment $type0/$type1", $Links,
917 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
918 $display_filename, $where, $what);
919 if(count($HookResults[1]) <= 1) {
920 $HookResults = do_hook("attachment $type0/*", $Links,
921 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
922 $display_filename, $where, $what);
923 }
924
925 $Links = $HookResults[1];
926 $DefaultLink = $HookResults[6];
927
928 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
929 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
930 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
931 '</b>&nbsp;&nbsp;</small></TD>' .
932 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
933 '<TD><SMALL>';
934 if ($message->header->description) {
935 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
936 }
937 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
938
939
940 $SkipSpaces = 1;
941 foreach ($Links as $Val) {
942 if ($SkipSpaces) {
943 $SkipSpaces = 0;
944 } else {
945 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
946 }
947 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
948 }
949
950 unset($Links);
951
952 $body .= "</SMALL></TD></TR>\n";
953 }
954 } else {
955 for ($i = 0; $i < count($message->entities); $i++) {
956 $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
957 }
958 }
959 }
960 return( $body );
961 }
962
963
964 /** this function decodes the body depending on the encoding type. **/
965 function decodeBody($body, $encoding) {
966 $body = str_replace("\r\n", "\n", $body);
967 $encoding = strtolower($encoding);
968
969 global $show_html_default;
970
971 if ($encoding == 'quoted-printable') {
972 $body = quoted_printable_decode($body);
973
974
975 while (ereg("=\n", $body))
976 $body = ereg_replace ("=\n", "", $body);
977
978 } else if ($encoding == 'base64') {
979 $body = base64_decode($body);
980 }
981
982 // All other encodings are returned raw.
983 return $body;
984 }
985
986 /*
987 * This functions decode strings that is encoded according to
988 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
989 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
990 */
991 function decodeHeader ($string, $utfencode=true) {
992 if (is_array($string)) {
993 $string = implode("\n", $string);
994 }
995 $i = 0;
996 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
997 $string, $res)) {
998 $prefix = $res[1];
999 // Ignore white-space between consecutive encoded-words
1000 if (strspn($res[2], " \t") != strlen($res[2])) {
1001 $prefix .= $res[2];
1002 }
1003
1004 if (ucfirst($res[4]) == 'B') {
1005 $replace = base64_decode($res[5]);
1006 } else {
1007 $replace = str_replace('_', ' ', $res[5]);
1008 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
1009 $replace);
1010 /* Only encode into entities by default. Some places
1011 don't need the encoding, like the compose form. */
1012 if ($utfencode) {
1013 $replace = charset_decode($res[3], $replace);
1014 }
1015 }
1016 $string = $prefix . $replace . substr($string, strlen($res[0]));
1017 $i = strlen($prefix) + strlen($replace);
1018 }
1019 return( $string );
1020 }
1021
1022 /*
1023 * Encode a string according to RFC 1522 for use in headers if it
1024 * contains 8-bit characters or anything that looks like it should
1025 * be encoded.
1026 */
1027 function encodeHeader ($string) {
1028 global $default_charset;
1029
1030 // Encode only if the string contains 8-bit characters or =?
1031 $j = strlen( $string );
1032 $l = strstr($string, '=?'); // Must be encoded ?
1033 $ret = '';
1034 for( $i=0; $i < $j; ++$i) {
1035 switch( $string{$i} ) {
1036 case '=':
1037 $ret .= '=3D';
1038 break;
1039 case '?':
1040 $ret .= '=3F';
1041 break;
1042 case '_':
1043 $ret .= '=5F';
1044 break;
1045 case ' ':
1046 $ret .= '_';
1047 break;
1048 default:
1049 $k = ord( $string{$i} );
1050 if ( $k > 126 ) {
1051 $ret .= sprintf("=%02X", $k);
1052 $l = TRUE;
1053 } else
1054 $ret .= $string{$i};
1055 }
1056 }
1057
1058 if ( $l ) {
1059 $string = "=?$default_charset?Q?$ret?=";
1060 }
1061
1062 return( $string );
1063 }
1064
1065 /*
1066 Strips dangerous tags from html messages.
1067 */
1068 function MagicHTML( $body, $id ) {
1069
1070 global $message, $HTTP_SERVER_VARS,
1071 $attachment_common_show_images;
1072
1073 $attachment_common_show_images =
1074 FALSE; // Don't display attached images in HTML mode
1075 $j = strlen( $body ); // Legnth of the HTML
1076 $ret = ''; // Returned string
1077 $bgcolor = '#ffffff'; // Background style color (defaults to white)
1078 $textcolor = '#000000'; // Foreground style color (defaults to black)
1079 $leftmargin = ''; // Left margin style
1080 $title = ''; // HTML title if any
1081
1082 $i = 0;
1083 while ( $i < $j ) {
1084 if ( $body{$i} == '<' ) {
1085 $pos = $i + 1;
1086 $tag = '';
1087 while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
1088 $body{$pos} == "\n") {
1089 $pos ++;
1090 }
1091 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
1092 $body{$pos} != "\t" && $body{$pos} != "\n" &&
1093 $pos < $j ) {
1094 if ($body{$pos} == "<"){
1095 $tag = '';
1096 $pos++;
1097 }
1098 $tag .= $body{$pos};
1099 $pos ++;
1100 }
1101 /*
1102 A comment in HTML is only three characters and isn't
1103 guaranteed to have a space after it. This fudges so
1104 it will be caught by the switch statement.
1105 */
1106 if (ereg("!--", $tag)) {
1107 $tag = "!-- ";
1108 }
1109 switch( strtoupper( $tag ) ) {
1110 // Strips the entire tag and contents
1111 case 'APPL':
1112 case 'EMBE':
1113 case 'FRAM':
1114 case 'SCRI':
1115 case 'OBJE':
1116 $etg = '/' . $tag;
1117 while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
1118 $i < $j ) $i++;
1119 while ( $i < $j && $body{++$i} <> '>' );
1120 // $ret .= "<!-- $tag removed -->";
1121 break;
1122 // Substitute Title
1123 case 'TITL':
1124 $i += 5;
1125 while ( $body{$i} <> '>' && // </title>
1126 $i < $j )
1127 $i++;
1128 $i++;
1129 $title = '';
1130 while ( $body{$i} <> '<' && // </title>
1131 $i < $j ) {
1132 $title .= $body{$i};
1133 $i++;
1134 }
1135 $i += 7;
1136 break;
1137 // Destroy these tags
1138 case 'HTML':
1139 case 'HEAD':
1140 case '/HTM':
1141 case '/HEA':
1142 case '!DOC':
1143 case 'META':
1144 //case 'DIV ':
1145 //case '/DIV':
1146 case '!-- ':
1147 $i += 4;
1148 while ( $body{$i} <> '>' &&
1149 $i < $j )
1150 $i++;
1151 // $i++;
1152 break;
1153 case 'STYL':
1154 $i += 5;
1155 while ( $body{$i} <> '>' && // </title>
1156 $i < $j )
1157 $i++;
1158 $i++;
1159 // We parse the style to look for interesting stuff
1160 $styleblk = '';
1161 while ( $body{$i} <> '>' &&
1162 $i < $j ) {
1163 // First we get the name of the style
1164 $style = '';
1165 while ( $body{$i} <> '>' &&
1166 $body{$i} <> '<' &&
1167 $body{$i} <> '{' &&
1168 $i < $j ) {
1169 if ( isnoSep( $body{$i} ) )
1170 $style .= $body{$i};
1171 $i++;
1172 }
1173 stripComments( $i, $j, $body );
1174 $style = strtoupper( trim( $style ) );
1175 if ( $style == 'BODY' ) {
1176 // Next we look into the definitions of the body style
1177 while ( $body{$i} <> '>' &&
1178 $body{$i} <> '}' &&
1179 $i < $j ) {
1180 // We look for the background color if any.
1181 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
1182 $i += 17;
1183 $bgcolor = getStyleData( $i, $j, $body );
1184 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
1185 $i += 12;
1186 $leftmargin = getStyleData( $i, $j, $body );
1187 }
1188 $i++;
1189 }
1190 } else {
1191 // Other style are mantained
1192 $styleblk .= "$style ";
1193 while ( $body{$i} <> '>' &&
1194 $body{$i} <> '<' &&
1195 $body{$i} <> '}' &&
1196 $i < $j ) {
1197 $styleblk .= $body{$i};
1198 $i++;
1199 }
1200 $styleblk .= $body{$i};
1201 }
1202 stripComments( $i, $j, $body );
1203 if ( $body{$i} <> '>' )
1204 $i++;
1205 }
1206 if ( $styleblk <> '' )
1207 $ret .= "<style>$styleblk";
1208 break;
1209 case 'BODY':
1210 if ( $title <> '' )
1211 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
1212 $ret .= "<TABLE";
1213 $i += 5;
1214 if (! isset($base)) {
1215 $base = '';
1216 }
1217 $ret .= stripEvent( $i, $j, $body, $id, $base );
1218 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
1219 if ( $leftmargin <> '' )
1220 $ret .= "<td width=$leftmargin>&nbsp;</td>";
1221 $ret .= '<td>';
1222 if (strtolower($bgcolor) == 'ffffff' ||
1223 strtolower($bgcolor) == '#ffffff')
1224 $ret .= '<font color=#000000>';
1225 break;
1226 case 'BASE':
1227 $i += 4;
1228 $base = '';
1229 if ( strncasecmp($body{$i}, 'font', 4) ) {
1230 $i += 5;
1231 while ( !isNoSep( $body{$i} ) && $i < $j ) {
1232 $i++;
1233 }
1234 while ( $body{$i} <> '>' && $i < $j ) {
1235 $base .= $body{$i};
1236 $i++;
1237 }
1238 $ret .= "<BASEFONT $base>\n";
1239 break;
1240 }
1241 $i++;
1242 while ( !isNoSep( $body{$i} ) &&
1243 $i < $j ) {
1244 $i++;
1245 }
1246 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
1247 $i += 5;
1248 while ( !isNoSep( $body{$i} ) &&
1249 $i < $j ) {
1250 $i++;
1251 }
1252 while ( $body{$i} <> '>' &&
1253 $i < $j ) {
1254 if ( $body{$i} <> '"' ) {
1255 $base .= $body{$i};
1256 }
1257 $i++;
1258 }
1259 // Debuging $ret .= "<!-- base == $base -->";
1260 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1261 $ret .= "\n<BASE HREF=\"$base\">\n";
1262 }
1263 }
1264 break;
1265 case '/BOD':
1266 $ret .= '</font></td></tr></TABLE>';
1267 $i += 6;
1268 break;
1269 default:
1270 // Following tags can contain some event handler, lets search it
1271 stripComments( $i, $j, $body );
1272 if (! isset($base)) {
1273 $base = '';
1274 }
1275 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1276 // $ret .= "<!-- $tag detected -->";
1277 }
1278 } else {
1279 $ret .= $body{$i};
1280 }
1281 $i++;
1282 }
1283
1284 return( "\n\n<!-- HTML Output ahead -->\n" .
1285 $ret .
1286 /* Base is illegal within HTML
1287 "\n<!-- END of HTML Output --><base href=\"".
1288 get_location() . '/'.
1289 "\">\n\n" );
1290 */
1291 "\n<!-- END of HTML Output -->\n\n" );
1292 }
1293
1294 function isNoSep( $char ) {
1295
1296 switch( $char ) {
1297 case ' ':
1298 case "\n":
1299 case "\t":
1300 case "\r":
1301 case '>':
1302 case '"':
1303 return( FALSE );
1304 break;
1305 default:
1306 return( TRUE );
1307 }
1308
1309 }
1310
1311 /*
1312 The following function is usefull to remove extra data that can cause
1313 html not to display properly. Especialy with MS stuff.
1314 */
1315
1316 function stripComments( &$i, $j, &$body ) {
1317
1318 while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
1319 $i < $j ) {
1320 $i += 5;
1321 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1322 $i < $j )
1323 $i++;
1324 $i++;
1325 }
1326
1327 return;
1328
1329 }
1330
1331 /* Gets the style data of a specific style */
1332
1333 function getStyleData( &$i, $j, &$body ) {
1334
1335 // We skip spaces
1336 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1337 $i < $j ) {
1338 $i++;
1339 }
1340 // And get the color
1341 $ret = '';
1342 while ( isNoSep( $body{$i} ) &&
1343 $i < $j ) {
1344 $ret .= $body{$i};
1345 $i++;
1346 }
1347
1348 return( $ret );
1349 }
1350
1351 /*
1352 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1353 change on with no (onload -> noload)
1354 */
1355
1356 function stripEvent( &$i, $j, &$body, $id, $base ) {
1357
1358 global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
1359
1360 $ret = '';
1361
1362 while ( $body{$i} <> '>' &&
1363 $i < $j ) {
1364 /**
1365 * [ 545933 ] Cross-site scripting vulnerability
1366 * <hr>
1367 * <img x="<foo>" src=javascript:alert(1) y="</foo>">
1368 * <hr>
1369 *
1370 * This code will ignore anything within the quotes
1371 * so they don't mess us up.
1372 */
1373 if ( $body{$i} == '"' || $body{$i} == "'" ){
1374 $quotechar = $body{$i};
1375 do {
1376 $ret .= $body{$i};
1377 $i++;
1378 } while ($body{$i} != $quotechar && $i < $j);
1379 }
1380 $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
1381 switch( $etg ) {
1382 case 'src':
1383 // This is probably a src specification
1384 $k = $i + 3;
1385 while( !isNoSep( $body{$k} )) {
1386 $k++;
1387 }
1388 if ( $body{$k} == '=' ) {
1389 /* It is indeed */
1390 $k++;
1391 while( !isNoSep( $body{$k} ) &&
1392 $k < $j ) {
1393 $k++;
1394 }
1395 $src = '';
1396 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1397 $k < $j ) {
1398 $src .= $body{$k};
1399 $k++;
1400 }
1401 $k++;
1402 while( !isNoSep( $body{$k} ) &&
1403 $k < $j ) {
1404 $k++;
1405 }
1406 $k++;
1407 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1408 $src = substr( $src, 4 );
1409 $src = "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
1410 urlencode( $message->header->mailbox ) .
1411 "&amp;passed_ent_id=" . find_ent_id( $src, $message );
1412 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
1413 stristr( $src, $base_uri ) ) {
1414 /* Javascript and local urls goes out */
1415 if (!$view_unsafe_images) {
1416 $src = '../images/' . _("sec_remove_eng.png");
1417 }
1418 $has_unsafe_images = 1;
1419 }
1420 $ret .= 'src="' . $src . '" ';
1421 $i = $k - 2;
1422 } else {
1423 $ret .= 'src';
1424 $i = $i + 3;
1425 }
1426
1427 break;
1428 case '../':
1429 // Retrolinks are not allowed without a base because they mess with SM security
1430 if ( $base == '' ) {
1431 $i += 2;
1432 } else {
1433 $ret .= '.';
1434 }
1435 break;
1436 case 'cid':
1437 // Internal link
1438 $k = $i-1;
1439 if ( $body{$i+3} == ':') {
1440 $i +=4;
1441 $name = '';
1442 while ( isNoSep( $body{$i} ) &&
1443 $i < $j ) {
1444 $name .= $body{$i++};
1445 }
1446 if ( $name <> '' ) {
1447 $ret .= "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
1448 urlencode( $message->header->mailbox ) .
1449 "&amp;passed_ent_id=" . find_ent_id( $name, $message );
1450 if ( $body{$k} == '"' )
1451 $ret .= '" ';
1452 else
1453 $ret .= ' ';
1454 }
1455 if ( $body{$i} == '>' )
1456 $i -= 1;
1457 }
1458 break;
1459 case ' on':
1460 case "\non":
1461 case "\ron":
1462 case "\ton":
1463 $ret .= ' no';
1464 $i += 2;
1465 break;
1466 case 'pt:':
1467 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
1468 $ret .= '_no/';
1469 } else {
1470 $ret .= $etg;
1471 }
1472 $i += 2;
1473 break;
1474 default:
1475 $ret .= $body{$i};
1476 }
1477 $i++;
1478 }
1479 return( $ret );
1480 }
1481
1482
1483 /* This function trys to locate the entity_id of a specific mime element */
1484
1485 function find_ent_id( $id, $message ) {
1486 $ret = '';
1487 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1488 if ( $message->entities[$i]->header->entity_id == '' || $message->entities[$i]->header->type ) {
1489 $ret = find_ent_id( $id, $message->entities[$i] );
1490 } else {
1491 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1492 $ret = $message->entities[$i]->header->entity_id;
1493 }
1494
1495 }
1496
1497 return( $ret );
1498
1499 }
1500 ?>