diff options
Diffstat (limited to 'plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php')
-rw-r--r-- | plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php | 212 |
1 files changed, 128 insertions, 84 deletions
diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php index 9900f16b5..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, @@ -122,6 +192,7 @@ class rcube_sieve_engine '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'), + 'socket_options' => $this->rc->config->get('managesieve_conn_options'), )); // try to connect to managesieve server and to fetch the script @@ -135,97 +206,65 @@ class rcube_sieve_engine $plugin['disabled'], $plugin['debug'], $plugin['auth_cid'], - $plugin['auth_pw'] + $plugin['auth_pw'], + $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]; + if (empty($script_name)) { + $script_name = 'roundcube'; } - 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 (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(); @@ -1826,17 +1865,22 @@ class rcube_sieve_engine $out .= '</div>'; // mailbox select - if ($action['type'] == 'fileinto') + if ($action['type'] == 'fileinto') { $mailbox = $this->mod_mailbox($action['target'], 'out'); - else + // make sure non-existing (or unsubscribed) mailbox is listed (#1489956) + $additional = array($mailbox); + } + else { $mailbox = ''; + } $select = $this->rc->folder_selector(array( - 'realnames' => false, - 'maxlength' => 100, - 'id' => 'action_mailbox' . $id, - 'name' => "_action_mailbox[$id]", - 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none') + 'realnames' => false, + 'maxlength' => 100, + 'id' => 'action_mailbox' . $id, + 'name' => "_action_mailbox[$id]", + 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none'), + 'additional' => $additional, )); $out .= $select->show($mailbox); $out .= '</td>'; |