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