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

Revision 5172, 25.9 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Ultimas sincronizacoes...

  • 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                $tempDir = $this->tempDir;
440                $file = "source_".$_SESSION[ 'phpgw_session' ][ 'session_id' ].".php";
441                $f = fopen($tempDir.'/'.$file,"w");
442                fputs($f, $this->export_msg_data( $params['msgs_to_export'], $params['folder'] ) );
443                fclose($f);
444               
445                return $tempDir.'/'.$file;
446        }
447
448    function export_msg_data($id_msg,$folder) {
449                $this->folder = $folder;
450                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
451
452                $this->connectImap();
453                $header         = $this-> getHeader($id_msg);
454                $body           = $this-> getBody($id_msg);
455
456                $msg_data = $header ."\r\n\r\n". $body;
457
458                imap_close($this->mbox_stream);
459                return $msg_data;
460        }
461
462                function export_to_archive($id_msg,$folder) {
463                $this->folder = $folder;
464                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","ISO_8859-1");
465                $tempDir = $this->tempDir;
466                                 
467                $this->connectImap();
468                $header         = $this-> getHeader($id_msg);
469                $body           = $this-> getBody($id_msg);
470               
471                $file = tempnam ($tempDir, 'source_#'.$id_msg);
472                $file .= '.php';
473                $fileName = basename ($file);
474                $f = fopen($file, "w");
475                fputs($f,$phpheader.$header ."\r\n\r\n". $body);
476                fclose($f);
477                $urlPath = 'tmpLclAtt/' . $fileName;
478                                 
479                imap_close($this->mbox_stream);
480                return "inc/gotodownload.php?idx_file=".$tempDir . '/'.$file."&newfilename=fonte_da_mensagem.txt";
481        }
482                                 
483        function remove_accents($string) {
484                /*
485                        $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç" , "?", "\"", "!", "@", "#", "$", "%", "š", "&", "*", "(", ")", "-", "=", "+", "Ž", "`", "[", "]", "{", "}", "~", "^", ",", "<", ">", ";", ":", "/", "?", "\\", "|", "¹", "²", "³", "£", "¢", "¬", "§", "ª", "º", "°", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
486                        $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");
487                        return str_replace( $array1, $array2, $string );
488                */
489                return strtr($string,
490                        "áàâãäéèêëíìîïóòôõöúùûüç?\"'!@#$%š&*()-=+Ž`[]{}~^,<>;:/?\\|¹²³£¢¬§ªº°ÁÀÂÃÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÇ",
491                        "aaaaaeeeeiiiiooooouuuuc___________________________________________AAAAAEEEEIIIIOOOOOUUUUC");
492        }
493
494        function get_attachments_headers( $folder, $id_number ){
495
496            $this->folder = mb_convert_encoding($folder, "UTF7-IMAP","UTF-8");
497               
498            $return_attachments = array();
499               
500            include_once("class.attachment.inc.php");
501
502            $imap_attachment = new attachment();
503            $imap_attachment->setStructureFromMail( $folder, $id_number );
504            $attachments = $imap_attachment->getAttachmentsInfo();
505
506                foreach($attachments as $i => $attachment){
507
508                    $fileContent = $imap_attachment->getAttachment( $attachment['pid'] );
509                       
510                    $headers = "<?php header('Content-Type: {$attachment['type']}');
511                                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
512                                header('Pragma: public');
513                                header('Expires: 0'); // set expiration time
514                                      header('Content-Disposition: attachment; filename=\"{$attachment['name']}\"');\n
515                                      echo '$fileContent';?>";
516                       
517                    $return_attachments[ $attachment['name'] ] = array( "content" => $headers, "pid" => $attachment['pid'] );
518                        }
519
520            return( $return_attachments );
521                        }
522                       
523        function get_attachments_in_array($params) {
524                $return_attachments = array();
525
526                $attachments = $this->get_attachments_headers( $params['folder'], $params['num_msg'] );
527
528                if( !empty( $attachments ) )
529                {
530                    foreach($attachments as $fileNameReal => $attachment){
531
532                            array_push($return_attachments,array('name' => $fileNameReal, 'pid' =>$attachment['pid'], 'contentType' => $this->getFileType( $fileNameReal  ) ));
533                }
534        }
535
536                return $return_attachments;
537
538        }
539       
540        private function getFileType($nameFile) {
541                $strFileType = strrev(substr(strrev(strtolower($nameFile)),0,4));
542                $ContentType = "application/octet-stream";
543                if ($strFileType == ".asf")
544                        $ContentType = "video/x-ms-asf";
545                if ($strFileType == ".avi")
546                        $ContentType = "video/avi";
547                if ($strFileType == ".doc")
548                        $ContentType = "application/msword";
549                if ($strFileType == ".zip")
550                        $ContentType = "application/zip";
551                if ($strFileType == ".xls")
552                        $ContentType = "application/vnd.ms-excel";
553                if ($strFileType == ".gif")
554                        $ContentType = "image/gif";
555                if ($strFileType == ".png")
556                        $ContentType = "image/png";
557                if ($strFileType == ".jpg" || $strFileType == "jpeg")
558                        $ContentType = "image/jpeg";
559                if ($strFileType == ".wav")
560                        $ContentType = "audio/wav";
561                if ($strFileType == ".mp3")
562                        $ContentType = "audio/mpeg3";
563                if ($strFileType == ".mpg" || $strFileType == "mpeg")
564                        $ContentType = "video/mpeg";
565                if ($strFileType == ".rtf")
566                        $ContentType = "application/rtf";
567                if ($strFileType == ".htm" || $strFileType == "html")
568                        $ContentType = "text/html";
569                if ($strFileType == ".xml")
570                        $ContentType = "text/xml";
571                if ($strFileType == ".xsl")
572                        $ContentType = "text/xsl";
573                if ($strFileType == ".css")
574                        $ContentType = "text/css";
575                if ($strFileType == ".php")
576                        $ContentType = "text/php";
577                if ($strFileType == ".asp")
578                        $ContentType = "text/asp";
579                if ($strFileType == ".pdf")
580                        $ContentType = "application/pdf";
581                if ($strFileType == ".txt")
582                        $ContentType = "text/plain";
583                if ($strFileType == ".log")
584                        $ContentType = "text/plain";
585                if ($strFileType == ".wmv")
586                        $ContentType = "video/x-ms-wmv";
587                if ($strFileType == ".sxc")
588                        $ContentType = "application/vnd.sun.xml.calc";
589                if ($strFileType == ".odt")
590                        $ContentType = "application/vnd.oasis.opendocument.text";
591                if ($strFileType == ".stc")
592                        $ContentType = "application/vnd.sun.xml.calc.template";
593                if ($strFileType == ".sxd")
594                        $ContentType = "application/vnd.sun.xml.draw";
595                if ($strFileType == ".std")
596                        $ContentType = "application/vnd.sun.xml.draw.template";
597                if ($strFileType == ".sxi")
598                        $ContentType = "application/vnd.sun.xml.impress";
599                if ($strFileType == ".sti")
600                        $ContentType = "application/vnd.sun.xml.impress.template";
601                if ($strFileType == ".sxm")
602                        $ContentType = "application/vnd.sun.xml.math";
603                if ($strFileType == ".sxw")
604                        $ContentType = "application/vnd.sun.xml.writer";
605                if ($strFileType == ".sxq")
606                        $ContentType = "application/vnd.sun.xml.writer.global";
607                if ($strFileType == ".stw")
608                        $ContentType = "application/vnd.sun.xml.writer.template";
609                if ($strFileType == ".ps")
610                        $ContentType = "application/postscript";
611                if ($strFileType == ".pps")
612                        $ContentType = "application/vnd.ms-powerpoint";
613                if ($strFileType == ".odt")
614                        $ContentType = "application/vnd.oasis.opendocument.text";
615                if ($strFileType == ".ott")
616                        $ContentType = "application/vnd.oasis.opendocument.text-template";
617                if ($strFileType == ".oth")
618                        $ContentType = "application/vnd.oasis.opendocument.text-web";
619                if ($strFileType == ".odm")
620                        $ContentType = "application/vnd.oasis.opendocument.text-master";
621                if ($strFileType == ".odg")
622                        $ContentType = "application/vnd.oasis.opendocument.graphics";
623                if ($strFileType == ".otg")
624                        $ContentType = "application/vnd.oasis.opendocument.graphics-template";
625                if ($strFileType == ".odp")
626                        $ContentType = "application/vnd.oasis.opendocument.presentation";
627                if ($strFileType == ".otp")
628                        $ContentType = "application/vnd.oasis.opendocument.presentation-template";
629                if ($strFileType == ".ods")
630                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet";
631                if ($strFileType == ".ots")
632                        $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template";
633                if ($strFileType == ".odc")
634                        $ContentType = "application/vnd.oasis.opendocument.chart";
635                if ($strFileType == ".odf")
636                        $ContentType = "application/vnd.oasis.opendocument.formula";
637                if ($strFileType == ".odi")
638                        $ContentType = "application/vnd.oasis.opendocument.image";
639                if ($strFileType == ".ndl")
640                        $ContentType = "application/vnd.lotus-notes";
641                if ($strFileType == ".eml")
642                        $ContentType = "text/plain";
643                if ($strFileType == ".png")
644                        $ContentType = "image/png";
645                return $ContentType;
646        }
647       
648        function download_all_attachments($params) {
649               
650                require_once $_SESSION['rootPath'].'/expressoMail1_2/inc/class.attachment.inc.php';
651                $atObj = new attachment();
652                $atObj->setStructureFromMail($params['folder'],$params['num_msg']);
653                $attachments = $atObj->getAttachmentsInfo();
654                $id_number = $params['num_msg'];               
655                $tempDir = $this->tempDir;
656                $tempSubDir = $_SESSION['phpgw_session']['session_id'];
657                $fileNames = '';
658                exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
659                $this-> folder = $params['folder'];
660                $this->folder = mb_convert_encoding($this->folder, "UTF7-IMAP","UTF-8");
661               
662                $fileNames = Array();
663                       
664                for ($i = 0; $i < count($attachments); $i++)
665                {
666                   $attachments[$i]['name'] = $this->remove_accents($attachments[$i]['name']);
667                   $fileNames[$i] = $attachments[$i]['name'];
668                }
669
670                for ($i = 0; $i < count($attachments); $i++)
671                {
672                        $fileName = $attachments[$i]['name'];
673                        $result = array_keys($fileNames, $fileName);
674
675                        // Detecta duplicatas
676                        if (count($result) > 1)
677                        {
678                            for ($j = 1; $j < count($result); $j++)
679                            {
680                                $replacement = '('.$j.')$0';
681                                if (preg_match('/\.\w{2,4}$/', $fileName))
682                                {
683                                    $fileNames[$result[$j]] = preg_replace('/\.\w{2,4}$/', $replacement, $fileName);
684                                }
685                                else
686                                {
687                                    $fileNames[$result[$j]] .= "($j)";
688                                }
689                                $attachments[$result[$j]]['name'] = $fileNames[$result[$j]];
690                            }
691                        }
692                        // Fim detecta duplicatas
693
694                        $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
695                        if(!$f)
696                                return False;                   
697                        $fileContent = $atObj->getAttachment( $attachments[$i]['pid'] );       
698                                fputs($f,$fileContent);
699                               
700                        fclose($f);
701               
702                }
703                imap_close($this->mbox_stream);
704                $nameFileZip = '';
705               
706                if(!empty($fileNames)) {
707                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir);                                         
708                        if($nameFileZip)
709                                $file =  $tempDir . '/'.$tempSubDir.'/'.$nameFileZip;
710                        else {
711                                $file = false;
712                        }
713                }
714                else
715                        $file = false; 
716                return $file;
717        }
718
719        function getHeader($msg_number){                       
720                return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID);
721        }
722       
723        function getBody($msg_number){
724                $header = imap_headerinfo($this->mbox_stream, imap_msgno($this->mbox_stream, $msg_number), 80, 255);
725                $body = imap_body($this->mbox_stream, $msg_number, FT_UID);
726                if(($header->Unseen == 'U') || ($header->Recent == 'N')){
727                        imap_clearflag_full($this->mbox_stream, $msg_number, "\\Seen", ST_UID);
728                }
729                return $body;
730        }
731
732        function decode_subject($string){
733                if ((strpos(strtolower($string), '=?iso-8859-1') !== false)
734                        || (strpos(strtolower($string), '=?windows-1252') !== false)){
735                        $elements = imap_mime_header_decode($string);
736                        foreach ($elements as $el)
737                                $return .= $el->text;
738                }
739                else if (strpos(strtolower($string), '=?utf-8') !== false) {
740                        $elements = imap_mime_header_decode($string);
741                        foreach ($elements as $el){
742                                $charset = $el->charset;
743                                $text    = $el->text;
744                                if(!strcasecmp($charset, "utf-8") ||
745                                !strcasecmp($charset, "utf-7")) {
746                                $text = iconv($charset, "ISO-8859-1", $text);
747                        }
748                        $return .= $text;
749                        }
750                }
751                else
752                        $return = $string;
753
754                return $this->remove_accents($return);         
755        }
756}
757// END CLASS
758?>
Note: See TracBrowser for help on using the repository browser.