Added documentation and fixed a minor issue
[squirrelmail.git] / src / download.php
1 <?php
2
3 /**
4 * download.php
5 *
6 * Copyright (c) 1999-2004 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
43 /* end globals */
44
45 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
46 $aMailbox = sqm_api_mailbox_select($imapConnection, $mailbox,array(),array());
47
48 if (isset($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) &&
49 is_object($aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT']) ) {
50 $message = $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'];
51 } else {
52 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
53 $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;
54 }
55
56 //$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
57
58 //$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
59 //if (!is_object($message)) {
60 // $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
61 //}
62 $subject = $message->rfc822_header->subject;
63 if ($ent_id) {
64 $message = &$message->getEntity($ent_id);
65 $header = $message->header;
66
67 if ($message->rfc822_header) {
68 $subject = $message->rfc822_header->subject;
69 } else {
70 $header = $message->header;
71 }
72 $type0 = $header->type0;
73 $type1 = $header->type1;
74 $encoding = strtolower($header->encoding);
75 } else {
76 /* raw message */
77 $type0 = 'message';
78 $type1 = 'rfc822';
79 $encoding = 'US-ASCII';
80 $header = $message->header;
81 }
82
83 /*
84 * lets redefine message as this particular entity that we wish to display.
85 * it should hold only the header for this entity. We need to fetch the body
86 * yet before we can display anything.
87 */
88
89 if (isset($override_type0)) {
90 $type0 = $override_type0;
91 }
92 if (isset($override_type1)) {
93 $type1 = $override_type1;
94 }
95 $filename = '';
96 if (is_object($message->header->disposition)) {
97 $filename = $header->disposition->getProperty('filename');
98 if (!$filename) {
99 $filename = $header->disposition->getProperty('name');
100 }
101 if (!$filename) {
102 $filename = $header->getParameter('name');
103 }
104 } else {
105 $filename = $header->getParameter('name');
106 }
107
108 //$filename = decodeHeader($filename, false, false); //Don't want html output nor utf8 because it will return html output
109 $filename = decodeHeader($filename, true, false); //Don't want html output
110 if (strlen($filename) < 1) {
111 //$filename = decodeHeader($subject, false, false); //Don't want html output nor utf8 because it will return html output
112 $filename = decodeHeader($subject, true, false); //Don't want html output
113 if ($type1 == 'plain' && $type0 == 'text')
114 $suffix = 'txt';
115 else if ($type1 == 'richtext' && $type0 == 'text')
116 $suffix = 'rtf';
117 else if ($type1 == 'postscript' && $type0 == 'application')
118 $suffix = 'ps';
119 else if ($type1 == 'rfc822' && $type0 == 'message')
120 $suffix = 'msg';
121 else
122 $suffix = $type1;
123
124 if ($filename == '')
125 $filename = 'untitled' . strip_tags($ent_id);
126 $filename = $filename . '.' . $suffix;
127 }
128
129 /*
130 * Note:
131 * The following sections display the attachment in different
132 * ways depending on how they choose. The first way will download
133 * under any circumstance. This sets the Content-type to be
134 * applicatin/octet-stream, which should be interpreted by the
135 * browser as "download me".
136 * The second method (view) is used for images or other formats
137 * that should be able to be handled by the browser. It will
138 * most likely display the attachment inline inside the browser.
139 * And finally, the third one will be used by default. If it
140 * is displayable (text or html), it will load them up in a text
141 * viewer (built in to squirrelmail). Otherwise, it sets the
142 * content-type as application/octet-stream
143 */
144 if (isset($absolute_dl) && $absolute_dl) {
145 SendDownloadHeaders($type0, $type1, $filename, 1);
146 } else {
147 SendDownloadHeaders($type0, $type1, $filename, 0);
148 }
149 /* be aware that any warning caused by download.php will corrupt the
150 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
151 mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
152 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
153 sqsession_register($mailbox_cache,'mailbox_cache');
154 ?>