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