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 | |
d068c0ec |
14 | if (!isset($strings_php)) |
15 | include("../functions/strings.php"); |
61423189 |
16 | if (!isset($config_php)) |
17 | include("../config/config.php"); |
d068c0ec |
18 | if (!isset($imap_php)) |
19 | include("../functions/imap.php"); |
20 | if (!isset($mime_php)) |
21 | include("../functions/mime.php"); |
22 | if (!isset($date_php)) |
23 | include("../functions/date.php"); |
441f2d33 |
24 | if (!isset($i18n_php)) |
25 | include("../functions/i18n.php"); |
6b96544a |
26 | |
bc104ef3 |
27 | session_start(); |
28 | header("Pragma: "); |
29 | header("Cache-Control: cache"); |
30 | |
7bff2bfc |
31 | include("../src/load_prefs.php"); |
d3cdb279 |
32 | |
11307a4c |
33 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
8442ac08 |
34 | global $where, $what, $charset; |
a037624d |
35 | global $startMessage; |
fdcca8d3 |
36 | |
7831268e |
37 | displayPageHeader($color, "None"); |
38 | |
a037624d |
39 | echo "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
180c2c09 |
40 | echo "<B><CENTER>"; |
aa8dcbd5 |
41 | echo _("Viewing a text attachment") . " - "; |
f4991a86 |
42 | if ($where && $what) { |
43 | // from a search |
44 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>"; |
45 | } else { |
46 | echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>"; |
47 | } |
a037624d |
48 | |
7831268e |
49 | $urlmailbox = urlencode($mailbox); |
a037624d |
50 | 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 |
51 | echo _("Download this as a file"); |
a037624d |
52 | echo "</A></CENTER><BR>"; |
53 | echo "</CENTER></B>"; |
54 | echo "</TD></TR></TABLE>"; |
55 | |
56 | echo "<TABLE WIDTH=98% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
57 | echo "<TR><TD BGCOLOR=\"$color[4]\"><TT>"; |
58 | |
9eea179c |
59 | if ($type1 != "html") |
60 | translateText($body, $wrap_at, $charset); |
61 | |
62 | echo $body; |
0a4cf77a |
63 | |
7831268e |
64 | echo "</TT></TD></TR></TABLE>"; |
65 | } |
66 | |
e1469126 |
67 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
813eba2f |
68 | sqimap_mailbox_select($imapConnection, $mailbox); |
6b96544a |
69 | |
70 | // $message contains all information about the message |
71 | // including header and body |
813eba2f |
72 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
ca1a555e |
73 | $top_header = $message->header; |
6b96544a |
74 | |
8beafbbc |
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 | $message = getEntity($message, $passed_ent_id); |
79 | |
80 | $header = $message->header; |
8beafbbc |
81 | |
8442ac08 |
82 | $charset = $header->charset; |
8beafbbc |
83 | $type0 = $header->type0; |
84 | $type1 = $header->type1; |
0db847e1 |
85 | if (isset($override_type0)) |
86 | $type0 = $override_type0; |
87 | if (isset($override_type1)) |
88 | $type1 = $override_type1; |
f9ab17bf |
89 | $filename = decodeHeader($header->filename); |
66e1a00e |
90 | if (!$filename) { |
91 | $filename = decodeHeader($header->name); |
92 | } |
6b96544a |
93 | |
7831268e |
94 | if (strlen($filename) < 1) { |
e9f8ea4e |
95 | if ($type1 == "plain" && $type0 == "text") $suffix = "txt"; |
96 | else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf"; |
97 | else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps"; |
98 | else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg"; |
99 | else $suffix = $type1; |
100 | |
101 | $filename = "untitled$passed_ent_id.$suffix"; |
7831268e |
102 | } |
6b96544a |
103 | |
c4809aca |
104 | // Note: |
105 | // The following sections display the attachment in different |
106 | // ways depending on how they choose. The first way will download |
107 | // under any circumstance. This sets the Content-type to be |
108 | // applicatin/octet-stream, which should be interpreted by the |
109 | // browser as "download me". |
110 | // The second method (view) is used for images or other formats |
111 | // that should be able to be handled by the browser. It will |
112 | // most likely display the attachment inline inside the browser. |
113 | // And finally, the third one will be used by default. If it |
114 | // is displayable (text or html), it will load them up in a text |
115 | // viewer (built in to squirrelmail). Otherwise, it sets the |
116 | // content-type as application/octet-stream |
a7ea7540 |
117 | |
61423189 |
118 | if (isset($absolute_dl) && $absolute_dl == "true") { |
7831268e |
119 | switch($type0) { |
120 | case "text": |
beb9e459 |
121 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
122 | $body = decodeBody($body, $header->encoding); |
a7ea7540 |
123 | header("Content-Disposition: attachment; filename=\"$filename\""); |
124 | header("Content-type: application/octet-stream; name=\"$filename\""); |
441f2d33 |
125 | set_up_language(getPref($data_dir, $username, "language")); |
623332f3 |
126 | if ($type1 == "plain") { |
d51894be |
127 | echo _("Subject") . ": " . decodeHeader($top_header->subject) . "\n"; |
128 | echo " " . _("From") . ": " . decodeHeader($top_header->from) . "\n"; |
129 | echo " " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n"; |
ca1a555e |
130 | echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
623332f3 |
131 | } |
d3cdb279 |
132 | echo trim($body); |
7831268e |
133 | break; |
134 | default: |
a7ea7540 |
135 | header("Content-Disposition: attachment; filename=\"$filename\""); |
136 | header("Content-type: application/octet-stream; name=\"$filename\""); |
beb9e459 |
137 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
138 | break; |
139 | } |
140 | } else { |
141 | switch ($type0) { |
142 | case "text": |
beb9e459 |
143 | if ($type1 == "plain" || $type1 == "html") { |
144 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
145 | $body = decodeBody($body, $header->encoding); |
146 | include("../functions/page_header.php"); |
147 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
23a8095f |
148 | } else { |
beb9e459 |
149 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
150 | $body = decodeBody($body, $header->encoding); |
a7ea7540 |
151 | header("Content-type: $type0/$type1; name=\"$filename\""); |
152 | header("Content-Disposition: attachment; filename=\"$filename\""); |
beb9e459 |
153 | echo $body; |
154 | } |
155 | break; |
7831268e |
156 | case "message": |
beb9e459 |
157 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
8beafbbc |
158 | $body = decodeBody($body, $header->encoding); |
fdcca8d3 |
159 | include("../functions/page_header.php"); |
11307a4c |
160 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
7831268e |
161 | break; |
162 | default: |
a48fbf9b |
163 | header("Content-type: $type0/$type1; name=\"$filename\""); |
0ae64f08 |
164 | header("Content-Disposition: attachment; filename=\"$filename\""); |
beb9e459 |
165 | mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); |
7831268e |
166 | break; |
167 | } |
b67e316d |
168 | } |
169 | |
813eba2f |
170 | sqimap_logout($imapConnection); |
7831268e |
171 | ?> |