source: trunk/expressoMail1_2/inc/class.exporteml.inc.php @ 5316

Revision 5316, 26.5 KB checked in by cristiano, 12 years ago (diff)

Ticket #2423 - Trocar chamada da variável rootPath

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2/***************************************************************************************\
3* Export EML Format Message Mail                                                                                                                *
4* Written by Nilton Neto (Celepar) <niltonneto@celepar.pr.gov.br>                                               *
5* ------------------------------------------------------------------------------------  *
6*  This program is free software; you can redistribute it and/or modify it                              *
7*   under the terms of the GNU General Public License as published by the                               *
8*  Free Software Foundation; either version 2 of the License, or (at your                               *
9*  option) any later version.                                                                                                                   *
10\****************************************************************************************/
11// BEGIN CLASS
12class ExportEml
13{
14        var $msg;
15        var $folder;
16        var $mbox_stream;
17        var $tempDir;
18       
19        function ExportEml() {
20           
21                //TODO: modificar o caminho hardcodificado '/tmp' para o definido na configuracao do expresso
22                //$this->tempDir = $GLOBALS['phpgw_info']['server']['temp_dir'];
23                $this->tempDir = '/tmp';
24        }
25       
26        function connectImap(){
27       
28                $username = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
29                $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
30                $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
31                $imap_port      = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
32               
33                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
34                {
35                        $imap_options = '/tls/novalidate-cert';
36                }
37                else
38                {
39                        $imap_options = '/notls/novalidate-cert';
40                }
41                $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password);
42        }
43       
44        //export message to EML Format
45        function parseEml($header, $body)       
46        {               
47                $sEmailHeader = $header;
48                $sEmailBody = $body;
49                $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody;             
50                return $sEMail;
51        }
52       
53        // create EML File.
54        // Funcao alterada para tratar a exportacao
55        // de mensagens arquivadas localmente.
56        // Rommel Cysne (rommel.cysne@serpro.gov.br)
57        // em 17/12/2008.
58        function createFileEml($sEMLData, $tempDir, $id, $subject=false, $i=false)
59    {
60        if($id)
61        {
62            $header    = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $id), 80, 255);
63            $subject = $this->decode_subject($header->fetchsubject);
64           
65            if (strlen($subject) > 60)
66                $subject = substr($subject, 0, 59);
67 
68                        //$subject = ereg_replace('/', '\'', $subject);
69                        $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
70                        $to =   "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
71                        $subject = strtr($subject,$from,$to);
72
73                        $subject = eregi_replace("[^a-zA-Z0-9_]", "_", $subject);
74                        $file = $subject."_".$id.".eml";
75                } else{
76                        // Se mensagem for arquivada localmente, $subject (assunto da mensagem)
77                        // sera passado para compor o nome do arquivo .eml;
78
79                        if($subject && $i){
80                                $from = "áàâãäéèêëíìîïóòôõöúùûüç?\"!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº° .ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ";
81                                $to =   "aaaaaeeeeiiiiooooouuuuc______________________________________________AAAAAEEEEIIIIOOOOOUUUUC";
82                                $subject = strtr($subject,$from,$to);
83
84                                $subject = eregi_replace("[^a-zA-Z0-9_]", "_", $subject);
85
86                                // é necessário que a sessão faça parte do nome do arquivo para que o mesmo não venha vazio o.O
87                                $file = $subject."_".$i."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml"; 
88                        } else{
89                                $file = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".eml";
90                }   
91        }
92       
93        $f = fopen($tempDir.'/'.$file,"w");
94        if(!$f)
95            return False;
96       
97        fputs($f,$sEMLData);
98        fclose($f);
99       
100        return $file;
101    }
102
103        function createFileZip($files, $tempDir){               
104                $tmp_zip_filename = "email_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".zip";
105               
106                if (!empty($files))
107                {
108                    if (is_array($files))
109                    {
110                        for ($i=0; $i < count($files); $i++)
111                        {
112                            $files[$i] = escapeshellarg($files[$i]);
113                        }
114                        $files = implode(' ', $files);
115                    }
116                    else
117                    {
118                        $files = escapeshellcmd($files);
119                    }
120                }
121               
122                $command = "cd " . escapeshellarg($tempDir) . " && nice zip -m9 " . escapeshellarg($tmp_zip_filename) . " " .  $files;
123                if(!exec($command)) {
124                        $command = "cd " .  escapeshellarg($tempDir) . " && rm ".$files." ". escapeshellarg($tmp_zip_filename);
125                        exec($command);
126                        return null;
127                }
128               
129                return $tmp_zip_filename;
130                               
131        }
132
133        function export_all($params){
134               
135                $this->folder = $params['folder'];
136                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
137                $fileNames = "";
138                $tempDir = $this->tempDir;
139                $this->connectImap();
140               
141                $msgs = imap_search($this->mbox_stream,"ALL",SE_UID);
142                if($msgs){
143                        foreach($msgs as $nMsgs){
144                                $header         = $this-> getHeader($nMsgs);                                                                   
145                                $body           = $this-> getBody($nMsgs);                     
146                                $sEMLData       = $this -> parseEml($header, $body);
147                                $fileName       = $this -> CreateFileEml($sEMLData, $tempDir,$nMsgs);
148                                if(!$fileName)  {
149                                        $error = True;                                 
150                                        break;
151                                }
152                                else
153                                        $fileNames .= "\"".$fileName."\" ";                     
154                               
155                        }
156                       
157                        imap_close($this->mbox_stream);
158                       
159                        $nameFileZip = 'False';                 
160                        if($fileNames && !$error) {                     
161                                $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
162                                if($nameFileZip)                       
163                                        $file = $tempDir.'/'.$nameFileZip;
164                                else {
165                                        $file = false;
166                                }                                                               
167                        }
168                        else
169                                $file = false;
170                }else{
171                        $file["empty_folder"] = true;
172                }
173                return $file;
174               
175        }
176
177        // Funcao alterada para tratar a exportacao
178        // de mensagens arquivadas localmente.
179        // Rommel Cysne (rommel.cysne@serpro.gov.br)
180        // em 17/12/2008.
181        // 
182        // Funcao alterada para que, quando houver 
183        // apenas um arquivo a ser exportado,
184        // não seja criado em zip
185        //
186        // Funcao altarada para exportar uma ou
187        // varia mensagens de um pesquisa
188
189        function makeAll($params) {
190        //Exporta menssagens selecionadas na pesquisa
191        if($params['folder'] === 'false'){
192               
193                $this->folder = $params['folder'];
194                $error = False;
195                $fileNames = "";
196               
197                $sel_msgs = explode(",", $params['msgs_to_export']);
198                @reset($sel_msgs);
199                $sorted_msgs = array();
200                foreach($sel_msgs as $idx => $sel_msg) {
201                        $sel_msg = explode(";", $sel_msg);
202                        if(array_key_exists($sel_msg[0], $sorted_msgs)){
203                                $sorted_msgs[$sel_msg[0]] .= ",".$sel_msg[1];
204                        }
205                        else {
206                                $sorted_msgs[$sel_msg[0]] = $sel_msg[1];
207                        }
208                }
209                       
210                unset($sorted_msgs['']);                       
211
212               
213                // Verifica se as n mensagens selecionadas
214                // se encontram em um mesmo folder
215                if (count($sorted_msgs)==1){
216                                                       
217                        $array_names_keys = array_keys($sorted_msgs);
218                        $this->folder = mb_convert_encoding($array_names_keys[0], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP");
219                        $msg_number = explode(',', $sorted_msgs[$array_names_keys[0]]);
220                        $tempDir = $this->tempDir;
221                        $this->connectImap();
222                       
223                        //verifica se apenas uma mensagem foi selecionada e exportar em .eml                   
224                        if(count($msg_number) == 1){
225                                $header         = $this->getHeader($msg_number[0]);
226                                $body           = $this->getBody($msg_number[0]);                       
227                                $sEMLData       = $this->parseEml($header, $body);                     
228                                $fileName       = $this->CreateFileEml($sEMLData, $tempDir, $msg_number[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]);
229               
230                               
231                                imap_close($this->mbox_stream);
232                                if (!$fileName) {
233                                        return false;
234                                }else{
235                                        return $tempDir.'/'.$fileName;
236                                }
237                        }
238                       
239                        //cria um .zip com as mensagens selecionadas
240                        for($i = 0; $i < count($msg_number); $i++)
241                        {
242                                $header         = $this-> getHeader($msg_number[$i]);                                                                                   
243                                $body           = $this-> getBody($msg_number[$i]);                     
244                                $sEMLData       = $this -> parseEml($header, $body);                   
245                                $fileName       = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$i]);
246
247                                if(!$fileName)
248                                {
249                                        $error = True;                                 
250                                        break;
251                                } else{
252                                        $fileNames .= "\"".$fileName."\" ";                     
253                                }
254                        }
255                        imap_close($this->mbox_stream);
256
257                        $nameFileZip = 'False';                 
258                        if($fileNames && !$error)
259                        {
260                                $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
261                                if($nameFileZip)
262                                {               
263                                        $file = $tempDir.'/'.$nameFileZip;
264                                } else {
265                                        $file = false;
266                                }                                                               
267                        }
268                        else
269                        {
270                                $file = false;
271                        }
272
273                        return $file;
274               
275                //exporta mensagens de diferentes pastas
276                }else{
277                        $array_names_keys = array_keys($sorted_msgs);
278                       
279                        for($i = 0; $i < count($array_names_keys); $i++){
280                                $this->folder = mb_convert_encoding($array_names_keys[$i], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP");
281                                $msg_number = explode(',', $sorted_msgs[$array_names_keys[$i]]);
282                                $tempDir = $this->tempDir;
283                                $this->connectImap();
284                               
285                                for($j = 0; $j < count($msg_number); $j++)
286                                {
287                                        $header         = $this-> getHeader($msg_number[$j]);                                                                                   
288                                        $body           = $this-> getBody($msg_number[$j]);                     
289                                        $sEMLData       = $this -> parseEml($header, $body);                   
290                                        $fileName       = $this -> CreateFileEml($sEMLData, $tempDir, $msg_number[$j]);
291
292                                        if(!$fileName)
293                                        {
294                                                $error = True;                                 
295                                                break;
296                                        } else{
297                                                $fileNames .= "\"".$fileName."\" ";                     
298                                        }
299                                }
300                                imap_close($this->mbox_stream);
301                        }
302                        $nameFileZip = 'False';                 
303                        if($fileNames && !$error)
304                        {
305                                $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
306                                if($nameFileZip)
307                                {               
308                                        $file = $tempDir.'/'.$nameFileZip;
309                                } else {
310                                        $file = false;
311                                }                                                               
312                        }
313                        else
314                        {
315                                $file = false;
316                        }
317                        return $file;
318                }
319        }else{
320                // Exportacao de mensagens arquivadas localmente
321                if($params['l_msg'] == "t")
322                {
323                // Recebe todos os subjects e bodies das mensagens locais selecionadas para exportacao
324                // e gera arrays com os conteudos separados;
325                $array_mesgs = explode('@@',$params['mesgs']);
326                $array_subjects = explode('@@',$params['subjects']);
327            $array_ids = explode(',', $params['msgs_to_export']);
328                        $tempDir = $this->tempDir;
329                       
330                        include_once("class.imap_functions.inc.php");
331                        $imapf = new imap_functions();
332
333                        // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip
334                        if (count($array_ids)==1)
335                        {
336                                $sEMLData=$imapf->treat_base64_from_post($array_mesgs[0]);
337                                $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[0],"offline");
338                                return $tempDir.'/'.$fileName;
339                        }
340
341                        // Para cada mensagem selecionada sera gerado um arquivo .eml cujo titulo sera o assunto (subject) da mesma;
342                foreach($array_ids as $i=>$id) {
343                                $sEMLData=$imapf->treat_base64_from_post($array_mesgs[$i]);
344                                $fileName=$this->CreateFileEml($sEMLData, $tempDir,'',$array_subjects[$i],$i);
345                                if(!$fileName){
346                                        $error = True;
347                                        break;
348                                } else{
349                                        $fileNames .= "\"".$fileName."\" ";
350                                }
351                        }
352                        $nameFileZip = 'False';
353                        if($fileNames && !$error) {
354                                $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
355                                if($nameFileZip){
356                                        $file = $tempDir.'/'.$nameFileZip;
357                                } else{
358                                        $file = false;
359                                }
360
361                        } else{
362                                $file = false;
363                        }
364
365            return $file;
366               
367                } else
368                // Exportacao de mensagens da caixa de entrada (imap) - processo original do Expresso
369                {
370                        $this-> folder = $params['folder'];
371                                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP");
372                        $array_ids = explode(',', $params['msgs_to_export']);
373                        $error = False;
374                        $fileNames = "";
375                        $tempDir = $this->tempDir;
376                        $this->connectImap();
377
378                        // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip
379                        if (count($array_ids)==1)
380                        {
381                                $header         = $this->getHeader($array_ids[0]);                                                                                     
382                                $body           = $this->getBody($array_ids[0]);                       
383                                $sEMLData       = $this->parseEml($header, $body);                     
384                                $fileName       = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]);
385
386                                imap_close($this->mbox_stream);
387                                if (!$fileName) {
388                                        return false;
389                                } else {
390                                        return $tempDir.'/'.$fileName;
391                                }
392                        }
393
394                        for($i = 0; $i < count($array_ids); $i++)
395                        {
396                                $header         = $this-> getHeader($array_ids[$i]);                                                                                   
397                                $body           = $this-> getBody($array_ids[$i]);                     
398                                $sEMLData       = $this -> parseEml($header, $body);                   
399                                $fileName       = $this -> CreateFileEml($sEMLData, $tempDir, $array_ids[$i]);
400
401                                if(!$fileName)
402                                {
403                                        $error = True;                                 
404                                        break;
405                                } else {
406                                        $fileNames .= "\"".$fileName."\" ";                     
407                                }
408                        }
409                        imap_close($this->mbox_stream);
410
411                        $nameFileZip = 'False';                 
412                        if($fileNames && !$error)
413                        {
414                                $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
415                                if($nameFileZip)
416                                {               
417                                        $file = $tempDir.'/'.$nameFileZip;
418                                } else {
419                                        $file = false;
420                                }                                                               
421                        }
422                        else
423                        {
424                                $file = false;
425                        }
426
427                        return $file;
428                }
429    }
430    }
431
432    function export_eml( $params ){
433
434        return $this->export_msg_data( $params['msgs_to_export'],
435                                       $params['folder'] );
436    }
437
438        function export_msg($params) {
439                $this-> folder = $params['folder'];
440                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP");
441                $array_ids = explode(',', $params['msgs_to_export']);
442                $error = False;
443                $fileNames = "";
444                $tempDir = $this->tempDir;
445                $this->connectImap();
446
447                // quando houver apenas um arquivo, exporta o .eml sem coloca-lo em zip
448                if (count($array_ids)==1)
449                {
450                        $header         = $this->getHeader($array_ids[0]);                                                                                     
451                        $body           = $this->getBody($array_ids[0]);                       
452                        $sEMLData       = $this->parseEml($header, $body);                     
453                        $fileName       = $this->CreateFileEml($sEMLData, $tempDir, $array_ids[0]."_".$_SESSION[ 'phpgw_session' ][ 'session_id' ]);
454
455                        imap_close($this->mbox_stream);
456                        if (!$fileName) {
457                                return false;
458                        } else {
459                                return $tempDir.'/'.$fileName;
460                        }
461                }
462        }
463
464    function export_msg_data($id_msg,$folder) {
465                $this->folder = $folder;
466                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
467
468                $this->connectImap();
469                $header         = $this-> getHeader($id_msg);
470                $body           = $this-> getBody($id_msg);
471
472                $msg_data = $header ."\r\n\r\n". $body;
473
474                imap_close($this->mbox_stream);
475                return $msg_data;
476        }
477
478                function export_to_archive($id_msg,$folder) {
479                $this->folder = $folder;
480                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
481                $tempDir = $this->tempDir;
482                                 
483                $this->connectImap();
484                $header         = $this-> getHeader($id_msg);
485                $body           = $this-> getBody($id_msg);
486               
487                $file = tempnam ($tempDir, 'source_#'.$id_msg);
488                $file .= '.php';
489                $fileName = basename ($file);
490                $f = fopen($file, "w");
491                fputs($f,$phpheader.$header ."\r\n\r\n". $body);
492                fclose($f);
493                $urlPath = 'tmpLclAtt/' . $fileName;
494                                 
495                imap_close($this->mbox_stream);
496                return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt";
497        }
498                                 
499        function remove_accents($string) {
500                /*
501                        $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
502                        $array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c" , "" , ""  , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" ,  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
503                        return str_replace( $array1, $array2, $string );
504                */
505                return strtr($string,
506                        "áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ",
507                        "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC");
508        }
509
510        function get_attachments_headers( $folder, $id_number ){
511
512            $this->folder = mb_convert_encoding($folder, "UTF7-IMAP","UTF-8");
513               
514            $return_attachments = array();
515               
516            include_once("class.attachment.inc.php");
517
518            $imap_attachment = new attachment();
519            $imap_attachment->setStructureFromMail( $folder, $id_number );
520            $attachments = $imap_attachment->getAttachmentsInfo();
521
522                foreach($attachments as $i => $attachment){
523
524                    $fileContent = $imap_attachment->getAttachment( $attachment['pid'] );
525                       
526                    $headers = "<?php header('Content-Type: {$attachment['type']}');
527                                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
528                                header('Pragma: public');
529                                header('Expires: 0'); // set expiration time
530                                      header('Content-Disposition: attachment; filename=\"{$attachment['name']}\"');\n
531                                      echo '$fileContent';?>";
532                       
533                    $return_attachments[ $attachment['name'] ] = array( "content" => $headers, "pid" => $attachment['pid'] );
534                        }
535
536            return( $return_attachments );
537                        }
538                       
539        function get_attachments_in_array($params) {
540                $return_attachments = array();
541
542                $attachments = $this->get_attachments_headers( $params['folder'], $params['num_msg'] );
543
544                if( !empty( $attachments ) )
545                {
546                    foreach($attachments as $fileNameReal => $attachment){
547
548                            array_push($return_attachments,array('name' => $fileNameReal, 'pid' =>$attachment['pid'], 'contentType' => $this->getFileType( $fileNameReal  ) ));
549                }
550        }
551
552                return $return_attachments;
553
554        }
555       
556        private function getFileType($nameFile) {
557                $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4));
558                $ContentType = "application/octet-stream";
559                if ($strFileType == ".asf")
560                        $ContentType = "video/x-ms-asf";
561                if ($strFileType == ".avi")
562                        $ContentType = "video/avi";
563                if ($strFileType == ".doc")
564                        $ContentType = "application/msword";
565                if ($strFileType == ".zip")
566                        $ContentType = "application/zip";
567                if ($strFileType == ".xls")
568                        $ContentType = "application/vnd.ms-excel";
569                if ($strFileType == ".gif")
570                        $ContentType = "image/gif";
571                if ($strFileType == ".png")
572                        $ContentType = "image/png";
573                if ($strFileType == ".jpg" || $strFileType == "jpeg")
574                        $ContentType = "image/jpeg";
575                if ($strFileType == ".wav")
576                        $ContentType = "audio/wav";
577                if ($strFileType == ".mp3")
578                        $ContentType = "audio/mpeg3";
579                if ($strFileType == ".mpg" || $strFileType == "mpeg")
580                        $ContentType = "video/mpeg";
581                if ($strFileType == ".rtf")
582                        $ContentType = "application/rtf";
583                if ($strFileType == ".htm" || $strFileType == "html")
584                        $ContentType = "text/html";
585                if ($strFileType == ".xml")
586                        $ContentType = "text/xml";
587                if ($strFileType == ".xsl")
588                        $ContentType = "text/xsl";
589                if ($strFileType == ".css")
590                        $ContentType = "text/css";
591                if ($strFileType == ".php")
592                        $ContentType = "text/php";
593                if ($strFileType == ".asp")
594                        $ContentType = "text/asp";
595                if ($strFileType == ".pdf")
596                        $ContentType = "application/pdf";
597                if ($strFileType == ".txt")
598                        $ContentType = "text/plain";
599                if ($strFileType == ".log")
600                        $ContentType = "text/plain";
601                if ($strFileType == ".wmv")
602                        $ContentType = "video/x-ms-wmv";
603                if ($strFileType == ".sxc")
604                        $ContentType = "application/vnd.sun.xml.calc";
605                if ($strFileType == ".odt")
606                        $ContentType = "application/vnd.oasis.opendocument.text";
607                if ($strFileType == ".stc")
608                        $ContentType = "application/vnd.sun.xml.calc.template";
609                if ($strFileType == ".sxd")
610                        $ContentType = "application/vnd.sun.xml.draw";
611                if ($strFileType == ".std")
612                        $ContentType = "application/vnd.sun.xml.draw.template";
613                if ($strFileType == ".sxi")
614                        $ContentType = "application/vnd.sun.xml.impress";
615                if ($strFileType == ".sti")
616                        $ContentType = "application/vnd.sun.xml.impress.template";
617                if ($strFileType == ".sxm")
618                        $ContentType = "application/vnd.sun.xml.math";
619                if ($strFileType == ".sxw")
620                        $ContentType = "application/vnd.sun.xml.writer";
621                if ($strFileType == ".sxq")
622                        $ContentType = "application/vnd.sun.xml.writer.global";
623                if ($strFileType == ".stw")
624                        $ContentType = "application/vnd.sun.xml.writer.template";
625                if ($strFileType == ".ps")
626                        $ContentType = "application/postscript";
627                if ($strFileType == ".pps")
628                        $ContentType = "application/vnd.ms-powerpoint";
629                if ($strFileType == ".odt")
630                        $ContentType = "application/vnd.oasis.opendocument.text";
631                if ($strFileType == ".ott")
632                        $ContentType = "application/vnd.oasis.opendocument.text-template";
633                if ($strFileType == ".oth")
634                        $ContentType = "application/vnd.oasis.opendocument.text-web";
635                if ($strFileType == ".odm")
636                        $ContentType = "application/vnd.oasis.opendocument.text-master";
637                if ($strFileType == ".odg")
638                        $ContentType = "application/vnd.oasis.opendocument.graphics";
639                if ($strFileType == ".otg")
640                        $ContentType = "application/vnd.oasis.opendocument.graphics-template";
641                if ($strFileType == ".odp")
642                        $ContentType = "application/vnd.oasis.opendocument.presentation";
643                if ($strFileType == ".otp")
644                        $ContentType = "application/vnd.oasis.opendocument.presentation-template";
645                if ($strFileType == ".ods")
646                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
647                if ($strFileType == ".ots")
648                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
649                if ($strFileType == ".odc")
650                        $ContentType = "application/vnd.oasis.opendocument.chart";
651                if ($strFileType == ".odf")
652                        $ContentType = "application/vnd.oasis.opendocument.formula";
653                if ($strFileType == ".odi")
654                        $ContentType = "application/vnd.oasis.opendocument.image";
655                if ($strFileType == ".ndl")
656                        $ContentType = "application/vnd.lotus-notes";
657                if ($strFileType == ".eml")
658                        $ContentType = "text/plain";
659                if ($strFileType == ".png")
660                        $ContentType = "image/png";
661                return $ContentType;
662        }
663       
664        function download_all_attachments($params) {
665               
666                require_once dirname(__FILE__).'/class.attachment.inc.php';
667                $atObj = new attachment();
668                $atObj->setStructureFromMail($params['folder'],$params['num_msg']);
669                $attachments = $atObj->getAttachmentsInfo();
670                $id_number = $params['num_msg'];               
671                $tempDir = $this->tempDir;
672                $tempSubDir = $_SESSION['phpgw_session']['session_id'];
673                $fileNames = '';
674                exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
675                $this-> folder = $params['folder'];
676                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
677               
678                $fileNames = Array();
679                       
680                for ($i = 0; $i < count($attachments); $i++)
681                {
682                   $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']);
683                   $fileNames[$i] = $attachments[$i]['name'];
684                }
685
686                for ($i = 0; $i < count($attachments); $i++)
687                {
688                        $fileName = $attachments[$i]['name'];
689                        $result = array_keys($fileNames, $fileName);
690
691                        // Detecta duplicatas
692                        if (count($result) > 1)
693                        {
694                            for ($j = 1; $j < count($result); $j++)
695                            {
696                                $replacement = '('.$j.')$0';
697                                if (preg_match('/\.\w{2,4}$/', $fileName))
698                                {
699                                    $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName);
700                                }
701                                else
702                                {
703                                    $fileNames[$result[$j]] .= "($j)";
704                                }
705                                $attachments[$result[$j]]['name'] = $fileNames[$result[$j]];
706                            }
707                        }
708                        // Fim detecta duplicatas
709
710                        $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
711                        if(!$f)
712                                return False;                   
713                        $fileContent = $atObj->getAttachment( $attachments[$i]['pid'] );       
714                                fputs($f,$fileContent);
715                               
716                        fclose($f);
717               
718                }
719                imap_close($this->mbox_stream);
720                $nameFileZip = '';
721               
722                if(!empty($fileNames)) {
723                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir);                                         
724                        if($nameFileZip)
725                                $file =  $tempDir . '/'.$tempSubDir.'/'.$nameFileZip;
726                        else {
727                                $file = false;
728                        }
729                }
730                else
731                        $file = false; 
732                return $file;
733        }
734
735        function getHeader($msg_number){                       
736                return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID);
737        }
738       
739        function getBody($msg_number){
740                $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255);
741                $body = imap_body($this->mbox_stream, $msg_number, FT_UID);
742                if(($header->Unseen == 'U') || ($header->Recent == 'N')){
743                        imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID);
744                }
745                return $body;
746        }
747
748        function decode_subject($string){
749                if ((strpos(strtolower($string), '=?iso-8859-1') !== false)
750                        || (strpos(strtolower($string), '=?windows-1252') !== false)){
751                        $elements = imap_mime_header_decode($string);
752                        foreach ($elements as $el)
753                                $return .= $el->text;
754                }
755                else if (strpos(strtolower($string), '=?utf-8') !== false) {
756                        $elements = imap_mime_header_decode($string);
757                        foreach ($elements as $el){
758                                $charset = $el->charset;
759                                $text    = $el->text;
760                                if(!strcasecmp($charset, "utf-8") ||
761                                !strcasecmp($charset, "utf-7")) {
762                                $text = iconv($charset, "ISO-8859-1", $text);
763                        }
764                        $return .= $text;
765                        }
766                }
767                else
768                        $return = $string;
769
770                return $this->remove_accents($return);         
771        }
772}
773// END CLASS
774?>
Note: See TracBrowser for help on using the repository browser.