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