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

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

Ticket #559 - Correção de problema, usando caminho relativo

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