Since we're requiring PHP >= 4.0.4, we can just use move_upload_file
[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 */
27
28$key = $_COOKIE['key'];
29$username = $_SESSION['username'];
30$onetimepad = $_SESSION['onetimepad'];
31$mailbox = $_GET['mailbox'];
9b761dbd 32$passed_id = (int) $_GET['passed_id'];
ce274df9 33$ent_id = $_GET['ent_id'];
1075eaf1 34$messages = $_SESSION['messages'];
88d916ee 35
36if (isset($_GET['absolute_dl'])) {
37 $absolute_dl = $_GET['absolute_dl'];
0b97a708 38}
1075eaf1 39
0b97a708 40/* end globals */
1e606225 41$mailbox = decodeHeader($mailbox);
97d7da3b 42
1075eaf1 43global $uid_support;
97d7da3b 44
1e606225 45$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
46$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
97d7da3b 47
1e606225 48$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
5a1c48cd 49if (!is_object($message)) {
50 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
51}
ff9d4297 52$subject = $message->rfc822_header->subject;
53$message = &$message->getEntity($ent_id);
1e606225 54$header = $message->header;
ff9d4297 55if ($message->rfc822_header) {
56 $subject = $message->rfc822_header->subject;
57 $charset = $header->content_type->properties['charset'];
58} else {
59 $header = $message->header;
60 $charset = $header->getParameter('charset');
61}
1e606225 62$type0 = $header->type0;
63$type1 = $header->type1;
64$encoding = strtolower($header->encoding);
97d7da3b 65
65c3ec94 66/*
67 * lets redefine message as this particular entity that we wish to display.
68 * it should hold only the header for this entity. We need to fetch the body
69 * yet before we can display anything.
70 */
65c3ec94 71
65c3ec94 72if (isset($override_type0)) {
73 $type0 = $override_type0;
74}
75if (isset($override_type1)) {
76 $type1 = $override_type1;
77}
ff9d4297 78$filename = '';
79if (is_object($message->header->disposition)) {
80 $filename = decodeHeader($header->disposition->getProperty('filename'));
81 if (!$filename) {
82 $filename = decodeHeader($header->disposition->getProperty('name'));
83 }
65c3ec94 84}
65c3ec94 85if (strlen($filename) < 1) {
86 if ($type1 == 'plain' && $type0 == 'text') {
87 $suffix = 'txt';
ff9d4297 88 $filename = $subject . '.txt';
65c3ec94 89 } else if ($type1 == 'richtext' && $type0 == 'text') {
90 $suffix = 'rtf';
ff9d4297 91 $filename = $subject . '.rtf';
65c3ec94 92 } else if ($type1 == 'postscript' && $type0 == 'application') {
93 $suffix = 'ps';
ff9d4297 94 $filename = $subject . '.ps';
97d7da3b 95 } else if ($type1 == 'rfc822' && $type0 == 'message') {
96 $suffix = 'eml';
ff9d4297 97 $filename = $subject . '.msg';
65c3ec94 98 } else {
99 $suffix = $type1;
100 }
101
631db37e 102 if (strlen($filename) < 1) {
9b761dbd 103 $filename = 'untitled'.strip_tags($ent_id).$suffix;
631db37e 104 } else {
ff9d4297 105 $filename = "$filename.$suffix";
631db37e 106 }
65c3ec94 107}
108
109/*
110 * Note:
111 * The following sections display the attachment in different
112 * ways depending on how they choose. The first way will download
113 * under any circumstance. This sets the Content-type to be
114 * applicatin/octet-stream, which should be interpreted by the
115 * browser as "download me".
116 * The second method (view) is used for images or other formats
117 * that should be able to be handled by the browser. It will
118 * most likely display the attachment inline inside the browser.
119 * And finally, the third one will be used by default. If it
120 * is displayable (text or html), it will load them up in a text
121 * viewer (built in to squirrelmail). Otherwise, it sets the
122 * content-type as application/octet-stream
123 */
88d916ee 124if (isset($absolute_dl) && $absolute_dl) {
5c553e34 125 DumpHeaders($type0, $type1, $filename, 1);
65c3ec94 126} else {
5c553e34 127 DumpHeaders($type0, $type1, $filename, 0);
65c3ec94 128}
38bca81c 129/* be aware that any warning caused by download.php will corrupt the
130 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 131mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
65c3ec94 132
65c3ec94 133/*
134 * This function is verified to work with Netscape and the *very latest*
135 * version of IE. I don't know if it works with Opera, but it should now.
136 */
137function DumpHeaders($type0, $type1, $filename, $force) {
0b97a708 138 global $_SERVER, $languages, $squirrelmail_language;
88d916ee 139 $isIE = $isIE6 = 0;
97d7da3b 140
0b97a708 141 $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
142
65c3ec94 143 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
144 strstr($HTTP_USER_AGENT, 'Opera') === false) {
36294f1a 145 $isIE = 1;
65c3ec94 146 }
147
97d7da3b 148 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false &&
149 strstr($HTTP_USER_AGENT, 'Opera') === false) {
150 $isIE6 = 1;
151 }
152
7dae3923 153 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
154 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 155 $filename =
156 $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT);
83be314a 157 } else {
7dae3923 158 $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename);
83be314a 159 }
65c3ec94 160
161 // A Pox on Microsoft and it's Office!
88d916ee 162 if (!$force) {
65c3ec94 163 // Try to show in browser window
164 header("Content-Disposition: inline; filename=\"$filename\"");
165 header("Content-Type: $type0/$type1; name=\"$filename\"");
166 } else {
167 // Try to pop up the "save as" box
168 // IE makes this hard. It pops up 2 save boxes, or none.
169 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
170 // But, accordint to Microsoft, it is "RFC compliant but doesn't
171 // take into account some deviations that allowed within the
172 // specification." Doesn't that mean RFC non-compliant?
173 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
174 //
175 // The best thing you can do for IE is to upgrade to the latest
176 // version
88d916ee 177 if ($isIE && !$isIE6) {
65c3ec94 178 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
179 // Do not have quotes around filename, but that applied to
180 // "attachment"... does it apply to inline too?
181 //
182 // This combination seems to work mostly. IE 5.5 SP 1 has
183 // known issues (see the Microsoft Knowledge Base)
184 header("Content-Disposition: inline; filename=$filename");
65c3ec94 185 // This works for most types, but doesn't work with Word files
186 header("Content-Type: application/download; name=\"$filename\"");
187
188 // These are spares, just in case. :-)
189 //header("Content-Type: $type0/$type1; name=\"$filename\"");
190 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
191 //header("Content-Type: application/octet-stream; name=\"$filename\"");
192 } else {
193 header("Content-Disposition: attachment; filename=\"$filename\"");
194 // application/octet-stream forces download for Netscape
195 header("Content-Type: application/octet-stream; name=\"$filename\"");
196 }
197 }
198}
631db37e 199?>