source: trunk/prototype/modules/filters/interceptors/FilterMapping.php @ 5692

Revision 5692, 18.1 KB checked in by airton, 12 years ago (diff)

Ticket #2088 - Melhorias no filtro de mensagens do Expresso - Alteracao em algumas repeticoes

Line 
1<?php
2/**
3*
4* Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
5*
6* This program is free software; you can redistribute it and/or modify it under
7* the terms of the GNU Affero General Public License version 3 as published by
8* the Free Software Foundation with the addition of the following permission
9* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
10* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
11* WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
12*
13* This program is distributed in the hope that it will be useful, but WITHOUT
14* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16* details.
17*
18* You should have received a copy of the GNU Affero General Public License
19* along with this program; if not, see www.gnu.org/licenses or write to
20* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21* MA 02110-1301 USA.
22*
23* This code is based on the OpenXchange Connector and on the Prognus pSync
24* Connector both developed by the community and licensed under the GPL
25* version 2 or above as published by the Free Software Foundation.
26*
27* You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
28* 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at
29* e-mail address prognus@prognus.com.br.
30*
31* Descrição rápida do arquivo
32*
33* Arquivo responsável pela manipulação dos filtros
34*
35* @package    filters
36* @license    http://www.gnu.org/copyleft/gpl.html GPL
37* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
38* @version    1.0
39* @sponsor    Caixa EconÃŽmica Federal
40* @since      Arquivo disponibilizado na versão 2.4
41*/
42
43
44
45/**
46* Classe responsável pela manipulação dos filtros.
47*
48*
49* @package    prototype
50* @license    http://www.gnu.org/copyleft/gpl.html GPL
51* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
52* @author     Airton Bordin Junior
53* @author     Gustavo Pereira dos Santos
54* @version    1.0
55* @since      Classe disponibilizada na versão 2.4
56*/
57class FilterMapping
58{
59       
60        var $service;
61       
62       
63        /**
64        * Método que formata o Script de acordo com a sintaxe do Sieve.
65        *
66        * @license    http://www.gnu.org/copyleft/gpl.html GPL
67        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
68        * @sponsor    Caixa EconÃŽmica Federal
69        * @author     Airton Bordin Junior
70        * @author         Gustavo Pereira dos Santos   
71        * @param      <Array> <$rules> <Array com as regras do usuário>
72        * @return     <Regra de acordo com a sintaxe do Sieve>
73        * @access     <public>
74        */
75        public function formatSieveScript( $rules )
76    {
77       
78                $require_fileinto = $require_flag = $require_reject = $require_vacation = $require_body = $require_imapflag = $vacation = $startswith = $endswith = false;
79
80                $script_rules = $script_header = $script_criteria = $vacation_action = "";
81
82                $i = 0;
83
84                foreach( $rules as $name => $data )
85                {
86                        /* Usado na opção Bloquear usuário do ExpressoMail */
87                        if($data['block']) {
88                                ($i >0) ? $script_match = 'elsif anyof' : $script_match = 'if anyof';
89                                $script_match = $script_match . "(address :is \"from\" [\"" .$data['name'] . "\"]) {\r\n"; 
90                                $script_match .= "fileinto \"INBOX/Spam\"; \r\n}\r\n";
91                                $script_rules .= $script_match;
92                                $script_match = "";
93                                $script_criteria = "";
94                                $require_fileinto = true;
95                                $i++;
96                                continue;
97                        }
98                       
99                        if( $data['enabled'] == 'false' )
100                                continue;
101
102                        $vacation = false;
103                        $criteria = $data['criteria'];
104                        $action   = $data['actions'];
105                       
106                        ($i >0) ? $script_match = 'els' : $script_match = '';
107                        $data['isExact'] == 'false' ?  $script_match .= 'if anyof (' : $script_match .= 'if allof (';
108
109                        foreach ($criteria as $j => $value);
110                        {                                       
111                                switch($criteria[$j]['field']) {
112                                        case 'To':   
113                                        case 'to':   
114                                        case 'CC':
115                                        case 'Cc':
116                                                $criteria[$j]['field'] = "[\"To\", \"TO\", \"Cc\", \"CC\"]";
117                                                $script_criteria .= "address :";
118                                                break;
119                                        case 'from':
120                                                $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\"";
121                                                $script_criteria .= "address :";
122                                                break;
123                                        case 'size':   
124                                                $criteria[$j]['field'] = '';
125                                                $script_criteria .= "size :";
126                                                break;
127                                        case 'subject':
128                                                $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\"";
129                                                $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "\", \"" . base64_encode($criteria[$j]['value']) . "";
130                                                $script_criteria .= "header :";
131                                                break;
132                                        case 'body':
133                                                $criteria[$j]['field'] = '';
134                                                $script_criteria .= "body :";
135                                                $require_body = true;
136                                                break;
137                                        case 'vacation':
138                                                continue;
139                                        default:
140                                                $script_criteria .= "header :";
141                                                break;
142                                }
143                               
144                                switch ($criteria[$j]['operator']) {
145                                        case '>':
146                                                $criteria[$j]['operator'] = "over";
147                                                break;
148                                        case '<':
149                                                $criteria[$j]['operator'] = "under";
150                                                break;
151                                        case '=':
152                                                $criteria[$j]['operator'] = "is";
153                                                $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]";
154                                                break;
155                                        case '*':
156                                                $criteria[$j]['operator'] = "contains";
157                                                $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]";
158                                                break;                                         
159                                        case '^':
160                                                $criteria[$j]['operator'] = "matches";
161                                                $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "*\"]";
162                                                $startswith = true;
163                                                break;
164                                        case '$':
165                                                $criteria[$j]['operator'] = "matches";
166                                                $criteria[$j]['value'] = "[\"*" . $criteria[$j]['value'] . "\"]";
167                                                $endswith = true;
168                                                break;
169                                        /*
170                                           TO-DO:
171                                           Arrrumar regra do "não contém".
172                                        */
173                                        case '!*':
174                                                $criteria[$j]['operator'] = "contains";
175                                                $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]";
176                                                break;
177                                }
178                               
179                                if ($criteria[$j]['field'] == "" || $criteria[$j]['field'] == "\"subject\"" || $startswith || $endswith)
180                                {
181                                        $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", ";
182                                        $startswith = $endswith = false;
183                                }
184                                else
185                                        $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", ";
186                        }
187                        $script_criteria = substr($script_criteria,0,-2);
188                        /* if ($vacation == false) */
189                        $script_criteria .= ")";
190
191                        $script_action = " {\r\n ";
192                        foreach ($action as $k=> $value);
193                        {
194                                switch ($action[$k]['type']) {
195                                        case 'redirect':
196                                                break;
197                                        case 'reject':
198                                                $require_reject = true;
199                                                break;
200                                        case 'fileinto':
201                                                $require_fileinto = true;
202                                                break;
203                                        case 'vacation':
204                                                $require_vacation = true;
205                                                $action[$k]['parameter'] = "\"" . $action[$k]['parameter'] . "\"";
206                                                $vacation_action = " :subject \"Fora do Escritório\" " . $action[$k]['parameter'] . ";";
207                                                $vacation = true;
208                                                continue;
209                                        case 'setflag':
210                                                $require_flag = true;
211                                                $action[$k]['parameter'] = "\\\\" . $action[$k]['parameter'];
212                                                break;
213                                        case 'discard':
214                                                break;
215                                }
216                                if ($vacation == false) $script_action .= $action[$k]['type'] . " \"" . $action[$k]['parameter'] . "\";\r\n ";
217                        }
218
219                        $script_action .= "}";
220                       
221                        if($vacation != true)
222                                $script_rules .= $script_match . $script_criteria . $script_action . "\r\n";
223
224                        if($data['id'] != "vacation")
225                                $i++;
226                        $script_match = "";
227                        $script_criteria = "";
228                       
229                }
230
231                if($require_reject || $require_fileinto || $require_vacation || $require_body || $require_flag)
232                {
233                        $script_header .= "require [";
234                        $require_reject ? $script_header .= "\"reject\", " : "";
235                        $require_fileinto ? $script_header .= "\"fileinto\", " : "";
236                        $require_vacation? $script_header .= "\"vacation\", " : ""; 
237                        $require_flag ? $script_header .= "\"imapflags\", " : ""; 
238                        $require_body ? $script_header .= "\"body\", " : "";  /* tem que instalar as extensões no Cyrus */
239                        $script_header = substr($script_header,0,-2);
240                        $script_header .= "];\r\n";
241                }
242
243                if( $vacation_action )
244                {
245                  $script_rules .= "vacation" . $vacation_action . "\r\n";
246                }
247
248                $json_data = json_encode($rules);
249               
250                $script_begin = "#Filtro gerado por Expresso Livre versão 2.4\r\n\r\n";
251
252                $content = $script_begin . $script_header . $script_rules . "\r\n\r\n#PseudoScript#" . "\r\n#" . $json_data;
253                //$content = "";
254                return( $content );
255        }
256       
257        /**
258        * Método que lê e faz o parser dos filtros antigos
259        *
260        * @license    http://www.gnu.org/copyleft/gpl.html GPL
261        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
262        * @sponsor    Caixa EconÃŽmica Federal
263        * @author     Airton Bordin Junior
264        * @author         Gustavo Pereira dos Santos   
265        * @param      <$scriptName> <Regras do usuário>
266        * @return     <Regra do usuário parseada>
267        * @access     <public>
268        */
269        public function readOldScript($scriptName)
270        {
271        // Recebe o conteúdo do array;
272        $lines = array();
273        $lines = preg_split("/\n/", $scriptName);
274
275        // Pega o tamanho da regra na primeira do script;
276        $size_rule = array_shift($lines);
277
278        // Recebe o tamanho do script, pela primeira linha;
279        $this->size = trim($size_rule);
280
281        // Verifica a composição do script; */
282         $line = array_shift($lines);
283
284        // Variaveis para a regra e o campo ferias;
285        $regexps = array('^ *##PSEUDO', '^ *#rule', '^ *#vacation', '^ *#mode');
286        $retorno['rule'] = array();
287
288        $line = array_shift($lines);
289        while (isset($line)) {
290            foreach ($regexps as $regp) {
291                if (preg_match("/$regp/i", $line)) {
292                    // Recebe todas as regras criadas no servidor;
293                    if (preg_match("/^ *#rule&&/i", $line)) {
294                        $retorno['rule'][] = $line . "\n";
295                    }
296                }
297            }
298            // Pega a proxima linha do sript;
299            $line = array_shift($lines);
300        }
301        return $retorno;
302    }
303       
304       
305       
306       
307        /**
308        * Método que faz o parsing do Script Sieve, transformando em Array.
309        *
310        * @license    http://www.gnu.org/copyleft/gpl.html GPL
311        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
312        * @sponsor    Caixa EconÃŽmica Federal
313        * @author     Airton Bordin Junior
314        * @author         Gustavo Pereira dos Santos   
315        * @param      <String> <$script> <Script Sieve com as regras do usuário>
316        * @return     <Regras do usuário em Array>
317        * @access     <public>
318        */
319        public function parseSieveScript( $script )
320        {
321                $old_rule = strripos($script, "##PSEUDO script start");
322               
323                if($old_rule) {
324                       
325                        $parsed_rule = $this->readOldScript($old_script);
326                        $array_rule = explode("&&", $parsed_rule['rule'][0]);
327
328                        $old_retorno = array();
329                        $old_retorno['isExact'] = false;
330                        $old_retorno['name'] = $array_rule[1];
331                        $old_retorno['criteria'] = array();
332                        $old_retorno['criteria'][0] = array();
333                        $old_retorno['criteria'][0]['value'] = $array_rule[5];
334                        $old_retorno['criteria'][0]['operator'] = '=';
335                        $old_retorno['criteria'][0]['field'] = 'subject';
336                        $old_retorno['actions'] = array();
337                        $old_retorno['actions'][0] = array();
338                        $old_retorno['actions'][0]['parameter'] = $array_rule[7];
339                        $old_retorno['actions'][0]['type'] = $array_rule[6];
340                        $old_retorno['enabled'] = true;
341                        $old_retorno['id'] = $array_rule[1];
342                        $old_retorno['applyMessages'] = '';
343
344                        return $old_retorno;
345                }
346
347                $pos = strripos($script, "#PseudoScript#");
348                $pseudo_script = substr( $script, $pos+17 );
349
350                $return = json_decode( $pseudo_script, true );
351
352                return $return;
353        }
354
355        var $rules = false;
356
357        /**
358        * Construtor da classe.
359        *
360        * @license    http://www.gnu.org/copyleft/gpl.html GPL
361        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
362        * @sponsor    Caixa EconÃŽmica Federal
363        * @author     Airton Bordin Junior
364        * @author         Gustavo Pereira dos Santos   
365        * @access     <public>
366        */
367        public function __construct()
368        {
369                $this->service = Controller::service("Sieve");
370        }
371
372       
373        /**
374        * Método que recupera as regras do usuário.
375        *
376        * @license    http://www.gnu.org/copyleft/gpl.html GPL
377        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
378        * @sponsor    Caixa EconÃŽmica Federal
379        * @author     Airton Bordin Junior
380        * @author         Gustavo Pereira dos Santos   
381        * @return     <Regras do usuário>
382        * @access     <public>
383        */
384        public function getRules()
385        {
386                $this->rules = Controller::find( array( 'concept' => 'filter' ) );
387
388                if( !$this->rules ) {
389                        $this->rules = array();
390                }
391                return( $this->rules );
392        }
393
394       
395        /**
396        * Método que aplica o filtro para as mensagens do usuário.
397        *
398        * @license    http://www.gnu.org/copyleft/gpl.html GPL
399        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
400        * @sponsor    Caixa EconÃŽmica Federal
401        * @author     Airton Bordin Junior
402        * @author         Gustavo Pereira dos Santos   
403        * @param      <$uri>
404        * @param      <$result>
405        * @param      <$criteria>
406        * @param      <$original>
407        * @access     <public>
408        */
409        public function applySieveFilter( &$uri , &$result , &$criteria , $original  )
410        {
411                $result['id'] = $uri['id'];
412                $rule_apply = array();
413               
414                $filter = Controller::read($uri);
415                $filter_ = $this->parseSieveScript($filter['content']);
416               
417                foreach ($filter_ as $f_) {
418                        if($f_['id'] == $uri['id']) {
419                                $rule_apply     = $f_;
420                        }
421                }
422               
423                $actions = array();
424                $actions['type']      = $rule_apply['actions'][0]['type'];
425                $actions['parameter'] = $rule_apply['actions'][0]['parameter'];
426               
427                $messages = $rule_apply['applyMessages'];
428                 
429                         
430                $proc = array();
431                $proc['keep'] = false;
432                     
433               
434                $imap = Controller::service( 'Imap' );
435                //$imap->apliSieveFilter($original['properties']['applyMessages'] , $proc );
436                $imap->apliSieveFilter($messages , $actions );
437                return $result;
438        }
439
440       
441        /**
442        * Método que lê o script do usuário.
443        *
444        * @license    http://www.gnu.org/copyleft/gpl.html GPL
445        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
446        * @sponsor    Caixa EconÃŽmica Federal
447        * @author     Airton Bordin Junior
448        * @author         Gustavo Pereira dos Santos   
449        * @param      <$uri>
450        * @param      <$result>
451        * @param      <$criteria>
452        * @param      <$original>
453        * @return     <Script do usuário>
454        * @access     <public>
455        */
456        public function readUserScript( &$uri , &$params , &$criteria , $original )
457        { 
458                $uri['id'] = $this->service->config['user'];
459        }
460 
461 
462        /**
463        * Método que seta o script do usuário.
464        *
465        * @license    http://www.gnu.org/copyleft/gpl.html GPL
466        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
467        * @sponsor    Caixa EconÃŽmica Federal
468        * @author     Airton Bordin Junior
469        * @author         Gustavo Pereira dos Santos   
470        * @param      <$uri>
471        * @param      <$result>
472        * @param      <$criteria>
473        * @param      <$original>
474        * @return     <Script do usuário>
475        * @access     <public>
476        */
477        public function setRule( &$uri , &$params , &$criteria , $original  )
478        {
479                if( !$this->rules )
480                $this->rules = $this->getRules();
481
482            $uri['id'] = $params['id'] = isset($params['id']) ? $params['id'] : urlencode($params['name']);
483
484            $i = 0;
485
486            for( ; isset($this->rules[$i]) && $this->rules[$i]['id'] !== $params['id']; $i++ );
487
488            $this->rules[$i] = array_merge( ( isset($this->rules[$i]) ? $this->rules[$i] : array() ), $params );
489
490            $params = array( 'name' => $this->service->config['user'],
491                             'content' => $this->formatSieveScript( $this->rules ),
492                             'active' => true );
493        }
494
495       
496        /**
497        * Método que deleta o script do usuário.
498        *
499        * @license    http://www.gnu.org/copyleft/gpl.html GPL
500        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
501        * @sponsor    Caixa EconÃŽmica Federal
502        * @author     Airton Bordin Junior
503        * @author         Gustavo Pereira dos Santos   
504        * @param      <$uri>
505        * @param      <$result>
506        * @param      <$criteria>
507        * @param      <$original>
508        * @access     <public>
509        */
510        public function deleteRule( &$uri, &$params, &$criteria, $original )
511        {
512                if( !$this->rules ) {   
513                        $this->rules = $this->getRules();
514                }         
515                $params['id'] = $uri['id'];
516
517                $update = false;
518               
519                $rules = array();
520
521                foreach( $this->rules as $i => $rule )
522                        if( $rule['id'] !== $uri['id'] )
523                                $rules[] = $this->rules[$i];
524
525                $this->rules = $rules;
526               
527                $uri['id'] = '';
528
529                $params = array( 'name' => $this->service->config['user'],
530                           'content' => $this->formatSieveScript( $this->rules ),
531                           'active' => true );
532
533                $URI = Controller::URI( $uri['concept'], $this->service->config['user'] );
534                $this->service->update( $URI, $params );
535       
536                return( false );
537        }
538
539       
540        /**
541        * Método que pega o script do usuário.
542        *
543        * @license    http://www.gnu.org/copyleft/gpl.html GPL
544        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
545        * @sponsor    Caixa EconÃŽmica Federal
546        * @author     Airton Bordin Junior
547        * @author         Gustavo Pereira dos Santos   
548        * @param      <$uri>
549        * @param      <$result>
550        * @param      <$criteria>
551        * @param      <$original>
552        * @return     <Script do usuário>
553        * @access     <public>
554        */
555        public function getSieveRule( &$uri , &$params , &$criteria , $original )
556        {         
557                $script = $this->parseSieveScript( $params['content'] );
558
559                foreach( $script as $i => $rule )
560                        if( $rule['name'] === $original['id'] )
561                                return( $params = $rule );
562        }
563
564       
565        /**
566        * Método que lista as regras do usuário.
567        *
568        * @license    http://www.gnu.org/copyleft/gpl.html GPL
569        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
570        * @sponsor    Caixa EconÃŽmica Federal
571        * @author     Airton Bordin Junior
572        * @author         Gustavo Pereira dos Santos   
573        * @param      <$uri>
574        * @param      <$result>
575        * @param      <$criteria>
576        * @param      <$original>
577        * @return     <Regras do usuário>
578        * @access     <public>
579        */
580        public function listSieveRules( &$uri , &$params , &$criteria , $original  )
581        {
582                $return = $params = $this->parseSieveScript( $params[0]['content'] );
583                return( $return );
584        }
585}
Note: See TracBrowser for help on using the repository browser.