| | |
| | | * |
| | | * Engine part of Managesieve plugin implementing UI and backend access. |
| | | * |
| | | * Copyright (C) 2008-2013, The Roundcube Dev Team |
| | | * Copyright (C) 2011-2013, Kolab Systems AG |
| | | * Copyright (C) 2008-2014, The Roundcube Dev Team |
| | | * 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 |
| | |
| | | 1 => 'notifyimportancehigh' |
| | | ); |
| | | |
| | | const VERSION = '8.0'; |
| | | const VERSION = '8.2'; |
| | | const PROGNAME = 'Roundcube (Managesieve)'; |
| | | const PORT = 4190; |
| | | |
| | |
| | | */ |
| | | function __construct($plugin) |
| | | { |
| | | $this->rc = rcmail::get_instance(); |
| | | $this->rc = rcube::get_instance(); |
| | | $this->plugin = $plugin; |
| | | } |
| | | |
| | |
| | | 'filtersetform' => array($this, 'filterset_form'), |
| | | )); |
| | | |
| | | // Get connection parameters |
| | | $host = $this->rc->config->get('managesieve_host', 'localhost'); |
| | | $port = $this->rc->config->get('managesieve_port'); |
| | | $tls = $this->rc->config->get('managesieve_usetls', false); |
| | | // connect to managesieve server |
| | | $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password'])); |
| | | |
| | | $host = rcube_utils::parse_host($host); |
| | | $host = rcube_utils::idn_to_ascii($host); |
| | | |
| | | // remove tls:// prefix, set TLS flag |
| | | if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) { |
| | | $tls = true; |
| | | } |
| | | |
| | | if (empty($port)) { |
| | | $port = getservbyname('sieve', 'tcp'); |
| | | if (empty($port)) { |
| | | $port = self::PORT; |
| | | } |
| | | } |
| | | |
| | | $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array( |
| | | 'user' => $_SESSION['username'], |
| | | 'password' => $this->rc->decrypt($_SESSION['password']), |
| | | 'host' => $host, |
| | | 'port' => $port, |
| | | 'usetls' => $tls, |
| | | 'auth_type' => $this->rc->config->get('managesieve_auth_type'), |
| | | '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( |
| | | $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())) { |
| | | // load current/active script |
| | | if (!$error) { |
| | | // Get list of scripts |
| | | $list = $this->list_scripts(); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | 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 (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_name) { |
| | | $this->sieve->load($script_name); |
| | | } |
| | | |
| | | $error = $this->sieve->error(); |
| | | $error = $this->load_script($script_name); |
| | | } |
| | | |
| | | // finally set script objects |
| | | if ($error) { |
| | | switch ($error) { |
| | | case SIEVE_ERROR_CONNECTION: |
| | | case SIEVE_ERROR_LOGIN: |
| | | 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__, |
| | |
| | | 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'); |
| | | $tls = $this->rc->config->get('managesieve_usetls', false); |
| | | |
| | | $host = rcube_utils::parse_host($host); |
| | | $host = rcube_utils::idn_to_ascii($host); |
| | | |
| | | // remove tls:// prefix, set TLS flag |
| | | if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) { |
| | | $tls = true; |
| | | } |
| | | |
| | | if (empty($port)) { |
| | | $port = getservbyname('sieve', 'tcp'); |
| | | if (empty($port)) { |
| | | $port = self::PORT; |
| | | } |
| | | } |
| | | |
| | | $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array( |
| | | 'user' => $username, |
| | | 'password' => $password, |
| | | 'host' => $host, |
| | | 'port' => $port, |
| | | 'usetls' => $tls, |
| | | 'auth_type' => $this->rc->config->get('managesieve_auth_type'), |
| | | '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'), |
| | | 'socket_options' => $this->rc->config->get('managesieve_conn_options'), |
| | | )); |
| | | |
| | | // try to connect to managesieve server and to fetch the script |
| | | $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'], |
| | | $plugin['socket_options'] |
| | | ); |
| | | |
| | | return $this->sieve->error(); |
| | | } |
| | | |
| | | /** |
| | | * Load specified (or active) script |
| | | * |
| | | * @param string $script_name Optional script name |
| | | * |
| | | * @return int Connection status: 0 on success, >0 on failure |
| | | */ |
| | | protected function load_script($script_name = null) |
| | | { |
| | | // Get list of scripts |
| | | $list = $this->list_scripts(); |
| | | |
| | | if ($script_name === null || $script_name === '') { |
| | | // get (first) active script |
| | | if (!empty($this->active)) { |
| | | $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 (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_name) { |
| | | $this->sieve->load($script_name); |
| | | } |
| | | |
| | | return $this->sieve->error(); |
| | | } |
| | | |
| | | /** |
| | | * User interface actions handler |
| | | */ |
| | | function actions() |
| | | { |
| | | $error = $this->start(); |
| | |
| | | } |
| | | } |
| | | else if ($action == 'setact' && !$error) { |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true); |
| | | $result = $this->activate_script($script_name); |
| | | $kep14 = $this->rc->config->get('managesieve_kolab_master'); |
| | | |
| | |
| | | } |
| | | } |
| | | else if ($action == 'deact' && !$error) { |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true); |
| | | $result = $this->deactivate_script($script_name); |
| | | |
| | | if ($result === true) { |
| | |
| | | } |
| | | } |
| | | else if ($action == 'setdel' && !$error) { |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true); |
| | | $result = $this->remove_script($script_name); |
| | | |
| | | if ($result === true) { |
| | |
| | | $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result)); |
| | | } |
| | | else if ($action == 'ruleadd') { |
| | | $rid = rcube_utils::get_input_value('_rid', rcube_utils::INPUT_GPC); |
| | | $rid = rcube_utils::get_input_value('_rid', rcube_utils::INPUT_POST); |
| | | $id = $this->genid(); |
| | | $content = $this->rule_div($fid, $id, false); |
| | | |
| | | $this->rc->output->command('managesieve_rulefill', $content, $id, $rid); |
| | | } |
| | | else if ($action == 'actionadd') { |
| | | $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_GPC); |
| | | $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST); |
| | | $id = $this->genid(); |
| | | $content = $this->action_div($fid, $id, false); |
| | | |
| | |
| | | |
| | | function filter_frame($attrib) |
| | | { |
| | | if (!$attrib['id']) |
| | | $attrib['id'] = 'rcmfilterframe'; |
| | | |
| | | $attrib['name'] = $attrib['id']; |
| | | |
| | | $this->rc->output->set_env('contentframe', $attrib['name']); |
| | | $this->rc->output->set_env('blankpage', $attrib['src'] ? |
| | | $this->rc->output->abs_url($attrib['src']) : 'program/resources/blank.gif'); |
| | | |
| | | return $this->rc->output->frame($attrib); |
| | | return $this->rc->output->frame($attrib, true); |
| | | } |
| | | |
| | | function filterset_form($attrib) |
| | |
| | | $out .= $hiddenfields->show(); |
| | | |
| | | // 'any' flag |
| | | if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) |
| | | if ((!isset($this->form) && empty($scr['tests']) && !empty($scr)) |
| | | || (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) |
| | | ) { |
| | | $any = true; |
| | | } |
| | | |
| | | // filter name input |
| | | $field_id = '_name'; |
| | |
| | | $out .= sprintf("%s<label for=\"%s\">%s</label>\n", |
| | | $input_join, $field_id, rcube::Q($this->plugin->gettext('filterany'))); |
| | | |
| | | $rows_num = isset($scr) ? sizeof($scr['tests']) : 1; |
| | | $rows_num = !empty($scr['tests']) ? sizeof($scr['tests']) : 1; |
| | | |
| | | $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>'; |
| | | for ($x=0; $x<$rows_num; $x++) |
| | |
| | | $select_op->add(rcube::Q($this->plugin->gettext('valuenotequals')), 'value-ne'); |
| | | } |
| | | |
| | | $test = self::rule_test($rule); |
| | | $target = ''; |
| | | |
| | | // target(s) input |
| | | if (in_array($rule['test'], array('header', 'address', 'envelope'))) { |
| | | $test = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is'); |
| | | $target = $rule['arg2']; |
| | | } |
| | | else if (in_array($rule['test'], array('body', 'date', 'currentdate'))) { |
| | | $test = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is'); |
| | | $target = $rule['arg']; |
| | | } |
| | | else if ($rule['test'] == 'size') { |
| | | $test = ''; |
| | | $target = ''; |
| | | if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) { |
| | | $sizetarget = $matches[1]; |
| | | $sizeitem = $matches[2]; |
| | | $sizeitem = $matches[2]; |
| | | } |
| | | else { |
| | | $sizetarget = $rule['arg']; |
| | | $sizeitem = $rule['item']; |
| | | $sizeitem = $rule['item']; |
| | | } |
| | | } |
| | | else { |
| | | $test = ($rule['not'] ? 'not' : '').$rule['test']; |
| | | $target = ''; |
| | | } |
| | | |
| | | // (current)date part select |
| | |
| | | $out .= $div ? "</div>\n" : ''; |
| | | |
| | | return $out; |
| | | } |
| | | |
| | | private static function rule_test(&$rule) |
| | | { |
| | | // first modify value/count tests with 'not' keyword |
| | | // we'll revert the meaning of operators |
| | | if ($rule['not'] && preg_match('/^(count|value)-([gteqnl]{2})/', $rule['type'], $m)) { |
| | | $rule['not'] = false; |
| | | |
| | | switch ($m[2]) { |
| | | case 'gt': $rule['type'] = $m[1] . '-le'; break; |
| | | case 'ge': $rule['type'] = $m[1] . '-lt'; break; |
| | | case 'lt': $rule['type'] = $m[1] . '-ge'; break; |
| | | case 'le': $rule['type'] = $m[1] . '-gt'; break; |
| | | case 'eq': $rule['type'] = $m[1] . '-ne'; break; |
| | | case 'ne': $rule['type'] = $m[1] . '-eq'; break; |
| | | } |
| | | } |
| | | else if ($rule['not'] && $rule['test'] == 'size') { |
| | | $rule['not'] = false; |
| | | $rule['type'] = $rule['type'] == 'over' ? 'under' : 'over'; |
| | | } |
| | | |
| | | $set = array('header', 'address', 'envelope', 'body', 'date', 'currentdate'); |
| | | |
| | | // build test string supported by select element |
| | | if ($rule['size']) { |
| | | $test = $rule['type']; |
| | | } |
| | | else if (in_array($rule['test'], $set)) { |
| | | $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ? $rule['type'] : 'is'); |
| | | } |
| | | else { |
| | | $test = ($rule['not'] ? 'not' : '') . $rule['test']; |
| | | } |
| | | |
| | | return $test; |
| | | } |
| | | |
| | | function action_div($fid, $id, $div=true) |
| | |
| | | $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>'; |
| | |
| | | |
| | | // 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 (!empty($exceptions)) { |
| | | $this->list = array_diff($this->list, (array)$exceptions); |
| | | } |
| | | } |
| | | |
| | | // reindex |
| | | if (!empty($this->list)) { |
| | | $this->list = array_values($this->list); |
| | | } |
| | | |
| | | return $this->list; |
| | |
| | | $i = 1; |
| | | |
| | | foreach ($this->script as $idx => $filter) { |
| | | if ($filter['type'] != 'if') { |
| | | if (empty($filter['actions'])) { |
| | | continue; |
| | | } |
| | | $fname = $filter['name'] ? $filter['name'] : "#$i"; |