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 print_a(array($data));
131 $ret[] = $data['LastError'];
132 }
133 }
134 }
135 }
136 return($ret);
137 }
140 /* Recursivly create a sieve script out of the given
141 * tags and tokens provided by $parsed.
142 * $id specifies the depth of the current element.
143 * $obj_id is the current tag-id handled by this function
144 */
145 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
146 {
147 $script ="";
148 if($parsed == NULL){
149 $parsed = $this->_parsed;
150 }
153 if(!is_array($parsed)){
154 return;
155 }
157 /* Walk through all elements */
158 foreach($parsed as $key => $data){
160 /* Create Inverse Tag */
161 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
162 $Inverse = TRUE;
163 }else{
164 $Inverse = FALSE;
165 }
167 /* Create elements */
168 switch($key)
169 {
171 /*******************
172 * True / False
173 *******************/
175 case "true" :
176 case "false" :
177 {
178 /* Invert this test if required */
179 if($Inverse){
180 $script .= "not ";
181 }
182 $script .= $key;
183 break;
184 }
187 /*******************
188 * Address
189 *******************/
191 case "address" :
192 {
193 /* [not] address
194 [address-part: tag]
195 [comparator: tag]
196 [match-type: tag]
197 <header-list: string-list>
198 <key-list: string-list>
199 */
201 /* Invert this test if required */
202 if($Inverse){
203 $script .= "not ";
204 }
206 $script .="address ";
208 /* Add address part tag */
209 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
210 $script .= $data['Address_Part']." ";
211 }
213 /* Add comparator */
214 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
215 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
216 }
218 /* Add match type */
219 $script .= $data['Match_type']." ";
221 /* Add special match type for count and value */
222 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
223 $script .= sieve_create_strings($data['Match_type_value'])." ";
224 }
226 $script .= sieve_create_strings($data['Key_List']);
227 $script .= " ";
228 $script .= sieve_create_strings($data['Value_List']);
229 break;
230 }
233 /*******************
234 * Header
235 *******************/
237 case "header" :
238 {
239 /* [not] header
240 [comparator: tag]
241 [match-type: tag]
242 <header-names: string-list>
243 <key-list: string-list>
244 */
246 /* Invert ? */
247 if($Inverse){
248 $script .= "not ";
249 }
251 $script .="header ";
253 /* Add address part tag */
254 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
255 $script .= $data['Address_Part']." ";
256 }
258 /* Add comparator */
259 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
260 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
261 }
263 /* Add match type */
264 $script .= $data['Match_type']." ";
266 /* Add special match type for count and value */
267 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
268 $script .= sieve_create_strings($data['Match_type_value'])." ";
269 }
271 $script .= sieve_create_strings($data['Key_List']);
272 $script .= " ";
273 $script .= sieve_create_strings($data['Value_List']);
274 break;
275 }
278 /*******************
279 * Envelope
280 *******************/
282 case "envelope" :
283 {
284 /* [not] envelope
285 [address-part: tag]
286 [comparator: tag]
287 [match-type: tag]
288 <envelope-part: string-list>
289 <key-list: string-list>
290 */
292 /* Invert */
293 if($Inverse){
294 $script .= "not ";
295 }
297 $script .="envelope ";
299 /* Add address part tag */
300 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
301 $script .= $data['Address_Part']." ";
302 }
304 /* Add comparator */
305 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
306 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
307 }
309 /* Add match type */
310 $script .= $data['Match_type']." ";
312 /* Add special match type for count and value */
313 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
314 $script .= sieve_create_strings($data['Match_type_value'])." ";
315 }
317 $script .= sieve_create_strings($data['Key_List']);
318 $script .= " ";
319 $script .= sieve_create_strings($data['Value_List']);
320 break;
321 }
324 /*******************
325 * Exists
326 *******************/
327 case "exists" :
328 {
329 /* [not] exists
330 <header-names: string-list>
331 */
333 /* Invert ? */
334 if($Inverse){
335 $script .= "not ";
336 }
338 $script .= "exists ".sieve_create_strings($data['Values']);
339 break;
340 }
343 /*******************
344 * Size
345 *******************/
346 case "size" :
347 {
348 /* [not] size
349 <":over" / ":under">
350 <limit: number>
351 */
353 /* Invert ? */
354 if($Inverse){
355 $script .= "not ";
356 }
358 /* Add size test */
359 $script .="size ";
360 $script .=$data['Match_type']." ";
361 foreach($data['Value_List'] as $val){
362 $script .= $val." ";
363 }
364 break;
365 }
368 /*******************
369 * Allof
370 *******************/
371 case "anyof" :
372 case "allof" :
373 {
374 /* allof <tests: test-list>
375 anyof <tests: test-list> */
378 /* Add spaces, to indent the code.*/
379 $block = "\n";
380 for($i = 0 ; $i < $id ; $i ++){
381 $block .= SIEVE_INDENT_TAB;
382 }
384 /* Add allof/anyof tag */
385 $script.= " ".$key." ( ";
387 /* Add each test parameter */
388 foreach($data as $key2 => $dat){
389 if(($key2 === "Inverse") && ($key2 == "Inverse")){
390 continue;
391 }
392 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
393 }
395 /* Remove last _,_ and close the tag */
396 $script = preg_replace("/,$/","",trim($script));
397 $script.= $block.")";
398 break ;
399 }
401 default :
402 {
403 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
404 }
405 }
406 }
407 return($script);
408 }
411 function add_test($data,$type)
412 {
413 switch($type)
414 {
415 case "header" :
416 case "address" :
417 case "envelope" :
418 {
419 /* Add to Tree */
420 $values = array( "Inverse" => FALSE,
421 "Comparator" => "",
422 "Expert" => FALSE,
423 "LastError" => "",
424 "Match_type" => ":contains",
425 "Match_type_value"=> "",
426 "Key_List" => array(_("emtpy")),
427 "Value_List" => array(_("empty"))) ;
428 if($type == "address"){
429 $values["Address_Part"] = ":all";
430 }
431 $data[$type]=$values;
433 $this->parent->add_require("relational");
434 if($type == "envelope"){
435 $this->parent->add_require("envelope");
436 }
439 break;
440 }
441 case "allof" :
442 case "anyof" :
443 {
444 $data[$type] = array("Inverse" => FALSE);
445 break;
446 }
447 case "size" :
448 {
449 $tmp= array(
450 "Inverse" => FALSE,
451 "Match_type" => ":over",
452 "Value_List" => array("1M"));
454 $tmp['LastError'] = "";
455 $data[$type] = $tmp;
456 break;
457 }
458 case "true":
459 {
460 $data['true'] = "true";
461 $data['true']['LastError'] = "";
462 break;
463 }
464 case "false":
465 {
466 $data['false'] = "false";
467 $data['false']['LastError'] = "";
468 break;
469 }
470 case "exists" :
471 {
472 $data['exists'] = array('Inverse' => FALSE,
473 'Values' => array(_("Nothing specified right now")),
474 'LastError' => "");
475 break;
476 }
477 default : echo "Still buggy ";exit;
478 }
480 return($data);
481 }
484 /* Ensure that all changes made on the ui
485 * will be saved.
486 */
487 function save_object()
488 {
490 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
491 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
492 }
494 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
495 $this->_parsed = $tmp;
496 }
499 /* Recursivly save all ui changes for the
500 * tags and tokens provided by $parsed.
501 * $id specifies the depth of the current element.
502 * $obj_id is the current tag-id handled by this function
503 */
504 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
505 {
506 /* Variable initialization */
507 $ret ="";
508 if($parsed == NULL){
509 $parsed = $this->_parsed;
510 }
512 if(!is_array($parsed)) return;
514 /* Walk through all elements */
515 foreach($parsed as $key => $data){
517 /* Id used to have unique html names */
518 $element_id = $this->object_id."_".$id."_".$obj_id;
520 foreach($_POST as $name => $value){
521 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
522 return(false);
523 }
524 }
527 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
528 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
529 }
531 /* Create elements */
532 switch($key)
533 {
534 /*******************
535 * Address
536 *******************/
538 case "envelope" :
539 case "header" :
540 case "address" :
541 {
542 /* [not] address
543 [address-part: tag]
544 [comparator: tag]
545 [match-type: tag]
546 <header-list: string-list>
547 <key-list: string-list>
548 */
550 /* Possible address parts we can select */
551 $address_parts = $this->address_parts;
552 $comparators = $this->comparators;
553 $match_types = $this->match_types;
554 $operators = $this->operators;
556 $parsed[$key]['LastError'] = "";
558 /* Toggle Inverse ? */
559 if(isset($_POST['toggle_inverse_'.$element_id])){
560 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
561 }
563 /* Check if we want to toggle the expert mode */
564 if(isset($_POST['Toggle_Expert_'.$element_id])){
565 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
566 }
568 /* Get address part */
569 if(isset($_POST['address_part_'.$element_id])){
570 $ap = $_POST['address_part_'.$element_id];
572 if(!isset($address_parts[$ap])){
573 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
574 }
575 $parsed[$key]['Address_Part'] = $ap;
576 }
578 /* Check if match type has changed */
579 if(isset($_POST['matchtype_'.$element_id])){
580 $mt = $_POST['matchtype_'.$element_id];
582 if(!isset($match_types[$mt])){
583 $parsed[$key]['LastError'] = _("Invalid match type given.");
584 }
585 $parsed[$key]['Match_type'] = $mt;
586 }
588 /* Get the comparator tag, if posted */
589 if(isset($_POST['comparator_'.$element_id])){
590 $cp = $_POST['comparator_'.$element_id];
592 if(!isset($comparators[$cp])){
593 $parsed[$key]['LastError'] = _("Invalid operator given.");
594 }
595 $parsed[$key]['Comparator'] = $cp;
597 if($cp == "i;ascii-numeric"){
598 $this->parent->add_require("comparator-i;ascii-numeric");
599 }
600 }
602 /* In case of :count and :value match types
603 * we have a special match operator we should save.
604 */
605 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
606 if(isset($_POST['operator_'.$element_id])){
607 $op = $_POST['operator_'.$element_id];
609 if(!isset($operators[$op])){
610 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
611 }
612 $parsed[$key]['Match_type_value'] = $op;
613 }
614 }
616 /* Get the address fields we should check, they are seperated by , */
617 if(isset($_POST['keys_'.$element_id])){
618 $vls = stripslashes($_POST['keys_'.$element_id]);
619 $tmp = array();
621 $tmp2 = split(",",$vls);
622 foreach($tmp2 as $val){
623 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
624 }
625 $parsed[$key]['Key_List'] = $tmp;
626 }
628 /* Get the values should check for, they are seperated by , */
629 if(isset($_POST['values_'.$element_id])){
630 $vls = stripslashes($_POST['values_'.$element_id]);
631 $tmp = array();
633 $tmp2 = split(",",$vls);
634 foreach($tmp2 as $val){
635 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
636 }
637 $parsed[$key]['Value_List'] = $tmp;
638 }
639 break;
640 }
642 /*******************
643 * TRUE FALSE
644 *******************/
646 case "true" :
647 case "false" :
648 {
649 $name = 'boolean_'.$element_id;
650 if(isset($_POST[$name])){
651 $key2 = $_POST[$name];
653 if($key != $key2) {
654 $parsed = array($key2 => $key2);
655 }
656 }
657 break;
658 }
660 /*******************
661 * Exists
662 *******************/
664 case "exists" :
665 {
666 /* Toggle Inverse ? */
667 if(isset($_POST['toggle_inverse_'.$element_id])){
668 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
669 }
671 /* get list of match values */
672 if(isset($_POST['Values_'.$element_id])){
673 $vls = stripslashes($_POST['Values_'.$element_id]);
674 $tmp = array();
676 $tmp2 = split(",",$vls);
677 foreach($tmp2 as $val){
678 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
679 }
680 $parsed['exists']['Values'] = $tmp;
681 }
682 break;
683 }
685 /*******************
686 * Size
687 *******************/
689 case "size" :
690 {
691 $Match_types = array( ":over" => _("greater than") ,
692 ":under" => _("lower than"));
694 $Units = array( "M" => _("Megabyte") ,
695 "K" => _("Kilobyte"));
697 /* Reset error */
698 $parsed[$key]['LastError'] ="";
700 /* Get match type */
701 if(isset($_POST['Match_type_'.$element_id])){
702 $mt = $_POST['Match_type_'.$element_id];
703 if(!isset($Match_types[$mt])){
704 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
705 }
706 $parsed[$key]['Match_type'] = $mt;
707 }
709 /* Get old values */
710 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
711 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
713 /* Get value */
714 if(isset($_POST['Value_'.$element_id])){
715 $vl = $_POST['Value_'.$element_id];
717 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
718 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
719 }
720 $value = preg_replace("/[^0-9]/","",$vl);
721 }
723 /* Get unit */
724 if(isset($_POST['Value_Unit_'.$element_id])){
725 $ut = $_POST['Value_Unit_'.$element_id];
727 if(!isset($Units[$ut])){
728 $parsed[$key]['LastError'] = _("No valid unit selected");
729 }
730 $unit = $ut;
731 }
732 $parsed[$key]['Value_List'] = array();
733 $parsed[$key]['Value_List'][0] = $value.$unit;
734 break;
735 }
737 /*******************
738 * Allof
739 *******************/
741 case "allof" :
742 {
743 if(isset($_POST['toggle_inverse_'.$element_id])){
744 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
745 }
746 foreach($data as $key2 => $dat){
747 if(($key2 === "Inverse") && ($key2 == "Inverse")){
748 continue;
749 }
750 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
751 if($tmp_data != false){
752 $parsed[$key][$key2] = $tmp_data;
753 }else{
754 unset( $parsed[$key][$key2]);
755 }
756 }
757 break ;
758 }
760 /*******************
761 * Anyof
762 *******************/
764 case "anyof" :
765 {
766 if(isset($_POST['toggle_inverse_'.$element_id])){
767 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
768 }
769 foreach($data as $key2 => $dat){
770 if(($key2 === "Inverse") && ($key2 == "Inverse")){
771 continue;
772 }
773 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
774 if($tmp_data != false){
775 $parsed[$key][$key2] = $tmp_data;
776 }else{
777 unset( $parsed[$key][$key2]);
778 }
779 }
780 break ;
781 }
782 }
783 }
784 return($parsed);
785 }
788 /* Return html element for IF */
789 function execute()
790 {
791 /* Create title */
792 $name = "<img alt='' src='images/small_filter.png' class='center'>";
793 $name .= "<b>"._("Condition")."</b>";
794 if($this->TYPE == "if"){
795 $name .= " - "._("If");
796 }elseif($this->TYPE == "elsif"){
797 $name .= " - "._("Else if");
798 }else{
799 $name .= " - "._("Else");
800 }
802 $smarty = get_smarty();
803 $smarty->assign("ID", $this->object_id);
805 /* Only display navigation elements if necessary */
806 if($this->TYPE == "if"){
807 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
808 }else{
809 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
810 }
812 $smarty->assign("Name", $name);
813 $smarty->assign("Contents", $this->get_as_html());
815 if($this->TYPE == "if"){
816 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
817 }else{
818 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
819 }
820 $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
821 return($str);
822 }
825 /* Returns all elements as html */
826 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
827 {
828 $ret ="";
829 if($parsed == NULL){
830 $parsed = $this->_parsed;
831 }
833 if((!is_array($parsed)) || !count($parsed)) {
834 $smarty = get_smarty();
835 $smarty->assign("ID",$this->object_id);
836 $smarty->assign("DisplayAdd",TRUE);
837 $smarty->assign("DisplayDel",FALSE);
838 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
839 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
840 return($ret);
841 }
843 /* Walk through all elements */
844 foreach($parsed as $key => $data){
846 /* Create Inverse Tag */
847 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
848 $Inverse = TRUE;
849 }else{
850 $Inverse = FALSE;
851 }
853 /* Id used to have unique html names */
854 $element_id = $this->object_id."_".$id."_".$obj_id;
856 /* Create elements */
857 switch($key)
858 {
860 /*******************
861 * TRUE FALSE
862 *******************/
864 case "true" :
865 case "false" :
866 {
867 /* Inverse element if required */
868 if($Inverse){
869 if($key == "true"){
870 $key = "false";
871 }else{
872 $key = "true";
873 }
874 }
876 /* Get template */
877 $smarty = get_smarty();
878 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
879 $smarty->assign("selected" , $key);
880 $smarty->assign("ID" , $element_id);
881 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
882 break;
883 }
886 /*******************
887 * Header
888 *******************/
890 case "header":
891 {
892 $address_parts = $this->address_parts;
893 $comparators = $this->comparators;
894 $match_types = $this->match_types;
895 $operators = $this->operators;
897 $smarty = get_smarty();
898 $smarty->assign("comparators",$comparators);
899 $smarty->assign("match_types",$match_types);
900 $smarty->assign("operators",$operators);
901 $smarty->assign("LastError",$data['LastError']);
902 $smarty->assign("match_type", $data['Match_type']);
903 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
904 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
906 $keys = "";
907 foreach($data['Key_List'] as $key){
908 $keys .= $key.", ";
909 }
910 $keys = preg_replace("/,$/","",trim($keys));
912 $values = "";
913 foreach($data['Value_List'] as $key){
914 $values .= $key.", ";
915 }
916 $values = preg_replace("/,$/","",trim($values));
918 $smarty->assign("keys",$keys);
919 $smarty->assign("Inverse",$Inverse);
920 $smarty->assign("values",$values);
921 $smarty->assign("Expert", $data['Expert']);
923 $smarty->assign("ID" , $element_id);
924 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
925 break;
926 }
929 /*******************
930 * Envelope
931 *******************/
933 case "envelope":
934 {
935 $address_parts = $this->address_parts;
936 $comparators = $this->comparators;
937 $match_types = $this->match_types;
938 $operators = $this->operators;
940 $smarty = get_smarty();
941 $smarty->assign("Inverse",$Inverse);
942 $smarty->assign("comparators",$comparators);
943 $smarty->assign("Expert", $data['Expert']);
944 $smarty->assign("match_types",$match_types);
945 $smarty->assign("operators",$operators);
946 $smarty->assign("LastError",$data['LastError']);
947 $smarty->assign("match_type", $data['Match_type']);
948 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
949 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
951 $keys = "";
952 foreach($data['Key_List'] as $key){
953 $keys .= $key.", ";
954 }
955 $keys = preg_replace("/,$/","",trim($keys));
957 $values = "";
958 foreach($data['Value_List'] as $key){
959 $values .= $key.", ";
960 }
961 $values = preg_replace("/,$/","",trim($values));
962 $smarty->assign("keys",$keys);
963 $smarty->assign("values",$values);
965 $smarty->assign("ID" , $element_id);
966 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
967 break;
968 }
971 /*******************
972 * Address
973 *******************/
975 case "address" :
976 {
977 $address_parts = $this->address_parts;
978 $comparators = $this->comparators;
979 $match_types = $this->match_types;
980 $operators = $this->operators;
982 $smarty = get_smarty();
983 $smarty->assign("Inverse",$Inverse);
984 $smarty->assign("address_parts",$address_parts);
985 $smarty->assign("comparators",$comparators);
986 $smarty->assign("match_types",$match_types);
987 $smarty->assign("LastError",$data['LastError']);
988 $smarty->assign("operators",$operators);
989 $smarty->assign("match_type", $data['Match_type']);
990 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
991 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
992 $smarty->assign("address_part", $data['Address_Part']);
993 $smarty->assign("Expert", $data['Expert']);
995 $keys = "";
996 foreach($data['Key_List'] as $key){
997 $keys .= $key.", ";
998 }
999 $keys = preg_replace("/,$/","",trim($keys));
1001 $values = "";
1002 foreach($data['Value_List'] as $key){
1003 $values .= $key.", ";
1004 }
1005 $values = preg_replace("/,$/","",trim($values));
1006 $smarty->assign("keys",$keys);
1007 $smarty->assign("values",$values);
1008 $smarty->assign("ID" , $element_id);
1009 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1010 break;
1011 }
1014 /*******************
1015 * Size
1016 *******************/
1018 case "size" :
1019 {
1020 $Match_types = array( ":over" => _("greater than") ,
1021 ":under" => _("lower than"));
1023 $Units = array( "M" => _("Megabyte") ,
1024 "K" => _("Kilobyte"));
1026 $Match_type = $data['Match_type'];
1027 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1028 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1030 $LastError = "";
1031 if(isset($data['LastError'])){
1032 $LastError = $data['LastError'];
1033 }
1035 $smarty = get_smarty();
1036 $smarty->assign("Inverse",$Inverse);
1037 $smarty->assign("LastError",$LastError);
1038 $smarty->assign("Match_types",$Match_types);
1039 $smarty->assign("Units",$Units);
1040 $smarty->assign("Match_type",$Match_type);
1041 $smarty->assign("Value",$Value);
1042 $smarty->assign("Value_Unit",$Value_Unit);
1043 $smarty->assign("ID" , $element_id);
1044 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1045 break;
1046 }
1048 /*******************
1049 * Exists
1050 *******************/
1052 case "exists" :
1053 {
1054 $LastError = "";
1055 if(isset($data['LastError'])){
1056 $LastError = $data['LastError'];
1057 }
1059 $Values = "";
1060 foreach($data['Values'] as $val){
1061 $Values .= $val.", ";
1062 }
1063 $Values = preg_replace("/,$/","",trim($Values));
1065 $smarty = get_smarty();
1066 $smarty->assign("LastError",$LastError);
1067 $smarty->assign("Values",$Values);
1068 $smarty->assign("Inverse",$Inverse);
1069 $smarty->assign("ID" , $element_id);
1070 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1071 break;
1072 }
1075 /*******************
1076 * All of
1077 *******************/
1079 case "allof" :
1080 {
1081 $Contents = "";
1082 foreach($data as $key => $dat){
1083 if(($key === "Inverse") && ($key == "Inverse")){
1084 continue;
1085 }
1086 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1087 }
1089 $smarty = get_smarty();
1090 $smarty->assign("ID" , $element_id);
1091 $smarty->assign("DisplayAdd",TRUE);
1092 $smarty->assign("DisplayDel",FALSE);
1093 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1094 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Click here to add a new test"),$cont_tmp);
1096 $smarty->assign("Inverse",$Inverse);
1097 $smarty->assign("Contents",$cont_tmp.$Contents);
1098 $smarty->assign("ID" , $element_id);
1099 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1101 $ret = $allof_tmp;
1102 break ;
1103 }
1106 /*******************
1107 * Any of
1108 *******************/
1110 case "anyof" :
1111 {
1112 $Contents = "";
1113 foreach($data as $key => $dat){
1114 if(($key === "Inverse") && ($key == "Inverse")){
1115 continue;
1116 }
1117 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1118 }
1119 $smarty = get_smarty();
1120 $smarty->assign("ID" , $element_id);
1121 $smarty->assign("DisplayAdd",TRUE);
1122 $smarty->assign("DisplayDel",FALSE);
1123 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1124 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1126 $smarty->assign("Inverse",$Inverse);
1127 $smarty->assign("Contents",$cont_tmp.$Contents);
1128 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1130 $ret = $allof_tmp;
1132 break ;
1133 }
1134 default :
1135 {
1136 trigger_error(_("Unhandled switch type"));
1137 }
1138 }
1139 }
1141 if(!isset($smarty)){
1142 $smarty =get_smarty();
1143 }
1145 $smarty->assign("ID",$element_id);
1146 $smarty->assign("DisplayAdd",FALSE);
1147 $smarty->assign("DisplayDel",TRUE);
1148 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1149 $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1150 return($ret);
1151 }
1154 /* Parse given token identified by $data[$id]
1155 * and return the parsed tokens.
1156 */
1157 function _parse($data,$id = 0)
1158 {
1159 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1160 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1161 $type = $data[$id]['text'];
1162 $tmp = array();
1164 /* Is there an identifier named 'not' to inverse this filter ? */
1165 $Inverse = FALSE;
1166 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1167 $Inverse = TRUE;
1168 $id ++;
1169 $type = $data[$id]['text'];
1170 }
1172 switch($type)
1173 {
1175 /****************
1176 * Parse - Envelope / Header / Address
1177 ****************/
1179 case "envelope" :
1180 case "header":
1181 case "address" :
1182 {
1183 /* Address matches are struckture as follows :
1184 [not]
1185 address
1186 [address-part: tag] all|localpart|domain|user|detail
1187 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1188 [match-type: tag] is|contains|matches|count|value
1189 <header-list: string-list>
1190 <key-list: string-list>
1191 */
1194 $part = "(:all|:localpart|:domain)";
1195 $operator = "(:contains|:is|:matches|:count|:value)";
1196 $value_op = "(lt|le|eq|ge|gt|ne)";
1198 $Address_Part = "";
1199 $Comparator = "";
1200 $Match_type = "";
1201 $Match_type_value = "";
1203 $Key_List = array();
1204 $Value_List = array();
1206 for($i = 0 ; $i < count($data) ; $i ++){
1208 /* Get next node */
1209 $node = $data[$i];
1211 /* Check address part definition */
1212 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1213 $Address_Part = $node['text'];
1214 }
1216 /* Check for match type */
1217 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1218 $Match_type = $node['text'];
1220 /* Get value operator */
1221 if(in_array($Match_type,array(":value",":count"))){
1222 $i ++;
1223 $node = $data[$i];
1225 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1226 $Match_type_value = $node['text'];
1227 }
1228 }
1229 }
1231 /* Check for a comparator */
1232 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1233 $i ++;
1234 $node = $data[$i];
1235 $Comparator = $node['text'];
1236 }
1238 /* Check for Key_List */
1239 elseif(count(sieve_get_strings($data,$i))){
1240 $tmp2 = sieve_get_strings($data,$i);
1241 $i = $tmp2['OFFSET'];
1243 if(!count($Key_List)){
1244 $Key_List = $tmp2['STRINGS'];
1245 }else{
1246 $Value_List = $tmp2['STRINGS'];
1247 }
1248 }
1250 }
1253 /* Add to Tree */
1254 $values = array( "Inverse" => $Inverse,
1255 "Comparator" => $Comparator,
1256 "Expert" => FALSE,
1257 "Match_type" => $Match_type,
1258 "Match_type_value"=> $Match_type_value,
1259 "Key_List" => $Key_List,
1260 "Value_List" => $Value_List) ;
1261 if($type == "address"){
1262 $values["Address_Part"] = $Address_Part;
1263 }
1264 $tmp[$type] = $values;
1265 $tmp[$type]['LastError'] = "";
1266 break;
1267 }
1270 /****************
1271 * Parse - Size
1272 ****************/
1274 case "size":
1275 {
1277 $ops = "(:over|:under)";
1279 $Match_type = "";
1281 for($i = $id ; $i < count($data); $i++){
1283 /* Get current node */
1284 $node = $data[$i];
1286 /* Get tag (under / over) */
1287 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1288 $Match_type = $node['text'];
1289 }
1291 /* Get Value_List, the value that we want to match for */
1292 elseif(count(sieve_get_strings($data,$i))){
1293 $tmp2 = sieve_get_strings($data,$i);
1294 $i = $tmp2['OFFSET'];
1296 $Value_List = $tmp2['STRINGS'];
1297 }
1298 }
1300 $tmp[$type]= array( "Inverse" => $Inverse,
1301 "Match_type" => $Match_type,
1302 "Value_List" => $Value_List);
1303 $tmp[$type]['LastError'] = "";
1304 break;
1305 }
1308 /****************
1309 * Parse - True / False
1310 ****************/
1312 case "true":
1313 {
1314 $tmp['true'] = "true";
1315 $tmp['true']['LastError'] = "";
1316 break;
1317 }
1318 case "false":
1319 {
1320 $tmp['false'] = "false";
1321 $tmp['false']['LastError'] = "";
1322 break;
1323 }
1326 /****************
1327 * Parse - Exists
1328 ****************/
1330 case "exists":
1331 {
1333 /* Skip first values, [if,not,exists] */
1334 $node = $data[$id];
1335 while(in_array($node['text'],array("if","not","exists"))){
1336 $id ++;
1337 $node = $data[$id];
1338 }
1340 /* Get values */
1341 $tmp2 = sieve_get_strings($data,$id);
1344 $tmp['exists'] = array('Inverse' => $Inverse,
1345 'Values' => $tmp2['STRINGS']);
1346 $tmp[$type]['LastError'] = "";
1347 break;
1348 }
1351 /****************
1352 * Parse - Allof
1353 ****************/
1355 case "allof" :
1356 {
1357 /* Get parameter and recursivly call this method
1358 * for each parameter
1359 */
1360 $id ++;
1361 $tmp2 = $this->get_parameter($data,$id);
1363 foreach($tmp2 as $parameter){
1364 $tmp['allof'][] = $this->_parse($parameter);
1365 }
1366 $tmp['allof']['Inverse'] = $Inverse;
1367 break;
1368 }
1371 /****************
1372 * Parse - Anyof
1373 ****************/
1375 case "anyof" :
1376 {
1377 /* Get parameter and recursivly call this method
1378 * for each parameter
1379 */
1380 $id ++;
1381 $tmp2 = $this->get_parameter($data,$id);
1383 foreach($tmp2 as $parameter){
1384 $tmp['anyof'][] = $this->_parse($parameter);
1385 }
1386 $tmp['anyof']['Inverse'] = $Inverse;
1387 break;
1388 }
1389 default : $tmp[$id] = $type;
1390 }
1392 return($tmp);
1393 }
1396 function get_parameter($data,$id)
1397 {
1398 $par = array();
1399 $open_brakets = 0;
1400 $next = NULL;
1401 $num = 0;
1402 for($i = $id ; $i < count($data) ; $i++ ){
1403 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1404 $open_brakets ++;
1405 }
1406 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1407 $num ++;
1408 }
1409 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1410 $par[$num][] = $data[$i];
1411 }
1412 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1413 $open_brakets --;
1414 }
1415 }
1416 return($par);
1417 }
1418 }
1420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1421 ?>