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