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

Revision 2360, 9.1 KB checked in by amuller, 14 years ago (diff)

Ticket #1008 - Adicionando informações sobre licenças

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