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 $this->elements = $elements;
53 $this->_parsed = $this->_parse($elements['ELEMENTS'],1);
54 }
57 /* Returns the sieve script for this
58 * if/else tag.
59 */
60 function get_sieve_script_part()
61 {
62 $tmp = "if ".$this->get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1);
63 return($tmp);
64 }
67 /* Recursivly create a sieve script out of the given
68 * tags and tokens provided by $parsed.
69 * $id specifies the depth of the current element.
70 * $obj_id is the current tag-id handled by this function
71 */
72 function get_sieve_script_part_recursive($parsed = NULL,$id = 1,$obj_id=1)
73 {
74 $script ="";
75 if($parsed == NULL){
76 $parsed = $this->_parsed;
77 }
79 /* Walk through all elements */
80 foreach($parsed as $key => $data){
82 /* Create Inverse Tag */
83 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
84 $Inverse = TRUE;
85 }else{
86 $Inverse = FALSE;
87 }
89 /* Create elements */
90 switch($key)
91 {
93 /*******************
94 * True / False
95 *******************/
97 case "true" :
98 case "false" :
99 {
100 /* Invert this test if required */
101 if($Inverse){
102 $script .= "not ";
103 }
105 $script .= $key;
107 break;
108 }
111 /*******************
112 * Address
113 *******************/
115 case "address" :
116 {
117 /* [not] address
118 [address-part: tag]
119 [comparator: tag]
120 [match-type: tag]
121 <header-list: string-list>
122 <key-list: string-list>
123 */
125 /* Invert this test if required */
126 if($Inverse){
127 $script .= "not ";
128 }
130 $script .="address ";
132 /* Add address part tag */
133 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
134 $script .= $data['Address_Part']." ";
135 }
137 /* Add comparator */
138 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
139 $script .= ":comparator ".$data['Comparator']." ";
140 }
142 /* Add match type */
143 $script .= $data['Match_type']." ";
145 /* Add special match type for count and value */
146 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
147 $script .= sieve_create_strings($data['Match_type_value'])." ";
148 }
150 $script .= sieve_create_strings($data['Key_List']);
151 $script .= " ";
152 $script .= sieve_create_strings($data['Value_List']);
153 break;
154 }
157 /*******************
158 * Header
159 *******************/
161 case "header" :
162 {
163 /* [not] header
164 [comparator: tag]
165 [match-type: tag]
166 <header-names: string-list>
167 <key-list: string-list>
168 */
170 /* Invert ? */
171 if($Inverse){
172 $script .= "not ";
173 }
175 $script .="header ";
177 /* Add address part tag */
178 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
179 $script .= $data['Address_Part']." ";
180 }
182 /* Add comparator */
183 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
184 $script .= ":comparator ".$data['Comparator']." ";
185 }
187 /* Add match type */
188 $script .= $data['Match_type']." ";
190 /* Add special match type for count and value */
191 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
192 $script .= sieve_create_strings($data['Match_type_value'])." ";
193 }
195 $script .= sieve_create_strings($data['Key_List']);
196 $script .= " ";
197 $script .= sieve_create_strings($data['Value_List']);
198 break;
199 }
202 /*******************
203 * Envelope
204 *******************/
206 case "envelope" :
207 {
208 /* [not] envelope
209 [address-part: tag]
210 [comparator: tag]
211 [match-type: tag]
212 <envelope-part: string-list>
213 <key-list: string-list>
214 */
216 /* Invert */
217 if($Inverse){
218 $script .= "not ";
219 }
221 $script .="envelope ";
223 /* Add address part tag */
224 if(!empty($data['Address_Part']) && $data['Address_Part'] != ":all"){
225 $script .= $data['Address_Part']." ";
226 }
228 /* Add comparator */
229 if(!empty($data['Comparator']) && $data['Comparator'] != ""){
230 $script .= ":comparator ".$data['Comparator']." ";
231 }
233 /* Add match type */
234 $script .= $data['Match_type']." ";
236 /* Add special match type for count and value */
237 if(in_array($data['Match_type'], array(":value",":count")) && !empty($data['Match_type_value'])) {
238 $script .= sieve_create_strings($data['Match_type_value'])." ";
239 }
241 $script .= sieve_create_strings($data['Key_List']);
242 $script .= " ";
243 $script .= sieve_create_strings($data['Value_List']);
244 break;
245 }
248 /*******************
249 * Exists
250 *******************/
251 case "exists" :
252 {
253 /* [not] exists
254 <header-names: string-list>
255 */
257 /* Invert ? */
258 if($Inverse){
259 $script .= "not ";
260 }
262 $script .= "exists ".sieve_create_strings($data['Values']);
263 break;
264 }
267 /*******************
268 * Size
269 *******************/
270 case "size" :
271 {
272 /* [not] size
273 <":over" / ":under">
274 <limit: number>
275 */
277 /* Invert ? */
278 if($Inverse){
279 $script .= "not ";
280 }
282 /* Add size test */
283 $script .="size ";
284 $script .=$data['Match_type']." ";
285 foreach($data['Value_List'] as $val){
286 $script .= $val." ";
287 }
288 break;
289 }
292 /*******************
293 * Allof
294 *******************/
295 case "anyof" :
296 case "allof" :
297 {
298 /* allof <tests: test-list>
299 anyof <tests: test-list> */
302 /* Add spaces, to indent the code.*/
303 $block = "\n";
304 for($i = 0 ; $i < $id ; $i ++){
305 $block .= SIEVE_INDENT_TAB;
306 }
308 /* Add allof/anyof tag */
309 $script.= " ".$key." ( ";
311 /* Add each test parameter */
312 foreach($data as $key2 => $dat){
313 if(($key2 === "Inverse") && ($key2 == "Inverse")){
314 continue;
315 }
316 $script.= $block.$this->get_sieve_script_part_recursive($dat, ($id +1),$key2).", ";
317 }
319 /* Remove last _,_ and close the tag */
320 $script = preg_replace("/,$/","",trim($script));
321 $script.= $block.")";
322 break ;
323 }
325 default :
326 {
327 $script .= "THERE IS SOME IMPLEMENTATION MISSING FOR SIEVE SCRIPT CREATION :".$key;
328 }
329 }
330 }
331 return($script);
332 }
335 /* Ensure that all changes made on the ui
336 * will be saved.
337 */
338 function save_object()
339 {
340 $tmp = $this->save_object_recursive($parsed = NULL,$id = 1,$obj_id=1);
341 $this->_parsed = $tmp;
342 }
345 /* Recursivly save all ui changes for the
346 * tags and tokens provided by $parsed.
347 * $id specifies the depth of the current element.
348 * $obj_id is the current tag-id handled by this function
349 */
350 function save_object_recursive($parsed = NULL,$id = 1,$obj_id=1)
351 {
352 /* Variable initialization */
353 $ret ="";
354 if($parsed == NULL){
355 $parsed = $this->_parsed;
356 }
358 /* Walk through all elements */
359 foreach($parsed as $key => $data){
361 /* Id used to have unique html names */
362 $element_id = $this->object_id."_".$id."_".$obj_id;
364 /* Create elements */
365 switch($key)
366 {
368 /*******************
369 * Address
370 *******************/
372 case "envelope" :
373 case "header" :
374 case "address" :
375 {
376 /* [not] address
377 [address-part: tag]
378 [comparator: tag]
379 [match-type: tag]
380 <header-list: string-list>
381 <key-list: string-list>
382 */
384 /* Possible address parts we can select */
385 $address_parts = $this->address_parts;
386 $comparators = $this->comparators;
387 $match_types = $this->match_types;
388 $operators = $this->operators;
390 /* Toggle Inverse ? */
391 if(isset($_POST['toggle_inverse_'.$element_id])){
392 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
393 }
395 /* Check if we want to toggle the expert mode */
396 if(isset($_POST['Toggle_Expert_'.$element_id])){
397 $parsed[$key]['Expert'] = !$parsed[$key]['Expert'];
398 }
400 /* Get address part */
401 if(isset($_POST['address_part_'.$element_id])){
402 $ap = $_POST['address_part_'.$element_id];
404 if(isset($address_parts[$ap])){
405 $parsed[$key]['Address_Part'] = $ap;
406 }
407 }
409 /* Check if match type has changed */
410 if(isset($_POST['matchtype_'.$element_id])){
411 $mt = $_POST['matchtype_'.$element_id];
413 if(isset($match_types[$mt])){
414 $parsed[$key]['Match_type'] = $mt;
415 }
416 }
418 /* Get the comparator tag, if posted */
419 if(isset($_POST['comparator_'.$element_id])){
420 $cp = $_POST['comparator_'.$element_id];
422 if(isset($comparators[$cp])){
423 $parsed[$key]['Comparator'] = $cp;
424 }
425 }
427 /* In case of :count and :value match types
428 * we have a special match operator we should save.
429 */
430 if(in_array($parsed[$key]['Match_type'],array(":value",":count"))){
431 if(isset($_POST['operator_'.$element_id])){
432 $op = $_POST['operator_'.$element_id];
434 if(isset($operators[$op])){
435 $parsed[$key]['Match_type_value'] = $op;
436 }
437 }
438 }
440 /* Get the address fields we should check, they are seperated by , */
441 if(isset($_POST['keys_'.$element_id])){
442 $vls = stripslashes($_POST['keys_'.$element_id]);
443 $tmp = array();
445 $tmp2 = split(",",$vls);
446 foreach($tmp2 as $val){
447 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
448 }
449 $parsed[$key]['Key_List'] = $tmp;
450 }
452 /* Get the values should check for, they are seperated by , */
453 if(isset($_POST['values_'.$element_id])){
454 $vls = stripslashes($_POST['values_'.$element_id]);
455 $tmp = array();
457 $tmp2 = split(",",$vls);
458 foreach($tmp2 as $val){
459 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
460 }
461 $parsed[$key]['Value_List'] = $tmp;
462 }
463 break;
464 }
466 /*******************
467 * TRUE FALSE
468 *******************/
470 case "true" :
471 case "false" :
472 {
473 $name = 'boolean_'.$element_id;
474 if(isset($_POST[$name])){
475 $key2 = $_POST[$name];
477 if($key != $key2) {
478 $parsed = array($key2 => $key2);
479 }
480 }
481 break;
482 }
484 /*******************
485 * Exists
486 *******************/
488 case "exists" :
489 {
490 /* Toggle Inverse ? */
491 if(isset($_POST['toggle_inverse_'.$element_id])){
492 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
493 }
495 /* get list of match values */
496 if(isset($_POST['Values_'.$element_id])){
497 $vls = stripslashes($_POST['Values_'.$element_id]);
498 $tmp = array();
500 $tmp2 = split(",",$vls);
501 foreach($tmp2 as $val){
502 $tmp[] = "\"".trim(preg_replace("/\"/","",$val))."\"";
503 }
504 $parsed['exists']['Values'] = $tmp;
505 }
506 break;
507 }
509 /*******************
510 * Size
511 *******************/
513 case "size" :
514 {
515 $Match_types = array( ":over" => _("greater than") ,
516 ":under" => _("lower than"));
518 $Units = array( "M" => _("Megabyte") ,
519 "K" => _("Kilobyte"));
521 /* Reset error */
522 $parsed['size']['LastError'] ="";
524 /* Get match type */
525 if(isset($_POST['Match_type_'.$element_id])){
526 $mt = $_POST['Match_type_'.$element_id];
527 if(isset($Match_types[$mt])){
528 $parsed['size']['Match_type'] = $mt;
529 }else{
530 $parsed['size']['LastError'] = _("Please select a valid match type in the list box below.");
531 }
532 }
534 /* Get old values */
535 $value = preg_replace("/[^0-9]*$/","",$parsed['size']['Value_List'][0]);
536 $unit = preg_replace("/^[0-9]*/","",$parsed['size']['Value_List'][0]);
538 /* Get value */
539 if(isset($_POST['Value_'.$element_id])){
540 $vl = $_POST['Value_'.$element_id];
542 if(is_numeric($vl) && preg_match("/^[0-9]*$/",$vl)){
543 $value = $vl;
544 }else{
545 $parsed['size']['LastError'] = _("Only numeric values are allowed here.");
546 }
547 }
549 /* Get unit */
550 if(isset($_POST['Value_Unit_'.$element_id])){
551 $ut = $_POST['Value_Unit_'.$element_id];
553 if(isset($Units[$ut])){
554 $unit = $ut;
555 }else{
556 $parsed['size']['LastError'] = _("No valid unit selected");
557 }
558 }
560 $parsed['size']['Value_List'][0] = $value.$unit;
561 break;
562 }
564 /*******************
565 * Allof
566 *******************/
568 case "allof" :
569 {
570 if(isset($_POST['toggle_inverse_'.$element_id])){
571 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
572 }
573 foreach($data as $key2 => $dat){
574 if(($key2 === "Inverse") && ($key2 == "Inverse")){
575 continue;
576 }
577 $parsed[$key][$key2] = $this->save_object_recursive($dat, ($id +1),$key2."-".$obj_id);
578 }
579 break ;
580 }
582 /*******************
583 * Anyof
584 *******************/
586 case "anyof" :
587 {
588 if(isset($_POST['toggle_inverse_'.$element_id])){
589 $parsed[$key]['Inverse'] = !$parsed[$key]['Inverse'];
590 }
591 foreach($data as $key2 => $dat){
592 if(($key2 === "Inverse") && ($key2 == "Inverse")){
593 continue;
594 }
595 $parsed[$key][$key2] = $this->save_object_recursive($dat, ($id + 1),$key2."-".$obj_id);
596 }
597 break ;
598 }
599 }
600 }
601 return($parsed);
602 }
605 /* Return html element for IF */
606 function execute()
607 {
608 /* Create title */
609 $name = "<img alt='' src='images/small_filter.png' class='center'>";
610 $name .= "<b>"._("Condition")."</b>";
611 if($this->TYPE == "if"){
612 $name .= " - "._("If");
613 }else{
614 $name .= " - "._("Else");
615 }
617 $smarty = get_smarty();
618 $smarty->assign("ID", $this->object_id);
620 /* Only display navigation elements if necessary */
621 if($this->TYPE == "if"){
622 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
623 }else{
624 $object_container = $smarty->fetch(get_template_path("templates/object_container_clear.tpl",TRUE,dirname(__FILE__)));
625 }
627 $smarty->assign("Name", $name);
628 $smarty->assign("Contents", $this->get_as_html());
629 $object = $smarty->fetch(get_template_path("templates/element_if_else.tpl",TRUE,dirname(__FILE__)));
633 $str = preg_replace("/%%OBJECT_CONTENT%%/",$object,$object_container);
636 return($str);
637 }
640 /* Returns all elements as html */
641 function get_as_html($parsed = NULL,$id = 1,$obj_id=1)
642 {
643 $ret ="";
644 if($parsed == NULL){
645 $parsed = $this->_parsed;
646 }
648 /* Walk through all elements */
649 foreach($parsed as $key => $data){
651 /* Create Inverse Tag */
652 if(is_array($data) && isset($data['Inverse']) && $data['Inverse']){
653 $str_inverse = "<font color='red'><b>"._("Not")."</b></font> ";
654 $Inverse = TRUE;
655 }else{
656 $str_inverse = "";
657 $Inverse = FALSE;
658 }
660 /* Id used to have unique html names */
661 $element_id = $this->object_id."_".$id."_".$obj_id;
663 /* Create elements */
664 switch($key)
665 {
667 /*******************
668 * TRUE FALSE
669 *******************/
671 case "true" :
672 case "false" :
673 {
674 /* Inverse element if required */
675 if($Inverse){
676 if($key == "true"){
677 $key = "false";
678 }else{
679 $key = "true";
680 }
681 }
683 /* Get template */
684 $smarty = get_smarty();
685 $smarty->assign("values" , array("false" => _("False"), "true" => _("True")));
686 $smarty->assign("selected" , $key);
687 $smarty->assign("ID" , $element_id);
688 $ret .= $smarty->fetch(get_template_path("templates/element_boolean.tpl",TRUE,dirname(__FILE__)));
689 break;
690 }
693 /*******************
694 * Header
695 *******************/
697 case "header":
698 {
699 $address_parts = $this->address_parts;
700 $comparators = $this->comparators;
701 $match_types = $this->match_types;
702 $operators = $this->operators;
704 $smarty = get_smarty();
705 $smarty->assign("comparators",$comparators);
706 $smarty->assign("match_types",$match_types);
707 $smarty->assign("operators",$operators);
709 $smarty->assign("match_type", $data['Match_type']);
710 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
711 $smarty->assign("comparator", $data['Comparator']);
714 $keys = "";
715 foreach($data['Key_List'] as $key){
716 $keys .= $key.", ";
717 }
718 $keys = preg_replace("/,$/","",trim($keys));
720 $values = "";
721 foreach($data['Value_List'] as $key){
722 $values .= $key.", ";
723 }
724 $values = preg_replace("/,$/","",trim($values));
726 $smarty->assign("keys",$keys);
727 $smarty->assign("Inverse",$Inverse);
728 $smarty->assign("values",$values);
729 $smarty->assign("Expert", $data['Expert']);
732 $smarty->assign("ID" , $element_id);
733 $ret .= $smarty->fetch(get_template_path("templates/element_header.tpl",TRUE,dirname(__FILE__)));
734 break;
735 }
738 /*******************
739 * Envelope
740 *******************/
742 case "envelope":
743 {
744 $address_parts = $this->address_parts;
745 $comparators = $this->comparators;
746 $match_types = $this->match_types;
747 $operators = $this->operators;
749 $smarty = get_smarty();
750 $smarty->assign("Inverse",$Inverse);
751 $smarty->assign("comparators",$comparators);
752 $smarty->assign("Expert", $data['Expert']);
753 $smarty->assign("match_types",$match_types);
754 $smarty->assign("operators",$operators);
756 $smarty->assign("match_type", $data['Match_type']);
757 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
758 $smarty->assign("comparator", $data['Comparator']);
760 $keys = "";
761 foreach($data['Key_List'] as $key){
762 $keys .= $key.", ";
763 }
764 $keys = preg_replace("/,$/","",trim($keys));
766 $values = "";
767 foreach($data['Value_List'] as $key){
768 $values .= $key.", ";
769 }
770 $values = preg_replace("/,$/","",trim($values));
771 $smarty->assign("keys",$keys);
772 $smarty->assign("values",$values);
774 $smarty->assign("ID" , $element_id);
775 $ret .= $smarty->fetch(get_template_path("templates/element_envelope.tpl",TRUE,dirname(__FILE__)));
776 break;
777 }
780 /*******************
781 * Address
782 *******************/
784 case "address" :
785 {
786 $address_parts = $this->address_parts;
787 $comparators = $this->comparators;
788 $match_types = $this->match_types;
789 $operators = $this->operators;
791 $smarty = get_smarty();
792 $smarty->assign("Inverse",$Inverse);
793 $smarty->assign("address_parts",$address_parts);
794 $smarty->assign("comparators",$comparators);
795 $smarty->assign("match_types",$match_types);
796 $smarty->assign("operators",$operators);
798 $smarty->assign("match_type", $data['Match_type']);
799 $smarty->assign("operator" , preg_replace("/\"/","",$data['Match_type_value']));
800 $smarty->assign("comparator", $data['Comparator']);
801 $smarty->assign("address_part", $data['Address_Part']);
803 $smarty->assign("Expert", $data['Expert']);
805 $keys = "";
806 foreach($data['Key_List'] as $key){
807 $keys .= $key.", ";
808 }
809 $keys = preg_replace("/,$/","",trim($keys));
811 $values = "";
812 foreach($data['Value_List'] as $key){
813 $values .= $key.", ";
814 }
815 $values = preg_replace("/,$/","",trim($values));
816 $smarty->assign("keys",$keys);
817 $smarty->assign("values",$values);
820 $smarty->assign("ID" , $element_id);
821 $ret .= $smarty->fetch(get_template_path("templates/element_address.tpl",TRUE,dirname(__FILE__)));
822 break;
823 }
826 /*******************
827 * Size
828 *******************/
830 case "size" :
831 {
832 $Match_types = array( ":over" => _("greater than") ,
833 ":under" => _("lower than"));
835 $Units = array( "M" => _("Megabyte") ,
836 "K" => _("Kilobyte"));
838 $Match_type = $data['Match_type'];
839 $Value = preg_replace("/[^0-9]/","",$data['Value_List'][0]);
840 $Value_Unit = preg_replace("/[0-9]/","",$data['Value_List'][0]);
842 $LastError = "";
843 if(isset($data['LastError'])){
844 $LastError = $data['LastError'];
845 }
847 $smarty = get_smarty();
848 $smarty->assign("Inverse",$Inverse);
849 $smarty->assign("LastError",$LastError);
850 $smarty->assign("Match_types",$Match_types);
851 $smarty->assign("Units",$Units);
852 $smarty->assign("Match_type",$Match_type);
853 $smarty->assign("Value",$Value);
854 $smarty->assign("Value_Unit",$Value_Unit);
855 $smarty->assign("ID" , $element_id);
856 $ret .= $smarty->fetch(get_template_path("templates/element_size.tpl",TRUE,dirname(__FILE__)));
857 break;
858 }
860 /*******************
861 * Exists
862 *******************/
865 case "exists" :
866 {
867 $LastError = "";
868 if(isset($data['LastError'])){
869 $LastError = $data['LastError'];
870 }
872 $Values = "";
873 foreach($data['Values'] as $val){
874 $Values .= $val.", ";
875 }
876 $Values = preg_replace("/,$/","",trim($Values));
878 $smarty = get_smarty();
879 $smarty->assign("LastError",$LastError);
880 $smarty->assign("Values",$Values);
881 $smarty->assign("Inverse",$Inverse);
882 $smarty->assign("ID" , $element_id);
883 $ret .= $smarty->fetch(get_template_path("templates/element_exists.tpl",TRUE,dirname(__FILE__)));
884 break;
885 }
888 /*******************
889 * All of
890 *******************/
892 case "allof" :
893 {
894 $Contents = "";
895 foreach($data as $key => $dat){
896 if(($key === "Inverse") && ($key == "Inverse")){
897 continue;
898 }
899 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
900 }
901 $smarty = get_smarty();
902 $smarty->assign("Inverse",$Inverse);
903 $smarty->assign("Contents",$Contents);
904 $smarty->assign("ID" , $element_id);
905 $ret .= $smarty->fetch(get_template_path("templates/element_allof.tpl",TRUE,dirname(__FILE__)));
906 break ;
907 }
910 /*******************
911 * Any of
912 *******************/
914 case "anyof" :
915 {
916 $Contents = "";
917 foreach($data as $key => $dat){
918 if(($key === "Inverse") && ($key == "Inverse")){
919 continue;
920 }
921 $Contents .= $this->get_as_html($dat, ($id +1),$key."-".$obj_id);
922 }
923 $smarty = get_smarty();
924 $smarty->assign("Inverse",$Inverse);
925 $smarty->assign("Contents",$Contents);
926 $smarty->assign("ID" , $element_id);
927 $ret .= $smarty->fetch(get_template_path("templates/element_anyof.tpl",TRUE,dirname(__FILE__)));
928 break ;
929 }
930 default :
931 {
932 $ret = "<table width='100%' cellspacing=0 cellpadding=0>
933 <tr>
934 <td style='background-color: #FEDCA9 ; border: solid 1px #EEEEEE'>";
935 $ret.= $key."<br>";
936 $ret.= " </td>
937 </tr>
938 </table>";
939 }
940 }
941 }
942 return($ret);
943 }
946 /* Parse given token identified by $data[$id]
947 * and return the parsed tokens.
948 */
949 function _parse($data,$id = 0)
950 {
951 $av_methods = array("address","allof","anyof","exists","false","header","not","size","true","envelope");
952 $av_match_type= array(":is",":contains",":matches",":over",":count",":value",":under");
953 $type = $data[$id]['text'];
954 $tmp = array();
956 /* Is there an identifier named 'not' to inverse this filter ? */
957 $Inverse = FALSE;
958 if($data[$id]['class'] == "identifier" && $data[$id]['text'] == "not"){
959 $Inverse = TRUE;
960 $id ++;
961 $type = $data[$id]['text'];
962 }
964 switch($type)
965 {
967 /****************
968 * Parse - Envelope / Header / Address
969 ****************/
971 case "envelope" :
972 case "header":
973 case "address" :
974 {
975 /* Address matches are struckture as follows :
976 [not]
977 address
978 [address-part: tag] all|localpart|domain|user|detail
979 [comparator: tag] i;octet i;ascii-casemap i;ascii-numeric
980 [match-type: tag] is|contains|matches|count|value
981 <header-list: string-list>
982 <key-list: string-list>
983 */
986 $part = "(:all|:localpart|:domain)";
987 $operator = "(:contains|:is|:matches|:count|:value)";
988 $value_op = "(lt|le|eq|ge|gt|ne)";
990 $Address_Part = "";
991 $Comparator = "";
992 $Match_type = "";
993 $Match_type_value = "";
995 $Key_List = array();
996 $Value_List = array();
998 for($i = 0 ; $i < count($data) ; $i ++){
1000 /* Get next node */
1001 $node = $data[$i];
1003 /* Check address part definition */
1004 if($node['class'] == "tag" && preg_match("/".$part."/i",$node['text'])){
1005 $Address_Part = $node['text'];
1006 }
1008 /* Check for match type */
1009 elseif($node['class'] == "tag" && preg_match("/".$operator."/i",$node['text'])){
1010 $Match_type = $node['text'];
1012 /* Get value operator */
1013 if(in_array($Match_type,array(":value",":count"))){
1014 $i ++;
1015 $node = $data[$i];
1017 if($node['class'] == "quoted-string" && preg_match("/".$value_op."/",$node['text'])){
1018 $Match_type_value = $node['text'];
1019 }
1020 }
1021 }
1023 /* Check for a comparator */
1024 elseif($node['class'] == "tag" && preg_match("/comparator/",$node['text'])){
1025 $i ++;
1026 $node = $data[$i];
1027 $Comparator = $node['text'];
1028 }
1030 /* Check for Key_List */
1031 elseif(count(sieve_get_strings($data,$i))){
1032 $tmp2 = sieve_get_strings($data,$i);
1033 $i = $tmp2['OFFSET'];
1035 if(!count($Key_List)){
1036 $Key_List = $tmp2['STRINGS'];
1037 }else{
1038 $Value_List = $tmp2['STRINGS'];
1039 }
1040 }
1042 }
1045 /* Add to Tree */
1046 $values = array( "Inverse" => $Inverse,
1047 "Comparator" => $Comparator,
1048 "Expert" => FALSE,
1049 "Match_type" => $Match_type,
1050 "Match_type_value"=> $Match_type_value,
1051 "Key_List" => $Key_List,
1052 "Value_List" => $Value_List) ;
1053 if($type == "address"){
1054 $values["Address_Part"] = $Address_Part;
1055 }
1056 $tmp[$type] = $values;
1057 break;
1058 }
1061 /****************
1062 * Parse - Size
1063 ****************/
1065 case "size":
1066 {
1068 $ops = "(:over|:under)";
1070 $Match_type = "";
1072 for($i = $id ; $i < count($data); $i++){
1074 /* Get current node */
1075 $node = $data[$i];
1077 /* Get tag (under / over) */
1078 if($node['class'] == "tag" && preg_match("/".$ops."/",$node['text'])){
1079 $Match_type = $node['text'];
1080 }
1082 /* Get Value_List, the value that we want to match for */
1083 elseif(count(sieve_get_strings($data,$i))){
1084 $tmp2 = sieve_get_strings($data,$i);
1085 $i = $tmp2['OFFSET'];
1087 $Value_List = $tmp2['STRINGS'];
1088 }
1089 }
1091 $tmp[$type]= array( "Inverse" => $Inverse,
1092 "Match_type" => $Match_type,
1093 "Value_List" => $Value_List);
1094 break;
1095 }
1098 /****************
1099 * Parse - True / False
1100 ****************/
1102 case "true":
1103 {
1104 $tmp['true'] = "true";
1105 break;
1106 }
1107 case "false":
1108 {
1109 $tmp['false'] = "false";
1110 break;
1111 }
1114 /****************
1115 * Parse - Exists
1116 ****************/
1118 case "exists":
1119 {
1121 /* Skip first values, [if,not,exists] */
1122 $node = $data[$id];
1123 while(in_array($node['text'],array("if","not","exists"))){
1124 $id ++;
1125 $node = $data[$id];
1126 }
1128 /* Get values */
1129 $tmp2 = sieve_get_strings($data,$id);
1132 $tmp['exists'] = array('Inverse' => $Inverse,
1133 'Values' => $tmp2['STRINGS']);
1134 break;
1135 }
1138 /****************
1139 * Parse - Allof
1140 ****************/
1142 case "allof" :
1143 {
1144 /* Get parameter and recursivly call this method
1145 * for each parameter
1146 */
1147 $id ++;
1148 $tmp2 = $this->get_parameter($data,$id);
1150 foreach($tmp2 as $parameter){
1151 $tmp['allof'][] = $this->_parse($parameter);
1152 }
1153 $tmp['allof']['Inverse'] = $Inverse;
1154 break;
1155 }
1158 /****************
1159 * Parse - Anyof
1160 ****************/
1162 case "anyof" :
1163 {
1164 /* Get parameter and recursivly call this method
1165 * for each parameter
1166 */
1167 $id ++;
1168 $tmp2 = $this->get_parameter($data,$id);
1170 foreach($tmp2 as $parameter){
1171 $tmp['anyof'][] = $this->_parse($parameter);
1172 }
1173 $tmp['anyof']['Inverse'] = $Inverse;
1174 break;
1175 }
1176 default : $tmp[$id] = $type;
1177 }
1179 return($tmp);
1180 }
1183 function get_parameter($data,$id)
1184 {
1185 $par = array();
1186 $open_brakets = 0;
1187 $next = NULL;
1188 $num = 0;
1189 for($i = $id ; $i < count($data) ; $i++ ){
1190 if(in_array($data[$i]['class'],array("left-parant","left-bracket"))){
1191 $open_brakets ++;
1192 }
1193 if($data[$i]['class'] == "comma" && $open_brakets == 1){
1194 $num ++;
1195 }
1196 if(!in_array($data[$i]['class'],array("comma","left-parant","right-parant")) || $open_brakets >1 ){
1197 $par[$num][] = $data[$i];
1198 }
1199 if(in_array($data[$i]['class'],array("right-parant","right-bracket"))){
1200 $open_brakets --;
1201 }
1202 }
1203 return($par);
1204 }
1205 }
1207 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1208 ?>