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