Code

Some changes
[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();
19   var $add_new          = FALSE;
20   var $add_new_id       = 0;
21   var $add_type         = "up";
22   var $add_element_type = "";
24   /* Create a html interface for the current sieve filter 
25    */
26   function dump()
27   {
28     error_reporting(E_ALL);
29       $element_types= array(
30           "sieve_keep"      => _("Keep"),
31           "sieve_comment"   => _("Comment"),
32           "sieve_fileinto"  => _("File into"),
33           "sieve_keep"      => _("Keep"),
34           "sieve_redirect"  => _("Redirect"),
35           "sieve_reject"    => _("Reject"),
36           "sieve_require"   => _("Require"),
37           "sieve_stop"      => _("Stop"),
38           "sieve_vacation"  => _("Vacation message"),
39           "sieve_if"        => _("If"));
42     if($this->add_new && isset($_POST['element_type']) && isset($element_types[$_POST['element_type']])){
43       $this->add_element_type = $_POST['element_type'];
44     }
47     if($this->add_new && isset($_POST['select_new_element_type'])){
48      
49       $ele = new $this->add_element_type(array(),microtime());
50       
51       $this->pap[] = $ele; 
52       $this->add_new = FALSE;
53     }
55     if($this->add_new){
57       $smarty = get_smarty();
58       $smarty->assign("element_types",$element_types );
59       $smarty->assign("element_type",$this->add_element_type);
61       $str = $smarty->fetch(get_template_path("templates/add_element.tpl",TRUE,dirname(__FILE__)));
62       return($str);
63     }
65     /* Only parse the tokens once */
66     if(!count($this->pap)){
67       $this->dump_ = "";
68       $this->mode_stack = array();
69       $this->pap = array();
70       $this->doDump_(0, '', true);
71     }
73     /* Create html results */
74     $smarty = get_smarty();
76     $this -> dump_ = "";
77     foreach($this->pap as $key => $object){
78       if(is_object($object)){
79         $this->dump_ .= preg_replace("/>/",">\n",$object->execute()); 
80       }
81     }
82     
83     /* Create html results */
84     $smarty = get_smarty();
85     $smarty->assign("Contents",$this->dump_);
86     $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
87     return ($ret);
88   }
91   /* This function walks through the object tree generated by the "Parse" class.
92    * All Commands will be resolved and grouped. So the Commands and their 
93    *  parameter are combined. Like "IF" and ":comparator"...
94    */  
95   function doDump_($node_id, $prefix, $last,$num = 1)
96   {
97     /* Indicates that current comman will only be valid for a single line. 
98      *  this command type will be removed from mode_stack after displaying it.
99      */
100     $rewoke_last = FALSE;
102     /* Get node */ 
103     $node = $this->nodes_[$node_id];
105     /* This closes the last mode */
106     if($node['class'] == "block-start"){
107       $tmp = array_pop($this->mode_stack);
108       $this->handle_elements($tmp,$node_id);
109       $this->handle_elements(array("TYPE" => "block_start"),$node_id);
110     }
112     /* This closes the last mode */
113     if($node['class'] == "block-end"){
114       $tmp = array_pop($this->mode_stack);
115       $this->handle_elements($tmp,$node_id);
116       $this->handle_elements(array("TYPE" => "block_end"),$node_id);
117     }
119     /* Semicolon indicates a new command */
120     if($node['class'] == "semicolon"){
121       $tmp =array_pop($this->mode_stack);
122       $this->handle_elements($tmp,$node_id);
123     }
125     /* Handle comments */
126     if($node['class'] == "comment"){
127       $this->mode_stack[] = array("TYPE" => $node['class']);
128       $rewoke_last = TRUE;
129     }
131     /* Handle identifiers */
132     $identifiers = array("else","if","elsif","end","reject","redirect","vacation","keep","discard","comment","fileinto","require","stop");
133     if($node['class'] == "identifier" && in_array($node['text'],$identifiers)){
134       $this->mode_stack[] = array("TYPE" => $node['text']); 
135     }
137     /* Add current node to current command stack */
138     end($this->mode_stack);
139     $key = key($this->mode_stack);
140     $this->mode_stack[$key]['ELEMENTS'][] = $node;
142     /* Remove last mode from mode stack, cause it was only valid for a single line */
143     if($rewoke_last){
144       $tmp =array_pop($this->mode_stack);
145       $this->handle_elements($tmp,$node_id);
146     }
148     /* If this is a sub element, just call this for all childs */       
149     if(isset($this->childs_[$node_id])){
150       $childs = $this->childs_[$node_id];
151       for ($i=0; $i<count($childs); ++$i)
152       {
153         $c_last = false;
154         if ($i+1 == count($childs))
155         {
156           $c_last = true;
157         }
158         $this->doDump_($childs[$i], "", $num);
159       }
160     }
161   }
164   /* Create a class for each resolved object.
165    * And append this class to a list of objects.
166    */
167   function handle_elements($data,$id)
168   {
169     if(!isset($data['TYPE'])){
170       return;
171     }
172     $type = $data['TYPE'];
173     
174     $class_name= "sieve_".$type ;
175     if(class_exists($class_name)){
176       $this->pap[] = new $class_name($data,$id);
177     }else{
178       echo "<font color='red'>Missing : ".$class_name."</font>"."<br>";
179     }
180   }
182   function save_object()
183   {
184     reset($this->pap);
185     foreach($this->pap as $key => $obj){
187       if(in_array(get_class($obj),array("sieve_if",
188                                         "sieve_elsif",
189                                         "sieve_vacation",
190                                         "sieve_comment",
191                                         "sieve_reject",
192                                         "sieve_fileinto",
193                                         "sieve_require",
194                                         "sieve_redirect"))){
197         if(isset($this->pap[$key]) && method_exists($this->pap[$key],"save_object")){
198           $this->pap[$key]->save_object();
199         }
200       }
202       $once = TRUE;
203       foreach($_POST as $name => $value){
205         if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
206           $once = FALSE;
207           $this->remove_object($key);
208         }
209         if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
210           $this->move_up_down($key,"up");
211           $once = FALSE;
212         }
213         if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
214           $this->move_up_down($key,"down");
215           $once = FALSE;
216         }
217         if(isset($obj->object_id) && preg_match("/^Add_Object_Top_".$obj->object_id."_/",$name) && $once){
218           $this->add_new_object($obj->object_id,"top");
219           $once = FALSE;
220         }
221         if(isset($obj->object_id) && preg_match("/^Add_Object_Bottom_".$obj->object_id."_/",$name) && $once){
222           $this->add_new_object($obj->object_id,"bottom");
223           $once = FALSE;
224         }
225       }
226     }
227   }
230   /* Add a new object at the given position */
231   function add_new_object($id,$top_bottom = "bottom")
232   {
233     $this->add_new    = TRUE;
234     $this->add_new_id = $id;
235     $this->add_type   = $top_bottom;
236   }
239   /* Remove the object at the given position */
240   function remove_object($key_id)
241   {
242     $class = get_class($this->pap[$key_id]);
243     if(in_array($class,array("sieve_if"))){
244       $block_start= $key_id;
245       $block_end  = $this->get_block_end($key_id);
246       for($i = $block_start ; $i <= $block_end ; $i ++ ){
247         unset($this->pap[$i]);
248       }
249     }else{
250       unset($this->pap[$key_id]);
251     }
252     $tmp = array();
253     foreach($this->pap as $element){
254       $tmp[] = $element;
255     }
256     $this->pap = $tmp;
257   }
260   /* This function moves a given element to another position.
261    * Single elements like "keep;" will simply be moved one posisition down/up.
262    * Multiple elements like if-elsif-else will be moved as block. 
263    * 
264    *  $key_id     specified the element that should be moved.
265    *  $direction  specifies to move elements "up" or "down"
266    */
267   function move_up_down($key_id,$direction = "down")
268   {
269      
270     /* Get the current element to decide what to move. */ 
271     $e_class = get_class($this->pap[$key_id]);
272       
273     if(in_array($e_class,array("sieve_if"))){
274       $block_start= $key_id;
275       $block_end  = $this->get_block_end($key_id);
277       /* Depending on the direction move up down */
278       if($direction == "down"){
279         $next_free  = $this->_get_next_free_move_slot($block_end,$direction); 
280       }else{
281         $next_free  = $this->_get_next_free_move_slot($block_start,$direction); 
282       }
284       /* Move the given block */ 
285       $this->move_multiple_elements($block_start,$block_end,$next_free);
286     }
288     if(in_array($e_class,array( "sieve_stop",
289                                 "sieve_keep",
290                                 "sieve_require", 
291                                 "sieve_stop",   
292                                 "sieve_reject", 
293                                 "sieve_fileinto",
294                                 "sieve_redirect", 
295                                 "sieve_discard"))){
296       $this->move_single_element($key_id,$this->_get_next_free_move_slot($key_id,$direction));
297     }
298   }
300   
301   /* Move the given block to position */
302   function move_multiple_elements($start,$end,$to)
303   {
304     /* Use class names for testing */
305     $data = $this->pap;
307     /* Get block to move */
308     $block_to_move = array_slice($data,$start, ($end - $start +1));
310     /* We want do move this block up */
311     if($end > $to){
312       
313       /* Get start block */
314       $start_block = array_slice($data,0,$to);
316       /* Get Get all elements between the block to move 
317        *  and next free position 
318        */
319       $block_to_free = array_slice($data,$to ,$start - $to );  
320       $block_to_end = array_slice($data,$end+1);
321       $new = array();
322       foreach($start_block as $block){
323         $new[] = $block;
324       }
325       foreach($block_to_move as $block){
326         $new[] = $block;
327       }
328       foreach($block_to_free as $block){
329         $new[] = $block;
330       }
331       foreach($block_to_end as $block){
332         $new[] = $block;
333       }
334       $old = $this->pap;
335       $this->pap = $new;
336     }
337     
339     /* We want to move this block down. */
340     if($to > $end){
342       /* Get start block */
343       $start_block = array_slice($data,0,$start);
345       /* Get Get all elements between the block to move 
346        *  and next free position 
347        */
348       $block_to_free = array_slice($data,$end +1,($to - $end  ));  
350       /* Get the rest 
351        */
352       $block_to_end = array_slice($data,$to+1);
354       $new = array();
355       foreach($start_block as $block){
356         $new[] = $block;
357       }
358       foreach($block_to_free as $block){
359         $new[] = $block;
360       }
361       foreach($block_to_move as $block){
362         $new[] = $block;
363       }
364       foreach($block_to_end as $block){
365         $new[] = $block;
366       }
367       $old = $this->pap;
368       $this->pap = $new;
369     }
370   }  
372   
373   /* This function returns the id of the element 
374    *  where the current block ends  
375    */
376   function get_block_end($start)
377   {
378     /* Only execute if this is a really a block element. 
379      * Block elements is only sieve_if
380      */
381     if(in_array(get_class($this->pap[$start]),array("sieve_if"))){
383       $class      = get_class($this->pap[$start]);
384       $next_class = get_class($this->pap[$start+1]);
385       $block_depth = 0;
387       $end = FALSE;
389       while(!$end && $start < count($this->pap)){
390  
391         if($class == "sieve_block_start"){
392           $block_depth ++;
393         }
395         if($class == "sieve_block_end"){
396           $block_depth --;
397         }
399         if( $block_depth == 0 && 
400             $class == "sieve_block_end" && 
401             !in_array($next_class,array("sieve_else","sieve_elsif"))){
402           $end = TRUE;
403           $start --;
404         }
405         $start ++;       
406         $class      = get_class($this->pap[$start]);
407         $next_class = get_class($this->pap[$start+1]);
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)
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         }
531         $key_id ++;
532         $current_class  = get_class($test[$key_id]);
533         if(in_array($current_class, $allowed_to_add_after)){
534           return($key_id);
535         } 
536       } 
537     }else{
538   
539       $test = $this->pap;
540       if($key_id == 0) {
541         return($key_id);
542       }
543       $key_id --;
544       while($key_id >=0 ){
545         $current_class  = get_class($test[$key_id]);
546         if(in_array($current_class, $allowed_to_add_before)){
547           return($key_id);
548         } 
549         $key_id --;
550       }
551       return(0);
552     }
553   }
556   /* Need to be reviewed */
557   function get_sieve_script()
558   {
559     $tmp ="";
560     if(count($this->pap)){
561       $buffer = "";    
562       foreach($this->pap as $part)  {
563         if(get_class($part) == "sieve_block_end"){
564           $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
565         }
566         $tmp2 = $part->get_sieve_script_part();
568         if(get_class($part) == "sieve_reject"){
569           $tmp.=$tmp2;
570         }else{
572           $tmp3 = split("\n",$tmp2);
573           foreach($tmp3 as $str){
574             $str2 = trim($str);
575             if(empty($str2)) continue;
576             $tmp.= $buffer.$str."\n";
577           }
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 Add_Element()
591   {
592     $tmp = array("ELEMENTS" => array(array("class" => "qouted-string","text"=> "Bla bla, later more")));
593     $this->pap[] = new sieve_comment($tmp,rand(1000,100000));
594   }
596   function check()
597   {
598                 $msgs = array();
599                 foreach($this->pap as $obj){
601                         $o_msgs = $obj->check();
602                         foreach($o_msgs as $o_msg){
603                                 $msgs[] = $o_msg;
604                         }
605                 }
606                 return($msgs);
607   }
611 /* Create valid sieve string/string-list 
612  *  out of a given array
613  */
614 function sieve_create_strings($data)
616   $ret = "";
617   if(is_array($data)){
618     if(count($data) == 1){
619       $ret = "\"";
620       foreach($data as $dat){
621         $ret .=$dat;
622       }
623       $ret.="\"";
624     }else{
625       foreach($data as $dat){
626         $ret.= "\"";
627         $ret.=$dat;
628         $ret.="\", ";
629       }
630       $ret = preg_replace("/,$/","",trim($ret));
631       $ret = "[".$ret."]";
632     }
633   }else{
635     $Multiline = preg_match("/\n/",$data);
636     $data = preg_replace("/\r/","",$data);;
638     if($Multiline){
639       $ret = "text: \r\n".$data."\r\n.\r\n";
640     }else{
641       $ret = "\"".$data."\"";
642     }
643   }
644   $ret = preg_replace("/\"\"/","\"",$ret);
645   $ret = preg_replace("/\n/","\r\n",$ret);
646   
647   return($ret);
650 /* This checks if there is a string at the current position 
651  *  in the token array. 
652  * If there is a string list at the current position,
653  *  this function will return a complete list of all
654  *  strings used in this list.
655  * It also returns an offset of the last token position 
656  */
657 function sieve_get_strings($data,$id)
659   $ret = array();
660   if($data[$id]['class'] == "left-bracket"){
661     while($data[$id]['class']  != "right-bracket" && $id < count($data)){
662       
663       if($data[$id]['class'] == "quoted-string"){
664         $ret[] = $data[$id]['text'];
665       }
666       $id ++;
667     }
668   }elseif($data[$id]['class'] == "quoted-string"){
669     $ret[] = $data[$id]['text'];
670   }elseif($data[$id]['class'] == "number"){
671     $ret[] = $data[$id]['text'];
672   }
673   return(array("OFFSET" => $id, "STRINGS" => $ret));
676 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
677 ?>