Reinsert Download as a file link
[squirrelmail.git] / src / download.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * download.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 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$
2b646597 13 * @package squirrelmail
35586184 14 */
15
2b646597 16/** Path for SquirrelMail required files. */
86725763 17define('SM_PATH','../');
18
19/* SquirrelMail required files. */
08185f2a 20require_once(SM_PATH . 'include/validate.php');
86725763 21require_once(SM_PATH . 'functions/imap.php');
22require_once(SM_PATH . 'functions/mime.php');
6b96544a 23
65c3ec94 24header('Pragma: ');
25header('Cache-Control: cache');
26
0b97a708 27/* globals */
1e12d1ff 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;
da2415c1 37}
1075eaf1 38
0b97a708 39/* end globals */
97d7da3b 40
1075eaf1 41global $uid_support;
97d7da3b 42
1e606225 43$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
44$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox);
97d7da3b 45
1e606225 46$message = &$messages[$mbx_response['UIDVALIDITY']]["$passed_id"];
5a1c48cd 47if (!is_object($message)) {
48 $message = sqimap_get_message($imapConnection,$passed_id, $mailbox);
49}
ff9d4297 50$subject = $message->rfc822_header->subject;
6914aa18 51if ($ent_id) {
52 $message = &$message->getEntity($ent_id);
53 $header = $message->header;
da2415c1 54
6914aa18 55 if ($message->rfc822_header) {
56 $subject = $message->rfc822_header->subject;
6914aa18 57 } else {
58 $header = $message->header;
6914aa18 59 }
60 $type0 = $header->type0;
61 $type1 = $header->type1;
62 $encoding = strtolower($header->encoding);
ff9d4297 63} else {
6914aa18 64 /* raw message */
65 $type0 = 'message';
66 $type1 = 'rfc822';
bc5ff7c9 67 $encoding = 'US-ASCII';
5d39ca4d 68 $header = $message->header;
ff9d4297 69}
97d7da3b 70
65c3ec94 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 */
65c3ec94 76
65c3ec94 77if (isset($override_type0)) {
78 $type0 = $override_type0;
79}
80if (isset($override_type1)) {
81 $type1 = $override_type1;
82}
ff9d4297 83$filename = '';
84if (is_object($message->header->disposition)) {
a91189d6 85 $filename = $header->disposition->getProperty('filename');
ff9d4297 86 if (!$filename) {
708ea172 87 $filename = $header->disposition->getProperty('name');
ff9d4297 88 }
6914aa18 89 if (!$filename) {
a91189d6 90 $filename = $header->getParameter('name');
da2415c1 91 }
cf22004d 92} else {
5d39ca4d 93 $filename = $header->getParameter('name');
65c3ec94 94}
bc5ff7c9 95
da2415c1 96$filename = decodeHeader($filename, true, false); //Don't want html output
65c3ec94 97if (strlen($filename) < 1) {
98 if ($type1 == 'plain' && $type0 == 'text') {
99 $suffix = 'txt';
708ea172 100 $filename = $subject . '.txt';
65c3ec94 101 } else if ($type1 == 'richtext' && $type0 == 'text') {
102 $suffix = 'rtf';
708ea172 103 $filename = $subject . '.rtf';
65c3ec94 104 } else if ($type1 == 'postscript' && $type0 == 'application') {
105 $suffix = 'ps';
708ea172 106 $filename = $subject . '.ps';
97d7da3b 107 } else if ($type1 == 'rfc822' && $type0 == 'message') {
108 $suffix = 'eml';
708ea172 109 $filename = $subject . '.msg';
65c3ec94 110 } else {
111 $suffix = $type1;
112 }
113
631db37e 114 if (strlen($filename) < 1) {
5d39ca4d 115 $filename = 'untitled'.strip_tags($ent_id).'.'.$suffix;
631db37e 116 } else {
ff9d4297 117 $filename = "$filename.$suffix";
631db37e 118 }
65c3ec94 119}
120
121/*
122 * Note:
123 * The following sections display the attachment in different
124 * ways depending on how they choose. The first way will download
125 * under any circumstance. This sets the Content-type to be
126 * applicatin/octet-stream, which should be interpreted by the
127 * browser as "download me".
128 * The second method (view) is used for images or other formats
129 * that should be able to be handled by the browser. It will
130 * most likely display the attachment inline inside the browser.
131 * And finally, the third one will be used by default. If it
132 * is displayable (text or html), it will load them up in a text
133 * viewer (built in to squirrelmail). Otherwise, it sets the
134 * content-type as application/octet-stream
135 */
88d916ee 136if (isset($absolute_dl) && $absolute_dl) {
da2415c1 137 SendDownloadHeaders($type0, $type1, $filename, 1);
65c3ec94 138} else {
da2415c1 139 SendDownloadHeaders($type0, $type1, $filename, 0);
65c3ec94 140}
38bca81c 141/* be aware that any warning caused by download.php will corrupt the
142 * attachment in case of ERROR reporting = E_ALL and the output is the screen */
ff9d4297 143mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding);
65c3ec94 144
2b646597 145?>