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