changed version to: 1.0pre3 [cvs]
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
ef870322 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
d068c0ec 12 if (!isset($config_php))
13 include("../config/config.php");
14 if (!isset($strings_php))
15 include("../functions/strings.php");
d068c0ec 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");
441f2d33 22 if (!isset($i18n_php))
23 include("../functions/i18n.php");
6b96544a 24
d3cdb279 25 include("../src/load_prefs.php");
26
11307a4c 27 function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
8442ac08 28 global $where, $what, $charset;
a037624d 29 global $startMessage;
fdcca8d3 30
7831268e 31 displayPageHeader($color, "None");
32
a037624d 33 echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
180c2c09 34 echo "<B><CENTER>";
aa8dcbd5 35 echo _("Viewing a text attachment") . " - ";
f4991a86 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 }
a037624d 42
7831268e 43 $urlmailbox = urlencode($mailbox);
a037624d 44 echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">";
180c2c09 45 echo _("Download this as a file");
a037624d 46 echo "</A></CENTER><BR>";
47 echo "</CENTER></B>";
48 echo "</TD></TR></TABLE>";
49
50 echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">";
51 echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
52
9eea179c 53 if ($type1 != "html")
54 translateText($body, $wrap_at, $charset);
55
56 echo $body;
0a4cf77a 57
7831268e 58 echo "</TT></TD></TR></TABLE>";
59 }
60
e1469126 61 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
813eba2f 62 sqimap_mailbox_select($imapConnection, $mailbox);
6b96544a 63
64 // $message contains all information about the message
65 // including header and body
813eba2f 66 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
ca1a555e 67 $top_header = $message->header;
6b96544a 68
8beafbbc 69 // lets redefine message as this particular entity that we wish to display.
70 // it should hold only the header for this entity. We need to fetch the body
71 // yet before we can display anything.
72 $message = getEntity($message, $passed_ent_id);
73
74 $header = $message->header;
8beafbbc 75
8442ac08 76 $charset = $header->charset;
8beafbbc 77 $type0 = $header->type0;
78 $type1 = $header->type1;
f9ab17bf 79 $filename = decodeHeader($header->filename);
6b96544a 80
7831268e 81 if (strlen($filename) < 1) {
e9f8ea4e 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";
7831268e 89 }
6b96544a 90
c4809aca 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
b67e316d 105 header("Pragma: ");
106 header("Content-Description: SquirrelMail Attachment");
7831268e 107 if ($absolute_dl == "true") {
108 switch($type0) {
109 case "text":
beb9e459 110 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
8beafbbc 111 $body = decodeBody($body, $header->encoding);
f1f3803a 112 header("Content-Disposition: filename=\"$filename\"");
a48fbf9b 113 header("Content-type: application/octet-stream; name=\"$filename\"");
441f2d33 114 set_up_language(getPref($data_dir, $username, "language"));
623332f3 115 if ($type1 == "plain") {
7aaa81fc 116 echo _("Subject") . ": " . decodeHeader(sqStripSlashes($top_header->subject)) . "\n";
117 echo " " . _("From") . ": " . decodeHeader(sqStripSlashes($top_header->from)) . "\n";
118 echo " " . _("To") . ": " . decodeHeader(sqStripSlashes(getLineOfAddrs($top_header->to))) . "\n";
ca1a555e 119 echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
623332f3 120 }
d3cdb279 121 echo trim($body);
7831268e 122 break;
123 default:
f1f3803a 124 header("Content-Disposition: filename=$filename");
a48fbf9b 125 header("Content-type: application/octet-stream; name=\"$filename\"");
beb9e459 126 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
7831268e 127 break;
128 }
129 } else {
130 switch ($type0) {
131 case "text":
beb9e459 132 if ($type1 == "plain" || $type1 == "html") {
133 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
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);
23a8095f 137 } else {
beb9e459 138 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
139 $body = decodeBody($body, $header->encoding);
140 header("Content-type: $type0/$type1; name=\"$filename\"");
f1f3803a 141 header("Content-Disposition: filename=\"$filename\"");
beb9e459 142 echo $body;
143 }
144 break;
7831268e 145 case "message":
beb9e459 146 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
8beafbbc 147 $body = decodeBody($body, $header->encoding);
fdcca8d3 148 include("../functions/page_header.php");
11307a4c 149 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
7831268e 150 break;
151 default:
a48fbf9b 152 header("Content-type: $type0/$type1; name=\"$filename\"");
f1f3803a 153 header("Content-Disposition: filename=\"$filename\"");
beb9e459 154 mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding);
7831268e 155 break;
156 }
b67e316d 157 }
158
813eba2f 159 sqimap_logout($imapConnection);
7831268e 160?>