be8ee42ae3c775ca86c5cab940f1d52d9a128291
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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
15 require_once('../src/validate.php');
16 require_once('../functions/imap.php');
17 require_once('../functions/mime.php');
18 require_once('../functions/date.php');
19
20 header('Pragma: ');
21 header('Cache-Control: cache');
22
23 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) {
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 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
50 $body = MagicHTML( $body, $id, $msg );
51 } else {
52 translateText($body, $wrap_at, $charset);
53 }
54
55 flush();
56 echo $body .
57 "</TT></TD></TR></TABLE>";
58 }
59
60 function viewMessage($imapConnection, $id, $mailbox, $ent_id, $color, $wrap_at, $extracted) {
61 global $startMessage;
62
63
64 $msg = sqimap_get_message($imapConnection, $id, $mailbox);
65 $msg = getEntity($msg, $ent_id);
66
67 $header = sqimap_get_ent_header($imapConnection,$id,$mailbox,$ent_id);
68 $header->id = $id;
69 $msg->header = $header;
70
71 $ent_ar = findDisplayEntity($msg, 0);
72 $body = '';
73 for ($i = 0; $i < count($ent_ar); $i++) {
74 $body .= formatBody($imapConnection, $msg, $color, $wrap_at, $ent_ar[$i], false);
75 }
76
77 $bodyheader = viewHeader($header, $color);
78 displayPageHeader($color, 'None');
79
80 echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER>";
81 if ($extracted) {
82 echo '<TR><TD width="100%"><center><h1>Message succesfully extracted</h1></center></TD></TR>';
83 }
84 echo "<TR><TD BGCOLOR=\"$color[0]\">".
85 "<B><CENTER>". _("Viewing a message attachment") . " - ";
86
87 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
88
89 $urlmailbox = urlencode($mailbox);
90
91 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_id&mailbox=$urlmailbox\">".
92 _("Download this as a file").
93 "</A></CENTER><BR>".
94 "</CENTER></B>".
95 "</TD></TR></TABLE>";
96 echo "<TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
97 "<TR><TD BGCOLOR=\"$color[4]\">";
98 echo "$bodyheader </TD></TR></TABLE>";
99
100 echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
101 "<TR><TD BGCOLOR=\"$color[4]\"><TT><BR>";
102 echo "$body </TT></TD></TR><table><br>";
103 echo '<table width="100%"><tr>'.
104 "<td bgcolor=\"$color[9]\" width=\"100%\" align=\"center\">".
105 '<form action="download.php" method="post"><small>'.
106 "<input type=\"hidden\" name=\"passed_id\" value=\"$id\">".
107 "<input type=\"hidden\" name=\"mailbox\" value=\"".$mailbox."\">".
108 "<input type=\"hidden\" name=\"startMessage\" value=\"$startMessage\">".
109 "<input type=\"hidden\" name=\"passed_ent_id\" value=\"$ent_id\">".
110 "<input type=\"hidden\" name=\"extract_message\" value=\"1\">".
111 _("Save to:") .
112 ' <select name="targetMailbox">';
113 get_extract_to_target_list($imapConnection);
114 echo '</select> '.'&nbsp'.
115 '<input type="submit" value="' . _("Extract") . '">'.
116 '</small>'.
117 '</form>'.
118 '</td></table>';
119
120 }
121
122 function get_extract_to_target_list($imapConnection) {
123
124 $boxes = sqimap_mailbox_list($imapConnection);
125 for ($i = 0; $i < count($boxes); $i++) {
126 if (!in_array('noselect', $boxes[$i]['flags'])) {
127 $box = $boxes[$i]['unformatted'];
128 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
129 if ( $box2 == 'INBOX' ) {
130 $box2 = _("INBOX");
131 }
132 echo "<option value=\"$box\">$box2</option>\n";
133 }
134 }
135 }
136
137
138 function viewHeader($header,$color) {
139
140 $bodyheader = '';
141
142 /** FORMAT THE FROM STRING **/
143 $from_name = decodeHeader(htmlspecialchars($header->from));
144 if(isset($from_name) && $from_name !='') {
145 $bodyheader .= makeTableEntry($from_name,_("From"), $color);
146 }
147
148 $subject_string = decodeHeader(htmlspecialchars($header->subject));
149 if(isset($subject_string) && $subject_string !='') {
150 $bodyheader .= makeTableEntry($subject_string,_("Subject:"), $color);
151 }
152 /** FORMAT THE TO STRING **/
153 $to = formatRecipientString($header->to, "to");
154 $to_string = $to['str'];
155 $url_to_string = $to['url_str'];
156 if(isset($to_string) && $to_string !='') {
157 $bodyheader .= makeTableEntry($to_string,_("To:"), $color);
158 }
159
160 /** FORMAT THE DATE STRING **/
161 $dateString = getLongDateString($header->date);
162 if(isset($dateString) && $dateString !='') {
163 $bodyheader .= makeTableEntry($dateString,_("Date:"), $color);
164 }
165
166 /** FORMAT THE CC STRING **/
167 $cc = formatRecipientString($header->cc, "cc");
168 $cc_string = $cc['str'];
169 $url_cc_string = $cc['url_str'];
170 if(isset($cc_string) && $cc_string !='') {
171 $bodyheader .= makeTableEntry($cc_string,_("Cc:"), $color);
172 }
173
174 /** FORMAT THE BCC STRING **/
175 $bcc = formatRecipientString($header->bcc, "bcc");
176 $bcc_string = $bcc['str'];
177 $url_bcc_string = $bcc['url_str'];
178 if(isset($bcc_string) && $bcc_string !='') {
179 $bodyheader .= makeTableEntry($bcc_string,_("Bcc:"), $color);
180 }
181
182 return $bodyheader;
183 }
184
185 function makeTableEntry($str, $str_name, $color) {
186 $entry = '<tr><td bgcolor="'."$color[0]".'" align right valign top>'."$str_name".'</td><td bgcolor="'."$color[0]".
187 '" valign top colspan=2><b>'."$str".'</b>&nbsp;</td></tr>'."\n";
188 return $entry;
189 }
190
191 function formatRecipientString($recipients, $item ) {
192 global $base_uri, $passed_id, $startMessage, $show_more_cc, $show_more, $show_more_bcc, $passed_ent_id;
193 global $where, $what, $mailbox, $sort;
194
195 /** TEXT STRINGS DEFINITIONS **/
196 $echo_more = _("more");
197 $echo_less = _("less");
198
199 if (!isset($show_more_cc)) {
200 $show_more_cc = FALSE;
201 }
202 if (!isset($show_more_bcc)) {
203 $show_more_bcc = FALSE;
204 }
205
206
207 $urlMailbox = urlencode($mailbox);
208 $i = 0;
209 $url_string = '';
210
211 if (isset ($recipients[0]) && trim($recipients[0])) {
212 $string = '';
213 $ary = explode(",",$recipients[0]);
214
215 switch ($item) {
216 case 'to':
217 $show = "&amp;show_more=1&amp;show_more_cc=$show_more_cc&amp;show_more_bcc=$show_more_bcc";
218 $show_n = "&amp;show_more=0&amp;show_more_cc=$show_more_cc&amp;show_more_bcc=$show_more_bcc";
219 break;
220 case 'cc':
221 $show = "&amp;show_more=$show_more&amp;show_more_cc=1&amp;show_more_bcc=$show_more_bcc";
222 $show_n = "&amp;show_more=$show_more&amp;show_more_cc=0&amp;show_more_bcc=$show_more_bcc";
223 $show_more = $show_more_cc;
224 break;
225 case 'bcc':
226 $show = "&amp;show_more=$show_more&amp;show_more_cc=$show_more_cc&amp;show_more_bcc=1";
227 $show_n = "&amp;show_more=$show_more&amp;show_more_cc=$show_more_cc&amp;show_more_bcc=0";
228 $show_more = $show_more_bcc;
229 break;
230 default:
231 $break;
232 }
233
234 while ($i < count($ary)) {
235 $ary[$i] = htmlspecialchars(decodeHeader($ary[$i]));
236 $url_string .= $ary[$i];
237 if ($string) {
238 $string = "$string<BR>$ary[$i]";
239 } else {
240 $string = "$ary[$i]";
241 }
242
243 $i++;
244 if (count($ary) > 1) {
245 if ($show_more == false) {
246 if ($i == 1) {
247
248 $string .= '&nbsp;(<A HREF="' . $base_uri .
249 "src/download.php?mailbox=$urlMailbox&amp;passed_id=$passed_id&amp;";
250 if (isset($where) && isset($what)) {
251 $string .= 'what=' . urlencode($what)."&amp;where=".urlencode($where)."&amp;passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
252 } else {
253 $string .= "sort=$sort&amp;startMessage=$startMessage"."&amp;passed_ent_id=$passed_ent_id$show\">$echo_more</A>)";
254 }
255 $i = count($ary);
256 }
257 } else if ($i == 1) {
258
259 $string .= '&nbsp;(<A HREF="' . $base_uri .
260 "src/download.php?mailbox=$urlMailbox&amp;passed_id=$passed_id&amp;";
261 if (isset($where) && isset($what)) {
262 $string .= 'what=' . urlencode($what)."&amp;where=".urlencode($where)."&amp;passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
263 } else {
264 $string .= "sort=$sort&amp;startMessage=$startMessage"."&amp;passed_ent_id=$passed_ent_id$show_n\">$echo_less</A>)";
265 }
266 }
267 }
268
269 }
270 }
271 else {
272 $string = '';
273 }
274 $url_string = urlencode($url_string);
275 $result = array();
276 $result['str'] = $string;
277 $result['url_str'] = $url_string;
278 return $result;
279
280 }
281
282
283 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
284 sqimap_mailbox_select($imapConnection, $mailbox);
285
286 $extracted = false;
287 if (isset($extract_message) && $extract_message) {
288 $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
289 $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message);
290 $cnt = count($read);
291 $body = '';
292 $length = 0;
293 for ($i=1;$i<$cnt;$i++) {
294 $length = $length + strlen($read[$i]);
295 $body .= $read[$i];
296 }
297 if (isset($targetMailbox) && $length>0) {
298 sqimap_append ($imapConnection, $targetMailbox, $length);
299 fputs($imapConnection,$body);
300 sqimap_append_done ($imapConnection);
301 $extracted = true;
302 }
303 }
304
305 if (isset($showHeaders)) {
306 $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
307 }
308 /*
309 * lets redefine message as this particular entity that we wish to display.
310 * it should hold only the header for this entity. We need to fetch the body
311 * yet before we can display anything.
312 */
313
314 $header = sqimap_get_mime_ent_header ($imapConnection, $passed_id, $mailbox, $passed_ent_id);
315 $header->entity_id = $passed_ent_id;
316 $header->mailbox = $mailbox;
317
318 $charset = $header->charset;
319 $type0 = $header->type0;
320 $type1 = $header->type1;
321 if (isset($override_type0)) {
322 $type0 = $override_type0;
323 }
324 if (isset($override_type1)) {
325 $type1 = $override_type1;
326 }
327 $filename = decodeHeader($header->filename);
328 if (!$filename) {
329 $filename = decodeHeader($header->name);
330 }
331
332 if (strlen($filename) < 1) {
333 if ($type1 == 'plain' && $type0 == 'text') {
334 $suffix = 'txt';
335 } else if ($type1 == 'richtext' && $type0 == 'text') {
336 $suffix = 'rtf';
337 } else if ($type1 == 'postscript' && $type0 == 'application') {
338 $suffix = 'ps';
339 } else if ($type1 == 'rfc822' && $type0 == 'message') {
340 $suffix = 'eml';
341 } else {
342 $suffix = $type1;
343 }
344
345 $filename = "untitled$passed_ent_id.$suffix";
346 }
347
348
349 /*
350 * Note:
351 * The following sections display the attachment in different
352 * ways depending on how they choose. The first way will download
353 * under any circumstance. This sets the Content-type to be
354 * applicatin/octet-stream, which should be interpreted by the
355 * browser as "download me".
356 * The second method (view) is used for images or other formats
357 * that should be able to be handled by the browser. It will
358 * most likely display the attachment inline inside the browser.
359 * And finally, the third one will be used by default. If it
360 * is displayable (text or html), it will load them up in a text
361 * viewer (built in to squirrelmail). Otherwise, it sets the
362 * content-type as application/octet-stream
363 */
364 if (isset($absolute_dl) && $absolute_dl == 'true') {
365 switch($type0) {
366 case 'text':
367 DumpHeaders($type0, $type1, $filename, 1);
368 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
369 $body = decodeBody($body, $header->encoding);
370 if ($type1 == 'plain' && isset($showHeaders)) {
371 echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n".
372 " " . _("From") . ": " . decodeHeader($top_header->from) . "\n".
373 " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n".
374 " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
375 } elseif ($type1 == 'html' && isset($showHeaders)) {
376 echo '<table><tr><th align=right>' . _("Subject").
377 ':</th><td>' . decodeHeader($top_header->subject).
378 "</td></tr>\n<tr><th align=right>" . _("From").
379 ':</th><td>' . decodeHeader($top_header->from).
380 "</td></tr>\n<tr><th align=right>" . _("To").
381 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)).
382 "</td></tr>\n<tr><th align=right>" . _("Date").
383 ':</th><td>' . getLongDateString($top_header->date).
384 "</td></tr>\n</table>\n<hr>\n";
385 }
386 echo $body;
387 break;
388
389 default:
390 DumpHeaders($type0, $type1, $filename, 1);
391 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
392 break;
393 }
394 } else {
395 switch ($type0) {
396 case 'text':
397 if ($type1 == 'plain' || $type1 == 'html') {
398 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
399 $body = decodeBody($body, $header->encoding);
400 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
401 } else {
402 DumpHeaders($type0, $type1, $filename, 0);
403 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
404 $body = decodeBody($body, $header->encoding);
405 echo $body;
406 }
407 break;
408 case 'message':
409 if ($type1 == 'rfc822' ) {
410 viewMessage($imapConnection, $passed_id, $mailbox, $passed_ent_id, $color, $wrap_at, $extracted);
411 } else {
412 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
413 $body = decodeBody($body, $msgheader->encoding);
414 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
415 }
416 break;
417 default:
418 DumpHeaders($type0, $type1, $filename, 0);
419 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
420 break;
421 }
422 }
423
424
425 /*
426 * This function is verified to work with Netscape and the *very latest*
427 * version of IE. I don't know if it works with Opera, but it should now.
428 */
429 function DumpHeaders($type0, $type1, $filename, $force) {
430 global $HTTP_USER_AGENT;
431
432 $isIE = 0;
433
434 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
435 strstr($HTTP_USER_AGENT, 'Opera') === false) {
436 $isIE = 1;
437 }
438
439 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
440 strstr($HTTP_USER_AGENT, 'Opera') === false) {
441 $isIE6 = 1;
442 }
443
444 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
445
446 // A Pox on Microsoft and it's Office!
447 if (! $force) {
448 // Try to show in browser window
449 header("Content-Disposition: inline; filename=\"$filename\"");
450 header("Content-Type: $type0/$type1; name=\"$filename\"");
451 } else {
452 // Try to pop up the "save as" box
453 // IE makes this hard. It pops up 2 save boxes, or none.
454 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
455 // But, accordint to Microsoft, it is "RFC compliant but doesn't
456 // take into account some deviations that allowed within the
457 // specification." Doesn't that mean RFC non-compliant?
458 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
459 //
460 // The best thing you can do for IE is to upgrade to the latest
461 // version
462 if ($isIE && !isset($isIE6)) {
463 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
464 // Do not have quotes around filename, but that applied to
465 // "attachment"... does it apply to inline too?
466 //
467 // This combination seems to work mostly. IE 5.5 SP 1 has
468 // known issues (see the Microsoft Knowledge Base)
469 header("Content-Disposition: inline; filename=$filename");
470
471 // This works for most types, but doesn't work with Word files
472 header("Content-Type: application/download; name=\"$filename\"");
473
474 // These are spares, just in case. :-)
475 //header("Content-Type: $type0/$type1; name=\"$filename\"");
476 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
477 //header("Content-Type: application/octet-stream; name=\"$filename\"");
478 } else {
479 header("Content-Disposition: attachment; filename=\"$filename\"");
480 // application/octet-stream forces download for Netscape
481 header("Content-Type: application/octet-stream; name=\"$filename\"");
482 }
483 }
484 }
485 ?>