diff options
Diffstat (limited to 'plugins/managesieve/lib/Roundcube')
3 files changed, 460 insertions, 120 deletions
diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php index 4a375d353..9900f16b5 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php @@ -24,21 +24,21 @@ class rcube_sieve_engine { - private $rc; - private $sieve; - private $errors; - private $form; - private $tips = array(); - private $script = array(); - private $exts = array(); - private $list; - private $active = array(); - private $headers = array( + protected $rc; + protected $sieve; + protected $errors; + protected $form; + protected $tips = array(); + protected $script = array(); + protected $exts = array(); + protected $list; + protected $active = array(); + protected $headers = array( 'subject' => 'Subject', 'from' => 'From', 'to' => 'To', ); - private $addr_headers = array( + protected $addr_headers = array( // Required "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to", // Additional (RFC 822 / RFC 2822) @@ -52,8 +52,18 @@ class rcube_sieve_engine // Undocumented "x-beenthere", ); + protected $notify_methods = array( + 'mailto', + // 'sms', + // 'tel', + ); + protected $notify_importance_options = array( + 3 => 'notifyimportancelow', + 2 => 'notifyimportancenormal', + 1 => 'notifyimportancehigh' + ); - const VERSION = '7.1'; + const VERSION = '8.0'; const PROGNAME = 'Roundcube (Managesieve)'; const PORT = 4190; @@ -70,7 +80,7 @@ class rcube_sieve_engine /** * Loads configuration, initializes plugin (including sieve connection) */ - function start() + function start($mode = null) { // register UI objects $this->rc->output->add_handlers(array( @@ -137,13 +147,16 @@ class rcube_sieve_engine $this->rc->session->remove('managesieve_current'); } - if (!empty($_GET['_set']) || !empty($_POST['_set'])) { - $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); - } - else if (!empty($_SESSION['managesieve_current'])) { - $script_name = $_SESSION['managesieve_current']; + if ($mode != 'vacation') { + if (!empty($_GET['_set']) || !empty($_POST['_set'])) { + $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); + } + else if (!empty($_SESSION['managesieve_current'])) { + $script_name = $_SESSION['managesieve_current']; + } } - else { + + if ($script_name === null || $script_name === '') { // get (first) active script if (!empty($this->active[0])) { $script_name = $this->active[0]; @@ -349,14 +362,13 @@ class rcube_sieve_engine header("Content-Type: application/octet-stream"); header("Content-Length: ".strlen($script)); - if ($browser->ie) + if ($browser->ie) { header("Content-Type: application/force-download"); - if ($browser->ie && $browser->ver < 7) - $filename = rawurlencode(abbreviate_string($script_name, 55)); - else if ($browser->ie) $filename = rawurlencode($script_name); - else + } + else { $filename = addcslashes($script_name, '\\"'); + } header("Content-Disposition: attachment; filename=\"$filename.txt\""); echo $script; @@ -556,9 +568,10 @@ class rcube_sieve_engine $varnames = rcube_utils::get_input_value('_action_varname', rcube_utils::INPUT_POST); $varvalues = rcube_utils::get_input_value('_action_varvalue', rcube_utils::INPUT_POST); $varmods = rcube_utils::get_input_value('_action_varmods', rcube_utils::INPUT_POST); - $notifyaddrs = rcube_utils::get_input_value('_action_notifyaddress', rcube_utils::INPUT_POST); - $notifybodies = rcube_utils::get_input_value('_action_notifybody', rcube_utils::INPUT_POST); - $notifymessages = rcube_utils::get_input_value('_action_notifymessage', rcube_utils::INPUT_POST); + $notifymethods = rcube_utils::get_input_value('_action_notifymethod', rcube_utils::INPUT_POST); + $notifytargets = rcube_utils::get_input_value('_action_notifytarget', rcube_utils::INPUT_POST, true); + $notifyoptions = rcube_utils::get_input_value('_action_notifyoption', rcube_utils::INPUT_POST, true); + $notifymessages = rcube_utils::get_input_value('_action_notifymessage', rcube_utils::INPUT_POST, true); $notifyfrom = rcube_utils::get_input_value('_action_notifyfrom', rcube_utils::INPUT_POST); $notifyimp = rcube_utils::get_input_value('_action_notifyimportance', rcube_utils::INPUT_POST); @@ -958,19 +971,27 @@ class rcube_sieve_engine break; case 'notify': - if (empty($notifyaddrs[$idx])) { - $this->errors['actions'][$i]['address'] = $this->plugin->gettext('cannotbeempty'); + if (empty($notifymethods[$idx])) { + $this->errors['actions'][$i]['method'] = $this->plugin->gettext('cannotbeempty'); } - else if (!rcube_utils::check_email($notifyaddrs[$idx])) { - $this->errors['actions'][$i]['address'] = $this->plugin->gettext('noemailwarning'); + if (empty($notifytargets[$idx])) { + $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } if (!empty($notifyfrom[$idx]) && !rcube_utils::check_email($notifyfrom[$idx])) { $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning'); } - $this->form['actions'][$i]['address'] = $notifyaddrs[$idx]; - $this->form['actions'][$i]['body'] = $notifybodies[$idx]; - $this->form['actions'][$i]['message'] = $notifymessages[$idx]; - $this->form['actions'][$i]['from'] = $notifyfrom[$idx]; + + // skip empty options + foreach ((array)$notifyoptions[$idx] as $opt_idx => $opt) { + if (!strlen(trim($opt))) { + unset($notifyoptions[$idx][$opt_idx]); + } + } + + $this->form['actions'][$i]['method'] = $notifymethods[$idx] . ':' . $notifytargets[$idx]; + $this->form['actions'][$i]['options'] = $notifyoptions[$idx]; + $this->form['actions'][$i]['message'] = $notifymessages[$idx]; + $this->form['actions'][$i]['from'] = $notifyfrom[$idx]; $this->form['actions'][$i]['importance'] = $notifyimp[$idx]; break; } @@ -980,12 +1001,14 @@ class rcube_sieve_engine } if (!$this->errors && !$error) { - // zapis skryptu + // save the script if (!isset($this->script[$fid])) { $fid = $this->sieve->script->add_rule($this->form); $new = true; - } else + } + else { $fid = $this->sieve->script->update_rule($fid, $this->form); + } if ($fid !== false) $save = $this->save_script(); @@ -1016,7 +1039,7 @@ class rcube_sieve_engine $this->send(); } - private function send() + protected function send() { // Handle form action if (isset($_GET['_framed']) || isset($_POST['_framed'])) { @@ -1026,7 +1049,8 @@ class rcube_sieve_engine else { $this->rc->output->send('managesieve.filteredit'); } - } else { + } + else { $this->rc->output->set_pagetitle($this->plugin->gettext('filters')); $this->rc->output->send('managesieve.managesieve'); } @@ -1642,11 +1666,12 @@ class rcube_sieve_engine $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id)); $domain_select->add(array_combine($domains, $domains)); - $parts = explode('@', $action['target']); - - if (!empty($parts)) { - $action['domain'] = array_pop($parts); - $action['target'] = implode('@', $parts); + if ($action['type'] == 'redirect') { + $parts = explode('@', $action['target']); + if (!empty($parts)) { + $action['domain'] = array_pop($parts); + $action['target'] = implode('@', $parts); + } } } @@ -1743,38 +1768,61 @@ class rcube_sieve_engine $out .= '</div>'; // notify - // skip :options tag - not used by the mailto method - $out .= '<div id="action_notify' .$id.'" style="display:' .($action['type']=='notify' ? 'inline' : 'none') .'">'; - $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('notifyaddress')) . '</span><br />' - .'<input type="text" name="_action_notifyaddress['.$id.']" id="action_notifyaddress'.$id.'" ' - .'value="' . rcube::Q($action['address']) . '" size="35" ' - . $this->error_class($id, 'action', 'address', 'action_notifyaddress') .' />'; - $out .= '<br /><span class="label">'. rcube::Q($this->plugin->gettext('notifybody')) .'</span><br />' - .'<textarea name="_action_notifybody['.$id.']" id="action_notifybody' .$id. '" ' - .'rows="3" cols="35" '. $this->error_class($id, 'action', 'method', 'action_notifybody') . '>' - . rcube::Q($action['body'], 'strict', false) . "</textarea>\n"; - $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('notifysubject')) . '</span><br />' - .'<input type="text" name="_action_notifymessage['.$id.']" id="action_notifymessage'.$id.'" ' - .'value="' . rcube::Q($action['message']) . '" size="35" ' - . $this->error_class($id, 'action', 'message', 'action_notifymessage') .' />'; - $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('notifyfrom')) . '</span><br />' - .'<input type="text" name="_action_notifyfrom['.$id.']" id="action_notifyfrom'.$id.'" ' - .'value="' . rcube::Q($action['from']) . '" size="35" ' - . $this->error_class($id, 'action', 'from', 'action_notifyfrom') .' />'; - $importance_options = array( - 3 => 'notifyimportancelow', - 2 => 'notifyimportancenormal', - 1 => 'notifyimportancehigh' - ); + $notify_methods = (array) $this->rc->config->get('managesieve_notify_methods'); + $importance_options = $this->notify_importance_options; + + if (empty($notify_methods)) { + $notify_methods = $this->notify_methods; + } + + list($method, $target) = explode(':', $action['method'], 2); + $method = strtolower($method); + + if ($method && !in_array($method, $notify_methods)) { + $notify_methods[] = $method; + } + + $select_method = new html_select(array( + 'name' => "_action_notifymethod[$id]", + 'id' => "_action_notifymethod$id", + 'class' => $this->error_class($id, 'action', 'method', 'action_notifymethod'), + )); + foreach ($notify_methods as $m_n) { + $select_method->add(rcube::Q($this->rc->text_exists('managesieve.notifymethod'.$m_n) ? $this->plugin->gettext('managesieve.notifymethod'.$m_n) : $m_n), $m_n); + } + $select_importance = new html_select(array( - 'name' => '_action_notifyimportance[' . $id . ']', - 'id' => '_action_notifyimportance' . $id, - 'class' => $this->error_class($id, 'action', 'importance', 'action_notifyimportance'))); + 'name' => "_action_notifyimportance[$id]", + 'id' => "_action_notifyimportance$id", + 'class' => $this->error_class($id, 'action', 'importance', 'action_notifyimportance') + )); foreach ($importance_options as $io_v => $io_n) { $select_importance->add(rcube::Q($this->plugin->gettext($io_n)), $io_v); } + + // @TODO: nice UI for mailto: (other methods too) URI parameters + $out .= '<div id="action_notify' .$id.'" style="display:' .($action['type'] == 'notify' ? 'inline' : 'none') .'">'; + $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('notifytarget')) . '</span><br />' + . $select_method->show($method) + .'<input type="text" name="_action_notifytarget['.$id.']" id="action_notifytarget'.$id.'" ' + .'value="' . rcube::Q($target) . '" size="25" ' + . $this->error_class($id, 'action', 'target', 'action_notifytarget') .' />'; + $out .= '<br /><span class="label">'. rcube::Q($this->plugin->gettext('notifymessage')) .'</span><br />' + .'<textarea name="_action_notifymessage['.$id.']" id="action_notifymessage' .$id. '" ' + .'rows="3" cols="35" '. $this->error_class($id, 'action', 'message', 'action_notifymessage') . '>' + . rcube::Q($action['message'], 'strict', false) . "</textarea>\n"; + if (in_array('enotify', $this->exts)) { + $out .= '<br /><span class="label">' .rcube::Q($this->plugin->gettext('notifyfrom')) . '</span><br />' + .'<input type="text" name="_action_notifyfrom['.$id.']" id="action_notifyfrom'.$id.'" ' + .'value="' . rcube::Q($action['from']) . '" size="35" ' + . $this->error_class($id, 'action', 'from', 'action_notifyfrom') .' />'; + } $out .= '<br /><span class="label">' . rcube::Q($this->plugin->gettext('notifyimportance')) . '</span><br />'; - $out .= $select_importance->show($action['importance'] ? $action['importance'] : 2); + $out .= $select_importance->show($action['importance'] ? (int) $action['importance'] : 2); + $out .= '<div id="action_notifyoption_div' . $id . '">' + .'<span class="label">' . rcube::Q($this->plugin->gettext('notifyoptions')) . '</span><br />' + .$this->list_input($id, 'action_notifyoption', (array)$action['options'], true, + $this->error_class($id, 'action', 'options', 'action_notifyoption'), 30) . '</div>'; $out .= '</div>'; // mailbox select @@ -1788,7 +1836,7 @@ class rcube_sieve_engine 'maxlength' => 100, 'id' => 'action_mailbox' . $id, 'name' => "_action_mailbox[$id]", - 'style' => 'display:'.(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none') + 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none') )); $out .= $select->show($mailbox); $out .= '</td>'; @@ -1808,12 +1856,12 @@ class rcube_sieve_engine return $out; } - private function genid() + protected function genid() { return preg_replace('/[^0-9]/', '', microtime(true)); } - private function strip_value($str, $allow_html = false, $trim = true) + protected function strip_value($str, $allow_html = false, $trim = true) { if (is_array($str)) { foreach ($str as $idx => $val) { @@ -1834,7 +1882,7 @@ class rcube_sieve_engine return $trim ? trim($str) : $str; } - private function error_class($id, $type, $target, $elem_prefix='') + protected function error_class($id, $type, $target, $elem_prefix='') { // TODO: tooltips if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) || @@ -1847,7 +1895,7 @@ class rcube_sieve_engine return ''; } - private function add_tip($id, $str, $error=false) + protected function add_tip($id, $str, $error=false) { if ($error) $str = html::span('sieve error', $str); @@ -1855,7 +1903,7 @@ class rcube_sieve_engine $this->tips[] = array($id, $str); } - private function print_tips() + protected function print_tips() { if (empty($this->tips)) return; @@ -1864,7 +1912,7 @@ class rcube_sieve_engine $this->rc->output->add_script($script, 'foot'); } - private function list_input($id, $name, $value, $enabled, $class, $size=null) + protected function list_input($id, $name, $value, $enabled, $class, $size=null) { $value = (array) $value; $value = array_map(array('rcube', 'Q'), $value); @@ -1880,7 +1928,7 @@ class rcube_sieve_engine /** * Validate input for date part elements */ - private function validate_date_part($type, $value) + protected function validate_date_part($type, $value) { // we do simple validation of date/part format switch ($type) { @@ -1925,7 +1973,7 @@ class rcube_sieve_engine * * @return string Mailbox name */ - private function mod_mailbox($mailbox, $mode = 'out') + protected function mod_mailbox($mailbox, $mode = 'out') { $delimiter = $_SESSION['imap_delimiter']; $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter'); @@ -2217,7 +2265,7 @@ class rcube_sieve_engine /** * Initializes internal script data */ - private function init_script() + protected function init_script() { $this->script = $this->sieve->script->as_array(); diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php index 6fbc3f89d..bc62d2ff4 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php @@ -38,7 +38,7 @@ class rcube_sieve_script 'imap4flags', // RFC5232 'include', // draft-ietf-sieve-include-12 'index', // RFC5260 - 'notify', // draft-ietf-sieve-notify-00 + 'notify', // draft-martin-sieve-notify-01, 'regex', // draft-ietf-sieve-regex-01 'reject', // RFC5429 'relational', // RFC3431 @@ -411,38 +411,38 @@ class rcube_sieve_script array_push($exts, $notify); $action_script .= 'notify'; - // Here we support only 00 version of notify draft, there - // were a couple regressions in 00 to 04 changelog, we use - // the version used by Cyrus + $method = $action['method']; + unset($action['method']); + $action['options'] = (array) $action['options']; + + // Here we support draft-martin-sieve-notify-01 used by Cyrus if ($notify == 'notify') { switch ($action['importance']) { case 1: $action_script .= " :high"; break; - case 2: $action_script .= " :normal"; break; + //case 2: $action_script .= " :normal"; break; case 3: $action_script .= " :low"; break; + } + // Old-draft way: :method "mailto" :options "email@address" + if (!empty($method)) { + $parts = explode(':', $method, 2); + $action['method'] = $parts[0]; + array_unshift($action['options'], $parts[1]); } + unset($action['importance']); + unset($action['from']); + unset($method); } - foreach (array('from', 'importance', 'options', 'message') as $n_tag) { + foreach (array('id', 'importance', 'method', 'options', 'from', 'message') as $n_tag) { if (!empty($action[$n_tag])) { $action_script .= " :$n_tag " . self::escape_string($action[$n_tag]); } } - if (!empty($action['address'])) { - $method = 'mailto:' . $action['address']; - if (!empty($action['body'])) { - $method .= '?body=' . rawurlencode($action['body']); - } - } - else { - $method = $action['method']; - } - - // method is optional in notify extension if (!empty($method)) { - $action_script .= ($notify == 'notify' ? " :method " : " ") . self::escape_string($method); + $action_script .= ' ' . self::escape_string($method); } break; @@ -585,6 +585,7 @@ class rcube_sieve_script if ($rule[0]['type'] == 'set') { unset($rule[0]['type']); $this->vars[] = $rule[0]; + unset($rule); } else { $rule = array('actions' => $rule); @@ -848,13 +849,11 @@ class rcube_sieve_script case 'notify': $action = array('type' => 'notify'); $priorities = array('high' => 1, 'normal' => 2, 'low' => 3); - $vargs = array('from', 'importance', 'options', 'message', 'method'); + $vargs = array('from', 'id', 'importance', 'options', 'message', 'method'); $args = array_keys($priorities); $action += $this->action_arguments($tokens, $args, $vargs); - // Here we support only 00 version of notify draft, there - // were a couple regressions in 00 to 04 changelog, we use - // the version used by Cyrus + // Here we'll convert draft-martin-sieve-notify-01 into RFC 5435 if (!isset($action['importance'])) { foreach ($priorities as $key => $val) { if (isset($action[$key])) { @@ -864,29 +863,19 @@ class rcube_sieve_script } } + $action['options'] = (array) $action['options']; + + // Old-draft way: :method "mailto" :options "email@address" + if (!empty($action['method']) && !empty($action['options'])) { + $action['method'] .= ':' . array_shift($action['options']); + } // unnamed parameter is a :method in enotify extension - if (!isset($action['method'])) { + else if (!isset($action['method'])) { $action['method'] = array_pop($tokens); } - $method_components = parse_url($action['method']); - if ($method_components['scheme'] == 'mailto') { - $action['address'] = $method_components['path']; - $method_params = array(); - if (array_key_exists('query', $method_components)) { - parse_str($method_components['query'], $method_params); - } - $method_params = array_change_key_case($method_params, CASE_LOWER); - // magic_quotes_gpc and magic_quotes_sybase affect the output of parse_str - if (ini_get('magic_quotes_gpc') || ini_get('magic_quotes_sybase')) { - array_map('stripslashes', $method_params); - } - $action['body'] = (array_key_exists('body', $method_params)) ? $method_params['body'] : ''; - } - $result[] = $action; break; - } if ($separator == $end) diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php new file mode 100644 index 000000000..636b5fcc1 --- /dev/null +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php @@ -0,0 +1,303 @@ +<?php + +/** + * Managesieve Vacation Engine + * + * Engine part of Managesieve plugin implementing UI and backend access. + * + * Copyright (C) 2011-2014, Kolab Systems AG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +class rcube_sieve_vacation extends rcube_sieve_engine +{ + function actions() + { + $error = $this->start('vacation'); + + // find current vacation rule + if (!$error) { + $this->vacation_rule(); + $this->vacation_post(); + } + + $this->plugin->add_label('vacation.saving'); + $this->rc->output->add_handlers(array( + 'vacationform' => array($this, 'vacation_form'), + )); + + $this->rc->output->set_pagetitle($this->plugin->gettext('vacation')); + $this->rc->output->send('managesieve.vacation'); + } + + private function vacation_rule() + { + $this->vacation = array(); + + if (empty($this->active)) { + return; + } + + $list = array(); + + // find (first) vacation rule + foreach ($this->script as $idx => $rule) { + if (empty($this->vacation) && !empty($rule['actions']) && $rule['actions'][0]['type'] == 'vacation') { + $this->vacation = array_merge($rule['actions'][0], array( + 'idx' => $idx, + 'disabled' => $rule['disabled'], + 'name' => $rule['name'], + 'tests' => $rule['tests'], + )); + } + else { + $list[$idx] = $rule['name']; + } + } + + $this->vacation['list'] = $list; + } + + private function vacation_post() + { + if (empty($_POST)) { + return; + } + + $status = rcube_utils::get_input_value('vacation_status', rcube_utils::INPUT_POST); + $subject = rcube_utils::get_input_value('vacation_subject', rcube_utils::INPUT_POST, true); + $reason = rcube_utils::get_input_value('vacation_reason', rcube_utils::INPUT_POST, true); + $addresses = rcube_utils::get_input_value('vacation_addresses', rcube_utils::INPUT_POST, true); + $interval = rcube_utils::get_input_value('vacation_interval', rcube_utils::INPUT_POST); + $interval_type = rcube_utils::get_input_value('vacation_interval_type', rcube_utils::INPUT_POST); + $date_from = rcube_utils::get_input_value('vacation_datefrom', rcube_utils::INPUT_POST); + $date_to = rcube_utils::get_input_value('vacation_dateto', rcube_utils::INPUT_POST); + $after = rcube_utils::get_input_value('vacation_after', rcube_utils::INPUT_POST); + + $interval_type = $interval_type == 'seconds' ? 'seconds' : 'days'; + $vacation_action['type'] = 'vacation'; + $vacation_action['reason'] = $this->strip_value(str_replace("\r\n", "\n", $reason)); + $vacation_action['subject'] = $subject; + $vacation_action['addresses'] = $addresses; + $vacation_action[$interval_type] = $interval; + $vacation_tests = (array) $this->vacation['tests']; + + foreach ((array) $vacation_action['addresses'] as $aidx => $address) { + $vacation_action['addresses'][$aidx] = $address = trim($address); + + if (empty($address)) { + unset($vacation_action['addresses'][$aidx]); + } + else if (!rcube_utils::check_email($address)) { + $error = 'noemailwarning'; + break; + } + } + + if ($vacation_action['reason'] == '') { + $error = 'managesieve.cannotbeempty'; + } + if ($vacation_action[$interval_type] && !preg_match('/^[0-9]+$/', $vacation_action[$interval_type])) { + $error = 'managesieve.forbiddenchars'; + } + + foreach (array('date_from', 'date_to') as $var) { + $date = $$var; + + if ($date && ($dt = rcube_utils::anytodatetime($date))) { + $type = 'value-' . ($var == 'date_from' ? 'ge' : 'le'); + $test = array( + 'test' => 'currentdate', + 'part' => 'date', + 'type' => $type, + 'arg' => $dt->format('Y-m-d'), + ); + + // find existing date rule + foreach ((array) $vacation_tests as $idx => $t) { + if ($t['test'] == 'currentdate' && $t['part'] == 'date' && $t['type'] == $type) { + $vacation_tests[$idx] = $test; + continue 2; + } + } + + $vacation_tests[] = $test; + } + } + + if (empty($vacation_tests)) { + $vacation_tests = $this->rc->config->get('managesieve_vacation_test', array(array('test' => 'true'))); + } + + // @TODO: handle situation when there's no active script + + if (!$error) { + $rule = $this->vacation; + $rule['type'] = 'if'; + $rule['name'] = $rule['name'] ?: $this->plugin->gettext('vacation'); + $rule['disabled'] = $status == 'off'; + $rule['actions'][0] = $vacation_action; + $rule['tests'] = $vacation_tests; + $rule['join'] = count($vacation_tests) > 1; + + // reset original vacation rule + if (isset($this->vacation['idx'])) { + $this->script[$this->vacation['idx']] = null; + } + + // re-order rules if needed + if (isset($after) && $after !== '') { + // add at target position + if ($after >= count($this->script) - 1) { + $this->script[] = $rule; + } + else { + $script = array(); + + foreach ($this->script as $idx => $r) { + if ($r) { + $script[] = $r; + } + + if ($idx == $after) { + $script[] = $rule; + } + } + + $this->script = $script; + } + } + else { + array_unshift($this->script, $rule); + } + + $this->sieve->script->content = array_values(array_filter($this->script)); + + if ($this->save_script()) { + $this->rc->output->show_message('managesieve.vacationsaved', 'confirmation'); + $this->rc->output->send(); + } + } + + $this->rc->output->show_message($error ? $error : 'managesieve.saveerror', 'error'); + $this->rc->output->send(); + } + + /** + * Independent vacation form + */ + public function vacation_form($attrib) + { + // check supported extensions + $date_extension = in_array('date', $this->exts); + $seconds_extension = in_array('vacation-seconds', $this->exts); + + // build FORM tag + $form_id = !empty($attrib['id']) ? $attrib['id'] : 'form'; + $out = $this->rc->output->request_form(array( + 'id' => $form_id, + 'name' => $form_id, + 'method' => 'post', + 'task' => 'settings', + 'action' => 'plugin.managesieve-vacation', + 'noclose' => true + ) + $attrib); + + // form elements + $subject = new html_inputfield(array('name' => 'vacation_subject', 'size' => 50)); + $reason = new html_textarea(array('name' => 'vacation_reason', 'cols' => 60, 'rows' => 8)); + $interval = new html_inputfield(array('name' => 'vacation_interval', 'size' => 5)); + $addresses = '<textarea name="vacation_addresses" data-type="list" data-size="30" style="display: none">' + . rcube::Q(implode("\n", (array) $this->vacation['addresses']), 'strict', false) . '</textarea>'; + $status = new html_select(array('name' => 'vacation_status')); + + $status->add($this->plugin->gettext('vacation.on'), 'on'); + $status->add($this->plugin->gettext('vacation.off'), 'off'); + + if ($this->rc->config->get('managesieve_vacation') != 2 && count($this->vacation['list'])) { + $after = new html_select(array('name' => 'vacation_after')); + + $after->add('', ''); + foreach ($this->vacation['list'] as $idx => $rule) { + $after->add($rule, $idx); + } + } + + $interval_txt = $interval->show(isset($this->vacation['seconds']) ? $this->vacation['seconds'] : $this->vacation['days']); + if ($seconds_extension) { + $interval_select = new html_select(array('name' => 'vacation_interval_type')); + $interval_select->add($this->plugin->gettext('days'), 'days'); + $interval_select->add($this->plugin->gettext('seconds'), 'seconds'); + $interval_txt .= ' ' . $interval_select->show(isset($this->vacation['seconds']) ? 'seconds' : 'days'); + } + else { + $interval_txt .= ' ' . $this->plugin->gettext('days'); + } + + if ($date_extension) { + $date_from = new html_inputfield(array('name' => 'vacation_datefrom', 'class' => 'datepicker', 'size' => 12)); + $date_to = new html_inputfield(array('name' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12)); + $date_format = $this->rc->config->get('date_format', 'Y-m-d'); + + foreach ((array) $this->vacation['tests'] as $test) { + if ($test['test'] == 'currentdate' && $test['part'] == 'date') { + $date = $this->rc->format_date($test['arg'], $date_format, false); + $date_value[$test['type'] == 'value-ge' ? 'from' : 'to'] = $date; + } + } + } + + // Message tab + $table = new html_table(array('cols' => 2)); + + $table->add('title', html::label('vacation_subject', $this->plugin->gettext('vacation.subject'))); + $table->add(null, $subject->show($this->vacation['subject'])); + $table->add('title', html::label('vacation_reason', $this->plugin->gettext('vacation.body'))); + $table->add(null, $reason->show($this->vacation['reason'])); + + if ($date_extension) { + $table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.dates'))); + $table->add(null, + $this->plugin->gettext('vacation.from'). ' ' . $date_from->show($date_value['from']) + . ' ' . $this->plugin->gettext('vacation.to'). ' ' . $date_to->show($date_value['to']) + ); + } + + $table->add('title', html::label('vacation_status', $this->plugin->gettext('vacation.status'))); + $table->add(null, $status->show($this->vacation['disabled'] ? 'off' : 'on')); + + $out .= html::tag('fieldset', $class, html::tag('legend', null, $this->plugin->gettext('vacation.reply')) . $table->show($attrib)); + + // Advanced tab + $table = new html_table(array('cols' => 2)); + + $table->add('title', $this->plugin->gettext('vacation.addresses')); + $table->add(null, $addresses); + $table->add('title', $this->plugin->gettext('vacation.interval')); + $table->add(null, $interval_txt); + if ($after) { + $table->add('title', $this->plugin->gettext('vacation.after')); + $table->add(null, $after->show($this->vacation['idx'] - 1)); + } + + $out .= html::tag('fieldset', $class, html::tag('legend', null, $this->plugin->gettext('vacation.advanced')) . $table->show($attrib)); + + $out .= '</form>'; + + $this->rc->output->add_gui_object('sieveform', $form_id); + + return $out; + } +} |