integrated patch 403567 (from januar 2001 !!)
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handles attachment downloads to the users computer.
10 * Also allows displaying of attachments when possible.
11 *
12 * $Id$
13 */
14
15 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
19
20 header('Pragma: ');
21 header('Cache-Control: cache');
22
23
24 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) {
25 global $where, $what, $charset;
26 global $startMessage;
27
28 displayPageHeader($color, 'None');
29 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
30 "<B><CENTER>".
31 _("Viewing a text attachment") . " - ";
32 if ($where && $what) {
33 // from a search
34 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
35 } else {
36 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
37 }
38
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").
42 "</A></CENTER><BR>".
43 "</CENTER></B>".
44 "</TD></TR></TABLE>".
45 "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
46 "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
47
48 if ($type1 == 'html') {
49 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
50 $msg = $msg->getEntity($entid);
51 $body = MagicHTML( $body, $id, $msg );
52 } else {
53 translateText($body, $wrap_at, $charset);
54 }
55
56 flush();
57 echo $body .
58 "</TT></TD></TR></TABLE>";
59 }
60
61 function get_extract_to_target_list($imapConnection) {
62 $boxes = sqimap_mailbox_list($imapConnection);
63 for ($i = 0; $i < count($boxes); $i++) {
64 if (!in_array('noselect', $boxes[$i]['flags'])) {
65 $box = $boxes[$i]['unformatted'];
66 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
67 if ( $box2 == 'INBOX' ) {
68 $box2 = _("INBOX");
69 }
70 echo "<option value=\"$box\">$box2</option>\n";
71 }
72 }
73 }
74 $mailbox = decodeHeader($mailbox);
75
76 global $messages, $uid_support;
77
78
79
80 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
81 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
82
83 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
84 $message = &$message->getEntity($passed_ent_id);
85
86 $header = $message->header;
87 $charset = $header->charset;
88 $type0 = $header->type0;
89 $type1 = $header->type1;
90 $encoding = strtolower($header->encoding);
91
92
93
94
95 /*
96 $extracted = false;
97 if (isset($extract_message) && $extract_message) {
98 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
99 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message, $uid_support);
100 $cnt = count($read);
101 $body = '';
102 $length = 0;
103 for ($i=1;$i<$cnt;$i++) {
104 $length = $length + strlen($read[$i]);
105 $body .= $read[$i];
106 }
107 if (isset($targetMailbox) && $length>0) {
108 sqimap_append ($imapConnection, $targetMailbox, $length);
109 fputs($imapConnection,$body);
110 sqimap_append_done ($imapConnection);
111 $extracted = true;
112 }
113 }
114
115 if (isset($showHeaders)) {
116 $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
117 }
118
119 */
120 /*
121 * lets redefine message as this particular entity that we wish to display.
122 * it should hold only the header for this entity. We need to fetch the body
123 * yet before we can display anything.
124 */
125
126 if (isset($override_type0)) {
127 $type0 = $override_type0;
128 }
129 if (isset($override_type1)) {
130 $type1 = $override_type1;
131 }
132
133 $filename = decodeHeader($message->header->filename);
134
135 if (!$filename) {
136 $filename = decodeHeader($message->header->name);
137 }
138
139 if (strlen($filename) < 1) {
140 if ($type1 == 'plain' && $type0 == 'text') {
141 $suffix = 'txt';
142 } else if ($type1 == 'richtext' && $type0 == 'text') {
143 $suffix = 'rtf';
144 } else if ($type1 == 'postscript' && $type0 == 'application') {
145 $suffix = 'ps';
146 } else if ($type1 == 'rfc822' && $type0 == 'message') {
147 $suffix = 'eml';
148 } else {
149 $suffix = $type1;
150 }
151
152 $filename = "untitled$passed_ent_id.$suffix";
153 }
154
155 /*
156 * Note:
157 * The following sections display the attachment in different
158 * ways depending on how they choose. The first way will download
159 * under any circumstance. This sets the Content-type to be
160 * applicatin/octet-stream, which should be interpreted by the
161 * browser as "download me".
162 * The second method (view) is used for images or other formats
163 * that should be able to be handled by the browser. It will
164 * most likely display the attachment inline inside the browser.
165 * And finally, the third one will be used by default. If it
166 * is displayable (text or html), it will load them up in a text
167 * viewer (built in to squirrelmail). Otherwise, it sets the
168 * content-type as application/octet-stream
169 */
170 if (isset($absolute_dl) && $absolute_dl == 'true') {
171 switch($type0) {
172 case 'text':
173 DumpHeaders($type0, $type1, $filename, 1);
174 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
175 $body = decodeBody($body, $encoding);
176 if ($type1 == 'plain' && isset($showHeaders)) {
177 echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n".
178 " " . _("From") . ": " . decodeHeader($top_header->from) . "\n".
179 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n".
180 " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
181 } elseif ($type1 == 'html' && isset($showHeaders)) {
182 echo '<table><tr><th align=right>' . _("Subject").
183 ':</th><td>' . decodeHeader($top_header->subject).
184 "</td></tr>\n<tr><th align=right>" . _("From").
185 ':</th><td>' . decodeHeader($top_header->from).
186 "</td></tr>\n<tr><th align=right>" . _("To").
187 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)).
188 "</td></tr>\n<tr><th align=right>" . _("Date").
189 ':</th><td>' . getLongDateString($top_header->date).
190 "</td></tr>\n</table>\n<hr>\n";
191 }
192 echo $body;
193 break;
194 default:
195 DumpHeaders($type0, $type1, $filename, 1);
196 // if ($message->decoded_body == '') {
197 // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding));
198 // }
199 // echo $message->decoded_body;
200
201 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding);
202 break;
203 }
204 } else {
205 switch ($type0) {
206 case 'text':
207 if ($type1 == 'plain' || $type1 == 'html') {
208 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
209 $body = decodeBody($body, $encoding);
210 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
211 } else {
212 DumpHeaders($type0, $type1, $filename, 0);
213 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
214 $body = decodeBody($body, $encoding);
215 echo $body;
216 }
217 break;
218 default:
219 DumpHeaders($type0, $type1, $filename, 0);
220 // if ($message->decoded_body == '') {
221 // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding));
222 //
223 // }
224 // echo $message->decoded_body;
225 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding);
226 break;
227 }
228 }
229
230 $message = &$message->getEntity('');
231 $messages[$mbx_response['UIDVALIDITY']]["$passed_id"] = &$message;
232
233 /*
234 * This function is verified to work with Netscape and the *very latest*
235 * version of IE. I don't know if it works with Opera, but it should now.
236 */
237 function DumpHeaders($type0, $type1, $filename, $force) {
238 global $HTTP_USER_AGENT;
239 $isIE = 0;
240
241
242 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
243 strstr($HTTP_USER_AGENT, 'Opera') === false) {
244 $isIE = 1;
245 }
246
247 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
248 strstr($HTTP_USER_AGENT, 'Opera') === false) {
249 $isIE6 = 1;
250 }
251
252 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
253
254 // A Pox on Microsoft and it's Office!
255 if (! $force) {
256 // Try to show in browser window
257 header("Content-Disposition: inline; filename=\"$filename\"");
258 header("Content-Type: $type0/$type1; name=\"$filename\"");
259 } else {
260 // Try to pop up the "save as" box
261 // IE makes this hard. It pops up 2 save boxes, or none.
262 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
263 // But, accordint to Microsoft, it is "RFC compliant but doesn't
264 // take into account some deviations that allowed within the
265 // specification." Doesn't that mean RFC non-compliant?
266 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
267 //
268 // The best thing you can do for IE is to upgrade to the latest
269 // version
270 if ($isIE && !isset($isIE6)) {
271 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
272 // Do not have quotes around filename, but that applied to
273 // "attachment"... does it apply to inline too?
274 //
275 // This combination seems to work mostly. IE 5.5 SP 1 has
276 // known issues (see the Microsoft Knowledge Base)
277 header("Content-Disposition: inline; filename=$filename");
278
279 // This works for most types, but doesn't work with Word files
280 header("Content-Type: application/download; name=\"$filename\"");
281
282 // These are spares, just in case. :-)
283 //header("Content-Type: $type0/$type1; name=\"$filename\"");
284 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
285 //header("Content-Type: application/octet-stream; name=\"$filename\"");
286 } else {
287 header("Content-Disposition: attachment; filename=\"$filename\"");
288 // application/octet-stream forces download for Netscape
289 header("Content-Type: application/octet-stream; name=\"$filename\"");
290 }
291 }
292 }
293 ?>