[5341] | 1 | <?php |
---|
[5539] | 2 | /** |
---|
| 3 | * |
---|
[6193] | 4 | * Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[5539] | 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, |
---|
[6193] | 28 | * 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at |
---|
[5539] | 29 | * e-mail address prognus@prognus.com.br. |
---|
| 30 | * |
---|
[6193] | 31 | * Descrição rápida do arquivo |
---|
[5539] | 32 | * |
---|
[6193] | 33 | * Arquivo responsável pela manipulação dos filtros |
---|
[5539] | 34 | * |
---|
| 35 | * @package filters |
---|
| 36 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 37 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[5539] | 38 | * @version 1.0 |
---|
[6193] | 39 | * @sponsor Caixa Econômica Federal |
---|
| 40 | * @since Arquivo disponibilizado na versão 2.4 |
---|
[5539] | 41 | */ |
---|
[5341] | 42 | |
---|
[5539] | 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
[6193] | 46 | * Classe responsável pela manipulação dos filtros. |
---|
[5539] | 47 | * |
---|
| 48 | * |
---|
| 49 | * @package prototype |
---|
| 50 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 51 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
[5862] | 52 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 53 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 54 | * @version 1.0 |
---|
[6193] | 55 | * @since Classe disponibilizada na versão 2.4 |
---|
[5539] | 56 | */ |
---|
[5341] | 57 | class FilterMapping |
---|
| 58 | { |
---|
[5539] | 59 | var $service; |
---|
| 60 | |
---|
[6021] | 61 | /** |
---|
[6193] | 62 | * Método que cria o ID da regra que está sendo criada. |
---|
[6021] | 63 | * |
---|
| 64 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 65 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 66 | * @sponsor Caixa Econômica Federal |
---|
[6021] | 67 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 68 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
| 69 | * @author Natan Fonseca <natan@prognus.com.br> |
---|
| 70 | * @param <$uri> |
---|
| 71 | * @param <$result> |
---|
| 72 | * @param <$criteria> |
---|
| 73 | * @param <$original> |
---|
| 74 | * @access <public> |
---|
| 75 | */ |
---|
| 76 | public function makeId(&$uri , &$result , &$criteria , $original) { |
---|
| 77 | $result['id'] = $uri['id']; |
---|
| 78 | } |
---|
[5539] | 79 | |
---|
[6021] | 80 | |
---|
[5539] | 81 | /** |
---|
[6193] | 82 | * Método que formata o Script de acordo com a sintaxe do Sieve. |
---|
[5539] | 83 | * |
---|
| 84 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 85 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 86 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 87 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 88 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 89 | * @param <Array> <$rules> <Array com as regras do usuário> |
---|
[5539] | 90 | * @return <Regra de acordo com a sintaxe do Sieve> |
---|
| 91 | * @access <public> |
---|
| 92 | */ |
---|
| 93 | public function formatSieveScript( $rules ) |
---|
[5341] | 94 | { |
---|
[5539] | 95 | $require_fileinto = $require_flag = $require_reject = $require_vacation = $require_body = $require_imapflag = $vacation = $startswith = $endswith = false; |
---|
[5341] | 96 | |
---|
[5629] | 97 | $script_rules = $script_header = $script_criteria = $vacation_action = ""; |
---|
| 98 | |
---|
[5539] | 99 | $i = 0; |
---|
[6009] | 100 | |
---|
[5539] | 101 | foreach( $rules as $name => $data ) |
---|
[6009] | 102 | { |
---|
| 103 | if( $data['enabled'] == 'false' ) |
---|
| 104 | continue; |
---|
| 105 | |
---|
[5747] | 106 | if(array_key_exists("block", $data)) { |
---|
[6193] | 107 | /* Usado na opção Bloquear usuário do ExpressoMail */ |
---|
[5747] | 108 | if($data['block']) { |
---|
| 109 | ($i >0) ? $script_match = 'elsif anyof' : $script_match = 'if anyof'; |
---|
| 110 | $script_match = $script_match . "(address :is \"from\" [\"" .$data['name'] . "\"]) {\r\n"; |
---|
| 111 | $script_match .= "fileinto \"INBOX/Spam\"; \r\n}\r\n"; |
---|
| 112 | $script_rules .= $script_match; |
---|
| 113 | $script_match = ""; |
---|
| 114 | $script_criteria = ""; |
---|
| 115 | $require_fileinto = true; |
---|
| 116 | $i++; |
---|
| 117 | continue; |
---|
| 118 | } |
---|
[5630] | 119 | } |
---|
[6009] | 120 | |
---|
[5629] | 121 | $vacation = false; |
---|
[5539] | 122 | $criteria = $data['criteria']; |
---|
| 123 | $action = $data['actions']; |
---|
| 124 | |
---|
| 125 | ($i >0) ? $script_match = 'els' : $script_match = ''; |
---|
| 126 | $data['isExact'] == 'false' ? $script_match .= 'if anyof (' : $script_match .= 'if allof ('; |
---|
[5341] | 127 | |
---|
[5728] | 128 | if( is_array($criteria) ) |
---|
[5996] | 129 | foreach ($criteria as $j => $value) |
---|
[5539] | 130 | { |
---|
[6193] | 131 | |
---|
[5539] | 132 | switch($criteria[$j]['field']) { |
---|
| 133 | case 'To': |
---|
| 134 | case 'to': |
---|
| 135 | case 'CC': |
---|
| 136 | case 'Cc': |
---|
| 137 | $criteria[$j]['field'] = "[\"To\", \"TO\", \"Cc\", \"CC\"]"; |
---|
| 138 | $script_criteria .= "address :"; |
---|
| 139 | break; |
---|
| 140 | case 'from': |
---|
| 141 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
| 142 | $script_criteria .= "address :"; |
---|
| 143 | break; |
---|
| 144 | case 'size': |
---|
| 145 | $criteria[$j]['field'] = ''; |
---|
| 146 | $script_criteria .= "size :"; |
---|
| 147 | break; |
---|
| 148 | case 'subject': |
---|
| 149 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
[6163] | 150 | $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "*\", \"" . base64_encode($criteria[$j]['value']) . ""; |
---|
[6348] | 151 | $criteria[$j]['operator'] = "^"; //temporary |
---|
[5539] | 152 | $script_criteria .= "header :"; |
---|
| 153 | break; |
---|
| 154 | case 'body': |
---|
| 155 | $criteria[$j]['field'] = ''; |
---|
| 156 | $script_criteria .= "body :"; |
---|
| 157 | $require_body = true; |
---|
| 158 | break; |
---|
| 159 | case 'vacation': |
---|
[5601] | 160 | continue; |
---|
[6163] | 161 | case 'hasAttachment': |
---|
[6348] | 162 | $criteria[$j]['field'] = ''; |
---|
| 163 | $script_criteria .= "body :"; |
---|
| 164 | $criteria[$j]['operator'] = "^^"; |
---|
| 165 | $require_body = true; |
---|
| 166 | break; |
---|
[5539] | 167 | default: |
---|
| 168 | $script_criteria .= "header :"; |
---|
| 169 | break; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | switch ($criteria[$j]['operator']) { |
---|
| 173 | case '>': |
---|
| 174 | $criteria[$j]['operator'] = "over"; |
---|
[6001] | 175 | $criteria[$j]['value'] = $criteria[$j]['value'] . "K"; |
---|
[5539] | 176 | break; |
---|
| 177 | case '<': |
---|
| 178 | $criteria[$j]['operator'] = "under"; |
---|
[6001] | 179 | $criteria[$j]['value'] = $criteria[$j]['value'] . "K"; |
---|
[5539] | 180 | break; |
---|
| 181 | case '=': |
---|
| 182 | $criteria[$j]['operator'] = "is"; |
---|
| 183 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 184 | break; |
---|
| 185 | case '*': |
---|
| 186 | $criteria[$j]['operator'] = "contains"; |
---|
| 187 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 188 | break; |
---|
| 189 | case '^': |
---|
| 190 | $criteria[$j]['operator'] = "matches"; |
---|
| 191 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "*\"]"; |
---|
| 192 | $startswith = true; |
---|
| 193 | break; |
---|
[6348] | 194 | case '^^': |
---|
| 195 | $criteria[$j]['operator'] = "raw :matches"; |
---|
| 196 | $criteria[$j]['value'] = "[\"*filename=*\"]"; |
---|
| 197 | $startswith = true; |
---|
| 198 | break; |
---|
[5539] | 199 | case '$': |
---|
| 200 | $criteria[$j]['operator'] = "matches"; |
---|
| 201 | $criteria[$j]['value'] = "[\"*" . $criteria[$j]['value'] . "\"]"; |
---|
| 202 | $endswith = true; |
---|
| 203 | break; |
---|
| 204 | /* |
---|
| 205 | TO-DO: |
---|
[6193] | 206 | Inconsistência na regra do "não contém". |
---|
[5539] | 207 | */ |
---|
| 208 | case '!*': |
---|
| 209 | $criteria[$j]['operator'] = "contains"; |
---|
| 210 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 211 | break; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if ($criteria[$j]['field'] == "" || $criteria[$j]['field'] == "\"subject\"" || $startswith || $endswith) |
---|
| 215 | { |
---|
| 216 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
| 217 | $startswith = $endswith = false; |
---|
| 218 | } |
---|
| 219 | else |
---|
| 220 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
| 221 | } |
---|
| 222 | $script_criteria = substr($script_criteria,0,-2); |
---|
[5629] | 223 | /* if ($vacation == false) */ |
---|
| 224 | $script_criteria .= ")"; |
---|
| 225 | |
---|
[5539] | 226 | $script_action = " {\r\n "; |
---|
[5728] | 227 | |
---|
[5747] | 228 | if( is_array($action) ) |
---|
[5996] | 229 | foreach ($action as $k => $value) |
---|
[5539] | 230 | { |
---|
| 231 | switch ($action[$k]['type']) { |
---|
| 232 | case 'redirect': |
---|
| 233 | break; |
---|
| 234 | case 'reject': |
---|
| 235 | $require_reject = true; |
---|
| 236 | break; |
---|
| 237 | case 'fileinto': |
---|
| 238 | $require_fileinto = true; |
---|
[6377] | 239 | $action[$k]['parameter'] = mb_convert_encoding($action[$k]['parameter'], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
[5539] | 240 | break; |
---|
| 241 | case 'vacation': |
---|
| 242 | $require_vacation = true; |
---|
| 243 | $action[$k]['parameter'] = "\"" . $action[$k]['parameter'] . "\""; |
---|
[6213] | 244 | $vacation_action = ' :subject "Fora do Escritório" ' . $action[$k]['parameter'] . ";"; |
---|
[5539] | 245 | $vacation = true; |
---|
[5601] | 246 | continue; |
---|
[5539] | 247 | case 'setflag': |
---|
| 248 | $require_flag = true; |
---|
| 249 | $action[$k]['parameter'] = "\\\\" . $action[$k]['parameter']; |
---|
| 250 | break; |
---|
| 251 | case 'discard': |
---|
| 252 | break; |
---|
| 253 | } |
---|
[5629] | 254 | if ($vacation == false) $script_action .= $action[$k]['type'] . " \"" . $action[$k]['parameter'] . "\";\r\n "; |
---|
[5539] | 255 | } |
---|
[5629] | 256 | $script_action .= "}"; |
---|
| 257 | |
---|
[6163] | 258 | if($vacation == false) |
---|
[5629] | 259 | $script_rules .= $script_match . $script_criteria . $script_action . "\r\n"; |
---|
[5539] | 260 | |
---|
[5629] | 261 | if($data['id'] != "vacation") |
---|
| 262 | $i++; |
---|
[5539] | 263 | $script_match = ""; |
---|
[5862] | 264 | $script_criteria = ""; |
---|
| 265 | $data['applyMessages'] = ""; |
---|
[5341] | 266 | } |
---|
[5539] | 267 | |
---|
| 268 | if($require_reject || $require_fileinto || $require_vacation || $require_body || $require_flag) |
---|
[5341] | 269 | { |
---|
[6348] | 270 | /* Para habilitar as funções desejadas, edite a diretiva sieve_extensions no arquivo de configuração "/etc/imapd.conf" */ |
---|
[5539] | 271 | $script_header .= "require ["; |
---|
| 272 | $require_reject ? $script_header .= "\"reject\", " : ""; |
---|
| 273 | $require_fileinto ? $script_header .= "\"fileinto\", " : ""; |
---|
| 274 | $require_vacation? $script_header .= "\"vacation\", " : ""; |
---|
| 275 | $require_flag ? $script_header .= "\"imapflags\", " : ""; |
---|
[6348] | 276 | $require_body ? $script_header .= "\"body\", " : ""; |
---|
[5539] | 277 | $script_header = substr($script_header,0,-2); |
---|
| 278 | $script_header .= "];\r\n"; |
---|
[5341] | 279 | } |
---|
| 280 | |
---|
[5629] | 281 | if( $vacation_action ) |
---|
[5601] | 282 | { |
---|
| 283 | $script_rules .= "vacation" . $vacation_action . "\r\n"; |
---|
| 284 | } |
---|
| 285 | |
---|
[5539] | 286 | $json_data = json_encode($rules); |
---|
| 287 | |
---|
[6348] | 288 | $script_begin = "#Filtro gerado por Expresso Livre\r\n\r\n"; |
---|
[5341] | 289 | |
---|
[5539] | 290 | $content = $script_begin . $script_header . $script_rules . "\r\n\r\n#PseudoScript#" . "\r\n#" . $json_data; |
---|
[6193] | 291 | |
---|
[5539] | 292 | return( $content ); |
---|
[5341] | 293 | } |
---|
[5539] | 294 | |
---|
[5672] | 295 | /** |
---|
[6193] | 296 | * Método que lê e faz o parser dos filtros antigos |
---|
[5672] | 297 | * |
---|
| 298 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 299 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 300 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 301 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 302 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 303 | * @param <$scriptName> <Regras do usuário> |
---|
| 304 | * @return <Regra do usuário parseada> |
---|
[5672] | 305 | * @access <public> |
---|
| 306 | */ |
---|
| 307 | public function readOldScript($scriptName) |
---|
| 308 | { |
---|
[6193] | 309 | // Recebe o conteúdo do array; |
---|
[5672] | 310 | $lines = array(); |
---|
| 311 | $lines = preg_split("/\n/", $scriptName); |
---|
[5341] | 312 | |
---|
[5672] | 313 | // Pega o tamanho da regra na primeira do script; |
---|
| 314 | $size_rule = array_shift($lines); |
---|
| 315 | |
---|
| 316 | // Recebe o tamanho do script, pela primeira linha; |
---|
[5862] | 317 | //$this->size = trim($size_rule); |
---|
[5672] | 318 | |
---|
[6193] | 319 | // Verifica a composição do script; */ |
---|
[5862] | 320 | $line = array_shift($lines); |
---|
[5672] | 321 | |
---|
| 322 | // Variaveis para a regra e o campo ferias; |
---|
[5862] | 323 | $regexps = array('##PSEUDO', '#rule', '#vacation', '#mode'); |
---|
[5672] | 324 | $retorno['rule'] = array(); |
---|
| 325 | |
---|
| 326 | $line = array_shift($lines); |
---|
[5862] | 327 | while (isset($line)) { |
---|
[5672] | 328 | foreach ($regexps as $regp) { |
---|
[5862] | 329 | if (preg_match("/$regp/i", $line)) { |
---|
[5672] | 330 | // Recebe todas as regras criadas no servidor; |
---|
[5862] | 331 | if (preg_match("/#rule&&/i", $line)) { |
---|
| 332 | $retorno['rule'][] = ltrim($line) . "\n"; |
---|
[5672] | 333 | } |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | // Pega a proxima linha do sript; |
---|
| 337 | $line = array_shift($lines); |
---|
| 338 | } |
---|
| 339 | return $retorno; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | |
---|
[5539] | 345 | /** |
---|
[6193] | 346 | * Método que faz o parsing do Script Sieve, transformando em Array. |
---|
[5539] | 347 | * |
---|
| 348 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 349 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 350 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 351 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 352 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 353 | * @param <String> <$script> <Script Sieve com as regras do usuário> |
---|
| 354 | * @return <Regras do usuário em Array> |
---|
[5539] | 355 | * @access <public> |
---|
| 356 | */ |
---|
| 357 | public function parseSieveScript( $script ) |
---|
[5341] | 358 | { |
---|
[6363] | 359 | $old_rule = strripos($script, "##PSEUDO script start"); |
---|
| 360 | /* Tem regra antiga */ |
---|
[5672] | 361 | if($old_rule) { |
---|
[5862] | 362 | $parsed_rule = $this->readOldScript($script); |
---|
| 363 | $old_rules = array(); |
---|
[6363] | 364 | $i_return = 0; |
---|
[5862] | 365 | foreach ($parsed_rule['rule'] as $i => $value) { |
---|
[6363] | 366 | $array_rule = explode("&&", $parsed_rule['rule'][$i]); |
---|
[5862] | 367 | |
---|
[6363] | 368 | $action_type = array(); |
---|
| 369 | $action_parameter = array(); |
---|
| 370 | $criteria_value = array(); |
---|
| 371 | $criteria_operator = array(); |
---|
| 372 | $criteria_field = array(); |
---|
| 373 | |
---|
| 374 | $i_criteria = 0; |
---|
| 375 | $i_action = 0; |
---|
[5672] | 376 | |
---|
[6193] | 377 | /* TO-DO: Ver as actions possíveis além de reject e fileinto */ |
---|
[5862] | 378 | switch($array_rule[6]) { |
---|
| 379 | case 'reject': |
---|
[6363] | 380 | $action_type[$i_action] = 'reject'; |
---|
| 381 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 382 | $i_action++; |
---|
[5862] | 383 | break; |
---|
| 384 | case 'folder': |
---|
[6363] | 385 | $action_type[$i_action] = 'fileinto'; |
---|
| 386 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 387 | $i_action++; |
---|
[5862] | 388 | break; |
---|
[6363] | 389 | case 'flagged': |
---|
| 390 | $action_type[$i_action] = 'setflag'; |
---|
| 391 | $action_parameter[$i_action] = 'flagged'; |
---|
| 392 | $i_action++; |
---|
| 393 | break; |
---|
| 394 | case 'address': |
---|
| 395 | $action_type[$i_action] = 'redirect'; |
---|
| 396 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 397 | $i_action++; |
---|
| 398 | break; |
---|
| 399 | /* Somente para tratar casos em que a ação não é suportada */ |
---|
| 400 | default: |
---|
| 401 | $action_type[$i_action] = 'setflag'; |
---|
| 402 | $action_parameter[$i_action] = 'flagged'; |
---|
| 403 | $i_action++; |
---|
| 404 | break; |
---|
| 405 | // Recuperar o cyrus_delimiter para forçar um fileinto para INBOX/trash |
---|
| 406 | //case 'discard': |
---|
| 407 | //$action_type[$i_action] = 'fileinto'; |
---|
| 408 | //$action_parameter[$i_action] =; |
---|
| 409 | //$i_action++; |
---|
| 410 | //break; |
---|
[5862] | 411 | } |
---|
| 412 | if($array_rule[3] != "") { |
---|
[6363] | 413 | $criteria_value[$i_criteria] = $array_rule[3]; |
---|
| 414 | $criteria_operator[$i_criteria] = '='; |
---|
| 415 | $criteria_field[$i_criteria] = 'from'; |
---|
| 416 | $i_criteria++; |
---|
[5862] | 417 | } |
---|
[6363] | 418 | if($array_rule[4] != "") { |
---|
| 419 | $criteria_value[$i_criteria] = $array_rule[4]; |
---|
| 420 | $criteria_operator[$i_criteria] = '='; |
---|
| 421 | $criteria_field[$i_criteria] = 'to'; |
---|
| 422 | $i_criteria++; |
---|
| 423 | } |
---|
| 424 | if($array_rule[5] != "") { |
---|
| 425 | $criteria_value[$i_criteria] = $array_rule[5]; |
---|
| 426 | $criteria_operator[$i_criteria] = '='; |
---|
| 427 | $criteria_field[$i_criteria] = 'subject'; |
---|
| 428 | $i_criteria++; |
---|
[5862] | 429 | } |
---|
| 430 | $old_retorno = array(); |
---|
[6363] | 431 | $old_retorno['isExact'] = true; |
---|
| 432 | $old_retorno['name'] = 'regra_migrada_' . $array_rule[1]; |
---|
| 433 | |
---|
| 434 | $old_retorno['criteria'] = array(); |
---|
| 435 | foreach($criteria_value as $j => $value) { |
---|
| 436 | $old_retorno['criteria'][$j] = array(); |
---|
| 437 | $old_retorno['criteria'][$j]['value'] = $criteria_value[$j]; |
---|
| 438 | $old_retorno['criteria'][$j]['operator'] = $criteria_operator[$j]; |
---|
| 439 | $old_retorno['criteria'][$j]['field'] = $criteria_field[$j]; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | $old_retorno['actions'] = array(); |
---|
| 443 | foreach($action_parameter as $j => $value) { |
---|
| 444 | $old_retorno['actions'][$j] = array(); |
---|
| 445 | $old_retorno['actions'][$j]['parameter'] = $action_parameter[$j]; |
---|
| 446 | $old_retorno['actions'][$j]['type'] = $action_type[$j]; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | $old_retorno['enabled'] = ($array_rule[2] == 'ENABLED') ? 'true' : 'false'; |
---|
| 450 | $old_retorno['id'] = 'Regra_migrada_' . $i_return; |
---|
| 451 | $old_retorno['applyMessages'] = ''; |
---|
[5862] | 452 | |
---|
[6363] | 453 | $old_rules[$i_return] = $old_retorno; |
---|
| 454 | $i_return++; |
---|
[5862] | 455 | } |
---|
| 456 | return $old_rules; |
---|
[5952] | 457 | } |
---|
[6193] | 458 | /* Não tem regra antiga */ |
---|
[5539] | 459 | $pos = strripos($script, "#PseudoScript#"); |
---|
| 460 | $pseudo_script = substr( $script, $pos+17 ); |
---|
[5341] | 461 | |
---|
[5539] | 462 | $return = json_decode( $pseudo_script, true ); |
---|
[5862] | 463 | |
---|
[5539] | 464 | return $return; |
---|
| 465 | } |
---|
[5341] | 466 | |
---|
[5539] | 467 | var $rules = false; |
---|
[5341] | 468 | |
---|
[5539] | 469 | /** |
---|
| 470 | * Construtor da classe. |
---|
| 471 | * |
---|
| 472 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 473 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 474 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 475 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 476 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 477 | * @access <public> |
---|
| 478 | */ |
---|
| 479 | public function __construct() |
---|
| 480 | { |
---|
| 481 | $this->service = Controller::service("Sieve"); |
---|
| 482 | } |
---|
[5341] | 483 | |
---|
[5539] | 484 | |
---|
| 485 | /** |
---|
[6193] | 486 | * Método que recupera as regras do usuário. |
---|
[5539] | 487 | * |
---|
| 488 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 489 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 490 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 491 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 492 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 493 | * @return <Regras do usuário> |
---|
[5539] | 494 | * @access <public> |
---|
| 495 | */ |
---|
| 496 | public function getRules() |
---|
| 497 | { |
---|
| 498 | $this->rules = Controller::find( array( 'concept' => 'filter' ) ); |
---|
[5341] | 499 | |
---|
[5539] | 500 | if( !$this->rules ) { |
---|
| 501 | $this->rules = array(); |
---|
| 502 | } |
---|
[5862] | 503 | |
---|
[5539] | 504 | return( $this->rules ); |
---|
| 505 | } |
---|
[5341] | 506 | |
---|
[5539] | 507 | |
---|
| 508 | /** |
---|
[6193] | 509 | * Método que aplica o filtro para as mensagens do usuário. |
---|
[5539] | 510 | * |
---|
| 511 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 512 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 513 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 514 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 515 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 516 | * @param <$uri> |
---|
| 517 | * @param <$result> |
---|
| 518 | * @param <$criteria> |
---|
| 519 | * @param <$original> |
---|
| 520 | * @access <public> |
---|
| 521 | */ |
---|
| 522 | public function applySieveFilter( &$uri , &$result , &$criteria , $original ) |
---|
| 523 | { |
---|
[5692] | 524 | |
---|
[5862] | 525 | //if() |
---|
| 526 | //{ |
---|
| 527 | $rule_apply = array(); |
---|
| 528 | |
---|
| 529 | $filter = Controller::read($uri); |
---|
| 530 | $filter_ = $this->parseSieveScript($filter['content']); |
---|
| 531 | |
---|
| 532 | foreach ($filter_ as $f_) { |
---|
| 533 | if($f_['id'] == $uri['id']) { |
---|
| 534 | $rule_apply = $f_; |
---|
| 535 | } |
---|
[5692] | 536 | } |
---|
[5862] | 537 | |
---|
| 538 | $actions = array(); |
---|
| 539 | $actions['type'] = $rule_apply['actions'][0]['type']; |
---|
| 540 | $actions['parameter'] = $rule_apply['actions'][0]['parameter']; |
---|
| 541 | /* Hardcoded */ |
---|
| 542 | $actions['keep'] = false; |
---|
| 543 | |
---|
| 544 | $messages_to_apply = array(); |
---|
| 545 | |
---|
| 546 | $messages = $rule_apply['applyMessages']; |
---|
| 547 | /* |
---|
| 548 | $proc = array(); |
---|
| 549 | $proc['keep'] = false; |
---|
| 550 | |
---|
| 551 | */ |
---|
| 552 | $imap = Controller::service( 'Imap' ); |
---|
[6163] | 553 | $imap->apliSieveFilter($messages, $actions); |
---|
[5862] | 554 | return $result; |
---|
| 555 | //} |
---|
[5539] | 556 | } |
---|
[5341] | 557 | |
---|
[5539] | 558 | |
---|
| 559 | /** |
---|
[6193] | 560 | * Método que lê o script do usuário. |
---|
[5539] | 561 | * |
---|
| 562 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 563 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 564 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 565 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 566 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 567 | * @param <$uri> |
---|
| 568 | * @param <$result> |
---|
| 569 | * @param <$criteria> |
---|
| 570 | * @param <$original> |
---|
[6193] | 571 | * @return <Script do usuário> |
---|
[5539] | 572 | * @access <public> |
---|
| 573 | */ |
---|
| 574 | public function readUserScript( &$uri , &$params , &$criteria , $original ) |
---|
| 575 | { |
---|
| 576 | $uri['id'] = $this->service->config['user']; |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | |
---|
| 580 | /** |
---|
[6193] | 581 | * Método que seta o script do usuário. |
---|
[5539] | 582 | * |
---|
| 583 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 584 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 585 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 586 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 587 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 588 | * @param <$uri> |
---|
| 589 | * @param <$result> |
---|
| 590 | * @param <$criteria> |
---|
| 591 | * @param <$original> |
---|
[6193] | 592 | * @return <Script do usuário> |
---|
[5539] | 593 | * @access <public> |
---|
| 594 | */ |
---|
| 595 | public function setRule( &$uri , &$params , &$criteria , $original ) |
---|
[5601] | 596 | { |
---|
[5630] | 597 | if( !$this->rules ) |
---|
[5539] | 598 | $this->rules = $this->getRules(); |
---|
[5341] | 599 | |
---|
[5601] | 600 | $uri['id'] = $params['id'] = isset($params['id']) ? $params['id'] : urlencode($params['name']); |
---|
[5341] | 601 | |
---|
[5601] | 602 | $i = 0; |
---|
[5341] | 603 | |
---|
[5629] | 604 | for( ; isset($this->rules[$i]) && $this->rules[$i]['id'] !== $params['id']; $i++ ); |
---|
[5601] | 605 | |
---|
[5629] | 606 | $this->rules[$i] = array_merge( ( isset($this->rules[$i]) ? $this->rules[$i] : array() ), $params ); |
---|
[5601] | 607 | |
---|
| 608 | $params = array( 'name' => $this->service->config['user'], |
---|
| 609 | 'content' => $this->formatSieveScript( $this->rules ), |
---|
| 610 | 'active' => true ); |
---|
[5539] | 611 | } |
---|
[5341] | 612 | |
---|
[5539] | 613 | |
---|
| 614 | /** |
---|
[6193] | 615 | * Método que deleta o script do usuário. |
---|
[5539] | 616 | * |
---|
| 617 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 618 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 619 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 620 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 621 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 622 | * @param <$uri> |
---|
| 623 | * @param <$result> |
---|
| 624 | * @param <$criteria> |
---|
| 625 | * @param <$original> |
---|
| 626 | * @access <public> |
---|
| 627 | */ |
---|
| 628 | public function deleteRule( &$uri, &$params, &$criteria, $original ) |
---|
| 629 | { |
---|
[5601] | 630 | if( !$this->rules ) { |
---|
| 631 | $this->rules = $this->getRules(); |
---|
| 632 | } |
---|
[5539] | 633 | $params['id'] = $uri['id']; |
---|
[5341] | 634 | |
---|
[5539] | 635 | $update = false; |
---|
[5601] | 636 | |
---|
| 637 | $rules = array(); |
---|
[5341] | 638 | |
---|
[5539] | 639 | foreach( $this->rules as $i => $rule ) |
---|
[5601] | 640 | if( $rule['id'] !== $uri['id'] ) |
---|
| 641 | $rules[] = $this->rules[$i]; |
---|
[5341] | 642 | |
---|
[5601] | 643 | $this->rules = $rules; |
---|
| 644 | |
---|
[5539] | 645 | $uri['id'] = ''; |
---|
[5341] | 646 | |
---|
[5539] | 647 | $params = array( 'name' => $this->service->config['user'], |
---|
| 648 | 'content' => $this->formatSieveScript( $this->rules ), |
---|
| 649 | 'active' => true ); |
---|
[5341] | 650 | |
---|
[5539] | 651 | $URI = Controller::URI( $uri['concept'], $this->service->config['user'] ); |
---|
| 652 | $this->service->update( $URI, $params ); |
---|
[5601] | 653 | |
---|
[5539] | 654 | return( false ); |
---|
| 655 | } |
---|
[5341] | 656 | |
---|
[5539] | 657 | |
---|
| 658 | /** |
---|
[6193] | 659 | * Método que pega o script do usuário. |
---|
[5539] | 660 | * |
---|
| 661 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 662 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 663 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 664 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 665 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 666 | * @param <$uri> |
---|
| 667 | * @param <$result> |
---|
| 668 | * @param <$criteria> |
---|
| 669 | * @param <$original> |
---|
[6193] | 670 | * @return <Script do usuário> |
---|
[5539] | 671 | * @access <public> |
---|
| 672 | */ |
---|
| 673 | public function getSieveRule( &$uri , &$params , &$criteria , $original ) |
---|
| 674 | { |
---|
| 675 | $script = $this->parseSieveScript( $params['content'] ); |
---|
[5341] | 676 | |
---|
[5539] | 677 | foreach( $script as $i => $rule ) |
---|
[5747] | 678 | if(is_array ($rule['name']) && is_array($original['id'])) |
---|
[5539] | 679 | if( $rule['name'] === $original['id'] ) |
---|
| 680 | return( $params = $rule ); |
---|
| 681 | } |
---|
[5341] | 682 | |
---|
[5539] | 683 | |
---|
| 684 | /** |
---|
[6193] | 685 | * Método que lista as regras do usuário. |
---|
[5539] | 686 | * |
---|
| 687 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 688 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 689 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 690 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 691 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 692 | * @param <$uri> |
---|
| 693 | * @param <$result> |
---|
| 694 | * @param <$criteria> |
---|
| 695 | * @param <$original> |
---|
[6193] | 696 | * @return <Regras do usuário> |
---|
[5539] | 697 | * @access <public> |
---|
| 698 | */ |
---|
| 699 | public function listSieveRules( &$uri , &$params , &$criteria , $original ) |
---|
[5629] | 700 | { |
---|
[5539] | 701 | $return = $params = $this->parseSieveScript( $params[0]['content'] ); |
---|
| 702 | return( $return ); |
---|
| 703 | } |
---|
| 704 | } |
---|