X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=plugins%2Fmanagesieve%2Fmanagesieve.php;h=d557907968d40e178c960275f3494cda8c543add;hb=76507f7c63a660742e76889ad6e3919f3dde3bb0;hp=d8b6084e45bccb517848327e6864fd5bf6f21eb0;hpb=07e1de2dcd3f3ff8910a3680493f035b3c693cf0;p=roundcube.git diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php index d8b6084..d557907 100644 --- a/plugins/managesieve/managesieve.php +++ b/plugins/managesieve/managesieve.php @@ -7,17 +7,33 @@ * It's clickable interface which operates on text scripts and communicates * with server using managesieve protocol. Adds Filters tab in Settings. * - * @version 3.0 - * @author Aleksander 'A.L.E.C' Machniak + * @version 5.0 + * @author Aleksander Machniak * * Configuration (see config.inc.php.dist) * - * $Id: managesieve.php 4555 2011-02-16 10:48:11Z alec $ + * Copyright (C) 2008-2011, The Roundcube Dev Team + * Copyright (C) 2011, 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 version 2 + * as published by the Free Software Foundation. + * + * 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * $Id: managesieve.php 5452 2011-11-18 14:44:48Z alec $ */ class managesieve extends rcube_plugin { - public $task = 'settings'; + public $task = 'mail|settings'; private $rc; private $sieve; @@ -26,29 +42,141 @@ class managesieve extends rcube_plugin private $tips = array(); private $script = array(); private $exts = array(); + private $list; + private $active = array(); private $headers = array( - 'subject' => 'Subject', - 'sender' => 'From', - 'recipient' => 'To', + 'subject' => 'Subject', + 'from' => 'From', + 'to' => 'To', + ); + private $addr_headers = array( + // Required + "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to", + // Additional (RFC 822 / RFC 2822) + "reply-to", "resent-reply-to", "resent-sender", "resent-cc", "resent-bcc", + // Non-standard (RFC 2076, draft-palme-mailext-headers-08.txt) + "for-approval", "for-handling", "for-comment", "apparently-to", "errors-to", + "delivered-to", "return-receipt-to", "x-admin", "read-receipt-to", + "x-confirm-reading-to", "return-receipt-requested", + "registered-mail-reply-requested-by", "mail-followup-to", "mail-reply-to", + "abuse-reports-to", "x-complaints-to", "x-report-abuse-to", + // Undocumented + "x-beenthere", ); + const VERSION = '5.0'; + const PROGNAME = 'Roundcube (Managesieve)'; + function init() { - // add Tab label/title - $this->add_texts('localization/', array('filters','managefilters')); + $this->rc = rcmail::get_instance(); // register actions $this->register_action('plugin.managesieve', array($this, 'managesieve_actions')); $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save')); - // include main js script + if ($this->rc->task == 'settings') { + $this->init_ui(); + } + else if ($this->rc->task == 'mail') { + // register message hook + $this->add_hook('message_headers_output', array($this, 'mail_headers')); + + // inject Create Filter popup stuff + if (empty($this->rc->action) || $this->rc->action == 'show') { + $this->mail_task_handler(); + } + } + } + + /** + * Initializes plugin's UI (localization, js script) + */ + private function init_ui() + { + if ($this->ui_initialized) + return; + + // load localization + $this->add_texts('localization/', array('filters','managefilters')); $this->include_script('managesieve.js'); + + $this->ui_initialized = true; } + /** + * Add UI elements to the 'mailbox view' and 'show message' UI. + */ + function mail_task_handler() + { + // use jQuery for popup window + $this->require_plugin('jqueryui'); + + // include js script and localization + $this->init_ui(); + + // include styles + $skin = $this->rc->config->get('skin'); + if (!file_exists($this->home."/skins/$skin/managesieve_mail.css")) + $skin = 'default'; + $this->include_stylesheet("skins/$skin/managesieve_mail.css"); + + // add 'Create filter' item to message menu + $this->api->add_content(html::tag('li', null, + $this->api->output->button(array( + 'command' => 'managesieve-create', + 'label' => 'managesieve.filtercreate', + 'type' => 'link', + 'classact' => 'filterlink active', + 'class' => 'filterlink', + ))), 'messagemenu'); + + // register some labels/messages + $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata', + 'managesieve.nodata', 'managesieve.nextstep', 'save'); + + $this->rc->session->remove('managesieve_current'); + } + + /** + * Get message headers for popup window + */ + function mail_headers($args) + { + $headers = $args['headers']; + $ret = array(); + + if ($headers->subject) + $ret[] = array('Subject', $this->rc->imap->decode_header($headers->subject)); + + // @TODO: List-Id, others? + foreach (array('From', 'To') as $h) { + $hl = strtolower($h); + if ($headers->$hl) { + $list = $this->rc->imap->decode_address_list($headers->$hl); + foreach ($list as $item) { + if ($item['mailto']) { + $ret[] = array($h, $item['mailto']); + } + } + } + } + + if ($this->rc->action == 'preview') + $this->rc->output->command('parent.set_env', array('sieve_headers' => $ret)); + else + $this->rc->output->set_env('sieve_headers', $ret); + + + return $args; + } + + /** + * Loads configuration, initializes plugin (including sieve connection) + */ function managesieve_start() { - $this->rc = rcmail::get_instance(); $this->load_config(); // register UI objects @@ -60,41 +188,57 @@ class managesieve extends rcube_plugin 'filtersetform' => array($this, 'filterset_form'), )); - require_once($this->home . '/lib/Net/Sieve.php'); - require_once($this->home . '/lib/rcube_sieve.php'); + // Add include path for internal classes + $include_path = $this->home . '/lib' . PATH_SEPARATOR; + $include_path .= ini_get('include_path'); + set_include_path($include_path); $host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost')); $port = $this->rc->config->get('managesieve_port', 2000); $host = rcube_idn_to_ascii($host); + $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array( + 'user' => $_SESSION['username'], + 'password' => $this->rc->decrypt($_SESSION['password']), + 'host' => $host, + 'port' => $port, + 'auth_type' => $this->rc->config->get('managesieve_auth_type'), + 'usetls' => $this->rc->config->get('managesieve_usetls', false), + 'disabled' => $this->rc->config->get('managesieve_disabled_extensions'), + 'debug' => $this->rc->config->get('managesieve_debug', false), + 'auth_cid' => $this->rc->config->get('managesieve_auth_cid'), + 'auth_pw' => $this->rc->config->get('managesieve_auth_pw'), + )); + // try to connect to managesieve server and to fetch the script - $this->sieve = new rcube_sieve($_SESSION['username'], - $this->rc->decrypt($_SESSION['password']), $host, $port, - $this->rc->config->get('managesieve_auth_type'), - $this->rc->config->get('managesieve_usetls', false), - $this->rc->config->get('managesieve_disabled_extensions'), - $this->rc->config->get('managesieve_debug', false), - $this->rc->config->get('managesieve_auth_cid'), - $this->rc->config->get('managesieve_auth_pw') + $this->sieve = new rcube_sieve( + $plugin['user'], + $plugin['password'], + $plugin['host'], + $plugin['port'], + $plugin['auth_type'], + $plugin['usetls'], + $plugin['disabled'], + $plugin['debug'], + $plugin['auth_cid'], + $plugin['auth_pw'] ); if (!($error = $this->sieve->error())) { + // Get list of scripts + $list = $this->list_scripts(); - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - $_SESSION['managesieve_active'] = $active; - - if (!empty($_GET['_set'])) { - $script_name = get_input_value('_set', RCUBE_INPUT_GET); + if (!empty($_GET['_set']) || !empty($_POST['_set'])) { + $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true); } else if (!empty($_SESSION['managesieve_current'])) { $script_name = $_SESSION['managesieve_current']; } else { - // get active script - if ($active) { - $script_name = $active; + // get (first) active script + if (!empty($this->active[0])) { + $script_name = $this->active[0]; } else if ($list) { $script_name = $list[0]; @@ -103,19 +247,25 @@ class managesieve extends rcube_plugin else { // if script not exists build default script contents $script_file = $this->rc->config->get('managesieve_default'); - $script_name = 'roundcube'; + $script_name = $this->rc->config->get('managesieve_script_name'); + + if (empty($script_name)) + $script_name = 'roundcube'; + if ($script_file && is_readable($script_file)) $content = file_get_contents($script_file); - // add script and set it active - if ($this->sieve->save_script($script_name, $content)) - if ($this->sieve->activate($script_name)) - $_SESSION['managesieve_active'] = $script_name; + // add script and set it active + if ($this->sieve->save_script($script_name, $content)) { + $this->activate_script($script_name); + $this->list[] = $script_name; + } } } - if ($script_name) + if ($script_name) { $this->sieve->load($script_name); + } $error = $this->sieve->error(); } @@ -141,9 +291,9 @@ class managesieve extends rcube_plugin $this->script = array(); } else { - $this->script = $this->sieve->script->as_array(); $this->exts = $this->sieve->get_extensions(); - $this->rc->output->set_env('active_set', $_SESSION['managesieve_active']); + $this->script = $this->sieve->script->as_array(); + $this->rc->output->set_env('currentset', $this->sieve->current); $_SESSION['managesieve_current'] = $this->sieve->current; } @@ -152,94 +302,131 @@ class managesieve extends rcube_plugin function managesieve_actions() { - // Init plugin and handle managesieve connection + $this->init_ui(); + $error = $this->managesieve_start(); // Handle user requests if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) { - $fid = (int) get_input_value('_fid', RCUBE_INPUT_GET); + $fid = (int) get_input_value('_fid', RCUBE_INPUT_POST); - if ($action == 'up' && !$error) { - if ($fid && isset($this->script[$fid]) && isset($this->script[$fid-1])) { - if ($this->sieve->script->update_rule($fid, $this->script[$fid-1]) !== false - && $this->sieve->script->update_rule($fid-1, $this->script[$fid]) !== false) { - $result = $this->sieve->save(); - } + if ($action == 'delete' && !$error) { + if (isset($this->script[$fid])) { + if ($this->sieve->script->delete_rule($fid)) + $result = $this->save_script(); - if ($result) { -// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->command('managesieve_updatelist', 'up', '', $fid); - } else - $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); + if ($result === true) { + $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation'); + $this->rc->output->command('managesieve_updatelist', 'del', array('id' => $fid)); + } else { + $this->rc->output->show_message('managesieve.filterdeleteerror', 'error'); + } } } - else if ($action == 'down' && !$error) { - if (isset($this->script[$fid]) && isset($this->script[$fid+1])) { - if ($this->sieve->script->update_rule($fid, $this->script[$fid+1]) !== false - && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false) { - $result = $this->sieve->save(); + else if ($action == 'move' && !$error) { + if (isset($this->script[$fid])) { + $to = (int) get_input_value('_to', RCUBE_INPUT_POST); + $rule = $this->script[$fid]; + + // remove rule + unset($this->script[$fid]); + $this->script = array_values($this->script); + + // add at target position + if ($to >= count($this->script)) { + $this->script[] = $rule; + } + else { + $script = array(); + foreach ($this->script as $idx => $r) { + if ($idx == $to) + $script[] = $rule; + $script[] = $r; + } + $this->script = $script; } + $this->sieve->script->content = $this->script; + $result = $this->save_script(); + if ($result === true) { -// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->command('managesieve_updatelist', 'down', '', $fid); + $result = $this->list_rules(); + + $this->rc->output->show_message('managesieve.moved', 'confirmation'); + $this->rc->output->command('managesieve_updatelist', 'list', + array('list' => $result, 'clear' => true, 'set' => $to)); } else { - $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); + $this->rc->output->show_message('managesieve.moveerror', 'error'); } } } - else if ($action == 'delete' && !$error) { + else if ($action == 'act' && !$error) { if (isset($this->script[$fid])) { - if ($this->sieve->script->delete_rule($fid)) - $result = $this->sieve->save(); + $rule = $this->script[$fid]; + $disabled = $rule['disabled'] ? true : false; + $rule['disabled'] = !$disabled; + $result = $this->sieve->script->update_rule($fid, $rule); + + if ($result !== false) + $result = $this->save_script(); if ($result === true) { - $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation'); - $this->rc->output->command('managesieve_updatelist', 'delete', '', $fid); + if ($rule['disabled']) + $this->rc->output->show_message('managesieve.deactivated', 'confirmation'); + else + $this->rc->output->show_message('managesieve.activated', 'confirmation'); + $this->rc->output->command('managesieve_updatelist', 'update', + array('id' => $fid, 'disabled' => $rule['disabled'])); } else { - $this->rc->output->show_message('managesieve.filterdeleteerror', 'error'); + if ($rule['disabled']) + $this->rc->output->show_message('managesieve.deactivateerror', 'error'); + else + $this->rc->output->show_message('managesieve.activateerror', 'error'); } } } else if ($action == 'setact' && !$error) { - $script_name = get_input_value('_set', RCUBE_INPUT_GPC); - $result = $this->sieve->activate($script_name); + $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true); + $result = $this->activate_script($script_name); + $kep14 = $this->rc->config->get('managesieve_kolab_master'); if ($result === true) { - $this->rc->output->set_env('active_set', $script_name); + $this->rc->output->set_env('active_sets', $this->active); $this->rc->output->show_message('managesieve.setactivated', 'confirmation'); - $this->rc->output->command('managesieve_reset'); - $_SESSION['managesieve_active'] = $script_name; + $this->rc->output->command('managesieve_updatelist', 'setact', + array('name' => $script_name, 'active' => true, 'all' => !$kep14)); } else { $this->rc->output->show_message('managesieve.setactivateerror', 'error'); } } else if ($action == 'deact' && !$error) { - $result = $this->sieve->deactivate(); + $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true); + $result = $this->deactivate_script($script_name); if ($result === true) { - $this->rc->output->set_env('active_set', ''); + $this->rc->output->set_env('active_sets', $this->active); $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation'); - $this->rc->output->command('managesieve_reset'); - $_SESSION['managesieve_active'] = ''; + $this->rc->output->command('managesieve_updatelist', 'setact', + array('name' => $script_name, 'active' => false)); } else { $this->rc->output->show_message('managesieve.setdeactivateerror', 'error'); } } else if ($action == 'setdel' && !$error) { - $script_name = get_input_value('_set', RCUBE_INPUT_GPC); - $result = $this->sieve->remove($script_name); + $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true); + $result = $this->remove_script($script_name); if ($result === true) { $this->rc->output->show_message('managesieve.setdeleted', 'confirmation'); - $this->rc->output->command('managesieve_reload'); + $this->rc->output->command('managesieve_updatelist', 'setdel', + array('name' => $script_name)); $this->rc->session->remove('managesieve_current'); } else { $this->rc->output->show_message('managesieve.setdeleteerror', 'error'); } } else if ($action == 'setget') { - $script_name = get_input_value('_set', RCUBE_INPUT_GPC); + $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true); $script = $this->sieve->get_script($script_name); if (PEAR::isError($script)) @@ -264,14 +451,19 @@ class managesieve extends rcube_plugin echo $script; exit; } - elseif ($action == 'ruleadd') { + else if ($action == 'list') { + $result = $this->list_rules(); + + $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result)); + } + else if ($action == 'ruleadd') { $rid = get_input_value('_rid', RCUBE_INPUT_GPC); $id = $this->genid(); $content = $this->rule_div($fid, $id, false); $this->rc->output->command('managesieve_rulefill', $content, $id, $rid); } - elseif ($action == 'actionadd') { + else if ($action == 'actionadd') { $aid = get_input_value('_aid', RCUBE_INPUT_GPC); $id = $this->genid(); $content = $this->action_div($fid, $id, false); @@ -281,25 +473,76 @@ class managesieve extends rcube_plugin $this->rc->output->send(); } + else if ($this->rc->task == 'mail') { + // Initialize the form + $rules = get_input_value('r', RCUBE_INPUT_GET); + if (!empty($rules)) { + $i = 0; + foreach ($rules as $rule) { + list($header, $value) = explode(':', $rule, 2); + $tests[$i] = array( + 'type' => 'contains', + 'test' => 'header', + 'arg1' => $header, + 'arg2' => $value, + ); + $i++; + } + + $this->form = array( + 'join' => count($tests) > 1 ? 'allof' : 'anyof', + 'name' => '', + 'tests' => $tests, + 'actions' => array( + 0 => array('type' => 'fileinto'), + 1 => array('type' => 'stop'), + ), + ); + } + } $this->managesieve_send(); } function managesieve_save() { + // load localization + $this->add_texts('localization/', array('filters','managefilters')); + + // include main js script + if ($this->api->output->type == 'html') { + $this->include_script('managesieve.js'); + } + // Init plugin and handle managesieve connection $error = $this->managesieve_start(); // filters set add action if (!empty($_POST['_newset'])) { - $name = get_input_value('_name', RCUBE_INPUT_POST); - $copy = get_input_value('_copy', RCUBE_INPUT_POST); - $from = get_input_value('_from', RCUBE_INPUT_POST); - - if (!$name) - $error = 'managesieve.emptyname'; - else if (mb_strlen($name)>128) - $error = 'managesieve.nametoolong'; + + $name = get_input_value('_name', RCUBE_INPUT_POST, true); + $copy = get_input_value('_copy', RCUBE_INPUT_POST, true); + $from = get_input_value('_from', RCUBE_INPUT_POST); + $exceptions = $this->rc->config->get('managesieve_filename_exceptions'); + $kolab = $this->rc->config->get('managesieve_kolab_master'); + $name_uc = mb_strtolower($name); + $list = $this->list_scripts(); + + if (!$name) { + $this->errors['name'] = $this->gettext('cannotbeempty'); + } + else if (mb_strlen($name) > 128) { + $this->errors['name'] = $this->gettext('nametoolong'); + } + else if (!empty($exceptions) && in_array($name, (array)$exceptions)) { + $this->errors['name'] = $this->gettext('namereserved'); + } + else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) { + $this->errors['name'] = $this->gettext('namereserved'); + } + else if (in_array($name, $list)) { + $this->errors['name'] = $this->gettext('setexist'); + } else if ($from == 'file') { // from file if (is_uploaded_file($_FILES['_file']['tmp_name'])) { @@ -308,13 +551,12 @@ class managesieve extends rcube_plugin // for security don't save script directly // check syntax before, like this... $this->sieve->load_script($file); - if (!$this->sieve->save($name)) { - $error = 'managesieve.setcreateerror'; + if (!$this->save_script($name)) { + $this->errors['file'] = $this->gettext('setcreateerror'); } } else { // upload failed $err = $_FILES['_file']['error']; - $error = true; if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { $msg = rcube_label(array('name' => 'filesizeerror', @@ -322,7 +564,7 @@ class managesieve extends rcube_plugin show_bytes(parse_bytes(ini_get('upload_max_filesize')))))); } else { - $error = 'fileuploaderror'; + $this->errors['file'] = $this->gettext('fileuploaderror'); } } } @@ -330,12 +572,19 @@ class managesieve extends rcube_plugin $error = 'managesieve.setcreateerror'; } - if (!$error) { + if (!$error && empty($this->errors)) { + // Find position of the new script on the list + $list[] = $name; + asort($list, SORT_LOCALE_STRING); + $list = array_values($list); + $index = array_search($name, $list); + $this->rc->output->show_message('managesieve.setcreated', 'confirmation'); - $this->rc->output->command('parent.managesieve_reload', $name); + $this->rc->output->command('parent.managesieve_updatelist', 'setadd', + array('name' => $name, 'index' => $index)); } else if ($msg) { $this->rc->output->command('display_message', $msg, 'error'); - } else { + } else if ($error) { $this->rc->output->show_message($error, 'error'); } } @@ -346,20 +595,27 @@ class managesieve extends rcube_plugin $join = trim(get_input_value('_join', RCUBE_INPUT_POST)); // and arrays - $headers = $_POST['_header']; - $cust_headers = $_POST['_custom_header']; - $ops = $_POST['_rule_op']; - $sizeops = $_POST['_rule_size_op']; - $sizeitems = $_POST['_rule_size_item']; - $sizetargets = $_POST['_rule_size_target']; - $targets = $_POST['_rule_target']; - $act_types = $_POST['_action_type']; - $mailboxes = $_POST['_action_mailbox']; - $act_targets = $_POST['_action_target']; - $area_targets = $_POST['_action_target_area']; - $reasons = $_POST['_action_reason']; - $addresses = $_POST['_action_addresses']; - $days = $_POST['_action_days']; + $headers = get_input_value('_header', RCUBE_INPUT_POST); + $cust_headers = get_input_value('_custom_header', RCUBE_INPUT_POST); + $ops = get_input_value('_rule_op', RCUBE_INPUT_POST); + $sizeops = get_input_value('_rule_size_op', RCUBE_INPUT_POST); + $sizeitems = get_input_value('_rule_size_item', RCUBE_INPUT_POST); + $sizetargets = get_input_value('_rule_size_target', RCUBE_INPUT_POST); + $targets = get_input_value('_rule_target', RCUBE_INPUT_POST, true); + $mods = get_input_value('_rule_mod', RCUBE_INPUT_POST); + $mod_types = get_input_value('_rule_mod_type', RCUBE_INPUT_POST); + $body_trans = get_input_value('_rule_trans', RCUBE_INPUT_POST); + $body_types = get_input_value('_rule_trans_type', RCUBE_INPUT_POST, true); + $comparators = get_input_value('_rule_comp', RCUBE_INPUT_POST); + $act_types = get_input_value('_action_type', RCUBE_INPUT_POST, true); + $mailboxes = get_input_value('_action_mailbox', RCUBE_INPUT_POST, true); + $act_targets = get_input_value('_action_target', RCUBE_INPUT_POST, true); + $area_targets = get_input_value('_action_target_area', RCUBE_INPUT_POST, true); + $reasons = get_input_value('_action_reason', RCUBE_INPUT_POST, true); + $addresses = get_input_value('_action_addresses', RCUBE_INPUT_POST, true); + $days = get_input_value('_action_days', RCUBE_INPUT_POST); + $subject = get_input_value('_action_subject', RCUBE_INPUT_POST, true); + $flags = get_input_value('_action_flags', RCUBE_INPUT_POST); // we need a "hack" for radiobuttons foreach ($sizeitems as $item) @@ -373,12 +629,13 @@ class managesieve extends rcube_plugin if ($name == '') $this->errors['name'] = $this->gettext('cannotbeempty'); - else + else { foreach($this->script as $idx => $rule) if($rule['name'] == $name && $idx != $fid) { $this->errors['name'] = $this->gettext('ruleexist'); break; } + } $i = 0; // rules @@ -387,23 +644,101 @@ class managesieve extends rcube_plugin } else { foreach ($headers as $idx => $header) { - $header = $this->strip_value($header); - $target = $this->strip_value($targets[$idx], true); - $op = $this->strip_value($ops[$idx]); + $header = $this->strip_value($header); + $target = $this->strip_value($targets[$idx], true); + $operator = $this->strip_value($ops[$idx]); + $comparator = $this->strip_value($comparators[$idx]); + + if ($header == 'size') { + $sizeop = $this->strip_value($sizeops[$idx]); + $sizeitem = $this->strip_value($items[$idx]); + $sizetarget = $this->strip_value($sizetargets[$idx]); + + $this->form['tests'][$i]['test'] = 'size'; + $this->form['tests'][$i]['type'] = $sizeop; + $this->form['tests'][$i]['arg'] = $sizetarget; + + if ($sizetarget == '') + $this->errors['tests'][$i]['sizetarget'] = $this->gettext('cannotbeempty'); + else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) { + $this->errors['tests'][$i]['sizetarget'] = $this->gettext('forbiddenchars'); + $this->form['tests'][$i]['item'] = $sizeitem; + } + else + $this->form['tests'][$i]['arg'] .= $m[1]; + } + else if ($header == 'body') { + $trans = $this->strip_value($body_trans[$idx]); + $trans_type = $this->strip_value($body_types[$idx], true); - // normal header - if (in_array($header, $this->headers)) { - if (preg_match('/^not/', $op)) + if (preg_match('/^not/', $operator)) $this->form['tests'][$i]['not'] = true; - $type = preg_replace('/^not/', '', $op); + $type = preg_replace('/^not/', '', $operator); + + if ($type == 'exists') { + $this->errors['tests'][$i]['op'] = true; + } + + $this->form['tests'][$i]['test'] = 'body'; + $this->form['tests'][$i]['type'] = $type; + $this->form['tests'][$i]['arg'] = $target; + + if ($target == '' && $type != 'exists') + $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); + else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target)) + $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars'); + + $this->form['tests'][$i]['part'] = $trans; + if ($trans == 'content') { + $this->form['tests'][$i]['content'] = $trans_type; + } + } + else { + $cust_header = $headers = $this->strip_value($cust_headers[$idx]); + $mod = $this->strip_value($mods[$idx]); + $mod_type = $this->strip_value($mod_types[$idx]); + + if (preg_match('/^not/', $operator)) + $this->form['tests'][$i]['not'] = true; + $type = preg_replace('/^not/', '', $operator); + + if ($header == '...') { + $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY); + + if (!count($headers)) + $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); + else { + foreach ($headers as $hr) + if (!preg_match('/^[a-z0-9-]+$/i', $hr)) + $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars'); + } + + if (empty($this->errors['tests'][$i]['header'])) + $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; + } if ($type == 'exists') { $this->form['tests'][$i]['test'] = 'exists'; - $this->form['tests'][$i]['arg'] = $header; + $this->form['tests'][$i]['arg'] = $header == '...' ? $cust_header : $header; } else { + $test = 'header'; + $header = $header == '...' ? $cust_header : $header; + + if ($mod == 'address' || $mod == 'envelope') { + $found = false; + if (empty($this->errors['tests'][$i]['header'])) { + foreach ((array)$header as $hdr) { + if (!in_array(strtolower(trim($hdr)), $this->addr_headers)) + $found = true; + } + } + if (!$found) + $test = $mod; + } + $this->form['tests'][$i]['type'] = $type; - $this->form['tests'][$i]['test'] = 'header'; + $this->form['tests'][$i]['test'] = $test; $this->form['tests'][$i]['arg1'] = $header; $this->form['tests'][$i]['arg2'] = $target; @@ -411,65 +746,20 @@ class managesieve extends rcube_plugin $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target)) $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars'); + + if ($mod) { + $this->form['tests'][$i]['part'] = $mod_type; + } } } - else - switch ($header) { - case 'size': - $sizeop = $this->strip_value($sizeops[$idx]); - $sizeitem = $this->strip_value($items[$idx]); - $sizetarget = $this->strip_value($sizetargets[$idx]); - - $this->form['tests'][$i]['test'] = 'size'; - $this->form['tests'][$i]['type'] = $sizeop; - $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem; - - if ($sizetarget == '') - $this->errors['tests'][$i]['sizetarget'] = $this->gettext('cannotbeempty'); - else if (!preg_match('/^[0-9]+(K|M|G)*$/i', $sizetarget)) - $this->errors['tests'][$i]['sizetarget'] = $this->gettext('forbiddenchars'); - break; - case '...': - $cust_header = $headers = $this->strip_value($cust_headers[$idx]); - - if (preg_match('/^not/', $op)) - $this->form['tests'][$i]['not'] = true; - $type = preg_replace('/^not/', '', $op); - - if ($cust_header == '') - $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); - else { - $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY); - - if (!count($headers)) - $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); - else { - foreach ($headers as $hr) - if (!preg_match('/^[a-z0-9-]+$/i', $hr)) - $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars'); - } - } - if (empty($this->errors['tests'][$i]['header'])) - $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; + if ($header != 'size' && $comparator) { + if (preg_match('/^(value|count)/', $this->form['tests'][$i]['type'])) + $comparator = 'i;ascii-numeric'; + + $this->form['tests'][$i]['comparator'] = $comparator; + } - if ($type == 'exists') { - $this->form['tests'][$i]['test'] = 'exists'; - $this->form['tests'][$i]['arg'] = $cust_header; - } - else { - $this->form['tests'][$i]['test'] = 'header'; - $this->form['tests'][$i]['type'] = $type; - $this->form['tests'][$i]['arg1'] = $cust_header; - $this->form['tests'][$i]['arg2'] = $target; - - if ($target == '') - $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); - else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target)) - $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars'); - } - break; - } $i++; } } @@ -481,15 +771,17 @@ class managesieve extends rcube_plugin $target = $this->strip_value($act_targets[$idx]); switch ($type) { + case 'fileinto': case 'fileinto_copy': $mailbox = $this->strip_value($mailboxes[$idx]); - $this->form['actions'][$i]['target'] = $mailbox; + $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in'); if ($type == 'fileinto_copy') { $type = 'fileinto'; $this->form['actions'][$i]['copy'] = true; } break; + case 'reject': case 'ereject': $target = $this->strip_value($area_targets[$idx]); @@ -498,6 +790,7 @@ class managesieve extends rcube_plugin // if ($target == '') // $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty'); break; + case 'redirect': case 'redirect_copy': $this->form['actions'][$i]['target'] = $target; @@ -512,12 +805,29 @@ class managesieve extends rcube_plugin $this->form['actions'][$i]['copy'] = true; } break; + + case 'addflag': + case 'setflag': + case 'removeflag': + $_target = array(); + if (empty($flags[$idx])) { + $this->errors['actions'][$i]['target'] = $this->gettext('noflagset'); + } + else { + foreach ($flags[$idx] as $flag) { + $_target[] = $this->strip_value($flag); + } + } + $this->form['actions'][$i]['target'] = $_target; + break; + case 'vacation': $reason = $this->strip_value($reasons[$idx]); $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason); $this->form['actions'][$i]['days'] = $days[$idx]; + $this->form['actions'][$i]['subject'] = $subject[$idx]; $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]); -// @TODO: vacation :subject, :mime, :from, :handle +// @TODO: vacation :mime, :from, :handle if ($this->form['actions'][$i]['addresses']) { foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) { @@ -543,7 +853,7 @@ class managesieve extends rcube_plugin $i++; } - if (!$this->errors) { + if (!$this->errors && !$error) { // zapis skryptu if (!isset($this->script[$fid])) { $fid = $this->sieve->script->add_rule($this->form); @@ -552,15 +862,23 @@ class managesieve extends rcube_plugin $fid = $this->sieve->script->update_rule($fid, $this->form); if ($fid !== false) - $save = $this->sieve->save(); + $save = $this->save_script(); if ($save && $fid !== false) { $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->add_script( - sprintf("rcmail.managesieve_updatelist('%s', '%s', %d, %d);", - isset($new) ? 'add' : 'update', Q($this->form['name']), - $fid, $this->form['disabled']), - 'foot'); + if ($this->rc->task != 'mail') { + $this->rc->output->command('parent.managesieve_updatelist', + isset($new) ? 'add' : 'update', + array( + 'name' => Q($this->form['name']), + 'id' => $fid, + 'disabled' => $this->form['disabled'] + )); + } + else { + $this->rc->output->command('managesieve_dialog_close'); + $this->rc->output->send('iframe'); + } } else { $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); @@ -596,14 +914,9 @@ class managesieve extends rcube_plugin $attrib['id'] = 'rcmfilterslist'; // define list of cols to be displayed - $a_show_cols = array('managesieve.filtername'); + $a_show_cols = array('name'); - foreach($this->script as $idx => $filter) - $result[] = array( - 'managesieve.filtername' => $filter['name'], - 'id' => $idx, - 'class' => $filter['disabled'] ? 'disabled' : '', - ); + $result = $this->list_rules(); // create XHTML table $out = rcube_table_output($attrib, $result, $a_show_cols, 'id'); @@ -619,35 +932,56 @@ class managesieve extends rcube_plugin } // return the filters list as '; @@ -716,7 +1052,7 @@ class managesieve extends rcube_plugin // script upload box $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30, - 'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : ''))); + 'type' => 'file', 'class' => ($this->errors['file'] ? 'error' : ''))); $out .= '
'; @@ -726,6 +1062,13 @@ class managesieve extends rcube_plugin $this->rc->output->add_gui_object('sieveform', 'filtersetform'); + if ($this->errors['name']) + $this->add_tip('_name', $this->errors['name'], true); + if ($this->errors['file']) + $this->add_tip('_file', $this->errors['file'], true); + + $this->print_tips(); + return $out; } @@ -763,10 +1106,17 @@ class managesieve extends rcube_plugin else $input_name = $input_name->show(); - $out .= sprintf("\n %s

