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