Fixed major problems with IMAP session handling in the MIME code. This
[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
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>";
34 echo _("Viewing a plain text attachment");
35 echo "</CENTER></B>";
7831268e 36 echo "</TD></TR><TR><TD BGCOLOR=\"$color[4]\">";
37 $urlmailbox = urlencode($mailbox);
9f2215a1 38 echo "<CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">";
180c2c09 39 echo _("Download this as a file");
aae41ae9 40 echo "</A></CENTER><BR><BR><TT>";
0a4cf77a 41 if ($type1 == "html")
11307a4c 42 echo $body;
0a4cf77a 43 else
11307a4c 44 echo translateText($body, $wrap_at);
0a4cf77a 45
7831268e 46 echo "</TT></TD></TR></TABLE>";
47 }
48
e1469126 49 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
813eba2f 50 sqimap_mailbox_select($imapConnection, $mailbox);
6b96544a 51
52 // $message contains all information about the message
53 // including header and body
813eba2f 54 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
6b96544a 55
8beafbbc 56 // lets redefine message as this particular entity that we wish to display.
57 // it should hold only the header for this entity. We need to fetch the body
58 // yet before we can display anything.
59 $message = getEntity($message, $passed_ent_id);
60
61 $header = $message->header;
62 $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
63
64 $type0 = $header->type0;
65 $type1 = $header->type1;
66 $filename = $header->filename;
6b96544a 67
7831268e 68 if (strlen($filename) < 1) {
c4809aca 69 $filename = "untitled$passed_ent_id.$type1";
7831268e 70 }
6b96544a 71
c4809aca 72 // Note:
73 // The following sections display the attachment in different
74 // ways depending on how they choose. The first way will download
75 // under any circumstance. This sets the Content-type to be
76 // applicatin/octet-stream, which should be interpreted by the
77 // browser as "download me".
78 // The second method (view) is used for images or other formats
79 // that should be able to be handled by the browser. It will
80 // most likely display the attachment inline inside the browser.
81 // And finally, the third one will be used by default. If it
82 // is displayable (text or html), it will load them up in a text
83 // viewer (built in to squirrelmail). Otherwise, it sets the
84 // content-type as application/octet-stream
85
7831268e 86 if ($absolute_dl == "true") {
87 switch($type0) {
88 case "text":
8beafbbc 89 $body = decodeBody($body, $header->encoding);
c4809aca 90 #header("Content-type: $type0/$type1; name=\"$filename\"");
91 header("Content-type: application/octet-stream; name=\"$filename\"");
7831268e 92 header("Content-Disposition: attachment; filename=\"$filename\"");
d3cdb279 93 echo trim($body);
7831268e 94 break;
95 default:
8beafbbc 96 $body = decodeBody($body, $header->encoding);
c4809aca 97 header("Content-type: application/octet-stream; name=\"$filename\"");
98 #header("Content-type: $type0/$type1; name=\"$filename\"");
6b96544a 99 header("Content-Disposition: attachment; filename=\"$filename\"");
100 echo $body;
7831268e 101 break;
102 }
c4809aca 103 } else if ($view == "true") {
104 $body = decodeBody ($body, $header->encoding);
105 header("Content-type: $type0/$type1; name=\"$filename\"");
106 header("Content-disposition: attachment; filename=\"$filename\"");
107 echo $body;
7831268e 108 } else {
109 switch ($type0) {
110 case "text":
8beafbbc 111 $body = decodeBody($body, $header->encoding);
11307a4c 112 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
7831268e 113 break;
114 case "message":
8beafbbc 115 $body = decodeBody($body, $header->encoding);
11307a4c 116 viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
7831268e 117 break;
118 default:
8beafbbc 119 $body = decodeBody($body, $header->encoding);
c4809aca 120 header("Content-type: applicatin/octet-stream; name=\"$filename\"");
6b96544a 121 header("Content-Disposition: attachment; filename=\"$filename\"");
122 echo $body;
7831268e 123 break;
124 }
6b96544a 125 }
126
813eba2f 127 sqimap_logout($imapConnection);
7831268e 128?>