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