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