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 | */ |
---|
57 | class 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 | $require_fileinto = $require_flag = $require_reject = $require_vacation = $require_body = $require_imapflag = $vacation = $startswith = $endswith = false; |
---|
78 | |
---|
79 | $script_rules = $script_header = $script_criteria = ""; |
---|
80 | $i = 0; |
---|
81 | foreach( $rules as $name => $data ) |
---|
82 | { |
---|
83 | //if( $data['enabled'] ) |
---|
84 | //continue; |
---|
85 | |
---|
86 | $criteria = $data['criteria']; |
---|
87 | $action = $data['actions']; |
---|
88 | |
---|
89 | ($i >0) ? $script_match = 'els' : $script_match = ''; |
---|
90 | $data['isExact'] == 'false' ? $script_match .= 'if anyof (' : $script_match .= 'if allof ('; |
---|
91 | |
---|
92 | for ($j=0; $j<count($criteria); $j++) |
---|
93 | { |
---|
94 | switch($criteria[$j]['field']) { |
---|
95 | case 'To': |
---|
96 | case 'to': |
---|
97 | case 'CC': |
---|
98 | case 'Cc': |
---|
99 | $criteria[$j]['field'] = "[\"To\", \"TO\", \"Cc\", \"CC\"]"; |
---|
100 | $script_criteria .= "address :"; |
---|
101 | break; |
---|
102 | case 'from': |
---|
103 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
104 | $script_criteria .= "address :"; |
---|
105 | break; |
---|
106 | case 'size': |
---|
107 | $criteria[$j]['field'] = ''; |
---|
108 | $script_criteria .= "size :"; |
---|
109 | break; |
---|
110 | case 'subject': |
---|
111 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
112 | $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "\", \"" . base64_encode($criteria[$j]['value']) . ""; |
---|
113 | $script_criteria .= "header :"; |
---|
114 | break; |
---|
115 | case 'body': |
---|
116 | $criteria[$j]['field'] = ''; |
---|
117 | $script_criteria .= "body :"; |
---|
118 | $require_body = true; |
---|
119 | break; |
---|
120 | case 'vacation': |
---|
121 | $require_vacation = true; |
---|
122 | $vacation = true; |
---|
123 | $script_match = "vacation"; |
---|
124 | break; |
---|
125 | default: |
---|
126 | $script_criteria .= "header :"; |
---|
127 | break; |
---|
128 | } |
---|
129 | |
---|
130 | switch ($criteria[$j]['operator']) { |
---|
131 | case '>': |
---|
132 | $criteria[$j]['operator'] = "over"; |
---|
133 | break; |
---|
134 | case '<': |
---|
135 | $criteria[$j]['operator'] = "under"; |
---|
136 | break; |
---|
137 | case '=': |
---|
138 | $criteria[$j]['operator'] = "is"; |
---|
139 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
140 | break; |
---|
141 | case '*': |
---|
142 | $criteria[$j]['operator'] = "contains"; |
---|
143 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
144 | break; |
---|
145 | case '^': |
---|
146 | $criteria[$j]['operator'] = "matches"; |
---|
147 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "*\"]"; |
---|
148 | $startswith = true; |
---|
149 | break; |
---|
150 | case '$': |
---|
151 | $criteria[$j]['operator'] = "matches"; |
---|
152 | $criteria[$j]['value'] = "[\"*" . $criteria[$j]['value'] . "\"]"; |
---|
153 | $endswith = true; |
---|
154 | break; |
---|
155 | /* |
---|
156 | TO-DO: |
---|
157 | Arrrumar regra do "não contém". |
---|
158 | */ |
---|
159 | case '!*': |
---|
160 | $criteria[$j]['operator'] = "contains"; |
---|
161 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
162 | break; |
---|
163 | } |
---|
164 | |
---|
165 | if ($criteria[$j]['field'] == "" || $criteria[$j]['field'] == "\"subject\"" || $startswith || $endswith) |
---|
166 | { |
---|
167 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
168 | $startswith = $endswith = false; |
---|
169 | } |
---|
170 | else |
---|
171 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
172 | } |
---|
173 | $script_criteria = substr($script_criteria,0,-2); |
---|
174 | if ($vacation == false) |
---|
175 | $script_criteria .= ")"; |
---|
176 | else |
---|
177 | $script_criteria = ""; |
---|
178 | $script_action = " {\r\n "; |
---|
179 | for ($k=0; $k<count($action); $k++) |
---|
180 | { |
---|
181 | switch ($action[$k]['type']) { |
---|
182 | case 'redirect': |
---|
183 | break; |
---|
184 | case 'reject': |
---|
185 | $require_reject = true; |
---|
186 | break; |
---|
187 | case 'fileinto': |
---|
188 | $require_fileinto = true; |
---|
189 | break; |
---|
190 | case 'vacation': |
---|
191 | $require_vacation = true; |
---|
192 | $action[$k]['parameter'] = "\"" . $action[$k]['parameter'] . "\""; |
---|
193 | $script_action = " :subject \"Fora do Escritório\" " . $action[$k]['parameter'] . ";"; |
---|
194 | $vacation = true; |
---|
195 | break; |
---|
196 | case 'setflag': |
---|
197 | $require_flag = true; |
---|
198 | $action[$k]['parameter'] = "\\\\" . $action[$k]['parameter']; |
---|
199 | break; |
---|
200 | case 'discard': |
---|
201 | break; |
---|
202 | } |
---|
203 | if ($vacation == false) $script_action .= $action[$k]['type'] . " \"" . $action[$k]['parameter'] . "\";\r\n "; |
---|
204 | } |
---|
205 | |
---|
206 | if ($vacation == false) $script_action .= "}"; |
---|
207 | |
---|
208 | $script_rules .= $script_match . $script_criteria . $script_action . "\r\n"; |
---|
209 | $script_match = ""; |
---|
210 | $script_criteria = ""; |
---|
211 | $i++; |
---|
212 | } |
---|
213 | |
---|
214 | if($require_reject || $require_fileinto || $require_vacation || $require_body || $require_flag) |
---|
215 | { |
---|
216 | $script_header .= "require ["; |
---|
217 | $require_reject ? $script_header .= "\"reject\", " : ""; |
---|
218 | $require_fileinto ? $script_header .= "\"fileinto\", " : ""; |
---|
219 | $require_vacation? $script_header .= "\"vacation\", " : ""; |
---|
220 | $require_flag ? $script_header .= "\"imapflags\", " : ""; |
---|
221 | $require_body ? $script_header .= "\"body\", " : ""; /* tem que instalar as extensões no Cyrus */ |
---|
222 | $script_header = substr($script_header,0,-2); |
---|
223 | $script_header .= "];\r\n"; |
---|
224 | } |
---|
225 | |
---|
226 | $json_data = json_encode($rules); |
---|
227 | |
---|
228 | $script_begin = "#Filtro gerado por Expresso Livre versão 2.4\r\n\r\n"; |
---|
229 | |
---|
230 | $content = $script_begin . $script_header . $script_rules . "\r\n\r\n#PseudoScript#" . "\r\n#" . $json_data; |
---|
231 | |
---|
232 | /* $content = ""; //fill script! */ |
---|
233 | return( $content ); |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | /** |
---|
238 | * Método que faz o parsing do Script Sieve, transformando em Array. |
---|
239 | * |
---|
240 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
241 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
242 | * @sponsor Caixa EconÃŽmica Federal |
---|
243 | * @author Airton Bordin Junior |
---|
244 | * @author Gustavo Pereira dos Santos |
---|
245 | * @param <String> <$script> <Script Sieve com as regras do usuário> |
---|
246 | * @return <Regras do usuário em Array> |
---|
247 | * @access <public> |
---|
248 | */ |
---|
249 | public function parseSieveScript( $script ) |
---|
250 | { |
---|
251 | $pos = strripos($script, "#PseudoScript#"); |
---|
252 | $pseudo_script = substr( $script, $pos+17 ); |
---|
253 | |
---|
254 | $return = json_decode( $pseudo_script, true ); |
---|
255 | |
---|
256 | return $return; |
---|
257 | } |
---|
258 | |
---|
259 | var $rules = false; |
---|
260 | |
---|
261 | /** |
---|
262 | * Construtor da classe. |
---|
263 | * |
---|
264 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
265 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
266 | * @sponsor Caixa EconÃŽmica Federal |
---|
267 | * @author Airton Bordin Junior |
---|
268 | * @author Gustavo Pereira dos Santos |
---|
269 | * @access <public> |
---|
270 | */ |
---|
271 | public function __construct() |
---|
272 | { |
---|
273 | $this->service = Controller::service("Sieve"); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | /** |
---|
278 | * Método que recupera as regras do usuário. |
---|
279 | * |
---|
280 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
281 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
282 | * @sponsor Caixa EconÃŽmica Federal |
---|
283 | * @author Airton Bordin Junior |
---|
284 | * @author Gustavo Pereira dos Santos |
---|
285 | * @return <Regras do usuário> |
---|
286 | * @access <public> |
---|
287 | */ |
---|
288 | public function getRules() |
---|
289 | { |
---|
290 | $this->rules = Controller::find( array( 'concept' => 'filter' ) ); |
---|
291 | |
---|
292 | if( !$this->rules ) { |
---|
293 | $this->rules = array(); |
---|
294 | } |
---|
295 | return( $this->rules ); |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | /** |
---|
300 | * Método que aplica o filtro para as mensagens do usuário. |
---|
301 | * |
---|
302 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
303 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
304 | * @sponsor Caixa EconÃŽmica Federal |
---|
305 | * @author Airton Bordin Junior |
---|
306 | * @author Gustavo Pereira dos Santos |
---|
307 | * @param <$uri> |
---|
308 | * @param <$result> |
---|
309 | * @param <$criteria> |
---|
310 | * @param <$original> |
---|
311 | * @access <public> |
---|
312 | */ |
---|
313 | public function applySieveFilter( &$uri , &$result , &$criteria , $original ) |
---|
314 | { |
---|
315 | if( $original['properties']['applyMessages'] ) |
---|
316 | { |
---|
317 | $filter = Controller::read($uri); |
---|
318 | |
---|
319 | $proc = array(); |
---|
320 | $proc['keep'] = false; |
---|
321 | |
---|
322 | foreach ($filter['actions'] as $v){ |
---|
323 | if($v['type'] == 'keep') |
---|
324 | $proc['keep'] = true; |
---|
325 | else{ |
---|
326 | $proc['type'] = $v['type']; |
---|
327 | $proc['parameter'] = $v['parameter']; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | $imap = Controller::service( 'Imap' ); |
---|
332 | $imap->apliSieveFilter($original['properties']['applyMessages'] , $proc ); |
---|
333 | return $result; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | /** |
---|
339 | * Método que lê o script do usuário. |
---|
340 | * |
---|
341 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
342 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
343 | * @sponsor Caixa EconÃŽmica Federal |
---|
344 | * @author Airton Bordin Junior |
---|
345 | * @author Gustavo Pereira dos Santos |
---|
346 | * @param <$uri> |
---|
347 | * @param <$result> |
---|
348 | * @param <$criteria> |
---|
349 | * @param <$original> |
---|
350 | * @return <Script do usuário> |
---|
351 | * @access <public> |
---|
352 | */ |
---|
353 | public function readUserScript( &$uri , &$params , &$criteria , $original ) |
---|
354 | { |
---|
355 | $uri['id'] = $this->service->config['user']; |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | /** |
---|
360 | * Método que seta o script do usuário. |
---|
361 | * |
---|
362 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
363 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
364 | * @sponsor Caixa EconÃŽmica Federal |
---|
365 | * @author Airton Bordin Junior |
---|
366 | * @author Gustavo Pereira dos Santos |
---|
367 | * @param <$uri> |
---|
368 | * @param <$result> |
---|
369 | * @param <$criteria> |
---|
370 | * @param <$original> |
---|
371 | * @return <Script do usuário> |
---|
372 | * @access <public> |
---|
373 | */ |
---|
374 | public function setRule( &$uri , &$params , &$criteria , $original ) |
---|
375 | { |
---|
376 | if( !$this->rules ) |
---|
377 | $this->rules = $this->getRules(); |
---|
378 | |
---|
379 | $params['id'] = isset($params['id']) ? $params['id'] : $params['name']; |
---|
380 | $update = false; |
---|
381 | |
---|
382 | foreach( $this->rules as $i => $rule ) |
---|
383 | if( $update = ($rule['id'] === $params['id']) ) |
---|
384 | { |
---|
385 | $this->rules[$i] = $params; |
---|
386 | break; |
---|
387 | } |
---|
388 | |
---|
389 | if( !$update ) |
---|
390 | $this->rules[] = $params; |
---|
391 | |
---|
392 | $content = $this->formatSieveScript( $this->rules ); |
---|
393 | |
---|
394 | $params = array( 'name' => $this->service->config['user'], |
---|
395 | 'content' => $content, |
---|
396 | 'active' => true ); |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | /** |
---|
401 | * Método que deleta o script do usuário. |
---|
402 | * |
---|
403 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
404 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
405 | * @sponsor Caixa EconÃŽmica Federal |
---|
406 | * @author Airton Bordin Junior |
---|
407 | * @author Gustavo Pereira dos Santos |
---|
408 | * @param <$uri> |
---|
409 | * @param <$result> |
---|
410 | * @param <$criteria> |
---|
411 | * @param <$original> |
---|
412 | * @access <public> |
---|
413 | */ |
---|
414 | public function deleteRule( &$uri, &$params, &$criteria, $original ) |
---|
415 | { |
---|
416 | if( !$this->rules ) |
---|
417 | $this->rules = $this->getRules(); |
---|
418 | |
---|
419 | $params['id'] = $uri['id']; |
---|
420 | |
---|
421 | $update = false; |
---|
422 | |
---|
423 | foreach( $this->rules as $i => $rule ) |
---|
424 | if( $rule['id'] === $uri['id'] ) |
---|
425 | unset( $this->rules[$i] ); |
---|
426 | |
---|
427 | $uri['id'] = ''; |
---|
428 | |
---|
429 | $params = array( 'name' => $this->service->config['user'], |
---|
430 | 'content' => $this->formatSieveScript( $this->rules ), |
---|
431 | 'active' => true ); |
---|
432 | |
---|
433 | $URI = Controller::URI( $uri['concept'], $this->service->config['user'] ); |
---|
434 | $this->service->update( $URI, $params ); |
---|
435 | |
---|
436 | return( false ); |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | /** |
---|
441 | * Método que pega o script do usuário. |
---|
442 | * |
---|
443 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
444 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
445 | * @sponsor Caixa EconÃŽmica Federal |
---|
446 | * @author Airton Bordin Junior |
---|
447 | * @author Gustavo Pereira dos Santos |
---|
448 | * @param <$uri> |
---|
449 | * @param <$result> |
---|
450 | * @param <$criteria> |
---|
451 | * @param <$original> |
---|
452 | * @return <Script do usuário> |
---|
453 | * @access <public> |
---|
454 | */ |
---|
455 | public function getSieveRule( &$uri , &$params , &$criteria , $original ) |
---|
456 | { |
---|
457 | $script = $this->parseSieveScript( $params['content'] ); |
---|
458 | |
---|
459 | foreach( $script as $i => $rule ) |
---|
460 | if( $rule['name'] === $original['id'] ) |
---|
461 | return( $params = $rule ); |
---|
462 | } |
---|
463 | |
---|
464 | |
---|
465 | /** |
---|
466 | * Método que lista as regras do usuário. |
---|
467 | * |
---|
468 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
469 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
470 | * @sponsor Caixa EconÃŽmica Federal |
---|
471 | * @author Airton Bordin Junior |
---|
472 | * @author Gustavo Pereira dos Santos |
---|
473 | * @param <$uri> |
---|
474 | * @param <$result> |
---|
475 | * @param <$criteria> |
---|
476 | * @param <$original> |
---|
477 | * @return <Regras do usuário> |
---|
478 | * @access <public> |
---|
479 | */ |
---|
480 | public function listSieveRules( &$uri , &$params , &$criteria , $original ) |
---|
481 | { |
---|
482 | $return = $params = $this->parseSieveScript( $params[0]['content'] ); |
---|
483 | return( $return ); |
---|
484 | } |
---|
485 | } |
---|