1 <?php
4 class sieve_if
5 {
6 var $_parsed = array();
7 var $TYPE = "if";
8 var $object_id = -1;
10 var $address_parts = array();
11 var $comparators = array();
12 var $match_types = array();
13 var $operators = array();
14 var $parent = NULL;
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,$parent)
21 {
22 $this->parent = $parent;
24 /* Possible address parts we can select */
25 $this->address_parts = array(
26 ":all" => _("Complete adress")." ("._("Default").")",
27 ":domain" => _("Domian part") ,
28 ":localpart" => _("Local part"));
30 /* comparator type */
31 $this->comparators = array(
32 "i;ascii-casemap" => _("Case insensitive")." ("._("Default").")",
33 "i;octet" => _("Case sensitive"),
34 "i;ascii-numeric" => _("Numeric"));
36 /* Match types */
37 $this->match_types = array(
38 ":is" => _("is"),
39 ":contains" => _("contains"),
40 ":matches" => _("matches"),
41 ":count" => _("count"),
42 ":value" => _("value is"));
44 /* Operators */
45 $this->operators = array(
46 "lt" => _("less than"),
47 "le" => _("less or equal"),
48 "eq" => _("equals"),
49 "ge" => _("greater or equal"),
50 "gt" => _("greater than"),
51 "ne" => _("not equal"));
53 $this->object_id = $object_id;
54 if($elements!=NULL){
55 $this->elements = $elements;
56 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
57 }
58 }
61 /* Returns the sieve script for this
62 * if/else tag.
63 */
64 function get_sieve_script_part()
65 {
66 $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
67 return($tmp);
68 }
71 /* Return error msgs */
72 function check()
73 {
74 $check = $this->check_recursive();
75 return($check);
76 }
79 /* Recursivly fetch all error msgs */
80 function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
81 {
82 $ret = array();
83 if($parsed == NULL){
84 $parsed = $this->_parsed;
85 }
87 /* Walk through all elements */
88 foreach($parsed as $key => $data){
90 /* Create elements */
91 switch($key)
92 {
93 /*******************
94 * Allof / Anyof
95 *******************/
96 case "anyof" :
97 case "allof" :
98 {
99 foreach($data as $key2 => $dat){
100 if(($key2 === "Inverse") && ($key2 == "Inverse")){
101 continue;
102 }
103 $msgs = $this->check_recursive($dat, ($id +1),$key2);
105 foreach($msgs as $msg){
106 $ret[] = $msg;
107 }
108 }
109 break;
110 }
112 /*******************
113 * True / False
114 *******************/
116 case "true" :
117 case "false" :
118 {
119 /* Can't fail anyway */
120 break;
121 }
123 /*******************
124 * Default
125 *******************/
127 default:
128 {
129 if(isset($data['LastError']) && !empty($data['LastError'])){
130 $ret[] = $data['LastError'];
131 }
132 }
133 }
134 }
135 return($ret);
136 }
139 /* Recursivly create a sieve script out of the given
140 * tags and tokens provided by $parsed.
141 * $id specifies the depth of the current element.
142 * $obj_id is the current tag-id handled by this function
143 */
144 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
145 {
146 $script ="";
147 if($parsed == NULL){
148 $parsed = $this->_parsed;
149 }
152 if(!is_array($parsed)){
153 return;
154 }
156 /* Walk through all elements */
157 foreach($parsed as $key => $data){
159 /* Create Inverse Tag */
160 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
161 $Inverse = TRUE;
162 }else{
163 $Inverse = FALSE;
164 }
166 /* Create elements */
167 switch($key)
168 {
170 /*******************
171 * True / False
172 *******************/
174 case "true" :
175 case "false" :
176 {
177 /* Invert this test if required */
178 if($Inverse){
179 $script .= "not ";
180 }
181 $script .= $key;
182 break;
183 }
186 /*******************
187 * Address
188 *******************/
190 case "address" :
191 {
192 /* [not] address
193 [address-part: tag]
194 [comparator: tag]
195 [match-type: tag]
196 <header-list: string-list>
197 <key-list: string-list>
198 */
200 /* Invert this test if required */
201 if($Inverse){
202 $script .= "not ";
203 }
205 $script .="address ";
207 /* Add address part tag */
208 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
209 $script .= $data['Address_Part']." ";
210 }
212 /* Add comparator */
213 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
214 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
215 }
217 /* Add match type */
218 $script .= $data['Match_type']." ";
220 /* Add special match type for count and value */
221 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
222 $script .= sieve_create_strings($data['Match_type_value'])." ";
223 }
225 $script .= sieve_create_strings($data['Key_List']);
226 $script .= " ";
227 $script .= sieve_create_strings($data['Value_List']);
228 break;
229 }
232 /*******************
233 * Header
234 *******************/
236 case "header" :
237 {
238 /* [not] header
239 [comparator: tag]
240 [match-type: tag]
241 <header-names: string-list>
242 <key-list: string-list>
243 */
245 /* Invert ? */
246 if($Inverse){
247 $script .= "not ";
248 }
250 $script .="header ";
252 /* Add address part tag */
253 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
254 $script .= $data['Address_Part']." ";
255 }
257 /* Add comparator */
258 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
259 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
260 }
262 /* Add match type */
263 $script .= $data['Match_type']." ";
265 /* Add special match type for count and value */
266 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
267 $script .= sieve_create_strings($data['Match_type_value'])." ";
268 }
270 $script .= sieve_create_strings($data['Key_List']);
271 $script .= " ";
272 $script .= sieve_create_strings($data['Value_List']);
273 break;
274 }
277 /*******************
278 * Envelope
279 *******************/
281 case "envelope" :
282 {
283 /* [not] envelope
284 [address-part: tag]
285 [comparator: tag]
286 [match-type: tag]
287 <envelope-part: string-list>
288 <key-list: string-list>
289 */
291 /* Invert */
292 if($Inverse){
293 $script .= "not ";
294 }
296 $script .="envelope ";
298 /* Add address part tag */
299 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
300 $script .= $data['Address_Part']." ";
301 }
303 /* Add comparator */
304 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
305 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
306 }
308 /* Add match type */
309 $script .= $data['Match_type']." ";
311 /* Add special match type for count and value */
312 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
313 $script .= sieve_create_strings($data['Match_type_value'])." ";
314 }
316 $script .= sieve_create_strings($data['Key_List']);
317 $script .= " ";
318 $script .= sieve_create_strings($data['Value_List']);
319 break;
320 }
323 /*******************
324 * Exists
325 *******************/
326 case "exists" :
327 {
328 /* [not] exists
329 <header-names: string-list>
330 */
332 /* Invert ? */
333 if($Inverse){
334 $script .= "not ";
335 }
337 $script .= "exists ".sieve_create_strings($data['Values']);
338 break;
339 }
342 /*******************
343 * Size
344 *******************/
345 case "size" :
346 {
347 /* [not] size
348 <":over" / ":under">
349 <limit: number>
350 */
352 /* Invert ? */
353 if($Inverse){
354 $script .= "not ";
355 }
357 /* Add size test */
358 $script .="size ";
359 $script .=$data['Match_type']." ";
360 foreach($data['Value_List'] as $val){
361 $script .= $val." ";
362 }
363 break;
364 }
367 /*******************
368 * Allof
369 *******************/
370 case "anyof" :
371 case "allof" :
372 {
373 /* allof <tests: test-list>
374 anyof <tests: test-list> */
377 /* Add spaces, to indent the code.*/
378 $block = "\n";
379 for($i = 0 ; $i < $id ; $i ++){
380 $block .= SIEVE_INDENT_TAB;
381 }
383 /* Add allof/anyof tag */
384 $script.= " ".$key." ( ";
386 /* Add each test parameter */
387 foreach($data as $key2 => $dat){
388 if(($key2 === "Inverse") && ($key2 == "Inverse")){
389 continue;
390 }
391 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
392 }
394 /* Remove last _,_ and close the tag */
395 $script = preg_replace("/,$/","",trim($script));
396 $script.= $block.")";
397 break ;
398 }
400 default :
401 {
402 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
403 }
404 }
405 }
406 return($script);
407 }
410 function add_test($data,$type)
411 {
412 switch($type)
413 {
414 case "header" :
415 case "address" :
416 case "envelope" :
417 {
418 /* Add to Tree */
419 $values = array( "Inverse" => FALSE,
420 "Comparator" => "",
421 "Expert" => FALSE,
422 "LastError" => "",
423 "Match_type" => ":contains",
424 "Match_type_value"=> "",
425 "Key_List" => array(_("emtpy")),
426 "Value_List" => array(_("empty"))) ;
427 if($type == "address"){
428 $values["Address_Part"] = ":all";
429 }
430 $data[$type]=$values;
432 $this->parent->add_require("relational");
433 if($type == "envelope"){
434 $this->parent->add_require("envelope");
435 }
438 break;
439 }
440 case "allof" :
441 case "anyof" :
442 {
443 $data[$type] = array("Inverse" => FALSE);
444 break;
445 }
446 case "size" :
447 {
448 $tmp= array(
449 "Inverse" => FALSE,
450 "Match_type" => ":over",
451 "Value_List" => array("1M"));
453 $tmp['LastError'] = "";
454 $data[$type] = $tmp;
455 break;
456 }
457 case "true":
458 {
459 $data['true'] = "true";
460 $data['true']['LastError'] = "";
461 break;
462 }
463 case "false":
464 {
465 $data['false'] = "false";
466 $data['false']['LastError'] = "";
467 break;
468 }
469 case "exists" :
470 {
471 $data['exists'] = array('Inverse' => FALSE,
472 'Values' => array(_("Nothing specified right now")),
473 'LastError' => "");
474 break;
475 }
476 default : echo "Still buggy ";exit;
477 }
479 return($data);
480 }
483 /* Ensure that all changes made on the ui
484 * will be saved.
485 */
486 function save_object()
487 {
489 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
490 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
491 }
493 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
494 $this->_parsed = $tmp;
495 }
498 /* Recursivly save all ui changes for the
499 * tags and tokens provided by $parsed.
500 * $id specifies the depth of the current element.
501 * $obj_id is the current tag-id handled by this function
502 */
503 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
504 {
505 /* Variable initialization */
506 $ret ="";
507 if($parsed == NULL){
508 $parsed = $this->_parsed;
509 }
511 if(!is_array($parsed)) return;
513 /* Walk through all elements */
514 foreach($parsed as $key => $data){
516 /* Id used to have unique html names */
517 $element_id = $this->object_id."_".$id."_".$obj_id;
519 foreach($_POST as $name => $value){
520 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
521 return(false);
522 }
523 }
526 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
527 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
528 }
530 /* Create elements */
531 switch($key)
532 {
533 /*******************
534 * Address
535 *******************/
537 case "envelope" :
538 case "header" :
539 case "address" :
540 {
541 /* [not] address
542 [address-part: tag]
543 [comparator: tag]
544 [match-type: tag]
545 <header-list: string-list>
546 <key-list: string-list>
547 */
549 /* Possible address parts we can select */
550 $address_parts = $this->address_parts;
551 $comparators = $this->comparators;
552 $match_types = $this->match_types;
553 $operators = $this->operators;
555 $parsed[$key]['LastError'] = "";
557 /* Toggle Inverse ? */
558 if(isset($_POST['toggle_inverse_'.$element_id])){
559 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
560 }
562 /* Check if we want to toggle the expert mode */
563 if(isset($_POST['Toggle_Expert_'.$element_id])){
564 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
565 }
567 /* Get address part */
568 if(isset($_POST['address_part_'.$element_id])){
569 $ap = $_POST['address_part_'.$element_id];
571 if(!isset($address_parts[$ap])){
572 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
573 }
574 $parsed[$key]['Address_Part'] = $ap;
575 }
577 /* Check if match type has changed */
578 if(isset($_POST['matchtype_'.$element_id])){
579 $mt = $_POST['matchtype_'.$element_id];
581 if(!isset($match_types[$mt])){
582 $parsed[$key]['LastError'] = _("Invalid match type given.");
583 }
584 $parsed[$key]['Match_type'] = $mt;
585 }
587 /* Get the comparator tag, if posted */
588 if(isset($_POST['comparator_'.$element_id])){
589 $cp = $_POST['comparator_'.$element_id];
591 if(!isset($comparators[$cp])){
592 $parsed[$key]['LastError'] = _("Invalid operator given.");
593 }
594 $parsed[$key]['Comparator'] = $cp;
596 if($cp == "i;ascii-numeric"){
597 $this->parent->add_require("comparator-i;ascii-numeric");
598 }
599 }
601 /* In case of :count and :value match types
602 * we have a special match operator we should save.
603 */
604 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
605 if(isset($_POST['operator_'.$element_id])){
606 $op = $_POST['operator_'.$element_id];
608 if(!isset($operators[$op])){
609 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
610 }
611 $parsed[$key]['Match_type_value'] = $op;
612 }
613 }
615 /* Get the address fields we should check, they are seperated by , */
616 if(isset($_POST['keys_'.$element_id])){
617 $vls = stripslashes($_POST['keys_'.$element_id]);
618 $tmp = array();
620 $tmp2 = split(",",$vls);
621 foreach($tmp2 as $val){
622 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
623 }
624 $parsed[$key]['Key_List'] = $tmp;
625 }
627 /* Get the values should check for, they are seperated by , */
628 if(isset($_POST['values_'.$element_id])){
629 $vls = stripslashes($_POST['values_'.$element_id]);
630 $tmp = array();
632 $tmp2 = split(",",$vls);
633 foreach($tmp2 as $val){
634 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
635 }
636 $parsed[$key]['Value_List'] = $tmp;
637 }
638 break;
639 }
641 /*******************
642 * TRUE FALSE
643 *******************/
645 case "true" :
646 case "false" :
647 {
648 $name = 'boolean_'.$element_id;
649 if(isset($_POST[$name])){
650 $key2 = $_POST[$name];
652 if($key != $key2) {
653 $parsed = array($key2 => $key2);
654 }
655 }
656 break;
657 }
659 /*******************
660 * Exists
661 *******************/
663 case "exists" :
664 {
665 /* Toggle Inverse ? */
666 if(isset($_POST['toggle_inverse_'.$element_id])){
667 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
668 }
670 /* get list of match values */
671 if(isset($_POST['Values_'.$element_id])){
672 $vls = stripslashes($_POST['Values_'.$element_id]);
673 $tmp = array();
675 $tmp2 = split(",",$vls);
676 foreach($tmp2 as $val){
677 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
678 }
679 $parsed['exists']['Values'] = $tmp;
680 }
681 break;
682 }
684 /*******************
685 * Size
686 *******************/
688 case "size" :
689 {
690 $Match_types = array( ":over" => _("greater than") ,
691 ":under" => _("lower than"));
693 $Units = array( "M" => _("Megabyte") ,
694 "K" => _("Kilobyte"));
696 /* Reset error */
697 $parsed[$key]['LastError'] ="";
699 /* Get match type */
700 if(isset($_POST['Match_type_'.$element_id])){
701 $mt = $_POST['Match_type_'.$element_id];
702 if(!isset($Match_types[$mt])){
703 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
704 }
705 $parsed[$key]['Match_type'] = $mt;
706 }
708 /* Get old values */
709 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
710 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
712 /* Get value */
713 if(isset($_POST['Value_'.$element_id])){
714 $vl = $_POST['Value_'.$element_id];
716 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
717 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
718 }
719 $value = preg_replace("/[^0-9]/","",$vl);
720 }
722 /* Get unit */
723 if(isset($_POST['Value_Unit_'.$element_id])){
724 $ut = $_POST['Value_Unit_'.$element_id];
726 if(!isset($Units[$ut])){
727 $parsed[$key]['LastError'] = _("No valid unit selected");
728 }
729 $unit = $ut;
730 }
731 $parsed[$key]['Value_List'] = array();
732 $parsed[$key]['Value_List'][0] = $value.$unit;
733 break;
734 }
736 /*******************
737 * Allof
738 *******************/
740 case "allof" :
741 {
742 if(isset($_POST['toggle_inverse_'.$element_id])){
743 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
744 }
745 foreach($data as $key2 => $dat){
746 if(($key2 === "Inverse") && ($key2 == "Inverse")){
747 continue;
748 }
749 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
750 if($tmp_data != false){
751 $parsed[$key][$key2] = $tmp_data;
752 }else{
753 unset( $parsed[$key][$key2]);
754 }
755 }
756 break ;
757 }
759 /*******************
760 * Anyof
761 *******************/
763 case "anyof" :
764 {
765 if(isset($_POST['toggle_inverse_'.$element_id])){
766 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
767 }
768 foreach($data as $key2 => $dat){
769 if(($key2 === "Inverse") && ($key2 == "Inverse")){
770 continue;
771 }
772 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
773 if($tmp_data != false){
774 $parsed[$key][$key2] = $tmp_data;
775 }else{
776 unset( $parsed[$key][$key2]);
777 }
778 }
779 break ;
780 }
781 }
782 }
783 return($parsed);
784 }
787 /* Return html element for IF */
788 function execute()
789 {
790 /* Create title */
791 $name = "<img alt='' src='images/small_filter.png' class='center'>";
792 $name .= "<b>"._("Condition")."</b>";
793 if($this->TYPE == "if"){
794 $name .= " - "._("If");
795 }elseif($this->TYPE == "elsif"){
796 $name .= " - "._("Else if");
797 }else{
798 $name .= " - "._("Else");
799 }
801 $smarty = get_smarty();
802 $smarty->assign("ID", $this->object_id);
804 /* Only display navigation elements if necessary */
805 if($this->TYPE == "if"){
806 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
807 }else{
808 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
809 }
811 $smarty->assign("Name", $name);
812 $smarty->assign("Contents", $this->get_as_html());
814 if($this->TYPE == "if"){
815 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
816 }else{
817 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
818 }
819 $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
820 return($str);
821 }
824 /* Returns all elements as html */
825 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
826 {
827 $ret ="";
828 if($parsed == NULL){
829 $parsed = $this->_parsed;
830 }
832 if((!is_array($parsed)) || !count($parsed)) {
833 $smarty = get_smarty();
834 $smarty->assign("ID",$this->object_id);
835 $smarty->assign("DisplayAdd",TRUE);
836 $smarty->assign("DisplayDel",FALSE);
837 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
838 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
839 return($ret);
840 }
842 /* Walk through all elements */
843 foreach($parsed as $key => $data){
845 /* Create Inverse Tag */
846 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
847 $Inverse = TRUE;
848 }else{
849 $Inverse = FALSE;
850 }
852 /* Id used to have unique html names */
853 $element_id = $this->object_id."_".$id."_".$obj_id;
855 /* Create elements */
856 switch($key)
857 {
859 /*******************
860 * TRUE FALSE
861 *******************/
863 case "true" :
864 case "false" :
865 {
866 /* Inverse element if required */
867 if($Inverse){
868 if($key == "true"){
869 $key = "false";
870 }else{
871 $key = "true";
872 }
873 }
875 /* Get template */
876 $smarty = get_smarty();
877 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
878 $smarty->assign("selected" , $key);
879 $smarty->assign("ID" , $element_id);
880 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
881 break;
882 }
885 /*******************
886 * Header
887 *******************/
889 case "header":
890 {
891 $address_parts = $this->address_parts;
892 $comparators = $this->comparators;
893 $match_types = $this->match_types;
894 $operators = $this->operators;
896 $smarty = get_smarty();
897 $smarty->assign("comparators",$comparators);
898 $smarty->assign("match_types",$match_types);
899 $smarty->assign("operators",$operators);
900 $smarty->assign("LastError",$data['LastError']);
901 $smarty->assign("match_type", $data['Match_type']);
902 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
903 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
905 $keys = "";
906 foreach($data['Key_List'] as $key){
907 $keys .= $key.", ";
908 }
909 $keys = preg_replace("/,$/","",trim($keys));
911 $values = "";
912 foreach($data['Value_List'] as $key){
913 $values .= $key.", ";
914 }
915 $values = preg_replace("/,$/","",trim($values));
917 $smarty->assign("keys",$keys);
918 $smarty->assign("Inverse",$Inverse);
919 $smarty->assign("values",$values);
920 $smarty->assign("Expert", $data['Expert']);
922 $smarty->assign("ID" , $element_id);
923 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
924 break;
925 }
928 /*******************
929 * Envelope
930 *******************/
932 case "envelope":
933 {
934 $address_parts = $this->address_parts;
935 $comparators = $this->comparators;
936 $match_types = $this->match_types;
937 $operators = $this->operators;
939 $smarty = get_smarty();
940 $smarty->assign("Inverse",$Inverse);
941 $smarty->assign("comparators",$comparators);
942 $smarty->assign("Expert", $data['Expert']);
943 $smarty->assign("match_types",$match_types);
944 $smarty->assign("operators",$operators);
945 $smarty->assign("LastError",$data['LastError']);
946 $smarty->assign("match_type", $data['Match_type']);
947 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
948 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
950 $keys = "";
951 foreach($data['Key_List'] as $key){
952 $keys .= $key.", ";
953 }
954 $keys = preg_replace("/,$/","",trim($keys));
956 $values = "";
957 foreach($data['Value_List'] as $key){
958 $values .= $key.", ";
959 }
960 $values = preg_replace("/,$/","",trim($values));
961 $smarty->assign("keys",$keys);
962 $smarty->assign("values",$values);
964 $smarty->assign("ID" , $element_id);
965 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
966 break;
967 }
970 /*******************
971 * Address
972 *******************/
974 case "address" :
975 {
976 $address_parts = $this->address_parts;
977 $comparators = $this->comparators;
978 $match_types = $this->match_types;
979 $operators = $this->operators;
981 $smarty = get_smarty();
982 $smarty->assign("Inverse",$Inverse);
983 $smarty->assign("address_parts",$address_parts);
984 $smarty->assign("comparators",$comparators);
985 $smarty->assign("match_types",$match_types);
986 $smarty->assign("LastError",$data['LastError']);
987 $smarty->assign("operators",$operators);
988 $smarty->assign("match_type", $data['Match_type']);
989 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
990 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
991 $smarty->assign("address_part", $data['Address_Part']);
992 $smarty->assign("Expert", $data['Expert']);
994 $keys = "";
995 foreach($data['Key_List'] as $key){
996 $keys .= $key.", ";
997 }
998 $keys = preg_replace("/,$/","",trim($keys));
1000 $values = "";
1001 foreach($data['Value_List'] as $key){
1002 $values .= $key.", ";
1003 }
1004 $values = preg_replace("/,$/","",trim($values));
1005 $smarty->assign("keys",$keys);
1006 $smarty->assign("values",$values);
1007 $smarty->assign("ID" , $element_id);
1008 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1009 break;
1010 }
1013 /*******************
1014 * Size
1015 *******************/
1017 case "size" :
1018 {
1019 $Match_types = array( ":over" => _("greater than") ,
1020 ":under" => _("lower than"));
1022 $Units = array( "M" => _("Megabyte") ,
1023 "K" => _("Kilobyte"));
1025 $Match_type = $data['Match_type'];
1026 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1027 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1029 $LastError = "";
1030 if(isset($data['LastError'])){
1031 $LastError = $data['LastError'];
1032 }
1034 $smarty = get_smarty();
1035 $smarty->assign("Inverse",$Inverse);
1036 $smarty->assign("LastError",$LastError);
1037 $smarty->assign("Match_types",$Match_types);
1038 $smarty->assign("Units",$Units);
1039 $smarty->assign("Match_type",$Match_type);
1040 $smarty->assign("Value",$Value);
1041 $smarty->assign("Value_Unit",$Value_Unit);
1042 $smarty->assign("ID" , $element_id);
1043 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1044 break;
1045 }
1047 /*******************
1048 * Exists
1049 *******************/
1051 case "exists" :
1052 {
1053 $LastError = "";
1054 if(isset($data['LastError'])){
1055 $LastError = $data['LastError'];
1056 }
1058 $Values = "";
1059 foreach($data['Values'] as $val){
1060 $Values .= $val.", ";
1061 }
1062 $Values = preg_replace("/,$/","",trim($Values));
1064 $smarty = get_smarty();
1065 $smarty->assign("LastError",$LastError);
1066 $smarty->assign("Values",$Values);
1067 $smarty->assign("Inverse",$Inverse);
1068 $smarty->assign("ID" , $element_id);
1069 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1070 break;
1071 }
1074 /*******************
1075 * All of
1076 *******************/
1078 case "allof" :
1079 {
1080 $Contents = "";
1081 foreach($data as $key => $dat){
1082 if(($key === "Inverse") && ($key == "Inverse")){
1083 continue;
1084 }
1085 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1086 }
1088 $smarty = get_smarty();
1089 $smarty->assign("ID" , $element_id);
1090 $smarty->assign("DisplayAdd",TRUE);
1091 $smarty->assign("DisplayDel",FALSE);
1092 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1093 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1095 $smarty->assign("Inverse",$Inverse);
1096 $smarty->assign("Contents",$cont_tmp.$Contents);
1097 $smarty->assign("ID" , $element_id);
1098 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1100 $ret = $allof_tmp;
1101 break ;
1102 }
1105 /*******************
1106 * Any of
1107 *******************/
1109 case "anyof" :
1110 {
1111 $Contents = "";
1112 foreach($data as $key => $dat){
1113 if(($key === "Inverse") && ($key == "Inverse")){
1114 continue;
1115 }
1116 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1117 }
1118 $smarty = get_smarty();
1119 $smarty->assign("ID" , $element_id);
1120 $smarty->assign("DisplayAdd",TRUE);
1121 $smarty->assign("DisplayDel",FALSE);
1122 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1123 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1125 $smarty->assign("Inverse",$Inverse);
1126 $smarty->assign("Contents",$cont_tmp.$Contents);
1127 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1129 $ret = $allof_tmp;
1131 break ;
1132 }
1133 default :
1134 {
1135 trigger_error(_("Unhandled switch type"));
1136 }
1137 }
1138 }
1140 if(!isset($smarty)){
1141 $smarty =get_smarty();
1142 }
1144 $smarty->assign("ID",$element_id);
1145 $smarty->assign("DisplayAdd",FALSE);
1146 $smarty->assign("DisplayDel",TRUE);
1147 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1148 $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1149 return($ret);
1150 }
1153 /* Parse given token identified by $data[$id]
1154 * and return the parsed tokens.
1155 */
1156 function _parse($data,$id = 0)
1157 {
1158 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1159 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1160 $type = $data[$id]['text'];
1161 $tmp = array();
1163 /* Is there an identifier named 'not' to inverse this filter ? */
1164 $Inverse = FALSE;
1165 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1166 $Inverse = TRUE;
1167 $id ++;
1168 $type = $data[$id]['text'];
1169 }
1171 switch($type)
1172 {
1174 /****************
1175 * Parse - Envelope / Header / Address
1176 ****************/
1178 case "envelope" :
1179 case "header":
1180 case "address" :
1181 {
1182 /* Address matches are struckture as follows :
1183 [not]
1184 address
1185 [address-part: tag] all|localpart|domain|user|detail
1186 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1187 [match-type: tag] is|contains|matches|count|value
1188 <header-list: string-list>
1189 <key-list: string-list>
1190 */
1193 $part = "(:all|:localpart|:domain)";
1194 $operator = "(:contains|:is|:matches|:count|:value)";
1195 $value_op = "(lt|le|eq|ge|gt|ne)";
1197 $Address_Part = "";
1198 $Comparator = "";
1199 $Match_type = "";
1200 $Match_type_value = "";
1202 $Key_List = array();
1203 $Value_List = array();
1205 for($i = 0 ; $i < count($data) ; $i ++){
1207 /* Get next node */
1208 $node = $data[$i];
1210 /* Check address part definition */
1211 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1212 $Address_Part = $node['text'];
1213 }
1215 /* Check for match type */
1216 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1217 $Match_type = $node['text'];
1219 /* Get value operator */
1220 if(in_array($Match_type,array(":value",":count"))){
1221 $i ++;
1222 $node = $data[$i];
1224 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1225 $Match_type_value = $node['text'];
1226 }
1227 }
1228 }
1230 /* Check for a comparator */
1231 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1232 $i ++;
1233 $node = $data[$i];
1234 $Comparator = $node['text'];
1235 }
1237 /* Check for Key_List */
1238 elseif(count(sieve_get_strings($data,$i))){
1239 $tmp2 = sieve_get_strings($data,$i);
1240 $i = $tmp2['OFFSET'];
1242 if(!count($Key_List)){
1243 $Key_List = $tmp2['STRINGS'];
1244 }else{
1245 $Value_List = $tmp2['STRINGS'];
1246 }
1247 }
1249 }
1252 /* Add to Tree */
1253 $values = array( "Inverse" => $Inverse,
1254 "Comparator" => $Comparator,
1255 "Expert" => FALSE,
1256 "Match_type" => $Match_type,
1257 "Match_type_value"=> $Match_type_value,
1258 "Key_List" => $Key_List,
1259 "Value_List" => $Value_List) ;
1260 if($type == "address"){
1261 $values["Address_Part"] = $Address_Part;
1262 }
1263 $tmp[$type] = $values;
1264 $tmp[$type]['LastError'] = "";
1265 break;
1266 }
1269 /****************
1270 * Parse - Size
1271 ****************/
1273 case "size":
1274 {
1276 $ops = "(:over|:under)";
1278 $Match_type = "";
1280 for($i = $id ; $i < count($data); $i++){
1282 /* Get current node */
1283 $node = $data[$i];
1285 /* Get tag (under / over) */
1286 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1287 $Match_type = $node['text'];
1288 }
1290 /* Get Value_List, the value that we want to match for */
1291 elseif(count(sieve_get_strings($data,$i))){
1292 $tmp2 = sieve_get_strings($data,$i);
1293 $i = $tmp2['OFFSET'];
1295 $Value_List = $tmp2['STRINGS'];
1296 }
1297 }
1299 $tmp[$type]= array( "Inverse" => $Inverse,
1300 "Match_type" => $Match_type,
1301 "Value_List" => $Value_List);
1302 $tmp[$type]['LastError'] = "";
1303 break;
1304 }
1307 /****************
1308 * Parse - True / False
1309 ****************/
1311 case "true":
1312 {
1313 $tmp['true'] = "true";
1314 $tmp['true']['LastError'] = "";
1315 break;
1316 }
1317 case "false":
1318 {
1319 $tmp['false'] = "false";
1320 $tmp['false']['LastError'] = "";
1321 break;
1322 }
1325 /****************
1326 * Parse - Exists
1327 ****************/
1329 case "exists":
1330 {
1332 /* Skip first values, [if,not,exists] */
1333 $node = $data[$id];
1334 while(in_array($node['text'],array("if","not","exists"))){
1335 $id ++;
1336 $node = $data[$id];
1337 }
1339 /* Get values */
1340 $tmp2 = sieve_get_strings($data,$id);
1343 $tmp['exists'] = array('Inverse' => $Inverse,
1344 'Values' => $tmp2['STRINGS']);
1345 $tmp[$type]['LastError'] = "";
1346 break;
1347 }
1350 /****************
1351 * Parse - Allof
1352 ****************/
1354 case "allof" :
1355 {
1356 /* Get parameter and recursivly call this method
1357 * for each parameter
1358 */
1359 $id ++;
1360 $tmp2 = $this->get_parameter($data,$id);
1362 foreach($tmp2 as $parameter){
1363 $tmp['allof'][] = $this->_parse($parameter);
1364 }
1365 $tmp['allof']['Inverse'] = $Inverse;
1366 break;
1367 }
1370 /****************
1371 * Parse - Anyof
1372 ****************/
1374 case "anyof" :
1375 {
1376 /* Get parameter and recursivly call this method
1377 * for each parameter
1378 */
1379 $id ++;
1380 $tmp2 = $this->get_parameter($data,$id);
1382 foreach($tmp2 as $parameter){
1383 $tmp['anyof'][] = $this->_parse($parameter);
1384 }
1385 $tmp['anyof']['Inverse'] = $Inverse;
1386 break;
1387 }
1388 default : $tmp[$id] = $type;
1389 }
1391 return($tmp);
1392 }
1395 function get_parameter($data,$id)
1396 {
1397 $par = array();
1398 $open_brakets = 0;
1399 $next = NULL;
1400 $num = 0;
1401 for($i = $id ; $i < count($data) ; $i++ ){
1402 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1403 $open_brakets ++;
1404 }
1405 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1406 $num ++;
1407 }
1408 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1409 $par[$num][] = $data[$i];
1410 }
1411 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1412 $open_brakets --;
1413 }
1414 }
1415 return($par);
1416 }
1417 }
1419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1420 ?>