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