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. */ |
16 | define('SM_PATH','../'); |
17 | |
18 | /* SquirrelMail required files. */ |
08185f2a |
19 | require_once(SM_PATH . 'include/validate.php'); |
86725763 |
20 | require_once(SM_PATH . 'functions/imap.php'); |
21 | require_once(SM_PATH . 'functions/mime.php'); |
6b96544a |
22 | |
65c3ec94 |
23 | header('Pragma: '); |
24 | header('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 | |
36 | if (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 |
43 | global $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 |
49 | if (!is_object($message)) { |
50 | $message = sqimap_get_message($imapConnection,$passed_id, $mailbox); |
51 | } |
ff9d4297 |
52 | $subject = $message->rfc822_header->subject; |
6914aa18 |
53 | if ($ent_id) { |
54 | $message = &$message->getEntity($ent_id); |
55 | $header = $message->header; |
56 | |
57 | if ($message->rfc822_header) { |
58 | $subject = $message->rfc822_header->subject; |
59 | $charset = $header->content_type->properties['charset']; |
60 | } else { |
61 | $header = $message->header; |
62 | $charset = $header->getParameter('charset'); |
63 | } |
64 | $type0 = $header->type0; |
65 | $type1 = $header->type1; |
66 | $encoding = strtolower($header->encoding); |
ff9d4297 |
67 | } else { |
6914aa18 |
68 | /* raw message */ |
69 | $type0 = 'message'; |
70 | $type1 = 'rfc822'; |
71 | $encoding = "US-ASCII"; |
ff9d4297 |
72 | } |
97d7da3b |
73 | |
65c3ec94 |
74 | /* |
75 | * lets redefine message as this particular entity that we wish to display. |
76 | * it should hold only the header for this entity. We need to fetch the body |
77 | * yet before we can display anything. |
78 | */ |
65c3ec94 |
79 | |
65c3ec94 |
80 | if (isset($override_type0)) { |
81 | $type0 = $override_type0; |
82 | } |
83 | if (isset($override_type1)) { |
84 | $type1 = $override_type1; |
85 | } |
ff9d4297 |
86 | $filename = ''; |
87 | if (is_object($message->header->disposition)) { |
88 | $filename = decodeHeader($header->disposition->getProperty('filename')); |
89 | if (!$filename) { |
90 | $filename = decodeHeader($header->disposition->getProperty('name')); |
91 | } |
6914aa18 |
92 | if (!$filename) { |
93 | $filename = decodeHeader($header->getParameter('name')); |
94 | } |
cf22004d |
95 | } else { |
96 | $filename = decodeHeader($header->getParameter('name')); |
65c3ec94 |
97 | } |
cf22004d |
98 | |
65c3ec94 |
99 | if (strlen($filename) < 1) { |
100 | if ($type1 == 'plain' && $type0 == 'text') { |
101 | $suffix = 'txt'; |
ff9d4297 |
102 | $filename = $subject . '.txt'; |
65c3ec94 |
103 | } else if ($type1 == 'richtext' && $type0 == 'text') { |
104 | $suffix = 'rtf'; |
ff9d4297 |
105 | $filename = $subject . '.rtf'; |
65c3ec94 |
106 | } else if ($type1 == 'postscript' && $type0 == 'application') { |
107 | $suffix = 'ps'; |
ff9d4297 |
108 | $filename = $subject . '.ps'; |
97d7da3b |
109 | } else if ($type1 == 'rfc822' && $type0 == 'message') { |
110 | $suffix = 'eml'; |
ff9d4297 |
111 | $filename = $subject . '.msg'; |
65c3ec94 |
112 | } else { |
113 | $suffix = $type1; |
114 | } |
115 | |
631db37e |
116 | if (strlen($filename) < 1) { |
9b761dbd |
117 | $filename = 'untitled'.strip_tags($ent_id).$suffix; |
631db37e |
118 | } else { |
ff9d4297 |
119 | $filename = "$filename.$suffix"; |
631db37e |
120 | } |
65c3ec94 |
121 | } |
122 | |
123 | /* |
124 | * Note: |
125 | * The following sections display the attachment in different |
126 | * ways depending on how they choose. The first way will download |
127 | * under any circumstance. This sets the Content-type to be |
128 | * applicatin/octet-stream, which should be interpreted by the |
129 | * browser as "download me". |
130 | * The second method (view) is used for images or other formats |
131 | * that should be able to be handled by the browser. It will |
132 | * most likely display the attachment inline inside the browser. |
133 | * And finally, the third one will be used by default. If it |
134 | * is displayable (text or html), it will load them up in a text |
135 | * viewer (built in to squirrelmail). Otherwise, it sets the |
136 | * content-type as application/octet-stream |
137 | */ |
88d916ee |
138 | if (isset($absolute_dl) && $absolute_dl) { |
5c553e34 |
139 | DumpHeaders($type0, $type1, $filename, 1); |
65c3ec94 |
140 | } else { |
5c553e34 |
141 | DumpHeaders($type0, $type1, $filename, 0); |
65c3ec94 |
142 | } |
38bca81c |
143 | /* be aware that any warning caused by download.php will corrupt the |
144 | * attachment in case of ERROR reporting = E_ALL and the output is the screen */ |
ff9d4297 |
145 | mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding); |
65c3ec94 |
146 | |
65c3ec94 |
147 | /* |
148 | * This function is verified to work with Netscape and the *very latest* |
149 | * version of IE. I don't know if it works with Opera, but it should now. |
150 | */ |
151 | function DumpHeaders($type0, $type1, $filename, $force) { |
0b97a708 |
152 | global $_SERVER, $languages, $squirrelmail_language; |
88d916ee |
153 | $isIE = $isIE6 = 0; |
97d7da3b |
154 | |
0b97a708 |
155 | $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT']; |
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! |
88d916ee |
176 | if (!$force) { |
65c3ec94 |
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 |
88d916ee |
191 | if ($isIE && !$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"); |
65c3ec94 |
199 | // This works for most types, but doesn't work with Word files |
200 | header("Content-Type: application/download; name=\"$filename\""); |
201 | |
202 | // These are spares, just in case. :-) |
203 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
204 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
205 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
206 | } else { |
207 | header("Content-Disposition: attachment; filename=\"$filename\""); |
208 | // application/octet-stream forces download for Netscape |
209 | header("Content-Type: application/octet-stream; name=\"$filename\""); |
210 | } |
211 | } |
212 | } |
631db37e |
213 | ?> |