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