bbf3953c2d0298791d902629235738eb14afefd2
[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 }
935 $Links['download link']['text'] = _("download");
936 $Links['download link']['href'] =
937 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
938 $ImageURL = '';
939
940 /* this executes the attachment hook with a specific MIME-type.
941 * if that doens't have results, it tries if there's a rule
942 * for a more generic type. */
943 $HookResults = do_hook("attachment $type0/$type1", $Links,
944 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
945 $display_filename, $where, $what);
946 if(count($HookResults[1]) <= 1) {
947 $HookResults = do_hook("attachment $type0/*", $Links,
948 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
949 $display_filename, $where, $what);
950 }
951
952 $Links = $HookResults[1];
953 $DefaultLink = $HookResults[6];
954
955 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
956 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
957 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
958 '</b>&nbsp;&nbsp;</small></TD>' .
959 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
960 '<TD><SMALL>';
961 if ($message->header->description) {
962 $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
963 }
964 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
965
966
967 $SkipSpaces = 1;
968 foreach ($Links as $Val) {
969 if ($SkipSpaces) {
970 $SkipSpaces = 0;
971 } else {
972 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
973 }
974 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
975 }
976
977 unset($Links);
978
979 $body .= "</SMALL></TD></TR>\n";
980 }
981 } else {
982 for ($i = 0; $i < count($message->entities); $i++) {
983 $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
984 }
985 }
986 }
987 return( $body );
988 }
989
990
991 /** this function decodes the body depending on the encoding type. **/
992 function decodeBody($body, $encoding) {
993 $body = str_replace("\r\n", "\n", $body);
994 $encoding = strtolower($encoding);
995
996 global $show_html_default;
997
998 if ($encoding == 'quoted-printable') {
999 $body = quoted_printable_decode($body);
1000
1001
1002 while (ereg("=\n", $body))
1003 $body = ereg_replace ("=\n", "", $body);
1004
1005 } else if ($encoding == 'base64') {
1006 $body = base64_decode($body);
1007 }
1008
1009 // All other encodings are returned raw.
1010 return $body;
1011 }
1012
1013 /*
1014 * This functions decode strings that is encoded according to
1015 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
1016 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
1017 */
1018 function decodeHeader ($string, $utfencode=true) {
1019 if (is_array($string)) {
1020 $string = implode("\n", $string);
1021 }
1022 $i = 0;
1023 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
1024 $string, $res)) {
1025 $prefix = $res[1];
1026 // Ignore white-space between consecutive encoded-words
1027 if (strspn($res[2], " \t") != strlen($res[2])) {
1028 $prefix .= $res[2];
1029 }
1030
1031 if (ucfirst($res[4]) == 'B') {
1032 $replace = base64_decode($res[5]);
1033 } else {
1034 $replace = str_replace('_', ' ', $res[5]);
1035 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
1036 $replace);
1037 /* Only encode into entities by default. Some places
1038 don't need the encoding, like the compose form. */
1039 if ($utfencode) {
1040 $replace = charset_decode($res[3], $replace);
1041 }
1042 }
1043 $string = $prefix . $replace . substr($string, strlen($res[0]));
1044 $i = strlen($prefix) + strlen($replace);
1045 }
1046 return( $string );
1047 }
1048
1049 /*
1050 * Encode a string according to RFC 1522 for use in headers if it
1051 * contains 8-bit characters or anything that looks like it should
1052 * be encoded.
1053 */
1054 function encodeHeader ($string) {
1055 global $default_charset;
1056
1057 // Encode only if the string contains 8-bit characters or =?
1058 $j = strlen( $string );
1059 $l = strstr($string, '=?'); // Must be encoded ?
1060 $ret = '';
1061 for( $i=0; $i < $j; ++$i) {
1062 switch( $string{$i} ) {
1063 case '=':
1064 $ret .= '=3D';
1065 break;
1066 case '?':
1067 $ret .= '=3F';
1068 break;
1069 case '_':
1070 $ret .= '=5F';
1071 break;
1072 case ' ':
1073 $ret .= '_';
1074 break;
1075 default:
1076 $k = ord( $string{$i} );
1077 if ( $k > 126 ) {
1078 $ret .= sprintf("=%02X", $k);
1079 $l = TRUE;
1080 } else
1081 $ret .= $string{$i};
1082 }
1083 }
1084
1085 if ( $l ) {
1086 $string = "=?$default_charset?Q?$ret?=";
1087 }
1088
1089 return( $string );
1090 }
1091
1092 /* This function trys to locate the entity_id of a specific mime element */
1093
1094 function find_ent_id( $id, $message ) {
1095 $ret = '';
1096 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1097 if (( $message->entities[$i]->header->type1 == 'alternative') ||
1098 ( $message->entities[$i]->header->type1 == 'related') ||
1099 ( $message->entities[$i]->header->type1 == 'mixed')) {
1100 $ret = find_ent_id( $id, $message->entities[$i] );
1101 } else {
1102 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1103 $ret = $message->entities[$i]->header->entity_id;
1104 }
1105
1106 }
1107 return( $ret );
1108 }
1109
1110 /**
1111 ** HTMLFILTER ROUTINES
1112 */
1113
1114 /**
1115 * This function returns the final tag out of the tag name, an array
1116 * of attributes, and the type of the tag. This function is called by
1117 * sq_sanitize internally.
1118 *
1119 * @param $tagname the name of the tag.
1120 * @param $attary the array of attributes and their values
1121 * @param $tagtype The type of the tag (see in comments).
1122 * @return a string with the final tag representation.
1123 */
1124 function sq_tagprint($tagname, $attary, $tagtype){
1125 $me = "sq_tagprint";
1126 if ($tagtype == 2){
1127 $fulltag = '</' . $tagname . '>';
1128 } else {
1129 $fulltag = '<' . $tagname;
1130 if (is_array($attary) && sizeof($attary)){
1131 $atts = Array();
1132 while (list($attname, $attvalue) = each($attary)){
1133 array_push($atts, "$attname=$attvalue");
1134 }
1135 $fulltag .= ' ' . join(" ", $atts);
1136 }
1137 if ($tagtype == 3){
1138 $fulltag .= " /";
1139 }
1140 $fulltag .= ">";
1141 }
1142 return $fulltag;
1143 }
1144
1145 /**
1146 * A small helper function to use with array_walk. Modifies a by-ref
1147 * value and makes it lowercase.
1148 *
1149 * @param $val a value passed by-ref.
1150 * @return void since it modifies a by-ref value.
1151 */
1152 function sq_casenormalize(&$val){
1153 $val = strtolower($val);
1154 }
1155
1156 /**
1157 * This function skips any whitespace from the current position within
1158 * a string and to the next non-whitespace value.
1159 *
1160 * @param $body the string
1161 * @param $offset the offset within the string where we should start
1162 * looking for the next non-whitespace character.
1163 * @return the location within the $body where the next
1164 * non-whitespace char is located.
1165 */
1166 function sq_skipspace($body, $offset){
1167 $me = "sq_skipspace";
1168 preg_match("/^(\s*)/s", substr($body, $offset), $matches);
1169 if (sizeof($matches{1})){
1170 $count = strlen($matches{1});
1171 $offset += $count;
1172 //if ($pos >= strlen($body)){
1173 //}
1174 }
1175 return $offset;
1176 }
1177
1178 /**
1179 * This function looks for the next character within a string. It's
1180 * really just a glorified "strpos", except it catches if failures
1181 * nicely.
1182 *
1183 * @param $body The string to look for needle in.
1184 * @param $offset Start looking from this position.
1185 * @param $needle The character/string to look for.
1186 * @return location of the next occurance of the needle, or
1187 * strlen($body) if needle wasn't found.
1188 */
1189 function sq_findnxstr($body, $offset, $needle){
1190 $me = "sq_findnxstr";
1191 $pos = strpos($body, $needle, $offset);
1192 if ($pos === FALSE){
1193 $pos = strlen($body);
1194 }
1195 return $pos;
1196 }
1197
1198 /**
1199 * This function takes a PCRE-style regexp and tries to match it
1200 * within the string.
1201 *
1202 * @param $body The string to look for needle in.
1203 * @param $offset Start looking from here.
1204 * @param $reg A PCRE-style regex to match.
1205 * @return Returns a false if no matches found, or an array
1206 * with the following members:
1207 * - integer with the location of the match within $body
1208 * - string with whatever content between offset and the match
1209 * - string with whatever it is we matched
1210 */
1211 function sq_findnxreg($body, $offset, $reg){
1212 $me = "sq_findnxreg";
1213 $matches = Array();
1214 $retarr = Array();
1215 preg_match("%^(.*?)($reg)%s", substr($body, $offset), $matches);
1216 if (!$matches{0}){
1217 $retarr = false;
1218 } else {
1219 $retarr{0} = $offset + strlen($matches{1});
1220 $retarr{1} = $matches{1};
1221 $retarr{2} = $matches{2};
1222 }
1223 return $retarr;
1224 }
1225
1226 /**
1227 * This function looks for the next tag.
1228 *
1229 * @param $body String where to look for the next tag.
1230 * @param $offset Start looking from here.
1231 * @return false if no more tags exist in the body, or
1232 * an array with the following members:
1233 * - string with the name of the tag
1234 * - array with attributes and their values
1235 * - integer with tag type (1, 2, or 3)
1236 * - integer where the tag starts (starting "<")
1237 * - integer where the tag ends (ending ">")
1238 * first three members will be false, if the tag is invalid.
1239 */
1240 function sq_getnxtag($body, $offset){
1241 $me = "sq_getnxtag";
1242 if ($offset > strlen($body)){
1243 return false;
1244 }
1245 $lt = sq_findnxstr($body, $offset, "<");
1246 if ($lt == strlen($body)){
1247 return false;
1248 }
1249 /**
1250 * We are here:
1251 * blah blah <tag attribute="value">
1252 * \---------^
1253 */
1254 $pos = sq_skipspace($body, $lt+1);
1255 if ($pos >= strlen($body)){
1256 return Array(false, false, false, $lt, strlen($body));
1257 }
1258 /**
1259 * There are 3 kinds of tags:
1260 * 1. Opening tag, e.g.:
1261 * <a href="blah">
1262 * 2. Closing tag, e.g.:
1263 * </a>
1264 * 3. XHTML-style content-less tag, e.g.:
1265 * <img src="blah"/>
1266 */
1267 $tagtype = false;
1268 switch (substr($body, $pos, 1)){
1269 case "/":
1270 $tagtype = 2;
1271 $pos++;
1272 break;
1273 case "!":
1274 /**
1275 * A comment or an SGML declaration.
1276 */
1277 if (substr($body, $pos+1, 2) == "--"){
1278 $gt = strpos($body, "-->", $pos)+2;
1279 if ($gt === false){
1280 $gt = strlen($body);
1281 }
1282 return Array(false, false, false, $lt, $gt);
1283 } else {
1284 $gt = sq_findnxstr($body, $pos, ">");
1285 return Array(false, false, false, $lt, $gt);
1286 }
1287 break;
1288 default:
1289 /**
1290 * Assume tagtype 1 for now. If it's type 3, we'll switch values
1291 * later.
1292 */
1293 $tagtype = 1;
1294 break;
1295 }
1296
1297 $tag_start = $pos;
1298 $tagname = '';
1299 /**
1300 * Look for next [\W-_], which will indicate the end of the tag name.
1301 */
1302 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1303 if ($regary == false){
1304 return Array(false, false, false, $lt, strlen($body));
1305 }
1306 list($pos, $tagname, $match) = $regary;
1307 $tagname = strtolower($tagname);
1308
1309 /**
1310 * $match can be either of these:
1311 * '>' indicating the end of the tag entirely.
1312 * '\s' indicating the end of the tag name.
1313 * '/' indicating that this is type-3 xhtml tag.
1314 *
1315 * Whatever else we find there indicates an invalid tag.
1316 */
1317 switch ($match){
1318 case "/":
1319 /**
1320 * This is an xhtml-style tag with a closing / at the
1321 * end, like so: <img src="blah"/>. Check if it's followed
1322 * by the closing bracket. If not, then this tag is invalid
1323 */
1324 if (substr($body, $pos, 2) == "/>"){
1325 $pos++;
1326 $tagtype = 3;
1327 } else {
1328 $gt = sq_findnxstr($body, $pos, ">");
1329 $retary = Array(false, false, false, $lt, $gt);
1330 return $retary;
1331 }
1332 case ">":
1333 return Array($tagname, false, $tagtype, $lt, $pos);
1334 break;
1335 default:
1336 /**
1337 * Check if it's whitespace
1338 */
1339 if (preg_match("/\s/", $match)){
1340 } else {
1341 /**
1342 * This is an invalid tag! Look for the next closing ">".
1343 */
1344 $gt = sq_findnxstr($body, $offset, ">");
1345 return Array(false, false, false, $lt, $gt);
1346 }
1347 }
1348
1349 /**
1350 * At this point we're here:
1351 * <tagname attribute='blah'>
1352 * \-------^
1353 *
1354 * At this point we loop in order to find all attributes.
1355 */
1356 $attname = '';
1357 $atttype = false;
1358 $attary = Array();
1359
1360 while ($pos <= strlen($body)){
1361 $pos = sq_skipspace($body, $pos);
1362 if ($pos == strlen($body)){
1363 /**
1364 * Non-closed tag.
1365 */
1366 return Array(false, false, false, $lt, $pos);
1367 }
1368 /**
1369 * See if we arrived at a ">" or "/>", which means that we reached
1370 * the end of the tag.
1371 */
1372 $matches = Array();
1373 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
1374 if ($matches{0}){
1375 /**
1376 * Yep. So we did.
1377 */
1378 $pos += strlen($matches{1});
1379 if ($matches{2} == "/>"){
1380 $tagtype = 3;
1381 $pos++;
1382 }
1383 return Array($tagname, $attary, $tagtype, $lt, $pos);
1384 }
1385 }
1386
1387 /**
1388 * There are several types of attributes, with optional
1389 * [:space:] between members.
1390 * Type 1:
1391 * attrname[:space:]=[:space:]'CDATA'
1392 * Type 2:
1393 * attrname[:space:]=[:space:]"CDATA"
1394 * Type 3:
1395 * attr[:space:]=[:space:]CDATA
1396 * Type 4:
1397 * attrname
1398 *
1399 * We leave types 1 and 2 the same, type 3 we check for
1400 * '"' and convert to "&quot" if needed, then wrap in
1401 * double quotes. Type 4 we convert into:
1402 * attrname="yes".
1403 */
1404 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1405 if ($regary == false){
1406 /**
1407 * Looks like body ended before the end of tag.
1408 */
1409 return Array(false, false, false, $lt, strlen($body));
1410 }
1411 list($pos, $attname, $match) = $regary;
1412 $attname = strtolower($attname);
1413 /**
1414 * We arrived at the end of attribute name. Several things possible
1415 * here:
1416 * '>' means the end of the tag and this is attribute type 4
1417 * '/' if followed by '>' means the same thing as above
1418 * '\s' means a lot of things -- look what it's followed by.
1419 * anything else means the attribute is invalid.
1420 */
1421 switch($match){
1422 case "/":
1423 /**
1424 * This is an xhtml-style tag with a closing / at the
1425 * end, like so: <img src="blah"/>. Check if it's followed
1426 * by the closing bracket. If not, then this tag is invalid
1427 */
1428 if (substr($body, $pos, 2) == "/>"){
1429 $pos++;
1430 $tagtype = 3;
1431 } else {
1432 $gt = getnxstr($body, $pos, ">");
1433 $retary = Array(false, false, false, $lt, $gt);
1434 return $retary;
1435 }
1436 case ">":
1437 $attary{$attname} = '"yes"';
1438 return Array($tagname, $attary, $tagtype, $lt, $pos);
1439 break;
1440 default:
1441 /**
1442 * Skip whitespace and see what we arrive at.
1443 */
1444 $pos = sq_skipspace($body, $pos);
1445 $char = substr($body, $pos, 1);
1446 /**
1447 * Two things are valid here:
1448 * '=' means this is attribute type 1 2 or 3.
1449 * \w means this was attribute type 4.
1450 * anything else we ignore and re-loop. End of tag and
1451 * invalid stuff will be caught by our checks at the beginning
1452 * of the loop.
1453 */
1454 if ($char == "="){
1455 $pos++;
1456 $pos = sq_skipspace($body, $pos);
1457 /**
1458 * Here are 3 possibilities:
1459 * "'" attribute type 1
1460 * '"' attribute type 2
1461 * everything else is the content of tag type 3
1462 */
1463 $quot = substr($body, $pos, 1);
1464 if ($quot == "'"){
1465 $regary = sq_findnxreg($body, $pos+1, "\'");
1466 if ($regary == false){
1467 return Array(false, false, false, $lt, strlen($body));
1468 }
1469 list($pos, $attval, $match) = $regary;
1470 $pos++;
1471 $attary{$attname} = "'" . $attval . "'";
1472 } else if ($quot == '"'){
1473 $regary = sq_findnxreg($body, $pos+1, '\"');
1474 if ($regary == false){
1475 return Array(false, false, false, $lt, strlen($body));
1476 }
1477 list($pos, $attval, $match) = $regary;
1478 $pos++;
1479 $attary{$attname} = '"' . $attval . '"';
1480 } else {
1481 /**
1482 * These are hateful. Look for \s, or >.
1483 */
1484 $regary = sq_findnxreg($body, $pos, "[\s>]");
1485 if ($regary == false){
1486 return Array(false, false, false, $lt, strlen($body));
1487 }
1488 list($pos, $attval, $match) = $regary;
1489 /**
1490 * If it's ">" it will be caught at the top.
1491 */
1492 $attval = preg_replace("/\"/s", "&quot;", $attval);
1493 $attary{$attname} = '"' . $attval . '"';
1494 }
1495 } else if (preg_match("|[\w/>]|", $char)) {
1496 /**
1497 * That was attribute type 4.
1498 */
1499 $attary{$attname} = '"yes"';
1500 } else {
1501 /**
1502 * An illegal character. Find next '>' and return.
1503 */
1504 $gt = sq_findnxstr($body, $pos, ">");
1505 return Array(false, false, false, $lt, $gt);
1506 }
1507 }
1508 }
1509 /**
1510 * The fact that we got here indicates that the tag end was never
1511 * found. Return invalid tag indication so it gets stripped.
1512 */
1513 return Array(false, false, false, $lt, strlen($body));
1514 }
1515
1516 /**
1517 * This function checks attribute values for entity-encoded values
1518 * and returns them translated into 8-bit strings so we can run
1519 * checks on them.
1520 *
1521 * @param $attvalue A string to run entity check against.
1522 * @return Translated value.
1523 */
1524 function sq_deent($attvalue){
1525 $me="sq_deent";
1526 /**
1527 * See if we have to run the checks first. All entities must start
1528 * with "&".
1529 */
1530 if (strpos($attvalue, "&") === false){
1531 return $attvalue;
1532 }
1533 /**
1534 * Check named entities first.
1535 */
1536 $trans = get_html_translation_table(HTML_ENTITIES);
1537 /**
1538 * Leave &quot; in, as it can mess us up.
1539 */
1540 $trans = array_flip($trans);
1541 unset($trans{"&quot;"});
1542 while (list($ent, $val) = each($trans)){
1543 $attvalue = preg_replace("/$ent*(\W)/si", "$val\\1", $attvalue);
1544 }
1545 /**
1546 * Now translate numbered entities from 1 to 255 if needed.
1547 */
1548 if (strpos($attvalue, "#") !== false){
1549 $omit = Array(34, 39);
1550 for ($asc=1; $asc<256; $asc++){
1551 if (!in_array($asc, $omit)){
1552 $chr = chr($asc);
1553 $attvalue = preg_replace("/\&#0*$asc;*(\D)/si", "$chr\\1",
1554 $attvalue);
1555 $attvalue = preg_replace("/\&#x0*".dechex($asc).";*(\W)/si",
1556 "$chr\\1", $attvalue);
1557 }
1558 }
1559 }
1560 return $attvalue;
1561 }
1562
1563 /**
1564 * This function runs various checks against the attributes.
1565 *
1566 * @param $tagname String with the name of the tag.
1567 * @param $attary Array with all tag attributes.
1568 * @param $rm_attnames See description for sq_sanitize
1569 * @param $bad_attvals See description for sq_sanitize
1570 * @param $add_attr_to_tag See description for sq_sanitize
1571 * @param $message message object
1572 * @param $id message id
1573 * @return Array with modified attributes.
1574 */
1575 function sq_fixatts($tagname,
1576 $attary,
1577 $rm_attnames,
1578 $bad_attvals,
1579 $add_attr_to_tag,
1580 $message,
1581 $id
1582 ){
1583 $me = "sq_fixatts";
1584 while (list($attname, $attvalue) = each($attary)){
1585 /**
1586 * See if this attribute should be removed.
1587 */
1588 foreach ($rm_attnames as $matchtag=>$matchattrs){
1589 if (preg_match($matchtag, $tagname)){
1590 foreach ($matchattrs as $matchattr){
1591 if (preg_match($matchattr, $attname)){
1592 unset($attary{$attname});
1593 continue;
1594 }
1595 }
1596 }
1597 }
1598 /**
1599 * Remove any entities.
1600 */
1601 $attvalue = sq_deent($attvalue);
1602
1603 /**
1604 * Now let's run checks on the attvalues.
1605 * I don't expect anyone to comprehend this. If you do,
1606 * get in touch with me so I can drive to where you live and
1607 * shake your hand personally. :)
1608 */
1609 foreach ($bad_attvals as $matchtag=>$matchattrs){
1610 if (preg_match($matchtag, $tagname)){
1611 foreach ($matchattrs as $matchattr=>$valary){
1612 if (preg_match($matchattr, $attname)){
1613 /**
1614 * There are two arrays in valary.
1615 * First is matches.
1616 * Second one is replacements
1617 */
1618 list($valmatch, $valrepl) = $valary;
1619 $newvalue =
1620 preg_replace($valmatch, $valrepl, $attvalue);
1621 if ($newvalue != $attvalue){
1622 $attary{$attname} = $newvalue;
1623 }
1624 }
1625 }
1626 }
1627 }
1628 /**
1629 * Turn cid: urls into http-friendly ones.
1630 */
1631 if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){
1632 $attary{$attname} = sq_cid2http($message, $id, $attvalue);
1633 }
1634 }
1635 /**
1636 * See if we need to append any attributes to this tag.
1637 */
1638 foreach ($add_attr_to_tag as $matchtag=>$addattary){
1639 if (preg_match($matchtag, $tagname)){
1640 $attary = array_merge($attary, $addattary);
1641 }
1642 }
1643 return $attary;
1644 }
1645
1646 /**
1647 * This function edits the style definition to make them friendly and
1648 * usable in squirrelmail.
1649 *
1650 * @param $message the message object
1651 * @param $id the message id
1652 * @param $content a string with whatever is between <style> and </style>
1653 * @return a string with edited content.
1654 */
1655 function sq_fixstyle($message, $id, $content){
1656 global $view_unsafe_images;
1657 $me = "sq_fixstyle";
1658 /**
1659 * First look for general BODY style declaration, which would be
1660 * like so:
1661 * body {background: blah-blah}
1662 * and change it to .bodyclass so we can just assign it to a <div>
1663 */
1664 $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
1665 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1666 /**
1667 * Fix url('blah') declarations.
1668 */
1669 $content = preg_replace("|url\(([\'\"])\s*\S+script\s*:.*?([\'\"])\)|si",
1670 "url(\\1$secremoveimg\\2)", $content);
1671 /**
1672 * Fix url('https*://.*) declarations but only if $view_unsafe_images
1673 * is false.
1674 */
1675 if (!$view_unsafe_images){
1676 $content = preg_replace("|url\(([\'\"])\s*https*:.*?([\'\"])\)|si",
1677 "url(\\1$secremoveimg\\2)", $content);
1678 }
1679
1680 /**
1681 * Fix urls that refer to cid:
1682 */
1683 while (preg_match("|url\(([\'\"]\s*cid:.*?[\'\"])\)|si", $content,
1684 $matches)){
1685 $cidurl = $matches{1};
1686 $httpurl = sq_cid2http($message, $id, $cidurl);
1687 $content = preg_replace("|url\($cidurl\)|si",
1688 "url($httpurl)", $content);
1689 }
1690
1691 /**
1692 * Fix stupid expression: declarations which lead to vulnerabilities
1693 * in IE.
1694 */
1695 $content = preg_replace("/expression\s*:/si", "idiocy:", $content);
1696 return $content;
1697 }
1698
1699 /**
1700 * This function converts cid: url's into the ones that can be viewed in
1701 * the browser.
1702 *
1703 * @param $message the message object
1704 * @param $id the message id
1705 * @param $cidurl the cid: url.
1706 * @return a string with a http-friendly url
1707 */
1708 function sq_cid2http($message, $id, $cidurl){
1709 /**
1710 * Get rid of quotes.
1711 */
1712 $quotchar = substr($cidurl, 0, 1);
1713 $cidurl = str_replace($quotchar, "", $cidurl);
1714 $cidurl = substr(trim($cidurl), 4);
1715 $httpurl = $quotchar . "../src/download.php?absolute_dl=true&amp;" .
1716 "passed_id=$id&amp;mailbox=" . urlencode($message->header->mailbox) .
1717 "&amp;passed_ent_id=" . find_ent_id($cidurl, $message) . $quotchar;
1718 return $httpurl;
1719 }
1720
1721 /**
1722 * This function changes the <body> tag into a <div> tag since we
1723 * can't really have a body-within-body.
1724 *
1725 * @param $attary an array of attributes and values of <body>
1726 * @return a modified array of attributes to be set for <div>
1727 */
1728 function sq_body2div($attary){
1729 $me = "sq_body2div";
1730 $divattary = Array("class"=>"'bodyclass'");
1731 $bgcolor="#ffffff";
1732 $text="#000000";
1733 $styledef="";
1734 if (is_array($attary) && sizeof($attary) > 0){
1735 foreach ($attary as $attname=>$attvalue){
1736 $quotchar = substr($attvalue, 0, 1);
1737 $attvalue = str_replace($quotchar, "", $attvalue);
1738 switch ($attname){
1739 case "background":
1740 $styledef .= "background-image: url('$attvalue'); ";
1741 break;
1742 case "bgcolor":
1743 $styledef .= "background-color: $attvalue; ";
1744 break;
1745 case "text":
1746 $styledef .= "color: $attvalue; ";
1747 }
1748 }
1749 if (strlen($styledef) > 0){
1750 $divattary{"style"} = "\"$styledef\"";
1751 }
1752 }
1753 return $divattary;
1754 }
1755
1756 /**
1757 * This is the main function and the one you should actually be calling.
1758 * There are several variables you should be aware of an which need
1759 * special description.
1760 *
1761 * Since the description is quite lengthy, see it here:
1762 * http://www.mricon.com/html/phpfilter.html
1763 *
1764 * @param $body the string with HTML you wish to filter
1765 * @param $tag_list see description above
1766 * @param $rm_tags_with_content see description above
1767 * @param $self_closing_tags see description above
1768 * @param $force_tag_closing see description above
1769 * @param $rm_attnames see description above
1770 * @param $bad_attvals see description above
1771 * @param $add_attr_to_tag see description above
1772 * @param $message message object
1773 * @param $id message id
1774 * @return sanitized html safe to show on your pages.
1775 */
1776 function sq_sanitize($body,
1777 $tag_list,
1778 $rm_tags_with_content,
1779 $self_closing_tags,
1780 $force_tag_closing,
1781 $rm_attnames,
1782 $bad_attvals,
1783 $add_attr_to_tag,
1784 $message,
1785 $id
1786 ){
1787 $me = "sq_sanitize";
1788 /**
1789 * Normalize rm_tags and rm_tags_with_content.
1790 */
1791 @array_walk($rm_tags, 'sq_casenormalize');
1792 @array_walk($rm_tags_with_content, 'sq_casenormalize');
1793 @array_walk($self_closing_tags, 'sq_casenormalize');
1794 /**
1795 * See if tag_list is of tags to remove or tags to allow.
1796 * false means remove these tags
1797 * true means allow these tags
1798 */
1799 $rm_tags = array_shift($tag_list);
1800 $curpos = 0;
1801 $open_tags = Array();
1802 $trusted = "<!-- begin sanitized html -->\n";
1803 $skip_content = false;
1804
1805 while (($curtag=sq_getnxtag($body, $curpos)) != FALSE){
1806 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
1807 $free_content = substr($body, $curpos, $lt-$curpos);
1808 /**
1809 * Take care of <style>
1810 */
1811 if ($tagname == "style" && $tagtype == 2){
1812 /**
1813 * This is a closing </style>. Edit the
1814 * content before we apply it.
1815 */
1816 $free_content = sq_fixstyle($message, $id, $free_content);
1817 } else if ($tagname == "body"){
1818 $tagname = "div";
1819 if ($tagtype == 1){
1820 $attary = sq_body2div($attary);
1821 }
1822 }
1823 if ($skip_content == false){
1824 $trusted .= $free_content;
1825 } else {
1826 }
1827 if ($tagname != FALSE){
1828 if ($tagtype == 2){
1829 if ($skip_content == $tagname){
1830 /**
1831 * Got to the end of tag we needed to remove.
1832 */
1833 $tagname = false;
1834 $skip_content = false;
1835 } else {
1836 if ($skip_content == false){
1837 if (isset($open_tags{$tagname}) &&
1838 $open_tags{$tagname} > 0){
1839 $open_tags{$tagname}--;
1840 } else {
1841 $tagname = false;
1842 }
1843 } else {
1844 }
1845 }
1846 } else {
1847 /**
1848 * $rm_tags_with_content
1849 */
1850 if ($skip_content == false){
1851 /**
1852 * See if this is a self-closing type and change
1853 * tagtype appropriately.
1854 */
1855 if ($tagtype == 1
1856 && in_array($tagname, $self_closing_tags)){
1857 $tagtype=3;
1858 }
1859 /**
1860 * See if we should skip this tag and any content
1861 * inside it.
1862 */
1863 if ($tagtype == 1 &&
1864 in_array($tagname, $rm_tags_with_content)){
1865 $skip_content = $tagname;
1866 } else {
1867 if (($rm_tags == false
1868 && in_array($tagname, $tag_list)) ||
1869 ($rm_tags == true &&
1870 !in_array($tagname, $tag_list))){
1871 $tagname = false;
1872 } else {
1873 if ($tagtype == 1){
1874 if (isset($open_tags{$tagname})){
1875 $open_tags{$tagname}++;
1876 } else {
1877 $open_tags{$tagname}=1;
1878 }
1879 }
1880 /**
1881 * This is where we run other checks.
1882 */
1883 if (is_array($attary) && sizeof($attary) > 0){
1884 $attary = sq_fixatts($tagname,
1885 $attary,
1886 $rm_attnames,
1887 $bad_attvals,
1888 $add_attr_to_tag,
1889 $message,
1890 $id
1891 );
1892 }
1893 }
1894 }
1895 } else {
1896 }
1897 }
1898 if ($tagname != false && $skip_content == false){
1899 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
1900 }
1901 } else {
1902 }
1903 $curpos = $gt+1;
1904 }
1905 $trusted .= substr($body, $curpos, strlen($body)-$curpos);
1906 if ($force_tag_closing == true){
1907 foreach ($open_tags as $tagname=>$opentimes){
1908 while ($opentimes > 0){
1909 $trusted .= '</' . $tagname . '>';
1910 $opentimes--;
1911 }
1912 }
1913 $trusted .= "\n";
1914 }
1915 $trusted .= "<!-- end sanitized html -->\n";
1916 return $trusted;
1917 }
1918
1919 /**
1920 * This is a wrapper function to call html sanitizing routines.
1921 *
1922 * @param $body the body of the message
1923 * @param $id the id of the message
1924 * @return a string with html safe to display in the browser.
1925 */
1926 function magicHTML($body, $id){
1927 global $attachment_common_show_images, $view_unsafe_images,
1928 $has_unsafe_images, $message;
1929 /**
1930 * Don't display attached images in HTML mode.
1931 */
1932 $attachment_common_show_images = false;
1933 $tag_list = Array(
1934 false,
1935 "object",
1936 "meta",
1937 "html",
1938 "head",
1939 "base"
1940 );
1941
1942 $rm_tags_with_content = Array(
1943 "script",
1944 "applet",
1945 "embed",
1946 "title"
1947 );
1948
1949 $self_closing_tags = Array(
1950 "img",
1951 "br",
1952 "hr",
1953 "input"
1954 );
1955
1956 $force_tag_closing = false;
1957
1958 $rm_attnames = Array(
1959 "/.*/" =>
1960 Array(
1961 "/target/si",
1962 "/^on.*/si"
1963 )
1964 );
1965
1966 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1967 $bad_attvals = Array(
1968 "/.*/" =>
1969 Array(
1970 "/^src|background|href|action/i" =>
1971 Array(
1972 Array(
1973 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1974 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si"
1975 ),
1976 Array(
1977 "\\1$secremoveimg\\2",
1978 "\\1$secremoveimg\\2"
1979 )
1980 ),
1981 "/^style/si" =>
1982 Array(
1983 Array(
1984 "/expression\s*:/si",
1985 "|url\(([\'\"])\s*\.\./.*([\'\"])\)|si",
1986 "/url\(([\'\"])\s*\S+script:.*([\'\"])\)/si"
1987 ),
1988 Array(
1989 "idiocy:",
1990 "url(\\1$secremoveimg\\2)",
1991 "url(\\1$secremoveimg\\2)"
1992 )
1993 )
1994 )
1995 );
1996 if (!$view_unsafe_images){
1997 /**
1998 * Remove any references to http/https if view_unsafe_images set
1999 * to false.
2000 */
2001 $addendum = Array(
2002 "/.*/" =>
2003 Array(
2004 "/^src|background/i" =>
2005 Array(
2006 Array(
2007 "/^([\'\"])\s*https*:.*([\'\"])/si"
2008 ),
2009 Array(
2010 "\\1$secremoveimg\\2"
2011 )
2012 ),
2013 "/^style/si" =>
2014 Array(
2015 Array(
2016 "/url\(([\'\"])\s*https*:.*([\'\"])\)/si"
2017 ),
2018 Array(
2019 "url(\\1$secremoveimg\\2)"
2020 )
2021 )
2022 )
2023 );
2024 $bad_attvals = array_merge($bad_attvals, $addendum);
2025 }
2026
2027 $add_attr_to_tag = Array(
2028 "/^a$/si" => Array('target'=>'"_new"')
2029 );
2030 $trusted = sq_sanitize($body,
2031 $tag_list,
2032 $rm_tags_with_content,
2033 $self_closing_tags,
2034 $force_tag_closing,
2035 $rm_attnames,
2036 $bad_attvals,
2037 $add_attr_to_tag,
2038 $message,
2039 $id
2040 );
2041 if (preg_match("|$secremoveimg|si", $trusted)){
2042 $has_unsafe_images = true;
2043 }
2044 return $trusted;
2045 }
2046 ?>