\n", + $out .= sprintf("\n %s\n", $field_id, Q($this->gettext('filtername')), $input_name); - $out .= '
' . Q($this->gettext('messagesrules')) . "\n"; + // filter set selector + if ($this->rc->task == 'mail') { + $out .= sprintf("\n  %s\n", + $field_id, Q($this->gettext('filterset')), + $this->filtersets_list(array('id' => 'sievescriptname'), true)); + } + + $out .= '

' . Q($this->gettext('messagesrules')) . "\n"; // any, allof, anyof radio buttons $field_id = '_allof'; @@ -842,50 +1192,60 @@ class managesieve extends rcube_plugin $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id]; $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']); - $out = $div ? '
'."\n" : ''; - - $out .= ''; + $out .= '
'; - // headers select $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id, - 'onchange' => 'header_select(' .$id .')')); + 'onchange' => 'rule_header_select(' .$id .')')); foreach($this->headers as $name => $val) $select_header->add(Q($this->gettext($name)), Q($val)); + if (in_array('body', $this->exts)) + $select_header->add(Q($this->gettext('body')), 'body'); $select_header->add(Q($this->gettext('size')), 'size'); $select_header->add(Q($this->gettext('...')), '...'); // TODO: list arguments + $aout = ''; - if ((isset($rule['test']) && $rule['test'] == 'header') - && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers)) - $out .= $select_header->show($rule['arg1']); + if ((isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) + && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers) + ) { + $aout .= $select_header->show($rule['arg1']); + } else if ((isset($rule['test']) && $rule['test'] == 'exists') - && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers)) - $out .= $select_header->show($rule['arg']); + && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers) + ) { + $aout .= $select_header->show($rule['arg']); + } else if (isset($rule['test']) && $rule['test'] == 'size') - $out .= $select_header->show('size'); + $aout .= $select_header->show('size'); + else if (isset($rule['test']) && $rule['test'] == 'body') + $aout .= $select_header->show('body'); else if (isset($rule['test']) && $rule['test'] != 'true') - $out .= $select_header->show('...'); + $aout .= $select_header->show('...'); else - $out .= $select_header->show(); - - $out .= ''; + $aout .= $select_header->show(); - if ((isset($rule['test']) && $rule['test'] == 'header') - && (is_array($rule['arg1']) || !in_array($rule['arg1'], $this->headers))) - $custom = is_array($rule['arg1']) ? implode(', ', $rule['arg1']) : $rule['arg1']; - else if ((isset($rule['test']) && $rule['test'] == 'exists') - && (is_array($rule['arg']) || !in_array($rule['arg'], $this->headers))) - $custom = is_array($rule['arg']) ? implode(', ', $rule['arg']) : $rule['arg']; + if (isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) { + if (is_array($rule['arg1'])) + $custom = implode(', ', $rule['arg1']); + else if (!in_array($rule['arg1'], $this->headers)) + $custom = $rule['arg1']; + } + else if (isset($rule['test']) && $rule['test'] == 'exists') { + if (is_array($rule['arg'])) + $custom = implode(', ', $rule['arg']); + else if (!in_array($rule['arg'], $this->headers)) + $custom = $rule['arg']; + } - $out .= '
+ $tout = '
error_class($id, 'test', 'header', 'custom_header_i') - .' value="' .Q($custom). '" size="20" /> 
' . "\n"; + .' value="' .Q($custom). '" size="15" /> 
' . "\n"; // matching type select (operator) $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id, 'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'), + 'class' => 'operator_selector', 'onchange' => 'rule_op_select('.$id.')')); $select_op->add(Q($this->gettext('filtercontains')), 'contains'); $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains'); @@ -893,52 +1253,66 @@ class managesieve extends rcube_plugin $select_op->add(Q($this->gettext('filterisnot')), 'notis'); $select_op->add(Q($this->gettext('filterexists')), 'exists'); $select_op->add(Q($this->gettext('filternotexists')), 'notexists'); -// $select_op->add(Q($this->gettext('filtermatches')), 'matches'); -// $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches'); - if (in_array('relational', $this->exts)) { - $select_op->add(Q($this->gettext('countisgreaterthan')), 'count-gt'); - $select_op->add(Q($this->gettext('countisgreaterthanequal')), 'count-ge'); - $select_op->add(Q($this->gettext('countislessthan')), 'count-lt'); - $select_op->add(Q($this->gettext('countislessthanequal')), 'count-le'); - $select_op->add(Q($this->gettext('countequals')), 'count-eq'); - $select_op->add(Q($this->gettext('countnotequals')), 'count-ne'); - $select_op->add(Q($this->gettext('valueisgreaterthan')), 'value-gt'); - $select_op->add(Q($this->gettext('valueisgreaterthanequal')), 'value-ge'); - $select_op->add(Q($this->gettext('valueislessthan')), 'value-lt'); - $select_op->add(Q($this->gettext('valueislessthanequal')), 'value-le'); - $select_op->add(Q($this->gettext('valueequals')), 'value-eq'); - $select_op->add(Q($this->gettext('valuenotequals')), 'value-ne'); - } + $select_op->add(Q($this->gettext('filtermatches')), 'matches'); + $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches'); + if (in_array('regex', $this->exts)) { + $select_op->add(Q($this->gettext('filterregex')), 'regex'); + $select_op->add(Q($this->gettext('filternotregex')), 'notregex'); + } + if (in_array('relational', $this->exts)) { + $select_op->add(Q($this->gettext('countisgreaterthan')), 'count-gt'); + $select_op->add(Q($this->gettext('countisgreaterthanequal')), 'count-ge'); + $select_op->add(Q($this->gettext('countislessthan')), 'count-lt'); + $select_op->add(Q($this->gettext('countislessthanequal')), 'count-le'); + $select_op->add(Q($this->gettext('countequals')), 'count-eq'); + $select_op->add(Q($this->gettext('countnotequals')), 'count-ne'); + $select_op->add(Q($this->gettext('valueisgreaterthan')), 'value-gt'); + $select_op->add(Q($this->gettext('valueisgreaterthanequal')), 'value-ge'); + $select_op->add(Q($this->gettext('valueislessthan')), 'value-lt'); + $select_op->add(Q($this->gettext('valueislessthanequal')), 'value-le'); + $select_op->add(Q($this->gettext('valueequals')), 'value-eq'); + $select_op->add(Q($this->gettext('valuenotequals')), 'value-ne'); + } // target input (TODO: lists) - if ($rule['test'] == 'header') { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']); + if (in_array($rule['test'], array('header', 'address', 'envelope'))) { + $test = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is'); $target = $rule['arg2']; } + else if ($rule['test'] == 'body') { + $test = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is'); + $target = $rule['arg']; + } else if ($rule['test'] == 'size') { - $out .= $select_op->show(); - if (preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches)) { + $test = ''; + $target = ''; + if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) { $sizetarget = $matches[1]; $sizeitem = $matches[2]; } + else { + $sizetarget = $rule['arg']; + $sizeitem = $rule['item']; + } } else { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']); - $target = ''; + $test = ($rule['not'] ? 'not' : '').$rule['test']; + $target = ''; } - $out .= 'show($test); + $tout .= 'error_class($id, 'test', 'target', 'rule_target') . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n"; $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id)); - $select_size_op->add(Q($this->gettext('filterunder')), 'under'); $select_size_op->add(Q($this->gettext('filterover')), 'over'); + $select_size_op->add(Q($this->gettext('filterunder')), 'under'); - $out .= '
'; - $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); - $out .= ''; + $tout .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); + $tout .= 'error_class($id, 'test', 'sizetarget', 'rule_size_i') .' /> '.rcube_label('B').' @@ -948,17 +1322,92 @@ class managesieve extends rcube_plugin . ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('MB').' '.rcube_label('GB'); - $out .= '
'; + $tout .= ''; + + // Advanced modifiers (address, envelope) + $select_mod = new html_select(array('name' => "_rule_mod[]", 'id' => 'rule_mod_op'.$id, + 'onchange' => 'rule_mod_select(' .$id .')')); + $select_mod->add(Q($this->gettext('none')), ''); + $select_mod->add(Q($this->gettext('address')), 'address'); + if (in_array('envelope', $this->exts)) + $select_mod->add(Q($this->gettext('envelope')), 'envelope'); + + $select_type = new html_select(array('name' => "_rule_mod_type[]", 'id' => 'rule_mod_type'.$id)); + $select_type->add(Q($this->gettext('allparts')), 'all'); + $select_type->add(Q($this->gettext('domain')), 'domain'); + $select_type->add(Q($this->gettext('localpart')), 'localpart'); + if (in_array('subaddress', $this->exts)) { + $select_type->add(Q($this->gettext('user')), 'user'); + $select_type->add(Q($this->gettext('detail')), 'detail'); + } + + $need_mod = $rule['test'] != 'size' && $rule['test'] != 'body'; + $mout = '
'; + $mout .= ' '; + $mout .= Q($this->gettext('modifier')) . ' '; + $mout .= $select_mod->show($rule['test']); + $mout .= ''; + $mout .= ' '; + $mout .= Q($this->gettext('modtype')) . ' '; + $mout .= $select_type->show($rule['part']); + $mout .= ''; + $mout .= '
'; + + // Advanced modifiers (body transformations) + $select_mod = new html_select(array('name' => "_rule_trans[]", 'id' => 'rule_trans_op'.$id, + 'onchange' => 'rule_trans_select(' .$id .')')); + $select_mod->add(Q($this->gettext('text')), 'text'); + $select_mod->add(Q($this->gettext('undecoded')), 'raw'); + $select_mod->add(Q($this->gettext('contenttype')), 'content'); + + $mout .= '
'; + $mout .= ' '; + $mout .= Q($this->gettext('modifier')) . ' '; + $mout .= $select_mod->show($rule['part']); + $mout .= 'error_class($id, 'test', 'part', 'rule_trans_type') .' />'; + $mout .= ''; + $mout .= '
'; + + // Advanced modifiers (body transformations) + $select_comp = new html_select(array('name' => "_rule_comp[]", 'id' => 'rule_comp_op'.$id)); + $select_comp->add(Q($this->gettext('default')), ''); + $select_comp->add(Q($this->gettext('octet')), 'i;octet'); + $select_comp->add(Q($this->gettext('asciicasemap')), 'i;ascii-casemap'); + if (in_array('comparator-i;ascii-numeric', $this->exts)) { + $select_comp->add(Q($this->gettext('asciinumeric')), 'i;ascii-numeric'); + } + + $mout .= '
'; + $mout .= ' '; + $mout .= Q($this->gettext('comparator')) . ' '; + $mout .= $select_comp->show($rule['comparator']); + $mout .= ''; + $mout .= '
'; + + // Build output table + $out = $div ? '
'."\n" : ''; + $out .= ''; + $out .= ''; + $out .= ''; + $out .= ''; // add/del buttons $out .= '
'; + $out .= '  '; + $out .= '' . $aout . '' . $tout . "\n"; + $out .= ''; $out .= ''; - $out .= ' '; - $out .= ''; - $out .= '
'; + $out .= ''; + $out .= ''; + $out .= '
'; $out .= $div ? "
\n" : ''; @@ -975,7 +1424,7 @@ class managesieve extends rcube_plugin $out .= ''; // add/del buttons $out .= ''; $out .= '
'; // action select - $select_action = new html_select(array('name' => "_action_type[]", 'id' => 'action_type'.$id, + $select_action = new html_select(array('name' => "_action_type[$id]", 'id' => 'action_type'.$id, 'onchange' => 'action_type_select(' .$id .')')); if (in_array('fileinto', $this->exts)) $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto'); @@ -991,6 +1440,11 @@ class managesieve extends rcube_plugin if (in_array('vacation', $this->exts)) $select_action->add(Q($this->gettext('messagereply')), 'vacation'); $select_action->add(Q($this->gettext('messagedelete')), 'discard'); + if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) { + $select_action->add(Q($this->gettext('setflags')), 'setflag'); + $select_action->add(Q($this->gettext('addflags')), 'addflag'); + $select_action->add(Q($this->gettext('removeflags')), 'removeflag'); + } $select_action->add(Q($this->gettext('rulestop')), 'stop'); $select_type = $action['type']; @@ -1004,12 +1458,12 @@ class managesieve extends rcube_plugin // actions target inputs $out .= ''; // shared targets - $out .= 'error_class($id, 'action', 'target', 'action_target') .' />'; - $out .= '\n"; @@ -1017,68 +1471,66 @@ class managesieve extends rcube_plugin // vacation $out .= '
'; $out .= ''. Q($this->gettext('vacationreason')) .'
' - .'\n"; + $out .= '
' .Q($this->gettext('vacationsubject')) . '
' + .'error_class($id, 'action', 'subject', 'action_subject') .' />'; $out .= '
' .Q($this->gettext('vacationaddresses')) . '
' - .'error_class($id, 'action', 'addresses', 'action_addr') .' />'; $out .= '
' . Q($this->gettext('vacationdays')) . '
' - .'error_class($id, 'action', 'days', 'action_days') .' />'; $out .= '
'; - // mailbox select - $out .= '' + . Q($this->gettext('flag'.$fidx)) .'
'; + } + $out .= ''; + // mailbox select if ($action['type'] == 'fileinto') - $mailbox = $action['target']; + $mailbox = $this->mod_mailbox($action['target'], 'out'); else $mailbox = ''; - foreach ($a_folders as $folder) { - $utf7folder = $this->rc->imap->mod_mailbox($folder); - $names = explode($delimiter, rcube_charset_convert($folder, 'UTF7-IMAP')); - $name = $names[sizeof($names)-1]; - - if ($replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter')) - $utf7folder = str_replace($delimiter, $replace_delimiter, $utf7folder); - - // convert to Sieve implementation encoding - $utf7folder = $this->mbox_encode($utf7folder, $mbox_encoding); - - if ($folder_class = rcmail_folder_classname($name)) - $foldername = $this->gettext($folder_class); - else - $foldername = $name; - - $out .= sprintf(''."\n", - htmlspecialchars($utf7folder), - ($mailbox == $utf7folder ? ' selected="selected"' : ''), - str_repeat(' ', 4 * (sizeof($names)-1)), - Q(abbreviate_string($foldername, 40 - (2 * sizeof($names)-1)))); - } - $out .= ''; + $this->rc->imap_connect(); + $select = rcmail_mailbox_select(array( + 'realnames' => false, + 'maxlength' => 100, + 'id' => 'action_mailbox' . $id, + 'name' => "_action_mailbox[$id]", + 'style' => 'display:'.(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none') + )); + $out .= $select->show($mailbox); $out .= '
'; - $out .= ' '; - $out .= ''; + $out .= ''; + $out .= ''; $out .= '
'; @@ -1115,11 +1567,6 @@ class managesieve extends rcube_plugin return ''; } - private function mbox_encode($text, $encoding) - { - return rcube_charset_convert($text, 'UTF7-IMAP', $encoding); - } - private function add_tip($id, $str, $error=false) { if ($error) @@ -1137,4 +1584,295 @@ class managesieve extends rcube_plugin $this->rc->output->add_script($script, 'foot'); } + /** + * Converts mailbox name from/to UTF7-IMAP from/to internal Sieve encoding + * with delimiter replacement. + * + * @param string $mailbox Mailbox name + * @param string $mode Conversion direction ('in'|'out') + * + * @return string Mailbox name + */ + private function mod_mailbox($mailbox, $mode = 'out') + { + $delimiter = $_SESSION['imap_delimiter']; + $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter'); + $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP'); + + if ($mode == 'out') { + $mailbox = rcube_charset_convert($mailbox, $mbox_encoding, 'UTF7-IMAP'); + if ($replace_delimiter && $replace_delimiter != $delimiter) + $mailbox = str_replace($replace_delimiter, $delimiter, $mailbox); + } + else { + $mailbox = rcube_charset_convert($mailbox, 'UTF7-IMAP', $mbox_encoding); + if ($replace_delimiter && $replace_delimiter != $delimiter) + $mailbox = str_replace($delimiter, $replace_delimiter, $mailbox); + } + + return $mailbox; + } + + /** + * List sieve scripts + * + * @return array Scripts list + */ + public function list_scripts() + { + if ($this->list !== null) { + return $this->list; + } + + $this->list = $this->sieve->get_scripts(); + + // Handle active script(s) and list of scripts according to Kolab's KEP:14 + if ($this->rc->config->get('managesieve_kolab_master')) { + + // Skip protected names + foreach ((array)$this->list as $idx => $name) { + $_name = strtoupper($name); + if ($_name == 'MASTER') + $master_script = $name; + else if ($_name == 'MANAGEMENT') + $management_script = $name; + else if($_name == 'USER') + $user_script = $name; + else + continue; + + unset($this->list[$idx]); + } + + // get active script(s), read USER script + if ($user_script) { + $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); + $filename_regex = '/'.preg_quote($extension, '/').'$/'; + $_SESSION['managesieve_user_script'] = $user_script; + + $this->sieve->load($user_script); + + foreach ($this->sieve->script->as_array() as $rules) { + foreach ($rules['actions'] as $action) { + if ($action['type'] == 'include' && empty($action['global'])) { + $name = preg_replace($filename_regex, '', $action['target']); + $this->active[] = $name; + } + } + } + } + // create USER script if it doesn't exist + else { + $content = "# USER Management Script\n" + ."#\n" + ."# This script includes the various active sieve scripts\n" + ."# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n" + ."#\n" + ."# For more information, see http://wiki.kolab.org/KEP:14#USER\n" + ."#\n"; + if ($this->sieve->save_script('USER', $content)) { + $_SESSION['managesieve_user_script'] = 'USER'; + if (empty($this->master_file)) + $this->sieve->activate('USER'); + } + } + } + else if (!empty($this->list)) { + // Get active script name + if ($active = $this->sieve->get_active()) { + $this->active = array($active); + } + } + + return $this->list; + } + + /** + * Removes sieve script + * + * @param string $name Script name + * + * @return bool True on success, False on failure + */ + public function remove_script($name) + { + $result = $this->sieve->remove($name); + + // Kolab's KEP:14 + if ($result && $this->rc->config->get('managesieve_kolab_master')) { + $this->deactivate_script($name); + } + + return $result; + } + + /** + * Activates sieve script + * + * @param string $name Script name + * + * @return bool True on success, False on failure + */ + public function activate_script($name) + { + // Kolab's KEP:14 + if ($this->rc->config->get('managesieve_kolab_master')) { + $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); + $user_script = $_SESSION['managesieve_user_script']; + + // if the script is not active... + if ($user_script && ($key = array_search($name, $this->active)) === false) { + // ...rewrite USER file adding appropriate include command + if ($this->sieve->load($user_script)) { + $script = $this->sieve->script->as_array(); + $list = array(); + $regexp = '/' . preg_quote($extension, '/') . '$/'; + + // Create new include entry + $rule = array( + 'actions' => array( + 0 => array( + 'target' => $name.$extension, + 'type' => 'include', + 'personal' => true, + ))); + + // get all active scripts for sorting + foreach ($script as $rid => $rules) { + foreach ($rules['actions'] as $aid => $action) { + if ($action['type'] == 'include' && empty($action['global'])) { + $target = $extension ? preg_replace($regexp, '', $action['target']) : $action['target']; + $list[] = $target; + } + } + } + $list[] = $name; + + // Sort and find current script position + asort($list, SORT_LOCALE_STRING); + $list = array_values($list); + $index = array_search($name, $list); + + // add rule at the end of the script + if ($index === false || $index == count($list)-1) { + $this->sieve->script->add_rule($rule); + } + // add rule at index position + else { + $script2 = array(); + foreach ($script as $rid => $rules) { + if ($rid == $index) { + $script2[] = $rule; + } + $script2[] = $rules; + } + $this->sieve->script->content = $script2; + } + + $result = $this->sieve->save(); + if ($result) { + $this->active[] = $name; + } + } + } + } + else { + $result = $this->sieve->activate($name); + if ($result) + $this->active = array($name); + } + + return $result; + } + + /** + * Deactivates sieve script + * + * @param string $name Script name + * + * @return bool True on success, False on failure + */ + public function deactivate_script($name) + { + // Kolab's KEP:14 + if ($this->rc->config->get('managesieve_kolab_master')) { + $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); + $user_script = $_SESSION['managesieve_user_script']; + + // if the script is active... + if ($user_script && ($key = array_search($name, $this->active)) !== false) { + // ...rewrite USER file removing appropriate include command + if ($this->sieve->load($user_script)) { + $script = $this->sieve->script->as_array(); + $name = $name.$extension; + + foreach ($script as $rid => $rules) { + foreach ($rules['actions'] as $aid => $action) { + if ($action['type'] == 'include' && empty($action['global']) + && $action['target'] == $name + ) { + break 2; + } + } + } + + // Entry found + if ($rid < count($script)) { + $this->sieve->script->delete_rule($rid); + $result = $this->sieve->save(); + if ($result) { + unset($this->active[$key]); + } + } + } + } + } + else { + $result = $this->sieve->deactivate(); + if ($result) + $this->active = array(); + } + + return $result; + } + + /** + * Saves current script (adding some variables) + */ + public function save_script($name = null) + { + // Kolab's KEP:14 + if ($this->rc->config->get('managesieve_kolab_master')) { + $this->sieve->script->set_var('EDITOR', self::PROGNAME); + $this->sieve->script->set_var('EDITOR_VERSION', self::VERSION); + } + + return $this->sieve->save($name); + } + + /** + * Returns list of rules from the current script + * + * @return array List of rules + */ + public function list_rules() + { + $result = array(); + $i = 1; + + foreach ($this->script as $idx => $filter) { + if ($filter['type'] != 'if') { + continue; + } + $fname = $filter['name'] ? $filter['name'] : "#$i"; + $result[] = array( + 'id' => $idx, + 'name' => Q($fname), + 'class' => $filter['disabled'] ? 'disabled' : '', + ); + $i++; + } + + return $result; + } }