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');
23 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
24 global $where, $what, $charset;
27 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 $body = MagicHTML( $body, $id );
51 translateText($body, $wrap_at, $charset);
56 "</TT></TD></TR></TABLE>";
59 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
60 sqimap_mailbox_select($imapConnection, $mailbox);
63 * $message contains all information about the message
64 * including header and body
66 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
67 $top_header = $message->header
;
70 * lets redefine message as this particular entity that we wish to display.
71 * it should hold only the header for this entity. We need to fetch the body
72 * yet before we can display anything.
74 $message = getEntity($message, $passed_ent_id);
76 $header = $message->header
;
78 $charset = $header->charset
;
79 $type0 = $header->type0
;
80 $type1 = $header->type1
;
81 if (isset($override_type0)) {
82 $type0 = $override_type0;
84 if (isset($override_type1)) {
85 $type1 = $override_type1;
87 $filename = decodeHeader($header->filename
);
89 $filename = decodeHeader($header->name
);
92 if (strlen($filename) < 1) {
93 if ($type1 == 'plain' && $type0 == 'text') {
95 } else if ($type1 == 'richtext' && $type0 == 'text') {
97 } else if ($type1 == 'postscript' && $type0 == 'application') {
99 } else if ($type1 == 'message' && $type0 == 'rfc822') {
105 $filename = "untitled$passed_ent_id.$suffix";
110 * The following sections display the attachment in different
111 * ways depending on how they choose. The first way will download
112 * under any circumstance. This sets the Content-type to be
113 * applicatin/octet-stream, which should be interpreted by the
114 * browser as "download me".
115 * The second method (view) is used for images or other formats
116 * that should be able to be handled by the browser. It will
117 * most likely display the attachment inline inside the browser.
118 * And finally, the third one will be used by default. If it
119 * is displayable (text or html), it will load them up in a text
120 * viewer (built in to squirrelmail). Otherwise, it sets the
121 * content-type as application/octet-stream
123 if (isset($absolute_dl) && $absolute_dl == 'true') {
126 DumpHeaders($type0, $type1, $filename, 1);
127 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
128 $body = decodeBody($body, $header->encoding
);
129 if ($type1 == 'plain' && isset($showHeaders)) {
130 echo _("Subject") . ": " . decodeHeader($top_header->subject
) . "\n".
131 " " . _("From") . ": " . decodeHeader($top_header->from
) . "\n".
132 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to
)) . "\n".
133 " " . _("Date") . ": " . getLongDateString($top_header->date
) . "\n\n";
134 } elseif ($type1 == 'html' && isset($showHeaders)) {
135 echo '<table><tr><th align=right>' . _("Subject").
136 ':</th><td>' . decodeHeader($top_header->subject
).
137 "</td></tr>\n<tr><th align=right>" . _("From").
138 ':</th><td>' . decodeHeader($top_header->from
).
139 "</td></tr>\n<tr><th align=right>" . _("To").
140 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to
)).
141 "</td></tr>\n<tr><th align=right>" . _("Date").
142 ':</th><td>' . getLongDateString($top_header->date
).
143 "</td></tr>\n</table>\n<hr>\n";
148 DumpHeaders($type0, $type1, $filename, 1);
149 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
155 if ($type1 == 'plain' ||
$type1 == 'html') {
156 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
157 $body = decodeBody($body, $header->encoding
);
158 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
160 DumpHeaders($type0, $type1, $filename, 0);
161 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
162 $body = decodeBody($body, $header->encoding
);
167 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
168 $body = decodeBody($body, $header->encoding
);
169 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
172 DumpHeaders($type0, $type1, $filename, 0);
173 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding
);
180 * This function is verified to work with Netscape and the *very latest*
181 * version of IE. I don't know if it works with Opera, but it should now.
183 function DumpHeaders($type0, $type1, $filename, $force) {
184 global $HTTP_USER_AGENT;
187 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
188 strstr($HTTP_USER_AGENT, 'Opera') === false) {
192 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
194 // A Pox on Microsoft and it's Office!
196 // Try to show in browser window
197 header("Content-Disposition: inline; filename=\"$filename\"");
198 header("Content-Type: $type0/$type1; name=\"$filename\"");
200 // Try to pop up the "save as" box
201 // IE makes this hard. It pops up 2 save boxes, or none.
202 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
203 // But, accordint to Microsoft, it is "RFC compliant but doesn't
204 // take into account some deviations that allowed within the
205 // specification." Doesn't that mean RFC non-compliant?
206 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
208 // The best thing you can do for IE is to upgrade to the latest
211 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
212 // Do not have quotes around filename, but that applied to
213 // "attachment"... does it apply to inline too?
215 // This combination seems to work mostly. IE 5.5 SP 1 has
216 // known issues (see the Microsoft Knowledge Base)
217 header("Content-Disposition: inline; filename=$filename");
219 // This works for most types, but doesn't work with Word files
220 header("Content-Type: application/download; name=\"$filename\"");
222 // These are spares, just in case. :-)
223 //header("Content-Type: $type0/$type1; name=\"$filename\"");
224 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
225 //header("Content-Type: application/octet-stream; name=\"$filename\"");
227 header("Content-Disposition: attachment; filename=\"$filename\"");
228 // application/octet-stream forces download for Netscape
229 header("Content-Type: application/octet-stream; name=\"$filename\"");