source: trunk/expressoMail1_2/inc/gotodownload.php @ 3114

Revision 3114, 8.3 KB checked in by amuller, 14 years ago (diff)

Ticket #1151 - Corrige problema de requisição presa no download

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3/***************************************************************************
4        * Expresso Livre                                                           *
5        * http://www.expressolivre.org                                             *
6        * --------------------------------------------                             *
7        *  This program is free software; you can redistribute it and/or modify it *
8        *  under the terms of the GNU General Public License as published by the   *
9        *  Free Software Foundation; either version 2 of the License, or (at your  *
10        *  option) any later version.                                              *
11        \**************************************************************************/
12if(!isset($GLOBALS['phpgw_info'])){
13        $GLOBALS['phpgw_info']['flags'] = array(
14                'currentapp' => 'expressoMail1_2',
15                'nonavbar'   => true,
16                'noheader'   => true
17        );
18}
19require_once '../../header.inc.php';
20
21
22        $msg_number = $_GET['msg_number'];
23        $idx_file = $_GET['idx_file'];
24        $newfilename = urldecode($_GET['newfilename']);
25        $msg_part = $_GET['msg_part'];
26        $msg_folder = $_GET['msg_folder'];
27        $msg_folder = mb_convert_encoding($msg_folder,"UTF7-IMAP", mb_detect_encoding($msg_folder, "UTF-8, ISO-8859-1", true));
28
29        $encoding = strtolower($_GET['encoding']);
30        $fileContent = "";
31
32        if($msg_number && $msg_part && $msg_folder && (intval($idx_file == '0' ? '1' : $idx_file))) {
33                $username = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
34                $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
35                $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
36                $imap_port      = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];         
37                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
38                {
39                        $imap_options = '/tls/novalidate-cert';
40                }
41                else
42                {
43                        $imap_options = '/notls/novalidate-cert';
44                }
45                $mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$msg_folder, $username, $password);         
46                $fileContent = imap_fetchbody($mbox_stream, $msg_number, $msg_part, FT_UID);
47                /*
48                 *Removed by Bug #546
49                 *include("class.imap_attachment.inc.php");
50                 *$imap_attachment = new imap_attachment();                     
51                 *$a = $imap_attachment->download_attachment($mbox_stream, $msg_number);
52                 *$filename = $a[$idx_file]['name'];
53                 */
54                $filename = $newfilename;
55        }
56        else
57                $filename = $idx_file;
58
59        $filename        = $filename    ? $filename     : "attachment.bin";
60        $newfilename = $newfilename ? $newfilename      : $filename;
61        $strFileType = strrev(substr(strrev(strtolower($filename)),0,4));
62
63        downloadFile($strFileType, $filename, $newfilename, $fileContent, $encoding);
64
65        function downloadFile($strFileType, $strFileName, $newFileName, $fileContent, $encoding) {
66                //avoid stuck request
67                session_write_close();
68
69                $ContentType = "application/octet-stream";
70
71                if ($strFileType == ".asf")
72                        $ContentType = "video/x-ms-asf";
73                if ($strFileType == ".avi")
74                        $ContentType = "video/avi";
75                if ($strFileType == ".doc")
76                        $ContentType = "application/msword";
77                if ($strFileType == ".zip")
78                        $ContentType = "application/zip";
79                if ($strFileType == ".xls")
80                        $ContentType = "application/vnd.ms-excel";
81                if ($strFileType == ".gif")
82                        $ContentType = "image/gif";
83                if ($strFileType == ".jpg" || $strFileType == "jpeg")
84                        $ContentType = "image/jpeg";
85                if ($strFileType == ".wav")
86                        $ContentType = "audio/wav";
87                if ($strFileType == ".mp3")
88                        $ContentType = "audio/mpeg3";
89                if ($strFileType == ".mpg" || $strFileType == "mpeg")
90                        $ContentType = "video/mpeg";
91                if ($strFileType == ".rtf")
92                        $ContentType = "application/rtf";
93                if ($strFileType == ".htm" || $strFileType == "html")
94                        $ContentType = "text/html";
95                if ($strFileType == ".xml")
96                        $ContentType = "text/xml";
97                if ($strFileType == ".xsl")
98                        $ContentType = "text/xsl";
99                if ($strFileType == ".css")
100                        $ContentType = "text/css";
101                if ($strFileType == ".php")
102                        $ContentType = "text/php";
103                if ($strFileType == ".asp")
104                        $ContentType = "text/asp";
105                if ($strFileType == ".pdf")
106                        $ContentType = "application/pdf";
107                if ($strFileType == ".txt")
108                        $ContentType = "text/plain";
109                if ($strFileType == ".log")
110                        $ContentType = "text/plain";
111                if ($strFileType == ".wmv")
112                        $ContentType = "video/x-ms-wmv";
113                if ($strFileType == ".sxc")
114                        $ContentType = "application/vnd.sun.xml.calc";
115                if ($strFileType == ".odt")
116                        $ContentType = "application/vnd.oasis.opendocument.text";
117                if ($strFileType == ".stc")
118                        $ContentType = "application/vnd.sun.xml.calc.template";
119                if ($strFileType == ".sxd")
120                        $ContentType = "application/vnd.sun.xml.draw";
121                if ($strFileType == ".std")
122                        $ContentType = "application/vnd.sun.xml.draw.template";
123                if ($strFileType == ".sxi")
124                        $ContentType = "application/vnd.sun.xml.impress";
125                if ($strFileType == ".sti")
126                        $ContentType = "application/vnd.sun.xml.impress.template";
127                if ($strFileType == ".sxm")
128                        $ContentType = "application/vnd.sun.xml.math";
129                if ($strFileType == ".sxw")
130                        $ContentType = "application/vnd.sun.xml.writer";
131                if ($strFileType == ".sxq")
132                        $ContentType = "application/vnd.sun.xml.writer.global";
133                if ($strFileType == ".stw")
134                        $ContentType = "application/vnd.sun.xml.writer.template";
135                if ($strFileType == ".ps")
136                        $ContentType = "application/postscript";
137                if ($strFileType == ".pps")
138                        $ContentType = "application/vnd.ms-powerpoint";
139                if ($strFileType == ".odt")
140                        $ContentType = "application/vnd.oasis.opendocument.text";
141                if ($strFileType == ".ott")
142                        $ContentType = "application/vnd.oasis.opendocument.text-template";
143                if ($strFileType == ".oth")
144                        $ContentType = "application/vnd.oasis.opendocument.text-web";
145                if ($strFileType == ".odm")
146                        $ContentType = "application/vnd.oasis.opendocument.text-master";
147                if ($strFileType == ".odg")
148                        $ContentType = "application/vnd.oasis.opendocument.graphics";
149                if ($strFileType == ".otg")
150                        $ContentType = "application/vnd.oasis.opendocument.graphics-template";
151                if ($strFileType == ".odp")
152                        $ContentType = "application/vnd.oasis.opendocument.presentation";
153                if ($strFileType == ".otp")
154                        $ContentType = "application/vnd.oasis.opendocument.presentation-template";
155                if ($strFileType == ".ods")
156                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
157                if ($strFileType == ".ots")
158                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
159                if ($strFileType == ".odc")
160                        $ContentType = "application/vnd.oasis.opendocument.chart";
161                if ($strFileType == ".odf")
162                        $ContentType = "application/vnd.oasis.opendocument.formula";
163                if ($strFileType == ".odi")
164                        $ContentType = "application/vnd.oasis.opendocument.image";
165                if ($strFileType == ".ndl")
166                        $ContentType = "application/vnd.lotus-notes";
167                if ($strFileType == ".eml")
168                        $ContentType = "text/plain";
169
170                header ("Content-Type: $ContentType");
171                header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
172                header("Pragma: public");
173                header("Expires: 0"); // set expiration time
174                header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");         
175                if($fileContent) {
176                        if($encoding == 'base64')
177                                echo imap_base64($fileContent);
178                        else if($encoding == 'quoted-printable')
179                                echo quoted_printable_decode($fileContent);
180                        else
181                                echo $fileContent;
182                }
183                else
184                        if (preg_match("#^".ini_get('session.save_path')."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$strFileName))
185                        {
186                                //reset time limit for big files
187                                set_time_limit(0);
188
189                                ob_end_flush();
190
191                                if ($fp = fopen ($strFileName, 'rb'))
192                                {
193                                        $bufferSize=1024;
194                                        for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize)
195                                        {
196                                                echo fread($fp, $i);
197                                                flush();
198                                        }
199                                        fclose ($fp);
200                                }
201                                //readfile($strFileName);
202                                exec("rm -f ".escapeshellcmd(escapeshellarg($strFileName)));
203                        }
204                        else
205                                if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
206                                        readfile($strFileName);
207                                }
208        }
209?>
Note: See TracBrowser for help on using the repository browser.