Code

Updated String handling in My_Tree class. To avoid losing quotes an every editing...
[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     $this->object_id       = $object_id;
24   
25     /* Possible address parts we can select */
26     $this->address_parts = array( 
27         ":all"       => _("Complete adress")."&nbsp;("._("Default").")",
28         ":domain"    => _("Domian part") ,
29         ":localpart" => _("Local part"));
31     /* comparator type */
32     $this->comparators   = array( 
33         "i;ascii-casemap" => _("Case insensitive")."&nbsp;("._("Default").")",
34         "i;octet"         => _("Case sensitive"),
35         "i;ascii-numeric" => _("Numeric"));
37     /* Match types */
38     $this->match_types  = array(  
39         ":is"         => _("is"),
40         ":regex"      => _("regex"),
41         ":contains"   => _("contains"),
42         ":matches"    => _("matches"),
43         ":count"      => _("count"),
44         ":value"      => _("value is"));
46     /* Operators */
47     $this->operators = array(     
48         "lt"  => _("less than"),
49         "le"  => _("less or equal"),
50         "eq"  => _("equals"),
51         "ge"  => _("greater or equal"),
52         "gt"  => _("greater than"),
53         "ne"  => _("not equal"));
55     /* Skip parsing if this element is new */
56     if($elements != NULL){
58       /* Remove comments from tests */  
59       $tmp = array();
60       foreach($elements['ELEMENTS'] as $ele){
61         if($ele['class'] != "comment"){
62           $tmp[] = $ele;
63         }
64       }
65       $elements['ELEMENTS'] = $tmp;
67       if($elements!=NULL){
68         $this->elements = $elements;
69         $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
70       }
71     }
72   }
75   /* Returns the sieve script for this 
76    *  if/else tag.
77    */
78   function get_sieve_script_part()
79   {
80     $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
81     return($tmp);
82   } 
85   /* Return error msgs */
86   function check()
87   {
88     $check = $this->check_recursive();
89     return($check);
90   }
91  
93   /* Recursivly fetch all error msgs */
94   function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
95   {
96     $ret = array();
97     if($parsed == NULL){
98       $parsed = $this->_parsed;
99     }
101     /* Walk through all elements */
102     foreach($parsed as $key => $data){
104       /* Create elements */
105       switch($key)
106       {
107         /*******************
108          * Allof / Anyof
109          *******************/
110         case "anyof" :
111         case "allof" :
112         { 
113           foreach($data as $key2 => $dat){
114             if(($key2 === "Inverse") && ($key2 == "Inverse")){
115               continue;
116             }
117             $msgs = $this->check_recursive($dat, ($id +1),$key2);
119             foreach($msgs as $msg){
120               $ret[] = $msg;
121             }
122           }
123           break;
124         }
126         /*******************
127          * True / False
128          *******************/
130         case "true" :
131         case "false" : 
132         {
133           /* Can't fail anyway */
134           break;
135         }
136     
137         /*******************
138          * Default
139          *******************/
141         default: 
142         {
143           if(isset($data['LastError']) && !empty($data['LastError'])){
144             $ret[] = $data['LastError'];
145           }
146         }
147       }
148     }
149     return($ret);
150   }
151  
153   /* Recursivly create a sieve script out of the given 
154    *  tags and tokens provided by $parsed.
155    *  $id       specifies the depth of the current element.
156    *  $obj_id   is the current tag-id handled by this function
157    */
158   function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
159   {
160     $script ="";
161     if($parsed == NULL){
162       $parsed = $this->_parsed;
163     }
166     if(!is_array($parsed)){
167       return;
168     }
170     /* Walk through all elements */
171     foreach($parsed as $key => $data){
173       /* Create Inverse Tag */
174       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
175         $Inverse = TRUE;
176       }else{
177         $Inverse = FALSE;
178       }
180       /* Create elements */
181       switch($key)
182       {
184         /*******************
185          * True / False
186          *******************/
188         case "true" :
189         case "false" :
190         {
191           /* Invert this test if required */
192           if($Inverse){
193             $script .= "not ";
194           }
195           $script .= $key;
196           break;
197         }
200         /*******************
201          * Address
202          *******************/
204         case "address" :   
205         {
206           /* [not] address 
207                         [address-part: tag] 
208                         [comparator: tag] 
209                         [match-type: tag] 
210                         <header-list: string-list> 
211                         <key-list: string-list> 
212           */
214           /* Invert this test if required */
215           if($Inverse){
216             $script .= "not ";
217           }
218   
219           $script .="address ";
220  
221           /* Add address part tag */ 
222           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
223             $script .= $data['Address_Part']." ";
224           }
226           /* Add comparator */
227           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
228             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
229           }
230     
231           /* Add match type */
232           $script .= $data['Match_type']." ";
234           /* Add special match type for count and value */
235           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
236             $script .= sieve_create_strings($data['Match_type_value'])." ";
237           }
239           $script .= sieve_create_strings($data['Key_List']);
240           $script .= " ";
241           $script .= sieve_create_strings($data['Value_List']);
242           break;
243         }
246         /*******************
247          * Header
248          *******************/
250         case "header" :   
251         {
252           /* [not] header   
253                 [comparator: tag] 
254                 [match-type: tag] 
255                 <header-names: string-list> 
256                 <key-list: string-list>
257           */
259           /* Invert ? */
260           if($Inverse){
261             $script .= "not ";
262           }
263   
264           $script .="header ";
265  
266           /* Add address part tag */ 
267           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
268             $script .= $data['Address_Part']." ";
269           }
271           /* Add comparator */
272           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
273             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
274           }
275     
276           /* Add match type */
277           $script .= $data['Match_type']." ";
279           /* Add special match type for count and value */
280           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
281             $script .= sieve_create_strings($data['Match_type_value'])." ";
282           }
284           $script .= sieve_create_strings($data['Key_List']);
285           $script .= " ";
286           $script .= sieve_create_strings($data['Value_List']);
287           break;
288         }
291         /*******************
292          * Envelope
293          *******************/
295         case "envelope" :   
296         {
297           /* [not]  envelope 
298                     [address-part: tag] 
299                     [comparator: tag] 
300                     [match-type: tag] 
301                     <envelope-part: string-list> 
302                     <key-list: string-list> 
303           */
305           /* Invert */
306           if($Inverse){
307             $script .= "not ";
308           }
309   
310           $script .="envelope ";
311  
312           /* Add address part tag */ 
313           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
314             $script .= $data['Address_Part']." ";
315           }
317           /* Add comparator */
318           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
319             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
320           }
321     
322           /* Add match type */
323           $script .= $data['Match_type']." ";
325           /* Add special match type for count and value */
326           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
327             $script .= sieve_create_strings($data['Match_type_value'])." ";
328           }
330           $script .= sieve_create_strings($data['Key_List']);
331           $script .= " ";
332           $script .= sieve_create_strings($data['Value_List']);
333           break;
334         }
337         /*******************
338          * Exists
339          *******************/
340         case "exists" : 
341         {
342           /* [not] exists 
343               <header-names: string-list> 
344           */
346           /* Invert ? */
347           if($Inverse){
348             $script .= "not ";
349           }
351           $script .= "exists ".sieve_create_strings($data['Values']);
352           break;
353         }
356         /*******************
357          * Size
358          *******************/
359         case "size" : 
360         {
361           /* [not] size 
362                 <":over" / ":under"> 
363                 <limit: number> 
364           */
366           /* Invert ? */
367           if($Inverse){
368             $script .= "not ";
369           }
370  
371           /* Add size test */ 
372           $script .="size ";
373           $script .=$data['Match_type']." ";
374           foreach($data['Value_List'] as $val){
375             $script .= $val." ";
376           }
377           break;
378         }
381         /*******************
382          * Allof
383          *******************/
384         case "anyof" :
385         case "allof" :
386         {
387           /* allof <tests: test-list>
388              anyof <tests: test-list> */
390  
391           /* Add spaces, to indent the code.*/ 
392           $block = "\n";
393           for($i = 0 ; $i < $id ; $i ++){
394             $block .= SIEVE_INDENT_TAB;
395           }          
397           /* Add allof/anyof tag */
398           if($Inverse){
399             $script .= "not ";
400           }
401           $script.= $key." ( ";
403           /* Add each test parameter */
404           foreach($data as $key2 => $dat){
405             if(($key2 === "Inverse") && ($key2 == "Inverse")){
406               continue;
407             }
408             $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
409           }
410     
411           /* Remove last _,_ and close the tag */
412           $script = preg_replace("/,$/","",trim($script));
413           $script.= $block.")";
414           break ;
415         }
417         default :
418         {
419           $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
420         }
421       }
422     }
423     return($script);
424   }
426   
427   function add_test($data,$type)
428   {
429     switch($type)
430     {
431       case "header" : 
432       case "address" : 
433       case "envelope" : 
434       {
435         /* Add to Tree */
436         $values = array(        "Inverse"         => FALSE,
437                                 "Comparator"      => "",
438                                 "Expert"          => FALSE,
439                                 "LastError"       => "",
440                                 "Match_type"      => ":contains",
441                                 "Match_type_value"=> "",
442                                 "Key_List"        => array(_("emtpy")),
443                                 "Value_List"      => array(_("empty"))) ;
444         if($type == "address"){
445           $values["Address_Part"]    = ":all";
446         }
447         $data[$type]=$values;
449         $this->parent->add_require("relational");
450         if($type == "envelope"){
451           $this->parent->add_require("envelope");
452         }
453     
455         break;
456       }
457       case "allof" :
458       case "anyof" :
459       {
460         $data[$type] = array("Inverse" => FALSE);
461         break;
462       }
463       case "size" :
464       {
465         $tmp= array( 
466             "Inverse"    => FALSE,
467             "Match_type" => ":over",
468             "Value_List" => array("1M"));
470         $tmp['LastError'] = "";
471         $data[$type] = $tmp;
472         break;
473       }
474       case "true":
475       {
476         $data['true'] = "true";
477         $data['true']['LastError'] = "";
478         break;
479       }
480       case "false":
481       {
482         $data['false'] = "false";
483         $data['false']['LastError'] = "";
484         break;
485       }
486       case "exists" :
487       {
488         $data['exists'] = array('Inverse' => FALSE,
489                                 'Values'  => array(_("Nothing specified right now")),
490                                 'LastError' => "");
491         break;
492       }
493       default : echo "Still buggy ";exit;
494     }
496     return($data);
497   }
500   /* Ensure that all changes made on the ui 
501    *  will be saved. 
502    */
503   function save_object()
504   {
505   
506     if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
507       $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
508     }
510     $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
511     $this->_parsed = $tmp;
512   }
515   /* Recursivly save all ui changes for the 
516    *  tags and tokens provided by $parsed.
517    *  $id       specifies the depth of the current element.
518    *  $obj_id   is the current tag-id handled by this function
519    */
520   function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
521   {
522     /* Variable initialization */ 
523     $ret ="";
524     if($parsed == NULL){
525       $parsed = $this->_parsed;
526     }
528     if(!is_array($parsed)) return;
530     /* Walk through all elements */
531     foreach($parsed as $key => $data){
533       /* Id used to have unique html names */
534       $element_id = $this->object_id."_".$id."_".$obj_id;
535       
536       foreach($_POST as $name => $value){
537         if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
538           return(false); 
539         }
540       }
542       
543       if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
544         $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
545       }
547       /* Create elements */
548       switch($key)
549       {
550         /*******************
551          * Address 
552          *******************/
554         case "envelope" :
555         case "header" : 
556         case "address" : 
557         {
558           /* [not] address 
559                         [address-part: tag] 
560                         [comparator: tag] 
561                         [match-type: tag] 
562                         <header-list: string-list> 
563                         <key-list: string-list> 
564           */
566           /* Possible address parts we can select */
567           $address_parts = $this->address_parts;
568           $comparators   = $this->comparators;
569           $match_types   = $this->match_types; 
570           $operators     = $this->operators;
572           $parsed[$key]['LastError'] = "";
574           /* Toggle Inverse ? */
575           if(isset($_POST['toggle_inverse_'.$element_id])){
576             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
577           }
579           /* Check if we want to toggle the expert mode */
580           if(isset($_POST['Toggle_Expert_'.$element_id])){
581             $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
582           }
584           /* Get address part */
585           if(isset($_POST['address_part_'.$element_id])){
586             $ap = $_POST['address_part_'.$element_id];
588             if(!isset($address_parts[$ap])){
589               $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
590             }
591             $parsed[$key]['Address_Part'] = $ap;
592           }
594           /* Check if match type has changed */
595           if(isset($_POST['matchtype_'.$element_id])){
596             $mt = $_POST['matchtype_'.$element_id];
598             if(!isset($match_types[$mt])){
599               $parsed[$key]['LastError'] = _("Invalid match type given.");
600             }
601             if($mt == ":regex"){
602               $this->parent->add_require("regex");
603             }
604             if($mt == ":count"){
605               $this->parent->add_require("comparator-i;ascii-numeric");
606             }
607             $parsed[$key]['Match_type'] = $mt;
608           }
610           /* Get the comparator tag, if posted */
611           if(isset($_POST['comparator_'.$element_id])){
612             $cp = $_POST['comparator_'.$element_id];
614             if(!isset($comparators[$cp])){
615               $parsed[$key]['LastError'] = _("Invalid operator given.");
616             }
617             $parsed[$key]['Comparator'] = $cp;
619             if($cp == "i;ascii-numeric"){
620               $this->parent->add_require("comparator-i;ascii-numeric");
621             }
622           }
624           /* In case of :count and :value match types 
625            *  we have a special match operator we should save.
626            */
627           if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
628             if(isset($_POST['operator_'.$element_id])){
629               $op = $_POST['operator_'.$element_id];
631               if(!isset($operators[$op])){
632                 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
633               }
634               $parsed[$key]['Match_type_value'] = $op;
635             }
636           }
638           /* Get the address fields we should check, they are seperated by , */
639           if(isset($_POST['keys_'.$element_id])){
640             $vls = stripslashes($_POST['keys_'.$element_id]);
641             $tmp = array();
643             $tmp2 = split(",",$vls);
644             foreach($tmp2 as $val){
645               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
646             }
647             $parsed[$key]['Key_List'] = $tmp;
648           }
650           /* Get the values should check for, they are seperated by , */
651           if(isset($_POST['values_'.$element_id])){
652             $vls = stripslashes($_POST['values_'.$element_id]);
653             $tmp = array();
655             $tmp2 = split(",",$vls);
656             foreach($tmp2 as $val){
657               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
658             }
659             $parsed[$key]['Value_List'] = $tmp;
660           }
661           break;
662         }
663  
664         /*******************
665          * TRUE FALSE 
666          *******************/
668         case "true" :
669         case "false" : 
670         {
671           $name = 'boolean_'.$element_id;
672           if(isset($_POST[$name])){
673             $key2 = $_POST[$name];
674             
675             if($key != $key2) {
676               $parsed = array($key2 => $key2); 
677             }
678           }
679           break;
680         }
682         /*******************
683          * Exists 
684          *******************/
686         case "exists" :
687         {
688           /* Toggle Inverse ? */
689           if(isset($_POST['toggle_inverse_'.$element_id])){
690             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
691           }
693           /* get list of match values */
694           if(isset($_POST['Values_'.$element_id])){
695             $vls = stripslashes($_POST['Values_'.$element_id]);
696             $tmp = array();          
697   
698             $tmp2 = split(",",$vls);
699             foreach($tmp2 as $val){
700               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
701             }
702             $parsed['exists']['Values'] = $tmp;
703           }
704           break;
705         }
707         /*******************
708          * Size 
709          *******************/
711         case "size" :
712         {
713           $Match_types = array( ":over" => _("greater than") ,
714                                 ":under" => _("lower than"));
716           $Units       = array( "M" => _("Megabyte"),
717                                 "K" => _("Kilobyte"),
718                                 ""  => _("Bytes"));
720           /* Toggle Inverse ? */
721           if(isset($_POST['toggle_inverse_'.$element_id])){
722             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
723           }
725           /* Reset error */
726           $parsed[$key]['LastError'] ="";
728           /* Get match type */
729           if(isset($_POST['Match_type_'.$element_id])){
730             $mt = $_POST['Match_type_'.$element_id];
731             if(!isset($Match_types[$mt])){
732               $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
733             }
734             $parsed[$key]['Match_type'] = $mt;
735           }
737           /* Get old values */
738           $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
739           $unit  = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
741           /* Get value */
742           if(isset($_POST['Value_'.$element_id])){
743             $vl = $_POST['Value_'.$element_id];
744          
745             if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
746               $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
747             }
748             $value = preg_replace("/[^0-9]/","",$vl);
749           }        
751           /* Get unit */
752           if(isset($_POST['Value_Unit_'.$element_id])){
753             $ut = $_POST['Value_Unit_'.$element_id];
754        
755             if(!isset($Units[$ut])){
756               $parsed[$key]['LastError'] = _("No valid unit selected");
757             }
758             $unit = $ut;
759           }       
760           $parsed[$key]['Value_List'] = array(); 
761           $parsed[$key]['Value_List'][0] = $value.$unit;
762           break;
763         }
765         /*******************
766          * Allof 
767          *******************/
768      
769         case "allof" : 
770         {
771           if(isset($_POST['toggle_inverse_'.$element_id])){
772             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
773           }
774           foreach($data as $key2 => $dat){
775             if(($key2 === "Inverse") && ($key2 == "Inverse")){
776               continue;
777             }
778             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
779             if($tmp_data != false){
780               $parsed[$key][$key2] = $tmp_data;
781             }else{
782               unset( $parsed[$key][$key2]);
783             }
784           }
785           break ;
786         } 
788         /*******************
789          * Anyof 
790          *******************/
791      
792         case "anyof" : 
793         {
794           if(isset($_POST['toggle_inverse_'.$element_id])){
795             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
796           }
797           foreach($data as $key2 => $dat){
798             if(($key2 === "Inverse") && ($key2 == "Inverse")){
799               continue;
800             }
801             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
802             if($tmp_data != false){
803               $parsed[$key][$key2] = $tmp_data;
804             }else{
805               unset( $parsed[$key][$key2]);
806             }
807           }
808           break ;
809         } 
810       }
811     } 
812     return($parsed);
813   }  
816   /* Return html element for IF */ 
817   function execute()
818   {
819     /* Create title */
820     $name  = "<img alt='' src='images/small_filter.png' class='center'>";
821     $name .= "<b>"._("Condition")."</b>";
822     if($this->TYPE == "if"){
823       $name .= "&nbsp;-&nbsp;"._("If");
824     }elseif($this->TYPE == "elsif"){
825       $name .= "&nbsp;-&nbsp;"._("Else if");
826     }else{
827       $name .= "&nbsp;-&nbsp;"._("Else");
828     }
830     $smarty = get_smarty();
831     $smarty->assign("ID", $this->object_id);
833     /* Only display navigation elements if necessary */
834     if($this->TYPE == "if"){
835       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
836     }else{
837       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
838     }
840     $smarty->assign("Name", $name);
841     $smarty->assign("Contents", $this->get_as_html());
843     if($this->TYPE == "if"){
844       $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
845     }else{
846       $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
847     }
848     $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
849     return($str);
850   }
852   
853   /* Returns all elements as html */
854   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
855   {
856     $ret ="";
857     if($parsed == NULL){
858       $parsed = $this->_parsed;
859     }
861     if((!is_array($parsed)) || !count($parsed)) {
862       $smarty = get_smarty();
863       $smarty->assign("ID",$this->object_id);
864       $smarty->assign("DisplayAdd",TRUE);
865       $smarty->assign("DisplayDel",FALSE);
866       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
867       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
868       return($ret);
869     }
871     /* Walk through all elements */
872     foreach($parsed as $key => $data){
874       /* Create Inverse Tag */
875       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
876         $Inverse = TRUE;
877       }else{
878         $Inverse = FALSE;
879       }
881       /* Id used to have unique html names */
882       $element_id = $this->object_id."_".$id."_".$obj_id;
884       /* Create elements */
885       switch($key)
886       {
887   
888         /*******************
889          * TRUE FALSE 
890          *******************/
892         case "true" :
893         case "false" : 
894         { 
895           /* Inverse element if required */
896           if($Inverse){        
897             if($key == "true"){
898               $key = "false";
899             }else{
900               $key = "true";
901             }           
902           }
904           /* Get template */
905           $smarty = get_smarty();
906           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
907           $smarty->assign("selected"  , $key); 
908           $smarty->assign("ID"  , $element_id); 
909           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
910           break;
911         }
914         /*******************
915          * Header 
916          *******************/
918         case "header": 
919         {
920           $address_parts = $this->address_parts;
921           $comparators   = $this->comparators;
922           $match_types   = $this->match_types; 
923           $operators     = $this->operators;
925           $smarty = get_smarty();
926           $smarty->assign("comparators",$comparators);
927           $smarty->assign("match_types",$match_types);
928           $smarty->assign("operators",$operators);
929           $smarty->assign("LastError",$data['LastError']);
930           $smarty->assign("match_type", $data['Match_type']);
931           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
932           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
934           $keys = "";
935           foreach($data['Key_List'] as $key){
936             $keys .= $key.", ";
937           }
938           $keys = preg_replace("/,$/","",trim($keys));
939    
940           $values = "";
941           foreach($data['Value_List'] as $key){
942             $values .= $key.", ";
943           }
944           $values = preg_replace("/,$/","",trim($values));
946           $smarty->assign("keys",$keys);
947           $smarty->assign("Inverse",$Inverse);
948           $smarty->assign("values",$values);
949           $smarty->assign("Expert", $data['Expert']);
950  
951           $smarty->assign("ID"  , $element_id); 
952           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
953           break;
954         }
957         /*******************
958          * Envelope 
959          *******************/
961         case "envelope":
962         {
963           $address_parts = $this->address_parts;
964           $comparators   = $this->comparators;
965           $match_types   = $this->match_types; 
966           $operators     = $this->operators;
968           $smarty = get_smarty();
969           $smarty->assign("Inverse",$Inverse);
970           $smarty->assign("comparators",$comparators);
971           $smarty->assign("Expert", $data['Expert']);
972           $smarty->assign("match_types",$match_types);
973           $smarty->assign("operators",$operators);
974           $smarty->assign("LastError",$data['LastError']);
975           $smarty->assign("match_type", $data['Match_type']);
976           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
977           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
979           $keys = "";
980           foreach($data['Key_List'] as $key){
981             $keys .= $key.", ";
982           }
983           $keys = preg_replace("/,$/","",trim($keys));
985           $values = "";
986           foreach($data['Value_List'] as $key){
987             $values .= $key.", ";
988           }
989           $values = preg_replace("/,$/","",trim($values));
990           $smarty->assign("keys",$keys);
991           $smarty->assign("values",$values);
993           $smarty->assign("ID"  , $element_id); 
994           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
995           break;
996         }
999         /*******************
1000          * Address 
1001          *******************/
1003         case "address" : 
1004         {
1005           $address_parts = $this->address_parts;
1006           $comparators   = $this->comparators;
1007           $match_types   = $this->match_types; 
1008           $operators     = $this->operators;
1010           $smarty = get_smarty();
1011           $smarty->assign("Inverse",$Inverse);
1012           $smarty->assign("address_parts",$address_parts);
1013           $smarty->assign("comparators",$comparators);
1014           $smarty->assign("match_types",$match_types);
1015           $smarty->assign("LastError",$data['LastError']);
1016           $smarty->assign("operators",$operators);
1017           $smarty->assign("match_type", $data['Match_type']);
1018           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
1019           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
1020           $smarty->assign("address_part", $data['Address_Part']);
1021           $smarty->assign("Expert", $data['Expert']);
1022         
1023           $keys = "";
1024           foreach($data['Key_List'] as $key){
1025             $keys .= $key.", ";
1026           }
1027           $keys = preg_replace("/,$/","",trim($keys));
1028    
1029           $values = "";
1030           foreach($data['Value_List'] as $key){
1031             $values .= $key.", ";
1032           }
1033           $values = preg_replace("/,$/","",trim($values));
1035           $smarty->assign("keys",$keys);
1036           $smarty->assign("values", $values);
1037           $smarty->assign("ID"  , $element_id); 
1038           $str = $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1039           $ret .= $str;
1040           break;
1041         }
1042       
1044         /*******************
1045          * Size 
1046          *******************/
1047         
1048         case "size" : 
1049         {
1050           $Match_types = array( ":over" => _("greater than") , 
1051                                 ":under" => _("lower than"));
1053           $Units       = array( "M" => _("Megabyte"),
1054                                 "K" => _("Kilobyte"),
1055                                 ""  => _("Bytes"));
1057           $Match_type   = $data['Match_type'];
1058           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1059           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1060        
1061           $LastError = "";
1062           if(isset($data['LastError'])){
1063             $LastError = $data['LastError'];
1064           }
1065  
1066           $smarty = get_smarty();
1067           $smarty->assign("Inverse",$Inverse);
1068           $smarty->assign("LastError",$LastError);
1069           $smarty->assign("Match_types",$Match_types);
1070           $smarty->assign("Units",$Units);
1071           $smarty->assign("Match_type",$Match_type);
1072           $smarty->assign("Value",$Value);
1073           $smarty->assign("Value_Unit",$Value_Unit);
1074           $smarty->assign("ID"  , $element_id); 
1075           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1076           break;
1077         }
1078         
1079         /*******************
1080          * Exists 
1081          *******************/
1082         
1083         case "exists" : 
1084         {
1085           $LastError = "";
1086           if(isset($data['LastError'])){
1087             $LastError = $data['LastError'];
1088           }
1089  
1090           $Values = "";
1091           foreach($data['Values'] as $val){
1092             $Values .= $val.", ";
1093           }
1094           $Values = preg_replace("/,$/","",trim($Values));
1096           $smarty = get_smarty();
1097           $smarty->assign("LastError",$LastError);
1098           $smarty->assign("Values",$Values);
1099           $smarty->assign("Inverse",$Inverse);
1100           $smarty->assign("ID"  , $element_id); 
1101           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1102           break;
1103         }
1104   
1106         /*******************
1107          * All of   
1108          *******************/
1110         case "allof" : 
1111         {
1112           $Contents = ""; 
1113           foreach($data as $key => $dat){
1114             if(($key === "Inverse") && ($key == "Inverse")){
1115               continue;
1116             }
1117             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1118           }
1120           $smarty = get_smarty();
1121           $smarty->assign("ID"  , $element_id); 
1122           $smarty->assign("DisplayAdd",TRUE);
1123           $smarty->assign("DisplayDel",FALSE);
1124           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1125           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1127           $smarty->assign("Inverse",$Inverse);
1128           $smarty->assign("Contents",$cont_tmp.$Contents);
1129           $smarty->assign("ID"  , $element_id); 
1130           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1132           $ret = $allof_tmp;
1133           break ;
1134         } 
1137         /*******************
1138          * Any of   
1139          *******************/
1141         case "anyof" : 
1142         {
1143           $Contents = ""; 
1144           foreach($data as $key => $dat){
1145             if(($key === "Inverse") && ($key == "Inverse")){
1146               continue;
1147             }
1148             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1149           }
1150           $smarty = get_smarty();
1151           $smarty->assign("ID"  , $element_id); 
1152           $smarty->assign("DisplayAdd",TRUE);
1153           $smarty->assign("DisplayDel",FALSE);
1154           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1155           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1157           $smarty->assign("Inverse",$Inverse);
1158           $smarty->assign("Contents",$cont_tmp.$Contents);
1159           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1161           $ret = $allof_tmp;
1163           break ;
1164         } 
1165         default : 
1166         {
1167           trigger_error(_("Unhandled switch type"));
1168         }
1169       }
1170     }
1171     
1172     if(!isset($smarty)){
1173       $smarty =get_smarty();
1174     }
1176     $smarty->assign("ID",$element_id);
1177     $smarty->assign("DisplayAdd",FALSE);
1178     $smarty->assign("DisplayDel",TRUE);
1179     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1180     $ret = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($ret,"\\"),$str);
1181     return($ret);
1182   }
1185   /* Parse given token identified by $data[$id] 
1186    *  and return the parsed tokens. 
1187    */
1188   function _parse($data,$id = 0)
1189   {
1190     $av_match_type = array();
1191     foreach($this->match_types as $name => $description){
1192       $av_match_type[] = $name;
1193     }
1194     $av_match_type[] = ":over";
1195     $av_match_type[] = ":under";
1199     $av_methods= array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1200     $type = $data[$id]['text'];
1201     $tmp = array();
1203     /* Is there an identifier named 'not' to inverse this filter ? */
1204     $Inverse = FALSE;
1205     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1206       $Inverse = TRUE;
1207       $id ++;
1208       $type = $data[$id]['text'];
1209     }
1211     switch($type)
1212     {
1214       /****************
1215        * Parse - Envelope / Header / Address
1216        ****************/ 
1218       case "envelope" : 
1219       case "header":
1220       case "address" : 
1221       {
1222         /* Address matches are struckture as follows :
1223            [not] 
1224            address 
1225                   [address-part: tag]           all|localpart|domain|user|detail
1226                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1227                   [match-type: tag]             is|contains|matches|count|value 
1228                   <header-list: string-list> 
1229                   <key-list: string-list>   
1230           */ 
1231    
1232         
1233         $part     = "(:all|:localpart|:domain)";
1234         $operator = "(:regex|:contains|:is|:matches|:count|:value)";
1235         $value_op = "(lt|le|eq|ge|gt|ne)";
1237         $Address_Part     = "";
1238         $Comparator       = "";        
1239         $Match_type       = "";    
1240         $Match_type_value = "";
1241   
1242         $Key_List         = array();
1243         $Value_List       = array();
1244   
1245         for($i = 0 ; $i < count($data) ; $i ++){
1246          
1247           /* Get next node */ 
1248           $node = $data[$i];
1249   
1250           /* Check address part definition */
1251           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1252             $Address_Part = $node['text'];
1253           }
1255           /* Check for match type  */
1256           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1257             $Match_type = $node['text'];
1259             /* Get value operator */
1260             if(in_array($Match_type,array(":value",":count"))){
1261               $i ++;        
1262               $node = $data[$i];
1264               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1265                 $Match_type_value = $node['text'];
1266               }
1267             }
1268           } 
1270           /* Check for a comparator */
1271           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1272             $i ++;
1273             $node = $data[$i];
1274             $Comparator = $node['text'];
1275           }
1276   
1277           /* Check for Key_List */  
1278           elseif(count(sieve_get_strings($data,$i))){
1279             $tmp2 = sieve_get_strings($data,$i);
1280             $i =  $tmp2['OFFSET'];
1282             if(!count($Key_List)){
1283               $Key_List = $tmp2['STRINGS'];
1284             }else{
1285               $Value_List = $tmp2['STRINGS']; 
1286             }
1287           } 
1288       
1289         }
1290  
1291          
1292         /* Add to Tree */ 
1293         $values = array( "Inverse"         => $Inverse,
1294                                 "Comparator"      => $Comparator,
1295                                 "Expert"          => FALSE,
1296                                 "Match_type"      => $Match_type,
1297                                 "Match_type_value"=> $Match_type_value,
1298                                 "Key_List"        => $Key_List,
1299                                 "Value_List"      => $Value_List) ;
1300         if($type == "address"){
1301           $values["Address_Part"]    = $Address_Part;
1302         }
1303         $tmp[$type] = $values;
1304         $tmp[$type]['LastError'] = "";
1305         break;
1306       }
1309       /****************
1310        * Parse - Size
1311        ****************/ 
1313       case "size":
1314       {
1315     
1316         $ops = "(:over|:under)";
1318         $Match_type = "";
1320         for($i = $id ; $i < count($data); $i++){
1322           /* Get current node */
1323           $node = $data[$i];
1325           /* Get tag (under / over) */
1326           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1327             $Match_type = $node['text'];
1328           }
1329           
1330           /* Get Value_List, the value that we want to match for */
1331           elseif(count(sieve_get_strings($data,$i))){
1332             $tmp2 = sieve_get_strings($data,$i);
1333             $i =  $tmp2['OFFSET'];
1334           
1335             $Value_List = $tmp2['STRINGS'];
1336           } 
1337         }        
1338     
1339         $tmp[$type]= array( "Inverse"    => $Inverse,
1340                             "Match_type" => $Match_type,
1341                             "Value_List" => $Value_List);
1342         $tmp[$type]['LastError'] = "";
1343         break;
1344       }
1347       /****************
1348        * Parse - True / False
1349        ****************/ 
1351       case "true": 
1352       {
1353         $tmp['true'] = "true";
1354         $tmp['true']['LastError'] = "";
1355         break;
1356       }
1357       case "false":
1358       {
1359         $tmp['false'] = "false";
1360         $tmp['false']['LastError'] = "";
1361         break;
1362       }
1365       /****************
1366        * Parse - Exists
1367        ****************/ 
1369       case "exists":
1370       {
1371         
1372         /* Skip first values, [if,not,exists] */
1373         $node = $data[$id];
1374         while(in_array($node['text'],array("if","not","exists"))){
1375           $id ++;
1376           $node = $data[$id];
1377         }
1379         /* Get values */
1380         $tmp2 = sieve_get_strings($data,$id);
1381   
1382         
1383         $tmp['exists'] = array('Inverse' => $Inverse,
1384                                'Values'  => $tmp2['STRINGS']);
1385         $tmp[$type]['LastError'] = "";
1386         break;
1387       }
1390       /****************
1391        * Parse - Allof
1392        ****************/ 
1394       case "allof" :
1395       {
1396         /* Get parameter and recursivly call this method 
1397          *  for each parameter 
1398          */
1399         $id ++;
1400         $tmp2 = $this->get_parameter($data,$id);
1401         
1402         foreach($tmp2 as $parameter){
1403           $tmp['allof'][] = $this->_parse($parameter);
1404         }
1405         $tmp['allof']['Inverse'] = $Inverse;
1406         break;
1407       }
1410       /****************
1411        * Parse - Anyof
1412        ****************/ 
1414       case "anyof" :
1415       {
1416         /* Get parameter and recursivly call this method 
1417          *  for each parameter 
1418          */
1419         $id ++;
1420         $tmp2 = $this->get_parameter($data,$id);
1422         foreach($tmp2 as $parameter){
1423           $tmp['anyof'][] = $this->_parse($parameter);
1424         }
1425         $tmp['anyof']['Inverse'] = $Inverse;
1426         break;
1427       }
1428       default : $tmp[$id] = $type; 
1429     }
1430     
1431     return($tmp); 
1432   }
1435   function get_parameter($data,$id)
1436   {
1437     $par = array();
1438     $open_brakets = 0;
1439     $next = NULL;
1440     $num = 0;
1441     for($i = $id ; $i < count($data) ; $i++ ){
1442       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1443         $open_brakets ++;
1444       }
1445       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1446         $num ++;
1447       }
1448       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1449         $par[$num][] = $data[$i];
1450       }
1451       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1452         $open_brakets --;
1453       }
1454     }
1455     return($par);
1456   }
1459 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1460 ?>