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;
23 $this->object_id = $object_id;
25 /* Possible address parts we can select */
26 $this->address_parts = array(
27 ":all" => _("Complete adress")." ("._("Default").")",
28 ":domain" => _("Domian part") ,
29 ":localpart" => _("Local part"));
31 /* comparator type */
32 $this->comparators = array(
33 "i;ascii-casemap" => _("Case insensitive")." ("._("Default").")",
34 "i;octet" => _("Case sensitive"),
35 "i;ascii-numeric" => _("Numeric"));
37 /* Match types */
38 $this->match_types = array(
39 ":is" => _("is"),
40 ":regex" => _("regex"),
41 ":contains" => _("contains"),
42 ":matches" => _("matches"),
43 ":count" => _("count"),
44 ":value" => _("value is"));
46 /* Operators */
47 $this->operators = array(
48 "lt" => _("less than"),
49 "le" => _("less or equal"),
50 "eq" => _("equals"),
51 "ge" => _("greater or equal"),
52 "gt" => _("greater than"),
53 "ne" => _("not equal"));
55 /* Skip parsing if this element is new */
56 if($elements != NULL){
58 /* Remove comments from tests */
59 $tmp = array();
60 foreach($elements['ELEMENTS'] as $ele){
61 if($ele['class'] != "comment"){
62 $tmp[] = $ele;
63 }
64 }
65 $elements['ELEMENTS'] = $tmp;
67 if($elements!=NULL){
68 $this->elements = $elements;
69 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
70 }
71 }
72 }
75 /* Returns the sieve script for this
76 * if/else tag.
77 */
78 function get_sieve_script_part()
79 {
80 $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
81 return($tmp);
82 }
85 /* Return error msgs */
86 function check()
87 {
88 $check = $this->check_recursive();
89 return($check);
90 }
93 /* Recursivly fetch all error msgs */
94 function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
95 {
96 $ret = array();
97 if($parsed == NULL){
98 $parsed = $this->_parsed;
99 }
101 /* Walk through all elements */
102 foreach($parsed as $key => $data){
104 /* Create elements */
105 switch($key)
106 {
107 /*******************
108 * Allof / Anyof
109 *******************/
110 case "anyof" :
111 case "allof" :
112 {
113 foreach($data as $key2 => $dat){
114 if(($key2 === "Inverse") && ($key2 == "Inverse")){
115 continue;
116 }
117 $msgs = $this->check_recursive($dat, ($id +1),$key2);
119 foreach($msgs as $msg){
120 $ret[] = $msg;
121 }
122 }
123 break;
124 }
126 /*******************
127 * True / False
128 *******************/
130 case "true" :
131 case "false" :
132 {
133 /* Can't fail anyway */
134 break;
135 }
137 /*******************
138 * Default
139 *******************/
141 default:
142 {
143 if(isset($data['LastError']) && !empty($data['LastError'])){
144 $ret[] = $data['LastError'];
145 }
146 }
147 }
148 }
149 return($ret);
150 }
153 /* Recursivly create a sieve script out of the given
154 * tags and tokens provided by $parsed.
155 * $id specifies the depth of the current element.
156 * $obj_id is the current tag-id handled by this function
157 */
158 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
159 {
160 $script ="";
161 if($parsed == NULL){
162 $parsed = $this->_parsed;
163 }
166 if(!is_array($parsed)){
167 return;
168 }
170 /* Walk through all elements */
171 foreach($parsed as $key => $data){
173 /* Create Inverse Tag */
174 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
175 $Inverse = TRUE;
176 }else{
177 $Inverse = FALSE;
178 }
180 /* Create elements */
181 switch($key)
182 {
184 /*******************
185 * True / False
186 *******************/
188 case "true" :
189 case "false" :
190 {
191 /* Invert this test if required */
192 if($Inverse){
193 $script .= "not ";
194 }
195 $script .= $key;
196 break;
197 }
200 /*******************
201 * Address
202 *******************/
204 case "address" :
205 {
206 /* [not] address
207 [address-part: tag]
208 [comparator: tag]
209 [match-type: tag]
210 <header-list: string-list>
211 <key-list: string-list>
212 */
214 /* Invert this test if required */
215 if($Inverse){
216 $script .= "not ";
217 }
219 $script .="address ";
221 /* Add address part tag */
222 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
223 $script .= $data['Address_Part']." ";
224 }
226 /* Add comparator */
227 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
228 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
229 }
231 /* Add match type */
232 $script .= $data['Match_type']." ";
234 /* Add special match type for count and value */
235 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
236 $script .= sieve_create_strings($data['Match_type_value'])." ";
237 }
239 $script .= sieve_create_strings($data['Key_List']);
240 $script .= " ";
241 $script .= sieve_create_strings($data['Value_List']);
242 break;
243 }
246 /*******************
247 * Header
248 *******************/
250 case "header" :
251 {
252 /* [not] header
253 [comparator: tag]
254 [match-type: tag]
255 <header-names: string-list>
256 <key-list: string-list>
257 */
259 /* Invert ? */
260 if($Inverse){
261 $script .= "not ";
262 }
264 $script .="header ";
266 /* Add address part tag */
267 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
268 $script .= $data['Address_Part']." ";
269 }
271 /* Add comparator */
272 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
273 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
274 }
276 /* Add match type */
277 $script .= $data['Match_type']." ";
279 /* Add special match type for count and value */
280 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
281 $script .= sieve_create_strings($data['Match_type_value'])." ";
282 }
284 $script .= sieve_create_strings($data['Key_List']);
285 $script .= " ";
286 $script .= sieve_create_strings($data['Value_List']);
287 break;
288 }
291 /*******************
292 * Envelope
293 *******************/
295 case "envelope" :
296 {
297 /* [not] envelope
298 [address-part: tag]
299 [comparator: tag]
300 [match-type: tag]
301 <envelope-part: string-list>
302 <key-list: string-list>
303 */
305 /* Invert */
306 if($Inverse){
307 $script .= "not ";
308 }
310 $script .="envelope ";
312 /* Add address part tag */
313 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
314 $script .= $data['Address_Part']." ";
315 }
317 /* Add comparator */
318 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
319 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
320 }
322 /* Add match type */
323 $script .= $data['Match_type']." ";
325 /* Add special match type for count and value */
326 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
327 $script .= sieve_create_strings($data['Match_type_value'])." ";
328 }
330 $script .= sieve_create_strings($data['Key_List']);
331 $script .= " ";
332 $script .= sieve_create_strings($data['Value_List']);
333 break;
334 }
337 /*******************
338 * Exists
339 *******************/
340 case "exists" :
341 {
342 /* [not] exists
343 <header-names: string-list>
344 */
346 /* Invert ? */
347 if($Inverse){
348 $script .= "not ";
349 }
351 $script .= "exists ".sieve_create_strings($data['Values']);
352 break;
353 }
356 /*******************
357 * Size
358 *******************/
359 case "size" :
360 {
361 /* [not] size
362 <":over" / ":under">
363 <limit: number>
364 */
366 /* Invert ? */
367 if($Inverse){
368 $script .= "not ";
369 }
371 /* Add size test */
372 $script .="size ";
373 $script .=$data['Match_type']." ";
374 foreach($data['Value_List'] as $val){
375 $script .= $val." ";
376 }
377 break;
378 }
381 /*******************
382 * Allof
383 *******************/
384 case "anyof" :
385 case "allof" :
386 {
387 /* allof <tests: test-list>
388 anyof <tests: test-list> */
391 /* Add spaces, to indent the code.*/
392 $block = "\n";
393 for($i = 0 ; $i < $id ; $i ++){
394 $block .= SIEVE_INDENT_TAB;
395 }
397 /* Add allof/anyof tag */
398 if($Inverse){
399 $script .= "not ";
400 }
401 $script.= $key." ( ";
403 /* Add each test parameter */
404 foreach($data as $key2 => $dat){
405 if(($key2 === "Inverse") && ($key2 == "Inverse")){
406 continue;
407 }
408 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
409 }
411 /* Remove last _,_ and close the tag */
412 $script = preg_replace("/,$/","",trim($script));
413 $script.= $block.")";
414 break ;
415 }
417 default :
418 {
419 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
420 }
421 }
422 }
423 return($script);
424 }
427 function add_test($data,$type)
428 {
429 switch($type)
430 {
431 case "header" :
432 case "address" :
433 case "envelope" :
434 {
435 /* Add to Tree */
436 $values = array( "Inverse" => FALSE,
437 "Comparator" => "",
438 "Expert" => FALSE,
439 "LastError" => "",
440 "Match_type" => ":contains",
441 "Match_type_value"=> "",
442 "Key_List" => array(_("emtpy")),
443 "Value_List" => array(_("empty"))) ;
444 if($type == "address"){
445 $values["Address_Part"] = ":all";
446 }
447 $data[$type]=$values;
449 $this->parent->add_require("relational");
450 if($type == "envelope"){
451 $this->parent->add_require("envelope");
452 }
455 break;
456 }
457 case "allof" :
458 case "anyof" :
459 {
460 $data[$type] = array("Inverse" => FALSE);
461 break;
462 }
463 case "size" :
464 {
465 $tmp= array(
466 "Inverse" => FALSE,
467 "Match_type" => ":over",
468 "Value_List" => array("1M"));
470 $tmp['LastError'] = "";
471 $data[$type] = $tmp;
472 break;
473 }
474 case "true":
475 {
476 $data['true'] = "true";
477 $data['true']['LastError'] = "";
478 break;
479 }
480 case "false":
481 {
482 $data['false'] = "false";
483 $data['false']['LastError'] = "";
484 break;
485 }
486 case "exists" :
487 {
488 $data['exists'] = array('Inverse' => FALSE,
489 'Values' => array(_("Nothing specified right now")),
490 'LastError' => "");
491 break;
492 }
493 default : echo "Still buggy ";exit;
494 }
496 return($data);
497 }
500 /* Ensure that all changes made on the ui
501 * will be saved.
502 */
503 function save_object()
504 {
506 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
507 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
508 }
510 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
511 $this->_parsed = $tmp;
512 }
515 /* Recursivly save all ui changes for the
516 * tags and tokens provided by $parsed.
517 * $id specifies the depth of the current element.
518 * $obj_id is the current tag-id handled by this function
519 */
520 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
521 {
522 /* Variable initialization */
523 $ret ="";
524 if($parsed == NULL){
525 $parsed = $this->_parsed;
526 }
528 if(!is_array($parsed)) return;
530 /* Walk through all elements */
531 foreach($parsed as $key => $data){
533 /* Id used to have unique html names */
534 $element_id = $this->object_id."_".$id."_".$obj_id;
536 foreach($_POST as $name => $value){
537 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
538 return(false);
539 }
540 }
543 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
544 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
545 }
547 /* Create elements */
548 switch($key)
549 {
550 /*******************
551 * Address
552 *******************/
554 case "envelope" :
555 case "header" :
556 case "address" :
557 {
558 /* [not] address
559 [address-part: tag]
560 [comparator: tag]
561 [match-type: tag]
562 <header-list: string-list>
563 <key-list: string-list>
564 */
566 /* Possible address parts we can select */
567 $address_parts = $this->address_parts;
568 $comparators = $this->comparators;
569 $match_types = $this->match_types;
570 $operators = $this->operators;
572 $parsed[$key]['LastError'] = "";
574 /* Toggle Inverse ? */
575 if(isset($_POST['toggle_inverse_'.$element_id])){
576 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
577 }
579 /* Check if we want to toggle the expert mode */
580 if(isset($_POST['Toggle_Expert_'.$element_id])){
581 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
582 }
584 /* Get address part */
585 if(isset($_POST['address_part_'.$element_id])){
586 $ap = $_POST['address_part_'.$element_id];
588 if(!isset($address_parts[$ap])){
589 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
590 }
591 $parsed[$key]['Address_Part'] = $ap;
592 }
594 /* Check if match type has changed */
595 if(isset($_POST['matchtype_'.$element_id])){
596 $mt = $_POST['matchtype_'.$element_id];
598 if(!isset($match_types[$mt])){
599 $parsed[$key]['LastError'] = _("Invalid match type given.");
600 }
601 if($mt == ":regex"){
602 $this->parent->add_require("regex");
603 }
604 $parsed[$key]['Match_type'] = $mt;
605 }
607 /* Get the comparator tag, if posted */
608 if(isset($_POST['comparator_'.$element_id])){
609 $cp = $_POST['comparator_'.$element_id];
611 if(!isset($comparators[$cp])){
612 $parsed[$key]['LastError'] = _("Invalid operator given.");
613 }
614 $parsed[$key]['Comparator'] = $cp;
616 if($cp == "i;ascii-numeric"){
617 $this->parent->add_require("comparator-i;ascii-numeric");
618 }
619 }
621 /* In case of :count and :value match types
622 * we have a special match operator we should save.
623 */
624 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
625 if(isset($_POST['operator_'.$element_id])){
626 $op = $_POST['operator_'.$element_id];
628 if(!isset($operators[$op])){
629 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
630 }
631 $parsed[$key]['Match_type_value'] = $op;
632 }
633 }
635 /* Get the address fields we should check, they are seperated by , */
636 if(isset($_POST['keys_'.$element_id])){
637 $vls = stripslashes($_POST['keys_'.$element_id]);
638 $tmp = array();
640 $tmp2 = split(",",$vls);
641 foreach($tmp2 as $val){
642 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
643 }
644 $parsed[$key]['Key_List'] = $tmp;
645 }
647 /* Get the values should check for, they are seperated by , */
648 if(isset($_POST['values_'.$element_id])){
649 $vls = stripslashes($_POST['values_'.$element_id]);
650 $tmp = array();
652 $tmp2 = split(",",$vls);
653 foreach($tmp2 as $val){
654 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
655 }
656 $parsed[$key]['Value_List'] = $tmp;
657 }
658 break;
659 }
661 /*******************
662 * TRUE FALSE
663 *******************/
665 case "true" :
666 case "false" :
667 {
668 $name = 'boolean_'.$element_id;
669 if(isset($_POST[$name])){
670 $key2 = $_POST[$name];
672 if($key != $key2) {
673 $parsed = array($key2 => $key2);
674 }
675 }
676 break;
677 }
679 /*******************
680 * Exists
681 *******************/
683 case "exists" :
684 {
685 /* Toggle Inverse ? */
686 if(isset($_POST['toggle_inverse_'.$element_id])){
687 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
688 }
690 /* get list of match values */
691 if(isset($_POST['Values_'.$element_id])){
692 $vls = stripslashes($_POST['Values_'.$element_id]);
693 $tmp = array();
695 $tmp2 = split(",",$vls);
696 foreach($tmp2 as $val){
697 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
698 }
699 $parsed['exists']['Values'] = $tmp;
700 }
701 break;
702 }
704 /*******************
705 * Size
706 *******************/
708 case "size" :
709 {
710 $Match_types = array( ":over" => _("greater than") ,
711 ":under" => _("lower than"));
713 $Units = array( "M" => _("Megabyte"),
714 "K" => _("Kilobyte"),
715 "" => _("Bytes"));
717 /* Toggle Inverse ? */
718 if(isset($_POST['toggle_inverse_'.$element_id])){
719 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
720 }
722 /* Reset error */
723 $parsed[$key]['LastError'] ="";
725 /* Get match type */
726 if(isset($_POST['Match_type_'.$element_id])){
727 $mt = $_POST['Match_type_'.$element_id];
728 if(!isset($Match_types[$mt])){
729 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
730 }
731 $parsed[$key]['Match_type'] = $mt;
732 }
734 /* Get old values */
735 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
736 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
738 /* Get value */
739 if(isset($_POST['Value_'.$element_id])){
740 $vl = $_POST['Value_'.$element_id];
742 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
743 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
744 }
745 $value = preg_replace("/[^0-9]/","",$vl);
746 }
748 /* Get unit */
749 if(isset($_POST['Value_Unit_'.$element_id])){
750 $ut = $_POST['Value_Unit_'.$element_id];
752 if(!isset($Units[$ut])){
753 $parsed[$key]['LastError'] = _("No valid unit selected");
754 }
755 $unit = $ut;
756 }
757 $parsed[$key]['Value_List'] = array();
758 $parsed[$key]['Value_List'][0] = $value.$unit;
759 break;
760 }
762 /*******************
763 * Allof
764 *******************/
766 case "allof" :
767 {
768 if(isset($_POST['toggle_inverse_'.$element_id])){
769 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
770 }
771 foreach($data as $key2 => $dat){
772 if(($key2 === "Inverse") && ($key2 == "Inverse")){
773 continue;
774 }
775 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
776 if($tmp_data != false){
777 $parsed[$key][$key2] = $tmp_data;
778 }else{
779 unset( $parsed[$key][$key2]);
780 }
781 }
782 break ;
783 }
785 /*******************
786 * Anyof
787 *******************/
789 case "anyof" :
790 {
791 if(isset($_POST['toggle_inverse_'.$element_id])){
792 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
793 }
794 foreach($data as $key2 => $dat){
795 if(($key2 === "Inverse") && ($key2 == "Inverse")){
796 continue;
797 }
798 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
799 if($tmp_data != false){
800 $parsed[$key][$key2] = $tmp_data;
801 }else{
802 unset( $parsed[$key][$key2]);
803 }
804 }
805 break ;
806 }
807 }
808 }
809 return($parsed);
810 }
813 /* Return html element for IF */
814 function execute()
815 {
816 /* Create title */
817 $name = "<img alt='' src='images/small_filter.png' class='center'>";
818 $name .= "<b>"._("Condition")."</b>";
819 if($this->TYPE == "if"){
820 $name .= " - "._("If");
821 }elseif($this->TYPE == "elsif"){
822 $name .= " - "._("Else if");
823 }else{
824 $name .= " - "._("Else");
825 }
827 $smarty = get_smarty();
828 $smarty->assign("ID", $this->object_id);
830 /* Only display navigation elements if necessary */
831 if($this->TYPE == "if"){
832 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
833 }else{
834 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
835 }
837 $smarty->assign("Name", $name);
838 $smarty->assign("Contents", $this->get_as_html());
840 if($this->TYPE == "if"){
841 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
842 }else{
843 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
844 }
845 $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
846 return($str);
847 }
850 /* Returns all elements as html */
851 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
852 {
853 $ret ="";
854 if($parsed == NULL){
855 $parsed = $this->_parsed;
856 }
858 if((!is_array($parsed)) || !count($parsed)) {
859 $smarty = get_smarty();
860 $smarty->assign("ID",$this->object_id);
861 $smarty->assign("DisplayAdd",TRUE);
862 $smarty->assign("DisplayDel",FALSE);
863 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
864 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
865 return($ret);
866 }
868 /* Walk through all elements */
869 foreach($parsed as $key => $data){
871 /* Create Inverse Tag */
872 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
873 $Inverse = TRUE;
874 }else{
875 $Inverse = FALSE;
876 }
878 /* Id used to have unique html names */
879 $element_id = $this->object_id."_".$id."_".$obj_id;
881 /* Create elements */
882 switch($key)
883 {
885 /*******************
886 * TRUE FALSE
887 *******************/
889 case "true" :
890 case "false" :
891 {
892 /* Inverse element if required */
893 if($Inverse){
894 if($key == "true"){
895 $key = "false";
896 }else{
897 $key = "true";
898 }
899 }
901 /* Get template */
902 $smarty = get_smarty();
903 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
904 $smarty->assign("selected" , $key);
905 $smarty->assign("ID" , $element_id);
906 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
907 break;
908 }
911 /*******************
912 * Header
913 *******************/
915 case "header":
916 {
917 $address_parts = $this->address_parts;
918 $comparators = $this->comparators;
919 $match_types = $this->match_types;
920 $operators = $this->operators;
922 $smarty = get_smarty();
923 $smarty->assign("comparators",$comparators);
924 $smarty->assign("match_types",$match_types);
925 $smarty->assign("operators",$operators);
926 $smarty->assign("LastError",$data['LastError']);
927 $smarty->assign("match_type", $data['Match_type']);
928 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
929 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
931 $keys = "";
932 foreach($data['Key_List'] as $key){
933 $keys .= $key.", ";
934 }
935 $keys = preg_replace("/,$/","",trim($keys));
937 $values = "";
938 foreach($data['Value_List'] as $key){
939 $values .= $key.", ";
940 }
941 $values = preg_replace("/,$/","",trim($values));
943 $smarty->assign("keys",$keys);
944 $smarty->assign("Inverse",$Inverse);
945 $smarty->assign("values",$values);
946 $smarty->assign("Expert", $data['Expert']);
948 $smarty->assign("ID" , $element_id);
949 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
950 break;
951 }
954 /*******************
955 * Envelope
956 *******************/
958 case "envelope":
959 {
960 $address_parts = $this->address_parts;
961 $comparators = $this->comparators;
962 $match_types = $this->match_types;
963 $operators = $this->operators;
965 $smarty = get_smarty();
966 $smarty->assign("Inverse",$Inverse);
967 $smarty->assign("comparators",$comparators);
968 $smarty->assign("Expert", $data['Expert']);
969 $smarty->assign("match_types",$match_types);
970 $smarty->assign("operators",$operators);
971 $smarty->assign("LastError",$data['LastError']);
972 $smarty->assign("match_type", $data['Match_type']);
973 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
974 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
976 $keys = "";
977 foreach($data['Key_List'] as $key){
978 $keys .= $key.", ";
979 }
980 $keys = preg_replace("/,$/","",trim($keys));
982 $values = "";
983 foreach($data['Value_List'] as $key){
984 $values .= $key.", ";
985 }
986 $values = preg_replace("/,$/","",trim($values));
987 $smarty->assign("keys",$keys);
988 $smarty->assign("values",$values);
990 $smarty->assign("ID" , $element_id);
991 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
992 break;
993 }
996 /*******************
997 * Address
998 *******************/
1000 case "address" :
1001 {
1002 $address_parts = $this->address_parts;
1003 $comparators = $this->comparators;
1004 $match_types = $this->match_types;
1005 $operators = $this->operators;
1007 $smarty = get_smarty();
1008 $smarty->assign("Inverse",$Inverse);
1009 $smarty->assign("address_parts",$address_parts);
1010 $smarty->assign("comparators",$comparators);
1011 $smarty->assign("match_types",$match_types);
1012 $smarty->assign("LastError",$data['LastError']);
1013 $smarty->assign("operators",$operators);
1014 $smarty->assign("match_type", $data['Match_type']);
1015 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
1016 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
1017 $smarty->assign("address_part", $data['Address_Part']);
1018 $smarty->assign("Expert", $data['Expert']);
1020 $keys = "";
1021 foreach($data['Key_List'] as $key){
1022 $keys .= $key.", ";
1023 }
1024 $keys = preg_replace("/,$/","",trim($keys));
1026 $values = "";
1027 foreach($data['Value_List'] as $key){
1028 $values .= $key.", ";
1029 }
1030 $values = preg_replace("/,$/","",trim($values));
1032 $smarty->assign("keys",$keys);
1033 $smarty->assign("values", $values);
1034 $smarty->assign("ID" , $element_id);
1035 $str = $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
1036 $ret .= $str;
1037 break;
1038 }
1041 /*******************
1042 * Size
1043 *******************/
1045 case "size" :
1046 {
1047 $Match_types = array( ":over" => _("greater than") ,
1048 ":under" => _("lower than"));
1050 $Units = array( "M" => _("Megabyte"),
1051 "K" => _("Kilobyte"),
1052 "" => _("Bytes"));
1054 $Match_type = $data['Match_type'];
1055 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1056 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1058 $LastError = "";
1059 if(isset($data['LastError'])){
1060 $LastError = $data['LastError'];
1061 }
1063 $smarty = get_smarty();
1064 $smarty->assign("Inverse",$Inverse);
1065 $smarty->assign("LastError",$LastError);
1066 $smarty->assign("Match_types",$Match_types);
1067 $smarty->assign("Units",$Units);
1068 $smarty->assign("Match_type",$Match_type);
1069 $smarty->assign("Value",$Value);
1070 $smarty->assign("Value_Unit",$Value_Unit);
1071 $smarty->assign("ID" , $element_id);
1072 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1073 break;
1074 }
1076 /*******************
1077 * Exists
1078 *******************/
1080 case "exists" :
1081 {
1082 $LastError = "";
1083 if(isset($data['LastError'])){
1084 $LastError = $data['LastError'];
1085 }
1087 $Values = "";
1088 foreach($data['Values'] as $val){
1089 $Values .= $val.", ";
1090 }
1091 $Values = preg_replace("/,$/","",trim($Values));
1093 $smarty = get_smarty();
1094 $smarty->assign("LastError",$LastError);
1095 $smarty->assign("Values",$Values);
1096 $smarty->assign("Inverse",$Inverse);
1097 $smarty->assign("ID" , $element_id);
1098 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1099 break;
1100 }
1103 /*******************
1104 * All of
1105 *******************/
1107 case "allof" :
1108 {
1109 $Contents = "";
1110 foreach($data as $key => $dat){
1111 if(($key === "Inverse") && ($key == "Inverse")){
1112 continue;
1113 }
1114 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1115 }
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%%/",_("Click here to add a new test"),$cont_tmp);
1124 $smarty->assign("Inverse",$Inverse);
1125 $smarty->assign("Contents",$cont_tmp.$Contents);
1126 $smarty->assign("ID" , $element_id);
1127 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1129 $ret = $allof_tmp;
1130 break ;
1131 }
1134 /*******************
1135 * Any of
1136 *******************/
1138 case "anyof" :
1139 {
1140 $Contents = "";
1141 foreach($data as $key => $dat){
1142 if(($key === "Inverse") && ($key == "Inverse")){
1143 continue;
1144 }
1145 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1146 }
1147 $smarty = get_smarty();
1148 $smarty->assign("ID" , $element_id);
1149 $smarty->assign("DisplayAdd",TRUE);
1150 $smarty->assign("DisplayDel",FALSE);
1151 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1152 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1154 $smarty->assign("Inverse",$Inverse);
1155 $smarty->assign("Contents",$cont_tmp.$Contents);
1156 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1158 $ret = $allof_tmp;
1160 break ;
1161 }
1162 default :
1163 {
1164 trigger_error(_("Unhandled switch type"));
1165 }
1166 }
1167 }
1169 if(!isset($smarty)){
1170 $smarty =get_smarty();
1171 }
1173 $smarty->assign("ID",$element_id);
1174 $smarty->assign("DisplayAdd",FALSE);
1175 $smarty->assign("DisplayDel",TRUE);
1176 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1177 $ret = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($ret,"\\"),$str);
1178 return($ret);
1179 }
1182 /* Parse given token identified by $data[$id]
1183 * and return the parsed tokens.
1184 */
1185 function _parse($data,$id = 0)
1186 {
1187 $av_match_type = array();
1188 foreach($this->match_types as $name => $description){
1189 $av_match_type[] = $name;
1190 }
1191 $av_match_type[] = ":over";
1192 $av_match_type[] = ":under";
1196 $av_methods= array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1197 $type = $data[$id]['text'];
1198 $tmp = array();
1200 /* Is there an identifier named 'not' to inverse this filter ? */
1201 $Inverse = FALSE;
1202 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1203 $Inverse = TRUE;
1204 $id ++;
1205 $type = $data[$id]['text'];
1206 }
1208 switch($type)
1209 {
1211 /****************
1212 * Parse - Envelope / Header / Address
1213 ****************/
1215 case "envelope" :
1216 case "header":
1217 case "address" :
1218 {
1219 /* Address matches are struckture as follows :
1220 [not]
1221 address
1222 [address-part: tag] all|localpart|domain|user|detail
1223 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1224 [match-type: tag] is|contains|matches|count|value
1225 <header-list: string-list>
1226 <key-list: string-list>
1227 */
1230 $part = "(:all|:localpart|:domain)";
1231 $operator = "(:regex|:contains|:is|:matches|:count|:value)";
1232 $value_op = "(lt|le|eq|ge|gt|ne)";
1234 $Address_Part = "";
1235 $Comparator = "";
1236 $Match_type = "";
1237 $Match_type_value = "";
1239 $Key_List = array();
1240 $Value_List = array();
1242 for($i = 0 ; $i < count($data) ; $i ++){
1244 /* Get next node */
1245 $node = $data[$i];
1247 /* Check address part definition */
1248 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1249 $Address_Part = $node['text'];
1250 }
1252 /* Check for match type */
1253 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1254 $Match_type = $node['text'];
1256 /* Get value operator */
1257 if(in_array($Match_type,array(":value",":count"))){
1258 $i ++;
1259 $node = $data[$i];
1261 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1262 $Match_type_value = $node['text'];
1263 }
1264 }
1265 }
1267 /* Check for a comparator */
1268 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1269 $i ++;
1270 $node = $data[$i];
1271 $Comparator = $node['text'];
1272 }
1274 /* Check for Key_List */
1275 elseif(count(sieve_get_strings($data,$i))){
1276 $tmp2 = sieve_get_strings($data,$i);
1277 $i = $tmp2['OFFSET'];
1279 if(!count($Key_List)){
1280 $Key_List = $tmp2['STRINGS'];
1281 }else{
1282 $Value_List = $tmp2['STRINGS'];
1283 }
1284 }
1286 }
1289 /* Add to Tree */
1290 $values = array( "Inverse" => $Inverse,
1291 "Comparator" => $Comparator,
1292 "Expert" => FALSE,
1293 "Match_type" => $Match_type,
1294 "Match_type_value"=> $Match_type_value,
1295 "Key_List" => $Key_List,
1296 "Value_List" => $Value_List) ;
1297 if($type == "address"){
1298 $values["Address_Part"] = $Address_Part;
1299 }
1300 $tmp[$type] = $values;
1301 $tmp[$type]['LastError'] = "";
1302 break;
1303 }
1306 /****************
1307 * Parse - Size
1308 ****************/
1310 case "size":
1311 {
1313 $ops = "(:over|:under)";
1315 $Match_type = "";
1317 for($i = $id ; $i < count($data); $i++){
1319 /* Get current node */
1320 $node = $data[$i];
1322 /* Get tag (under / over) */
1323 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1324 $Match_type = $node['text'];
1325 }
1327 /* Get Value_List, the value that we want to match for */
1328 elseif(count(sieve_get_strings($data,$i))){
1329 $tmp2 = sieve_get_strings($data,$i);
1330 $i = $tmp2['OFFSET'];
1332 $Value_List = $tmp2['STRINGS'];
1333 }
1334 }
1336 $tmp[$type]= array( "Inverse" => $Inverse,
1337 "Match_type" => $Match_type,
1338 "Value_List" => $Value_List);
1339 $tmp[$type]['LastError'] = "";
1340 break;
1341 }
1344 /****************
1345 * Parse - True / False
1346 ****************/
1348 case "true":
1349 {
1350 $tmp['true'] = "true";
1351 $tmp['true']['LastError'] = "";
1352 break;
1353 }
1354 case "false":
1355 {
1356 $tmp['false'] = "false";
1357 $tmp['false']['LastError'] = "";
1358 break;
1359 }
1362 /****************
1363 * Parse - Exists
1364 ****************/
1366 case "exists":
1367 {
1369 /* Skip first values, [if,not,exists] */
1370 $node = $data[$id];
1371 while(in_array($node['text'],array("if","not","exists"))){
1372 $id ++;
1373 $node = $data[$id];
1374 }
1376 /* Get values */
1377 $tmp2 = sieve_get_strings($data,$id);
1380 $tmp['exists'] = array('Inverse' => $Inverse,
1381 'Values' => $tmp2['STRINGS']);
1382 $tmp[$type]['LastError'] = "";
1383 break;
1384 }
1387 /****************
1388 * Parse - Allof
1389 ****************/
1391 case "allof" :
1392 {
1393 /* Get parameter and recursivly call this method
1394 * for each parameter
1395 */
1396 $id ++;
1397 $tmp2 = $this->get_parameter($data,$id);
1399 foreach($tmp2 as $parameter){
1400 $tmp['allof'][] = $this->_parse($parameter);
1401 }
1402 $tmp['allof']['Inverse'] = $Inverse;
1403 break;
1404 }
1407 /****************
1408 * Parse - Anyof
1409 ****************/
1411 case "anyof" :
1412 {
1413 /* Get parameter and recursivly call this method
1414 * for each parameter
1415 */
1416 $id ++;
1417 $tmp2 = $this->get_parameter($data,$id);
1419 foreach($tmp2 as $parameter){
1420 $tmp['anyof'][] = $this->_parse($parameter);
1421 }
1422 $tmp['anyof']['Inverse'] = $Inverse;
1423 break;
1424 }
1425 default : $tmp[$id] = $type;
1426 }
1428 return($tmp);
1429 }
1432 function get_parameter($data,$id)
1433 {
1434 $par = array();
1435 $open_brakets = 0;
1436 $next = NULL;
1437 $num = 0;
1438 for($i = $id ; $i < count($data) ; $i++ ){
1439 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1440 $open_brakets ++;
1441 }
1442 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1443 $num ++;
1444 }
1445 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1446 $par[$num][] = $data[$i];
1447 }
1448 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1449 $open_brakets --;
1450 }
1451 }
1452 return($par);
1453 }
1454 }
1456 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1457 ?>