adding hack to fix setlocale behavior on OpenBSD 3.8+ (#1427512). I don't think
[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
65c3ec94 25header('Pragma: ');
26header('Cache-Control: cache');
27
0b97a708 28/* globals */
1e12d1ff 29sqgetGlobalVar('key', $key, SQ_COOKIE);
30sqgetGlobalVar('username', $username, SQ_SESSION);
31sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
324ac3c5 32sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
1e12d1ff 33sqgetGlobalVar('messages', $messages, SQ_SESSION);
34sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
35sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
36sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
37if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
324ac3c5 38 $passed_id = (int) $temp;
da2415c1 39}
91c27aee 40if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
41 $account = 0;
42}
1075eaf1 43
fa7ba04e 44global $default_charset;
45set_my_charset();
46
0b97a708 47/* end globals */
97d7da3b 48
1e606225 49$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
91c27aee 50$aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
97d7da3b 51
324ac3c5 52if (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;
5a1c48cd 58}
324ac3c5 59
60//$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
61
62//$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
63//if (!is_object($message)) {
64// $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
65//}
ff9d4297 66$subject = $message->rfc822_header->subject;
6914aa18 67if ($ent_id) {
5813f2b7 68 // replace message with message part, if message part is requested.
69 $message = $message->getEntity($ent_id);
6914aa18 70 $header = $message->header;
da2415c1 71
6914aa18 72 if ($message->rfc822_header) {
73 $subject = $message->rfc822_header->subject;
6914aa18 74 } else {
75 $header = $message->header;
6914aa18 76 }
77 $type0 = $header->type0;
78 $type1 = $header->type1;
79 $encoding = strtolower($header->encoding);
ff9d4297 80} else {
6914aa18 81 /* raw message */
82 $type0 = 'message';
83 $type1 = 'rfc822';
bc5ff7c9 84 $encoding = 'US-ASCII';
5d39ca4d 85 $header = $message->header;
ff9d4297 86}
97d7da3b 87
1e606225 88/*
65c3ec94 89 * lets redefine message as this particular entity that we wish to display.
90 * it should hold only the header for this entity. We need to fetch the body
91 * yet before we can display anything.
92 */
65c3ec94 93
65c3ec94 94if (isset($override_type0)) {
95 $type0 = $override_type0;
96}
97if (isset($override_type1)) {
98 $type1 = $override_type1;
99}
ff9d4297 100$filename = '';
101if (is_object($message->header->disposition)) {
a91189d6 102 $filename = $header->disposition->getProperty('filename');
ff9d4297 103 if (!$filename) {
708ea172 104 $filename = $header->disposition->getProperty('name');
ff9d4297 105 }
6914aa18 106 if (!$filename) {
a91189d6 107 $filename = $header->getParameter('name');
da2415c1 108 }
cf22004d 109} else {
5d39ca4d 110 $filename = $header->getParameter('name');
65c3ec94 111}
bc5ff7c9 112
765470db 113$filename = decodeHeader($filename,true,false);
fa7ba04e 114$filename = charset_encode($filename,$default_charset,false);
115
116// If name is not set, use subject of email
65c3ec94 117if (strlen($filename) < 1) {
fa7ba04e 118 $filename = decodeHeader($subject, true, true);
119 $filename = charset_encode($filename,$default_charset,false);
89c7fc5a 120 if ($type1 == 'plain' && $type0 == 'text')
65c3ec94 121 $suffix = 'txt';
89c7fc5a 122 else if ($type1 == 'richtext' && $type0 == 'text')
65c3ec94 123 $suffix = 'rtf';
89c7fc5a 124 else if ($type1 == 'postscript' && $type0 == 'application')
65c3ec94 125 $suffix = 'ps';
89c7fc5a 126 else if ($type1 == 'rfc822' && $type0 == 'message')
127 $suffix = 'msg';
8e73da2b 128 else
65c3ec94 129 $suffix = $type1;
8e73da2b 130
131 if ($filename == '')
132 $filename = 'untitled' . strip_tags($ent_id);
89c7fc5a 133 $filename = $filename . '.' . $suffix;
65c3ec94 134}
135
136/*
137 * Note:
138 * The following sections display the attachment in different
139 * ways depending on how they choose. The first way will download
140 * under any circumstance. This sets the Content-type to be
141 * applicatin/octet-stream, which should be interpreted by the
142 * browser as "download me".
143 * The second method (view) is used for images or other formats
144 * that should be able to be handled by the browser. It will
145 * most likely display the attachment inline inside the browser.
146 * And finally, the third one will be used by default. If it
147 * is displayable (text or html), it will load them up in a text
598294a7 148 * viewer (built in to SquirrelMail). Otherwise, it sets the
65c3ec94 149 * content-type as application/octet-stream
150 */
88d916ee 151if (isset($absolute_dl) && $absolute_dl) {
da2415c1 152 SendDownloadHeaders($type0, $type1, $filename, 1);
65c3ec94 153} else {
da2415c1 154 SendDownloadHeaders($type0, $type1, $filename, 0);
65c3ec94 155}
38bca81c 156/* be aware that any warning caused by download.php will corrupt the
157 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 158mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
324ac3c5 159$mailbox_cache[$aMailbox['NAME']] = $aMailbox;
160sqsession_register($mailbox_cache,'mailbox_cache');
a2b193bc 161
134e4174 162?>