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