source: trunk/expressoMail1_2/inc/get_archive.php @ 5069

Revision 5069, 10.8 KB checked in by airton, 13 years ago (diff)

Ticket #2266 - Atualizar documentacao dos arquivos PHP

  • Property svn:executable set to *
Line 
1<?php
2/**
3*
4* Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
5*
6* This program is free software; you can redistribute it and/or modify it under
7* the terms of the GNU Affero General Public License version 3 as published by
8* the Free Software Foundation with the addition of the following permission
9* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
10* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
11* WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
12*
13* This program is distributed in the hope that it will be useful, but WITHOUT
14* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16* details.
17*
18* You should have received a copy of the GNU Affero General Public License
19* along with this program; if not, see www.gnu.org/licenses or write to
20* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21* MA 02110-1301 USA.
22*
23* This code is based on the OpenXchange Connector and on the Prognus pSync
24* Connector both developed by the community and licensed under the GPL
25* version 2 or above as published by the Free Software Foundation.
26*
27* You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
28* 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at
29* e-mail address prognus@prognus.com.br.
30*
31*
32* @package    expressoMail
33* @license    http://www.gnu.org/copyleft/gpl.html GPL
34* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
35* @version    1.0
36* @sponsor    Caixa Econômica Federal
37*/
38
39/*
40 * Definitions
41 */
42if(!isset($GLOBALS['phpgw_info']))
43{
44    $GLOBALS['phpgw_info']['flags'] = array(
45            'currentapp' => 'expressoMail1_2',
46            'nonavbar'   => true,
47            'noheader'   => true
48    );
49}
50
51$GLOBALS['phpgw_info']['server']['download_temp_dir'] = '/tmp';
52//-----------------------//
53
54/*
55 * Requires
56 */
57require_once 'class.attachment.inc.php';
58require_once '../../header.inc.php';
59//-----------------------//
60
61/*
62 * Get variables
63 */
64$newFilename = urldecode($_GET['newFilename']);
65$indexFile = $_GET['idx_file'];
66$indexPart = $_GET['indexPart'];
67$msgNumber = $_GET['msgNumber'];
68$msgFolder = $_GET['msgFolder'];
69//----------------------------------------------//
70
71
72/**
73* Método que faz o download do arquivo
74*
75* @license    http://www.gnu.org/copyleft/gpl.html GPL
76* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
77* @sponsor    Caixa Econômica Federal
78* @param  $strFileType
79* @param  $strFileName
80* @param  $newFileName
81* @param  $fileContent
82*/
83function downloadFile($strFileType, $strFileName, $newFileName, $fileContent) {
84
85        $ContentType = "application/octet-stream";
86
87        if ($strFileType == ".asf")
88                $ContentType = "video/x-ms-asf";
89        if ($strFileType == ".avi")
90                $ContentType = "video/avi";
91        if ($strFileType == ".doc")
92                $ContentType = "application/msword";
93        if ($strFileType == ".zip")
94                $ContentType = "application/zip";
95        if ($strFileType == ".xls")
96                $ContentType = "application/vnd.ms-excel";
97        if ($strFileType == ".gif")
98                $ContentType = "image/gif";
99        if ($strFileType == ".jpg" || $strFileType == "jpeg")
100                $ContentType = "image/jpeg";
101        if ($strFileType == ".wav")
102                $ContentType = "audio/wav";
103        if ($strFileType == ".mp3")
104                $ContentType = "audio/mpeg3";
105        if ($strFileType == ".mpg" || $strFileType == "mpeg")
106                $ContentType = "video/mpeg";
107        if ($strFileType == ".rtf")
108                $ContentType = "application/rtf";
109        if ($strFileType == ".htm" || $strFileType == "html")
110                $ContentType = "text/html";
111        if ($strFileType == ".xml")
112                $ContentType = "text/xml";
113        if ($strFileType == ".xsl")
114                $ContentType = "text/xsl";
115        if ($strFileType == ".css")
116                $ContentType = "text/css";
117        if ($strFileType == ".php")
118                $ContentType = "text/php";
119        if ($strFileType == ".asp")
120                $ContentType = "text/asp";
121        if ($strFileType == ".pdf")
122                $ContentType = "application/pdf";
123        if ($strFileType == ".txt")
124                $ContentType = "text/plain";
125        if ($strFileType == ".log")
126                $ContentType = "text/plain";
127        if ($strFileType == ".wmv")
128                $ContentType = "video/x-ms-wmv";
129        if ($strFileType == ".sxc")
130                $ContentType = "application/vnd.sun.xml.calc";
131        if ($strFileType == ".odt")
132                $ContentType = "application/vnd.oasis.opendocument.text";
133        if ($strFileType == ".stc")
134                $ContentType = "application/vnd.sun.xml.calc.template";
135        if ($strFileType == ".sxd")
136                $ContentType = "application/vnd.sun.xml.draw";
137        if ($strFileType == ".std")
138                $ContentType = "application/vnd.sun.xml.draw.template";
139        if ($strFileType == ".sxi")
140                $ContentType = "application/vnd.sun.xml.impress";
141        if ($strFileType == ".sti")
142                $ContentType = "application/vnd.sun.xml.impress.template";
143        if ($strFileType == ".sxm")
144                $ContentType = "application/vnd.sun.xml.math";
145        if ($strFileType == ".sxw")
146                $ContentType = "application/vnd.sun.xml.writer";
147        if ($strFileType == ".sxq")
148                $ContentType = "application/vnd.sun.xml.writer.global";
149        if ($strFileType == ".stw")
150                $ContentType = "application/vnd.sun.xml.writer.template";
151        if ($strFileType == ".ps")
152                $ContentType = "application/postscript";
153        if ($strFileType == ".pps")
154                $ContentType = "application/vnd.ms-powerpoint";
155        if ($strFileType == ".odt")
156                $ContentType = "application/vnd.oasis.opendocument.text";
157        if ($strFileType == ".ott")
158                $ContentType = "application/vnd.oasis.opendocument.text-template";
159        if ($strFileType == ".oth")
160                $ContentType = "application/vnd.oasis.opendocument.text-web";
161        if ($strFileType == ".odm")
162                $ContentType = "application/vnd.oasis.opendocument.text-master";
163        if ($strFileType == ".odg")
164                $ContentType = "application/vnd.oasis.opendocument.graphics";
165        if ($strFileType == ".otg")
166                $ContentType = "application/vnd.oasis.opendocument.graphics-template";
167        if ($strFileType == ".odp")
168                $ContentType = "application/vnd.oasis.opendocument.presentation";
169        if ($strFileType == ".otp")
170                $ContentType = "application/vnd.oasis.opendocument.presentation-template";
171        if ($strFileType == ".ods")
172                $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
173        if ($strFileType == ".ots")
174                $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
175        if ($strFileType == ".odc")
176                $ContentType = "application/vnd.oasis.opendocument.chart";
177        if ($strFileType == ".odf")
178                $ContentType = "application/vnd.oasis.opendocument.formula";
179        if ($strFileType == ".odi")
180                $ContentType = "application/vnd.oasis.opendocument.image";
181        if ($strFileType == ".ndl")
182                $ContentType = "application/vnd.lotus-notes";
183        if ($strFileType == ".eml")
184                $ContentType = "text/plain";
185
186        header ("Content-Type: $ContentType");
187        header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
188        header("Pragma: public");
189        header("Expires: 0"); // set expiration time
190        header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\"");
191
192        if($fileContent)
193        {
194          echo $fileContent;
195        }
196        else
197        {
198                $tempDir = $GLOBALS['phpgw_info']['server']['download_temp_dir'];
199                if (preg_match("#^".$tempDir."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z0-9_]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$strFileName))
200                {
201                //avoid stuck request
202                        session_write_close();
203
204                        //reset time limit for big files
205                        set_time_limit(0);
206                        ob_end_flush();
207
208                        if ($fp = fopen ($strFileName, 'rb'))
209                        {
210                                $bufferSize=1024;
211                                for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize)
212                                {
213                                        echo fread($fp, $i);
214                                        flush();
215                                }
216                                fclose ($fp);
217                        }
218
219                        //removendo pelo php, garante a suportabilidade cross-platform
220                        cleanup( dirname( $strFileName ) );
221
222                }
223                else
224                {
225                        if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) {
226                                readfile($strFileName);
227                        }
228                }
229        }
230}
231
232
233/**
234* Método que limpa o diretório
235*
236* @license    http://www.gnu.org/copyleft/gpl.html GPL
237* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
238* @sponsor    Caixa Econômica Federal
239* @param  $dir
240*/
241function cleanup( $dir )
242{
243    if ( is_dir( $dir ) )
244    {
245        $files = scandir($dir);
246
247        foreach( $files as $file )
248            if( $file != "." && $file != ".." )
249                cleanup( $dir.'/'.$file );
250
251        reset( $files ); //?
252       
253        if(!rmdir( $dir ))
254            return;
255    }
256    else
257    {
258        if(!unlink( $dir ))
259        return;
260    }
261
262}
263//-----------------------//
264
265/*
266 * Main
267 */
268
269
270if($msgNumber != 'null' && $indexPart !== null && $msgFolder)
271{
272
273    $attachment = new attachment(PHPGW_INCLUDE_ROOT);
274    $attachment->setStructureFromMail($msgFolder, $msgNumber);
275    $fileContent = $attachment->getAttachment($indexPart);
276
277    if($newFilename)
278        $filename = $newFilename;
279    else
280    {
281        $info = $attachment->getAttachmentInfo($indexPart);
282        $filename = $info['name'];
283    }
284
285
286}
287else
288    $filename = $indexFile;
289
290
291$filename        = $filename    ? $filename     : "attachment.bin";
292$newFilename = $newFilename ? $newFilename      : $filename;
293
294
295$strFileType = strrev(substr(strrev(strtolower($filename)),0,4));
296
297downloadFile($strFileType,  $filename, $newFilename, $fileContent)
298//-----------------------//
299
300?>
Note: See TracBrowser for help on using the repository browser.