6 * Copyright (c) 1999-2003 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');
27 sqgetGlobalVar('key', $key, SQ_COOKIE
);
28 sqgetGlobalVar('username', $username, SQ_SESSION
);
29 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
);
30 sqgetGlobalVar('messages', $messages, SQ_SESSION
);
31 sqgetGlobalVar('mailbox', $mailbox, SQ_GET
);
32 sqgetGlobalVar('ent_id', $ent_id, SQ_GET
);
33 sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET
);
34 if ( sqgetGlobalVar('passed_id', $temp, SQ_GET
) ) {
35 $passed_id = (int) $temp;
42 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
43 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
45 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
46 if (!is_object($message)) {
47 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
49 $subject = $message->rfc822_header
->subject
;
51 $message = &$message->getEntity($ent_id);
52 $header = $message->header
;
54 if ($message->rfc822_header
) {
55 $subject = $message->rfc822_header
->subject
;
56 if (isset($header->content_type
)) {
57 $charset = $header->content_type
->properties
['charset'];
62 $header = $message->header
;
63 $charset = $header->getParameter('charset');
65 $type0 = $header->type0
;
66 $type1 = $header->type1
;
67 $encoding = strtolower($header->encoding
);
72 $encoding = 'US-ASCII';
73 $header = $message->header
;
77 * lets redefine message as this particular entity that we wish to display.
78 * it should hold only the header for this entity. We need to fetch the body
79 * yet before we can display anything.
82 if (isset($override_type0)) {
83 $type0 = $override_type0;
85 if (isset($override_type1)) {
86 $type1 = $override_type1;
89 if (is_object($message->header
->disposition
)) {
90 $filename = $header->disposition
->getProperty('filename');
92 $filename = $header->disposition
->getProperty('name');
95 $filename = $header->getParameter('name');
98 $filename = $header->getParameter('name');
101 $filename = decodeHeader($filename);
102 if (strlen($filename) < 1) {
103 if ($type1 == 'plain' && $type0 == 'text') {
105 $filename = $subject . '.txt';
106 } else if ($type1 == 'richtext' && $type0 == 'text') {
108 $filename = $subject . '.rtf';
109 } else if ($type1 == 'postscript' && $type0 == 'application') {
111 $filename = $subject . '.ps';
112 } else if ($type1 == 'rfc822' && $type0 == 'message') {
114 $filename = $subject . '.msg';
119 if (strlen($filename) < 1) {
120 $filename = 'untitled'.strip_tags($ent_id).'.'.$suffix;
122 $filename = "$filename.$suffix";
128 * The following sections display the attachment in different
129 * ways depending on how they choose. The first way will download
130 * under any circumstance. This sets the Content-type to be
131 * applicatin/octet-stream, which should be interpreted by the
132 * browser as "download me".
133 * The second method (view) is used for images or other formats
134 * that should be able to be handled by the browser. It will
135 * most likely display the attachment inline inside the browser.
136 * And finally, the third one will be used by default. If it
137 * is displayable (text or html), it will load them up in a text
138 * viewer (built in to squirrelmail). Otherwise, it sets the
139 * content-type as application/octet-stream
141 if (isset($absolute_dl) && $absolute_dl) {
142 DumpHeaders($type0, $type1, $filename, 1);
144 DumpHeaders($type0, $type1, $filename, 0);
146 /* be aware that any warning caused by download.php will corrupt the
147 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
148 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
151 * This function is verified to work with Netscape and the *very latest*
152 * version of IE. I don't know if it works with Opera, but it should now.
154 function DumpHeaders($type0, $type1, $filename, $force) {
155 global $languages, $squirrelmail_language;
158 sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER
);
160 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
161 strstr($HTTP_USER_AGENT, 'Opera') === false) {
165 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
166 strstr($HTTP_USER_AGENT, 'Opera') === false) {
170 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
171 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
173 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
175 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
178 // A Pox on Microsoft and it's Office!
180 // Try to show in browser window
181 header("Content-Disposition: inline; filename=\"$filename\"");
182 header("Content-Type: $type0/$type1; name=\"$filename\"");
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
192 // The best thing you can do for IE is to upgrade to the latest
194 if ($isIE && !$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?
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 // This works for most types, but doesn't work with Word files
203 header("Content-Type: application/download; name=\"$filename\"");
205 // These are spares, just in case. :-)
206 //header("Content-Type: $type0/$type1; name=\"$filename\"");
207 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
208 //header("Content-Type: application/octet-stream; name=\"$filename\"");
210 header("Content-Disposition: attachment; filename=\"$filename\"");
211 // application/octet-stream forces download for Netscape
212 header("Content-Type: application/octet-stream; name=\"$filename\"");