don't add delimiter to folder name, when folder with subfolders is
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Handles attachment downloads to the users computer.
7 * Also allows displaying of attachments when possible.
8 *
9 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Include the SquirrelMail initialization file.
17 */
18 require('../include/init.php');
19
20 /* SquirrelMail required files. */
21 require(SM_PATH . 'functions/imap_general.php');
22 require(SM_PATH . 'functions/mailbox_display.php');
23 require(SM_PATH . 'functions/mime.php');
24
25 header('Pragma: ');
26 header('Cache-Control: cache');
27
28 /* globals */
29 sqgetGlobalVar('key', $key, SQ_COOKIE);
30 sqgetGlobalVar('username', $username, SQ_SESSION);
31 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
32 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
33 sqgetGlobalVar('messages', $messages, SQ_SESSION);
34 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
35 sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
36 sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
37 if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
38 $passed_id = (int) $temp;
39 }
40 if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
41 $account = 0;
42 }
43
44 global $default_charset;
45 set_my_charset();
46
47 /* end globals */
48
49 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
50 $aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
51
52 if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) &&
53 is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
54 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
55 } else {
56 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
57 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
58 }
59
60 $subject = $message->rfc822_header->subject;
61 if ($ent_id) {
62 // replace message with message part, if message part is requested.
63 $message = $message->getEntity($ent_id);
64 $header = $message->header;
65
66 if ($message->rfc822_header) {
67 $subject = $message->rfc822_header->subject;
68 } else {
69 $header = $message->header;
70 }
71 $type0 = $header->type0;
72 $type1 = $header->type1;
73 $encoding = strtolower($header->encoding);
74 } else {
75 /* raw message */
76 $type0 = 'message';
77 $type1 = 'rfc822';
78 $encoding = 'US-ASCII';
79 $header = $message->header;
80 }
81
82 /*
83 * lets redefine message as this particular entity that we wish to display.
84 * it should hold only the header for this entity. We need to fetch the body
85 * yet before we can display anything.
86 */
87
88 if (isset($override_type0)) {
89 $type0 = $override_type0;
90 }
91 if (isset($override_type1)) {
92 $type1 = $override_type1;
93 }
94 $filename = '';
95 if (is_object($message->header->disposition)) {
96 $filename = $header->disposition->getProperty('filename');
97 if (!$filename) {
98 $filename = $header->disposition->getProperty('name');
99 }
100 if (!$filename) {
101 $filename = $header->getParameter('name');
102 }
103 } else {
104 $filename = $header->getParameter('name');
105 }
106
107 $filename = decodeHeader($filename,true,false);
108 $filename = charset_encode($filename,$default_charset,false);
109
110 // If name is not set, use subject of email
111 if (strlen($filename) < 1) {
112 $filename = decodeHeader($subject, true, true);
113 $filename = charset_encode($filename,$default_charset,false);
114 if ($type1 == 'plain' && $type0 == 'text')
115 $suffix = 'txt';
116 else if ($type1 == 'richtext' && $type0 == 'text')
117 $suffix = 'rtf';
118 else if ($type1 == 'postscript' && $type0 == 'application')
119 $suffix = 'ps';
120 else if ($type1 == 'rfc822' && $type0 == 'message')
121 $suffix = 'msg';
122 else
123 $suffix = $type1;
124
125 if ($filename == '')
126 $filename = 'untitled' . strip_tags($ent_id);
127 $filename = $filename . '.' . $suffix;
128 }
129
130 /**
131 * Update mailbox_cache and close session in order to prevent
132 * script locking on larger downloads. SendDownloadHeaders() and
133 * mime_print_body_lines() don't write information to session.
134 * mime_print_body_lines() call duration depends on size of
135 * attachment and script can cause interface lockups, if session
136 * is not closed.
137 */
138 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
139 sqsession_register($mailbox_cache,'mailbox_cache');
140 session_write_close();
141
142 /*
143 * Note:
144 * The following sections display the attachment in different
145 * ways depending on how they choose. The first way will download
146 * under any circumstance. This sets the Content-type to be
147 * applicatin/octet-stream, which should be interpreted by the
148 * browser as "download me".
149 * The second method (view) is used for images or other formats
150 * that should be able to be handled by the browser. It will
151 * most likely display the attachment inline inside the browser.
152 * And finally, the third one will be used by default. If it
153 * is displayable (text or html), it will load them up in a text
154 * viewer (built in to SquirrelMail). Otherwise, it sets the
155 * content-type as application/octet-stream
156 */
157 if (isset($absolute_dl) && $absolute_dl) {
158 SendDownloadHeaders($type0, $type1, $filename, 1);
159 } else {
160 SendDownloadHeaders($type0, $type1, $filename, 0);
161 }
162 /* be aware that any warning caused by download.php will corrupt the
163 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
164 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
165