SieveS = new SieveS(); //$this->scriptfile = $GLOBALS['HTTP_SESSION_VARS']['phpgw_info']['expressomail']['user']['account_lid']; $this->scriptfile = $_SESSION['phpgw_info']['expressomail']['user']['account_lid']; $this->username = $this->scriptfile; $this->reply = ""; $this->rules = ""; $this->errstr = ""; $this->size = ""; $this->continuebit = 1; $this->sizebit = 2; $this->anyofbit = 4; $this->keepbit = 8; $this->regexbit = 128; } function init_a(){ //Abre a conexão $this->SieveS->start(); $this->reply = $this->SieveS->getscript(); if(!$this->reply){ $aux = $this->SieveS->putscript($this->scriptfile, $this->createScript()); if(!$aux){ // Caso de erro, grava dentro da variável errstr; $this->errstr = "O arquivo não foi criado"; } // Mata a variavel; unset($aux); // Ativa o script; $aux = $this->SieveS->activatescript($this->scriptfile); if(!$aux){ // Caso de erro, grava dentro da variavel errstr; $this->errstr = "O arquivo não foi ativado"; } else{ $this->reply = $this->SieveS->getscript(); $this->rules = $this->readScript($this->reply); } }else{ $this->rules = $this->readScript($this->reply); } //Fecha a conexao $this->SieveS->close(); if($this->rules){ return $this->rules; }else{ return $this->errstr; } } function rec_rules($params){ $newr1 = array(); $newr2 = array(); $newr3 = array(); $var_decode = rawurldecode($params['arfilter']); $narray = explode("_end_",$var_decode); foreach($narray as $key=>$tmp){ if($tmp != ""){ $newr1[] = $tmp; } } unset($key); unset($tmp); foreach($newr1 as $key=>$tmp){ $tmp2 = explode("_begin_##", $tmp); foreach($tmp2 as $tmp3){ if($tmp3 != ""){ $newr2[] = trim($tmp3); } } } unset($tmp); unset($tmp2); unset($tmp3); foreach($newr2 as $tmp){ if(trim($tmp) != ""){ $tmp2 = explode("##",$tmp); foreach($tmp2 as $tmp3){ $tmp4 .= trim($tmp3) . "&&"; } $newr3[] = substr($tmp4,0,(strlen($tmp4)-4)); unset($tmp2); unset($tmp3); unset($tmp4); } } $tmp = $newr3[count($newr3)-1]; if(substr($tmp,0,9) == "#vacation"){ $this->newout = array_pop($newr3); foreach($newr3 as $key=>$tmp){ $this->newrules[] = $tmp; } }else{ foreach($newr3 as $tmp){ $this->newrules[] = $tmp; } } unset($tmp); $tmp = explode("&&",$this->newout); $tmp1 = explode(",",$tmp[2]); foreach($tmp1 as $key=>$tmp2){ $tmp3 .= stripslashes(trim($tmp2)).", "; } $tmp3 = substr($tmp3,0,(strlen($tmp3)-2)); unset($tmp); unset($tmp1); unset($tmp2); unset($key); $tmp = explode("&&",$this->newout); foreach($tmp as $key=>$tmp1){ if($key == 2){ $tmp2 .= trim($tmp3) . "&&"; }else{ $tmp2 .= trim($tmp1) . "&&"; } } unset($this->newout); $this->newout = substr($tmp2,0,(strlen($tmp2)-2)); //Abre a conexao $this->SieveS->start(); $this->errstr = ""; // Escreve a nova regra; $this->reply = $this->SieveS->getscript(); if($this->reply){ $this->errstr = $this->SieveS->deletescript($this->scriptfile); } //Escreve a(s) nova(s) regra(s); $this->errstr = $this->SieveS->putscript($this->scriptfile,$this->write_rule()); //Ativa o script; $this->errstr = $this->SieveS->activatescript($this->scriptfile); //Fecha a conexao $this->SieveS->close(); if($this->errstr){ return "Ok"; }else{ return "Problemas na criação do arquivo!"; } } function convert_specialchar($input){ $special_char = false; for ( $i = 0; $i < strlen($input); $i++ ) if ( preg_match('/[ÁáÉéÍíÓóÚúÀàÈèÌìÒòÙùÇçÃãÕõÂâÊêÔô®©§ªºÐ¹²³°¢¿Æ£µøæß«»Ø÷±¬]/', $input[$i]) ) { $special_char = true; $input = preg_replace('/'.$input[$i].'/', '=' . bin2hex($input[$i]), $input); } if ( $special_char ) { /* When using arrays with pattern and replacement, the keys are processed in the order they appear in the array. See preg_replace in php.net/preg_replace */ $patterns[0] = '/=c3/i'; $patterns[1] = '/ /'; $replacements[1] = ''; $replacements[0] = '_'; $input = preg_replace($patterns, $replacements, $input); } if (($input{0}) && (!eregi('^[a-zA-Z0-9=-]+$',$input))) return false; else return($input); } // Grava a nova regra; function write_rule(){ // Variaveis; $rule = array(); $vacation = array(); $newruletext = ""; $activerules = 0; $regexused = 0; $rejectused = 0; $newscriptbody = ""; $continue = 1; // Recebe os valores das regras; foreach($this->newrules as $tmp){ $tmp1 = explode("&&",$tmp); $rule['priority'] = $tmp1[1]; $rule['status'] = $tmp1[2]; $rule['from'] = $this->convert_specialchar($tmp1[3]); $rule['to'] = $this->convert_specialchar($tmp1[4]); $rule['subject'] = $this->convert_specialchar($tmp1[5]); $rule['action'] = $tmp1[6]; $rule['action_arg'] = utf8_encode(preg_replace("/\\r\\n/","\r\n",$tmp1[7])); $rule['flg'] = $tmp1[8]; $rule['field'] = $tmp1[9]; $rule['field_val'] = $tmp1[10]; $rule['size'] = $tmp1[11]; $rule['continue'] = ($tmp1[8] & $this->continuebit); $rule['gthan'] = ($tmp1[8] & $this->sizebit); $rule['anyof'] = ($tmp1[8] & $this->anyofbit); $rule['keep'] = ($tmp1[8] & $this->keepbit); $rule['regexp'] = ($tmp1[8] & $this->regexbit); $rule['unconditional'] = 0; if (!$rule['from'] && !$rule['to'] && !$rule['subject'] && !$rule['field'] && !$rule['size'] && $rule['action']){ $rule['unconditional'] = 1; } unset($tmp1); // Monta as regras; if ($rule['status'] != 'ENABLED') { } else { $activerules = 1; // Condições para montagem das regras; $anyall = "allof"; if ($rule['anyof']) $anyall = "anyof"; if ($rule['regexp']) { $regexused = 1; } $started = 0; if (!$rule['unconditional']) { if (!$continue) $newruletext .= "els"; $newruletext .= "if " . $anyall . " ("; if ($rule['from']) { if (preg_match("/^\s*!/", $rule['from'])){ $newruletext .= 'not '; $rule['from'] = preg_replace("/^\s*!/","",$rule['from']); } $match = ':contains'; if (preg_match("/\*|\?/", $rule['from'])) $match = ':matches'; if ($rule['regexp']) $match = ':regex'; $newruletext .= "header " . $match . " [\"From\"]"; $newruletext .= " \"" . $rule['from'] . "\""; $started = 1; } if ($rule['to']) { if ($started) $newruletext .= ", "; if (preg_match("/^\s*!/", $rule['to'])){ $newruletext .= 'not '; $rule['to'] = preg_replace("/^\s*!/","",$rule['to']); } $match = ':contains'; if (preg_match("/\*|\?/", $rule['to'])) $match = ':matches'; if ($rule['regexp']) $match = ':regex'; $newruletext .= "address " . $match . " [\"To\",\"TO\",\"Cc\",\"CC\"]"; $newruletext .= " \"" . $rule['to'] . "\""; $started = 1; } if ($rule['subject']) { if ($started) $newruletext .= ", "; if (preg_match("/^\s*!/", $rule['subject'])){ $newruletext .= 'not '; $rule['subject'] = preg_replace("/^\s*!/","",$rule['subject']); } $match = ':contains'; if (preg_match("/\*|\?/", $rule['subject'])) $match = ':matches'; if ($rule['regexp']) $match = ':regex'; $newruletext .= "header " . $match . " \"subject\""; $newruletext .= " \"" . $rule['subject'] . "\""; $started = 1; } if ($rule['field'] && $rule['field_val']) { if ($started) $newruletext .= ", "; if (preg_match("/^\s*!/", $rule['field_val'])){ $newruletext .= 'not '; $rule['field_val'] = preg_replace("/^\s*!/","",$rule['field_val']); } $match = ':contains'; if (preg_match("/\*|\?/", $rule['field_val'])) $match = ':matches'; if ($rule['regexp']) $match = ':regex'; $newruletext .= "header " . $match . " \"" . $rule['field'] . "\""; $newruletext .= " \"" . $rule['field_val'] . "\""; $started = 1; } if ($rule['size']) { $xthan = " :under "; if ($rule['gthan']) $xthan = " :over "; if ($started) $newruletext .= ", "; $newruletext .= "size " . $xthan . $rule['size'] . "K"; $started = 1; } } // Ações if (!$rule['unconditional']) $newruletext .= ") {\n\t"; if (preg_match("/folder/i",$rule['action'])) { $newruletext .= "fileinto \"" . $rule['action_arg'] . "\";"; } if (preg_match("/reject/i",$rule['action'])) { $newruletext .= "reject text: \n" . $rule['action_arg'] . "\n.\n;"; $rejectused = 1; } if (preg_match("/address/i",$rule['action'])) { $newruletext .= "redirect \"" . $rule['action_arg'] . "\";"; } if (preg_match("/discard/i",$rule['action'])) { $newruletext .= "discard;"; } if ($rule['keep']) $newruletext .= "\n\tfileinto \"INBOX\";"; //if ($rule['keep']) $newruletext .= "\n\tkeep;"; if (!$rule['unconditional']) $newruletext .= "\n}"; $continue = 0; if ($rule['continue']) $continue = 1; if ($rule['unconditional']) $continue = 1; $newscriptbody .= $newruletext . "\n\n"; unset($newruletext); } }// Fim do Foreach; // Para a regras fora do escritorio; unset($tmp); if($this->newout != ""){ $aux = explode("&&",$this->newout); $vacation['days'] = $aux[1]; $vacation['addresses'] = $aux[2]; $vacation['text'] = preg_replace("/\\\\n/","\r\n",$aux[3]); $vacation['status'] = $aux[4]; } // Monta a regra para fora do escritorio; if ($vacation['status'] == 'on') { $newscriptbody .= "vacation :days " . $vacation['days'] . " :addresses ["; $newscriptbody .= $vacation['addresses']; $newscriptbody .= "] text:\n" . utf8_encode($vacation['text']) . "\n.\n;\n\n"; } // Cria o cabeçalho do arquivo; $newscripthead = ""; $newscripthead .= "#Mail filter rules for " .$this->username . "\n"; $newscripthead .= '#Generated by ' . $this->username . ' using Expressomail '; $newscripthead .= "\n"; // Continuação do cabeçalho do arquivo; if ($activerules) { $newscripthead .= "require [\"fileinto\""; if($regexused){ $newscripthead .= ",\"regex\""; } if($rejectused){ $newscripthead .= ",\"reject\""; } if($this->newout && $vacation['status'] == 'on'){ $newscripthead .= ",\"vacation\""; } $newscripthead .= "];\n\n"; }else{ if($vacation && $vacation['status'] == 'on'){ $newscripthead .= "require [\"vacation\"];\n\n"; } } //preg_replace("/[\\n\\r]/","", $p); // Cria o rodapé do arquivo; $newscriptfoot = ""; $newscriptfoot .= "##PSEUDO script start\n"; // Lê as regras; foreach($this->newrules as $tmp){ $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$tmp). "\n"; } // Lê as regras fora do escritório; if($this->newout != ""){ $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$this->newout)."\n"; } $newscriptfoot .= "#mode&&basic\n"; $newscript = $newscripthead . $newscriptbody . $newscriptfoot; // Destroi as variaveis; unset($rule); unset($vacation); unset($activerules); unset($regexused); unset($rejectused); unset($newscripthead); unset($newscriptbody); unset($newscriptfoot); unset($continue); unset($this->newrules); unset($this->newout); // Retorna o script construido; return $newscript; }// Fim da Função // Cria o script sieve, caso nao possua; function createScript(){ // Cria o cabeçalho do arquivo; $newScriptHead = ""; $newScriptHead .= "#Mail filter rules for " .$this->username . "\n"; $newScriptHead .= '#Generated by ' . $this->username . ' using ExpressoMail '; $newScriptHead .= "\n"; //Cria o rodapé do arquivo; $newScriptFoot = ""; $newScriptFoot .= "##PSEUDO Script Start\n"; $newScriptFoot .= "#mode&&basic\n"; //Para passar para o arquivo; $newScript = $newScriptHead . $newScriptFoot; return $newScript; } //Lê o conteúdo do script; function readScript($scriptName){ // Verifica se a conexão foi bem sucedida; if(!$scriptName){ $this->errstr = "Não foi possível conectar com o Servidor"; return "false 2"; } // Recebe o conteúdo do array; $lines = array(); $lines = preg_split("/\n/",$scriptName); // Pega o tamanho da regra na primeira do script; $size_rule = array_shift($lines); // Recebe o tamanho do script, pela primeira linha; $this->size = trim($size_rule); // Verifica a composição do script; $line = array_shift($lines); if(!preg_match("/^# ?Mail(.*)rules for/", $line)){ $this->errstr = "Formato nao reconhecido"; return false; } // Variaveis para a regra e o campo ferias; $regexps = array('^ *##PSEUDO','^ *#rule','^ *#vacation','^ *#mode'); $retorno['rule'] = array(); $retorno['vacation'] = array(); $retorno['mode'] = array(); $line = array_shift($lines); while (isset($line)){ foreach($regexps as $regp){ if(preg_match("/$regp/i",$line)){ // Recebe todas as regras criadas no servidor; if(preg_match("/^ *#rule&&/i",$line)){ $retorno['rule'][] = $line . "\n"; } if(preg_match("/^ *#vacation&&/i",$line)){ $retorno['vacation'][] = $line . "\n"; } if(preg_match("/^ *#mode&&(.*)/i",$line)){ $retorno['mode'][]= $line . "\n"; } } } // Pega a proxima linha do sript; $line = array_shift($lines); } return $retorno; } }//Fim da Classe ?>