1 <?php
3 class sieve_vacation
4 {
5 var $days = FALSE;
6 var $subject = FALSE;
7 var $from = "";
8 var $mime = "";
9 var $handle = "";
10 var $reason = "\"I am not available, currently.\"";
11 var $addresses= array();
12 var $object_id= -1;
13 var $Expert = FALSE;
14 var $parent = NULL;
16 function sieve_vacation($data,$object_id,$parent)
17 {
18 $this->parent = $parent;
19 $this->object_id = $object_id;
20 $this->parent->add_require("vacation");
22 /* Usage: vacation [":days" number] [":subject" string]
23 [":from" string] [":addresses" string-list]
24 [":mime"] [":handle" string] <reason: string> */
26 /* Not all attribute types are supported by the sieve class right now */
27 $known_attrs = array(":days",":subject",":from",":mime",":handle");
29 /* skip if empty */
30 if(($data == NULL) || !is_array($data)) return;
32 /* Walk through elements */
33 for($i = 0 ; $i < count($data['ELEMENTS']) ; $i ++){
35 /* get current element */
36 $node = $data['ELEMENTS'][$i];
38 /* Check if tag is in the specified list of attributes */
39 if($node['class'] == "tag" && in_array($node['text'],$known_attrs)){
41 $var = preg_replace("/\:/","",$node['text']);
42 $this->$var = $data['ELEMENTS'][$i+1]['text'];
43 $i ++;
44 }
46 /* Check for addresses */
47 if($node['class'] == "tag" && $node['text'] == ":addresses") {
48 $this->addresses = array();
49 $i ++;
51 /* Multiple or single address given */
52 if($data['ELEMENTS'][$i]['class'] == "left-bracket"){
53 while($data['ELEMENTS'][$i]['class'] != "right-bracket" && ($i < count($data['ELEMENTS']))){
54 $i ++;
55 if($data['ELEMENTS'][$i]['class'] == "quoted-string"){
56 $this->addresses[] = preg_replace("/\"/i","",$data['ELEMENTS'][$i]['text']);
57 }
58 }
59 }else{
60 $this->addresses[] = $data['ELEMENTS'][$i]['text'] ;
61 }
62 }
64 /* Add the vacation message */
65 if(in_array($node['class'],array("quoted-string","multi-line"))){
67 $tmp = sieve_get_strings($data['ELEMENTS'],$i);
68 $strs= $tmp['STRINGS'];
70 $data = "";
71 foreach($strs as $str){
72 $data .= $str;
73 }
74 $this->reason = $data;//preg_replace("/\"/","",$data);
75 }
76 }
77 }
79 function get_sieve_script_part()
80 {
81 $str = "vacation ";
82 if($this->days){
83 $str.= ":days ".$this->days;
84 }
86 if(count($this->addresses)){
87 $str .= ":addresses ".sieve_create_strings($this->addresses);
88 if($this->subject){
89 $str.= ":subject ".sieve_create_strings($this->subject);
90 }
91 }
92 if($this->mime){
93 $str.= ":mime ".sieve_create_strings($this->mime);
94 }
95 $str .= "\n ".sieve_create_strings($this->reason);
96 return($str." ; \n");
97 }
99 function save_object()
100 {
101 /* Get release date */
102 if(isset($_POST['vacation_release_'.$this->object_id])){
103 $this->days = $_POST['vacation_release_'.$this->object_id];
104 }
106 /* Check if we want to toggle the expert mode */
107 if(isset($_POST['Toggle_Expert_'.$this->object_id])){
108 $this->Expert = !$this->Expert;
109 }
111 /* Get release date */
112 if(isset($_POST['vacation_receiver_'.$this->object_id])){
113 $vr = stripslashes ($_POST['vacation_receiver_'.$this->object_id]);
114 $tmp = array();
115 $tmp2 = split(",",$vr);
116 foreach($tmp2 as $val){
117 $ad = trim(preg_replace("/\"/","",$val));
118 if(!empty($ad)){
119 $tmp[] = "\"".$ad."\"";
120 }
121 }
122 $this->addresses = $tmp;
123 }
125 /* Get reason */
126 if(isset($_POST['vacation_reason_'.$this->object_id])){
127 $vr = stripslashes ($_POST['vacation_reason_'.$this->object_id]);
128 $this->reason = "\"".trim(preg_replace("/\"/","",$vr))."\"";
129 }
130 }
132 function check()
133 {
134 $msgs = array();
135 $err = FALSE;
136 foreach($this->addresses as $addr){
137 if(!is_email(preg_replace("/\"/","",$addr))){
138 $err = true;
139 }
140 }
141 if($err){
142 $msgs[] = _("Alternative sender addresse must be valid email addresses.");
143 }
144 return($msgs);
145 }
147 function execute()
148 {
149 $Addresses = "";
150 foreach($this->addresses as $key){
151 $Addresses .= $key.", ";
152 }
153 $Addresses = preg_replace("/,$/","",trim($Addresses));
155 $smarty = get_smarty();
156 $smarty->assign("LastError",$this->check());
157 $smarty->assign("LastErrorCnt",count($this->check()));
158 $smarty->assign("Reason",$this->reason);
159 $smarty->assign("Addresses",$Addresses);
160 $smarty->assign("Subject",$this->subject);
161 $smarty->assign("Days",$this->days);
162 $smarty->assign("ID",$this->object_id);
163 $smarty->assign("Expert",$this->Expert);
165 $object_container = $smarty->fetch(get_template_path("templates/object_container.tpl",TRUE,dirname(__FILE__)));
166 $object= $smarty->fetch(get_template_path("templates/element_vacation.tpl",TRUE,dirname(__FILE__)));
167 $str = preg_replace("/%%OBJECT_CONTENT%%/",addcslashes($object,"\\"),$object_container);
168 return($str);
169 }
170 }
172 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
173 ?>