be8ee42ae3c775ca86c5cab940f1d52d9a128291
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Handles attachment downloads to the users computer.
10 * Also allows displaying of attachments when possible.
15 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
21 header('Cache-Control: cache');
23 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) {
24 global $where, $what, $charset;
27 displayPageHeader($color, 'None');
29 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
31 _("Viewing a text attachment") . " - ";
32 if ($where && $what) {
34 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
36 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
39 $urlmailbox = urlencode($mailbox);
40 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">".
41 _("Download this as a file").
45 "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
46 "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
48 if ($type1 == 'html') {
49 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
50 $body = MagicHTML( $body, $id, $msg );
52 translateText($body, $wrap_at, $charset);
57 "</TT></TD></TR></TABLE>";
60 function viewMessage($imapConnection, $id, $mailbox, $ent_id, $color, $wrap_at, $extracted) {
64 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
65 $msg = getEntity($msg, $ent_id);
67 $header = sqimap_get_ent_header($imapConnection,$id,$mailbox,$ent_id);
69 $msg->header
= $header;
71 $ent_ar = findDisplayEntity($msg, 0);
73 for ($i = 0; $i < count($ent_ar); $i++
) {
74 $body .= formatBody($imapConnection, $msg, $color, $wrap_at, $ent_ar[$i], false);
77 $bodyheader = viewHeader($header, $color);
78 displayPageHeader($color, 'None');
80 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>";
82 echo '<TR><TD width="100%"><center><h1>Message succesfully extracted</h1></center></TD></TR>';
84 echo "<TR><TD BGCOLOR=\"$color[0]\">".
85 "<B><CENTER>". _("Viewing a message attachment") . " - ";
87 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
89 $urlmailbox = urlencode($mailbox);
91 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_id&mailbox=$urlmailbox\">".
92 _("Download this as a file").
96 echo "<TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
97 "<TR><TD BGCOLOR=\"$color[4]\">";
98 echo "$bodyheader </TD></TR></TABLE>";
100 echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
101 "<TR><TD BGCOLOR=\"$color[4]\"><TT><BR>";
102 echo "$body </TT></TD></TR><table><br>";
103 echo '<table width="100%"><tr>'.
104 "<td bgcolor=\"$color[9]\" width=\"100%\" align=\"center\">".
105 '<form action="download.php" method="post"><small>'.
106 "<input type=\"hidden\" name=\"passed_id\" value=\"$id\">".
107 "<input type=\"hidden\" name=\"mailbox\" value=\"".$mailbox."\">".
108 "<input type=\"hidden\" name=\"startMessage\" value=\"$startMessage\">".
109 "<input type=\"hidden\" name=\"passed_ent_id\" value=\"$ent_id\">".
110 "<input type=\"hidden\" name=\"extract_message\" value=\"1\">".
112 ' <select name="targetMailbox">';
113 get_extract_to_target_list($imapConnection);
114 echo '</select> '.' '.
115 '<input type="submit" value="' . _("Extract") . '">'.
122 function get_extract_to_target_list($imapConnection) {
124 $boxes = sqimap_mailbox_list($imapConnection);
125 for ($i = 0; $i < count($boxes); $i++
) {
126 if (!in_array('noselect', $boxes[$i]['flags'])) {
127 $box = $boxes[$i]['unformatted'];
128 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
129 if ( $box2 == 'INBOX' ) {
132 echo "<option value=\"$box\">$box2</option>\n";
138 function viewHeader($header,$color) {
142 /** FORMAT THE FROM STRING **/
143 $from_name = decodeHeader(htmlspecialchars($header->from
));
144 if(isset($from_name) && $from_name !='') {
145 $bodyheader .= makeTableEntry($from_name,_("From"), $color);
148 $subject_string = decodeHeader(htmlspecialchars($header->subject
));
149 if(isset($subject_string) && $subject_string !='') {
150 $bodyheader .= makeTableEntry($subject_string,_("Subject:"), $color);
152 /** FORMAT THE TO STRING **/
153 $to = formatRecipientString($header->to
, "to");
154 $to_string = $to['str'];
155 $url_to_string = $to['url_str'];
156 if(isset($to_string) && $to_string !='') {
157 $bodyheader .= makeTableEntry($to_string,_("To:"), $color);
160 /** FORMAT THE DATE STRING **/
161 $dateString = getLongDateString($header->date
);
162 if(isset($dateString) && $dateString !='') {
163 $bodyheader .= makeTableEntry($dateString,_("Date:"), $color);
166 /** FORMAT THE CC STRING **/
167 $cc = formatRecipientString($header->cc
, "cc");
168 $cc_string = $cc['str'];
169 $url_cc_string = $cc['url_str'];
170 if(isset($cc_string) && $cc_string !='') {
171 $bodyheader .= makeTableEntry($cc_string,_("Cc:"), $color);
174 /** FORMAT THE BCC STRING **/
175 $bcc = formatRecipientString($header->bcc
, "bcc");
176 $bcc_string = $bcc['str'];
177 $url_bcc_string = $bcc['url_str'];
178 if(isset($bcc_string) && $bcc_string !='') {
179 $bodyheader .= makeTableEntry($bcc_string,_("Bcc:"), $color);
185 function makeTableEntry($str, $str_name, $color) {
186 $entry = '<tr><td bgcolor="'."$color[0]".'" align right valign top>'."$str_name".'</td><td bgcolor="'."$color[0]".
187 '" valign top colspan=2><b>'."$str".'</b> </td></tr>'."\n";
191 function formatRecipientString($recipients, $item ) {
192 global $base_uri, $passed_id, $startMessage, $show_more_cc, $show_more, $show_more_bcc, $passed_ent_id;
193 global $where, $what, $mailbox, $sort;
195 /** TEXT STRINGS DEFINITIONS **/
196 $echo_more = _("more");
197 $echo_less = _("less");
199 if (!isset($show_more_cc)) {
200 $show_more_cc = FALSE;
202 if (!isset($show_more_bcc)) {
203 $show_more_bcc = FALSE;
207 $urlMailbox = urlencode($mailbox);
211 if (isset ($recipients[0]) && trim($recipients[0])) {
213 $ary = explode(",",$recipients[0]);
217 $show = "&show_more=1&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
218 $show_n = "&show_more=0&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
221 $show = "&show_more=$show_more&show_more_cc=1&show_more_bcc=$show_more_bcc";
222 $show_n = "&show_more=$show_more&show_more_cc=0&show_more_bcc=$show_more_bcc";
223 $show_more = $show_more_cc;
226 $show = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=1";
227 $show_n = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=0";
228 $show_more = $show_more_bcc;
234 while ($i < count($ary)) {
235 $ary[$i] = htmlspecialchars(decodeHeader($ary[$i]));
236 $url_string .= $ary[$i];
238 $string = "$string<BR>$ary[$i]";
240 $string = "$ary[$i]";
244 if (count($ary) > 1) {
245 if ($show_more == false) {
248 $string .= ' (<A HREF="' . $base_uri .
249 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
250 if (isset($where) && isset($what)) {
251 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
253 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
257 } else if ($i == 1) {
259 $string .= ' (<A HREF="' . $base_uri .
260 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
261 if (isset($where) && isset($what)) {
262 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
264 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
274 $url_string = urlencode($url_string);
276 $result['str'] = $string;
277 $result['url_str'] = $url_string;
283 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
284 sqimap_mailbox_select($imapConnection, $mailbox);
287 if (isset($extract_message) && $extract_message) {
288 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
289 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message);
293 for ($i=1;$i<$cnt;$i++
) {
294 $length = $length +
strlen($read[$i]);
297 if (isset($targetMailbox) && $length>0) {
298 sqimap_append ($imapConnection, $targetMailbox, $length);
299 fputs($imapConnection,$body);
300 sqimap_append_done ($imapConnection);
305 if (isset($showHeaders)) {
306 $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
309 * lets redefine message as this particular entity that we wish to display.
310 * it should hold only the header for this entity. We need to fetch the body
311 * yet before we can display anything.
314 $header = sqimap_get_mime_ent_header ($imapConnection, $passed_id, $mailbox, $passed_ent_id);
315 $header->entity_id
= $passed_ent_id;
316 $header->mailbox
= $mailbox;
318 $charset = $header->charset
;
319 $type0 = $header->type0
;
320 $type1 = $header->type1
;
321 if (isset($override_type0)) {
322 $type0 = $override_type0;
324 if (isset($override_type1)) {
325 $type1 = $override_type1;
327 $filename = decodeHeader($header->filename
);
329 $filename = decodeHeader($header->name
);
332 if (strlen($filename) < 1) {
333 if ($type1 == 'plain' && $type0 == 'text') {
335 } else if ($type1 == 'richtext' && $type0 == 'text') {
337 } else if ($type1 == 'postscript' && $type0 == 'application') {
339 } else if ($type1 == 'rfc822' && $type0 == 'message') {
345 $filename = "untitled$passed_ent_id.$suffix";
351 * The following sections display the attachment in different
352 * ways depending on how they choose. The first way will download
353 * under any circumstance. This sets the Content-type to be
354 * applicatin/octet-stream, which should be interpreted by the
355 * browser as "download me".
356 * The second method (view) is used for images or other formats
357 * that should be able to be handled by the browser. It will
358 * most likely display the attachment inline inside the browser.
359 * And finally, the third one will be used by default. If it
360 * is displayable (text or html), it will load them up in a text
361 * viewer (built in to squirrelmail). Otherwise, it sets the
362 * content-type as application/octet-stream
364 if (isset($absolute_dl) && $absolute_dl == 'true') {
367 DumpHeaders($type0, $type1, $filename, 1);
368 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
369 $body = decodeBody($body, $header->encoding
);
370 if ($type1 == 'plain' && isset($showHeaders)) {
371 echo _("Subject") . ": " . decodeHeader($top_header->subject
) . "\n".
372 " " . _("From") . ": " . decodeHeader($top_header->from
) . "\n".
373 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to
)) . "\n".
374 " " . _("Date") . ": " . getLongDateString($top_header->date
) . "\n\n";
375 } elseif ($type1 == 'html' && isset($showHeaders)) {
376 echo '<table><tr><th align=right>' . _("Subject").
377 ':</th><td>' . decodeHeader($top_header->subject
).
378 "</td></tr>\n<tr><th align=right>" . _("From").
379 ':</th><td>' . decodeHeader($top_header->from
).
380 "</td></tr>\n<tr><th align=right>" . _("To").
381 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to
)).
382 "</td></tr>\n<tr><th align=right>" . _("Date").
383 ':</th><td>' . getLongDateString($top_header->date
).
384 "</td></tr>\n</table>\n<hr>\n";
390 DumpHeaders($type0, $type1, $filename, 1);
391 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
397 if ($type1 == 'plain' ||
$type1 == 'html') {
398 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
399 $body = decodeBody($body, $header->encoding
);
400 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
402 DumpHeaders($type0, $type1, $filename, 0);
403 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
404 $body = decodeBody($body, $header->encoding
);
409 if ($type1 == 'rfc822' ) {
410 viewMessage($imapConnection, $passed_id, $mailbox, $passed_ent_id, $color, $wrap_at, $extracted);
412 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
413 $body = decodeBody($body, $msgheader->encoding
);
414 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
418 DumpHeaders($type0, $type1, $filename, 0);
419 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
426 * This function is verified to work with Netscape and the *very latest*
427 * version of IE. I don't know if it works with Opera, but it should now.
429 function DumpHeaders($type0, $type1, $filename, $force) {
430 global $HTTP_USER_AGENT;
434 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
435 strstr($HTTP_USER_AGENT, 'Opera') === false) {
439 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
440 strstr($HTTP_USER_AGENT, 'Opera') === false) {
444 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
446 // A Pox on Microsoft and it's Office!
448 // Try to show in browser window
449 header("Content-Disposition: inline; filename=\"$filename\"");
450 header("Content-Type: $type0/$type1; name=\"$filename\"");
452 // Try to pop up the "save as" box
453 // IE makes this hard. It pops up 2 save boxes, or none.
454 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
455 // But, accordint to Microsoft, it is "RFC compliant but doesn't
456 // take into account some deviations that allowed within the
457 // specification." Doesn't that mean RFC non-compliant?
458 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
460 // The best thing you can do for IE is to upgrade to the latest
462 if ($isIE && !isset($isIE6)) {
463 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
464 // Do not have quotes around filename, but that applied to
465 // "attachment"... does it apply to inline too?
467 // This combination seems to work mostly. IE 5.5 SP 1 has
468 // known issues (see the Microsoft Knowledge Base)
469 header("Content-Disposition: inline; filename=$filename");
471 // This works for most types, but doesn't work with Word files
472 header("Content-Type: application/download; name=\"$filename\"");
474 // These are spares, just in case. :-)
475 //header("Content-Type: $type0/$type1; name=\"$filename\"");
476 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
477 //header("Content-Type: application/octet-stream; name=\"$filename\"");
479 header("Content-Disposition: attachment; filename=\"$filename\"");
480 // application/octet-stream forces download for Netscape
481 header("Content-Type: application/octet-stream; name=\"$filename\"");