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 "fasle" :
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 if($type == "envelope"){
433 $this->parent->add_require("envelope");
434 }
437 break;
438 }
439 case "allof" :
440 case "anyof" :
441 {
442 $data[$type] = array("Inverse" => FALSE);
443 break;
444 }
445 case "size" :
446 {
447 $tmp= array(
448 "Inverse" => FALSE,
449 "Match_type" => ":over",
450 "Value_List" => array(1,"M"));
452 $tmp['LastError'] = "";
453 $data[$type] = $tmp;
454 break;
455 }
456 case "true":
457 {
458 $data['true'] = "true";
459 $data['true']['LastError'] = "";
460 break;
461 }
462 case "false":
463 {
464 $data['false'] = "false";
465 $data['false']['LastError'] = "";
466 break;
467 }
468 case "exists" :
469 {
470 $data['exists'] = array('Inverse' => FALSE,
471 'Values' => array(_("Nothing specified right now")),
472 'LastError' => "");
473 break;
474 }
475 default : echo "Still buggy ";exit;
476 }
478 return($data);
479 }
482 /* Ensure that all changes made on the ui
483 * will be saved.
484 */
485 function save_object()
486 {
488 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
489 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
490 }
492 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
493 $this->_parsed = $tmp;
494 }
497 /* Recursivly save all ui changes for the
498 * tags and tokens provided by $parsed.
499 * $id specifies the depth of the current element.
500 * $obj_id is the current tag-id handled by this function
501 */
502 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
503 {
504 /* Variable initialization */
505 $ret ="";
506 if($parsed == NULL){
507 $parsed = $this->_parsed;
508 }
510 if(!is_array($parsed)) return;
512 /* Walk through all elements */
513 foreach($parsed as $key => $data){
515 /* Id used to have unique html names */
516 $element_id = $this->object_id."_".$id."_".$obj_id;
518 foreach($_POST as $name => $value){
519 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
520 return(false);
521 }
522 }
525 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
526 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
527 }
529 /* Create elements */
530 switch($key)
531 {
532 /*******************
533 * Address
534 *******************/
536 case "envelope" :
537 case "header" :
538 case "address" :
539 {
540 /* [not] address
541 [address-part: tag]
542 [comparator: tag]
543 [match-type: tag]
544 <header-list: string-list>
545 <key-list: string-list>
546 */
548 /* Possible address parts we can select */
549 $address_parts = $this->address_parts;
550 $comparators = $this->comparators;
551 $match_types = $this->match_types;
552 $operators = $this->operators;
554 $parsed[$key]['LastError'] = "";
556 /* Toggle Inverse ? */
557 if(isset($_POST['toggle_inverse_'.$element_id])){
558 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
559 }
561 /* Check if we want to toggle the expert mode */
562 if(isset($_POST['Toggle_Expert_'.$element_id])){
563 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
564 }
566 /* Get address part */
567 if(isset($_POST['address_part_'.$element_id])){
568 $ap = $_POST['address_part_'.$element_id];
570 if(!isset($address_parts[$ap])){
571 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
572 }
573 $parsed[$key]['Address_Part'] = $ap;
574 }
576 /* Check if match type has changed */
577 if(isset($_POST['matchtype_'.$element_id])){
578 $mt = $_POST['matchtype_'.$element_id];
580 if(!isset($match_types[$mt])){
581 $parsed[$key]['LastError'] = _("Invalid match type given.");
582 }
583 $parsed[$key]['Match_type'] = $mt;
584 }
586 /* Get the comparator tag, if posted */
587 if(isset($_POST['comparator_'.$element_id])){
588 $cp = $_POST['comparator_'.$element_id];
590 if(!isset($comparators[$cp])){
591 $parsed[$key]['LastError'] = _("Invalid operator given.");
592 }
593 $parsed[$key]['Comparator'] = $cp;
595 if($cp == "i;ascii-numeric"){
596 $this->parent->add_require("comparator-i;ascii-numeric");
597 }
598 }
600 /* In case of :count and :value match types
601 * we have a special match operator we should save.
602 */
603 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
604 if(isset($_POST['operator_'.$element_id])){
605 $op = $_POST['operator_'.$element_id];
607 if(!isset($operators[$op])){
608 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
609 }
610 $parsed[$key]['Match_type_value'] = $op;
611 }
612 }
614 /* Get the address fields we should check, they are seperated by , */
615 if(isset($_POST['keys_'.$element_id])){
616 $vls = stripslashes($_POST['keys_'.$element_id]);
617 $tmp = array();
619 $tmp2 = split(",",$vls);
620 foreach($tmp2 as $val){
621 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
622 }
623 $parsed[$key]['Key_List'] = $tmp;
624 }
626 /* Get the values should check for, they are seperated by , */
627 if(isset($_POST['values_'.$element_id])){
628 $vls = stripslashes($_POST['values_'.$element_id]);
629 $tmp = array();
631 $tmp2 = split(",",$vls);
632 foreach($tmp2 as $val){
633 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
634 }
635 $parsed[$key]['Value_List'] = $tmp;
636 }
637 break;
638 }
640 /*******************
641 * TRUE FALSE
642 *******************/
644 case "true" :
645 case "false" :
646 {
647 $name = 'boolean_'.$element_id;
648 if(isset($_POST[$name])){
649 $key2 = $_POST[$name];
651 if($key != $key2) {
652 $parsed = array($key2 => $key2);
653 }
654 }
655 break;
656 }
658 /*******************
659 * Exists
660 *******************/
662 case "exists" :
663 {
664 /* Toggle Inverse ? */
665 if(isset($_POST['toggle_inverse_'.$element_id])){
666 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
667 }
669 /* get list of match values */
670 if(isset($_POST['Values_'.$element_id])){
671 $vls = stripslashes($_POST['Values_'.$element_id]);
672 $tmp = array();
674 $tmp2 = split(",",$vls);
675 foreach($tmp2 as $val){
676 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
677 }
678 $parsed['exists']['Values'] = $tmp;
679 }
680 break;
681 }
683 /*******************
684 * Size
685 *******************/
687 case "size" :
688 {
689 $Match_types = array( ":over" => _("greater than") ,
690 ":under" => _("lower than"));
692 $Units = array( "M" => _("Megabyte") ,
693 "K" => _("Kilobyte"));
695 /* Reset error */
696 $parsed[$key]['LastError'] ="";
698 /* Get match type */
699 if(isset($_POST['Match_type_'.$element_id])){
700 $mt = $_POST['Match_type_'.$element_id];
701 if(!isset($Match_types[$mt])){
702 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
703 }
704 $parsed[$key]['Match_type'] = $mt;
705 }
707 /* Get old values */
708 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
709 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
711 /* Get value */
712 if(isset($_POST['Value_'.$element_id])){
713 $vl = $_POST['Value_'.$element_id];
715 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
716 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
717 }
718 $value = preg_replace("/[^0-9]/","",$vl);
719 }
721 /* Get unit */
722 if(isset($_POST['Value_Unit_'.$element_id])){
723 $ut = $_POST['Value_Unit_'.$element_id];
725 if(!isset($Units[$ut])){
726 $parsed[$key]['LastError'] = _("No valid unit selected");
727 }
728 $unit = $ut;
729 }
730 $parsed[$key]['Value_List'] = array();
731 $parsed[$key]['Value_List'][0] = $value.$unit;
732 break;
733 }
735 /*******************
736 * Allof
737 *******************/
739 case "allof" :
740 {
741 if(isset($_POST['toggle_inverse_'.$element_id])){
742 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
743 }
744 foreach($data as $key2 => $dat){
745 if(($key2 === "Inverse") && ($key2 == "Inverse")){
746 continue;
747 }
748 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
749 if($tmp_data != false){
750 $parsed[$key][$key2] = $tmp_data;
751 }else{
752 unset( $parsed[$key][$key2]);
753 }
754 }
755 break ;
756 }
758 /*******************
759 * Anyof
760 *******************/
762 case "anyof" :
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 }
780 }
781 }
782 return($parsed);
783 }
786 /* Return html element for IF */
787 function execute()
788 {
789 /* Create title */
790 $name = "<img alt='' src='images/small_filter.png' class='center'>";
791 $name .= "<b>"._("Condition")."</b>";
792 if($this->TYPE == "if"){
793 $name .= " - "._("If");
794 }elseif($this->TYPE == "elsif"){
795 $name .= " - "._("Else if");
796 }else{
797 $name .= " - "._("Else");
798 }
800 $smarty = get_smarty();
801 $smarty->assign("ID", $this->object_id);
803 /* Only display navigation elements if necessary */
804 if($this->TYPE == "if"){
805 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
806 }else{
807 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
808 }
810 $smarty->assign("Name", $name);
811 $smarty->assign("Contents", $this->get_as_html());
813 if($this->TYPE == "if"){
814 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
815 }else{
816 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
817 }
818 $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
819 return($str);
820 }
823 /* Returns all elements as html */
824 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
825 {
826 $ret ="";
827 if($parsed == NULL){
828 $parsed = $this->_parsed;
829 }
831 if((!is_array($parsed)) || !count($parsed)) {
832 $smarty = get_smarty();
833 $smarty->assign("ID",$this->object_id);
834 $smarty->assign("DisplayAdd",TRUE);
835 $smarty->assign("DisplayDel",FALSE);
836 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
837 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
838 return($ret);
839 }
841 /* Walk through all elements */
842 foreach($parsed as $key => $data){
844 /* Create Inverse Tag */
845 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
846 $Inverse = TRUE;
847 }else{
848 $Inverse = FALSE;
849 }
851 /* Id used to have unique html names */
852 $element_id = $this->object_id."_".$id."_".$obj_id;
854 /* Create elements */
855 switch($key)
856 {
858 /*******************
859 * TRUE FALSE
860 *******************/
862 case "true" :
863 case "false" :
864 {
865 /* Inverse element if required */
866 if($Inverse){
867 if($key == "true"){
868 $key = "false";
869 }else{
870 $key = "true";
871 }
872 }
874 /* Get template */
875 $smarty = get_smarty();
876 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
877 $smarty->assign("selected" , $key);
878 $smarty->assign("ID" , $element_id);
879 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
880 break;
881 }
884 /*******************
885 * Header
886 *******************/
888 case "header":
889 {
890 $address_parts = $this->address_parts;
891 $comparators = $this->comparators;
892 $match_types = $this->match_types;
893 $operators = $this->operators;
895 $smarty = get_smarty();
896 $smarty->assign("comparators",$comparators);
897 $smarty->assign("match_types",$match_types);
898 $smarty->assign("operators",$operators);
899 $smarty->assign("LastError",$data['LastError']);
900 $smarty->assign("match_type", $data['Match_type']);
901 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
902 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
904 $keys = "";
905 foreach($data['Key_List'] as $key){
906 $keys .= $key.", ";
907 }
908 $keys = preg_replace("/,$/","",trim($keys));
910 $values = "";
911 foreach($data['Value_List'] as $key){
912 $values .= $key.", ";
913 }
914 $values = preg_replace("/,$/","",trim($values));
916 $smarty->assign("keys",$keys);
917 $smarty->assign("Inverse",$Inverse);
918 $smarty->assign("values",$values);
919 $smarty->assign("Expert", $data['Expert']);
921 $smarty->assign("ID" , $element_id);
922 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
923 break;
924 }
927 /*******************
928 * Envelope
929 *******************/
931 case "envelope":
932 {
933 $address_parts = $this->address_parts;
934 $comparators = $this->comparators;
935 $match_types = $this->match_types;
936 $operators = $this->operators;
938 $smarty = get_smarty();
939 $smarty->assign("Inverse",$Inverse);
940 $smarty->assign("comparators",$comparators);
941 $smarty->assign("Expert", $data['Expert']);
942 $smarty->assign("match_types",$match_types);
943 $smarty->assign("operators",$operators);
944 $smarty->assign("LastError",$data['LastError']);
945 $smarty->assign("match_type", $data['Match_type']);
946 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
947 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
949 $keys = "";
950 foreach($data['Key_List'] as $key){
951 $keys .= $key.", ";
952 }
953 $keys = preg_replace("/,$/","",trim($keys));
955 $values = "";
956 foreach($data['Value_List'] as $key){
957 $values .= $key.", ";
958 }
959 $values = preg_replace("/,$/","",trim($values));
960 $smarty->assign("keys",$keys);
961 $smarty->assign("values",$values);
963 $smarty->assign("ID" , $element_id);
964 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
965 break;
966 }
969 /*******************
970 * Address
971 *******************/
973 case "address" :
974 {
975 $address_parts = $this->address_parts;
976 $comparators = $this->comparators;
977 $match_types = $this->match_types;
978 $operators = $this->operators;
980 $smarty = get_smarty();
981 $smarty->assign("Inverse",$Inverse);
982 $smarty->assign("address_parts",$address_parts);
983 $smarty->assign("comparators",$comparators);
984 $smarty->assign("match_types",$match_types);
985 $smarty->assign("LastError",$data['LastError']);
986 $smarty->assign("operators",$operators);
987 $smarty->assign("match_type", $data['Match_type']);
988 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
989 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
990 $smarty->assign("address_part", $data['Address_Part']);
991 $smarty->assign("Expert", $data['Expert']);
993 $keys = "";
994 foreach($data['Key_List'] as $key){
995 $keys .= $key.", ";
996 }
997 $keys = preg_replace("/,$/","",trim($keys));
999 $values = "";
1000 foreach($data['Value_List'] as $key){
1001 $values .= $key.", ";
1002 }
1003 $values = preg_replace("/,$/","",trim($values));
1004 $smarty->assign("keys",$keys);
1005 $smarty->assign("values",$values);
1006 $smarty->assign("ID" , $element_id);
1007 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1008 break;
1009 }
1012 /*******************
1013 * Size
1014 *******************/
1016 case "size" :
1017 {
1018 $Match_types = array( ":over" => _("greater than") ,
1019 ":under" => _("lower than"));
1021 $Units = array( "M" => _("Megabyte") ,
1022 "K" => _("Kilobyte"));
1024 $Match_type = $data['Match_type'];
1025 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1026 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1028 $LastError = "";
1029 if(isset($data['LastError'])){
1030 $LastError = $data['LastError'];
1031 }
1033 $smarty = get_smarty();
1034 $smarty->assign("Inverse",$Inverse);
1035 $smarty->assign("LastError",$LastError);
1036 $smarty->assign("Match_types",$Match_types);
1037 $smarty->assign("Units",$Units);
1038 $smarty->assign("Match_type",$Match_type);
1039 $smarty->assign("Value",$Value);
1040 $smarty->assign("Value_Unit",$Value_Unit);
1041 $smarty->assign("ID" , $element_id);
1042 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1043 break;
1044 }
1046 /*******************
1047 * Exists
1048 *******************/
1050 case "exists" :
1051 {
1052 $LastError = "";
1053 if(isset($data['LastError'])){
1054 $LastError = $data['LastError'];
1055 }
1057 $Values = "";
1058 foreach($data['Values'] as $val){
1059 $Values .= $val.", ";
1060 }
1061 $Values = preg_replace("/,$/","",trim($Values));
1063 $smarty = get_smarty();
1064 $smarty->assign("LastError",$LastError);
1065 $smarty->assign("Values",$Values);
1066 $smarty->assign("Inverse",$Inverse);
1067 $smarty->assign("ID" , $element_id);
1068 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1069 break;
1070 }
1073 /*******************
1074 * All of
1075 *******************/
1077 case "allof" :
1078 {
1079 $Contents = "";
1080 foreach($data as $key => $dat){
1081 if(($key === "Inverse") && ($key == "Inverse")){
1082 continue;
1083 }
1084 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1085 }
1087 $smarty = get_smarty();
1088 $smarty->assign("ID" , $element_id);
1089 $smarty->assign("DisplayAdd",TRUE);
1090 $smarty->assign("DisplayDel",FALSE);
1091 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1092 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1094 $smarty->assign("Inverse",$Inverse);
1095 $smarty->assign("Contents",$cont_tmp.$Contents);
1096 $smarty->assign("ID" , $element_id);
1097 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1099 $ret = $allof_tmp;
1100 break ;
1101 }
1104 /*******************
1105 * Any of
1106 *******************/
1108 case "anyof" :
1109 {
1110 $Contents = "";
1111 foreach($data as $key => $dat){
1112 if(($key === "Inverse") && ($key == "Inverse")){
1113 continue;
1114 }
1115 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1116 }
1117 $smarty = get_smarty();
1118 $smarty->assign("ID" , $element_id);
1119 $smarty->assign("DisplayAdd",TRUE);
1120 $smarty->assign("DisplayDel",FALSE);
1121 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1122 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1124 $smarty->assign("Inverse",$Inverse);
1125 $smarty->assign("Contents",$cont_tmp.$Contents);
1126 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1128 $ret = $allof_tmp;
1130 break ;
1131 }
1132 default :
1133 {
1134 $ret = "<table width='100%' cellspacing=0 cellpadding=0>
1135 <tr>
1136 <td style='background-color: #FEDCA9 ; border: solid 1px #EEEEEE'>";
1137 $ret.= $key."<br>";
1138 $ret.= " </td>
1139 </tr>
1140 </table>";
1141 }
1142 }
1143 }
1145 if(!isset($smarty)){
1146 $smarty =get_smarty();
1147 }
1149 $smarty->assign("ID",$element_id);
1150 $smarty->assign("DisplayAdd",FALSE);
1151 $smarty->assign("DisplayDel",TRUE);
1152 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1153 $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1154 return($ret);
1155 }
1158 /* Parse given token identified by $data[$id]
1159 * and return the parsed tokens.
1160 */
1161 function _parse($data,$id = 0)
1162 {
1163 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1164 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1165 $type = $data[$id]['text'];
1166 $tmp = array();
1168 /* Is there an identifier named 'not' to inverse this filter ? */
1169 $Inverse = FALSE;
1170 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1171 $Inverse = TRUE;
1172 $id ++;
1173 $type = $data[$id]['text'];
1174 }
1176 switch($type)
1177 {
1179 /****************
1180 * Parse - Envelope / Header / Address
1181 ****************/
1183 case "envelope" :
1184 case "header":
1185 case "address" :
1186 {
1187 /* Address matches are struckture as follows :
1188 [not]
1189 address
1190 [address-part: tag] all|localpart|domain|user|detail
1191 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1192 [match-type: tag] is|contains|matches|count|value
1193 <header-list: string-list>
1194 <key-list: string-list>
1195 */
1198 $part = "(:all|:localpart|:domain)";
1199 $operator = "(:contains|:is|:matches|:count|:value)";
1200 $value_op = "(lt|le|eq|ge|gt|ne)";
1202 $Address_Part = "";
1203 $Comparator = "";
1204 $Match_type = "";
1205 $Match_type_value = "";
1207 $Key_List = array();
1208 $Value_List = array();
1210 for($i = 0 ; $i < count($data) ; $i ++){
1212 /* Get next node */
1213 $node = $data[$i];
1215 /* Check address part definition */
1216 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1217 $Address_Part = $node['text'];
1218 }
1220 /* Check for match type */
1221 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1222 $Match_type = $node['text'];
1224 /* Get value operator */
1225 if(in_array($Match_type,array(":value",":count"))){
1226 $i ++;
1227 $node = $data[$i];
1229 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1230 $Match_type_value = $node['text'];
1231 }
1232 }
1233 }
1235 /* Check for a comparator */
1236 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1237 $i ++;
1238 $node = $data[$i];
1239 $Comparator = $node['text'];
1240 }
1242 /* Check for Key_List */
1243 elseif(count(sieve_get_strings($data,$i))){
1244 $tmp2 = sieve_get_strings($data,$i);
1245 $i = $tmp2['OFFSET'];
1247 if(!count($Key_List)){
1248 $Key_List = $tmp2['STRINGS'];
1249 }else{
1250 $Value_List = $tmp2['STRINGS'];
1251 }
1252 }
1254 }
1257 /* Add to Tree */
1258 $values = array( "Inverse" => $Inverse,
1259 "Comparator" => $Comparator,
1260 "Expert" => FALSE,
1261 "Match_type" => $Match_type,
1262 "Match_type_value"=> $Match_type_value,
1263 "Key_List" => $Key_List,
1264 "Value_List" => $Value_List) ;
1265 if($type == "address"){
1266 $values["Address_Part"] = $Address_Part;
1267 }
1268 $tmp[$type] = $values;
1269 $tmp[$type]['LastError'] = "";
1270 break;
1271 }
1274 /****************
1275 * Parse - Size
1276 ****************/
1278 case "size":
1279 {
1281 $ops = "(:over|:under)";
1283 $Match_type = "";
1285 for($i = $id ; $i < count($data); $i++){
1287 /* Get current node */
1288 $node = $data[$i];
1290 /* Get tag (under / over) */
1291 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1292 $Match_type = $node['text'];
1293 }
1295 /* Get Value_List, the value that we want to match for */
1296 elseif(count(sieve_get_strings($data,$i))){
1297 $tmp2 = sieve_get_strings($data,$i);
1298 $i = $tmp2['OFFSET'];
1300 $Value_List = $tmp2['STRINGS'];
1301 }
1302 }
1304 $tmp[$type]= array( "Inverse" => $Inverse,
1305 "Match_type" => $Match_type,
1306 "Value_List" => $Value_List);
1307 $tmp[$type]['LastError'] = "";
1308 break;
1309 }
1312 /****************
1313 * Parse - True / False
1314 ****************/
1316 case "true":
1317 {
1318 $tmp['true'] = "true";
1319 $tmp['true']['LastError'] = "";
1320 break;
1321 }
1322 case "false":
1323 {
1324 $tmp['false'] = "false";
1325 $tmp['false']['LastError'] = "";
1326 break;
1327 }
1330 /****************
1331 * Parse - Exists
1332 ****************/
1334 case "exists":
1335 {
1337 /* Skip first values, [if,not,exists] */
1338 $node = $data[$id];
1339 while(in_array($node['text'],array("if","not","exists"))){
1340 $id ++;
1341 $node = $data[$id];
1342 }
1344 /* Get values */
1345 $tmp2 = sieve_get_strings($data,$id);
1348 $tmp['exists'] = array('Inverse' => $Inverse,
1349 'Values' => $tmp2['STRINGS']);
1350 $tmp[$type]['LastError'] = "";
1351 break;
1352 }
1355 /****************
1356 * Parse - Allof
1357 ****************/
1359 case "allof" :
1360 {
1361 /* Get parameter and recursivly call this method
1362 * for each parameter
1363 */
1364 $id ++;
1365 $tmp2 = $this->get_parameter($data,$id);
1367 foreach($tmp2 as $parameter){
1368 $tmp['allof'][] = $this->_parse($parameter);
1369 }
1370 $tmp['allof']['Inverse'] = $Inverse;
1371 break;
1372 }
1375 /****************
1376 * Parse - Anyof
1377 ****************/
1379 case "anyof" :
1380 {
1381 /* Get parameter and recursivly call this method
1382 * for each parameter
1383 */
1384 $id ++;
1385 $tmp2 = $this->get_parameter($data,$id);
1387 foreach($tmp2 as $parameter){
1388 $tmp['anyof'][] = $this->_parse($parameter);
1389 }
1390 $tmp['anyof']['Inverse'] = $Inverse;
1391 break;
1392 }
1393 default : $tmp[$id] = $type;
1394 }
1396 return($tmp);
1397 }
1400 function get_parameter($data,$id)
1401 {
1402 $par = array();
1403 $open_brakets = 0;
1404 $next = NULL;
1405 $num = 0;
1406 for($i = $id ; $i < count($data) ; $i++ ){
1407 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1408 $open_brakets ++;
1409 }
1410 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1411 $num ++;
1412 }
1413 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1414 $par[$num][] = $data[$i];
1415 }
1416 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1417 $open_brakets --;
1418 }
1419 }
1420 return($par);
1421 }
1422 }
1424 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1425 ?>