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();
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)
21 {
22 /* Possible address parts we can select */
23 $this->address_parts = array(
24 ":all" => _("Complete adress")." ("._("Default").")",
25 ":domain" => _("Domian part") ,
26 ":localpart" => _("Local part"));
28 /* comparator type */
29 $this->comparators = array(
30 "i;ascii-casemap" => _("Case insensitive")." ("._("Default").")",
31 "i;octet" => _("Case sensitive"),
32 "i;ascii-numeric" => _("Numeric"));
34 /* Match types */
35 $this->match_types = array(
36 ":is" => _("is"),
37 ":contains" => _("contains"),
38 ":matches" => _("matches"),
39 ":count" => _("count"),
40 ":value" => _("value is"));
42 /* Operators */
43 $this->operators = array(
44 "lt" => _("less than"),
45 "le" => _("less or equal"),
46 "eq" => _("equals"),
47 "ge" => _("greater or equal"),
48 "gt" => _("greater than"),
49 "ne" => _("not equal"));
51 $this->object_id = $object_id;
52 if($elements!=NULL){
53 $this->elements = $elements;
54 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
55 }
56 }
59 /* Returns the sieve script for this
60 * if/else tag.
61 */
62 function get_sieve_script_part()
63 {
64 $tmp = $this->TYPE." ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
65 return($tmp);
66 }
69 /* Return error msgs */
70 function check()
71 {
72 $check = $this->check_recursive();
73 return($check);
74 }
77 /* Recursivly fetch all error msgs */
78 function check_recursive($parsed = NULL,$id = 1,$obj_id=1)
79 {
80 $ret = array();
81 if($parsed == NULL){
82 $parsed = $this->_parsed;
83 }
85 /* Walk through all elements */
86 foreach($parsed as $key => $data){
88 /* Create elements */
89 switch($key)
90 {
91 /*******************
92 * Allof / Anyof
93 *******************/
94 case "anyof" :
95 case "allof" :
96 {
97 foreach($data as $key2 => $dat){
98 if(($key2 === "Inverse") && ($key2 == "Inverse")){
99 continue;
100 }
101 $msgs = $this->check_recursive($dat, ($id +1),$key2);
103 foreach($msgs as $msg){
104 $ret[] = $msg;
105 }
106 }
107 break;
108 }
110 /*******************
111 * True / False
112 *******************/
114 case "true" :
115 case "fasle" :
116 {
117 /* Can't fail anyway */
118 break;
119 }
121 /*******************
122 * Default
123 *******************/
125 default:
126 {
127 if(isset($data['LastError']) && !empty($data['LastError'])){
128 $ret[] = $data['LastError'];
129 }
130 }
131 }
132 }
133 return($ret);
134 }
137 /* Recursivly create a sieve script out of the given
138 * tags and tokens provided by $parsed.
139 * $id specifies the depth of the current element.
140 * $obj_id is the current tag-id handled by this function
141 */
142 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
143 {
144 $script ="";
145 if($parsed == NULL){
146 $parsed = $this->_parsed;
147 }
150 if(!is_array($parsed)){
151 return;
152 }
154 /* Walk through all elements */
155 foreach($parsed as $key => $data){
157 /* Create Inverse Tag */
158 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
159 $Inverse = TRUE;
160 }else{
161 $Inverse = FALSE;
162 }
164 /* Create elements */
165 switch($key)
166 {
168 /*******************
169 * True / False
170 *******************/
172 case "true" :
173 case "false" :
174 {
175 /* Invert this test if required */
176 if($Inverse){
177 $script .= "not ";
178 }
179 $script .= $key;
180 break;
181 }
184 /*******************
185 * Address
186 *******************/
188 case "address" :
189 {
190 /* [not] address
191 [address-part: tag]
192 [comparator: tag]
193 [match-type: tag]
194 <header-list: string-list>
195 <key-list: string-list>
196 */
198 /* Invert this test if required */
199 if($Inverse){
200 $script .= "not ";
201 }
203 $script .="address ";
205 /* Add address part tag */
206 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
207 $script .= $data['Address_Part']." ";
208 }
210 /* Add comparator */
211 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
212 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
213 }
215 /* Add match type */
216 $script .= $data['Match_type']." ";
218 /* Add special match type for count and value */
219 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
220 $script .= sieve_create_strings($data['Match_type_value'])." ";
221 }
223 $script .= sieve_create_strings($data['Key_List']);
224 $script .= " ";
225 $script .= sieve_create_strings($data['Value_List']);
226 break;
227 }
230 /*******************
231 * Header
232 *******************/
234 case "header" :
235 {
236 /* [not] header
237 [comparator: tag]
238 [match-type: tag]
239 <header-names: string-list>
240 <key-list: string-list>
241 */
243 /* Invert ? */
244 if($Inverse){
245 $script .= "not ";
246 }
248 $script .="header ";
250 /* Add address part tag */
251 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
252 $script .= $data['Address_Part']." ";
253 }
255 /* Add comparator */
256 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
257 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
258 }
260 /* Add match type */
261 $script .= $data['Match_type']." ";
263 /* Add special match type for count and value */
264 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
265 $script .= sieve_create_strings($data['Match_type_value'])." ";
266 }
268 $script .= sieve_create_strings($data['Key_List']);
269 $script .= " ";
270 $script .= sieve_create_strings($data['Value_List']);
271 break;
272 }
275 /*******************
276 * Envelope
277 *******************/
279 case "envelope" :
280 {
281 /* [not] envelope
282 [address-part: tag]
283 [comparator: tag]
284 [match-type: tag]
285 <envelope-part: string-list>
286 <key-list: string-list>
287 */
289 /* Invert */
290 if($Inverse){
291 $script .= "not ";
292 }
294 $script .="envelope ";
296 /* Add address part tag */
297 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
298 $script .= $data['Address_Part']." ";
299 }
301 /* Add comparator */
302 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
303 $script .= preg_replace('/\"\"/',"\"", ":comparator \"".$data['Comparator']."\" ");
304 }
306 /* Add match type */
307 $script .= $data['Match_type']." ";
309 /* Add special match type for count and value */
310 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
311 $script .= sieve_create_strings($data['Match_type_value'])." ";
312 }
314 $script .= sieve_create_strings($data['Key_List']);
315 $script .= " ";
316 $script .= sieve_create_strings($data['Value_List']);
317 break;
318 }
321 /*******************
322 * Exists
323 *******************/
324 case "exists" :
325 {
326 /* [not] exists
327 <header-names: string-list>
328 */
330 /* Invert ? */
331 if($Inverse){
332 $script .= "not ";
333 }
335 $script .= "exists ".sieve_create_strings($data['Values']);
336 break;
337 }
340 /*******************
341 * Size
342 *******************/
343 case "size" :
344 {
345 /* [not] size
346 <":over" / ":under">
347 <limit: number>
348 */
350 /* Invert ? */
351 if($Inverse){
352 $script .= "not ";
353 }
355 /* Add size test */
356 $script .="size ";
357 $script .=$data['Match_type']." ";
358 foreach($data['Value_List'] as $val){
359 $script .= $val." ";
360 }
361 break;
362 }
365 /*******************
366 * Allof
367 *******************/
368 case "anyof" :
369 case "allof" :
370 {
371 /* allof <tests: test-list>
372 anyof <tests: test-list> */
375 /* Add spaces, to indent the code.*/
376 $block = "\n";
377 for($i = 0 ; $i < $id ; $i ++){
378 $block .= SIEVE_INDENT_TAB;
379 }
381 /* Add allof/anyof tag */
382 $script.= " ".$key." ( ";
384 /* Add each test parameter */
385 foreach($data as $key2 => $dat){
386 if(($key2 === "Inverse") && ($key2 == "Inverse")){
387 continue;
388 }
389 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
390 }
392 /* Remove last _,_ and close the tag */
393 $script = preg_replace("/,$/","",trim($script));
394 $script.= $block.")";
395 break ;
396 }
398 default :
399 {
400 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
401 }
402 }
403 }
404 return($script);
405 }
408 function add_test($data,$type)
409 {
410 switch($type)
411 {
412 case "header" :
413 case "address" :
414 case "envelope" :
415 {
416 /* Add to Tree */
417 $values = array( "Inverse" => FALSE,
418 "Comparator" => "",
419 "Expert" => FALSE,
420 "LastError" => "",
421 "Match_type" => ":contains",
422 "Match_type_value"=> "",
423 "Key_List" => array(_("emtpy")),
424 "Value_List" => array(_("empty"))) ;
425 if($type == "address"){
426 $values["Address_Part"] = ":all";
427 }
428 $data[$type]=$values;
429 break;
430 }
431 case "allof" :
432 case "anyof" :
433 {
434 $data[$type] = array("Inverse" => FALSE);
435 break;
436 }
437 case "size" :
438 {
439 $tmp= array(
440 "Inverse" => FALSE,
441 "Match_type" => ":contains",
442 "Value_List" => array(1,"M"));
444 $tmp['LastError'] = "";
445 $data[$type] = $tmp;
446 break;
447 }
448 case "true":
449 {
450 $data['true'] = "true";
451 $data['true']['LastError'] = "";
452 break;
453 }
454 case "false":
455 {
456 $data['false'] = "false";
457 $data['false']['LastError'] = "";
458 break;
459 }
460 case "exists" :
461 {
462 $data['exists'] = array('Inverse' => FALSE,
463 'Values' => array(_("Nothing specified right now")),
464 'LastError' => "");
465 break;
466 }
467 default : echo "Still buggy ";exit;
468 }
470 return($data);
471 }
474 /* Ensure that all changes made on the ui
475 * will be saved.
476 */
477 function save_object()
478 {
480 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$this->object_id])){
481 $this->_parsed = $this->add_test($this->_parsed,$_POST["test_type_to_add_".$this->object_id]);
482 }
484 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
485 $this->_parsed = $tmp;
486 }
489 /* Recursivly save all ui changes for the
490 * tags and tokens provided by $parsed.
491 * $id specifies the depth of the current element.
492 * $obj_id is the current tag-id handled by this function
493 */
494 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
495 {
496 /* Variable initialization */
497 $ret ="";
498 if($parsed == NULL){
499 $parsed = $this->_parsed;
500 }
502 if(!is_array($parsed)) return;
504 /* Walk through all elements */
505 foreach($parsed as $key => $data){
507 /* Id used to have unique html names */
508 $element_id = $this->object_id."_".$id."_".$obj_id;
510 foreach($_POST as $name => $value){
511 if(preg_match("/Remove_Test_Object_".$element_id."_(x|y)/",$name)) {
512 return(false);
513 }
514 }
517 if(isset($_POST['add_type']) && isset($_POST["test_type_to_add_".$element_id])){
518 $parsed[$key][] = $this->add_test(array(),$_POST["test_type_to_add_".$element_id]);
519 }
521 /* Create elements */
522 switch($key)
523 {
524 /*******************
525 * Address
526 *******************/
528 case "envelope" :
529 case "header" :
530 case "address" :
531 {
532 /* [not] address
533 [address-part: tag]
534 [comparator: tag]
535 [match-type: tag]
536 <header-list: string-list>
537 <key-list: string-list>
538 */
540 /* Possible address parts we can select */
541 $address_parts = $this->address_parts;
542 $comparators = $this->comparators;
543 $match_types = $this->match_types;
544 $operators = $this->operators;
546 $parsed[$key]['LastError'] = "";
548 /* Toggle Inverse ? */
549 if(isset($_POST['toggle_inverse_'.$element_id])){
550 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
551 }
553 /* Check if we want to toggle the expert mode */
554 if(isset($_POST['Toggle_Expert_'.$element_id])){
555 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
556 }
558 /* Get address part */
559 if(isset($_POST['address_part_'.$element_id])){
560 $ap = $_POST['address_part_'.$element_id];
562 if(!isset($address_parts[$ap])){
563 $parsed[$key]['LastError'] = _("Invalid type of address part.") ;
564 }
565 $parsed[$key]['Address_Part'] = $ap;
566 }
568 /* Check if match type has changed */
569 if(isset($_POST['matchtype_'.$element_id])){
570 $mt = $_POST['matchtype_'.$element_id];
572 if(!isset($match_types[$mt])){
573 $parsed[$key]['LastError'] = _("Invalid match type given.");
574 }
575 $parsed[$key]['Match_type'] = $mt;
576 }
578 /* Get the comparator tag, if posted */
579 if(isset($_POST['comparator_'.$element_id])){
580 $cp = $_POST['comparator_'.$element_id];
582 if(!isset($comparators[$cp])){
583 $parsed[$key]['LastError'] = _("Invalid operator given.");
584 }
585 $parsed[$key]['Comparator'] = $cp;
586 }
588 /* In case of :count and :value match types
589 * we have a special match operator we should save.
590 */
591 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
592 if(isset($_POST['operator_'.$element_id])){
593 $op = $_POST['operator_'.$element_id];
595 if(!isset($operators[$op])){
596 $parsed[$key]['LastError'] = _("Please specify a valid operator.");
597 }
598 $parsed[$key]['Match_type_value'] = $op;
599 }
600 }
602 /* Get the address fields we should check, they are seperated by , */
603 if(isset($_POST['keys_'.$element_id])){
604 $vls = stripslashes($_POST['keys_'.$element_id]);
605 $tmp = array();
607 $tmp2 = split(",",$vls);
608 foreach($tmp2 as $val){
609 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
610 }
611 $parsed[$key]['Key_List'] = $tmp;
612 }
614 /* Get the values should check for, they are seperated by , */
615 if(isset($_POST['values_'.$element_id])){
616 $vls = stripslashes($_POST['values_'.$element_id]);
617 $tmp = array();
619 $tmp2 = split(",",$vls);
620 foreach($tmp2 as $val){
621 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
622 }
623 $parsed[$key]['Value_List'] = $tmp;
624 }
625 break;
626 }
628 /*******************
629 * TRUE FALSE
630 *******************/
632 case "true" :
633 case "false" :
634 {
635 $name = 'boolean_'.$element_id;
636 if(isset($_POST[$name])){
637 $key2 = $_POST[$name];
639 if($key != $key2) {
640 $parsed = array($key2 => $key2);
641 }
642 }
643 break;
644 }
646 /*******************
647 * Exists
648 *******************/
650 case "exists" :
651 {
652 /* Toggle Inverse ? */
653 if(isset($_POST['toggle_inverse_'.$element_id])){
654 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
655 }
657 /* get list of match values */
658 if(isset($_POST['Values_'.$element_id])){
659 $vls = stripslashes($_POST['Values_'.$element_id]);
660 $tmp = array();
662 $tmp2 = split(",",$vls);
663 foreach($tmp2 as $val){
664 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
665 }
666 $parsed['exists']['Values'] = $tmp;
667 }
668 break;
669 }
671 /*******************
672 * Size
673 *******************/
675 case "size" :
676 {
677 $Match_types = array( ":over" => _("greater than") ,
678 ":under" => _("lower than"));
680 $Units = array( "M" => _("Megabyte") ,
681 "K" => _("Kilobyte"));
683 /* Reset error */
684 $parsed[$key]['LastError'] ="";
686 /* Get match type */
687 if(isset($_POST['Match_type_'.$element_id])){
688 $mt = $_POST['Match_type_'.$element_id];
689 if(!isset($Match_types[$mt])){
690 $parsed[$key]['LastError'] = _("Please select a valid match type in the list box below.");
691 }
692 $parsed[$key]['Match_type'] = $mt;
693 }
695 /* Get old values */
696 $value = preg_replace("/[^0-9]*$/","",$parsed[$key]['Value_List'][0]);
697 $unit = preg_replace("/^[0-9]*/","",$parsed[$key]['Value_List'][0]);
699 /* Get value */
700 if(isset($_POST['Value_'.$element_id])){
701 $vl = $_POST['Value_'.$element_id];
703 if(!(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl))){
704 $parsed[$key]['LastError'] = _("Only numeric values are allowed here.");
705 }
706 $value = preg_replace("/[^0-9]/","",$vl);
707 }
709 /* Get unit */
710 if(isset($_POST['Value_Unit_'.$element_id])){
711 $ut = $_POST['Value_Unit_'.$element_id];
713 if(!isset($Units[$ut])){
714 $parsed[$key]['LastError'] = _("No valid unit selected");
715 }
716 $unit = $ut;
717 }
718 $parsed[$key]['Value_List'] = array();
719 $parsed[$key]['Value_List'][0] = $value.$unit;
720 break;
721 }
723 /*******************
724 * Allof
725 *******************/
727 case "allof" :
728 {
729 if(isset($_POST['toggle_inverse_'.$element_id])){
730 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
731 }
732 foreach($data as $key2 => $dat){
733 if(($key2 === "Inverse") && ($key2 == "Inverse")){
734 continue;
735 }
736 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
737 if($tmp_data != false){
738 $parsed[$key][$key2] = $tmp_data;
739 }else{
740 unset( $parsed[$key][$key2]);
741 }
742 }
743 break ;
744 }
746 /*******************
747 * Anyof
748 *******************/
750 case "anyof" :
751 {
752 if(isset($_POST['toggle_inverse_'.$element_id])){
753 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
754 }
755 foreach($data as $key2 => $dat){
756 if(($key2 === "Inverse") && ($key2 == "Inverse")){
757 continue;
758 }
759 $tmp_data = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
760 if($tmp_data != false){
761 $parsed[$key][$key2] = $tmp_data;
762 }else{
763 unset( $parsed[$key][$key2]);
764 }
765 }
766 break ;
767 }
768 }
769 }
770 return($parsed);
771 }
774 /* Return html element for IF */
775 function execute()
776 {
777 /* Create title */
778 $name = "<img alt='' src='images/small_filter.png' class='center'>";
779 $name .= "<b>"._("Condition")."</b>";
780 if($this->TYPE == "if"){
781 $name .= " - "._("If");
782 }elseif($this->TYPE == "elsif"){
783 $name .= " - "._("Else if");
784 }else{
785 $name .= " - "._("Else");
786 }
788 $smarty = get_smarty();
789 $smarty->assign("ID", $this->object_id);
791 /* Only display navigation elements if necessary */
792 if($this->TYPE == "if"){
793 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
794 }else{
795 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
796 }
798 $smarty->assign("Name", $name);
799 $smarty->assign("Contents", $this->get_as_html());
801 if($this->TYPE == "if"){
802 $object = $smarty->fetch(get_template_path("templates/element_if.tpl",TRUE,dirname(__FILE__)));
803 }else{
804 $object = $smarty->fetch(get_template_path("templates/element_elsif.tpl",TRUE,dirname(__FILE__)));
805 }
806 $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
807 return($str);
808 }
811 /* Returns all elements as html */
812 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
813 {
814 $ret ="";
815 if($parsed == NULL){
816 $parsed = $this->_parsed;
817 }
819 if((!is_array($parsed)) || !count($parsed)) {
820 $smarty = get_smarty();
821 $smarty->assign("ID",$this->object_id);
822 $smarty->assign("DisplayAdd",TRUE);
823 $smarty->assign("DisplayDel",FALSE);
824 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
825 $ret .= preg_replace("/%%OBJECT_CONTENT%%/",_("Empty"),$str);
826 return($ret);
827 }
829 /* Walk through all elements */
830 foreach($parsed as $key => $data){
832 /* Create Inverse Tag */
833 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
834 $Inverse = TRUE;
835 }else{
836 $Inverse = FALSE;
837 }
839 /* Id used to have unique html names */
840 $element_id = $this->object_id."_".$id."_".$obj_id;
842 /* Create elements */
843 switch($key)
844 {
846 /*******************
847 * TRUE FALSE
848 *******************/
850 case "true" :
851 case "false" :
852 {
853 /* Inverse element if required */
854 if($Inverse){
855 if($key == "true"){
856 $key = "false";
857 }else{
858 $key = "true";
859 }
860 }
862 /* Get template */
863 $smarty = get_smarty();
864 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
865 $smarty->assign("selected" , $key);
866 $smarty->assign("ID" , $element_id);
867 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
868 break;
869 }
872 /*******************
873 * Header
874 *******************/
876 case "header":
877 {
878 $address_parts = $this->address_parts;
879 $comparators = $this->comparators;
880 $match_types = $this->match_types;
881 $operators = $this->operators;
883 $smarty = get_smarty();
884 $smarty->assign("comparators",$comparators);
885 $smarty->assign("match_types",$match_types);
886 $smarty->assign("operators",$operators);
887 $smarty->assign("LastError",$data['LastError']);
888 $smarty->assign("match_type", $data['Match_type']);
889 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
890 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
892 $keys = "";
893 foreach($data['Key_List'] as $key){
894 $keys .= $key.", ";
895 }
896 $keys = preg_replace("/,$/","",trim($keys));
898 $values = "";
899 foreach($data['Value_List'] as $key){
900 $values .= $key.", ";
901 }
902 $values = preg_replace("/,$/","",trim($values));
904 $smarty->assign("keys",$keys);
905 $smarty->assign("Inverse",$Inverse);
906 $smarty->assign("values",$values);
907 $smarty->assign("Expert", $data['Expert']);
909 $smarty->assign("ID" , $element_id);
910 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
911 break;
912 }
915 /*******************
916 * Envelope
917 *******************/
919 case "envelope":
920 {
921 $address_parts = $this->address_parts;
922 $comparators = $this->comparators;
923 $match_types = $this->match_types;
924 $operators = $this->operators;
926 $smarty = get_smarty();
927 $smarty->assign("Inverse",$Inverse);
928 $smarty->assign("comparators",$comparators);
929 $smarty->assign("Expert", $data['Expert']);
930 $smarty->assign("match_types",$match_types);
931 $smarty->assign("operators",$operators);
932 $smarty->assign("LastError",$data['LastError']);
933 $smarty->assign("match_type", $data['Match_type']);
934 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
935 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
937 $keys = "";
938 foreach($data['Key_List'] as $key){
939 $keys .= $key.", ";
940 }
941 $keys = preg_replace("/,$/","",trim($keys));
943 $values = "";
944 foreach($data['Value_List'] as $key){
945 $values .= $key.", ";
946 }
947 $values = preg_replace("/,$/","",trim($values));
948 $smarty->assign("keys",$keys);
949 $smarty->assign("values",$values);
951 $smarty->assign("ID" , $element_id);
952 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
953 break;
954 }
957 /*******************
958 * Address
959 *******************/
961 case "address" :
962 {
963 $address_parts = $this->address_parts;
964 $comparators = $this->comparators;
965 $match_types = $this->match_types;
966 $operators = $this->operators;
968 $smarty = get_smarty();
969 $smarty->assign("Inverse",$Inverse);
970 $smarty->assign("address_parts",$address_parts);
971 $smarty->assign("comparators",$comparators);
972 $smarty->assign("match_types",$match_types);
973 $smarty->assign("LastError",$data['LastError']);
974 $smarty->assign("operators",$operators);
975 $smarty->assign("match_type", $data['Match_type']);
976 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
977 $smarty->assign("comparator", preg_replace("/\"/","",$data['Comparator']));
978 $smarty->assign("address_part", $data['Address_Part']);
979 $smarty->assign("Expert", $data['Expert']);
981 $keys = "";
982 foreach($data['Key_List'] as $key){
983 $keys .= $key.", ";
984 }
985 $keys = preg_replace("/,$/","",trim($keys));
987 $values = "";
988 foreach($data['Value_List'] as $key){
989 $values .= $key.", ";
990 }
991 $values = preg_replace("/,$/","",trim($values));
992 $smarty->assign("keys",$keys);
993 $smarty->assign("values",$values);
994 $smarty->assign("ID" , $element_id);
995 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
996 break;
997 }
1000 /*******************
1001 * Size
1002 *******************/
1004 case "size" :
1005 {
1006 $Match_types = array( ":over" => _("greater than") ,
1007 ":under" => _("lower than"));
1009 $Units = array( "M" => _("Megabyte") ,
1010 "K" => _("Kilobyte"));
1012 $Match_type = $data['Match_type'];
1013 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
1014 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
1016 $LastError = "";
1017 if(isset($data['LastError'])){
1018 $LastError = $data['LastError'];
1019 }
1021 $smarty = get_smarty();
1022 $smarty->assign("Inverse",$Inverse);
1023 $smarty->assign("LastError",$LastError);
1024 $smarty->assign("Match_types",$Match_types);
1025 $smarty->assign("Units",$Units);
1026 $smarty->assign("Match_type",$Match_type);
1027 $smarty->assign("Value",$Value);
1028 $smarty->assign("Value_Unit",$Value_Unit);
1029 $smarty->assign("ID" , $element_id);
1030 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
1031 break;
1032 }
1034 /*******************
1035 * Exists
1036 *******************/
1038 case "exists" :
1039 {
1040 $LastError = "";
1041 if(isset($data['LastError'])){
1042 $LastError = $data['LastError'];
1043 }
1045 $Values = "";
1046 foreach($data['Values'] as $val){
1047 $Values .= $val.", ";
1048 }
1049 $Values = preg_replace("/,$/","",trim($Values));
1051 $smarty = get_smarty();
1052 $smarty->assign("LastError",$LastError);
1053 $smarty->assign("Values",$Values);
1054 $smarty->assign("Inverse",$Inverse);
1055 $smarty->assign("ID" , $element_id);
1056 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
1057 break;
1058 }
1061 /*******************
1062 * All of
1063 *******************/
1065 case "allof" :
1066 {
1067 $Contents = "";
1068 foreach($data as $key => $dat){
1069 if(($key === "Inverse") && ($key == "Inverse")){
1070 continue;
1071 }
1072 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1073 }
1075 $smarty = get_smarty();
1076 $smarty->assign("ID" , $element_id);
1077 $smarty->assign("DisplayAdd",TRUE);
1078 $smarty->assign("DisplayDel",FALSE);
1079 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1080 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1082 $smarty->assign("Inverse",$Inverse);
1083 $smarty->assign("Contents",$cont_tmp.$Contents);
1084 $smarty->assign("ID" , $element_id);
1085 $allof_tmp = $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
1087 $ret = $allof_tmp;
1088 break ;
1089 }
1092 /*******************
1093 * Any of
1094 *******************/
1096 case "anyof" :
1097 {
1098 $Contents = "";
1099 foreach($data as $key => $dat){
1100 if(($key === "Inverse") && ($key == "Inverse")){
1101 continue;
1102 }
1103 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
1104 }
1105 $smarty = get_smarty();
1106 $smarty->assign("ID" , $element_id);
1107 $smarty->assign("DisplayAdd",TRUE);
1108 $smarty->assign("DisplayDel",FALSE);
1109 $cont_tmp = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1110 $cont_tmp = preg_replace("/%%OBJECT_CONTENT%%/",_("Klick here to add a new test"),$cont_tmp);
1112 $smarty->assign("Inverse",$Inverse);
1113 $smarty->assign("Contents",$cont_tmp.$Contents);
1114 $allof_tmp = $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
1116 $ret = $allof_tmp;
1118 break ;
1119 }
1120 default :
1121 {
1122 $ret = "<table width='100%' cellspacing=0 cellpadding=0>
1123 <tr>
1124 <td style='background-color: #FEDCA9 ; border: solid 1px #EEEEEE'>";
1125 $ret.= $key."<br>";
1126 $ret.= " </td>
1127 </tr>
1128 </table>";
1129 }
1130 }
1131 }
1133 if(!isset($smarty)){
1134 $smarty =get_smarty();
1135 }
1137 $smarty->assign("ID",$element_id);
1138 $smarty->assign("DisplayAdd",FALSE);
1139 $smarty->assign("DisplayDel",TRUE);
1140 $str = $smarty->fetch(get_template_path("templates/object_test_container.tpl",TRUE,dirname(__FILE__)));
1141 $ret = preg_replace("/%%OBJECT_CONTENT%%/",$ret,$str);
1142 return($ret);
1143 }
1146 /* Parse given token identified by $data[$id]
1147 * and return the parsed tokens.
1148 */
1149 function _parse($data,$id = 0)
1150 {
1151 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
1152 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
1153 $type = $data[$id]['text'];
1154 $tmp = array();
1156 /* Is there an identifier named 'not' to inverse this filter ? */
1157 $Inverse = FALSE;
1158 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
1159 $Inverse = TRUE;
1160 $id ++;
1161 $type = $data[$id]['text'];
1162 }
1164 switch($type)
1165 {
1167 /****************
1168 * Parse - Envelope / Header / Address
1169 ****************/
1171 case "envelope" :
1172 case "header":
1173 case "address" :
1174 {
1175 /* Address matches are struckture as follows :
1176 [not]
1177 address
1178 [address-part: tag] all|localpart|domain|user|detail
1179 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
1180 [match-type: tag] is|contains|matches|count|value
1181 <header-list: string-list>
1182 <key-list: string-list>
1183 */
1186 $part = "(:all|:localpart|:domain)";
1187 $operator = "(:contains|:is|:matches|:count|:value)";
1188 $value_op = "(lt|le|eq|ge|gt|ne)";
1190 $Address_Part = "";
1191 $Comparator = "";
1192 $Match_type = "";
1193 $Match_type_value = "";
1195 $Key_List = array();
1196 $Value_List = array();
1198 for($i = 0 ; $i < count($data) ; $i ++){
1200 /* Get next node */
1201 $node = $data[$i];
1203 /* Check address part definition */
1204 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1205 $Address_Part = $node['text'];
1206 }
1208 /* Check for match type */
1209 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1210 $Match_type = $node['text'];
1212 /* Get value operator */
1213 if(in_array($Match_type,array(":value",":count"))){
1214 $i ++;
1215 $node = $data[$i];
1217 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1218 $Match_type_value = $node['text'];
1219 }
1220 }
1221 }
1223 /* Check for a comparator */
1224 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1225 $i ++;
1226 $node = $data[$i];
1227 $Comparator = $node['text'];
1228 }
1230 /* Check for Key_List */
1231 elseif(count(sieve_get_strings($data,$i))){
1232 $tmp2 = sieve_get_strings($data,$i);
1233 $i = $tmp2['OFFSET'];
1235 if(!count($Key_List)){
1236 $Key_List = $tmp2['STRINGS'];
1237 }else{
1238 $Value_List = $tmp2['STRINGS'];
1239 }
1240 }
1242 }
1245 /* Add to Tree */
1246 $values = array( "Inverse" => $Inverse,
1247 "Comparator" => $Comparator,
1248 "Expert" => FALSE,
1249 "Match_type" => $Match_type,
1250 "Match_type_value"=> $Match_type_value,
1251 "Key_List" => $Key_List,
1252 "Value_List" => $Value_List) ;
1253 if($type == "address"){
1254 $values["Address_Part"] = $Address_Part;
1255 }
1256 $tmp[$type] = $values;
1257 $tmp[$type]['LastError'] = "";
1258 break;
1259 }
1262 /****************
1263 * Parse - Size
1264 ****************/
1266 case "size":
1267 {
1269 $ops = "(:over|:under)";
1271 $Match_type = "";
1273 for($i = $id ; $i < count($data); $i++){
1275 /* Get current node */
1276 $node = $data[$i];
1278 /* Get tag (under / over) */
1279 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1280 $Match_type = $node['text'];
1281 }
1283 /* Get Value_List, the value that we want to match for */
1284 elseif(count(sieve_get_strings($data,$i))){
1285 $tmp2 = sieve_get_strings($data,$i);
1286 $i = $tmp2['OFFSET'];
1288 $Value_List = $tmp2['STRINGS'];
1289 }
1290 }
1292 $tmp[$type]= array( "Inverse" => $Inverse,
1293 "Match_type" => $Match_type,
1294 "Value_List" => $Value_List);
1295 $tmp[$type]['LastError'] = "";
1296 break;
1297 }
1300 /****************
1301 * Parse - True / False
1302 ****************/
1304 case "true":
1305 {
1306 $tmp['true'] = "true";
1307 $tmp['true']['LastError'] = "";
1308 break;
1309 }
1310 case "false":
1311 {
1312 $tmp['false'] = "false";
1313 $tmp['false']['LastError'] = "";
1314 break;
1315 }
1318 /****************
1319 * Parse - Exists
1320 ****************/
1322 case "exists":
1323 {
1325 /* Skip first values, [if,not,exists] */
1326 $node = $data[$id];
1327 while(in_array($node['text'],array("if","not","exists"))){
1328 $id ++;
1329 $node = $data[$id];
1330 }
1332 /* Get values */
1333 $tmp2 = sieve_get_strings($data,$id);
1336 $tmp['exists'] = array('Inverse' => $Inverse,
1337 'Values' => $tmp2['STRINGS']);
1338 $tmp[$type]['LastError'] = "";
1339 break;
1340 }
1343 /****************
1344 * Parse - Allof
1345 ****************/
1347 case "allof" :
1348 {
1349 /* Get parameter and recursivly call this method
1350 * for each parameter
1351 */
1352 $id ++;
1353 $tmp2 = $this->get_parameter($data,$id);
1355 foreach($tmp2 as $parameter){
1356 $tmp['allof'][] = $this->_parse($parameter);
1357 }
1358 $tmp['allof']['Inverse'] = $Inverse;
1359 break;
1360 }
1363 /****************
1364 * Parse - Anyof
1365 ****************/
1367 case "anyof" :
1368 {
1369 /* Get parameter and recursivly call this method
1370 * for each parameter
1371 */
1372 $id ++;
1373 $tmp2 = $this->get_parameter($data,$id);
1375 foreach($tmp2 as $parameter){
1376 $tmp['anyof'][] = $this->_parse($parameter);
1377 }
1378 $tmp['anyof']['Inverse'] = $Inverse;
1379 break;
1380 }
1381 default : $tmp[$id] = $type;
1382 }
1384 return($tmp);
1385 }
1388 function get_parameter($data,$id)
1389 {
1390 $par = array();
1391 $open_brakets = 0;
1392 $next = NULL;
1393 $num = 0;
1394 for($i = $id ; $i < count($data) ; $i++ ){
1395 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1396 $open_brakets ++;
1397 }
1398 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1399 $num ++;
1400 }
1401 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1402 $par[$num][] = $data[$i];
1403 }
1404 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1405 $open_brakets --;
1406 }
1407 }
1408 return($par);
1409 }
1410 }
1412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1413 ?>