source: trunk/expressoMail1_2/inc/class.SieveS.inc.php @ 1040

Revision 1040, 8.6 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Atualização de download de arquivos e sessão

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2if(!isset($GLOBALS['phpgw_info'])){
3        $GLOBALS['phpgw_info']['flags'] = array(
4                'currentapp' => 'expressoMail1_2',
5                'nonavbar'   => true,
6                'noheader'   => true
7        );
8}
9require_once '../header.inc.php';
10
11
12//Conecta com o Servidor e o serviço Sieve;
13class SieveS{
14               
15        var $host;
16        var $port;
17        var $user;
18        var $pass;
19        var $proxy;
20       
21        var $implementation;
22        var $saslmethods;
23        var $extensions;
24        var $starttls_avail;
25        var $socket;
26        var $socket_timeout;
27       
28        var $scriptlist;
29        var $activescript;
30        var $errstr;
31        var $errnum;
32        var $ScriptS;
33       
34        function SieveS(){
35               
36                $this->host = $_SESSION['phpgw_info']['expressomail']['email_server']['imapSieveServer'];
37                $this->port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapSievePort'];
38                $this->user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];   
39                $this->pass = $_SESSION['phpgw_info']['expressomail']['user']['passwd'];
40                $this->proxy = '';
41               
42                $this->socket_timeout = 5;
43                $this->implementation = array('unknown');
44                $this->saslmethods        = array('unknown');
45                $this->extensions         = array('unknown');
46                $this->starttls_avail = false;
47                $this->scriptlist     = array();
48                $this->activescript   = '';
49                $this->errstr             = '';
50                $this->errnum             = '';
51               
52        }       
53       
54        function start(){
55                // Cria a conexao;
56                if(!isset($this->socket)){
57                        $this->socket = fsockopen($this->host, $this->port, $this->errnum, $this->errstr, "60");
58
59                }
60                // Verifica a conexao;
61                if(!$this->socket){
62                        return "não conectado";
63                }
64               
65                $said = $this->read();
66                if (!preg_match("/timsieved/i",$said)) {
67                    $this->close();
68                    $this->errstr = "start: bad response from $this->host: $said";
69                    return false;
70                }
71               
72                if (preg_match("/IMPLEMENTATION/",$said)){
73                  while (!preg_match("/^OK/",$said)) {
74                    if (preg_match("/^\"IMPLEMENTATION\" +\"(.*)\"/",$said,$bits)){
75                                $this->implementation = $bits[1];
76                    }
77                    elseif (preg_match("/^\"SASL\" +\"(.*)\"/",$said,$bits)) {
78                                $auth_types = $bits[1];
79                                $this->saslmethods = split(" ", $auth_types);
80                    }
81                    elseif (preg_match("/^\"SIEVE\" +\"(.*)\"/",$said,$bits)) {
82                                $extensions = $bits[1];
83                                $this->extensions = split(" ", $extensions);
84                    }
85                elseif (preg_match("/^\"STARTTLS\"/",$said)){
86                   $this->starttls_avail = true;
87                }
88                    $said = $this->read();
89                  }
90                }
91                else
92                {
93                    // assume cyrus v1.
94                    if (preg_match("/\"(.+)\" +\"(.+)\"/",$said,$bits)) {
95                                $this->implementation = $bits[1];
96                                $sasl_str = $bits[2];  // should look like: SASL={PLAIN,...}
97                    }
98                        if (preg_match("/SASL=\{(.+)\}/",$sasl_str,$morebits)) {
99                            $auth_types = $morebits[1];
100                            $this->saslmethods = split(", ", $auth_types);
101                        }else {
102                                // a bit desperate if we get here.
103                                $this->implementation = $said;
104                                $this->saslmethods = $said;
105                    }
106                }
107               
108                $authstr = $this->proxy . "\x00" . $this->user . "\x00" . $this->pass;
109                $encoded = base64_encode($authstr);             
110                $len = strlen($encoded);
111
112                //fputs($this->socket,"AUTHENTICATE \"PLAIN\" \{$len+}\r\n");
113                //fputs($this->socket,"$encoded\r\n");
114
115                fwrite($this->socket, 'AUTHENTICATE "PLAIN" {' . $len . '+}' . "\r\n");
116                fwrite($this->socket,"$encoded\r\n");
117               
118                $said = $this->read();
119       
120                if (preg_match("/NO/",$said)) {
121                    $this->close();
122                    $this->errstr = "start: authentication failure connecting to $this->host";
123                    return false;
124                }
125                elseif (!preg_match("/OK/",$said)) {
126                    $this->close();
127                    $this->errstr = "start: bad authentication response from $this->host: $said";
128                    return false;
129                }
130       
131                return true;
132               
133        }
134       
135        function close(){
136       
137                if(!$this->socket){
138                        return true;   
139                }       
140                fputs($this->socket,"LOGOUT\r\n");
141                $rc = fclose($this->socket);
142                if($rc != 1){
143                        $this->errstr = "close: failed closing socket to $this->server";
144                        return false;
145                }
146                return true;
147        }
148       
149        function read(){
150       
151                $buffer = '';
152               
153                // Verifca a conexao;
154                if(!$this->socket){
155                        return $buffer;
156                }
157               
158                //Funções do php
159                socket_set_timeout($this->socket,$this->socket_timeout);
160                socket_set_blocking($this->socket,true);
161               
162                //Lê um caracter de cada vez e o adiciona na variavel buffer;
163                while(!feof($this->socket)){
164                        $char = fread($this->socket,1);
165                       
166                        $status = socket_get_status($this->socket);
167                        if($status['timed_out'])
168                                return $buffer;
169                       
170                        if(($char == "\n") || ($char == "\r")){
171                                if($char == "\r")
172                                        fread($this->socket,1);
173                                return $buffer;
174                        }
175                        $buffer .= $char;
176                }
177                return $buffer;
178        }
179/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180// Manipulação dos scripts
181/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182               
183        function listscripts(){
184               
185                $bits = '';
186
187                //Verifica a conexao
188                if(!$this->socket){
189                        $this->errstr = "listscripts: sem conexão para o servidor $this->host";
190                        return false;
191                }
192
193                $scripts = array();
194                fputs($this->socket,"LISTSCRIPTS\r\n");
195       
196                $said = $this->read();
197                while (!preg_match("/^OK/",$said) && !preg_match("/^NO/",$said)) {
198       
199                    // Cyrus v1 script lines look like '"script*"' with the
200                    // asterisk denoting the active script. Cyrus v2 script
201                    // lines will look like '"script" ACTIVE' if active.
202       
203                    if (preg_match("/^\"(.+)\"\s*(.+)*$/m",$said,$bits)) {
204                        if (preg_match("/\*$/",$bits[1])){
205                            $bits[1] = preg_replace("/\*$/","",$bits[1]);
206                            $this->activescript = $bits[1];
207                        }
208                        if (isset($bits[2]) && $bits[2] == 'ACTIVE')
209                            $this->activescript = $bits[1];
210                        array_push($scripts,$bits[1]);
211                    }
212                    $said = $this->read();
213                }
214       
215                if (preg_match("/^OK/",$said)) {
216                    $this->scriptlist = $scripts;
217            return $this->scriptlist;
218        }
219               
220        }
221       
222        // Pega o conteudo do script no servidor       
223        function getscript(){
224
225                $scriptfile = $this->listscripts();
226               
227                // verifica se existe o script;
228                if($scriptfile == ""){
229                        return "Falta o script";
230                }
231               
232                if(!$this->socket){
233                        return "Falha na conexao";     
234                }
235               
236                $script = '';
237               
238                fputs($this->socket,"GETSCRIPT \"$scriptfile[0]\"\r\n");
239                $said = $this->read();
240                while ((!preg_match("/^OK/",$said)) && (!preg_match("/^NO/",$said))) {
241                    // replace newlines which read() removed
242                    if (!preg_match("/\n$/",$said)) $said .= "\n";
243                    $script .= $said;
244                    $said = $this->read();
245                }
246               
247                if($said == "OK"){
248                        return $script;
249                }else{
250                        return false;
251                }
252        }
253
254        //envia para o servidor o nome do script($scriptfile) e seu conteudo($script)
255        function putscript ($scriptfile,$script) {
256                if (!isset($scriptfile)) {
257                    $this->errstr = "Não foi possível enviar o script para o servidor";
258                    return false;
259            }
260                if (!isset($script)) {
261                    $this->errstr = "Não foi possível enviar o script para o servidor";
262                    return false;
263            }
264                if (!$this->socket) {
265                    $this->errstr = "Sem conexão com o servidor $this->server";
266                    return false;
267            }
268       
269                $len = strlen($script);
270
271                //fputs($this->socket,"PUTSCRIPT \"$scriptfile\" \{$len+}\r\n");
272                //fputs($this->socket,"$script\r\n");
273       
274                fwrite($this->socket, 'PUTSCRIPT "'.$scriptfile.'" {' . $len . '+}' . "\r\n"); 
275                fwrite($this->socket,"$script\r\n");
276       
277                $said = '';
278                while ($said == '') {
279                    $said = $this->read();
280                }
281         
282            if (preg_match("/^OK/",$said)) {
283                    return true;
284                }
285       
286            $this->errstr = "Não foi possível enviar o $scriptfile: $said";
287            return false;
288    }
289   
290    // Ativa o script para o servico sieve;
291    function activatescript ($scriptfile) {
292                if (!isset($scriptfile)) {
293                    $this->errstr = "activatescript: no script file specified";
294                    return false;
295            }
296       
297            if (!$this->socket) {
298                    $this->errstr = "activatescript: no connection open to $this->server";
299                    return false;
300            }
301       
302                fputs($this->socket,"SETACTIVE \"$scriptfile\"\r\n");
303       
304                $said = $this->read();
305       
306                if (preg_match("/^OK/",$said)) {
307                    return true;
308            }
309       
310                $this->errstr = "activatescript: could not activate script $scriptfile: $said";
311            return false;
312    }
313
314    // Deleta o script do serviço sieve;
315    function deletescript ($scriptName) {
316        if(!isset($scriptName)){
317                $this->errstr = "deletescript: no script file specified";
318                return false;
319        }
320       
321        // Verifica a conexão;
322        if(!$this->socket){
323                $this->errstr = "deletescript : no connection open to $this->server";
324                return false;
325        }
326   
327        fputs($this->socket,"DELETESCRIPT \"$scriptName\"\r\n");
328       
329        $said = $this->read();
330       
331        if(preg_match("/^OK/",$said)) {
332                return true;   
333        }
334       
335        $this->errstr = "deletescript: could not delete script $scriptName: $said";
336        return false;
337    }
338}
339?>
Note: See TracBrowser for help on using the repository browser.