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