d935a20aa8f1245225fc950eba7dddf21aa87e43
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($last_type != "if"){
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);
584 $tmp.= $buffer.$str."\n";
585 }
586 if(get_class($part) == "sieve_block_start"){
587 $buffer .= SIEVE_INDENT_TAB;
588 }
589 }
590 }
591 if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
592 # $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
593 }
594 return($tmp);
595 }
597 function check()
598 {
599 $msgs = array();
601 /* Some logical checks.
602 * like : only sieve_comment can appear before require.
603 */
605 /* Ensure that there are no command before require
606 * - Get id of last require tag
607 * - Collect object types in from of this tag.
608 * - Check if there are tags collected that are not allowed
609 */
610 $last_found_at = -1;
611 $objs = array();
612 foreach($this->pap as $key => $obj){
613 if(get_class($obj) == "sieve_require"){
614 $last_found_at = $key;
615 }
616 }
617 foreach($this->pap as $key => $obj){
618 if($key == $last_found_at) break;
619 if(!in_array(get_class($obj),array("sieve_comment","sieve_require"))){
620 $objs[] = get_class($obj);
621 }
622 }
623 if(count($objs) && $last_found_at != -1){
624 $str = _("Require must be the first command in the script.");
625 $msgs[] = $str;
626 print_red($str);;
627 }
629 foreach($this->pap as $obj){
630 $o_msgs = $obj->check();
631 foreach($o_msgs as $o_msg){
632 $msgs[] = $o_msg;
633 }
634 }
635 return($msgs);
636 }
639 /* We are forced to add a new require.
640 * This function is called by the
641 * sieveElement_Classes->parent->add_require()
642 */
643 function add_require($str)
644 {
645 $require_id = -1;
646 foreach($this->pap as $key => $obj){
647 if(get_class($obj) == "sieve_require"){
648 $require_id = $key;
649 }
650 }
652 /* No require found, add one */
653 if($require_id == -1){
654 $require = new sieve_require(NULL,preg_replace("/[^0-9]/","",microtime()),$this);
655 $require -> Add_Require($str);
656 $new = array();
657 $new[] = $require;
658 foreach($this->pap as $obj){
659 $new[] = $obj;
660 }
661 $this->pap = $new;
662 } else {
663 $this->pap[$require_id]->Add_Require($str);
664 }
665 }
666 }
669 /* Create valid sieve string/string-list
670 * out of a given array
671 */
672 function sieve_create_strings($data,$force_string = FALSE)
673 {
674 $ret = "";
675 if(is_array($data)){
676 if(count($data) == 1){
677 $ret = "\"";
678 foreach($data as $dat){
679 $ret .=$dat;
680 }
681 $ret.="\"";
682 }else{
683 foreach($data as $dat){
684 $ret.= "\"";
685 $ret.=$dat;
686 $ret.="\", ";
687 }
688 $ret = preg_replace("/,$/","",trim($ret));
689 $ret = "[".$ret."]";
690 }
691 }else{
693 $Multiline = preg_match("/\n/",$data);
694 $data = preg_replace("/\r/","",$data);;
696 if($Multiline && !$force_string){
697 $ret = "text: \r\n".$data."\r\n.\r\n";
698 }else{
699 $ret = "\"".$data."\"";
700 }
701 }
702 $ret = preg_replace("/\"\"/","\"",$ret);
703 $ret = preg_replace("/\n/","\r\n",$ret);
705 return($ret);
706 }
708 /* This checks if there is a string at the current position
709 * in the token array.
710 * If there is a string list at the current position,
711 * this function will return a complete list of all
712 * strings used in this list.
713 * It also returns an offset of the last token position
714 */
715 function sieve_get_strings($data,$id)
716 {
717 $ret = array();
718 if($data[$id]['class'] == "left-bracket"){
719 while(isset($data[$id]) && $data[$id]['class'] != "right-bracket" && $id < count($data)){
721 if($data[$id]['class'] == "quoted-string"){
722 $ret[] = $data[$id]['text'];
723 }
725 $id ++;
726 }
727 }elseif($data[$id]['class'] == "quoted-string"){
728 $ret[] = $data[$id]['text'];
729 }elseif($data[$id]['class'] == "number"){
730 $ret[] = $data[$id]['text'];
731 }elseif($data[$id]['class'] == "multi-line"){
732 $str = trim(preg_replace("/^text:/","",$data[$id]['text']));
733 $str = trim(preg_replace("/\.$/","",$str));
734 $ret[] = $str;
735 }
737 return(array("OFFSET" => $id, "STRINGS" => $ret));
738 }
740 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
741 ?>