59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
02474e43 |
4 | * mime.php |
5 | * |
6 | * Copyright (c) 1999-2005 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 | * @version $Id$ |
13 | * @package squirrelmail |
14 | */ |
b74ba498 |
15 | |
d6c32258 |
16 | /** The typical includes... */ |
b68edc75 |
17 | require_once(SM_PATH . 'functions/imap.php'); |
18 | require_once(SM_PATH . 'functions/attachment_common.php'); |
2c25d36a |
19 | /** add sqm_baseuri()*/ |
20 | include_once(SM_PATH . 'functions/display_messages.php'); |
8beafbbc |
21 | |
7c7b74b3 |
22 | /* -------------------------------------------------------------------------- */ |
23 | /* MIME DECODING */ |
24 | /* -------------------------------------------------------------------------- */ |
b74ba498 |
25 | |
d6c32258 |
26 | /** |
02474e43 |
27 | * Get the MIME structure |
28 | * |
29 | * This function gets the structure of a message and stores it in the "message" class. |
30 | * It will return this object for use with all relevant header information and |
31 | * fully parsed into the standard "message" object format. |
32 | */ |
a4a70693 |
33 | function mime_structure ($bodystructure, $flags=array()) { |
c9d78ab4 |
34 | |
3d8371be |
35 | /* Isolate the body structure and remove beginning and end parenthesis. */ |
a4a70693 |
36 | $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13)); |
451f74a2 |
37 | $read = trim(substr ($read, 0, -1)); |
22efa9fb |
38 | $i = 0; |
39 | $msg = Message::parseStructure($read,$i); |
9de42168 |
40 | if (!is_object($msg)) { |
bd9c880b |
41 | include_once(SM_PATH . 'functions/display_messages.php'); |
3d8371be |
42 | global $color, $mailbox; |
c96c32f4 |
43 | /* removed urldecode because $_GET is auto urldecoded ??? */ |
a48eba8f |
44 | displayPageHeader( $color, $mailbox ); |
5e8de8b6 |
45 | $errormessage = _("SquirrelMail could not decode the bodystructure of the message"); |
3c621ba1 |
46 | $errormessage .= '<br />'._("the provided bodystructure by your imap-server").':<br /><br />'; |
472e7acb |
47 | $errormessage .= '<pre>' . htmlspecialchars($read) . '</pre>'; |
9de42168 |
48 | plain_error_message( $errormessage, $color ); |
3d8371be |
49 | echo '</body></html>'; |
9de42168 |
50 | exit; |
51 | } |
a4a70693 |
52 | if (count($flags)) { |
7a9e9c89 |
53 | foreach ($flags as $flag) { |
54 | $char = strtoupper($flag{1}); |
55 | switch ($char) { |
3d8371be |
56 | case 'S': |
57 | if (strtolower($flag) == '\\seen') { |
58 | $msg->is_seen = true; |
59 | } |
60 | break; |
61 | case 'A': |
62 | if (strtolower($flag) == '\\answered') { |
63 | $msg->is_answered = true; |
64 | } |
65 | break; |
66 | case 'D': |
67 | if (strtolower($flag) == '\\deleted') { |
68 | $msg->is_deleted = true; |
69 | } |
70 | break; |
71 | case 'F': |
72 | if (strtolower($flag) == '\\flagged') { |
73 | $msg->is_flagged = true; |
74 | } |
75 | break; |
76 | case 'M': |
77 | if (strtolower($flag) == '$mdnsent') { |
78 | $msg->is_mdnsent = true; |
79 | } |
80 | break; |
81 | default: |
82 | break; |
7a9e9c89 |
83 | } |
84 | } |
451f74a2 |
85 | } |
7a9e9c89 |
86 | // listEntities($msg); |
3d8371be |
87 | return $msg; |
451f74a2 |
88 | } |
b74ba498 |
89 | |
22efa9fb |
90 | |
91 | |
3d8371be |
92 | /* This starts the parsing of a particular structure. It is called recursively, |
02474e43 |
93 | * so it can be passed different structures. It returns an object of type |
94 | * $message. |
95 | * First, it checks to see if it is a multipart message. If it is, then it |
96 | * handles that as it sees is necessary. If it is just a regular entity, |
97 | * then it parses it and adds the necessary header information (by calling out |
98 | * to mime_get_elements() |
99 | */ |
451f74a2 |
100 | |
4d592352 |
101 | function mime_fetch_body($imap_stream, $id, $ent_id=1, $fetch_size=0) { |
3d8371be |
102 | /* Do a bit of error correction. If we couldn't find the entity id, just guess |
02474e43 |
103 | * that it is the first one. That is usually the case anyway. |
104 | */ |
7c7b74b3 |
105 | |
09a4bde3 |
106 | if (!$ent_id) { |
08b7f7cc |
107 | $cmd = "FETCH $id BODY[]"; |
1035e159 |
108 | } else { |
08b7f7cc |
109 | $cmd = "FETCH $id BODY[$ent_id]"; |
09a4bde3 |
110 | } |
3d8371be |
111 | |
4d592352 |
112 | if ($fetch_size!=0) $cmd .= "<0.$fetch_size>"; |
da2415c1 |
113 | |
6201339c |
114 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, TRUE); |
77b88425 |
115 | do { |
3d8371be |
116 | $topline = trim(array_shift($data)); |
117 | } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ; |
a4a70693 |
118 | |
451f74a2 |
119 | $wholemessage = implode('', $data); |
120 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
3d8371be |
121 | $ret = substr($wholemessage, 0, $regs[1]); |
122 | /* There is some information in the content info header that could be important |
02474e43 |
123 | * in order to parse html messages. Let's get them here. |
124 | */ |
0600bdf1 |
125 | // if ($ret{0} == '<') { |
6201339c |
126 | // $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, TRUE); |
0600bdf1 |
127 | // } |
451f74a2 |
128 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
129 | $ret = $regs[1]; |
130 | } else { |
131 | global $where, $what, $mailbox, $passed_id, $startMessage; |
3d8371be |
132 | $par = 'mailbox=' . urlencode($mailbox) . '&passed_id=' . $passed_id; |
451f74a2 |
133 | if (isset($where) && isset($what)) { |
3d8371be |
134 | $par .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
a3daaaf3 |
135 | } else { |
3d8371be |
136 | $par .= '&startMessage=' . $startMessage . '&show_more=0'; |
451f74a2 |
137 | } |
e5ea9327 |
138 | $par .= '&response=' . urlencode($response) . |
3d8371be |
139 | '&message=' . urlencode($message) . |
140 | '&topline=' . urlencode($topline); |
a019eeb8 |
141 | |
3c621ba1 |
142 | echo '<tt><br />' . |
02474e43 |
143 | '<table width="80%"><tr>' . |
144 | '<tr><td colspan="2">' . |
145 | _("Body retrieval error. The reason for this is most probably that the message is malformed.") . |
146 | '</td></tr>' . |
147 | '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
148 | '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
149 | '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
150 | '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
151 | "</table><br /></tt></font><hr />"; |
346817d4 |
152 | |
6201339c |
153 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, TRUE); |
451f74a2 |
154 | array_shift($data); |
155 | $wholemessage = implode('', $data); |
a019eeb8 |
156 | |
346817d4 |
157 | $ret = $wholemessage; |
a3daaaf3 |
158 | } |
3d8371be |
159 | return $ret; |
451f74a2 |
160 | } |
d4467150 |
161 | |
1035e159 |
162 | function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) { |
1035e159 |
163 | |
3d8371be |
164 | /* Don't kill the connection if the browser is over a dialup |
02474e43 |
165 | * and it would take over 30 seconds to download it. |
166 | * Don't call set_time_limit in safe mode. |
167 | */ |
b7206e1d |
168 | |
3d8371be |
169 | if (!ini_get('safe_mode')) { |
b7206e1d |
170 | set_time_limit(0); |
171 | } |
7c7b74b3 |
172 | /* in case of base64 encoded attachments, do not buffer them. |
02474e43 |
173 | Instead, echo the decoded attachment directly to screen */ |
7c7b74b3 |
174 | if (strtolower($encoding) == 'base64') { |
175 | if (!$ent_id) { |
02474e43 |
176 | $query = "FETCH $id BODY[]"; |
7c7b74b3 |
177 | } else { |
02474e43 |
178 | $query = "FETCH $id BODY[$ent_id]"; |
7c7b74b3 |
179 | } |
6201339c |
180 | sqimap_run_command($imap_stream,$query,true,$response,$message,TRUE,'sqimap_base64_decode','php://stdout',true); |
1d142b8d |
181 | } else { |
02474e43 |
182 | $body = mime_fetch_body ($imap_stream, $id, $ent_id); |
183 | echo decodeBody($body, $encoding); |
1d142b8d |
184 | } |
346817d4 |
185 | |
da2415c1 |
186 | /* |
02474e43 |
187 | TODO, use the same method for quoted printable. |
188 | However, I assume that quoted printable attachments aren't that large |
189 | so the performancegain / memory usage drop will be minimal. |
190 | If we decide to add that then we need to adapt sqimap_fread because |
191 | we need to split te result on \n and fread doesn't stop at \n. That |
192 | means we also should provide $results from sqimap_fread (by ref) to |
193 | te function and set $no_return to false. The $filter function for |
194 | quoted printable should handle unsetting of $results. |
7c7b74b3 |
195 | */ |
da2415c1 |
196 | /* |
02474e43 |
197 | TODO 2: find out how we write to the output stream php://stdout. fwrite |
198 | doesn't work because 'php://stdout isn't a stream. |
7c7b74b3 |
199 | */ |
200 | |
5d9c6f73 |
201 | return; |
202 | /* |
451f74a2 |
203 | fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n"); |
204 | $cnt = 0; |
205 | $continue = true; |
1d142b8d |
206 | $read = fgets ($imap_stream,8192); |
207 | |
208 | |
451f74a2 |
209 | // This could be bad -- if the section has sqimap_session_id() . ' OK' |
210 | // or similar, it will kill the download. |
1d142b8d |
211 | while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) { |
5d9c6f73 |
212 | if (trim($read) == ')==') { |
213 | $read1 = $read; |
214 | $read = fgets ($imap_stream,4096); |
215 | if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
216 | return; |
217 | } else { |
218 | echo decodeBody($read1, $encoding) . |
02474e43 |
219 | decodeBody($read, $encoding); |
5d9c6f73 |
220 | } |
221 | } else if ($cnt) { |
222 | echo decodeBody($read, $encoding); |
223 | } |
224 | $read = fgets ($imap_stream,4096); |
225 | $cnt++; |
1d142b8d |
226 | // break; |
451f74a2 |
227 | } |
5d9c6f73 |
228 | */ |
451f74a2 |
229 | } |
beb9e459 |
230 | |
451f74a2 |
231 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
d4467150 |
232 | |
3d8371be |
233 | /* This is here for debugging purposes. It will print out a list |
02474e43 |
234 | * of all the entity IDs that are in the $message object. |
235 | */ |
451f74a2 |
236 | function listEntities ($message) { |
3d8371be |
237 | if ($message) { |
3c621ba1 |
238 | echo "<tt>" . $message->entity_id . ' : ' . $message->type0 . '/' . $message->type1 . ' parent = '. $message->parent->entity_id. '<br />'; |
3d8371be |
239 | for ($i = 0; isset($message->entities[$i]); $i++) { |
240 | echo "$i : "; |
241 | $msg = listEntities($message->entities[$i]); |
242 | |
243 | if ($msg) { |
244 | echo "return: "; |
245 | return $msg; |
246 | } |
247 | } |
a4a70693 |
248 | } |
451f74a2 |
249 | } |
f0c4dc12 |
250 | |
f792c641 |
251 | function getPriorityStr($priority) { |
3d8371be |
252 | $priority_level = substr($priority,0,1); |
253 | |
254 | switch($priority_level) { |
255 | /* Check for a higher then normal priority. */ |
256 | case '1': |
257 | case '2': |
258 | $priority_string = _("High"); |
259 | break; |
260 | |
261 | /* Check for a lower then normal priority. */ |
262 | case '4': |
263 | case '5': |
264 | $priority_string = _("Low"); |
265 | break; |
266 | |
267 | /* Check for a normal priority. */ |
268 | case '3': |
269 | default: |
270 | $priority_level = '3'; |
271 | $priority_string = _("Normal"); |
272 | break; |
273 | |
274 | } |
275 | return $priority_string; |
f792c641 |
276 | } |
277 | |
451f74a2 |
278 | /* returns a $message object for a particular entity id */ |
279 | function getEntity ($message, $ent_id) { |
a4a70693 |
280 | return $message->getEntity($ent_id); |
451f74a2 |
281 | } |
8beafbbc |
282 | |
3d8371be |
283 | /* translateText |
02474e43 |
284 | * Extracted from strings.php 23/03/2002 |
285 | */ |
da4c66e8 |
286 | |
287 | function translateText(&$body, $wrap_at, $charset) { |
3d8371be |
288 | global $where, $what; /* from searching */ |
289 | global $color; /* color theme */ |
da4c66e8 |
290 | |
b68edc75 |
291 | require_once(SM_PATH . 'functions/url_parser.php'); |
da4c66e8 |
292 | |
293 | $body_ary = explode("\n", $body); |
da4c66e8 |
294 | for ($i=0; $i < count($body_ary); $i++) { |
295 | $line = $body_ary[$i]; |
296 | if (strlen($line) - 2 >= $wrap_at) { |
c7aff938 |
297 | sqWordWrap($line, $wrap_at, $charset); |
da4c66e8 |
298 | } |
299 | $line = charset_decode($charset, $line); |
300 | $line = str_replace("\t", ' ', $line); |
301 | |
302 | parseUrl ($line); |
303 | |
3d8371be |
304 | $quotes = 0; |
da4c66e8 |
305 | $pos = 0; |
3d8371be |
306 | $j = strlen($line); |
da4c66e8 |
307 | |
3d8371be |
308 | while ($pos < $j) { |
da4c66e8 |
309 | if ($line[$pos] == ' ') { |
3d8371be |
310 | $pos++; |
da4c66e8 |
311 | } else if (strpos($line, '>', $pos) === $pos) { |
312 | $pos += 4; |
3d8371be |
313 | $quotes++; |
da4c66e8 |
314 | } else { |
315 | break; |
316 | } |
317 | } |
3d8371be |
318 | |
83c94382 |
319 | if ($quotes % 2) { |
3d8371be |
320 | if (!isset($color[13])) { |
da4c66e8 |
321 | $color[13] = '#800000'; |
322 | } |
4c25967c |
323 | $line = '<font color="' . $color[13] . '">' . $line . '</font>'; |
324 | } elseif ($quotes) { |
325 | if (!isset($color[14])) { |
326 | $color[14] = '#FF0000'; |
327 | } |
328 | $line = '<font color="' . $color[14] . '">' . $line . '</font>'; |
da4c66e8 |
329 | } |
3d8371be |
330 | |
da4c66e8 |
331 | $body_ary[$i] = $line; |
332 | } |
333 | $body = '<pre>' . implode("\n", $body_ary) . '</pre>'; |
334 | } |
335 | |
a2bfcbce |
336 | /** |
da1b55ad |
337 | * This returns a parsed string called $body. That string can then |
338 | * be displayed as the actual message in the HTML. It contains |
339 | * everything needed, including HTML Tags, Attachments at the |
340 | * bottom, etc. |
341 | * |
342 | * Since 1.2.0 function uses message_body hook. |
343 | * Till 1.3.0 function included output of formatAttachments(). |
344 | * |
345 | * @param resource $imap_stream imap connection resource |
346 | * @param object $message squirrelmail message object |
347 | * @param array $color squirrelmail color theme array |
348 | * @param integer $wrap_at number of characters per line |
349 | * @param string $ent_num (since 1.3.0) message part id |
350 | * @param integer $id (since 1.3.0) message id |
351 | * @param string $mailbox (since 1.3.0) imap folder name |
352 | * @param boolean $clean (since 1.5.1) Do not output stuff that's irrelevant for the printable version. |
353 | * @return string html formated message text |
354 | */ |
a2bfcbce |
355 | function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX', $clean=FALSE) { |
3d8371be |
356 | /* This if statement checks for the entity to show as the |
02474e43 |
357 | * primary message. To add more of them, just put them in the |
358 | * order that is their priority. |
359 | */ |
ce68b76b |
360 | global $startMessage, $languages, $squirrelmail_language, |
2c25d36a |
361 | $show_html_default, $sort, $has_unsafe_images, $passed_ent_id, $use_iframe,$iframe_height; |
362 | |
363 | // workaround for not updated config.php |
364 | if (! isset($use_iframe)) $use_iframe = false; |
77bfbd2e |
365 | |
5262d9a6 |
366 | if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) { |
367 | $view_unsafe_images = false; |
77bfbd2e |
368 | } |
d03c24f4 |
369 | |
cc34b00d |
370 | $body = ''; |
23bcec6f |
371 | $urlmailbox = urlencode($mailbox); |
451f74a2 |
372 | $body_message = getEntity($message, $ent_num); |
373 | if (($body_message->header->type0 == 'text') || |
374 | ($body_message->header->type0 == 'rfc822')) { |
3d8371be |
375 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
451f74a2 |
376 | $body = decodeBody($body, $body_message->header->encoding); |
e842b215 |
377 | |
378 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
33a55f5a |
379 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) { |
e842b215 |
380 | if (mb_detect_encoding($body) != 'ASCII') { |
33a55f5a |
381 | $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode',$body); |
e842b215 |
382 | } |
383 | } |
451f74a2 |
384 | $hookResults = do_hook("message_body", $body); |
385 | $body = $hookResults[1]; |
23bcec6f |
386 | |
3d8371be |
387 | /* If there are other types that shouldn't be formatted, add |
02474e43 |
388 | * them here. |
389 | */ |
3d8371be |
390 | |
451f74a2 |
391 | if ($body_message->header->type1 == 'html') { |
3d8371be |
392 | if ($show_html_default <> 1) { |
85015544 |
393 | $entity_conv = array(' ' => ' ', |
02474e43 |
394 | '<p>' => "\n", |
395 | '<P>' => "\n", |
396 | '<br>' => "\n", |
397 | '<BR>' => "\n", |
398 | '<br />' => "\n", |
399 | '<BR />' => "\n", |
400 | '>' => '>', |
401 | '<' => '<'); |
85015544 |
402 | $body = strtr($body, $entity_conv); |
3d8371be |
403 | $body = strip_tags($body); |
85015544 |
404 | $body = trim($body); |
405 | translateText($body, $wrap_at, |
02474e43 |
406 | $body_message->header->getParameter('charset')); |
2c25d36a |
407 | } elseif ($use_iframe && ! $clean) { |
408 | // $clean is used to remove iframe in printable view. |
409 | |
410 | // creating iframe url |
411 | $iframeurl=sqm_baseuri().'src/view_html.php?' |
412 | . 'mailbox=' . $urlmailbox |
413 | . '&passed_id=' . $id |
414 | . '&ent_id=' . $ent_num |
415 | . '&view_unsafe_images=' . (int) $view_unsafe_images; |
416 | |
417 | // adding warning message |
418 | $body = html_tag('div',_("Viewing HTML formatted email"),'center'); |
419 | |
420 | /** |
421 | * height can't be set to 100%, because it does not work as expected when |
422 | * iframe is inside the table. Browsers do not create full height objects |
423 | * even when iframe is not nested. Maybe there is some way to get full size |
424 | * with CSS. Tested in firefox 1.02 and opera 7.53 |
425 | * |
426 | * width="100%" does not work as expected, when table width is not set (automatic) |
427 | * |
428 | * tokul: I think <iframe> are safer sandbox than <object>. Objects might |
429 | * need special handling for IE and IE6SP2. |
430 | */ |
431 | $body.= "<div><iframe name=\"message_frame\" width=\"100%\" height=\"$iframe_height\" src=\"$iframeurl\"" |
432 | .' frameborder="1" marginwidth="0" marginheight="0" scrolling="auto">' . "\n"; |
433 | |
434 | // Message for browsers without iframe support |
435 | $body.= _("Your browser does not support inline frames. You can view HTML formated message by following below link."); |
436 | $body.= "<br /><a href=\"$iframeurl\">"._("View HTML Message")."</a>"; |
437 | |
438 | // close iframe |
439 | $body.="</iframe></div>\n"; |
a3daaaf3 |
440 | } else { |
2c25d36a |
441 | // old way of html rendering |
442 | $body = magicHTML($body, $id, $message, $mailbox); |
a3daaaf3 |
443 | } |
451f74a2 |
444 | } else { |
3d8371be |
445 | translateText($body, $wrap_at, |
02474e43 |
446 | $body_message->header->getParameter('charset')); |
451f74a2 |
447 | } |
a2bfcbce |
448 | |
449 | // if this is the clean display (i.e. printer friendly), stop here. |
450 | if ( $clean ) { |
451 | return $body; |
452 | } |
453 | |
83cf04bd |
454 | $link = 'passed_id=' . $id . '&ent_id='.$ent_num. |
08b7f7cc |
455 | '&mailbox=' . $urlmailbox .'&sort=' . $sort . |
da2415c1 |
456 | '&startMessage=' . $startMessage . '&show_more=0'; |
08b7f7cc |
457 | if (isset($passed_ent_id)) { |
458 | $link .= '&passed_ent_id='.$passed_ent_id; |
459 | } |
8d863f64 |
460 | $body .= '<center><small><a href="download.php?absolute_dl=true&' . |
02474e43 |
461 | $link . '">' . _("Download this as a file") . '</a>'; |
7aad7b77 |
462 | if ($view_unsafe_images) { |
23f617b8 |
463 | $text = _("Hide Unsafe Images"); |
7aad7b77 |
464 | } else { |
08b7f7cc |
465 | if (isset($has_unsafe_images) && $has_unsafe_images) { |
466 | $link .= '&view_unsafe_images=1'; |
467 | $text = _("View Unsafe Images"); |
468 | } else { |
469 | $text = ''; |
470 | } |
3d8371be |
471 | } |
83cf04bd |
472 | if($text != '') { |
473 | $body .= ' | <a href="read_body.php?' . $link . '">' . $text . '</a>'; |
474 | } |
3c621ba1 |
475 | $body .= '</small></center><br />' . "\n"; |
3d8371be |
476 | } |
477 | return $body; |
451f74a2 |
478 | } |
b74ba498 |
479 | |
da1b55ad |
480 | /** |
481 | * Displays attachment links and information |
da1b55ad |
482 | * |
483 | * Since 1.3.0 function is not included in formatBody() call. |
484 | * |
485 | * Since 1.0.2 uses attachment $type0/$type1 hook. |
486 | * Since 1.2.5 uses attachment $type0/* hook. |
487 | * Since 1.5.0 uses attachments_bottom hook. |
488 | * |
489 | * @param object $message SquirrelMail message object |
490 | * @param array $exclude_id message parts that are not attachments. |
491 | * @param string $mailbox mailbox name |
492 | * @param integer $id message id |
493 | * @return string html formated attachment information. |
494 | */ |
23bcec6f |
495 | function formatAttachments($message, $exclude_id, $mailbox, $id) { |
80dcd298 |
496 | global $where, $what, $startMessage, $color, $passed_ent_id; |
451f74a2 |
497 | |
23bcec6f |
498 | $att_ar = $message->getAttachments($exclude_id); |
451f74a2 |
499 | |
23bcec6f |
500 | if (!count($att_ar)) return ''; |
2e25760a |
501 | |
8f5425ec |
502 | $attachments = ''; |
2e25760a |
503 | |
23bcec6f |
504 | $urlMailbox = urlencode($mailbox); |
505 | |
506 | foreach ($att_ar as $att) { |
fdc9d9b5 |
507 | $ent = $att->entity_id; |
2e25760a |
508 | $header = $att->header; |
f0c4dc12 |
509 | $type0 = strtolower($header->type0); |
510 | $type1 = strtolower($header->type1); |
2e25760a |
511 | $name = ''; |
21dab2dc |
512 | $links['download link']['text'] = _("Download"); |
2c25d36a |
513 | $links['download link']['href'] = sqm_baseuri() . |
6b04287c |
514 | "src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&ent_id=$ent"; |
2e25760a |
515 | if ($type0 =='message' && $type1 == 'rfc822') { |
2c25d36a |
516 | $default_page = sqm_baseuri() . 'src/read_body.php'; |
2e25760a |
517 | $rfc822_header = $att->rfc822_header; |
098ea084 |
518 | $filename = $rfc822_header->subject; |
6cc08d8b |
519 | if (trim( $filename ) == '') { |
520 | $filename = 'untitled-[' . $ent . ']' ; |
08b7f7cc |
521 | } |
2e25760a |
522 | $from_o = $rfc822_header->from; |
523 | if (is_object($from_o)) { |
04ea844e |
524 | $from_name = decodeHeader($from_o->getAddress(false)); |
2e25760a |
525 | } else { |
526 | $from_name = _("Unknown sender"); |
f0c4dc12 |
527 | } |
3d8371be |
528 | $description = $from_name; |
23bcec6f |
529 | } else { |
2c25d36a |
530 | $default_page = sqm_baseuri() . 'src/download.php'; |
02474e43 |
531 | $filename = $att->getFilename(); |
f810c0b2 |
532 | if ($header->description) { |
098ea084 |
533 | $description = decodeHeader($header->description); |
f810c0b2 |
534 | } else { |
3d8371be |
535 | $description = ''; |
536 | } |
2e25760a |
537 | } |
538 | |
539 | $display_filename = $filename; |
3d8371be |
540 | if (isset($passed_ent_id)) { |
541 | $passed_ent_id_link = '&passed_ent_id='.$passed_ent_id; |
542 | } else { |
543 | $passed_ent_id_link = ''; |
544 | } |
545 | $defaultlink = $default_page . "?startMessage=$startMessage" |
02474e43 |
546 | . "&passed_id=$id&mailbox=$urlMailbox" |
547 | . '&ent_id='.$ent.$passed_ent_id_link; |
2e25760a |
548 | if ($where && $what) { |
02474e43 |
549 | $defaultlink .= '&where='. urlencode($where).'&what='.urlencode($what); |
2e25760a |
550 | } |
21dab2dc |
551 | |
3d8371be |
552 | /* This executes the attachment hook with a specific MIME-type. |
02474e43 |
553 | * If that doesn't have results, it tries if there's a rule |
554 | * for a more generic type. |
555 | */ |
3d8371be |
556 | $hookresults = do_hook("attachment $type0/$type1", $links, |
02474e43 |
557 | $startMessage, $id, $urlMailbox, $ent, $defaultlink, |
558 | $display_filename, $where, $what); |
3d8371be |
559 | if(count($hookresults[1]) <= 1) { |
560 | $hookresults = do_hook("attachment $type0/*", $links, |
02474e43 |
561 | $startMessage, $id, $urlMailbox, $ent, $defaultlink, |
562 | $display_filename, $where, $what); |
2e25760a |
563 | } |
451f74a2 |
564 | |
3d8371be |
565 | $links = $hookresults[1]; |
566 | $defaultlink = $hookresults[6]; |
77b88425 |
567 | |
3c621ba1 |
568 | $attachments .= '<tr><td>' . |
569 | '<a href="'.$defaultlink.'">'.decodeHeader($display_filename).'</a> </td>' . |
570 | '<td><small><b>' . show_readable_size($header->size) . |
571 | '</b> </small></td>' . |
572 | '<td><small>[ '.htmlspecialchars($type0).'/'.htmlspecialchars($type1).' ] </small></td>' . |
573 | '<td><small>'; |
f810c0b2 |
574 | $attachments .= '<b>' . $description . '</b>'; |
3c621ba1 |
575 | $attachments .= '</small></td><td><small> '; |
b74ba498 |
576 | |
3d8371be |
577 | $skipspaces = 1; |
578 | foreach ($links as $val) { |
579 | if ($skipspaces) { |
580 | $skipspaces = 0; |
2e25760a |
581 | } else { |
582 | $attachments .= ' | '; |
77b88425 |
583 | } |
2c25d36a |
584 | $attachments .= '<a href="' . $val['href'] . '">' |
585 | . (isset($val['text']) && !empty($val['text']) ? $val['text'] : '') |
586 | . (isset($val['extra']) && !empty($val['extra']) ? $val['extra'] : '') |
587 | . '</a>'; |
2e25760a |
588 | } |
3d8371be |
589 | unset($links); |
3c621ba1 |
590 | $attachments .= "</td></tr>\n"; |
2e25760a |
591 | } |
9ad17edb |
592 | $attachmentadd = do_hook_function('attachments_bottom',$attachments); |
593 | if ($attachmentadd != '') |
594 | $attachments = $attachmentadd; |
2e25760a |
595 | return $attachments; |
451f74a2 |
596 | } |
b74ba498 |
597 | |
7c7b74b3 |
598 | function sqimap_base64_decode(&$string) { |
7c0ec1d8 |
599 | |
b17a8968 |
600 | // Base64 encoded data goes in pairs of 4 bytes. To achieve on the |
7c0ec1d8 |
601 | // fly decoding (to reduce memory usage) you have to check if the |
602 | // data has incomplete pairs |
603 | |
b17a8968 |
604 | // Remove the noise in order to check if the 4 bytes pairs are complete |
7c0ec1d8 |
605 | $string = str_replace(array("\r\n","\n", "\r", " "),array('','','',''),$string); |
606 | |
42ce44f8 |
607 | $sStringRem = ''; |
7c0ec1d8 |
608 | $iMod = strlen($string) % 4; |
609 | if ($iMod) { |
610 | $sStringRem = substr($string,-$iMod); |
b17a8968 |
611 | // Check if $sStringRem contains padding characters |
7c0ec1d8 |
612 | if (substr($sStringRem,-1) != '=') { |
613 | $string = substr($string,0,-$iMod); |
614 | } else { |
615 | $sStringRem = ''; |
616 | } |
617 | } |
7c7b74b3 |
618 | $string = base64_decode($string); |
7c0ec1d8 |
619 | return $sStringRem; |
7c7b74b3 |
620 | } |
621 | |
7c0ec1d8 |
622 | |
3d8371be |
623 | /* This function decodes the body depending on the encoding type. */ |
451f74a2 |
624 | function decodeBody($body, $encoding) { |
3d8371be |
625 | global $show_html_default; |
83be314a |
626 | |
b583c3e8 |
627 | $body = str_replace("\r\n", "\n", $body); |
628 | $encoding = strtolower($encoding); |
3d8371be |
629 | |
5166f86a |
630 | $encoding_handler = do_hook_function('decode_body', $encoding); |
631 | |
632 | |
633 | // plugins get first shot at decoding the body |
634 | // |
635 | if (!empty($encoding_handler) && function_exists($encoding_handler)) { |
636 | $body = $encoding_handler('decode', $body); |
637 | |
638 | } else if ($encoding == 'quoted-printable' || |
3d8371be |
639 | $encoding == 'quoted_printable') { |
b583c3e8 |
640 | $body = quoted_printable_decode($body); |
3d8371be |
641 | |
b583c3e8 |
642 | while (ereg("=\n", $body)) { |
643 | $body = ereg_replace ("=\n", '', $body); |
644 | } |
3d8371be |
645 | |
b583c3e8 |
646 | } else if ($encoding == 'base64') { |
647 | $body = base64_decode($body); |
648 | } |
3d8371be |
649 | |
b583c3e8 |
650 | // All other encodings are returned raw. |
3d8371be |
651 | return $body; |
451f74a2 |
652 | } |
653 | |
9f7f68c3 |
654 | /** |
02474e43 |
655 | * Decodes headers |
656 | * |
657 | * This functions decode strings that is encoded according to |
658 | * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
659 | * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002 |
660 | * |
661 | * @param string $string header string that has to be made readable |
662 | * @param boolean $utfencode change message in order to be readable on user's charset. defaults to true |
663 | * @param boolean $htmlsave preserve spaces and sanitize html special characters. defaults to true |
664 | * @param boolean $decide decide if string can be utfencoded. defaults to false |
665 | * @return string decoded header string |
666 | */ |
9f7f68c3 |
667 | function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) { |
d6f584fc |
668 | global $languages, $squirrelmail_language,$default_charset; |
79e07c7e |
669 | if (is_array($string)) { |
670 | $string = implode("\n", $string); |
671 | } |
da2415c1 |
672 | |
10dec454 |
673 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
33a55f5a |
674 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader')) { |
675 | $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader', $string); |
08b7f7cc |
676 | // Do we need to return at this point? |
677 | // return $string; |
83be314a |
678 | } |
79e07c7e |
679 | $i = 0; |
08b7f7cc |
680 | $iLastMatch = -2; |
db65b6b0 |
681 | $encoded = true; |
0a06275a |
682 | |
098ea084 |
683 | $aString = explode(' ',$string); |
08b7f7cc |
684 | $ret = ''; |
098ea084 |
685 | foreach ($aString as $chunk) { |
358a78a1 |
686 | if ($encoded && $chunk === '') { |
08b7f7cc |
687 | continue; |
358a78a1 |
688 | } elseif ($chunk === '') { |
08b7f7cc |
689 | $ret .= ' '; |
690 | continue; |
691 | } |
098ea084 |
692 | $encoded = false; |
08b7f7cc |
693 | /* if encoded words are not separated by a linear-space-white we still catch them */ |
694 | $j = $i-1; |
7e6ca3e8 |
695 | |
08b7f7cc |
696 | while ($match = preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=(.*)$/Ui',$chunk,$res)) { |
697 | /* if the last chunk isn't an encoded string then put back the space, otherwise don't */ |
698 | if ($iLastMatch !== $j) { |
699 | if ($htmlsave) { |
9f7f68c3 |
700 | $ret .= ' '; |
08b7f7cc |
701 | } else { |
702 | $ret .= ' '; |
703 | } |
704 | } |
705 | $iLastMatch = $i; |
706 | $j = $i; |
cb718de0 |
707 | if ($htmlsave) { |
708 | $ret .= htmlspecialchars($res[1]); |
709 | } else { |
710 | $ret .= $res[1]; |
711 | } |
098ea084 |
712 | $encoding = ucfirst($res[3]); |
d6f584fc |
713 | |
714 | /* decide about valid decoding */ |
715 | if ($decide && is_conversion_safe($res[2])) { |
02474e43 |
716 | $utfencode=true; |
717 | $can_be_encoded=true; |
d6f584fc |
718 | } else { |
02474e43 |
719 | $can_be_encoded=false; |
d6f584fc |
720 | } |
098ea084 |
721 | switch ($encoding) |
722 | { |
723 | case 'B': |
724 | $replace = base64_decode($res[4]); |
fab65ca9 |
725 | if ($utfencode) { |
726 | if ($can_be_encoded) { |
727 | /* convert string to different charset, |
02474e43 |
728 | * if functions asks for it (usually in compose) |
729 | */ |
fab65ca9 |
730 | $ret .= charset_convert($res[2],$replace,$default_charset); |
731 | } else { |
732 | // convert string to html codes in order to display it |
733 | $ret .= charset_decode($res[2],$replace); |
734 | } |
d6f584fc |
735 | } else { |
fab65ca9 |
736 | if ($htmlsave) { |
737 | $replace = htmlspecialchars($replace); |
738 | } |
739 | $ret.= $replace; |
d6f584fc |
740 | } |
098ea084 |
741 | break; |
742 | case 'Q': |
098ea084 |
743 | $replace = str_replace('_', ' ', $res[4]); |
da2415c1 |
744 | $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))', |
79e07c7e |
745 | $replace); |
098ea084 |
746 | if ($utfencode) { |
02474e43 |
747 | if ($can_be_encoded) { |
d6f584fc |
748 | /* convert string to different charset, |
02474e43 |
749 | * if functions asks for it (usually in compose) |
750 | */ |
d6f584fc |
751 | $replace = charset_convert($res[2], $replace,$default_charset); |
02474e43 |
752 | } else { |
d6f584fc |
753 | // convert string to html codes in order to display it |
098ea084 |
754 | $replace = charset_decode($res[2], $replace); |
02474e43 |
755 | } |
098ea084 |
756 | } else { |
757 | if ($htmlsave) { |
c96c32f4 |
758 | $replace = htmlspecialchars($replace); |
098ea084 |
759 | } |
760 | } |
08b7f7cc |
761 | $ret .= $replace; |
098ea084 |
762 | break; |
763 | default: |
764 | break; |
79e07c7e |
765 | } |
098ea084 |
766 | $chunk = $res[5]; |
767 | $encoded = true; |
08b7f7cc |
768 | } |
769 | if (!$encoded) { |
770 | if ($htmlsave) { |
9f7f68c3 |
771 | $ret .= ' '; |
08b7f7cc |
772 | } else { |
773 | $ret .= ' '; |
da2415c1 |
774 | } |
08b7f7cc |
775 | } |
dc3d13a7 |
776 | |
777 | if (!$encoded && $htmlsave) { |
778 | $ret .= htmlspecialchars($chunk); |
779 | } else { |
780 | $ret .= $chunk; |
781 | } |
098ea084 |
782 | ++$i; |
783 | } |
fd81e884 |
784 | /* remove the first added space */ |
785 | if ($ret) { |
786 | if ($htmlsave) { |
9f7f68c3 |
787 | $ret = substr($ret,5); |
fd81e884 |
788 | } else { |
789 | $ret = substr($ret,1); |
790 | } |
791 | } |
da2415c1 |
792 | |
08b7f7cc |
793 | return $ret; |
451f74a2 |
794 | } |
795 | |
9f7f68c3 |
796 | /** |
02474e43 |
797 | * Encodes header as quoted-printable |
798 | * |
799 | * Encode a string according to RFC 1522 for use in headers if it |
800 | * contains 8-bit characters or anything that looks like it should |
801 | * be encoded. |
802 | * |
803 | * @param string $string header string, that has to be encoded |
804 | * @return string quoted-printable encoded string |
805 | */ |
451f74a2 |
806 | function encodeHeader ($string) { |
6fbd125b |
807 | global $default_charset, $languages, $squirrelmail_language; |
83be314a |
808 | |
10dec454 |
809 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
33a55f5a |
810 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader')) { |
811 | return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader', $string); |
83be314a |
812 | } |
793cc001 |
813 | |
451f74a2 |
814 | // Encode only if the string contains 8-bit characters or =? |
3d8371be |
815 | $j = strlen($string); |
098ea084 |
816 | $max_l = 75 - strlen($default_charset) - 7; |
817 | $aRet = array(); |
451f74a2 |
818 | $ret = ''; |
c96c32f4 |
819 | $iEncStart = $enc_init = false; |
0d53f0f9 |
820 | $cur_l = $iOffset = 0; |
3d8371be |
821 | for($i = 0; $i < $j; ++$i) { |
c96c32f4 |
822 | switch($string{$i}) |
823 | { |
824 | case '=': |
825 | case '<': |
826 | case '>': |
827 | case ',': |
828 | case '?': |
829 | case '_': |
830 | if ($iEncStart === false) { |
831 | $iEncStart = $i; |
832 | } |
833 | $cur_l+=3; |
834 | if ($cur_l > ($max_l-2)) { |
08b7f7cc |
835 | /* if there is an stringpart that doesn't need encoding, add it */ |
c96c32f4 |
836 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
837 | $aRet[] = "=?$default_charset?Q?$ret?="; |
838 | $iOffset = $i; |
839 | $cur_l = 0; |
840 | $ret = ''; |
841 | $iEncStart = false; |
842 | } else { |
843 | $ret .= sprintf("=%02X",ord($string{$i})); |
844 | } |
845 | break; |
846 | case '(': |
847 | case ')': |
848 | if ($iEncStart !== false) { |
08b7f7cc |
849 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
c96c32f4 |
850 | $aRet[] = "=?$default_charset?Q?$ret?="; |
851 | $iOffset = $i; |
852 | $cur_l = 0; |
853 | $ret = ''; |
854 | $iEncStart = false; |
855 | } |
856 | break; |
857 | case ' ': |
858 | if ($iEncStart !== false) { |
098ea084 |
859 | $cur_l++; |
860 | if ($cur_l > $max_l) { |
08b7f7cc |
861 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
c96c32f4 |
862 | $aRet[] = "=?$default_charset?Q?$ret?="; |
863 | $iOffset = $i; |
864 | $cur_l = 0; |
865 | $ret = ''; |
866 | $iEncStart = false; |
08b7f7cc |
867 | } else { |
c96c32f4 |
868 | $ret .= '_'; |
869 | } |
870 | } |
871 | break; |
872 | default: |
873 | $k = ord($string{$i}); |
874 | if ($k > 126) { |
875 | if ($iEncStart === false) { |
325978ac |
876 | // do not start encoding in the middle of a string, also take the rest of the word. |
877 | $sLeadString = substr($string,0,$i); |
878 | $aLeadString = explode(' ',$sLeadString); |
da2415c1 |
879 | $sToBeEncoded = array_pop($aLeadString); |
325978ac |
880 | $iEncStart = $i - strlen($sToBeEncoded); |
881 | $ret .= $sToBeEncoded; |
882 | $cur_l += strlen($sToBeEncoded); |
c96c32f4 |
883 | } |
884 | $cur_l += 3; |
08b7f7cc |
885 | /* first we add the encoded string that reached it's max size */ |
c96c32f4 |
886 | if ($cur_l > ($max_l-2)) { |
08b7f7cc |
887 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
888 | $aRet[] = "=?$default_charset?Q?$ret?= "; /* the next part is also encoded => separate by space */ |
c96c32f4 |
889 | $cur_l = 3; |
890 | $ret = ''; |
891 | $iOffset = $i; |
08b7f7cc |
892 | $iEncStart = $i; |
c96c32f4 |
893 | } |
08b7f7cc |
894 | $enc_init = true; |
c96c32f4 |
895 | $ret .= sprintf("=%02X", $k); |
896 | } else { |
897 | if ($iEncStart !== false) { |
098ea084 |
898 | $cur_l++; |
899 | if ($cur_l > $max_l) { |
08b7f7cc |
900 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
c96c32f4 |
901 | $aRet[] = "=?$default_charset?Q?$ret?="; |
902 | $iEncStart = false; |
903 | $iOffset = $i; |
904 | $cur_l = 0; |
098ea084 |
905 | $ret = ''; |
08b7f7cc |
906 | } else { |
c96c32f4 |
907 | $ret .= $string{$i}; |
908 | } |
3d8371be |
909 | } |
c96c32f4 |
910 | } |
911 | break; |
f7b3ba37 |
912 | } |
451f74a2 |
913 | } |
793cc001 |
914 | |
c96c32f4 |
915 | if ($enc_init) { |
916 | if ($iEncStart !== false) { |
917 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
918 | $aRet[] = "=?$default_charset?Q?$ret?="; |
919 | } else { |
920 | $aRet[] = substr($string,$iOffset); |
921 | } |
922 | $string = implode('',$aRet); |
451f74a2 |
923 | } |
3d8371be |
924 | return $string; |
451f74a2 |
925 | } |
b74ba498 |
926 | |
691a2d25 |
927 | /* This function trys to locate the entity_id of a specific mime element */ |
3d8371be |
928 | function find_ent_id($id, $message) { |
a171b359 |
929 | for ($i = 0, $ret = ''; $ret == '' && $i < count($message->entities); $i++) { |
930 | if ($message->entities[$i]->header->type0 == 'multipart') { |
3d8371be |
931 | $ret = find_ent_id($id, $message->entities[$i]); |
451f74a2 |
932 | } else { |
3d8371be |
933 | if (strcasecmp($message->entities[$i]->header->id, $id) == 0) { |
d8cffbab |
934 | // if (sq_check_save_extension($message->entities[$i])) { |
a171b359 |
935 | return $message->entities[$i]->entity_id; |
da2415c1 |
936 | // } |
c8f5f606 |
937 | } elseif (!empty($message->entities[$i]->header->parameters['name'])) { |
938 | /** |
939 | * This is part of a fix for Outlook Express 6.x generating |
940 | * cid URLs without creating content-id headers |
941 | * @@JA - 20050207 |
942 | */ |
943 | if (strcasecmp($message->entities[$i]->header->parameters['name'], $id) == 0) { |
944 | return $message->entities[$i]->entity_id; |
945 | } |
3d8371be |
946 | } |
a3daaaf3 |
947 | } |
451f74a2 |
948 | } |
3d8371be |
949 | return $ret; |
451f74a2 |
950 | } |
a3daaaf3 |
951 | |
e5e9381a |
952 | function sq_check_save_extension($message) { |
953 | $filename = $message->getFilename(); |
954 | $ext = substr($filename, strrpos($filename,'.')+1); |
955 | $save_extensions = array('jpg','jpeg','gif','png','bmp'); |
3d8371be |
956 | return in_array($ext, $save_extensions); |
e5e9381a |
957 | } |
958 | |
959 | |
691a2d25 |
960 | /** |
02474e43 |
961 | ** HTMLFILTER ROUTINES |
962 | */ |
451f74a2 |
963 | |
2dd879b8 |
964 | /** |
02474e43 |
965 | * This function is more or less a wrapper around stripslashes. Apparently |
966 | * Explorer is stupid enough to just remove the backslashes and then |
967 | * execute the content of the attribute as if nothing happened. |
968 | * Who does that? |
969 | * |
970 | * @param attvalue The value of the attribute |
971 | * @return attvalue The value of the attribute stripslashed. |
972 | */ |
2dd879b8 |
973 | function sq_unbackslash($attvalue){ |
974 | /** |
02474e43 |
975 | * Remove any backslashes. See if there are any first. |
976 | */ |
91220ed1 |
977 | |
2dd879b8 |
978 | if (strstr($attvalue, '\\') !== false){ |
979 | $attvalue = stripslashes($attvalue); |
980 | } |
981 | return $attvalue; |
982 | } |
983 | |
984 | /** |
02474e43 |
985 | * Kill any tabs, newlines, or carriage returns. Our friends the |
986 | * makers of the browser with 95% market value decided that it'd |
987 | * be funny to make "java[tab]script" be just as good as "javascript". |
988 | * |
989 | * @param attvalue The attribute value before extraneous spaces removed. |
990 | * @return attvalue The attribute value after extraneous spaces removed. |
991 | */ |
2dd879b8 |
992 | function sq_unspace($attvalue){ |
993 | if (strcspn($attvalue, "\t\r\n") != strlen($attvalue)){ |
994 | $attvalue = str_replace(Array("\t", "\r", "\n"), Array('', '', ''), |
995 | $attvalue); |
996 | } |
997 | return $attvalue; |
998 | } |
999 | |
691a2d25 |
1000 | /** |
02474e43 |
1001 | * This function returns the final tag out of the tag name, an array |
1002 | * of attributes, and the type of the tag. This function is called by |
1003 | * sq_sanitize internally. |
1004 | * |
1005 | * @param $tagname the name of the tag. |
1006 | * @param $attary the array of attributes and their values |
1007 | * @param $tagtype The type of the tag (see in comments). |
1008 | * @return a string with the final tag representation. |
1009 | */ |
691a2d25 |
1010 | function sq_tagprint($tagname, $attary, $tagtype){ |
b583c3e8 |
1011 | $me = 'sq_tagprint'; |
3d8371be |
1012 | |
691a2d25 |
1013 | if ($tagtype == 2){ |
1014 | $fulltag = '</' . $tagname . '>'; |
1015 | } else { |
1016 | $fulltag = '<' . $tagname; |
1017 | if (is_array($attary) && sizeof($attary)){ |
1018 | $atts = Array(); |
1019 | while (list($attname, $attvalue) = each($attary)){ |
1020 | array_push($atts, "$attname=$attvalue"); |
1021 | } |
1022 | $fulltag .= ' ' . join(" ", $atts); |
1023 | } |
1024 | if ($tagtype == 3){ |
b583c3e8 |
1025 | $fulltag .= ' /'; |
691a2d25 |
1026 | } |
b583c3e8 |
1027 | $fulltag .= '>'; |
451f74a2 |
1028 | } |
691a2d25 |
1029 | return $fulltag; |
451f74a2 |
1030 | } |
a3daaaf3 |
1031 | |
691a2d25 |
1032 | /** |
02474e43 |
1033 | * A small helper function to use with array_walk. Modifies a by-ref |
1034 | * value and makes it lowercase. |
1035 | * |
1036 | * @param $val a value passed by-ref. |
1037 | * @return void since it modifies a by-ref value. |
1038 | */ |
691a2d25 |
1039 | function sq_casenormalize(&$val){ |
1040 | $val = strtolower($val); |
1041 | } |
451f74a2 |
1042 | |
691a2d25 |
1043 | /** |
02474e43 |
1044 | * This function skips any whitespace from the current position within |
1045 | * a string and to the next non-whitespace value. |
1046 | * |
1047 | * @param $body the string |
1048 | * @param $offset the offset within the string where we should start |
1049 | * looking for the next non-whitespace character. |
1050 | * @return the location within the $body where the next |
1051 | * non-whitespace char is located. |
1052 | */ |
691a2d25 |
1053 | function sq_skipspace($body, $offset){ |
b583c3e8 |
1054 | $me = 'sq_skipspace'; |
3d8371be |
1055 | preg_match('/^(\s*)/s', substr($body, $offset), $matches); |
691a2d25 |
1056 | if (sizeof($matches{1})){ |
1057 | $count = strlen($matches{1}); |
1058 | $offset += $count; |
451f74a2 |
1059 | } |
691a2d25 |
1060 | return $offset; |
451f74a2 |
1061 | } |
a3daaaf3 |
1062 | |
691a2d25 |
1063 | /** |
02474e43 |
1064 | * This function looks for the next character within a string. It's |
1065 | * really just a glorified "strpos", except it catches if failures |
1066 | * nicely. |
1067 | * |
1068 | * @param $body The string to look for needle in. |
1069 | * @param $offset Start looking from this position. |
1070 | * @param $needle The character/string to look for. |
1071 | * @return location of the next occurance of the needle, or |
1072 | * strlen($body) if needle wasn't found. |
1073 | */ |
691a2d25 |
1074 | function sq_findnxstr($body, $offset, $needle){ |
3d8371be |
1075 | $me = 'sq_findnxstr'; |
691a2d25 |
1076 | $pos = strpos($body, $needle, $offset); |
1077 | if ($pos === FALSE){ |
1078 | $pos = strlen($body); |
451f74a2 |
1079 | } |
691a2d25 |
1080 | return $pos; |
451f74a2 |
1081 | } |
a3daaaf3 |
1082 | |
691a2d25 |
1083 | /** |
02474e43 |
1084 | * This function takes a PCRE-style regexp and tries to match it |
1085 | * within the string. |
1086 | * |
1087 | * @param $body The string to look for needle in. |
1088 | * @param $offset Start looking from here. |
1089 | * @param $reg A PCRE-style regex to match. |
1090 | * @return Returns a false if no matches found, or an array |
1091 | * with the following members: |
1092 | * - integer with the location of the match within $body |
1093 | * - string with whatever content between offset and the match |
1094 | * - string with whatever it is we matched |
1095 | */ |
691a2d25 |
1096 | function sq_findnxreg($body, $offset, $reg){ |
b583c3e8 |
1097 | $me = 'sq_findnxreg'; |
691a2d25 |
1098 | $matches = Array(); |
1099 | $retarr = Array(); |
7d06541f |
1100 | preg_match("%^(.*?)($reg)%si", substr($body, $offset), $matches); |
1101 | if (!isset($matches{0}) || !$matches{0}){ |
691a2d25 |
1102 | $retarr = false; |
1103 | } else { |
1104 | $retarr{0} = $offset + strlen($matches{1}); |
1105 | $retarr{1} = $matches{1}; |
1106 | $retarr{2} = $matches{2}; |
1107 | } |
1108 | return $retarr; |
1109 | } |
a3daaaf3 |
1110 | |
691a2d25 |
1111 | /** |
02474e43 |
1112 | * This function looks for the next tag. |
1113 | * |
1114 | * @param $body String where to look for the next tag. |
1115 | * @param $offset Start looking from here. |
1116 | * @return false if no more tags exist in the body, or |
1117 | * an array with the following members: |
1118 | * - string with the name of the tag |
1119 | * - array with attributes and their values |
1120 | * - integer with tag type (1, 2, or 3) |
1121 | * - integer where the tag starts (starting "<") |
1122 | * - integer where the tag ends (ending ">") |
1123 | * first three members will be false, if the tag is invalid. |
1124 | */ |
691a2d25 |
1125 | function sq_getnxtag($body, $offset){ |
b583c3e8 |
1126 | $me = 'sq_getnxtag'; |
691a2d25 |
1127 | if ($offset > strlen($body)){ |
1128 | return false; |
1129 | } |
1130 | $lt = sq_findnxstr($body, $offset, "<"); |
1131 | if ($lt == strlen($body)){ |
1132 | return false; |
1133 | } |
1134 | /** |
02474e43 |
1135 | * We are here: |
1136 | * blah blah <tag attribute="value"> |
1137 | * \---------^ |
1138 | */ |
691a2d25 |
1139 | $pos = sq_skipspace($body, $lt+1); |
1140 | if ($pos >= strlen($body)){ |
1141 | return Array(false, false, false, $lt, strlen($body)); |
1142 | } |
1143 | /** |
02474e43 |
1144 | * There are 3 kinds of tags: |
1145 | * 1. Opening tag, e.g.: |
1146 | * <a href="blah"> |
1147 | * 2. Closing tag, e.g.: |
1148 | * </a> |
1149 | * 3. XHTML-style content-less tag, e.g.: |
1150 | * <img src="blah" /> |
1151 | */ |
691a2d25 |
1152 | $tagtype = false; |
1153 | switch (substr($body, $pos, 1)){ |
3d8371be |
1154 | case '/': |
1155 | $tagtype = 2; |
1156 | $pos++; |
1157 | break; |
1158 | case '!': |
1159 | /** |
02474e43 |
1160 | * A comment or an SGML declaration. |
1161 | */ |
3d8371be |
1162 | if (substr($body, $pos+1, 2) == "--"){ |
1163 | $gt = strpos($body, "-->", $pos); |
1164 | if ($gt === false){ |
1165 | $gt = strlen($body); |
1166 | } else { |
1167 | $gt += 2; |
1168 | } |
1169 | return Array(false, false, false, $lt, $gt); |
bb8d0799 |
1170 | } else { |
3d8371be |
1171 | $gt = sq_findnxstr($body, $pos, ">"); |
1172 | return Array(false, false, false, $lt, $gt); |
1173 | } |
1174 | break; |
1175 | default: |
1176 | /** |
02474e43 |
1177 | * Assume tagtype 1 for now. If it's type 3, we'll switch values |
1178 | * later. |
1179 | */ |
3d8371be |
1180 | $tagtype = 1; |
1181 | break; |
691a2d25 |
1182 | } |
a3daaaf3 |
1183 | |
691a2d25 |
1184 | $tagname = ''; |
1185 | /** |
02474e43 |
1186 | * Look for next [\W-_], which will indicate the end of the tag name. |
1187 | */ |
691a2d25 |
1188 | $regary = sq_findnxreg($body, $pos, "[^\w\-_]"); |
1189 | if ($regary == false){ |
1190 | return Array(false, false, false, $lt, strlen($body)); |
1191 | } |
1192 | list($pos, $tagname, $match) = $regary; |
1193 | $tagname = strtolower($tagname); |
1194 | |
1195 | /** |
02474e43 |
1196 | * $match can be either of these: |
1197 | * '>' indicating the end of the tag entirely. |
1198 | * '\s' indicating the end of the tag name. |
1199 | * '/' indicating that this is type-3 xhtml tag. |
1200 | * |
1201 | * Whatever else we find there indicates an invalid tag. |
1202 | */ |
691a2d25 |
1203 | switch ($match){ |
3d8371be |
1204 | case '/': |
691a2d25 |
1205 | /** |
02474e43 |
1206 | * This is an xhtml-style tag with a closing / at the |
1207 | * end, like so: <img src="blah" />. Check if it's followed |
1208 | * by the closing bracket. If not, then this tag is invalid |
1209 | */ |
3d8371be |
1210 | if (substr($body, $pos, 2) == "/>"){ |
1211 | $pos++; |
1212 | $tagtype = 3; |
1213 | } else { |
1214 | $gt = sq_findnxstr($body, $pos, ">"); |
1215 | $retary = Array(false, false, false, $lt, $gt); |
1216 | return $retary; |
1217 | } |
1218 | case '>': |
1219 | return Array($tagname, false, $tagtype, $lt, $pos); |
1220 | break; |
1221 | default: |
1222 | /** |
02474e43 |
1223 | * Check if it's whitespace |
1224 | */ |
3d8371be |
1225 | if (!preg_match('/\s/', $match)){ |
1226 | /** |
02474e43 |
1227 | * This is an invalid tag! Look for the next closing ">". |
1228 | */ |
7d06541f |
1229 | $gt = sq_findnxstr($body, $lt, ">"); |
3d8371be |
1230 | return Array(false, false, false, $lt, $gt); |
1231 | } |
1232 | break; |
691a2d25 |
1233 | } |
3d8371be |
1234 | |
691a2d25 |
1235 | /** |
02474e43 |
1236 | * At this point we're here: |
1237 | * <tagname attribute='blah'> |
1238 | * \-------^ |
1239 | * |
1240 | * At this point we loop in order to find all attributes. |
1241 | */ |
691a2d25 |
1242 | $attname = ''; |
691a2d25 |
1243 | $attary = Array(); |
1244 | |
1245 | while ($pos <= strlen($body)){ |
1246 | $pos = sq_skipspace($body, $pos); |
1247 | if ($pos == strlen($body)){ |
1248 | /** |
02474e43 |
1249 | * Non-closed tag. |
1250 | */ |
691a2d25 |
1251 | return Array(false, false, false, $lt, $pos); |
1252 | } |
1253 | /** |
02474e43 |
1254 | * See if we arrived at a ">" or "/>", which means that we reached |
1255 | * the end of the tag. |
1256 | */ |
691a2d25 |
1257 | $matches = Array(); |
164800ad |
1258 | if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) { |
c828931c |
1259 | /** |
02474e43 |
1260 | * Yep. So we did. |
1261 | */ |
c828931c |
1262 | $pos += strlen($matches{1}); |
1263 | if ($matches{2} == "/>"){ |
1264 | $tagtype = 3; |
1265 | $pos++; |
1266 | } |
1267 | return Array($tagname, $attary, $tagtype, $lt, $pos); |
1268 | } |
a3daaaf3 |
1269 | |
cca46357 |
1270 | /** |
02474e43 |
1271 | * There are several types of attributes, with optional |
1272 | * [:space:] between members. |
1273 | * Type 1: |
1274 | * attrname[:space:]=[:space:]'CDATA' |
1275 | * Type 2: |
1276 | * attrname[:space:]=[:space:]"CDATA" |
1277 | * Type 3: |
1278 | * attr[:space:]=[:space:]CDATA |
1279 | * Type 4: |
1280 | * attrname |
1281 | * |
1282 | * We leave types 1 and 2 the same, type 3 we check for |
1283 | * '"' and convert to """ if needed, then wrap in |
1284 | * double quotes. Type 4 we convert into: |
1285 | * attrname="yes". |
1286 | */ |
3f7c623f |
1287 | $regary = sq_findnxreg($body, $pos, "[^:\w\-_]"); |
691a2d25 |
1288 | if ($regary == false){ |
1289 | /** |
02474e43 |
1290 | * Looks like body ended before the end of tag. |
1291 | */ |
691a2d25 |
1292 | return Array(false, false, false, $lt, strlen($body)); |
cca46357 |
1293 | } |
691a2d25 |
1294 | list($pos, $attname, $match) = $regary; |
1295 | $attname = strtolower($attname); |
1296 | /** |
02474e43 |
1297 | * We arrived at the end of attribute name. Several things possible |
1298 | * here: |
1299 | * '>' means the end of the tag and this is attribute type 4 |
1300 | * '/' if followed by '>' means the same thing as above |
1301 | * '\s' means a lot of things -- look what it's followed by. |
1302 | * anything else means the attribute is invalid. |
1303 | */ |
691a2d25 |
1304 | switch($match){ |
3d8371be |
1305 | case '/': |
691a2d25 |
1306 | /** |
02474e43 |
1307 | * This is an xhtml-style tag with a closing / at the |
1308 | * end, like so: <img src="blah" />. Check if it's followed |
1309 | * by the closing bracket. If not, then this tag is invalid |
1310 | */ |
3d8371be |
1311 | if (substr($body, $pos, 2) == "/>"){ |
691a2d25 |
1312 | $pos++; |
3d8371be |
1313 | $tagtype = 3; |
691a2d25 |
1314 | } else { |
3d8371be |
1315 | $gt = sq_findnxstr($body, $pos, ">"); |
1316 | $retary = Array(false, false, false, $lt, $gt); |
1317 | return $retary; |
1318 | } |
1319 | case '>': |
1320 | $attary{$attname} = '"yes"'; |
1321 | return Array($tagname, $attary, $tagtype, $lt, $pos); |
1322 | break; |
1323 | default: |
1324 | /** |
02474e43 |
1325 | * Skip whitespace and see what we arrive at. |
1326 | */ |
3d8371be |
1327 | $pos = sq_skipspace($body, $pos); |
1328 | $char = substr($body, $pos, 1); |
1329 | /** |
02474e43 |
1330 | * Two things are valid here: |
1331 | * '=' means this is attribute type 1 2 or 3. |
1332 | * \w means this was attribute type 4. |
1333 | * anything else we ignore and re-loop. End of tag and |
1334 | * invalid stuff will be caught by our checks at the beginning |
1335 | * of the loop. |
1336 | */ |
3d8371be |
1337 | if ($char == "="){ |
1338 | $pos++; |
1339 | $pos = sq_skipspace($body, $pos); |
691a2d25 |
1340 | /** |
02474e43 |
1341 | * Here are 3 possibilities: |
1342 | * "'" attribute type 1 |
1343 | * '"' attribute type 2 |
1344 | * everything else is the content of tag type 3 |
1345 | */ |
3d8371be |
1346 | $quot = substr($body, $pos, 1); |
1347 | if ($quot == "'"){ |
1348 | $regary = sq_findnxreg($body, $pos+1, "\'"); |
1349 | if ($regary == false){ |
1350 | return Array(false, false, false, $lt, strlen($body)); |
1351 | } |
1352 | list($pos, $attval, $match) = $regary; |
1353 | $pos++; |
1354 | $attary{$attname} = "'" . $attval . "'"; |
1355 | } else if ($quot == '"'){ |
1356 | $regary = sq_findnxreg($body, $pos+1, '\"'); |
1357 | if ($regary == false){ |
1358 | return Array(false, false, false, $lt, strlen($body)); |
1359 | } |
1360 | list($pos, $attval, $match) = $regary; |
1361 | $pos++; |
1362 | $attary{$attname} = '"' . $attval . '"'; |
1363 | } else { |
1364 | /** |
02474e43 |
1365 | * These are hateful. Look for \s, or >. |
1366 | */ |
3d8371be |
1367 | $regary = sq_findnxreg($body, $pos, "[\s>]"); |
1368 | if ($regary == false){ |
1369 | return Array(false, false, false, $lt, strlen($body)); |
1370 | } |
1371 | list($pos, $attval, $match) = $regary; |
1372 | /** |
02474e43 |
1373 | * If it's ">" it will be caught at the top. |
1374 | */ |
3d8371be |
1375 | $attval = preg_replace("/\"/s", """, $attval); |
1376 | $attary{$attname} = '"' . $attval . '"'; |
7e235a1a |
1377 | } |
3d8371be |
1378 | } else if (preg_match("|[\w/>]|", $char)) { |
691a2d25 |
1379 | /** |
02474e43 |
1380 | * That was attribute type 4. |
1381 | */ |
3d8371be |
1382 | $attary{$attname} = '"yes"'; |
1383 | } else { |
1384 | /** |
02474e43 |
1385 | * An illegal character. Find next '>' and return. |
1386 | */ |
3d8371be |
1387 | $gt = sq_findnxstr($body, $pos, ">"); |
1388 | return Array(false, false, false, $lt, $gt); |
451f74a2 |
1389 | } |
3d8371be |
1390 | break; |
691a2d25 |
1391 | } |
1392 | } |
1393 | /** |
02474e43 |
1394 | * The fact that we got here indicates that the tag end was never |
1395 | * found. Return invalid tag indication so it gets stripped. |
1396 | */ |
691a2d25 |
1397 | return Array(false, false, false, $lt, strlen($body)); |
1398 | } |
1399 | |
1400 | /** |
02474e43 |
1401 | * This function checks attribute values for entity-encoded values |
1402 | * and returns them translated into 8-bit strings so we can run |
1403 | * checks on them. |
1404 | * |
1405 | * @param $attvalue A string to run entity check against. |
1406 | * @return Translated value. |
1407 | */ |
91220ed1 |
1408 | |
691a2d25 |
1409 | function sq_deent($attvalue){ |
b583c3e8 |
1410 | $me = 'sq_deent'; |
691a2d25 |
1411 | /** |
02474e43 |
1412 | * See if we have to run the checks first. All entities must start |
1413 | * with "&". |
1414 | */ |
91220ed1 |
1415 | if (strpos($attvalue, '&') === false){ |
691a2d25 |
1416 | return $attvalue; |
1417 | } |
1418 | /** |
02474e43 |
1419 | * Check named entities first. |
1420 | */ |
691a2d25 |
1421 | $trans = get_html_translation_table(HTML_ENTITIES); |
1422 | /** |
02474e43 |
1423 | * Leave " in, as it can mess us up. |
1424 | */ |
691a2d25 |
1425 | $trans = array_flip($trans); |
91220ed1 |
1426 | unset($trans{'"'}); |
691a2d25 |
1427 | while (list($ent, $val) = each($trans)){ |
91220ed1 |
1428 | $attvalue = preg_replace('/' . $ent . '*/si', $val, $attvalue); |
691a2d25 |
1429 | } |
1430 | /** |
02474e43 |
1431 | * Now translate numbered entities from 1 to 255 if needed. |
1432 | */ |
91220ed1 |
1433 | if (strpos($attvalue, '#') !== false){ |
691a2d25 |
1434 | $omit = Array(34, 39); |
91220ed1 |
1435 | for ($asc = 256; $asc >= 0; $asc--){ |
691a2d25 |
1436 | if (!in_array($asc, $omit)){ |
1437 | $chr = chr($asc); |
91220ed1 |
1438 | $octrule = '/\�*' . $asc . ';*/si'; |
1439 | $hexrule = '/\�*' . dechex($asc) . ';*/si'; |
1440 | $attvalue = preg_replace($octrule, $chr, $attvalue); |
1441 | $attvalue = preg_replace($hexrule, $chr, $attvalue); |
a3daaaf3 |
1442 | } |
691a2d25 |
1443 | } |
1444 | } |
1445 | return $attvalue; |
1446 | } |
1447 | |
1448 | /** |
02474e43 |
1449 | * This function runs various checks against the attributes. |
1450 | * |
1451 | * @param $tagname String with the name of the tag. |
1452 | * @param $attary Array with all tag attributes. |
1453 | * @param $rm_attnames See description for sq_sanitize |
1454 | * @param $bad_attvals See description for sq_sanitize |
1455 | * @param $add_attr_to_tag See description for sq_sanitize |
1456 | * @param $message message object |
1457 | * @param $id message id |
1458 | * @return Array with modified attributes. |
1459 | */ |
da2415c1 |
1460 | function sq_fixatts($tagname, |
1461 | $attary, |
691a2d25 |
1462 | $rm_attnames, |
1463 | $bad_attvals, |
1464 | $add_attr_to_tag, |
1465 | $message, |
b3af12ef |
1466 | $id, |
3d8371be |
1467 | $mailbox |
691a2d25 |
1468 | ){ |
b583c3e8 |
1469 | $me = 'sq_fixatts'; |
691a2d25 |
1470 | while (list($attname, $attvalue) = each($attary)){ |
1471 | /** |
02474e43 |
1472 | * See if this attribute should be removed. |
1473 | */ |
691a2d25 |
1474 | foreach ($rm_attnames as $matchtag=>$matchattrs){ |
1475 | if (preg_match($matchtag, $tagname)){ |
1476 | foreach ($matchattrs as $matchattr){ |
1477 | if (preg_match($matchattr, $attname)){ |
1478 | unset($attary{$attname}); |
1479 | continue; |
1480 | } |
451f74a2 |
1481 | } |
451f74a2 |
1482 | } |
691a2d25 |
1483 | } |
1484 | /** |
02474e43 |
1485 | * Remove any backslashes, entities, and extraneous whitespace. |
1486 | */ |
2dd879b8 |
1487 | $attvalue = sq_unbackslash($attvalue); |
691a2d25 |
1488 | $attvalue = sq_deent($attvalue); |
2dd879b8 |
1489 | $attvalue = sq_unspace($attvalue); |
691a2d25 |
1490 | |
af861a34 |
1491 | /** |
02474e43 |
1492 | * Remove \r \n \t \0 " " "\\" |
1493 | */ |
8d863f64 |
1494 | $attvalue = str_replace(Array("\r", "\n", "\t", "\0", " ", "\\"), |
af861a34 |
1495 | Array('', '','','','',''), $attvalue); |
1496 | |
691a2d25 |
1497 | /** |
02474e43 |
1498 | * Now let's run checks on the attvalues. |
1499 | * I don't expect anyone to comprehend this. If you do, |
1500 | * get in touch with me so I can drive to where you live and |
1501 | * shake your hand personally. :) |
1502 | */ |
691a2d25 |
1503 | foreach ($bad_attvals as $matchtag=>$matchattrs){ |
1504 | if (preg_match($matchtag, $tagname)){ |
1505 | foreach ($matchattrs as $matchattr=>$valary){ |
1506 | if (preg_match($matchattr, $attname)){ |
1507 | /** |
02474e43 |
1508 | * There are two arrays in valary. |
1509 | * First is matches. |
1510 | * Second one is replacements |
1511 | */ |
691a2d25 |
1512 | list($valmatch, $valrepl) = $valary; |
da2415c1 |
1513 | $newvalue = |
691a2d25 |
1514 | preg_replace($valmatch, $valrepl, $attvalue); |
1515 | if ($newvalue != $attvalue){ |
1516 | $attary{$attname} = $newvalue; |
1517 | } |
1518 | } |
1519 | } |
451f74a2 |
1520 | } |
a3daaaf3 |
1521 | } |
834a1027 |
1522 | |
1523 | |
1524 | /** |
1525 | * Replace empty src tags with the blank image. src is only used |
1526 | * for frames, images, and image inputs. Doing a replace should |
1527 | * not affect them working as should be, however it will stop |
1528 | * IE from being kicked off when src for img tags are not set |
1529 | */ |
5a6fde9e |
1530 | if (($attname == 'src') && ($attvalue == '""')) { |
834a1027 |
1531 | $attary{$attname} = '"' . SM_PATH . 'images/blank.png"'; |
1532 | } |
1533 | |
691a2d25 |
1534 | /** |
02474e43 |
1535 | * Turn cid: urls into http-friendly ones. |
1536 | */ |
691a2d25 |
1537 | if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){ |
b3af12ef |
1538 | $attary{$attname} = sq_cid2http($message, $id, $attvalue, $mailbox); |
691a2d25 |
1539 | } |
ff940ebc |
1540 | |
1541 | /** |
1542 | * "Hack" fix for Outlook using propriatary outbind:// protocol in img tags. |
1543 | * One day MS might actually make it match something useful, for now, falling |
1544 | * back to using cid2http, so we can grab the blank.png. |
1545 | */ |
1546 | if (preg_match("/^[\'\"]\s*outbind:\/\//si", $attvalue)) { |
1547 | $attary{$attname} = sq_cid2http($message, $id, $attvalue, $mailbox); |
1548 | } |
1549 | |
a3daaaf3 |
1550 | } |
691a2d25 |
1551 | /** |
02474e43 |
1552 | * See if we need to append any attributes to this tag. |
1553 | */ |
691a2d25 |
1554 | foreach ($add_attr_to_tag as $matchtag=>$addattary){ |
1555 | if (preg_match($matchtag, $tagname)){ |
1556 | $attary = array_merge($attary, $addattary); |
1557 | } |
1558 | } |
1559 | return $attary; |
451f74a2 |
1560 | } |
a3daaaf3 |
1561 | |
691a2d25 |
1562 | /** |
02474e43 |
1563 | * This function edits the style definition to make them friendly and |
1564 | * usable in SquirrelMail. |
1565 | * |
1566 | * @param $message the message object |
1567 | * @param $id the message id |
1568 | * @param $content a string with whatever is between <style> and </style> |
1569 | * @param $mailbox the message mailbox |
1570 | * @return a string with edited content. |
1571 | */ |
e60a299a |
1572 | function sq_fixstyle($body, $pos, $message, $id, $mailbox){ |
691a2d25 |
1573 | global $view_unsafe_images; |
b583c3e8 |
1574 | $me = 'sq_fixstyle'; |
7d06541f |
1575 | $ret = sq_findnxreg($body, $pos, '</\s*style\s*>'); |
1576 | if ($ret == FALSE){ |
1577 | return array(FALSE, strlen($body)); |
1578 | } |
1579 | $newpos = $ret[0] + strlen($ret[2]); |
1580 | $content = $ret[1]; |
691a2d25 |
1581 | /** |
02474e43 |
1582 | * First look for general BODY style declaration, which would be |
1583 | * like so: |
1584 | * body {background: blah-blah} |
1585 | * and change it to .bodyclass so we can just assign it to a <div> |
1586 | */ |
691a2d25 |
1587 | $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content); |
3d8371be |
1588 | $secremoveimg = '../images/' . _("sec_remove_eng.png"); |
691a2d25 |
1589 | /** |
02474e43 |
1590 | * Fix url('blah') declarations. |
1591 | */ |
7d06541f |
1592 | $content = preg_replace("|url\s*\(\s*([\'\"])\s*\S+script\s*:.*?([\'\"])\s*\)|si", |
691a2d25 |
1593 | "url(\\1$secremoveimg\\2)", $content); |
1594 | /** |
02474e43 |
1595 | * Fix url('https*://.*) declarations but only if $view_unsafe_images |
1596 | * is false. |
1597 | */ |
691a2d25 |
1598 | if (!$view_unsafe_images){ |
7d06541f |
1599 | $content = preg_replace("|url\s*\(\s*([\'\"])\s*https*:.*?([\'\"])\s*\)|si", |
691a2d25 |
1600 | "url(\\1$secremoveimg\\2)", $content); |
1601 | } |
da2415c1 |
1602 | |
691a2d25 |
1603 | /** |
02474e43 |
1604 | * Fix urls that refer to cid: |
1605 | */ |
da2415c1 |
1606 | while (preg_match("|url\s*\(\s*([\'\"]\s*cid:.*?[\'\"])\s*\)|si", |
02474e43 |
1607 | $content, $matches)){ |
691a2d25 |
1608 | $cidurl = $matches{1}; |
e60a299a |
1609 | $httpurl = sq_cid2http($message, $id, $cidurl, $mailbox); |
7d06541f |
1610 | $content = preg_replace("|url\s*\(\s*$cidurl\s*\)|si", |
691a2d25 |
1611 | "url($httpurl)", $content); |
1612 | } |
a3daaaf3 |
1613 | |
691a2d25 |
1614 | /** |
02474e43 |
1615 | * Fix stupid css declarations which lead to vulnerabilities |
1616 | * in IE. |
1617 | */ |
f83c60a2 |
1618 | $match = Array('/expression/i', |
02474e43 |
1619 | '/behaviou*r/i', |
1620 | '/binding/i', |
1621 | '/include-source/i'); |
2dd879b8 |
1622 | $replace = Array('idiocy', 'idiocy', 'idiocy', 'idiocy'); |
bb8d0799 |
1623 | $content = preg_replace($match, $replace, $content); |
7d06541f |
1624 | return array($content, $newpos); |
691a2d25 |
1625 | } |
a3daaaf3 |
1626 | |
691a2d25 |
1627 | /** |
02474e43 |
1628 | * This function converts cid: url's into the ones that can be viewed in |
1629 | * the browser. |
1630 | * |
1631 | * @param $message the message object |
1632 | * @param $id the message id |
1633 | * @param $cidurl the cid: url. |
1634 | * @param $mailbox the message mailbox |
1635 | * @return a string with a http-friendly url |
1636 | */ |
b3af12ef |
1637 | function sq_cid2http($message, $id, $cidurl, $mailbox){ |
691a2d25 |
1638 | /** |
02474e43 |
1639 | * Get rid of quotes. |
1640 | */ |
691a2d25 |
1641 | $quotchar = substr($cidurl, 0, 1); |
2dd879b8 |
1642 | if ($quotchar == '"' || $quotchar == "'"){ |
1643 | $cidurl = str_replace($quotchar, "", $cidurl); |
1644 | } else { |
1645 | $quotchar = ''; |
1646 | } |
691a2d25 |
1647 | $cidurl = substr(trim($cidurl), 4); |
e5e9381a |
1648 | $linkurl = find_ent_id($cidurl, $message); |
1649 | /* in case of non-save cid links $httpurl should be replaced by a sort of |
02474e43 |
1650 | unsave link image */ |
e5e9381a |
1651 | $httpurl = ''; |
c8f5f606 |
1652 | |
1653 | /** |
1654 | * This is part of a fix for Outlook Express 6.x generating |
1655 | * cid URLs without creating content-id headers. These images are |
1656 | * not part of the multipart/related html mail. The html contains |
1657 | * <img src="cid:{some_id}/image_filename.ext"> references to |
1658 | * attached images with as goal to render them inline although |
1659 | * the attachment disposition property is not inline. |
1660 | **/ |
1661 | |
1662 | if (empty($linkurl)) { |
1663 | if (preg_match('/{.*}\//', $cidurl)) { |
1664 | $cidurl = preg_replace('/{.*}\//','', $cidurl); |
1665 | if (!empty($cidurl)) { |
1666 | $linkurl = find_ent_id($cidurl, $message); |
1667 | } |
1668 | } |
1669 | } |
1670 | |
1671 | if (!empty($linkurl)) { |
6b04287c |
1672 | $httpurl = $quotchar . SM_PATH . 'src/download.php?absolute_dl=true&' . |
c8f5f606 |
1673 | "passed_id=$id&mailbox=" . urlencode($mailbox) . |
1674 | '&ent_id=' . $linkurl . $quotchar; |
1675 | } else { |
1676 | /** |
1677 | * If we couldn't generate a proper img url, drop in a blank image |
1678 | * instead of sending back empty, otherwise it causes unusual behaviour |
1679 | */ |
59a3dab6 |
1680 | $httpurl = $quotchar . SM_PATH . 'images/blank.png'; |
e5e9381a |
1681 | } |
c8f5f606 |
1682 | |
691a2d25 |
1683 | return $httpurl; |
1684 | } |
1685 | |
1686 | /** |
02474e43 |
1687 | * This function changes the <body> tag into a <div> tag since we |
1688 | * can't really have a body-within-body. |
1689 | * |
1690 | * @param $attary an array of attributes and values of <body> |
1691 | * @param $mailbox mailbox we're currently reading (for cid2http) |
1692 | * @param $message current message (for cid2http) |
1693 | * @param $id current message id (for cid2http) |
1694 | * @return a modified array of attributes to be set for <div> |
1695 | */ |
2dd879b8 |
1696 | function sq_body2div($attary, $mailbox, $message, $id){ |
b583c3e8 |
1697 | $me = 'sq_body2div'; |
3d8371be |
1698 | $divattary = Array('class' => "'bodyclass'"); |
b583c3e8 |
1699 | $text = '#000000'; |
80b4debd |
1700 | $has_bgc_stl = $has_txt_stl = false; |
b583c3e8 |
1701 | $styledef = ''; |
691a2d25 |
1702 | if (is_array($attary) && sizeof($attary) > 0){ |
1703 | foreach ($attary as $attname=>$attvalue){ |
1704 | $quotchar = substr($attvalue, 0, 1); |
1705 | $attvalue = str_replace($quotchar, "", $attvalue); |
1706 | switch ($attname){ |
3d8371be |
1707 | case 'background': |
da2415c1 |
1708 | $attvalue = sq_cid2http($message, $id, |
2dd879b8 |
1709 | $attvalue, $mailbox); |
3d8371be |
1710 | $styledef .= "background-image: url('$attvalue'); "; |
1711 | break; |
1712 | case 'bgcolor': |
80b4debd |
1713 | $has_bgc_stl = true; |
3d8371be |
1714 | $styledef .= "background-color: $attvalue; "; |
1715 | break; |
1716 | case 'text': |
80b4debd |
1717 | $has_txt_stl = true; |
3d8371be |
1718 | $styledef .= "color: $attvalue; "; |
1719 | break; |
691a2d25 |
1720 | } |
a3daaaf3 |
1721 | } |
80b4debd |
1722 | // Outlook defines a white bgcolor and no text color. This can lead to |
1723 | // white text on a white bg with certain themes. |
1724 | if ($has_bgc_stl && !$has_txt_stl) { |
1725 | $styledef .= "color: $text; "; |
1726 | } |
691a2d25 |
1727 | if (strlen($styledef) > 0){ |
1728 | $divattary{"style"} = "\"$styledef\""; |
1729 | } |
1730 | } |
1731 | return $divattary; |
1732 | } |
a3daaaf3 |
1733 | |
691a2d25 |
1734 | /** |
02474e43 |
1735 | * This is the main function and the one you should actually be calling. |
1736 | * There are several variables you should be aware of an which need |
1737 | * special description. |
1738 | * |
1739 | * Since the description is quite lengthy, see it here: |
1740 | * http://linux.duke.edu/projects/mini/htmlfilter/ |
1741 | * |
1742 | * @param $body the string with HTML you wish to filter |
1743 | * @param $tag_list see description above |
1744 | * @param $rm_tags_with_content see description above |
1745 | * @param $self_closing_tags see description above |
1746 | * @param $force_tag_closing see description above |
1747 | * @param $rm_attnames see description above |
1748 | * @param $bad_attvals see description above |
1749 | * @param $add_attr_to_tag see description above |
1750 | * @param $message message object |
1751 | * @param $id message id |
1752 | * @return sanitized html safe to show on your pages. |
1753 | */ |
da2415c1 |
1754 | function sq_sanitize($body, |
02474e43 |
1755 | $tag_list, |
1756 | $rm_tags_with_content, |
1757 | $self_closing_tags, |
1758 | $force_tag_closing, |
1759 | $rm_attnames, |
1760 | $bad_attvals, |
1761 | $add_attr_to_tag, |
1762 | $message, |
1763 | $id, |
1764 | $mailbox |
1765 | ){ |
b583c3e8 |
1766 | $me = 'sq_sanitize'; |
7d06541f |
1767 | $rm_tags = array_shift($tag_list); |
691a2d25 |
1768 | /** |
02474e43 |
1769 | * Normalize rm_tags and rm_tags_with_content. |
1770 | */ |
7d06541f |
1771 | @array_walk($tag_list, 'sq_casenormalize'); |
691a2d25 |
1772 | @array_walk($rm_tags_with_content, 'sq_casenormalize'); |
1773 | @array_walk($self_closing_tags, 'sq_casenormalize'); |
1774 | /** |
02474e43 |
1775 | * See if tag_list is of tags to remove or tags to allow. |
1776 | * false means remove these tags |
1777 | * true means allow these tags |
1778 | */ |
691a2d25 |
1779 | $curpos = 0; |
1780 | $open_tags = Array(); |
2dd879b8 |
1781 | $trusted = "\n<!-- begin sanitized html -->\n"; |
691a2d25 |
1782 | $skip_content = false; |
bb8d0799 |
1783 | /** |
02474e43 |
1784 | * Take care of netscape's stupid javascript entities like |
1785 | * &{alert('boo')}; |
1786 | */ |
bb8d0799 |
1787 | $body = preg_replace("/&(\{.*?\};)/si", "&\\1", $body); |
691a2d25 |
1788 | |
7d06541f |
1789 | while (($curtag = sq_getnxtag($body, $curpos)) != FALSE){ |
691a2d25 |
1790 | list($tagname, $attary, $tagtype, $lt, $gt) = $curtag; |
1791 | $free_content = substr($body, $curpos, $lt-$curpos); |
1792 | /** |
02474e43 |
1793 | * Take care of <style> |
1794 | */ |
7d06541f |
1795 | if ($tagname == "style" && $tagtype == 1){ |
da2415c1 |
1796 | list($free_content, $curpos) = |
e60a299a |
1797 | sq_fixstyle($body, $gt+1, $message, $id, $mailbox); |
7d06541f |
1798 | if ($free_content != FALSE){ |
1799 | $trusted .= sq_tagprint($tagname, $attary, $tagtype); |
1800 | $trusted .= $free_content; |
1801 | $trusted .= sq_tagprint($tagname, false, 2); |
1802 | } |
1803 | continue; |
691a2d25 |
1804 | } |
1805 | if ($skip_content == false){ |
1806 | $trusted .= $free_content; |
691a2d25 |
1807 | } |
1808 | if ($tagname != FALSE){ |
1809 | if ($tagtype == 2){ |
1810 | if ($skip_content == $tagname){ |
1811 | /** |
02474e43 |
1812 | * Got to the end of tag we needed to remove. |
1813 | */ |
691a2d25 |
1814 | $tagname = false; |
1815 | $skip_content = false; |
1816 | } else { |
1817 | if ($skip_content == false){ |
c828931c |
1818 | if ($tagname == "body"){ |
1819 | $tagname = "div"; |
2dd879b8 |
1820 | } |
da2415c1 |
1821 | if (isset($open_tags{$tagname}) && |
2dd879b8 |
1822 | $open_tags{$tagname} > 0){ |
1823 | $open_tags{$tagname}--; |
691a2d25 |
1824 | } else { |
2dd879b8 |
1825 | $tagname = false; |
691a2d25 |
1826 | } |
691a2d25 |
1827 | } |
1828 | } |
1829 | } else { |
1830 | /** |
02474e43 |
1831 | * $rm_tags_with_content |
1832 | */ |
691a2d25 |
1833 | if ($skip_content == false){ |
1834 | /** |
02474e43 |
1835 | * See if this is a self-closing type and change |
1836 | * tagtype appropriately. |
1837 | */ |
691a2d25 |
1838 | if ($tagtype == 1 |
1839 | && in_array($tagname, $self_closing_tags)){ |
2dd879b8 |
1840 | $tagtype = 3; |
691a2d25 |
1841 | } |
1842 | /** |
02474e43 |
1843 | * See if we should skip this tag and any content |
1844 | * inside it. |
1845 | */ |
691a2d25 |
1846 | if ($tagtype == 1 && |
1847 | in_array($tagname, $rm_tags_with_content)){ |
1848 | $skip_content = $tagname; |
1849 | } else { |
da2415c1 |
1850 | if (($rm_tags == false |
02474e43 |
1851 | && in_array($tagname, $tag_list)) || |
691a2d25 |
1852 | ($rm_tags == true && |
02474e43 |
1853 | !in_array($tagname, $tag_list))){ |
691a2d25 |
1854 | $tagname = false; |
1855 | } else { |
2dd879b8 |
1856 | /** |
02474e43 |
1857 | * Convert body into div. |
1858 | */ |
2dd879b8 |
1859 | if ($tagname == "body"){ |
1860 | $tagname = "div"; |
da2415c1 |
1861 | $attary = sq_body2div($attary, $mailbox, |
02474e43 |
1862 | $message, $id); |
2dd879b8 |
1863 | } |
691a2d25 |
1864 | if ($tagtype == 1){ |
1865 | if (isset($open_tags{$tagname})){ |
1866 | $open_tags{$tagname}++; |
1867 | } else { |
1868 | $open_tags{$tagname}=1; |
1869 | } |
1870 | } |
1871 | /** |
02474e43 |
1872 | * This is where we run other checks. |
1873 | */ |
691a2d25 |
1874 | if (is_array($attary) && sizeof($attary) > 0){ |
1875 | $attary = sq_fixatts($tagname, |
02474e43 |
1876 | $attary, |
1877 | $rm_attnames, |
1878 | $bad_attvals, |
1879 | $add_attr_to_tag, |
1880 | $message, |
1881 | $id, |
1882 | $mailbox |
1883 | ); |
691a2d25 |
1884 | } |
1885 | } |
1886 | } |
691a2d25 |
1887 | } |
1888 | } |
1889 | if ($tagname != false && $skip_content == false){ |
1890 | $trusted .= sq_tagprint($tagname, $attary, $tagtype); |
1891 | } |
691a2d25 |
1892 | } |
1893 | $curpos = $gt+1; |
a3daaaf3 |
1894 | } |
691a2d25 |
1895 | $trusted .= substr($body, $curpos, strlen($body)-$curpos); |
1896 | if ($force_tag_closing == true){ |
1897 | foreach ($open_tags as $tagname=>$opentimes){ |
1898 | while ($opentimes > 0){ |
1899 | $trusted .= '</' . $tagname . '>'; |
1900 | $opentimes--; |
1901 | } |
1902 | } |
1903 | $trusted .= "\n"; |
1904 | } |
1905 | $trusted .= "<!-- end sanitized html -->\n"; |
1906 | return $trusted; |
1907 | } |
451f74a2 |
1908 | |
691a2d25 |
1909 | /** |
02474e43 |
1910 | * This is a wrapper function to call html sanitizing routines. |
1911 | * |
1912 | * @param $body the body of the message |
1913 | * @param $id the id of the message |
1814c39b |
1914 | * @param $message |
1915 | * @param $mailbox |
1916 | * @param boolean $take_mailto_links When TRUE, converts mailto: links |
1917 | * into internal SM compose links |
1918 | * (optional; default = TRUE) |
02474e43 |
1919 | * @return a string with html safe to display in the browser. |
1920 | */ |
1814c39b |
1921 | function magicHTML($body, $id, $message, $mailbox = 'INBOX', $take_mailto_links = true) { |
376667f7 |
1922 | |
1923 | require_once(SM_PATH . 'functions/url_parser.php'); // for $MailTo_PReg_Match |
1924 | |
691a2d25 |
1925 | global $attachment_common_show_images, $view_unsafe_images, |
02474e43 |
1926 | $has_unsafe_images; |
376667f7 |
1927 | |
691a2d25 |
1928 | /** |
02474e43 |
1929 | * Don't display attached images in HTML mode. |
1930 | */ |
691a2d25 |
1931 | $attachment_common_show_images = false; |
1932 | $tag_list = Array( |
02474e43 |
1933 | false, |
1934 | "object", |
1935 | "meta", |
1936 | "html", |
1937 | "head", |
1938 | "base", |
1939 | "link", |
1940 | "frame", |
1941 | "iframe", |
1942 | "plaintext", |
1943 | "marquee" |
1944 | ); |
691a2d25 |
1945 | |
1946 | $rm_tags_with_content = Array( |
02474e43 |
1947 | "script", |
1948 | "applet", |
1949 | "embed", |
1950 | "title", |
1951 | "frameset", |
1952 | "xml" |
1953 | ); |
691a2d25 |
1954 | |
1955 | $self_closing_tags = Array( |
1956 | "img", |
1957 | "br", |
1958 | "hr", |
ff940ebc |
1959 | "input", |
1960 | "outbind" |
691a2d25 |
1961 | ); |
1962 | |
2dd879b8 |
1963 | $force_tag_closing = true; |
691a2d25 |
1964 | |
1965 | $rm_attnames = Array( |
02474e43 |
1966 | "/.*/" => |
1967 | Array( |
1968 | "/target/i", |
1969 | "/^on.*/i", |
1970 | "/^dynsrc/i", |
1971 | "/^data.*/i", |
1972 | "/^lowsrc.*/i" |
1973 | ) |
1974 | ); |
691a2d25 |
1975 | |
1976 | $secremoveimg = "../images/" . _("sec_remove_eng.png"); |
1977 | $bad_attvals = Array( |
1978 | "/.*/" => |
1979 | Array( |
0a6ec9b5 |
1980 | "/^src|background/i" => |
691a2d25 |
1981 | Array( |
02474e43 |
1982 | Array( |
f83c60a2 |
1983 | "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si", |
1984 | "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si", |
1985 | "/^([\'\"])\s*about\s*:.*([\'\"])/si" |
691a2d25 |
1986 | ), |
02474e43 |
1987 | Array( |
691a2d25 |
1988 | "\\1$secremoveimg\\2", |
bb8d0799 |
1989 | "\\1$secremoveimg\\2", |
3d8371be |
1990 | "\\1$secremoveimg\\2", |
1991 | "\\1$secremoveimg\\2" |
691a2d25 |
1992 | ) |
1993 | ), |
0a6ec9b5 |
1994 | "/^href|action/i" => |
1995 | Array( |
02474e43 |
1996 | Array( |
f83c60a2 |
1997 | "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si", |
1998 | "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si", |
1999 | "/^([\'\"])\s*about\s*:.*([\'\"])/si" |
0a6ec9b5 |
2000 | ), |
02474e43 |
2001 | Array( |
2dd879b8 |
2002 | "\\1#\\1", |
2003 | "\\1#\\1", |
2004 | "\\1#\\1", |
2005 | "\\1#\\1" |
0a6ec9b5 |
2006 | ) |
2007 | ), |
f83c60a2 |
2008 | "/^style/i" => |
691a2d25 |
2009 | Array( |
02474e43 |
2010 | Array( |
f83c60a2 |
2011 | "/expression/i", |
2012 | "/binding/i", |
2013 | "/behaviou*r/i", |
2dd879b8 |
2014 | "/include-source/i", |
4eacb6ed |
2015 | "/position\s*:\s*absolute/i", |
7d06541f |
2016 | "/url\s*\(\s*([\'\"])\s*\S+script\s*:.*([\'\"])\s*\)/si", |
2017 | "/url\s*\(\s*([\'\"])\s*mocha\s*:.*([\'\"])\s*\)/si", |
af861a34 |
2018 | "/url\s*\(\s*([\'\"])\s*about\s*:.*([\'\"])\s*\)/si", |
2019 | "/(.*)\s*:\s*url\s*\(\s*([\'\"]*)\s*\S+script\s*:.*([\'\"]*)\s*\)/si" |
02474e43 |
2020 | ), |
2021 | Array( |
bb8d0799 |
2022 | "idiocy", |
3d8371be |
2023 | "idiocy", |
2024 | "idiocy", |
2dd879b8 |
2025 | "idiocy", |
4eacb6ed |
2026 | "", |
2dd879b8 |
2027 | "url(\\1#\\1)", |
2028 | "url(\\1#\\1)", |
2029 | "url(\\1#\\1)", |
af861a34 |
2030 | "url(\\1#\\1)", |
2031 | "\\1:url(\\2#\\3)" |
02474e43 |
2032 | ) |
2033 | ) |
691a2d25 |
2034 | ) |
2035 | ); |
5262d9a6 |
2036 | if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) { |
2dd879b8 |
2037 | $view_unsafe_images = false; |
45071bd6 |
2038 | } |
691a2d25 |
2039 | if (!$view_unsafe_images){ |
2040 | /** |
02474e43 |
2041 | * Remove any references to http/https if view_unsafe_images set |
2042 | * to false. |
2043 | */ |
2044 | array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[0], |
f83c60a2 |
2045 | '/^([\'\"])\s*https*:.*([\'\"])/si'); |
02474e43 |
2046 | array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[1], |
2dd879b8 |
2047 | "\\1$secremoveimg\\1"); |
02474e43 |
2048 | array_push($bad_attvals{'/.*/'}{'/^style/i'}[0], |
43d678b6 |
2049 | '/url\(([\'\"])\s*https*:.*([\'\"])\)/si'); |
02474e43 |
2050 | array_push($bad_attvals{'/.*/'}{'/^style/i'}[1], |
2dd879b8 |
2051 | "url(\\1$secremoveimg\\1)"); |
691a2d25 |
2052 | } |
451f74a2 |
2053 | |
691a2d25 |
2054 | $add_attr_to_tag = Array( |
da2415c1 |
2055 | "/^a$/i" => |
c25f2fbb |
2056 | Array('target'=>'"_blank"', |
02474e43 |
2057 | 'title'=>'"'._("This external link will open in a new window").'"' |
2dd879b8 |
2058 | ) |
2059 | ); |
da2415c1 |
2060 | $trusted = sq_sanitize($body, |
02474e43 |
2061 | $tag_list, |
2062 | $rm_tags_with_content, |
2063 | $self_closing_tags, |
2064 | $force_tag_closing, |
2065 | $rm_attnames, |
2066 | $bad_attvals, |
2067 | $add_attr_to_tag, |
2068 | $message, |
2069 | $id, |
2070 | $mailbox |
2071 | ); |
f83c60a2 |
2072 | if (preg_match("|$secremoveimg|i", $trusted)){ |
691a2d25 |
2073 | $has_unsafe_images = true; |
da2415c1 |
2074 | } |
ebeb8348 |
2075 | |
2076 | |
376667f7 |
2077 | // we want to parse mailto's in HTML output, change to SM compose links |
2078 | // this is a modified version of code from url_parser.php... but Marc is |
2079 | // right: we need a better filtering implementation; adding this randomly |
2080 | // here is not a great solution |
2081 | // |
1814c39b |
2082 | if ($take_mailto_links) { |
2083 | // parseUrl($trusted); // this even parses URLs inside of tags... too aggressive |
2084 | global $MailTo_PReg_Match; |
2085 | $MailTo_PReg_Match = '/mailto:' . substr($MailTo_PReg_Match, 1); |
2086 | if ((preg_match_all($MailTo_PReg_Match, $trusted, $regs)) && ($regs[0][0] != '')) { |
2087 | foreach ($regs[0] as $i => $mailto_before) { |
2088 | $mailto_params = $regs[10][$i]; |
2089 | |
2090 | // get rid of any tailing quote since we have to add send_to to the end |
2091 | // |
2092 | if (substr($mailto_before, strlen($mailto_before) - 1) == '"') |
2093 | $mailto_before = substr($mailto_before, 0, strlen($mailto_before) - 1); |
2094 | if (substr($mailto_params, strlen($mailto_params) - 1) == '"') |
2095 | $mailto_params = substr($mailto_params, 0, strlen($mailto_params) - 1); |
2096 | |
2097 | if ($regs[1][$i]) { //if there is an email addr before '?', we need to merge it with the params |
2098 | $to = 'to=' . $regs[1][$i]; |
2099 | if (strpos($mailto_params, 'to=') > -1) //already a 'to=' |
2100 | $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params); |
2101 | else { |
2102 | if ($mailto_params) //already some params, append to them |
2103 | $mailto_params .= '&' . $to; |
2104 | else |
2105 | $mailto_params .= '?' . $to; |
2106 | } |
376667f7 |
2107 | } |
1814c39b |
2108 | |
2109 | $url_str = preg_replace(array('/to=/i', '/(?<!b)cc=/i', '/bcc=/i'), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params); |
2110 | |
2111 | // we'll already have target=_blank, no need to allow comp_in_new |
2112 | // here (which would be a lot more work anyway) |
2113 | // |
2114 | global $compose_new_win; |
2115 | $temp_comp_in_new = $compose_new_win; |
2116 | $compose_new_win = 0; |
2117 | $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before); |
2118 | $compose_new_win = $temp_comp_in_new; |
2119 | |
2120 | // remove <a href=" and anything after the next quote (we only |
2121 | // need the uri, not the link HTML) in compose uri |
2122 | // |
2123 | $comp_uri = substr($comp_uri, 9); |
2124 | $comp_uri = substr($comp_uri, 0, strpos($comp_uri, '"', 1)); |
2125 | $trusted = str_replace($mailto_before, $comp_uri, $trusted); |
376667f7 |
2126 | } |
376667f7 |
2127 | } |
2128 | } |
ebeb8348 |
2129 | |
691a2d25 |
2130 | return $trusted; |
451f74a2 |
2131 | } |
a4a70693 |
2132 | |
da2415c1 |
2133 | /** |
02474e43 |
2134 | * function SendDownloadHeaders - send file to the browser |
2135 | * |
2136 | * Original Source: SM core src/download.php |
2137 | * moved here to make it available to other code, and separate |
2138 | * front end from back end functionality. |
2139 | * |
2140 | * @param string $type0 first half of mime type |
2141 | * @param string $type1 second half of mime type |
2142 | * @param string $filename filename to tell the browser for downloaded file |
2143 | * @param boolean $force whether to force the download dialog to pop |
2144 | * @param optional integer $filesize send the Content-Header and length to the browser |
2145 | * @return void |
2146 | */ |
2147 | function SendDownloadHeaders($type0, $type1, $filename, $force, $filesize=0) { |
2148 | global $languages, $squirrelmail_language; |
2149 | $isIE = $isIE6 = 0; |
2150 | |
2151 | sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER); |
2152 | |
2153 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
2154 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
2155 | $isIE = 1; |
2156 | } |
2157 | |
2158 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false && |
2159 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
2160 | $isIE6 = 1; |
2161 | } |
2162 | |
2163 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
2164 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename')) { |
2165 | $filename = |
2166 | call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename', $filename, $HTTP_USER_AGENT); |
2167 | } else { |
2168 | $filename = ereg_replace('[\\/:\*\?"<>\|;]', '_', str_replace(' ', ' ', $filename)); |
2169 | } |
2170 | |
2171 | // A Pox on Microsoft and it's Internet Explorer! |
2172 | // |
2173 | // IE has lots of bugs with file downloads. |
2174 | // It also has problems with SSL. Both of these cause problems |
2175 | // for us in this function. |
2176 | // |
2177 | // See this article on Cache Control headers and SSL |
2178 | // http://support.microsoft.com/default.aspx?scid=kb;en-us;323308 |
2179 | // |
2180 | // The best thing you can do for IE is to upgrade to the latest |
2181 | // version |
2182 | //set all the Cache Control Headers for IE |
2183 | if ($isIE) { |
2184 | $filename=rawurlencode($filename); |
2185 | header ("Pragma: public"); |
2186 | header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); # HTTP/1.1 |
2187 | header ("Cache-Control: post-check=0, pre-check=0", false); |
2188 | header ("Cache-control: private"); |
2189 | |
2190 | //set the inline header for IE, we'll add the attachment header later if we need it |
2191 | header ("Content-Disposition: inline; filename=$filename"); |
2192 | } |
2193 | |
2194 | if (!$force) { |
2195 | // Try to show in browser window |
2196 | header ("Content-Disposition: inline; filename=\"$filename\""); |
2197 | header ("Content-Type: $type0/$type1; name=\"$filename\""); |
2198 | } else { |
2199 | // Try to pop up the "save as" box |
2200 | |
2201 | // IE makes this hard. It pops up 2 save boxes, or none. |
2202 | // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP |
2203 | // http://support.microsoft.com/default.aspx?scid=kb;EN-US;260519 |
2204 | // But, according to Microsoft, it is "RFC compliant but doesn't |
2205 | // take into account some deviations that allowed within the |
2206 | // specification." Doesn't that mean RFC non-compliant? |
2207 | // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP |
2208 | |
2209 | // all browsers need the application/octet-stream header for this |
2210 | header ("Content-Type: application/octet-stream; name=\"$filename\""); |
2211 | |
2212 | // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp |
2213 | // Do not have quotes around filename, but that applied to |
2214 | // "attachment"... does it apply to inline too? |
2215 | header ("Content-Disposition: attachment; filename=\"$filename\""); |
2216 | |
2217 | if ($isIE && !$isIE6) { |
2218 | // This combination seems to work mostly. IE 5.5 SP 1 has |
2219 | // known issues (see the Microsoft Knowledge Base) |
2220 | |
2221 | // This works for most types, but doesn't work with Word files |
2222 | header ("Content-Type: application/download; name=\"$filename\""); |
2223 | |
2224 | // These are spares, just in case. :-) |
2225 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
2226 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
2227 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
2228 | } else { |
2229 | // another application/octet-stream forces download for Netscape |
2230 | header ("Content-Type: application/octet-stream; name=\"$filename\""); |
2231 | } |
2232 | } |
2233 | |
2234 | //send the content-length header if the calling function provides it |
2235 | if ($filesize > 0) { |
2236 | header("Content-Length: $filesize"); |
2237 | } |
07c49f57 |
2238 | |
8d863f64 |
2239 | } // end fn SendDownloadHeaders |
da2415c1 |
2240 | |
da1b55ad |
2241 | ?> |