Ignore:
Timestamp:
03/11/11 09:23:24 (13 years ago)
Author:
alexandrecorreia
Message:

Ticket #1650 - Filtros de tamanho de mensagem maior que zero nao sao criados.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/expressoMail1_2/inc/class.ScriptS.inc.php

    r3849 r3859  
    11<?php 
     2 
    23//Inclui o arquivo contendo a classe SieveS; 
    34include('class.SieveS.inc.php'); 
    45 
    56class ScriptS { 
    6          
     7 
    78        //Declaração de Variáveis; 
    8         var $SieveS;                                    // Var para criação do objeto; 
    9         var $reply;                                             // Var para resposta; 
    10         var $scriptfile;                                // Nome do script; 
    11         var $username;                                  // Nome do usuario; 
    12         var $rules;                                             // Regras do sieve; 
    13         var $errstr;                                    // Erros retornados; 
    14         var $size;                                              // Tamanho; 
    15         var $so;                                                // Verifica se a regra foi criada por outro tipo serviço de filtros; 
     9        var $SieveS;      // Var para criação do objeto; 
     10        var $reply;             // Var para resposta; 
     11        var $scriptfile;         // Nome do script; 
     12        var $username;    // Nome do usuario; 
     13        var $rules;             // Regras do sieve; 
     14        var $errstr;      // Erros retornados; 
     15        var $size;              // Tamanho; 
     16        var $so;                // Verifica se a regra foi criada por outro tipo serviço de filtros; 
    1617        var $continuebit; 
    1718        var $sizebit; 
     
    2425        var $EmailVoip; 
    2526        var $EmailExpresso; 
    26          
    27         function ScriptS(){ 
     27 
     28        function ScriptS() { 
    2829 
    2930                //Cria o objeto; 
    30                 $this->SieveS     = new SieveS(); 
    31                  
     31                $this->SieveS = new SieveS(); 
     32 
    3233                //$this->scriptfile = $GLOBALS['HTTP_SESSION_VARS']['phpgw_info']['expressomail']['user']['account_lid']; 
    3334                $this->scriptfile = $_SESSION['phpgw_info']['expressomail']['user']['account_lid']; 
    34                 $this->username   = $this->scriptfile; 
    35                  
    36                 $this->reply      = ""; 
    37                 $this->rules      = "";  
    38                 $this->errstr     = ""; 
    39                 $this->size               = ""; 
    40                  
    41                 $this->continuebit      = 1; 
    42         $this->sizebit          = 1; 
    43         $this->anyofbit         = 4; 
    44         $this->keepbit          = 8; 
    45         $this->regexbit         = 128; 
    46          
    47         $this->EmailVoip = trim($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_email_redirect']); 
    48         $this->EmailExpresso = trim($_SESSION['phpgw_info']['expressomail']['user']['email']); 
    49          
     35                $this->username = $this->scriptfile; 
     36 
     37                $this->reply = ""; 
     38                $this->rules = ""; 
     39                $this->errstr = ""; 
     40                $this->size = ""; 
     41 
     42                $this->continuebit = 1; 
     43                $this->sizebit = 1; 
     44                $this->anyofbit = 4; 
     45                $this->keepbit = 8; 
     46                $this->regexbit = 128; 
     47 
     48                $this->EmailVoip = trim($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_email_redirect']); 
     49                $this->EmailExpresso = trim($_SESSION['phpgw_info']['expressomail']['user']['email']); 
    5050        } 
    51          
    52         function init_a(){ 
    53                  
     51 
     52        function init_a() { 
     53 
    5454                //Abre a conexão 
    5555                $this->SieveS->start(); 
    56                  
     56 
    5757                $this->reply = $this->SieveS->getscript(); 
    58                  
    59                 if(!$this->reply){ 
    60          
     58 
     59                if (!$this->reply) { 
     60 
    6161                        $aux = $this->SieveS->putscript($this->scriptfile, $this->createScript()); 
    62                         if(!$aux){ 
     62                        if (!$aux) { 
    6363                                // Caso de erro, grava dentro da variável errstr; 
    64                                 $this->errstr = "Error: file not created";  
    65                                 return $this->errstr;  
     64                                $this->errstr = "Error: file not created"; 
     65                                return $this->errstr; 
    6666                        } 
    6767                        // Mata a variavel; 
     
    6969                        // Ativa o script; 
    7070                        $aux = $this->SieveS->activatescript($this->scriptfile); 
    71                          
    72                         if(!$aux){ 
     71 
     72                        if (!$aux) { 
    7373                                // Caso de erro, grava dentro da variavel errstr; 
    74                                 $this->errstr = "Error: error to activate file";  
    75                                 return $this->errstr;  
    76                         } 
    77                         else{ 
     74                                $this->errstr = "Error: error to activate file"; 
     75                                return $this->errstr; 
     76                        } else { 
    7877                                $this->reply = $this->SieveS->getscript(); 
    7978                                $this->rules = $this->readScript($this->reply); 
    8079                        } 
    81                 }else{ 
     80                } else { 
    8281                        $this->rules = $this->readScript($this->reply); 
    83                 }                
     82                } 
    8483 
    8584                //Fecha a conexao 
    8685                $this->SieveS->close(); 
    87                          
    88                 if($this->rules) 
     86 
     87                if ($this->rules) 
    8988                        return $this->rules; 
    90                          
    9189        } 
    92          
    93         function rec_rules($params){ 
    94                  
    95                 $newr1  = array(); 
    96                 $newr2  = array(); 
    97                 $newr3  = array(); 
    98          
     90 
     91        function rec_rules($params) { 
     92 
     93                $newr1 = array(); 
     94                $newr2 = array(); 
     95                $newr3 = array(); 
     96 
    9997                $var_decode = rawurldecode($params['arfilter']); 
    10098                $var_decode = preg_replace('/\n\./', '.', $var_decode); 
    101                  
    102                 $narray  = explode("_end_",$var_decode); 
    103  
    104                 foreach($narray as $key=>$tmp){ 
    105                         if($tmp != ""){ 
     99 
     100                $narray = explode("_end_", $var_decode); 
     101 
     102                foreach ($narray as $key => $tmp) { 
     103                        if ($tmp != "") { 
    106104                                $newr1[] = $tmp; 
    107105                        } 
     
    109107                unset($key); 
    110108                unset($tmp); 
    111                 foreach($newr1 as $key=>$tmp){ 
     109                foreach ($newr1 as $key => $tmp) { 
    112110                        $tmp2 = explode("_begin_##", $tmp); 
    113                         foreach($tmp2 as $tmp3){ 
    114                                 if($tmp3 != ""){ 
    115                                         $newr2[] = trim($tmp3);          
    116                                 }        
    117                         }        
    118                 } 
    119                  
     111                        foreach ($tmp2 as $tmp3) { 
     112                                if ($tmp3 != "") { 
     113                                        $newr2[] = trim($tmp3); 
     114                                } 
     115                        } 
     116                } 
     117 
    120118                unset($tmp); 
    121119                unset($tmp2); 
    122120                unset($tmp3); 
    123121 
    124                 foreach($newr2 as $tmp){ 
    125                         if(trim($tmp) != ""){    
    126                                 $tmp2 = explode("##",$tmp); 
    127                                 foreach($tmp2 as $tmp3){ 
    128                                         $tmp4 .= trim($tmp3) . "&&";     
    129                                 } 
    130                                 $newr3[] = substr($tmp4,0,(strlen($tmp4)-4));                    
     122                foreach ($newr2 as $tmp) { 
     123                        if (trim($tmp) != "") { 
     124                                $tmp2 = explode("##", $tmp); 
     125                                foreach ($tmp2 as $tmp3) { 
     126                                        $tmp4 .= trim($tmp3) . "&&"; 
     127                                } 
     128                                $newr3[] = substr($tmp4, 0, (strlen($tmp4) - 4)); 
    131129                                unset($tmp2); 
    132130                                unset($tmp3); 
     
    134132                        } 
    135133                } 
    136                  
    137                 $tmp = $newr3[count($newr3)-1]; 
    138                  
    139                 if(substr($tmp,0,9) == "#vacation"){ 
     134 
     135                $tmp = $newr3[count($newr3) - 1]; 
     136 
     137                if (substr($tmp, 0, 9) == "#vacation") { 
    140138                        $this->newout = array_pop($newr3); 
    141                         foreach($newr3 as $key=>$tmp){ 
     139                        foreach ($newr3 as $key => $tmp) { 
    142140                                $this->newrules[] = $tmp; 
    143141                        } 
    144                 }else{ 
    145                         foreach($newr3 as $tmp){ 
    146                                 $this->newrules[] = $tmp;        
    147                         } 
    148                 } 
    149                                  
     142                } else { 
     143                        foreach ($newr3 as $tmp) { 
     144                                $this->newrules[] = $tmp; 
     145                        } 
     146                } 
     147 
    150148                unset($tmp); 
    151                 $tmp  = explode("&&",$this->newout); 
    152                 $tmp1 = explode(",",$tmp[2]);  
    153                 foreach($tmp1 as $key=>$tmp2){ 
    154                         $tmp3 .= stripslashes(trim($tmp2)).", "; 
    155                 } 
    156                 $tmp3 = substr($tmp3,0,(strlen($tmp3)-2)); 
    157                  
     149                $tmp = explode("&&", $this->newout); 
     150                $tmp1 = explode(",", $tmp[2]); 
     151                foreach ($tmp1 as $key => $tmp2) { 
     152                        $tmp3 .= stripslashes(trim($tmp2)) . ", "; 
     153                } 
     154                $tmp3 = substr($tmp3, 0, (strlen($tmp3) - 2)); 
     155 
    158156                unset($tmp); 
    159157                unset($tmp1); 
    160158                unset($tmp2); 
    161159                unset($key); 
    162                 $tmp = explode("&&",$this->newout); 
    163                 foreach($tmp as $key=>$tmp1){ 
    164                         if($key == 2){ 
    165                                 $tmp2 .= trim($tmp3) . "&&";     
    166                         }else{ 
    167                                 $tmp2 .= trim($tmp1) . "&&";     
    168                         }        
     160                $tmp = explode("&&", $this->newout); 
     161                foreach ($tmp as $key => $tmp1) { 
     162                        if ($key == 2) { 
     163                                $tmp2 .= trim($tmp3) . "&&"; 
     164                        } else { 
     165                                $tmp2 .= trim($tmp1) . "&&"; 
     166                        } 
    169167                } 
    170168                unset($this->newout); 
    171                 $this->newout = substr($tmp2,0,(strlen($tmp2)-2)); 
    172                  
     169                $this->newout = substr($tmp2, 0, (strlen($tmp2) - 2)); 
     170 
    173171                //Abre a conexao 
    174172                $this->SieveS->start(); 
    175173                $this->errstr = ""; 
    176                  
     174 
    177175                // Escreve a nova regra; 
    178176                $this->reply = $this->SieveS->getscript(); 
    179177                /* 
    180                 if($this->reply){ 
    181                         $this->errstr = $this->SieveS->deletescript($this->scriptfile);          
    182                 } 
    183                 */ 
    184  
    185                 $error_log_file="/home/expressolivre/sieve_error.log"; 
     178                  if($this->reply){ 
     179                  $this->errstr = $this->SieveS->deletescript($this->scriptfile); 
     180                  } 
     181                 */ 
     182 
     183                $error_log_file = "/home/expressolivre/sieve_error.log"; 
    186184                //Escreve a(s) nova(s) regra(s); 
    187185                $newrule = $this->write_rule(); 
    188186                if (strlen($newrule) > 0) 
    189                         $this->errstr = $this->SieveS->putscript($this->scriptfile,$newrule); 
    190                 else 
    191                 { 
    192                         if($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True") 
    193                                 error_log(date("D M j G:i:s T Y").": SieveError, Invalid rule for " 
    194                                         .$_SESSION['phpgw_info']['expressomail']['user']['userid']."=>" 
    195                                         .$this->teste."\nRule:" 
    196                                         .$var_decode."\n", 3, $error_log_file); 
     187                        $this->errstr = $this->SieveS->putscript($this->scriptfile, $newrule); 
     188                else { 
     189                        if ($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True") 
     190                                error_log(date("D M j G:i:s T Y") . ": SieveError, Invalid rule for " 
     191                                                  . $_SESSION['phpgw_info']['expressomail']['user']['userid'] . "=>" 
     192                                                  . $this->teste . "\nRule:" 
     193                                                  . $var_decode . "\n", 3, $error_log_file); 
    197194                        return "Invalid rule\n" . $this->teste; 
    198195                } 
    199                  
     196 
    200197                //Ativa o script; 
    201198                $this->errstr = $this->SieveS->activatescript($this->scriptfile); 
    202                  
     199 
    203200                //Fecha a conexao 
    204201                $this->SieveS->close(); 
    205                  
    206                 if($this->errstr) 
    207                 { 
     202 
     203                if ($this->errstr) { 
    208204                        return "Ok"; 
    209                 } 
    210                 else 
    211                 { 
    212                         if($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True") 
    213                         error_log(date("D M j G:i:s T Y") 
    214                         .": SieveError, Problem for " 
    215                         .$_SESSION['phpgw_info']['expressomail']['user']['userid']."=>" 
    216                         ." " 
    217                         .$this->SieveS->errstr."\n", 3, $error_log_file); 
     205                } else { 
     206                        if ($_SESSION['phpgw_info']['server']['expressomail']['expressoMail_enable_log_messages'] == "True") 
     207                                error_log(date("D M j G:i:s T Y") 
     208                                                  . ": SieveError, Problem for " 
     209                                                  . $_SESSION['phpgw_info']['expressomail']['user']['userid'] . "=>" 
     210                                                  . " " 
     211                                                  . $this->SieveS->errstr . "\n", 3, $error_log_file); 
    218212                        return "Problemas na criação do arquivo!\n" . $this->teste; 
    219213                } 
    220214        } 
    221215 
    222     function convert_specialchar($input) 
    223     { 
    224       $special_char = false; 
    225           for ( $i = 0; $i < strlen($input); $i++ ) 
    226           { 
    227                 if ( preg_match('/[^a-z0-9.@~_+=&\'*^\ \-\[\]]/i', $input[$i]) ) 
    228               { 
    229                   $special_char = true; 
    230                   $input = preg_replace('/\\'.$input[$i].'/', '=' . bin2hex($input[$i]), $input); 
    231               } 
    232           }    
    233        
    234       if ( $special_char ) 
    235           { 
    236                    /* * 
    237                     * When using arrays with pattern and replacement,  
    238                     * the keys are processed in the order they appear in the array.  
    239                     * See preg_replace in php.net/preg_replace  
    240                     * */ 
    241                    $patterns[0] = '/=c3/i'; 
    242                    $patterns[1] = '/ /'; 
    243                    $replacements[1] = ''; 
    244                    $replacements[0] = '_'; 
    245                    $input = preg_replace($patterns, $replacements, $input); 
    246            } 
    247         
    248        return($input); 
    249     } 
     216        function convert_specialchar($input) { 
     217                $special_char = false; 
     218                for ($i = 0; $i < strlen($input); $i++) { 
     219                        if (preg_match('/[^a-z0-9.@~_+=&\'*^\ \-\[\]]/i', $input[$i])) { 
     220                                $special_char = true; 
     221                                $input = preg_replace('/\\' . $input[$i] . '/', '=' . bin2hex($input[$i]), $input); 
     222                        } 
     223                } 
     224 
     225                if ($special_char) { 
     226                        /*                       * 
     227                         * When using arrays with pattern and replacement,  
     228                         * the keys are processed in the order they appear in the array.  
     229                         * See preg_replace in php.net/preg_replace  
     230                         * */ 
     231                        $patterns[0] = '/=c3/i'; 
     232                        $patterns[1] = '/ /'; 
     233                        $replacements[1] = ''; 
     234                        $replacements[0] = '_'; 
     235                        $input = preg_replace($patterns, $replacements, $input); 
     236                } 
     237 
     238                return($input); 
     239        } 
    250240 
    251241        // build the rule 
    252         function write_rule(){ 
     242        function write_rule() { 
    253243 
    254244                // Variaveis; 
     
    260250                $rejectused = 0; 
    261251                $notify = 0; 
    262                 $flaggedused = 0; 
     252                $flaggedused = 0; 
    263253                $newscriptbody = ""; 
    264         $continue = 1; 
    265         $tmpSubject = ""; 
    266          
     254                $continue = 1; 
     255                $tmpSubject = ""; 
     256 
    267257                // Recebe os valores das regras; 
    268                 foreach($this->newrules as $tmp){ 
    269                   $tmp1 = explode("&&",$tmp); 
    270                   $rule['priority']      = $tmp1[1]; 
    271                   $rule['status']        = $tmp1[2]; 
    272           $rule['from']              = $this->convert_specialchar($tmp1[3]); 
    273           $rule['to']            = $this->convert_specialchar($tmp1[4]); 
    274                   $tmpSubject                    = $tmp1[5]; 
    275                   $rule['subject']               = $this->convert_specialchar($tmp1[5]); 
    276           $rule['action']            = $tmp1[6]; 
    277                   $rule['action_arg']    = utf8_encode(preg_replace("/\\r\\n/","\r\n",$tmp1[7])); 
    278           $rule['flg']               = $tmp1[8];    
    279           $rule['field']                 = $tmp1[9]; 
    280           $rule['field_val']     = $tmp1[10]; 
    281           $rule['size']                    = $tmp1[11]; 
    282           $rule['continue']        = ($tmp1[8] & $this->continuebit); 
    283           $rule['gthan']                   = ($tmp1[8] & $this->sizebit);  
    284           $rule['anyof']                   = ($tmp1[8] & $this->anyofbit); 
    285           $rule['keep']                    = ($tmp1[8] & $this->keepbit); 
    286           $rule['regexp']              = ($tmp1[8] & $this->regexbit); 
    287           $rule['unconditional']   = 0; 
    288                    
    289                   if (!$rule['from'] && !$rule['to'] && !$rule['subject'] && !$rule['field'] && empty($rule['size']) && $rule['action']) 
    290                   { 
    291                         $rule['unconditional'] = 1; 
    292                          
    293                         if( $rule['unconditional'] && ($rule['size'] == "0" || $rule['size'] == 0 ) ) 
    294                                 $rule['unconditional'] = 0; 
    295                   } 
    296                   unset($tmp1); 
    297                
    298               // Monta as regras; 
    299               if ($rule['status'] != 'ENABLED') { 
    300               } 
    301               else { 
    302                 $activerules = 1; 
    303           
    304                 // Condições para montagem das regras; 
    305                 $anyall = "allof"; 
    306                 if ($rule['anyof']) $anyall = "anyof"; 
    307                 if ($rule['regexp']) { 
    308                     $regexused = 1; 
    309                 } 
    310                 $started = 0; 
    311           
    312                 if (!$rule['unconditional']) { 
    313                     if (!$continue) $newruletext .= "els"; 
    314                     $newruletext .= "if " . $anyall . " ("; 
    315                     if ($rule['from']) { 
    316                         if (preg_match("/^\s*!/", $rule['from'])){ 
    317                             $newruletext .= 'not '; 
    318                             $rule['from'] = preg_replace("/^\s*!/","",$rule['from']); 
    319                         } 
    320                         $match = ':contains'; 
    321                         if (preg_match("/\*|\?/", $rule['from'])) $match = ':matches'; 
    322                         if ($rule['regexp']) $match = ':regex'; 
    323                         $newruletext .= "header " . $match . " [\"From\"]"; 
    324                         $newruletext .= " \"" . $rule['from'] . "\""; 
    325                         $started = 1; 
    326                     } 
    327                     if ($rule['to']) { 
    328                         if ($started) $newruletext .= ", "; 
    329                         if (preg_match("/^\s*!/", $rule['to'])){ 
    330                             $newruletext .= 'not '; 
    331                             $rule['to'] = preg_replace("/^\s*!/","",$rule['to']); 
    332                         } 
    333                         $match = ':contains'; 
    334                         if (preg_match("/\*|\?/", $rule['to'])) $match = ':matches'; 
    335                         if ($rule['regexp']) $match = ':regex'; 
    336                         $newruletext .= "address " . $match . " [\"To\",\"TO\",\"Cc\",\"CC\"]"; 
    337                         $newruletext .= " \"" . $rule['to'] . "\""; 
    338                         $started = 1; 
    339                     } 
    340                     if ($rule['subject']) { 
    341                         if ($started) $newruletext .= ", "; 
    342                         if (preg_match("/^\s*!/", $rule['subject'])){ 
    343                             $newruletext .= 'not '; 
    344                             $rule['subject'] = preg_replace("/^\s*!/","",$rule['subject']); 
    345                         } 
    346                         $match = ':contains'; 
    347                         if (preg_match("/\*|\?/", $rule['subject'])) $match = ':matches'; 
    348                         if ($rule['regexp']) $match = ':regex'; 
    349                         $newruletext .= "header " . $match . " \"subject\""; 
    350                         $newruletext .= " \"" . $rule['subject'] . "\""; 
    351                         $started = 1; 
    352                     } 
    353                     if ($rule['field'] && $rule['field_val']) { 
    354                         if ($started) $newruletext .= ", "; 
    355                         if (preg_match("/^\s*!/", $rule['field_val'])){ 
    356                             $newruletext .= 'not '; 
    357                             $rule['field_val'] = preg_replace("/^\s*!/","",$rule['field_val']); 
    358                         } 
    359                         $match = ':contains'; 
    360                         if (preg_match("/\*|\?/", $rule['field_val'])) $match = ':matches'; 
    361                         if ($rule['regexp']) $match = ':regex'; 
    362                         $newruletext .= "header " . $match . " \"" . $rule['field'] . "\""; 
    363                         $newruletext .= " \"" . $rule['field_val'] . "\""; 
    364                         $started = 1; 
    365                     } 
    366                      
    367                     if ( $rule['size'] >= 0 ) { 
    368                         $xthan = " :under "; 
    369                         if ($rule['gthan']) $xthan = " :over "; 
    370                         if ($started) $newruletext .= ", "; 
    371                                          
    372                                         if($rule['size'] != 0 && $rule['size'] != "0") 
    373                                                 $newruletext .= "size " . $xthan . $rule['size'] ."K"; 
    374                                         else 
    375                                                 $newruletext .= "size " . $xthan . "0K"; 
    376                                                  
    377                         $started = 1; 
    378                     } 
    379           
    380                 } 
    381                 // Don't write half rule! 
    382                 if (strlen($newruletext) == 0)  
    383                         return false; 
    384                 // Actions 
    385                 if (!$rule['unconditional']) $newruletext .= ") {\n\t"; 
    386          
    387                 if (preg_match("/folder/i",$rule['action'])) 
    388                 { 
    389                     $newruletext .= "fileinto \"" . $rule['action_arg'] . "\";"; 
    390                 } 
    391                  
    392                 if (preg_match("/reject/i",$rule['action'])) 
    393                 { 
    394                     $newruletext .= "reject text: \n" . $rule['action_arg'] . "\n.\n;"; 
    395                     $rejectused = 1; 
    396                 } 
    397                 if (preg_match("/flagged/i",$rule['action'])) 
    398                 { 
    399                     $newruletext .= "addflag \"\\\\Flagged\";"; 
    400                     $flaggedused = 1; 
    401                 } 
    402                 if (preg_match("/address/i",$rule['action'])) 
    403                 { 
    404                     $newruletext .= "redirect \"" . $rule['action_arg'] . "\";"; 
    405                 } 
    406                  
    407                 if (preg_match("/notify/i",$rule['action'])) 
    408                 { 
    409                                 $newruletext .= "notify :method \"mailto\" :options [\"".$this->EmailVoip ."\"]:" . 
    410                                                                 "message \"<expressovoip><from>". $this->EmailExpresso."</from>". 
    411                                                                 "<br/><Subject>".utf8_encode($tmpSubject)."</Subject></expressovoip>\";"; 
    412                                 $notify = 1;                                                     
    413                 } 
    414                  
    415                 if (preg_match("/discard/i",$rule['action'])) { 
    416                     $newruletext .= "discard;"; 
    417                 } 
    418                 if ($rule['keep']) $newruletext .= "\n\tfileinto \"INBOX\";"; 
    419                 //if ($rule['keep']) $newruletext .= "\n\tkeep;";                
    420                 if (!$rule['unconditional']) $newruletext .= "\n}"; 
    421           
    422                 $continue = 0; 
    423                 if ($rule['continue']) $continue = 1; 
    424                 if ($rule['unconditional']) $continue = 1; 
    425           
    426                 $newscriptbody .= $newruletext . "\n\n"; 
    427                 unset($newruletext); 
    428               } 
     258                foreach ($this->newrules as $tmp) { 
     259                        $tmp1 = explode("&&", $tmp); 
     260                        $rule['priority'] = $tmp1[1]; 
     261                        $rule['status'] = $tmp1[2]; 
     262                        $rule['from'] = $this->convert_specialchar($tmp1[3]); 
     263                        $rule['to'] = $this->convert_specialchar($tmp1[4]); 
     264                        $tmpSubject = $tmp1[5]; 
     265                        $rule['subject'] = $this->convert_specialchar($tmp1[5]); 
     266                        $rule['action'] = $tmp1[6]; 
     267                        $rule['action_arg'] = utf8_encode(preg_replace("/\\r\\n/", "\r\n", $tmp1[7])); 
     268                        $rule['flg'] = $tmp1[8]; 
     269                        $rule['field'] = $tmp1[9]; 
     270                        $rule['field_val'] = $tmp1[10]; 
     271                        $rule['size'] = $tmp1[11]; 
     272                        $rule['continue'] = ($tmp1[8] & $this->continuebit); 
     273                        $rule['gthan'] = ($tmp1[8] & $this->sizebit); 
     274                        $rule['anyof'] = ($tmp1[8] & $this->anyofbit); 
     275                        $rule['keep'] = ($tmp1[8] & $this->keepbit); 
     276                        $rule['regexp'] = ($tmp1[8] & $this->regexbit); 
     277                        $rule['unconditional'] = 0; 
     278 
     279                        if (!$rule['from'] && !$rule['to'] && !$rule['subject'] && !$rule['field'] && empty($rule['size']) && $rule['action']) { 
     280                                $rule['unconditional'] = 1; 
     281 
     282                                if ($rule['unconditional'] && ($rule['size'] == "0" || $rule['size'] == 0 )) 
     283                                        $rule['unconditional'] = 0; 
     284                        } 
     285                        unset($tmp1); 
     286 
     287                        // Monta as regras; 
     288                        if ($rule['status'] != 'ENABLED') { 
     289                                 
     290                        } else { 
     291                                $activerules = 1; 
     292 
     293                                // Condições para montagem das regras; 
     294                                $anyall = "allof"; 
     295                                if ($rule['anyof']) 
     296                                        $anyall = "anyof"; 
     297                                if ($rule['regexp']) { 
     298                                        $regexused = 1; 
     299                                } 
     300                                $started = 0; 
     301 
     302                                if (!$rule['unconditional']) { 
     303                                        if (!$continue) 
     304                                                $newruletext .= "els"; 
     305                                        $newruletext .= "if " . $anyall . " ("; 
     306                                        if ($rule['from']) { 
     307                                                if (preg_match("/^\s*!/", $rule['from'])) { 
     308                                                        $newruletext .= 'not '; 
     309                                                        $rule['from'] = preg_replace("/^\s*!/", "", $rule['from']); 
     310                                                } 
     311                                                $match = ':contains'; 
     312                                                if (preg_match("/\*|\?/", $rule['from'])) 
     313                                                        $match = ':matches'; 
     314                                                if ($rule['regexp']) 
     315                                                        $match = ':regex'; 
     316                                                $newruletext .= "header " . $match . " [\"From\"]"; 
     317                                                $newruletext .= " \"" . $rule['from'] . "\""; 
     318                                                $started = 1; 
     319                                        } 
     320                                        if ($rule['to']) { 
     321                                                if ($started) 
     322                                                        $newruletext .= ", "; 
     323                                                if (preg_match("/^\s*!/", $rule['to'])) { 
     324                                                        $newruletext .= 'not '; 
     325                                                        $rule['to'] = preg_replace("/^\s*!/", "", $rule['to']); 
     326                                                } 
     327                                                $match = ':contains'; 
     328                                                if (preg_match("/\*|\?/", $rule['to'])) 
     329                                                        $match = ':matches'; 
     330                                                if ($rule['regexp']) 
     331                                                        $match = ':regex'; 
     332                                                $newruletext .= "address " . $match . " [\"To\",\"TO\",\"Cc\",\"CC\"]"; 
     333                                                $newruletext .= " \"" . $rule['to'] . "\""; 
     334                                                $started = 1; 
     335                                        } 
     336                                        if ($rule['subject']) { 
     337                                                if ($started) 
     338                                                        $newruletext .= ", "; 
     339                                                if (preg_match("/^\s*!/", $rule['subject'])) { 
     340                                                        $newruletext .= 'not '; 
     341                                                        $rule['subject'] = preg_replace("/^\s*!/", "", $rule['subject']); 
     342                                                } 
     343                                                $match = ':contains'; 
     344                                                if (preg_match("/\*|\?/", $rule['subject'])) 
     345                                                        $match = ':matches'; 
     346                                                if ($rule['regexp']) 
     347                                                        $match = ':regex'; 
     348                                                $newruletext .= "header " . $match . " \"subject\""; 
     349                                                $newruletext .= " \"" . $rule['subject'] . "\""; 
     350                                                $started = 1; 
     351                                        } 
     352                                        if ($rule['field'] && $rule['field_val']) { 
     353                                                if ($started) 
     354                                                        $newruletext .= ", "; 
     355                                                if (preg_match("/^\s*!/", $rule['field_val'])) { 
     356                                                        $newruletext .= 'not '; 
     357                                                        $rule['field_val'] = preg_replace("/^\s*!/", "", $rule['field_val']); 
     358                                                } 
     359                                                $match = ':contains'; 
     360                                                if (preg_match("/\*|\?/", $rule['field_val'])) 
     361                                                        $match = ':matches'; 
     362                                                if ($rule['regexp']) 
     363                                                        $match = ':regex'; 
     364                                                $newruletext .= "header " . $match . " \"" . $rule['field'] . "\""; 
     365                                                $newruletext .= " \"" . $rule['field_val'] . "\""; 
     366                                                $started = 1; 
     367                                        } 
     368 
     369                                        if ($rule['size'] != '')  
     370                                        { 
     371                                                if ($rule['size'] == 0) { 
     372                                                        $xthan = " :under "; 
     373                                                        if ($rule['gthan']) 
     374                                                                $xthan = " :over "; 
     375                                                        if ($started) 
     376                                                                $newruletext .= ", "; 
     377 
     378                                                        $newruletext .= "size " . $xthan . "0K"; 
     379                                                        $started = 1; 
     380                                                } 
     381 
     382                                                if ($rule['size'] > 0) { 
     383                                                        $xthan = " :under "; 
     384                                                        if ($rule['gthan']) 
     385                                                                $xthan = " :over "; 
     386                                                        if ($started) 
     387                                                                $newruletext .= ", "; 
     388 
     389                                                        $newruletext .= "size " . $xthan . $rule['size'] . "K"; 
     390                                                        $started = 1; 
     391                                                } 
     392                                        } 
     393                                } 
     394 
     395                                // Don't write half rule! 
     396                                if (strlen($newruletext) == 0) 
     397                                        return false; 
     398                                // Actions 
     399                                if (!$rule['unconditional']) 
     400                                        $newruletext .= ") {\n\t"; 
     401 
     402                                if (preg_match("/folder/i", $rule['action'])) { 
     403                                        $newruletext .= "fileinto \"" . $rule['action_arg'] . "\";"; 
     404                                } 
     405 
     406                                if (preg_match("/reject/i", $rule['action'])) { 
     407                                        $newruletext .= "reject text: \n" . $rule['action_arg'] . "\n.\n;"; 
     408                                        $rejectused = 1; 
     409                                } 
     410                                if (preg_match("/flagged/i", $rule['action'])) { 
     411                                        $newruletext .= "addflag \"\\\\Flagged\";"; 
     412                                        $flaggedused = 1; 
     413                                } 
     414                                if (preg_match("/address/i", $rule['action'])) { 
     415                                        $newruletext .= "redirect \"" . $rule['action_arg'] . "\";"; 
     416                                } 
     417 
     418                                if (preg_match("/notify/i", $rule['action'])) { 
     419                                        $newruletext .= "notify :method \"mailto\" :options [\"" . $this->EmailVoip . "\"]:" . 
     420                                                          "message \"<expressovoip><from>" . $this->EmailExpresso . "</from>" . 
     421                                                          "<br/><Subject>" . utf8_encode($tmpSubject) . "</Subject></expressovoip>\";"; 
     422                                        $notify = 1; 
     423                                } 
     424 
     425                                if (preg_match("/discard/i", $rule['action'])) { 
     426                                        $newruletext .= "discard;"; 
     427                                } 
     428                                if ($rule['keep']) 
     429                                        $newruletext .= "\n\tfileinto \"INBOX\";"; 
     430                                //if ($rule['keep']) $newruletext .= "\n\tkeep;";                
     431                                if (!$rule['unconditional']) 
     432                                        $newruletext .= "\n}"; 
     433 
     434                                $continue = 0; 
     435                                if ($rule['continue']) 
     436                                        $continue = 1; 
     437                                if ($rule['unconditional']) 
     438                                        $continue = 1; 
     439 
     440                                $newscriptbody .= $newruletext . "\n\n"; 
     441                                unset($newruletext); 
     442                        } 
    429443                }// Fim do Foreach; 
    430                 $this->teste = $newscriptbody;           
     444                $this->teste = $newscriptbody; 
    431445                // Para a regras fora do escritorio; 
    432         unset($tmp); 
    433         if($this->newout != ""){ 
    434                 $aux                                    = explode("&&",$this->newout); 
    435                 $vacation['days']               = $aux[1]; 
    436                 $vacation['addresses']  = $aux[2];               
    437                 $vacation['text']               = preg_replace("/\\\\n/","\r\n",$aux[3]); 
    438                 $vacation['status']     = $aux[4]; 
    439             } 
    440                  
     446                unset($tmp); 
     447                if ($this->newout != "") { 
     448                        $aux = explode("&&", $this->newout); 
     449                        $vacation['days'] = $aux[1]; 
     450                        $vacation['addresses'] = $aux[2]; 
     451                        $vacation['text'] = preg_replace("/\\\\n/", "\r\n", $aux[3]); 
     452                        $vacation['status'] = $aux[4]; 
     453                } 
     454 
    441455                // Monta a regra para fora do escritorio; 
    442             if ($vacation['status'] == 'on') { 
     456                if ($vacation['status'] == 'on') { 
    443457                        $newscriptbody .= "vacation :days " . $vacation['days'] . " :addresses ["; 
    444             $newscriptbody .= $vacation['addresses']; 
    445             $newscriptbody .= "] text:\n" . utf8_encode($vacation['text']) . "\n.\n;\n\n"; 
    446             } 
    447                  
     458                        $newscriptbody .= $vacation['addresses']; 
     459                        $newscriptbody .= "] text:\n" . utf8_encode($vacation['text']) . "\n.\n;\n\n"; 
     460                } 
     461 
    448462                // Cria o cabeçalho do arquivo; 
    449                 $newscripthead  = ""; 
    450                 $newscripthead .= "#Mail filter rules for " .$this->username . "\n"; 
     463                $newscripthead = ""; 
     464                $newscripthead .= "#Mail filter rules for " . $this->username . "\n"; 
    451465                $newscripthead .= '#Generated by ' . $this->username . ' using Expressomail '; 
    452466                $newscripthead .= "\n"; 
    453467 
    454468                // Continuação do cabeçalho do arquivo;                  
    455             if ($activerules) { 
    456                 $newscripthead .= "require [\"fileinto\""; 
    457                  
    458                 if($notify){ 
    459                         $newscripthead .= ",\"notify\"";                         
    460                 } 
    461                 if($regexused){ 
    462                         $newscripthead .= ",\"regex\""; 
    463                 } 
    464                 if($rejectused){ 
    465                         $newscripthead .= ",\"reject\""; 
    466                 } 
    467                 if($flaggedused){ 
    468                         $newscripthead .= ",\"imapflags\""; 
    469                 } 
    470                 if($this->newout && $vacation['status'] == 'on'){ 
    471                     $newscripthead .= ",\"vacation\""; 
    472                 } 
    473                 $newscripthead .= "];\n\n"; 
    474             }else{ 
    475                         if($vacation && $vacation['status'] == 'on'){ 
    476                             $newscripthead .= "require [\"vacation\"];\n\n"; 
    477                         } 
    478             } 
    479                  
    480             // Cria o rodapé do arquivo; 
    481             $newscriptfoot = ""; 
    482             $newscriptfoot .= "##PSEUDO script start\n"; 
    483             // Lê as regras; 
    484             foreach($this->newrules as $tmp){ 
    485                         $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$tmp). "\n"; 
    486             } 
    487             // Lê as regras fora do escritório; 
    488             if($this->newout != ""){ 
    489                         $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$this->newout)."\n"; 
    490             } 
     469                if ($activerules) { 
     470                        $newscripthead .= "require [\"fileinto\""; 
     471 
     472                        if ($notify) { 
     473                                $newscripthead .= ",\"notify\""; 
     474                        } 
     475                        if ($regexused) { 
     476                                $newscripthead .= ",\"regex\""; 
     477                        } 
     478                        if ($rejectused) { 
     479                                $newscripthead .= ",\"reject\""; 
     480                        } 
     481                        if ($flaggedused) { 
     482                                $newscripthead .= ",\"imapflags\""; 
     483                        } 
     484                        if ($this->newout && $vacation['status'] == 'on') { 
     485                                $newscripthead .= ",\"vacation\""; 
     486                        } 
     487                        $newscripthead .= "];\n\n"; 
     488                } else { 
     489                        if ($vacation && $vacation['status'] == 'on') { 
     490                                $newscripthead .= "require [\"vacation\"];\n\n"; 
     491                        } 
     492                } 
     493 
     494                // Cria o rodapé do arquivo; 
     495                $newscriptfoot = ""; 
     496                $newscriptfoot .= "##PSEUDO script start\n"; 
     497                // Lê as regras; 
     498                foreach ($this->newrules as $tmp) { 
     499                        $newscriptfoot .= preg_replace("/[\\n\\r]/", " ", $tmp) . "\n"; 
     500                } 
     501                // Lê as regras fora do escritório; 
     502                if ($this->newout != "") { 
     503                        $newscriptfoot .= preg_replace("/[\\n\\r]/", " ", $this->newout) . "\n"; 
     504                } 
    491505                $newscriptfoot .= "#mode&&basic\n"; 
    492                  
    493                 $newscript = $newscripthead . $newscriptbody . $newscriptfoot;  
     506 
     507                $newscript = $newscripthead . $newscriptbody . $newscriptfoot; 
    494508 
    495509                // Destroi as variaveis; 
     
    499513                unset($regexused); 
    500514                unset($rejectused); 
    501                 unset($flaggedused); 
     515                unset($flaggedused); 
    502516                unset($newscripthead); 
    503517                unset($newscriptbody); 
    504         unset($newscriptfoot); 
    505         unset($continue); 
    506         unset($this->newrules); 
    507         unset($this->newout); 
    508                  
     518                unset($newscriptfoot); 
     519                unset($continue); 
     520                unset($this->newrules); 
     521                unset($this->newout); 
     522 
    509523                // Retorna o script construido; 
    510524                return $newscript; 
    511          
    512         }// Fim da Função 
    513  
     525        } 
     526 
     527// Fim da Função 
    514528        // Cria o script sieve, caso nao possua; 
    515         function createScript(){ 
    516                  
     529        function createScript() { 
     530 
    517531                // Cria o cabeçalho do arquivo; 
    518                 $newScriptHead  = ""; 
    519                 $newScriptHead .= "#Mail filter rules for " .$this->username . "\n"; 
    520                 $newScriptHead .= '#Generated by ' . $this->username . ' using ExpressoMail ';   
     532                $newScriptHead = ""; 
     533                $newScriptHead .= "#Mail filter rules for " . $this->username . "\n"; 
     534                $newScriptHead .= '#Generated by ' . $this->username . ' using ExpressoMail '; 
    521535                $newScriptHead .= "\n"; 
    522536 
    523537                //Cria o rodapé do arquivo; 
    524                 $newScriptFoot  = ""; 
     538                $newScriptFoot = ""; 
    525539                $newScriptFoot .= "##PSEUDO Script Start\n"; 
    526540                $newScriptFoot .= "#mode&&basic\n"; 
    527                  
     541 
    528542                //Para passar para o arquivo; 
    529543                $newScript = $newScriptHead . $newScriptFoot; 
     
    531545                return $newScript; 
    532546        } 
    533          
     547 
    534548        //Lê o conteúdo do script; 
    535         function readScript($scriptName){ 
    536                  
     549        function readScript($scriptName) { 
     550 
    537551                // Verifica se a conexão foi bem sucedida; 
    538                 if(!$scriptName){        
     552                if (!$scriptName) { 
    539553                        $this->errstr = "Não foi possível conectar com o Servidor"; 
    540                         return "false 2";        
    541                 } 
    542                  
     554                        return "false 2"; 
     555                } 
     556 
    543557                // Recebe o conteúdo do array; 
    544558                $lines = array(); 
    545                 $lines = preg_split("/\n/",$scriptName); 
    546                  
     559                $lines = preg_split("/\n/", $scriptName); 
     560 
    547561                // Pega o tamanho da regra na primeira do script; 
    548562                $size_rule = array_shift($lines); 
    549                  
     563 
    550564                // Recebe o tamanho do script, pela primeira linha; 
    551565                $this->size = trim($size_rule); 
    552                  
     566 
    553567                // Verifica a composição do script; 
    554568                $line = array_shift($lines); 
    555                 if(!preg_match("/^# ?Mail(.*)rules for/", $line)){ 
     569                if (!preg_match("/^# ?Mail(.*)rules for/", $line)) { 
    556570                        $this->errstr = "Formato nao reconhecido"; 
    557571                        return false; 
    558572                } 
    559                  
     573 
    560574                // Variaveis para a regra e o campo ferias; 
    561                 $regexps  = array('^ *##PSEUDO','^ *#rule','^ *#vacation','^ *#mode'); 
    562                 $retorno['rule']         = array(); 
     575                $regexps = array('^ *##PSEUDO', '^ *#rule', '^ *#vacation', '^ *#mode'); 
     576                $retorno['rule'] = array(); 
    563577                $retorno['vacation'] = array(); 
    564                 $retorno['mode']        = array(); 
    565                  
     578                $retorno['mode'] = array(); 
     579 
    566580                $line = array_shift($lines); 
    567                 while (isset($line)){ 
    568                         foreach($regexps as $regp){ 
    569                                 if(preg_match("/$regp/i",$line)){ 
     581                while (isset($line)) { 
     582                        foreach ($regexps as $regp) { 
     583                                if (preg_match("/$regp/i", $line)) { 
    570584                                        // Recebe todas as regras criadas no servidor; 
    571                                         if(preg_match("/^ *#rule&&/i",$line)){ 
     585                                        if (preg_match("/^ *#rule&&/i", $line)) { 
    572586                                                $retorno['rule'][] = $line . "\n"; 
    573587                                        } 
    574                                         if(preg_match("/^ *#vacation&&/i",$line)){ 
    575                                                 $retorno['vacation'][] = $line . "\n";   
    576                                         } 
    577                                         if(preg_match("/^ *#mode&&(.*)/i",$line)){ 
    578                                                 $retorno['mode'][]= $line . "\n";                                                
    579                                         }                                        
     588                                        if (preg_match("/^ *#vacation&&/i", $line)) { 
     589                                                $retorno['vacation'][] = $line . "\n"; 
     590                                        } 
     591                                        if (preg_match("/^ *#mode&&(.*)/i", $line)) { 
     592                                                $retorno['mode'][] = $line . "\n"; 
     593                                        } 
    580594                                } 
    581595                        } 
     
    583597                        $line = array_shift($lines); 
    584598                } 
    585                 return $retorno;         
     599                return $retorno; 
    586600        } 
    587                  
    588 }//Fim da Classe 
     601 
     602} 
     603 
     604//Fim da Classe 
    589605?> 
Note: See TracChangeset for help on using the changeset viewer.