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