Code

modified templates
[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 = $this->TYPE." ".$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     }elseif($this->TYPE == "elsif"){
783       $name .= "&nbsp;-&nbsp;"._("Else if");
784     }else{
785       $name .= "&nbsp;-&nbsp;"._("Else");
786     }
788     $smarty = get_smarty();
789     $smarty->assign("ID", $this->object_id);
791     /* Only display navigation elements if necessary */
792     if($this->TYPE == "if"){
793       $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
794     }else{
795       $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
796     }
798     $smarty->assign("Name", $name);
799     $smarty->assign("Contents", $this->get_as_html());
800     $object = $smarty->fetch(get_template_path("templates/element_if_else.tpl",TRUE,dirname(__FILE__)));
801     $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
802     return($str);
803   }
805   
806   /* Returns all elements as html */
807   function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
808   {
809     $ret ="";
810     if($parsed == NULL){
811       $parsed = $this->_parsed;
812     }
814     if((!is_array($parsed)) || !count($parsed)) {
815       $smarty = get_smarty();
816       $smarty->assign("ID",$this->object_id);
817       $smarty->assign("DisplayAdd",TRUE);
818       $smarty->assign("DisplayDel",FALSE);
819       $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
820       $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
821       return($ret);
822     }
824     /* Walk through all elements */
825     foreach($parsed as $key => $data){
827       /* Create Inverse Tag */
828       if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
829         $Inverse = TRUE;
830       }else{
831         $Inverse = FALSE;
832       }
834       /* Id used to have unique html names */
835       $element_id = $this->object_id."_".$id."_".$obj_id;
837       /* Create elements */
838       switch($key)
839       {
840   
841         /*******************
842          * TRUE FALSE 
843          *******************/
845         case "true" :
846         case "false" : 
847         { 
848           /* Inverse element if required */
849           if($Inverse){        
850             if($key == "true"){
851               $key = "false";
852             }else{
853               $key = "true";
854             }           
855           }
857           /* Get template */
858           $smarty = get_smarty();
859           $smarty->assign("values"    , array("false" => _("False"), "true" => _("True")));
860           $smarty->assign("selected"  , $key); 
861           $smarty->assign("ID"  , $element_id); 
862           $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
863           break;
864         }
867         /*******************
868          * Header 
869          *******************/
871         case "header": 
872         {
873           $address_parts = $this->address_parts;
874           $comparators   = $this->comparators;
875           $match_types   = $this->match_types; 
876           $operators     = $this->operators;
878           $smarty = get_smarty();
879           $smarty->assign("comparators",$comparators);
880           $smarty->assign("match_types",$match_types);
881           $smarty->assign("operators",$operators);
882           $smarty->assign("LastError",$data['LastError']);
883           $smarty->assign("match_type", $data['Match_type']);
884           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
885           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
887           $keys = "";
888           foreach($data['Key_List'] as $key){
889             $keys .= $key.", ";
890           }
891           $keys = preg_replace("/,$/","",trim($keys));
892    
893           $values = "";
894           foreach($data['Value_List'] as $key){
895             $values .= $key.", ";
896           }
897           $values = preg_replace("/,$/","",trim($values));
899           $smarty->assign("keys",$keys);
900           $smarty->assign("Inverse",$Inverse);
901           $smarty->assign("values",$values);
902           $smarty->assign("Expert", $data['Expert']);
903  
904           $smarty->assign("ID"  , $element_id); 
905           $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
906           break;
907         }
910         /*******************
911          * Envelope 
912          *******************/
914         case "envelope":
915         {
916           $address_parts = $this->address_parts;
917           $comparators   = $this->comparators;
918           $match_types   = $this->match_types; 
919           $operators     = $this->operators;
921           $smarty = get_smarty();
922           $smarty->assign("Inverse",$Inverse);
923           $smarty->assign("comparators",$comparators);
924           $smarty->assign("Expert", $data['Expert']);
925           $smarty->assign("match_types",$match_types);
926           $smarty->assign("operators",$operators);
927           $smarty->assign("LastError",$data['LastError']);
928           $smarty->assign("match_type", $data['Match_type']);
929           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
930           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
932           $keys = "";
933           foreach($data['Key_List'] as $key){
934             $keys .= $key.", ";
935           }
936           $keys = preg_replace("/,$/","",trim($keys));
938           $values = "";
939           foreach($data['Value_List'] as $key){
940             $values .= $key.", ";
941           }
942           $values = preg_replace("/,$/","",trim($values));
943           $smarty->assign("keys",$keys);
944           $smarty->assign("values",$values);
946           $smarty->assign("ID"  , $element_id); 
947           $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
948           break;
949         }
952         /*******************
953          * Address 
954          *******************/
956         case "address" : 
957         {
958           $address_parts = $this->address_parts;
959           $comparators   = $this->comparators;
960           $match_types   = $this->match_types; 
961           $operators     = $this->operators;
963           $smarty = get_smarty();
964           $smarty->assign("Inverse",$Inverse);
965           $smarty->assign("address_parts",$address_parts);
966           $smarty->assign("comparators",$comparators);
967           $smarty->assign("match_types",$match_types);
968           $smarty->assign("LastError",$data['LastError']);
969           $smarty->assign("operators",$operators);
970           $smarty->assign("match_type", $data['Match_type']);
971           $smarty->assign("operator"  , preg_replace("/\"/","",$data['Match_type_value']));
972           $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
973           $smarty->assign("address_part", $data['Address_Part']);
974           $smarty->assign("Expert", $data['Expert']);
975         
976           $keys = "";
977           foreach($data['Key_List'] as $key){
978             $keys .= $key.", ";
979           }
980           $keys = preg_replace("/,$/","",trim($keys));
981    
982           $values = "";
983           foreach($data['Value_List'] as $key){
984             $values .= $key.", ";
985           }
986           $values = preg_replace("/,$/","",trim($values));
987           $smarty->assign("keys",$keys);
988           $smarty->assign("values",$values);
989           $smarty->assign("ID"  , $element_id); 
990           $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
991           break;
992         }
993       
995         /*******************
996          * Size 
997          *******************/
998         
999         case "size" : 
1000         {
1001           $Match_types = array( ":over" => _("greater than") , 
1002                                 ":under" => _("lower than"));
1004           $Units       = array( "M" => _("Megabyte") , 
1005                                 "K" => _("Kilobyte")); 
1007           $Match_type   = $data['Match_type'];
1008           $Value        = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1009           $Value_Unit   = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1010        
1011           $LastError = "";
1012           if(isset($data['LastError'])){
1013             $LastError = $data['LastError'];
1014           }
1015  
1016           $smarty = get_smarty();
1017           $smarty->assign("Inverse",$Inverse);
1018           $smarty->assign("LastError",$LastError);
1019           $smarty->assign("Match_types",$Match_types);
1020           $smarty->assign("Units",$Units);
1021           $smarty->assign("Match_type",$Match_type);
1022           $smarty->assign("Value",$Value);
1023           $smarty->assign("Value_Unit",$Value_Unit);
1024           $smarty->assign("ID"  , $element_id); 
1025           $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1026           break;
1027         }
1028         
1029         /*******************
1030          * Exists 
1031          *******************/
1032         
1033         case "exists" : 
1034         {
1035           $LastError = "";
1036           if(isset($data['LastError'])){
1037             $LastError = $data['LastError'];
1038           }
1039  
1040           $Values = "";
1041           foreach($data['Values'] as $val){
1042             $Values .= $val.", ";
1043           }
1044           $Values = preg_replace("/,$/","",trim($Values));
1046           $smarty = get_smarty();
1047           $smarty->assign("LastError",$LastError);
1048           $smarty->assign("Values",$Values);
1049           $smarty->assign("Inverse",$Inverse);
1050           $smarty->assign("ID"  , $element_id); 
1051           $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1052           break;
1053         }
1054   
1056         /*******************
1057          * All of   
1058          *******************/
1060         case "allof" : 
1061         {
1062           $Contents = ""; 
1063           foreach($data as $key => $dat){
1064             if(($key === "Inverse") && ($key == "Inverse")){
1065               continue;
1066             }
1067             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1068           }
1070           $smarty = get_smarty();
1071           $smarty->assign("ID"  , $element_id); 
1072           $smarty->assign("DisplayAdd",TRUE);
1073           $smarty->assign("DisplayDel",FALSE);
1074           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1075           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1077           $smarty->assign("Inverse",$Inverse);
1078           $smarty->assign("Contents",$cont_tmp.$Contents);
1079           $smarty->assign("ID"  , $element_id); 
1080           $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1082           $ret = $allof_tmp;
1083           break ;
1084         } 
1087         /*******************
1088          * Any of   
1089          *******************/
1091         case "anyof" : 
1092         {
1093           $Contents = ""; 
1094           foreach($data as $key => $dat){
1095             if(($key === "Inverse") && ($key == "Inverse")){
1096               continue;
1097             }
1098             $Contents .=        $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1099           }
1100           $smarty = get_smarty();
1101           $smarty->assign("ID"  , $element_id); 
1102           $smarty->assign("DisplayAdd",TRUE);
1103           $smarty->assign("DisplayDel",FALSE);
1104           $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1105           $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1107           $smarty->assign("Inverse",$Inverse);
1108           $smarty->assign("Contents",$cont_tmp.$Contents);
1109           $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1111           $ret = $allof_tmp;
1113           break ;
1114         } 
1115         default : 
1116         {
1117           $ret = "<table width='100%'  cellspacing=0 cellpadding=0>
1118                     <tr>
1119                       <td style='background-color: #FEDCA9 ; border: solid 1px        #EEEEEE'>";
1120           $ret.= $key."<br>"; 
1121           $ret.= "    </td>
1122                     </tr>
1123                   </table>";
1124         }
1125       }
1126     }
1127     
1128     if(!isset($smarty)){
1129       $smarty =get_smarty();
1130     }
1132     $smarty->assign("ID",$element_id);
1133     $smarty->assign("DisplayAdd",FALSE);
1134     $smarty->assign("DisplayDel",TRUE);
1135     $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1136     $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1137     return($ret);
1138   }
1141   /* Parse given token identified by $data[$id] 
1142    *  and return the parsed tokens. 
1143    */
1144   function _parse($data,$id = 0)
1145   {
1146     $av_methods   = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1147     $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1148     $type = $data[$id]['text'];
1149     $tmp = array();
1151     /* Is there an identifier named 'not' to inverse this filter ? */
1152     $Inverse = FALSE;
1153     if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1154       $Inverse = TRUE;
1155       $id ++;
1156       $type = $data[$id]['text'];
1157     }
1159     switch($type)
1160     {
1162       /****************
1163        * Parse - Envelope / Header / Address
1164        ****************/ 
1166       case "envelope" : 
1167       case "header":
1168       case "address" : 
1169       {
1170         /* Address matches are struckture as follows :
1171            [not] 
1172            address 
1173                   [address-part: tag]           all|localpart|domain|user|detail
1174                   [comparator: tag]             i;octet i;ascii-casemap i;ascii-numeric
1175                   [match-type: tag]             is|contains|matches|count|value 
1176                   <header-list: string-list> 
1177                   <key-list: string-list>   
1178           */ 
1179    
1180         
1181         $part     = "(:all|:localpart|:domain)";
1182         $operator = "(:contains|:is|:matches|:count|:value)";
1183         $value_op = "(lt|le|eq|ge|gt|ne)";
1185         $Address_Part     = "";
1186         $Comparator       = "";        
1187         $Match_type       = "";    
1188         $Match_type_value = "";
1189   
1190         $Key_List         = array();
1191         $Value_List       = array();
1192   
1193         for($i = 0 ; $i < count($data) ; $i ++){
1194          
1195           /* Get next node */ 
1196           $node = $data[$i];
1197   
1198           /* Check address part definition */
1199           if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1200             $Address_Part = $node['text'];
1201           }
1203           /* Check for match type  */
1204           elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1205             $Match_type = $node['text'];
1207             /* Get value operator */
1208             if(in_array($Match_type,array(":value",":count"))){
1209               $i ++;        
1210               $node = $data[$i];
1212               if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1213                 $Match_type_value = $node['text'];
1214               }
1215             }
1216           } 
1218           /* Check for a comparator */
1219           elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1220             $i ++;
1221             $node = $data[$i];
1222             $Comparator = $node['text'];
1223           }
1224   
1225           /* Check for Key_List */  
1226           elseif(count(sieve_get_strings($data,$i))){
1227             $tmp2 = sieve_get_strings($data,$i);
1228             $i =  $tmp2['OFFSET'];
1230             if(!count($Key_List)){
1231               $Key_List = $tmp2['STRINGS'];
1232             }else{
1233               $Value_List = $tmp2['STRINGS']; 
1234             }
1235           } 
1236       
1237         }
1238  
1239          
1240         /* Add to Tree */ 
1241         $values = array( "Inverse"         => $Inverse,
1242                                 "Comparator"      => $Comparator,
1243                                 "Expert"          => FALSE,
1244                                 "Match_type"      => $Match_type,
1245                                 "Match_type_value"=> $Match_type_value,
1246                                 "Key_List"        => $Key_List,
1247                                 "Value_List"      => $Value_List) ;
1248         if($type == "address"){
1249           $values["Address_Part"]    = $Address_Part;
1250         }
1251         $tmp[$type] = $values;
1252         $tmp[$type]['LastError'] = "";
1253         break;
1254       }
1257       /****************
1258        * Parse - Size
1259        ****************/ 
1261       case "size":
1262       {
1263     
1264         $ops = "(:over|:under)";
1266         $Match_type = "";
1268         for($i = $id ; $i < count($data); $i++){
1270           /* Get current node */
1271           $node = $data[$i];
1273           /* Get tag (under / over) */
1274           if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1275             $Match_type = $node['text'];
1276           }
1277           
1278           /* Get Value_List, the value that we want to match for */
1279           elseif(count(sieve_get_strings($data,$i))){
1280             $tmp2 = sieve_get_strings($data,$i);
1281             $i =  $tmp2['OFFSET'];
1282           
1283             $Value_List = $tmp2['STRINGS'];
1284           } 
1285         }        
1286     
1287         $tmp[$type]= array( "Inverse"    => $Inverse,
1288                             "Match_type" => $Match_type,
1289                             "Value_List" => $Value_List);
1290         $tmp[$type]['LastError'] = "";
1291         break;
1292       }
1295       /****************
1296        * Parse - True / False
1297        ****************/ 
1299       case "true": 
1300       {
1301         $tmp['true'] = "true";
1302         $tmp['true']['LastError'] = "";
1303         break;
1304       }
1305       case "false":
1306       {
1307         $tmp['false'] = "false";
1308         $tmp['false']['LastError'] = "";
1309         break;
1310       }
1313       /****************
1314        * Parse - Exists
1315        ****************/ 
1317       case "exists":
1318       {
1319         
1320         /* Skip first values, [if,not,exists] */
1321         $node = $data[$id];
1322         while(in_array($node['text'],array("if","not","exists"))){
1323           $id ++;
1324           $node = $data[$id];
1325         }
1327         /* Get values */
1328         $tmp2 = sieve_get_strings($data,$id);
1329   
1330         
1331         $tmp['exists'] = array('Inverse' => $Inverse,
1332                                'Values'  => $tmp2['STRINGS']);
1333         $tmp[$type]['LastError'] = "";
1334         break;
1335       }
1338       /****************
1339        * Parse - Allof
1340        ****************/ 
1342       case "allof" :
1343       {
1344         /* Get parameter and recursivly call this method 
1345          *  for each parameter 
1346          */
1347         $id ++;
1348         $tmp2 = $this->get_parameter($data,$id);
1349         
1350         foreach($tmp2 as $parameter){
1351           $tmp['allof'][] = $this->_parse($parameter);
1352         }
1353         $tmp['allof']['Inverse'] = $Inverse;
1354         break;
1355       }
1358       /****************
1359        * Parse - Anyof
1360        ****************/ 
1362       case "anyof" :
1363       {
1364         /* Get parameter and recursivly call this method 
1365          *  for each parameter 
1366          */
1367         $id ++;
1368         $tmp2 = $this->get_parameter($data,$id);
1370         foreach($tmp2 as $parameter){
1371           $tmp['anyof'][] = $this->_parse($parameter);
1372         }
1373         $tmp['anyof']['Inverse'] = $Inverse;
1374         break;
1375       }
1376       default : $tmp[$id] = $type; 
1377     }
1378     
1379     return($tmp); 
1380   }
1383   function get_parameter($data,$id)
1384   {
1385     $par = array();
1386     $open_brakets = 0;
1387     $next = NULL;
1388     $num = 0;
1389     for($i = $id ; $i < count($data) ; $i++ ){
1390       if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1391         $open_brakets ++;
1392       }
1393       if($data[$i]['class'] == "comma" && $open_brakets == 1){
1394         $num ++;
1395       }
1396       if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1397         $par[$num][] = $data[$i];
1398       }
1399       if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1400         $open_brakets --;
1401       }
1402     }
1403     return($par);
1404   }
1407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1408 ?>