Happy New Year
[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 *
353d074a 9 * @copyright 1999-2018 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
ebd2391c 15/** This is the download page */
16define('PAGE_NAME', 'download');
17
30967a1e 18/**
202bcbcc 19 * Include the SquirrelMail initialization file.
30967a1e 20 */
202bcbcc 21require('../include/init.php');
86725763 22
23/* SquirrelMail required files. */
202bcbcc 24require(SM_PATH . 'functions/imap_general.php');
25require(SM_PATH . 'functions/mailbox_display.php');
26require(SM_PATH . 'functions/mime.php');
6b96544a 27
2b82cab1 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 */
38require(SM_PATH . 'functions/imap_messages.php');
39require(SM_PATH . 'functions/date.php');
40
65c3ec94 41header('Pragma: ');
42header('Cache-Control: cache');
43
0b97a708 44/* globals */
324ac3c5 45sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
1e12d1ff 46sqgetGlobalVar('messages', $messages, SQ_SESSION);
47sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
48sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
49sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
fa26ab45 50sqgetGlobalVar('force_crlf', $force_crlf, SQ_GET);
51bbe8fa 51sqgetGlobalVar('passed_id', $passed_id, SQ_GET, NULL, SQ_TYPE_BIGINT);
91c27aee 52if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
53 $account = 0;
54}
1075eaf1 55
fa7ba04e 56global $default_charset;
57set_my_charset();
58
0b97a708 59/* end globals */
97d7da3b 60
a9805897 61global $imap_stream_options; // in case not defined in config
62$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
91c27aee 63$aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
97d7da3b 64
324ac3c5 65if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) &&
66 is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
67 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
68} else {
2b82cab1 69 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
70 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
5a1c48cd 71}
324ac3c5 72
ff9d4297 73$subject = $message->rfc822_header->subject;
6914aa18 74if ($ent_id) {
5813f2b7 75 // replace message with message part, if message part is requested.
76 $message = $message->getEntity($ent_id);
6914aa18 77 $header = $message->header;
da2415c1 78
6914aa18 79 if ($message->rfc822_header) {
80 $subject = $message->rfc822_header->subject;
6914aa18 81 } else {
82 $header = $message->header;
6914aa18 83 }
84 $type0 = $header->type0;
85 $type1 = $header->type1;
86 $encoding = strtolower($header->encoding);
ff9d4297 87} else {
6914aa18 88 /* raw message */
89 $type0 = 'message';
90 $type1 = 'rfc822';
3c21f499 91 $encoding = '7bit';
5d39ca4d 92 $header = $message->header;
ff9d4297 93}
97d7da3b 94
1e606225 95/*
65c3ec94 96 * lets redefine message as this particular entity that we wish to display.
97 * it should hold only the header for this entity. We need to fetch the body
98 * yet before we can display anything.
99 */
65c3ec94 100
65c3ec94 101if (isset($override_type0)) {
102 $type0 = $override_type0;
103}
104if (isset($override_type1)) {
105 $type1 = $override_type1;
106}
ff9d4297 107$filename = '';
108if (is_object($message->header->disposition)) {
a91189d6 109 $filename = $header->disposition->getProperty('filename');
ff9d4297 110 if (!$filename) {
708ea172 111 $filename = $header->disposition->getProperty('name');
ff9d4297 112 }
6914aa18 113 if (!$filename) {
a91189d6 114 $filename = $header->getParameter('name');
da2415c1 115 }
cf22004d 116} else {
5d39ca4d 117 $filename = $header->getParameter('name');
65c3ec94 118}
bc5ff7c9 119
765470db 120$filename = decodeHeader($filename,true,false);
fa7ba04e 121$filename = charset_encode($filename,$default_charset,false);
122
123// If name is not set, use subject of email
65c3ec94 124if (strlen($filename) < 1) {
fa7ba04e 125 $filename = decodeHeader($subject, true, true);
126 $filename = charset_encode($filename,$default_charset,false);
89c7fc5a 127 if ($type1 == 'plain' && $type0 == 'text')
65c3ec94 128 $suffix = 'txt';
89c7fc5a 129 else if ($type1 == 'richtext' && $type0 == 'text')
65c3ec94 130 $suffix = 'rtf';
89c7fc5a 131 else if ($type1 == 'postscript' && $type0 == 'application')
65c3ec94 132 $suffix = 'ps';
89c7fc5a 133 else if ($type1 == 'rfc822' && $type0 == 'message')
675c2a6f 134 $suffix = 'eml';
8e73da2b 135 else
65c3ec94 136 $suffix = $type1;
8e73da2b 137
138 if ($filename == '')
139 $filename = 'untitled' . strip_tags($ent_id);
89c7fc5a 140 $filename = $filename . '.' . $suffix;
65c3ec94 141}
142
e4baf0ee 143/**
144 * Update mailbox_cache and close session in order to prevent
145 * script locking on larger downloads. SendDownloadHeaders() and
146 * mime_print_body_lines() don't write information to session.
147 * mime_print_body_lines() call duration depends on size of
148 * attachment and script can cause interface lockups, if session
149 * is not closed.
150 */
151$mailbox_cache[$aMailbox['NAME']] = $aMailbox;
152sqsession_register($mailbox_cache,'mailbox_cache');
153session_write_close();
154
65c3ec94 155/*
156 * Note:
157 * The following sections display the attachment in different
158 * ways depending on how they choose. The first way will download
159 * under any circumstance. This sets the Content-type to be
160 * applicatin/octet-stream, which should be interpreted by the
161 * browser as "download me".
162 * The second method (view) is used for images or other formats
163 * that should be able to be handled by the browser. It will
164 * most likely display the attachment inline inside the browser.
165 * And finally, the third one will be used by default. If it
166 * is displayable (text or html), it will load them up in a text
598294a7 167 * viewer (built in to SquirrelMail). Otherwise, it sets the
65c3ec94 168 * content-type as application/octet-stream
169 */
88d916ee 170if (isset($absolute_dl) && $absolute_dl) {
da2415c1 171 SendDownloadHeaders($type0, $type1, $filename, 1);
65c3ec94 172} else {
da2415c1 173 SendDownloadHeaders($type0, $type1, $filename, 0);
65c3ec94 174}
38bca81c 175/* be aware that any warning caused by download.php will corrupt the
176 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
d671df04 177mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding, 'php://stdout', $force_crlf);
a2b193bc 178