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, $color, $wrap_at) {
63 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
64 $msg = getEntity($msg, $ent_id);
66 $header = sqimap_get_ent_header($imapConnection,$id,$mailbox,$ent_id);
67 $msg->header
= $header;
68 $msg->header
->id
= $id;
69 $body = formatBody($imapConnection, $msg, $color, $wrap_at);
70 $bodyheader = viewHeader($header, $color);
71 displayPageHeader($color, 'None');
73 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
74 "<B><CENTER>". _("Viewing a message attachment") . " - ";
76 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
78 $urlmailbox = urlencode($mailbox);
80 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\">".
81 _("Download this as a file").
85 echo "<TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
86 "<TR><TD BGCOLOR=\"$color[4]\">";
87 echo "$bodyheader </TD></TR></TABLE>";
89 echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
90 "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
91 echo "$body </TT></TD></TR></TABLE>";
95 function viewHeader($header,$color) {
99 /** FORMAT THE FROM STRING **/
100 $from_name = decodeHeader(htmlspecialchars($header->from
));
101 if(isset($from_name) && $from_name !='') {
102 $bodyheader .= makeTableEntry($from_name,_("From"), $color);
105 $subject_string = decodeHeader(htmlspecialchars($header->subject
));
106 if(isset($subject_string) && $subject_string !='') {
107 $bodyheader .= makeTableEntry($subject_string,_("Subject:"), $color);
109 /** FORMAT THE TO STRING **/
110 $to = formatRecipientString($header->to
, "to");
111 $to_string = $to['str'];
112 $url_to_string = $to['url_str'];
113 if(isset($to_string) && $to_string !='') {
114 $bodyheader .= makeTableEntry($to_string,_("To:"), $color);
117 /** FORMAT THE DATE STRING **/
118 $dateString = getLongDateString($header->date
);
119 if(isset($dateString) && $dateString !='') {
120 $bodyheader .= makeTableEntry($dateString,_("Date:"), $color);
123 /** FORMAT THE CC STRING **/
124 $cc = formatRecipientString($header->cc
, "cc");
125 $cc_string = $cc['str'];
126 $url_cc_string = $cc['url_str'];
127 if(isset($cc_string) && $cc_string !='') {
128 $bodyheader .= makeTableEntry($cc_string,_("Cc:"), $color);
131 /** FORMAT THE BCC STRING **/
132 $bcc = formatRecipientString($header->bcc
, "bcc");
133 $bcc_string = $bcc['str'];
134 $url_bcc_string = $bcc['url_str'];
135 if(isset($bcc_string) && $bcc_string !='') {
136 $bodyheader .= makeTableEntry($bcc_string,_("Bcc:"), $color);
142 function makeTableEntry($str, $str_name, $color) {
143 $entry = '<tr><td bgcolor="'."$color[0]".'" align right valign top>'."$str_name".'</td><td bgcolor="'."$color[0]".
144 '" valign top colspan=2><b>'."$str".'</b> </td></tr>'."\n";
148 function formatRecipientString($recipients, $item ) {
149 global $base_uri, $passed_id, $startMessage, $show_more_cc, $show_more, $show_more_bcc, $passed_ent_id;
150 global $where, $what, $mailbox, $sort;
152 /** TEXT STRINGS DEFINITIONS **/
153 $echo_more = _("more");
154 $echo_less = _("less");
156 if (!isset($show_more_cc)) {
157 $show_more_cc = FALSE;
159 if (!isset($show_more_bcc)) {
160 $show_more_bcc = FALSE;
164 $urlMailbox = urlencode($mailbox);
168 if (isset ($recipients[0]) && trim($recipients[0])) {
170 $ary = explode(",",$recipients[0]);
174 $show = "&show_more=1&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
175 $show_n = "&show_more=0&show_more_cc=$show_more_cc&show_more_bcc=$show_more_bcc";
178 $show = "&show_more=$show_more&show_more_cc=1&show_more_bcc=$show_more_bcc";
179 $show_n = "&show_more=$show_more&show_more_cc=0&show_more_bcc=$show_more_bcc";
180 $show_more = $show_more_cc;
183 $show = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=1";
184 $show_n = "&show_more=$show_more&show_more_cc=$show_more_cc&show_more_bcc=0";
185 $show_more = $show_more_bcc;
191 while ($i < count($ary)) {
192 $ary[$i] = htmlspecialchars(decodeHeader($ary[$i]));
193 $url_string .= $ary[$i];
195 $string = "$string<BR>$ary[$i]";
197 $string = "$ary[$i]";
201 if (count($ary) > 1) {
202 if ($show_more == false) {
205 $string .= ' (<A HREF="' . $base_uri .
206 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
207 if (isset($where) && isset($what)) {
208 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
210 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
214 } else if ($i == 1) {
216 $string .= ' (<A HREF="' . $base_uri .
217 "src/download.php?mailbox=$urlMailbox&passed_id=$passed_id&";
218 if (isset($where) && isset($what)) {
219 $string .= 'what=' . urlencode($what)."&where=".urlencode($where)."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
221 $string .= "sort=$sort&startMessage=$startMessage"."&passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
231 $url_string = urlencode($url_string);
233 $result['str'] = $string;
234 $result['url_str'] = $url_string;
240 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
241 sqimap_mailbox_select($imapConnection, $mailbox);
243 if (isset($showHeaders)) {
244 $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
247 * lets redefine message as this particular entity that we wish to display.
248 * it should hold only the header for this entity. We need to fetch the body
249 * yet before we can display anything.
252 $header = sqimap_get_mime_ent_header ($imapConnection, $passed_id, $mailbox, $passed_ent_id);
253 $header->entity_id
= $passed_ent_id;
254 $header->mailbox
= $mailbox;
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, $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\"");