Code

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