Code

Some changes for the sieve filter
[gosa.git] / include / sieve / class_sieveElement_If.inc
1 <?php
4 class sieve_if 
5 {
6   var $_parsed  = array();
7   var $TYPE     = "if";
8   var $id     = -1;
10   function sieve_if($elements,$object_id)
11   {
12     $this->id = $object_id;
13     $this->elements = $elements;
14     $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
15   }
18   /* Return html element for IF */ 
19   function execute()
20   {
21     /* Create title */
22     $name  = "<img src='images/small_filter.png' class='center'>";
23     $name .= "<b>"._("Condition")."</b>";
24     if($this->TYPE == "if"){
25       $name .= "&nbsp;-&nbsp;"._("If");
26     }else{
27       $name .= "&nbsp;-&nbsp;"._("Else");
28     }
30     /* Create new html block */
31     $str  ="<table cellspacing=0 width='100%'>
32               <tr>
33                 <td style='width:100%;background-color:#DDDDDD; padding:5px; border: solid 2px #AAAAAA;'>".
34                   $name;
35     $str .= $this->get_as_html();  
36     $str .= "   </td>
37               </tr>
38             </table>";
39     return($str);
40   }
42   
43   /* Returns all elements as html */
44   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
45   {
46     $header_parts = array(":all",":localpart",":domain",":user",":detail");
48     $ret ="";
49     if($parsed == NULL){
50       $parsed = $this->_parsed;
51     }
53     /* Walk through all elements */
54     foreach($parsed as $key => $data){
56       /* Create Inverse Tag */
57       if(isset($data['Inverse']) && $data['Inverse']){
58         $str_inverse = "<font color='red'><b>"._("Not")."</b></font>&nbsp;";
59         $Inverse = TRUE;
60       }else{
61         $str_inverse = "";
62         $Inverse = FALSE;
63       }
65       /* Id used to have unique html names */
66       $element_id = $this->id."_".$id."_".$obj_id;
68       /* Create elements */
69       switch($key)
70       {
71   
72         /*******************
73          * TRUE FALSE 
74          *******************/
76         case "true" :
77         case "false" : 
78         { 
79           /* Inverse element if required */
80           if($Inverse){        
81             if($key == "true"){
82               $key = "false";
83             }else{
84               $key = "true";
85             }           
86           }
88           /* Get template */
89           $smarty = get_smarty();
90           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
91           $smarty->assign("selected"  , $key); 
92           $smarty->assign("ID"  , $element_id); 
93           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
94           break;
95         }
98         /*******************
99          * Header  
100          *******************/
102         case "address" : 
103         {
104           $ret ="";    
106           print_a($data);
108           $comparators = array("i;octet" => _("Normal"),"i;ascii-casemap"=>_("Case sensitive"),"i;ascii-numeric"=>_("Numeric"));
109           $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
110           $mtv = array("lt","le","eq","ge","gt","ne");
112           $smarty = get_smarty();
113           $smarty->assign("c_values"    , $comparators);
114           $smarty->assign("c_selected"  , $data['Comparator']);
116           $smarty->assign("match_types",$av_match_type);
117           $smarty->assign("match_type",$data['Match_type']);
119           $smarty->assign("match_type_value",$data['Match_type_value']);
120           $smarty->assign("match_type_values",$mtv);
121  
122           $keys = "";
123           foreach($data['Key_List'] as $key){
124             $keys .= $key."\n";
125           }
126    
127           $values = "";
128           foreach($data['Value_List'] as $key){
129             $values .= $key."\n";
130           }
131           $smarty->assign("keys",$keys);
132           $smarty->assign("values",$values);
134  
135           $smarty->assign("ID"  , $element_id); 
136           $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
137           break;
138         }
139       
141         /*******************
142          * All of   
143          *******************/
145         case "allof" : 
146         {
147           $ret = "<table width='100%'  cellspacing=0 cellpadding=0>
148                     <tr>
149                       <td style='text-align:center; vertical-align: middle; width:45px; 
150                                  background-color: #BDBDBD; border: solid 1px #EEEEEE'>".
151                         "<b>".$str_inverse._("All of")."</b>".
152                         "<img class='center' src='images/select_ogroup.png'>".
153                      "</td>";
154           $ret.= "    <td style='background-color:#BDBDBD ; border: solid 1px #EEEEEE'>";
155           foreach($data as $key => $dat){
156             if($key == "Inverse" ){
157               continue;
158             }
159             $ret.=        $this->get_as_html($dat, ($id +1),$key);
160           }
161           $ret.= "    </td>
162                     </tr>
163                   </table>";
164           break ;
165         } 
168         /*******************
169          * Any of   
170          *******************/
172         case "anyof" : 
173         {
174           $ret = "<table width='100%' cellspacing=0 cellpadding=0>
175                     <tr>
176                       <td style='text-align:center; vertical-align: middle; width:45px; 
177                                  background-color: #AAAAAA; border: solid 1px #EEEEEE'>".
178                         "<b>".$str_inverse._("Any of")."</b>".
179                         "<img class='center' src='images/select_department.png'>".
180                      "</td>";
181           $ret.= "    <td style='background-color: #AAAAAA ; border: solid 1px #EEEEEE'>";
182           foreach($data as $key => $dat){
183             if($key == "Inverse" ){
184               continue;
185             }
186             $ret.=        $this->get_as_html($dat, ($id + 1),$key);
187           }
188           $ret.= "    </td>
189                     </tr>
190                   </table>";
191           break ;
192         }
193         default : 
194           {
195             $ret = "<table width='100%'  cellspacing=0 cellpadding=0>
196                       <tr>
197                         <td style='background-color: #FEDCA9 ; border: solid 1px        #EEEEEE'>";
198             $ret.= $key."<br>"; 
199             $ret.= "    </td>
200                       </tr>
201                     </table>";
203           }
204       }
205     }
206     return($ret);
207   }
210   function _parse($data,$id = 0)
211   {
212     $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
213     $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
214     
215     $type = $data[$id]['text'];
217     $tmp = array();
220     /* Is there an identifier named 'not' to inverse this filter ? */
221     $Inverse = FALSE;
222     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
223       $Inverse = TRUE;
224       $id ++;
225       $type = $data[$id]['text'];
226     }
228     switch($type)
229     {
230   
231       case "envelope" : 
232       case "header":
233       case "address" : 
234       {
235         /* Address matches are struckture as follows :
236            [not] 
237            address 
238                   [address-part: tag]           all|localpart|domain|user|detail
239                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
240                   [match-type: tag]             is|contains|matches|count|value 
241                   <header-list: string-list> 
242                   <key-list: string-list>   
243           */ 
244    
245         
246         $part     = "(:all|:localpart|:domain)";
247         $operator = "(:contains|:is|:matches|:count|:value)";
248         $value_op = "(lt|le|eq|ge|gt|ne)";
250         $Address_Part     = "";
251         $Comparator       = "";        
252         $Match_type       = "";    
253         $Match_type_value = "";
254   
255         $Key_List         = array();
256         $Value_List       = array();
257   
258         for($i = 0 ; $i < count($data) ; $i ++){
259          
260           /* Get next node */ 
261           $node = $data[$i];
262   
263           /* Check address part definition */
264           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
265             $Address_Part = $node['text'];
266           }
268           /* Check for match type  */
269           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
270             $Match_type = $node['text'];
272             /* Get value operator */
273             if($Match_type == ":value"){
274               $i ++;        
275               $node = $data[$i];
276               
277               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
278                 $Match_type_value = $node['text'];
279               }
280             }
281           } 
282   
283           /* Check for a comparator */
284           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
285             $i ++;
286             $node = $data[$i];
287             $Comparator = $node['text'];
288           }
289   
290           /* Check for Key_List */  
291           elseif(count(sieve_get_strings($data,$i))){
292             $tmp2 = sieve_get_strings($data,$i);
293             $i =  $tmp2['OFFSET'];
295             if(!count($Key_List)){
296               $Key_List = $tmp2['STRINGS'];
297             }else{
298               $Value_List = $tmp2['STRINGS']; 
299             }
300           } 
301       
302         }
303  
304          
305         /* Add to Tree */ 
306         $values = array( "Inverse"         => $Inverse,
307                                 "Comparator"      => $Comparator,
308                                 "Match_type"      => $Match_type,
309                                 "Match_type_value"=> $Match_type_value,
310                                 "Key_List"        => $Key_List,
311                                 "Value_List"      => $Value_List) ;
312         if($type == "address"){
313           $values["Address_Part"]    = $Address_Part;
314         }
315         $tmp[$type] = $values;
316         break;
317       }
320       case "size":
321       {
322     
323         $ops = "(:over|:under)";
325         $Match_type = "";
327         for($i = $id ; $i < count($data); $i++){
329           /* Get current node */
330           $node = $data[$i];
332           /* Get tag (under / over) */
333           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
334             $Match_type = $node['text'];
335           }
336           
337           /* Get Value_List, the value that we want to match for */
338           elseif(count(sieve_get_strings($data,$i))){
339             $tmp2 = sieve_get_strings($data,$i);
340             $i =  $tmp2['OFFSET'];
341           
342             $Value_List = $tmp2['STRINGS'];
343           } 
344         }        
345     
346         $tmp[$type]= array( "Inverse"    => $Inverse,
347                             "Match_type" => $Match_type,
348                             "Value_List" => $Value_List);
349         break;
350       }
351       case "true": 
352       {
353         $tmp['true'] = "true";
354         break;
355       }
356       case "false":
357       {
358         $tmp['false'] = "false";
359         break;
360       }
361       case "allof" :
362       {
363         $id ++;
364         $tmp2 = $this->get_parameter($data,$id);
365         
366         foreach($tmp2 as $parameter){
367           $tmp['allof'][] = $this->_parse($parameter);
368         }
369         $tmp['allof']['Inverse'] = $Inverse;
370         break;
371       }
373       case "anyof" :
374       {
375         $id ++;
376         $tmp2 = $this->get_parameter($data,$id);
378         foreach($tmp2 as $parameter){
379           $tmp['anyof'][] = $this->_parse($parameter);
380         }
381         $tmp['anyof']['Inverse'] = $Inverse;
382         break;
383       }
384       default : $tmp[$id] = $type; 
385     }
386     
387     return($tmp); 
388   }
391   function get_parameter($data,$id)
392   {
393     $par = array();
394     $open_brakets = 0;
395     $next = NULL;
396     $num = 0;
397     for($i = $id ; $i < count($data) ; $i++ ){
398       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
399         $open_brakets ++;
400       }
401       if($data[$i]['class'] == "comma" && $open_brakets == 1){
402         $num ++;
403       }
404       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
405         $par[$num][] = $data[$i];
406       }
407       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
408         $open_brakets --;
409       }
410     }
411     return($par);
412   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>