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 = '', |
27 | $entity_id = 0, $message_id = 0, $name = '', $priority = 3; |
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)); |
71 | $read = trim(substr ($read, 0, -1)); |
72 | $end = mime_match_parenthesis(0, $read); |
73 | while ($end == strlen($read)-1) { |
74 | $read = trim(substr ($read, 0, -1)); |
75 | $read = trim(substr ($read, 1)); |
76 | $end = mime_match_parenthesis(0, $read); |
77 | } |
77b88425 |
78 | |
451f74a2 |
79 | $msg = mime_parse_structure ($read, 0); |
80 | $msg->header = $header; |
77b88425 |
81 | |
451f74a2 |
82 | return( $msg ); |
83 | } |
b74ba498 |
84 | |
451f74a2 |
85 | /* this starts the parsing of a particular structure. It is called recursively, |
86 | * so it can be passed different structures. It returns an object of type |
87 | * $message. |
88 | * First, it checks to see if it is a multipart message. If it is, then it |
89 | * handles that as it sees is necessary. If it is just a regular entity, |
90 | * then it parses it and adds the necessary header information (by calling out |
91 | * to mime_get_elements() |
92 | */ |
93 | function mime_parse_structure ($structure, $ent_id) { |
94 | |
95 | $msg = new message(); |
96 | if ($structure{0} == '(') { |
97 | $ent_id = mime_new_element_level($ent_id); |
98 | $start = $end = -1; |
99 | do { |
100 | $start = $end+1; |
101 | $end = mime_match_parenthesis ($start, $structure); |
102 | |
103 | $element = substr($structure, $start+1, ($end - $start)-1); |
104 | $ent_id = mime_increment_id ($ent_id); |
105 | $newmsg = mime_parse_structure ($element, $ent_id); |
106 | $msg->addEntity ($newmsg); |
107 | } while ($structure{$end+1} == '('); |
108 | } else { |
109 | // parse the elements |
110 | $msg = mime_get_element ($structure, $msg, $ent_id); |
111 | } |
112 | return $msg; |
113 | } |
e4a256af |
114 | |
451f74a2 |
115 | /* Increments the element ID. An element id can look like any of |
116 | * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments |
117 | * the last number of the element id, changing 1.2 to 1.3. |
118 | */ |
119 | function mime_increment_id ($id) { |
120 | |
77b88425 |
121 | if (strpos($id, '.')) { |
122 | $first = substr($id, 0, strrpos($id, '.')); |
123 | $last = substr($id, strrpos($id, '.')+1); |
451f74a2 |
124 | $last++; |
77b88425 |
125 | $new = $first . '.' .$last; |
451f74a2 |
126 | } else { |
127 | $new = $id + 1; |
128 | } |
77b88425 |
129 | |
451f74a2 |
130 | return $new; |
131 | } |
132 | |
133 | /* |
134 | * See comment for mime_increment_id(). |
135 | * This adds another level on to the entity_id changing 1.3 to 1.3.0 |
136 | * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented |
137 | * before it can be used. I left it this way so as not to have |
138 | * to make a special case if it is the first entity_id. It |
139 | * always increments it, and that works fine. |
140 | */ |
141 | function mime_new_element_level ($id) { |
142 | |
77b88425 |
143 | if (!$id) { |
144 | $id = 0; |
145 | } else { |
146 | $id = $id . '.0'; |
147 | } |
451f74a2 |
148 | |
77b88425 |
149 | return( $id ); |
451f74a2 |
150 | } |
151 | |
152 | function mime_get_element (&$structure, $msg, $ent_id) { |
153 | |
154 | $elem_num = 1; |
155 | $msg->header = new msg_header(); |
156 | $msg->header->entity_id = $ent_id; |
157 | $properties = array(); |
158 | |
159 | while (strlen($structure) > 0) { |
160 | $structure = trim($structure); |
161 | $char = $structure{0}; |
162 | |
163 | if (strtolower(substr($structure, 0, 3)) == 'nil') { |
164 | $text = ''; |
165 | $structure = substr($structure, 3); |
166 | } else if ($char == '"') { |
167 | // loop through until we find the matching quote, and return that as a string |
168 | $pos = 1; |
169 | $text = ''; |
170 | while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) { |
171 | $text .= $char; |
172 | $pos++; |
173 | } |
174 | $structure = substr($structure, strlen($text) + 2); |
175 | } else if ($char == '(') { |
176 | // comment me |
177 | $end = mime_match_parenthesis (0, $structure); |
178 | $sub = substr($structure, 1, $end-1); |
179 | $properties = mime_get_props($properties, $sub); |
180 | $structure = substr($structure, strlen($sub) + 2); |
181 | } else { |
182 | // loop through until we find a space or an end parenthesis |
183 | $pos = 0; |
184 | $char = $structure{$pos}; |
185 | $text = ''; |
186 | while ($char != ' ' && $char != ')' && $pos < strlen($structure)) { |
187 | $text .= $char; |
188 | $pos++; |
189 | $char = $structure{$pos}; |
190 | } |
191 | $structure = substr($structure, strlen($text)); |
192 | } |
193 | |
194 | // This is where all the text parts get put into the header |
195 | switch ($elem_num) { |
196 | case 1: |
197 | $msg->header->type0 = strtolower($text); |
198 | break; |
199 | case 2: |
200 | $msg->header->type1 = strtolower($text); |
201 | break; |
202 | case 4: // Id |
203 | // Invisimail enclose images with <> |
204 | $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) ); |
205 | break; |
206 | case 5: |
207 | $msg->header->description = $text; |
208 | break; |
209 | case 6: |
210 | $msg->header->encoding = strtolower($text); |
211 | break; |
212 | case 7: |
213 | $msg->header->size = $text; |
214 | break; |
215 | default: |
216 | if ($msg->header->type0 == 'text' && $elem_num == 8) { |
217 | // This is a plain text message, so lets get the number of lines |
218 | // that it contains. |
219 | $msg->header->num_lines = $text; |
220 | |
221 | } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) { |
222 | // This is an encapsulated message, so lets start all over again and |
223 | // parse this message adding it on to the existing one. |
224 | $structure = trim($structure); |
225 | if ( $structure{0} == '(' ) { |
226 | $e = mime_match_parenthesis (0, $structure); |
227 | $structure = substr($structure, 0, $e); |
228 | $structure = substr($structure, 1); |
229 | $m = mime_parse_structure($structure, $msg->header->entity_id); |
230 | |
231 | // the following conditional is there to correct a bug that wasn't |
232 | // incrementing the entity IDs correctly because of the special case |
233 | // that message/rfc822 is. This fixes it fine. |
234 | if (substr($structure, 1, 1) != '(') |
235 | $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id)); |
236 | |
237 | // Now we'll go through and reformat the results. |
238 | if ($m->entities) { |
239 | for ($i=0; $i < count($m->entities); $i++) { |
240 | $msg->addEntity($m->entities[$i]); |
241 | } |
242 | } else { |
243 | $msg->addEntity($m); |
244 | } |
245 | $structure = ""; |
246 | } |
247 | } |
248 | break; |
249 | } |
250 | $elem_num++; |
251 | $text = ""; |
252 | } |
253 | // loop through the additional properties and put those in the various headers |
f0c4dc12 |
254 | // if ($msg->header->type0 != 'message') { |
451f74a2 |
255 | for ($i=0; $i < count($properties); $i++) { |
256 | $msg->header->{$properties[$i]['name']} = $properties[$i]['value']; |
257 | } |
f0c4dc12 |
258 | // } |
451f74a2 |
259 | |
260 | return $msg; |
261 | } |
262 | |
263 | /* |
264 | * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't |
265 | * figure out how to do this part, so I decided to go to bed. I woke up |
266 | * in the morning and had a flash of insight. I went to the white-board |
267 | * and scribbled it out, then spent a bit programming it, and this is the |
268 | * result. Nothing complicated, but I think my brain was fried yesterday. |
269 | * Funny how that happens some times. |
270 | * |
271 | * This gets properties in a nested parenthesisized list. For example, |
272 | * this would get passed something like: ("attachment" ("filename" "luke.tar.gz")) |
273 | * This returns an array called $props with all paired up properties. |
274 | * It ignores the "attachment" for now, maybe that should change later |
275 | * down the road. In this case, what is returned is: |
276 | * $props[0]["name"] = "filename"; |
277 | * $props[0]["value"] = "luke.tar.gz"; |
278 | */ |
279 | function mime_get_props ($props, $structure) { |
280 | |
281 | while (strlen($structure) > 0) { |
282 | $structure = trim($structure); |
283 | $char = $structure{0}; |
284 | |
285 | if ($char == '"') { |
286 | $pos = 1; |
287 | $tmp = ''; |
288 | while ( ( $char = $structure{$pos} ) != '"' && |
289 | $pos < strlen($structure)) { |
290 | $tmp .= $char; |
291 | $pos++; |
292 | } |
293 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
294 | $char = $structure{0}; |
295 | |
296 | if ($char == '"') { |
297 | $pos = 1; |
298 | $value = ''; |
299 | while ( ( $char = $structure{$pos} ) != '"' && |
300 | $pos < strlen($structure) ) { |
301 | $value .= $char; |
302 | $pos++; |
303 | } |
304 | $structure = trim(substr($structure, strlen($tmp) + 2)); |
305 | |
306 | $k = count($props); |
307 | $props[$k]['name'] = strtolower($tmp); |
308 | $props[$k]['value'] = $value; |
309 | } else if ($char == '(') { |
310 | $end = mime_match_parenthesis (0, $structure); |
311 | $sub = substr($structure, 1, $end-1); |
312 | if (! isset($props)) |
313 | $props = array(); |
314 | $props = mime_get_props($props, $sub); |
315 | $structure = substr($structure, strlen($sub) + 2); |
316 | } |
317 | return $props; |
318 | } else if ($char == '(') { |
319 | $end = mime_match_parenthesis (0, $structure); |
320 | $sub = substr($structure, 1, $end-1); |
321 | $props = mime_get_props($props, $sub); |
322 | $structure = substr($structure, strlen($sub) + 2); |
323 | return $props; |
324 | } else { |
325 | return $props; |
326 | } |
327 | } |
328 | } |
329 | |
330 | /* |
331 | * Matches parenthesis. It will return the position of the matching |
332 | * parenthesis in $structure. For instance, if $structure was: |
333 | * ("text" "plain" ("val1name", "1") nil ... ) |
334 | * x x |
335 | * then this would return 42 to match up those two. |
336 | */ |
337 | function mime_match_parenthesis ($pos, $structure) { |
338 | |
339 | $j = strlen( $structure ); |
340 | |
341 | // ignore all extra characters |
342 | // If inside of a string, skip string -- Boundary IDs and other |
343 | // things can have ) in them. |
344 | if ( $structure{$pos} != '(' ) { |
345 | return( $j ); |
346 | } |
347 | |
348 | while ( $pos < $j ) { |
349 | $pos++; |
350 | if ($structure{$pos} == ')') { |
8beafbbc |
351 | return $pos; |
451f74a2 |
352 | } elseif ($structure{$pos} == '"') { |
b74ba498 |
353 | $pos++; |
451f74a2 |
354 | while ( $structure{$pos} != '"' && |
355 | $pos < $j ) { |
356 | if (substr($structure, $pos, 2) == '\\"') { |
b74ba498 |
357 | $pos++; |
451f74a2 |
358 | } elseif (substr($structure, $pos, 2) == '\\\\') { |
b74ba498 |
359 | $pos++; |
451f74a2 |
360 | } |
b74ba498 |
361 | $pos++; |
5ffe5a7e |
362 | } |
451f74a2 |
363 | } elseif ( $structure{$pos} == '(' ) { |
8beafbbc |
364 | $pos = mime_match_parenthesis ($pos, $structure); |
451f74a2 |
365 | } |
366 | } |
367 | echo _("Error decoding mime structure. Report this as a bug!") . '<br>'; |
368 | return( $pos ); |
369 | } |
370 | |
346817d4 |
371 | function mime_fetch_body($imap_stream, $id, $ent_id ) { |
09a4bde3 |
372 | |
373 | /* |
374 | * do a bit of error correction. If we couldn't find the entity id, just guess |
375 | * that it is the first one. That is usually the case anyway. |
376 | */ |
377 | if (!$ent_id) { |
451f74a2 |
378 | $ent_id = 1; |
09a4bde3 |
379 | } |
346817d4 |
380 | |
6ab1bd9e |
381 | $cmd = "FETCH $id BODY[$ent_id]"; |
346817d4 |
382 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message); |
77b88425 |
383 | |
384 | do { |
e976319c |
385 | $topline = trim(array_shift( $data )); |
386 | } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ; |
451f74a2 |
387 | $wholemessage = implode('', $data); |
388 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
77b88425 |
389 | |
451f74a2 |
390 | $ret = substr( $wholemessage, 0, $regs[1] ); |
391 | /* |
392 | There is some information in the content info header that could be important |
393 | in order to parse html messages. Let's get them here. |
394 | */ |
395 | if ( $ret{0} == '<' ) { |
1c72b151 |
396 | $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message); |
e5ea9327 |
397 | /* BASE within HTML documents is illegal (see w3 spec) |
398 | * $base = ''; |
399 | * $k = 10; |
400 | * foreach( $data as $d ) { |
401 | * if ( substr( $d, 0, 13 ) == 'Content-Base:' ) { |
402 | * $j = strlen( $d ); |
403 | * $i = 13; |
404 | * $base = ''; |
405 | * while ( $i < $j && |
406 | * ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) ) |
407 | * $i++; |
408 | * while ( $i < $j ) { |
409 | * if ( isNoSep( $d{$i} ) ) |
410 | * $base .= $d{$i}; |
411 | * $i++; |
412 | * } |
413 | * $k = 0; |
414 | * } elseif ( $k == 1 && !isnosep( $d{0} ) ) { |
415 | * $base .= substr( $d, 1 ); |
416 | * } |
417 | * $k++; |
418 | * } |
419 | * if ( $base <> '' ) { |
420 | * $ret = "<base href=\"$base\">" . $ret; |
421 | * } |
7e235a1a |
422 | * */ |
451f74a2 |
423 | } |
424 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
425 | $ret = $regs[1]; |
426 | } else { |
427 | global $where, $what, $mailbox, $passed_id, $startMessage; |
e5ea9327 |
428 | $par = 'mailbox=' . urlencode($mailbox) . "&passed_id=$passed_id"; |
451f74a2 |
429 | if (isset($where) && isset($what)) { |
e5ea9327 |
430 | $par .= '&where='. urlencode($where) . "&what=" . urlencode($what); |
a3daaaf3 |
431 | } else { |
e5ea9327 |
432 | $par .= "&startMessage=$startMessage&show_more=0"; |
451f74a2 |
433 | } |
e5ea9327 |
434 | $par .= '&response=' . urlencode($response) . |
435 | '&message=' . urlencode($message). |
436 | '&topline=' . urlencode($topline); |
a019eeb8 |
437 | |
346817d4 |
438 | echo '<tt><br>' . |
439 | '<table width="80%"><tr>' . |
440 | '<tr><td colspan=2>' . |
451f74a2 |
441 | _("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 |
442 | " <A HREF=\"../src/retrievalerror.php?$par\"><br>" . |
443 | _("Submit message") . '</A><BR> ' . |
444 | '</td></tr>' . |
445 | '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
446 | '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
447 | '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
448 | '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
449 | "</table><BR></tt></font><hr>"; |
450 | |
1c72b151 |
451 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message); |
451f74a2 |
452 | array_shift($data); |
453 | $wholemessage = implode('', $data); |
a019eeb8 |
454 | |
346817d4 |
455 | $ret = $wholemessage; |
a3daaaf3 |
456 | } |
451f74a2 |
457 | return( $ret ); |
458 | } |
d4467150 |
459 | |
451f74a2 |
460 | function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { |
461 | // do a bit of error correction. If we couldn't find the entity id, just guess |
462 | // that it is the first one. That is usually the case anyway. |
463 | if (!$ent_id) { |
464 | $ent_id = 1; |
465 | } |
466 | $sid = sqimap_session_id(); |
467 | // Don't kill the connection if the browser is over a dialup |
468 | // and it would take over 30 seconds to download it. |
b7206e1d |
469 | |
470 | // don´t call set_time_limit in safe mode. |
471 | if (!ini_get("safe_mode")) { |
472 | set_time_limit(0); |
473 | } |
346817d4 |
474 | |
451f74a2 |
475 | fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n"); |
476 | $cnt = 0; |
477 | $continue = true; |
478 | $read = fgets ($imap_stream,4096); |
479 | // This could be bad -- if the section has sqimap_session_id() . ' OK' |
480 | // or similar, it will kill the download. |
481 | while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
482 | if (trim($read) == ')==') { |
483 | $read1 = $read; |
b74ba498 |
484 | $read = fgets ($imap_stream,4096); |
451f74a2 |
485 | if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
486 | return; |
487 | } else { |
488 | echo decodeBody($read1, $encoding) . |
489 | decodeBody($read, $encoding); |
490 | } |
491 | } else if ($cnt) { |
492 | echo decodeBody($read, $encoding); |
b74ba498 |
493 | } |
451f74a2 |
494 | $read = fgets ($imap_stream,4096); |
495 | $cnt++; |
496 | } |
497 | } |
beb9e459 |
498 | |
451f74a2 |
499 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
d4467150 |
500 | |
aceb0d5c |
501 | |
d4467150 |
502 | |
451f74a2 |
503 | /* This is the first function called. It decides if this is a multipart |
504 | message or if it should be handled as a single entity |
505 | */ |
506 | function decodeMime ($imap_stream, &$header) { |
507 | global $username, $key, $imapServerAddress, $imapPort; |
508 | return mime_structure ($imap_stream, $header); |
509 | } |
ea48eb25 |
510 | |
451f74a2 |
511 | // This is here for debugging purposese. It will print out a list |
512 | // of all the entity IDs that are in the $message object. |
f0c4dc12 |
513 | |
451f74a2 |
514 | function listEntities ($message) { |
515 | if ($message) { |
516 | if ($message->header->entity_id) |
517 | echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>'; |
518 | for ($i = 0; $message->entities[$i]; $i++) { |
519 | $msg = listEntities($message->entities[$i], $ent_id); |
520 | if ($msg) |
521 | return $msg; |
522 | } |
523 | } |
524 | } |
f0c4dc12 |
525 | |
451f74a2 |
526 | |
527 | /* returns a $message object for a particular entity id */ |
528 | function getEntity ($message, $ent_id) { |
529 | if ($message) { |
530 | if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) { |
8beafbbc |
531 | return $message; |
451f74a2 |
532 | } else { |
cd928157 |
533 | for ($i = 0; isset($message->entities[$i]); $i++) { |
451f74a2 |
534 | $msg = getEntity ($message->entities[$i], $ent_id); |
535 | if ($msg) { |
536 | return $msg; |
537 | } |
b1dadc61 |
538 | } |
451f74a2 |
539 | } |
540 | } |
541 | } |
8beafbbc |
542 | |
451f74a2 |
543 | /* |
544 | * figures out what entity to display and returns the $message object |
545 | * for that entity. |
546 | */ |
547 | function findDisplayEntity ($message, $textOnly = 1) { |
548 | global $show_html_default; |
549 | |
550 | $entity = 0; |
551 | |
552 | if ($message) { |
553 | if ( $message->header->type0 == 'multipart' && |
554 | ( $message->header->type1 == 'alternative' || |
f0c4dc12 |
555 | $message->header->type1 == 'mixed' || |
451f74a2 |
556 | $message->header->type1 == 'related' ) && |
557 | $show_html_default && ! $textOnly ) { |
558 | $entity = findDisplayEntityHTML($message); |
559 | } |
f0c4dc12 |
560 | |
451f74a2 |
561 | // Show text/plain or text/html -- the first one we find. |
562 | if ( $entity == 0 && |
563 | $message->header->type0 == 'text' && |
564 | ( $message->header->type1 == 'plain' || |
565 | $message->header->type1 == 'html' ) && |
566 | isset($message->header->entity_id) ) { |
567 | $entity = $message->header->entity_id; |
568 | } |
569 | |
570 | $i = 0; |
571 | while ($entity == 0 && isset($message->entities[$i]) ) { |
572 | $entity = findDisplayEntity($message->entities[$i], $textOnly); |
573 | $i++; |
a3daaaf3 |
574 | } |
a3daaaf3 |
575 | } |
451f74a2 |
576 | |
577 | return( $entity ); |
578 | } |
b74ba498 |
579 | |
451f74a2 |
580 | /* Shows the HTML version */ |
581 | function findDisplayEntityHTML ($message) { |
8405ee35 |
582 | |
451f74a2 |
583 | if ( $message->header->type0 == 'text' && |
584 | $message->header->type1 == 'html' && |
585 | isset($message->header->entity_id)) { |
586 | return $message->header->entity_id; |
587 | } |
588 | for ($i = 0; isset($message->entities[$i]); $i ++) { |
f0c4dc12 |
589 | if ( $message->header->type0 == 'message' && |
590 | $message->header->type1 == 'rfc822' && |
591 | isset($message->header->entity_id)) { |
592 | return 0; |
593 | } |
451f74a2 |
594 | $entity = findDisplayEntityHTML($message->entities[$i]); |
595 | if ($entity != 0) { |
596 | return $entity; |
597 | } |
598 | } |
599 | |
600 | return 0; |
601 | } |
602 | |
da4c66e8 |
603 | /* |
604 | * translateText |
605 | * Extracted from strings.php 23/03/2002 |
606 | */ |
607 | |
608 | function translateText(&$body, $wrap_at, $charset) { |
609 | global $where, $what; /* from searching */ |
610 | global $color; /* color theme */ |
611 | |
612 | require_once('../functions/url_parser.php'); |
613 | |
614 | $body_ary = explode("\n", $body); |
615 | $PriorQuotes = 0; |
616 | for ($i=0; $i < count($body_ary); $i++) { |
617 | $line = $body_ary[$i]; |
618 | if (strlen($line) - 2 >= $wrap_at) { |
619 | sqWordWrap($line, $wrap_at); |
620 | } |
621 | $line = charset_decode($charset, $line); |
622 | $line = str_replace("\t", ' ', $line); |
623 | |
624 | parseUrl ($line); |
625 | |
626 | $Quotes = 0; |
627 | $pos = 0; |
628 | $j = strlen( $line ); |
629 | |
630 | while ( $pos < $j ) { |
631 | if ($line[$pos] == ' ') { |
632 | $pos ++; |
633 | } else if (strpos($line, '>', $pos) === $pos) { |
634 | $pos += 4; |
635 | $Quotes ++; |
636 | } else { |
637 | break; |
638 | } |
639 | } |
640 | |
641 | if ($Quotes > 1) { |
642 | if (! isset($color[14])) { |
643 | $color[14] = '#FF0000'; |
644 | } |
645 | $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>'; |
646 | } elseif ($Quotes) { |
647 | if (! isset($color[13])) { |
648 | $color[13] = '#800000'; |
649 | } |
650 | $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>'; |
651 | } |
652 | |
653 | $body_ary[$i] = $line; |
654 | } |
655 | $body = '<pre>' . implode("\n", $body_ary) . '</pre>'; |
656 | } |
657 | |
451f74a2 |
658 | /* This returns a parsed string called $body. That string can then |
659 | be displayed as the actual message in the HTML. It contains |
660 | everything needed, including HTML Tags, Attachments at the |
661 | bottom, etc. |
662 | */ |
663 | function formatBody($imap_stream, $message, $color, $wrap_at) { |
664 | // this if statement checks for the entity to show as the |
665 | // primary message. To add more of them, just put them in the |
666 | // order that is their priority. |
e198531f |
667 | global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body, |
d03c24f4 |
668 | $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort; |
669 | |
670 | $has_unsafe_images = 0; |
7e235a1a |
671 | |
451f74a2 |
672 | $id = $message->header->id; |
f0c4dc12 |
673 | |
451f74a2 |
674 | $urlmailbox = urlencode($message->header->mailbox); |
7e235a1a |
675 | |
451f74a2 |
676 | // Get the right entity and redefine message to be this entity |
677 | // Pass the 0 to mean that we want the 'best' viewable one |
678 | $ent_num = findDisplayEntity ($message, 0); |
679 | $body_message = getEntity($message, $ent_num); |
f0c4dc12 |
680 | |
451f74a2 |
681 | if (($body_message->header->type0 == 'text') || |
682 | ($body_message->header->type0 == 'rfc822')) { |
451f74a2 |
683 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
684 | $body = decodeBody($body, $body_message->header->encoding); |
685 | $hookResults = do_hook("message_body", $body); |
686 | $body = $hookResults[1]; |
7e235a1a |
687 | |
451f74a2 |
688 | // If there are other types that shouldn't be formatted, add |
689 | // them here |
690 | if ($body_message->header->type1 == 'html') { |
691 | if ( $show_html_default <> 1 ) { |
a3daaaf3 |
692 | $body = strip_tags( $body ); |
693 | translateText($body, $wrap_at, $body_message->header->charset); |
694 | } else { |
695 | $body = MagicHTML( $body, $id ); |
696 | } |
451f74a2 |
697 | } else { |
9eea179c |
698 | translateText($body, $wrap_at, $body_message->header->charset); |
451f74a2 |
699 | } |
793cc001 |
700 | |
e5ea9327 |
701 | $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 |
702 | if ($has_unsafe_images) { |
703 | if ($view_unsafe_images) { |
3d1c7564 |
704 | $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 |
705 | } else { |
3d1c7564 |
706 | $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 |
707 | } |
7e235a1a |
708 | } |
77b88425 |
709 | |
451f74a2 |
710 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
451f74a2 |
711 | if (isset($message->entities[0])) { |
712 | $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id); |
713 | } |
451f74a2 |
714 | } else { |
715 | $body = formatAttachments ($message, -1, $message->header->mailbox, $id); |
716 | } |
717 | return ($body); |
718 | } |
b74ba498 |
719 | |
451f74a2 |
720 | /* |
721 | * A recursive function that returns a list of attachments with links |
722 | * to where to download these attachments |
723 | */ |
77b88425 |
724 | function formatAttachments($message, $ent_id, $mailbox, $id) { |
725 | global $where, $what; |
726 | global $startMessage, $color; |
727 | static $ShownHTML = 0; |
451f74a2 |
728 | |
77b88425 |
729 | $body = ''; |
730 | if ($ShownHTML == 0) { |
451f74a2 |
731 | |
77b88425 |
732 | $ShownHTML = 1; |
451f74a2 |
733 | $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" . |
77b88425 |
734 | "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" . |
735 | _("Attachments") . ':' . |
736 | "</B></TH></TR><TR><TD>\n" . |
737 | "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" . |
738 | formatAttachments($message, $ent_id, $mailbox, $id) . |
739 | "</TABLE></TD></TR></TABLE>"; |
740 | |
741 | } else if ($message) { |
f0c4dc12 |
742 | $header = $message->header; |
743 | $type0 = strtolower($header->type0); |
744 | $type1 = strtolower($header->type1); |
745 | $name = decodeHeader($header->name); |
746 | |
747 | if ($type0 =='message' && $type1 = 'rfc822') { |
748 | |
749 | $filename = decodeHeader($message->header->filename); |
750 | if (trim($filename) == '') { |
751 | if (trim($name) == '') { |
752 | $display_filename = 'untitled-[' . $message->header->entity_id . ']' ; |
753 | } else { |
754 | $display_filename = $name; |
755 | $filename = $name; |
756 | } |
757 | } else { |
758 | $display_filename = $filename; |
759 | } |
760 | |
761 | $urlMailbox = urlencode($mailbox); |
762 | $ent = urlencode($message->header->entity_id); |
763 | |
764 | $DefaultLink = |
765 | "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
766 | if ($where && $what) { |
767 | $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
768 | } |
769 | $Links['download link']['text'] = _("download"); |
770 | $Links['download link']['href'] = |
771 | "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
772 | $ImageURL = ''; |
773 | |
774 | /* this executes the attachment hook with a specific MIME-type. |
775 | * if that doens't have results, it tries if there's a rule |
776 | * for a more generic type. */ |
777 | $HookResults = do_hook("attachment $type0/$type1", $Links, |
778 | $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what); |
779 | if(count($HookResults[1]) <= 1) { |
780 | $HookResults = do_hook("attachment $type0/*", $Links, |
781 | $startMessage, $id, $urlMailbox, $ent, $DefaultLink, |
782 | $display_filename, $where, $what); |
783 | } |
784 | |
785 | $Links = $HookResults[1]; |
786 | $DefaultLink = $HookResults[6]; |
787 | |
788 | $body .= '<TR><TD> </TD><TD>' . |
789 | "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" . |
790 | '<TD><SMALL><b>' . show_readable_size($message->header->size) . |
791 | '</b> </small></TD>' . |
792 | "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" . |
793 | '<TD><SMALL>'; |
794 | if ($message->header->description) { |
795 | $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>'; |
796 | } |
797 | $body .= '</SMALL></TD><TD><SMALL> '; |
798 | |
799 | |
800 | $SkipSpaces = 1; |
801 | foreach ($Links as $Val) { |
802 | if ($SkipSpaces) { |
803 | $SkipSpaces = 0; |
804 | } else { |
805 | $body .= ' | '; |
806 | } |
807 | $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>'; |
808 | } |
809 | |
810 | unset($Links); |
77b88425 |
811 | |
f0c4dc12 |
812 | $body .= "</SMALL></TD></TR>\n"; |
813 | |
814 | return( $body ); |
815 | |
816 | } elseif (!$message->entities) { |
77b88425 |
817 | |
818 | $type0 = strtolower($message->header->type0); |
819 | $type1 = strtolower($message->header->type1); |
820 | $name = decodeHeader($message->header->name); |
821 | |
822 | if ($message->header->entity_id != $ent_id) { |
823 | $filename = decodeHeader($message->header->filename); |
824 | if (trim($filename) == '') { |
825 | if (trim($name) == '') { |
826 | if ( trim( $message->header->id ) == '' ) |
827 | $display_filename = 'untitled-[' . $message->header->entity_id . ']' ; |
828 | else |
829 | $display_filename = 'cid: ' . $message->header->id; |
830 | // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ; |
831 | } else { |
832 | $display_filename = $name; |
833 | $filename = $name; |
834 | } |
835 | } else { |
836 | $display_filename = $filename; |
837 | } |
451f74a2 |
838 | |
77b88425 |
839 | $urlMailbox = urlencode($mailbox); |
840 | $ent = urlencode($message->header->entity_id); |
451f74a2 |
841 | |
77b88425 |
842 | $DefaultLink = |
e5ea9327 |
843 | "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
77b88425 |
844 | if ($where && $what) { |
e5ea9327 |
845 | $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
77b88425 |
846 | } |
847 | $Links['download link']['text'] = _("download"); |
848 | $Links['download link']['href'] = |
e5ea9327 |
849 | "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent"; |
77b88425 |
850 | $ImageURL = ''; |
851 | |
852 | /* this executes the attachment hook with a specific MIME-type. |
853 | * if that doens't have results, it tries if there's a rule |
854 | * for a more generic type. */ |
855 | $HookResults = do_hook("attachment $type0/$type1", $Links, |
856 | $startMessage, $id, $urlMailbox, $ent, $DefaultLink, |
857 | $display_filename, $where, $what); |
858 | if(count($HookResults[1]) <= 1) { |
859 | $HookResults = do_hook("attachment $type0/*", $Links, |
860 | $startMessage, $id, $urlMailbox, $ent, $DefaultLink, |
861 | $display_filename, $where, $what); |
862 | } |
451f74a2 |
863 | |
77b88425 |
864 | $Links = $HookResults[1]; |
865 | $DefaultLink = $HookResults[6]; |
866 | |
867 | $body .= '<TR><TD> </TD><TD>' . |
868 | "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" . |
869 | '<TD><SMALL><b>' . show_readable_size($message->header->size) . |
870 | '</b> </small></TD>' . |
871 | "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" . |
872 | '<TD><SMALL>'; |
873 | if ($message->header->description) { |
874 | $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>'; |
875 | } |
876 | $body .= '</SMALL></TD><TD><SMALL> '; |
b74ba498 |
877 | |
b74ba498 |
878 | |
77b88425 |
879 | $SkipSpaces = 1; |
880 | foreach ($Links as $Val) { |
881 | if ($SkipSpaces) { |
882 | $SkipSpaces = 0; |
883 | } else { |
884 | $body .= ' | '; |
885 | } |
886 | $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>'; |
887 | } |
888 | |
889 | unset($Links); |
890 | |
891 | $body .= "</SMALL></TD></TR>\n"; |
892 | } |
893 | } else { |
894 | for ($i = 0; $i < count($message->entities); $i++) { |
895 | $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id); |
896 | } |
451f74a2 |
897 | } |
77b88425 |
898 | } |
899 | return( $body ); |
451f74a2 |
900 | } |
b74ba498 |
901 | |
b74ba498 |
902 | |
451f74a2 |
903 | /** this function decodes the body depending on the encoding type. **/ |
904 | function decodeBody($body, $encoding) { |
905 | $body = str_replace("\r\n", "\n", $body); |
906 | $encoding = strtolower($encoding); |
b74ba498 |
907 | |
451f74a2 |
908 | global $show_html_default; |
4809f489 |
909 | |
451f74a2 |
910 | if ($encoding == 'quoted-printable') { |
911 | $body = quoted_printable_decode($body); |
4809f489 |
912 | |
7831268e |
913 | |
451f74a2 |
914 | while (ereg("=\n", $body)) |
915 | $body = ereg_replace ("=\n", "", $body); |
358f007e |
916 | |
451f74a2 |
917 | } else if ($encoding == 'base64') { |
918 | $body = base64_decode($body); |
919 | } |
b74ba498 |
920 | |
451f74a2 |
921 | // All other encodings are returned raw. |
922 | return $body; |
923 | } |
924 | |
925 | /* |
926 | * This functions decode strings that is encoded according to |
927 | * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
928 | */ |
929 | function decodeHeader ($string, $utfencode=true) { |
18aa168e |
930 | |
931 | if ( is_array( $string ) ) { |
932 | $string = implode("\n", $string ); |
933 | } |
934 | |
935 | if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', |
451f74a2 |
936 | $string, $res)) { |
18aa168e |
937 | if (ucfirst($res[2]) == 'B') { |
451f74a2 |
938 | $replace = base64_decode($res[3]); |
18aa168e |
939 | } else { |
0f8a1ce9 |
940 | $replace = str_replace('_', ' ', $res[3]); |
18aa168e |
941 | // Convert lowercase Quoted Printable to uppercase for |
942 | // quoted_printable_decode to understand it. |
943 | while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", |
944 | $replace, $res)) { |
945 | $replace = str_replace($res[1], strtoupper($res[1]), $replace); |
946 | } |
451f74a2 |
947 | $replace = quoted_printable_decode($replace); |
18aa168e |
948 | } |
949 | /* Only encode into entities by default. Some places |
451f74a2 |
950 | don't need the encoding, like the compose form. */ |
18aa168e |
951 | if ($utfencode){ |
952 | $replace = charset_decode ($res[1], $replace); |
953 | } |
954 | |
955 | // Remove the name of the character set. |
956 | $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', |
957 | $replace, $string); |
451f74a2 |
958 | |
18aa168e |
959 | // In case there should be more encoding in the string: recurse |
960 | $string = decodeHeader($string); |
961 | } |
451f74a2 |
962 | |
18aa168e |
963 | return ($string); |
451f74a2 |
964 | } |
965 | |
966 | /* |
967 | * Encode a string according to RFC 1522 for use in headers if it |
968 | * contains 8-bit characters or anything that looks like it should |
969 | * be encoded. |
970 | */ |
971 | function encodeHeader ($string) { |
972 | global $default_charset; |
793cc001 |
973 | |
451f74a2 |
974 | // Encode only if the string contains 8-bit characters or =? |
975 | $j = strlen( $string ); |
976 | $l = strstr($string, '=?'); // Must be encoded ? |
977 | $ret = ''; |
978 | for( $i=0; $i < $j; ++$i) { |
f7b3ba37 |
979 | switch( $string{$i} ) { |
980 | case '=': |
b74ba498 |
981 | $ret .= '=3D'; |
982 | break; |
451f74a2 |
983 | case '?': |
b74ba498 |
984 | $ret .= '=3F'; |
985 | break; |
451f74a2 |
986 | case '_': |
b74ba498 |
987 | $ret .= '=5F'; |
988 | break; |
451f74a2 |
989 | case ' ': |
b74ba498 |
990 | $ret .= '_'; |
991 | break; |
451f74a2 |
992 | default: |
b74ba498 |
993 | $k = ord( $string{$i} ); |
451f74a2 |
994 | if ( $k > 126 ) { |
b74ba498 |
995 | $ret .= sprintf("=%02X", $k); |
996 | $l = TRUE; |
997 | } else |
998 | $ret .= $string{$i}; |
f7b3ba37 |
999 | } |
451f74a2 |
1000 | } |
793cc001 |
1001 | |
451f74a2 |
1002 | if ( $l ) { |
f7b3ba37 |
1003 | $string = "=?$default_charset?Q?$ret?="; |
451f74a2 |
1004 | } |
346817d4 |
1005 | |
451f74a2 |
1006 | return( $string ); |
1007 | } |
b74ba498 |
1008 | |
451f74a2 |
1009 | /* |
a3daaaf3 |
1010 | Strips dangerous tags from html messages. |
451f74a2 |
1011 | */ |
1012 | function MagicHTML( $body, $id ) { |
1013 | |
793cc001 |
1014 | global $message, $HTTP_SERVER_VARS, |
5106a9be |
1015 | $attachment_common_show_images; |
451f74a2 |
1016 | |
5106a9be |
1017 | $attachment_common_show_images = |
1018 | FALSE; // Don't display attached images in HTML mode |
451f74a2 |
1019 | $j = strlen( $body ); // Legnth of the HTML |
1020 | $ret = ''; // Returned string |
1021 | $bgcolor = '#ffffff'; // Background style color (defaults to white) |
1022 | $textcolor = '#000000'; // Foreground style color (defaults to black) |
1023 | $leftmargin = ''; // Left margin style |
1024 | $title = ''; // HTML title if any |
1025 | |
1026 | $i = 0; |
1027 | while ( $i < $j ) { |
1028 | if ( $body{$i} == '<' ) { |
1029 | $pos = $i + 1; |
1030 | $tag = ''; |
1031 | while ($body{$pos} == ' ' || $body{$pos} == "\t" || |
793cc001 |
1032 | $body{$pos} == "\n" ) { |
451f74a2 |
1033 | $pos ++; |
1034 | } |
1035 | while (strlen($tag) < 4 && $body{$pos} != ' ' && |
793cc001 |
1036 | $body{$pos} != "\t" && $body{$pos} != "\n" && |
1037 | $pos < $j ) { |
451f74a2 |
1038 | $tag .= $body{$pos}; |
1039 | $pos ++; |
1040 | } |
cedad33a |
1041 | /* |
1042 | A comment in HTML is only three characters and isn't |
1043 | guaranteed to have a space after it. This fudges so |
1044 | it will be caught by the switch statement. |
1045 | */ |
1046 | if (ereg("!--", $tag)) { |
1047 | $tag = "!-- "; |
1048 | } |
451f74a2 |
1049 | switch( strtoupper( $tag ) ) { |
1050 | // Strips the entire tag and contents |
1051 | case 'APPL': |
cedad33a |
1052 | case 'EMBE': |
451f74a2 |
1053 | case 'FRAM': |
1054 | case 'SCRI': |
1055 | case 'OBJE': |
1056 | $etg = '/' . $tag; |
1057 | while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg && |
1058 | $i < $j ) $i++; |
1059 | while ( $i < $j && $body{++$i} <> '>' ); |
1060 | // $ret .= "<!-- $tag removed -->"; |
1061 | break; |
1062 | // Substitute Title |
1063 | case 'TITL': |
1064 | $i += 5; |
1065 | while ( $body{$i} <> '>' && // </title> |
1066 | $i < $j ) |
a3daaaf3 |
1067 | $i++; |
451f74a2 |
1068 | $i++; |
1069 | $title = ''; |
1070 | while ( $body{$i} <> '<' && // </title> |
1071 | $i < $j ) { |
1072 | $title .= $body{$i}; |
1073 | $i++; |
1074 | } |
1075 | $i += 7; |
1076 | break; |
1077 | // Destroy these tags |
1078 | case 'HTML': |
1079 | case 'HEAD': |
1080 | case '/HTM': |
1081 | case '/HEA': |
1082 | case '!DOC': |
1083 | case 'META': |
89863d1d |
1084 | //case 'DIV ': |
1085 | //case '/DIV': |
451f74a2 |
1086 | case '!-- ': |
1087 | $i += 4; |
1088 | while ( $body{$i} <> '>' && |
1089 | $i < $j ) |
1090 | $i++; |
1091 | // $i++; |
1092 | break; |
1093 | case 'STYL': |
1094 | $i += 5; |
1095 | while ( $body{$i} <> '>' && // </title> |
1096 | $i < $j ) |
1097 | $i++; |
1098 | $i++; |
1099 | // We parse the style to look for interesting stuff |
1100 | $styleblk = ''; |
1101 | while ( $body{$i} <> '>' && |
1102 | $i < $j ) { |
1103 | // First we get the name of the style |
1104 | $style = ''; |
1105 | while ( $body{$i} <> '>' && |
1106 | $body{$i} <> '<' && |
1107 | $body{$i} <> '{' && |
1108 | $i < $j ) { |
1109 | if ( isnoSep( $body{$i} ) ) |
1110 | $style .= $body{$i}; |
1111 | $i++; |
1112 | } |
1113 | stripComments( $i, $j, $body ); |
1114 | $style = strtoupper( trim( $style ) ); |
1115 | if ( $style == 'BODY' ) { |
1116 | // Next we look into the definitions of the body style |
1117 | while ( $body{$i} <> '>' && |
1118 | $body{$i} <> '}' && |
a3daaaf3 |
1119 | $i < $j ) { |
451f74a2 |
1120 | // We look for the background color if any. |
1121 | if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) { |
1122 | $i += 17; |
1123 | $bgcolor = getStyleData( $i, $j, $body ); |
1124 | } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) { |
1125 | $i += 12; |
1126 | $leftmargin = getStyleData( $i, $j, $body ); |
1127 | } |
a3daaaf3 |
1128 | $i++; |
1129 | } |
451f74a2 |
1130 | } else { |
1131 | // Other style are mantained |
1132 | $styleblk .= "$style "; |
1133 | while ( $body{$i} <> '>' && |
1134 | $body{$i} <> '<' && |
1135 | $body{$i} <> '}' && |
1136 | $i < $j ) { |
1137 | $styleblk .= $body{$i}; |
a3daaaf3 |
1138 | $i++; |
451f74a2 |
1139 | } |
1140 | $styleblk .= $body{$i}; |
1141 | } |
1142 | stripComments( $i, $j, $body ); |
1143 | if ( $body{$i} <> '>' ) |
1144 | $i++; |
1145 | } |
1146 | if ( $styleblk <> '' ) |
1147 | $ret .= "<style>$styleblk"; |
1148 | break; |
1149 | case 'BODY': |
1150 | if ( $title <> '' ) |
1151 | $ret .= '<b>' . _("Title:") . " </b>$title<br>\n"; |
1152 | $ret .= "<TABLE"; |
1153 | $i += 5; |
1154 | if (! isset($base)) { |
1155 | $base = ''; |
1156 | } |
1157 | $ret .= stripEvent( $i, $j, $body, $id, $base ); |
1158 | $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>"; |
1159 | if ( $leftmargin <> '' ) |
1160 | $ret .= "<td width=$leftmargin> </td>"; |
1161 | $ret .= '<td>'; |
1162 | if (strtolower($bgcolor) == 'ffffff' || |
1163 | strtolower($bgcolor) == '#ffffff') |
1164 | $ret .= '<font color=#000000>'; |
1165 | break; |
1166 | case 'BASE': |
cedad33a |
1167 | $i += 4; |
451f74a2 |
1168 | $base = ''; |
cedad33a |
1169 | if ( strncasecmp($body{$i}, 'font', 4) ) { |
1170 | $i += 5; |
1171 | while ( !isNoSep( $body{$i} ) && $i < $j ) { |
1172 | $i++; |
1173 | } |
1174 | while ( $body{$i} <> '>' && $i < $j ) { |
1175 | $base .= $body{$i}; |
1176 | $i++; |
1177 | } |
1178 | $ret .= "<BASEFONT $base>\n"; |
1179 | break; |
1180 | } |
1181 | $i++; |
451f74a2 |
1182 | while ( !isNoSep( $body{$i} ) && |
1183 | $i < $j ) { |
1184 | $i++; |
1185 | } |
1186 | if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) { |
a3daaaf3 |
1187 | $i += 5; |
451f74a2 |
1188 | while ( !isNoSep( $body{$i} ) && |
1189 | $i < $j ) { |
a3daaaf3 |
1190 | $i++; |
451f74a2 |
1191 | } |
1192 | while ( $body{$i} <> '>' && |
a3daaaf3 |
1193 | $i < $j ) { |
451f74a2 |
1194 | if ( $body{$i} <> '"' ) { |
1195 | $base .= $body{$i}; |
a3daaaf3 |
1196 | } |
451f74a2 |
1197 | $i++; |
a3daaaf3 |
1198 | } |
451f74a2 |
1199 | // Debuging $ret .= "<!-- base == $base -->"; |
1200 | if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) { |
1201 | $ret .= "\n<BASE HREF=\"$base\">\n"; |
a3daaaf3 |
1202 | } |
a3daaaf3 |
1203 | } |
451f74a2 |
1204 | break; |
1205 | case '/BOD': |
1206 | $ret .= '</font></td></tr></TABLE>'; |
1207 | $i += 6; |
a3daaaf3 |
1208 | break; |
1209 | default: |
451f74a2 |
1210 | // Following tags can contain some event handler, lets search it |
1211 | stripComments( $i, $j, $body ); |
1212 | if (! isset($base)) { |
1213 | $base = ''; |
1214 | } |
1215 | $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>'; |
1216 | // $ret .= "<!-- $tag detected -->"; |
1217 | } |
1218 | } else { |
1219 | $ret .= $body{$i}; |
a3daaaf3 |
1220 | } |
451f74a2 |
1221 | $i++; |
1222 | } |
a3daaaf3 |
1223 | |
451f74a2 |
1224 | return( "\n\n<!-- HTML Output ahead -->\n" . |
1225 | $ret . |
7e235a1a |
1226 | /* Base is illegal within HTML |
451f74a2 |
1227 | "\n<!-- END of HTML Output --><base href=\"". |
494424e3 |
1228 | get_location() . '/'. |
451f74a2 |
1229 | "\">\n\n" ); |
7e235a1a |
1230 | */ |
e5ea9327 |
1231 | "\n<!-- END of HTML Output -->\n\n" ); |
451f74a2 |
1232 | } |
a3daaaf3 |
1233 | |
451f74a2 |
1234 | function isNoSep( $char ) { |
1235 | |
1236 | switch( $char ) { |
1237 | case ' ': |
1238 | case "\n": |
1239 | case "\t": |
1240 | case "\r": |
1241 | case '>': |
1242 | case '"': |
1243 | return( FALSE ); |
1244 | break; |
1245 | default: |
1246 | return( TRUE ); |
1247 | } |
a3daaaf3 |
1248 | |
451f74a2 |
1249 | } |
a3daaaf3 |
1250 | |
451f74a2 |
1251 | /* |
1252 | The following function is usefull to remove extra data that can cause |
1253 | html not to display properly. Especialy with MS stuff. |
1254 | */ |
1255 | |
1256 | function stripComments( &$i, $j, &$body ) { |
1257 | |
ae35570b |
1258 | while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' && |
451f74a2 |
1259 | $i < $j ) { |
1260 | $i += 5; |
1261 | while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' && |
1262 | $i < $j ) |
a3daaaf3 |
1263 | $i++; |
451f74a2 |
1264 | $i++; |
1265 | } |
a3daaaf3 |
1266 | |
451f74a2 |
1267 | return; |
a3daaaf3 |
1268 | |
451f74a2 |
1269 | } |
a3daaaf3 |
1270 | |
451f74a2 |
1271 | /* Gets the style data of a specific style */ |
a3daaaf3 |
1272 | |
451f74a2 |
1273 | function getStyleData( &$i, $j, &$body ) { |
a3daaaf3 |
1274 | |
451f74a2 |
1275 | // We skip spaces |
1276 | while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) && |
1277 | $i < $j ) { |
1278 | $i++; |
1279 | } |
1280 | // And get the color |
1281 | $ret = ''; |
1282 | while ( isNoSep( $body{$i} ) && |
1283 | $i < $j ) { |
1284 | $ret .= $body{$i}; |
1285 | $i++; |
1286 | } |
a3daaaf3 |
1287 | |
451f74a2 |
1288 | return( $ret ); |
1289 | } |
a3daaaf3 |
1290 | |
451f74a2 |
1291 | /* |
1292 | Private function for strip_dangerous_tag. Look for event based coded and "remove" it |
1293 | change on with no (onload -> noload) |
1294 | */ |
a3daaaf3 |
1295 | |
451f74a2 |
1296 | function stripEvent( &$i, $j, &$body, $id, $base ) { |
a3daaaf3 |
1297 | |
d03c24f4 |
1298 | global $message, $base_uri, $has_unsafe_images, $view_unsafe_images; |
a3daaaf3 |
1299 | |
451f74a2 |
1300 | $ret = ''; |
a3daaaf3 |
1301 | |
451f74a2 |
1302 | while ( $body{$i} <> '>' && |
1303 | $i < $j ) { |
1304 | $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2}); |
1305 | switch( $etg ) { |
1306 | case 'src': |
1307 | // This is probably a src specification |
1308 | $k = $i + 3; |
1309 | while( !isNoSep( $body{$k} )) { |
1310 | $k++; |
1311 | } |
1312 | if ( $body{$k} == '=' ) { |
1313 | /* It is indeed */ |
1314 | $k++; |
1315 | while( !isNoSep( $body{$k} ) && |
1316 | $k < $j ) { |
1317 | $k++; |
1318 | } |
1319 | $src = ''; |
1320 | while ( $body{$k} <> '>' && isNoSep( $body{$k} ) && |
1321 | $k < $j ) { |
1322 | $src .= $body{$k}; |
1323 | $k++; |
1324 | } |
f9cabaf8 |
1325 | $k++; |
451f74a2 |
1326 | while( !isNoSep( $body{$k} ) && |
1327 | $k < $j ) { |
1328 | $k++; |
1329 | } |
f9cabaf8 |
1330 | $k++; |
451f74a2 |
1331 | if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) { |
1332 | $src = substr( $src, 4 ); |
e5ea9327 |
1333 | $src = "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" . |
451f74a2 |
1334 | urlencode( $message->header->mailbox ) . |
7e235a1a |
1335 | "&passed_ent_id=" . find_ent_id( $src, $message ); |
1336 | } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' || |
451f74a2 |
1337 | stristr( $src, $base_uri ) ) { |
1338 | /* Javascript and local urls goes out */ |
7e235a1a |
1339 | if (!$view_unsafe_images) { |
d03c24f4 |
1340 | $src = '../images/' . _("sec_remove_eng.png"); |
7e235a1a |
1341 | } |
1342 | $has_unsafe_images = 1; |
451f74a2 |
1343 | } |
1344 | $ret .= 'src="' . $src . '" '; |
89863d1d |
1345 | $i = $k - 2; |
451f74a2 |
1346 | } else { |
1347 | $ret .= 'src'; |
1348 | $i = $i + 3; |
1349 | } |
1350 | |
1351 | break; |
1352 | case '../': |
1353 | // Retrolinks are not allowed without a base because they mess with SM security |
1354 | if ( $base == '' ) { |
a3daaaf3 |
1355 | $i += 2; |
451f74a2 |
1356 | } else { |
1357 | $ret .= '.'; |
a3daaaf3 |
1358 | } |
451f74a2 |
1359 | break; |
1360 | case 'cid': |
1361 | // Internal link |
1362 | $k = $i-1; |
1363 | if ( $body{$i+3} == ':') { |
1364 | $i +=4; |
1365 | $name = ''; |
1366 | while ( isNoSep( $body{$i} ) && |
1367 | $i < $j ) { |
1368 | $name .= $body{$i++}; |
1369 | } |
1370 | if ( $name <> '' ) { |
e5ea9327 |
1371 | $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" . |
451f74a2 |
1372 | urlencode( $message->header->mailbox ) . |
e5ea9327 |
1373 | "&passed_ent_id=" . find_ent_id( $name, $message ); |
451f74a2 |
1374 | if ( $body{$k} == '"' ) |
1375 | $ret .= '" '; |
1376 | else |
1377 | $ret .= ' '; |
1378 | } |
1379 | if ( $body{$i} == '>' ) |
1380 | $i -= 1; |
1381 | } |
1382 | break; |
1383 | case ' on': |
1384 | case "\non": |
1385 | case "\ron": |
1386 | case "\ton": |
1387 | $ret .= ' no'; |
1388 | $i += 2; |
1389 | break; |
1390 | case 'pt:': |
1391 | if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) { |
1392 | $ret .= '_no/'; |
1393 | } else { |
1394 | $ret .= $etg; |
1395 | } |
1396 | $i += 2; |
1397 | break; |
1398 | default: |
1399 | $ret .= $body{$i}; |
a3daaaf3 |
1400 | } |
451f74a2 |
1401 | $i++; |
a3daaaf3 |
1402 | } |
451f74a2 |
1403 | return( $ret ); |
1404 | } |
a3daaaf3 |
1405 | |
1406 | |
451f74a2 |
1407 | /* This function trys to locate the entity_id of a specific mime element */ |
a3daaaf3 |
1408 | |
451f74a2 |
1409 | function find_ent_id( $id, $message ) { |
a3daaaf3 |
1410 | |
451f74a2 |
1411 | $ret = ''; |
1412 | for ($i=0; $ret == '' && $i < count($message->entities); $i++) { |
a3daaaf3 |
1413 | |
451f74a2 |
1414 | if ( $message->entities[$i]->header->entity_id == '' ) { |
1415 | $ret = find_ent_id( $id, $message->entities[$i] ); |
1416 | } else { |
1417 | if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 ) |
1418 | $ret = $message->entities[$i]->header->entity_id; |
a3daaaf3 |
1419 | } |
1420 | |
a3daaaf3 |
1421 | } |
451f74a2 |
1422 | |
1423 | return( $ret ); |
1424 | |
1425 | } |
1c72b151 |
1426 | ?> |