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