Removed cache-control header
[squirrelmail.git] / src / download.php
1 <?php
2 /**
3 ** download.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Handles attachment downloads to the users computer.
9 ** Also allows displaying of attachments when possible.
10 **/
11
12 if (!isset($config_php))
13 include("../config/config.php");
14 if (!isset($strings_php))
15 include("../functions/strings.php");
16 if (!isset($imap_php))
17 include("../functions/imap.php");
18 if (!isset($mime_php))
19 include("../functions/mime.php");
20 if (!isset($date_php))
21 include("../functions/date.php");
22 if (!isset($i18n_php))
23 include("../functions/i18n.php");
24
25 include("../src/load_prefs.php");
26
27 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
28 global $where, $what, $charset;
29 global $startMessage;
30
31 displayPageHeader($color, "None");
32
33 echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
34 echo "<B><CENTER>";
35 echo _("Viewing a text attachment") . " - ";
36 if ($where && $what) {
37 // from a search
38 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
39 } else {
40 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
41 }
42
43 $urlmailbox = urlencode($mailbox);
44 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">";
45 echo _("Download this as a file");
46 echo "</A></CENTER><BR>";
47 echo "</CENTER></B>";
48 echo "</TD></TR></TABLE>";
49
50 echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
51 echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
52
53 if ($type1 != "html")
54 translateText($body, $wrap_at, $charset);
55
56 echo $body;
57
58 echo "</TT></TD></TR></TABLE>";
59 }
60
61 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
62 sqimap_mailbox_select($imapConnection, $mailbox);
63
64 // $message contains all information about the message
65 // including header and body
66 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
67 $top_header = $message->header;
68
69 // lets redefine message as this particular entity that we wish to display.
70 // it should hold only the header for this entity. We need to fetch the body
71 // yet before we can display anything.
72 $message = getEntity($message, $passed_ent_id);
73
74 $header = $message->header;
75 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
76
77 $charset = $header->charset;
78 $type0 = $header->type0;
79 $type1 = $header->type1;
80 $filename = decodeHeader($header->filename);
81
82 if (strlen($filename) < 1) {
83 if ($type1 == "plain" && $type0 == "text") $suffix = "txt";
84 else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf";
85 else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps";
86 else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg";
87 else $suffix = $type1;
88
89 $filename = "untitled$passed_ent_id.$suffix";
90 }
91
92 // Note:
93 // The following sections display the attachment in different
94 // ways depending on how they choose. The first way will download
95 // under any circumstance. This sets the Content-type to be
96 // applicatin/octet-stream, which should be interpreted by the
97 // browser as "download me".
98 // The second method (view) is used for images or other formats
99 // that should be able to be handled by the browser. It will
100 // most likely display the attachment inline inside the browser.
101 // And finally, the third one will be used by default. If it
102 // is displayable (text or html), it will load them up in a text
103 // viewer (built in to squirrelmail). Otherwise, it sets the
104 // content-type as application/octet-stream
105
106 header("Pragma: ");
107 header("Content-Description: SquirrelMail Attachment");
108 if ($absolute_dl == "true") {
109 switch($type0) {
110 case "text":
111 $body = decodeBody($body, $header->encoding);
112 header("Content-Disposition: attachment; filename=\"$filename\"");
113 header("Content-type: application/octet-stream; name=\"$filename\"");
114 set_up_language(getPref($data_dir, $username, "language"));
115 if ($type1 == "plain") {
116 echo _("Subject") . ": " . decodeHeader(sqStripSlashes($top_header->subject)) . "\n";
117 echo " " . _("From") . ": " . decodeHeader(sqStripSlashes($top_header->from)) . "\n";
118 echo " " . _("To") . ": " . decodeHeader(sqStripSlashes(getLineOfAddrs($top_header->to))) . "\n";
119 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
120 }
121 echo trim($body);
122 break;
123 default:
124 $body = decodeBody($body, $header->encoding);
125 header("Content-Disposition: attachment; filename=\"$filename\"");
126 header("Content-type: application/octet-stream; name=\"$filename\"");
127 echo $body;
128 break;
129 }
130 } else {
131 switch ($type0) {
132 case "text":
133 $body = decodeBody($body, $header->encoding);
134 include("../functions/page_header.php");
135 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
136 break;
137 case "message":
138 $body = decodeBody($body, $header->encoding);
139 include("../functions/page_header.php");
140 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
141 break;
142 default:
143 $body = decodeBody($body, $header->encoding);
144 header("Content-type: $type0/$type1; name=\"$filename\"");
145 header("Content-Disposition: attachment; filename=\"$filename\"");
146 echo $body;
147 break;
148 }
149 }
150
151 sqimap_mailbox_close($imapConnection);
152 sqimap_logout($imapConnection);
153 ?>