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