6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Handles attachment downloads to the users computer.
10 * Also allows displaying of attachments when possible.
15 /* Path for SquirrelMail required files. */
16 define('SM_PATH','../');
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');
24 header('Cache-Control: cache');
26 function get_extract_to_target_list($imapConnection) {
27 $boxes = sqimap_mailbox_list($imapConnection);
28 for ($i = 0; $i < count($boxes); $i++
) {
29 if (!in_array('noselect', $boxes[$i]['flags'])) {
30 $box = $boxes[$i]['unformatted'];
31 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
32 if ( $box2 == 'INBOX' ) {
35 echo "<option value=\"$box\">$box2</option>\n";
39 $mailbox = decodeHeader($mailbox);
41 global $messages, $uid_support;
43 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
44 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
45 if (!isset($passed_ent_id)) {
49 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
50 if (!is_object($message)) {
51 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
53 $subject = $message->rfc822_header
->subject
;
54 $message = &$message->getEntity($ent_id);
55 $header = $message->header
;
56 if ($message->rfc822_header
) {
57 $subject = $message->rfc822_header
->subject
;
58 $charset = $header->content_type
->properties
['charset'];
60 $header = $message->header
;
61 $charset = $header->getParameter('charset');
63 $type0 = $header->type0
;
64 $type1 = $header->type1
;
65 $encoding = strtolower($header->encoding
);
69 if (isset($extract_message) && $extract_message) {
70 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
71 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message, $uid_support);
75 for ($i=1;$i<$cnt;$i++) {
76 $length = $length + strlen($read[$i]);
79 if (isset($targetMailbox) && $length>0) {
80 sqimap_append ($imapConnection, $targetMailbox, $length);
81 fputs($imapConnection,$body);
82 sqimap_append_done ($imapConnection);
90 * lets redefine message as this particular entity that we wish to display.
91 * it should hold only the header for this entity. We need to fetch the body
92 * yet before we can display anything.
95 if (isset($override_type0)) {
96 $type0 = $override_type0;
98 if (isset($override_type1)) {
99 $type1 = $override_type1;
102 if (is_object($message->header
->disposition
)) {
103 $filename = decodeHeader($header->disposition
->getProperty('filename'));
105 $filename = decodeHeader($header->disposition
->getProperty('name'));
108 if (strlen($filename) < 1) {
109 if ($type1 == 'plain' && $type0 == 'text') {
111 $filename = $subject . '.txt';
112 } else if ($type1 == 'richtext' && $type0 == 'text') {
114 $filename = $subject . '.rtf';
115 } else if ($type1 == 'postscript' && $type0 == 'application') {
117 $filename = $subject . '.ps';
118 } else if ($type1 == 'rfc822' && $type0 == 'message') {
120 $filename = $subject . '.msg';
125 if (strlen($filename) < 1) {
126 $filename = "untitled$ent_id.$suffix";
128 $filename = "$filename.$suffix";
134 * The following sections display the attachment in different
135 * ways depending on how they choose. The first way will download
136 * under any circumstance. This sets the Content-type to be
137 * applicatin/octet-stream, which should be interpreted by the
138 * browser as "download me".
139 * The second method (view) is used for images or other formats
140 * that should be able to be handled by the browser. It will
141 * most likely display the attachment inline inside the browser.
142 * And finally, the third one will be used by default. If it
143 * is displayable (text or html), it will load them up in a text
144 * viewer (built in to squirrelmail). Otherwise, it sets the
145 * content-type as application/octet-stream
147 if (isset($absolute_dl) && $absolute_dl == 'true') {
148 DumpHeaders($type0, $type1, $filename, 1);
150 DumpHeaders($type0, $type1, $filename, 0);
152 /* be aware that any warning caused by download.php will corrupt the
153 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
154 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
157 * This function is verified to work with Netscape and the *very latest*
158 * version of IE. I don't know if it works with Opera, but it should now.
160 function DumpHeaders($type0, $type1, $filename, $force) {
161 global $HTTP_USER_AGENT, $languages, $squirrelmail_language;
164 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
165 strstr($HTTP_USER_AGENT, 'Opera') === false) {
169 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
170 strstr($HTTP_USER_AGENT, 'Opera') === false) {
174 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
175 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
177 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
179 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
182 // A Pox on Microsoft and it's Office!
184 // Try to show in browser window
185 header("Content-Disposition: inline; filename=\"$filename\"");
186 header("Content-Type: $type0/$type1; name=\"$filename\"");
188 // Try to pop up the "save as" box
189 // IE makes this hard. It pops up 2 save boxes, or none.
190 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
191 // But, accordint to Microsoft, it is "RFC compliant but doesn't
192 // take into account some deviations that allowed within the
193 // specification." Doesn't that mean RFC non-compliant?
194 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
196 // The best thing you can do for IE is to upgrade to the latest
198 if ($isIE && !isset($isIE6)) {
199 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
200 // Do not have quotes around filename, but that applied to
201 // "attachment"... does it apply to inline too?
203 // This combination seems to work mostly. IE 5.5 SP 1 has
204 // known issues (see the Microsoft Knowledge Base)
205 header("Content-Disposition: inline; filename=$filename");
207 // This works for most types, but doesn't work with Word files
208 header("Content-Type: application/download; name=\"$filename\"");
210 // These are spares, just in case. :-)
211 //header("Content-Type: $type0/$type1; name=\"$filename\"");
212 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
213 //header("Content-Type: application/octet-stream; name=\"$filename\"");
215 header("Content-Disposition: attachment; filename=\"$filename\"");
216 // application/octet-stream forces download for Netscape
217 header("Content-Type: application/octet-stream; name=\"$filename\"");