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