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