1 <?php
4 class sieve_if
5 {
6 var $_parsed = array();
7 var $TYPE = "if";
8 var $id = -1;
10 var $address_parts = array();
11 var $comparators = array();
12 var $match_types = array();
13 var $operators = array();
16 /* Initialize class
17 * $elements contains all tokens that belongs to this if/else tag
18 * $object_id cotains an unique tag id, to be able to create uniqe html post names
19 */
20 function sieve_if($elements,$object_id)
21 {
22 /* Possible address parts we can select */
23 $this->address_parts = array(
24 ":all" => _("Complete adress")." ("._("Default").")",
25 ":domain" => _("Domian part") ,
26 ":localpart" => _("Local part"));
28 /* comparator type */
29 $this->comparators = array(
30 "i;ascii-casemap" => _("Case insensitive")." ("._("Default").")",
31 "i;octet" => _("Case sensitive"),
32 "i;ascii-numeric" => _("Numeric"));
34 /* Match types */
35 $this->match_types = array(
36 ":is" => _("is"),
37 ":contains" => _("contains"),
38 ":matches" => _("matches"),
39 ":count" => _("count"),
40 ":value" => _("value is"));
42 /* Operators */
43 $this->operators = array(
44 "lt" => _("less than"),
45 "le" => _("less or equal"),
46 "eq" => _("equals"),
47 "ge" => _("greater or equal"),
48 "gt" => _("greater than"),
49 "ne" => _("not equal"));
51 $this->id = $object_id;
52 $this->elements = $elements;
53 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
54 }
57 /* Returns the sieve script for this
58 * if/else tag.
59 */
60 function get_sieve_script_part()
61 {
62 $tmp = "if ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
63 return($tmp);
64 }
67 /* Recursivly create a sieve script out of the given
68 * tags and tokens provided by $parsed.
69 * $id specifies the depth of the current element.
70 * $obj_id is the current tag-id handled by this function
71 */
72 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
73 {
74 $script ="";
75 if($parsed == NULL){
76 $parsed = $this->_parsed;
77 }
79 /* Walk through all elements */
80 foreach($parsed as $key => $data){
82 /* Create Inverse Tag */
83 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
84 $Inverse = TRUE;
85 }else{
86 $Inverse = FALSE;
87 }
89 /* Create elements */
90 switch($key)
91 {
93 /*******************
94 * True / False
95 *******************/
97 case "true" :
98 case "false" :
99 {
100 /* Invert this test if required */
101 if($Inverse){
102 $script .= "not ";
103 }
105 $script .= $key;
107 break;
108 }
111 /*******************
112 * Address
113 *******************/
115 case "address" :
116 {
117 /* [not] address
118 [address-part: tag]
119 [comparator: tag]
120 [match-type: tag]
121 <header-list: string-list>
122 <key-list: string-list>
123 */
125 /* Invert this test if required */
126 if($Inverse){
127 $script .= "not ";
128 }
130 $script .="address ";
132 /* Add address part tag */
133 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
134 $script .= $data['Address_Part']." ";
135 }
137 /* Add comparator */
138 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
139 $script .= ":comparator ".$data['Comparator']." ";
140 }
142 /* Add match type */
143 $script .= $data['Match_type']." ";
145 /* Add special match type for count and value */
146 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
147 $script .= sieve_create_strings($data['Match_type_value'])." ";
148 }
150 $script .= sieve_create_strings($data['Key_List']);
151 $script .= " ";
152 $script .= sieve_create_strings($data['Value_List']);
153 break;
154 }
157 /*******************
158 * Header
159 *******************/
161 case "header" :
162 {
163 /* [not] header
164 [comparator: tag]
165 [match-type: tag]
166 <header-names: string-list>
167 <key-list: string-list>
168 */
170 /* Invert ? */
171 if($Inverse){
172 $script .= "not ";
173 }
175 $script .="header ";
177 /* Add address part tag */
178 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
179 $script .= $data['Address_Part']." ";
180 }
182 /* Add comparator */
183 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
184 $script .= ":comparator ".$data['Comparator']." ";
185 }
187 /* Add match type */
188 $script .= $data['Match_type']." ";
190 /* Add special match type for count and value */
191 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
192 $script .= sieve_create_strings($data['Match_type_value'])." ";
193 }
195 $script .= sieve_create_strings($data['Key_List']);
196 $script .= " ";
197 $script .= sieve_create_strings($data['Value_List']);
198 break;
199 }
202 /*******************
203 * Envelope
204 *******************/
206 case "envelope" :
207 {
208 /* [not] envelope
209 [address-part: tag]
210 [comparator: tag]
211 [match-type: tag]
212 <envelope-part: string-list>
213 <key-list: string-list>
214 */
216 /* Invert */
217 if($Inverse){
218 $script .= "not ";
219 }
221 $script .="envelope ";
223 /* Add address part tag */
224 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
225 $script .= $data['Address_Part']." ";
226 }
228 /* Add comparator */
229 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
230 $script .= ":comparator ".$data['Comparator']." ";
231 }
233 /* Add match type */
234 $script .= $data['Match_type']." ";
236 /* Add special match type for count and value */
237 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
238 $script .= sieve_create_strings($data['Match_type_value'])." ";
239 }
241 $script .= sieve_create_strings($data['Key_List']);
242 $script .= " ";
243 $script .= sieve_create_strings($data['Value_List']);
244 break;
245 }
248 /*******************
249 * Exists
250 *******************/
251 case "exists" :
252 {
253 /* [not] exists
254 <header-names: string-list>
255 */
257 /* Invert ? */
258 if($Inverse){
259 $script .= "not ";
260 }
262 $script .= "exists ".sieve_create_strings($data['Values']);
263 break;
264 }
267 /*******************
268 * Size
269 *******************/
270 case "size" :
271 {
272 /* [not] size
273 <":over" / ":under">
274 <limit: number>
275 */
277 /* Invert ? */
278 if($Inverse){
279 $script .= "not ";
280 }
282 /* Add size test */
283 $script .="size ";
284 $script .=$data['Match_type']." ";
285 foreach($data['Value_List'] as $val){
286 $script .= $val." ";
287 }
288 break;
289 }
292 /*******************
293 * Allof
294 *******************/
295 case "anyof" :
296 case "allof" :
297 {
298 /* allof <tests: test-list>
299 anyof <tests: test-list> */
302 /* Add spaces, to indent the code.*/
303 $block = "\n";
304 for($i = 0 ; $i < $id ; $i ++){
305 $block .= SIEVE_INDENT_TAB;
306 }
308 /* Add allof/anyof tag */
309 $script.= " ".$key." ( ";
311 /* Add each test parameter */
312 foreach($data as $key2 => $dat){
313 if(($key2 === "Inverse") && ($key2 == "Inverse")){
314 continue;
315 }
316 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
317 }
319 /* Remove last _,_ and close the tag */
320 $script = preg_replace("/,$/","",trim($script));
321 $script.= $block.")";
322 break ;
323 }
325 default :
326 {
327 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
328 }
329 }
330 }
331 return($script);
332 }
335 /* Ensure that all changes made on the ui
336 * will be saved.
337 */
338 function save_object()
339 {
340 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
341 $this->_parsed = $tmp;
342 }
345 /* Recursivly save all ui changes for the
346 * tags and tokens provided by $parsed.
347 * $id specifies the depth of the current element.
348 * $obj_id is the current tag-id handled by this function
349 */
350 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
351 {
352 /* Variable initialization */
353 $ret ="";
354 if($parsed == NULL){
355 $parsed = $this->_parsed;
356 }
358 /* Walk through all elements */
359 foreach($parsed as $key => $data){
361 /* Id used to have unique html names */
362 $element_id = $this->id."_".$id."_".$obj_id;
364 /* Create elements */
365 switch($key)
366 {
368 /*******************
369 * Address
370 *******************/
372 case "envelope" :
373 case "header" :
374 case "address" :
375 {
376 /* [not] address
377 [address-part: tag]
378 [comparator: tag]
379 [match-type: tag]
380 <header-list: string-list>
381 <key-list: string-list>
382 */
384 /* Possible address parts we can select */
385 $address_parts = $this->address_parts;
386 $comparators = $this->comparators;
387 $match_types = $this->match_types;
388 $operators = $this->operators;
390 /* Toggle Inverse ? */
391 if(isset($_POST['toggle_inverse_'.$element_id])){
392 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
393 }
395 /* Check if we want to toggle the expert mode */
396 if(isset($_POST['Toggle_Expert_'.$element_id])){
397 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
398 }
400 /* Get address part */
401 if(isset($_POST['address_part_'.$element_id])){
402 $ap = $_POST['address_part_'.$element_id];
404 if(isset($address_parts[$ap])){
405 $parsed[$key]['Address_Part'] = $ap;
406 }
407 }
409 /* Check if match type has changed */
410 if(isset($_POST['matchtype_'.$element_id])){
411 $mt = $_POST['matchtype_'.$element_id];
413 if(isset($match_types[$mt])){
414 $parsed[$key]['Match_type'] = $mt;
415 }
416 }
418 /* Get the comparator tag, if posted */
419 if(isset($_POST['comparator_'.$element_id])){
420 $cp = $_POST['comparator_'.$element_id];
422 if(isset($comparators[$cp])){
423 $parsed[$key]['Comparator'] = $cp;
424 }
425 }
427 /* In case of :count and :value match types
428 * we have a special match operator we should save.
429 */
430 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
431 if(isset($_POST['operator_'.$element_id])){
432 $op = $_POST['operator_'.$element_id];
434 if(isset($operators[$op])){
435 $parsed[$key]['Match_type_value'] = $op;
436 }
437 }
438 }
440 /* Get the address fields we should check, they are seperated by , */
441 if(isset($_POST['keys_'.$element_id])){
442 $vls = stripslashes($_POST['keys_'.$element_id]);
443 $tmp = array();
445 $tmp2 = split(",",$vls);
446 foreach($tmp2 as $val){
447 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
448 }
449 $parsed[$key]['Key_List'] = $tmp;
450 }
452 /* Get the values should check for, they are seperated by , */
453 if(isset($_POST['values_'.$element_id])){
454 $vls = stripslashes($_POST['values_'.$element_id]);
455 $tmp = array();
457 $tmp2 = split(",",$vls);
458 foreach($tmp2 as $val){
459 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
460 }
461 $parsed[$key]['Value_List'] = $tmp;
462 }
463 break;
464 }
466 /*******************
467 * TRUE FALSE
468 *******************/
470 case "true" :
471 case "false" :
472 {
473 $name = 'boolean_'.$element_id;
474 if(isset($_POST[$name])){
475 $key2 = $_POST[$name];
477 if($key != $key2) {
478 $parsed = array($key2 => $key2);
479 }
480 }
481 break;
482 }
484 /*******************
485 * Exists
486 *******************/
488 case "exists" :
489 {
490 /* Toggle Inverse ? */
491 if(isset($_POST['toggle_inverse_'.$element_id])){
492 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
493 }
495 /* get list of match values */
496 if(isset($_POST['Values_'.$element_id])){
497 $vls = stripslashes($_POST['Values_'.$element_id]);
498 $tmp = array();
500 $tmp2 = split(",",$vls);
501 foreach($tmp2 as $val){
502 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
503 }
504 $parsed['exists']['Values'] = $tmp;
505 }
506 break;
507 }
509 /*******************
510 * Size
511 *******************/
513 case "size" :
514 {
515 $Match_types = array( ":over" => _("greater than") ,
516 ":under" => _("lower than"));
518 $Units = array( "M" => _("Megabyte") ,
519 "K" => _("Kilobyte"));
521 /* Reset error */
522 $parsed['size']['LastError'] ="";
524 /* Get match type */
525 if(isset($_POST['Match_type_'.$element_id])){
526 $mt = $_POST['Match_type_'.$element_id];
527 if(isset($Match_types[$mt])){
528 $parsed['size']['Match_type'] = $mt;
529 }else{
530 $parsed['size']['LastError'] = _("Please select a valid match type in the list box below.");
531 }
532 }
534 /* Get old values */
535 $value = preg_replace("/[^0-9]*$/","",$parsed['size']['Value_List'][0]);
536 $unit = preg_replace("/^[0-9]*/","",$parsed['size']['Value_List'][0]);
538 /* Get value */
539 if(isset($_POST['Value_'.$element_id])){
540 $vl = $_POST['Value_'.$element_id];
542 if(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl)){
543 $value = $vl;
544 }else{
545 $parsed['size']['LastError'] = _("Only numeric values are allowed here.");
546 }
547 }
549 /* Get unit */
550 if(isset($_POST['Value_Unit_'.$element_id])){
551 $ut = $_POST['Value_Unit_'.$element_id];
553 if(isset($Units[$ut])){
554 $unit = $ut;
555 }else{
556 $parsed['size']['LastError'] = _("No valid unit selected");
557 }
558 }
560 $parsed['size']['Value_List'][0] = $value.$unit;
561 break;
562 }
564 /*******************
565 * Allof
566 *******************/
568 case "allof" :
569 {
570 if(isset($_POST['toggle_inverse_'.$element_id])){
571 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
572 }
573 foreach($data as $key2 => $dat){
574 if(($key2 === "Inverse") && ($key2 == "Inverse")){
575 continue;
576 }
577 $parsed[$key][$key2] = $this->save_object_recursive($dat, ($id +1),$key2);
578 }
579 break ;
580 }
582 /*******************
583 * Anyof
584 *******************/
586 case "anyof" :
587 {
588 if(isset($_POST['toggle_inverse_'.$element_id])){
589 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
590 }
591 foreach($data as $key2 => $dat){
592 if(($key2 === "Inverse") && ($key2 == "Inverse")){
593 continue;
594 }
595 $parsed[$key][$key2] = $this->save_object_recursive($dat, ($id + 1),$key2);
596 }
597 break ;
598 }
599 }
600 }
601 return($parsed);
602 }
605 /* Return html element for IF */
606 function execute()
607 {
608 /* Create title */
609 $name = "<img alt='' src='images/small_filter.png' class='center'>";
610 $name .= "<b>"._("Condition")."</b>";
611 if($this->TYPE == "if"){
612 $name .= " - "._("If");
613 }else{
614 $name .= " - "._("Else");
615 }
617 $smarty = get_smarty();
618 $smarty->assign("Name", $name);
619 $smarty->assign("Contents", $this->get_as_html());
620 return($smarty->fetch(get_template_path("templates/element_if_else.tpl",TRUE,dirname(__FILE__))));
621 }
624 /* Returns all elements as html */
625 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
626 {
627 $ret ="";
628 if($parsed == NULL){
629 $parsed = $this->_parsed;
630 }
632 /* Walk through all elements */
633 foreach($parsed as $key => $data){
635 /* Create Inverse Tag */
636 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
637 $str_inverse = "<font color='red'><b>"._("Not")."</b></font> ";
638 $Inverse = TRUE;
639 }else{
640 $str_inverse = "";
641 $Inverse = FALSE;
642 }
644 /* Id used to have unique html names */
645 $element_id = $this->id."_".$id."_".$obj_id;
647 /* Create elements */
648 switch($key)
649 {
651 /*******************
652 * TRUE FALSE
653 *******************/
655 case "true" :
656 case "false" :
657 {
658 /* Inverse element if required */
659 if($Inverse){
660 if($key == "true"){
661 $key = "false";
662 }else{
663 $key = "true";
664 }
665 }
667 /* Get template */
668 $smarty = get_smarty();
669 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
670 $smarty->assign("selected" , $key);
671 $smarty->assign("ID" , $element_id);
672 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
673 break;
674 }
677 /*******************
678 * Header
679 *******************/
681 case "header":
682 {
683 $address_parts = $this->address_parts;
684 $comparators = $this->comparators;
685 $match_types = $this->match_types;
686 $operators = $this->operators;
688 $smarty = get_smarty();
689 $smarty->assign("comparators",$comparators);
690 $smarty->assign("match_types",$match_types);
691 $smarty->assign("operators",$operators);
693 $smarty->assign("match_type", $data['Match_type']);
694 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
695 $smarty->assign("comparator", $data['Comparator']);
698 $keys = "";
699 foreach($data['Key_List'] as $key){
700 $keys .= $key.", ";
701 }
702 $keys = preg_replace("/,$/","",trim($keys));
704 $values = "";
705 foreach($data['Value_List'] as $key){
706 $values .= $key.", ";
707 }
708 $values = preg_replace("/,$/","",trim($values));
710 $smarty->assign("keys",$keys);
711 $smarty->assign("Inverse",$Inverse);
712 $smarty->assign("values",$values);
713 $smarty->assign("Expert", $data['Expert']);
716 $smarty->assign("ID" , $element_id);
717 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
718 break;
719 }
722 /*******************
723 * Envelope
724 *******************/
726 case "envelope":
727 {
728 $address_parts = $this->address_parts;
729 $comparators = $this->comparators;
730 $match_types = $this->match_types;
731 $operators = $this->operators;
733 $smarty = get_smarty();
734 $smarty->assign("Inverse",$Inverse);
735 $smarty->assign("comparators",$comparators);
736 $smarty->assign("Expert", $data['Expert']);
737 $smarty->assign("match_types",$match_types);
738 $smarty->assign("operators",$operators);
740 $smarty->assign("match_type", $data['Match_type']);
741 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
742 $smarty->assign("comparator", $data['Comparator']);
744 $keys = "";
745 foreach($data['Key_List'] as $key){
746 $keys .= $key.", ";
747 }
748 $keys = preg_replace("/,$/","",trim($keys));
750 $values = "";
751 foreach($data['Value_List'] as $key){
752 $values .= $key.", ";
753 }
754 $values = preg_replace("/,$/","",trim($values));
755 $smarty->assign("keys",$keys);
756 $smarty->assign("values",$values);
758 $smarty->assign("ID" , $element_id);
759 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
760 break;
761 }
764 /*******************
765 * Address
766 *******************/
768 case "address" :
769 {
770 $address_parts = $this->address_parts;
771 $comparators = $this->comparators;
772 $match_types = $this->match_types;
773 $operators = $this->operators;
775 $smarty = get_smarty();
776 $smarty->assign("Inverse",$Inverse);
777 $smarty->assign("address_parts",$address_parts);
778 $smarty->assign("comparators",$comparators);
779 $smarty->assign("match_types",$match_types);
780 $smarty->assign("operators",$operators);
782 $smarty->assign("match_type", $data['Match_type']);
783 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
784 $smarty->assign("comparator", $data['Comparator']);
785 $smarty->assign("address_part", $data['Address_Part']);
787 $smarty->assign("Expert", $data['Expert']);
789 $keys = "";
790 foreach($data['Key_List'] as $key){
791 $keys .= $key.", ";
792 }
793 $keys = preg_replace("/,$/","",trim($keys));
795 $values = "";
796 foreach($data['Value_List'] as $key){
797 $values .= $key.", ";
798 }
799 $values = preg_replace("/,$/","",trim($values));
800 $smarty->assign("keys",$keys);
801 $smarty->assign("values",$values);
804 $smarty->assign("ID" , $element_id);
805 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
806 break;
807 }
810 /*******************
811 * Size
812 *******************/
814 case "size" :
815 {
816 $Match_types = array( ":over" => _("greater than") ,
817 ":under" => _("lower than"));
819 $Units = array( "M" => _("Megabyte") ,
820 "K" => _("Kilobyte"));
822 $Match_type = $data['Match_type'];
823 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
824 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
826 $LastError = "";
827 if(isset($data['LastError'])){
828 $LastError = $data['LastError'];
829 }
831 $smarty = get_smarty();
832 $smarty->assign("Inverse",$Inverse);
833 $smarty->assign("LastError",$LastError);
834 $smarty->assign("Match_types",$Match_types);
835 $smarty->assign("Units",$Units);
836 $smarty->assign("Match_type",$Match_type);
837 $smarty->assign("Value",$Value);
838 $smarty->assign("Value_Unit",$Value_Unit);
839 $smarty->assign("ID" , $element_id);
840 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
841 break;
842 }
844 /*******************
845 * Exists
846 *******************/
849 case "exists" :
850 {
851 $LastError = "";
852 if(isset($data['LastError'])){
853 $LastError = $data['LastError'];
854 }
856 $Values = "";
857 foreach($data['Values'] as $val){
858 $Values .= $val.", ";
859 }
860 $Values = preg_replace("/,$/","",trim($Values));
862 $smarty = get_smarty();
863 $smarty->assign("LastError",$LastError);
864 $smarty->assign("Values",$Values);
865 $smarty->assign("Inverse",$Inverse);
866 $smarty->assign("ID" , $element_id);
867 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
868 break;
869 }
872 /*******************
873 * All of
874 *******************/
876 case "allof" :
877 {
878 $Contents = "";
879 foreach($data as $key => $dat){
880 if(($key === "Inverse") && ($key == "Inverse")){
881 continue;
882 }
883 $Contents .= $this->get_as_html($dat, ($id +1),$key);
884 }
885 $smarty = get_smarty();
886 $smarty->assign("Inverse",$Inverse);
887 $smarty->assign("Contents",$Contents);
888 $smarty->assign("ID" , $element_id);
889 $ret .= $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
890 break ;
891 }
894 /*******************
895 * Any of
896 *******************/
898 case "anyof" :
899 {
900 $Contents = "";
901 foreach($data as $key => $dat){
902 if(($key === "Inverse") && ($key == "Inverse")){
903 continue;
904 }
905 $Contents .= $this->get_as_html($dat, ($id +1),$key);
906 }
907 $smarty = get_smarty();
908 $smarty->assign("Inverse",$Inverse);
909 $smarty->assign("Contents",$Contents);
910 $smarty->assign("ID" , $element_id);
911 $ret .= $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
912 break ;
913 }
914 default :
915 {
916 $ret = "<table width='100%' cellspacing=0 cellpadding=0>
917 <tr>
918 <td style='background-color: #FEDCA9 ; border: solid 1px #EEEEEE'>";
919 $ret.= $key."<br>";
920 $ret.= " </td>
921 </tr>
922 </table>";
923 }
924 }
925 }
926 return($ret);
927 }
930 /* Parse given token identified by $data[$id]
931 * and return the parsed tokens.
932 */
933 function _parse($data,$id = 0)
934 {
935 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
936 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
937 $type = $data[$id]['text'];
938 $tmp = array();
940 /* Is there an identifier named 'not' to inverse this filter ? */
941 $Inverse = FALSE;
942 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
943 $Inverse = TRUE;
944 $id ++;
945 $type = $data[$id]['text'];
946 }
948 switch($type)
949 {
951 /****************
952 * Parse - Envelope / Header / Address
953 ****************/
955 case "envelope" :
956 case "header":
957 case "address" :
958 {
959 /* Address matches are struckture as follows :
960 [not]
961 address
962 [address-part: tag] all|localpart|domain|user|detail
963 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
964 [match-type: tag] is|contains|matches|count|value
965 <header-list: string-list>
966 <key-list: string-list>
967 */
970 $part = "(:all|:localpart|:domain)";
971 $operator = "(:contains|:is|:matches|:count|:value)";
972 $value_op = "(lt|le|eq|ge|gt|ne)";
974 $Address_Part = "";
975 $Comparator = "";
976 $Match_type = "";
977 $Match_type_value = "";
979 $Key_List = array();
980 $Value_List = array();
982 for($i = 0 ; $i < count($data) ; $i ++){
984 /* Get next node */
985 $node = $data[$i];
987 /* Check address part definition */
988 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
989 $Address_Part = $node['text'];
990 }
992 /* Check for match type */
993 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
994 $Match_type = $node['text'];
996 /* Get value operator */
997 if(in_array($Match_type,array(":value",":count"))){
998 $i ++;
999 $node = $data[$i];
1001 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1002 $Match_type_value = $node['text'];
1003 }
1004 }
1005 }
1007 /* Check for a comparator */
1008 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1009 $i ++;
1010 $node = $data[$i];
1011 $Comparator = $node['text'];
1012 }
1014 /* Check for Key_List */
1015 elseif(count(sieve_get_strings($data,$i))){
1016 $tmp2 = sieve_get_strings($data,$i);
1017 $i = $tmp2['OFFSET'];
1019 if(!count($Key_List)){
1020 $Key_List = $tmp2['STRINGS'];
1021 }else{
1022 $Value_List = $tmp2['STRINGS'];
1023 }
1024 }
1026 }
1029 /* Add to Tree */
1030 $values = array( "Inverse" => $Inverse,
1031 "Comparator" => $Comparator,
1032 "Expert" => FALSE,
1033 "Match_type" => $Match_type,
1034 "Match_type_value"=> $Match_type_value,
1035 "Key_List" => $Key_List,
1036 "Value_List" => $Value_List) ;
1037 if($type == "address"){
1038 $values["Address_Part"] = $Address_Part;
1039 }
1040 $tmp[$type] = $values;
1041 break;
1042 }
1045 /****************
1046 * Parse - Size
1047 ****************/
1049 case "size":
1050 {
1052 $ops = "(:over|:under)";
1054 $Match_type = "";
1056 for($i = $id ; $i < count($data); $i++){
1058 /* Get current node */
1059 $node = $data[$i];
1061 /* Get tag (under / over) */
1062 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1063 $Match_type = $node['text'];
1064 }
1066 /* Get Value_List, the value that we want to match for */
1067 elseif(count(sieve_get_strings($data,$i))){
1068 $tmp2 = sieve_get_strings($data,$i);
1069 $i = $tmp2['OFFSET'];
1071 $Value_List = $tmp2['STRINGS'];
1072 }
1073 }
1075 $tmp[$type]= array( "Inverse" => $Inverse,
1076 "Match_type" => $Match_type,
1077 "Value_List" => $Value_List);
1078 break;
1079 }
1082 /****************
1083 * Parse - True / False
1084 ****************/
1086 case "true":
1087 {
1088 $tmp['true'] = "true";
1089 break;
1090 }
1091 case "false":
1092 {
1093 $tmp['false'] = "false";
1094 break;
1095 }
1098 /****************
1099 * Parse - Exists
1100 ****************/
1102 case "exists":
1103 {
1105 /* Skip first values, [if,not,exists] */
1106 $node = $data[$id];
1107 while(in_array($node['text'],array("if","not","exists"))){
1108 $id ++;
1109 $node = $data[$id];
1110 }
1112 /* Get values */
1113 $tmp2 = sieve_get_strings($data,$id);
1116 $tmp['exists'] = array('Inverse' => $Inverse,
1117 'Values' => $tmp2['STRINGS']);
1118 break;
1119 }
1122 /****************
1123 * Parse - Allof
1124 ****************/
1126 case "allof" :
1127 {
1128 /* Get parameter and recursivly call this method
1129 * for each parameter
1130 */
1131 $id ++;
1132 $tmp2 = $this->get_parameter($data,$id);
1134 foreach($tmp2 as $parameter){
1135 $tmp['allof'][] = $this->_parse($parameter);
1136 }
1137 $tmp['allof']['Inverse'] = $Inverse;
1138 break;
1139 }
1142 /****************
1143 * Parse - Anyof
1144 ****************/
1146 case "anyof" :
1147 {
1148 /* Get parameter and recursivly call this method
1149 * for each parameter
1150 */
1151 $id ++;
1152 $tmp2 = $this->get_parameter($data,$id);
1154 foreach($tmp2 as $parameter){
1155 $tmp['anyof'][] = $this->_parse($parameter);
1156 }
1157 $tmp['anyof']['Inverse'] = $Inverse;
1158 break;
1159 }
1160 default : $tmp[$id] = $type;
1161 }
1163 return($tmp);
1164 }
1167 function get_parameter($data,$id)
1168 {
1169 $par = array();
1170 $open_brakets = 0;
1171 $next = NULL;
1172 $num = 0;
1173 for($i = $id ; $i < count($data) ; $i++ ){
1174 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1175 $open_brakets ++;
1176 }
1177 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1178 $num ++;
1179 }
1180 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1181 $par[$num][] = $data[$i];
1182 }
1183 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1184 $open_brakets --;
1185 }
1186 }
1187 return($par);
1188 }
1189 }
1191 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1192 ?>