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

Revision 2, 6.3 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • 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       
18        function connectImap(){
19       
20                $username = $_SESSION['phpgw_info']['expressomail']['user']['userid'];
21                $password = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
22                $imap_server = $_SESSION['phpgw_info']['expressomail']['email_server']['imapServer'];
23                $imap_port      = $_SESSION['phpgw_info']['expressomail']['email_server']['imapPort'];
24               
25                if ($_SESSION['phpgw_info']['expressomail']['email_server']['imapTLSEncryption'] == 'yes')
26                {
27                        $imap_options = '/tls/novalidate-cert';
28                }
29                else
30                {
31                        $imap_options = '/notls/novalidate-cert';
32                }
33                $this->mbox_stream = imap_open("{".$imap_server.":".$imap_port.$imap_options."}".$this->folder, $username, $password);
34        }
35       
36        //export message to EML Format
37        function parseEml($header, $body)       
38        {               
39                $sEmailHeader = $header;
40                $sEmailBody = $body;
41                $sEMail = $sEmailHeader . "\r\n\r\n" . $sEmailBody;             
42                return $sEMail;
43        }
44       
45        // create EML File.
46        function createFileEml($sEMLData, $tempDir)
47        {
48                $file = "email_".md5(microtime()).".eml";
49                $f = fopen($tempDir.'/'.$file,"w");
50               
51                if(!$f)
52                        return False;
53               
54                fputs($f,$sEMLData);
55                fclose($f);
56
57                return $file;   
58        }
59       
60        function createFileZip($files, $tempDir){
61               
62                $tmp_zip_filename =     "email_".md5(microtime()).".zip";
63                $command = "cd " . $tempDir . "; nice zip -m " . $tmp_zip_filename . " " . $files;                     
64                if(!exec($command)) {
65                        $command = 'cd ' . $tempDir . '; rm '.$files;
66                        exec($command);
67                        return null;
68                }
69               
70                return $tmp_zip_filename;
71                               
72        }
73
74        function export_all($params){
75               
76                $this->folder = $params['folder'];
77                $fileNames = "";
78                $tempDir = ini_get("session.save_path");
79                $this->connectImap();
80               
81                $msgs = imap_search($this->mbox_stream,"ALL",SE_UID);
82                foreach($msgs as $nMsgs){
83                        $header         = $this-> getHeader($nMsgs);                                                                   
84                        $body           = $this-> getBody($nMsgs);                     
85                        $sEMLData       = $this -> parseEml($header, $body);
86                        $fileName       = $this -> CreateFileEml($sEMLData, $tempDir);
87                        if(!$fileName)  {
88                                $error = True;                                 
89                                break;
90                        }
91                        else
92                                $fileNames .= $fileName.' ';                   
93                       
94                }
95                imap_close($this->mbox_stream);
96               
97                $nameFileZip = 'False';                 
98                if($fileNames && !$error) {                     
99                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
100                        if($nameFileZip)                       
101                                $file = $tempDir.'/'.$nameFileZip;
102                        else {
103                                $file = false;
104                        }                                                               
105                }
106                else
107                        $file = false;
108                       
109                return $file;
110               
111        }
112
113        function makeAll($params) {
114               
115                $this-> folder = $params['folder'];
116                $array_ids = explode(',', $params['msgs_to_export']);   
117                $error = False;         
118                $fileNames = "";
119                $tempDir = ini_get("session.save_path");
120                $this->connectImap();
121                               
122                for($i = 0; $i < count($array_ids); $i++) {
123                               
124                        $header         = $this-> getHeader($array_ids[$i]);                                                                   
125                        $body           = $this-> getBody($array_ids[$i]);                     
126                        $sEMLData       = $this -> parseEml($header, $body);
127                        $fileName       = $this -> CreateFileEml($sEMLData, $tempDir);
128                       
129                        if(!$fileName)  {
130                                $error = True;                                 
131                                break;
132                        }
133                        else
134                                $fileNames .= $fileName.' ';                   
135                       
136                }
137                imap_close($this->mbox_stream);
138               
139               
140                $nameFileZip = 'False';                 
141                if($fileNames && !$error) {                     
142                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir);
143                        if($nameFileZip)                       
144                                $file = $tempDir.'/'.$nameFileZip;
145                        else {
146                                $file = false;
147                        }                                                               
148                }
149                else
150                        $file = false;
151                       
152                return $file;
153        }
154
155        function export_msg($params) {
156                $this-> folder = $params['folder'];
157                $id_number = $params['msgs_to_export'];
158                $tempDir = ini_get("session.save_path");
159               
160                $this->connectImap();
161                $header         = $this-> getHeader($id_number);
162                $body           = $this-> getBody($id_number);
163               
164                $file = "source_".md5(microtime()).".txt";
165                $f = fopen($tempDir.'/'.$file,"w");
166                fputs($f,$header ."\r\n\r\n". $body);
167                fclose($f);
168               
169                imap_close($this->mbox_stream);
170                return $tempDir.'/'.$file;
171        }
172
173        function remove_accents($string) {
174                        return strtr($string,
175                        "?Ó??ó?Ý?úÁÀÃÂÄÇÉÈÊËÍÌ?ÎÏÑÕÔÓÒÖÚÙ?ÛÜ?áàãâäçéèêëíì?îïñóòõôöúù?ûüýÿ'\"",
176                        "SOZsozYYuAAAAACEEEEIIIIINOOOOOUUUUUsaaaaaceeeeiiiiinooooouuuuuyy__");
177        }
178       
179        function download_all_attachments($params) {
180               
181                $id_number = $params['num_msg'];               
182                $attachments =unserialize(rawurldecode($params['s_attachments']));
183               
184                $tempDir = ini_get("session.save_path");
185                $tempSubDir = md5(microtime());
186                $fileNames = '';
187                exec('mkdir ' . $tempDir . '/'.$tempSubDir.'; cd ' . $tempDir . '/'.$tempSubDir);
188                $this-> folder = $params['folder'];
189                $this->connectImap();
190                include("class.imap_attachment.inc.php");
191                $imap_attachment = new imap_attachment();
192                $attachments = $imap_attachment->download_attachment($this->mbox_stream, $id_number);
193               
194                foreach($attachments as $i => $attachment){
195                        if($i && $i == 'names')
196                                continue;
197                                                                                                                                               
198                       
199                        $fileName = $this->remove_accents($attachment['name']);
200                        $f = fopen($tempDir . '/'.$tempSubDir.'/'.$fileName,"wb");
201                        if(!$f)
202                                return False;                   
203                                               
204                        $fileNames .= "'".$fileName."' ";
205                        $fileContent = imap_fetchbody($this->mbox_stream, $id_number,$attachment['pid'], FT_UID);
206                        if($attachment['encoding'] == 'base64')
207                                fputs($f,imap_base64($fileContent));
208                        else           
209                                fputs($f,$fileContent);
210                               
211                        fclose($f);
212               
213                }
214                imap_close($this->mbox_stream);
215                $nameFileZip = '';
216               
217                if($fileNames) {
218                        $nameFileZip = $this -> createFileZip($fileNames, $tempDir . '/'.$tempSubDir);                                         
219                        if($nameFileZip)
220                                $file =  $tempDir . '/'.$tempSubDir.'/'.$nameFileZip;
221                        else {
222                                $file = false;
223                        }
224                }
225                else
226                        $file = false;
227                               
228                return $file;
229        }
230
231        function getHeader($msg_number){                       
232                return imap_fetchheader($this->mbox_stream, $msg_number, FT_UID);
233        }
234       
235        function getBody($msg_number){
236                return imap_body($this->mbox_stream, $msg_number, FT_UID);
237        }
238
239}
240// END CLASS
241?>
Note: See TracBrowser for help on using the repository browser.