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) {
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 $body = MagicHTML( $body, $id );
51 translateText($body, $wrap_at, $charset);
56 "</TT></TD></TR></TABLE>";
59 function viewMessage($imapConnection, $id, $mailbox, $ent_id, $msg, $color, $wrap_at) {
61 $header = sqimap_get_ent_header($imapConnection,$id,$mailbox,$ent_id);
62 $msg->header
= $header;
63 $msg->header
->id
= $id;
64 $body = formatBody($imapConnection, $msg, $color, $wrap_at);
65 $bodyheader = viewHeader($header, $color);
67 displayPageHeader($color, 'None');
69 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
70 "<B><CENTER>". _("Viewing a message attachment") . " - ";
72 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
74 $urlmailbox = urlencode($mailbox);
76 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\">".
77 _("Download this as a file").
81 echo "<TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
82 "<TR><TD BGCOLOR=\"$color[4]\">";
83 echo "$bodyheader </TD></TR></TABLE>";
85 echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
86 "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
87 echo "$body </TT></TD></TR></TABLE>";
91 function viewHeader($header,$color) {
95 /** FORMAT THE FROM STRING **/
96 $from_name = decodeHeader(htmlspecialchars($header->from
));
97 if(isset($from_name) && $from_name !='') {
98 $bodyheader .= makeTableEntry($from_name,_("From"), $color);
101 $subject_string = decodeHeader(htmlspecialchars($header->subject
));
102 if(isset($subject_string) && $subject_string !='') {
103 $bodyheader .= makeTableEntry($subject_string,_("Subject:"), $color);
105 /** FORMAT THE TO STRING **/
106 $to = formatRecipientString($header->to
, "to");
107 $to_string = $to['str'];
108 $url_to_string = $to['url_str'];
109 if(isset($to_string) && $to_string !='') {
110 $bodyheader .= makeTableEntry($to_string,_("To:"), $color);
113 /** FORMAT THE DATE STRING **/
114 $dateString = getLongDateString($header->date
);
115 if(isset($dateString) && $dateString !='') {
116 $bodyheader .= makeTableEntry($dateString,_("Date:"), $color);
119 /** FORMAT THE CC STRING **/
120 $cc = formatRecipientString($header->cc
, "cc");
121 $cc_string = $cc['str'];
122 $url_cc_string = $cc['url_str'];
123 if(isset($cc_string) && $cc_string !='') {
124 $bodyheader .= makeTableEntry($cc_string,_("Cc:"), $color);
127 /** FORMAT THE BCC STRING **/
128 $bcc = formatRecipientString($header->bcc
, "bcc");
129 $bcc_string = $bcc['str'];
130 $url_bcc_string = $bcc['url_str'];
131 if(isset($bcc_string) && $bcc_string !='') {
132 $bodyheader .= makeTableEntry($bcc_string,_("Bcc:"), $color);
138 function makeTableEntry($str, $str_name, $color) {
139 $entry = '<tr><td bgcolor="'."$color[0]".'" align right valign top>'."$str_name".'</td><td bgcolor="'."$color[0]".
140 '" valign top colspan=2><b>'."$str".'</b> </td></tr>'."\n";
144 function formatRecipientString($recipients, $item ) {
145 global $base_uri, $passed_id, $startMessage, $show_more_cc, $show_more, $show_more_bcc, $passed_ent_id;
146 global $where, $what, $mailbox, $sort;
148 /** TEXT STRINGS DEFINITIONS **/
149 $echo_more = _("more");
150 $echo_less = _("less");
152 if (!isset($show_more_cc)) {
153 $show_more_cc = FALSE;
155 if (!isset($show_more_bcc)) {
156 $show_more_bcc = FALSE;
160 $urlMailbox = urlencode($mailbox);
164 if (isset ($recipients[0]) && trim($recipients[0])) {
166 $ary = explode(",",$recipients[0]);
170 $show = "&show_more=1&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
171 $show_n = "&show_more=0&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
174 $show = "&show_more=$show_more&show_more_cc=1&show_more_bcc=$show_more_bcc";
175 $show_n = "&show_more=$show_more&show_more_cc=0&show_more_bcc=$show_more_bcc";
176 $show_more = $show_more_cc;
179 $show = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=1";
180 $show_n = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=0";
181 $show_more = $show_more_bcc;
187 while ($i < count($ary)) {
188 $ary[$i] = htmlspecialchars(decodeHeader($ary[$i]));
189 $url_string .= $ary[$i];
191 $string = "$string<BR>$ary[$i]";
193 $string = "$ary[$i]";
197 if (count($ary) > 1) {
198 if ($show_more == false) {
201 $string .= ' (<A HREF="' . $base_uri .
202 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
203 if (isset($where) && isset($what)) {
204 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
206 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
210 } else if ($i == 1) {
212 $string .= ' (<A HREF="' . $base_uri .
213 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
214 if (isset($where) && isset($what)) {
215 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
217 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
227 $url_string = urlencode($url_string);
229 $result['str'] = $string;
230 $result['url_str'] = $url_string;
236 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
237 sqimap_mailbox_select($imapConnection, $mailbox);
240 * $message contains all information about the message
241 * including header and body
243 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
245 $top_header = $message->header
;
248 * lets redefine message as this particular entity that we wish to display.
249 * it should hold only the header for this entity. We need to fetch the body
250 * yet before we can display anything.
252 $message = getEntity($message, $passed_ent_id);
254 $header = $message->header
;
256 $charset = $header->charset
;
257 $type0 = $header->type0
;
258 $type1 = $header->type1
;
259 if (isset($override_type0)) {
260 $type0 = $override_type0;
262 if (isset($override_type1)) {
263 $type1 = $override_type1;
265 $filename = decodeHeader($header->filename
);
267 $filename = decodeHeader($header->name
);
270 if (strlen($filename) < 1) {
271 if ($type1 == 'plain' && $type0 == 'text') {
273 } else if ($type1 == 'richtext' && $type0 == 'text') {
275 } else if ($type1 == 'postscript' && $type0 == 'application') {
277 } else if ($type1 == 'rfc822' && $type0 == 'message') {
283 $filename = "untitled$passed_ent_id.$suffix";
289 * The following sections display the attachment in different
290 * ways depending on how they choose. The first way will download
291 * under any circumstance. This sets the Content-type to be
292 * applicatin/octet-stream, which should be interpreted by the
293 * browser as "download me".
294 * The second method (view) is used for images or other formats
295 * that should be able to be handled by the browser. It will
296 * most likely display the attachment inline inside the browser.
297 * And finally, the third one will be used by default. If it
298 * is displayable (text or html), it will load them up in a text
299 * viewer (built in to squirrelmail). Otherwise, it sets the
300 * content-type as application/octet-stream
302 if (isset($absolute_dl) && $absolute_dl == 'true') {
305 DumpHeaders($type0, $type1, $filename, 1);
306 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
307 $body = decodeBody($body, $header->encoding
);
308 if ($type1 == 'plain' && isset($showHeaders)) {
309 echo _("Subject") . ": " . decodeHeader($top_header->subject
) . "\n".
310 " " . _("From") . ": " . decodeHeader($top_header->from
) . "\n".
311 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to
)) . "\n".
312 " " . _("Date") . ": " . getLongDateString($top_header->date
) . "\n\n";
313 } elseif ($type1 == 'html' && isset($showHeaders)) {
314 echo '<table><tr><th align=right>' . _("Subject").
315 ':</th><td>' . decodeHeader($top_header->subject
).
316 "</td></tr>\n<tr><th align=right>" . _("From").
317 ':</th><td>' . decodeHeader($top_header->from
).
318 "</td></tr>\n<tr><th align=right>" . _("To").
319 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to
)).
320 "</td></tr>\n<tr><th align=right>" . _("Date").
321 ':</th><td>' . getLongDateString($top_header->date
).
322 "</td></tr>\n</table>\n<hr>\n";
328 DumpHeaders($type0, $type1, $filename, 1);
329 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
335 if ($type1 == 'plain' ||
$type1 == 'html') {
336 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
337 $body = decodeBody($body, $header->encoding
);
338 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
340 DumpHeaders($type0, $type1, $filename, 0);
341 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
342 $body = decodeBody($body, $header->encoding
);
347 if ($type1 == 'rfc822' ) {
348 viewMessage($imapConnection, $passed_id, $mailbox, $passed_ent_id, $message, $color, $wrap_at);
350 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
351 $body = decodeBody($body, $msgheader->encoding
);
352 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
356 DumpHeaders($type0, $type1, $filename, 0);
357 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
364 * This function is verified to work with Netscape and the *very latest*
365 * version of IE. I don't know if it works with Opera, but it should now.
367 function DumpHeaders($type0, $type1, $filename, $force) {
368 global $HTTP_USER_AGENT;
372 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
373 strstr($HTTP_USER_AGENT, 'Opera') === false) {
377 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
378 strstr($HTTP_USER_AGENT, 'Opera') === false) {
382 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
384 // A Pox on Microsoft and it's Office!
386 // Try to show in browser window
387 header("Content-Disposition: inline; filename=\"$filename\"");
388 header("Content-Type: $type0/$type1; name=\"$filename\"");
390 // Try to pop up the "save as" box
391 // IE makes this hard. It pops up 2 save boxes, or none.
392 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
393 // But, accordint to Microsoft, it is "RFC compliant but doesn't
394 // take into account some deviations that allowed within the
395 // specification." Doesn't that mean RFC non-compliant?
396 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
398 // The best thing you can do for IE is to upgrade to the latest
400 if ($isIE && !isset($isIE6)) {
401 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
402 // Do not have quotes around filename, but that applied to
403 // "attachment"... does it apply to inline too?
405 // This combination seems to work mostly. IE 5.5 SP 1 has
406 // known issues (see the Microsoft Knowledge Base)
407 header("Content-Disposition: inline; filename=$filename");
409 // This works for most types, but doesn't work with Word files
410 header("Content-Type: application/download; name=\"$filename\"");
412 // These are spares, just in case. :-)
413 //header("Content-Type: $type0/$type1; name=\"$filename\"");
414 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
415 //header("Content-Type: application/octet-stream; name=\"$filename\"");
417 header("Content-Disposition: attachment; filename=\"$filename\"");
418 // application/octet-stream forces download for Netscape
419 header("Content-Type: application/octet-stream; name=\"$filename\"");