896e86be9d1241f566549a9ee9630e676e0eb7b0
[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-2007 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 /** This is the download page */
16 define('PAGE_NAME', 'download');
17
18 /**
19 * Include the SquirrelMail initialization file.
20 */
21 require('../include/init.php');
22
23 /* SquirrelMail required files. */
24 require(SM_PATH . 'functions/imap_general.php');
25 require(SM_PATH . 'functions/mailbox_display.php');
26 require(SM_PATH . 'functions/mime.php');
27
28 /**
29 * If a message is viewed from the search page, $aMailbox[$passed_id]['MESSAGE_OBJECT']
30 * is not initialized, which makes this page error out on line 65 with an
31 * undefined function. We need to include some additional files in case the
32 * object has not been initialized.
33 *
34 * TODO: Determine why the object in question is not initialized when coming from
35 * a search page and correct. Once that is done, we can remove these
36 * includes.
37 */
38 require(SM_PATH . 'functions/imap_messages.php');
39 require(SM_PATH . 'functions/date.php');
40
41 header('Pragma: ');
42 header('Cache-Control: cache');
43
44 /* globals */
45 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
46 sqgetGlobalVar('messages', $messages, SQ_SESSION);
47 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
48 sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
49 sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
50 sqgetGlobalVar('force_crlf', $force_crlf, SQ_GET);
51 if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
52 $passed_id = (int) $temp;
53 }
54 if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
55 $account = 0;
56 }
57
58 global $default_charset;
59 set_my_charset();
60
61 /* end globals */
62
63 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
64 $aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
65
66 if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) &&
67 is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
68 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
69 } else {
70 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
71 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
72 }
73
74 $subject = $message->rfc822_header->subject;
75 if ($ent_id) {
76 // replace message with message part, if message part is requested.
77 $message = $message->getEntity($ent_id);
78 $header = $message->header;
79
80 if ($message->rfc822_header) {
81 $subject = $message->rfc822_header->subject;
82 } else {
83 $header = $message->header;
84 }
85 $type0 = $header->type0;
86 $type1 = $header->type1;
87 $encoding = strtolower($header->encoding);
88 } else {
89 /* raw message */
90 $type0 = 'message';
91 $type1 = 'rfc822';
92 $encoding = 'US-ASCII';
93 $header = $message->header;
94 }
95
96 /*
97 * lets redefine message as this particular entity that we wish to display.
98 * it should hold only the header for this entity. We need to fetch the body
99 * yet before we can display anything.
100 */
101
102 if (isset($override_type0)) {
103 $type0 = $override_type0;
104 }
105 if (isset($override_type1)) {
106 $type1 = $override_type1;
107 }
108 $filename = '';
109 if (is_object($message->header->disposition)) {
110 $filename = $header->disposition->getProperty('filename');
111 if (!$filename) {
112 $filename = $header->disposition->getProperty('name');
113 }
114 if (!$filename) {
115 $filename = $header->getParameter('name');
116 }
117 } else {
118 $filename = $header->getParameter('name');
119 }
120
121 $filename = decodeHeader($filename,true,false);
122 $filename = charset_encode($filename,$default_charset,false);
123
124 // If name is not set, use subject of email
125 if (strlen($filename) < 1) {
126 $filename = decodeHeader($subject, true, true);
127 $filename = charset_encode($filename,$default_charset,false);
128 if ($type1 == 'plain' && $type0 == 'text')
129 $suffix = 'txt';
130 else if ($type1 == 'richtext' && $type0 == 'text')
131 $suffix = 'rtf';
132 else if ($type1 == 'postscript' && $type0 == 'application')
133 $suffix = 'ps';
134 else if ($type1 == 'rfc822' && $type0 == 'message')
135 $suffix = 'eml';
136 else
137 $suffix = $type1;
138
139 if ($filename == '')
140 $filename = 'untitled' . strip_tags($ent_id);
141 $filename = $filename . '.' . $suffix;
142 }
143
144 /**
145 * Update mailbox_cache and close session in order to prevent
146 * script locking on larger downloads. SendDownloadHeaders() and
147 * mime_print_body_lines() don't write information to session.
148 * mime_print_body_lines() call duration depends on size of
149 * attachment and script can cause interface lockups, if session
150 * is not closed.
151 */
152 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
153 sqsession_register($mailbox_cache,'mailbox_cache');
154 session_write_close();
155
156 /*
157 * Note:
158 * The following sections display the attachment in different
159 * ways depending on how they choose. The first way will download
160 * under any circumstance. This sets the Content-type to be
161 * applicatin/octet-stream, which should be interpreted by the
162 * browser as "download me".
163 * The second method (view) is used for images or other formats
164 * that should be able to be handled by the browser. It will
165 * most likely display the attachment inline inside the browser.
166 * And finally, the third one will be used by default. If it
167 * is displayable (text or html), it will load them up in a text
168 * viewer (built in to SquirrelMail). Otherwise, it sets the
169 * content-type as application/octet-stream
170 */
171 if (isset($absolute_dl) && $absolute_dl) {
172 SendDownloadHeaders($type0, $type1, $filename, 1);
173 } else {
174 SendDownloadHeaders($type0, $type1, $filename, 0);
175 }
176 /* be aware that any warning caused by download.php will corrupt the
177 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
178 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding, 'php://stdout', $force_crlf);
179