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 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
21 header('Cache-Control: cache');
24 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) {
25 global $where, $what, $charset;
28 displayPageHeader($color, 'None');
29 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
31 _("Viewing a text attachment") . " - ";
32 if ($where && $what) {
34 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
36 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
39 $urlmailbox = urlencode($mailbox);
40 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">".
41 _("Download this as a file").
45 "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
46 "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
48 if ($type1 == 'html') {
49 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
50 $msg = $msg->getEntity($entid);
51 $body = MagicHTML( $body, $id, $msg );
53 translateText($body, $wrap_at, $charset);
58 "</TT></TD></TR></TABLE>";
61 function get_extract_to_target_list($imapConnection) {
62 $boxes = sqimap_mailbox_list($imapConnection);
63 for ($i = 0; $i < count($boxes); $i++
) {
64 if (!in_array('noselect', $boxes[$i]['flags'])) {
65 $box = $boxes[$i]['unformatted'];
66 $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']);
67 if ( $box2 == 'INBOX' ) {
70 echo "<option value=\"$box\">$box2</option>\n";
74 $mailbox = decodeHeader($mailbox);
76 global $messages, $uid_support;
80 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
81 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
83 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
84 $message = &$message->getEntity($passed_ent_id);
86 $header = $message->header
;
87 $charset = $header->charset
;
88 $type0 = $header->type0
;
89 $type1 = $header->type1
;
90 $encoding = strtolower($header->encoding
);
97 if (isset($extract_message) && $extract_message) {
98 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
99 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message, $uid_support);
103 for ($i=1;$i<$cnt;$i++) {
104 $length = $length + strlen($read[$i]);
107 if (isset($targetMailbox) && $length>0) {
108 sqimap_append ($imapConnection, $targetMailbox, $length);
109 fputs($imapConnection,$body);
110 sqimap_append_done ($imapConnection);
115 if (isset($showHeaders)) {
116 $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
121 * lets redefine message as this particular entity that we wish to display.
122 * it should hold only the header for this entity. We need to fetch the body
123 * yet before we can display anything.
126 if (isset($override_type0)) {
127 $type0 = $override_type0;
129 if (isset($override_type1)) {
130 $type1 = $override_type1;
133 $filename = decodeHeader($message->header
->filename
);
136 $filename = decodeHeader($message->header
->name
);
139 if (strlen($filename) < 1) {
140 if ($type1 == 'plain' && $type0 == 'text') {
142 } else if ($type1 == 'richtext' && $type0 == 'text') {
144 } else if ($type1 == 'postscript' && $type0 == 'application') {
146 } else if ($type1 == 'rfc822' && $type0 == 'message') {
152 $filename = "untitled$passed_ent_id.$suffix";
157 * The following sections display the attachment in different
158 * ways depending on how they choose. The first way will download
159 * under any circumstance. This sets the Content-type to be
160 * applicatin/octet-stream, which should be interpreted by the
161 * browser as "download me".
162 * The second method (view) is used for images or other formats
163 * that should be able to be handled by the browser. It will
164 * most likely display the attachment inline inside the browser.
165 * And finally, the third one will be used by default. If it
166 * is displayable (text or html), it will load them up in a text
167 * viewer (built in to squirrelmail). Otherwise, it sets the
168 * content-type as application/octet-stream
170 if (isset($absolute_dl) && $absolute_dl == 'true') {
173 DumpHeaders($type0, $type1, $filename, 1);
174 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
175 $body = decodeBody($body, $encoding);
176 if ($type1 == 'plain' && isset($showHeaders)) {
177 echo _("Subject") . ": " . decodeHeader($top_header->subject
) . "\n".
178 " " . _("From") . ": " . decodeHeader($top_header->from
) . "\n".
179 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to
)) . "\n".
180 " " . _("Date") . ": " . getLongDateString($top_header->date
) . "\n\n";
181 } elseif ($type1 == 'html' && isset($showHeaders)) {
182 echo '<table><tr><th align=right>' . _("Subject").
183 ':</th><td>' . decodeHeader($top_header->subject
).
184 "</td></tr>\n<tr><th align=right>" . _("From").
185 ':</th><td>' . decodeHeader($top_header->from
).
186 "</td></tr>\n<tr><th align=right>" . _("To").
187 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to
)).
188 "</td></tr>\n<tr><th align=right>" . _("Date").
189 ':</th><td>' . getLongDateString($top_header->date
).
190 "</td></tr>\n</table>\n<hr>\n";
195 DumpHeaders($type0, $type1, $filename, 1);
196 // if ($message->decoded_body == '') {
197 // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding));
199 // echo $message->decoded_body;
201 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding);
207 if ($type1 == 'plain' ||
$type1 == 'html') {
208 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
209 $body = decodeBody($body, $encoding);
210 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
212 DumpHeaders($type0, $type1, $filename, 0);
213 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
214 $body = decodeBody($body, $encoding);
219 DumpHeaders($type0, $type1, $filename, 0);
220 // if ($message->decoded_body == '') {
221 // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding));
224 // echo $message->decoded_body;
225 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding);
230 $message = &$message->getEntity('');
231 $messages[$mbx_response['UIDVALIDITY']]["$passed_id"] = &$message;
234 * This function is verified to work with Netscape and the *very latest*
235 * version of IE. I don't know if it works with Opera, but it should now.
237 function DumpHeaders($type0, $type1, $filename, $force) {
238 global $HTTP_USER_AGENT;
242 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
243 strstr($HTTP_USER_AGENT, 'Opera') === false) {
247 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
248 strstr($HTTP_USER_AGENT, 'Opera') === false) {
252 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
254 // A Pox on Microsoft and it's Office!
256 // Try to show in browser window
257 header("Content-Disposition: inline; filename=\"$filename\"");
258 header("Content-Type: $type0/$type1; name=\"$filename\"");
260 // Try to pop up the "save as" box
261 // IE makes this hard. It pops up 2 save boxes, or none.
262 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
263 // But, accordint to Microsoft, it is "RFC compliant but doesn't
264 // take into account some deviations that allowed within the
265 // specification." Doesn't that mean RFC non-compliant?
266 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
268 // The best thing you can do for IE is to upgrade to the latest
270 if ($isIE && !isset($isIE6)) {
271 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
272 // Do not have quotes around filename, but that applied to
273 // "attachment"... does it apply to inline too?
275 // This combination seems to work mostly. IE 5.5 SP 1 has
276 // known issues (see the Microsoft Knowledge Base)
277 header("Content-Disposition: inline; filename=$filename");
279 // This works for most types, but doesn't work with Word files
280 header("Content-Type: application/download; name=\"$filename\"");
282 // These are spares, just in case. :-)
283 //header("Content-Type: $type0/$type1; name=\"$filename\"");
284 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
285 //header("Content-Type: application/octet-stream; name=\"$filename\"");
287 header("Content-Disposition: attachment; filename=\"$filename\"");
288 // application/octet-stream forces download for Netscape
289 header("Content-Type: application/octet-stream; name=\"$filename\"");