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