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