Fixed silly, nasty, bug...
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 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
31define('download_php', true); // Used for preferences
32
33require_once('../src/validate.php');
34require_once('../functions/imap.php');
35require_once('../functions/mime.php');
36require_once('../functions/date.php');
6b96544a 37
bc104ef3 38 header("Pragma: ");
39 header("Cache-Control: cache");
40
11307a4c 41 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
8442ac08 42 global $where, $what, $charset;
a037624d 43 global $startMessage;
fdcca8d3 44
2d367c68 45 displayPageHeader($color, 'None');
7831268e 46
a037624d 47 echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
180c2c09 48 echo "<B><CENTER>";
aa8dcbd5 49 echo _("Viewing a text attachment") . " - ";
f4991a86 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 }
a037624d 56
7831268e 57 $urlmailbox = urlencode($mailbox);
a037624d 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\">";
180c2c09 59 echo _("Download this as a file");
a037624d 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
c6b144dd 67 if ($type1 == 'html') {
68 $body = MagicHTML( $body, $id );
69 } else {
9eea179c 70 translateText($body, $wrap_at, $charset);
c6b144dd 71 }
9eea179c 72
c6b144dd 73 flush();
9eea179c 74 echo $body;
0a4cf77a 75
7831268e 76 echo "</TT></TD></TR></TABLE>";
77 }
78
e1469126 79 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
813eba2f 80 sqimap_mailbox_select($imapConnection, $mailbox);
6b96544a 81
82 // $message contains all information about the message
83 // including header and body
813eba2f 84 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
ca1a555e 85 $top_header = $message->header;
6b96544a 86
8beafbbc 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;
8beafbbc 93
8442ac08 94 $charset = $header->charset;
8beafbbc 95 $type0 = $header->type0;
96 $type1 = $header->type1;
0db847e1 97 if (isset($override_type0))
98 $type0 = $override_type0;
99 if (isset($override_type1))
100 $type1 = $override_type1;
f9ab17bf 101 $filename = decodeHeader($header->filename);
66e1a00e 102 if (!$filename) {
103 $filename = decodeHeader($header->name);
104 }
6b96544a 105
7831268e 106 if (strlen($filename) < 1) {
e9f8ea4e 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";
7831268e 114 }
6b96544a 115
c4809aca 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
a7ea7540 129
61423189 130 if (isset($absolute_dl) && $absolute_dl == "true") {
7831268e 131 switch($type0) {
132 case "text":
36294f1a 133 DumpHeaders($type0, $type1, $filename, 1);
beb9e459 134 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
8beafbbc 135 $body = decodeBody($body, $header->encoding);
a900c1e6 136 if ($type1 == "plain" && isset($showHeaders)) {
d51894be 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";
ca1a555e 140 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
623332f3 141 }
a900c1e6 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 }
36294f1a 153 echo $body;
7831268e 154 break;
155 default:
36294f1a 156 DumpHeaders($type0, $type1, $filename, 1);
beb9e459 157 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
7831268e 158 break;
159 }
160 } else {
161 switch ($type0) {
162 case "text":
beb9e459 163 if ($type1 == "plain" || $type1 == "html") {
164 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
165 $body = decodeBody($body, $header->encoding);
beb9e459 166 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
23a8095f 167 } else {
36294f1a 168 DumpHeaders($type0, $type1, $filename, 0);
beb9e459 169 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
170 $body = decodeBody($body, $header->encoding);
beb9e459 171 echo $body;
172 }
173 break;
7831268e 174 case "message":
beb9e459 175 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
8beafbbc 176 $body = decodeBody($body, $header->encoding);
11307a4c 177 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
7831268e 178 break;
179 default:
36294f1a 180 DumpHeaders($type0, $type1, $filename, 0);
beb9e459 181 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
7831268e 182 break;
183 }
36294f1a 184 }
185
186
2a0372ff 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.
36294f1a 189 function DumpHeaders($type0, $type1, $filename, $force)
190 {
191 global $HTTP_USER_AGENT;
192
193 $isIE = 0;
2a0372ff 194 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
195 strstr($HTTP_USER_AGENT, 'Opera') === false) {
36294f1a 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 }
35586184 243?>