source: companies/serpro/expressoMail1_2/inc/class.SieveS.inc.php @ 903

Revision 903, 8.5 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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