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 | |
1e606225 |
23 | |
0f859719 |
24 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) { |
65c3ec94 |
25 | global $where, $what, $charset; |
26 | global $startMessage; |
27 | |
28 | displayPageHeader($color, 'None'); |
1e606225 |
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") . " - "; |
65c3ec94 |
32 | if ($where && $what) { |
33 | // from a search |
1e606225 |
34 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>"; |
65c3ec94 |
35 | } else { |
1e606225 |
36 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>"; |
65c3ec94 |
37 | } |
3530b83b |
38 | |
1e606225 |
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>"; |
65c3ec94 |
47 | |
48 | if ($type1 == 'html') { |
0f859719 |
49 | $msg = sqimap_get_message($imapConnection, $id, $mailbox); |
1e606225 |
50 | $msg = $msg->getEntity($entid); |
0f859719 |
51 | $body = MagicHTML( $body, $id, $msg ); |
65c3ec94 |
52 | } else { |
53 | translateText($body, $wrap_at, $charset); |
54 | } |
55 | |
56 | flush(); |
1e606225 |
57 | echo $body . |
58 | "</TT></TD></TR></TABLE>"; |
13b13ded |
59 | } |
60 | |
61 | function get_extract_to_target_list($imapConnection) { |
13b13ded |
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' ) { |
68 | $box2 = _("INBOX"); |
69 | } |
70 | echo "<option value=\"$box\">$box2</option>\n"; |
71 | } |
72 | } |
97d7da3b |
73 | } |
1e606225 |
74 | $mailbox = decodeHeader($mailbox); |
97d7da3b |
75 | |
1e606225 |
76 | global $messages, $uid_support; |
97d7da3b |
77 | |
97d7da3b |
78 | |
97d7da3b |
79 | |
1e606225 |
80 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
81 | $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox); |
97d7da3b |
82 | |
1e606225 |
83 | $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"]; |
84 | $message = &$message->getEntity($passed_ent_id); |
97d7da3b |
85 | |
1e606225 |
86 | $header = $message->header; |
87 | $charset = $header->charset; |
88 | $type0 = $header->type0; |
89 | $type1 = $header->type1; |
90 | $encoding = strtolower($header->encoding); |
97d7da3b |
91 | |
97d7da3b |
92 | |
93 | |
65c3ec94 |
94 | |
1e606225 |
95 | /* |
13b13ded |
96 | $extracted = false; |
97 | if (isset($extract_message) && $extract_message) { |
98 | $cmd = "FETCH $passed_id BODY[$passed_ent_id]"; |
1e606225 |
99 | $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message, $uid_support); |
13b13ded |
100 | $cnt = count($read); |
101 | $body = ''; |
102 | $length = 0; |
103 | for ($i=1;$i<$cnt;$i++) { |
104 | $length = $length + strlen($read[$i]); |
105 | $body .= $read[$i]; |
106 | } |
107 | if (isset($targetMailbox) && $length>0) { |
108 | sqimap_append ($imapConnection, $targetMailbox, $length); |
109 | fputs($imapConnection,$body); |
110 | sqimap_append_done ($imapConnection); |
111 | $extracted = true; |
112 | } |
113 | } |
114 | |
247240ed |
115 | if (isset($showHeaders)) { |
116 | $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox); |
117 | } |
1e606225 |
118 | |
119 | */ |
65c3ec94 |
120 | /* |
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. |
124 | */ |
65c3ec94 |
125 | |
65c3ec94 |
126 | if (isset($override_type0)) { |
127 | $type0 = $override_type0; |
128 | } |
129 | if (isset($override_type1)) { |
130 | $type1 = $override_type1; |
131 | } |
1e606225 |
132 | |
133 | $filename = decodeHeader($message->header->filename); |
134 | |
65c3ec94 |
135 | if (!$filename) { |
1e606225 |
136 | $filename = decodeHeader($message->header->name); |
65c3ec94 |
137 | } |
138 | |
139 | if (strlen($filename) < 1) { |
140 | if ($type1 == 'plain' && $type0 == 'text') { |
141 | $suffix = 'txt'; |
631db37e |
142 | $filename = $header->subject . '.txt'; |
65c3ec94 |
143 | } else if ($type1 == 'richtext' && $type0 == 'text') { |
144 | $suffix = 'rtf'; |
631db37e |
145 | $filename = $header->subject . '.rtf'; |
65c3ec94 |
146 | } else if ($type1 == 'postscript' && $type0 == 'application') { |
147 | $suffix = 'ps'; |
631db37e |
148 | $filename = $header->subject . '.ps'; |
97d7da3b |
149 | } else if ($type1 == 'rfc822' && $type0 == 'message') { |
150 | $suffix = 'eml'; |
631db37e |
151 | $filename = $header->subject . '.msg'; |
65c3ec94 |
152 | } else { |
153 | $suffix = $type1; |
154 | } |
155 | |
631db37e |
156 | if (strlen($filename) < 1) { |
65c3ec94 |
157 | $filename = "untitled$passed_ent_id.$suffix"; |
631db37e |
158 | } else { |
159 | $filename = "$filename.$suffix"; |
160 | } |
65c3ec94 |
161 | } |
162 | |
163 | /* |
164 | * Note: |
165 | * The following sections display the attachment in different |
166 | * ways depending on how they choose. The first way will download |
167 | * under any circumstance. This sets the Content-type to be |
168 | * applicatin/octet-stream, which should be interpreted by the |
169 | * browser as "download me". |
170 | * The second method (view) is used for images or other formats |
171 | * that should be able to be handled by the browser. It will |
172 | * most likely display the attachment inline inside the browser. |
173 | * And finally, the third one will be used by default. If it |
174 | * is displayable (text or html), it will load them up in a text |
175 | * viewer (built in to squirrelmail). Otherwise, it sets the |
176 | * content-type as application/octet-stream |
177 | */ |
178 | if (isset($absolute_dl) && $absolute_dl == 'true') { |
179 | switch($type0) { |
180 | case 'text': |
181 | DumpHeaders($type0, $type1, $filename, 1); |
182 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
1e606225 |
183 | $body = decodeBody($body, $encoding); |
65c3ec94 |
184 | if ($type1 == 'plain' && isset($showHeaders)) { |
185 | echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n". |
186 | " " . _("From") . ": " . decodeHeader($top_header->from) . "\n". |
187 | " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n". |
188 | " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
189 | } elseif ($type1 == 'html' && isset($showHeaders)) { |
1e606225 |
190 | echo '<table><tr><th align=right>' . _("Subject"). |
191 | ':</th><td>' . decodeHeader($top_header->subject). |
192 | "</td></tr>\n<tr><th align=right>" . _("From"). |
193 | ':</th><td>' . decodeHeader($top_header->from). |
194 | "</td></tr>\n<tr><th align=right>" . _("To"). |
195 | ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)). |
196 | "</td></tr>\n<tr><th align=right>" . _("Date"). |
197 | ':</th><td>' . getLongDateString($top_header->date). |
198 | "</td></tr>\n</table>\n<hr>\n"; |
97d7da3b |
199 | } |
65c3ec94 |
200 | echo $body; |
201 | break; |
202 | default: |
203 | DumpHeaders($type0, $type1, $filename, 1); |
1e606225 |
204 | // if ($message->decoded_body == '') { |
205 | // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding)); |
206 | // } |
207 | // echo $message->decoded_body; |
208 | |
209 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding); |
65c3ec94 |
210 | break; |
211 | } |
212 | } else { |
213 | switch ($type0) { |
214 | case 'text': |
215 | if ($type1 == 'plain' || $type1 == 'html') { |
beb9e459 |
216 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
1e606225 |
217 | $body = decodeBody($body, $encoding); |
0f859719 |
218 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection); |
65c3ec94 |
219 | } else { |
220 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
221 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
1e606225 |
222 | $body = decodeBody($body, $encoding); |
65c3ec94 |
223 | echo $body; |
224 | } |
225 | break; |
65c3ec94 |
226 | default: |
227 | DumpHeaders($type0, $type1, $filename, 0); |
1e606225 |
228 | // if ($message->decoded_body == '') { |
229 | // $message->setBody(mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding)); |
230 | // |
231 | // } |
232 | // echo $message->decoded_body; |
233 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $encoding); |
65c3ec94 |
234 | break; |
235 | } |
236 | } |
237 | |
1e606225 |
238 | $message = &$message->getEntity(''); |
239 | $messages[$mbx_response['UIDVALIDITY']]["$passed_id"] = &$message; |
65c3ec94 |
240 | |
241 | /* |
242 | * This function is verified to work with Netscape and the *very latest* |
243 | * version of IE. I don't know if it works with Opera, but it should now. |
244 | */ |
245 | function DumpHeaders($type0, $type1, $filename, $force) { |
246 | global $HTTP_USER_AGENT; |
65c3ec94 |
247 | $isIE = 0; |
97d7da3b |
248 | |
1e606225 |
249 | |
65c3ec94 |
250 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
251 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
36294f1a |
252 | $isIE = 1; |
65c3ec94 |
253 | } |
254 | |
97d7da3b |
255 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false && |
256 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
257 | $isIE6 = 1; |
258 | } |
259 | |
65c3ec94 |
260 | $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename); |
261 | |
262 | // A Pox on Microsoft and it's Office! |
263 | if (! $force) { |
264 | // Try to show in browser window |
265 | header("Content-Disposition: inline; filename=\"$filename\""); |
266 | header("Content-Type: $type0/$type1; name=\"$filename\""); |
267 | } else { |
268 | // Try to pop up the "save as" box |
269 | // IE makes this hard. It pops up 2 save boxes, or none. |
270 | // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP |
271 | // But, accordint to Microsoft, it is "RFC compliant but doesn't |
272 | // take into account some deviations that allowed within the |
273 | // specification." Doesn't that mean RFC non-compliant? |
274 | // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP |
275 | // |
276 | // The best thing you can do for IE is to upgrade to the latest |
277 | // version |
97d7da3b |
278 | if ($isIE && !isset($isIE6)) { |
65c3ec94 |
279 | // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp |
280 | // Do not have quotes around filename, but that applied to |
281 | // "attachment"... does it apply to inline too? |
282 | // |
283 | // This combination seems to work mostly. IE 5.5 SP 1 has |
284 | // known issues (see the Microsoft Knowledge Base) |
285 | header("Content-Disposition: inline; filename=$filename"); |
286 | |
287 | // This works for most types, but doesn't work with Word files |
288 | header("Content-Type: application/download; name=\"$filename\""); |
289 | |
290 | // These are spares, just in case. :-) |
291 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
292 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
293 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
294 | } else { |
295 | header("Content-Disposition: attachment; filename=\"$filename\""); |
296 | // application/octet-stream forces download for Netscape |
297 | header("Content-Type: application/octet-stream; name=\"$filename\""); |
298 | } |
299 | } |
300 | } |
631db37e |
301 | ?> |