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

Revision 1036, 8.5 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Atualização de segurança

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