From 613f96839cbf63d475a4f56cb1841647bb23ad0c Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Tue, 26 Aug 2014 01:24:21 -0400 Subject: Added simple API to manage vacation rule --- .../lib/Roundcube/rcube_sieve_engine.php | 189 ++++++++++++--------- 1 file changed, 113 insertions(+), 76 deletions(-) (limited to 'plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php') diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php index 59e116ffb..302c7c7a1 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php @@ -73,7 +73,7 @@ class rcube_sieve_engine */ function __construct($plugin) { - $this->rc = rcmail::get_instance(); + $this->rc = rcube::get_instance(); $this->plugin = $plugin; } @@ -91,6 +91,76 @@ class rcube_sieve_engine 'filtersetform' => array($this, 'filterset_form'), )); + // connect to managesieve server + $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password'])); + + // load current/active script + if (!$error) { + // Get list of scripts + $list = $this->list_scripts(); + + // reset current script when entering filters UI (#1489412) + if ($this->rc->action == 'plugin.managesieve') { + $this->rc->session->remove('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']; + } + } + + $error = $this->load_script($script_name); + } + + // finally set script objects + if ($error) { + switch ($error) { + case rcube_sieve::ERROR_CONNECTION: + case rcube_sieve::ERROR_LOGIN: + $this->rc->output->show_message('managesieve.filterconnerror', 'error'); + rcube::raise_error(array('code' => 403, 'type' => 'php', + 'file' => __FILE__, 'line' => __LINE__, + 'message' => "Unable to connect to managesieve on $host:$port"), true, false); + break; + + default: + $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); + break; + } + + // reload interface in case of possible error when specified script wasn't found (#1489412) + if ($script_name !== null && !empty($list) && !in_array($script_name, $list)) { + $this->rc->output->command('reload', 500); + } + + // to disable 'Add filter' button set env variable + $this->rc->output->set_env('filterconnerror', true); + $this->script = array(); + } + else { + $this->exts = $this->sieve->get_extensions(); + $this->init_script(); + $this->rc->output->set_env('currentset', $this->sieve->current); + $_SESSION['managesieve_current'] = $this->sieve->current; + } + + return $error; + } + + /** + * Connect to configured managesieve server + * + * @param string $username User login + * @param string $password User password + * + * @return int Connection status: 0 on success, >0 on failure + */ + public function connect($username, $password) + { // Get connection parameters $host = $this->rc->config->get('managesieve_host', 'localhost'); $port = $this->rc->config->get('managesieve_port'); @@ -112,8 +182,8 @@ class rcube_sieve_engine } $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array( - 'user' => $_SESSION['username'], - 'password' => $this->rc->decrypt($_SESSION['password']), + 'user' => $username, + 'password' => $password, 'host' => $host, 'port' => $port, 'usetls' => $tls, @@ -140,94 +210,61 @@ class rcube_sieve_engine $plugin['socket_options'] ); - if (!($error = $this->sieve->error())) { - // Get list of scripts - $list = $this->list_scripts(); + return $this->sieve->error(); + } - // reset current script when entering filters UI (#1489412) - if ($this->rc->action == 'plugin.managesieve') { - $this->rc->session->remove('managesieve_current'); - } + /** + * Load specified (or active) script + * + * @param string $script_name Optional script name + * + * @return int Connection status: 0 on success, >0 on failure + */ + public function load_script($script_name = null) + { + // Get list of scripts + $list = $this->list_scripts(); - 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']; - } + if ($script_name === null || $script_name === '') { + // get (first) active script + if (!empty($this->active[0])) { + $script_name = $this->active[0]; + } + 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 = $this->rc->config->get('managesieve_script_name'); - if ($script_name === null || $script_name === '') { - // get (first) active script - if (!empty($this->active[0])) { - $script_name = $this->active[0]; - } - else if ($list) { - $script_name = $list[0]; + if (empty($script_name)) { + $script_name = 'roundcube'; } - // create a new (initial) script - else { - // if script not exists build default script contents - $script_file = $this->rc->config->get('managesieve_default'); - $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)) { - $this->activate_script($script_name); - $this->list[] = $script_name; - } + if ($script_file && is_readable($script_file)) { + $content = file_get_contents($script_file); } - } - if ($script_name) { - $this->sieve->load($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; + } } - - $error = $this->sieve->error(); } - // finally set script objects - if ($error) { - switch ($error) { - case SIEVE_ERROR_CONNECTION: - case SIEVE_ERROR_LOGIN: - $this->rc->output->show_message('managesieve.filterconnerror', 'error'); - rcube::raise_error(array('code' => 403, 'type' => 'php', - 'file' => __FILE__, 'line' => __LINE__, - 'message' => "Unable to connect to managesieve on $host:$port"), true, false); - break; - - default: - $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); - break; - } - - // reload interface in case of possible error when specified script wasn't found (#1489412) - if ($script_name !== null && !empty($list) && !in_array($script_name, $list)) { - $this->rc->output->command('reload', 500); - } - - // to disable 'Add filter' button set env variable - $this->rc->output->set_env('filterconnerror', true); - $this->script = array(); - } - else { - $this->exts = $this->sieve->get_extensions(); - $this->init_script(); - $this->rc->output->set_env('currentset', $this->sieve->current); - $_SESSION['managesieve_current'] = $this->sieve->current; + if ($script_name) { + $this->sieve->load($script_name); } - return $error; + return $this->sieve->error(); } + /** + * User interface actions handler + */ function actions() { $error = $this->start(); -- cgit v1.2.3