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