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. |
10 | **/ |
11 | |
2a32fc83 |
12 | session_start(); |
13 | |
d068c0ec |
14 | if (!isset($config_php)) |
15 | include("../config/config.php"); |
16 | if (!isset($strings_php)) |
17 | include("../functions/strings.php"); |
18 | if (!isset($page_header_php)) |
19 | include("../functions/page_header.php"); |
20 | if (!isset($imap_php)) |
21 | include("../functions/imap.php"); |
22 | if (!isset($mime_php)) |
23 | include("../functions/mime.php"); |
24 | if (!isset($date_php)) |
25 | include("../functions/date.php"); |
6b96544a |
26 | |
d3cdb279 |
27 | include("../src/load_prefs.php"); |
28 | |
11307a4c |
29 | function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) { |
7831268e |
30 | displayPageHeader($color, "None"); |
31 | |
32 | echo "<BR><TABLE WIDTH=90% BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">"; |
180c2c09 |
33 | echo "<B><CENTER>"; |
aa8dcbd5 |
34 | echo _("Viewing a text attachment") . " - "; |
35 | echo "<a href=\"read_body.php?mailbox=$mailbox&passed_id=$id\">". _("View message") . "</a>"; |
180c2c09 |
36 | echo "</CENTER></B>"; |
7831268e |
37 | echo "</TD></TR><TR><TD BGCOLOR=\"$color[4]\">"; |
38 | $urlmailbox = urlencode($mailbox); |
9f2215a1 |
39 | echo "<CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">"; |
180c2c09 |
40 | echo _("Download this as a file"); |
aae41ae9 |
41 | echo "</A></CENTER><BR><BR><TT>"; |
0a4cf77a |
42 | if ($type1 == "html") |
11307a4c |
43 | echo $body; |
0a4cf77a |
44 | else |
11307a4c |
45 | echo translateText($body, $wrap_at); |
0a4cf77a |
46 | |
7831268e |
47 | echo "</TT></TD></TR></TABLE>"; |
48 | } |
49 | |
e1469126 |
50 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
813eba2f |
51 | sqimap_mailbox_select($imapConnection, $mailbox); |
6b96544a |
52 | |
53 | // $message contains all information about the message |
54 | // including header and body |
813eba2f |
55 | $message = sqimap_get_message($imapConnection, $passed_id, $mailbox); |
ca1a555e |
56 | $top_header = $message->header; |
6b96544a |
57 | |
8beafbbc |
58 | // lets redefine message as this particular entity that we wish to display. |
59 | // it should hold only the header for this entity. We need to fetch the body |
60 | // yet before we can display anything. |
61 | $message = getEntity($message, $passed_ent_id); |
62 | |
63 | $header = $message->header; |
64 | $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); |
65 | |
66 | $type0 = $header->type0; |
67 | $type1 = $header->type1; |
68 | $filename = $header->filename; |
6b96544a |
69 | |
7831268e |
70 | if (strlen($filename) < 1) { |
e9f8ea4e |
71 | # set some standard suffixes to the filenames if the filename isn't known |
72 | |
73 | if ($type1 == "plain" && $type0 == "text") $suffix = "txt"; |
74 | else if ($type1 == "richtext" && $type0 == "text") $suffix = "rtf"; |
75 | else if ($type1 == "postscript" && $type0 == "application") $suffix = "ps"; |
76 | else if ($type1 == "message" && $type0 == "rfc822") $suffix = "msg"; |
77 | else $suffix = $type1; |
78 | |
79 | $filename = "untitled$passed_ent_id.$suffix"; |
7831268e |
80 | } |
6b96544a |
81 | |
c4809aca |
82 | // Note: |
83 | // The following sections display the attachment in different |
84 | // ways depending on how they choose. The first way will download |
85 | // under any circumstance. This sets the Content-type to be |
86 | // applicatin/octet-stream, which should be interpreted by the |
87 | // browser as "download me". |
88 | // The second method (view) is used for images or other formats |
89 | // that should be able to be handled by the browser. It will |
90 | // most likely display the attachment inline inside the browser. |
91 | // And finally, the third one will be used by default. If it |
92 | // is displayable (text or html), it will load them up in a text |
93 | // viewer (built in to squirrelmail). Otherwise, it sets the |
94 | // content-type as application/octet-stream |
95 | |
7831268e |
96 | if ($absolute_dl == "true") { |
97 | switch($type0) { |
98 | case "text": |
8beafbbc |
99 | $body = decodeBody($body, $header->encoding); |
c4809aca |
100 | header("Content-type: application/octet-stream; name=\"$filename\""); |
7831268e |
101 | header("Content-Disposition: attachment; filename=\"$filename\""); |
623332f3 |
102 | if ($type1 == "plain") { |
ca1a555e |
103 | echo _("Subject") . ": " . decodeHeader(stripslashes($top_header->subject)) . "\n"; |
104 | echo " " . _("From") . ": " . decodeHeader(stripslashes($top_header->from)) . "\n"; |
105 | echo " " . _("To") . ": " . decodeHeader(stripslashes(getLineOfAddrs($top_header->to))) . "\n"; |
106 | echo " " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n"; |
623332f3 |
107 | } |
d3cdb279 |
108 | echo trim($body); |
7831268e |
109 | break; |
110 | default: |
8beafbbc |
111 | $body = decodeBody($body, $header->encoding); |
c4809aca |
112 | header("Content-type: application/octet-stream; name=\"$filename\""); |
6b96544a |
113 | header("Content-Disposition: attachment; filename=\"$filename\""); |
114 | echo $body; |
7831268e |
115 | break; |
116 | } |
117 | } else { |
118 | switch ($type0) { |
119 | case "text": |
8beafbbc |
120 | $body = decodeBody($body, $header->encoding); |
11307a4c |
121 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
7831268e |
122 | break; |
123 | case "message": |
8beafbbc |
124 | $body = decodeBody($body, $header->encoding); |
11307a4c |
125 | viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); |
7831268e |
126 | break; |
127 | default: |
8beafbbc |
128 | $body = decodeBody($body, $header->encoding); |
aa8dcbd5 |
129 | header("Content-type: $type0/$type1; name=\"$filename\""); |
6b96544a |
130 | header("Content-Disposition: attachment; filename=\"$filename\""); |
131 | echo $body; |
7831268e |
132 | break; |
133 | } |
6b96544a |
134 | } |
135 | |
813eba2f |
136 | sqimap_logout($imapConnection); |
7831268e |
137 | ?> |