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