commit | author | age
|
50a57e
|
1 |
<?php |
AM |
2 |
|
|
3 |
/** |
|
4 |
* Managesieve Vacation Engine |
|
5 |
* |
|
6 |
* Engine part of Managesieve plugin implementing UI and backend access. |
|
7 |
* |
|
8 |
* Copyright (C) 2011-2014, Kolab Systems AG |
|
9 |
* |
|
10 |
* This program is free software: you can redistribute it and/or modify |
|
11 |
* it under the terms of the GNU General Public License as published by |
|
12 |
* the Free Software Foundation, either version 3 of the License, or |
|
13 |
* (at your option) any later version. |
|
14 |
* |
|
15 |
* This program is distributed in the hope that it will be useful, |
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 |
* GNU General Public License for more details. |
|
19 |
* |
|
20 |
* You should have received a copy of the GNU General Public License |
|
21 |
* along with this program. If not, see http://www.gnu.org/licenses/. |
|
22 |
*/ |
|
23 |
|
|
24 |
class rcube_sieve_vacation extends rcube_sieve_engine |
|
25 |
{ |
613f96
|
26 |
protected $error; |
dc9cc7
|
27 |
protected $script_name; |
AM |
28 |
protected $vacation = array(); |
613f96
|
29 |
|
50a57e
|
30 |
function actions() |
AM |
31 |
{ |
|
32 |
$error = $this->start('vacation'); |
|
33 |
|
|
34 |
// find current vacation rule |
|
35 |
if (!$error) { |
|
36 |
$this->vacation_rule(); |
|
37 |
$this->vacation_post(); |
|
38 |
} |
dc9cc7
|
39 |
|
50a57e
|
40 |
$this->plugin->add_label('vacation.saving'); |
AM |
41 |
$this->rc->output->add_handlers(array( |
|
42 |
'vacationform' => array($this, 'vacation_form'), |
|
43 |
)); |
|
44 |
|
|
45 |
$this->rc->output->set_pagetitle($this->plugin->gettext('vacation')); |
|
46 |
$this->rc->output->send('managesieve.vacation'); |
|
47 |
} |
|
48 |
|
dc9cc7
|
49 |
/** |
AM |
50 |
* Find and load sieve script with/for vacation rule |
|
51 |
* |
1b39d9
|
52 |
* @param string $script_name Optional script name |
AM |
53 |
* |
dc9cc7
|
54 |
* @return int Connection status: 0 on success, >0 on failure |
AM |
55 |
*/ |
1b39d9
|
56 |
protected function load_script($script_name = null) |
dc9cc7
|
57 |
{ |
AM |
58 |
if ($this->script_name !== null) { |
|
59 |
return 0; |
|
60 |
} |
|
61 |
|
|
62 |
$list = $this->list_scripts(); |
|
63 |
$master = $this->rc->config->get('managesieve_kolab_master'); |
|
64 |
$included = array(); |
|
65 |
|
|
66 |
$this->script_name = false; |
|
67 |
|
|
68 |
// first try the active script(s)... |
|
69 |
if (!empty($this->active)) { |
|
70 |
// Note: there can be more than one active script on KEP:14-enabled server |
|
71 |
foreach ($this->active as $script) { |
|
72 |
if ($this->sieve->load($script)) { |
|
73 |
foreach ($this->sieve->script->as_array() as $rule) { |
|
74 |
if (!empty($rule['actions'])) { |
|
75 |
if ($rule['actions'][0]['type'] == 'vacation') { |
|
76 |
$this->script_name = $script; |
|
77 |
return 0; |
|
78 |
} |
|
79 |
else if (empty($master) && $rule['actions'][0]['type'] == 'include') { |
|
80 |
$included[] = $rule['actions'][0]['target']; |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
// ...else try scripts included in active script (not for KEP:14) |
|
88 |
foreach ($included as $script) { |
|
89 |
if ($this->sieve->load($script)) { |
|
90 |
foreach ($this->sieve->script->as_array() as $rule) { |
|
91 |
if (!empty($rule['actions']) && $rule['actions'][0]['type'] == 'vacation') { |
|
92 |
$this->script_name = $script; |
|
93 |
return 0; |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
// try all other scripts |
|
101 |
if (!empty($list)) { |
|
102 |
// else try included scripts |
|
103 |
foreach (array_diff($list, $included, $this->active) as $script) { |
|
104 |
if ($this->sieve->load($script)) { |
|
105 |
foreach ($this->sieve->script->as_array() as $rule) { |
|
106 |
if (!empty($rule['actions']) && $rule['actions'][0]['type'] == 'vacation') { |
|
107 |
$this->script_name = $script; |
|
108 |
return 0; |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
// none of the scripts contains existing vacation rule |
|
115 |
// use any (first) active or just existing script (in that order) |
|
116 |
if (!empty($this->active)) { |
|
117 |
$this->sieve->load($this->script_name = $this->active[0]); |
|
118 |
} |
|
119 |
else { |
|
120 |
$this->sieve->load($this->script_name = $list[0]); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
return $this->sieve->error(); |
|
125 |
} |
|
126 |
|
50a57e
|
127 |
private function vacation_rule() |
AM |
128 |
{ |
04009e
|
129 |
if ($this->script_name === false || $this->script_name === null || !$this->sieve->load($this->script_name)) { |
50a57e
|
130 |
return; |
AM |
131 |
} |
|
132 |
|
dc9cc7
|
133 |
$list = array(); |
AM |
134 |
$active = in_array($this->script_name, $this->active); |
50a57e
|
135 |
|
AM |
136 |
// find (first) vacation rule |
|
137 |
foreach ($this->script as $idx => $rule) { |
|
138 |
if (empty($this->vacation) && !empty($rule['actions']) && $rule['actions'][0]['type'] == 'vacation') { |
c883f6
|
139 |
foreach ($rule['actions'] as $act) { |
AM |
140 |
if ($act['type'] == 'discard' || $act['type'] == 'keep') { |
|
141 |
$action = $act['type']; |
|
142 |
} |
|
143 |
else if ($act['type'] == 'redirect') { |
|
144 |
$action = $act['copy'] ? 'copy' : 'redirect'; |
|
145 |
$target = $act['target']; |
|
146 |
} |
|
147 |
} |
|
148 |
|
50a57e
|
149 |
$this->vacation = array_merge($rule['actions'][0], array( |
AM |
150 |
'idx' => $idx, |
dc9cc7
|
151 |
'disabled' => $rule['disabled'] || !$active, |
50a57e
|
152 |
'name' => $rule['name'], |
AM |
153 |
'tests' => $rule['tests'], |
c883f6
|
154 |
'action' => $action ?: 'keep', |
AM |
155 |
'target' => $target, |
50a57e
|
156 |
)); |
AM |
157 |
} |
dc9cc7
|
158 |
else if ($active) { |
50a57e
|
159 |
$list[$idx] = $rule['name']; |
AM |
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
$this->vacation['list'] = $list; |
|
164 |
} |
|
165 |
|
|
166 |
private function vacation_post() |
|
167 |
{ |
|
168 |
if (empty($_POST)) { |
|
169 |
return; |
|
170 |
} |
|
171 |
|
147530
|
172 |
$date_extension = in_array('date', $this->exts); |
JL |
173 |
$regex_extension = in_array('regex', $this->exts); |
|
174 |
|
581b6b
|
175 |
// set user's timezone |
AM |
176 |
try { |
|
177 |
$timezone = new DateTimeZone($this->rc->config->get('timezone', 'GMT')); |
|
178 |
} |
|
179 |
catch (Exception $e) { |
|
180 |
$timezone = new DateTimeZone('GMT'); |
|
181 |
} |
|
182 |
|
50a57e
|
183 |
$status = rcube_utils::get_input_value('vacation_status', rcube_utils::INPUT_POST); |
162bc8
|
184 |
$from = rcube_utils::get_input_value('vacation_from', rcube_utils::INPUT_POST); |
50a57e
|
185 |
$subject = rcube_utils::get_input_value('vacation_subject', rcube_utils::INPUT_POST, true); |
AM |
186 |
$reason = rcube_utils::get_input_value('vacation_reason', rcube_utils::INPUT_POST, true); |
|
187 |
$addresses = rcube_utils::get_input_value('vacation_addresses', rcube_utils::INPUT_POST, true); |
|
188 |
$interval = rcube_utils::get_input_value('vacation_interval', rcube_utils::INPUT_POST); |
|
189 |
$interval_type = rcube_utils::get_input_value('vacation_interval_type', rcube_utils::INPUT_POST); |
|
190 |
$date_from = rcube_utils::get_input_value('vacation_datefrom', rcube_utils::INPUT_POST); |
|
191 |
$date_to = rcube_utils::get_input_value('vacation_dateto', rcube_utils::INPUT_POST); |
581b6b
|
192 |
$time_from = rcube_utils::get_input_value('vacation_timefrom', rcube_utils::INPUT_POST); |
AM |
193 |
$time_to = rcube_utils::get_input_value('vacation_timeto', rcube_utils::INPUT_POST); |
50a57e
|
194 |
$after = rcube_utils::get_input_value('vacation_after', rcube_utils::INPUT_POST); |
c883f6
|
195 |
$action = rcube_utils::get_input_value('vacation_action', rcube_utils::INPUT_POST); |
AM |
196 |
$target = rcube_utils::get_input_value('action_target', rcube_utils::INPUT_POST, true); |
|
197 |
$target_domain = rcube_utils::get_input_value('action_domain', rcube_utils::INPUT_POST); |
50a57e
|
198 |
|
AM |
199 |
$interval_type = $interval_type == 'seconds' ? 'seconds' : 'days'; |
|
200 |
$vacation_action['type'] = 'vacation'; |
|
201 |
$vacation_action['reason'] = $this->strip_value(str_replace("\r\n", "\n", $reason)); |
162bc8
|
202 |
$vacation_action['subject'] = trim($subject); |
AM |
203 |
$vacation_action['from'] = trim($from); |
50a57e
|
204 |
$vacation_action['addresses'] = $addresses; |
AM |
205 |
$vacation_action[$interval_type] = $interval; |
|
206 |
$vacation_tests = (array) $this->vacation['tests']; |
|
207 |
|
|
208 |
foreach ((array) $vacation_action['addresses'] as $aidx => $address) { |
|
209 |
$vacation_action['addresses'][$aidx] = $address = trim($address); |
|
210 |
|
|
211 |
if (empty($address)) { |
|
212 |
unset($vacation_action['addresses'][$aidx]); |
|
213 |
} |
|
214 |
else if (!rcube_utils::check_email($address)) { |
|
215 |
$error = 'noemailwarning'; |
|
216 |
break; |
|
217 |
} |
162bc8
|
218 |
} |
AM |
219 |
|
|
220 |
if (!empty($vacation_action['from']) && !rcube_utils::check_email($vacation_action['from'])) { |
|
221 |
$error = 'noemailwarning'; |
50a57e
|
222 |
} |
AM |
223 |
|
|
224 |
if ($vacation_action['reason'] == '') { |
9828c1
|
225 |
$error = 'managesieve.emptyvacationbody'; |
50a57e
|
226 |
} |
9828c1
|
227 |
|
50a57e
|
228 |
if ($vacation_action[$interval_type] && !preg_match('/^[0-9]+$/', $vacation_action[$interval_type])) { |
AM |
229 |
$error = 'managesieve.forbiddenchars'; |
|
230 |
} |
|
231 |
|
f72815
|
232 |
// find and remove existing date/regex/true rules |
AM |
233 |
foreach ((array) $vacation_tests as $idx => $t) { |
581b6b
|
234 |
if ($t['test'] == 'currentdate' || $t['test'] == 'true' |
f72815
|
235 |
|| ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received') |
AM |
236 |
) { |
|
237 |
unset($vacation_tests[$idx]); |
|
238 |
} |
|
239 |
} |
|
240 |
|
147530
|
241 |
if ($date_extension) { |
JL |
242 |
foreach (array('date_from', 'date_to') as $var) { |
581b6b
|
243 |
$time = ${str_replace('date', 'time', $var)}; |
AM |
244 |
$date = trim($$var . ' ' . $time); |
|
245 |
|
|
246 |
if ($date && ($dt = rcube_utils::anytodatetime($date, $timezone))) { |
|
247 |
if ($time) { |
|
248 |
$vacation_tests[] = array( |
|
249 |
'test' => 'currentdate', |
5a6415
|
250 |
'part' => 'iso8601', |
581b6b
|
251 |
'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'), |
AM |
252 |
'zone' => $dt->format('O'), |
5a6415
|
253 |
'arg' => str_replace('+00:00', 'Z', strtoupper($dt->format('c'))), |
AM |
254 |
); |
|
255 |
} |
|
256 |
else { |
|
257 |
$vacation_tests[] = array( |
|
258 |
'test' => 'currentdate', |
|
259 |
'part' => 'date', |
|
260 |
'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'), |
|
261 |
'zone' => $dt->format('O'), |
|
262 |
'arg' => $dt->format('Y-m-d'), |
581b6b
|
263 |
); |
AM |
264 |
} |
147530
|
265 |
} |
JL |
266 |
} |
|
267 |
} |
319751
|
268 |
else if ($regex_extension) { |
AM |
269 |
// Add date range rules if range specified |
147530
|
270 |
if ($date_from && $date_to) { |
f72815
|
271 |
if ($tests = self::build_regexp_tests($date_from, $date_to, $error)) { |
AM |
272 |
$vacation_tests = array_merge($vacation_tests, $tests); |
147530
|
273 |
} |
50a57e
|
274 |
} |
AM |
275 |
} |
|
276 |
|
c883f6
|
277 |
if ($action == 'redirect' || $action == 'copy') { |
AM |
278 |
if ($target_domain) { |
|
279 |
$target .= '@' . $target_domain; |
|
280 |
} |
|
281 |
|
|
282 |
if (empty($target) || !rcube_utils::check_email($target)) { |
|
283 |
$error = 'noemailwarning'; |
|
284 |
} |
|
285 |
} |
|
286 |
|
50a57e
|
287 |
if (empty($vacation_tests)) { |
AM |
288 |
$vacation_tests = $this->rc->config->get('managesieve_vacation_test', array(array('test' => 'true'))); |
|
289 |
} |
|
290 |
|
|
291 |
if (!$error) { |
|
292 |
$rule = $this->vacation; |
|
293 |
$rule['type'] = 'if'; |
|
294 |
$rule['name'] = $rule['name'] ?: $this->plugin->gettext('vacation'); |
|
295 |
$rule['disabled'] = $status == 'off'; |
|
296 |
$rule['tests'] = $vacation_tests; |
319751
|
297 |
$rule['join'] = $date_extension ? count($vacation_tests) > 1 : false; |
c883f6
|
298 |
$rule['actions'] = array($vacation_action); |
dc9cc7
|
299 |
$rule['after'] = $after; |
c883f6
|
300 |
|
AM |
301 |
if ($action && $action != 'keep') { |
|
302 |
$rule['actions'][] = array( |
|
303 |
'type' => $action == 'discard' ? 'discard' : 'redirect', |
|
304 |
'copy' => $action == 'copy', |
|
305 |
'target' => $action != 'discard' ? $target : '', |
|
306 |
); |
|
307 |
} |
50a57e
|
308 |
|
dc9cc7
|
309 |
if ($this->save_vacation_script($rule)) { |
50a57e
|
310 |
$this->rc->output->show_message('managesieve.vacationsaved', 'confirmation'); |
AM |
311 |
$this->rc->output->send(); |
|
312 |
} |
|
313 |
} |
|
314 |
|
9f1f75
|
315 |
$this->rc->output->show_message($error ?: 'managesieve.saveerror', 'error'); |
50a57e
|
316 |
$this->rc->output->send(); |
AM |
317 |
} |
|
318 |
|
|
319 |
/** |
|
320 |
* Independent vacation form |
|
321 |
*/ |
|
322 |
public function vacation_form($attrib) |
|
323 |
{ |
|
324 |
// check supported extensions |
|
325 |
$date_extension = in_array('date', $this->exts); |
147530
|
326 |
$regex_extension = in_array('regex', $this->exts); |
50a57e
|
327 |
$seconds_extension = in_array('vacation-seconds', $this->exts); |
AM |
328 |
|
|
329 |
// build FORM tag |
9f1f75
|
330 |
$form_id = $attrib['id'] ?: 'form'; |
50a57e
|
331 |
$out = $this->rc->output->request_form(array( |
AM |
332 |
'id' => $form_id, |
|
333 |
'name' => $form_id, |
|
334 |
'method' => 'post', |
|
335 |
'task' => 'settings', |
|
336 |
'action' => 'plugin.managesieve-vacation', |
|
337 |
'noclose' => true |
|
338 |
) + $attrib); |
|
339 |
|
9c38c5
|
340 |
$auto_addr = $this->rc->config->get('managesieve_vacation_addresses_init'); |
AM |
341 |
$addresses = !$auto_addr || count($this->vacation) > 1 ? (array) $this->vacation['addresses'] : $this->user_emails(); |
|
342 |
|
50a57e
|
343 |
// form elements |
162bc8
|
344 |
$from = new html_inputfield(array('name' => 'vacation_from', 'id' => 'vacation_from', 'size' => 50)); |
13b33d
|
345 |
$subject = new html_inputfield(array('name' => 'vacation_subject', 'id' => 'vacation_subject', 'size' => 50)); |
AM |
346 |
$reason = new html_textarea(array('name' => 'vacation_reason', 'id' => 'vacation_reason', 'cols' => 60, 'rows' => 8)); |
|
347 |
$interval = new html_inputfield(array('name' => 'vacation_interval', 'id' => 'vacation_interval', 'size' => 5)); |
|
348 |
$addresses = '<textarea name="vacation_addresses" id="vacation_addresses" data-type="list" data-size="30" style="display: none">' |
9c38c5
|
349 |
. rcube::Q(implode("\n", $addresses), 'strict', false) . '</textarea>'; |
13b33d
|
350 |
$status = new html_select(array('name' => 'vacation_status', 'id' => 'vacation_status')); |
c883f6
|
351 |
$action = new html_select(array('name' => 'vacation_action', 'id' => 'vacation_action', 'onchange' => 'vacation_action_select()')); |
9c38c5
|
352 |
$addresses_link = new html_inputfield(array( |
AM |
353 |
'type' => 'button', |
|
354 |
'href' => '#', |
|
355 |
'class' => 'button', |
|
356 |
'onclick' => rcmail_output::JS_OBJECT_NAME . '.managesieve_vacation_addresses()' |
|
357 |
)); |
50a57e
|
358 |
|
AM |
359 |
$status->add($this->plugin->gettext('vacation.on'), 'on'); |
|
360 |
$status->add($this->plugin->gettext('vacation.off'), 'off'); |
c883f6
|
361 |
|
AM |
362 |
$action->add($this->plugin->gettext('vacation.keep'), 'keep'); |
|
363 |
$action->add($this->plugin->gettext('vacation.discard'), 'discard'); |
|
364 |
$action->add($this->plugin->gettext('vacation.redirect'), 'redirect'); |
|
365 |
if (in_array('copy', $this->exts)) { |
|
366 |
$action->add($this->plugin->gettext('vacation.copy'), 'copy'); |
|
367 |
} |
50a57e
|
368 |
|
AM |
369 |
if ($this->rc->config->get('managesieve_vacation') != 2 && count($this->vacation['list'])) { |
13b33d
|
370 |
$after = new html_select(array('name' => 'vacation_after', 'id' => 'vacation_after')); |
50a57e
|
371 |
|
AM |
372 |
$after->add('', ''); |
|
373 |
foreach ($this->vacation['list'] as $idx => $rule) { |
|
374 |
$after->add($rule, $idx); |
|
375 |
} |
|
376 |
} |
|
377 |
|
fa8577
|
378 |
$interval_txt = $interval->show(self::vacation_interval($this->vacation)); |
50a57e
|
379 |
if ($seconds_extension) { |
AM |
380 |
$interval_select = new html_select(array('name' => 'vacation_interval_type')); |
|
381 |
$interval_select->add($this->plugin->gettext('days'), 'days'); |
|
382 |
$interval_select->add($this->plugin->gettext('seconds'), 'seconds'); |
|
383 |
$interval_txt .= ' ' . $interval_select->show(isset($this->vacation['seconds']) ? 'seconds' : 'days'); |
|
384 |
} |
|
385 |
else { |
|
386 |
$interval_txt .= ' ' . $this->plugin->gettext('days'); |
|
387 |
} |
|
388 |
|
147530
|
389 |
if ($date_extension || $regex_extension) { |
13b33d
|
390 |
$date_from = new html_inputfield(array('name' => 'vacation_datefrom', 'id' => 'vacation_datefrom', 'class' => 'datepicker', 'size' => 12)); |
581b6b
|
391 |
$date_to = new html_inputfield(array('name' => 'vacation_dateto', 'id' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12)); |
50a57e
|
392 |
$date_format = $this->rc->config->get('date_format', 'Y-m-d'); |
147530
|
393 |
} |
50a57e
|
394 |
|
147530
|
395 |
if ($date_extension) { |
581b6b
|
396 |
$time_from = new html_inputfield(array('name' => 'vacation_timefrom', 'id' => 'vacation_timefrom', 'size' => 6)); |
AM |
397 |
$time_to = new html_inputfield(array('name' => 'vacation_timeto', 'id' => 'vacation_timeto', 'size' => 6)); |
|
398 |
$time_format = $this->rc->config->get('time_format', 'H:i'); |
|
399 |
$date_value = array(); |
|
400 |
|
50a57e
|
401 |
foreach ((array) $this->vacation['tests'] as $test) { |
5a6415
|
402 |
if ($test['test'] == 'currentdate') { |
581b6b
|
403 |
$idx = $test['type'] == 'value-ge' ? 'from' : 'to'; |
5a6415
|
404 |
|
AM |
405 |
if ($test['part'] == 'date') { |
|
406 |
$date_value[$idx]['date'] = $test['arg']; |
|
407 |
} |
|
408 |
else if ($test['part'] == 'iso8601') { |
|
409 |
$date_value[$idx]['datetime'] = $test['arg']; |
581b6b
|
410 |
} |
AM |
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
foreach ($date_value as $idx => $value) { |
5a6415
|
415 |
$date = $value['datetime'] ?: $value['date']; |
AM |
416 |
$date_value[$idx] = $this->rc->format_date($date, $date_format, false); |
581b6b
|
417 |
|
5a6415
|
418 |
if (!empty($value['datetime'])) { |
AM |
419 |
$date_value['time_' . $idx] = $this->rc->format_date($date, $time_format, true); |
50a57e
|
420 |
} |
AM |
421 |
} |
147530
|
422 |
} |
319751
|
423 |
else if ($regex_extension) { |
147530
|
424 |
// Sieve 'date' extension not available, read start/end from RegEx based rules instead |
f72815
|
425 |
if ($date_tests = self::parse_regexp_tests($this->vacation['tests'])) { |
AM |
426 |
$date_value['from'] = $this->rc->format_date($date_tests['from'], $date_format, false); |
|
427 |
$date_value['to'] = $this->rc->format_date($date_tests['to'], $date_format, false); |
147530
|
428 |
} |
50a57e
|
429 |
} |
AM |
430 |
|
c883f6
|
431 |
// force domain selection in redirect email input |
AM |
432 |
$domains = (array) $this->rc->config->get('managesieve_domains'); |
|
433 |
$redirect = $this->vacation['action'] == 'redirect' || $this->vacation['action'] == 'copy'; |
|
434 |
|
|
435 |
if (!empty($domains)) { |
|
436 |
sort($domains); |
|
437 |
|
|
438 |
$domain_select = new html_select(array('name' => 'action_domain', 'id' => 'action_domain')); |
|
439 |
$domain_select->add(array_combine($domains, $domains)); |
|
440 |
|
|
441 |
if ($redirect && $this->vacation['target']) { |
|
442 |
$parts = explode('@', $this->vacation['target']); |
|
443 |
if (!empty($parts)) { |
|
444 |
$this->vacation['domain'] = array_pop($parts); |
|
445 |
$this->vacation['target'] = implode('@', $parts); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
|
|
450 |
// redirect target |
|
451 |
$action_target = ' <span id="action_target_span" style="display:' . ($redirect ? 'inline' : 'none') . '">' |
|
452 |
. '<input type="text" name="action_target" id="action_target"' |
|
453 |
. ' value="' .($redirect ? rcube::Q($this->vacation['target'], 'strict', false) : '') . '"' |
|
454 |
. (!empty($domains) ? ' size="20"' : ' size="35"') . '/>' |
|
455 |
. (!empty($domains) ? ' @ ' . $domain_select->show($this->vacation['domain']) : '') |
|
456 |
. '</span>'; |
|
457 |
|
50a57e
|
458 |
// Message tab |
AM |
459 |
$table = new html_table(array('cols' => 2)); |
|
460 |
|
|
461 |
$table->add('title', html::label('vacation_subject', $this->plugin->gettext('vacation.subject'))); |
|
462 |
$table->add(null, $subject->show($this->vacation['subject'])); |
|
463 |
$table->add('title', html::label('vacation_reason', $this->plugin->gettext('vacation.body'))); |
|
464 |
$table->add(null, $reason->show($this->vacation['reason'])); |
|
465 |
|
147530
|
466 |
if ($date_extension || $regex_extension) { |
581b6b
|
467 |
$table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.start'))); |
AM |
468 |
$table->add(null, $date_from->show($date_value['from']) . ($time_from ? ' ' . $time_from->show($date_value['time_from']) : '')); |
|
469 |
$table->add('title', html::label('vacation_dateto', $this->plugin->gettext('vacation.end'))); |
|
470 |
$table->add(null, $date_to->show($date_value['to']) . ($time_to ? ' ' . $time_to->show($date_value['time_to']) : '')); |
50a57e
|
471 |
} |
AM |
472 |
|
|
473 |
$table->add('title', html::label('vacation_status', $this->plugin->gettext('vacation.status'))); |
9828c1
|
474 |
$table->add(null, $status->show(!isset($this->vacation['disabled']) || $this->vacation['disabled'] ? 'off' : 'on')); |
50a57e
|
475 |
|
AM |
476 |
$out .= html::tag('fieldset', $class, html::tag('legend', null, $this->plugin->gettext('vacation.reply')) . $table->show($attrib)); |
|
477 |
|
|
478 |
// Advanced tab |
|
479 |
$table = new html_table(array('cols' => 2)); |
|
480 |
|
162bc8
|
481 |
$table->add('title', html::label('vacation_from', $this->plugin->gettext('vacation.from'))); |
AM |
482 |
$table->add(null, $from->show($this->vacation['from'])); |
13b33d
|
483 |
$table->add('title', html::label('vacation_addresses', $this->plugin->gettext('vacation.addresses'))); |
9c38c5
|
484 |
$table->add(null, $addresses . $addresses_link->show($this->plugin->gettext('filladdresses'))); |
13b33d
|
485 |
$table->add('title', html::label('vacation_interval', $this->plugin->gettext('vacation.interval'))); |
50a57e
|
486 |
$table->add(null, $interval_txt); |
c883f6
|
487 |
|
50a57e
|
488 |
if ($after) { |
13b33d
|
489 |
$table->add('title', html::label('vacation_after', $this->plugin->gettext('vacation.after'))); |
50a57e
|
490 |
$table->add(null, $after->show($this->vacation['idx'] - 1)); |
AM |
491 |
} |
|
492 |
|
c883f6
|
493 |
$table->add('title', html::label('vacation_action', $this->plugin->gettext('vacation.action'))); |
AM |
494 |
$table->add('vacation', $action->show($this->vacation['action']) . $action_target); |
|
495 |
|
50a57e
|
496 |
$out .= html::tag('fieldset', $class, html::tag('legend', null, $this->plugin->gettext('vacation.advanced')) . $table->show($attrib)); |
AM |
497 |
|
|
498 |
$out .= '</form>'; |
|
499 |
|
|
500 |
$this->rc->output->add_gui_object('sieveform', $form_id); |
|
501 |
|
581b6b
|
502 |
if ($time_format) { |
AM |
503 |
$this->rc->output->set_env('time_format', $time_format); |
|
504 |
} |
|
505 |
|
50a57e
|
506 |
return $out; |
AM |
507 |
} |
f72815
|
508 |
|
AM |
509 |
public static function build_regexp_tests($date_from, $date_to, &$error) |
|
510 |
{ |
|
511 |
$tests = array(); |
|
512 |
$dt_from = rcube_utils::anytodatetime($date_from); |
|
513 |
$dt_to = rcube_utils::anytodatetime($date_to); |
|
514 |
$interval = $dt_from->diff($dt_to); |
|
515 |
|
|
516 |
if ($interval->invert || $interval->days > 365) { |
|
517 |
$error = 'managesieve.invaliddateformat'; |
|
518 |
return; |
|
519 |
} |
|
520 |
|
|
521 |
$dt_i = $dt_from; |
|
522 |
$interval = new DateInterval('P1D'); |
|
523 |
$matchexp = ''; |
|
524 |
|
|
525 |
while (!$dt_i->diff($dt_to)->invert) { |
|
526 |
$days = (int) $dt_i->format('d'); |
|
527 |
$matchexp .= $days < 10 ? "[ 0]$days" : $days; |
|
528 |
|
|
529 |
if ($days == $dt_i->format('t') || $dt_i->diff($dt_to)->days == 0) { |
|
530 |
$test = array( |
|
531 |
'test' => 'header', |
|
532 |
'type' => 'regex', |
|
533 |
'arg1' => 'received', |
|
534 |
'arg2' => '('.$matchexp.') '.$dt_i->format('M Y') |
|
535 |
); |
|
536 |
|
|
537 |
$tests[] = $test; |
|
538 |
$matchexp = ''; |
|
539 |
} |
|
540 |
else { |
|
541 |
$matchexp .= '|'; |
|
542 |
} |
|
543 |
|
|
544 |
$dt_i->add($interval); |
|
545 |
} |
|
546 |
|
|
547 |
return $tests; |
|
548 |
} |
|
549 |
|
|
550 |
public static function parse_regexp_tests($tests) |
|
551 |
{ |
|
552 |
$rx_from = '/^\(([0-9]{2}).*\)\s([A-Za-z]+)\s([0-9]{4})/'; |
|
553 |
$rx_to = '/^\(.*([0-9]{2})\)\s([A-Za-z]+)\s([0-9]{4})/'; |
|
554 |
$result = array(); |
|
555 |
|
|
556 |
foreach ((array) $tests as $test) { |
|
557 |
if ($test['test'] == 'header' && $test['type'] == 'regex' && $test['arg1'] == 'received') { |
|
558 |
$textexp = preg_replace('/\[ ([^\]]*)\]/', '0', $test['arg2']); |
|
559 |
|
|
560 |
if (!$result['from'] && preg_match($rx_from, $textexp, $matches)) { |
|
561 |
$result['from'] = $matches[1]." ".$matches[2]." ".$matches[3]; |
|
562 |
} |
|
563 |
|
|
564 |
if (preg_match($rx_to, $textexp, $matches)) { |
|
565 |
$result['to'] = $matches[1]." ".$matches[2]." ".$matches[3]; |
|
566 |
} |
|
567 |
} |
|
568 |
} |
|
569 |
|
|
570 |
return $result; |
|
571 |
} |
613f96
|
572 |
|
AM |
573 |
/** |
fa8577
|
574 |
* Get current vacation interval |
AM |
575 |
*/ |
|
576 |
public static function vacation_interval(&$vacation) |
|
577 |
{ |
|
578 |
$rcube = rcube::get_instance(); |
|
579 |
|
|
580 |
if (isset($vacation['seconds'])) { |
|
581 |
$interval = $vacation['seconds']; |
|
582 |
} |
|
583 |
else if (isset($vacation['days'])) { |
|
584 |
$interval = $vacation['days']; |
|
585 |
} |
|
586 |
else if ($interval_cfg = $rcube->config->get('managesieve_vacation_interval')) { |
|
587 |
if (preg_match('/^([0-9]+)s$/', $interval_cfg, $m)) { |
|
588 |
if ($seconds_extension) { |
|
589 |
$vacation['seconds'] = ($interval = intval($m[1])) ? $interval : null; |
|
590 |
} |
|
591 |
else { |
|
592 |
$vacation['days'] = $interval = ceil(intval($m[1])/86400); |
|
593 |
} |
|
594 |
} |
|
595 |
else { |
|
596 |
$vacation['days'] = $interval = intval($interval_cfg); |
|
597 |
} |
|
598 |
} |
|
599 |
|
9f1f75
|
600 |
return $interval ?: ''; |
fa8577
|
601 |
} |
AM |
602 |
|
|
603 |
/** |
dc9cc7
|
604 |
* Saves vacation script (adding some variables) |
AM |
605 |
*/ |
|
606 |
protected function save_vacation_script($rule) |
|
607 |
{ |
|
608 |
// if script does not exist create a new one |
04009e
|
609 |
if ($this->script_name === null || $this->script_name === false) { |
dc9cc7
|
610 |
$this->script_name = $this->rc->config->get('managesieve_script_name'); |
AM |
611 |
if (empty($this->script_name)) { |
|
612 |
$this->script_name = 'roundcube'; |
|
613 |
} |
|
614 |
|
3e0ad2
|
615 |
// use default script contents |
AM |
616 |
if (!$this->rc->config->get('managesieve_kolab_master')) { |
|
617 |
$script_file = $this->rc->config->get('managesieve_default'); |
|
618 |
if ($script_file && is_readable($script_file)) { |
|
619 |
$content = file_get_contents($script_file); |
|
620 |
} |
|
621 |
} |
|
622 |
|
|
623 |
// create and load script |
|
624 |
if ($this->sieve->save_script($this->script_name, $content)) { |
|
625 |
$this->sieve->load($this->script_name); |
|
626 |
} |
dc9cc7
|
627 |
} |
AM |
628 |
|
3e0ad2
|
629 |
$script_active = in_array($this->script_name, $this->active); |
dc9cc7
|
630 |
|
3e0ad2
|
631 |
// re-order rules if needed |
AM |
632 |
if (isset($rule['after']) && $rule['after'] !== '') { |
|
633 |
// reset original vacation rule |
|
634 |
if (isset($this->vacation['idx'])) { |
|
635 |
$this->script[$this->vacation['idx']] = null; |
dc9cc7
|
636 |
} |
3e0ad2
|
637 |
|
AM |
638 |
// add at target position |
|
639 |
if ($rule['after'] >= count($this->script) - 1) { |
|
640 |
$this->script[] = $rule; |
dc9cc7
|
641 |
} |
AM |
642 |
else { |
3e0ad2
|
643 |
$script = array(); |
dc9cc7
|
644 |
|
AM |
645 |
foreach ($this->script as $idx => $r) { |
3e0ad2
|
646 |
if ($r) { |
AM |
647 |
$script[] = $r; |
|
648 |
} |
|
649 |
|
|
650 |
if ($idx == $rule['after']) { |
|
651 |
$script[] = $rule; |
dc9cc7
|
652 |
} |
AM |
653 |
} |
3e0ad2
|
654 |
|
AM |
655 |
$this->script = $script; |
dc9cc7
|
656 |
} |
3e0ad2
|
657 |
|
AM |
658 |
$this->script = array_values(array_filter($this->script)); |
|
659 |
} |
|
660 |
// update original vacation rule if it exists |
|
661 |
else if (isset($this->vacation['idx'])) { |
|
662 |
$this->script[$this->vacation['idx']] = $rule; |
|
663 |
} |
|
664 |
// otherwise put vacation rule on top |
|
665 |
else { |
|
666 |
array_unshift($this->script, $rule); |
|
667 |
} |
|
668 |
|
|
669 |
// if the script was not active, we need to de-activate |
|
670 |
// all rules except the vacation rule, but only if it is not disabled |
|
671 |
if (!$script_active && !$rule['disabled']) { |
|
672 |
foreach ($this->script as $idx => $r) { |
|
673 |
if (empty($r['actions']) || $r['actions'][0]['type'] != 'vacation') { |
|
674 |
$this->script[$idx]['disabled'] = true; |
|
675 |
} |
|
676 |
} |
|
677 |
} |
|
678 |
|
|
679 |
if (!$this->sieve->script) { |
|
680 |
return false; |
dc9cc7
|
681 |
} |
AM |
682 |
|
|
683 |
$this->sieve->script->content = $this->script; |
|
684 |
|
|
685 |
// save the script |
|
686 |
$saved = $this->save_script($this->script_name); |
|
687 |
|
|
688 |
// activate the script |
|
689 |
if ($saved && !$script_active && !$rule['disabled']) { |
|
690 |
$this->activate_script($this->script_name); |
|
691 |
} |
|
692 |
|
|
693 |
return $saved; |
|
694 |
} |
|
695 |
|
|
696 |
/** |
613f96
|
697 |
* API: get vacation rule |
AM |
698 |
* |
|
699 |
* @return array Vacation rule information |
|
700 |
*/ |
|
701 |
public function get_vacation() |
|
702 |
{ |
|
703 |
$this->exts = $this->sieve->get_extensions(); |
|
704 |
$this->init_script(); |
|
705 |
$this->vacation_rule(); |
|
706 |
|
|
707 |
// check supported extensions |
|
708 |
$date_extension = in_array('date', $this->exts); |
|
709 |
$regex_extension = in_array('regex', $this->exts); |
|
710 |
$seconds_extension = in_array('vacation-seconds', $this->exts); |
|
711 |
|
|
712 |
// set user's timezone |
|
713 |
try { |
|
714 |
$timezone = new DateTimeZone($this->rc->config->get('timezone', 'GMT')); |
|
715 |
} |
|
716 |
catch (Exception $e) { |
|
717 |
$timezone = new DateTimeZone('GMT'); |
|
718 |
} |
|
719 |
|
|
720 |
if ($date_extension) { |
|
721 |
$date_value = array(); |
|
722 |
foreach ((array) $this->vacation['tests'] as $test) { |
|
723 |
if ($test['test'] == 'currentdate') { |
|
724 |
$idx = $test['type'] == 'value-ge' ? 'start' : 'end'; |
|
725 |
|
|
726 |
if ($test['part'] == 'date') { |
|
727 |
$date_value[$idx]['date'] = $test['arg']; |
|
728 |
} |
|
729 |
else if ($test['part'] == 'iso8601') { |
|
730 |
$date_value[$idx]['datetime'] = $test['arg']; |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
|
|
735 |
foreach ($date_value as $idx => $value) { |
|
736 |
$$idx = new DateTime($value['datetime'] ?: $value['date'], $timezone); |
|
737 |
} |
|
738 |
} |
|
739 |
else if ($regex_extension) { |
|
740 |
// Sieve 'date' extension not available, read start/end from RegEx based rules instead |
|
741 |
if ($date_tests = self::parse_regexp_tests($this->vacation['tests'])) { |
|
742 |
$from = new DateTime($date_tests['from'] . ' ' . '00:00:00', $timezone); |
|
743 |
$to = new DateTime($date_tests['to'] . ' ' . '23:59:59', $timezone); |
|
744 |
} |
|
745 |
} |
|
746 |
|
|
747 |
if (isset($this->vacation['seconds'])) { |
|
748 |
$interval = $this->vacation['seconds'] . 's'; |
|
749 |
} |
|
750 |
else if (isset($this->vacation['days'])) { |
|
751 |
$interval = $this->vacation['days'] . 'd'; |
|
752 |
} |
|
753 |
|
|
754 |
$vacation = array( |
|
755 |
'supported' => $this->exts, |
|
756 |
'interval' => $interval, |
|
757 |
'start' => $start, |
|
758 |
'end' => $end, |
|
759 |
'enabled' => $this->vacation['reason'] && empty($this->vacation['disabled']), |
|
760 |
'message' => $this->vacation['reason'], |
|
761 |
'subject' => $this->vacation['subject'], |
|
762 |
'action' => $this->vacation['action'], |
|
763 |
'target' => $this->vacation['target'], |
|
764 |
'addresses' => $this->vacation['addresses'], |
162bc8
|
765 |
'from' => $this->vacation['from'], |
613f96
|
766 |
); |
AM |
767 |
|
|
768 |
return $vacation; |
|
769 |
} |
|
770 |
|
|
771 |
/** |
|
772 |
* API: set vacation rule |
|
773 |
* |
|
774 |
* @param array $vacation Vacation rule information (see self::get_vacation()) |
|
775 |
* |
|
776 |
* @return bool True on success, False on failure |
|
777 |
*/ |
|
778 |
public function set_vacation($data) |
|
779 |
{ |
|
780 |
$this->exts = $this->sieve->get_extensions(); |
|
781 |
$this->error = false; |
|
782 |
|
|
783 |
$this->init_script(); |
|
784 |
$this->vacation_rule(); |
|
785 |
|
|
786 |
// check supported extensions |
|
787 |
$date_extension = in_array('date', $this->exts); |
|
788 |
$regex_extension = in_array('regex', $this->exts); |
|
789 |
$seconds_extension = in_array('vacation-seconds', $this->exts); |
|
790 |
|
|
791 |
$vacation['type'] = 'vacation'; |
|
792 |
$vacation['reason'] = $this->strip_value(str_replace("\r\n", "\n", $data['message'])); |
|
793 |
$vacation['addresses'] = $data['addresses']; |
162bc8
|
794 |
$vacation['subject'] = trim($data['subject']); |
AM |
795 |
$vacation['from'] = trim($data['from']); |
613f96
|
796 |
$vacation_tests = (array) $this->vacation['tests']; |
AM |
797 |
|
|
798 |
foreach ((array) $vacation['addresses'] as $aidx => $address) { |
|
799 |
$vacation['addresses'][$aidx] = $address = trim($address); |
|
800 |
|
|
801 |
if (empty($address)) { |
|
802 |
unset($vacation['addresses'][$aidx]); |
|
803 |
} |
|
804 |
else if (!rcube_utils::check_email($address)) { |
|
805 |
$this->error = "Invalid address in vacation addresses: $address"; |
|
806 |
return false; |
|
807 |
} |
|
808 |
} |
|
809 |
|
162bc8
|
810 |
if (!empty($vacation['from']) && !rcube_utils::check_email($vacation['from'])) { |
AM |
811 |
$this->error = "Invalid address in 'from': " . $vacation['from']; |
|
812 |
return false; |
|
813 |
} |
|
814 |
|
613f96
|
815 |
if ($vacation['reason'] == '') { |
AM |
816 |
$this->error = "No vacation message specified"; |
|
817 |
return false; |
|
818 |
} |
|
819 |
|
|
820 |
if ($data['interval']) { |
|
821 |
if (!preg_match('/^([0-9]+)\s*([sd])$/', $data['interval'], $m)) { |
|
822 |
$this->error = "Invalid vacation interval value: " . $data['interval']; |
|
823 |
return false; |
|
824 |
} |
|
825 |
else if ($m[1]) { |
|
826 |
$vacation[strtolower($m[2]) == 's' ? 'seconds' : 'days'] = $m[1]; |
|
827 |
} |
|
828 |
} |
|
829 |
|
|
830 |
// find and remove existing date/regex/true rules |
|
831 |
foreach ((array) $vacation_tests as $idx => $t) { |
|
832 |
if ($t['test'] == 'currentdate' || $t['test'] == 'true' |
|
833 |
|| ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received') |
|
834 |
) { |
|
835 |
unset($vacation_tests[$idx]); |
|
836 |
} |
|
837 |
} |
|
838 |
|
|
839 |
if ($date_extension) { |
|
840 |
foreach (array('start', 'end') as $var) { |
|
841 |
if ($dt = $data[$var]) { |
|
842 |
$vacation_tests[] = array( |
|
843 |
'test' => 'currentdate', |
|
844 |
'part' => 'iso8601', |
|
845 |
'type' => 'value-' . ($var == 'start' ? 'ge' : 'le'), |
|
846 |
'zone' => $dt->format('O'), |
|
847 |
'arg' => str_replace('+00:00', 'Z', strtoupper($dt->format('c'))), |
|
848 |
); |
|
849 |
} |
|
850 |
} |
|
851 |
} |
|
852 |
else if ($regex_extension) { |
|
853 |
// Add date range rules if range specified |
|
854 |
if ($data['start'] && $data['end']) { |
|
855 |
if ($tests = self::build_regexp_tests($data['start'], $data['end'], $error)) { |
|
856 |
$vacation_tests = array_merge($vacation_tests, $tests); |
|
857 |
} |
|
858 |
|
|
859 |
if ($error) { |
|
860 |
$this->error = "Invalid dates specified or unsupported period length"; |
|
861 |
return false; |
|
862 |
} |
|
863 |
} |
|
864 |
} |
|
865 |
|
|
866 |
if ($data['action'] == 'redirect' || $data['action'] == 'copy') { |
|
867 |
if (empty($data['target']) || !rcube_utils::check_email($data['target'])) { |
|
868 |
$this->error = "Invalid address in action taget: " . $data['target']; |
|
869 |
return false; |
|
870 |
} |
|
871 |
} |
|
872 |
else if ($data['action'] && $data['action'] != 'keep' && $data['action'] != 'discard') { |
|
873 |
$this->error = "Unsupported vacation action: " . $data['action']; |
|
874 |
return false; |
|
875 |
} |
|
876 |
|
|
877 |
if (empty($vacation_tests)) { |
|
878 |
$vacation_tests = $this->rc->config->get('managesieve_vacation_test', array(array('test' => 'true'))); |
|
879 |
} |
|
880 |
|
|
881 |
$rule = $this->vacation; |
|
882 |
$rule['type'] = 'if'; |
|
883 |
$rule['name'] = $rule['name'] ?: 'Out-of-Office'; |
|
884 |
$rule['disabled'] = isset($data['enabled']) && !$data['enabled']; |
|
885 |
$rule['tests'] = $vacation_tests; |
|
886 |
$rule['join'] = $date_extension ? count($vacation_tests) > 1 : false; |
|
887 |
$rule['actions'] = array($vacation); |
|
888 |
|
|
889 |
if ($data['action'] && $data['action'] != 'keep') { |
|
890 |
$rule['actions'][] = array( |
|
891 |
'type' => $data['action'] == 'discard' ? 'discard' : 'redirect', |
|
892 |
'copy' => $data['action'] == 'copy', |
|
893 |
'target' => $data['action'] != 'discard' ? $data['target'] : '', |
|
894 |
); |
|
895 |
} |
|
896 |
|
dc9cc7
|
897 |
return $this->save_vacation_script($rule); |
613f96
|
898 |
} |
AM |
899 |
|
|
900 |
/** |
|
901 |
* API: connect to managesieve server |
|
902 |
*/ |
|
903 |
public function connect($username, $password) |
|
904 |
{ |
|
905 |
if (!parent::connect($username, $password)) { |
|
906 |
return $this->load_script(); |
|
907 |
} |
|
908 |
} |
|
909 |
|
|
910 |
/** |
|
911 |
* API: Returns last error |
|
912 |
* |
|
913 |
* @return string Error message |
|
914 |
*/ |
|
915 |
public function get_error() |
|
916 |
{ |
|
917 |
return $this->error; |
|
918 |
} |
50a57e
|
919 |
} |