source: branches/2.2.0.1/expressoMail1_2/inc/gotodownload.php @ 4595

Revision 4595, 8.0 KB checked in by brunocosta, 13 years ago (diff)

Ticket #2004 - foram modificados os headers passados para o user agent no download de anexos para arquivamento.

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