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