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