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