Allow choice of line endings when downloading or decoding strings (bodies)
[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 *
4b5049de 9 * @copyright &copy; 1999-2007 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);
1e12d1ff 51if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
324ac3c5 52 $passed_id = (int) $temp;
da2415c1 53}
91c27aee 54if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
55 $account = 0;
56}
1075eaf1 57
fa7ba04e 58global $default_charset;
59set_my_charset();
60
0b97a708 61/* end globals */
97d7da3b 62
906f7e9f 63$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
91c27aee 64$aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
97d7da3b 65
324ac3c5 66if (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 {
2b82cab1 70 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
71 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
5a1c48cd 72}
324ac3c5 73
ff9d4297 74$subject = $message->rfc822_header->subject;
6914aa18 75if ($ent_id) {
5813f2b7 76 // replace message with message part, if message part is requested.
77 $message = $message->getEntity($ent_id);
6914aa18 78 $header = $message->header;
da2415c1 79
6914aa18 80 if ($message->rfc822_header) {
81 $subject = $message->rfc822_header->subject;
6914aa18 82 } else {
83 $header = $message->header;
6914aa18 84 }
85 $type0 = $header->type0;
86 $type1 = $header->type1;
87 $encoding = strtolower($header->encoding);
ff9d4297 88} else {
6914aa18 89 /* raw message */
90 $type0 = 'message';
91 $type1 = 'rfc822';
bc5ff7c9 92 $encoding = 'US-ASCII';
5d39ca4d 93 $header = $message->header;
ff9d4297 94}
97d7da3b 95
65c3ec94 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 */
65c3ec94 101
65c3ec94 102if (isset($override_type0)) {
103 $type0 = $override_type0;
104}
105if (isset($override_type1)) {
106 $type1 = $override_type1;
107}
ff9d4297 108$filename = '';
109if (is_object($message->header->disposition)) {
a91189d6 110 $filename = $header->disposition->getProperty('filename');
ff9d4297 111 if (!$filename) {
708ea172 112 $filename = $header->disposition->getProperty('name');
ff9d4297 113 }
6914aa18 114 if (!$filename) {
a91189d6 115 $filename = $header->getParameter('name');
da2415c1 116 }
cf22004d 117} else {
5d39ca4d 118 $filename = $header->getParameter('name');
65c3ec94 119}
bc5ff7c9 120
765470db 121$filename = decodeHeader($filename,true,false);
fa7ba04e 122$filename = charset_encode($filename,$default_charset,false);
123
124// If name is not set, use subject of email
65c3ec94 125if (strlen($filename) < 1) {
fa7ba04e 126 $filename = decodeHeader($subject, true, true);
127 $filename = charset_encode($filename,$default_charset,false);
89c7fc5a 128 if ($type1 == 'plain' && $type0 == 'text')
65c3ec94 129 $suffix = 'txt';
89c7fc5a 130 else if ($type1 == 'richtext' && $type0 == 'text')
65c3ec94 131 $suffix = 'rtf';
89c7fc5a 132 else if ($type1 == 'postscript' && $type0 == 'application')
65c3ec94 133 $suffix = 'ps';
89c7fc5a 134 else if ($type1 == 'rfc822' && $type0 == 'message')
675c2a6f 135 $suffix = 'eml';
8e73da2b 136 else
65c3ec94 137 $suffix = $type1;
8e73da2b 138
139 if ($filename == '')
140 $filename = 'untitled' . strip_tags($ent_id);
89c7fc5a 141 $filename = $filename . '.' . $suffix;
65c3ec94 142}
143
e4baf0ee 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;
153sqsession_register($mailbox_cache,'mailbox_cache');
154session_write_close();
155
65c3ec94 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
598294a7 168 * viewer (built in to SquirrelMail). Otherwise, it sets the
65c3ec94 169 * content-type as application/octet-stream
170 */
88d916ee 171if (isset($absolute_dl) && $absolute_dl) {
da2415c1 172 SendDownloadHeaders($type0, $type1, $filename, 1);
65c3ec94 173} else {
da2415c1 174 SendDownloadHeaders($type0, $type1, $filename, 0);
65c3ec94 175}
38bca81c 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 */
fa26ab45 178mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding, $force_crlf);
a2b193bc 179