5cf6fb839977022d1536aace664966848ff5a508
[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 ** $Id$
12 **/
13
14 define('download_php', true); // Used for preferences
15
16 include('../src/validate.php');
17 include("../functions/imap.php");
18 include("../functions/mime.php");
19 include("../functions/date.php");
20
21 header("Pragma: ");
22 header("Cache-Control: cache");
23
24 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
25 global $where, $what, $charset;
26 global $startMessage;
27
28 displayPageHeader($color, "None");
29
30 echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
31 echo "<B><CENTER>";
32 echo _("Viewing a text attachment") . " - ";
33 if ($where && $what) {
34 // from a search
35 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
36 } else {
37 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
38 }
39
40 $urlmailbox = urlencode($mailbox);
41 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">";
42 echo _("Download this as a file");
43 echo "</A></CENTER><BR>";
44 echo "</CENTER></B>";
45 echo "</TD></TR></TABLE>";
46
47 echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
48 echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
49
50 if ($type1 != "html")
51 translateText($body, $wrap_at, $charset);
52
53 echo $body;
54
55 echo "</TT></TD></TR></TABLE>";
56 }
57
58 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
59 sqimap_mailbox_select($imapConnection, $mailbox);
60
61 // $message contains all information about the message
62 // including header and body
63 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
64 $top_header = $message->header;
65
66 // lets redefine message as this particular entity that we wish to display.
67 // it should hold only the header for this entity. We need to fetch the body
68 // yet before we can display anything.
69 $message = getEntity($message, $passed_ent_id);
70
71 $header = $message->header;
72
73 $charset = $header->charset;
74 $type0 = $header->type0;
75 $type1 = $header->type1;
76 if (isset($override_type0))
77 $type0 = $override_type0;
78 if (isset($override_type1))
79 $type1 = $override_type1;
80 $filename = decodeHeader($header->filename);
81 if (!$filename) {
82 $filename = decodeHeader($header->name);
83 }
84
85 if (strlen($filename) < 1) {
86 if ($type1 == "plain" && $type0 == "text") $suffix = "txt";
87 else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf";
88 else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps";
89 else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg";
90 else $suffix = $type1;
91
92 $filename = "untitled$passed_ent_id.$suffix";
93 }
94
95 // Note:
96 // The following sections display the attachment in different
97 // ways depending on how they choose. The first way will download
98 // under any circumstance. This sets the Content-type to be
99 // applicatin/octet-stream, which should be interpreted by the
100 // browser as "download me".
101 // The second method (view) is used for images or other formats
102 // that should be able to be handled by the browser. It will
103 // most likely display the attachment inline inside the browser.
104 // And finally, the third one will be used by default. If it
105 // is displayable (text or html), it will load them up in a text
106 // viewer (built in to squirrelmail). Otherwise, it sets the
107 // content-type as application/octet-stream
108
109 if (isset($absolute_dl) && $absolute_dl == "true") {
110 switch($type0) {
111 case "text":
112 set_up_language(getPref($data_dir, $username, "language"));
113 DumpHeaders($type0, $type1, $filename, 1);
114 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
115 $body = decodeBody($body, $header->encoding);
116 if ($type1 == "plain" && isset($showHeaders)) {
117 echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n";
118 echo " " . _("From") . ": " . decodeHeader($top_header->from) . "\n";
119 echo " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n";
120 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
121 }
122 elseif ($type1 == "html" && isset($showHeaders)) {
123 echo '<table><tr><th align=right>' . _("Subject");
124 echo ':</th><td>' . decodeHeader($top_header->subject);
125 echo "</td></tr>\n<tr><th align=right>" . _("From");
126 echo ':</th><td>' . decodeHeader($top_header->from);
127 echo "</td></tr>\n<tr><th align=right>" . _("To");
128 echo ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to));
129 echo "</td></tr>\n<tr><th align=right>" . _("Date");
130 echo ':</th><td>' . getLongDateString($top_header->date);
131 echo "</td></tr>\n</table>\n<hr>\n";
132 }
133 echo $body;
134 break;
135 default:
136 DumpHeaders($type0, $type1, $filename, 1);
137 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
138 break;
139 }
140 } else {
141 switch ($type0) {
142 case "text":
143 if ($type1 == "plain" || $type1 == "html") {
144 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
145 $body = decodeBody($body, $header->encoding);
146 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
147 } else {
148 DumpHeaders($type0, $type1, $filename, 0);
149 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
150 $body = decodeBody($body, $header->encoding);
151 echo $body;
152 }
153 break;
154 case "message":
155 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
156 $body = decodeBody($body, $header->encoding);
157 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
158 break;
159 default:
160 DumpHeaders($type0, $type1, $filename, 0);
161 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
162 break;
163 }
164 }
165
166
167 // This function is verified to work with Netscape and the *very latest*
168 // version of IE. I don't know if it works with Opera, but it should now.
169 function DumpHeaders($type0, $type1, $filename, $force)
170 {
171 global $HTTP_USER_AGENT;
172
173 $isIE = 0;
174 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
175 strstr($HTTP_USER_AGENT, 'Opera') === false) {
176 $isIE = 1;
177 }
178
179 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
180
181 // A Pox on Microsoft and it's Office!
182 if (! $force)
183 {
184 // Try to show in browser window
185 header("Content-Disposition: inline; filename=\"$filename\"");
186 header("Content-Type: $type0/$type1; name=\"$filename\"");
187 }
188 else
189 {
190 // Try to pop up the "save as" box
191 // IE makes this hard. It pops up 2 save boxes, or none.
192 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
193 // But, accordint to Microsoft, it is "RFC compliant but doesn't
194 // take into account some deviations that allowed within the
195 // specification." Doesn't that mean RFC non-compliant?
196 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
197 //
198 // The best thing you can do for IE is to upgrade to the latest
199 // version
200 if ($isIE) {
201 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
202 // Do not have quotes around filename, but that applied to
203 // "attachment"... does it apply to inline too?
204 //
205 // This combination seems to work mostly. IE 5.5 SP 1 has
206 // known issues (see the Microsoft Knowledge Base)
207 header("Content-Disposition: inline; filename=$filename");
208
209 // This works for most types, but doesn't work with Word files
210 header("Content-Type: application/download; name=\"$filename\"");
211
212 // These are spares, just in case. :-)
213 //header("Content-Type: $type0/$type1; name=\"$filename\"");
214 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
215 //header("Content-Type: application/octet-stream; name=\"$filename\"");
216 } else {
217 header("Content-Disposition: attachment; filename=\"$filename\"");
218 // application/octet-stream forces download for Netscape
219 header("Content-Type: application/octet-stream; name=\"$filename\"");
220 }
221 }
222 }
223 ?>