removed error messages
[squirrelmail.git] / src / download.php
1 <?php
2 /**
3 ** download.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Handles attachment downloads to the users computer.
9 ** Also allows displaying of attachments when possible.
10 **
11 ** $Id$
12 **/
13
14 if (!isset($strings_php))
15 include("../functions/strings.php");
16 if (!isset($config_php))
17 include("../config/config.php");
18 if (!isset($imap_php))
19 include("../functions/imap.php");
20 if (!isset($mime_php))
21 include("../functions/mime.php");
22 if (!isset($date_php))
23 include("../functions/date.php");
24 if (!isset($i18n_php))
25 include("../functions/i18n.php");
26
27 session_start();
28 header("Pragma: ");
29 header("Cache-Control: cache");
30
31 include("../src/load_prefs.php");
32
33 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
34 global $where, $what, $charset;
35 global $startMessage;
36
37 displayPageHeader($color, "None");
38
39 echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
40 echo "<B><CENTER>";
41 echo _("Viewing a text attachment") . " - ";
42 if ($where && $what) {
43 // from a search
44 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
45 } else {
46 echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
47 }
48
49 $urlmailbox = urlencode($mailbox);
50 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">";
51 echo _("Download this as a file");
52 echo "</A></CENTER><BR>";
53 echo "</CENTER></B>";
54 echo "</TD></TR></TABLE>";
55
56 echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
57 echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
58
59 if ($type1 != "html")
60 translateText($body, $wrap_at, $charset);
61
62 echo $body;
63
64 echo "</TT></TD></TR></TABLE>";
65 }
66
67 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
68 sqimap_mailbox_select($imapConnection, $mailbox);
69
70 // $message contains all information about the message
71 // including header and body
72 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
73 $top_header = $message->header;
74
75 // lets redefine message as this particular entity that we wish to display.
76 // it should hold only the header for this entity. We need to fetch the body
77 // yet before we can display anything.
78 $message = getEntity($message, $passed_ent_id);
79
80 $header = $message->header;
81
82 $charset = $header->charset;
83 $type0 = $header->type0;
84 $type1 = $header->type1;
85 $filename = decodeHeader($header->filename);
86
87 if (strlen($filename) < 1) {
88 if ($type1 == "plain" && $type0 == "text") $suffix = "txt";
89 else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf";
90 else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps";
91 else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg";
92 else $suffix = $type1;
93
94 $filename = "untitled$passed_ent_id.$suffix";
95 }
96
97 // Note:
98 // The following sections display the attachment in different
99 // ways depending on how they choose. The first way will download
100 // under any circumstance. This sets the Content-type to be
101 // applicatin/octet-stream, which should be interpreted by the
102 // browser as "download me".
103 // The second method (view) is used for images or other formats
104 // that should be able to be handled by the browser. It will
105 // most likely display the attachment inline inside the browser.
106 // And finally, the third one will be used by default. If it
107 // is displayable (text or html), it will load them up in a text
108 // viewer (built in to squirrelmail). Otherwise, it sets the
109 // content-type as application/octet-stream
110
111 if (isset($absolute_dl) && $absolute_dl == "true") {
112 switch($type0) {
113 case "text":
114 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
115 $body = decodeBody($body, $header->encoding);
116 header("Content-Disposition: attachment; filename=\"$filename\"");
117 header("Content-type: application/octet-stream; name=\"$filename\"");
118 set_up_language(getPref($data_dir, $username, "language"));
119 if ($type1 == "plain") {
120 echo _("Subject") . ": " . decodeHeader(sqStripSlashes($top_header->subject)) . "\n";
121 echo " " . _("From") . ": " . decodeHeader(sqStripSlashes($top_header->from)) . "\n";
122 echo " " . _("To") . ": " . decodeHeader(sqStripSlashes(getLineOfAddrs($top_header->to))) . "\n";
123 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
124 }
125 echo trim($body);
126 break;
127 default:
128 header("Content-Disposition: attachment; filename=\"$filename\"");
129 header("Content-type: application/octet-stream; name=\"$filename\"");
130 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
131 break;
132 }
133 } else {
134 switch ($type0) {
135 case "text":
136 if ($type1 == "plain" || $type1 == "html") {
137 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
138 $body = decodeBody($body, $header->encoding);
139 include("../functions/page_header.php");
140 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
141 } else {
142 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
143 $body = decodeBody($body, $header->encoding);
144 header("Content-type: $type0/$type1; name=\"$filename\"");
145 header("Content-Disposition: attachment; filename=\"$filename\"");
146 echo $body;
147 }
148 break;
149 case "message":
150 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
151 $body = decodeBody($body, $header->encoding);
152 include("../functions/page_header.php");
153 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
154 break;
155 default:
156 header("Content-type: $type0/$type1; name=\"$filename\"");
157 header("Content-Disposition: attachment; filename=\"$filename\"");
158 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
159 break;
160 }
161 }
162
163 sqimap_logout($imapConnection);
164 ?>