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