Code

38d252302e4085fb0cf8824305a85bc4ecd56a09
[gosa.git] / include / sieve / class_My_Tree.inc
1 <?php
5 /* This class is inherited from the original 'Tree'
6  *  class written by Heiko Hund.
7  * It is partly rewritten to create a useable html interface 
8  *  for each single sieve token. 
9  * This gives us the ability to edit existing sieve filters. 
10  */
11 class My_Tree extends Tree
12 {
13   var $dumpFn_;
14   var $dump_;
16   var $mode_stack = array();
17   var $pap              = array();
18   var $parent = NULL;
20         function My_Tree(&$root,$parent)
21         {
22     $this->parent = $parent;
23                 $this->_construct($root);
24         }
26   function execute()
27   {
28     return($this->dump());
29   }
31   /* Create a html interface for the current sieve filter 
32    */
33   function dump()
34   {
35     /**************
36      * Handle new elements 
37      **************/
39     /* Only parse the tokens once */
40     if(!count($this->pap)){
41       $this->dump_ = "";
42       $this->mode_stack = array();
43       $this->pap = array();
44       $this->doDump_(0, '', true);
46       /* Add left elements */
47       if(count($this->mode_stack)){
48         foreach($this->mode_stack as $element){
49           $this->handle_elements( $element,preg_replace("/[^0-9]/","",microtime()));
50         }
51       }
52     }
54     /* Create html results */
55     $smarty = get_smarty();
57     $block_indent_start = $smarty->fetch(get_template_path("templates/block_indent_start.tpl",TRUE,dirname(__FILE__)));
58     $block_indent_stop  = $smarty->fetch(get_template_path("templates/block_indent_stop.tpl",TRUE,dirname(__FILE__))); 
59   
60   
62     $this -> dump_ = "";
63     $ends = array();
64     if(!count($this->pap)){
65 #      $this->pap[] = new sieve_comment(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
66     }
67     foreach($this->pap as $key => $object){
68       if(is_object($object)){
69         $end = $this->get_block_end($key,false);
71         if(isset($ends[$key])){
72           $this->dump_  .= $block_indent_stop;
73         }
74         $this->dump_ .= preg_replace("/>/",">\n",$object->execute()); 
75         if($end != $key && in_array(get_class($object),array("sieve_if","sieve_else","sieve_elsif"))) {
76           $ends[$end] = $end;  
77           $this->dump_  .= $block_indent_start;
78         }
79       }
80     }
81     
82     return($this->dump_);
83   }
86   /* This function walks through the object tree generated by the "Parse" class.
87    * All Commands will be resolved and grouped. So the Commands and their 
88    *  parameter are combined. Like "IF" and ":comparator"...
89    */  
90   function doDump_($node_id, $prefix, $last,$num = 1)
91   {
92     /* Indicates that current comman will only be valid for a single line. 
93      *  this command type will be removed from mode_stack after displaying it.
94      */
95     $rewoke_last = FALSE;
97     /* Get node */ 
98     $node = $this->nodes_[$node_id];
100     /* Get last element class and type */
101     $last_class = "";
102     $last_type  = "";
103     if(count($this->mode_stack)){
104       $key = key($this->mode_stack);
105       $tmp = array_reverse($this->mode_stack[$key]['ELEMENTS']);
106       $last_class = $tmp[key($tmp)]['class'];
107       
108       if(isset($this->mode_stack[$key]['TYPE'])){
109         $last_type  = $this->mode_stack[$key]['TYPE'];
110       }
111     }
113     /* This closes the last mode */
114     if($node['class'] == "block-start"){
115       $tmp = array_pop($this->mode_stack);
116       $this->handle_elements($tmp,$node_id);
117       $this->handle_elements(array("TYPE" => "block_start"),preg_replace("/[^0-9]/","",microtime()));
118     }
120     /* This closes the last mode */
121     if($node['class'] == "block-end"){
122       $tmp = array_pop($this->mode_stack);
123       $this->handle_elements($tmp,$node_id);
124       $this->handle_elements(array("TYPE" => "block_end"),preg_replace("/[^0-9]/","",microtime()));
125     }
127     /* Semicolon indicates a new command */
128     if($node['class'] == "semicolon"){
129       $tmp =array_pop($this->mode_stack);
130       $this->handle_elements($tmp,$node_id);
131     }
133     /* We can't handle comments within if tag right now */
134     if(!in_array_ics($last_type,array("if","elsif"))){
136       /* Comments require special attention.
137        * We do not want to create a single comment element 
138        *  foreach each  "#comment"  string found in the script. 
139        * Sometimes comments are used like this 
140        *   # This is a comment
141        *   #  and it still is a comment 
142        *   #  ...
143        * So we combine them to one single comment.
144        */
145       if($last_class != "comment" && $node['class'] == "comment"){
146         $tmp =array_pop($this->mode_stack);
147         $this->handle_elements($tmp,$node_id);
148         $this->mode_stack[] = array("TYPE" => $node['class']); 
149       }  
151       if($last_class == "comment" && $node['class'] != "comment"){
152         $tmp =array_pop($this->mode_stack);
153         $this->handle_elements($tmp,$node_id);
154       }
155     }
157     /* Handle identifiers */
158     $identifiers = array("else","if","elsif","end","reject","redirect","vacation","keep","discard","fileinto","require","stop");
159     if($node['class'] == "identifier" && in_array($node['text'],$identifiers)){
160       $this->mode_stack[] = array("TYPE" => $node['text']); 
161     }
163     if(!($last_type == "if" && $node['class'] == "comment")){
164       /* Add current node to current command stack */
165       end($this->mode_stack);
166       $key = key($this->mode_stack);
167       $this->mode_stack[$key]['ELEMENTS'][] = $node;
168     }
170     /* Remove last mode from mode stack, cause it was only valid for a single line */
171     if($rewoke_last){
172       $tmp =array_pop($this->mode_stack);
173       $this->handle_elements($tmp,$node_id);
174     }
176     /* If this is a sub element, just call this for all childs */       
177     if(isset($this->childs_[$node_id])){
178       $childs = $this->childs_[$node_id];
179       for ($i=0; $i<count($childs); ++$i)
180       {
181         $c_last = false;
182         if ($i+1 == count($childs))
183         {
184           $c_last = true;
185         }
186         $this->doDump_($childs[$i], "", $num);
187       }
188     }
189   }
192   /* Create a class for each resolved object.
193    * And append this class to a list of objects.
194    */
195   function handle_elements($data,$id)
196   {
197     if(!isset($data['TYPE'])){
198       return;
199     }
200     $type = $data['TYPE'];
201     
202     $class_name= "sieve_".$type ;
203     if(class_exists($class_name)){
204       $this->pap[] = new $class_name($data,$id,$this);
205     }else{
206       echo "<font color='red'>Missing : ".$class_name."</font>"."<br>";
207     }
208   }
210   function save_object()
211   {
212     reset($this->pap);
213     foreach($this->pap as $key => $obj){
215       if(in_array(get_class($obj),array("sieve_if",
216                                         "sieve_elsif",
217                                         "sieve_vacation",
218                                         "sieve_comment",
219                                         "sieve_reject",
220                                         "sieve_fileinto",
221                                         "sieve_require",
222                                         "sieve_redirect"))){
225         if(isset($this->pap[$key]) && method_exists($this->pap[$key],"save_object")){
226           $this->pap[$key]->save_object();
227         }
228       }
229     }
230   }
233   /* Remove the object at the given position */
234   function remove_object($key_id)
235   {
236     if(count($this->pap) == 1){
237       print_red(_("Can't remove last element."));
238       return;
239     }
241     if(!isset($this->pap[$key_id])){
242       trigger_error(_("Can't remove element with object_id=".$key_id.", there is no object with this identifier. Remove aborted."));
243       return(false);
244     }
246     $class = get_class($this->pap[$key_id]);
247     if(in_array($class,array("sieve_if","sieve_elsif","sieve_else"))){
248       $block_start= $key_id;
249       $block_end  = $this->get_block_end($key_id);
251       for($i = $block_start ; $i <= $block_end ; $i ++ ){
252         unset($this->pap[$i]);
253       }
254     }else{
255       unset($this->pap[$key_id]);
256     }
257     $tmp = array();
258     foreach($this->pap as $element){
259       $tmp[] = $element;
260     }
261     $this->pap = $tmp;
262   }
265   /* This function moves a given element to another position.
266    * Single elements like "keep;" will simply be moved one posisition down/up.
267    * Multiple elements like if-elsif-else will be moved as block. 
268    * 
269    *  $key_id     specified the element that should be moved.
270    *  $direction  specifies to move elements "up" or "down"
271    */
272   function move_up_down($key_id,$direction = "down")
273   {
274      
275     /* Get the current element to decide what to move. */ 
276     $e_class = get_class($this->pap[$key_id]);
277       
278     if(in_array($e_class,array("sieve_if"))){
279       $block_start= $key_id;
280       $block_end  = $this->get_block_end($key_id);
282       /* Depending on the direction move up down */
283       if($direction == "down"){
284         $next_free  = $this->_get_next_free_move_slot($block_end,$direction); 
285       }else{
286         $next_free  = $this->_get_next_free_move_slot($block_start,$direction); 
287       }
289       /* Move the given block */ 
290       $this->move_multiple_elements($block_start,$block_end,$next_free);
291     }
293     if(in_array($e_class,array( "sieve_stop",
294                                 "sieve_keep",
295                                 "sieve_require",
296                                 "sieve_comment",
297                                 "sieve_vacation",
298                                 "sieve_stop",   
299                                 "sieve_reject", 
300                                 "sieve_fileinto",
301                                 "sieve_redirect", 
302                                 "sieve_discard"))){
303       $this->move_single_element($key_id,$this->_get_next_free_move_slot($key_id,$direction));
304     }
305   }
307   
308   /* Move the given block to position */
309   function move_multiple_elements($start,$end,$to)
310   {
311     /* Use class names for testing */
312     $data = $this->pap;
314     /* Get block to move */
315     $block_to_move = array_slice($data,$start, ($end - $start +1));
317     /* We want do move this block up */
318     if($end > $to){
319       
320       /* Get start block */
321       $start_block = array_slice($data,0,$to);
323       /* Get Get all elements between the block to move 
324        *  and next free position 
325        */
326       $block_to_free = array_slice($data,$to ,$start - $to );  
327       $block_to_end = array_slice($data,$end+1);
328       $new = array();
329       foreach($start_block as $block){
330         $new[] = $block;
331       }
332       foreach($block_to_move as $block){
333         $new[] = $block;
334       }
335       foreach($block_to_free as $block){
336         $new[] = $block;
337       }
338       foreach($block_to_end as $block){
339         $new[] = $block;
340       }
341       $old = $this->pap;
342       $this->pap = $new;
343     }
344     
346     /* We want to move this block down. */
347     if($to > $end){
349       /* Get start block */
350       $start_block = array_slice($data,0,$start);
352       /* Get Get all elements between the block to move 
353        *  and next free position 
354        */
355       $block_to_free = array_slice($data,$end +1,($to - $end  ));  
357       /* Get the rest 
358        */
359       $block_to_end = array_slice($data,$to+1);
361       $new = array();
362       foreach($start_block as $block){
363         $new[] = $block;
364       }
365       foreach($block_to_free as $block){
366         $new[] = $block;
367       }
368       foreach($block_to_move as $block){
369         $new[] = $block;
370       }
371       foreach($block_to_end as $block){
372         $new[] = $block;
373       }
374       $old = $this->pap;
375       $this->pap = $new;
376     }
377   }  
379   
380   /* This function returns the id of the element 
381    *  where the current block ends  
382    */
383   function get_block_end($start,$complete = TRUE)
384   {
385     /* Only execute if this is a really a block element. 
386      * Block elements is only sieve_if
387      */
388     if(in_array(get_class($this->pap[$start]),array("sieve_if","sieve_elsif","sieve_else"))){
390       $class      = get_class($this->pap[$start]);
391       $next_class = get_class($this->pap[$start+1]);
392       $block_depth = 0;
394       $end = FALSE;
396       while(!$end && $start < count($this->pap)){
397  
398         if($class == "sieve_block_start"){
399           $block_depth ++;
400         }
402         if($class == "sieve_block_end"){
403           $block_depth --;
404         }
406         if($complete){
407           if( $block_depth == 0 && 
408               $class == "sieve_block_end" && 
409               !in_array($next_class,array("sieve_else","sieve_elsif"))){
410             $end = TRUE;
411             $start --;
412           }
413         }else{
415           if( $block_depth == 0 && 
416               $class == "sieve_block_end" ){ 
417             $end = TRUE;
418             $start --;
419           }
420         }
422         $start ++;       
423         $class      = get_class($this->pap[$start]);
424         
425         if(isset($this->pap[$start+1])){ 
426           $next_class = get_class($this->pap[$start+1]);
427         }else{
428           $next_class ="";
429         }
430       }
431     }
432     return($start);
433   }
436   /* This function moves the single element at 
437    *  position $from to position $to.
438    */
439   function move_single_element($from,$to)
440   {
441     if($from == $to) {
442       return;
443     }
445     $ret = array();
446     $tmp = $this->pap;
448     $begin = array();
449     $middle = array();
450     $end = array();
451     $element = $this->pap[$from];
453     if($from > $to ){
455       /* Get all element in fron to element to move */    
456       if($from  != 0){
457         $begin = array_slice($tmp,0,$to);
458       }
460       /* Get all elements between */
461       $middle = array_slice($tmp,$to , ($from - ($to) ));  
462     
463       /* Get the rest */ 
464       $end  = array_slice($tmp,$from+1);
465  
466       foreach($begin as $data){
467         $ret[] = $data;
468       }
469       $ret[] = $element;
470       foreach($middle as $data){
471         $ret[] = $data;
472       }
473       foreach($end as $data){
474         $ret[] = $data;
475       }
476       $this->pap = $ret;
477     }
478     if($from < $to ){
480       /* Get all element in fron to element to move */    
481       if($from  != 0){
482         $begin = array_slice($tmp,0,$from);
483       }
485       /* Get all elements between */
486       $middle = array_slice($tmp,$from+1 , ($to - ($from)));  
487     
488       /* Get the rest */ 
489       $end  = array_slice($tmp,$to+1);
490  
491       foreach($begin as $data){
492         $ret[] = $data;
493       }
494       foreach($middle as $data){
495         $ret[] = $data;
496       }
497       $ret[] = $element;
498       foreach($end as $data){
499         $ret[] = $data;
500       }
501       $this->pap = $ret;
502     }
503   }
506   /* Returns the next free position where we 
507    *  can add a new sinle element 
508    *    $key_id     = Current position
509    *    $direction  = Forward or backward.
510    */
511   function _get_next_free_move_slot($key_id,$direction,$include_self = FALSE)
512   {
513     $last_class = "";
514     $current_class ="";
515     $next_class = "";
517     /* After this elements we can add new elements 
518      *  without having any trouble.
519      */
520     $allowed_to_add_after = array("sieve_keep",
521                                   "sieve_require", 
522                                   "sieve_stop", 
523                                   "sieve_reject", 
524                                   "sieve_fileinto", 
525                                   "sieve_redirect", 
526                                   "sieve_discard",
527                                   "sieve_comment",
528                                   "sieve_block_start"
529                                  );
531     /* Before this elements we can add new elements 
532      *  without having any trouble.
533      */
534     $allowed_to_add_before = array("sieve_keep",
535                                   "sieve_require", 
536                                   "sieve_stop", 
537                                   "sieve_reject", 
538                                   "sieve_fileinto", 
539                                   "sieve_comment",
540                                   "sieve_redirect", 
541                                   "sieve_discard",
542                                   "sieve_if", 
543                                   "sieve_block_end"
544                                  );
546     if($direction == "down"){
547     
548       $test = $this->pap;
549       while($key_id < count($test)){
550         if(($key_id+1) == count($test)) {
551           return($key_id);
552         }
554         if(!$include_self){
555           $key_id ++;
556         }
557         $include_self = FALSE;
558         $current_class  = get_class($test[$key_id]);
559         if(in_array($current_class, $allowed_to_add_after)){
560           return($key_id);
561         } 
562       } 
563     }else{
564   
565       $test = $this->pap;
566       if($key_id == 0) {
567         return($key_id);
568       }
569       if(!$include_self){
570         $key_id --;
571       }
572       while($key_id >=0 ){
573         $current_class  = get_class($test[$key_id]);
574         if(in_array($current_class, $allowed_to_add_before)){
575           return($key_id);
576         } 
577         $key_id --;
578       }
579       return(0);
580     }
581   }
584   /* Need to be reviewed */
585   function get_sieve_script()
586   {
587     $tmp ="";
588     if(count($this->pap)){
589       $buffer = "";    
590       foreach($this->pap as $part)  {
591         if(get_class($part) == "sieve_block_end"){
592           $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
593         }
594         $tmp2 = $part->get_sieve_script_part();
596         $tmp3 = split("\n",$tmp2);
597         foreach($tmp3 as $str){
598           $str2 = trim($str);
600           /* If the current line only contains an '.'    
601            *  we must skip the line indent. 
602            * The text: statement uses a single '.' to mark the text end.
603            * This '.' must be the only char in the current line, no
604            *  whitespaces are allowed here.
605            */
606           if($str2 == "."){
607             $tmp.=$str."\n";
608           }else{
609             $tmp.= $buffer.$str."\n";
610           }
611         }
612         if(get_class($part) == "sieve_block_start"){
613           $buffer .= SIEVE_INDENT_TAB;
614         }
615       }
616     }
617     if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
618 #      $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
619     }
620     return($tmp);
621   }
623   function check()
624   {
625                 $msgs = array();
627     /* Some logical checks. 
628      *  like :  only sieve_comment can appear before require.
629      */
630     
631     /* Ensure that there are no command before require 
632      *  - Get id of last require tag
633      *  - Collect object types in from of this tag. 
634      *  - Check if there are tags collected that are not allowed 
635      */
636     $last_found_at = -1; 
637     $objs = array();
638     foreach($this->pap as $key => $obj){
639       if(get_class($obj) == "sieve_require"){
640         $last_found_at = $key;
641       }
642     }
643     foreach($this->pap as $key => $obj){
644       if($key == $last_found_at) break;  
645       if(!in_array(get_class($obj),array("sieve_comment","sieve_require"))){
646         $objs[] = get_class($obj);
647       }
648     }
649     if(count($objs) && $last_found_at != -1){
650       $str = _("Require must be the first command in the script.");  
651       $msgs[] = $str;
652       print_red($str);;
653     }
654     
655                 foreach($this->pap as $obj){
656                         $o_msgs = $obj->check();
657                         foreach($o_msgs as $o_msg){
658                                 $msgs[] = $o_msg;
659                         }
660                 }
661                 return($msgs);
662   }
665   /* We are forced to add a new require.
666    *  This function is called by the 
667    *  sieveElement_Classes->parent->add_require()  
668    */ 
669   function add_require($str)
670   {
671     $require_id = -1;
672     foreach($this->pap as $key => $obj){
673       if(get_class($obj) == "sieve_require"){
674         $require_id = $key;
675       }
676     }
677   
678     /* No require found, add one */
679     if($require_id == -1){
680       $require = new sieve_require(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
681       $require -> Add_Require($str);
682       $new = array();
683       $new[] = $require;
684       foreach($this->pap as $obj){
685         $new[] = $obj;
686       }
687       $this->pap = $new;
688     } else { 
689       $this->pap[$require_id]->Add_Require($str);
690     } 
691   }
695 /* Create valid sieve string/string-list 
696  *  out of a given array
697  */
698 function sieve_create_strings($data,$force_string = FALSE)
700   $ret = "";
701   if(is_array($data)){
702     if(count($data) == 1){
703       $ret = "\"";
704       foreach($data as $dat){
705         $ret .=$dat;
706       }
707       $ret.="\"";
708     }else{
709       foreach($data as $dat){
710         $ret.= "\"";
711         $ret.=$dat;
712         $ret.="\", ";
713       }
714       $ret = preg_replace("/,$/","",trim($ret));
715       $ret = "[".$ret."]";
716     }
717   }else{
719     $Multiline = preg_match("/\n/",$data);
720     $data = preg_replace("/\r/","",$data);;
722     if($Multiline && !$force_string){
723       $ret = "text: \r\n".$data."\r\n.\r\n";
724     }else{
725       $ret = "\"".$data."\"";
726     }
727   }
728   $ret = preg_replace("/\"\"/","\"",$ret);
729   $ret = preg_replace("/\n/","\r\n",$ret);
730   
731   return($ret);
734 /* This checks if there is a string at the current position 
735  *  in the token array. 
736  * If there is a string list at the current position,
737  *  this function will return a complete list of all
738  *  strings used in this list.
739  * It also returns an offset of the last token position 
740  */
741 function sieve_get_strings($data,$id)
743   $ret = array();
744   if($data[$id]['class'] == "left-bracket"){
745     while(isset($data[$id]) && $data[$id]['class']  != "right-bracket" && $id < count($data)){
746       
747       if($data[$id]['class'] == "quoted-string"){
748         $ret[] = $data[$id]['text'];
749       }
751       $id ++;
752     }
753   }elseif($data[$id]['class'] == "quoted-string"){
754     $ret[] = $data[$id]['text'];
755   }elseif($data[$id]['class'] == "number"){
756     $ret[] = $data[$id]['text'];
757   }elseif($data[$id]['class'] == "multi-line"){
758     $str = trim(preg_replace("/^text:/","",$data[$id]['text']));
759     $str = trim(preg_replace("/\.$/","",$str));
760     $ret[] = $str;
761   }
762   
763   return(array("OFFSET" => $id, "STRINGS" => $ret));
766 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
767 ?>