From ac67db19802abd7d2a18c1bb804c3bfd887e9bdf Mon Sep 17 00:00:00 2001 From: alecpl Date: Mon, 2 Nov 2009 07:41:21 +0000 Subject: - Managesieve 2.0: multi-script support --- plugins/managesieve/managesieve.php | 233 ++++++++++++++++++++++++++++++------ 1 file changed, 194 insertions(+), 39 deletions(-) (limited to 'plugins/managesieve/managesieve.php') diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php index 0c8c492e4..92185e083 100644 --- a/plugins/managesieve/managesieve.php +++ b/plugins/managesieve/managesieve.php @@ -7,7 +7,7 @@ * It's clickable interface which operates on text scripts and communicates * with server using managesieve protocol. Adds Filters tab in Settings. * - * @version 1.7 + * @version 2.0 * @author Aleksander 'A.L.E.C' Machniak * * Configuration (see config.inc.php.dist): @@ -35,7 +35,7 @@ class managesieve extends rcube_plugin $this->add_texts('localization/', array('filters','managefilters')); // register actions - $this->register_action('plugin.managesieve', array($this, 'managesieve_init')); + $this->register_action('plugin.managesieve', array($this, 'managesieve_actions')); $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save')); // include main js script @@ -52,8 +52,10 @@ class managesieve extends rcube_plugin // register UI objects $this->rc->output->add_handlers(array( 'filterslist' => array($this, 'filters_list'), + 'filtersetslist' => array($this, 'filtersets_list'), 'filterframe' => array($this, 'filter_frame'), 'filterform' => array($this, 'filter_form'), + 'filtersetform' => array($this, 'filterset_form'), )); require_once($this->home . '/lib/Net/Sieve.php'); @@ -65,23 +67,51 @@ class managesieve extends rcube_plugin str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')), $this->rc->config->get('managesieve_port', 2000), $this->rc->config->get('managesieve_usetls', false), - $this->rc->config->get('managesieve_disabled_extensions')); + $this->rc->config->get('managesieve_disabled_extensions'), + $this->rc->config->get('managesieve_debug', false) + ); - $error = $this->sieve->error(); + if (!($error = $this->sieve->error())) { + + $list = $this->sieve->get_scripts(); + $active = $this->sieve->get_active(); + $_SESSION['managesieve_active'] = $active; + + if (!empty($_GET['_sid'])) { + $script_name = get_input_value('_sid', RCUBE_INPUT_GET); + } else if (!empty($_SESSION['managesieve_current'])) { + $script_name = $_SESSION['managesieve_current']; + } else { + // get active script + if ($active) { + $script_name = $active; + } else if ($list) { + $script_name = $list[0]; + // create a new (initial) script + } else { + // if script not exists build default script contents + $script_file = $this->rc->config->get('managesieve_default'); + $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; + } + } - if ($error == SIEVE_ERROR_NOT_EXISTS) - { - // if script not exists build default script contents - $script_file = $this->rc->config->get('managesieve_default'); - if ($script_file && is_readable($script_file)) - $this->sieve->script->add_text(file_get_contents($script_file)); - // that's not exactly an error - $error = false; + if ($script_name) + $this->sieve->load($script_name); + + $error = $this->sieve->error(); } - elseif ($error) + + // finally set script objects + if ($error) { - switch ($error) - { + switch ($error) { case SIEVE_ERROR_CONNECTION: case SIEVE_ERROR_LOGIN: $this->rc->output->show_message('managesieve.filterconnerror', 'error'); @@ -90,26 +120,22 @@ class managesieve extends rcube_plugin $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); break; } - // to disable 'Add filter' button set env variable $this->rc->output->set_env('filterconnerror', true); - } - - // finally set script objects - if ($error) - { $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']); + $_SESSION['managesieve_current'] = $this->sieve->current; } return $error; } - function managesieve_init() + function managesieve_actions() { // Init plugin and handle managesieve connection $error = $this->managesieve_start(); @@ -134,7 +160,7 @@ class managesieve extends rcube_plugin $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); } } - elseif ($action=='down' && !$error) + else if ($action=='down' && !$error) { if (isset($this->script[$fid]) && isset($this->script[$fid+1])) { @@ -142,27 +168,56 @@ class managesieve extends rcube_plugin && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false) $result = $this->sieve->save(); - if ($result) { + if ($result === true) { // $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); $this->rc->output->command('managesieve_updatelist', 'down', '', $fid); - } else + } else { $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); - } + } + } } - elseif ($action=='delete' && !$error) + else if ($action=='delete' && !$error) { if (isset($this->script[$fid])) { if ($this->sieve->script->delete_rule($fid)) $result = $this->sieve->save(); - if (!$result) - $this->rc->output->show_message('managesieve.filterdeleteerror', 'error'); - else { + if ($result === true) { $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation'); $this->rc->output->command('managesieve_updatelist', 'delete', '', $fid); + } else { + $this->rc->output->show_message('managesieve.filterdeleteerror', 'error'); } - } + } + } + else if ($action=='setact' && !$error) + { + $script_name = get_input_value('_set', RCUBE_INPUT_GPC); + $result = $this->sieve->activate($script_name); + + if ($result === true) { + $this->rc->output->set_env('active_set', $script_name); + $this->rc->output->show_message('managesieve.setactivated', 'confirmation'); + $this->rc->output->command('enable_command', 'plugin.managesieve-setact', false); + $this->rc->output->command('managesieve_reset', $script_name); + $_SESSION['managesieve_active'] = $script_name; + } else { + $this->rc->output->show_message('managesieve.setactivateerror', 'error'); + } + } + else if ($action=='setdel' && !$error) + { + $script_name = get_input_value('_set', RCUBE_INPUT_GPC); + $result = $this->sieve->remove($script_name); + + if ($result === true) { + $this->rc->output->show_message('managesieve.setdeleted', 'confirmation'); + $this->rc->output->command('managesieve_reload'); + rcube_sess_unset('managesieve_current'); + } else { + $this->rc->output->show_message('managesieve.setdeleteerror', 'error'); + } } elseif ($action=='ruleadd') { @@ -192,8 +247,29 @@ class managesieve extends rcube_plugin // Init plugin and handle managesieve connection $error = $this->managesieve_start(); - // add/edit action - if (isset($_POST['_name'])) + // filters set add action + if (!empty($_POST['_newset'])) + { + $name = get_input_value('_name', RCUBE_INPUT_GPC); + $copy = get_input_value('_copy', RCUBE_INPUT_GPC); + + if (!$name) + $error = 'managesieve.emptyname'; + else if (mb_strlen($name)>128) + $error = 'managesieve.nametoolong'; + else if (!$this->sieve->copy($name, $copy)) + $error = 'managesieve.setcreateerror'; + + if (!$error) { + $this->rc->output->show_message('managesieve.setcreated', 'confirmation'); + $this->rc->output->command('parent.managesieve_reload', $name); +// rcube_sess_unset('managesieve_current'); + } else { + $this->rc->output->show_message($error, 'error'); + } + } + // filter add/edit action + else if (isset($_POST['_name'])) { $name = trim(get_input_value('_name', RCUBE_INPUT_POST)); $fid = trim(get_input_value('_fid', RCUBE_INPUT_POST)); @@ -422,9 +498,12 @@ class managesieve extends rcube_plugin private function managesieve_send() { // Handle form action - if (isset($_GET['_framed']) || isset($_POST['_framed'])) - $this->rc->output->send('managesieve.managesieveedit'); - else { + if (isset($_GET['_framed']) || isset($_POST['_framed'])) { + if (isset($_GET['_newset']) || isset($_POST['_newset'])) + $this->rc->output->send('managesieve.setedit'); + else + $this->rc->output->send('managesieve.filteredit'); + } else { $this->rc->output->set_pagetitle($this->gettext('filters')); $this->rc->output->send('managesieve.managesieve'); } @@ -451,7 +530,34 @@ class managesieve extends rcube_plugin $this->rc->output->include_script('list.js'); // add some labels to client - $this->rc->output->add_label('managesieve.filterconfirmdelete'); + $this->rc->output->add_label('managesieve.filterdeleteconfirm'); + + return $out; + } + + // return the filters list as