removed displaying attachments in case we are dealing with
[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 global $mailbox;
96 $properties = array();
97 $msg = new message();
98 if ($structure{0} == '(') {
99 $old_ent_id = $ent_id;
100 $ent_id = mime_new_element_level($ent_id);
101 $start = $end = -1;
102 do {
103 $start = $end+1;
104 $end = mime_match_parenthesis ($start, $structure);
105
106 /* check if we are dealing with a new entity-level */
107 $i = strrpos($ent_id,'.');
108 if ($i>0) {
109 $ent = substr($ent_id, $i+1);
110 } else {
111 $ent = '';
112 }
113 /* add "forgotten" parent entities (alternative and relative) */
114 if ($ent == '0') {
115 /* new entity levels have information about the type (type1) and
116 * the properties. This information is situated at the end of the
117 * structure string like for example (example between the brackets)
118 * [ "RELATED" ("BOUNDARY" "myboundary" "TYPE" "plain/html") ]
119 */
120
121 /* get the involved properties for parsing to mime_get_properties */
122 $startprop = strrpos($structure,'(');
123 $properties_str = substr($structure,$startprop);
124 $endprop = mime_match_parenthesis ($startprop, $structure);
125 $propstr = substr($structure, $startprop + 1, ($endprop - $startprop)-1);
126 /* cut off the used properties */
127 if ($startprop) {
128 $structure_end = substr($structure, $endprop+2);
129 $structure = trim(substr($structure,0,$startprop));
130 }
131
132 /* get type1 */
133 $pos = strrpos($structure,' ');
134 $type1 = strtolower(substr($structure, $pos+2, (count($structure)-2)));
135
136 /* cut off type1 */
137 if ($pos && $startprop) {
138 $structure = trim(substr($structure, 0, $pos));
139 }
140
141 /* process the found information */
142 $properties = mime_get_props($properties, $properties_str);
143 if (count($properties)>0) {
144 $msg->header->entity_id = $old_ent_id;
145 $msg->header->type0 = 'multipart';
146 $msg->header->type1 = $type1;
147 for ($i=0; $i < count($properties); $i++) {
148 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
149 }
150 }
151 $structure = $structure . ' ' . $structure_end;
152 }
153 $element = substr($structure, $start+1, ($end - $start)-1);
154 $ent_id = mime_increment_id ($ent_id);
155 $newmsg = mime_parse_structure ($element, $ent_id);
156 /* set mailbox in case of message/rfc822 entities */
157 if (isset($newmsg->header->type0) && isset($newmsg->header->type1)) {
158 if ($newmsg->header->type0 == 'message' && $newmsg->header->type1 == 'rfc822') {
159 $newmsg->header->mailbox=$mailbox;
160 }
161 }
162 $msg->addEntity ($newmsg);
163
164 } while ($structure{$end+1} == '(');
165 } else {
166 // parse the elements
167 $msg = mime_get_element ($structure, $msg, $ent_id);
168 }
169 return $msg;
170 }
171
172
173 /* Increments the element ID. An element id can look like any of
174 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
175 * the last number of the element id, changing 1.2 to 1.3.
176 */
177 function mime_increment_id ($id) {
178
179 if (strpos($id, '.')) {
180 $first = substr($id, 0, strrpos($id, '.'));
181 $last = substr($id, strrpos($id, '.')+1);
182 $last++;
183 $new = $first . '.' .$last;
184 } else {
185 $new = $id + 1;
186 }
187
188 return $new;
189 }
190
191 /*
192 * See comment for mime_increment_id().
193 * This adds another level on to the entity_id changing 1.3 to 1.3.0
194 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
195 * before it can be used. I left it this way so as not to have
196 * to make a special case if it is the first entity_id. It
197 * always increments it, and that works fine.
198 */
199 function mime_new_element_level ($id) {
200
201 if (!$id) {
202 $id = 0;
203 } else {
204 $id = $id . '.0';
205 }
206
207 return( $id );
208 }
209
210 function mime_get_element (&$structure, $msg, $ent_id) {
211
212 $elem_num = 1;
213 $msg->header = new msg_header();
214 $msg->header->entity_id = $ent_id;
215 $properties = array();
216 while (strlen($structure) > 0) {
217 $structure = trim($structure);
218 $char = $structure{0};
219
220 if (strtolower(substr($structure, 0, 3)) == 'nil') {
221 $text = '';
222 $structure = substr($structure, 3);
223 } else if ($char == '"') {
224 // loop through until we find the matching quote, and return that as a string
225 $pos = 1;
226 $text = '';
227 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
228 $text .= $char;
229 $pos++;
230 }
231 $structure = substr($structure, strlen($text) + 2);
232 } else if ($char == '{') {
233 /**
234 * loop through until we find the matching quote,
235 * and return that as a string
236 */
237 $pos = 1;
238 $len = '';
239 while (($char = $structure{$pos}) != '}'
240 && $pos < strlen($structure)) {
241 $len .= $char;
242 $pos++;
243 }
244 $structure = substr($structure, strlen($len) + 4);
245 $text = substr($structure, 0, $len);
246 $structure = substr($structure, $len + 1);
247 } else if ($char == '(') {
248 // comment me
249 $end = mime_match_parenthesis (0, $structure);
250 $sub = substr($structure, 1, $end-1);
251 $properties = mime_get_props($properties, $sub);
252 $structure = substr($structure, strlen($sub) + 2);
253 } else {
254 // loop through until we find a space or an end parenthesis
255 $pos = 0;
256 $char = $structure{$pos};
257 $text = '';
258 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
259 $text .= $char;
260 $pos++;
261 $char = $structure{$pos};
262 }
263 $structure = substr($structure, strlen($text));
264 }
265
266 // This is where all the text parts get put into the header
267 switch ($elem_num) {
268 case 1:
269 $msg->header->type0 = strtolower($text);
270 break;
271 case 2:
272 $msg->header->type1 = strtolower($text);
273 break;
274 case 4: // Id
275 // Invisimail enclose images with <>
276 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
277 break;
278 case 5:
279 $msg->header->description = $text;
280 break;
281 case 6:
282 $msg->header->encoding = strtolower($text);
283 break;
284 case 7:
285 $msg->header->size = $text;
286 break;
287 default:
288 if ($msg->header->type0 == 'text' && $elem_num == 8) {
289 // This is a plain text message, so lets get the number of lines
290 // that it contains.
291 $msg->header->num_lines = $text;
292
293 } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
294 // This is an encapsulated message, so lets start all over again and
295 // parse this message adding it on to the existing one.
296 $structure = trim($structure);
297 if ( $structure{0} == '(' ) {
298 $e = mime_match_parenthesis (0, $structure);
299 $structure = substr($structure, 0, $e);
300 $structure = substr($structure, 1);
301 $m = mime_parse_structure($structure, $msg->header->entity_id);
302
303 // the following conditional is there to correct a bug that wasn't
304 // incrementing the entity IDs correctly because of the special case
305 // that message/rfc822 is. This fixes it fine.
306 if (substr($structure, 1, 1) != '(')
307 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
308
309 // Now we'll go through and reformat the results.
310 if ($m->entities) {
311 for ($i=0; $i < count($m->entities); $i++) {
312 $msg->addEntity($m->entities[$i]);
313 }
314 } else {
315 $msg->addEntity($m);
316 }
317 $structure = "";
318 }
319 }
320 break;
321 }
322 $elem_num++;
323 $text = "";
324 }
325 // loop through the additional properties and put those in the various headers
326 for ($i=0; $i < count($properties); $i++) {
327 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
328 }
329
330 return $msg;
331 }
332
333 /*
334 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
335 * figure out how to do this part, so I decided to go to bed. I woke up
336 * in the morning and had a flash of insight. I went to the white-board
337 * and scribbled it out, then spent a bit programming it, and this is the
338 * result. Nothing complicated, but I think my brain was fried yesterday.
339 * Funny how that happens some times.
340 *
341 * This gets properties in a nested parenthesisized list. For example,
342 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
343 * This returns an array called $props with all paired up properties.
344 * It ignores the "attachment" for now, maybe that should change later
345 * down the road. In this case, what is returned is:
346 * $props[0]["name"] = "filename";
347 * $props[0]["value"] = "luke.tar.gz";
348 */
349 function mime_get_props ($props, $structure) {
350
351 while (strlen($structure) > 0) {
352 $structure = trim($structure);
353 $char = $structure{0};
354 if ($char == '"') {
355 $pos = 1;
356 $tmp = '';
357 while ( ( $char = $structure{$pos} ) != '"' &&
358 $pos < strlen($structure)) {
359 $tmp .= $char;
360 $pos++;
361 }
362 $structure = trim(substr($structure, strlen($tmp) + 2));
363 $char = $structure{0};
364
365 if ($char == '"') {
366 $pos = 1;
367 $value = '';
368 while ( ( $char = $structure{$pos} ) != '"' &&
369 $pos < strlen($structure) ) {
370 $value .= $char;
371 $pos++;
372 }
373 $structure = trim(substr($structure, strlen($value) + 2));
374 $k = count($props);
375 $props[$k]['name'] = strtolower($tmp);
376 $props[$k]['value'] = $value;
377 if ($structure != '') {
378 mime_get_props($props, $structure);
379 } else {
380 return $props;
381 }
382 } else if ($char == '(') {
383 $end = mime_match_parenthesis (0, $structure);
384 $sub = substr($structure, 1, $end-1);
385 if (! isset($props))
386 $props = array();
387 $props = mime_get_props($props, $sub);
388 $structure = substr($structure, strlen($sub) + 2);
389 return $props;
390 }
391 } else if ($char == '(') {
392 $end = mime_match_parenthesis (0, $structure);
393 $sub = substr($structure, 1, $end-1);
394 $props = mime_get_props($props, $sub);
395 $structure = substr($structure, strlen($sub) + 2);
396 return $props;
397 } else {
398 return $props;
399 }
400 }
401 }
402
403 /*
404 * Matches parenthesis. It will return the position of the matching
405 * parenthesis in $structure. For instance, if $structure was:
406 * ("text" "plain" ("val1name", "1") nil ... )
407 * x x
408 * then this would return 42 to match up those two.
409 */
410 function mime_match_parenthesis ($pos, $structure) {
411
412 $j = strlen( $structure );
413
414 // ignore all extra characters
415 // If inside of a string, skip string -- Boundary IDs and other
416 // things can have ) in them.
417 if ( $structure{$pos} != '(' ) {
418 return( $j );
419 }
420
421 while ( $pos < $j ) {
422 $pos++;
423 if ($structure{$pos} == ')') {
424 return $pos;
425 } elseif ($structure{$pos} == '"') {
426 $pos++;
427 while ( $structure{$pos} != '"' &&
428 $pos < $j ) {
429 if (substr($structure, $pos, 2) == '\\"') {
430 $pos++;
431 } elseif (substr($structure, $pos, 2) == '\\\\') {
432 $pos++;
433 }
434 $pos++;
435 }
436 } elseif ( $structure{$pos} == '(' ) {
437 $pos = mime_match_parenthesis ($pos, $structure);
438 }
439 }
440 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
441 return( $pos );
442 }
443
444 function mime_fetch_body($imap_stream, $id, $ent_id ) {
445
446 /*
447 * do a bit of error correction. If we couldn't find the entity id, just guess
448 * that it is the first one. That is usually the case anyway.
449 */
450 if (!$ent_id) {
451 $ent_id = 1;
452 }
453
454 $cmd = "FETCH $id BODY[$ent_id]";
455 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
456
457 do {
458 $topline = trim(array_shift( $data ));
459 } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ;
460 $wholemessage = implode('', $data);
461 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
462
463 $ret = substr( $wholemessage, 0, $regs[1] );
464 /*
465 There is some information in the content info header that could be important
466 in order to parse html messages. Let's get them here.
467 */
468 if ( $ret{0} == '<' ) {
469 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
470 /* BASE within HTML documents is illegal (see w3 spec)
471 * $base = '';
472 * $k = 10;
473 * foreach( $data as $d ) {
474 * if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
475 * $j = strlen( $d );
476 * $i = 13;
477 * $base = '';
478 * while ( $i < $j &&
479 * ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
480 * $i++;
481 * while ( $i < $j ) {
482 * if ( isNoSep( $d{$i} ) )
483 * $base .= $d{$i};
484 * $i++;
485 * }
486 * $k = 0;
487 * } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
488 * $base .= substr( $d, 1 );
489 * }
490 * $k++;
491 * }
492 * if ( $base <> '' ) {
493 * $ret = "<base href=\"$base\">" . $ret;
494 * }
495 * */
496 }
497 } else if (ereg('"([^"]*)"', $topline, $regs)) {
498 $ret = $regs[1];
499 } else {
500 global $where, $what, $mailbox, $passed_id, $startMessage;
501 $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
502 if (isset($where) && isset($what)) {
503 $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
504 } else {
505 $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
506 }
507 $par .= '&amp;response=' . urlencode($response) .
508 '&amp;message=' . urlencode($message).
509 '&amp;topline=' . urlencode($topline);
510
511 echo '<tt><br>' .
512 '<table width="80%"><tr>' .
513 '<tr><td colspan=2>' .
514 _("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!") .
515 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
516 _("Submit message") . '</A><BR>&nbsp;' .
517 '</td></tr>' .
518 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
519 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
520 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
521 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
522 "</table><BR></tt></font><hr>";
523
524 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
525 array_shift($data);
526 $wholemessage = implode('', $data);
527
528 $ret = $wholemessage;
529 }
530 return( $ret );
531 }
532
533 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
534 // do a bit of error correction. If we couldn't find the entity id, just guess
535 // that it is the first one. That is usually the case anyway.
536 if (!$ent_id) {
537 $ent_id = 1;
538 }
539 $sid = sqimap_session_id();
540 // Don't kill the connection if the browser is over a dialup
541 // and it would take over 30 seconds to download it.
542
543 // donĀ“t call set_time_limit in safe mode.
544 if (!ini_get("safe_mode")) {
545 set_time_limit(0);
546 }
547
548 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
549 $cnt = 0;
550 $continue = true;
551 $read = fgets ($imap_stream,4096);
552 // This could be bad -- if the section has sqimap_session_id() . ' OK'
553 // or similar, it will kill the download.
554 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
555 if (trim($read) == ')==') {
556 $read1 = $read;
557 $read = fgets ($imap_stream,4096);
558 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
559 return;
560 } else {
561 echo decodeBody($read1, $encoding) .
562 decodeBody($read, $encoding);
563 }
564 } else if ($cnt) {
565 echo decodeBody($read, $encoding);
566 }
567 $read = fgets ($imap_stream,4096);
568 $cnt++;
569 }
570 }
571
572 /* -[ END MIME DECODING ]----------------------------------------------------------- */
573
574
575
576 /* This is the first function called. It decides if this is a multipart
577 message or if it should be handled as a single entity
578 */
579 function decodeMime ($imap_stream, &$header) {
580 global $username, $key, $imapServerAddress, $imapPort;
581 return mime_structure ($imap_stream, $header);
582 }
583
584 // This is here for debugging purposese. It will print out a list
585 // of all the entity IDs that are in the $message object.
586
587 function listEntities ($message) {
588 if ($message) {
589 if ($message->header->entity_id)
590 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
591 for ($i = 0; $message->entities[$i]; $i++) {
592 $msg = listEntities($message->entities[$i], $ent_id);
593 if ($msg)
594 return $msg;
595 }
596 }
597 }
598
599
600 /* returns a $message object for a particular entity id */
601 function getEntity ($message, $ent_id) {
602 if ($message) {
603 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id))
604 {
605 return $message;
606 } else {
607 for ($i = 0; isset($message->entities[$i]); $i++) {
608 $msg = getEntity ($message->entities[$i], $ent_id);
609 if ($msg) {
610 return $msg;
611 }
612 }
613 }
614 }
615 }
616
617 /*
618 * figures out what entity to display and returns the $message object
619 * for that entity.
620 */
621 function findDisplayEntity ($msg, $textOnly = 1) {
622 global $show_html_default;
623
624 $entity = 0;
625
626 if ($msg) {
627 if ( $msg->header->type0 == 'multipart' &&
628 ( $msg->header->type1 == 'alternative' ||
629 $msg->header->type1 == 'mixed' ||
630 $msg->header->type1 == 'related' ) &&
631 $show_html_default && ! $textOnly ) {
632 $entity = findDisplayEntityHTML($msg);
633 }
634
635 // Show text/plain or text/html -- the first one we find.
636 if ( $entity == 0 &&
637 $msg->header->type0 == 'text' &&
638 ( $msg->header->type1 == 'plain' ||
639 $msg->header->type1 == 'html' ) &&
640 isset($msg->header->entity_id) ) {
641 $entity = $msg->header->entity_id;
642 }
643
644 $i = 0;
645 while ($entity == 0 && isset($msg->entities[$i]) ) {
646 $entity = findDisplayEntity($msg->entities[$i], $textOnly);
647 $i++;
648 }
649 }
650
651 return( $entity );
652 }
653
654 /* Shows the HTML version */
655 function findDisplayEntityHTML ($message) {
656
657 if ( $message->header->type0 == 'text' &&
658 $message->header->type1 == 'html' &&
659 isset($message->header->entity_id)) {
660 return $message->header->entity_id;
661 }
662 for ($i = 0; isset($message->entities[$i]); $i ++) {
663 if ( $message->header->type0 == 'message' &&
664 $message->header->type1 == 'rfc822' &&
665 isset($message->header->entity_id)) {
666 return 0;
667 }
668 $entity = findDisplayEntityHTML($message->entities[$i]);
669 if ($entity != 0) {
670 return $entity;
671 }
672 }
673
674 return 0;
675 }
676
677 /*
678 * translateText
679 * Extracted from strings.php 23/03/2002
680 */
681
682 function translateText(&$body, $wrap_at, $charset) {
683 global $where, $what; /* from searching */
684 global $color; /* color theme */
685
686 require_once('../functions/url_parser.php');
687
688 $body_ary = explode("\n", $body);
689 $PriorQuotes = 0;
690 for ($i=0; $i < count($body_ary); $i++) {
691 $line = $body_ary[$i];
692 if (strlen($line) - 2 >= $wrap_at) {
693 sqWordWrap($line, $wrap_at);
694 }
695 $line = charset_decode($charset, $line);
696 $line = str_replace("\t", ' ', $line);
697
698 parseUrl ($line);
699
700 $Quotes = 0;
701 $pos = 0;
702 $j = strlen( $line );
703
704 while ( $pos < $j ) {
705 if ($line[$pos] == ' ') {
706 $pos ++;
707 } else if (strpos($line, '&gt;', $pos) === $pos) {
708 $pos += 4;
709 $Quotes ++;
710 } else {
711 break;
712 }
713 }
714
715 if ($Quotes > 1) {
716 if (! isset($color[14])) {
717 $color[14] = '#FF0000';
718 }
719 $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
720 } elseif ($Quotes) {
721 if (! isset($color[13])) {
722 $color[13] = '#800000';
723 }
724 $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
725 }
726
727 $body_ary[$i] = $line;
728 }
729 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
730 }
731
732 /* debugfunction for looping through entities and displaying correct entities */
733 function listMyEntities ($message) {
734
735 if ($message) {
736 if ($message->header->entity_id) {
737 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
738 }
739 if (!($message->header->type0 == 'message' && $message->header->type1 == 'rfc822')) {
740 if (isset($message->header->boundary) ) {
741 $ent_id = $message->header->entity_id;
742 $var = $message->header->boundary;
743 if ($var !='')
744 echo "<b>$ent_id boundary = $var</b><br>";
745 }
746 if (isset($message->header->type) ) {
747 $var = $message->header->type;
748 if ($var !='')
749 echo "<b>$ent_id type = $var</b><br>";
750 }
751 for ($i = 0; $message->entities[$i]; $i++) {
752 $msg = listMyEntities($message->entities[$i]);
753 }
754
755 if ($msg ) return $msg;
756 }
757 }
758
759 }
760
761
762
763 /* This returns a parsed string called $body. That string can then
764 be displayed as the actual message in the HTML. It contains
765 everything needed, including HTML Tags, Attachments at the
766 bottom, etc.
767 */
768 function formatBody($imap_stream, $message, $color, $wrap_at) {
769 // this if statement checks for the entity to show as the
770 // primary message. To add more of them, just put them in the
771 // order that is their priority.
772 global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body,
773 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
774
775 $has_unsafe_images = 0;
776
777 $id = $message->header->id;
778
779 $urlmailbox = urlencode($message->header->mailbox);
780 // Get the right entity and redefine message to be this entity
781 // Pass the 0 to mean that we want the 'best' viewable one
782 $ent_num = findDisplayEntity ($message, 0);
783 $body_message = getEntity($message, $ent_num);
784 if (($body_message->header->type0 == 'text') ||
785 ($body_message->header->type0 == 'rfc822')) {
786 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
787 $body = decodeBody($body, $body_message->header->encoding);
788 $hookResults = do_hook("message_body", $body);
789 $body = $hookResults[1];
790 // If there are other types that shouldn't be formatted, add
791 // them here
792 if ($body_message->header->type1 == 'html') {
793 if ( $show_html_default <> 1 ) {
794 $body = strip_tags( $body );
795 translateText($body, $wrap_at, $body_message->header->charset);
796 } else {
797 $body = MagicHTML( $body, $id, $message );
798 }
799 } else {
800 translateText($body, $wrap_at, $body_message->header->charset);
801 }
802
803 $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>";
804 if ($has_unsafe_images) {
805 if ($view_unsafe_images) {
806 $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";
807 } else {
808 $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";
809 }
810 }
811
812 /** Display the ATTACHMENTS: message if there's more than one part **/
813 if (isset($message->entities[1])) {
814 /* Header-type alternative means we choose the best one to display
815 so don't show the alternatives as attachment. Header-type related
816 means that the attachments are already part of the related message.
817 */
818 if ($message->header->type1 !='related' && $message->header->type1 !='alternative') {
819 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
820 }
821 }
822 } else {
823 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
824 }
825 return ($body);
826 }
827
828 /*
829 * A recursive function that returns a list of attachments with links
830 * to where to download these attachments
831 */
832 function formatAttachments($message, $ent_id, $mailbox, $id) {
833 global $where, $what;
834 global $startMessage, $color;
835 static $ShownHTML = 0;
836
837 $body = '';
838 if ($ShownHTML == 0) {
839
840 $ShownHTML = 1;
841 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
842 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
843 _("Attachments") . ':' .
844 "</B></TH></TR><TR><TD>\n" .
845 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
846 formatAttachments($message, $ent_id, $mailbox, $id) .
847 "</TABLE></TD></TR></TABLE>";
848
849 } else if ($message) {
850 $header = $message->header;
851 $type0 = strtolower($header->type0);
852 $type1 = strtolower($header->type1);
853 $name = '';
854 if (isset($header->name)) {
855 $name = decodeHeader($header->name);
856 }
857 if ($type0 =='message' && $type1 == 'rfc822') {
858
859 $filename = decodeHeader($message->header->filename);
860 if (trim($filename) == '') {
861 if (trim($name) == '') {
862 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
863 } else {
864 $display_filename = $name;
865 $filename = $name;
866 }
867 } else {
868 $display_filename = $filename;
869 }
870
871 $urlMailbox = urlencode($mailbox);
872 $ent = urlencode($message->header->entity_id);
873
874 $DefaultLink =
875 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
876 if ($where && $what) {
877 $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
878 }
879 $Links['download link']['text'] = _("download");
880 $Links['download link']['href'] =
881 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
882 $ImageURL = '';
883
884 /* this executes the attachment hook with a specific MIME-type.
885 * if that doens't have results, it tries if there's a rule
886 * for a more generic type. */
887 $HookResults = do_hook("attachment $type0/$type1", $Links,
888 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
889 if(count($HookResults[1]) <= 1) {
890 $HookResults = do_hook("attachment $type0/*", $Links,
891 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
892 $display_filename, $where, $what);
893 }
894
895 $Links = $HookResults[1];
896 $DefaultLink = $HookResults[6];
897
898 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
899 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
900 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
901 '</b>&nbsp;&nbsp;</small></TD>' .
902 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
903 '<TD><SMALL>';
904 if ($message->header->description) {
905 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
906 }
907 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
908
909
910 $SkipSpaces = 1;
911 foreach ($Links as $Val) {
912 if ($SkipSpaces) {
913 $SkipSpaces = 0;
914 } else {
915 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
916 }
917 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
918 }
919
920 unset($Links);
921
922 $body .= "</SMALL></TD></TR>\n";
923
924 return( $body );
925
926 } elseif (!$message->entities) {
927
928 $type0 = strtolower($message->header->type0);
929 $type1 = strtolower($message->header->type1);
930 $name = decodeHeader($message->header->name);
931
932 if ($message->header->entity_id != $ent_id) {
933 $filename = decodeHeader($message->header->filename);
934 if (trim($filename) == '') {
935 if (trim($name) == '') {
936 if ( trim( $message->header->id ) == '' )
937 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
938 else
939 $display_filename = 'cid: ' . $message->header->id;
940 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
941 } else {
942 $display_filename = $name;
943 $filename = $name;
944 }
945 } else {
946 $display_filename = $filename;
947 }
948
949 $urlMailbox = urlencode($mailbox);
950 $ent = urlencode($message->header->entity_id);
951
952 $DefaultLink =
953 "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
954 if ($where && $what) {
955 $DefaultLink = '&amp;where='. urlencode($where).'&amp;what='.urlencode($what);
956 }
957 $Links['download link']['text'] = _("download");
958 $Links['download link']['href'] =
959 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
960 $ImageURL = '';
961
962 /* this executes the attachment hook with a specific MIME-type.
963 * if that doens't have results, it tries if there's a rule
964 * for a more generic type. */
965 $HookResults = do_hook("attachment $type0/$type1", $Links,
966 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
967 $display_filename, $where, $what);
968 if(count($HookResults[1]) <= 1) {
969 $HookResults = do_hook("attachment $type0/*", $Links,
970 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
971 $display_filename, $where, $what);
972 }
973
974 $Links = $HookResults[1];
975 $DefaultLink = $HookResults[6];
976
977 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
978 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
979 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
980 '</b>&nbsp;&nbsp;</small></TD>' .
981 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
982 '<TD><SMALL>';
983 if ($message->header->description) {
984 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
985 }
986 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
987
988
989 $SkipSpaces = 1;
990 foreach ($Links as $Val) {
991 if ($SkipSpaces) {
992 $SkipSpaces = 0;
993 } else {
994 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
995 }
996 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
997 }
998
999 unset($Links);
1000
1001 $body .= "</SMALL></TD></TR>\n";
1002 }
1003 } else {
1004 for ($i = 0; $i < count($message->entities); $i++) {
1005 $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
1006 }
1007 }
1008 }
1009 return( $body );
1010 }
1011
1012
1013 /** this function decodes the body depending on the encoding type. **/
1014 function decodeBody($body, $encoding) {
1015 $body = str_replace("\r\n", "\n", $body);
1016 $encoding = strtolower($encoding);
1017
1018 global $show_html_default;
1019
1020 if ($encoding == 'quoted-printable' ||
1021 $encoding == 'quoted_printable') {
1022 $body = quoted_printable_decode($body);
1023
1024
1025 while (ereg("=\n", $body))
1026 $body = ereg_replace ("=\n", "", $body);
1027
1028 } else if ($encoding == 'base64') {
1029 $body = base64_decode($body);
1030 }
1031
1032 // All other encodings are returned raw.
1033 return $body;
1034 }
1035
1036 /*
1037 * This functions decode strings that is encoded according to
1038 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
1039 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
1040 */
1041 function decodeHeader ($string, $utfencode=true) {
1042 if (is_array($string)) {
1043 $string = implode("\n", $string);
1044 }
1045 $i = 0;
1046 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
1047 $string, $res)) {
1048 $prefix = $res[1];
1049 // Ignore white-space between consecutive encoded-words
1050 if (strspn($res[2], " \t") != strlen($res[2])) {
1051 $prefix .= $res[2];
1052 }
1053
1054 if (ucfirst($res[4]) == 'B') {
1055 $replace = base64_decode($res[5]);
1056 } else {
1057 $replace = str_replace('_', ' ', $res[5]);
1058 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
1059 $replace);
1060 /* Only encode into entities by default. Some places
1061 don't need the encoding, like the compose form. */
1062 if ($utfencode) {
1063 $replace = charset_decode($res[3], $replace);
1064 }
1065 }
1066 $string = $prefix . $replace . substr($string, strlen($res[0]));
1067 $i = strlen($prefix) + strlen($replace);
1068 }
1069 return( $string );
1070 }
1071
1072 /*
1073 * Encode a string according to RFC 1522 for use in headers if it
1074 * contains 8-bit characters or anything that looks like it should
1075 * be encoded.
1076 */
1077 function encodeHeader ($string) {
1078 global $default_charset;
1079
1080 // Encode only if the string contains 8-bit characters or =?
1081 $j = strlen( $string );
1082 $l = strstr($string, '=?'); // Must be encoded ?
1083 $ret = '';
1084 for( $i=0; $i < $j; ++$i) {
1085 switch( $string{$i} ) {
1086 case '=':
1087 $ret .= '=3D';
1088 break;
1089 case '?':
1090 $ret .= '=3F';
1091 break;
1092 case '_':
1093 $ret .= '=5F';
1094 break;
1095 case ' ':
1096 $ret .= '_';
1097 break;
1098 default:
1099 $k = ord( $string{$i} );
1100 if ( $k > 126 ) {
1101 $ret .= sprintf("=%02X", $k);
1102 $l = TRUE;
1103 } else
1104 $ret .= $string{$i};
1105 }
1106 }
1107
1108 if ( $l ) {
1109 $string = "=?$default_charset?Q?$ret?=";
1110 }
1111
1112 return( $string );
1113 }
1114
1115 /* This function trys to locate the entity_id of a specific mime element */
1116
1117 function find_ent_id( $id, $message ) {
1118 $ret = '';
1119 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1120 if (( $message->entities[$i]->header->type1 == 'alternative') ||
1121 ( $message->entities[$i]->header->type1 == 'related') ||
1122 ( $message->entities[$i]->header->type1 == 'mixed')) {
1123 $ret = find_ent_id( $id, $message->entities[$i] );
1124 } else {
1125 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1126 $ret = $message->entities[$i]->header->entity_id;
1127 }
1128
1129 }
1130 return( $ret );
1131 }
1132
1133 /**
1134 ** HTMLFILTER ROUTINES
1135 */
1136
1137 /**
1138 * This function returns the final tag out of the tag name, an array
1139 * of attributes, and the type of the tag. This function is called by
1140 * sq_sanitize internally.
1141 *
1142 * @param $tagname the name of the tag.
1143 * @param $attary the array of attributes and their values
1144 * @param $tagtype The type of the tag (see in comments).
1145 * @return a string with the final tag representation.
1146 */
1147 function sq_tagprint($tagname, $attary, $tagtype){
1148 $me = "sq_tagprint";
1149 if ($tagtype == 2){
1150 $fulltag = '</' . $tagname . '>';
1151 } else {
1152 $fulltag = '<' . $tagname;
1153 if (is_array($attary) && sizeof($attary)){
1154 $atts = Array();
1155 while (list($attname, $attvalue) = each($attary)){
1156 array_push($atts, "$attname=$attvalue");
1157 }
1158 $fulltag .= ' ' . join(" ", $atts);
1159 }
1160 if ($tagtype == 3){
1161 $fulltag .= " /";
1162 }
1163 $fulltag .= ">";
1164 }
1165 return $fulltag;
1166 }
1167
1168 /**
1169 * A small helper function to use with array_walk. Modifies a by-ref
1170 * value and makes it lowercase.
1171 *
1172 * @param $val a value passed by-ref.
1173 * @return void since it modifies a by-ref value.
1174 */
1175 function sq_casenormalize(&$val){
1176 $val = strtolower($val);
1177 }
1178
1179 /**
1180 * This function skips any whitespace from the current position within
1181 * a string and to the next non-whitespace value.
1182 *
1183 * @param $body the string
1184 * @param $offset the offset within the string where we should start
1185 * looking for the next non-whitespace character.
1186 * @return the location within the $body where the next
1187 * non-whitespace char is located.
1188 */
1189 function sq_skipspace($body, $offset){
1190 $me = "sq_skipspace";
1191 preg_match("/^(\s*)/s", substr($body, $offset), $matches);
1192 if (sizeof($matches{1})){
1193 $count = strlen($matches{1});
1194 $offset += $count;
1195 }
1196 return $offset;
1197 }
1198
1199 /**
1200 * This function looks for the next character within a string. It's
1201 * really just a glorified "strpos", except it catches if failures
1202 * nicely.
1203 *
1204 * @param $body The string to look for needle in.
1205 * @param $offset Start looking from this position.
1206 * @param $needle The character/string to look for.
1207 * @return location of the next occurance of the needle, or
1208 * strlen($body) if needle wasn't found.
1209 */
1210 function sq_findnxstr($body, $offset, $needle){
1211 $me = "sq_findnxstr";
1212 $pos = strpos($body, $needle, $offset);
1213 if ($pos === FALSE){
1214 $pos = strlen($body);
1215 }
1216 return $pos;
1217 }
1218
1219 /**
1220 * This function takes a PCRE-style regexp and tries to match it
1221 * within the string.
1222 *
1223 * @param $body The string to look for needle in.
1224 * @param $offset Start looking from here.
1225 * @param $reg A PCRE-style regex to match.
1226 * @return Returns a false if no matches found, or an array
1227 * with the following members:
1228 * - integer with the location of the match within $body
1229 * - string with whatever content between offset and the match
1230 * - string with whatever it is we matched
1231 */
1232 function sq_findnxreg($body, $offset, $reg){
1233 $me = "sq_findnxreg";
1234 $matches = Array();
1235 $retarr = Array();
1236 preg_match("%^(.*?)($reg)%s", substr($body, $offset), $matches);
1237 if (!$matches{0}){
1238 $retarr = false;
1239 } else {
1240 $retarr{0} = $offset + strlen($matches{1});
1241 $retarr{1} = $matches{1};
1242 $retarr{2} = $matches{2};
1243 }
1244 return $retarr;
1245 }
1246
1247 /**
1248 * This function looks for the next tag.
1249 *
1250 * @param $body String where to look for the next tag.
1251 * @param $offset Start looking from here.
1252 * @return false if no more tags exist in the body, or
1253 * an array with the following members:
1254 * - string with the name of the tag
1255 * - array with attributes and their values
1256 * - integer with tag type (1, 2, or 3)
1257 * - integer where the tag starts (starting "<")
1258 * - integer where the tag ends (ending ">")
1259 * first three members will be false, if the tag is invalid.
1260 */
1261 function sq_getnxtag($body, $offset){
1262 $me = "sq_getnxtag";
1263 if ($offset > strlen($body)){
1264 return false;
1265 }
1266 $lt = sq_findnxstr($body, $offset, "<");
1267 if ($lt == strlen($body)){
1268 return false;
1269 }
1270 /**
1271 * We are here:
1272 * blah blah <tag attribute="value">
1273 * \---------^
1274 */
1275 $pos = sq_skipspace($body, $lt+1);
1276 if ($pos >= strlen($body)){
1277 return Array(false, false, false, $lt, strlen($body));
1278 }
1279 /**
1280 * There are 3 kinds of tags:
1281 * 1. Opening tag, e.g.:
1282 * <a href="blah">
1283 * 2. Closing tag, e.g.:
1284 * </a>
1285 * 3. XHTML-style content-less tag, e.g.:
1286 * <img src="blah"/>
1287 */
1288 $tagtype = false;
1289 switch (substr($body, $pos, 1)){
1290 case "/":
1291 $tagtype = 2;
1292 $pos++;
1293 break;
1294 case "!":
1295 /**
1296 * A comment or an SGML declaration.
1297 */
1298 if (substr($body, $pos+1, 2) == "--"){
1299 $gt = strpos($body, "-->", $pos)+2;
1300 if ($gt === false){
1301 $gt = strlen($body);
1302 }
1303 return Array(false, false, false, $lt, $gt);
1304 } else {
1305 $gt = sq_findnxstr($body, $pos, ">");
1306 return Array(false, false, false, $lt, $gt);
1307 }
1308 break;
1309 default:
1310 /**
1311 * Assume tagtype 1 for now. If it's type 3, we'll switch values
1312 * later.
1313 */
1314 $tagtype = 1;
1315 break;
1316 }
1317
1318 $tag_start = $pos;
1319 $tagname = '';
1320 /**
1321 * Look for next [\W-_], which will indicate the end of the tag name.
1322 */
1323 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1324 if ($regary == false){
1325 return Array(false, false, false, $lt, strlen($body));
1326 }
1327 list($pos, $tagname, $match) = $regary;
1328 $tagname = strtolower($tagname);
1329
1330 /**
1331 * $match can be either of these:
1332 * '>' indicating the end of the tag entirely.
1333 * '\s' indicating the end of the tag name.
1334 * '/' indicating that this is type-3 xhtml tag.
1335 *
1336 * Whatever else we find there indicates an invalid tag.
1337 */
1338 switch ($match){
1339 case "/":
1340 /**
1341 * This is an xhtml-style tag with a closing / at the
1342 * end, like so: <img src="blah"/>. Check if it's followed
1343 * by the closing bracket. If not, then this tag is invalid
1344 */
1345 if (substr($body, $pos, 2) == "/>"){
1346 $pos++;
1347 $tagtype = 3;
1348 } else {
1349 $gt = sq_findnxstr($body, $pos, ">");
1350 $retary = Array(false, false, false, $lt, $gt);
1351 return $retary;
1352 }
1353 case ">":
1354 return Array($tagname, false, $tagtype, $lt, $pos);
1355 break;
1356 default:
1357 /**
1358 * Check if it's whitespace
1359 */
1360 if (preg_match("/\s/", $match)){
1361 } else {
1362 /**
1363 * This is an invalid tag! Look for the next closing ">".
1364 */
1365 $gt = sq_findnxstr($body, $offset, ">");
1366 return Array(false, false, false, $lt, $gt);
1367 }
1368 }
1369
1370 /**
1371 * At this point we're here:
1372 * <tagname attribute='blah'>
1373 * \-------^
1374 *
1375 * At this point we loop in order to find all attributes.
1376 */
1377 $attname = '';
1378 $atttype = false;
1379 $attary = Array();
1380
1381 while ($pos <= strlen($body)){
1382 $pos = sq_skipspace($body, $pos);
1383 if ($pos == strlen($body)){
1384 /**
1385 * Non-closed tag.
1386 */
1387 return Array(false, false, false, $lt, $pos);
1388 }
1389 /**
1390 * See if we arrived at a ">" or "/>", which means that we reached
1391 * the end of the tag.
1392 */
1393 $matches = Array();
1394 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
1395 /**
1396 * Yep. So we did.
1397 */
1398 $pos += strlen($matches{1});
1399 if ($matches{2} == "/>"){
1400 $tagtype = 3;
1401 $pos++;
1402 }
1403 return Array($tagname, $attary, $tagtype, $lt, $pos);
1404 }
1405
1406 /**
1407 * There are several types of attributes, with optional
1408 * [:space:] between members.
1409 * Type 1:
1410 * attrname[:space:]=[:space:]'CDATA'
1411 * Type 2:
1412 * attrname[:space:]=[:space:]"CDATA"
1413 * Type 3:
1414 * attr[:space:]=[:space:]CDATA
1415 * Type 4:
1416 * attrname
1417 *
1418 * We leave types 1 and 2 the same, type 3 we check for
1419 * '"' and convert to "&quot" if needed, then wrap in
1420 * double quotes. Type 4 we convert into:
1421 * attrname="yes".
1422 */
1423 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1424 if ($regary == false){
1425 /**
1426 * Looks like body ended before the end of tag.
1427 */
1428 return Array(false, false, false, $lt, strlen($body));
1429 }
1430 list($pos, $attname, $match) = $regary;
1431 $attname = strtolower($attname);
1432 /**
1433 * We arrived at the end of attribute name. Several things possible
1434 * here:
1435 * '>' means the end of the tag and this is attribute type 4
1436 * '/' if followed by '>' means the same thing as above
1437 * '\s' means a lot of things -- look what it's followed by.
1438 * anything else means the attribute is invalid.
1439 */
1440 switch($match){
1441 case "/":
1442 /**
1443 * This is an xhtml-style tag with a closing / at the
1444 * end, like so: <img src="blah"/>. Check if it's followed
1445 * by the closing bracket. If not, then this tag is invalid
1446 */
1447 if (substr($body, $pos, 2) == "/>"){
1448 $pos++;
1449 $tagtype = 3;
1450 } else {
1451 $gt = sq_findnxstr($body, $pos, ">");
1452 $retary = Array(false, false, false, $lt, $gt);
1453 return $retary;
1454 }
1455 case ">":
1456 $attary{$attname} = '"yes"';
1457 return Array($tagname, $attary, $tagtype, $lt, $pos);
1458 break;
1459 default:
1460 /**
1461 * Skip whitespace and see what we arrive at.
1462 */
1463 $pos = sq_skipspace($body, $pos);
1464 $char = substr($body, $pos, 1);
1465 /**
1466 * Two things are valid here:
1467 * '=' means this is attribute type 1 2 or 3.
1468 * \w means this was attribute type 4.
1469 * anything else we ignore and re-loop. End of tag and
1470 * invalid stuff will be caught by our checks at the beginning
1471 * of the loop.
1472 */
1473 if ($char == "="){
1474 $pos++;
1475 $pos = sq_skipspace($body, $pos);
1476 /**
1477 * Here are 3 possibilities:
1478 * "'" attribute type 1
1479 * '"' attribute type 2
1480 * everything else is the content of tag type 3
1481 */
1482 $quot = substr($body, $pos, 1);
1483 if ($quot == "'"){
1484 $regary = sq_findnxreg($body, $pos+1, "\'");
1485 if ($regary == false){
1486 return Array(false, false, false, $lt, strlen($body));
1487 }
1488 list($pos, $attval, $match) = $regary;
1489 $pos++;
1490 $attary{$attname} = "'" . $attval . "'";
1491 } else if ($quot == '"'){
1492 $regary = sq_findnxreg($body, $pos+1, '\"');
1493 if ($regary == false){
1494 return Array(false, false, false, $lt, strlen($body));
1495 }
1496 list($pos, $attval, $match) = $regary;
1497 $pos++;
1498 $attary{$attname} = '"' . $attval . '"';
1499 } else {
1500 /**
1501 * These are hateful. Look for \s, or >.
1502 */
1503 $regary = sq_findnxreg($body, $pos, "[\s>]");
1504 if ($regary == false){
1505 return Array(false, false, false, $lt, strlen($body));
1506 }
1507 list($pos, $attval, $match) = $regary;
1508 /**
1509 * If it's ">" it will be caught at the top.
1510 */
1511 $attval = preg_replace("/\"/s", "&quot;", $attval);
1512 $attary{$attname} = '"' . $attval . '"';
1513 }
1514 } else if (preg_match("|[\w/>]|", $char)) {
1515 /**
1516 * That was attribute type 4.
1517 */
1518 $attary{$attname} = '"yes"';
1519 } else {
1520 /**
1521 * An illegal character. Find next '>' and return.
1522 */
1523 $gt = sq_findnxstr($body, $pos, ">");
1524 return Array(false, false, false, $lt, $gt);
1525 }
1526 }
1527 }
1528 /**
1529 * The fact that we got here indicates that the tag end was never
1530 * found. Return invalid tag indication so it gets stripped.
1531 */
1532 return Array(false, false, false, $lt, strlen($body));
1533 }
1534
1535 /**
1536 * This function checks attribute values for entity-encoded values
1537 * and returns them translated into 8-bit strings so we can run
1538 * checks on them.
1539 *
1540 * @param $attvalue A string to run entity check against.
1541 * @return Translated value.
1542 */
1543 function sq_deent($attvalue){
1544 $me="sq_deent";
1545 /**
1546 * See if we have to run the checks first. All entities must start
1547 * with "&".
1548 */
1549 if (strpos($attvalue, "&") === false){
1550 return $attvalue;
1551 }
1552 /**
1553 * Check named entities first.
1554 */
1555 $trans = get_html_translation_table(HTML_ENTITIES);
1556 /**
1557 * Leave &quot; in, as it can mess us up.
1558 */
1559 $trans = array_flip($trans);
1560 unset($trans{"&quot;"});
1561 while (list($ent, $val) = each($trans)){
1562 $attvalue = preg_replace("/$ent*(\W)/si", "$val\\1", $attvalue);
1563 }
1564 /**
1565 * Now translate numbered entities from 1 to 255 if needed.
1566 */
1567 if (strpos($attvalue, "#") !== false){
1568 $omit = Array(34, 39);
1569 for ($asc=1; $asc<256; $asc++){
1570 if (!in_array($asc, $omit)){
1571 $chr = chr($asc);
1572 $attvalue = preg_replace("/\&#0*$asc;*(\D)/si", "$chr\\1",
1573 $attvalue);
1574 $attvalue = preg_replace("/\&#x0*".dechex($asc).";*(\W)/si",
1575 "$chr\\1", $attvalue);
1576 }
1577 }
1578 }
1579 return $attvalue;
1580 }
1581
1582 /**
1583 * This function runs various checks against the attributes.
1584 *
1585 * @param $tagname String with the name of the tag.
1586 * @param $attary Array with all tag attributes.
1587 * @param $rm_attnames See description for sq_sanitize
1588 * @param $bad_attvals See description for sq_sanitize
1589 * @param $add_attr_to_tag See description for sq_sanitize
1590 * @param $message message object
1591 * @param $id message id
1592 * @return Array with modified attributes.
1593 */
1594 function sq_fixatts($tagname,
1595 $attary,
1596 $rm_attnames,
1597 $bad_attvals,
1598 $add_attr_to_tag,
1599 $message,
1600 $id
1601 ){
1602 $me = "sq_fixatts";
1603 while (list($attname, $attvalue) = each($attary)){
1604 /**
1605 * See if this attribute should be removed.
1606 */
1607 foreach ($rm_attnames as $matchtag=>$matchattrs){
1608 if (preg_match($matchtag, $tagname)){
1609 foreach ($matchattrs as $matchattr){
1610 if (preg_match($matchattr, $attname)){
1611 unset($attary{$attname});
1612 continue;
1613 }
1614 }
1615 }
1616 }
1617 /**
1618 * Remove any entities.
1619 */
1620 $attvalue = sq_deent($attvalue);
1621
1622 /**
1623 * Now let's run checks on the attvalues.
1624 * I don't expect anyone to comprehend this. If you do,
1625 * get in touch with me so I can drive to where you live and
1626 * shake your hand personally. :)
1627 */
1628 foreach ($bad_attvals as $matchtag=>$matchattrs){
1629 if (preg_match($matchtag, $tagname)){
1630 foreach ($matchattrs as $matchattr=>$valary){
1631 if (preg_match($matchattr, $attname)){
1632 /**
1633 * There are two arrays in valary.
1634 * First is matches.
1635 * Second one is replacements
1636 */
1637 list($valmatch, $valrepl) = $valary;
1638 $newvalue =
1639 preg_replace($valmatch, $valrepl, $attvalue);
1640 if ($newvalue != $attvalue){
1641 $attary{$attname} = $newvalue;
1642 }
1643 }
1644 }
1645 }
1646 }
1647 /**
1648 * Turn cid: urls into http-friendly ones.
1649 */
1650 if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){
1651 $attary{$attname} = sq_cid2http($message, $id, $attvalue);
1652 }
1653 }
1654 /**
1655 * See if we need to append any attributes to this tag.
1656 */
1657 foreach ($add_attr_to_tag as $matchtag=>$addattary){
1658 if (preg_match($matchtag, $tagname)){
1659 $attary = array_merge($attary, $addattary);
1660 }
1661 }
1662 return $attary;
1663 }
1664
1665 /**
1666 * This function edits the style definition to make them friendly and
1667 * usable in squirrelmail.
1668 *
1669 * @param $message the message object
1670 * @param $id the message id
1671 * @param $content a string with whatever is between <style> and </style>
1672 * @return a string with edited content.
1673 */
1674 function sq_fixstyle($message, $id, $content){
1675 global $view_unsafe_images;
1676 $me = "sq_fixstyle";
1677 /**
1678 * First look for general BODY style declaration, which would be
1679 * like so:
1680 * body {background: blah-blah}
1681 * and change it to .bodyclass so we can just assign it to a <div>
1682 */
1683 $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
1684 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1685 /**
1686 * Fix url('blah') declarations.
1687 */
1688 $content = preg_replace("|url\(([\'\"])\s*\S+script\s*:.*?([\'\"])\)|si",
1689 "url(\\1$secremoveimg\\2)", $content);
1690 /**
1691 * Fix url('https*://.*) declarations but only if $view_unsafe_images
1692 * is false.
1693 */
1694 if (!$view_unsafe_images){
1695 $content = preg_replace("|url\(([\'\"])\s*https*:.*?([\'\"])\)|si",
1696 "url(\\1$secremoveimg\\2)", $content);
1697 }
1698
1699 /**
1700 * Fix urls that refer to cid:
1701 */
1702 while (preg_match("|url\(([\'\"]\s*cid:.*?[\'\"])\)|si", $content,
1703 $matches)){
1704 $cidurl = $matches{1};
1705 $httpurl = sq_cid2http($message, $id, $cidurl);
1706 $content = preg_replace("|url\($cidurl\)|si",
1707 "url($httpurl)", $content);
1708 }
1709
1710 /**
1711 * Fix stupid expression: declarations which lead to vulnerabilities
1712 * in IE.
1713 */
1714 $content = preg_replace("/expression\s*:/si", "idiocy:", $content);
1715 return $content;
1716 }
1717
1718 /**
1719 * This function converts cid: url's into the ones that can be viewed in
1720 * the browser.
1721 *
1722 * @param $message the message object
1723 * @param $id the message id
1724 * @param $cidurl the cid: url.
1725 * @return a string with a http-friendly url
1726 */
1727 function sq_cid2http($message, $id, $cidurl){
1728 /**
1729 * Get rid of quotes.
1730 */
1731 $quotchar = substr($cidurl, 0, 1);
1732 $cidurl = str_replace($quotchar, "", $cidurl);
1733 $cidurl = substr(trim($cidurl), 4);
1734 $httpurl = $quotchar . "../src/download.php?absolute_dl=true&amp;" .
1735 "passed_id=$id&amp;mailbox=" . urlencode($message->header->mailbox) .
1736 "&amp;passed_ent_id=" . find_ent_id($cidurl, $message) . $quotchar;
1737 return $httpurl;
1738 }
1739
1740 /**
1741 * This function changes the <body> tag into a <div> tag since we
1742 * can't really have a body-within-body.
1743 *
1744 * @param $attary an array of attributes and values of <body>
1745 * @return a modified array of attributes to be set for <div>
1746 */
1747 function sq_body2div($attary){
1748 $me = "sq_body2div";
1749 $divattary = Array("class"=>"'bodyclass'");
1750 $bgcolor="#ffffff";
1751 $text="#000000";
1752 $styledef="";
1753 if (is_array($attary) && sizeof($attary) > 0){
1754 foreach ($attary as $attname=>$attvalue){
1755 $quotchar = substr($attvalue, 0, 1);
1756 $attvalue = str_replace($quotchar, "", $attvalue);
1757 switch ($attname){
1758 case "background":
1759 $styledef .= "background-image: url('$attvalue'); ";
1760 break;
1761 case "bgcolor":
1762 $styledef .= "background-color: $attvalue; ";
1763 break;
1764 case "text":
1765 $styledef .= "color: $attvalue; ";
1766 }
1767 }
1768 if (strlen($styledef) > 0){
1769 $divattary{"style"} = "\"$styledef\"";
1770 }
1771 }
1772 return $divattary;
1773 }
1774
1775 /**
1776 * This is the main function and the one you should actually be calling.
1777 * There are several variables you should be aware of an which need
1778 * special description.
1779 *
1780 * Since the description is quite lengthy, see it here:
1781 * http://www.mricon.com/html/phpfilter.html
1782 *
1783 * @param $body the string with HTML you wish to filter
1784 * @param $tag_list see description above
1785 * @param $rm_tags_with_content see description above
1786 * @param $self_closing_tags see description above
1787 * @param $force_tag_closing see description above
1788 * @param $rm_attnames see description above
1789 * @param $bad_attvals see description above
1790 * @param $add_attr_to_tag see description above
1791 * @param $message message object
1792 * @param $id message id
1793 * @return sanitized html safe to show on your pages.
1794 */
1795 function sq_sanitize($body,
1796 $tag_list,
1797 $rm_tags_with_content,
1798 $self_closing_tags,
1799 $force_tag_closing,
1800 $rm_attnames,
1801 $bad_attvals,
1802 $add_attr_to_tag,
1803 $message,
1804 $id
1805 ){
1806 $me = "sq_sanitize";
1807 /**
1808 * Normalize rm_tags and rm_tags_with_content.
1809 */
1810 @array_walk($rm_tags, 'sq_casenormalize');
1811 @array_walk($rm_tags_with_content, 'sq_casenormalize');
1812 @array_walk($self_closing_tags, 'sq_casenormalize');
1813 /**
1814 * See if tag_list is of tags to remove or tags to allow.
1815 * false means remove these tags
1816 * true means allow these tags
1817 */
1818 $rm_tags = array_shift($tag_list);
1819 $curpos = 0;
1820 $open_tags = Array();
1821 $trusted = "<!-- begin sanitized html -->\n";
1822 $skip_content = false;
1823
1824 while (($curtag=sq_getnxtag($body, $curpos)) != FALSE){
1825 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
1826 $free_content = substr($body, $curpos, $lt-$curpos);
1827 /**
1828 * Take care of <style>
1829 */
1830 if ($tagname == "style" && $tagtype == 2){
1831 /**
1832 * This is a closing </style>. Edit the
1833 * content before we apply it.
1834 */
1835 $free_content = sq_fixstyle($message, $id, $free_content);
1836 }
1837 if ($skip_content == false){
1838 $trusted .= $free_content;
1839 } else {
1840 }
1841 if ($tagname != FALSE){
1842 if ($tagtype == 2){
1843 if ($skip_content == $tagname){
1844 /**
1845 * Got to the end of tag we needed to remove.
1846 */
1847 $tagname = false;
1848 $skip_content = false;
1849 } else {
1850 if ($skip_content == false){
1851 if ($tagname == "body"){
1852 $tagname = "div";
1853 } else {
1854 if (isset($open_tags{$tagname}) &&
1855 $open_tags{$tagname} > 0){
1856 $open_tags{$tagname}--;
1857 } else {
1858 $tagname = false;
1859 }
1860 }
1861 } else {
1862 }
1863 }
1864 } else {
1865 /**
1866 * $rm_tags_with_content
1867 */
1868 if ($skip_content == false){
1869 /**
1870 * See if this is a self-closing type and change
1871 * tagtype appropriately.
1872 */
1873 if ($tagtype == 1
1874 && in_array($tagname, $self_closing_tags)){
1875 $tagtype=3;
1876 }
1877 /**
1878 * See if we should skip this tag and any content
1879 * inside it.
1880 */
1881 if ($tagtype == 1 &&
1882 in_array($tagname, $rm_tags_with_content)){
1883 $skip_content = $tagname;
1884 } else {
1885 if (($rm_tags == false
1886 && in_array($tagname, $tag_list)) ||
1887 ($rm_tags == true &&
1888 !in_array($tagname, $tag_list))){
1889 $tagname = false;
1890 } else {
1891 if ($tagtype == 1){
1892 if (isset($open_tags{$tagname})){
1893 $open_tags{$tagname}++;
1894 } else {
1895 $open_tags{$tagname}=1;
1896 }
1897 }
1898 /**
1899 * This is where we run other checks.
1900 */
1901 if (is_array($attary) && sizeof($attary) > 0){
1902 $attary = sq_fixatts($tagname,
1903 $attary,
1904 $rm_attnames,
1905 $bad_attvals,
1906 $add_attr_to_tag,
1907 $message,
1908 $id
1909 );
1910 }
1911 /**
1912 * Convert body into div.
1913 */
1914 if ($tagname == "body"){
1915 $tagname = "div";
1916 $attary = sq_body2div($attary, $message, $id);
1917 }
1918 }
1919 }
1920 } else {
1921 }
1922 }
1923 if ($tagname != false && $skip_content == false){
1924 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
1925 }
1926 } else {
1927 }
1928 $curpos = $gt+1;
1929 }
1930 $trusted .= substr($body, $curpos, strlen($body)-$curpos);
1931 if ($force_tag_closing == true){
1932 foreach ($open_tags as $tagname=>$opentimes){
1933 while ($opentimes > 0){
1934 $trusted .= '</' . $tagname . '>';
1935 $opentimes--;
1936 }
1937 }
1938 $trusted .= "\n";
1939 }
1940 $trusted .= "<!-- end sanitized html -->\n";
1941 return $trusted;
1942 }
1943
1944 /**
1945 * This is a wrapper function to call html sanitizing routines.
1946 *
1947 * @param $body the body of the message
1948 * @param $id the id of the message
1949 * @return a string with html safe to display in the browser.
1950 */
1951 function magicHTML($body, $id, $message){
1952 global $attachment_common_show_images, $view_unsafe_images,
1953 $has_unsafe_images;
1954 /**
1955 * Don't display attached images in HTML mode.
1956 */
1957 $attachment_common_show_images = false;
1958 $tag_list = Array(
1959 false,
1960 "object",
1961 "meta",
1962 "html",
1963 "head",
1964 "base"
1965 );
1966
1967 $rm_tags_with_content = Array(
1968 "script",
1969 "applet",
1970 "embed",
1971 "title"
1972 );
1973
1974 $self_closing_tags = Array(
1975 "img",
1976 "br",
1977 "hr",
1978 "input"
1979 );
1980
1981 $force_tag_closing = false;
1982
1983 $rm_attnames = Array(
1984 "/.*/" =>
1985 Array(
1986 "/target/si",
1987 "/^on.*/si"
1988 )
1989 );
1990
1991 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1992 $bad_attvals = Array(
1993 "/.*/" =>
1994 Array(
1995 "/^src|background|href|action/i" =>
1996 Array(
1997 Array(
1998 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1999 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si"
2000 ),
2001 Array(
2002 "\\1$secremoveimg\\2",
2003 "\\1$secremoveimg\\2"
2004 )
2005 ),
2006 "/^style/si" =>
2007 Array(
2008 Array(
2009 "/expression\s*:/si",
2010 "|url\(([\'\"])\s*\.\./.*([\'\"])\)|si",
2011 "/url\(([\'\"])\s*\S+script:.*([\'\"])\)/si"
2012 ),
2013 Array(
2014 "idiocy:",
2015 "url(\\1$secremoveimg\\2)",
2016 "url(\\1$secremoveimg\\2)"
2017 )
2018 )
2019 )
2020 );
2021 if (!$view_unsafe_images){
2022 /**
2023 * Remove any references to http/https if view_unsafe_images set
2024 * to false.
2025 */
2026 $addendum = Array(
2027 "/.*/" =>
2028 Array(
2029 "/^src|background/i" =>
2030 Array(
2031 Array(
2032 "/^([\'\"])\s*https*:.*([\'\"])/si"
2033 ),
2034 Array(
2035 "\\1$secremoveimg\\2"
2036 )
2037 ),
2038 "/^style/si" =>
2039 Array(
2040 Array(
2041 "/url\(([\'\"])\s*https*:.*([\'\"])\)/si"
2042 ),
2043 Array(
2044 "url(\\1$secremoveimg\\2)"
2045 )
2046 )
2047 )
2048 );
2049 $bad_attvals = array_merge($bad_attvals, $addendum);
2050 }
2051
2052 $add_attr_to_tag = Array(
2053 "/^a$/si" => Array('target'=>'"_new"')
2054 );
2055 $trusted = sq_sanitize($body,
2056 $tag_list,
2057 $rm_tags_with_content,
2058 $self_closing_tags,
2059 $force_tag_closing,
2060 $rm_attnames,
2061 $bad_attvals,
2062 $add_attr_to_tag,
2063 $message,
2064 $id
2065 );
2066 if (preg_match("|$secremoveimg|si", $trusted)){
2067 $has_unsafe_images = true;
2068 }
2069 return $trusted;
2070 }
2071 ?>