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__)));
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);
71 if($end != $key && get_class($object) == "sieve_if") {
72 $ends[$end] = $end;
73 $this->dump_ .= $block_indent_start;
74 }
75 $this->dump_ .= preg_replace("/>/",">\n",$object->execute());
76 if(isset($ends[$key])){
77 $this->dump_ .= $block_indent_stop;
78 }
79 }
80 }
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'];
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'];
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 {
275 /* Get the current element to decide what to move. */
276 $e_class = get_class($this->pap[$key_id]);
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 }
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){
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 }
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 }
380 /* This function returns the id of the element
381 * where the current block ends
382 */
383 function get_block_end($start)
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)){
398 if($class == "sieve_block_start"){
399 $block_depth ++;
400 }
402 if($class == "sieve_block_end"){
403 $block_depth --;
404 }
406 if( $block_depth == 0 &&
407 $class == "sieve_block_end" &&
408 !in_array($next_class,array("sieve_else","sieve_elsif"))){
409 $end = TRUE;
410 $start --;
411 }
412 $start ++;
413 $class = get_class($this->pap[$start]);
415 if(isset($this->pap[$start+1])){
416 $next_class = get_class($this->pap[$start+1]);
417 }else{
418 $next_class ="";
419 }
420 }
421 }
422 return($start);
423 }
426 /* This function moves the single element at
427 * position $from to position $to.
428 */
429 function move_single_element($from,$to)
430 {
431 if($from == $to) {
432 return;
433 }
435 $ret = array();
436 $tmp = $this->pap;
438 $begin = array();
439 $middle = array();
440 $end = array();
441 $element = $this->pap[$from];
443 if($from > $to ){
445 /* Get all element in fron to element to move */
446 if($from != 0){
447 $begin = array_slice($tmp,0,$to);
448 }
450 /* Get all elements between */
451 $middle = array_slice($tmp,$to , ($from - ($to) ));
453 /* Get the rest */
454 $end = array_slice($tmp,$from+1);
456 foreach($begin as $data){
457 $ret[] = $data;
458 }
459 $ret[] = $element;
460 foreach($middle as $data){
461 $ret[] = $data;
462 }
463 foreach($end as $data){
464 $ret[] = $data;
465 }
466 $this->pap = $ret;
467 }
468 if($from < $to ){
470 /* Get all element in fron to element to move */
471 if($from != 0){
472 $begin = array_slice($tmp,0,$from);
473 }
475 /* Get all elements between */
476 $middle = array_slice($tmp,$from+1 , ($to - ($from)));
478 /* Get the rest */
479 $end = array_slice($tmp,$to+1);
481 foreach($begin as $data){
482 $ret[] = $data;
483 }
484 foreach($middle as $data){
485 $ret[] = $data;
486 }
487 $ret[] = $element;
488 foreach($end as $data){
489 $ret[] = $data;
490 }
491 $this->pap = $ret;
492 }
493 }
496 /* Returns the next free position where we
497 * can add a new sinle element
498 * $key_id = Current position
499 * $direction = Forward or backward.
500 */
501 function _get_next_free_move_slot($key_id,$direction,$include_self = FALSE)
502 {
503 $last_class = "";
504 $current_class ="";
505 $next_class = "";
507 /* After this elements we can add new elements
508 * without having any trouble.
509 */
510 $allowed_to_add_after = array("sieve_keep",
511 "sieve_require",
512 "sieve_stop",
513 "sieve_reject",
514 "sieve_fileinto",
515 "sieve_redirect",
516 "sieve_discard",
517 "sieve_comment",
518 "sieve_block_start"
519 );
521 /* Before this elements we can add new elements
522 * without having any trouble.
523 */
524 $allowed_to_add_before = array("sieve_keep",
525 "sieve_require",
526 "sieve_stop",
527 "sieve_reject",
528 "sieve_fileinto",
529 "sieve_comment",
530 "sieve_redirect",
531 "sieve_discard",
532 "sieve_if",
533 "sieve_block_end"
534 );
536 if($direction == "down"){
538 $test = $this->pap;
539 while($key_id < count($test)){
540 if(($key_id+1) == count($test)) {
541 return($key_id);
542 }
544 if(!$include_self){
545 $key_id ++;
546 }
547 $include_self = FALSE;
548 $current_class = get_class($test[$key_id]);
549 if(in_array($current_class, $allowed_to_add_after)){
550 return($key_id);
551 }
552 }
553 }else{
555 $test = $this->pap;
556 if($key_id == 0) {
557 return($key_id);
558 }
559 if(!$include_self){
560 $key_id --;
561 }
562 while($key_id >=0 ){
563 $current_class = get_class($test[$key_id]);
564 if(in_array($current_class, $allowed_to_add_before)){
565 return($key_id);
566 }
567 $key_id --;
568 }
569 return(0);
570 }
571 }
574 /* Need to be reviewed */
575 function get_sieve_script()
576 {
577 $tmp ="";
578 if(count($this->pap)){
579 $buffer = "";
580 foreach($this->pap as $part) {
581 if(get_class($part) == "sieve_block_end"){
582 $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
583 }
584 $tmp2 = $part->get_sieve_script_part();
586 $tmp3 = split("\n",$tmp2);
587 foreach($tmp3 as $str){
588 $str2 = trim($str);
590 /* If the current line only contains an '.'
591 * we must skip the line indent.
592 * The text: statement uses a single '.' to mark the text end.
593 * This '.' must be the only char in the current line, no
594 * whitespaces are allowed here.
595 */
596 if($str2 == "."){
597 $tmp.=$str."\n";
598 }else{
599 $tmp.= $buffer.$str."\n";
600 }
601 }
602 if(get_class($part) == "sieve_block_start"){
603 $buffer .= SIEVE_INDENT_TAB;
604 }
605 }
606 }
607 if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
608 # $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
609 }
610 return($tmp);
611 }
613 function check()
614 {
615 $msgs = array();
617 /* Some logical checks.
618 * like : only sieve_comment can appear before require.
619 */
621 /* Ensure that there are no command before require
622 * - Get id of last require tag
623 * - Collect object types in from of this tag.
624 * - Check if there are tags collected that are not allowed
625 */
626 $last_found_at = -1;
627 $objs = array();
628 foreach($this->pap as $key => $obj){
629 if(get_class($obj) == "sieve_require"){
630 $last_found_at = $key;
631 }
632 }
633 foreach($this->pap as $key => $obj){
634 if($key == $last_found_at) break;
635 if(!in_array(get_class($obj),array("sieve_comment","sieve_require"))){
636 $objs[] = get_class($obj);
637 }
638 }
639 if(count($objs) && $last_found_at != -1){
640 $str = _("Require must be the first command in the script.");
641 $msgs[] = $str;
642 print_red($str);;
643 }
645 foreach($this->pap as $obj){
646 $o_msgs = $obj->check();
647 foreach($o_msgs as $o_msg){
648 $msgs[] = $o_msg;
649 }
650 }
651 return($msgs);
652 }
655 /* We are forced to add a new require.
656 * This function is called by the
657 * sieveElement_Classes->parent->add_require()
658 */
659 function add_require($str)
660 {
661 $require_id = -1;
662 foreach($this->pap as $key => $obj){
663 if(get_class($obj) == "sieve_require"){
664 $require_id = $key;
665 }
666 }
668 /* No require found, add one */
669 if($require_id == -1){
670 $require = new sieve_require(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
671 $require -> Add_Require($str);
672 $new = array();
673 $new[] = $require;
674 foreach($this->pap as $obj){
675 $new[] = $obj;
676 }
677 $this->pap = $new;
678 } else {
679 $this->pap[$require_id]->Add_Require($str);
680 }
681 }
682 }
685 /* Create valid sieve string/string-list
686 * out of a given array
687 */
688 function sieve_create_strings($data,$force_string = FALSE)
689 {
690 $ret = "";
691 if(is_array($data)){
692 if(count($data) == 1){
693 $ret = "\"";
694 foreach($data as $dat){
695 $ret .=$dat;
696 }
697 $ret.="\"";
698 }else{
699 foreach($data as $dat){
700 $ret.= "\"";
701 $ret.=$dat;
702 $ret.="\", ";
703 }
704 $ret = preg_replace("/,$/","",trim($ret));
705 $ret = "[".$ret."]";
706 }
707 }else{
709 $Multiline = preg_match("/\n/",$data);
710 $data = preg_replace("/\r/","",$data);;
712 if($Multiline && !$force_string){
713 $ret = "text: \r\n".$data."\r\n.\r\n";
714 }else{
715 $ret = "\"".$data."\"";
716 }
717 }
718 $ret = preg_replace("/\"\"/","\"",$ret);
719 $ret = preg_replace("/\n/","\r\n",$ret);
721 return($ret);
722 }
724 /* This checks if there is a string at the current position
725 * in the token array.
726 * If there is a string list at the current position,
727 * this function will return a complete list of all
728 * strings used in this list.
729 * It also returns an offset of the last token position
730 */
731 function sieve_get_strings($data,$id)
732 {
733 $ret = array();
734 if($data[$id]['class'] == "left-bracket"){
735 while(isset($data[$id]) && $data[$id]['class'] != "right-bracket" && $id < count($data)){
737 if($data[$id]['class'] == "quoted-string"){
738 $ret[] = $data[$id]['text'];
739 }
741 $id ++;
742 }
743 }elseif($data[$id]['class'] == "quoted-string"){
744 $ret[] = $data[$id]['text'];
745 }elseif($data[$id]['class'] == "number"){
746 $ret[] = $data[$id]['text'];
747 }elseif($data[$id]['class'] == "multi-line"){
748 $str = trim(preg_replace("/^text:/","",$data[$id]['text']));
749 $str = trim(preg_replace("/\.$/","",$str));
750 $ret[] = $str;
751 }
753 return(array("OFFSET" => $id, "STRINGS" => $ret));
754 }
756 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
757 ?>