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