Code

361fa3f916518e1ee9567a1958e66a42104095b3
[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);
45     }
47     /* Create html results */
48     $smarty = get_smarty();
50     $block_indent_start = $smarty->fetch(get_template_path("templates/block_indent_start.tpl",TRUE,dirname(__FILE__)));
51     $block_indent_stop  = $smarty->fetch(get_template_path("templates/block_indent_stop.tpl",TRUE,dirname(__FILE__))); 
52   
53   
55     $this -> dump_ = "";
56     $ends = array();
57     if(!count($this->pap)){
58 #      $this->pap[] = new sieve_comment(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
59     }
60     foreach($this->pap as $key => $object){
61       if(is_object($object)){
62         $end = $this->get_block_end($key);
64         if($end != $key && get_class($object) == "sieve_if") {
65           $ends[$end] = $end;  
66           $this->dump_  .= $block_indent_start;
67         }
68         $this->dump_ .= preg_replace("/>/",">\n",$object->execute()); 
69         if(isset($ends[$key])){
70           $this->dump_  .= $block_indent_stop;
71         }
72       }
73     }
74     
75     return($this->dump_);
76   }
79   /* This function walks through the object tree generated by the "Parse" class.
80    * All Commands will be resolved and grouped. So the Commands and their 
81    *  parameter are combined. Like "IF" and ":comparator"...
82    */  
83   function doDump_($node_id, $prefix, $last,$num = 1)
84   {
85     /* Indicates that current comman will only be valid for a single line. 
86      *  this command type will be removed from mode_stack after displaying it.
87      */
88     $rewoke_last = FALSE;
90     /* Get node */ 
91     $node = $this->nodes_[$node_id];
93     /* Get last element class and type */
94     $last_class = "";
95     $last_type  = "";
96     if(count($this->mode_stack)){
97       $key = key($this->mode_stack);
98       $tmp = array_reverse($this->mode_stack[$key]['ELEMENTS']);
99       $last_class = $tmp[key($tmp)]['class'];
100       
101       if(isset($this->mode_stack[$key]['TYPE'])){
102         $last_type  = $this->mode_stack[$key]['TYPE'];
103       }
104     }
106     /* This closes the last mode */
107     if($node['class'] == "block-start"){
108       $tmp = array_pop($this->mode_stack);
109       $this->handle_elements($tmp,$node_id);
110       $this->handle_elements(array("TYPE" => "block_start"),preg_replace("/[^0-9]/","",microtime()));
111     }
113     /* This closes the last mode */
114     if($node['class'] == "block-end"){
115       $tmp = array_pop($this->mode_stack);
116       $this->handle_elements($tmp,$node_id);
117       $this->handle_elements(array("TYPE" => "block_end"),preg_replace("/[^0-9]/","",microtime()));
118     }
120     /* Semicolon indicates a new command */
121     if($node['class'] == "semicolon"){
122       $tmp =array_pop($this->mode_stack);
123       $this->handle_elements($tmp,$node_id);
124     }
126     /* We can't handle comments within if tag right now */
127     if($last_type != "if"){
129       /* Comments require special attention.
130        * We do not want to create a single comment element 
131        *  foreach each  "#comment"  string found in the script. 
132        * Sometimes comments are used like this 
133        *   # This is a comment
134        *   #  and it still is a comment 
135        *   #  ...
136        * So we combine them to one single comment.
137        */
138       if($last_class != "comment" && $node['class'] == "comment"){
139         $tmp =array_pop($this->mode_stack);
140         $this->handle_elements($tmp,$node_id);
141         $this->mode_stack[] = array("TYPE" => $node['class']); 
142       }  
144       if($last_class == "comment" && $node['class'] != "comment"){
145         $tmp =array_pop($this->mode_stack);
146         $this->handle_elements($tmp,$node_id);
147       }
148     }
150     /* Handle identifiers */
151     $identifiers = array("else","if","elsif","end","reject","redirect","vacation","keep","discard","fileinto","require","stop");
152     if($node['class'] == "identifier" && in_array($node['text'],$identifiers)){
153       $this->mode_stack[] = array("TYPE" => $node['text']); 
154     }
156     if(!($last_type == "if" && $node['class'] == "comment")){
157       /* Add current node to current command stack */
158       end($this->mode_stack);
159       $key = key($this->mode_stack);
160       $this->mode_stack[$key]['ELEMENTS'][] = $node;
161     }
163     /* Remove last mode from mode stack, cause it was only valid for a single line */
164     if($rewoke_last){
165       $tmp =array_pop($this->mode_stack);
166       $this->handle_elements($tmp,$node_id);
167     }
169     /* If this is a sub element, just call this for all childs */       
170     if(isset($this->childs_[$node_id])){
171       $childs = $this->childs_[$node_id];
172       for ($i=0; $i<count($childs); ++$i)
173       {
174         $c_last = false;
175         if ($i+1 == count($childs))
176         {
177           $c_last = true;
178         }
179         $this->doDump_($childs[$i], "", $num);
180       }
181     }
182   }
185   /* Create a class for each resolved object.
186    * And append this class to a list of objects.
187    */
188   function handle_elements($data,$id)
189   {
190     if(!isset($data['TYPE'])){
191       return;
192     }
193     $type = $data['TYPE'];
194     
195     $class_name= "sieve_".$type ;
196     if(class_exists($class_name)){
197       $this->pap[] = new $class_name($data,$id,$this);
198     }else{
199       echo "<font color='red'>Missing : ".$class_name."</font>"."<br>";
200     }
201   }
203   function save_object()
204   {
205     reset($this->pap);
206     foreach($this->pap as $key => $obj){
208       if(in_array(get_class($obj),array("sieve_if",
209                                         "sieve_elsif",
210                                         "sieve_vacation",
211                                         "sieve_comment",
212                                         "sieve_reject",
213                                         "sieve_fileinto",
214                                         "sieve_require",
215                                         "sieve_redirect"))){
218         if(isset($this->pap[$key]) && method_exists($this->pap[$key],"save_object")){
219           $this->pap[$key]->save_object();
220         }
221       }
222     }
223   }
226   /* Remove the object at the given position */
227   function remove_object($key_id)
228   {
229     if(count($this->pap) == 1){
230       print_red(_("Can't remove last element."));
231       return;
232     }
234     $class = get_class($this->pap[$key_id]);
235     if(in_array($class,array("sieve_if","sieve_elsif","sieve_else"))){
236       $block_start= $key_id;
237       $block_end  = $this->get_block_end($key_id);
239       for($i = $block_start ; $i <= $block_end ; $i ++ ){
240         unset($this->pap[$i]);
241       }
242     }else{
243       unset($this->pap[$key_id]);
244     }
245     $tmp = array();
246     foreach($this->pap as $element){
247       $tmp[] = $element;
248     }
249     $this->pap = $tmp;
250   }
253   /* This function moves a given element to another position.
254    * Single elements like "keep;" will simply be moved one posisition down/up.
255    * Multiple elements like if-elsif-else will be moved as block. 
256    * 
257    *  $key_id     specified the element that should be moved.
258    *  $direction  specifies to move elements "up" or "down"
259    */
260   function move_up_down($key_id,$direction = "down")
261   {
262      
263     /* Get the current element to decide what to move. */ 
264     $e_class = get_class($this->pap[$key_id]);
265       
266     if(in_array($e_class,array("sieve_if"))){
267       $block_start= $key_id;
268       $block_end  = $this->get_block_end($key_id);
270       /* Depending on the direction move up down */
271       if($direction == "down"){
272         $next_free  = $this->_get_next_free_move_slot($block_end,$direction); 
273       }else{
274         $next_free  = $this->_get_next_free_move_slot($block_start,$direction); 
275       }
277       /* Move the given block */ 
278       $this->move_multiple_elements($block_start,$block_end,$next_free);
279     }
281     if(in_array($e_class,array( "sieve_stop",
282                                 "sieve_keep",
283                                 "sieve_require",
284                                 "sieve_comment",
285                                 "sieve_vacation",
286                                 "sieve_stop",   
287                                 "sieve_reject", 
288                                 "sieve_fileinto",
289                                 "sieve_redirect", 
290                                 "sieve_discard"))){
291       $this->move_single_element($key_id,$this->_get_next_free_move_slot($key_id,$direction));
292     }
293   }
295   
296   /* Move the given block to position */
297   function move_multiple_elements($start,$end,$to)
298   {
299     /* Use class names for testing */
300     $data = $this->pap;
302     /* Get block to move */
303     $block_to_move = array_slice($data,$start, ($end - $start +1));
305     /* We want do move this block up */
306     if($end > $to){
307       
308       /* Get start block */
309       $start_block = array_slice($data,0,$to);
311       /* Get Get all elements between the block to move 
312        *  and next free position 
313        */
314       $block_to_free = array_slice($data,$to ,$start - $to );  
315       $block_to_end = array_slice($data,$end+1);
316       $new = array();
317       foreach($start_block as $block){
318         $new[] = $block;
319       }
320       foreach($block_to_move as $block){
321         $new[] = $block;
322       }
323       foreach($block_to_free as $block){
324         $new[] = $block;
325       }
326       foreach($block_to_end as $block){
327         $new[] = $block;
328       }
329       $old = $this->pap;
330       $this->pap = $new;
331     }
332     
334     /* We want to move this block down. */
335     if($to > $end){
337       /* Get start block */
338       $start_block = array_slice($data,0,$start);
340       /* Get Get all elements between the block to move 
341        *  and next free position 
342        */
343       $block_to_free = array_slice($data,$end +1,($to - $end  ));  
345       /* Get the rest 
346        */
347       $block_to_end = array_slice($data,$to+1);
349       $new = array();
350       foreach($start_block as $block){
351         $new[] = $block;
352       }
353       foreach($block_to_free as $block){
354         $new[] = $block;
355       }
356       foreach($block_to_move as $block){
357         $new[] = $block;
358       }
359       foreach($block_to_end as $block){
360         $new[] = $block;
361       }
362       $old = $this->pap;
363       $this->pap = $new;
364     }
365   }  
367   
368   /* This function returns the id of the element 
369    *  where the current block ends  
370    */
371   function get_block_end($start)
372   {
373     /* Only execute if this is a really a block element. 
374      * Block elements is only sieve_if
375      */
376     if(in_array(get_class($this->pap[$start]),array("sieve_if","sieve_elsif","sieve_else"))){
378       $class      = get_class($this->pap[$start]);
379       $next_class = get_class($this->pap[$start+1]);
380       $block_depth = 0;
382       $end = FALSE;
384       while(!$end && $start < count($this->pap)){
385  
386         if($class == "sieve_block_start"){
387           $block_depth ++;
388         }
390         if($class == "sieve_block_end"){
391           $block_depth --;
392         }
394         if( $block_depth == 0 && 
395             $class == "sieve_block_end" && 
396             !in_array($next_class,array("sieve_else","sieve_elsif"))){
397           $end = TRUE;
398           $start --;
399         }
400         $start ++;       
401         $class      = get_class($this->pap[$start]);
402         
403         if(isset($this->pap[$start+1])){ 
404           $next_class = get_class($this->pap[$start+1]);
405         }else{
406           $next_class ="";
407         }
408       }
409     }
410     return($start);
411   }
414   /* This function moves the single element at 
415    *  position $from to position $to.
416    */
417   function move_single_element($from,$to)
418   {
419     if($from == $to) {
420       return;
421     }
423     $ret = array();
424     $tmp = $this->pap;
426     $begin = array();
427     $middle = array();
428     $end = array();
429     $element = $this->pap[$from];
431     if($from > $to ){
433       /* Get all element in fron to element to move */    
434       if($from  != 0){
435         $begin = array_slice($tmp,0,$to);
436       }
438       /* Get all elements between */
439       $middle = array_slice($tmp,$to , ($from - ($to) ));  
440     
441       /* Get the rest */ 
442       $end  = array_slice($tmp,$from+1);
443  
444       foreach($begin as $data){
445         $ret[] = $data;
446       }
447       $ret[] = $element;
448       foreach($middle as $data){
449         $ret[] = $data;
450       }
451       foreach($end as $data){
452         $ret[] = $data;
453       }
454       $this->pap = $ret;
455     }
456     if($from < $to ){
458       /* Get all element in fron to element to move */    
459       if($from  != 0){
460         $begin = array_slice($tmp,0,$from);
461       }
463       /* Get all elements between */
464       $middle = array_slice($tmp,$from+1 , ($to - ($from)));  
465     
466       /* Get the rest */ 
467       $end  = array_slice($tmp,$to+1);
468  
469       foreach($begin as $data){
470         $ret[] = $data;
471       }
472       foreach($middle as $data){
473         $ret[] = $data;
474       }
475       $ret[] = $element;
476       foreach($end as $data){
477         $ret[] = $data;
478       }
479       $this->pap = $ret;
480     }
481   }
484   /* Returns the next free position where we 
485    *  can add a new sinle element 
486    *    $key_id     = Current position
487    *    $direction  = Forward or backward.
488    */
489   function _get_next_free_move_slot($key_id,$direction,$include_self = FALSE)
490   {
491     $last_class = "";
492     $current_class ="";
493     $next_class = "";
495     /* After this elements we can add new elements 
496      *  without having any trouble.
497      */
498     $allowed_to_add_after = array("sieve_keep",
499                                   "sieve_require", 
500                                   "sieve_stop", 
501                                   "sieve_reject", 
502                                   "sieve_fileinto", 
503                                   "sieve_redirect", 
504                                   "sieve_discard",
505                                   "sieve_comment",
506                                   "sieve_block_start"
507                                  );
509     /* Before this elements we can add new elements 
510      *  without having any trouble.
511      */
512     $allowed_to_add_before = array("sieve_keep",
513                                   "sieve_require", 
514                                   "sieve_stop", 
515                                   "sieve_reject", 
516                                   "sieve_fileinto", 
517                                   "sieve_comment",
518                                   "sieve_redirect", 
519                                   "sieve_discard",
520                                   "sieve_if", 
521                                   "sieve_block_end"
522                                  );
524     if($direction == "down"){
525     
526       $test = $this->pap;
527       while($key_id < count($test)){
528         if(($key_id+1) == count($test)) {
529           return($key_id);
530         }
532         if(!$include_self){
533           $key_id ++;
534         }
535         $include_self = FALSE;
536         $current_class  = get_class($test[$key_id]);
537         if(in_array($current_class, $allowed_to_add_after)){
538           return($key_id);
539         } 
540       } 
541     }else{
542   
543       $test = $this->pap;
544       if($key_id == 0) {
545         return($key_id);
546       }
547       if(!$include_self){
548         $key_id --;
549       }
550       while($key_id >=0 ){
551         $current_class  = get_class($test[$key_id]);
552         if(in_array($current_class, $allowed_to_add_before)){
553           return($key_id);
554         } 
555         $key_id --;
556       }
557       return(0);
558     }
559   }
562   /* Need to be reviewed */
563   function get_sieve_script()
564   {
565     $tmp ="";
566     if(count($this->pap)){
567       $buffer = "";    
568       foreach($this->pap as $part)  {
569         if(get_class($part) == "sieve_block_end"){
570           $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
571         }
572         $tmp2 = $part->get_sieve_script_part();
574         $tmp3 = split("\n",$tmp2);
575         foreach($tmp3 as $str){
576           $str2 = trim($str);
577           $tmp.= $buffer.$str."\n";
578         }
579         if(get_class($part) == "sieve_block_start"){
580           $buffer .= SIEVE_INDENT_TAB;
581         }
582       }
583     }
584     if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
585 #      $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
586     }
587     return($tmp);
588   }
590   function check()
591   {
592                 $msgs = array();
594     /* Some logical checks. 
595      *  like :  only sieve_comment can appear before require.
596      */
597     
598     /* Ensure that there are no command before require 
599      *  - Get id of last require tag
600      *  - Collect object types in from of this tag. 
601      *  - Check if there are tags collected that are not allowed 
602      */
603     $last_found_at = -1; 
604     $objs = array();
605     foreach($this->pap as $key => $obj){
606       if(get_class($obj) == "sieve_require"){
607         $last_found_at = $key;
608       }
609     }
610     foreach($this->pap as $key => $obj){
611       if($key == $last_found_at) break;  
612       if(!in_array(get_class($obj),array("sieve_comment","sieve_require"))){
613         $objs[] = get_class($obj);
614       }
615     }
616     if(count($objs) && $last_found_at != -1){
617       $str = _("Require must be the first command in the script.");  
618       $msgs[] = $str;
619       print_red($str);;
620     }
621     
622                 foreach($this->pap as $obj){
623                         $o_msgs = $obj->check();
624                         foreach($o_msgs as $o_msg){
625                                 $msgs[] = $o_msg;
626                         }
627                 }
628                 return($msgs);
629   }
632   /* We are forced to add a new require.
633    *  This function is called by the 
634    *  sieveElement_Classes->parent->add_require()  
635    */ 
636   function add_require($str)
637   {
638     $require_id = -1;
639     foreach($this->pap as $key => $obj){
640       if(get_class($obj) == "sieve_require"){
641         $require_id = $key;
642       }
643     }
644   
645     /* No require found, add one */
646     if($require_id == -1){
647       $require = new sieve_require(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
648       $require -> Add_Require($str);
649       $new = array();
650       $new[] = $require;
651       foreach($this->pap as $obj){
652         $new[] = $obj;
653       }
654       $this->pap = $new;
655     } else { 
656       $this->pap[$require_id]->Add_Require($str);
657     } 
658   }
662 /* Create valid sieve string/string-list 
663  *  out of a given array
664  */
665 function sieve_create_strings($data,$force_string = FALSE)
667   $ret = "";
668   if(is_array($data)){
669     if(count($data) == 1){
670       $ret = "\"";
671       foreach($data as $dat){
672         $ret .=$dat;
673       }
674       $ret.="\"";
675     }else{
676       foreach($data as $dat){
677         $ret.= "\"";
678         $ret.=$dat;
679         $ret.="\", ";
680       }
681       $ret = preg_replace("/,$/","",trim($ret));
682       $ret = "[".$ret."]";
683     }
684   }else{
686     $Multiline = preg_match("/\n/",$data);
687     $data = preg_replace("/\r/","",$data);;
689     if($Multiline && !$force_string){
690       $ret = "text: \r\n".$data."\r\n.\r\n";
691     }else{
692       $ret = "\"".$data."\"";
693     }
694   }
695   $ret = preg_replace("/\"\"/","\"",$ret);
696   $ret = preg_replace("/\n/","\r\n",$ret);
697   
698   return($ret);
701 /* This checks if there is a string at the current position 
702  *  in the token array. 
703  * If there is a string list at the current position,
704  *  this function will return a complete list of all
705  *  strings used in this list.
706  * It also returns an offset of the last token position 
707  */
708 function sieve_get_strings($data,$id)
710   $ret = array();
711   if($data[$id]['class'] == "left-bracket"){
712     while(isset($data[$id]) && $data[$id]['class']  != "right-bracket" && $id < count($data)){
713       
714       if($data[$id]['class'] == "quoted-string"){
715         $ret[] = $data[$id]['text'];
716       }
718       $id ++;
719     }
720   }elseif($data[$id]['class'] == "quoted-string"){
721     $ret[] = $data[$id]['text'];
722   }elseif($data[$id]['class'] == "number"){
723     $ret[] = $data[$id]['text'];
724   }elseif($data[$id]['class'] == "multi-line"){
725     $str = trim(preg_replace("/^text:/","",$data[$id]['text']));
726     $str = trim(preg_replace("/\.$/","",$str));
727     $ret[] = $str;
728   }
729   
730   return(array("OFFSET" => $id, "STRINGS" => $ret));
733 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
734 ?>