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

Revision 4746, 8.1 KB checked in by brunocosta, 13 years ago (diff)

Ticket #2126 - Acerta tipo MIME nos anexos de msgs arquivados.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[1473]1<?php
[1037]2if(!isset($GLOBALS['phpgw_info'])){
[1011]3        $GLOBALS['phpgw_info']['flags'] = array(
4                'currentapp' => 'expressoMail1_2',
5                'nonavbar'   => true,
6                'noheader'   => true
7        );
[1037]8}
[1040]9require_once '../../header.inc.php';
[1036]10
[1040]11
[2]12        $msg_number = $_GET['msg_number'];
13        $idx_file = $_GET['idx_file'];
[3369]14        $newfilename = html_entity_decode(rawurldecode($_GET['newfilename']));
[2]15        $msg_part = $_GET['msg_part'];
16        $msg_folder = $_GET['msg_folder'];
[178]17        $msg_folder = mb_convert_encoding($msg_folder,"UTF7-IMAP", mb_detect_encoding($msg_folder, "UTF-8, ISO-8859-1", true));
[1036]18
[2]19        $encoding = strtolower($_GET['encoding']);
20        $fileContent = "";
[1036]21
[2]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'];
[3369]26                $imap_port      = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
[27]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                }
[3369]35                $mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$msg_folder, $username, $password);
[1036]36                $fileContent = imap_fetchbody($mbox_stream, $msg_number, $msg_part, FT_UID);
[1005]37                /*
[1036]38                 *Removed by Bug #546
39                 *include("class.imap_attachment.inc.php");
[3369]40                 *$imap_attachment = new imap_attachment();
[1036]41                 *$a = $imap_attachment->download_attachment($mbox_stream, $msg_number);
42                 *$filename = $a[$idx_file]['name'];
43                 */
[1005]44                $filename = $newfilename;
[2]45        }
46        else
47                $filename = $idx_file;
48
[1036]49        $filename        = $filename    ? $filename     : "attachment.bin";
50        $newfilename = $newfilename ? $newfilename      : $filename;
51        $strFileType = strrev(substr(strrev(strtolower($filename)),0,4));
[4746]52        if(strpos($strFileType ,"." )===false)
53                $strFileType = strrev(substr(strrev(strtolower($newfilename)),0,4));
[1036]54
55        downloadFile($strFileType, $filename, $newfilename, $fileContent, $encoding);
56
57        function downloadFile($strFileType, $strFileName, $newFileName, $fileContent, $encoding) {
[3369]58                //avoid stuck request
59                session_write_close();
[1036]60                $ContentType = "application/octet-stream";
61
[3369]62                if ($strFileType == ".asf")
[1036]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";
[3369]86                if ($strFileType == ".xml")
[1036]87                        $ContentType = "text/xml";
[3369]88                if ($strFileType == ".xsl")
[1036]89                        $ContentType = "text/xsl";
[3369]90                if ($strFileType == ".css")
[1036]91                        $ContentType = "text/css";
[3369]92                if ($strFileType == ".php")
[1036]93                        $ContentType = "text/php";
[3369]94                if ($strFileType == ".asp")
[1036]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";
[1899]158                if ($strFileType == ".eml")
159                        $ContentType = "text/plain";
[1036]160
161                header ("Content-Type: $ContentType");
[1040]162                header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
[1036]163                header("Pragma: public");
164                header("Expires: 0"); // set expiration time
[3369]165                header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
[4595]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) {
[1036]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                }
[2]177                else
[3240]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                        }else{
188                            header("HTTP/1.1 404 Not Found");
189                        }
190
191
[1597]192                        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))
[1040]193                        {
[3139]194                                if ( ! preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
[3240]195                                        //reset time limit for big files
196                                        set_time_limit(0);
197                                        ob_end_flush();
[3018]198
[3240]199                                        if ($fp = fopen ($strFileName, 'rb'))
200                                        {
201                                                $bufferSize=1024;
202                                                for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize)
203                                                {
204                                                        echo fread($fp, $i);
205                                                        flush();
206                                                }
207                                                fclose ($fp);
208                                        }
209                                        //readfile($strFileName);
[3018]210
211                                        exec("rm -f ".escapeshellcmd(escapeshellarg($strFileName)));
[1937]212                                }
[3240]213                                else
[3139]214                                        readfile($strFileName);
[3018]215                        }
[3080]216        }
[1036]217?>
Note: See TracBrowser for help on using the repository browser.