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