Code

56a25b385855c15872b76246ead4401f33d20d24
[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         ":regex"      => _("regex"),
40         ":contains"   => _("contains"),
41         ":matches"    => _("matches"),
42         ":count"      => _("count"),
43         ":value"      => _("value is"));
45     /* Operators */
46     $this->operators = array(     
47         "lt"  => _("less than"),
48         "le"  => _("less or equal"),
49         "eq"  => _("equals"),
50         "ge"  => _("greater or equal"),
51         "gt"  => _("greater than"),
52         "ne"  => _("not equal"));
54     $this->object_id       = $object_id;
55     if($elements!=NULL){
56       $this->elements = $elements;
57       $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
58     }
59   }
62   /* Returns the sieve script for this 
63    *  if/else tag.
64    */
65   function get_sieve_script_part()
66   {
67     $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
68     return($tmp);
69   } 
72   /* Return error msgs */
73   function check()
74   {
75     $check = $this->check_recursive();
76     return($check);
77   }
78  
80   /* Recursivly fetch all error msgs */
81   function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
82   {
83     $ret = array();
84     if($parsed == NULL){
85       $parsed = $this->_parsed;
86     }
88     /* Walk through all elements */
89     foreach($parsed as $key => $data){
91       /* Create elements */
92       switch($key)
93       {
94         /*******************
95          * Allof / Anyof
96          *******************/
97         case "anyof" :
98         case "allof" :
99         { 
100           foreach($data as $key2 => $dat){
101             if(($key2 === "Inverse") && ($key2 == "Inverse")){
102               continue;
103             }
104             $msgs = $this->check_recursive($dat, ($id +1),$key2);
106             foreach($msgs as $msg){
107               $ret[] = $msg;
108             }
109           }
110           break;
111         }
113         /*******************
114          * True / False
115          *******************/
117         case "true" :
118         case "false" : 
119         {
120           /* Can't fail anyway */
121           break;
122         }
123     
124         /*******************
125          * Default
126          *******************/
128         default: 
129         {
130           if(isset($data['LastError']) && !empty($data['LastError'])){
131             $ret[] = $data['LastError'];
132           }
133         }
134       }
135     }
136     return($ret);
137   }
138  
140   /* Recursivly create a sieve script out of the given 
141    *  tags and tokens provided by $parsed.
142    *  $id       specifies the depth of the current element.
143    *  $obj_id   is the current tag-id handled by this function
144    */
145   function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
146   {
147     $script ="";
148     if($parsed == NULL){
149       $parsed = $this->_parsed;
150     }
153     if(!is_array($parsed)){
154       return;
155     }
157     /* Walk through all elements */
158     foreach($parsed as $key => $data){
160       /* Create Inverse Tag */
161       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
162         $Inverse = TRUE;
163       }else{
164         $Inverse = FALSE;
165       }
167       /* Create elements */
168       switch($key)
169       {
171         /*******************
172          * True / False
173          *******************/
175         case "true" :
176         case "false" :
177         {
178           /* Invert this test if required */
179           if($Inverse){
180             $script .= "not ";
181           }
182           $script .= $key;
183           break;
184         }
187         /*******************
188          * Address
189          *******************/
191         case "address" :   
192         {
193           /* [not] address 
194                         [address-part: tag] 
195                         [comparator: tag] 
196                         [match-type: tag] 
197                         <header-list: string-list> 
198                         <key-list: string-list> 
199           */
201           /* Invert this test if required */
202           if($Inverse){
203             $script .= "not ";
204           }
205   
206           $script .="address ";
207  
208           /* Add address part tag */ 
209           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
210             $script .= $data['Address_Part']." ";
211           }
213           /* Add comparator */
214           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
215             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
216           }
217     
218           /* Add match type */
219           $script .= $data['Match_type']." ";
221           /* Add special match type for count and value */
222           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
223             $script .= sieve_create_strings($data['Match_type_value'])." ";
224           }
226           $script .= sieve_create_strings($data['Key_List']);
227           $script .= " ";
228           $script .= sieve_create_strings($data['Value_List']);
229           break;
230         }
233         /*******************
234          * Header
235          *******************/
237         case "header" :   
238         {
239           /* [not] header   
240                 [comparator: tag] 
241                 [match-type: tag] 
242                 <header-names: string-list> 
243                 <key-list: string-list>
244           */
246           /* Invert ? */
247           if($Inverse){
248             $script .= "not ";
249           }
250   
251           $script .="header ";
252  
253           /* Add address part tag */ 
254           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
255             $script .= $data['Address_Part']." ";
256           }
258           /* Add comparator */
259           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
260             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
261           }
262     
263           /* Add match type */
264           $script .= $data['Match_type']." ";
266           /* Add special match type for count and value */
267           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
268             $script .= sieve_create_strings($data['Match_type_value'])." ";
269           }
271           $script .= sieve_create_strings($data['Key_List']);
272           $script .= " ";
273           $script .= sieve_create_strings($data['Value_List']);
274           break;
275         }
278         /*******************
279          * Envelope
280          *******************/
282         case "envelope" :   
283         {
284           /* [not]  envelope 
285                     [address-part: tag] 
286                     [comparator: tag] 
287                     [match-type: tag] 
288                     <envelope-part: string-list> 
289                     <key-list: string-list> 
290           */
292           /* Invert */
293           if($Inverse){
294             $script .= "not ";
295           }
296   
297           $script .="envelope ";
298  
299           /* Add address part tag */ 
300           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
301             $script .= $data['Address_Part']." ";
302           }
304           /* Add comparator */
305           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
306             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
307           }
308     
309           /* Add match type */
310           $script .= $data['Match_type']." ";
312           /* Add special match type for count and value */
313           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
314             $script .= sieve_create_strings($data['Match_type_value'])." ";
315           }
317           $script .= sieve_create_strings($data['Key_List']);
318           $script .= " ";
319           $script .= sieve_create_strings($data['Value_List']);
320           break;
321         }
324         /*******************
325          * Exists
326          *******************/
327         case "exists" : 
328         {
329           /* [not] exists 
330               <header-names: string-list> 
331           */
333           /* Invert ? */
334           if($Inverse){
335             $script .= "not ";
336           }
338           $script .= "exists ".sieve_create_strings($data['Values']);
339           break;
340         }
343         /*******************
344          * Size
345          *******************/
346         case "size" : 
347         {
348           /* [not] size 
349                 <":over" / ":under"> 
350                 <limit: number> 
351           */
353           /* Invert ? */
354           if($Inverse){
355             $script .= "not ";
356           }
357  
358           /* Add size test */ 
359           $script .="size ";
360           $script .=$data['Match_type']." ";
361           foreach($data['Value_List'] as $val){
362             $script .= $val." ";
363           }
364           break;
365         }
368         /*******************
369          * Allof
370          *******************/
371         case "anyof" :
372         case "allof" :
373         {
374           /* allof <tests: test-list>
375              anyof <tests: test-list> */
377  
378           /* Add spaces, to indent the code.*/ 
379           $block = "\n";
380           for($i = 0 ; $i < $id ; $i ++){
381             $block .= SIEVE_INDENT_TAB;
382           }          
384           /* Add allof/anyof tag */
385           if($Inverse){
386             $script .= "not ";
387           }
388           $script.= $key." ( ";
390           /* Add each test parameter */
391           foreach($data as $key2 => $dat){
392             if(($key2 === "Inverse") && ($key2 == "Inverse")){
393               continue;
394             }
395             $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
396           }
397     
398           /* Remove last _,_ and close the tag */
399           $script = preg_replace("/,$/","",trim($script));
400           $script.= $block.")";
401           break ;
402         }
404         default :
405         {
406           $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
407         }
408       }
409     }
410     return($script);
411   }
413   
414   function add_test($data,$type)
415   {
416     switch($type)
417     {
418       case "header" : 
419       case "address" : 
420       case "envelope" : 
421       {
422         /* Add to Tree */
423         $values = array(        "Inverse"         => FALSE,
424                                 "Comparator"      => "",
425                                 "Expert"          => FALSE,
426                                 "LastError"       => "",
427                                 "Match_type"      => ":contains",
428                                 "Match_type_value"=> "",
429                                 "Key_List"        => array(_("emtpy")),
430                                 "Value_List"      => array(_("empty"))) ;
431         if($type == "address"){
432           $values["Address_Part"]    = ":all";
433         }
434         $data[$type]=$values;
436         $this->parent->add_require("relational");
437         if($type == "envelope"){
438           $this->parent->add_require("envelope");
439         }
440     
442         break;
443       }
444       case "allof" :
445       case "anyof" :
446       {
447         $data[$type] = array("Inverse" => FALSE);
448         break;
449       }
450       case "size" :
451       {
452         $tmp= array( 
453             "Inverse"    => FALSE,
454             "Match_type" => ":over",
455             "Value_List" => array("1M"));
457         $tmp['LastError'] = "";
458         $data[$type] = $tmp;
459         break;
460       }
461       case "true":
462       {
463         $data['true'] = "true";
464         $data['true']['LastError'] = "";
465         break;
466       }
467       case "false":
468       {
469         $data['false'] = "false";
470         $data['false']['LastError'] = "";
471         break;
472       }
473       case "exists" :
474       {
475         $data['exists'] = array('Inverse' => FALSE,
476                                 'Values'  => array(_("Nothing specified right now")),
477                                 'LastError' => "");
478         break;
479       }
480       default : echo "Still buggy ";exit;
481     }
483     return($data);
484   }
487   /* Ensure that all changes made on the ui 
488    *  will be saved. 
489    */
490   function save_object()
491   {
492   
493     if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
494       $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
495     }
497     $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
498     $this->_parsed = $tmp;
499   }
502   /* Recursivly save all ui changes for the 
503    *  tags and tokens provided by $parsed.
504    *  $id       specifies the depth of the current element.
505    *  $obj_id   is the current tag-id handled by this function
506    */
507   function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
508   {
509     /* Variable initialization */ 
510     $ret ="";
511     if($parsed == NULL){
512       $parsed = $this->_parsed;
513     }
515     if(!is_array($parsed)) return;
517     /* Walk through all elements */
518     foreach($parsed as $key => $data){
520       /* Id used to have unique html names */
521       $element_id = $this->object_id."_".$id."_".$obj_id;
522       
523       foreach($_POST as $name => $value){
524         if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
525           return(false); 
526         }
527       }
529       
530       if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
531         $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
532       }
534       /* Create elements */
535       switch($key)
536       {
537         /*******************
538          * Address 
539          *******************/
541         case "envelope" :
542         case "header" : 
543         case "address" : 
544         {
545           /* [not] address 
546                         [address-part: tag] 
547                         [comparator: tag] 
548                         [match-type: tag] 
549                         <header-list: string-list> 
550                         <key-list: string-list> 
551           */
553           /* Possible address parts we can select */
554           $address_parts = $this->address_parts;
555           $comparators   = $this->comparators;
556           $match_types   = $this->match_types; 
557           $operators     = $this->operators;
559           $parsed[$key]['LastError'] = "";
561           /* Toggle Inverse ? */
562           if(isset($_POST['toggle_inverse_'.$element_id])){
563             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
564           }
566           /* Check if we want to toggle the expert mode */
567           if(isset($_POST['Toggle_Expert_'.$element_id])){
568             $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
569           }
571           /* Get address part */
572           if(isset($_POST['address_part_'.$element_id])){
573             $ap = $_POST['address_part_'.$element_id];
575             if(!isset($address_parts[$ap])){
576               $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
577             }
578             $parsed[$key]['Address_Part'] = $ap;
579           }
581           /* Check if match type has changed */
582           if(isset($_POST['matchtype_'.$element_id])){
583             $mt = $_POST['matchtype_'.$element_id];
585             if(!isset($match_types[$mt])){
586               $parsed[$key]['LastError'] = _("Invalid match type given.");
587             }
588               if($mt == ":regex"){
589                 $this->parent->add_require("regex");
590               }
591             $parsed[$key]['Match_type'] = $mt;
592           }
594           /* Get the comparator tag, if posted */
595           if(isset($_POST['comparator_'.$element_id])){
596             $cp = $_POST['comparator_'.$element_id];
598             if(!isset($comparators[$cp])){
599               $parsed[$key]['LastError'] = _("Invalid operator given.");
600             }
601             $parsed[$key]['Comparator'] = $cp;
603             if($cp == "i;ascii-numeric"){
604               $this->parent->add_require("comparator-i;ascii-numeric");
605             }
606           }
608           /* In case of :count and :value match types 
609            *  we have a special match operator we should save.
610            */
611           if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
612             if(isset($_POST['operator_'.$element_id])){
613               $op = $_POST['operator_'.$element_id];
615               if(!isset($operators[$op])){
616                 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
617               }
618               $parsed[$key]['Match_type_value'] = $op;
619             }
620           }
622           /* Get the address fields we should check, they are seperated by , */
623           if(isset($_POST['keys_'.$element_id])){
624             $vls = stripslashes($_POST['keys_'.$element_id]);
625             $tmp = array();
627             $tmp2 = split(",",$vls);
628             foreach($tmp2 as $val){
629               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
630             }
631             $parsed[$key]['Key_List'] = $tmp;
632           }
634           /* Get the values should check for, they are seperated by , */
635           if(isset($_POST['values_'.$element_id])){
636             $vls = stripslashes($_POST['values_'.$element_id]);
637             $tmp = array();
639             $tmp2 = split(",",$vls);
640             foreach($tmp2 as $val){
641               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
642             }
643             $parsed[$key]['Value_List'] = $tmp;
644           }
645           break;
646         }
647  
648         /*******************
649          * TRUE FALSE 
650          *******************/
652         case "true" :
653         case "false" : 
654         {
655           $name = 'boolean_'.$element_id;
656           if(isset($_POST[$name])){
657             $key2 = $_POST[$name];
658             
659             if($key != $key2) {
660               $parsed = array($key2 => $key2); 
661             }
662           }
663           break;
664         }
666         /*******************
667          * Exists 
668          *******************/
670         case "exists" :
671         {
672           /* Toggle Inverse ? */
673           if(isset($_POST['toggle_inverse_'.$element_id])){
674             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
675           }
677           /* get list of match values */
678           if(isset($_POST['Values_'.$element_id])){
679             $vls = stripslashes($_POST['Values_'.$element_id]);
680             $tmp = array();          
681   
682             $tmp2 = split(",",$vls);
683             foreach($tmp2 as $val){
684               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
685             }
686             $parsed['exists']['Values'] = $tmp;
687           }
688           break;
689         }
691         /*******************
692          * Size 
693          *******************/
695         case "size" :
696         {
697           $Match_types = array( ":over" => _("greater than") ,
698                                 ":under" => _("lower than"));
700           $Units       = array( "M" => _("Megabyte"),
701                                 "K" => _("Kilobyte"),
702                                 ""  => _("Bytes"));
704           /* Toggle Inverse ? */
705           if(isset($_POST['toggle_inverse_'.$element_id])){
706             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
707           }
709           /* Reset error */
710           $parsed[$key]['LastError'] ="";
712           /* Get match type */
713           if(isset($_POST['Match_type_'.$element_id])){
714             $mt = $_POST['Match_type_'.$element_id];
715             if(!isset($Match_types[$mt])){
716               $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
717             }
718             $parsed[$key]['Match_type'] = $mt;
719           }
721           /* Get old values */
722           $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
723           $unit  = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
725           /* Get value */
726           if(isset($_POST['Value_'.$element_id])){
727             $vl = $_POST['Value_'.$element_id];
728          
729             if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
730               $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
731             }
732             $value = preg_replace("/[^0-9]/","",$vl);
733           }        
735           /* Get unit */
736           if(isset($_POST['Value_Unit_'.$element_id])){
737             $ut = $_POST['Value_Unit_'.$element_id];
738        
739             if(!isset($Units[$ut])){
740               $parsed[$key]['LastError'] = _("No valid unit selected");
741             }
742             $unit = $ut;
743           }       
744           $parsed[$key]['Value_List'] = array(); 
745           $parsed[$key]['Value_List'][0] = $value.$unit;
746           break;
747         }
749         /*******************
750          * Allof 
751          *******************/
752      
753         case "allof" : 
754         {
755           if(isset($_POST['toggle_inverse_'.$element_id])){
756             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
757           }
758           foreach($data as $key2 => $dat){
759             if(($key2 === "Inverse") && ($key2 == "Inverse")){
760               continue;
761             }
762             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
763             if($tmp_data != false){
764               $parsed[$key][$key2] = $tmp_data;
765             }else{
766               unset( $parsed[$key][$key2]);
767             }
768           }
769           break ;
770         } 
772         /*******************
773          * Anyof 
774          *******************/
775      
776         case "anyof" : 
777         {
778           if(isset($_POST['toggle_inverse_'.$element_id])){
779             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
780           }
781           foreach($data as $key2 => $dat){
782             if(($key2 === "Inverse") && ($key2 == "Inverse")){
783               continue;
784             }
785             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
786             if($tmp_data != false){
787               $parsed[$key][$key2] = $tmp_data;
788             }else{
789               unset( $parsed[$key][$key2]);
790             }
791           }
792           break ;
793         } 
794       }
795     } 
796     return($parsed);
797   }  
800   /* Return html element for IF */ 
801   function execute()
802   {
803     /* Create title */
804     $name  = "<img alt='' src='images/small_filter.png' class='center'>";
805     $name .= "<b>"._("Condition")."</b>";
806     if($this->TYPE == "if"){
807       $name .= "&nbsp;-&nbsp;"._("If");
808     }elseif($this->TYPE == "elsif"){
809       $name .= "&nbsp;-&nbsp;"._("Else if");
810     }else{
811       $name .= "&nbsp;-&nbsp;"._("Else");
812     }
814     $smarty = get_smarty();
815     $smarty->assign("ID", $this->object_id);
817     /* Only display navigation elements if necessary */
818     if($this->TYPE == "if"){
819       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
820     }else{
821       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
822     }
824     $smarty->assign("Name", $name);
825     $smarty->assign("Contents", $this->get_as_html());
827     if($this->TYPE == "if"){
828       $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
829     }else{
830       $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
831     }
832     $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
833     return($str);
834   }
836   
837   /* Returns all elements as html */
838   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
839   {
840     $ret ="";
841     if($parsed == NULL){
842       $parsed = $this->_parsed;
843     }
845     if((!is_array($parsed)) || !count($parsed)) {
846       $smarty = get_smarty();
847       $smarty->assign("ID",$this->object_id);
848       $smarty->assign("DisplayAdd",TRUE);
849       $smarty->assign("DisplayDel",FALSE);
850       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
851       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
852       return($ret);
853     }
855     /* Walk through all elements */
856     foreach($parsed as $key => $data){
858       /* Create Inverse Tag */
859       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
860         $Inverse = TRUE;
861       }else{
862         $Inverse = FALSE;
863       }
865       /* Id used to have unique html names */
866       $element_id = $this->object_id."_".$id."_".$obj_id;
868       /* Create elements */
869       switch($key)
870       {
871   
872         /*******************
873          * TRUE FALSE 
874          *******************/
876         case "true" :
877         case "false" : 
878         { 
879           /* Inverse element if required */
880           if($Inverse){        
881             if($key == "true"){
882               $key = "false";
883             }else{
884               $key = "true";
885             }           
886           }
888           /* Get template */
889           $smarty = get_smarty();
890           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
891           $smarty->assign("selected"  , $key); 
892           $smarty->assign("ID"  , $element_id); 
893           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
894           break;
895         }
898         /*******************
899          * Header 
900          *******************/
902         case "header": 
903         {
904           $address_parts = $this->address_parts;
905           $comparators   = $this->comparators;
906           $match_types   = $this->match_types; 
907           $operators     = $this->operators;
909           $smarty = get_smarty();
910           $smarty->assign("comparators",$comparators);
911           $smarty->assign("match_types",$match_types);
912           $smarty->assign("operators",$operators);
913           $smarty->assign("LastError",$data['LastError']);
914           $smarty->assign("match_type", $data['Match_type']);
915           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
916           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
918           $keys = "";
919           foreach($data['Key_List'] as $key){
920             $keys .= $key.", ";
921           }
922           $keys = preg_replace("/,$/","",trim($keys));
923    
924           $values = "";
925           foreach($data['Value_List'] as $key){
926             $values .= $key.", ";
927           }
928           $values = preg_replace("/,$/","",trim($values));
930           $smarty->assign("keys",$keys);
931           $smarty->assign("Inverse",$Inverse);
932           $smarty->assign("values",$values);
933           $smarty->assign("Expert", $data['Expert']);
934  
935           $smarty->assign("ID"  , $element_id); 
936           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
937           break;
938         }
941         /*******************
942          * Envelope 
943          *******************/
945         case "envelope":
946         {
947           $address_parts = $this->address_parts;
948           $comparators   = $this->comparators;
949           $match_types   = $this->match_types; 
950           $operators     = $this->operators;
952           $smarty = get_smarty();
953           $smarty->assign("Inverse",$Inverse);
954           $smarty->assign("comparators",$comparators);
955           $smarty->assign("Expert", $data['Expert']);
956           $smarty->assign("match_types",$match_types);
957           $smarty->assign("operators",$operators);
958           $smarty->assign("LastError",$data['LastError']);
959           $smarty->assign("match_type", $data['Match_type']);
960           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
961           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
963           $keys = "";
964           foreach($data['Key_List'] as $key){
965             $keys .= $key.", ";
966           }
967           $keys = preg_replace("/,$/","",trim($keys));
969           $values = "";
970           foreach($data['Value_List'] as $key){
971             $values .= $key.", ";
972           }
973           $values = preg_replace("/,$/","",trim($values));
974           $smarty->assign("keys",$keys);
975           $smarty->assign("values",$values);
977           $smarty->assign("ID"  , $element_id); 
978           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
979           break;
980         }
983         /*******************
984          * Address 
985          *******************/
987         case "address" : 
988         {
989           $address_parts = $this->address_parts;
990           $comparators   = $this->comparators;
991           $match_types   = $this->match_types; 
992           $operators     = $this->operators;
994           $smarty = get_smarty();
995           $smarty->assign("Inverse",$Inverse);
996           $smarty->assign("address_parts",$address_parts);
997           $smarty->assign("comparators",$comparators);
998           $smarty->assign("match_types",$match_types);
999           $smarty->assign("LastError",$data['LastError']);
1000           $smarty->assign("operators",$operators);
1001           $smarty->assign("match_type", $data['Match_type']);
1002           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
1003           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
1004           $smarty->assign("address_part", $data['Address_Part']);
1005           $smarty->assign("Expert", $data['Expert']);
1006         
1007           $keys = "";
1008           foreach($data['Key_List'] as $key){
1009             $keys .= $key.", ";
1010           }
1011           $keys = preg_replace("/,$/","",trim($keys));
1012    
1013           $values = "";
1014           foreach($data['Value_List'] as $key){
1015             $values .= $key.", ";
1016           }
1017           $values = preg_replace("/,$/","",trim($values));
1019           $smarty->assign("keys",$keys);
1020           $smarty->assign("values", $values);
1021           $smarty->assign("ID"  , $element_id); 
1022           $str = $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1023           $ret .= $str;
1024           break;
1025         }
1026       
1028         /*******************
1029          * Size 
1030          *******************/
1031         
1032         case "size" : 
1033         {
1034           $Match_types = array( ":over" => _("greater than") , 
1035                                 ":under" => _("lower than"));
1037           $Units       = array( "M" => _("Megabyte"),
1038                                 "K" => _("Kilobyte"),
1039                                 ""  => _("Bytes"));
1041           $Match_type   = $data['Match_type'];
1042           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1043           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1044        
1045           $LastError = "";
1046           if(isset($data['LastError'])){
1047             $LastError = $data['LastError'];
1048           }
1049  
1050           $smarty = get_smarty();
1051           $smarty->assign("Inverse",$Inverse);
1052           $smarty->assign("LastError",$LastError);
1053           $smarty->assign("Match_types",$Match_types);
1054           $smarty->assign("Units",$Units);
1055           $smarty->assign("Match_type",$Match_type);
1056           $smarty->assign("Value",$Value);
1057           $smarty->assign("Value_Unit",$Value_Unit);
1058           $smarty->assign("ID"  , $element_id); 
1059           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1060           break;
1061         }
1062         
1063         /*******************
1064          * Exists 
1065          *******************/
1066         
1067         case "exists" : 
1068         {
1069           $LastError = "";
1070           if(isset($data['LastError'])){
1071             $LastError = $data['LastError'];
1072           }
1073  
1074           $Values = "";
1075           foreach($data['Values'] as $val){
1076             $Values .= $val.", ";
1077           }
1078           $Values = preg_replace("/,$/","",trim($Values));
1080           $smarty = get_smarty();
1081           $smarty->assign("LastError",$LastError);
1082           $smarty->assign("Values",$Values);
1083           $smarty->assign("Inverse",$Inverse);
1084           $smarty->assign("ID"  , $element_id); 
1085           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1086           break;
1087         }
1088   
1090         /*******************
1091          * All of   
1092          *******************/
1094         case "allof" : 
1095         {
1096           $Contents = ""; 
1097           foreach($data as $key => $dat){
1098             if(($key === "Inverse") && ($key == "Inverse")){
1099               continue;
1100             }
1101             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1102           }
1104           $smarty = get_smarty();
1105           $smarty->assign("ID"  , $element_id); 
1106           $smarty->assign("DisplayAdd",TRUE);
1107           $smarty->assign("DisplayDel",FALSE);
1108           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1109           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1111           $smarty->assign("Inverse",$Inverse);
1112           $smarty->assign("Contents",$cont_tmp.$Contents);
1113           $smarty->assign("ID"  , $element_id); 
1114           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1116           $ret = $allof_tmp;
1117           break ;
1118         } 
1121         /*******************
1122          * Any of   
1123          *******************/
1125         case "anyof" : 
1126         {
1127           $Contents = ""; 
1128           foreach($data as $key => $dat){
1129             if(($key === "Inverse") && ($key == "Inverse")){
1130               continue;
1131             }
1132             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1133           }
1134           $smarty = get_smarty();
1135           $smarty->assign("ID"  , $element_id); 
1136           $smarty->assign("DisplayAdd",TRUE);
1137           $smarty->assign("DisplayDel",FALSE);
1138           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1139           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1141           $smarty->assign("Inverse",$Inverse);
1142           $smarty->assign("Contents",$cont_tmp.$Contents);
1143           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1145           $ret = $allof_tmp;
1147           break ;
1148         } 
1149         default : 
1150         {
1151           trigger_error(_("Unhandled switch type"));
1152         }
1153       }
1154     }
1155     
1156     if(!isset($smarty)){
1157       $smarty =get_smarty();
1158     }
1160     $smarty->assign("ID",$element_id);
1161     $smarty->assign("DisplayAdd",FALSE);
1162     $smarty->assign("DisplayDel",TRUE);
1163     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1164     $ret = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($ret,"\\"),$str);
1165     return($ret);
1166   }
1169   /* Parse given token identified by $data[$id] 
1170    *  and return the parsed tokens. 
1171    */
1172   function _parse($data,$id = 0)
1173   {
1174     $av_match_type = array();
1175     foreach($this->match_types as $name => $description){
1176       $av_match_type[] = $name;
1177     }
1178     $av_match_type[] = ":over";
1179     $av_match_type[] = ":under";
1183     $av_methods= array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1184     $type = $data[$id]['text'];
1185     $tmp = array();
1187     /* Is there an identifier named 'not' to inverse this filter ? */
1188     $Inverse = FALSE;
1189     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1190       $Inverse = TRUE;
1191       $id ++;
1192       $type = $data[$id]['text'];
1193     }
1195     switch($type)
1196     {
1198       /****************
1199        * Parse - Envelope / Header / Address
1200        ****************/ 
1202       case "envelope" : 
1203       case "header":
1204       case "address" : 
1205       {
1206         /* Address matches are struckture as follows :
1207            [not] 
1208            address 
1209                   [address-part: tag]           all|localpart|domain|user|detail
1210                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1211                   [match-type: tag]             is|contains|matches|count|value 
1212                   <header-list: string-list> 
1213                   <key-list: string-list>   
1214           */ 
1215    
1216         
1217         $part     = "(:all|:localpart|:domain)";
1218         $operator = "(:regex|:contains|:is|:matches|:count|:value)";
1219         $value_op = "(lt|le|eq|ge|gt|ne)";
1221         $Address_Part     = "";
1222         $Comparator       = "";        
1223         $Match_type       = "";    
1224         $Match_type_value = "";
1225   
1226         $Key_List         = array();
1227         $Value_List       = array();
1228   
1229         for($i = 0 ; $i < count($data) ; $i ++){
1230          
1231           /* Get next node */ 
1232           $node = $data[$i];
1233   
1234           /* Check address part definition */
1235           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1236             $Address_Part = $node['text'];
1237           }
1239           /* Check for match type  */
1240           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1241             $Match_type = $node['text'];
1243             /* Get value operator */
1244             if(in_array($Match_type,array(":value",":count"))){
1245               $i ++;        
1246               $node = $data[$i];
1248               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1249                 $Match_type_value = $node['text'];
1250               }
1251             }
1252           } 
1254           /* Check for a comparator */
1255           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1256             $i ++;
1257             $node = $data[$i];
1258             $Comparator = $node['text'];
1259           }
1260   
1261           /* Check for Key_List */  
1262           elseif(count(sieve_get_strings($data,$i))){
1263             $tmp2 = sieve_get_strings($data,$i);
1264             $i =  $tmp2['OFFSET'];
1266             if(!count($Key_List)){
1267               $Key_List = $tmp2['STRINGS'];
1268             }else{
1269               $Value_List = $tmp2['STRINGS']; 
1270             }
1271           } 
1272       
1273         }
1274  
1275          
1276         /* Add to Tree */ 
1277         $values = array( "Inverse"         => $Inverse,
1278                                 "Comparator"      => $Comparator,
1279                                 "Expert"          => FALSE,
1280                                 "Match_type"      => $Match_type,
1281                                 "Match_type_value"=> $Match_type_value,
1282                                 "Key_List"        => $Key_List,
1283                                 "Value_List"      => $Value_List) ;
1284         if($type == "address"){
1285           $values["Address_Part"]    = $Address_Part;
1286         }
1287         $tmp[$type] = $values;
1288         $tmp[$type]['LastError'] = "";
1289         break;
1290       }
1293       /****************
1294        * Parse - Size
1295        ****************/ 
1297       case "size":
1298       {
1299     
1300         $ops = "(:over|:under)";
1302         $Match_type = "";
1304         for($i = $id ; $i < count($data); $i++){
1306           /* Get current node */
1307           $node = $data[$i];
1309           /* Get tag (under / over) */
1310           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1311             $Match_type = $node['text'];
1312           }
1313           
1314           /* Get Value_List, the value that we want to match for */
1315           elseif(count(sieve_get_strings($data,$i))){
1316             $tmp2 = sieve_get_strings($data,$i);
1317             $i =  $tmp2['OFFSET'];
1318           
1319             $Value_List = $tmp2['STRINGS'];
1320           } 
1321         }        
1322     
1323         $tmp[$type]= array( "Inverse"    => $Inverse,
1324                             "Match_type" => $Match_type,
1325                             "Value_List" => $Value_List);
1326         $tmp[$type]['LastError'] = "";
1327         break;
1328       }
1331       /****************
1332        * Parse - True / False
1333        ****************/ 
1335       case "true": 
1336       {
1337         $tmp['true'] = "true";
1338         $tmp['true']['LastError'] = "";
1339         break;
1340       }
1341       case "false":
1342       {
1343         $tmp['false'] = "false";
1344         $tmp['false']['LastError'] = "";
1345         break;
1346       }
1349       /****************
1350        * Parse - Exists
1351        ****************/ 
1353       case "exists":
1354       {
1355         
1356         /* Skip first values, [if,not,exists] */
1357         $node = $data[$id];
1358         while(in_array($node['text'],array("if","not","exists"))){
1359           $id ++;
1360           $node = $data[$id];
1361         }
1363         /* Get values */
1364         $tmp2 = sieve_get_strings($data,$id);
1365   
1366         
1367         $tmp['exists'] = array('Inverse' => $Inverse,
1368                                'Values'  => $tmp2['STRINGS']);
1369         $tmp[$type]['LastError'] = "";
1370         break;
1371       }
1374       /****************
1375        * Parse - Allof
1376        ****************/ 
1378       case "allof" :
1379       {
1380         /* Get parameter and recursivly call this method 
1381          *  for each parameter 
1382          */
1383         $id ++;
1384         $tmp2 = $this->get_parameter($data,$id);
1385         
1386         foreach($tmp2 as $parameter){
1387           $tmp['allof'][] = $this->_parse($parameter);
1388         }
1389         $tmp['allof']['Inverse'] = $Inverse;
1390         break;
1391       }
1394       /****************
1395        * Parse - Anyof
1396        ****************/ 
1398       case "anyof" :
1399       {
1400         /* Get parameter and recursivly call this method 
1401          *  for each parameter 
1402          */
1403         $id ++;
1404         $tmp2 = $this->get_parameter($data,$id);
1406         foreach($tmp2 as $parameter){
1407           $tmp['anyof'][] = $this->_parse($parameter);
1408         }
1409         $tmp['anyof']['Inverse'] = $Inverse;
1410         break;
1411       }
1412       default : $tmp[$id] = $type; 
1413     }
1414     
1415     return($tmp); 
1416   }
1419   function get_parameter($data,$id)
1420   {
1421     $par = array();
1422     $open_brakets = 0;
1423     $next = NULL;
1424     $num = 0;
1425     for($i = $id ; $i < count($data) ; $i++ ){
1426       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1427         $open_brakets ++;
1428       }
1429       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1430         $num ++;
1431       }
1432       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1433         $par[$num][] = $data[$i];
1434       }
1435       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1436         $open_brakets --;
1437       }
1438     }
1439     return($par);
1440   }
1443 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1444 ?>