- moved DumpHeaders fn from src/download.php to mime.php
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2003 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 * $Id$
13 */
14
15 /* Path for SquirrelMail required files. */
16 define('SM_PATH','../');
17
18 /* SquirrelMail required files. */
19 require_once(SM_PATH . 'include/validate.php');
20 require_once(SM_PATH . 'functions/imap.php');
21 require_once(SM_PATH . 'functions/mime.php');
22
23 header('Pragma: ');
24 header('Cache-Control: cache');
25
26 /* globals */
27 sqgetGlobalVar('key', $key, SQ_COOKIE);
28 sqgetGlobalVar('username', $username, SQ_SESSION);
29 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
30 sqgetGlobalVar('messages', $messages, SQ_SESSION);
31 sqgetGlobalVar('mailbox', $mailbox, SQ_GET);
32 sqgetGlobalVar('ent_id', $ent_id, SQ_GET);
33 sqgetGlobalVar('absolute_dl',$absolute_dl, SQ_GET);
34 if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
35 $passed_id = (int) $temp;
36 }
37
38 /* end globals */
39
40 global $uid_support;
41
42 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
43 $mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
44
45 $message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
46 if (!is_object($message)) {
47 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
48 }
49 $subject = $message->rfc822_header->subject;
50 if ($ent_id) {
51 $message = &$message->getEntity($ent_id);
52 $header = $message->header;
53
54 if ($message->rfc822_header) {
55 $subject = $message->rfc822_header->subject;
56 } else {
57 $header = $message->header;
58 }
59 $type0 = $header->type0;
60 $type1 = $header->type1;
61 $encoding = strtolower($header->encoding);
62 } else {
63 /* raw message */
64 $type0 = 'message';
65 $type1 = 'rfc822';
66 $encoding = 'US-ASCII';
67 $header = $message->header;
68 }
69
70 /*
71 * lets redefine message as this particular entity that we wish to display.
72 * it should hold only the header for this entity. We need to fetch the body
73 * yet before we can display anything.
74 */
75
76 if (isset($override_type0)) {
77 $type0 = $override_type0;
78 }
79 if (isset($override_type1)) {
80 $type1 = $override_type1;
81 }
82 $filename = '';
83 if (is_object($message->header->disposition)) {
84 $filename = $header->disposition->getProperty('filename');
85 if (!$filename) {
86 $filename = $header->disposition->getProperty('name');
87 }
88 if (!$filename) {
89 $filename = $header->getParameter('name');
90 }
91 } else {
92 $filename = $header->getParameter('name');
93 }
94
95 $filename = decodeHeader($filename, true, false); //Don't want html output
96 if (strlen($filename) < 1) {
97 if ($type1 == 'plain' && $type0 == 'text') {
98 $suffix = 'txt';
99 $filename = $subject . '.txt';
100 } else if ($type1 == 'richtext' && $type0 == 'text') {
101 $suffix = 'rtf';
102 $filename = $subject . '.rtf';
103 } else if ($type1 == 'postscript' && $type0 == 'application') {
104 $suffix = 'ps';
105 $filename = $subject . '.ps';
106 } else if ($type1 == 'rfc822' && $type0 == 'message') {
107 $suffix = 'eml';
108 $filename = $subject . '.msg';
109 } else {
110 $suffix = $type1;
111 }
112
113 if (strlen($filename) < 1) {
114 $filename = 'untitled'.strip_tags($ent_id).'.'.$suffix;
115 } else {
116 $filename = "$filename.$suffix";
117 }
118 }
119
120 /*
121 * Note:
122 * The following sections display the attachment in different
123 * ways depending on how they choose. The first way will download
124 * under any circumstance. This sets the Content-type to be
125 * applicatin/octet-stream, which should be interpreted by the
126 * browser as "download me".
127 * The second method (view) is used for images or other formats
128 * that should be able to be handled by the browser. It will
129 * most likely display the attachment inline inside the browser.
130 * And finally, the third one will be used by default. If it
131 * is displayable (text or html), it will load them up in a text
132 * viewer (built in to squirrelmail). Otherwise, it sets the
133 * content-type as application/octet-stream
134 */
135 if (isset($absolute_dl) && $absolute_dl) {
136 SendDownloadHeaders($type0, $type1, $filename, 1);
137 } else {
138 SendDownloadHeaders($type0, $type1, $filename, 0);
139 }
140 /* be aware that any warning caused by download.php will corrupt the
141 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
142 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
143
144 ?>