[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 | |
---|
[6754] | 43 | use prototype\api\Config as Config; |
---|
[5539] | 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; |
---|
[6392] | 60 | var $msgs_apply = array(); |
---|
[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; |
---|
[5629] | 96 | $script_rules = $script_header = $script_criteria = $vacation_action = ""; |
---|
[5539] | 97 | $i = 0; |
---|
[6009] | 98 | |
---|
[5539] | 99 | foreach( $rules as $name => $data ) |
---|
[6009] | 100 | { |
---|
| 101 | if( $data['enabled'] == 'false' ) |
---|
| 102 | continue; |
---|
| 103 | |
---|
[6771] | 104 | if(array_key_exists("block", $data)) |
---|
| 105 | { |
---|
[6193] | 106 | /* Usado na opção Bloquear usuário do ExpressoMail */ |
---|
[5747] | 107 | if($data['block']) { |
---|
| 108 | ($i >0) ? $script_match = 'elsif anyof' : $script_match = 'if anyof'; |
---|
| 109 | $script_match = $script_match . "(address :is \"from\" [\"" .$data['name'] . "\"]) {\r\n"; |
---|
| 110 | $script_match .= "fileinto \"INBOX/Spam\"; \r\n}\r\n"; |
---|
| 111 | $script_rules .= $script_match; |
---|
| 112 | $script_match = ""; |
---|
| 113 | $script_criteria = ""; |
---|
| 114 | $require_fileinto = true; |
---|
| 115 | $i++; |
---|
| 116 | continue; |
---|
| 117 | } |
---|
[5630] | 118 | } |
---|
[6009] | 119 | |
---|
[5629] | 120 | $vacation = false; |
---|
[5539] | 121 | $criteria = $data['criteria']; |
---|
| 122 | $action = $data['actions']; |
---|
| 123 | |
---|
[6824] | 124 | ($i >0 && $verifyNextRule == 'false') ? $script_match = 'els' : $script_match = ''; |
---|
[5539] | 125 | $data['isExact'] == 'false' ? $script_match .= 'if anyof (' : $script_match .= 'if allof ('; |
---|
[5341] | 126 | |
---|
[6824] | 127 | $verifyNextRule = 'false'; |
---|
| 128 | |
---|
[5728] | 129 | if( is_array($criteria) ) |
---|
[5996] | 130 | foreach ($criteria as $j => $value) |
---|
[5539] | 131 | { |
---|
[6193] | 132 | |
---|
[6771] | 133 | switch(strtoupper($criteria[$j]['field'])) { |
---|
| 134 | case 'TO': |
---|
[5539] | 135 | case 'CC': |
---|
| 136 | $criteria[$j]['field'] = "[\"To\", \"TO\", \"Cc\", \"CC\"]"; |
---|
| 137 | $script_criteria .= "address :"; |
---|
| 138 | break; |
---|
[6771] | 139 | case 'FROM': |
---|
[5539] | 140 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
| 141 | $script_criteria .= "address :"; |
---|
| 142 | break; |
---|
[6771] | 143 | case 'SIZE': |
---|
[5539] | 144 | $criteria[$j]['field'] = ''; |
---|
| 145 | $script_criteria .= "size :"; |
---|
| 146 | break; |
---|
[6771] | 147 | case 'SUBJECT': |
---|
[5539] | 148 | $criteria[$j]['field'] = "\"" . $criteria[$j]['field'] . "\""; |
---|
| 149 | $script_criteria .= "header :"; |
---|
[6771] | 150 | if($criteria[$j]['operator'] == "$") { |
---|
| 151 | $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "\", \"*" . base64_encode($criteria[$j]['value']) . ""; |
---|
| 152 | break; |
---|
| 153 | } |
---|
| 154 | if($criteria[$j]['operator'] == "^") { |
---|
| 155 | $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "*\", \"" . base64_encode($criteria[$j]['value']) . ""; |
---|
| 156 | break; |
---|
| 157 | } |
---|
| 158 | $criteria[$j]['value'] = "" . imap_8bit($criteria[$j]['value']) . "\", \"" . base64_encode($criteria[$j]['value']) . ""; |
---|
| 159 | break; |
---|
| 160 | case 'BODY': |
---|
[5539] | 161 | $criteria[$j]['field'] = ''; |
---|
| 162 | $script_criteria .= "body :"; |
---|
| 163 | $require_body = true; |
---|
| 164 | break; |
---|
[6771] | 165 | case 'VACATION': |
---|
[5601] | 166 | continue; |
---|
[6771] | 167 | case 'HASATTACHMENT': |
---|
[6348] | 168 | $criteria[$j]['field'] = ''; |
---|
| 169 | $script_criteria .= "body :"; |
---|
| 170 | $criteria[$j]['operator'] = "^^"; |
---|
| 171 | $require_body = true; |
---|
| 172 | break; |
---|
[5539] | 173 | default: |
---|
| 174 | $script_criteria .= "header :"; |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | switch ($criteria[$j]['operator']) { |
---|
| 179 | case '>': |
---|
| 180 | $criteria[$j]['operator'] = "over"; |
---|
[6001] | 181 | $criteria[$j]['value'] = $criteria[$j]['value'] . "K"; |
---|
[5539] | 182 | break; |
---|
| 183 | case '<': |
---|
| 184 | $criteria[$j]['operator'] = "under"; |
---|
[6001] | 185 | $criteria[$j]['value'] = $criteria[$j]['value'] . "K"; |
---|
[5539] | 186 | break; |
---|
| 187 | case '=': |
---|
| 188 | $criteria[$j]['operator'] = "is"; |
---|
| 189 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 190 | break; |
---|
| 191 | case '*': |
---|
| 192 | $criteria[$j]['operator'] = "contains"; |
---|
| 193 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 194 | break; |
---|
| 195 | case '^': |
---|
| 196 | $criteria[$j]['operator'] = "matches"; |
---|
| 197 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "*\"]"; |
---|
| 198 | $startswith = true; |
---|
| 199 | break; |
---|
[6348] | 200 | case '^^': |
---|
| 201 | $criteria[$j]['operator'] = "raw :matches"; |
---|
| 202 | $criteria[$j]['value'] = "[\"*filename=*\"]"; |
---|
| 203 | $startswith = true; |
---|
| 204 | break; |
---|
[5539] | 205 | case '$': |
---|
| 206 | $criteria[$j]['operator'] = "matches"; |
---|
| 207 | $criteria[$j]['value'] = "[\"*" . $criteria[$j]['value'] . "\"]"; |
---|
| 208 | $endswith = true; |
---|
| 209 | break; |
---|
| 210 | /* |
---|
| 211 | TO-DO: |
---|
[6193] | 212 | Inconsistência na regra do "não contém". |
---|
[5539] | 213 | */ |
---|
| 214 | case '!*': |
---|
| 215 | $criteria[$j]['operator'] = "contains"; |
---|
| 216 | $criteria[$j]['value'] = "[\"" . $criteria[$j]['value'] . "\"]"; |
---|
| 217 | break; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | if ($criteria[$j]['field'] == "" || $criteria[$j]['field'] == "\"subject\"" || $startswith || $endswith) |
---|
| 221 | { |
---|
| 222 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
| 223 | $startswith = $endswith = false; |
---|
| 224 | } |
---|
| 225 | else |
---|
| 226 | $script_criteria .= $criteria[$j]['operator'] . " " . $criteria[$j]['field'] . " " . $criteria[$j]['value'] . ", "; |
---|
| 227 | } |
---|
| 228 | $script_criteria = substr($script_criteria,0,-2); |
---|
[5629] | 229 | $script_criteria .= ")"; |
---|
| 230 | |
---|
[6754] | 231 | $action_addFlag = ''; |
---|
[5728] | 232 | |
---|
[5747] | 233 | if( is_array($action) ) |
---|
[5996] | 234 | foreach ($action as $k => $value) |
---|
[5539] | 235 | { |
---|
| 236 | switch ($action[$k]['type']) { |
---|
[6754] | 237 | case 'setflag': |
---|
| 238 | $require_flag = true; |
---|
| 239 | $action[$k]['parameter'] = "\\\\" . $action[$k]['parameter']; |
---|
| 240 | break; |
---|
| 241 | case 'addflag': |
---|
| 242 | $require_flag = true; |
---|
| 243 | $action_addFlag = "addflag \"" . $action[$k]['parameter'] . "\";\r\n "; |
---|
| 244 | break; |
---|
[5539] | 245 | case 'redirect': |
---|
| 246 | break; |
---|
| 247 | case 'reject': |
---|
| 248 | $require_reject = true; |
---|
| 249 | break; |
---|
| 250 | case 'fileinto': |
---|
| 251 | $require_fileinto = true; |
---|
[6377] | 252 | $action[$k]['parameter'] = mb_convert_encoding($action[$k]['parameter'], "UTF7-IMAP","UTF-8, ISO-8859-1, UTF7-IMAP"); |
---|
[5539] | 253 | break; |
---|
| 254 | case 'vacation': |
---|
| 255 | $require_vacation = true; |
---|
| 256 | $action[$k]['parameter'] = "\"" . $action[$k]['parameter'] . "\""; |
---|
[6213] | 257 | $vacation_action = ' :subject "Fora do Escritório" ' . $action[$k]['parameter'] . ";"; |
---|
[5539] | 258 | $vacation = true; |
---|
[5601] | 259 | continue; |
---|
[5539] | 260 | case 'discard': |
---|
| 261 | break; |
---|
| 262 | } |
---|
[6754] | 263 | if ($vacation == false && $action[$k]['type'] != 'addflag') $script_action .= $action[$k]['type'] . " \"" . $action[$k]['parameter'] . "\";\r\n "; |
---|
[5539] | 264 | } |
---|
[5629] | 265 | |
---|
[6754] | 266 | /* ATENÇÃO: Colocar sempre o comando addflag antes de qualquer outro no caso de ações compostas no Sieve */ |
---|
| 267 | if ($action_addFlag != '') $script_action = $action_addFlag . $script_action; |
---|
| 268 | |
---|
| 269 | $script_action = "{\r\n " . $script_action . "}"; |
---|
| 270 | $action_addFlag = ''; |
---|
[6163] | 271 | if($vacation == false) |
---|
[5629] | 272 | $script_rules .= $script_match . $script_criteria . $script_action . "\r\n"; |
---|
[5539] | 273 | |
---|
[5629] | 274 | if($data['id'] != "vacation") |
---|
| 275 | $i++; |
---|
[5539] | 276 | $script_match = ""; |
---|
[5862] | 277 | $script_criteria = ""; |
---|
[6754] | 278 | $script_action = ""; |
---|
[6824] | 279 | $data['applyMessages'] = ""; |
---|
| 280 | |
---|
| 281 | $verifyNextRule = $data['verifyNextRule']; |
---|
[5341] | 282 | } |
---|
[5539] | 283 | |
---|
| 284 | if($require_reject || $require_fileinto || $require_vacation || $require_body || $require_flag) |
---|
[5341] | 285 | { |
---|
[6348] | 286 | /* Para habilitar as funções desejadas, edite a diretiva sieve_extensions no arquivo de configuração "/etc/imapd.conf" */ |
---|
[5539] | 287 | $script_header .= "require ["; |
---|
| 288 | $require_reject ? $script_header .= "\"reject\", " : ""; |
---|
| 289 | $require_fileinto ? $script_header .= "\"fileinto\", " : ""; |
---|
| 290 | $require_vacation? $script_header .= "\"vacation\", " : ""; |
---|
| 291 | $require_flag ? $script_header .= "\"imapflags\", " : ""; |
---|
[6348] | 292 | $require_body ? $script_header .= "\"body\", " : ""; |
---|
[5539] | 293 | $script_header = substr($script_header,0,-2); |
---|
| 294 | $script_header .= "];\r\n"; |
---|
[5341] | 295 | } |
---|
| 296 | |
---|
[5629] | 297 | if( $vacation_action ) |
---|
[5601] | 298 | $script_rules .= "vacation" . $vacation_action . "\r\n"; |
---|
| 299 | |
---|
[6392] | 300 | foreach ($rules as &$values) { |
---|
| 301 | if($values['applyMessages']) |
---|
| 302 | $this->msgs_apply[] = $values['applyMessages']; |
---|
| 303 | $values['applyMessages'] = array(); |
---|
| 304 | } |
---|
| 305 | |
---|
[5539] | 306 | $json_data = json_encode($rules); |
---|
[6348] | 307 | $script_begin = "#Filtro gerado por Expresso Livre\r\n\r\n"; |
---|
[5539] | 308 | $content = $script_begin . $script_header . $script_rules . "\r\n\r\n#PseudoScript#" . "\r\n#" . $json_data; |
---|
[6754] | 309 | |
---|
[5539] | 310 | return( $content ); |
---|
[5341] | 311 | } |
---|
[5539] | 312 | |
---|
[5672] | 313 | /** |
---|
[6193] | 314 | * Método que lê e faz o parser dos filtros antigos |
---|
[5672] | 315 | * |
---|
| 316 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 317 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 318 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 319 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 320 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 321 | * @param <$scriptName> <Regras do usuário> |
---|
| 322 | * @return <Regra do usuário parseada> |
---|
[5672] | 323 | * @access <public> |
---|
| 324 | */ |
---|
| 325 | public function readOldScript($scriptName) |
---|
| 326 | { |
---|
[6193] | 327 | // Recebe o conteúdo do array; |
---|
[5672] | 328 | $lines = array(); |
---|
| 329 | $lines = preg_split("/\n/", $scriptName); |
---|
[5341] | 330 | |
---|
[5672] | 331 | // Pega o tamanho da regra na primeira do script; |
---|
| 332 | $size_rule = array_shift($lines); |
---|
| 333 | |
---|
| 334 | // Recebe o tamanho do script, pela primeira linha; |
---|
[5862] | 335 | //$this->size = trim($size_rule); |
---|
[5672] | 336 | |
---|
[6193] | 337 | // Verifica a composição do script; */ |
---|
[5862] | 338 | $line = array_shift($lines); |
---|
[5672] | 339 | |
---|
| 340 | // Variaveis para a regra e o campo ferias; |
---|
[5862] | 341 | $regexps = array('##PSEUDO', '#rule', '#vacation', '#mode'); |
---|
[5672] | 342 | $retorno['rule'] = array(); |
---|
| 343 | |
---|
| 344 | $line = array_shift($lines); |
---|
[5862] | 345 | while (isset($line)) { |
---|
[5672] | 346 | foreach ($regexps as $regp) { |
---|
[5862] | 347 | if (preg_match("/$regp/i", $line)) { |
---|
[5672] | 348 | // Recebe todas as regras criadas no servidor; |
---|
[5862] | 349 | if (preg_match("/#rule&&/i", $line)) { |
---|
| 350 | $retorno['rule'][] = ltrim($line) . "\n"; |
---|
[5672] | 351 | } |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | // Pega a proxima linha do sript; |
---|
| 355 | $line = array_shift($lines); |
---|
| 356 | } |
---|
| 357 | return $retorno; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | |
---|
[5539] | 363 | /** |
---|
[6193] | 364 | * Método que faz o parsing do Script Sieve, transformando em Array. |
---|
[5539] | 365 | * |
---|
| 366 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 367 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 368 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 369 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 370 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 371 | * @param <String> <$script> <Script Sieve com as regras do usuário> |
---|
| 372 | * @return <Regras do usuário em Array> |
---|
[5539] | 373 | * @access <public> |
---|
| 374 | */ |
---|
| 375 | public function parseSieveScript( $script ) |
---|
[5341] | 376 | { |
---|
[6363] | 377 | $old_rule = strripos($script, "##PSEUDO script start"); |
---|
| 378 | /* Tem regra antiga */ |
---|
[5672] | 379 | if($old_rule) { |
---|
[5862] | 380 | $parsed_rule = $this->readOldScript($script); |
---|
| 381 | $old_rules = array(); |
---|
[6363] | 382 | $i_return = 0; |
---|
[5862] | 383 | foreach ($parsed_rule['rule'] as $i => $value) { |
---|
[6363] | 384 | $array_rule = explode("&&", $parsed_rule['rule'][$i]); |
---|
[5862] | 385 | |
---|
[6363] | 386 | $action_type = array(); |
---|
| 387 | $action_parameter = array(); |
---|
| 388 | $criteria_value = array(); |
---|
| 389 | $criteria_operator = array(); |
---|
| 390 | $criteria_field = array(); |
---|
| 391 | |
---|
| 392 | $i_criteria = 0; |
---|
| 393 | $i_action = 0; |
---|
[5672] | 394 | |
---|
[6193] | 395 | /* TO-DO: Ver as actions possíveis além de reject e fileinto */ |
---|
[5862] | 396 | switch($array_rule[6]) { |
---|
| 397 | case 'reject': |
---|
[6363] | 398 | $action_type[$i_action] = 'reject'; |
---|
| 399 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 400 | $i_action++; |
---|
[5862] | 401 | break; |
---|
| 402 | case 'folder': |
---|
[6363] | 403 | $action_type[$i_action] = 'fileinto'; |
---|
| 404 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 405 | $i_action++; |
---|
[5862] | 406 | break; |
---|
[6363] | 407 | case 'flagged': |
---|
| 408 | $action_type[$i_action] = 'setflag'; |
---|
| 409 | $action_parameter[$i_action] = 'flagged'; |
---|
| 410 | $i_action++; |
---|
| 411 | break; |
---|
| 412 | case 'address': |
---|
| 413 | $action_type[$i_action] = 'redirect'; |
---|
| 414 | $action_parameter[$i_action] = $array_rule[7]; |
---|
| 415 | $i_action++; |
---|
| 416 | break; |
---|
| 417 | /* Somente para tratar casos em que a ação não é suportada */ |
---|
| 418 | default: |
---|
| 419 | $action_type[$i_action] = 'setflag'; |
---|
| 420 | $action_parameter[$i_action] = 'flagged'; |
---|
| 421 | $i_action++; |
---|
| 422 | break; |
---|
| 423 | // Recuperar o cyrus_delimiter para forçar um fileinto para INBOX/trash |
---|
| 424 | //case 'discard': |
---|
| 425 | //$action_type[$i_action] = 'fileinto'; |
---|
| 426 | //$action_parameter[$i_action] =; |
---|
| 427 | //$i_action++; |
---|
| 428 | //break; |
---|
[5862] | 429 | } |
---|
| 430 | if($array_rule[3] != "") { |
---|
[6363] | 431 | $criteria_value[$i_criteria] = $array_rule[3]; |
---|
| 432 | $criteria_operator[$i_criteria] = '='; |
---|
| 433 | $criteria_field[$i_criteria] = 'from'; |
---|
| 434 | $i_criteria++; |
---|
[5862] | 435 | } |
---|
[6363] | 436 | if($array_rule[4] != "") { |
---|
| 437 | $criteria_value[$i_criteria] = $array_rule[4]; |
---|
| 438 | $criteria_operator[$i_criteria] = '='; |
---|
| 439 | $criteria_field[$i_criteria] = 'to'; |
---|
| 440 | $i_criteria++; |
---|
| 441 | } |
---|
| 442 | if($array_rule[5] != "") { |
---|
| 443 | $criteria_value[$i_criteria] = $array_rule[5]; |
---|
| 444 | $criteria_operator[$i_criteria] = '='; |
---|
| 445 | $criteria_field[$i_criteria] = 'subject'; |
---|
| 446 | $i_criteria++; |
---|
[5862] | 447 | } |
---|
| 448 | $old_retorno = array(); |
---|
[6363] | 449 | $old_retorno['isExact'] = true; |
---|
| 450 | $old_retorno['name'] = 'regra_migrada_' . $array_rule[1]; |
---|
| 451 | |
---|
| 452 | $old_retorno['criteria'] = array(); |
---|
| 453 | foreach($criteria_value as $j => $value) { |
---|
| 454 | $old_retorno['criteria'][$j] = array(); |
---|
| 455 | $old_retorno['criteria'][$j]['value'] = $criteria_value[$j]; |
---|
| 456 | $old_retorno['criteria'][$j]['operator'] = $criteria_operator[$j]; |
---|
| 457 | $old_retorno['criteria'][$j]['field'] = $criteria_field[$j]; |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | $old_retorno['actions'] = array(); |
---|
| 461 | foreach($action_parameter as $j => $value) { |
---|
| 462 | $old_retorno['actions'][$j] = array(); |
---|
| 463 | $old_retorno['actions'][$j]['parameter'] = $action_parameter[$j]; |
---|
| 464 | $old_retorno['actions'][$j]['type'] = $action_type[$j]; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | $old_retorno['enabled'] = ($array_rule[2] == 'ENABLED') ? 'true' : 'false'; |
---|
| 468 | $old_retorno['id'] = 'Regra_migrada_' . $i_return; |
---|
| 469 | $old_retorno['applyMessages'] = ''; |
---|
[5862] | 470 | |
---|
[6363] | 471 | $old_rules[$i_return] = $old_retorno; |
---|
| 472 | $i_return++; |
---|
[5862] | 473 | } |
---|
| 474 | return $old_rules; |
---|
[5952] | 475 | } |
---|
[6193] | 476 | /* Não tem regra antiga */ |
---|
[5539] | 477 | $pos = strripos($script, "#PseudoScript#"); |
---|
| 478 | $pseudo_script = substr( $script, $pos+17 ); |
---|
[5341] | 479 | |
---|
[5539] | 480 | $return = json_decode( $pseudo_script, true ); |
---|
[5862] | 481 | |
---|
[5539] | 482 | return $return; |
---|
| 483 | } |
---|
[5341] | 484 | |
---|
[5539] | 485 | var $rules = false; |
---|
[5341] | 486 | |
---|
[5539] | 487 | /** |
---|
| 488 | * Construtor da classe. |
---|
| 489 | * |
---|
| 490 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 491 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 492 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 493 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 494 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 495 | * @access <public> |
---|
| 496 | */ |
---|
| 497 | public function __construct() |
---|
| 498 | { |
---|
| 499 | $this->service = Controller::service("Sieve"); |
---|
| 500 | } |
---|
[5341] | 501 | |
---|
[5539] | 502 | |
---|
| 503 | /** |
---|
[6193] | 504 | * Método que recupera as regras do usuário. |
---|
[5539] | 505 | * |
---|
| 506 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 507 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 508 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 509 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 510 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[6193] | 511 | * @return <Regras do usuário> |
---|
[5539] | 512 | * @access <public> |
---|
| 513 | */ |
---|
| 514 | public function getRules() |
---|
| 515 | { |
---|
| 516 | $this->rules = Controller::find( array( 'concept' => 'filter' ) ); |
---|
[5341] | 517 | |
---|
[5539] | 518 | if( !$this->rules ) { |
---|
| 519 | $this->rules = array(); |
---|
| 520 | } |
---|
| 521 | return( $this->rules ); |
---|
| 522 | } |
---|
[5341] | 523 | |
---|
[5539] | 524 | |
---|
| 525 | /** |
---|
[6193] | 526 | * Método que aplica o filtro para as mensagens do usuário. |
---|
[5539] | 527 | * |
---|
| 528 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 529 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 530 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 531 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 532 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 533 | * @param <$uri> |
---|
| 534 | * @param <$result> |
---|
| 535 | * @param <$criteria> |
---|
| 536 | * @param <$original> |
---|
| 537 | * @access <public> |
---|
| 538 | */ |
---|
| 539 | public function applySieveFilter( &$uri , &$result , &$criteria , $original ) |
---|
| 540 | { |
---|
[6392] | 541 | $rule_apply = array(); |
---|
[5862] | 542 | |
---|
[6392] | 543 | $filter = Controller::read($uri); |
---|
| 544 | $filter_ = $this->parseSieveScript($filter['content']); |
---|
| 545 | |
---|
| 546 | foreach ($filter_ as $f_) { |
---|
| 547 | if($f_['id'] == $uri['id']) { |
---|
| 548 | $rule_apply = $f_; |
---|
[5692] | 549 | } |
---|
[6392] | 550 | } |
---|
| 551 | |
---|
| 552 | $actions = array(); |
---|
| 553 | $actions['type'] = $rule_apply['actions'][0]['type']; |
---|
| 554 | $actions['parameter'] = $rule_apply['actions'][0]['parameter']; |
---|
| 555 | /* Hardcoded */ |
---|
| 556 | $actions['keep'] = false; |
---|
[5862] | 557 | |
---|
[6392] | 558 | //$messages = $rule_apply['applyMessages']; |
---|
| 559 | $messages = $this->msgs_apply[0]; |
---|
| 560 | $this->msgs_apply = array(); |
---|
| 561 | |
---|
| 562 | $imap = Controller::service( 'Imap' ); |
---|
| 563 | $imap->apliSieveFilter($messages, $actions); |
---|
| 564 | return $result; |
---|
[5539] | 565 | } |
---|
[5341] | 566 | |
---|
[5539] | 567 | |
---|
| 568 | /** |
---|
[6193] | 569 | * Método que lê o script do usuário. |
---|
[5539] | 570 | * |
---|
| 571 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 572 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 573 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 574 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 575 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 576 | * @param <$uri> |
---|
| 577 | * @param <$result> |
---|
| 578 | * @param <$criteria> |
---|
| 579 | * @param <$original> |
---|
[6193] | 580 | * @return <Script do usuário> |
---|
[5539] | 581 | * @access <public> |
---|
| 582 | */ |
---|
| 583 | public function readUserScript( &$uri , &$params , &$criteria , $original ) |
---|
| 584 | { |
---|
| 585 | $uri['id'] = $this->service->config['user']; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | /** |
---|
[6193] | 590 | * Método que seta o script do usuário. |
---|
[5539] | 591 | * |
---|
| 592 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 593 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 594 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 595 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 596 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 597 | * @param <$uri> |
---|
| 598 | * @param <$result> |
---|
| 599 | * @param <$criteria> |
---|
| 600 | * @param <$original> |
---|
[6193] | 601 | * @return <Script do usuário> |
---|
[5539] | 602 | * @access <public> |
---|
| 603 | */ |
---|
| 604 | public function setRule( &$uri , &$params , &$criteria , $original ) |
---|
[5601] | 605 | { |
---|
[5630] | 606 | if( !$this->rules ) |
---|
[6392] | 607 | $this->rules = $this->getRules(); |
---|
[5341] | 608 | |
---|
[5601] | 609 | $uri['id'] = $params['id'] = isset($params['id']) ? $params['id'] : urlencode($params['name']); |
---|
[5341] | 610 | |
---|
[5601] | 611 | $i = 0; |
---|
[5341] | 612 | |
---|
[5629] | 613 | for( ; isset($this->rules[$i]) && $this->rules[$i]['id'] !== $params['id']; $i++ ); |
---|
[5601] | 614 | |
---|
[5629] | 615 | $this->rules[$i] = array_merge( ( isset($this->rules[$i]) ? $this->rules[$i] : array() ), $params ); |
---|
[5601] | 616 | |
---|
| 617 | $params = array( 'name' => $this->service->config['user'], |
---|
| 618 | 'content' => $this->formatSieveScript( $this->rules ), |
---|
| 619 | 'active' => true ); |
---|
[5539] | 620 | } |
---|
[5341] | 621 | |
---|
[5539] | 622 | |
---|
| 623 | /** |
---|
[6193] | 624 | * Método que deleta o script do usuário. |
---|
[5539] | 625 | * |
---|
| 626 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 627 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 628 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 629 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 630 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 631 | * @param <$uri> |
---|
| 632 | * @param <$result> |
---|
| 633 | * @param <$criteria> |
---|
| 634 | * @param <$original> |
---|
| 635 | * @access <public> |
---|
| 636 | */ |
---|
| 637 | public function deleteRule( &$uri, &$params, &$criteria, $original ) |
---|
| 638 | { |
---|
[5601] | 639 | if( !$this->rules ) { |
---|
| 640 | $this->rules = $this->getRules(); |
---|
| 641 | } |
---|
[5539] | 642 | $params['id'] = $uri['id']; |
---|
[5341] | 643 | |
---|
[5601] | 644 | $rules = array(); |
---|
[5341] | 645 | |
---|
[5539] | 646 | foreach( $this->rules as $i => $rule ) |
---|
[5601] | 647 | if( $rule['id'] !== $uri['id'] ) |
---|
| 648 | $rules[] = $this->rules[$i]; |
---|
[5341] | 649 | |
---|
[5601] | 650 | $this->rules = $rules; |
---|
| 651 | |
---|
[5539] | 652 | $uri['id'] = ''; |
---|
[5341] | 653 | |
---|
[5539] | 654 | $params = array( 'name' => $this->service->config['user'], |
---|
| 655 | 'content' => $this->formatSieveScript( $this->rules ), |
---|
| 656 | 'active' => true ); |
---|
[5341] | 657 | |
---|
[5539] | 658 | $URI = Controller::URI( $uri['concept'], $this->service->config['user'] ); |
---|
| 659 | $this->service->update( $URI, $params ); |
---|
[5601] | 660 | |
---|
[5539] | 661 | return( false ); |
---|
| 662 | } |
---|
[5341] | 663 | |
---|
[5539] | 664 | |
---|
| 665 | /** |
---|
[6193] | 666 | * Método que pega o script do usuário. |
---|
[5539] | 667 | * |
---|
| 668 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 669 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 670 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 671 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 672 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 673 | * @param <$uri> |
---|
| 674 | * @param <$result> |
---|
| 675 | * @param <$criteria> |
---|
| 676 | * @param <$original> |
---|
[6193] | 677 | * @return <Script do usuário> |
---|
[5539] | 678 | * @access <public> |
---|
| 679 | */ |
---|
| 680 | public function getSieveRule( &$uri , &$params , &$criteria , $original ) |
---|
| 681 | { |
---|
| 682 | $script = $this->parseSieveScript( $params['content'] ); |
---|
[5341] | 683 | |
---|
[5539] | 684 | foreach( $script as $i => $rule ) |
---|
[5747] | 685 | if(is_array ($rule['name']) && is_array($original['id'])) |
---|
[5539] | 686 | if( $rule['name'] === $original['id'] ) |
---|
| 687 | return( $params = $rule ); |
---|
| 688 | } |
---|
[5341] | 689 | |
---|
[5539] | 690 | |
---|
| 691 | /** |
---|
[6193] | 692 | * Método que lista as regras do usuário. |
---|
[5539] | 693 | * |
---|
| 694 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
[6193] | 695 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 696 | * @sponsor Caixa Econômica Federal |
---|
[5862] | 697 | * @author Airton Bordin Junior <airton@prognus.com.br> |
---|
| 698 | * @author Gustavo Pereira dos Santos <gustavo@prognus.com.br> |
---|
[5539] | 699 | * @param <$uri> |
---|
| 700 | * @param <$result> |
---|
| 701 | * @param <$criteria> |
---|
| 702 | * @param <$original> |
---|
[6193] | 703 | * @return <Regras do usuário> |
---|
[5539] | 704 | * @access <public> |
---|
| 705 | */ |
---|
| 706 | public function listSieveRules( &$uri , &$params , &$criteria , $original ) |
---|
[5629] | 707 | { |
---|
[5539] | 708 | $return = $params = $this->parseSieveScript( $params[0]['content'] ); |
---|
| 709 | return( $return ); |
---|
| 710 | } |
---|
[6754] | 711 | |
---|
| 712 | /** |
---|
| 713 | * Método que insere no ldap as informações do vacation |
---|
| 714 | * |
---|
| 715 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 716 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 717 | * @sponsor Caixa Econômica Federal |
---|
| 718 | * @author Cristiano Corrêa Schmidt |
---|
| 719 | * @param <$uri> |
---|
| 720 | * @param <$result> |
---|
| 721 | * @param <$criteria> |
---|
| 722 | * @param <$original> |
---|
| 723 | * @return <void> |
---|
| 724 | * @access public |
---|
| 725 | */ |
---|
| 726 | public function verifyVacationRule( &$uri , &$params , &$criteria , $original ) |
---|
| 727 | { |
---|
| 728 | if( $original['properties']['id'] === 'vacation' ) |
---|
| 729 | { |
---|
| 730 | |
---|
| 731 | $user = Controller::read(array('concept' => 'user' , 'id' => config::me('uidNumber') , 'service' => 'OpenLDAP')); |
---|
| 732 | $ldapConf = Config::service('OpenLDAP', 'config'); |
---|
| 733 | $con = ldap_connect( $ldapConf['host'] ); |
---|
| 734 | ldap_set_option( $con,LDAP_OPT_PROTOCOL_VERSION, 3 ); |
---|
| 735 | ldap_bind( $con, $ldapConf['user'], $ldapConf['password']); |
---|
| 736 | |
---|
| 737 | $info = array(); |
---|
| 738 | if(!in_array('Vacation', $user['objectClass'])) |
---|
| 739 | $info['objectClass'] = 'Vacation'; |
---|
| 740 | |
---|
| 741 | $info['vacationActive'] = strtoupper($original['properties']['enabled']); |
---|
| 742 | |
---|
| 743 | if(isset($original['properties']['actions']) && isset($original['properties']['actions'][0]['parameter'])) |
---|
| 744 | $info['vacationInfo'] = $original['properties']['actions'][0]['parameter']; |
---|
| 745 | else if( !isset($user['vacationInfo']) ) |
---|
| 746 | { |
---|
| 747 | $rules = $this->getRules(); |
---|
| 748 | if(is_array($rules)) |
---|
| 749 | foreach ($rules as $rule) |
---|
| 750 | if($rule['id'] === 'vacation') |
---|
| 751 | $info['vacationInfo'] = $rule['actions'][0]['parameter']; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | if(!in_array('Vacation', $user['objectClass'])) |
---|
| 755 | ldap_mod_add ( $con , $user['dn'] , $info ); |
---|
| 756 | else |
---|
| 757 | ldap_modify ( $con , $user['dn'] , $info ); |
---|
| 758 | |
---|
| 759 | |
---|
| 760 | ldap_close($con); |
---|
| 761 | |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | } |
---|
| 765 | |
---|
| 766 | /** |
---|
| 767 | * Método que remove do ldap as informações do vacation |
---|
| 768 | * |
---|
| 769 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 770 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 771 | * @sponsor Caixa Econômica Federal |
---|
| 772 | * @author Cristiano Corrêa Schmidt |
---|
| 773 | * @param <$uri> |
---|
| 774 | * @param <$result> |
---|
| 775 | * @param <$criteria> |
---|
| 776 | * @param <$original> |
---|
| 777 | * @return <void> |
---|
| 778 | * @access public |
---|
| 779 | */ |
---|
| 780 | public function deleteVacationRule( &$uri , &$params , &$criteria , $original ) |
---|
| 781 | { |
---|
| 782 | if( $original['URI']['id'] === 'vacation' ) |
---|
| 783 | { |
---|
| 784 | $user = Controller::read(array('concept' => 'user' , 'id' => config::me('uidNumber') , 'service' => 'OpenLDAP')); |
---|
| 785 | $ldapConf = Config::service('OpenLDAP', 'config'); |
---|
| 786 | $con = ldap_connect( $ldapConf['host'] ); |
---|
| 787 | ldap_set_option( $con,LDAP_OPT_PROTOCOL_VERSION, 3 ); |
---|
| 788 | ldap_bind( $con, $ldapConf['user'], $ldapConf['password']); |
---|
| 789 | $info = array(); |
---|
| 790 | $info['vacationActive'] = 'FALSE'; |
---|
| 791 | $info['vacationInfo'] = ""; |
---|
| 792 | ldap_modify ( $con , $user['dn'] , $info ); |
---|
| 793 | ldap_close($con); |
---|
| 794 | } |
---|
| 795 | } |
---|
[5539] | 796 | } |
---|