source: trunk/expressoMail1_2/inc/class.ScriptS.inc.php @ 2

Revision 2, 14.4 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3//Inclui o arquivo contendo a classe SieveS;
4include('class.SieveS.inc.php');
5
6class ScriptS {
7       
8        //Declaração de Variáveis;
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;                                              // var tamanho;
16        var $so;                                                // para verificar se a regra foi criada por outro tipo serviço de filtros;
17        var $continuebit;
18        var $sizebit;
19        var $anyofbit;
20        var $keepbit;
21        var $regexbit;
22        var $newrules = array();
23        var $newout;
24       
25        function ScriptS(){
26
27                //Cria o objeto;
28                $this->SieveS     = new SieveS();
29               
30                //$this->scriptfile = $GLOBALS['HTTP_SESSION_VARS']['phpgw_info']['expressomail']['user']['account_lid'];
31                $this->scriptfile = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
32                $this->username   = $this->scriptfile;
33               
34                $this->reply      = "";
35                $this->rules      = "";
36                $this->errstr     = "";
37                $this->size               = "";
38               
39                $this->continuebit      = 1;
40        $this->sizebit          = 2;
41        $this->anyofbit         = 4;
42        $this->keepbit          = 8;
43        $this->regexbit         = 128;
44       
45        }
46       
47        function init_a(){
48               
49                //Abre a conexão
50                $this->SieveS->start();
51               
52                $this->reply = $this->SieveS->getscript();
53               
54                if(!$this->reply){
55       
56                        $aux = $this->SieveS->putscript($this->scriptfile, $this->createScript());
57                        if(!$aux){
58                                // Caso de erro, grava dentro da variável errstr;
59                                $this->errstr = "O arquivo não foi criado";
60                        }
61                        // Mata a variavel;
62                        unset($aux);
63                        // Ativa o script;
64                        $aux = $this->SieveS->activatescript($this->scriptfile);
65                       
66                        if(!$aux){
67                                // Caso de erro, grava dentro da variavel errstr;
68                                $this->errstr = "O arquivo não foi ativado";
69                        }
70                        else{
71                                $this->reply = $this->SieveS->getscript();
72                                $this->rules = $this->readScript($this->reply);
73                        }
74                }else{
75                        $this->rules = $this->readScript($this->reply);
76                }               
77
78                //Fecha a conexao
79                $this->SieveS->close();
80                       
81                if($this->rules){               
82                        return $this->rules;
83                }else{
84                        return $this->errstr;
85                }
86                       
87        }
88       
89        function rec_rules($params){
90               
91                $newr1  = array();
92                $newr2  = array();
93                $newr3  = array();
94       
95                $var_decode = rawurldecode($params['arfilter']);
96               
97                $narray  = explode("_end_",$var_decode);
98               
99                foreach($narray as $key=>$tmp){
100                        if($tmp != ""){
101                                $newr1[] = $tmp;
102                        }
103                }
104                unset($key);
105                unset($tmp);
106                foreach($newr1 as $key=>$tmp){
107                        $tmp2 = explode("_begin_##", $tmp);
108                        foreach($tmp2 as $tmp3){
109                                if($tmp3 != ""){
110                                        $newr2[] = trim($tmp3);         
111                                }       
112                        }       
113                }
114               
115                unset($tmp);
116                unset($tmp2);
117                unset($tmp3);
118
119                foreach($newr2 as $tmp){
120                        if(trim($tmp) != ""){   
121                                $tmp2 = explode("##",$tmp);
122                                foreach($tmp2 as $tmp3){
123                                        $tmp4 .= trim($tmp3) . "&&";   
124                                }
125                                $newr3[] = substr($tmp4,0,(strlen($tmp4)-4));                   
126                                unset($tmp2);
127                                unset($tmp3);
128                                unset($tmp4);
129                        }
130                }
131               
132                $tmp = $newr3[count($newr3)-1];
133               
134                if(substr($tmp,0,9) == "#vacation"){
135                        $this->newout = array_pop($newr3);
136                        foreach($newr3 as $key=>$tmp){
137                                $this->newrules[] = $tmp;
138                        }
139                }else{
140                        foreach($newr3 as $tmp){
141                                $this->newrules[] = $tmp;       
142                        }
143                }
144                               
145                unset($tmp);
146                $tmp  = explode("&&",$this->newout);
147                $tmp1 = explode(",",$tmp[2]);
148                foreach($tmp1 as $key=>$tmp2){
149                        $tmp3 .= stripslashes(trim($tmp2)).", ";
150                }
151                $tmp3 = substr($tmp3,0,(strlen($tmp3)-2));
152               
153                unset($tmp);
154                unset($tmp1);
155                unset($tmp2);
156                unset($key);
157                $tmp = explode("&&",$this->newout);
158                foreach($tmp as $key=>$tmp1){
159                        if($key == 2){
160                                $tmp2 .= trim($tmp3) . "&&";   
161                        }else{
162                                $tmp2 .= trim($tmp1) . "&&";   
163                        }       
164                }
165                unset($this->newout);
166                $this->newout = substr($tmp2,0,(strlen($tmp2)-2));
167               
168                //Abre a conexao
169                $this->SieveS->start();
170                $this->errstr = "";
171               
172                // Escreve a nova regra;
173                $this->reply = $this->SieveS->getscript();
174               
175                if($this->reply){
176                        $this->errstr = $this->SieveS->deletescript($this->scriptfile);         
177                }
178
179                //Escreve a(s) nova(s) regra(s);
180                $this->errstr = $this->SieveS->putscript($this->scriptfile,$this->write_rule());
181               
182                //Ativa o script;
183                $this->errstr = $this->SieveS->activatescript($this->scriptfile);
184               
185                //Fecha a conexao
186                $this->SieveS->close();
187               
188                if($this->errstr){
189                        return "Ok";
190                }else{
191                        return "Problemas na criação do arquivo!";
192                }
193        }
194
195        // Grava a nova regra;
196        function write_rule(){
197
198                // Variaveis;
199                $rule = array();
200                $vacation = array();
201                $newruletext = "";
202                $activerules = 0;
203                $regexused = 0;
204                $rejectused = 0;
205                $newscriptbody = "";
206        $continue = 1;
207       
208                // Recebe os valores das regras;
209                foreach($this->newrules as $tmp){
210                  $tmp1 = explode("&&",$tmp);
211                  $rule['priority']      = $tmp1[1];
212                  $rule['status']        = $tmp1[2];
213          $rule['from']              = $tmp1[3];
214          $rule['to']            = $tmp1[4];
215          $rule['subject']       = utf8_encode($tmp1[5]);
216          $rule['action']            = $tmp1[6];
217                  $rule['action_arg']    = utf8_encode(preg_replace("/\\r\\n/","\r\n",$tmp1[7]));
218          $rule['flg']               = $tmp1[8];   
219          $rule['field']                 = $tmp1[9];
220          $rule['field_val']     = $tmp1[10];
221          $rule['size']                    = $tmp1[11];
222          $rule['continue']        = ($tmp1[8] & $this->continuebit);
223          $rule['gthan']                   = ($tmp1[8] & $this->sizebit);
224          $rule['anyof']                   = ($tmp1[8] & $this->anyofbit);
225          $rule['keep']                    = ($tmp1[8] & $this->keepbit);
226          $rule['regexp']              = ($tmp1[8] & $this->regexbit);
227          $rule['unconditional']   = 0;
228                 
229                  if (!$rule['from'] && !$rule['to'] && !$rule['subject'] && !$rule['field'] && !$rule['size'] && $rule['action']){
230                         $rule['unconditional'] = 1;
231                  }
232                  unset($tmp1);
233             
234              // Monta as regras;
235              if ($rule['status'] != 'ENABLED') {
236              }
237              else {
238                $activerules = 1;
239         
240                // Condições para montagem das regras;
241                $anyall = "allof";
242                if ($rule['anyof']) $anyall = "anyof";
243                if ($rule['regexp']) {
244                    $regexused = 1;
245                }
246                $started = 0;
247         
248                if (!$rule['unconditional']) {
249                    if (!$continue) $newruletext .= "els";
250                    $newruletext .= "if " . $anyall . " (";
251                    if ($rule['from']) {
252                        if (preg_match("/^\s*!/", $rule['from'])){
253                            $newruletext .= 'not ';
254                            $rule['from'] = preg_replace("/^\s*!/","",$rule['from']);
255                        }
256                        $match = ':contains';
257                        if (preg_match("/\*|\?/", $rule['from'])) $match = ':matches';
258                        if ($rule['regexp']) $match = ':regex';
259                        $newruletext .= "address " . $match . " [\"From\"]";
260                        $newruletext .= " \"" . $rule['from'] . "\"";
261                        $started = 1;
262                    }
263                    if ($rule['to']) {
264                        if ($started) $newruletext .= ", ";
265                        if (preg_match("/^\s*!/", $rule['to'])){
266                            $newruletext .= 'not ';
267                            $rule['to'] = preg_replace("/^\s*!/","",$rule['to']);
268                        }
269                        $match = ':contains';
270                        if (preg_match("/\*|\?/", $rule['to'])) $match = ':matches';
271                        if ($rule['regexp']) $match = ':regex';
272                        $newruletext .= "address " . $match . " [\"To\",\"TO\",\"Cc\",\"CC\"]";
273                        $newruletext .= " \"" . $rule['to'] . "\"";
274                        $started = 1;
275                    }
276                    if ($rule['subject']) {
277                        if ($started) $newruletext .= ", ";
278                        if (preg_match("/^\s*!/", $rule['subject'])){
279                            $newruletext .= 'not ';
280                            $rule['subject'] = preg_replace("/^\s*!/","",$rule['subject']);
281                        }
282                        $match = ':contains';
283                        if (preg_match("/\*|\?/", $rule['subject'])) $match = ':matches';
284                        if ($rule['regexp']) $match = ':regex';
285                        $newruletext .= "header " . $match . " \"subject\"";
286                        $newruletext .= " \"" . $rule['subject'] . "\"";
287                        $started = 1;
288                    }
289                    if ($rule['field'] && $rule['field_val']) {
290                        if ($started) $newruletext .= ", ";
291                        if (preg_match("/^\s*!/", $rule['field_val'])){
292                            $newruletext .= 'not ';
293                            $rule['field_val'] = preg_replace("/^\s*!/","",$rule['field_val']);
294                        }
295                        $match = ':contains';
296                        if (preg_match("/\*|\?/", $rule['field_val'])) $match = ':matches';
297                        if ($rule['regexp']) $match = ':regex';
298                        $newruletext .= "header " . $match . " \"" . $rule['field'] . "\"";
299                        $newruletext .= " \"" . $rule['field_val'] . "\"";
300                        $started = 1;
301                    }
302                    if ($rule['size']) {
303                        $xthan = " :under ";
304                        if ($rule['gthan']) $xthan = " :over ";
305                        if ($started) $newruletext .= ", ";
306                        $newruletext .= "size " . $xthan . $rule['size'] . "K";
307                        $started = 1;
308                    }
309         
310                }
311         
312                // Ações
313                if (!$rule['unconditional']) $newruletext .= ") {\n\t";
314       
315                if (preg_match("/folder/i",$rule['action'])) {
316                    $newruletext .= "fileinto \"" . $rule['action_arg'] . "\";";
317                }
318                if (preg_match("/reject/i",$rule['action'])) {
319                    $newruletext .= "reject text: \n" . $rule['action_arg'] . "\n.\n;";
320                    $rejectused = 1;
321                }
322                if (preg_match("/address/i",$rule['action'])) {
323                    $newruletext .= "redirect \"" . $rule['action_arg'] . "\";";
324                }
325                if (preg_match("/discard/i",$rule['action'])) {
326                    $newruletext .= "discard;";
327                }
328                if ($rule['keep']) $newruletext .= "\n\tfileinto \"INBOX\";";
329                //if ($rule['keep']) $newruletext .= "\n\tkeep;";               
330                if (!$rule['unconditional']) $newruletext .= "\n}";
331         
332                $continue = 0;
333                if ($rule['continue']) $continue = 1;
334                if ($rule['unconditional']) $continue = 1;
335         
336                $newscriptbody .= $newruletext . "\n\n";
337                unset($newruletext);
338              }
339                }// Fim do Foreach;
340               
341                // Para a regras fora do escritorio;
342        unset($tmp);
343        if($this->newout != ""){
344                $aux                                    = explode("&&",$this->newout);
345                $vacation['days']               = $aux[1];
346                $vacation['addresses']  = $aux[2];             
347                $vacation['text']               = preg_replace("/\\\\n/","\r\n",$aux[3]);
348                $vacation['status']     = $aux[4];
349            }
350               
351                // Monta a regra para fora do escritorio;
352            if ($vacation['status'] == 'on') {
353                        $newscriptbody .= "vacation :days " . $vacation['days'] . " :addresses [";
354            $newscriptbody .= $vacation['addresses'];
355            $newscriptbody .= "] text:\n" . utf8_encode($vacation['text']) . "\n.\n;\n\n";
356            }
357               
358                // Cria o cabeçalho do arquivo;
359                $newscripthead  = "";
360                $newscripthead .= "#Mail filter rules for " .$this->username . "\n";
361                $newscripthead .= '#Generated by ' . $this->username . ' using Expressomail ';
362                $newscripthead .= "\n";
363
364                // Continuação do cabeçalho do arquivo;                 
365            if ($activerules) {
366                $newscripthead .= "require [\"fileinto\"";
367                if($regexused){
368                        $newscripthead .= ",\"regex\"";
369                }
370                if($rejectused){
371                        $newscripthead .= ",\"reject\"";
372                }
373                if($this->newout && $vacation['status'] == 'on'){
374                    $newscripthead .= ",\"vacation\"";
375                }
376                $newscripthead .= "];\n\n";
377            }else{
378                        if($vacation && $vacation['status'] == 'on'){
379                            $newscripthead .= "require [\"vacation\"];\n\n";
380                        }
381            }
382               
383                //preg_replace("/[\\n\\r]/","", $p);           
384               
385 
386            // Cria o rodapé do arquivo;
387            $newscriptfoot  = "";
388            $newscriptfoot .= "##PSEUDO script start\n";
389            // Lê as regras;
390            foreach($this->newrules as $tmp){
391                        $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$tmp). "\n";
392            }
393            // Lê as regras fora do escritório;
394            if($this->newout != ""){
395                        $newscriptfoot .= preg_replace("/[\\n\\r]/"," ",$this->newout)."\n";
396            }
397                $newscriptfoot .= "#mode&&basic\n";
398               
399                $newscript = $newscripthead . $newscriptbody . $newscriptfoot;
400
401                // Destroi as variaveis;
402                unset($rule);
403                unset($vacation);
404                unset($activerules);
405                unset($regexused);
406                unset($rejectused);
407                unset($newscripthead);
408                unset($newscriptbody);
409        unset($newscriptfoot);
410        unset($continue);
411        unset($this->newrules);
412        unset($this->newout);
413               
414                // Retorna o script construido;
415                return $newscript;
416       
417        }// Fim da Função
418
419        // Cria o script sieve, caso nao possua;
420        function createScript(){
421               
422                // Cria o cabeçalho do arquivo;
423                $newScriptHead  = "";
424                $newScriptHead .= "#Mail filter rules for " .$this->username . "\n";
425                $newScriptHead .= '#Generated by ' . $this->username . ' using ExpressoMail '; 
426                $newScriptHead .= "\n";
427
428                //Cria o rodapé do arquivo;
429                $newScriptFoot  = "";
430                $newScriptFoot .= "##PSEUDO Script Start\n";
431                $newScriptFoot .= "#mode&&basic\n";
432               
433                //Para passar para o arquivo;
434                $newScript = $newScriptHead . $newScriptFoot;
435
436                return $newScript;
437        }
438       
439        //Lê o conteúdo do script;
440        function readScript($scriptName){
441               
442                // Verifica se a conexão foi bem sucedida;
443                if(!$scriptName){       
444                        $this->errstr = "Não foi possível conectar com o Servidor";
445                        return "false 2";       
446                }
447               
448                // Recebe o conteúdo do array;
449                $lines = array();
450                $lines = preg_split("/\n/",$scriptName);
451               
452                // Pega o tamanho da regra na primeira do script;
453                $size_rule = array_shift($lines);
454               
455                // Recebe o tamanho do script, pela primeira linha;
456                $this->size = trim($size_rule);
457               
458                // Verifica a composição do script;
459                $line = array_shift($lines);
460                if(!preg_match("/^# ?Mail(.*)rules for/", $line)){
461                        $this->errstr = "Formato nao reconhecido";
462                        return false;
463                }
464               
465                // Variaveis para a regra e o campo ferias;
466                $regexps  = array('^ *##PSEUDO','^ *#rule','^ *#vacation','^ *#mode');
467                $retorno['rule']         = array();
468                $retorno['vacation'] = array();
469                $retorno['mode']         = array();
470               
471                $line = array_shift($lines);
472                while (isset($line)){
473                        foreach($regexps as $regp){
474                                if(preg_match("/$regp/i",$line)){
475                                        // Recebe todas as regras criadas no servidor;
476                                        if(preg_match("/^ *#rule&&/i",$line)){
477                                                $retorno['rule'][] = $line . "\n";
478                                        }
479                                        if(preg_match("/^ *#vacation&&/i",$line)){
480                                                $retorno['vacation'][] = $line . "\n"; 
481                                        }
482                                        if(preg_match("/^ *#mode&&(.*)/i",$line)){
483                                                $retorno['mode'][]= $line . "\n";                                               
484                                        }                                       
485                                }
486                        }
487                        // Pega a proxima linha do sript;
488                        $line = array_shift($lines);
489                }
490                return $retorno;       
491        }
492               
493}//Fim da Classe
494?>
Note: See TracBrowser for help on using the repository browser.