59177427 |
1 | <?php |
895905c0 |
2 | |
ef870322 |
3 | /** |
4 | ** download.php |
5 | ** |
895905c0 |
6 | ** Copyright (c) 1999-2001 The SquirrelMail development team |
ef870322 |
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. |
245a6892 |
11 | ** |
12 | ** $Id$ |
ef870322 |
13 | **/ |
14 | |
f740c049 |
15 | define('download_php', true); // Used for preferences |
16 | |
ff8a98e7 |
17 | require_once('../src/validate.php'); |
18 | require_once('../functions/imap.php'); |
19 | require_once('../functions/mime.php'); |
20 | require_once('../functions/date.php'); |
6b96544a |
21 | |
bc104ef3 |
22 | header("Pragma: "); |
23 | header("Cache-Control: cache"); |
24 | |
11307a4c |
25 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
8442ac08 |
26 | global $where, $what, $charset; |
a037624d |
27 | global $startMessage; |
fdcca8d3 |
28 | |
2d367c68 |
29 | displayPageHeader($color, 'None'); |
7831268e |
30 | |
a037624d |
31 | echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
180c2c09 |
32 | echo "<B><CENTER>"; |
aa8dcbd5 |
33 | echo _("Viewing a text attachment") . " - "; |
f4991a86 |
34 | if ($where && $what) { |
35 | // from a search |
36 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>"; |
37 | } else { |
38 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>"; |
39 | } |
a037624d |
40 | |
7831268e |
41 | $urlmailbox = urlencode($mailbox); |
a037624d |
42 | 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 |
43 | echo _("Download this as a file"); |
a037624d |
44 | echo "</A></CENTER><BR>"; |
45 | echo "</CENTER></B>"; |
46 | echo "</TD></TR></TABLE>"; |
47 | |
48 | echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
49 | echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>"; |
50 | |
c6b144dd |
51 | if ($type1 == 'html') { |
52 | $body = MagicHTML( $body, $id ); |
53 | } else { |
9eea179c |
54 | translateText($body, $wrap_at, $charset); |
c6b144dd |
55 | } |
9eea179c |
56 | |
c6b144dd |
57 | flush(); |
9eea179c |
58 | echo $body; |
0a4cf77a |
59 | |
7831268e |
60 | echo "</TT></TD></TR></TABLE>"; |
61 | } |
62 | |
e1469126 |
63 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
813eba2f |
64 | sqimap_mailbox_select($imapConnection, $mailbox); |
6b96544a |
65 | |
66 | // $message contains all information about the message |
67 | // including header and body |
813eba2f |
68 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
ca1a555e |
69 | $top_header = $message->header; |
6b96544a |
70 | |
8beafbbc |
71 | // lets redefine message as this particular entity that we wish to display. |
72 | // it should hold only the header for this entity. We need to fetch the body |
73 | // yet before we can display anything. |
74 | $message = getEntity($message, $passed_ent_id); |
75 | |
76 | $header = $message->header; |
8beafbbc |
77 | |
8442ac08 |
78 | $charset = $header->charset; |
8beafbbc |
79 | $type0 = $header->type0; |
80 | $type1 = $header->type1; |
0db847e1 |
81 | if (isset($override_type0)) |
82 | $type0 = $override_type0; |
83 | if (isset($override_type1)) |
84 | $type1 = $override_type1; |
f9ab17bf |
85 | $filename = decodeHeader($header->filename); |
66e1a00e |
86 | if (!$filename) { |
87 | $filename = decodeHeader($header->name); |
88 | } |
6b96544a |
89 | |
7831268e |
90 | if (strlen($filename) < 1) { |
e9f8ea4e |
91 | if ($type1 == "plain" && $type0 == "text") $suffix = "txt"; |
92 | else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf"; |
93 | else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps"; |
94 | else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg"; |
95 | else $suffix = $type1; |
96 | |
97 | $filename = "untitled$passed_ent_id.$suffix"; |
7831268e |
98 | } |
6b96544a |
99 | |
c4809aca |
100 | // Note: |
101 | // The following sections display the attachment in different |
102 | // ways depending on how they choose. The first way will download |
103 | // under any circumstance. This sets the Content-type to be |
104 | // applicatin/octet-stream, which should be interpreted by the |
105 | // browser as "download me". |
106 | // The second method (view) is used for images or other formats |
107 | // that should be able to be handled by the browser. It will |
108 | // most likely display the attachment inline inside the browser. |
109 | // And finally, the third one will be used by default. If it |
110 | // is displayable (text or html), it will load them up in a text |
111 | // viewer (built in to squirrelmail). Otherwise, it sets the |
112 | // content-type as application/octet-stream |
a7ea7540 |
113 | |
61423189 |
114 | if (isset($absolute_dl) && $absolute_dl == "true") { |
7831268e |
115 | switch($type0) { |
116 | case "text": |
36294f1a |
117 | DumpHeaders($type0, $type1, $filename, 1); |
beb9e459 |
118 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
119 | $body = decodeBody($body, $header->encoding); |
a900c1e6 |
120 | if ($type1 == "plain" && isset($showHeaders)) { |
d51894be |
121 | echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n"; |
122 | echo " " . _("From") . ": " . decodeHeader($top_header->from) . "\n"; |
123 | echo " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n"; |
ca1a555e |
124 | echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
623332f3 |
125 | } |
a900c1e6 |
126 | elseif ($type1 == "html" && isset($showHeaders)) { |
127 | echo '<table><tr><th align=right>' . _("Subject"); |
128 | echo ':</th><td>' . decodeHeader($top_header->subject); |
129 | echo "</td></tr>\n<tr><th align=right>" . _("From"); |
130 | echo ':</th><td>' . decodeHeader($top_header->from); |
131 | echo "</td></tr>\n<tr><th align=right>" . _("To"); |
132 | echo ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)); |
133 | echo "</td></tr>\n<tr><th align=right>" . _("Date"); |
134 | echo ':</th><td>' . getLongDateString($top_header->date); |
135 | echo "</td></tr>\n</table>\n<hr>\n"; |
136 | } |
36294f1a |
137 | echo $body; |
7831268e |
138 | break; |
139 | default: |
36294f1a |
140 | DumpHeaders($type0, $type1, $filename, 1); |
beb9e459 |
141 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
142 | break; |
143 | } |
144 | } else { |
145 | switch ($type0) { |
146 | case "text": |
beb9e459 |
147 | if ($type1 == "plain" || $type1 == "html") { |
148 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
149 | $body = decodeBody($body, $header->encoding); |
beb9e459 |
150 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
23a8095f |
151 | } else { |
36294f1a |
152 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
153 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
154 | $body = decodeBody($body, $header->encoding); |
beb9e459 |
155 | echo $body; |
156 | } |
157 | break; |
7831268e |
158 | case "message": |
beb9e459 |
159 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
160 | $body = decodeBody($body, $header->encoding); |
11307a4c |
161 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
7831268e |
162 | break; |
163 | default: |
36294f1a |
164 | DumpHeaders($type0, $type1, $filename, 0); |
beb9e459 |
165 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
166 | break; |
167 | } |
36294f1a |
168 | } |
169 | |
170 | |
2a0372ff |
171 | // This function is verified to work with Netscape and the *very latest* |
172 | // version of IE. I don't know if it works with Opera, but it should now. |
36294f1a |
173 | function DumpHeaders($type0, $type1, $filename, $force) |
174 | { |
175 | global $HTTP_USER_AGENT; |
176 | |
177 | $isIE = 0; |
2a0372ff |
178 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
179 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
36294f1a |
180 | $isIE = 1; |
181 | } |
182 | |
183 | $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename); |
184 | |
185 | // A Pox on Microsoft and it's Office! |
186 | if (! $force) |
187 | { |
188 | // Try to show in browser window |
189 | header("Content-Disposition: inline; filename=\"$filename\""); |
190 | header("Content-Type: $type0/$type1; name=\"$filename\""); |
191 | } |
192 | else |
193 | { |
194 | // Try to pop up the "save as" box |
195 | // IE makes this hard. It pops up 2 save boxes, or none. |
196 | // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP |
197 | // But, accordint to Microsoft, it is "RFC compliant but doesn't |
198 | // take into account some deviations that allowed within the |
199 | // specification." Doesn't that mean RFC non-compliant? |
200 | // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP |
201 | // |
202 | // The best thing you can do for IE is to upgrade to the latest |
203 | // version |
204 | if ($isIE) { |
205 | // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp |
206 | // Do not have quotes around filename, but that applied to |
207 | // "attachment"... does it apply to inline too? |
208 | // |
209 | // This combination seems to work mostly. IE 5.5 SP 1 has |
210 | // known issues (see the Microsoft Knowledge Base) |
211 | header("Content-Disposition: inline; filename=$filename"); |
212 | |
213 | // This works for most types, but doesn't work with Word files |
214 | header("Content-Type: application/download; name=\"$filename\""); |
215 | |
216 | // These are spares, just in case. :-) |
217 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
218 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
219 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
220 | } else { |
221 | header("Content-Disposition: attachment; filename=\"$filename\""); |
222 | // application/octet-stream forces download for Netscape |
223 | header("Content-Type: application/octet-stream; name=\"$filename\""); |
224 | } |
225 | } |
226 | } |
ff8a98e7 |
227 | ?> |