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