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 $class = get_class($this->pap[$key_id]);
242 if(in_array($class,array("sieve_if","sieve_elsif","sieve_else"))){
243 $block_start= $key_id;
244 $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 {
270 /* Get the current element to decide what to move. */
271 $e_class = get_class($this->pap[$key_id]);
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_comment",
292 "sieve_vacation",
293 "sieve_stop",
294 "sieve_reject",
295 "sieve_fileinto",
296 "sieve_redirect",
297 "sieve_discard"))){
298 $this->move_single_element($key_id,$this->_get_next_free_move_slot($key_id,$direction));
299 }
300 }
303 /* Move the given block to position */
304 function move_multiple_elements($start,$end,$to)
305 {
306 /* Use class names for testing */
307 $data = $this->pap;
309 /* Get block to move */
310 $block_to_move = array_slice($data,$start, ($end - $start +1));
312 /* We want do move this block up */
313 if($end > $to){
315 /* Get start block */
316 $start_block = array_slice($data,0,$to);
318 /* Get Get all elements between the block to move
319 * and next free position
320 */
321 $block_to_free = array_slice($data,$to ,$start - $to );
322 $block_to_end = array_slice($data,$end+1);
323 $new = array();
324 foreach($start_block as $block){
325 $new[] = $block;
326 }
327 foreach($block_to_move as $block){
328 $new[] = $block;
329 }
330 foreach($block_to_free as $block){
331 $new[] = $block;
332 }
333 foreach($block_to_end as $block){
334 $new[] = $block;
335 }
336 $old = $this->pap;
337 $this->pap = $new;
338 }
341 /* We want to move this block down. */
342 if($to > $end){
344 /* Get start block */
345 $start_block = array_slice($data,0,$start);
347 /* Get Get all elements between the block to move
348 * and next free position
349 */
350 $block_to_free = array_slice($data,$end +1,($to - $end ));
352 /* Get the rest
353 */
354 $block_to_end = array_slice($data,$to+1);
356 $new = array();
357 foreach($start_block as $block){
358 $new[] = $block;
359 }
360 foreach($block_to_free as $block){
361 $new[] = $block;
362 }
363 foreach($block_to_move as $block){
364 $new[] = $block;
365 }
366 foreach($block_to_end as $block){
367 $new[] = $block;
368 }
369 $old = $this->pap;
370 $this->pap = $new;
371 }
372 }
375 /* This function returns the id of the element
376 * where the current block ends
377 */
378 function get_block_end($start)
379 {
380 /* Only execute if this is a really a block element.
381 * Block elements is only sieve_if
382 */
383 if(in_array(get_class($this->pap[$start]),array("sieve_if","sieve_elsif","sieve_else"))){
385 $class = get_class($this->pap[$start]);
386 $next_class = get_class($this->pap[$start+1]);
387 $block_depth = 0;
389 $end = FALSE;
391 while(!$end && $start < count($this->pap)){
393 if($class == "sieve_block_start"){
394 $block_depth ++;
395 }
397 if($class == "sieve_block_end"){
398 $block_depth --;
399 }
401 if( $block_depth == 0 &&
402 $class == "sieve_block_end" &&
403 !in_array($next_class,array("sieve_else","sieve_elsif"))){
404 $end = TRUE;
405 $start --;
406 }
407 $start ++;
408 $class = get_class($this->pap[$start]);
410 if(isset($this->pap[$start+1])){
411 $next_class = get_class($this->pap[$start+1]);
412 }else{
413 $next_class ="";
414 }
415 }
416 }
417 return($start);
418 }
421 /* This function moves the single element at
422 * position $from to position $to.
423 */
424 function move_single_element($from,$to)
425 {
426 if($from == $to) {
427 return;
428 }
430 $ret = array();
431 $tmp = $this->pap;
433 $begin = array();
434 $middle = array();
435 $end = array();
436 $element = $this->pap[$from];
438 if($from > $to ){
440 /* Get all element in fron to element to move */
441 if($from != 0){
442 $begin = array_slice($tmp,0,$to);
443 }
445 /* Get all elements between */
446 $middle = array_slice($tmp,$to , ($from - ($to) ));
448 /* Get the rest */
449 $end = array_slice($tmp,$from+1);
451 foreach($begin as $data){
452 $ret[] = $data;
453 }
454 $ret[] = $element;
455 foreach($middle as $data){
456 $ret[] = $data;
457 }
458 foreach($end as $data){
459 $ret[] = $data;
460 }
461 $this->pap = $ret;
462 }
463 if($from < $to ){
465 /* Get all element in fron to element to move */
466 if($from != 0){
467 $begin = array_slice($tmp,0,$from);
468 }
470 /* Get all elements between */
471 $middle = array_slice($tmp,$from+1 , ($to - ($from)));
473 /* Get the rest */
474 $end = array_slice($tmp,$to+1);
476 foreach($begin as $data){
477 $ret[] = $data;
478 }
479 foreach($middle as $data){
480 $ret[] = $data;
481 }
482 $ret[] = $element;
483 foreach($end as $data){
484 $ret[] = $data;
485 }
486 $this->pap = $ret;
487 }
488 }
491 /* Returns the next free position where we
492 * can add a new sinle element
493 * $key_id = Current position
494 * $direction = Forward or backward.
495 */
496 function _get_next_free_move_slot($key_id,$direction,$include_self = FALSE)
497 {
498 $last_class = "";
499 $current_class ="";
500 $next_class = "";
502 /* After this elements we can add new elements
503 * without having any trouble.
504 */
505 $allowed_to_add_after = array("sieve_keep",
506 "sieve_require",
507 "sieve_stop",
508 "sieve_reject",
509 "sieve_fileinto",
510 "sieve_redirect",
511 "sieve_discard",
512 "sieve_comment",
513 "sieve_block_start"
514 );
516 /* Before this elements we can add new elements
517 * without having any trouble.
518 */
519 $allowed_to_add_before = array("sieve_keep",
520 "sieve_require",
521 "sieve_stop",
522 "sieve_reject",
523 "sieve_fileinto",
524 "sieve_comment",
525 "sieve_redirect",
526 "sieve_discard",
527 "sieve_if",
528 "sieve_block_end"
529 );
531 if($direction == "down"){
533 $test = $this->pap;
534 while($key_id < count($test)){
535 if(($key_id+1) == count($test)) {
536 return($key_id);
537 }
539 if(!$include_self){
540 $key_id ++;
541 }
542 $include_self = FALSE;
543 $current_class = get_class($test[$key_id]);
544 if(in_array($current_class, $allowed_to_add_after)){
545 return($key_id);
546 }
547 }
548 }else{
550 $test = $this->pap;
551 if($key_id == 0) {
552 return($key_id);
553 }
554 if(!$include_self){
555 $key_id --;
556 }
557 while($key_id >=0 ){
558 $current_class = get_class($test[$key_id]);
559 if(in_array($current_class, $allowed_to_add_before)){
560 return($key_id);
561 }
562 $key_id --;
563 }
564 return(0);
565 }
566 }
569 /* Need to be reviewed */
570 function get_sieve_script()
571 {
572 $tmp ="";
573 if(count($this->pap)){
574 $buffer = "";
575 foreach($this->pap as $part) {
576 if(get_class($part) == "sieve_block_end"){
577 $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
578 }
579 $tmp2 = $part->get_sieve_script_part();
581 $tmp3 = split("\n",$tmp2);
582 foreach($tmp3 as $str){
583 $str2 = trim($str);
585 /* If the current line only contains an '.'
586 * we must skip the line indent.
587 * The text: statement uses a single '.' to mark the text end.
588 * This '.' must be the only char in the current line, no
589 * whitespaces are allowed here.
590 */
591 if($str2 == "."){
592 $tmp.=$str."\n";
593 }else{
594 $tmp.= $buffer.$str."\n";
595 }
596 }
597 if(get_class($part) == "sieve_block_start"){
598 $buffer .= SIEVE_INDENT_TAB;
599 }
600 }
601 }
602 if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
603 # $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
604 }
605 return($tmp);
606 }
608 function check()
609 {
610 $msgs = array();
612 /* Some logical checks.
613 * like : only sieve_comment can appear before require.
614 */
616 /* Ensure that there are no command before require
617 * - Get id of last require tag
618 * - Collect object types in from of this tag.
619 * - Check if there are tags collected that are not allowed
620 */
621 $last_found_at = -1;
622 $objs = array();
623 foreach($this->pap as $key => $obj){
624 if(get_class($obj) == "sieve_require"){
625 $last_found_at = $key;
626 }
627 }
628 foreach($this->pap as $key => $obj){
629 if($key == $last_found_at) break;
630 if(!in_array(get_class($obj),array("sieve_comment","sieve_require"))){
631 $objs[] = get_class($obj);
632 }
633 }
634 if(count($objs) && $last_found_at != -1){
635 $str = _("Require must be the first command in the script.");
636 $msgs[] = $str;
637 print_red($str);;
638 }
640 foreach($this->pap as $obj){
641 $o_msgs = $obj->check();
642 foreach($o_msgs as $o_msg){
643 $msgs[] = $o_msg;
644 }
645 }
646 return($msgs);
647 }
650 /* We are forced to add a new require.
651 * This function is called by the
652 * sieveElement_Classes->parent->add_require()
653 */
654 function add_require($str)
655 {
656 $require_id = -1;
657 foreach($this->pap as $key => $obj){
658 if(get_class($obj) == "sieve_require"){
659 $require_id = $key;
660 }
661 }
663 /* No require found, add one */
664 if($require_id == -1){
665 $require = new sieve_require(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
666 $require -> Add_Require($str);
667 $new = array();
668 $new[] = $require;
669 foreach($this->pap as $obj){
670 $new[] = $obj;
671 }
672 $this->pap = $new;
673 } else {
674 $this->pap[$require_id]->Add_Require($str);
675 }
676 }
677 }
680 /* Create valid sieve string/string-list
681 * out of a given array
682 */
683 function sieve_create_strings($data,$force_string = FALSE)
684 {
685 $ret = "";
686 if(is_array($data)){
687 if(count($data) == 1){
688 $ret = "\"";
689 foreach($data as $dat){
690 $ret .=$dat;
691 }
692 $ret.="\"";
693 }else{
694 foreach($data as $dat){
695 $ret.= "\"";
696 $ret.=$dat;
697 $ret.="\", ";
698 }
699 $ret = preg_replace("/,$/","",trim($ret));
700 $ret = "[".$ret."]";
701 }
702 }else{
704 $Multiline = preg_match("/\n/",$data);
705 $data = preg_replace("/\r/","",$data);;
707 if($Multiline && !$force_string){
708 $ret = "text: \r\n".$data."\r\n.\r\n";
709 }else{
710 $ret = "\"".$data."\"";
711 }
712 }
713 $ret = preg_replace("/\"\"/","\"",$ret);
714 $ret = preg_replace("/\n/","\r\n",$ret);
716 return($ret);
717 }
719 /* This checks if there is a string at the current position
720 * in the token array.
721 * If there is a string list at the current position,
722 * this function will return a complete list of all
723 * strings used in this list.
724 * It also returns an offset of the last token position
725 */
726 function sieve_get_strings($data,$id)
727 {
728 $ret = array();
729 if($data[$id]['class'] == "left-bracket"){
730 while(isset($data[$id]) && $data[$id]['class'] != "right-bracket" && $id < count($data)){
732 if($data[$id]['class'] == "quoted-string"){
733 $ret[] = $data[$id]['text'];
734 }
736 $id ++;
737 }
738 }elseif($data[$id]['class'] == "quoted-string"){
739 $ret[] = $data[$id]['text'];
740 }elseif($data[$id]['class'] == "number"){
741 $ret[] = $data[$id]['text'];
742 }elseif($data[$id]['class'] == "multi-line"){
743 $str = trim(preg_replace("/^text:/","",$data[$id]['text']));
744 $str = trim(preg_replace("/\.$/","",$str));
745 $ret[] = $str;
746 }
748 return(array("OFFSET" => $id, "STRINGS" => $ret));
749 }
751 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
752 ?>