Code

Updated comment handling
[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     print_a($elements);;
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         ":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             print_a(array($data));
132             $ret[] = $data['LastError'];
133           }
134         }
135       }
136     }
137     return($ret);
138   }
139  
141   /* Recursivly create a sieve script out of the given 
142    *  tags and tokens provided by $parsed.
143    *  $id       specifies the depth of the current element.
144    *  $obj_id   is the current tag-id handled by this function
145    */
146   function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
147   {
148     $script ="";
149     if($parsed == NULL){
150       $parsed = $this->_parsed;
151     }
154     if(!is_array($parsed)){
155       return;
156     }
158     /* Walk through all elements */
159     foreach($parsed as $key => $data){
161       /* Create Inverse Tag */
162       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
163         $Inverse = TRUE;
164       }else{
165         $Inverse = FALSE;
166       }
168       /* Create elements */
169       switch($key)
170       {
172         /*******************
173          * True / False
174          *******************/
176         case "true" :
177         case "false" :
178         {
179           /* Invert this test if required */
180           if($Inverse){
181             $script .= "not ";
182           }
183           $script .= $key;
184           break;
185         }
188         /*******************
189          * Address
190          *******************/
192         case "address" :   
193         {
194           /* [not] address 
195                         [address-part: tag] 
196                         [comparator: tag] 
197                         [match-type: tag] 
198                         <header-list: string-list> 
199                         <key-list: string-list> 
200           */
202           /* Invert this test if required */
203           if($Inverse){
204             $script .= "not ";
205           }
206   
207           $script .="address ";
208  
209           /* Add address part tag */ 
210           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
211             $script .= $data['Address_Part']." ";
212           }
214           /* Add comparator */
215           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
216             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
217           }
218     
219           /* Add match type */
220           $script .= $data['Match_type']." ";
222           /* Add special match type for count and value */
223           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
224             $script .= sieve_create_strings($data['Match_type_value'])." ";
225           }
227           $script .= sieve_create_strings($data['Key_List']);
228           $script .= " ";
229           $script .= sieve_create_strings($data['Value_List']);
230           break;
231         }
234         /*******************
235          * Header
236          *******************/
238         case "header" :   
239         {
240           /* [not] header   
241                 [comparator: tag] 
242                 [match-type: tag] 
243                 <header-names: string-list> 
244                 <key-list: string-list>
245           */
247           /* Invert ? */
248           if($Inverse){
249             $script .= "not ";
250           }
251   
252           $script .="header ";
253  
254           /* Add address part tag */ 
255           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
256             $script .= $data['Address_Part']." ";
257           }
259           /* Add comparator */
260           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
261             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
262           }
263     
264           /* Add match type */
265           $script .= $data['Match_type']." ";
267           /* Add special match type for count and value */
268           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
269             $script .= sieve_create_strings($data['Match_type_value'])." ";
270           }
272           $script .= sieve_create_strings($data['Key_List']);
273           $script .= " ";
274           $script .= sieve_create_strings($data['Value_List']);
275           break;
276         }
279         /*******************
280          * Envelope
281          *******************/
283         case "envelope" :   
284         {
285           /* [not]  envelope 
286                     [address-part: tag] 
287                     [comparator: tag] 
288                     [match-type: tag] 
289                     <envelope-part: string-list> 
290                     <key-list: string-list> 
291           */
293           /* Invert */
294           if($Inverse){
295             $script .= "not ";
296           }
297   
298           $script .="envelope ";
299  
300           /* Add address part tag */ 
301           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
302             $script .= $data['Address_Part']." ";
303           }
305           /* Add comparator */
306           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
307             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
308           }
309     
310           /* Add match type */
311           $script .= $data['Match_type']." ";
313           /* Add special match type for count and value */
314           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
315             $script .= sieve_create_strings($data['Match_type_value'])." ";
316           }
318           $script .= sieve_create_strings($data['Key_List']);
319           $script .= " ";
320           $script .= sieve_create_strings($data['Value_List']);
321           break;
322         }
325         /*******************
326          * Exists
327          *******************/
328         case "exists" : 
329         {
330           /* [not] exists 
331               <header-names: string-list> 
332           */
334           /* Invert ? */
335           if($Inverse){
336             $script .= "not ";
337           }
339           $script .= "exists ".sieve_create_strings($data['Values']);
340           break;
341         }
344         /*******************
345          * Size
346          *******************/
347         case "size" : 
348         {
349           /* [not] size 
350                 <":over" / ":under"> 
351                 <limit: number> 
352           */
354           /* Invert ? */
355           if($Inverse){
356             $script .= "not ";
357           }
358  
359           /* Add size test */ 
360           $script .="size ";
361           $script .=$data['Match_type']." ";
362           foreach($data['Value_List'] as $val){
363             $script .= $val." ";
364           }
365           break;
366         }
369         /*******************
370          * Allof
371          *******************/
372         case "anyof" :
373         case "allof" :
374         {
375           /* allof <tests: test-list>
376              anyof <tests: test-list> */
378  
379           /* Add spaces, to indent the code.*/ 
380           $block = "\n";
381           for($i = 0 ; $i < $id ; $i ++){
382             $block .= SIEVE_INDENT_TAB;
383           }          
385           /* Add allof/anyof tag */
386           $script.= " ".$key." ( ";
388           /* Add each test parameter */
389           foreach($data as $key2 => $dat){
390             if(($key2 === "Inverse") && ($key2 == "Inverse")){
391               continue;
392             }
393             $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
394           }
395     
396           /* Remove last _,_ and close the tag */
397           $script = preg_replace("/,$/","",trim($script));
398           $script.= $block.")";
399           break ;
400         }
402         default :
403         {
404           $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
405         }
406       }
407     }
408     return($script);
409   }
411   
412   function add_test($data,$type)
413   {
414     switch($type)
415     {
416       case "header" : 
417       case "address" : 
418       case "envelope" : 
419       {
420         /* Add to Tree */
421         $values = array(        "Inverse"         => FALSE,
422                                 "Comparator"      => "",
423                                 "Expert"          => FALSE,
424                                 "LastError"       => "",
425                                 "Match_type"      => ":contains",
426                                 "Match_type_value"=> "",
427                                 "Key_List"        => array(_("emtpy")),
428                                 "Value_List"      => array(_("empty"))) ;
429         if($type == "address"){
430           $values["Address_Part"]    = ":all";
431         }
432         $data[$type]=$values;
434         $this->parent->add_require("relational");
435         if($type == "envelope"){
436           $this->parent->add_require("envelope");
437         }
438     
440         break;
441       }
442       case "allof" :
443       case "anyof" :
444       {
445         $data[$type] = array("Inverse" => FALSE);
446         break;
447       }
448       case "size" :
449       {
450         $tmp= array( 
451             "Inverse"    => FALSE,
452             "Match_type" => ":over",
453             "Value_List" => array("1M"));
455         $tmp['LastError'] = "";
456         $data[$type] = $tmp;
457         break;
458       }
459       case "true":
460       {
461         $data['true'] = "true";
462         $data['true']['LastError'] = "";
463         break;
464       }
465       case "false":
466       {
467         $data['false'] = "false";
468         $data['false']['LastError'] = "";
469         break;
470       }
471       case "exists" :
472       {
473         $data['exists'] = array('Inverse' => FALSE,
474                                 'Values'  => array(_("Nothing specified right now")),
475                                 'LastError' => "");
476         break;
477       }
478       default : echo "Still buggy ";exit;
479     }
481     return($data);
482   }
485   /* Ensure that all changes made on the ui 
486    *  will be saved. 
487    */
488   function save_object()
489   {
490   
491     if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
492       $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
493     }
495     $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
496     $this->_parsed = $tmp;
497   }
500   /* Recursivly save all ui changes for the 
501    *  tags and tokens provided by $parsed.
502    *  $id       specifies the depth of the current element.
503    *  $obj_id   is the current tag-id handled by this function
504    */
505   function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
506   {
507     /* Variable initialization */ 
508     $ret ="";
509     if($parsed == NULL){
510       $parsed = $this->_parsed;
511     }
513     if(!is_array($parsed)) return;
515     /* Walk through all elements */
516     foreach($parsed as $key => $data){
518       /* Id used to have unique html names */
519       $element_id = $this->object_id."_".$id."_".$obj_id;
520       
521       foreach($_POST as $name => $value){
522         if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
523           return(false); 
524         }
525       }
527       
528       if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
529         $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
530       }
532       /* Create elements */
533       switch($key)
534       {
535         /*******************
536          * Address 
537          *******************/
539         case "envelope" :
540         case "header" : 
541         case "address" : 
542         {
543           /* [not] address 
544                         [address-part: tag] 
545                         [comparator: tag] 
546                         [match-type: tag] 
547                         <header-list: string-list> 
548                         <key-list: string-list> 
549           */
551           /* Possible address parts we can select */
552           $address_parts = $this->address_parts;
553           $comparators   = $this->comparators;
554           $match_types   = $this->match_types; 
555           $operators     = $this->operators;
557           $parsed[$key]['LastError'] = "";
559           /* Toggle Inverse ? */
560           if(isset($_POST['toggle_inverse_'.$element_id])){
561             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
562           }
564           /* Check if we want to toggle the expert mode */
565           if(isset($_POST['Toggle_Expert_'.$element_id])){
566             $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
567           }
569           /* Get address part */
570           if(isset($_POST['address_part_'.$element_id])){
571             $ap = $_POST['address_part_'.$element_id];
573             if(!isset($address_parts[$ap])){
574               $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
575             }
576             $parsed[$key]['Address_Part'] = $ap;
577           }
579           /* Check if match type has changed */
580           if(isset($_POST['matchtype_'.$element_id])){
581             $mt = $_POST['matchtype_'.$element_id];
583             if(!isset($match_types[$mt])){
584               $parsed[$key]['LastError'] = _("Invalid match type given.");
585             }
586             $parsed[$key]['Match_type'] = $mt;
587           }
589           /* Get the comparator tag, if posted */
590           if(isset($_POST['comparator_'.$element_id])){
591             $cp = $_POST['comparator_'.$element_id];
593             if(!isset($comparators[$cp])){
594               $parsed[$key]['LastError'] = _("Invalid operator given.");
595             }
596             $parsed[$key]['Comparator'] = $cp;
598             if($cp == "i;ascii-numeric"){
599               $this->parent->add_require("comparator-i;ascii-numeric");
600             }
601           }
603           /* In case of :count and :value match types 
604            *  we have a special match operator we should save.
605            */
606           if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
607             if(isset($_POST['operator_'.$element_id])){
608               $op = $_POST['operator_'.$element_id];
610               if(!isset($operators[$op])){
611                 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
612               }
613               $parsed[$key]['Match_type_value'] = $op;
614             }
615           }
617           /* Get the address fields we should check, they are seperated by , */
618           if(isset($_POST['keys_'.$element_id])){
619             $vls = stripslashes($_POST['keys_'.$element_id]);
620             $tmp = array();
622             $tmp2 = split(",",$vls);
623             foreach($tmp2 as $val){
624               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
625             }
626             $parsed[$key]['Key_List'] = $tmp;
627           }
629           /* Get the values should check for, they are seperated by , */
630           if(isset($_POST['values_'.$element_id])){
631             $vls = stripslashes($_POST['values_'.$element_id]);
632             $tmp = array();
634             $tmp2 = split(",",$vls);
635             foreach($tmp2 as $val){
636               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
637             }
638             $parsed[$key]['Value_List'] = $tmp;
639           }
640           break;
641         }
642  
643         /*******************
644          * TRUE FALSE 
645          *******************/
647         case "true" :
648         case "false" : 
649         {
650           $name = 'boolean_'.$element_id;
651           if(isset($_POST[$name])){
652             $key2 = $_POST[$name];
653             
654             if($key != $key2) {
655               $parsed = array($key2 => $key2); 
656             }
657           }
658           break;
659         }
661         /*******************
662          * Exists 
663          *******************/
665         case "exists" :
666         {
667           /* Toggle Inverse ? */
668           if(isset($_POST['toggle_inverse_'.$element_id])){
669             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
670           }
672           /* get list of match values */
673           if(isset($_POST['Values_'.$element_id])){
674             $vls = stripslashes($_POST['Values_'.$element_id]);
675             $tmp = array();          
676   
677             $tmp2 = split(",",$vls);
678             foreach($tmp2 as $val){
679               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
680             }
681             $parsed['exists']['Values'] = $tmp;
682           }
683           break;
684         }
686         /*******************
687          * Size 
688          *******************/
690         case "size" :
691         {
692           $Match_types = array( ":over" => _("greater than") ,
693                                 ":under" => _("lower than"));
695           $Units       = array( "M" => _("Megabyte") ,
696                                 "K" => _("Kilobyte"));
698           /* Reset error */
699           $parsed[$key]['LastError'] ="";
701           /* Get match type */
702           if(isset($_POST['Match_type_'.$element_id])){
703             $mt = $_POST['Match_type_'.$element_id];
704             if(!isset($Match_types[$mt])){
705               $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
706             }
707             $parsed[$key]['Match_type'] = $mt;
708           }
710           /* Get old values */
711           $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
712           $unit  = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
714           /* Get value */
715           if(isset($_POST['Value_'.$element_id])){
716             $vl = $_POST['Value_'.$element_id];
717          
718             if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
719               $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
720             }
721             $value = preg_replace("/[^0-9]/","",$vl);
722           }        
724           /* Get unit */
725           if(isset($_POST['Value_Unit_'.$element_id])){
726             $ut = $_POST['Value_Unit_'.$element_id];
727        
728             if(!isset($Units[$ut])){
729               $parsed[$key]['LastError'] = _("No valid unit selected");
730             }
731             $unit = $ut;
732           }       
733           $parsed[$key]['Value_List'] = array(); 
734           $parsed[$key]['Value_List'][0] = $value.$unit;
735           break;
736         }
738         /*******************
739          * Allof 
740          *******************/
741      
742         case "allof" : 
743         {
744           if(isset($_POST['toggle_inverse_'.$element_id])){
745             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
746           }
747           foreach($data as $key2 => $dat){
748             if(($key2 === "Inverse") && ($key2 == "Inverse")){
749               continue;
750             }
751             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
752             if($tmp_data != false){
753               $parsed[$key][$key2] = $tmp_data;
754             }else{
755               unset( $parsed[$key][$key2]);
756             }
757           }
758           break ;
759         } 
761         /*******************
762          * Anyof 
763          *******************/
764      
765         case "anyof" : 
766         {
767           if(isset($_POST['toggle_inverse_'.$element_id])){
768             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
769           }
770           foreach($data as $key2 => $dat){
771             if(($key2 === "Inverse") && ($key2 == "Inverse")){
772               continue;
773             }
774             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
775             if($tmp_data != false){
776               $parsed[$key][$key2] = $tmp_data;
777             }else{
778               unset( $parsed[$key][$key2]);
779             }
780           }
781           break ;
782         } 
783       }
784     } 
785     return($parsed);
786   }  
789   /* Return html element for IF */ 
790   function execute()
791   {
792     /* Create title */
793     $name  = "<img alt='' src='images/small_filter.png' class='center'>";
794     $name .= "<b>"._("Condition")."</b>";
795     if($this->TYPE == "if"){
796       $name .= "&nbsp;-&nbsp;"._("If");
797     }elseif($this->TYPE == "elsif"){
798       $name .= "&nbsp;-&nbsp;"._("Else if");
799     }else{
800       $name .= "&nbsp;-&nbsp;"._("Else");
801     }
803     $smarty = get_smarty();
804     $smarty->assign("ID", $this->object_id);
806     /* Only display navigation elements if necessary */
807     if($this->TYPE == "if"){
808       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
809     }else{
810       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
811     }
813     $smarty->assign("Name", $name);
814     $smarty->assign("Contents", $this->get_as_html());
816     if($this->TYPE == "if"){
817       $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
818     }else{
819       $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
820     }
821     $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
822     return($str);
823   }
825   
826   /* Returns all elements as html */
827   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
828   {
829     $ret ="";
830     if($parsed == NULL){
831       $parsed = $this->_parsed;
832     }
834     if((!is_array($parsed)) || !count($parsed)) {
835       $smarty = get_smarty();
836       $smarty->assign("ID",$this->object_id);
837       $smarty->assign("DisplayAdd",TRUE);
838       $smarty->assign("DisplayDel",FALSE);
839       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
840       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
841       return($ret);
842     }
844     /* Walk through all elements */
845     foreach($parsed as $key => $data){
847       /* Create Inverse Tag */
848       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
849         $Inverse = TRUE;
850       }else{
851         $Inverse = FALSE;
852       }
854       /* Id used to have unique html names */
855       $element_id = $this->object_id."_".$id."_".$obj_id;
857       /* Create elements */
858       switch($key)
859       {
860   
861         /*******************
862          * TRUE FALSE 
863          *******************/
865         case "true" :
866         case "false" : 
867         { 
868           /* Inverse element if required */
869           if($Inverse){        
870             if($key == "true"){
871               $key = "false";
872             }else{
873               $key = "true";
874             }           
875           }
877           /* Get template */
878           $smarty = get_smarty();
879           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
880           $smarty->assign("selected"  , $key); 
881           $smarty->assign("ID"  , $element_id); 
882           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
883           break;
884         }
887         /*******************
888          * Header 
889          *******************/
891         case "header": 
892         {
893           $address_parts = $this->address_parts;
894           $comparators   = $this->comparators;
895           $match_types   = $this->match_types; 
896           $operators     = $this->operators;
898           $smarty = get_smarty();
899           $smarty->assign("comparators",$comparators);
900           $smarty->assign("match_types",$match_types);
901           $smarty->assign("operators",$operators);
902           $smarty->assign("LastError",$data['LastError']);
903           $smarty->assign("match_type", $data['Match_type']);
904           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
905           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
907           $keys = "";
908           foreach($data['Key_List'] as $key){
909             $keys .= $key.", ";
910           }
911           $keys = preg_replace("/,$/","",trim($keys));
912    
913           $values = "";
914           foreach($data['Value_List'] as $key){
915             $values .= $key.", ";
916           }
917           $values = preg_replace("/,$/","",trim($values));
919           $smarty->assign("keys",$keys);
920           $smarty->assign("Inverse",$Inverse);
921           $smarty->assign("values",$values);
922           $smarty->assign("Expert", $data['Expert']);
923  
924           $smarty->assign("ID"  , $element_id); 
925           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
926           break;
927         }
930         /*******************
931          * Envelope 
932          *******************/
934         case "envelope":
935         {
936           $address_parts = $this->address_parts;
937           $comparators   = $this->comparators;
938           $match_types   = $this->match_types; 
939           $operators     = $this->operators;
941           $smarty = get_smarty();
942           $smarty->assign("Inverse",$Inverse);
943           $smarty->assign("comparators",$comparators);
944           $smarty->assign("Expert", $data['Expert']);
945           $smarty->assign("match_types",$match_types);
946           $smarty->assign("operators",$operators);
947           $smarty->assign("LastError",$data['LastError']);
948           $smarty->assign("match_type", $data['Match_type']);
949           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
950           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
952           $keys = "";
953           foreach($data['Key_List'] as $key){
954             $keys .= $key.", ";
955           }
956           $keys = preg_replace("/,$/","",trim($keys));
958           $values = "";
959           foreach($data['Value_List'] as $key){
960             $values .= $key.", ";
961           }
962           $values = preg_replace("/,$/","",trim($values));
963           $smarty->assign("keys",$keys);
964           $smarty->assign("values",$values);
966           $smarty->assign("ID"  , $element_id); 
967           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
968           break;
969         }
972         /*******************
973          * Address 
974          *******************/
976         case "address" : 
977         {
978           $address_parts = $this->address_parts;
979           $comparators   = $this->comparators;
980           $match_types   = $this->match_types; 
981           $operators     = $this->operators;
983           $smarty = get_smarty();
984           $smarty->assign("Inverse",$Inverse);
985           $smarty->assign("address_parts",$address_parts);
986           $smarty->assign("comparators",$comparators);
987           $smarty->assign("match_types",$match_types);
988           $smarty->assign("LastError",$data['LastError']);
989           $smarty->assign("operators",$operators);
990           $smarty->assign("match_type", $data['Match_type']);
991           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
992           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
993           $smarty->assign("address_part", $data['Address_Part']);
994           $smarty->assign("Expert", $data['Expert']);
995         
996           $keys = "";
997           foreach($data['Key_List'] as $key){
998             $keys .= $key.", ";
999           }
1000           $keys = preg_replace("/,$/","",trim($keys));
1001    
1002           $values = "";
1003           foreach($data['Value_List'] as $key){
1004             $values .= $key.", ";
1005           }
1006           $values = preg_replace("/,$/","",trim($values));
1007           $smarty->assign("keys",$keys);
1008           $smarty->assign("values",$values);
1009           $smarty->assign("ID"  , $element_id); 
1010           $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1011           break;
1012         }
1013       
1015         /*******************
1016          * Size 
1017          *******************/
1018         
1019         case "size" : 
1020         {
1021           $Match_types = array( ":over" => _("greater than") , 
1022                                 ":under" => _("lower than"));
1024           $Units       = array( "M" => _("Megabyte") , 
1025                                 "K" => _("Kilobyte")); 
1027           $Match_type   = $data['Match_type'];
1028           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1029           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1030        
1031           $LastError = "";
1032           if(isset($data['LastError'])){
1033             $LastError = $data['LastError'];
1034           }
1035  
1036           $smarty = get_smarty();
1037           $smarty->assign("Inverse",$Inverse);
1038           $smarty->assign("LastError",$LastError);
1039           $smarty->assign("Match_types",$Match_types);
1040           $smarty->assign("Units",$Units);
1041           $smarty->assign("Match_type",$Match_type);
1042           $smarty->assign("Value",$Value);
1043           $smarty->assign("Value_Unit",$Value_Unit);
1044           $smarty->assign("ID"  , $element_id); 
1045           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1046           break;
1047         }
1048         
1049         /*******************
1050          * Exists 
1051          *******************/
1052         
1053         case "exists" : 
1054         {
1055           $LastError = "";
1056           if(isset($data['LastError'])){
1057             $LastError = $data['LastError'];
1058           }
1059  
1060           $Values = "";
1061           foreach($data['Values'] as $val){
1062             $Values .= $val.", ";
1063           }
1064           $Values = preg_replace("/,$/","",trim($Values));
1066           $smarty = get_smarty();
1067           $smarty->assign("LastError",$LastError);
1068           $smarty->assign("Values",$Values);
1069           $smarty->assign("Inverse",$Inverse);
1070           $smarty->assign("ID"  , $element_id); 
1071           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1072           break;
1073         }
1074   
1076         /*******************
1077          * All of   
1078          *******************/
1080         case "allof" : 
1081         {
1082           $Contents = ""; 
1083           foreach($data as $key => $dat){
1084             if(($key === "Inverse") && ($key == "Inverse")){
1085               continue;
1086             }
1087             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1088           }
1090           $smarty = get_smarty();
1091           $smarty->assign("ID"  , $element_id); 
1092           $smarty->assign("DisplayAdd",TRUE);
1093           $smarty->assign("DisplayDel",FALSE);
1094           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1095           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1097           $smarty->assign("Inverse",$Inverse);
1098           $smarty->assign("Contents",$cont_tmp.$Contents);
1099           $smarty->assign("ID"  , $element_id); 
1100           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1102           $ret = $allof_tmp;
1103           break ;
1104         } 
1107         /*******************
1108          * Any of   
1109          *******************/
1111         case "anyof" : 
1112         {
1113           $Contents = ""; 
1114           foreach($data as $key => $dat){
1115             if(($key === "Inverse") && ($key == "Inverse")){
1116               continue;
1117             }
1118             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1119           }
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%%/",_("Klick here to add a new test"),$cont_tmp);
1127           $smarty->assign("Inverse",$Inverse);
1128           $smarty->assign("Contents",$cont_tmp.$Contents);
1129           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1131           $ret = $allof_tmp;
1133           break ;
1134         } 
1135         default : 
1136         {
1137           trigger_error(_("Unhandled switch type"));
1138         }
1139       }
1140     }
1141     
1142     if(!isset($smarty)){
1143       $smarty =get_smarty();
1144     }
1146     $smarty->assign("ID",$element_id);
1147     $smarty->assign("DisplayAdd",FALSE);
1148     $smarty->assign("DisplayDel",TRUE);
1149     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1150     $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1151     return($ret);
1152   }
1155   /* Parse given token identified by $data[$id] 
1156    *  and return the parsed tokens. 
1157    */
1158   function _parse($data,$id = 0)
1159   {
1160     $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1161     $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1162     $type = $data[$id]['text'];
1163     $tmp = array();
1165     /* Is there an identifier named 'not' to inverse this filter ? */
1166     $Inverse = FALSE;
1167     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1168       $Inverse = TRUE;
1169       $id ++;
1170       $type = $data[$id]['text'];
1171     }
1173     switch($type)
1174     {
1176       /****************
1177        * Parse - Envelope / Header / Address
1178        ****************/ 
1180       case "envelope" : 
1181       case "header":
1182       case "address" : 
1183       {
1184         /* Address matches are struckture as follows :
1185            [not] 
1186            address 
1187                   [address-part: tag]           all|localpart|domain|user|detail
1188                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1189                   [match-type: tag]             is|contains|matches|count|value 
1190                   <header-list: string-list> 
1191                   <key-list: string-list>   
1192           */ 
1193    
1194         
1195         $part     = "(:all|:localpart|:domain)";
1196         $operator = "(:contains|:is|:matches|:count|:value)";
1197         $value_op = "(lt|le|eq|ge|gt|ne)";
1199         $Address_Part     = "";
1200         $Comparator       = "";        
1201         $Match_type       = "";    
1202         $Match_type_value = "";
1203   
1204         $Key_List         = array();
1205         $Value_List       = array();
1206   
1207         for($i = 0 ; $i < count($data) ; $i ++){
1208          
1209           /* Get next node */ 
1210           $node = $data[$i];
1211   
1212           /* Check address part definition */
1213           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1214             $Address_Part = $node['text'];
1215           }
1217           /* Check for match type  */
1218           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1219             $Match_type = $node['text'];
1221             /* Get value operator */
1222             if(in_array($Match_type,array(":value",":count"))){
1223               $i ++;        
1224               $node = $data[$i];
1226               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1227                 $Match_type_value = $node['text'];
1228               }
1229             }
1230           } 
1232           /* Check for a comparator */
1233           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1234             $i ++;
1235             $node = $data[$i];
1236             $Comparator = $node['text'];
1237           }
1238   
1239           /* Check for Key_List */  
1240           elseif(count(sieve_get_strings($data,$i))){
1241             $tmp2 = sieve_get_strings($data,$i);
1242             $i =  $tmp2['OFFSET'];
1244             if(!count($Key_List)){
1245               $Key_List = $tmp2['STRINGS'];
1246             }else{
1247               $Value_List = $tmp2['STRINGS']; 
1248             }
1249           } 
1250       
1251         }
1252  
1253          
1254         /* Add to Tree */ 
1255         $values = array( "Inverse"         => $Inverse,
1256                                 "Comparator"      => $Comparator,
1257                                 "Expert"          => FALSE,
1258                                 "Match_type"      => $Match_type,
1259                                 "Match_type_value"=> $Match_type_value,
1260                                 "Key_List"        => $Key_List,
1261                                 "Value_List"      => $Value_List) ;
1262         if($type == "address"){
1263           $values["Address_Part"]    = $Address_Part;
1264         }
1265         $tmp[$type] = $values;
1266         $tmp[$type]['LastError'] = "";
1267         break;
1268       }
1271       /****************
1272        * Parse - Size
1273        ****************/ 
1275       case "size":
1276       {
1277     
1278         $ops = "(:over|:under)";
1280         $Match_type = "";
1282         for($i = $id ; $i < count($data); $i++){
1284           /* Get current node */
1285           $node = $data[$i];
1287           /* Get tag (under / over) */
1288           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1289             $Match_type = $node['text'];
1290           }
1291           
1292           /* Get Value_List, the value that we want to match for */
1293           elseif(count(sieve_get_strings($data,$i))){
1294             $tmp2 = sieve_get_strings($data,$i);
1295             $i =  $tmp2['OFFSET'];
1296           
1297             $Value_List = $tmp2['STRINGS'];
1298           } 
1299         }        
1300     
1301         $tmp[$type]= array( "Inverse"    => $Inverse,
1302                             "Match_type" => $Match_type,
1303                             "Value_List" => $Value_List);
1304         $tmp[$type]['LastError'] = "";
1305         break;
1306       }
1309       /****************
1310        * Parse - True / False
1311        ****************/ 
1313       case "true": 
1314       {
1315         $tmp['true'] = "true";
1316         $tmp['true']['LastError'] = "";
1317         break;
1318       }
1319       case "false":
1320       {
1321         $tmp['false'] = "false";
1322         $tmp['false']['LastError'] = "";
1323         break;
1324       }
1327       /****************
1328        * Parse - Exists
1329        ****************/ 
1331       case "exists":
1332       {
1333         
1334         /* Skip first values, [if,not,exists] */
1335         $node = $data[$id];
1336         while(in_array($node['text'],array("if","not","exists"))){
1337           $id ++;
1338           $node = $data[$id];
1339         }
1341         /* Get values */
1342         $tmp2 = sieve_get_strings($data,$id);
1343   
1344         
1345         $tmp['exists'] = array('Inverse' => $Inverse,
1346                                'Values'  => $tmp2['STRINGS']);
1347         $tmp[$type]['LastError'] = "";
1348         break;
1349       }
1352       /****************
1353        * Parse - Allof
1354        ****************/ 
1356       case "allof" :
1357       {
1358         /* Get parameter and recursivly call this method 
1359          *  for each parameter 
1360          */
1361         $id ++;
1362         $tmp2 = $this->get_parameter($data,$id);
1363         
1364         foreach($tmp2 as $parameter){
1365           $tmp['allof'][] = $this->_parse($parameter);
1366         }
1367         $tmp['allof']['Inverse'] = $Inverse;
1368         break;
1369       }
1372       /****************
1373        * Parse - Anyof
1374        ****************/ 
1376       case "anyof" :
1377       {
1378         /* Get parameter and recursivly call this method 
1379          *  for each parameter 
1380          */
1381         $id ++;
1382         $tmp2 = $this->get_parameter($data,$id);
1384         foreach($tmp2 as $parameter){
1385           $tmp['anyof'][] = $this->_parse($parameter);
1386         }
1387         $tmp['anyof']['Inverse'] = $Inverse;
1388         break;
1389       }
1390       default : $tmp[$id] = $type; 
1391     }
1392     
1393     return($tmp); 
1394   }
1397   function get_parameter($data,$id)
1398   {
1399     $par = array();
1400     $open_brakets = 0;
1401     $next = NULL;
1402     $num = 0;
1403     for($i = $id ; $i < count($data) ; $i++ ){
1404       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1405         $open_brakets ++;
1406       }
1407       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1408         $num ++;
1409       }
1410       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1411         $par[$num][] = $data[$i];
1412       }
1413       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1414         $open_brakets --;
1415       }
1416     }
1417     return($par);
1418   }
1421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1422 ?>