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