rg=0
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * download.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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
86725763 15/* Path for SquirrelMail required files. */
16define('SM_PATH','../');
17
18/* SquirrelMail required files. */
08185f2a 19require_once(SM_PATH . 'include/validate.php');
86725763 20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/mime.php');
6b96544a 22
65c3ec94 23header('Pragma: ');
24header('Cache-Control: cache');
25
0b97a708 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$passed_ent_id = $_GET['passed_ent_id'];
34$base_uri = $base_uri = $_SESSION['base_uri'];
35
36if (isset($_GET['startMessage'])) {
37 $startMessage = $_GET['startMessage'];
38}
39if(isset($_GET['where'])) {
40 $where = $_GET['where'];
41}
42if(isset($_GET['what'])) {
43 $what = $_GET['what'];
44}
45if(isset($_GET['showHeaders'])) {
46 $showHeaders = $_GET['showHeaders'];
47}
48if(isset($_GET['absolute_dl'])) {
49 $absolute_dl = $_GET['absolute_dl'];
50}
51if (isset($_GET['show_more_cc'])) {
52 $show_more = $_GET['show_more_cc'];
53}
54if(isset($_GET['show_more_bcc'])) {
55 $show_more = $_GET['show_more_bcc'];
56}
57if(isset($_GET['show_more'])) {
58 $show_more = $_GET['show_more'];
59}
60if(isset($_GET['sort'])) {
61 $sort = $_GET['sort'];
62}
63
64/* end globals */
65
13b13ded 66function get_extract_to_target_list($imapConnection) {
13b13ded 67 $boxes = sqimap_mailbox_list($imapConnection);
68 for ($i = 0; $i < count($boxes); $i++) {
69 if (!in_array('noselect', $boxes[$i]['flags'])) {
70 $box = $boxes[$i]['unformatted'];
71 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
72 if ( $box2 == 'INBOX' ) {
73 $box2 = _("INBOX");
74 }
75 echo "<option value=\"$box\">$box2</option>\n";
76 }
77 }
97d7da3b 78}
1e606225 79$mailbox = decodeHeader($mailbox);
97d7da3b 80
1e606225 81global $messages, $uid_support;
97d7da3b 82
1e606225 83$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
84$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
5c553e34 85if (!isset($passed_ent_id)) {
86 $passed_ent_id = '';
87}
97d7da3b 88
1e606225 89$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
5a1c48cd 90if (!is_object($message)) {
91 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
92}
ff9d4297 93$subject = $message->rfc822_header->subject;
94$message = &$message->getEntity($ent_id);
1e606225 95$header = $message->header;
ff9d4297 96if ($message->rfc822_header) {
97 $subject = $message->rfc822_header->subject;
98 $charset = $header->content_type->properties['charset'];
99} else {
100 $header = $message->header;
101 $charset = $header->getParameter('charset');
102}
1e606225 103$type0 = $header->type0;
104$type1 = $header->type1;
105$encoding = strtolower($header->encoding);
97d7da3b 106
1e606225 107/*
13b13ded 108$extracted = false;
109if (isset($extract_message) && $extract_message) {
110 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
1e606225 111 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message, $uid_support);
13b13ded 112 $cnt = count($read);
113 $body = '';
114 $length = 0;
115 for ($i=1;$i<$cnt;$i++) {
116 $length = $length + strlen($read[$i]);
117 $body .= $read[$i];
118 }
119 if (isset($targetMailbox) && $length>0) {
120 sqimap_append ($imapConnection, $targetMailbox, $length);
121 fputs($imapConnection,$body);
122 sqimap_append_done ($imapConnection);
123 $extracted = true;
124 }
125}
126
1e606225 127
128*/
65c3ec94 129/*
130 * lets redefine message as this particular entity that we wish to display.
131 * it should hold only the header for this entity. We need to fetch the body
132 * yet before we can display anything.
133 */
65c3ec94 134
65c3ec94 135if (isset($override_type0)) {
136 $type0 = $override_type0;
137}
138if (isset($override_type1)) {
139 $type1 = $override_type1;
140}
ff9d4297 141$filename = '';
142if (is_object($message->header->disposition)) {
143 $filename = decodeHeader($header->disposition->getProperty('filename'));
144 if (!$filename) {
145 $filename = decodeHeader($header->disposition->getProperty('name'));
146 }
65c3ec94 147}
65c3ec94 148if (strlen($filename) < 1) {
149 if ($type1 == 'plain' && $type0 == 'text') {
150 $suffix = 'txt';
ff9d4297 151 $filename = $subject . '.txt';
65c3ec94 152 } else if ($type1 == 'richtext' && $type0 == 'text') {
153 $suffix = 'rtf';
ff9d4297 154 $filename = $subject . '.rtf';
65c3ec94 155 } else if ($type1 == 'postscript' && $type0 == 'application') {
156 $suffix = 'ps';
ff9d4297 157 $filename = $subject . '.ps';
97d7da3b 158 } else if ($type1 == 'rfc822' && $type0 == 'message') {
159 $suffix = 'eml';
ff9d4297 160 $filename = $subject . '.msg';
65c3ec94 161 } else {
162 $suffix = $type1;
163 }
164
631db37e 165 if (strlen($filename) < 1) {
ff9d4297 166 $filename = "untitled$ent_id.$suffix";
631db37e 167 } else {
ff9d4297 168 $filename = "$filename.$suffix";
631db37e 169 }
65c3ec94 170}
171
172/*
173 * Note:
174 * The following sections display the attachment in different
175 * ways depending on how they choose. The first way will download
176 * under any circumstance. This sets the Content-type to be
177 * applicatin/octet-stream, which should be interpreted by the
178 * browser as "download me".
179 * The second method (view) is used for images or other formats
180 * that should be able to be handled by the browser. It will
181 * most likely display the attachment inline inside the browser.
182 * And finally, the third one will be used by default. If it
183 * is displayable (text or html), it will load them up in a text
184 * viewer (built in to squirrelmail). Otherwise, it sets the
185 * content-type as application/octet-stream
186 */
187if (isset($absolute_dl) && $absolute_dl == 'true') {
5c553e34 188 DumpHeaders($type0, $type1, $filename, 1);
65c3ec94 189} else {
5c553e34 190 DumpHeaders($type0, $type1, $filename, 0);
65c3ec94 191}
38bca81c 192/* be aware that any warning caused by download.php will corrupt the
193 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 194mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
65c3ec94 195
65c3ec94 196/*
197 * This function is verified to work with Netscape and the *very latest*
198 * version of IE. I don't know if it works with Opera, but it should now.
199 */
200function DumpHeaders($type0, $type1, $filename, $force) {
0b97a708 201 global $_SERVER, $languages, $squirrelmail_language;
65c3ec94 202 $isIE = 0;
97d7da3b 203
0b97a708 204 $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
205
65c3ec94 206 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
207 strstr($HTTP_USER_AGENT, 'Opera') === false) {
36294f1a 208 $isIE = 1;
65c3ec94 209 }
210
97d7da3b 211 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
212 strstr($HTTP_USER_AGENT, 'Opera') === false) {
213 $isIE6 = 1;
214 }
215
7dae3923 216 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
217 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 218 $filename =
219 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
83be314a 220 } else {
7dae3923 221 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
83be314a 222 }
65c3ec94 223
224 // A Pox on Microsoft and it's Office!
225 if (! $force) {
226 // Try to show in browser window
227 header("Content-Disposition: inline; filename=\"$filename\"");
228 header("Content-Type: $type0/$type1; name=\"$filename\"");
229 } else {
230 // Try to pop up the "save as" box
231 // IE makes this hard. It pops up 2 save boxes, or none.
232 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
233 // But, accordint to Microsoft, it is "RFC compliant but doesn't
234 // take into account some deviations that allowed within the
235 // specification." Doesn't that mean RFC non-compliant?
236 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
237 //
238 // The best thing you can do for IE is to upgrade to the latest
239 // version
97d7da3b 240 if ($isIE && !isset($isIE6)) {
65c3ec94 241 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
242 // Do not have quotes around filename, but that applied to
243 // "attachment"... does it apply to inline too?
244 //
245 // This combination seems to work mostly. IE 5.5 SP 1 has
246 // known issues (see the Microsoft Knowledge Base)
247 header("Content-Disposition: inline; filename=$filename");
248
249 // This works for most types, but doesn't work with Word files
250 header("Content-Type: application/download; name=\"$filename\"");
251
252 // These are spares, just in case. :-)
253 //header("Content-Type: $type0/$type1; name=\"$filename\"");
254 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
255 //header("Content-Type: application/octet-stream; name=\"$filename\"");
256 } else {
257 header("Content-Disposition: attachment; filename=\"$filename\"");
258 // application/octet-stream forces download for Netscape
259 header("Content-Type: application/octet-stream; name=\"$filename\"");
260 }
261 }
262}
631db37e 263?>