Code

Fixed some errors
[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();
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)
21   {
22     /* Possible address parts we can select */
23     $this->address_parts = array( 
24         ":all"       => _("Complete adress")."&nbsp;("._("Default").")",
25         ":domain"    => _("Domian part") ,
26         ":localpart" => _("Local part"));
28     /* comparator type */
29     $this->comparators   = array( 
30         "i;ascii-casemap" => _("Case insensitive")."&nbsp;("._("Default").")",
31         "i;octet"         => _("Case sensitive"),
32         "i;ascii-numeric" => _("Numeric"));
34     /* Match types */
35     $this->match_types  = array(  
36         ":is"         => _("is"),
37         ":contains"   => _("contains"),
38         ":matches"    => _("matches"),
39         ":count"      => _("count"),
40         ":value"      => _("value is"));
42     /* Operators */
43     $this->operators = array(     
44         "lt"  => _("less than"),
45         "le"  => _("less or equal"),
46         "eq"  => _("equals"),
47         "ge"  => _("greater or equal"),
48         "gt"  => _("greater than"),
49         "ne"  => _("not equal"));
51     $this->object_id       = $object_id;
52     if($elements!=NULL){
53       $this->elements = $elements;
54       $this->_parsed  = $this->_parse($elements['ELEMENTS'],1);
55     }
56   }
59   /* Returns the sieve script for this 
60    *  if/else tag.
61    */
62   function get_sieve_script_part()
63   {
64     $tmp = "if ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
65     return($tmp);
66   } 
69   /* Return error msgs */
70   function check()
71   {
72     $check = $this->check_recursive();
73     return($check);
74   }
75  
77   /* Recursivly fetch all error msgs */
78   function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
79   {
80     $ret = array();
81     if($parsed == NULL){
82       $parsed = $this->_parsed;
83     }
85     /* Walk through all elements */
86     foreach($parsed as $key => $data){
88       /* Create elements */
89       switch($key)
90       {
91         /*******************
92          * Allof / Anyof
93          *******************/
94         case "anyof" :
95         case "allof" :
96         { 
97           foreach($data as $key2 => $dat){
98             if(($key2 === "Inverse") && ($key2 == "Inverse")){
99               continue;
100             }
101             $msgs = $this->check_recursive($dat, ($id +1),$key2);
103             foreach($msgs as $msg){
104               $ret[] = $msg;
105             }
106           }
107           break;
108         }
110         /*******************
111          * True / False
112          *******************/
114         case "true" :
115         case "fasle" : 
116         {
117           /* Can't fail anyway */
118           break;
119         }
120     
121         /*******************
122          * Default
123          *******************/
125         default: 
126         {
127           if(isset($data['LastError']) && !empty($data['LastError'])){
128             $ret[] = $data['LastError'];
129           }
130         }
131       }
132     }
133     return($ret);
134   }
135  
137   /* Recursivly create a sieve script out of the given 
138    *  tags and tokens provided by $parsed.
139    *  $id       specifies the depth of the current element.
140    *  $obj_id   is the current tag-id handled by this function
141    */
142   function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
143   {
144     $script ="";
145     if($parsed == NULL){
146       $parsed = $this->_parsed;
147     }
150     if(!is_array($parsed)){
151       return;
152     }
154     /* Walk through all elements */
155     foreach($parsed as $key => $data){
157       /* Create Inverse Tag */
158       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
159         $Inverse = TRUE;
160       }else{
161         $Inverse = FALSE;
162       }
164       /* Create elements */
165       switch($key)
166       {
168         /*******************
169          * True / False
170          *******************/
172         case "true" :
173         case "false" :
174         {
175           /* Invert this test if required */
176           if($Inverse){
177             $script .= "not ";
178           }
179           $script .= $key;
180           break;
181         }
184         /*******************
185          * Address
186          *******************/
188         case "address" :   
189         {
190           /* [not] address 
191                         [address-part: tag] 
192                         [comparator: tag] 
193                         [match-type: tag] 
194                         <header-list: string-list> 
195                         <key-list: string-list> 
196           */
198           /* Invert this test if required */
199           if($Inverse){
200             $script .= "not ";
201           }
202   
203           $script .="address ";
204  
205           /* Add address part tag */ 
206           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
207             $script .= $data['Address_Part']." ";
208           }
210           /* Add comparator */
211           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
212             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
213           }
214     
215           /* Add match type */
216           $script .= $data['Match_type']." ";
218           /* Add special match type for count and value */
219           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
220             $script .= sieve_create_strings($data['Match_type_value'])." ";
221           }
223           $script .= sieve_create_strings($data['Key_List']);
224           $script .= " ";
225           $script .= sieve_create_strings($data['Value_List']);
226           break;
227         }
230         /*******************
231          * Header
232          *******************/
234         case "header" :   
235         {
236           /* [not] header   
237                 [comparator: tag] 
238                 [match-type: tag] 
239                 <header-names: string-list> 
240                 <key-list: string-list>
241           */
243           /* Invert ? */
244           if($Inverse){
245             $script .= "not ";
246           }
247   
248           $script .="header ";
249  
250           /* Add address part tag */ 
251           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
252             $script .= $data['Address_Part']." ";
253           }
255           /* Add comparator */
256           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
257             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
258           }
259     
260           /* Add match type */
261           $script .= $data['Match_type']." ";
263           /* Add special match type for count and value */
264           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
265             $script .= sieve_create_strings($data['Match_type_value'])." ";
266           }
268           $script .= sieve_create_strings($data['Key_List']);
269           $script .= " ";
270           $script .= sieve_create_strings($data['Value_List']);
271           break;
272         }
275         /*******************
276          * Envelope
277          *******************/
279         case "envelope" :   
280         {
281           /* [not]  envelope 
282                     [address-part: tag] 
283                     [comparator: tag] 
284                     [match-type: tag] 
285                     <envelope-part: string-list> 
286                     <key-list: string-list> 
287           */
289           /* Invert */
290           if($Inverse){
291             $script .= "not ";
292           }
293   
294           $script .="envelope ";
295  
296           /* Add address part tag */ 
297           if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
298             $script .= $data['Address_Part']." ";
299           }
301           /* Add comparator */
302           if(!empty($data['Comparator']) && $data['Comparator'] != ""){
303             $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
304           }
305     
306           /* Add match type */
307           $script .= $data['Match_type']." ";
309           /* Add special match type for count and value */
310           if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
311             $script .= sieve_create_strings($data['Match_type_value'])." ";
312           }
314           $script .= sieve_create_strings($data['Key_List']);
315           $script .= " ";
316           $script .= sieve_create_strings($data['Value_List']);
317           break;
318         }
321         /*******************
322          * Exists
323          *******************/
324         case "exists" : 
325         {
326           /* [not] exists 
327               <header-names: string-list> 
328           */
330           /* Invert ? */
331           if($Inverse){
332             $script .= "not ";
333           }
335           $script .= "exists ".sieve_create_strings($data['Values']);
336           break;
337         }
340         /*******************
341          * Size
342          *******************/
343         case "size" : 
344         {
345           /* [not] size 
346                 <":over" / ":under"> 
347                 <limit: number> 
348           */
350           /* Invert ? */
351           if($Inverse){
352             $script .= "not ";
353           }
354  
355           /* Add size test */ 
356           $script .="size ";
357           $script .=$data['Match_type']." ";
358           foreach($data['Value_List'] as $val){
359             $script .= $val." ";
360           }
361           break;
362         }
365         /*******************
366          * Allof
367          *******************/
368         case "anyof" :
369         case "allof" :
370         {
371           /* allof <tests: test-list>
372              anyof <tests: test-list> */
374  
375           /* Add spaces, to indent the code.*/ 
376           $block = "\n";
377           for($i = 0 ; $i < $id ; $i ++){
378             $block .= SIEVE_INDENT_TAB;
379           }          
381           /* Add allof/anyof tag */
382           $script.= " ".$key." ( ";
384           /* Add each test parameter */
385           foreach($data as $key2 => $dat){
386             if(($key2 === "Inverse") && ($key2 == "Inverse")){
387               continue;
388             }
389             $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
390           }
391     
392           /* Remove last _,_ and close the tag */
393           $script = preg_replace("/,$/","",trim($script));
394           $script.= $block.")";
395           break ;
396         }
398         default :
399         {
400           $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
401         }
402       }
403     }
404     return($script);
405   }
407   
408   function add_test($data,$type)
409   {
410     switch($type)
411     {
412       case "header" : 
413       case "address" : 
414       case "envelope" : 
415       {
416         /* Add to Tree */
417         $values = array(        "Inverse"         => FALSE,
418                                 "Comparator"      => "",
419                                 "Expert"          => FALSE,
420                                 "LastError"       => "",
421                                 "Match_type"      => ":contains",
422                                 "Match_type_value"=> "",
423                                 "Key_List"        => array(_("emtpy")),
424                                 "Value_List"      => array(_("empty"))) ;
425         if($type == "address"){
426           $values["Address_Part"]    = ":all";
427         }
428         $data[$type]=$values;
429         break;
430       }
431       case "allof" :
432       case "anyof" :
433       {
434         $data[$type] = array("Inverse" => FALSE);
435         break;
436       }
437       case "size" :
438       {
439         $tmp= array( 
440             "Inverse"    => FALSE,
441             "Match_type" => ":contains",
442             "Value_List" => array(1,"M"));
444         $tmp['LastError'] = "";
445         $data[$type] = $tmp;
446         break;
447       }
448       case "true":
449       {
450         $data['true'] = "true";
451         $data['true']['LastError'] = "";
452         break;
453       }
454       case "false":
455       {
456         $data['false'] = "false";
457         $data['false']['LastError'] = "";
458         break;
459       }
460       case "exists" :
461       {
462         $data['exists'] = array('Inverse' => FALSE,
463                                 'Values'  => array(_("Nothing specified right now")),
464                                 'LastError' => "");
465         break;
466       }
467       default : echo "Still buggy ";exit;
468     }
470     return($data);
471   }
474   /* Ensure that all changes made on the ui 
475    *  will be saved. 
476    */
477   function save_object()
478   {
479   
480     if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
481       $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
482     }
484     $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
485     $this->_parsed = $tmp;
486   }
489   /* Recursivly save all ui changes for the 
490    *  tags and tokens provided by $parsed.
491    *  $id       specifies the depth of the current element.
492    *  $obj_id   is the current tag-id handled by this function
493    */
494   function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
495   {
496     /* Variable initialization */ 
497     $ret ="";
498     if($parsed == NULL){
499       $parsed = $this->_parsed;
500     }
502     if(!is_array($parsed)) return;
504     /* Walk through all elements */
505     foreach($parsed as $key => $data){
507       /* Id used to have unique html names */
508       $element_id = $this->object_id."_".$id."_".$obj_id;
509       
510       foreach($_POST as $name => $value){
511         if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
512           return(false); 
513         }
514       }
516       
517       if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
518         $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
519       }
521       /* Create elements */
522       switch($key)
523       {
524         /*******************
525          * Address 
526          *******************/
528         case "envelope" :
529         case "header" : 
530         case "address" : 
531         {
532           /* [not] address 
533                         [address-part: tag] 
534                         [comparator: tag] 
535                         [match-type: tag] 
536                         <header-list: string-list> 
537                         <key-list: string-list> 
538           */
540           /* Possible address parts we can select */
541           $address_parts = $this->address_parts;
542           $comparators   = $this->comparators;
543           $match_types   = $this->match_types; 
544           $operators     = $this->operators;
546           $parsed[$key]['LastError'] = "";
548           /* Toggle Inverse ? */
549           if(isset($_POST['toggle_inverse_'.$element_id])){
550             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
551           }
553           /* Check if we want to toggle the expert mode */
554           if(isset($_POST['Toggle_Expert_'.$element_id])){
555             $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
556           }
558           /* Get address part */
559           if(isset($_POST['address_part_'.$element_id])){
560             $ap = $_POST['address_part_'.$element_id];
562             if(!isset($address_parts[$ap])){
563               $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
564             }
565             $parsed[$key]['Address_Part'] = $ap;
566           }
568           /* Check if match type has changed */
569           if(isset($_POST['matchtype_'.$element_id])){
570             $mt = $_POST['matchtype_'.$element_id];
572             if(!isset($match_types[$mt])){
573               $parsed[$key]['LastError'] = _("Invalid match type given.");
574             }
575             $parsed[$key]['Match_type'] = $mt;
576           }
578           /* Get the comparator tag, if posted */
579           if(isset($_POST['comparator_'.$element_id])){
580             $cp = $_POST['comparator_'.$element_id];
582             if(!isset($comparators[$cp])){
583               $parsed[$key]['LastError'] = _("Invalid operator given.");
584             }
585             $parsed[$key]['Comparator'] = $cp;
586           }
588           /* In case of :count and :value match types 
589            *  we have a special match operator we should save.
590            */
591           if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
592             if(isset($_POST['operator_'.$element_id])){
593               $op = $_POST['operator_'.$element_id];
595               if(!isset($operators[$op])){
596                 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
597               }
598               $parsed[$key]['Match_type_value'] = $op;
599             }
600           }
602           /* Get the address fields we should check, they are seperated by , */
603           if(isset($_POST['keys_'.$element_id])){
604             $vls = stripslashes($_POST['keys_'.$element_id]);
605             $tmp = array();
607             $tmp2 = split(",",$vls);
608             foreach($tmp2 as $val){
609               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
610             }
611             $parsed[$key]['Key_List'] = $tmp;
612           }
614           /* Get the values should check for, they are seperated by , */
615           if(isset($_POST['values_'.$element_id])){
616             $vls = stripslashes($_POST['values_'.$element_id]);
617             $tmp = array();
619             $tmp2 = split(",",$vls);
620             foreach($tmp2 as $val){
621               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
622             }
623             $parsed[$key]['Value_List'] = $tmp;
624           }
625           break;
626         }
627  
628         /*******************
629          * TRUE FALSE 
630          *******************/
632         case "true" :
633         case "false" : 
634         {
635           $name = 'boolean_'.$element_id;
636           if(isset($_POST[$name])){
637             $key2 = $_POST[$name];
638             
639             if($key != $key2) {
640               $parsed = array($key2 => $key2); 
641             }
642           }
643           break;
644         }
646         /*******************
647          * Exists 
648          *******************/
650         case "exists" :
651         {
652           /* Toggle Inverse ? */
653           if(isset($_POST['toggle_inverse_'.$element_id])){
654             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
655           }
657           /* get list of match values */
658           if(isset($_POST['Values_'.$element_id])){
659             $vls = stripslashes($_POST['Values_'.$element_id]);
660             $tmp = array();          
661   
662             $tmp2 = split(",",$vls);
663             foreach($tmp2 as $val){
664               $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
665             }
666             $parsed['exists']['Values'] = $tmp;
667           }
668           break;
669         }
671         /*******************
672          * Size 
673          *******************/
675         case "size" :
676         {
677           $Match_types = array( ":over" => _("greater than") ,
678                                 ":under" => _("lower than"));
680           $Units       = array( "M" => _("Megabyte") ,
681                                 "K" => _("Kilobyte"));
683           /* Reset error */
684           $parsed[$key]['LastError'] ="";
686           /* Get match type */
687           if(isset($_POST['Match_type_'.$element_id])){
688             $mt = $_POST['Match_type_'.$element_id];
689             if(!isset($Match_types[$mt])){
690               $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
691             }
692             $parsed[$key]['Match_type'] = $mt;
693           }
695           /* Get old values */
696           $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
697           $unit  = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
699           /* Get value */
700           if(isset($_POST['Value_'.$element_id])){
701             $vl = $_POST['Value_'.$element_id];
702          
703             if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
704               $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
705             }
706             $value = preg_replace("/[^0-9]/","",$vl);
707           }        
709           /* Get unit */
710           if(isset($_POST['Value_Unit_'.$element_id])){
711             $ut = $_POST['Value_Unit_'.$element_id];
712        
713             if(!isset($Units[$ut])){
714               $parsed[$key]['LastError'] = _("No valid unit selected");
715             }
716             $unit = $ut;
717           }       
718           $parsed[$key]['Value_List'] = array(); 
719           $parsed[$key]['Value_List'][0] = $value.$unit;
720           break;
721         }
723         /*******************
724          * Allof 
725          *******************/
726      
727         case "allof" : 
728         {
729           if(isset($_POST['toggle_inverse_'.$element_id])){
730             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
731           }
732           foreach($data as $key2 => $dat){
733             if(($key2 === "Inverse") && ($key2 == "Inverse")){
734               continue;
735             }
736             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
737             if($tmp_data != false){
738               $parsed[$key][$key2] = $tmp_data;
739             }else{
740               unset( $parsed[$key][$key2]);
741             }
742           }
743           break ;
744         } 
746         /*******************
747          * Anyof 
748          *******************/
749      
750         case "anyof" : 
751         {
752           if(isset($_POST['toggle_inverse_'.$element_id])){
753             $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
754           }
755           foreach($data as $key2 => $dat){
756             if(($key2 === "Inverse") && ($key2 == "Inverse")){
757               continue;
758             }
759             $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
760             if($tmp_data != false){
761               $parsed[$key][$key2] = $tmp_data;
762             }else{
763               unset( $parsed[$key][$key2]);
764             }
765           }
766           break ;
767         } 
768       }
769     } 
770     return($parsed);
771   }  
774   /* Return html element for IF */ 
775   function execute()
776   {
777     /* Create title */
778     $name  = "<img alt='' src='images/small_filter.png' class='center'>";
779     $name .= "<b>"._("Condition")."</b>";
780     if($this->TYPE == "if"){
781       $name .= "&nbsp;-&nbsp;"._("If");
782     }else{
783       $name .= "&nbsp;-&nbsp;"._("Else");
784     }
786     $smarty = get_smarty();
787     $smarty->assign("ID", $this->object_id);
789     /* Only display navigation elements if necessary */
790     if($this->TYPE == "if"){
791       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
792     }else{
793       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
794     }
796     $smarty->assign("Name", $name);
797     $smarty->assign("Contents", $this->get_as_html());
798     $object = $smarty->fetch(get_template_path("templates/element_if_else.tpl",TRUE,dirname(__FILE__)));
799     $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
800     return($str);
801   }
803   
804   /* Returns all elements as html */
805   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
806   {
807     $ret ="";
808     if($parsed == NULL){
809       $parsed = $this->_parsed;
810     }
812     if((!is_array($parsed)) || !count($parsed)) {
813       $smarty = get_smarty();
814       $smarty->assign("ID",$this->object_id);
815       $smarty->assign("DisplayAdd",TRUE);
816       $smarty->assign("DisplayDel",FALSE);
817       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
818       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
819       return($ret);
820     }
822     /* Walk through all elements */
823     foreach($parsed as $key => $data){
825       /* Create Inverse Tag */
826       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
827         $Inverse = TRUE;
828       }else{
829         $Inverse = FALSE;
830       }
832       /* Id used to have unique html names */
833       $element_id = $this->object_id."_".$id."_".$obj_id;
835       /* Create elements */
836       switch($key)
837       {
838   
839         /*******************
840          * TRUE FALSE 
841          *******************/
843         case "true" :
844         case "false" : 
845         { 
846           /* Inverse element if required */
847           if($Inverse){        
848             if($key == "true"){
849               $key = "false";
850             }else{
851               $key = "true";
852             }           
853           }
855           /* Get template */
856           $smarty = get_smarty();
857           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
858           $smarty->assign("selected"  , $key); 
859           $smarty->assign("ID"  , $element_id); 
860           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
861           break;
862         }
865         /*******************
866          * Header 
867          *******************/
869         case "header": 
870         {
871           $address_parts = $this->address_parts;
872           $comparators   = $this->comparators;
873           $match_types   = $this->match_types; 
874           $operators     = $this->operators;
876           $smarty = get_smarty();
877           $smarty->assign("comparators",$comparators);
878           $smarty->assign("match_types",$match_types);
879           $smarty->assign("operators",$operators);
880           $smarty->assign("LastError",$data['LastError']);
881           $smarty->assign("match_type", $data['Match_type']);
882           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
883           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
885           $keys = "";
886           foreach($data['Key_List'] as $key){
887             $keys .= $key.", ";
888           }
889           $keys = preg_replace("/,$/","",trim($keys));
890    
891           $values = "";
892           foreach($data['Value_List'] as $key){
893             $values .= $key.", ";
894           }
895           $values = preg_replace("/,$/","",trim($values));
897           $smarty->assign("keys",$keys);
898           $smarty->assign("Inverse",$Inverse);
899           $smarty->assign("values",$values);
900           $smarty->assign("Expert", $data['Expert']);
901  
902           $smarty->assign("ID"  , $element_id); 
903           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
904           break;
905         }
908         /*******************
909          * Envelope 
910          *******************/
912         case "envelope":
913         {
914           $address_parts = $this->address_parts;
915           $comparators   = $this->comparators;
916           $match_types   = $this->match_types; 
917           $operators     = $this->operators;
919           $smarty = get_smarty();
920           $smarty->assign("Inverse",$Inverse);
921           $smarty->assign("comparators",$comparators);
922           $smarty->assign("Expert", $data['Expert']);
923           $smarty->assign("match_types",$match_types);
924           $smarty->assign("operators",$operators);
925           $smarty->assign("LastError",$data['LastError']);
926           $smarty->assign("match_type", $data['Match_type']);
927           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
928           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
930           $keys = "";
931           foreach($data['Key_List'] as $key){
932             $keys .= $key.", ";
933           }
934           $keys = preg_replace("/,$/","",trim($keys));
936           $values = "";
937           foreach($data['Value_List'] as $key){
938             $values .= $key.", ";
939           }
940           $values = preg_replace("/,$/","",trim($values));
941           $smarty->assign("keys",$keys);
942           $smarty->assign("values",$values);
944           $smarty->assign("ID"  , $element_id); 
945           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
946           break;
947         }
950         /*******************
951          * Address 
952          *******************/
954         case "address" : 
955         {
956           $address_parts = $this->address_parts;
957           $comparators   = $this->comparators;
958           $match_types   = $this->match_types; 
959           $operators     = $this->operators;
961           $smarty = get_smarty();
962           $smarty->assign("Inverse",$Inverse);
963           $smarty->assign("address_parts",$address_parts);
964           $smarty->assign("comparators",$comparators);
965           $smarty->assign("match_types",$match_types);
966           $smarty->assign("LastError",$data['LastError']);
967           $smarty->assign("operators",$operators);
968           $smarty->assign("match_type", $data['Match_type']);
969           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
970           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
971           $smarty->assign("address_part", $data['Address_Part']);
972           $smarty->assign("Expert", $data['Expert']);
973         
974           $keys = "";
975           foreach($data['Key_List'] as $key){
976             $keys .= $key.", ";
977           }
978           $keys = preg_replace("/,$/","",trim($keys));
979    
980           $values = "";
981           foreach($data['Value_List'] as $key){
982             $values .= $key.", ";
983           }
984           $values = preg_replace("/,$/","",trim($values));
985           $smarty->assign("keys",$keys);
986           $smarty->assign("values",$values);
987           $smarty->assign("ID"  , $element_id); 
988           $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
989           break;
990         }
991       
993         /*******************
994          * Size 
995          *******************/
996         
997         case "size" : 
998         {
999           $Match_types = array( ":over" => _("greater than") , 
1000                                 ":under" => _("lower than"));
1002           $Units       = array( "M" => _("Megabyte") , 
1003                                 "K" => _("Kilobyte")); 
1005           $Match_type   = $data['Match_type'];
1006           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1007           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1008        
1009           $LastError = "";
1010           if(isset($data['LastError'])){
1011             $LastError = $data['LastError'];
1012           }
1013  
1014           $smarty = get_smarty();
1015           $smarty->assign("Inverse",$Inverse);
1016           $smarty->assign("LastError",$LastError);
1017           $smarty->assign("Match_types",$Match_types);
1018           $smarty->assign("Units",$Units);
1019           $smarty->assign("Match_type",$Match_type);
1020           $smarty->assign("Value",$Value);
1021           $smarty->assign("Value_Unit",$Value_Unit);
1022           $smarty->assign("ID"  , $element_id); 
1023           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1024           break;
1025         }
1026         
1027         /*******************
1028          * Exists 
1029          *******************/
1030         
1031         case "exists" : 
1032         {
1033           $LastError = "";
1034           if(isset($data['LastError'])){
1035             $LastError = $data['LastError'];
1036           }
1037  
1038           $Values = "";
1039           foreach($data['Values'] as $val){
1040             $Values .= $val.", ";
1041           }
1042           $Values = preg_replace("/,$/","",trim($Values));
1044           $smarty = get_smarty();
1045           $smarty->assign("LastError",$LastError);
1046           $smarty->assign("Values",$Values);
1047           $smarty->assign("Inverse",$Inverse);
1048           $smarty->assign("ID"  , $element_id); 
1049           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1050           break;
1051         }
1052   
1054         /*******************
1055          * All of   
1056          *******************/
1058         case "allof" : 
1059         {
1060           $Contents = ""; 
1061           foreach($data as $key => $dat){
1062             if(($key === "Inverse") && ($key == "Inverse")){
1063               continue;
1064             }
1065             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1066           }
1068           $smarty = get_smarty();
1069           $smarty->assign("ID"  , $element_id); 
1070           $smarty->assign("DisplayAdd",TRUE);
1071           $smarty->assign("DisplayDel",FALSE);
1072           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1073           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1075           $smarty->assign("Inverse",$Inverse);
1076           $smarty->assign("Contents",$cont_tmp.$Contents);
1077           $smarty->assign("ID"  , $element_id); 
1078           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1080           $ret = $allof_tmp;
1081           break ;
1082         } 
1085         /*******************
1086          * Any of   
1087          *******************/
1089         case "anyof" : 
1090         {
1091           $Contents = ""; 
1092           foreach($data as $key => $dat){
1093             if(($key === "Inverse") && ($key == "Inverse")){
1094               continue;
1095             }
1096             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1097           }
1098           $smarty = get_smarty();
1099           $smarty->assign("ID"  , $element_id); 
1100           $smarty->assign("DisplayAdd",TRUE);
1101           $smarty->assign("DisplayDel",FALSE);
1102           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1103           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1105           $smarty->assign("Inverse",$Inverse);
1106           $smarty->assign("Contents",$cont_tmp.$Contents);
1107           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1109           $ret = $allof_tmp;
1111           break ;
1112         } 
1113         default : 
1114         {
1115           $ret = "<table width='100%'  cellspacing=0 cellpadding=0>
1116                     <tr>
1117                       <td style='background-color: #FEDCA9 ; border: solid 1px        #EEEEEE'>";
1118           $ret.= $key."<br>"; 
1119           $ret.= "    </td>
1120                     </tr>
1121                   </table>";
1122         }
1123       }
1124     }
1125     
1126     if(!isset($smarty)){
1127       $smarty =get_smarty();
1128     }
1130     $smarty->assign("ID",$element_id);
1131     $smarty->assign("DisplayAdd",FALSE);
1132     $smarty->assign("DisplayDel",TRUE);
1133     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1134     $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1135     return($ret);
1136   }
1139   /* Parse given token identified by $data[$id] 
1140    *  and return the parsed tokens. 
1141    */
1142   function _parse($data,$id = 0)
1143   {
1144     $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1145     $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1146     $type = $data[$id]['text'];
1147     $tmp = array();
1149     /* Is there an identifier named 'not' to inverse this filter ? */
1150     $Inverse = FALSE;
1151     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1152       $Inverse = TRUE;
1153       $id ++;
1154       $type = $data[$id]['text'];
1155     }
1157     switch($type)
1158     {
1160       /****************
1161        * Parse - Envelope / Header / Address
1162        ****************/ 
1164       case "envelope" : 
1165       case "header":
1166       case "address" : 
1167       {
1168         /* Address matches are struckture as follows :
1169            [not] 
1170            address 
1171                   [address-part: tag]           all|localpart|domain|user|detail
1172                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1173                   [match-type: tag]             is|contains|matches|count|value 
1174                   <header-list: string-list> 
1175                   <key-list: string-list>   
1176           */ 
1177    
1178         
1179         $part     = "(:all|:localpart|:domain)";
1180         $operator = "(:contains|:is|:matches|:count|:value)";
1181         $value_op = "(lt|le|eq|ge|gt|ne)";
1183         $Address_Part     = "";
1184         $Comparator       = "";        
1185         $Match_type       = "";    
1186         $Match_type_value = "";
1187   
1188         $Key_List         = array();
1189         $Value_List       = array();
1190   
1191         for($i = 0 ; $i < count($data) ; $i ++){
1192          
1193           /* Get next node */ 
1194           $node = $data[$i];
1195   
1196           /* Check address part definition */
1197           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1198             $Address_Part = $node['text'];
1199           }
1201           /* Check for match type  */
1202           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1203             $Match_type = $node['text'];
1205             /* Get value operator */
1206             if(in_array($Match_type,array(":value",":count"))){
1207               $i ++;        
1208               $node = $data[$i];
1210               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1211                 $Match_type_value = $node['text'];
1212               }
1213             }
1214           } 
1216           /* Check for a comparator */
1217           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1218             $i ++;
1219             $node = $data[$i];
1220             $Comparator = $node['text'];
1221           }
1222   
1223           /* Check for Key_List */  
1224           elseif(count(sieve_get_strings($data,$i))){
1225             $tmp2 = sieve_get_strings($data,$i);
1226             $i =  $tmp2['OFFSET'];
1228             if(!count($Key_List)){
1229               $Key_List = $tmp2['STRINGS'];
1230             }else{
1231               $Value_List = $tmp2['STRINGS']; 
1232             }
1233           } 
1234       
1235         }
1236  
1237          
1238         /* Add to Tree */ 
1239         $values = array( "Inverse"         => $Inverse,
1240                                 "Comparator"      => $Comparator,
1241                                 "Expert"          => FALSE,
1242                                 "Match_type"      => $Match_type,
1243                                 "Match_type_value"=> $Match_type_value,
1244                                 "Key_List"        => $Key_List,
1245                                 "Value_List"      => $Value_List) ;
1246         if($type == "address"){
1247           $values["Address_Part"]    = $Address_Part;
1248         }
1249         $tmp[$type] = $values;
1250         $tmp[$type]['LastError'] = "";
1251         break;
1252       }
1255       /****************
1256        * Parse - Size
1257        ****************/ 
1259       case "size":
1260       {
1261     
1262         $ops = "(:over|:under)";
1264         $Match_type = "";
1266         for($i = $id ; $i < count($data); $i++){
1268           /* Get current node */
1269           $node = $data[$i];
1271           /* Get tag (under / over) */
1272           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1273             $Match_type = $node['text'];
1274           }
1275           
1276           /* Get Value_List, the value that we want to match for */
1277           elseif(count(sieve_get_strings($data,$i))){
1278             $tmp2 = sieve_get_strings($data,$i);
1279             $i =  $tmp2['OFFSET'];
1280           
1281             $Value_List = $tmp2['STRINGS'];
1282           } 
1283         }        
1284     
1285         $tmp[$type]= array( "Inverse"    => $Inverse,
1286                             "Match_type" => $Match_type,
1287                             "Value_List" => $Value_List);
1288         $tmp[$type]['LastError'] = "";
1289         break;
1290       }
1293       /****************
1294        * Parse - True / False
1295        ****************/ 
1297       case "true": 
1298       {
1299         $tmp['true'] = "true";
1300         $tmp['true']['LastError'] = "";
1301         break;
1302       }
1303       case "false":
1304       {
1305         $tmp['false'] = "false";
1306         $tmp['false']['LastError'] = "";
1307         break;
1308       }
1311       /****************
1312        * Parse - Exists
1313        ****************/ 
1315       case "exists":
1316       {
1317         
1318         /* Skip first values, [if,not,exists] */
1319         $node = $data[$id];
1320         while(in_array($node['text'],array("if","not","exists"))){
1321           $id ++;
1322           $node = $data[$id];
1323         }
1325         /* Get values */
1326         $tmp2 = sieve_get_strings($data,$id);
1327   
1328         
1329         $tmp['exists'] = array('Inverse' => $Inverse,
1330                                'Values'  => $tmp2['STRINGS']);
1331         $tmp[$type]['LastError'] = "";
1332         break;
1333       }
1336       /****************
1337        * Parse - Allof
1338        ****************/ 
1340       case "allof" :
1341       {
1342         /* Get parameter and recursivly call this method 
1343          *  for each parameter 
1344          */
1345         $id ++;
1346         $tmp2 = $this->get_parameter($data,$id);
1347         
1348         foreach($tmp2 as $parameter){
1349           $tmp['allof'][] = $this->_parse($parameter);
1350         }
1351         $tmp['allof']['Inverse'] = $Inverse;
1352         break;
1353       }
1356       /****************
1357        * Parse - Anyof
1358        ****************/ 
1360       case "anyof" :
1361       {
1362         /* Get parameter and recursivly call this method 
1363          *  for each parameter 
1364          */
1365         $id ++;
1366         $tmp2 = $this->get_parameter($data,$id);
1368         foreach($tmp2 as $parameter){
1369           $tmp['anyof'][] = $this->_parse($parameter);
1370         }
1371         $tmp['anyof']['Inverse'] = $Inverse;
1372         break;
1373       }
1374       default : $tmp[$id] = $type; 
1375     }
1376     
1377     return($tmp); 
1378   }
1381   function get_parameter($data,$id)
1382   {
1383     $par = array();
1384     $open_brakets = 0;
1385     $next = NULL;
1386     $num = 0;
1387     for($i = $id ; $i < count($data) ; $i++ ){
1388       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1389         $open_brakets ++;
1390       }
1391       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1392         $num ++;
1393       }
1394       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1395         $par[$num][] = $data[$i];
1396       }
1397       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1398         $open_brakets --;
1399       }
1400     }
1401     return($par);
1402   }
1405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1406 ?>