59177427 |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * download.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
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 | |
35586184 |
15 | require_once('../src/validate.php'); |
16 | require_once('../functions/imap.php'); |
17 | require_once('../functions/mime.php'); |
18 | require_once('../functions/date.php'); |
6b96544a |
19 | |
65c3ec94 |
20 | header('Pragma: '); |
21 | header('Cache-Control: cache'); |
22 | |
23 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
24 | global $where, $what, $charset; |
25 | global $startMessage; |
26 | |
27 | displayPageHeader($color, 'None'); |
28 | |
29 | echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">". |
30 | "<B><CENTER>". |
31 | _("Viewing a text attachment") . " - "; |
32 | if ($where && $what) { |
33 | // from a search |
34 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>"; |
35 | } else { |
36 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>"; |
37 | } |
38 | |
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"). |
42 | "</A></CENTER><BR>". |
43 | "</CENTER></B>". |
44 | "</TD></TR></TABLE>". |
45 | "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">". |
46 | "<TR><TD BGCOLOR=\"$color[4]\"><TT>"; |
47 | |
48 | if ($type1 == 'html') { |
49 | $body = MagicHTML( $body, $id ); |
50 | } else { |
51 | translateText($body, $wrap_at, $charset); |
52 | } |
53 | |
54 | flush(); |
55 | echo $body . |
56 | "</TT></TD></TR></TABLE>"; |
57 | } |
58 | |
59 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
60 | sqimap_mailbox_select($imapConnection, $mailbox); |
61 | |
62 | /* |
63 | * $message contains all information about the message |
64 | * including header and body |
65 | */ |
66 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
67 | $top_header = $message->header; |
68 | |
69 | /* |
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. |
73 | */ |
74 | $message = getEntity($message, $passed_ent_id); |
75 | |
76 | $header = $message->header; |
77 | |
78 | $charset = $header->charset; |
79 | $type0 = $header->type0; |
80 | $type1 = $header->type1; |
81 | if (isset($override_type0)) { |
82 | $type0 = $override_type0; |
83 | } |
84 | if (isset($override_type1)) { |
85 | $type1 = $override_type1; |
86 | } |
87 | $filename = decodeHeader($header->filename); |
88 | if (!$filename) { |
89 | $filename = decodeHeader($header->name); |
90 | } |
91 | |
92 | if (strlen($filename) < 1) { |
93 | if ($type1 == 'plain' && $type0 == 'text') { |
94 | $suffix = 'txt'; |
95 | } else if ($type1 == 'richtext' && $type0 == 'text') { |
96 | $suffix = 'rtf'; |
97 | } else if ($type1 == 'postscript' && $type0 == 'application') { |
98 | $suffix = 'ps'; |
99 | } else if ($type1 == 'message' && $type0 == 'rfc822') { |
100 | $suffix = 'msg'; |
101 | } else { |
102 | $suffix = $type1; |
103 | } |
104 | |
105 | $filename = "untitled$passed_ent_id.$suffix"; |
106 | } |
107 | |
108 | /* |
109 | * Note: |
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 |
122 | */ |
123 | if (isset($absolute_dl) && $absolute_dl == 'true') { |
124 | switch($type0) { |
125 | case 'text': |
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"; |
144 | } |
145 | echo $body; |
146 | break; |
147 | default: |
148 | DumpHeaders($type0, $type1, $filename, 1); |
149 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
150 | break; |
151 | } |
152 | } else { |
153 | switch ($type0) { |
154 | case 'text': |
155 | if ($type1 == 'plain' || $type1 == 'html') { |
beb9e459 |
156 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
157 | $body = decodeBody($body, $header->encoding); |
65c3ec94 |
158 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
159 | } else { |
160 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
161 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
162 | $body = decodeBody($body, $header->encoding); |
65c3ec94 |
163 | echo $body; |
164 | } |
165 | break; |
166 | case 'message': |
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); |
170 | break; |
171 | default: |
172 | DumpHeaders($type0, $type1, $filename, 0); |
173 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
174 | break; |
175 | } |
176 | } |
177 | |
178 | |
179 | /* |
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. |
182 | */ |
183 | function DumpHeaders($type0, $type1, $filename, $force) { |
184 | global $HTTP_USER_AGENT; |
185 | |
186 | $isIE = 0; |
187 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
188 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
36294f1a |
189 | $isIE = 1; |
65c3ec94 |
190 | } |
191 | |
192 | $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename); |
193 | |
194 | // A Pox on Microsoft and it's Office! |
195 | if (! $force) { |
196 | // Try to show in browser window |
197 | header("Content-Disposition: inline; filename=\"$filename\""); |
198 | header("Content-Type: $type0/$type1; name=\"$filename\""); |
199 | } else { |
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 |
207 | // |
208 | // The best thing you can do for IE is to upgrade to the latest |
209 | // version |
210 | if ($isIE) { |
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? |
214 | // |
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"); |
218 | |
219 | // This works for most types, but doesn't work with Word files |
220 | header("Content-Type: application/download; name=\"$filename\""); |
221 | |
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\""); |
226 | } else { |
227 | header("Content-Disposition: attachment; filename=\"$filename\""); |
228 | // application/octet-stream forces download for Netscape |
229 | header("Content-Type: application/octet-stream; name=\"$filename\""); |
230 | } |
231 | } |
232 | } |
35586184 |
233 | ?> |