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