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