f0a31f58492146e7ec797bd88d4a1c7418752d00
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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") $suffix = "txt";
108 else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf";
109 else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps";
110 else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg";
111 else $suffix = $type1;
112
113 $filename = "untitled$passed_ent_id.$suffix";
114 }
115
116 // Note:
117 // The following sections display the attachment in different
118 // ways depending on how they choose. The first way will download
119 // under any circumstance. This sets the Content-type to be
120 // applicatin/octet-stream, which should be interpreted by the
121 // browser as "download me".
122 // The second method (view) is used for images or other formats
123 // that should be able to be handled by the browser. It will
124 // most likely display the attachment inline inside the browser.
125 // And finally, the third one will be used by default. If it
126 // is displayable (text or html), it will load them up in a text
127 // viewer (built in to squirrelmail). Otherwise, it sets the
128 // content-type as application/octet-stream
129
130 if (isset($absolute_dl) && $absolute_dl == "true") {
131 switch($type0) {
132 case "text":
133 DumpHeaders($type0, $type1, $filename, 1);
134 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
135 $body = decodeBody($body, $header->encoding);
136 if ($type1 == "plain" && isset($showHeaders)) {
137 echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n";
138 echo " " . _("From") . ": " . decodeHeader($top_header->from) . "\n";
139 echo " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n";
140 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
141 }
142 elseif ($type1 == "html" && isset($showHeaders)) {
143 echo '<table><tr><th align=right>' . _("Subject");
144 echo ':</th><td>' . decodeHeader($top_header->subject);
145 echo "</td></tr>\n<tr><th align=right>" . _("From");
146 echo ':</th><td>' . decodeHeader($top_header->from);
147 echo "</td></tr>\n<tr><th align=right>" . _("To");
148 echo ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to));
149 echo "</td></tr>\n<tr><th align=right>" . _("Date");
150 echo ':</th><td>' . getLongDateString($top_header->date);
151 echo "</td></tr>\n</table>\n<hr>\n";
152 }
153 echo $body;
154 break;
155 default:
156 DumpHeaders($type0, $type1, $filename, 1);
157 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
158 break;
159 }
160 } else {
161 switch ($type0) {
162 case "text":
163 if ($type1 == "plain" || $type1 == "html") {
164 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
165 $body = decodeBody($body, $header->encoding);
166 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
167 } else {
168 DumpHeaders($type0, $type1, $filename, 0);
169 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
170 $body = decodeBody($body, $header->encoding);
171 echo $body;
172 }
173 break;
174 case "message":
175 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
176 $body = decodeBody($body, $header->encoding);
177 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
178 break;
179 default:
180 DumpHeaders($type0, $type1, $filename, 0);
181 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
182 break;
183 }
184 }
185
186
187 // This function is verified to work with Netscape and the *very latest*
188 // version of IE. I don't know if it works with Opera, but it should now.
189 function DumpHeaders($type0, $type1, $filename, $force)
190 {
191 global $HTTP_USER_AGENT;
192
193 $isIE = 0;
194 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
195 strstr($HTTP_USER_AGENT, 'Opera') === false) {
196 $isIE = 1;
197 }
198
199 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
200
201 // A Pox on Microsoft and it's Office!
202 if (! $force)
203 {
204 // Try to show in browser window
205 header("Content-Disposition: inline; filename=\"$filename\"");
206 header("Content-Type: $type0/$type1; name=\"$filename\"");
207 }
208 else
209 {
210 // Try to pop up the "save as" box
211 // IE makes this hard. It pops up 2 save boxes, or none.
212 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
213 // But, accordint to Microsoft, it is "RFC compliant but doesn't
214 // take into account some deviations that allowed within the
215 // specification." Doesn't that mean RFC non-compliant?
216 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
217 //
218 // The best thing you can do for IE is to upgrade to the latest
219 // version
220 if ($isIE) {
221 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
222 // Do not have quotes around filename, but that applied to
223 // "attachment"... does it apply to inline too?
224 //
225 // This combination seems to work mostly. IE 5.5 SP 1 has
226 // known issues (see the Microsoft Knowledge Base)
227 header("Content-Disposition: inline; filename=$filename");
228
229 // This works for most types, but doesn't work with Word files
230 header("Content-Type: application/download; name=\"$filename\"");
231
232 // These are spares, just in case. :-)
233 //header("Content-Type: $type0/$type1; name=\"$filename\"");
234 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
235 //header("Content-Type: application/octet-stream; name=\"$filename\"");
236 } else {
237 header("Content-Disposition: attachment; filename=\"$filename\"");
238 // application/octet-stream forces download for Netscape
239 header("Content-Type: application/octet-stream; name=\"$filename\"");
240 }
241 }
242 }
243 ?>