rg=0 fix
[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 /* Path for SquirrelMail required files. */
16 define('SM_PATH','../');
17
18 /* SquirrelMail required files. */
19 require_once(SM_PATH . 'include/validate.php');
20 require_once(SM_PATH . 'functions/imap.php');
21 require_once(SM_PATH . 'functions/mime.php');
22
23 header('Pragma: ');
24 header('Cache-Control: cache');
25
26 /* globals */
27
28 $key = $_COOKIE['key'];
29 $username = $_SESSION['username'];
30 $onetimepad = $_SESSION['onetimepad'];
31 $mailbox = $_GET['mailbox'];
32 $passed_id = $_GET['passed_id'];
33 $ent_id = $_GET['ent_id'];
34 $messages = $_SESSION['messages'];
35 if (isset($_GET['passed_ent_id'])) {
36 $passed_ent_id = $_GET['passed_ent_id'];
37 } else {
38 $passed_ent_id = '';
39 }
40
41 if (isset($_GET['absolute_dl'])) {
42 $absolute_dl = $_GET['absolute_dl'];
43 }
44
45 /* end globals */
46 $mailbox = decodeHeader($mailbox);
47
48 global $uid_support;
49
50 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
51 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
52
53 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
54 if (!is_object($message)) {
55 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
56 }
57 $subject = $message->rfc822_header->subject;
58 $message = &$message->getEntity($ent_id);
59 $header = $message->header;
60 if ($message->rfc822_header) {
61 $subject = $message->rfc822_header->subject;
62 $charset = $header->content_type->properties['charset'];
63 } else {
64 $header = $message->header;
65 $charset = $header->getParameter('charset');
66 }
67 $type0 = $header->type0;
68 $type1 = $header->type1;
69 $encoding = strtolower($header->encoding);
70
71 /*
72 * lets redefine message as this particular entity that we wish to display.
73 * it should hold only the header for this entity. We need to fetch the body
74 * yet before we can display anything.
75 */
76
77 if (isset($override_type0)) {
78 $type0 = $override_type0;
79 }
80 if (isset($override_type1)) {
81 $type1 = $override_type1;
82 }
83 $filename = '';
84 if (is_object($message->header->disposition)) {
85 $filename = decodeHeader($header->disposition->getProperty('filename'));
86 if (!$filename) {
87 $filename = decodeHeader($header->disposition->getProperty('name'));
88 }
89 }
90 if (strlen($filename) < 1) {
91 if ($type1 == 'plain' && $type0 == 'text') {
92 $suffix = 'txt';
93 $filename = $subject . '.txt';
94 } else if ($type1 == 'richtext' && $type0 == 'text') {
95 $suffix = 'rtf';
96 $filename = $subject . '.rtf';
97 } else if ($type1 == 'postscript' && $type0 == 'application') {
98 $suffix = 'ps';
99 $filename = $subject . '.ps';
100 } else if ($type1 == 'rfc822' && $type0 == 'message') {
101 $suffix = 'eml';
102 $filename = $subject . '.msg';
103 } else {
104 $suffix = $type1;
105 }
106
107 if (strlen($filename) < 1) {
108 $filename = "untitled$ent_id.$suffix";
109 } else {
110 $filename = "$filename.$suffix";
111 }
112 }
113
114 /*
115 * Note:
116 * The following sections display the attachment in different
117 * ways depending on how they choose. The first way will download
118 * under any circumstance. This sets the Content-type to be
119 * applicatin/octet-stream, which should be interpreted by the
120 * browser as "download me".
121 * The second method (view) is used for images or other formats
122 * that should be able to be handled by the browser. It will
123 * most likely display the attachment inline inside the browser.
124 * And finally, the third one will be used by default. If it
125 * is displayable (text or html), it will load them up in a text
126 * viewer (built in to squirrelmail). Otherwise, it sets the
127 * content-type as application/octet-stream
128 */
129 if (isset($absolute_dl) && $absolute_dl) {
130 DumpHeaders($type0, $type1, $filename, 1);
131 } else {
132 DumpHeaders($type0, $type1, $filename, 0);
133 }
134 /* be aware that any warning caused by download.php will corrupt the
135 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
136 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
137
138 /*
139 * This function is verified to work with Netscape and the *very latest*
140 * version of IE. I don't know if it works with Opera, but it should now.
141 */
142 function DumpHeaders($type0, $type1, $filename, $force) {
143 global $_SERVER, $languages, $squirrelmail_language;
144 $isIE = $isIE6 = 0;
145
146 $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
147
148 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
149 strstr($HTTP_USER_AGENT, 'Opera') === false) {
150 $isIE = 1;
151 }
152
153 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
154 strstr($HTTP_USER_AGENT, 'Opera') === false) {
155 $isIE6 = 1;
156 }
157
158 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
159 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
160 $filename =
161 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
162 } else {
163 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
164 }
165
166 // A Pox on Microsoft and it's Office!
167 if (!$force) {
168 // Try to show in browser window
169 header("Content-Disposition: inline; filename=\"$filename\"");
170 header("Content-Type: $type0/$type1; name=\"$filename\"");
171 } else {
172 // Try to pop up the "save as" box
173 // IE makes this hard. It pops up 2 save boxes, or none.
174 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
175 // But, accordint to Microsoft, it is "RFC compliant but doesn't
176 // take into account some deviations that allowed within the
177 // specification." Doesn't that mean RFC non-compliant?
178 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
179 //
180 // The best thing you can do for IE is to upgrade to the latest
181 // version
182 if ($isIE && !$isIE6) {
183 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
184 // Do not have quotes around filename, but that applied to
185 // "attachment"... does it apply to inline too?
186 //
187 // This combination seems to work mostly. IE 5.5 SP 1 has
188 // known issues (see the Microsoft Knowledge Base)
189 header("Content-Disposition: inline; filename=$filename");
190 // This works for most types, but doesn't work with Word files
191 header("Content-Type: application/download; name=\"$filename\"");
192
193 // These are spares, just in case. :-)
194 //header("Content-Type: $type0/$type1; name=\"$filename\"");
195 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
196 //header("Content-Type: application/octet-stream; name=\"$filename\"");
197 } else {
198 header("Content-Disposition: attachment; filename=\"$filename\"");
199 // application/octet-stream forces download for Netscape
200 header("Content-Type: application/octet-stream; name=\"$filename\"");
201 }
202 }
203 }
204 ?>