String fix
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * download.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handles attachment downloads to the users computer.
10 * Also allows displaying of attachments when possible.
11 *
30967a1e 12 * @version $Id$
2b646597 13 * @package squirrelmail
35586184 14 */
15
30967a1e 16/**
324ac3c5 17 * Path for SquirrelMail required files.
30967a1e 18 * @ignore
19 */
86725763 20define('SM_PATH','../');
21
22/* SquirrelMail required files. */
08185f2a 23require_once(SM_PATH . 'include/validate.php');
86725763 24require_once(SM_PATH . 'functions/imap.php');
25require_once(SM_PATH . 'functions/mime.php');
6b96544a 26
65c3ec94 27header('Pragma: ');
28header('Cache-Control: cache');
29
0b97a708 30/* globals */
1e12d1ff 31sqgetGlobalVar('key', $key, SQ_COOKIE);
32sqgetGlobalVar('username', $username, SQ_SESSION);
33sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
324ac3c5 34sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
1e12d1ff 35sqgetGlobalVar('messages', $messages, SQ_SESSION);
36sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
37sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
38sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
39if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
324ac3c5 40 $passed_id = (int) $temp;
da2415c1 41}
91c27aee 42if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
43 $account = 0;
44}
1075eaf1 45
fa7ba04e 46global $default_charset;
47set_my_charset();
48
0b97a708 49/* end globals */
97d7da3b 50
1e606225 51$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
91c27aee 52$aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
97d7da3b 53
324ac3c5 54if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) &&
55 is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
56 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
57} else {
58 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
59 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
5a1c48cd 60}
324ac3c5 61
62//$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
63
64//$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
65//if (!is_object($message)) {
66// $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
67//}
ff9d4297 68$subject = $message->rfc822_header->subject;
6914aa18 69if ($ent_id) {
5813f2b7 70 // replace message with message part, if message part is requested.
71 $message = $message->getEntity($ent_id);
6914aa18 72 $header = $message->header;
da2415c1 73
6914aa18 74 if ($message->rfc822_header) {
75 $subject = $message->rfc822_header->subject;
6914aa18 76 } else {
77 $header = $message->header;
6914aa18 78 }
79 $type0 = $header->type0;
80 $type1 = $header->type1;
81 $encoding = strtolower($header->encoding);
ff9d4297 82} else {
6914aa18 83 /* raw message */
84 $type0 = 'message';
85 $type1 = 'rfc822';
bc5ff7c9 86 $encoding = 'US-ASCII';
5d39ca4d 87 $header = $message->header;
ff9d4297 88}
97d7da3b 89
65c3ec94 90/*
91 * lets redefine message as this particular entity that we wish to display.
92 * it should hold only the header for this entity. We need to fetch the body
93 * yet before we can display anything.
94 */
65c3ec94 95
65c3ec94 96if (isset($override_type0)) {
97 $type0 = $override_type0;
98}
99if (isset($override_type1)) {
100 $type1 = $override_type1;
101}
ff9d4297 102$filename = '';
103if (is_object($message->header->disposition)) {
a91189d6 104 $filename = $header->disposition->getProperty('filename');
ff9d4297 105 if (!$filename) {
708ea172 106 $filename = $header->disposition->getProperty('name');
ff9d4297 107 }
6914aa18 108 if (!$filename) {
a91189d6 109 $filename = $header->getParameter('name');
da2415c1 110 }
cf22004d 111} else {
5d39ca4d 112 $filename = $header->getParameter('name');
65c3ec94 113}
bc5ff7c9 114
765470db 115$filename = decodeHeader($filename,true,false);
fa7ba04e 116$filename = charset_encode($filename,$default_charset,false);
117
118// If name is not set, use subject of email
65c3ec94 119if (strlen($filename) < 1) {
fa7ba04e 120 $filename = decodeHeader($subject, true, true);
121 $filename = charset_encode($filename,$default_charset,false);
89c7fc5a 122 if ($type1 == 'plain' && $type0 == 'text')
65c3ec94 123 $suffix = 'txt';
89c7fc5a 124 else if ($type1 == 'richtext' && $type0 == 'text')
65c3ec94 125 $suffix = 'rtf';
89c7fc5a 126 else if ($type1 == 'postscript' && $type0 == 'application')
65c3ec94 127 $suffix = 'ps';
89c7fc5a 128 else if ($type1 == 'rfc822' && $type0 == 'message')
129 $suffix = 'msg';
8e73da2b 130 else
65c3ec94 131 $suffix = $type1;
8e73da2b 132
133 if ($filename == '')
134 $filename = 'untitled' . strip_tags($ent_id);
89c7fc5a 135 $filename = $filename . '.' . $suffix;
65c3ec94 136}
137
138/*
139 * Note:
140 * The following sections display the attachment in different
141 * ways depending on how they choose. The first way will download
142 * under any circumstance. This sets the Content-type to be
143 * applicatin/octet-stream, which should be interpreted by the
144 * browser as "download me".
145 * The second method (view) is used for images or other formats
146 * that should be able to be handled by the browser. It will
147 * most likely display the attachment inline inside the browser.
148 * And finally, the third one will be used by default. If it
149 * is displayable (text or html), it will load them up in a text
598294a7 150 * viewer (built in to SquirrelMail). Otherwise, it sets the
65c3ec94 151 * content-type as application/octet-stream
152 */
88d916ee 153if (isset($absolute_dl) && $absolute_dl) {
da2415c1 154 SendDownloadHeaders($type0, $type1, $filename, 1);
65c3ec94 155} else {
da2415c1 156 SendDownloadHeaders($type0, $type1, $filename, 0);
65c3ec94 157}
38bca81c 158/* be aware that any warning caused by download.php will corrupt the
159 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 160mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
324ac3c5 161$mailbox_cache[$aMailbox['NAME']] = $aMailbox;
162sqsession_register($mailbox_cache,'mailbox_cache');
a2b193bc 163
134e4174 164?>