source: sandbox/2.2.0.2/expressoMail1_2/inc/gotodownload.php @ 4348

Revision 4348, 7.5 KB checked in by adriano, 13 years ago (diff)

Ticket #1876 - Problemas na exportacao de mensagem - corrige problemas na exportacao de mensagens do Expresso Mail

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