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 ":regex" => _("regex"),
40 ":contains" => _("contains"),
41 ":matches" => _("matches"),
42 ":count" => _("count"),
43 ":value" => _("value is"));
45 /* Operators */
46 $this->operators = array(
47 "lt" => _("less than"),
48 "le" => _("less or equal"),
49 "eq" => _("equals"),
50 "ge" => _("greater or equal"),
51 "gt" => _("greater than"),
52 "ne" => _("not equal"));
54 /* Remove comments from tests */
55 $tmp = array();
56 foreach($elements['ELEMENTS'] as $ele){
57 if($ele['class'] != "comment"){
58 $tmp[] = $ele;
59 }
60 }
61 $elements['ELEMENTS'] = $tmp;
63 $this->object_id = $object_id;
64 if($elements!=NULL){
65 $this->elements = $elements;
66 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
67 }
68 }
71 /* Returns the sieve script for this
72 * if/else tag.
73 */
74 function get_sieve_script_part()
75 {
76 $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
77 return($tmp);
78 }
81 /* Return error msgs */
82 function check()
83 {
84 $check = $this->check_recursive();
85 return($check);
86 }
89 /* Recursivly fetch all error msgs */
90 function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
91 {
92 $ret = array();
93 if($parsed == NULL){
94 $parsed = $this->_parsed;
95 }
97 /* Walk through all elements */
98 foreach($parsed as $key => $data){
100 /* Create elements */
101 switch($key)
102 {
103 /*******************
104 * Allof / Anyof
105 *******************/
106 case "anyof" :
107 case "allof" :
108 {
109 foreach($data as $key2 => $dat){
110 if(($key2 === "Inverse") && ($key2 == "Inverse")){
111 continue;
112 }
113 $msgs = $this->check_recursive($dat, ($id +1),$key2);
115 foreach($msgs as $msg){
116 $ret[] = $msg;
117 }
118 }
119 break;
120 }
122 /*******************
123 * True / False
124 *******************/
126 case "true" :
127 case "false" :
128 {
129 /* Can't fail anyway */
130 break;
131 }
133 /*******************
134 * Default
135 *******************/
137 default:
138 {
139 if(isset($data['LastError']) && !empty($data['LastError'])){
140 $ret[] = $data['LastError'];
141 }
142 }
143 }
144 }
145 return($ret);
146 }
149 /* Recursivly create a sieve script out of the given
150 * tags and tokens provided by $parsed.
151 * $id specifies the depth of the current element.
152 * $obj_id is the current tag-id handled by this function
153 */
154 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
155 {
156 $script ="";
157 if($parsed == NULL){
158 $parsed = $this->_parsed;
159 }
162 if(!is_array($parsed)){
163 return;
164 }
166 /* Walk through all elements */
167 foreach($parsed as $key => $data){
169 /* Create Inverse Tag */
170 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
171 $Inverse = TRUE;
172 }else{
173 $Inverse = FALSE;
174 }
176 /* Create elements */
177 switch($key)
178 {
180 /*******************
181 * True / False
182 *******************/
184 case "true" :
185 case "false" :
186 {
187 /* Invert this test if required */
188 if($Inverse){
189 $script .= "not ";
190 }
191 $script .= $key;
192 break;
193 }
196 /*******************
197 * Address
198 *******************/
200 case "address" :
201 {
202 /* [not] address
203 [address-part: tag]
204 [comparator: tag]
205 [match-type: tag]
206 <header-list: string-list>
207 <key-list: string-list>
208 */
210 /* Invert this test if required */
211 if($Inverse){
212 $script .= "not ";
213 }
215 $script .="address ";
217 /* Add address part tag */
218 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
219 $script .= $data['Address_Part']." ";
220 }
222 /* Add comparator */
223 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
224 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
225 }
227 /* Add match type */
228 $script .= $data['Match_type']." ";
230 /* Add special match type for count and value */
231 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
232 $script .= sieve_create_strings($data['Match_type_value'])." ";
233 }
235 $script .= sieve_create_strings($data['Key_List']);
236 $script .= " ";
237 $script .= sieve_create_strings($data['Value_List']);
238 break;
239 }
242 /*******************
243 * Header
244 *******************/
246 case "header" :
247 {
248 /* [not] header
249 [comparator: tag]
250 [match-type: tag]
251 <header-names: string-list>
252 <key-list: string-list>
253 */
255 /* Invert ? */
256 if($Inverse){
257 $script .= "not ";
258 }
260 $script .="header ";
262 /* Add address part tag */
263 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
264 $script .= $data['Address_Part']." ";
265 }
267 /* Add comparator */
268 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
269 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
270 }
272 /* Add match type */
273 $script .= $data['Match_type']." ";
275 /* Add special match type for count and value */
276 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
277 $script .= sieve_create_strings($data['Match_type_value'])." ";
278 }
280 $script .= sieve_create_strings($data['Key_List']);
281 $script .= " ";
282 $script .= sieve_create_strings($data['Value_List']);
283 break;
284 }
287 /*******************
288 * Envelope
289 *******************/
291 case "envelope" :
292 {
293 /* [not] envelope
294 [address-part: tag]
295 [comparator: tag]
296 [match-type: tag]
297 <envelope-part: string-list>
298 <key-list: string-list>
299 */
301 /* Invert */
302 if($Inverse){
303 $script .= "not ";
304 }
306 $script .="envelope ";
308 /* Add address part tag */
309 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
310 $script .= $data['Address_Part']." ";
311 }
313 /* Add comparator */
314 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
315 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
316 }
318 /* Add match type */
319 $script .= $data['Match_type']." ";
321 /* Add special match type for count and value */
322 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
323 $script .= sieve_create_strings($data['Match_type_value'])." ";
324 }
326 $script .= sieve_create_strings($data['Key_List']);
327 $script .= " ";
328 $script .= sieve_create_strings($data['Value_List']);
329 break;
330 }
333 /*******************
334 * Exists
335 *******************/
336 case "exists" :
337 {
338 /* [not] exists
339 <header-names: string-list>
340 */
342 /* Invert ? */
343 if($Inverse){
344 $script .= "not ";
345 }
347 $script .= "exists ".sieve_create_strings($data['Values']);
348 break;
349 }
352 /*******************
353 * Size
354 *******************/
355 case "size" :
356 {
357 /* [not] size
358 <":over" / ":under">
359 <limit: number>
360 */
362 /* Invert ? */
363 if($Inverse){
364 $script .= "not ";
365 }
367 /* Add size test */
368 $script .="size ";
369 $script .=$data['Match_type']." ";
370 foreach($data['Value_List'] as $val){
371 $script .= $val." ";
372 }
373 break;
374 }
377 /*******************
378 * Allof
379 *******************/
380 case "anyof" :
381 case "allof" :
382 {
383 /* allof <tests: test-list>
384 anyof <tests: test-list> */
387 /* Add spaces, to indent the code.*/
388 $block = "\n";
389 for($i = 0 ; $i < $id ; $i ++){
390 $block .= SIEVE_INDENT_TAB;
391 }
393 /* Add allof/anyof tag */
394 if($Inverse){
395 $script .= "not ";
396 }
397 $script.= $key." ( ";
399 /* Add each test parameter */
400 foreach($data as $key2 => $dat){
401 if(($key2 === "Inverse") && ($key2 == "Inverse")){
402 continue;
403 }
404 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
405 }
407 /* Remove last _,_ and close the tag */
408 $script = preg_replace("/,$/","",trim($script));
409 $script.= $block.")";
410 break ;
411 }
413 default :
414 {
415 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
416 }
417 }
418 }
419 return($script);
420 }
423 function add_test($data,$type)
424 {
425 switch($type)
426 {
427 case "header" :
428 case "address" :
429 case "envelope" :
430 {
431 /* Add to Tree */
432 $values = array( "Inverse" => FALSE,
433 "Comparator" => "",
434 "Expert" => FALSE,
435 "LastError" => "",
436 "Match_type" => ":contains",
437 "Match_type_value"=> "",
438 "Key_List" => array(_("emtpy")),
439 "Value_List" => array(_("empty"))) ;
440 if($type == "address"){
441 $values["Address_Part"] = ":all";
442 }
443 $data[$type]=$values;
445 $this->parent->add_require("relational");
446 if($type == "envelope"){
447 $this->parent->add_require("envelope");
448 }
451 break;
452 }
453 case "allof" :
454 case "anyof" :
455 {
456 $data[$type] = array("Inverse" => FALSE);
457 break;
458 }
459 case "size" :
460 {
461 $tmp= array(
462 "Inverse" => FALSE,
463 "Match_type" => ":over",
464 "Value_List" => array("1M"));
466 $tmp['LastError'] = "";
467 $data[$type] = $tmp;
468 break;
469 }
470 case "true":
471 {
472 $data['true'] = "true";
473 $data['true']['LastError'] = "";
474 break;
475 }
476 case "false":
477 {
478 $data['false'] = "false";
479 $data['false']['LastError'] = "";
480 break;
481 }
482 case "exists" :
483 {
484 $data['exists'] = array('Inverse' => FALSE,
485 'Values' => array(_("Nothing specified right now")),
486 'LastError' => "");
487 break;
488 }
489 default : echo "Still buggy ";exit;
490 }
492 return($data);
493 }
496 /* Ensure that all changes made on the ui
497 * will be saved.
498 */
499 function save_object()
500 {
502 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
503 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
504 }
506 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
507 $this->_parsed = $tmp;
508 }
511 /* Recursivly save all ui changes for the
512 * tags and tokens provided by $parsed.
513 * $id specifies the depth of the current element.
514 * $obj_id is the current tag-id handled by this function
515 */
516 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
517 {
518 /* Variable initialization */
519 $ret ="";
520 if($parsed == NULL){
521 $parsed = $this->_parsed;
522 }
524 if(!is_array($parsed)) return;
526 /* Walk through all elements */
527 foreach($parsed as $key => $data){
529 /* Id used to have unique html names */
530 $element_id = $this->object_id."_".$id."_".$obj_id;
532 foreach($_POST as $name => $value){
533 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
534 return(false);
535 }
536 }
539 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
540 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
541 }
543 /* Create elements */
544 switch($key)
545 {
546 /*******************
547 * Address
548 *******************/
550 case "envelope" :
551 case "header" :
552 case "address" :
553 {
554 /* [not] address
555 [address-part: tag]
556 [comparator: tag]
557 [match-type: tag]
558 <header-list: string-list>
559 <key-list: string-list>
560 */
562 /* Possible address parts we can select */
563 $address_parts = $this->address_parts;
564 $comparators = $this->comparators;
565 $match_types = $this->match_types;
566 $operators = $this->operators;
568 $parsed[$key]['LastError'] = "";
570 /* Toggle Inverse ? */
571 if(isset($_POST['toggle_inverse_'.$element_id])){
572 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
573 }
575 /* Check if we want to toggle the expert mode */
576 if(isset($_POST['Toggle_Expert_'.$element_id])){
577 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
578 }
580 /* Get address part */
581 if(isset($_POST['address_part_'.$element_id])){
582 $ap = $_POST['address_part_'.$element_id];
584 if(!isset($address_parts[$ap])){
585 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
586 }
587 $parsed[$key]['Address_Part'] = $ap;
588 }
590 /* Check if match type has changed */
591 if(isset($_POST['matchtype_'.$element_id])){
592 $mt = $_POST['matchtype_'.$element_id];
594 if(!isset($match_types[$mt])){
595 $parsed[$key]['LastError'] = _("Invalid match type given.");
596 }
597 if($mt == ":regex"){
598 $this->parent->add_require("regex");
599 }
600 $parsed[$key]['Match_type'] = $mt;
601 }
603 /* Get the comparator tag, if posted */
604 if(isset($_POST['comparator_'.$element_id])){
605 $cp = $_POST['comparator_'.$element_id];
607 if(!isset($comparators[$cp])){
608 $parsed[$key]['LastError'] = _("Invalid operator given.");
609 }
610 $parsed[$key]['Comparator'] = $cp;
612 if($cp == "i;ascii-numeric"){
613 $this->parent->add_require("comparator-i;ascii-numeric");
614 }
615 }
617 /* In case of :count and :value match types
618 * we have a special match operator we should save.
619 */
620 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
621 if(isset($_POST['operator_'.$element_id])){
622 $op = $_POST['operator_'.$element_id];
624 if(!isset($operators[$op])){
625 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
626 }
627 $parsed[$key]['Match_type_value'] = $op;
628 }
629 }
631 /* Get the address fields we should check, they are seperated by , */
632 if(isset($_POST['keys_'.$element_id])){
633 $vls = stripslashes($_POST['keys_'.$element_id]);
634 $tmp = array();
636 $tmp2 = split(",",$vls);
637 foreach($tmp2 as $val){
638 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
639 }
640 $parsed[$key]['Key_List'] = $tmp;
641 }
643 /* Get the values should check for, they are seperated by , */
644 if(isset($_POST['values_'.$element_id])){
645 $vls = stripslashes($_POST['values_'.$element_id]);
646 $tmp = array();
648 $tmp2 = split(",",$vls);
649 foreach($tmp2 as $val){
650 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
651 }
652 $parsed[$key]['Value_List'] = $tmp;
653 }
654 break;
655 }
657 /*******************
658 * TRUE FALSE
659 *******************/
661 case "true" :
662 case "false" :
663 {
664 $name = 'boolean_'.$element_id;
665 if(isset($_POST[$name])){
666 $key2 = $_POST[$name];
668 if($key != $key2) {
669 $parsed = array($key2 => $key2);
670 }
671 }
672 break;
673 }
675 /*******************
676 * Exists
677 *******************/
679 case "exists" :
680 {
681 /* Toggle Inverse ? */
682 if(isset($_POST['toggle_inverse_'.$element_id])){
683 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
684 }
686 /* get list of match values */
687 if(isset($_POST['Values_'.$element_id])){
688 $vls = stripslashes($_POST['Values_'.$element_id]);
689 $tmp = array();
691 $tmp2 = split(",",$vls);
692 foreach($tmp2 as $val){
693 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
694 }
695 $parsed['exists']['Values'] = $tmp;
696 }
697 break;
698 }
700 /*******************
701 * Size
702 *******************/
704 case "size" :
705 {
706 $Match_types = array( ":over" => _("greater than") ,
707 ":under" => _("lower than"));
709 $Units = array( "M" => _("Megabyte"),
710 "K" => _("Kilobyte"),
711 "" => _("Bytes"));
713 /* Toggle Inverse ? */
714 if(isset($_POST['toggle_inverse_'.$element_id])){
715 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
716 }
718 /* Reset error */
719 $parsed[$key]['LastError'] ="";
721 /* Get match type */
722 if(isset($_POST['Match_type_'.$element_id])){
723 $mt = $_POST['Match_type_'.$element_id];
724 if(!isset($Match_types[$mt])){
725 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
726 }
727 $parsed[$key]['Match_type'] = $mt;
728 }
730 /* Get old values */
731 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
732 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
734 /* Get value */
735 if(isset($_POST['Value_'.$element_id])){
736 $vl = $_POST['Value_'.$element_id];
738 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
739 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
740 }
741 $value = preg_replace("/[^0-9]/","",$vl);
742 }
744 /* Get unit */
745 if(isset($_POST['Value_Unit_'.$element_id])){
746 $ut = $_POST['Value_Unit_'.$element_id];
748 if(!isset($Units[$ut])){
749 $parsed[$key]['LastError'] = _("No valid unit selected");
750 }
751 $unit = $ut;
752 }
753 $parsed[$key]['Value_List'] = array();
754 $parsed[$key]['Value_List'][0] = $value.$unit;
755 break;
756 }
758 /*******************
759 * Allof
760 *******************/
762 case "allof" :
763 {
764 if(isset($_POST['toggle_inverse_'.$element_id])){
765 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
766 }
767 foreach($data as $key2 => $dat){
768 if(($key2 === "Inverse") && ($key2 == "Inverse")){
769 continue;
770 }
771 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
772 if($tmp_data != false){
773 $parsed[$key][$key2] = $tmp_data;
774 }else{
775 unset( $parsed[$key][$key2]);
776 }
777 }
778 break ;
779 }
781 /*******************
782 * Anyof
783 *******************/
785 case "anyof" :
786 {
787 if(isset($_POST['toggle_inverse_'.$element_id])){
788 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
789 }
790 foreach($data as $key2 => $dat){
791 if(($key2 === "Inverse") && ($key2 == "Inverse")){
792 continue;
793 }
794 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
795 if($tmp_data != false){
796 $parsed[$key][$key2] = $tmp_data;
797 }else{
798 unset( $parsed[$key][$key2]);
799 }
800 }
801 break ;
802 }
803 }
804 }
805 return($parsed);
806 }
809 /* Return html element for IF */
810 function execute()
811 {
812 /* Create title */
813 $name = "<img alt='' src='images/small_filter.png' class='center'>";
814 $name .= "<b>"._("Condition")."</b>";
815 if($this->TYPE == "if"){
816 $name .= " - "._("If");
817 }elseif($this->TYPE == "elsif"){
818 $name .= " - "._("Else if");
819 }else{
820 $name .= " - "._("Else");
821 }
823 $smarty = get_smarty();
824 $smarty->assign("ID", $this->object_id);
826 /* Only display navigation elements if necessary */
827 if($this->TYPE == "if"){
828 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
829 }else{
830 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
831 }
833 $smarty->assign("Name", $name);
834 $smarty->assign("Contents", $this->get_as_html());
836 if($this->TYPE == "if"){
837 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
838 }else{
839 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
840 }
841 $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
842 return($str);
843 }
846 /* Returns all elements as html */
847 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
848 {
849 $ret ="";
850 if($parsed == NULL){
851 $parsed = $this->_parsed;
852 }
854 if((!is_array($parsed)) || !count($parsed)) {
855 $smarty = get_smarty();
856 $smarty->assign("ID",$this->object_id);
857 $smarty->assign("DisplayAdd",TRUE);
858 $smarty->assign("DisplayDel",FALSE);
859 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
860 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
861 return($ret);
862 }
864 /* Walk through all elements */
865 foreach($parsed as $key => $data){
867 /* Create Inverse Tag */
868 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
869 $Inverse = TRUE;
870 }else{
871 $Inverse = FALSE;
872 }
874 /* Id used to have unique html names */
875 $element_id = $this->object_id."_".$id."_".$obj_id;
877 /* Create elements */
878 switch($key)
879 {
881 /*******************
882 * TRUE FALSE
883 *******************/
885 case "true" :
886 case "false" :
887 {
888 /* Inverse element if required */
889 if($Inverse){
890 if($key == "true"){
891 $key = "false";
892 }else{
893 $key = "true";
894 }
895 }
897 /* Get template */
898 $smarty = get_smarty();
899 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
900 $smarty->assign("selected" , $key);
901 $smarty->assign("ID" , $element_id);
902 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
903 break;
904 }
907 /*******************
908 * Header
909 *******************/
911 case "header":
912 {
913 $address_parts = $this->address_parts;
914 $comparators = $this->comparators;
915 $match_types = $this->match_types;
916 $operators = $this->operators;
918 $smarty = get_smarty();
919 $smarty->assign("comparators",$comparators);
920 $smarty->assign("match_types",$match_types);
921 $smarty->assign("operators",$operators);
922 $smarty->assign("LastError",$data['LastError']);
923 $smarty->assign("match_type", $data['Match_type']);
924 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
925 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
927 $keys = "";
928 foreach($data['Key_List'] as $key){
929 $keys .= $key.", ";
930 }
931 $keys = preg_replace("/,$/","",trim($keys));
933 $values = "";
934 foreach($data['Value_List'] as $key){
935 $values .= $key.", ";
936 }
937 $values = preg_replace("/,$/","",trim($values));
939 $smarty->assign("keys",$keys);
940 $smarty->assign("Inverse",$Inverse);
941 $smarty->assign("values",$values);
942 $smarty->assign("Expert", $data['Expert']);
944 $smarty->assign("ID" , $element_id);
945 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
946 break;
947 }
950 /*******************
951 * Envelope
952 *******************/
954 case "envelope":
955 {
956 $address_parts = $this->address_parts;
957 $comparators = $this->comparators;
958 $match_types = $this->match_types;
959 $operators = $this->operators;
961 $smarty = get_smarty();
962 $smarty->assign("Inverse",$Inverse);
963 $smarty->assign("comparators",$comparators);
964 $smarty->assign("Expert", $data['Expert']);
965 $smarty->assign("match_types",$match_types);
966 $smarty->assign("operators",$operators);
967 $smarty->assign("LastError",$data['LastError']);
968 $smarty->assign("match_type", $data['Match_type']);
969 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
970 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
972 $keys = "";
973 foreach($data['Key_List'] as $key){
974 $keys .= $key.", ";
975 }
976 $keys = preg_replace("/,$/","",trim($keys));
978 $values = "";
979 foreach($data['Value_List'] as $key){
980 $values .= $key.", ";
981 }
982 $values = preg_replace("/,$/","",trim($values));
983 $smarty->assign("keys",$keys);
984 $smarty->assign("values",$values);
986 $smarty->assign("ID" , $element_id);
987 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
988 break;
989 }
992 /*******************
993 * Address
994 *******************/
996 case "address" :
997 {
998 $address_parts = $this->address_parts;
999 $comparators = $this->comparators;
1000 $match_types = $this->match_types;
1001 $operators = $this->operators;
1003 $smarty = get_smarty();
1004 $smarty->assign("Inverse",$Inverse);
1005 $smarty->assign("address_parts",$address_parts);
1006 $smarty->assign("comparators",$comparators);
1007 $smarty->assign("match_types",$match_types);
1008 $smarty->assign("LastError",$data['LastError']);
1009 $smarty->assign("operators",$operators);
1010 $smarty->assign("match_type", $data['Match_type']);
1011 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
1012 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
1013 $smarty->assign("address_part", $data['Address_Part']);
1014 $smarty->assign("Expert", $data['Expert']);
1016 $keys = "";
1017 foreach($data['Key_List'] as $key){
1018 $keys .= $key.", ";
1019 }
1020 $keys = preg_replace("/,$/","",trim($keys));
1022 $values = "";
1023 foreach($data['Value_List'] as $key){
1024 $values .= $key.", ";
1025 }
1026 $values = preg_replace("/,$/","",trim($values));
1028 $smarty->assign("keys",$keys);
1029 $smarty->assign("values", $values);
1030 $smarty->assign("ID" , $element_id);
1031 $str = $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1032 $ret .= $str;
1033 break;
1034 }
1037 /*******************
1038 * Size
1039 *******************/
1041 case "size" :
1042 {
1043 $Match_types = array( ":over" => _("greater than") ,
1044 ":under" => _("lower than"));
1046 $Units = array( "M" => _("Megabyte"),
1047 "K" => _("Kilobyte"),
1048 "" => _("Bytes"));
1050 $Match_type = $data['Match_type'];
1051 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1052 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1054 $LastError = "";
1055 if(isset($data['LastError'])){
1056 $LastError = $data['LastError'];
1057 }
1059 $smarty = get_smarty();
1060 $smarty->assign("Inverse",$Inverse);
1061 $smarty->assign("LastError",$LastError);
1062 $smarty->assign("Match_types",$Match_types);
1063 $smarty->assign("Units",$Units);
1064 $smarty->assign("Match_type",$Match_type);
1065 $smarty->assign("Value",$Value);
1066 $smarty->assign("Value_Unit",$Value_Unit);
1067 $smarty->assign("ID" , $element_id);
1068 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1069 break;
1070 }
1072 /*******************
1073 * Exists
1074 *******************/
1076 case "exists" :
1077 {
1078 $LastError = "";
1079 if(isset($data['LastError'])){
1080 $LastError = $data['LastError'];
1081 }
1083 $Values = "";
1084 foreach($data['Values'] as $val){
1085 $Values .= $val.", ";
1086 }
1087 $Values = preg_replace("/,$/","",trim($Values));
1089 $smarty = get_smarty();
1090 $smarty->assign("LastError",$LastError);
1091 $smarty->assign("Values",$Values);
1092 $smarty->assign("Inverse",$Inverse);
1093 $smarty->assign("ID" , $element_id);
1094 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1095 break;
1096 }
1099 /*******************
1100 * All of
1101 *******************/
1103 case "allof" :
1104 {
1105 $Contents = "";
1106 foreach($data as $key => $dat){
1107 if(($key === "Inverse") && ($key == "Inverse")){
1108 continue;
1109 }
1110 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1111 }
1113 $smarty = get_smarty();
1114 $smarty->assign("ID" , $element_id);
1115 $smarty->assign("DisplayAdd",TRUE);
1116 $smarty->assign("DisplayDel",FALSE);
1117 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1118 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1120 $smarty->assign("Inverse",$Inverse);
1121 $smarty->assign("Contents",$cont_tmp.$Contents);
1122 $smarty->assign("ID" , $element_id);
1123 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1125 $ret = $allof_tmp;
1126 break ;
1127 }
1130 /*******************
1131 * Any of
1132 *******************/
1134 case "anyof" :
1135 {
1136 $Contents = "";
1137 foreach($data as $key => $dat){
1138 if(($key === "Inverse") && ($key == "Inverse")){
1139 continue;
1140 }
1141 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1142 }
1143 $smarty = get_smarty();
1144 $smarty->assign("ID" , $element_id);
1145 $smarty->assign("DisplayAdd",TRUE);
1146 $smarty->assign("DisplayDel",FALSE);
1147 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1148 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1150 $smarty->assign("Inverse",$Inverse);
1151 $smarty->assign("Contents",$cont_tmp.$Contents);
1152 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1154 $ret = $allof_tmp;
1156 break ;
1157 }
1158 default :
1159 {
1160 trigger_error(_("Unhandled switch type"));
1161 }
1162 }
1163 }
1165 if(!isset($smarty)){
1166 $smarty =get_smarty();
1167 }
1169 $smarty->assign("ID",$element_id);
1170 $smarty->assign("DisplayAdd",FALSE);
1171 $smarty->assign("DisplayDel",TRUE);
1172 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1173 $ret = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($ret,"\\"),$str);
1174 return($ret);
1175 }
1178 /* Parse given token identified by $data[$id]
1179 * and return the parsed tokens.
1180 */
1181 function _parse($data,$id = 0)
1182 {
1183 $av_match_type = array();
1184 foreach($this->match_types as $name => $description){
1185 $av_match_type[] = $name;
1186 }
1187 $av_match_type[] = ":over";
1188 $av_match_type[] = ":under";
1192 $av_methods= array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1193 $type = $data[$id]['text'];
1194 $tmp = array();
1196 /* Is there an identifier named 'not' to inverse this filter ? */
1197 $Inverse = FALSE;
1198 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1199 $Inverse = TRUE;
1200 $id ++;
1201 $type = $data[$id]['text'];
1202 }
1204 switch($type)
1205 {
1207 /****************
1208 * Parse - Envelope / Header / Address
1209 ****************/
1211 case "envelope" :
1212 case "header":
1213 case "address" :
1214 {
1215 /* Address matches are struckture as follows :
1216 [not]
1217 address
1218 [address-part: tag] all|localpart|domain|user|detail
1219 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1220 [match-type: tag] is|contains|matches|count|value
1221 <header-list: string-list>
1222 <key-list: string-list>
1223 */
1226 $part = "(:all|:localpart|:domain)";
1227 $operator = "(:regex|:contains|:is|:matches|:count|:value)";
1228 $value_op = "(lt|le|eq|ge|gt|ne)";
1230 $Address_Part = "";
1231 $Comparator = "";
1232 $Match_type = "";
1233 $Match_type_value = "";
1235 $Key_List = array();
1236 $Value_List = array();
1238 for($i = 0 ; $i < count($data) ; $i ++){
1240 /* Get next node */
1241 $node = $data[$i];
1243 /* Check address part definition */
1244 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1245 $Address_Part = $node['text'];
1246 }
1248 /* Check for match type */
1249 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1250 $Match_type = $node['text'];
1252 /* Get value operator */
1253 if(in_array($Match_type,array(":value",":count"))){
1254 $i ++;
1255 $node = $data[$i];
1257 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1258 $Match_type_value = $node['text'];
1259 }
1260 }
1261 }
1263 /* Check for a comparator */
1264 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1265 $i ++;
1266 $node = $data[$i];
1267 $Comparator = $node['text'];
1268 }
1270 /* Check for Key_List */
1271 elseif(count(sieve_get_strings($data,$i))){
1272 $tmp2 = sieve_get_strings($data,$i);
1273 $i = $tmp2['OFFSET'];
1275 if(!count($Key_List)){
1276 $Key_List = $tmp2['STRINGS'];
1277 }else{
1278 $Value_List = $tmp2['STRINGS'];
1279 }
1280 }
1282 }
1285 /* Add to Tree */
1286 $values = array( "Inverse" => $Inverse,
1287 "Comparator" => $Comparator,
1288 "Expert" => FALSE,
1289 "Match_type" => $Match_type,
1290 "Match_type_value"=> $Match_type_value,
1291 "Key_List" => $Key_List,
1292 "Value_List" => $Value_List) ;
1293 if($type == "address"){
1294 $values["Address_Part"] = $Address_Part;
1295 }
1296 $tmp[$type] = $values;
1297 $tmp[$type]['LastError'] = "";
1298 break;
1299 }
1302 /****************
1303 * Parse - Size
1304 ****************/
1306 case "size":
1307 {
1309 $ops = "(:over|:under)";
1311 $Match_type = "";
1313 for($i = $id ; $i < count($data); $i++){
1315 /* Get current node */
1316 $node = $data[$i];
1318 /* Get tag (under / over) */
1319 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1320 $Match_type = $node['text'];
1321 }
1323 /* Get Value_List, the value that we want to match for */
1324 elseif(count(sieve_get_strings($data,$i))){
1325 $tmp2 = sieve_get_strings($data,$i);
1326 $i = $tmp2['OFFSET'];
1328 $Value_List = $tmp2['STRINGS'];
1329 }
1330 }
1332 $tmp[$type]= array( "Inverse" => $Inverse,
1333 "Match_type" => $Match_type,
1334 "Value_List" => $Value_List);
1335 $tmp[$type]['LastError'] = "";
1336 break;
1337 }
1340 /****************
1341 * Parse - True / False
1342 ****************/
1344 case "true":
1345 {
1346 $tmp['true'] = "true";
1347 $tmp['true']['LastError'] = "";
1348 break;
1349 }
1350 case "false":
1351 {
1352 $tmp['false'] = "false";
1353 $tmp['false']['LastError'] = "";
1354 break;
1355 }
1358 /****************
1359 * Parse - Exists
1360 ****************/
1362 case "exists":
1363 {
1365 /* Skip first values, [if,not,exists] */
1366 $node = $data[$id];
1367 while(in_array($node['text'],array("if","not","exists"))){
1368 $id ++;
1369 $node = $data[$id];
1370 }
1372 /* Get values */
1373 $tmp2 = sieve_get_strings($data,$id);
1376 $tmp['exists'] = array('Inverse' => $Inverse,
1377 'Values' => $tmp2['STRINGS']);
1378 $tmp[$type]['LastError'] = "";
1379 break;
1380 }
1383 /****************
1384 * Parse - Allof
1385 ****************/
1387 case "allof" :
1388 {
1389 /* Get parameter and recursivly call this method
1390 * for each parameter
1391 */
1392 $id ++;
1393 $tmp2 = $this->get_parameter($data,$id);
1395 foreach($tmp2 as $parameter){
1396 $tmp['allof'][] = $this->_parse($parameter);
1397 }
1398 $tmp['allof']['Inverse'] = $Inverse;
1399 break;
1400 }
1403 /****************
1404 * Parse - Anyof
1405 ****************/
1407 case "anyof" :
1408 {
1409 /* Get parameter and recursivly call this method
1410 * for each parameter
1411 */
1412 $id ++;
1413 $tmp2 = $this->get_parameter($data,$id);
1415 foreach($tmp2 as $parameter){
1416 $tmp['anyof'][] = $this->_parse($parameter);
1417 }
1418 $tmp['anyof']['Inverse'] = $Inverse;
1419 break;
1420 }
1421 default : $tmp[$id] = $type;
1422 }
1424 return($tmp);
1425 }
1428 function get_parameter($data,$id)
1429 {
1430 $par = array();
1431 $open_brakets = 0;
1432 $next = NULL;
1433 $num = 0;
1434 for($i = $id ; $i < count($data) ; $i++ ){
1435 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1436 $open_brakets ++;
1437 }
1438 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1439 $num ++;
1440 }
1441 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1442 $par[$num][] = $data[$i];
1443 }
1444 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1445 $open_brakets --;
1446 }
1447 }
1448 return($par);
1449 }
1450 }
1452 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1453 ?>