source: branches/2.4/expressoMail1_2/inc/get_archive.php @ 7250

Revision 7250, 8.9 KB checked in by eduardow, 12 years ago (diff)

Ticket #3109 - Problema para abrir anexos - erro da session

  • Property svn:executable set to *
Line 
1<?php
2/*
3 * Definitions
4 */
5if(!isset($GLOBALS['phpgw_info']))
6{
7    $GLOBALS['phpgw_info']['flags'] = array(
8            'currentapp' => 'expressoMail1_2',
9            'nonavbar'   => true,
10            'noheader'   => true
11    );
12}
13
14$GLOBALS['phpgw_info']['server']['download_temp_dir'] = '/tmp';
15//-----------------------//
16
17//-----------------------//
18
19/*
20 * Get variables
21 */
22if(array_key_exists('newFilename', $_GET ) && urldecode($_GET['newFilename']) !== 'application.octet-stream')
23        $newFilename = urldecode($_GET['newFilename']);
24else
25        $newFilename = null;
26if(array_key_exists('idx_file', $_GET ))
27        $indexFile = $_GET['idx_file'];
28else
29        $indexFile = null;
30
31$indexPart = $_GET['indexPart'];
32$msgNumber = $_GET['msgNumber'];
33$msgFolder = $_GET['msgFolder'];
34if(array_key_exists('fileType', $_GET ))
35        $ContentType = $_GET['fileType'];
36else
37        $ContentType = null;
38if(array_key_exists('image', $_GET ))
39        $image = $_GET['image'];
40else
41        $image = null;
42//----------------------------------------------//
43
44/*
45 * Requires
46 */
47//if( $indexFile )
48    require_once '../../header.inc.php';
49//else
50//    require_once '../../header.session.inc.php';
51
52
53/*
54 * Functions
55 */
56
57function unhtmlentities($string)
58{
59    $string = utf8_encode($string);
60   
61    static $trans_tbl;
62 
63    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
64    $string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);
65
66    if (!isset($trans_tbl))
67        {
68        $trans_tbl = array();
69       
70        foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
71            $trans_tbl[$key] = utf8_encode($val);
72        }
73    return strtr($string, $trans_tbl);
74}
75
76function code2utf($num)
77{
78    if ($num < 128) return chr($num);
79    if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
80    if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
81    if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
82    return '';
83}
84
85//-----------------------//
86
87/*
88 * Main
89 */
90
91
92if($msgNumber != 'null' && $indexPart !== null && $msgFolder)
93{
94    require_once dirname(__FILE__).'/class.attachment.inc.php';
95
96    $attachment = new attachment();
97    $attachment->setStructureFromMail($msgFolder, $msgNumber);
98    $fileContent = $attachment->getAttachment($indexPart);
99    $info = $attachment->getAttachmentInfo($indexPart);
100    $filename = $newFilename ? $newFilename : $info['name'];       
101    $filename = unhtmlentities($filename);
102}
103else
104{
105    if( $image && $_SESSION['phpgw_info']['expressomail']['contact_photo'] )
106                                {
107        $image = "thumbnail";
108
109        $photo = array( 'width' => 60, 'height' => 80, 'quality' => 100 );
110
111        $fileContent = $_SESSION['phpgw_info']['expressomail']['contact_photo'][0];
112        unset( $_SESSION['phpgw_info']['expressomail']['contact_photo'] );
113                }
114
115    $filename = $indexFile;
116}
117
118$filename = $filename   ? $filename     : "attachment.bin";
119$newFilename = $newFilename ? $newFilename      : $filename;
120$newFilename = unhtmlentities($newFilename);
121$disposition = $image ? "inline" : "attachment; filename=\"". addslashes($newFilename)."\"";
122
123if( !$ContentType || $ContentType == 'application/octet-stream')
124{
125   
126    if( strstr($_SERVER['HTTP_USER_AGENT'],'MSIE') && $disposition != 'inline' )
127        $ContentType = 'application-download';
128                else
129                {
130        function guessContentType( $fileName )
131        {
132            $strFileType = strtolower(substr ( $fileName , strrpos($fileName, '.') ));
133                         
134            switch( $strFileType )
135            {
136                case ".asf": return "video/x-ms-asf";
137                case ".avi": return "video/avi";
138                case ".doc": return "application/msword";
139                case ".zip": return "application/zip";
140                case ".xls": return "application/vnd.ms-excel";
141                case ".gif": return "image/gif";
142                case ".bmp": return "image/bmp";
143                case ".jpeg":
144                case ".jpg": return "image/jpeg";
145                case ".wav": return "audio/wav";
146                case ".mp3": return "audio/mpeg3";
147                case ".mpeg":
148                case ".mpg": return "video/mpeg";
149                case ".rtf": return "application/rtf";
150                case ".html":
151                case ".htm": return "text/html";
152                case ".xml": return "text/xml";
153                case ".xsl": return "text/xsl";
154                case ".css": return "text/css";
155                case ".php": return "text/php";
156                case ".asp": return "text/asp";
157                case ".pdf": return "application/pdf";
158                case ".png": return "image/png";
159                case ".txt": return "text/plain";
160                case ".log": return "text/plain";
161                case ".wmv": return "video/x-ms-wmv";
162                case ".sxc": return "application/vnd.sun.xml.calc";
163                case ".odt": return "application/vnd.oasis.opendocument.text";
164                case ".stc": return "application/vnd.sun.xml.calc.template";
165                case ".sxd": return "application/vnd.sun.xml.draw";
166                case ".std": return "application/vnd.sun.xml.draw.template";
167                case ".sxi": return "application/vnd.sun.xml.impress";
168                case ".sti": return "application/vnd.sun.xml.impress.template";
169                case ".sxm": return "application/vnd.sun.xml.math";
170                case ".sxw": return "application/vnd.sun.xml.writer";
171                case ".sxq": return "application/vnd.sun.xml.writer.global";
172                case ".stw": return "application/vnd.sun.xml.writer.template";
173                case ".pps": return "application/vnd.ms-powerpoint";
174                case ".odt": return "application/vnd.oasis.opendocument.text";
175                case ".ott": return "application/vnd.oasis.opendocument.text-template";
176                case ".oth": return "application/vnd.oasis.opendocument.text-web";
177                case ".odm": return "application/vnd.oasis.opendocument.text-master";
178                case ".odg": return "application/vnd.oasis.opendocument.graphics";
179                case ".otg": return "application/vnd.oasis.opendocument.graphics-template";
180                case ".odp": return "application/vnd.oasis.opendocument.presentation";
181                case ".otp": return "application/vnd.oasis.opendocument.presentation-template";
182                case ".ods": return "application/vnd.oasis.opendocument.spreadsheet";
183                case ".ots": return "application/vnd.oasis.opendocument.spreadsheet-template";
184                case ".odc": return "application/vnd.oasis.opendocument.chart";
185                case ".odf": return "application/vnd.oasis.opendocument.formula";
186                case ".odi": return "application/vnd.oasis.opendocument.image";
187                case ".ndl": return "application/vnd.lotus-notes";
188                case ".eml": return "text/plain";
189                case ".ps" : return "application/postscript";
190                default    : return "application/octet-stream";
191                        }
192                }
193
194        $ContentType = guessContentType( $filename );
195        }
196}
197
198header("Content-Type: $ContentType");
199header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
200header("Pragma: public");
201header("Expires: 0"); // set expiration time
202header("Content-Disposition: $disposition");
203
204if( $fileContent )
205{
206    header("Content-Length: ". mb_strlen($fileContent));
207   
208    if( isset($info["encoding"]) )
209    {
210        header("Content-transfer-encoding: ".$info["encoding"] );
211    //    header("Content-encoding: ".$info["encoding"] );
212    }
213   
214    if( $image === 'thumbnail'  && (strtolower(substr ( $info['name'] , (strrpos($info['name'], '.')+1))) !== 'bmp') )
215    {
216        $pic = imagecreatefromstring( $fileContent );
217
218        if( $pic )
219        {
220            $width = imagesx($pic);
221            $height = imagesy($pic);
222
223            if(!isset($photo))
224                        $photo = array( 'width' => 160, 'height' => max( ( 160 * $height / $width ), 1 ) );
225
226            $thumb = imagecreatetruecolor( $photo['width'], $photo['height'] );
227
228            if(array_key_exists('quality', $photo)  && $photo['quality'] )
229                        imagecopyresampled($thumb, $pic, 0, 0, 0, 0, $photo['width'], $photo['height'], $width, $height);
230            else
231                imagecopyresized($thumb, $pic, 0, 0, 0, 0, $photo['width'], $photo['height'], $width, $height); # resize image into thumb
232
233            imagejpeg( $thumb,"", 100 ); # Thumbnail as JPEG
234        }
235    }
236    else
237        echo $fileContent;
238}
239else
240{   
241    /**
242    * Delete Diretorio
243    * @param string $dir
244    */
245    function cleanup( $dir )
246    {
247    if ( is_dir( $dir ) )
248    {
249        $files = scandir($dir);
250
251        foreach( $files as $file )
252            if( $file != "." && $file != ".." )
253                cleanup( $dir.'/'.$file );
254
255        reset( $files ); //?
256       
257        if(!rmdir( $dir ))
258            return;
259    }
260    else
261    {
262        if(!unlink( $dir ))
263        return;
264    }
265
266    }
267
268    $tempDir = $GLOBALS['phpgw_info']['server']['download_temp_dir'];
269
270    header("Content-Length: ". filesize($filename));
271    header("Content-encoding: text/plain" );
272   
273    if (preg_match("#^".$tempDir."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z0-9_]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$filename))
274    {
275        session_write_close();
276        set_time_limit(0);
277        ob_clean();
278        flush();
279        readfile($filename);
280
281        //removendo pelo php, garante a suportabilidade cross-platform
282        cleanup(  $filename  );
283
284    }
285    else
286    {
287            if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$filename)) {
288                    readfile($filename);
289            }
290    }
291}
292       
293//-----------------------//
294
295?>
Note: See TracBrowser for help on using the repository browser.