DO NOT buffer base64 encoded attachments before we decode them. Instead
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * download.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handles attachment downloads to the users computer.
10 * Also allows displaying of attachments when possible.
11 *
12 * $Id$
13 */
14
86725763 15/* Path for SquirrelMail required files. */
16define('SM_PATH','../');
17
18/* SquirrelMail required files. */
08185f2a 19require_once(SM_PATH . 'include/validate.php');
86725763 20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/mime.php');
6b96544a 22
65c3ec94 23header('Pragma: ');
24header('Cache-Control: cache');
25
0b97a708 26/* globals */
1e12d1ff 27sqgetGlobalVar('key', $key, SQ_COOKIE);
28sqgetGlobalVar('username', $username, SQ_SESSION);
29sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
30sqgetGlobalVar('messages', $messages, SQ_SESSION);
31sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
32sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
33sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
34if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
35 $passed_id = (int) $temp;
36}
1075eaf1 37
0b97a708 38/* end globals */
97d7da3b 39
1075eaf1 40global $uid_support;
97d7da3b 41
1e606225 42$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
43$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
97d7da3b 44
1e606225 45$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
5a1c48cd 46if (!is_object($message)) {
47 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
48}
ff9d4297 49$subject = $message->rfc822_header->subject;
6914aa18 50if ($ent_id) {
51 $message = &$message->getEntity($ent_id);
52 $header = $message->header;
53
54 if ($message->rfc822_header) {
55 $subject = $message->rfc822_header->subject;
56 $charset = $header->content_type->properties['charset'];
57 } else {
58 $header = $message->header;
59 $charset = $header->getParameter('charset');
60 }
61 $type0 = $header->type0;
62 $type1 = $header->type1;
63 $encoding = strtolower($header->encoding);
ff9d4297 64} else {
6914aa18 65 /* raw message */
66 $type0 = 'message';
67 $type1 = 'rfc822';
bc5ff7c9 68 $encoding = 'US-ASCII';
5d39ca4d 69 $header = $message->header;
ff9d4297 70}
97d7da3b 71
65c3ec94 72/*
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 */
65c3ec94 77
65c3ec94 78if (isset($override_type0)) {
79 $type0 = $override_type0;
80}
81if (isset($override_type1)) {
82 $type1 = $override_type1;
83}
ff9d4297 84$filename = '';
85if (is_object($message->header->disposition)) {
a91189d6 86 $filename = $header->disposition->getProperty('filename');
ff9d4297 87 if (!$filename) {
a91189d6 88 $filename = $header->disposition->getProperty('name');
ff9d4297 89 }
6914aa18 90 if (!$filename) {
a91189d6 91 $filename = $header->getParameter('name');
6914aa18 92 }
cf22004d 93} else {
5d39ca4d 94 $filename = $header->getParameter('name');
65c3ec94 95}
bc5ff7c9 96
a91189d6 97$filename = decodeHeader($filename);
65c3ec94 98if (strlen($filename) < 1) {
99 if ($type1 == 'plain' && $type0 == 'text') {
100 $suffix = 'txt';
ff9d4297 101 $filename = $subject . '.txt';
65c3ec94 102 } else if ($type1 == 'richtext' && $type0 == 'text') {
103 $suffix = 'rtf';
ff9d4297 104 $filename = $subject . '.rtf';
65c3ec94 105 } else if ($type1 == 'postscript' && $type0 == 'application') {
106 $suffix = 'ps';
ff9d4297 107 $filename = $subject . '.ps';
97d7da3b 108 } else if ($type1 == 'rfc822' && $type0 == 'message') {
109 $suffix = 'eml';
ff9d4297 110 $filename = $subject . '.msg';
65c3ec94 111 } else {
112 $suffix = $type1;
113 }
114
631db37e 115 if (strlen($filename) < 1) {
5d39ca4d 116 $filename = 'untitled'.strip_tags($ent_id).'.'.$suffix;
631db37e 117 } else {
ff9d4297 118 $filename = "$filename.$suffix";
631db37e 119 }
65c3ec94 120}
121
122/*
123 * Note:
124 * The following sections display the attachment in different
125 * ways depending on how they choose. The first way will download
126 * under any circumstance. This sets the Content-type to be
127 * applicatin/octet-stream, which should be interpreted by the
128 * browser as "download me".
129 * The second method (view) is used for images or other formats
130 * that should be able to be handled by the browser. It will
131 * most likely display the attachment inline inside the browser.
132 * And finally, the third one will be used by default. If it
133 * is displayable (text or html), it will load them up in a text
134 * viewer (built in to squirrelmail). Otherwise, it sets the
135 * content-type as application/octet-stream
136 */
88d916ee 137if (isset($absolute_dl) && $absolute_dl) {
5c553e34 138 DumpHeaders($type0, $type1, $filename, 1);
65c3ec94 139} else {
5c553e34 140 DumpHeaders($type0, $type1, $filename, 0);
65c3ec94 141}
38bca81c 142/* be aware that any warning caused by download.php will corrupt the
143 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 144mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
65c3ec94 145
65c3ec94 146/*
147 * This function is verified to work with Netscape and the *very latest*
148 * version of IE. I don't know if it works with Opera, but it should now.
149 */
150function DumpHeaders($type0, $type1, $filename, $force) {
1e12d1ff 151 global $languages, $squirrelmail_language;
88d916ee 152 $isIE = $isIE6 = 0;
97d7da3b 153
1e12d1ff 154 sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER);
0b97a708 155
65c3ec94 156 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
157 strstr($HTTP_USER_AGENT, 'Opera') === false) {
36294f1a 158 $isIE = 1;
65c3ec94 159 }
160
97d7da3b 161 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
162 strstr($HTTP_USER_AGENT, 'Opera') === false) {
163 $isIE6 = 1;
164 }
165
7dae3923 166 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
167 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 168 $filename =
169 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
83be314a 170 } else {
7dae3923 171 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
83be314a 172 }
65c3ec94 173
174 // A Pox on Microsoft and it's Office!
88d916ee 175 if (!$force) {
65c3ec94 176 // Try to show in browser window
177 header("Content-Disposition: inline; filename=\"$filename\"");
178 header("Content-Type: $type0/$type1; name=\"$filename\"");
179 } else {
180 // Try to pop up the "save as" box
181 // IE makes this hard. It pops up 2 save boxes, or none.
182 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
183 // But, accordint to Microsoft, it is "RFC compliant but doesn't
184 // take into account some deviations that allowed within the
185 // specification." Doesn't that mean RFC non-compliant?
186 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
187 //
188 // The best thing you can do for IE is to upgrade to the latest
189 // version
88d916ee 190 if ($isIE && !$isIE6) {
65c3ec94 191 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
192 // Do not have quotes around filename, but that applied to
193 // "attachment"... does it apply to inline too?
194 //
195 // This combination seems to work mostly. IE 5.5 SP 1 has
196 // known issues (see the Microsoft Knowledge Base)
197 header("Content-Disposition: inline; filename=$filename");
65c3ec94 198 // This works for most types, but doesn't work with Word files
199 header("Content-Type: application/download; name=\"$filename\"");
200
201 // These are spares, just in case. :-)
202 //header("Content-Type: $type0/$type1; name=\"$filename\"");
203 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
204 //header("Content-Type: application/octet-stream; name=\"$filename\"");
205 } else {
206 header("Content-Disposition: attachment; filename=\"$filename\"");
207 // application/octet-stream forces download for Netscape
208 header("Content-Type: application/octet-stream; name=\"$filename\"");
209 }
210 }
211}
631db37e 212?>