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