Code

Updated error msgs in semantic class
[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 $object_id     = -1;
10   var $address_parts    = array();
11   var $comparators      = array();
12   var $match_types      = array();
13   var $operators        = array();
14   var $parent           = NULL;
15   
16   /* Initialize class 
17    *  $elements   contains all tokens that belongs to this if/else tag
18    *  $object_id  cotains an unique tag id, to be able to create uniqe html post names
19    */
20   function sieve_if($elements,$object_id,$parent)
21   {
22     $this->parent = $parent;
23   
24     /* Possible address parts we can select */
25     $this->address_parts = array( 
26         ":all"       => _("Complete adress")."&nbsp;("._("Default").")",
27         ":domain"    => _("Domian part") ,
28         ":localpart" => _("Local part"));
30     /* comparator type */
31     $this->comparators   = array( 
32         "i;ascii-casemap" => _("Case insensitive")."&nbsp;("._("Default").")",
33         "i;octet"         => _("Case sensitive"),
34         "i;ascii-numeric" => _("Numeric"));
36     /* Match types */
37     $this->match_types  = array(  
38         ":is"         => _("is"),
39         ":contains"   => _("contains"),
40         ":matches"    => _("matches"),
41         ":count"      => _("count"),
42         ":value"      => _("value is"));
44     /* Operators */
45     $this->operators = array(     
46         "lt"  => _("less than"),
47         "le"  => _("less or equal"),
48         "eq"  => _("equals"),
49         "ge"  => _("greater or equal"),
50         "gt"  => _("greater than"),
51         "ne"  => _("not equal"));
53     $this->object_id       = $object_id;
54     if($elements!=NULL){
55       $this->elements = $elements;
56       $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
57     }
58   }
61   /* Returns the sieve script for this 
62    *  if/else tag.
63    */
64   function get_sieve_script_part()
65   {
66     $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
67     return($tmp);
68   } 
71   /* Return error msgs */
72   function check()
73   {
74     $check = $this->check_recursive();
75     return($check);
76   }
77  
79   /* Recursivly fetch all error msgs */
80   function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
81   {
82     $ret = array();
83     if($parsed == NULL){
84       $parsed = $this->_parsed;
85     }
87     /* Walk through all elements */
88     foreach($parsed as $key => $data){
90       /* Create elements */
91       switch($key)
92       {
93         /*******************
94          * Allof / Anyof
95          *******************/
96         case "anyof" :
97         case "allof" :
98         { 
99           foreach($data as $key2 => $dat){
100             if(($key2 === "Inverse") && ($key2 == "Inverse")){
101               continue;
102             }
103             $msgs = $this->check_recursive($dat, ($id +1),$key2);
105             foreach($msgs as $msg){
106               $ret[] = $msg;
107             }
108           }
109           break;
110         }
112         /*******************
113          * True / False
114          *******************/
116         case "true" :
117         case "false" : 
118         {
119           /* Can't fail anyway */
120           break;
121         }
122     
123         /*******************
124          * Default
125          *******************/
127         default: 
128         {
129           if(isset($data['LastError']) && !empty($data['LastError'])){
130             $ret[] = $data['LastError'];
131           }
132         }
133       }
134     }
135     return($ret);
136   }
137  
139   /* Recursivly create a sieve script out of the given 
140    *  tags and tokens provided by $parsed.
141    *  $id       specifies the depth of the current element.
142    *  $obj_id   is the current tag-id handled by this function
143    */
144   function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
145   {
146     $script ="";
147     if($parsed == NULL){
148       $parsed = $this->_parsed;
149     }
152     if(!is_array($parsed)){
153       return;
154     }
156     /* Walk through all elements */
157     foreach($parsed as $key => $data){
159       /* Create Inverse Tag */
160       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
161         $Inverse = TRUE;
162       }else{
163         $Inverse = FALSE;
164       }
166       /* Create elements */
167       switch($key)
168       {
170         /*******************
171          * True / False
172          *******************/
174         case "true" :
175         case "false" :
176         {
177           /* Invert this test if required */
178           if($Inverse){
179             $script .= "not ";
180           }
181           $script .= $key;
182           break;
183         }
186         /*******************
187          * Address
188          *******************/
190         case "address" :   
191         {
192           /* [not] address 
193                         [address-part: tag] 
194                         [comparator: tag] 
195                         [match-type: tag] 
196                         <header-list: string-list> 
197                         <key-list: string-list> 
198           */
200           /* Invert this test if required */
201           if($Inverse){
202             $script .= "not ";
203           }
204   
205           $script .="address ";
206  
207           /* Add address part tag */ 
208           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
209             $script .= $data['Address_Part']." ";
210           }
212           /* Add comparator */
213           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
214             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
215           }
216     
217           /* Add match type */
218           $script .= $data['Match_type']." ";
220           /* Add special match type for count and value */
221           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
222             $script .= sieve_create_strings($data['Match_type_value'])." ";
223           }
225           $script .= sieve_create_strings($data['Key_List']);
226           $script .= " ";
227           $script .= sieve_create_strings($data['Value_List']);
228           break;
229         }
232         /*******************
233          * Header
234          *******************/
236         case "header" :   
237         {
238           /* [not] header   
239                 [comparator: tag] 
240                 [match-type: tag] 
241                 <header-names: string-list> 
242                 <key-list: string-list>
243           */
245           /* Invert ? */
246           if($Inverse){
247             $script .= "not ";
248           }
249   
250           $script .="header ";
251  
252           /* Add address part tag */ 
253           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
254             $script .= $data['Address_Part']." ";
255           }
257           /* Add comparator */
258           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
259             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
260           }
261     
262           /* Add match type */
263           $script .= $data['Match_type']." ";
265           /* Add special match type for count and value */
266           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
267             $script .= sieve_create_strings($data['Match_type_value'])." ";
268           }
270           $script .= sieve_create_strings($data['Key_List']);
271           $script .= " ";
272           $script .= sieve_create_strings($data['Value_List']);
273           break;
274         }
277         /*******************
278          * Envelope
279          *******************/
281         case "envelope" :   
282         {
283           /* [not]  envelope 
284                     [address-part: tag] 
285                     [comparator: tag] 
286                     [match-type: tag] 
287                     <envelope-part: string-list> 
288                     <key-list: string-list> 
289           */
291           /* Invert */
292           if($Inverse){
293             $script .= "not ";
294           }
295   
296           $script .="envelope ";
297  
298           /* Add address part tag */ 
299           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
300             $script .= $data['Address_Part']." ";
301           }
303           /* Add comparator */
304           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
305             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
306           }
307     
308           /* Add match type */
309           $script .= $data['Match_type']." ";
311           /* Add special match type for count and value */
312           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
313             $script .= sieve_create_strings($data['Match_type_value'])." ";
314           }
316           $script .= sieve_create_strings($data['Key_List']);
317           $script .= " ";
318           $script .= sieve_create_strings($data['Value_List']);
319           break;
320         }
323         /*******************
324          * Exists
325          *******************/
326         case "exists" : 
327         {
328           /* [not] exists 
329               <header-names: string-list> 
330           */
332           /* Invert ? */
333           if($Inverse){
334             $script .= "not ";
335           }
337           $script .= "exists ".sieve_create_strings($data['Values']);
338           break;
339         }
342         /*******************
343          * Size
344          *******************/
345         case "size" : 
346         {
347           /* [not] size 
348                 <":over" / ":under"> 
349                 <limit: number> 
350           */
352           /* Invert ? */
353           if($Inverse){
354             $script .= "not ";
355           }
356  
357           /* Add size test */ 
358           $script .="size ";
359           $script .=$data['Match_type']." ";
360           foreach($data['Value_List'] as $val){
361             $script .= $val." ";
362           }
363           break;
364         }
367         /*******************
368          * Allof
369          *******************/
370         case "anyof" :
371         case "allof" :
372         {
373           /* allof <tests: test-list>
374              anyof <tests: test-list> */
376  
377           /* Add spaces, to indent the code.*/ 
378           $block = "\n";
379           for($i = 0 ; $i < $id ; $i ++){
380             $block .= SIEVE_INDENT_TAB;
381           }          
383           /* Add allof/anyof tag */
384           if($Inverse){
385             $script .= "not ";
386           }
387           $script.= $key." ( ";
389           /* Add each test parameter */
390           foreach($data as $key2 => $dat){
391             if(($key2 === "Inverse") && ($key2 == "Inverse")){
392               continue;
393             }
394             $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
395           }
396     
397           /* Remove last _,_ and close the tag */
398           $script = preg_replace("/,$/","",trim($script));
399           $script.= $block.")";
400           break ;
401         }
403         default :
404         {
405           $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
406         }
407       }
408     }
409     return($script);
410   }
412   
413   function add_test($data,$type)
414   {
415     switch($type)
416     {
417       case "header" : 
418       case "address" : 
419       case "envelope" : 
420       {
421         /* Add to Tree */
422         $values = array(        "Inverse"         => FALSE,
423                                 "Comparator"      => "",
424                                 "Expert"          => FALSE,
425                                 "LastError"       => "",
426                                 "Match_type"      => ":contains",
427                                 "Match_type_value"=> "",
428                                 "Key_List"        => array(_("emtpy")),
429                                 "Value_List"      => array(_("empty"))) ;
430         if($type == "address"){
431           $values["Address_Part"]    = ":all";
432         }
433         $data[$type]=$values;
435         $this->parent->add_require("relational");
436         if($type == "envelope"){
437           $this->parent->add_require("envelope");
438         }
439     
441         break;
442       }
443       case "allof" :
444       case "anyof" :
445       {
446         $data[$type] = array("Inverse" => FALSE);
447         break;
448       }
449       case "size" :
450       {
451         $tmp= array( 
452             "Inverse"    => FALSE,
453             "Match_type" => ":over",
454             "Value_List" => array("1M"));
456         $tmp['LastError'] = "";
457         $data[$type] = $tmp;
458         break;
459       }
460       case "true":
461       {
462         $data['true'] = "true";
463         $data['true']['LastError'] = "";
464         break;
465       }
466       case "false":
467       {
468         $data['false'] = "false";
469         $data['false']['LastError'] = "";
470         break;
471       }
472       case "exists" :
473       {
474         $data['exists'] = array('Inverse' => FALSE,
475                                 'Values'  => array(_("Nothing specified right now")),
476                                 'LastError' => "");
477         break;
478       }
479       default : echo "Still buggy ";exit;
480     }
482     return($data);
483   }
486   /* Ensure that all changes made on the ui 
487    *  will be saved. 
488    */
489   function save_object()
490   {
491   
492     if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
493       $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
494     }
496     $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
497     $this->_parsed = $tmp;
498   }
501   /* Recursivly save all ui changes for the 
502    *  tags and tokens provided by $parsed.
503    *  $id       specifies the depth of the current element.
504    *  $obj_id   is the current tag-id handled by this function
505    */
506   function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
507   {
508     /* Variable initialization */ 
509     $ret ="";
510     if($parsed == NULL){
511       $parsed = $this->_parsed;
512     }
514     if(!is_array($parsed)) return;
516     /* Walk through all elements */
517     foreach($parsed as $key => $data){
519       /* Id used to have unique html names */
520       $element_id = $this->object_id."_".$id."_".$obj_id;
521       
522       foreach($_POST as $name => $value){
523         if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
524           return(false); 
525         }
526       }
528       
529       if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
530         $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
531       }
533       /* Create elements */
534       switch($key)
535       {
536         /*******************
537          * Address 
538          *******************/
540         case "envelope" :
541         case "header" : 
542         case "address" : 
543         {
544           /* [not] address 
545                         [address-part: tag] 
546                         [comparator: tag] 
547                         [match-type: tag] 
548                         <header-list: string-list> 
549                         <key-list: string-list> 
550           */
552           /* Possible address parts we can select */
553           $address_parts = $this->address_parts;
554           $comparators   = $this->comparators;
555           $match_types   = $this->match_types; 
556           $operators     = $this->operators;
558           $parsed[$key]['LastError'] = "";
560           /* Toggle Inverse ? */
561           if(isset($_POST['toggle_inverse_'.$element_id])){
562             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
563           }
565           /* Check if we want to toggle the expert mode */
566           if(isset($_POST['Toggle_Expert_'.$element_id])){
567             $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
568           }
570           /* Get address part */
571           if(isset($_POST['address_part_'.$element_id])){
572             $ap = $_POST['address_part_'.$element_id];
574             if(!isset($address_parts[$ap])){
575               $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
576             }
577             $parsed[$key]['Address_Part'] = $ap;
578           }
580           /* Check if match type has changed */
581           if(isset($_POST['matchtype_'.$element_id])){
582             $mt = $_POST['matchtype_'.$element_id];
584             if(!isset($match_types[$mt])){
585               $parsed[$key]['LastError'] = _("Invalid match type given.");
586             }
587             $parsed[$key]['Match_type'] = $mt;
588           }
590           /* Get the comparator tag, if posted */
591           if(isset($_POST['comparator_'.$element_id])){
592             $cp = $_POST['comparator_'.$element_id];
594             if(!isset($comparators[$cp])){
595               $parsed[$key]['LastError'] = _("Invalid operator given.");
596             }
597             $parsed[$key]['Comparator'] = $cp;
599             if($cp == "i;ascii-numeric"){
600               $this->parent->add_require("comparator-i;ascii-numeric");
601             }
602           }
604           /* In case of :count and :value match types 
605            *  we have a special match operator we should save.
606            */
607           if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
608             if(isset($_POST['operator_'.$element_id])){
609               $op = $_POST['operator_'.$element_id];
611               if(!isset($operators[$op])){
612                 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
613               }
614               $parsed[$key]['Match_type_value'] = $op;
615             }
616           }
618           /* Get the address fields we should check, they are seperated by , */
619           if(isset($_POST['keys_'.$element_id])){
620             $vls = stripslashes($_POST['keys_'.$element_id]);
621             $tmp = array();
623             $tmp2 = split(",",$vls);
624             foreach($tmp2 as $val){
625               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
626             }
627             $parsed[$key]['Key_List'] = $tmp;
628           }
630           /* Get the values should check for, they are seperated by , */
631           if(isset($_POST['values_'.$element_id])){
632             $vls = stripslashes($_POST['values_'.$element_id]);
633             $tmp = array();
635             $tmp2 = split(",",$vls);
636             foreach($tmp2 as $val){
637               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
638             }
639             $parsed[$key]['Value_List'] = $tmp;
640           }
641           break;
642         }
643  
644         /*******************
645          * TRUE FALSE 
646          *******************/
648         case "true" :
649         case "false" : 
650         {
651           $name = 'boolean_'.$element_id;
652           if(isset($_POST[$name])){
653             $key2 = $_POST[$name];
654             
655             if($key != $key2) {
656               $parsed = array($key2 => $key2); 
657             }
658           }
659           break;
660         }
662         /*******************
663          * Exists 
664          *******************/
666         case "exists" :
667         {
668           /* Toggle Inverse ? */
669           if(isset($_POST['toggle_inverse_'.$element_id])){
670             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
671           }
673           /* get list of match values */
674           if(isset($_POST['Values_'.$element_id])){
675             $vls = stripslashes($_POST['Values_'.$element_id]);
676             $tmp = array();          
677   
678             $tmp2 = split(",",$vls);
679             foreach($tmp2 as $val){
680               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
681             }
682             $parsed['exists']['Values'] = $tmp;
683           }
684           break;
685         }
687         /*******************
688          * Size 
689          *******************/
691         case "size" :
692         {
693           $Match_types = array( ":over" => _("greater than") ,
694                                 ":under" => _("lower than"));
696           $Units       = array( "M" => _("Megabyte") ,
697                                 "K" => _("Kilobyte"));
699           /* Toggle Inverse ? */
700           if(isset($_POST['toggle_inverse_'.$element_id])){
701             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
702           }
704           /* Reset error */
705           $parsed[$key]['LastError'] ="";
707           /* Get match type */
708           if(isset($_POST['Match_type_'.$element_id])){
709             $mt = $_POST['Match_type_'.$element_id];
710             if(!isset($Match_types[$mt])){
711               $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
712             }
713             $parsed[$key]['Match_type'] = $mt;
714           }
716           /* Get old values */
717           $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
718           $unit  = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
720           /* Get value */
721           if(isset($_POST['Value_'.$element_id])){
722             $vl = $_POST['Value_'.$element_id];
723          
724             if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
725               $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
726             }
727             $value = preg_replace("/[^0-9]/","",$vl);
728           }        
730           /* Get unit */
731           if(isset($_POST['Value_Unit_'.$element_id])){
732             $ut = $_POST['Value_Unit_'.$element_id];
733        
734             if(!isset($Units[$ut])){
735               $parsed[$key]['LastError'] = _("No valid unit selected");
736             }
737             $unit = $ut;
738           }       
739           $parsed[$key]['Value_List'] = array(); 
740           $parsed[$key]['Value_List'][0] = $value.$unit;
741           break;
742         }
744         /*******************
745          * Allof 
746          *******************/
747      
748         case "allof" : 
749         {
750           if(isset($_POST['toggle_inverse_'.$element_id])){
751             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
752           }
753           foreach($data as $key2 => $dat){
754             if(($key2 === "Inverse") && ($key2 == "Inverse")){
755               continue;
756             }
757             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
758             if($tmp_data != false){
759               $parsed[$key][$key2] = $tmp_data;
760             }else{
761               unset( $parsed[$key][$key2]);
762             }
763           }
764           break ;
765         } 
767         /*******************
768          * Anyof 
769          *******************/
770      
771         case "anyof" : 
772         {
773           if(isset($_POST['toggle_inverse_'.$element_id])){
774             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
775           }
776           foreach($data as $key2 => $dat){
777             if(($key2 === "Inverse") && ($key2 == "Inverse")){
778               continue;
779             }
780             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
781             if($tmp_data != false){
782               $parsed[$key][$key2] = $tmp_data;
783             }else{
784               unset( $parsed[$key][$key2]);
785             }
786           }
787           break ;
788         } 
789       }
790     } 
791     return($parsed);
792   }  
795   /* Return html element for IF */ 
796   function execute()
797   {
798     /* Create title */
799     $name  = "<img alt='' src='images/small_filter.png' class='center'>";
800     $name .= "<b>"._("Condition")."</b>";
801     if($this->TYPE == "if"){
802       $name .= "&nbsp;-&nbsp;"._("If");
803     }elseif($this->TYPE == "elsif"){
804       $name .= "&nbsp;-&nbsp;"._("Else if");
805     }else{
806       $name .= "&nbsp;-&nbsp;"._("Else");
807     }
809     $smarty = get_smarty();
810     $smarty->assign("ID", $this->object_id);
812     /* Only display navigation elements if necessary */
813     if($this->TYPE == "if"){
814       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
815     }else{
816       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
817     }
819     $smarty->assign("Name", $name);
820     $smarty->assign("Contents", $this->get_as_html());
822     if($this->TYPE == "if"){
823       $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
824     }else{
825       $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
826     }
827     $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
828     return($str);
829   }
831   
832   /* Returns all elements as html */
833   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
834   {
835     $ret ="";
836     if($parsed == NULL){
837       $parsed = $this->_parsed;
838     }
840     if((!is_array($parsed)) || !count($parsed)) {
841       $smarty = get_smarty();
842       $smarty->assign("ID",$this->object_id);
843       $smarty->assign("DisplayAdd",TRUE);
844       $smarty->assign("DisplayDel",FALSE);
845       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
846       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
847       return($ret);
848     }
850     /* Walk through all elements */
851     foreach($parsed as $key => $data){
853       /* Create Inverse Tag */
854       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
855         $Inverse = TRUE;
856       }else{
857         $Inverse = FALSE;
858       }
860       /* Id used to have unique html names */
861       $element_id = $this->object_id."_".$id."_".$obj_id;
863       /* Create elements */
864       switch($key)
865       {
866   
867         /*******************
868          * TRUE FALSE 
869          *******************/
871         case "true" :
872         case "false" : 
873         { 
874           /* Inverse element if required */
875           if($Inverse){        
876             if($key == "true"){
877               $key = "false";
878             }else{
879               $key = "true";
880             }           
881           }
883           /* Get template */
884           $smarty = get_smarty();
885           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
886           $smarty->assign("selected"  , $key); 
887           $smarty->assign("ID"  , $element_id); 
888           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
889           break;
890         }
893         /*******************
894          * Header 
895          *******************/
897         case "header": 
898         {
899           $address_parts = $this->address_parts;
900           $comparators   = $this->comparators;
901           $match_types   = $this->match_types; 
902           $operators     = $this->operators;
904           $smarty = get_smarty();
905           $smarty->assign("comparators",$comparators);
906           $smarty->assign("match_types",$match_types);
907           $smarty->assign("operators",$operators);
908           $smarty->assign("LastError",$data['LastError']);
909           $smarty->assign("match_type", $data['Match_type']);
910           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
911           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
913           $keys = "";
914           foreach($data['Key_List'] as $key){
915             $keys .= $key.", ";
916           }
917           $keys = preg_replace("/,$/","",trim($keys));
918    
919           $values = "";
920           foreach($data['Value_List'] as $key){
921             $values .= $key.", ";
922           }
923           $values = preg_replace("/,$/","",trim($values));
925           $smarty->assign("keys",$keys);
926           $smarty->assign("Inverse",$Inverse);
927           $smarty->assign("values",$values);
928           $smarty->assign("Expert", $data['Expert']);
929  
930           $smarty->assign("ID"  , $element_id); 
931           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
932           break;
933         }
936         /*******************
937          * Envelope 
938          *******************/
940         case "envelope":
941         {
942           $address_parts = $this->address_parts;
943           $comparators   = $this->comparators;
944           $match_types   = $this->match_types; 
945           $operators     = $this->operators;
947           $smarty = get_smarty();
948           $smarty->assign("Inverse",$Inverse);
949           $smarty->assign("comparators",$comparators);
950           $smarty->assign("Expert", $data['Expert']);
951           $smarty->assign("match_types",$match_types);
952           $smarty->assign("operators",$operators);
953           $smarty->assign("LastError",$data['LastError']);
954           $smarty->assign("match_type", $data['Match_type']);
955           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
956           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
958           $keys = "";
959           foreach($data['Key_List'] as $key){
960             $keys .= $key.", ";
961           }
962           $keys = preg_replace("/,$/","",trim($keys));
964           $values = "";
965           foreach($data['Value_List'] as $key){
966             $values .= $key.", ";
967           }
968           $values = preg_replace("/,$/","",trim($values));
969           $smarty->assign("keys",$keys);
970           $smarty->assign("values",$values);
972           $smarty->assign("ID"  , $element_id); 
973           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
974           break;
975         }
978         /*******************
979          * Address 
980          *******************/
982         case "address" : 
983         {
984           $address_parts = $this->address_parts;
985           $comparators   = $this->comparators;
986           $match_types   = $this->match_types; 
987           $operators     = $this->operators;
989           $smarty = get_smarty();
990           $smarty->assign("Inverse",$Inverse);
991           $smarty->assign("address_parts",$address_parts);
992           $smarty->assign("comparators",$comparators);
993           $smarty->assign("match_types",$match_types);
994           $smarty->assign("LastError",$data['LastError']);
995           $smarty->assign("operators",$operators);
996           $smarty->assign("match_type", $data['Match_type']);
997           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
998           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
999           $smarty->assign("address_part", $data['Address_Part']);
1000           $smarty->assign("Expert", $data['Expert']);
1001         
1002           $keys = "";
1003           foreach($data['Key_List'] as $key){
1004             $keys .= $key.", ";
1005           }
1006           $keys = preg_replace("/,$/","",trim($keys));
1007    
1008           $values = "";
1009           foreach($data['Value_List'] as $key){
1010             $values .= $key.", ";
1011           }
1012           $values = preg_replace("/,$/","",trim($values));
1013           $smarty->assign("keys",$keys);
1014           $smarty->assign("values",$values);
1015           $smarty->assign("ID"  , $element_id); 
1016           $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1017           break;
1018         }
1019       
1021         /*******************
1022          * Size 
1023          *******************/
1024         
1025         case "size" : 
1026         {
1027           $Match_types = array( ":over" => _("greater than") , 
1028                                 ":under" => _("lower than"));
1030           $Units       = array( "M" => _("Megabyte") , 
1031                                 "K" => _("Kilobyte")); 
1033           $Match_type   = $data['Match_type'];
1034           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1035           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1036        
1037           $LastError = "";
1038           if(isset($data['LastError'])){
1039             $LastError = $data['LastError'];
1040           }
1041  
1042           $smarty = get_smarty();
1043           $smarty->assign("Inverse",$Inverse);
1044           $smarty->assign("LastError",$LastError);
1045           $smarty->assign("Match_types",$Match_types);
1046           $smarty->assign("Units",$Units);
1047           $smarty->assign("Match_type",$Match_type);
1048           $smarty->assign("Value",$Value);
1049           $smarty->assign("Value_Unit",$Value_Unit);
1050           $smarty->assign("ID"  , $element_id); 
1051           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1052           break;
1053         }
1054         
1055         /*******************
1056          * Exists 
1057          *******************/
1058         
1059         case "exists" : 
1060         {
1061           $LastError = "";
1062           if(isset($data['LastError'])){
1063             $LastError = $data['LastError'];
1064           }
1065  
1066           $Values = "";
1067           foreach($data['Values'] as $val){
1068             $Values .= $val.", ";
1069           }
1070           $Values = preg_replace("/,$/","",trim($Values));
1072           $smarty = get_smarty();
1073           $smarty->assign("LastError",$LastError);
1074           $smarty->assign("Values",$Values);
1075           $smarty->assign("Inverse",$Inverse);
1076           $smarty->assign("ID"  , $element_id); 
1077           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1078           break;
1079         }
1080   
1082         /*******************
1083          * All of   
1084          *******************/
1086         case "allof" : 
1087         {
1088           $Contents = ""; 
1089           foreach($data as $key => $dat){
1090             if(($key === "Inverse") && ($key == "Inverse")){
1091               continue;
1092             }
1093             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1094           }
1096           $smarty = get_smarty();
1097           $smarty->assign("ID"  , $element_id); 
1098           $smarty->assign("DisplayAdd",TRUE);
1099           $smarty->assign("DisplayDel",FALSE);
1100           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1101           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1103           $smarty->assign("Inverse",$Inverse);
1104           $smarty->assign("Contents",$cont_tmp.$Contents);
1105           $smarty->assign("ID"  , $element_id); 
1106           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1108           $ret = $allof_tmp;
1109           break ;
1110         } 
1113         /*******************
1114          * Any of   
1115          *******************/
1117         case "anyof" : 
1118         {
1119           $Contents = ""; 
1120           foreach($data as $key => $dat){
1121             if(($key === "Inverse") && ($key == "Inverse")){
1122               continue;
1123             }
1124             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1125           }
1126           $smarty = get_smarty();
1127           $smarty->assign("ID"  , $element_id); 
1128           $smarty->assign("DisplayAdd",TRUE);
1129           $smarty->assign("DisplayDel",FALSE);
1130           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1131           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1133           $smarty->assign("Inverse",$Inverse);
1134           $smarty->assign("Contents",$cont_tmp.$Contents);
1135           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1137           $ret = $allof_tmp;
1139           break ;
1140         } 
1141         default : 
1142         {
1143           trigger_error(_("Unhandled switch type"));
1144         }
1145       }
1146     }
1147     
1148     if(!isset($smarty)){
1149       $smarty =get_smarty();
1150     }
1152     $smarty->assign("ID",$element_id);
1153     $smarty->assign("DisplayAdd",FALSE);
1154     $smarty->assign("DisplayDel",TRUE);
1155     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1156     $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1157     return($ret);
1158   }
1161   /* Parse given token identified by $data[$id] 
1162    *  and return the parsed tokens. 
1163    */
1164   function _parse($data,$id = 0)
1165   {
1166     $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1167     $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1168     $type = $data[$id]['text'];
1169     $tmp = array();
1171     /* Is there an identifier named 'not' to inverse this filter ? */
1172     $Inverse = FALSE;
1173     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1174       $Inverse = TRUE;
1175       $id ++;
1176       $type = $data[$id]['text'];
1177     }
1179     switch($type)
1180     {
1182       /****************
1183        * Parse - Envelope / Header / Address
1184        ****************/ 
1186       case "envelope" : 
1187       case "header":
1188       case "address" : 
1189       {
1190         /* Address matches are struckture as follows :
1191            [not] 
1192            address 
1193                   [address-part: tag]           all|localpart|domain|user|detail
1194                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1195                   [match-type: tag]             is|contains|matches|count|value 
1196                   <header-list: string-list> 
1197                   <key-list: string-list>   
1198           */ 
1199    
1200         
1201         $part     = "(:all|:localpart|:domain)";
1202         $operator = "(:contains|:is|:matches|:count|:value)";
1203         $value_op = "(lt|le|eq|ge|gt|ne)";
1205         $Address_Part     = "";
1206         $Comparator       = "";        
1207         $Match_type       = "";    
1208         $Match_type_value = "";
1209   
1210         $Key_List         = array();
1211         $Value_List       = array();
1212   
1213         for($i = 0 ; $i < count($data) ; $i ++){
1214          
1215           /* Get next node */ 
1216           $node = $data[$i];
1217   
1218           /* Check address part definition */
1219           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1220             $Address_Part = $node['text'];
1221           }
1223           /* Check for match type  */
1224           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1225             $Match_type = $node['text'];
1227             /* Get value operator */
1228             if(in_array($Match_type,array(":value",":count"))){
1229               $i ++;        
1230               $node = $data[$i];
1232               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1233                 $Match_type_value = $node['text'];
1234               }
1235             }
1236           } 
1238           /* Check for a comparator */
1239           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1240             $i ++;
1241             $node = $data[$i];
1242             $Comparator = $node['text'];
1243           }
1244   
1245           /* Check for Key_List */  
1246           elseif(count(sieve_get_strings($data,$i))){
1247             $tmp2 = sieve_get_strings($data,$i);
1248             $i =  $tmp2['OFFSET'];
1250             if(!count($Key_List)){
1251               $Key_List = $tmp2['STRINGS'];
1252             }else{
1253               $Value_List = $tmp2['STRINGS']; 
1254             }
1255           } 
1256       
1257         }
1258  
1259          
1260         /* Add to Tree */ 
1261         $values = array( "Inverse"         => $Inverse,
1262                                 "Comparator"      => $Comparator,
1263                                 "Expert"          => FALSE,
1264                                 "Match_type"      => $Match_type,
1265                                 "Match_type_value"=> $Match_type_value,
1266                                 "Key_List"        => $Key_List,
1267                                 "Value_List"      => $Value_List) ;
1268         if($type == "address"){
1269           $values["Address_Part"]    = $Address_Part;
1270         }
1271         $tmp[$type] = $values;
1272         $tmp[$type]['LastError'] = "";
1273         break;
1274       }
1277       /****************
1278        * Parse - Size
1279        ****************/ 
1281       case "size":
1282       {
1283     
1284         $ops = "(:over|:under)";
1286         $Match_type = "";
1288         for($i = $id ; $i < count($data); $i++){
1290           /* Get current node */
1291           $node = $data[$i];
1293           /* Get tag (under / over) */
1294           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1295             $Match_type = $node['text'];
1296           }
1297           
1298           /* Get Value_List, the value that we want to match for */
1299           elseif(count(sieve_get_strings($data,$i))){
1300             $tmp2 = sieve_get_strings($data,$i);
1301             $i =  $tmp2['OFFSET'];
1302           
1303             $Value_List = $tmp2['STRINGS'];
1304           } 
1305         }        
1306     
1307         $tmp[$type]= array( "Inverse"    => $Inverse,
1308                             "Match_type" => $Match_type,
1309                             "Value_List" => $Value_List);
1310         $tmp[$type]['LastError'] = "";
1311         break;
1312       }
1315       /****************
1316        * Parse - True / False
1317        ****************/ 
1319       case "true": 
1320       {
1321         $tmp['true'] = "true";
1322         $tmp['true']['LastError'] = "";
1323         break;
1324       }
1325       case "false":
1326       {
1327         $tmp['false'] = "false";
1328         $tmp['false']['LastError'] = "";
1329         break;
1330       }
1333       /****************
1334        * Parse - Exists
1335        ****************/ 
1337       case "exists":
1338       {
1339         
1340         /* Skip first values, [if,not,exists] */
1341         $node = $data[$id];
1342         while(in_array($node['text'],array("if","not","exists"))){
1343           $id ++;
1344           $node = $data[$id];
1345         }
1347         /* Get values */
1348         $tmp2 = sieve_get_strings($data,$id);
1349   
1350         
1351         $tmp['exists'] = array('Inverse' => $Inverse,
1352                                'Values'  => $tmp2['STRINGS']);
1353         $tmp[$type]['LastError'] = "";
1354         break;
1355       }
1358       /****************
1359        * Parse - Allof
1360        ****************/ 
1362       case "allof" :
1363       {
1364         /* Get parameter and recursivly call this method 
1365          *  for each parameter 
1366          */
1367         $id ++;
1368         $tmp2 = $this->get_parameter($data,$id);
1369         
1370         foreach($tmp2 as $parameter){
1371           $tmp['allof'][] = $this->_parse($parameter);
1372         }
1373         $tmp['allof']['Inverse'] = $Inverse;
1374         break;
1375       }
1378       /****************
1379        * Parse - Anyof
1380        ****************/ 
1382       case "anyof" :
1383       {
1384         /* Get parameter and recursivly call this method 
1385          *  for each parameter 
1386          */
1387         $id ++;
1388         $tmp2 = $this->get_parameter($data,$id);
1390         foreach($tmp2 as $parameter){
1391           $tmp['anyof'][] = $this->_parse($parameter);
1392         }
1393         $tmp['anyof']['Inverse'] = $Inverse;
1394         break;
1395       }
1396       default : $tmp[$id] = $type; 
1397     }
1398     
1399     return($tmp); 
1400   }
1403   function get_parameter($data,$id)
1404   {
1405     $par = array();
1406     $open_brakets = 0;
1407     $next = NULL;
1408     $num = 0;
1409     for($i = $id ; $i < count($data) ; $i++ ){
1410       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1411         $open_brakets ++;
1412       }
1413       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1414         $num ++;
1415       }
1416       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1417         $par[$num][] = $data[$i];
1418       }
1419       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1420         $open_brakets --;
1421       }
1422     }
1423     return($par);
1424   }
1427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1428 ?>