String fix
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
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 *
12 * @version $Id$
13 * @package squirrelmail
14 */
15
16 /**
17 * Path for SquirrelMail required files.
18 * @ignore
19 */
20 define('SM_PATH','../');
21
22 /* SquirrelMail required files. */
23 require_once(SM_PATH . 'include/validate.php');
24 require_once(SM_PATH . 'functions/imap.php');
25 require_once(SM_PATH . 'functions/mime.php');
26
27 header('Pragma: ');
28 header('Cache-Control: cache');
29
30 /* globals */
31 sqgetGlobalVar('key', $key, SQ_COOKIE);
32 sqgetGlobalVar('username', $username, SQ_SESSION);
33 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
34 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
35 sqgetGlobalVar('messages', $messages, SQ_SESSION);
36 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
37 sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
38 sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
39 if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
40 $passed_id = (int) $temp;
41 }
42 if (!sqgetGlobalVar('account', $account, SQ_GET) ) {
43 $account = 0;
44 }
45
46 global $default_charset;
47 set_my_charset();
48
49 /* end globals */
50
51 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
52 $aMailbox = sqm_api_mailbox_select($imapConnection, $account, $mailbox,array(),array());
53
54 if (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;
60 }
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 //}
68 $subject = $message->rfc822_header->subject;
69 if ($ent_id) {
70 // replace message with message part, if message part is requested.
71 $message = $message->getEntity($ent_id);
72 $header = $message->header;
73
74 if ($message->rfc822_header) {
75 $subject = $message->rfc822_header->subject;
76 } else {
77 $header = $message->header;
78 }
79 $type0 = $header->type0;
80 $type1 = $header->type1;
81 $encoding = strtolower($header->encoding);
82 } else {
83 /* raw message */
84 $type0 = 'message';
85 $type1 = 'rfc822';
86 $encoding = 'US-ASCII';
87 $header = $message->header;
88 }
89
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 */
95
96 if (isset($override_type0)) {
97 $type0 = $override_type0;
98 }
99 if (isset($override_type1)) {
100 $type1 = $override_type1;
101 }
102 $filename = '';
103 if (is_object($message->header->disposition)) {
104 $filename = $header->disposition->getProperty('filename');
105 if (!$filename) {
106 $filename = $header->disposition->getProperty('name');
107 }
108 if (!$filename) {
109 $filename = $header->getParameter('name');
110 }
111 } else {
112 $filename = $header->getParameter('name');
113 }
114
115 $filename = decodeHeader($filename,true,false);
116 $filename = charset_encode($filename,$default_charset,false);
117
118 // If name is not set, use subject of email
119 if (strlen($filename) < 1) {
120 $filename = decodeHeader($subject, true, true);
121 $filename = charset_encode($filename,$default_charset,false);
122 if ($type1 == 'plain' && $type0 == 'text')
123 $suffix = 'txt';
124 else if ($type1 == 'richtext' && $type0 == 'text')
125 $suffix = 'rtf';
126 else if ($type1 == 'postscript' && $type0 == 'application')
127 $suffix = 'ps';
128 else if ($type1 == 'rfc822' && $type0 == 'message')
129 $suffix = 'msg';
130 else
131 $suffix = $type1;
132
133 if ($filename == '')
134 $filename = 'untitled' . strip_tags($ent_id);
135 $filename = $filename . '.' . $suffix;
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
150 * viewer (built in to SquirrelMail). Otherwise, it sets the
151 * content-type as application/octet-stream
152 */
153 if (isset($absolute_dl) && $absolute_dl) {
154 SendDownloadHeaders($type0, $type1, $filename, 1);
155 } else {
156 SendDownloadHeaders($type0, $type1, $filename, 0);
157 }
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 */
160 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
161 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
162 sqsession_register($mailbox_cache,'mailbox_cache');
163
164 ?>