| | |
| | | * |
| | | * 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.3'; |
| | | const PROGNAME = 'Roundcube (Managesieve)'; |
| | | const PORT = 4190; |
| | | |
| | |
| | | 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: |
| | |
| | | $plugin['socket_options'] |
| | | ); |
| | | |
| | | return $this->sieve->error(); |
| | | $error = $this->sieve->error(); |
| | | |
| | | if ($error) { |
| | | rcube::raise_error(array( |
| | | 'code' => 403, |
| | | 'file' => __FILE__, |
| | | 'line' => __LINE__, |
| | | 'message' => "Unable to connect to managesieve on $host:$port" |
| | | ), true, false); |
| | | } |
| | | |
| | | return $error; |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return int Connection status: 0 on success, >0 on failure |
| | | */ |
| | | public function load_script($script_name = null) |
| | | 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[0])) { |
| | | if (!empty($this->active)) { |
| | | $script_name = $this->active[0]; |
| | | } |
| | | else if ($list) { |
| | |
| | | } |
| | | } |
| | | 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) { |
| | |
| | | } |
| | | else if ($action == 'setget') { |
| | | $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); |
| | | $script = $this->sieve->get_script($script_name); |
| | | $script = $this->sieve->get_script($script_name); |
| | | |
| | | if (PEAR::isError($script)) |
| | | if (is_a($script, 'PEAR_Error')) { |
| | | exit; |
| | | } |
| | | |
| | | $browser = new rcube_browser; |
| | | |
| | |
| | | $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++) |
| | |
| | | } |
| | | |
| | | if (isset($rule['test'])) { |
| | | if (in_array($rule['test'], array('header', 'address', 'envelope')) |
| | | && !is_array($rule['arg1']) |
| | | && ($header = strtolower($rule['arg1'])) |
| | | && isset($this->headers[$header]) |
| | | ) { |
| | | $test = $header; |
| | | if (in_array($rule['test'], array('header', 'address', 'envelope'))) { |
| | | if (is_array($rule['arg1']) && count($rule['arg1']) == 1) { |
| | | $rule['arg1'] = $rule['arg1'][0]; |
| | | } |
| | | |
| | | $matches = ($header = strtolower($rule['arg1'])) && isset($this->headers[$header]); |
| | | $test = $matches ? $header : '...'; |
| | | } |
| | | else if ($rule['test'] == 'exists' |
| | | && !is_array($rule['arg']) |
| | | && ($header = strtolower($rule['arg'])) |
| | | && isset($this->headers[$header]) |
| | | ) { |
| | | $test = $header; |
| | | else if ($rule['test'] == 'exists') { |
| | | if (is_array($rule['arg']) && count($rule['arg']) == 1) { |
| | | $rule['arg'] = $rule['arg'][0]; |
| | | } |
| | | |
| | | $matches = ($header = strtolower($rule['arg'])) && isset($this->headers[$header]); |
| | | $test = $matches ? $header : '...'; |
| | | } |
| | | else if (in_array($rule['test'], array('size', 'body', 'date', 'currentdate'))) { |
| | | $test = $rule['test']; |
| | |
| | | $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) |
| | |
| | | |
| | | // 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); |
| | |
| | | foreach ($rules['actions'] as $action) { |
| | | if ($action['type'] == 'include' && empty($action['global'])) { |
| | | $name = preg_replace($filename_regex, '', $action['target']); |
| | | $this->active[] = $name; |
| | | // make sure the script exist |
| | | if (in_array($name, $this->list)) { |
| | | $this->active[] = $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"; |
| | |
| | | */ |
| | | protected function init_script() |
| | | { |
| | | $this->script = $this->sieve->script->as_array(); |
| | | |
| | | if (!$this->script) { |
| | | if (!$this->sieve->script) { |
| | | return; |
| | | } |
| | | |
| | | $this->script = $this->sieve->script->as_array(); |
| | | |
| | | $headers = array(); |
| | | $exceptions = array('date', 'currentdate', 'size', 'body'); |
| | | |