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

Revision 5509, 9.9 KB checked in by gustavo, 12 years ago (diff)

Ticket #2488 - Adicionar cabecalho de licenca em arquivos que nao o possuem

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2                /***************************************************************************
3                * Expresso Livre                                                           *
4                * http://www.expressolivre.org                                             *
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               
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 ( is_resource( $this -> socket ) && ( ! feof( $this -> socket ) ) )
164                {
165                        $char = fread($this->socket,1);
166                       
167                        $status = socket_get_status($this->socket);
168                        if($status['timed_out'])
169                                return $buffer;
170                       
171                        if(($char == "\n") || ($char == "\r")){
172                                if($char == "\r")
173                                        fread($this->socket,1);
174                                return $buffer;
175                        }
176                        $buffer .= $char;
177                }
178                return $buffer;
179        }
180/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
181// Manipulação dos scripts
182/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
183               
184        function listscripts(){
185               
186                $bits = '';
187
188                //Verifica a conexao
189                if(!is_resource($this->socket)){
190                        $this->errstr = "listscripts: sem conexão para o servidor $this->host";
191                        return false;
192                }
193
194                $scripts = array();
195                fputs($this->socket,"LISTSCRIPTS\r\n");
196       
197                $said = $this->read();
198               
199                // DEBUG: Para compatibilizar o sieve com o REFERRAL
200                $tmp = array();
201        if ( preg_match('|^BYE \(REFERRAL "(sieve://)?([^/"]+)"\)|', $said, $tmp) ){
202                        // Redefine a configuracao do servidor sieve, baseado no referral
203                        $this->host = $tmp[2];
204                        // Forca reiniciar a conexao, mas agora para o novo servidor
205                        $this->close ();
206                        unset ($this->socket);
207                        $this->start ();
208
209                        // Excuta novamente esta funcao (libscripts) para continuar a sequncia do codigo
210                        // Chama-la recursivamente nao funciona. A funcao fica em loop
211                        if(!is_resource($this->socket)){
212                                $this->errstr = "listscripts: sem conexao para o servidor $this->host";
213                                return false;
214                        }
215                        fputs($this->socket,"LISTSCRIPTS\r\n");
216                        $said = $this->read();
217        }
218
219               
220                while (is_resource($this->socket) && (!preg_match("/^OK/",$said) && !preg_match("/^NO/",$said))) {
221       
222                    // Cyrus v1 script lines look like '"script*"' with the
223                    // asterisk denoting the active script. Cyrus v2 script
224                    // lines will look like '"script" ACTIVE' if active.
225       
226                    if (preg_match("/^\"(.+)\"\s*(.+)*$/m",$said,$bits)) {
227                        if (preg_match("/\*$/",$bits[1])){
228                            $bits[1] = preg_replace("/\*$/","",$bits[1]);
229                            $this->activescript = $bits[1];
230                        }
231                        if (isset($bits[2]) && $bits[2] == 'ACTIVE')
232                            $this->activescript = $bits[1];
233                        array_push($scripts,$bits[1]);
234                    }
235                    $said = $this->read();
236                }
237       
238                if (preg_match("/^OK/",$said)) {
239                    $this->scriptlist = $scripts;
240            return $this->scriptlist;
241        }
242               
243        }
244       
245        // Pega o conteudo do script no servidor       
246        function getscript(){
247
248                $scriptfile = $this->listscripts();
249               
250                // verifica se existe o script;
251                if($scriptfile == ""){
252                        return "Falta o script";
253                }
254               
255                if(!$this->socket){
256                        return "Falha na conexao";     
257                }
258               
259                $script = '';
260               
261                fputs($this->socket,"GETSCRIPT \"$scriptfile[0]\"\r\n");
262                $said = $this->read();
263                while ((!preg_match("/^OK/",$said)) && (!preg_match("/^NO/",$said))) {
264                    // replace newlines which read() removed
265                    if (!preg_match("/\n$/",$said)) $said .= "\n";
266                    $script .= $said;
267                    $said = $this->read();
268                }
269               
270                if($said == "OK"){
271                        return $script;
272                }else{
273                        return false;
274                }
275        }
276
277        //envia para o servidor o nome do script($scriptfile) e seu conteudo($script)
278        function putscript ($scriptfile,$script) {
279                if (!isset($scriptfile)) {
280                    $this->errstr = "Não foi possível enviar o script para o servidor";
281                    return false;
282            }
283                if (!isset($script)) {
284                    $this->errstr = "Não foi possível enviar o script para o servidor";
285                    return false;
286            }
287                if (!$this->socket) {
288                    $this->errstr = "Sem conexão com o servidor $this->server";
289                    return false;
290            }
291       
292                $len = strlen($script);
293
294                //fputs($this->socket,"PUTSCRIPT \"$scriptfile\" \{$len+}\r\n");
295                //fputs($this->socket,"$script\r\n");
296       
297                fwrite($this->socket, 'PUTSCRIPT "'.$scriptfile.'" {' . $len . '+}' . "\r\n"); 
298                fwrite($this->socket,"$script\r\n");
299       
300                $said = '';
301                while ($said == '') {
302                    $said = $this->read();
303                }
304         
305            if (preg_match("/^OK/",$said)) {
306                    return true;
307                }
308       
309            $this->errstr = "Não foi possível enviar o $scriptfile: $said";
310            return false;
311    }
312   
313    // Ativa o script para o servico sieve;
314    function activatescript ($scriptfile) {
315                if (!isset($scriptfile)) {
316                    $this->errstr = "activatescript: no script file specified";
317                    return false;
318            }
319       
320            if (!$this->socket) {
321                    $this->errstr = "activatescript: no connection open to $this->server";
322                    return false;
323            }
324       
325                fputs($this->socket,"SETACTIVE \"$scriptfile\"\r\n");
326       
327                $said = $this->read();
328       
329                if (preg_match("/^OK/",$said)) {
330                    return true;
331            }
332       
333                $this->errstr = "activatescript: could not activate script $scriptfile: $said";
334            return false;
335    }
336
337    // Deleta o script do serviço sieve;
338    function deletescript ($scriptName) {
339        if(!isset($scriptName)){
340                $this->errstr = "deletescript: no script file specified";
341                return false;
342        }
343       
344        // Verifica a conexão;
345        if(!$this->socket){
346                $this->errstr = "deletescript : no connection open to $this->server";
347                return false;
348        }
349   
350        fputs($this->socket,"DELETESCRIPT \"$scriptName\"\r\n");
351       
352        $said = $this->read();
353       
354        if(preg_match("/^OK/",$said)) {
355                return true;   
356        }
357       
358        $this->errstr = "deletescript: could not delete script $scriptName: $said";
359        return false;
360    }
361}
362?>
Note: See TracBrowser for help on using the repository browser.