source: sandbox/2.3-MailArchiver/expressoMail1_2/inc/gotodownload.php @ 6779

Revision 6779, 8.2 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado Expresso(branch 2.3) integrado ao MailArchiver?.

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        if(strpos($strFileType ,"." )===false)
53                $strFileType = strrev(substr(strrev(strtolower($newfilename)),0,4));
54
55        downloadFile($strFileType, $filename, $newfilename, $fileContent, $encoding);
56
57        function downloadFile($strFileType, $strFileName, $newFileName, $fileContent, $encoding) {
58                //avoid stuck request
59                session_write_close();
60                $ContentType = "application/octet-stream";
61
62                if ($strFileType == ".asf")
63                        $ContentType = "video/x-ms-asf";
64                if ($strFileType == ".avi")
65                        $ContentType = "video/avi";
66                if ($strFileType == ".doc")
67                        $ContentType = "application/msword";
68                if ($strFileType == ".zip")
69                        $ContentType = "application/zip";
70                if ($strFileType == ".xls")
71                        $ContentType = "application/vnd.ms-excel";
72                if ($strFileType == ".gif")
73                        $ContentType = "image/gif";
74                if ($strFileType == ".jpg" || $strFileType == "jpeg")
75                        $ContentType = "image/jpeg";
76                if ($strFileType == ".wav")
77                        $ContentType = "audio/wav";
78                if ($strFileType == ".mp3")
79                        $ContentType = "audio/mpeg3";
80                if ($strFileType == ".mpg" || $strFileType == "mpeg")
81                        $ContentType = "video/mpeg";
82                if ($strFileType == ".rtf")
83                        $ContentType = "application/rtf";
84                if ($strFileType == ".htm" || $strFileType == "html")
85                        $ContentType = "text/html";
86                if ($strFileType == ".xml")
87                        $ContentType = "text/xml";
88                if ($strFileType == ".xsl")
89                        $ContentType = "text/xsl";
90                if ($strFileType == ".css")
91                        $ContentType = "text/css";
92                if ($strFileType == ".php")
93                        $ContentType = "text/php";
94                if ($strFileType == ".asp")
95                        $ContentType = "text/asp";
96                if ($strFileType == ".pdf")
97                        $ContentType = "application/pdf";
98                if ($strFileType == ".txt")
99                        $ContentType = "text/plain";
100                if ($strFileType == ".log")
101                        $ContentType = "text/plain";
102                if ($strFileType == ".wmv")
103                        $ContentType = "video/x-ms-wmv";
104                if ($strFileType == ".sxc")
105                        $ContentType = "application/vnd.sun.xml.calc";
106                if ($strFileType == ".odt")
107                        $ContentType = "application/vnd.oasis.opendocument.text";
108                if ($strFileType == ".stc")
109                        $ContentType = "application/vnd.sun.xml.calc.template";
110                if ($strFileType == ".sxd")
111                        $ContentType = "application/vnd.sun.xml.draw";
112                if ($strFileType == ".std")
113                        $ContentType = "application/vnd.sun.xml.draw.template";
114                if ($strFileType == ".sxi")
115                        $ContentType = "application/vnd.sun.xml.impress";
116                if ($strFileType == ".sti")
117                        $ContentType = "application/vnd.sun.xml.impress.template";
118                if ($strFileType == ".sxm")
119                        $ContentType = "application/vnd.sun.xml.math";
120                if ($strFileType == ".sxw")
121                        $ContentType = "application/vnd.sun.xml.writer";
122                if ($strFileType == ".sxq")
123                        $ContentType = "application/vnd.sun.xml.writer.global";
124                if ($strFileType == ".stw")
125                        $ContentType = "application/vnd.sun.xml.writer.template";
126                if ($strFileType == ".ps")
127                        $ContentType = "application/postscript";
128                if ($strFileType == ".pps")
129                        $ContentType = "application/vnd.ms-powerpoint";
130                if ($strFileType == ".odt")
131                        $ContentType = "application/vnd.oasis.opendocument.text";
132                if ($strFileType == ".ott")
133                        $ContentType = "application/vnd.oasis.opendocument.text-template";
134                if ($strFileType == ".oth")
135                        $ContentType = "application/vnd.oasis.opendocument.text-web";
136                if ($strFileType == ".odm")
137                        $ContentType = "application/vnd.oasis.opendocument.text-master";
138                if ($strFileType == ".odg")
139                        $ContentType = "application/vnd.oasis.opendocument.graphics";
140                if ($strFileType == ".otg")
141                        $ContentType = "application/vnd.oasis.opendocument.graphics-template";
142                if ($strFileType == ".odp")
143                        $ContentType = "application/vnd.oasis.opendocument.presentation";
144                if ($strFileType == ".otp")
145                        $ContentType = "application/vnd.oasis.opendocument.presentation-template";
146                if ($strFileType == ".ods")
147                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
148                if ($strFileType == ".ots")
149                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
150                if ($strFileType == ".odc")
151                        $ContentType = "application/vnd.oasis.opendocument.chart";
152                if ($strFileType == ".odf")
153                        $ContentType = "application/vnd.oasis.opendocument.formula";
154                if ($strFileType == ".odi")
155                        $ContentType = "application/vnd.oasis.opendocument.image";
156                if ($strFileType == ".ndl")
157                        $ContentType = "application/vnd.lotus-notes";
158                if ($strFileType == ".eml")
159                        $ContentType = "text/plain";
160
161                header ("Content-Type: $ContentType");
162                header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
163                header("Pragma: public");
164                header("Expires: 0"); // set expiration time
165                header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
166                // No IE para que os nomes de arquivos com caracteres especiais no download fiquei corretos o nome deve ser codificado com urlencode.
167                 if (preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT']))
168                        $newFileName=urlencode($newFileName);
169                if($fileContent) {
170                        if($encoding == 'base64')
171                                echo imap_base64($fileContent);
172                        else if($encoding == 'quoted-printable')
173                                echo quoted_printable_decode($fileContent);
174                        else
175                                echo $fileContent;
176                }
177                else
178
179                        if (strstr($strFileName,$GLOBALS['phpgw']->session->sessionid)&&file_exists($strFileName))
180                        {
181                            header("Content-Type: $ContentType");
182                            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
183                            header("Pragma: public");
184                            header("Expires: 0"); // set expiration time
185                            header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
186                            readfile($strFileName);
187                            return ;
188                        }else{
189                            header("HTTP/1.1 404 Not Found");
190                        }
191
192
193                        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))
194                        {
195                                if ( ! preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
196                                        //reset time limit for big files
197                                        set_time_limit(0);
198                                        ob_end_flush();
199
200                                        if ($fp = fopen ($strFileName, 'rb'))
201                                        {
202                                                $bufferSize=1024;
203                                                for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize)
204                                                {
205                                                        echo fread($fp, $i);
206                                                        flush();
207                                                }
208                                                fclose ($fp);
209                                        }
210                                        //readfile($strFileName);
211
212                                        exec("rm -f ".escapeshellcmd(escapeshellarg($strFileName)));
213                                }
214                                else
215                                        readfile($strFileName);
216                                        return;
217                        }
218        }
219?>
Note: See TracBrowser for help on using the repository browser.