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