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 | |
15 | /*****************************************************************/ |
16 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
17 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
18 | /*** + Base level indent should begin at left margin, as ***/ |
19 | /*** the require_once below looks. ***/ |
20 | /*** + All identation should consist of four space blocks ***/ |
21 | /*** + Tab characters are evil. ***/ |
22 | /*** + all comments should use "slash-star ... star-slash" ***/ |
23 | /*** style -- no pound characters, no slash-slash style ***/ |
24 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
25 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
26 | /*** + Please use ' instead of ", when possible. Note " ***/ |
27 | /*** should always be used in _( ) function calls. ***/ |
28 | /*** Thank you for your help making the SM code more readable. ***/ |
29 | /*****************************************************************/ |
30 | |
31 | define('download_php', true); // Used for preferences |
32 | |
33 | require_once('../src/validate.php'); |
34 | require_once('../functions/imap.php'); |
35 | require_once('../functions/mime.php'); |
36 | require_once('../functions/date.php'); |
6b96544a |
37 | |
bc104ef3 |
38 | header("Pragma: "); |
39 | header("Cache-Control: cache"); |
40 | |
11307a4c |
41 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
8442ac08 |
42 | global $where, $what, $charset; |
a037624d |
43 | global $startMessage; |
fdcca8d3 |
44 | |
2d367c68 |
45 | displayPageHeader($color, 'None'); |
7831268e |
46 | |
bd9bbfef |
47 | echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
180c2c09 |
48 | echo "<B><CENTER>"; |
aa8dcbd5 |
49 | echo _("Viewing a text attachment") . " - "; |
f4991a86 |
50 | if ($where && $what) { |
51 | // from a search |
52 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>"; |
53 | } else { |
54 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>"; |
55 | } |
a037624d |
56 | |
7831268e |
57 | $urlmailbox = urlencode($mailbox); |
a037624d |
58 | echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">"; |
180c2c09 |
59 | echo _("Download this as a file"); |
a037624d |
60 | echo "</A></CENTER><BR>"; |
61 | echo "</CENTER></B>"; |
62 | echo "</TD></TR></TABLE>"; |
63 | |
bd9bbfef |
64 | echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
a037624d |
65 | echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>"; |
66 | |
c6b144dd |
67 | if ($type1 == 'html') { |
68 | $body = MagicHTML( $body, $id ); |
69 | } else { |
9eea179c |
70 | translateText($body, $wrap_at, $charset); |
c6b144dd |
71 | } |
9eea179c |
72 | |
c6b144dd |
73 | flush(); |
9eea179c |
74 | echo $body; |
0a4cf77a |
75 | |
7831268e |
76 | echo "</TT></TD></TR></TABLE>"; |
77 | } |
78 | |
e1469126 |
79 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
813eba2f |
80 | sqimap_mailbox_select($imapConnection, $mailbox); |
6b96544a |
81 | |
82 | // $message contains all information about the message |
83 | // including header and body |
813eba2f |
84 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
ca1a555e |
85 | $top_header = $message->header; |
6b96544a |
86 | |
8beafbbc |
87 | // lets redefine message as this particular entity that we wish to display. |
88 | // it should hold only the header for this entity. We need to fetch the body |
89 | // yet before we can display anything. |
90 | $message = getEntity($message, $passed_ent_id); |
91 | |
92 | $header = $message->header; |
8beafbbc |
93 | |
8442ac08 |
94 | $charset = $header->charset; |
8beafbbc |
95 | $type0 = $header->type0; |
96 | $type1 = $header->type1; |
0db847e1 |
97 | if (isset($override_type0)) |
98 | $type0 = $override_type0; |
99 | if (isset($override_type1)) |
100 | $type1 = $override_type1; |
f9ab17bf |
101 | $filename = decodeHeader($header->filename); |
66e1a00e |
102 | if (!$filename) { |
103 | $filename = decodeHeader($header->name); |
104 | } |
6b96544a |
105 | |
7831268e |
106 | if (strlen($filename) < 1) { |
5be9f195 |
107 | if ($type1 == 'plain' && $type0 == 'text') { |
108 | $suffix = 'txt'; |
109 | } else if ($type1 == 'richtext' && $type0 == 'text') { |
110 | $suffix = 'rtf'; |
111 | } else if ($type1 == 'postscript' && $type0 == 'application') { |
112 | $suffix = 'ps'; |
113 | } else if ($type1 == 'message' && $type0 == 'rfc822') { |
114 | $suffix = 'msg'; |
115 | } else { |
116 | $suffix = $type1; |
117 | } |
e9f8ea4e |
118 | |
119 | $filename = "untitled$passed_ent_id.$suffix"; |
7831268e |
120 | } |
6b96544a |
121 | |
c4809aca |
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 |
a7ea7540 |
135 | |
61423189 |
136 | if (isset($absolute_dl) && $absolute_dl == "true") { |
7831268e |
137 | switch($type0) { |
138 | case "text": |
36294f1a |
139 | DumpHeaders($type0, $type1, $filename, 1); |
beb9e459 |
140 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
141 | $body = decodeBody($body, $header->encoding); |
a900c1e6 |
142 | if ($type1 == "plain" && isset($showHeaders)) { |
d51894be |
143 | echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n"; |
144 | echo " " . _("From") . ": " . decodeHeader($top_header->from) . "\n"; |
145 | echo " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n"; |
ca1a555e |
146 | echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
623332f3 |
147 | } |
a900c1e6 |
148 | elseif ($type1 == "html" && isset($showHeaders)) { |
149 | echo '<table><tr><th align=right>' . _("Subject"); |
150 | echo ':</th><td>' . decodeHeader($top_header->subject); |
151 | echo "</td></tr>\n<tr><th align=right>" . _("From"); |
152 | echo ':</th><td>' . decodeHeader($top_header->from); |
153 | echo "</td></tr>\n<tr><th align=right>" . _("To"); |
154 | echo ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)); |
155 | echo "</td></tr>\n<tr><th align=right>" . _("Date"); |
156 | echo ':</th><td>' . getLongDateString($top_header->date); |
157 | echo "</td></tr>\n</table>\n<hr>\n"; |
158 | } |
36294f1a |
159 | echo $body; |
7831268e |
160 | break; |
161 | default: |
36294f1a |
162 | DumpHeaders($type0, $type1, $filename, 1); |
beb9e459 |
163 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
164 | break; |
165 | } |
166 | } else { |
167 | switch ($type0) { |
168 | case "text": |
beb9e459 |
169 | if ($type1 == "plain" || $type1 == "html") { |
170 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
171 | $body = decodeBody($body, $header->encoding); |
beb9e459 |
172 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
23a8095f |
173 | } else { |
36294f1a |
174 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
175 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
176 | $body = decodeBody($body, $header->encoding); |
beb9e459 |
177 | echo $body; |
178 | } |
179 | break; |
7831268e |
180 | case "message": |
beb9e459 |
181 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
182 | $body = decodeBody($body, $header->encoding); |
11307a4c |
183 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
7831268e |
184 | break; |
185 | default: |
36294f1a |
186 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
187 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
188 | break; |
189 | } |
36294f1a |
190 | } |
191 | |
192 | |
2a0372ff |
193 | // This function is verified to work with Netscape and the *very latest* |
194 | // version of IE. I don't know if it works with Opera, but it should now. |
36294f1a |
195 | function DumpHeaders($type0, $type1, $filename, $force) |
196 | { |
197 | global $HTTP_USER_AGENT; |
198 | |
199 | $isIE = 0; |
2a0372ff |
200 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
201 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
36294f1a |
202 | $isIE = 1; |
203 | } |
204 | |
205 | $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename); |
206 | |
207 | // A Pox on Microsoft and it's Office! |
208 | if (! $force) |
209 | { |
210 | // Try to show in browser window |
211 | header("Content-Disposition: inline; filename=\"$filename\""); |
212 | header("Content-Type: $type0/$type1; name=\"$filename\""); |
213 | } |
214 | else |
215 | { |
216 | // Try to pop up the "save as" box |
217 | // IE makes this hard. It pops up 2 save boxes, or none. |
218 | // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP |
219 | // But, accordint to Microsoft, it is "RFC compliant but doesn't |
220 | // take into account some deviations that allowed within the |
221 | // specification." Doesn't that mean RFC non-compliant? |
222 | // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP |
223 | // |
224 | // The best thing you can do for IE is to upgrade to the latest |
225 | // version |
226 | if ($isIE) { |
227 | // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp |
228 | // Do not have quotes around filename, but that applied to |
229 | // "attachment"... does it apply to inline too? |
230 | // |
231 | // This combination seems to work mostly. IE 5.5 SP 1 has |
232 | // known issues (see the Microsoft Knowledge Base) |
233 | header("Content-Disposition: inline; filename=$filename"); |
234 | |
235 | // This works for most types, but doesn't work with Word files |
236 | header("Content-Type: application/download; name=\"$filename\""); |
237 | |
238 | // These are spares, just in case. :-) |
239 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
240 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
241 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
242 | } else { |
243 | header("Content-Disposition: attachment; filename=\"$filename\""); |
244 | // application/octet-stream forces download for Netscape |
245 | header("Content-Type: application/octet-stream; name=\"$filename\""); |
246 | } |
247 | } |
248 | } |
35586184 |
249 | ?> |