From 6601c9e127d61ec04a62c452a9f611e9d15ba171 Mon Sep 17 00:00:00 2001 From: alec Date: Tue, 4 May 2010 13:27:13 +0000 Subject: - Fix download button state when sets list is empty - Fix errors when sets list is empty - Code re-formatting git-svn-id: https://svn.roundcube.net/trunk@3593 208e9e7b-5314-0410-a742-e7e81cd9613c --- plugins/managesieve/Changelog | 4 + plugins/managesieve/config.inc.php.dist | 1 + plugins/managesieve/lib/rcube_sieve.php | 945 ++++++++------- plugins/managesieve/managesieve.js | 276 ++--- plugins/managesieve/managesieve.php | 2025 +++++++++++++++---------------- 5 files changed, 1602 insertions(+), 1649 deletions(-) (limited to 'plugins') diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog index 323c7f2cf..3ba381f1b 100644 --- a/plugins/managesieve/Changelog +++ b/plugins/managesieve/Changelog @@ -1,5 +1,9 @@ +* version 2.5 [2010-05-04] +----------------------------------------------------------- - Fix filters set label after activation - Fix filters set activation, add possibility to deactivate sets (#1486699) +- Fix download button state when sets list is empty +- Fix errors when sets list is empty * version 2.4 [2010-04-01] ----------------------------------------------------------- diff --git a/plugins/managesieve/config.inc.php.dist b/plugins/managesieve/config.inc.php.dist index bac68c77c..158c4dedd 100644 --- a/plugins/managesieve/config.inc.php.dist +++ b/plugins/managesieve/config.inc.php.dist @@ -35,3 +35,4 @@ $rcmail_config['managesieve_disabled_extensions'] = array(); $rcmail_config['managesieve_debug'] = false; ?> + diff --git a/plugins/managesieve/lib/rcube_sieve.php b/plugins/managesieve/lib/rcube_sieve.php index 01d259ae3..2ecf6f1e7 100644 --- a/plugins/managesieve/lib/rcube_sieve.php +++ b/plugins/managesieve/lib/rcube_sieve.php @@ -13,48 +13,49 @@ define('SIEVE_ERROR_CONNECTION', 1); define('SIEVE_ERROR_LOGIN', 2); -define('SIEVE_ERROR_NOT_EXISTS', 3); // script not exists -define('SIEVE_ERROR_INSTALL', 4); // script installation -define('SIEVE_ERROR_ACTIVATE', 5); // script activation -define('SIEVE_ERROR_DELETE', 6); // script deletion -define('SIEVE_ERROR_INTERNAL', 7); // internal error -define('SIEVE_ERROR_DEACTIVATE', 8); // script activation -define('SIEVE_ERROR_OTHER', 255); // other/unknown error +define('SIEVE_ERROR_NOT_EXISTS', 3); // script not exists +define('SIEVE_ERROR_INSTALL', 4); // script installation +define('SIEVE_ERROR_ACTIVATE', 5); // script activation +define('SIEVE_ERROR_DELETE', 6); // script deletion +define('SIEVE_ERROR_INTERNAL', 7); // internal error +define('SIEVE_ERROR_DEACTIVATE', 8); // script activation +define('SIEVE_ERROR_OTHER', 255); // other/unknown error class rcube_sieve { - private $sieve; // Net_Sieve object - private $error = false; // error flag - private $list = array(); // scripts list + private $sieve; // Net_Sieve object + private $error = false; // error flag + private $list = array(); // scripts list + + public $script; // rcube_sieve_script object + public $current; // name of currently loaded script + private $disabled; // array of disabled extensions - public $script; // rcube_sieve_script object - public $current; // name of currently loaded script - private $disabled; // array of disabled extensions /** - * Object constructor - * - * @param string Username (to managesieve login) - * @param string Password (to managesieve login) - * @param string Managesieve server hostname/address - * @param string Managesieve server port number - * @param string Enable/disable TLS use - * @param array Disabled extensions - */ + * Object constructor + * + * @param string Username (to managesieve login) + * @param string Password (to managesieve login) + * @param string Managesieve server hostname/address + * @param string Managesieve server port number + * @param string Enable/disable TLS use + * @param array Disabled extensions + */ public function __construct($username, $password='', $host='localhost', $port=2000, - $usetls=true, $disabled=array(), $debug=false) + $usetls=true, $disabled=array(), $debug=false) { - $this->sieve = new Net_Sieve(); - + $this->sieve = new Net_Sieve(); + if ($debug) - $this->sieve->setDebug(true, array($this, 'debug_handler')); - + $this->sieve->setDebug(true, array($this, 'debug_handler')); + if (PEAR::isError($this->sieve->connect($host, $port, NULL, $usetls))) - return $this->_set_error(SIEVE_ERROR_CONNECTION); + return $this->_set_error(SIEVE_ERROR_CONNECTION); if (PEAR::isError($this->sieve->login($username, $password))) - return $this->_set_error(SIEVE_ERROR_LOGIN); + return $this->_set_error(SIEVE_ERROR_LOGIN); $this->disabled = $disabled; } @@ -64,212 +65,212 @@ class rcube_sieve } /** - * Getter for error code - */ + * Getter for error code + */ public function error() { - return $this->error ? $this->error : false; + return $this->error ? $this->error : false; } - + /** - * Saves current script into server - */ + * Saves current script into server + */ public function save($name = null) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); - - if (!$this->script) - return $this->_set_error(SIEVE_ERROR_INTERNAL); - - if (!$name) - $name = $this->current; + return $this->_set_error(SIEVE_ERROR_INTERNAL); + + if (!$this->script) + return $this->_set_error(SIEVE_ERROR_INTERNAL); + + if (!$name) + $name = $this->current; $script = $this->script->as_text(); if (!$script) - $script = '/* empty script */'; + $script = '/* empty script */'; if (PEAR::isError($this->sieve->installScript($name, $script))) - return $this->_set_error(SIEVE_ERROR_INSTALL); + return $this->_set_error(SIEVE_ERROR_INSTALL); return true; } /** - * Saves text script into server - */ + * Saves text script into server + */ public function save_script($name, $content = null) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); - + return $this->_set_error(SIEVE_ERROR_INTERNAL); + if (!$content) - $content = '/* empty script */'; + $content = '/* empty script */'; if (PEAR::isError($this->sieve->installScript($name, $content))) - return $this->_set_error(SIEVE_ERROR_INSTALL); + return $this->_set_error(SIEVE_ERROR_INSTALL); return true; } /** - * Activates specified script - */ + * Activates specified script + */ public function activate($name = null) { - if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + if (!$this->sieve) + return $this->_set_error(SIEVE_ERROR_INTERNAL); - if (!$name) - $name = $this->current; + if (!$name) + $name = $this->current; if (PEAR::isError($this->sieve->setActive($name))) - return $this->_set_error(SIEVE_ERROR_ACTIVATE); + return $this->_set_error(SIEVE_ERROR_ACTIVATE); return true; } /** - * De-activates specified script - */ + * De-activates specified script + */ public function deactivate() { - if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + if (!$this->sieve) + return $this->_set_error(SIEVE_ERROR_INTERNAL); if (PEAR::isError($this->sieve->setActive(''))) - return $this->_set_error(SIEVE_ERROR_DEACTIVATE); + return $this->_set_error(SIEVE_ERROR_DEACTIVATE); return true; } /** - * Removes specified script - */ + * Removes specified script + */ public function remove($name = null) { - if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + if (!$this->sieve) + return $this->_set_error(SIEVE_ERROR_INTERNAL); - if (!$name) - $name = $this->current; + if (!$name) + $name = $this->current; - // script must be deactivated first - if ($name == $this->sieve->getActive()) + // script must be deactivated first + if ($name == $this->sieve->getActive()) if (PEAR::isError($this->sieve->setActive(''))) - return $this->_set_error(SIEVE_ERROR_DELETE); + return $this->_set_error(SIEVE_ERROR_DELETE); if (PEAR::isError($this->sieve->removeScript($name))) return $this->_set_error(SIEVE_ERROR_DELETE); - if ($name == $this->current) - $this->current = null; + if ($name == $this->current) + $this->current = null; return true; } /** - * Gets list of supported by server Sieve extensions - */ + * Gets list of supported by server Sieve extensions + */ public function get_extensions() { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); - - $ext = $this->sieve->getExtensions(); - // we're working on lower-cased names - $ext = array_map('strtolower', (array) $ext); + return $this->_set_error(SIEVE_ERROR_INTERNAL); - if ($this->script) { - $supported = $this->script->get_extensions(); - foreach ($ext as $idx => $ext_name) - if (!in_array($ext_name, $supported)) - unset($ext[$idx]); - } + $ext = $this->sieve->getExtensions(); + // we're working on lower-cased names + $ext = array_map('strtolower', (array) $ext); - return array_values($ext); + if ($this->script) { + $supported = $this->script->get_extensions(); + foreach ($ext as $idx => $ext_name) + if (!in_array($ext_name, $supported)) + unset($ext[$idx]); + } + + return array_values($ext); } /** - * Gets list of scripts from server - */ + * Gets list of scripts from server + */ public function get_scripts() { if (!$this->list) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); - - $this->list = $this->sieve->listScripts(); + return $this->_set_error(SIEVE_ERROR_INTERNAL); + + $this->list = $this->sieve->listScripts(); - if (PEAR::isError($this->list)) - return $this->_set_error(SIEVE_ERROR_OTHER); - } + if (PEAR::isError($this->list)) + return $this->_set_error(SIEVE_ERROR_OTHER); + } return $this->list; } /** - * Returns active script name - */ + * Returns active script name + */ public function get_active() { - if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + if (!$this->sieve) + return $this->_set_error(SIEVE_ERROR_INTERNAL); - return $this->sieve->getActive(); + return $this->sieve->getActive(); } - + /** - * Loads script by name - */ + * Loads script by name + */ public function load($name) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + return $this->_set_error(SIEVE_ERROR_INTERNAL); if ($this->current == $name) - return true; - - $script = $this->sieve->getScript($name); - + return true; + + $script = $this->sieve->getScript($name); + if (PEAR::isError($script)) - return $this->_set_error(SIEVE_ERROR_OTHER); + return $this->_set_error(SIEVE_ERROR_OTHER); - // try to parse from Roundcube format + // try to parse from Roundcube format $this->script = $this->_parse($script); - $this->current = $name; + $this->current = $name; - return true; + return true; } /** - * Loads script from text content - */ + * Loads script from text content + */ public function load_script($script) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + return $this->_set_error(SIEVE_ERROR_INTERNAL); - // try to parse from Roundcube format + // try to parse from Roundcube format $this->script = $this->_parse($script); } /** - * Creates rcube_sieve_script object from text script - */ + * Creates rcube_sieve_script object from text script + */ private function _parse($txt) { - // try to parse from Roundcube format + // try to parse from Roundcube format $script = new rcube_sieve_script($txt, $this->disabled); // ... else try to import from different formats if (empty($script->content)) { - $script = $this->_import_rules($txt); - $script = new rcube_sieve_script($script, $this->disabled); - } + $script = $this->_import_rules($txt); + $script = new rcube_sieve_script($script, $this->disabled); + } // replace all elsif with if+stop, we support only ifs foreach ($script->content as $idx => $rule) { @@ -284,41 +285,41 @@ class rcube_sieve } } - return $script; + return $script; } /** - * Gets specified script as text - */ + * Gets specified script as text + */ public function get_script($name) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + return $this->_set_error(SIEVE_ERROR_INTERNAL); - $content = $this->sieve->getScript($name); + $content = $this->sieve->getScript($name); + + if (PEAR::isError($content)) + return $this->_set_error(SIEVE_ERROR_OTHER); - if (PEAR::isError($content)) - return $this->_set_error(SIEVE_ERROR_OTHER); - return $content; } /** - * Creates empty script or copy of other script - */ + * Creates empty script or copy of other script + */ public function copy($name, $copy) { if (!$this->sieve) - return $this->_set_error(SIEVE_ERROR_INTERNAL); + return $this->_set_error(SIEVE_ERROR_INTERNAL); + + if ($copy) { + $content = $this->sieve->getScript($copy); + + if (PEAR::isError($content)) + return $this->_set_error(SIEVE_ERROR_OTHER); + } - if ($copy) { - $content = $this->sieve->getScript($copy); - - if (PEAR::isError($content)) - return $this->_set_error(SIEVE_ERROR_OTHER); - } - - return $this->save_script($name, $content); + return $this->save_script($name, $content); } private function _import_rules($script) @@ -328,14 +329,14 @@ class rcube_sieve // Squirrelmail (Avelsieve) if ($tokens = preg_split('/(#START_SIEVE_RULE.*END_SIEVE_RULE)\n/', $script, -1, PREG_SPLIT_DELIM_CAPTURE)) { - foreach($tokens as $token) { - if (preg_match('/^#START_SIEVE_RULE.*/', $token, $matches)) { - $name[$i] = "unnamed rule ".($i+1); + foreach($tokens as $token) { + if (preg_match('/^#START_SIEVE_RULE.*/', $token, $matches)) { + $name[$i] = "unnamed rule ".($i+1); $content .= "# rule:[".$name[$i]."]\n"; } - elseif (isset($name[$i])) { - $content .= "if $token\n"; - $i++; + elseif (isset($name[$i])) { + $content .= "if $token\n"; + $i++; } } } @@ -359,53 +360,53 @@ class rcube_sieve private function _set_error($error) { - $this->error = $error; + $this->error = $error; return false; } /** - * This is our own debug handler for connection - * @access public - */ + * This is our own debug handler for connection + */ public function debug_handler(&$sieve, $message) { write_log('sieve', preg_replace('/\r\n$/', '', $message)); - } + } } + class rcube_sieve_script { - public $content = array(); // script rules array + public $content = array(); // script rules array - private $supported = array( // extensions supported by class + private $supported = array( // extensions supported by class 'fileinto', 'reject', 'ereject', - 'vacation', // RFC5230 + 'vacation', // RFC5230 // TODO: (most wanted first) body, imapflags, notify, regex ); - + /** - * Object constructor - * - * @param string Script's text content - * @param array Disabled extensions - */ + * Object constructor + * + * @param string Script's text content + * @param array Disabled extensions + */ public function __construct($script, $disabled=NULL) { if (!empty($disabled)) foreach ($disabled as $ext) if (($idx = array_search($ext, $this->supported)) !== false) - unset($this->supported[$idx]); + unset($this->supported[$idx]); $this->content = $this->_parse_text($script); } /** - * Adds script contents as text to the script array (at the end) - * - * @param string Text script contents - */ + * Adds script contents as text to the script array (at the end) + * + * @param string Text script contents + */ public function add_text($script) { $content = $this->_parse_text($script); @@ -419,19 +420,19 @@ class rcube_sieve_script foreach ($content as $elem) { if (!isset($names[$elem['name']])) { array_push($this->content, $elem); - $result = true; - } + $result = true; + } } return $result; } /** - * Adds rule to the script (at the end) - * - * @param string Rule name - * @param array Rule content (as array) - */ + * Adds rule to the script (at the end) + * + * @param string Rule name + * @param array Rule content (as array) + */ public function add_rule($content) { // TODO: check this->supported @@ -443,8 +444,8 @@ class rcube_sieve_script { if(isset($this->content[$index])) { unset($this->content[$index]); - return true; - } + return true; + } return false; } @@ -457,15 +458,15 @@ class rcube_sieve_script { // TODO: check this->supported if ($this->content[$index]) { - $this->content[$index] = $content; - return $index; - } + $this->content[$index] = $content; + return $index; + } return false; } /** - * Returns script as text - */ + * Returns script as text + */ public function as_text() { $script = ''; @@ -474,112 +475,112 @@ class rcube_sieve_script // rules foreach ($this->content as $rule) { - $extension = ''; - $tests = array(); - $i = 0; - - // header - $script .= '# rule:[' . $rule['name'] . "]\n"; - - // constraints expressions - foreach ($rule['tests'] as $test) { - $tests[$i] = ''; - switch ($test['test']) { - case 'size': - $tests[$i] .= ($test['not'] ? 'not ' : ''); - $tests[$i] .= 'size :' . ($test['type']=='under' ? 'under ' : 'over ') . $test['arg']; - break; - case 'true': - $tests[$i] .= ($test['not'] ? 'not true' : 'true'); - break; - case 'exists': - $tests[$i] .= ($test['not'] ? 'not ' : ''); - if (is_array($test['arg'])) - $tests[$i] .= 'exists ["' . implode('", "', $this->_escape_string($test['arg'])) . '"]'; - else - $tests[$i] .= 'exists "' . $this->_escape_string($test['arg']) . '"'; - break; - case 'header': - $tests[$i] .= ($test['not'] ? 'not ' : ''); - $tests[$i] .= 'header :' . $test['type']; - if (is_array($test['arg1'])) - $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg1'])) . '"]'; - else - $tests[$i] .= ' "' . $this->_escape_string($test['arg1']) . '"'; - if (is_array($test['arg2'])) - $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg2'])) . '"]'; - else - $tests[$i] .= ' "' . $this->_escape_string($test['arg2']) . '"'; - break; - } - $i++; - } - -// $script .= ($idx>0 ? 'els' : '').($rule['join'] ? 'if allof (' : 'if anyof ('); + $extension = ''; + $tests = array(); + $i = 0; + + // header + $script .= '# rule:[' . $rule['name'] . "]\n"; + + // constraints expressions + foreach ($rule['tests'] as $test) { + $tests[$i] = ''; + switch ($test['test']) { + case 'size': + $tests[$i] .= ($test['not'] ? 'not ' : ''); + $tests[$i] .= 'size :' . ($test['type']=='under' ? 'under ' : 'over ') . $test['arg']; + break; + case 'true': + $tests[$i] .= ($test['not'] ? 'not true' : 'true'); + break; + case 'exists': + $tests[$i] .= ($test['not'] ? 'not ' : ''); + if (is_array($test['arg'])) + $tests[$i] .= 'exists ["' . implode('", "', $this->_escape_string($test['arg'])) . '"]'; + else + $tests[$i] .= 'exists "' . $this->_escape_string($test['arg']) . '"'; + break; + case 'header': + $tests[$i] .= ($test['not'] ? 'not ' : ''); + $tests[$i] .= 'header :' . $test['type']; + if (is_array($test['arg1'])) + $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg1'])) . '"]'; + else + $tests[$i] .= ' "' . $this->_escape_string($test['arg1']) . '"'; + if (is_array($test['arg2'])) + $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg2'])) . '"]'; + else + $tests[$i] .= ' "' . $this->_escape_string($test['arg2']) . '"'; + break; + } + $i++; + } + +// $script .= ($idx>0 ? 'els' : '').($rule['join'] ? 'if allof (' : 'if anyof ('); // disabled rule: if false #.... $script .= 'if' . ($rule['disabled'] ? ' false #' : ''); - $script .= $rule['join'] ? ' allof (' : ' anyof ('; - if (sizeof($tests) > 1) - $script .= implode(", ", $tests); - else if (sizeof($tests)) - $script .= $tests[0]; - else - $script .= 'true'; - $script .= ")\n{\n"; - - // action(s) - foreach ($rule['actions'] as $action) { - switch ($action['type']) { - case 'fileinto': - $extension = 'fileinto'; - $script .= "\tfileinto \"" . $this->_escape_string($action['target']) . "\";\n"; - break; - case 'redirect': - $script .= "\tredirect \"" . $this->_escape_string($action['target']) . "\";\n"; - break; - case 'reject': - case 'ereject': - $extension = $action['type']; - if (strpos($action['target'], "\n")!==false) - $script .= "\t".$action['type']." text:\n" . $action['target'] . "\n.\n;\n"; - else - $script .= "\t".$action['type']." \"" . $this->_escape_string($action['target']) . "\";\n"; - break; - case 'keep': - case 'discard': - case 'stop': - $script .= "\t" . $action['type'] .";\n"; - break; - case 'vacation': - $extension = 'vacation'; - $script .= "\tvacation"; - if ($action['days']) - $script .= " :days " . $action['days']; - if ($action['addresses']) - $script .= " :addresses " . $this->_print_list($action['addresses']); - if ($action['subject']) - $script .= " :subject \"" . $this->_escape_string($action['subject']) . "\""; - if ($action['handle']) - $script .= " :handle \"" . $this->_escape_string($action['handle']) . "\""; - if ($action['from']) - $script .= " :from \"" . $this->_escape_string($action['from']) . "\""; - if ($action['mime']) - $script .= " :mime"; - if (strpos($action['reason'], "\n")!==false) - $script .= " text:\n" . $action['reason'] . "\n.\n;\n"; - else - $script .= " \"" . $this->_escape_string($action['reason']) . "\";\n"; - break; - } - - if ($extension && !isset($exts[$extension])) - $exts[$extension] = $extension; - } - - $script .= "}\n"; - $idx++; - } - + $script .= $rule['join'] ? ' allof (' : ' anyof ('; + if (sizeof($tests) > 1) + $script .= implode(", ", $tests); + else if (sizeof($tests)) + $script .= $tests[0]; + else + $script .= 'true'; + $script .= ")\n{\n"; + + // action(s) + foreach ($rule['actions'] as $action) { + switch ($action['type']) { + case 'fileinto': + $extension = 'fileinto'; + $script .= "\tfileinto \"" . $this->_escape_string($action['target']) . "\";\n"; + break; + case 'redirect': + $script .= "\tredirect \"" . $this->_escape_string($action['target']) . "\";\n"; + break; + case 'reject': + case 'ereject': + $extension = $action['type']; + if (strpos($action['target'], "\n")!==false) + $script .= "\t".$action['type']." text:\n" . $action['target'] . "\n.\n;\n"; + else + $script .= "\t".$action['type']." \"" . $this->_escape_string($action['target']) . "\";\n"; + break; + case 'keep': + case 'discard': + case 'stop': + $script .= "\t" . $action['type'] .";\n"; + break; + case 'vacation': + $extension = 'vacation'; + $script .= "\tvacation"; + if ($action['days']) + $script .= " :days " . $action['days']; + if ($action['addresses']) + $script .= " :addresses " . $this->_print_list($action['addresses']); + if ($action['subject']) + $script .= " :subject \"" . $this->_escape_string($action['subject']) . "\""; + if ($action['handle']) + $script .= " :handle \"" . $this->_escape_string($action['handle']) . "\""; + if ($action['from']) + $script .= " :from \"" . $this->_escape_string($action['from']) . "\""; + if ($action['mime']) + $script .= " :mime"; + if (strpos($action['reason'], "\n")!==false) + $script .= " text:\n" . $action['reason'] . "\n.\n;\n"; + else + $script .= " \"" . $this->_escape_string($action['reason']) . "\";\n"; + break; + } + + if ($extension && !isset($exts[$extension])) + $exts[$extension] = $extension; + } + + $script .= "}\n"; + $idx++; + } + // requires if (sizeof($exts)) $script = 'require ["' . implode('","', $exts) . "\"];\n" . $script; @@ -588,28 +589,28 @@ class rcube_sieve_script } /** - * Returns script object - * - */ + * Returns script object + * + */ public function as_array() { return $this->content; } /** - * Returns array of supported extensions - * - */ + * Returns array of supported extensions + * + */ public function get_extensions() { return array_values($this->supported); } /** - * Converts text script to rules array - * - * @param string Text script - */ + * Converts text script to rules array + * + * @param string Text script + */ private function _parse_text($script) { $i = 0; @@ -621,32 +622,32 @@ class rcube_sieve_script // tokenize rules if ($tokens = preg_split('/(# rule:\[.*\])\r?\n/', $script, -1, PREG_SPLIT_DELIM_CAPTURE)) { foreach($tokens as $token) { - if (preg_match('/^# rule:\[(.*)\]/', $token, $matches)) { - $content[$i]['name'] = $matches[1]; - } - elseif (isset($content[$i]['name']) && sizeof($content[$i]) == 1) { - if ($rule = $this->_tokenize_rule($token)) { - $content[$i] = array_merge($content[$i], $rule); - $i++; - } - else // unknown rule format - unset($content[$i]); - } - } + if (preg_match('/^# rule:\[(.*)\]/', $token, $matches)) { + $content[$i]['name'] = $matches[1]; + } + else if (isset($content[$i]['name']) && sizeof($content[$i]) == 1) { + if ($rule = $this->_tokenize_rule($token)) { + $content[$i] = array_merge($content[$i], $rule); + $i++; + } + else // unknown rule format + unset($content[$i]); + } + } } return $content; } /** - * Convert text script fragment to rule object - * - * @param string Text rule - */ + * Convert text script fragment to rule object + * + * @param string Text rule + */ private function _tokenize_rule($content) { $result = NULL; - + if (preg_match('/^(if|elsif|else)\s+((true|false|not\s+true|allof|anyof|exists|header|not|size)(.*))\s+\{(.*)\}$/sm', trim($content), $matches)) { @@ -660,28 +661,28 @@ class rcube_sieve_script else $disabled = false; - list($tests, $join) = $this->_parse_tests($tests); - $actions = $this->_parse_actions(trim($matches[5])); + list($tests, $join) = $this->_parse_tests($tests); + $actions = $this->_parse_actions(trim($matches[5])); - if ($tests && $actions) - $result = array( - 'type' => $matches[1], - 'tests' => $tests, - 'actions' => $actions, - 'join' => $join, + if ($tests && $actions) + $result = array( + 'type' => $matches[1], + 'tests' => $tests, + 'actions' => $actions, + 'join' => $join, 'disabled' => $disabled, - ); - } + ); + } return $result; - } + } /** - * Parse body of actions section - * - * @param string Text body - * @return array Array of parsed action type/target pairs - */ + * Parse body of actions section + * + * @param string Text body + * @return array Array of parsed action type/target pairs + */ private function _parse_actions($content) { $result = NULL; @@ -706,55 +707,55 @@ class rcube_sieve_script // parse actions body if (preg_match_all($pattern, $content, $mm, PREG_SET_ORDER)) { - foreach ($mm as $m) { - $content = trim($m[0]); - - if(preg_match('/^(discard|keep|stop)/', $content, $matches)) { - $result[] = array('type' => $matches[1]); - } - elseif(preg_match('/^fileinto/', $content)) { - $result[] = array('type' => 'fileinto', 'target' => $this->_parse_string($m[sizeof($m)-1])); - } - elseif(preg_match('/^redirect/', $content)) { - $result[] = array('type' => 'redirect', 'target' => $this->_parse_string($m[sizeof($m)-1])); - } - elseif(preg_match('/^(reject|ereject)\s+(.*);$/sm', $content, $matches)) { - $result[] = array('type' => $matches[1], 'target' => $this->_parse_string($matches[2])); - } - elseif(preg_match('/^vacation\s+(.*);$/sm', $content, $matches)) { - $vacation = array('type' => 'vacation'); - - if (preg_match('/:(days)\s+([0-9]+)/', $content, $vm)) { - $vacation['days'] = $vm[2]; - $content = preg_replace('/:(days)\s+([0-9]+)/', '', $content); - } - if (preg_match('/:(subject)\s+(".*?[^\\\]")/', $content, $vm)) { - $vacation['subject'] = $vm[2]; - $content = preg_replace('/:(subject)\s+(".*?[^\\\]")/', '', $content); - } - if (preg_match('/:(addresses)\s+\[(.*?[^\\\])\]/', $content, $vm)) { - $vacation['addresses'] = $this->_parse_list($vm[2]); - $content = preg_replace('/:(addresses)\s+\[(.*?[^\\\])\]/', '', $content); - } - if (preg_match('/:(handle)\s+(".*?[^\\\]")/', $content, $vm)) { - $vacation['handle'] = $vm[2]; - $content = preg_replace('/:(handle)\s+(".*?[^\\\]")/', '', $content); - } - if (preg_match('/:(from)\s+(".*?[^\\\]")/', $content, $vm)) { - $vacation['from'] = $vm[2]; - $content = preg_replace('/:(from)\s+(".*?[^\\\]")/', '', $content); - } - - $content = preg_replace('/^vacation/', '', $content); - $content = preg_replace('/;$/', '', $content); - $content = trim($content); - - if (preg_match('/^:(mime)/', $content, $vm)) { - $vacation['mime'] = true; - $content = preg_replace('/^:mime/', '', $content); - } - - $vacation['reason'] = $this->_parse_string($content); + foreach ($mm as $m) { + $content = trim($m[0]); + + if(preg_match('/^(discard|keep|stop)/', $content, $matches)) { + $result[] = array('type' => $matches[1]); + } + else if(preg_match('/^fileinto/', $content)) { + $result[] = array('type' => 'fileinto', 'target' => $this->_parse_string($m[sizeof($m)-1])); + } + else if(preg_match('/^redirect/', $content)) { + $result[] = array('type' => 'redirect', 'target' => $this->_parse_string($m[sizeof($m)-1])); + } + else if(preg_match('/^(reject|ereject)\s+(.*);$/sm', $content, $matches)) { + $result[] = array('type' => $matches[1], 'target' => $this->_parse_string($matches[2])); + } + else if(preg_match('/^vacation\s+(.*);$/sm', $content, $matches)) { + $vacation = array('type' => 'vacation'); + + if (preg_match('/:(days)\s+([0-9]+)/', $content, $vm)) { + $vacation['days'] = $vm[2]; + $content = preg_replace('/:(days)\s+([0-9]+)/', '', $content); + } + if (preg_match('/:(subject)\s+(".*?[^\\\]")/', $content, $vm)) { + $vacation['subject'] = $vm[2]; + $content = preg_replace('/:(subject)\s+(".*?[^\\\]")/', '', $content); + } + if (preg_match('/:(addresses)\s+\[(.*?[^\\\])\]/', $content, $vm)) { + $vacation['addresses'] = $this->_parse_list($vm[2]); + $content = preg_replace('/:(addresses)\s+\[(.*?[^\\\])\]/', '', $content); + } + if (preg_match('/:(handle)\s+(".*?[^\\\]")/', $content, $vm)) { + $vacation['handle'] = $vm[2]; + $content = preg_replace('/:(handle)\s+(".*?[^\\\]")/', '', $content); + } + if (preg_match('/:(from)\s+(".*?[^\\\]")/', $content, $vm)) { + $vacation['from'] = $vm[2]; + $content = preg_replace('/:(from)\s+(".*?[^\\\]")/', '', $content); + } + + $content = preg_replace('/^vacation/', '', $content); + $content = preg_replace('/;$/', '', $content); + $content = trim($content); + + if (preg_match('/^:(mime)/', $content, $vm)) { + $vacation['mime'] = true; + $content = preg_replace('/^:mime/', '', $content); + } + + $vacation['reason'] = $this->_parse_string($content); $result[] = $vacation; } @@ -762,25 +763,25 @@ class rcube_sieve_script } return $result; - } - + } + /** - * Parse test/conditions section - * - * @param string Text - */ + * Parse test/conditions section + * + * @param string Text + */ private function _parse_tests($content) { $result = NULL; // lists if (preg_match('/^(allof|anyof)\s+\((.*)\)$/sm', $content, $matches)) { - $content = $matches[2]; - $join = $matches[1]=='allof' ? true : false; - } + $content = $matches[2]; + $join = $matches[1]=='allof' ? true : false; + } else - $join = false; - + $join = false; + // supported tests regular expressions // TODO: comparators, envelope $patterns[] = '(not\s+)?(exists)\s+\[(.*?[^\\\])\]'; @@ -791,56 +792,56 @@ class rcube_sieve_script $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)\s+(".*?[^\\\]")\s+(".*?[^\\\]")'; $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)\s+\[(.*?[^\\\]")\]\s+(".*?[^\\\]")'; $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)\s+(".*?[^\\\]")\s+\[(.*?[^\\\]")\]'; - + // join patterns... $pattern = '/(' . implode(')|(', $patterns) . ')/'; // ...and parse tests list if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) { - foreach ($matches as $match) { - $size = sizeof($match); - - if (preg_match('/^(not\s+)?size/', $match[0])) { - $result[] = array( - 'test' => 'size', - 'not' => $match[$size-4] ? true : false, - 'type' => $match[$size-2], // under/over - 'arg' => $match[$size-1], // value - ); - } - elseif (preg_match('/^(not\s+)?header/', $match[0])) { - $result[] = array( - 'test' => 'header', - 'not' => $match[$size-5] ? true : false, - 'type' => $match[$size-3], // is/contains/matches - 'arg1' => $this->_parse_list($match[$size-2]), // header(s) - 'arg2' => $this->_parse_list($match[$size-1]), // string(s) - ); - } - elseif (preg_match('/^(not\s+)?exists/', $match[0])) { - $result[] = array( - 'test' => 'exists', - 'not' => $match[$size-3] ? true : false, - 'arg' => $this->_parse_list($match[$size-1]), // header(s) - ); - } - elseif (preg_match('/^(not\s+)?true/', $match[0])) { - $result[] = array( - 'test' => 'true', - 'not' => $match[$size-2] ? true : false, - ); - } - } - } + foreach ($matches as $match) { + $size = sizeof($match); + + if (preg_match('/^(not\s+)?size/', $match[0])) { + $result[] = array( + 'test' => 'size', + 'not' => $match[$size-4] ? true : false, + 'type' => $match[$size-2], // under/over + 'arg' => $match[$size-1], // value + ); + } + else if (preg_match('/^(not\s+)?header/', $match[0])) { + $result[] = array( + 'test' => 'header', + 'not' => $match[$size-5] ? true : false, + 'type' => $match[$size-3], // is/contains/matches + 'arg1' => $this->_parse_list($match[$size-2]), // header(s) + 'arg2' => $this->_parse_list($match[$size-1]), // string(s) + ); + } + else if (preg_match('/^(not\s+)?exists/', $match[0])) { + $result[] = array( + 'test' => 'exists', + 'not' => $match[$size-3] ? true : false, + 'arg' => $this->_parse_list($match[$size-1]), // header(s) + ); + } + else if (preg_match('/^(not\s+)?true/', $match[0])) { + $result[] = array( + 'test' => 'true', + 'not' => $match[$size-2] ? true : false, + ); + } + } + } return array($result, $join); - } + } /** - * Parse string value - * - * @param string Text - */ + * Parse string value + * + * @param string Text + */ private function _parse_string($content) { $text = ''; @@ -848,81 +849,81 @@ class rcube_sieve_script if (preg_match('/^text:(.*)\.$/sm', $content, $matches)) $text = trim($matches[1]); - elseif (preg_match('/^"(.*)"$/', $content, $matches)) + else if (preg_match('/^"(.*)"$/', $content, $matches)) $text = str_replace('\"', '"', $matches[1]); return $text; - } + } /** - * Escape special chars in string value - * - * @param string Text - */ + * Escape special chars in string value + * + * @param string Text + */ private function _escape_string($content) { $replace['/"/'] = '\\"'; - + if (is_array($content)) { - for ($x=0, $y=sizeof($content); $x<$y; $x++) - $content[$x] = preg_replace(array_keys($replace), array_values($replace), $content[$x]); - - return $content; - } + for ($x=0, $y=sizeof($content); $x<$y; $x++) + $content[$x] = preg_replace(array_keys($replace), + array_values($replace), $content[$x]); + + return $content; + } else return preg_replace(array_keys($replace), array_values($replace), $content); } /** - * Parse string or list of strings to string or array of strings - * - * @param string Text - */ + * Parse string or list of strings to string or array of strings + * + * @param string Text + */ private function _parse_list($content) { $result = array(); - + for ($x=0, $len=strlen($content); $x<$len; $x++) { - switch ($content[$x]) { - case '\\': - $str .= $content[++$x]; + switch ($content[$x]) { + case '\\': + $str .= $content[++$x]; + break; + case '"': + if (isset($str)) { + $result[] = $str; + unset($str); + } + else + $str = ''; break; - case '"': - if (isset($str)) { - $result[] = $str; - unset($str); - } - else - $str = ''; - break; - default: - if(isset($str)) - $str .= $content[$x]; - break; - } - } - + default: + if(isset($str)) + $str .= $content[$x]; + break; + } + } + if (sizeof($result)>1) return $result; - elseif (sizeof($result) == 1) - return $result[0]; + else if (sizeof($result) == 1) + return $result[0]; else return NULL; - } + } /** - * Convert array of elements to list of strings - * - * @param string Text - */ + * Convert array of elements to list of strings + * + * @param string Text + */ private function _print_list($list) { $list = (array) $list; foreach($list as $idx => $val) $list[$idx] = $this->_escape_string($val); - + return '["' . implode('","', $list) . '"]'; } } -?> diff --git a/plugins/managesieve/managesieve.js b/plugins/managesieve/managesieve.js index a700ba55b..74f6db88b 100644 --- a/plugins/managesieve/managesieve.js +++ b/plugins/managesieve/managesieve.js @@ -5,10 +5,10 @@ if (window.rcmail) { var tab = $('').attr('id', 'settingstabpluginmanagesieve').addClass('tablink'); var button = $('').attr('href', rcmail.env.comm_path+'&_action=plugin.managesieve') - .attr('title', rcmail.gettext('managesieve.managefilters')) - .html(rcmail.gettext('managesieve.filters')) - .bind('click', function(e){ return rcmail.command('plugin.managesieve', this) }) - .appendTo(tab); + .attr('title', rcmail.gettext('managesieve.managefilters')) + .html(rcmail.gettext('managesieve.filters')) + .bind('click', function(e){ return rcmail.command('plugin.managesieve', this) }) + .appendTo(tab); // add button and register commands rcmail.add_element(tab, 'tabs'); @@ -25,29 +25,29 @@ if (window.rcmail) { rcmail.register_command('plugin.managesieve-setget', function() { rcmail.managesieve_setget() }, true); if (rcmail.env.action == 'plugin.managesieve') { - if (rcmail.gui_objects.sieveform) { - rcmail.enable_command('plugin.managesieve-save', true); - } + if (rcmail.gui_objects.sieveform) { + rcmail.enable_command('plugin.managesieve-save', true); + } else { - rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', - 'plugin.managesieve-down', false); + rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', + 'plugin.managesieve-down', false); rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror); - } + } if (rcmail.gui_objects.filterslist) { - var p = rcmail; - rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist, {multiselect:false, draggable:false, keyboard:false}); - rcmail.filters_list.addEventListener('select', function(o){ p.managesieve_select(o); }); - rcmail.filters_list.init(); - rcmail.filters_list.focus(); + var p = rcmail; + rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist, {multiselect:false, draggable:false, keyboard:false}); + rcmail.filters_list.addEventListener('select', function(o){ p.managesieve_select(o); }); + rcmail.filters_list.init(); + rcmail.filters_list.focus(); - rcmail.enable_command('plugin.managesieve-set', 'plugin.managesieve-setget', true); - rcmail.enable_command('plugin.managesieve-setact', rcmail.gui_objects.filtersetslist.length); - rcmail.enable_command('plugin.managesieve-setdel', rcmail.gui_objects.filtersetslist.length > 1); + rcmail.enable_command('plugin.managesieve-set', true); + rcmail.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', rcmail.gui_objects.filtersetslist.length); + rcmail.enable_command('plugin.managesieve-setdel', rcmail.gui_objects.filtersetslist.length > 1); $('#'+rcmail.buttons['plugin.managesieve-setact'][0].id).attr('title', rcmail.gettext('managesieve.filterset' + (rcmail.gui_objects.filtersetslist.value == rcmail.env.active_set ? 'deact' : 'act'))); - } + } } if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled) $('#disabled').attr('checked', true); @@ -69,7 +69,7 @@ rcube_webmail.prototype.managesieve_del = function() var id = this.filters_list.get_single_selection(); if (confirm(this.get_label('managesieve.filterdeleteconfirm'))) this.http_request('plugin.managesieve', - '_act=delete&_fid='+this.filters_list.rows[id].uid, true); + '_act=delete&_fid='+this.filters_list.rows[id].uid, true); }; rcube_webmail.prototype.managesieve_up = function() @@ -89,11 +89,10 @@ rcube_webmail.prototype.managesieve_down = function() rcube_webmail.prototype.managesieve_rowid = function(id) { var i, rows = this.filters_list.rows; - + for (i=0; i id) - rows[i].uid = rows[i].uid-1; - } - break; + if (rows[i] != null && rows[i].uid > id) + rows[i].uid = rows[i].uid-1; + } + break; case 'down': var from, fromstatus, status, rows = this.filters_list.rows; - // we need only to replace filter names... + // we need only to replace filter names... for (var i=0; i0; i--) { if (rows[i] == null) { // removed row - } + } else if (i == id) { - this.enable_command('plugin.managesieve-down', false); - break; - } + this.enable_command('plugin.managesieve-down', false); + break; + } else { - this.enable_command('plugin.managesieve-down', true); - break; - } + this.enable_command('plugin.managesieve-down', true); + break; + } } }; @@ -292,26 +291,26 @@ rcube_webmail.prototype.managesieve_ruleadd = function(id) rcube_webmail.prototype.managesieve_rulefill = function(content, id, after) { if (content != '') { - // create new element - var div = document.getElementById('rules'), - row = document.createElement('div'); - - this.managesieve_insertrow(div, row, after); - // fill row after inserting (for IE) - row.setAttribute('id', 'rulerow'+id); - row.className = 'rulerow'; + // create new element + var div = document.getElementById('rules'), + row = document.createElement('div'); + + this.managesieve_insertrow(div, row, after); + // fill row after inserting (for IE) + row.setAttribute('id', 'rulerow'+id); + row.className = 'rulerow'; row.innerHTML = content; - this.managesieve_formbuttons(div); + this.managesieve_formbuttons(div); } }; rcube_webmail.prototype.managesieve_ruledel = function(id) { if (confirm(this.get_label('managesieve.ruledeleteconfirm'))) { - var row = document.getElementById('rulerow'+id); - row.parentNode.removeChild(row); - this.managesieve_formbuttons(document.getElementById('rules')); + var row = document.getElementById('rulerow'+id); + row.parentNode.removeChild(row); + this.managesieve_formbuttons(document.getElementById('rules')); } }; @@ -323,13 +322,13 @@ rcube_webmail.prototype.managesieve_actionadd = function(id) rcube_webmail.prototype.managesieve_actionfill = function(content, id, after) { if (content != '') { - var div = document.getElementById('actions'), - row = document.createElement('div'); + var div = document.getElementById('actions'), + row = document.createElement('div'); - this.managesieve_insertrow(div, row, after); - // fill row after inserting (for IE) - row.className = 'actionrow'; - row.setAttribute('id', 'actionrow'+id); + this.managesieve_insertrow(div, row, after); + // fill row after inserting (for IE) + row.className = 'actionrow'; + row.setAttribute('id', 'actionrow'+id); row.innerHTML = content; this.managesieve_formbuttons(div); @@ -339,9 +338,9 @@ rcube_webmail.prototype.managesieve_actionfill = function(content, id, after) rcube_webmail.prototype.managesieve_actiondel = function(id) { if (confirm(this.get_label('managesieve.actiondeleteconfirm'))) { - var row = document.getElementById('actionrow'+id); - row.parentNode.removeChild(row); - this.managesieve_formbuttons(document.getElementById('actions')); + var row = document.getElementById('actionrow'+id); + row.parentNode.removeChild(row); + this.managesieve_formbuttons(document.getElementById('actions')); } }; @@ -350,7 +349,7 @@ rcube_webmail.prototype.managesieve_insertrow = function(div, row, after) { for (var i=0; i0 || buttons.length>1) { - $(button).removeClass('disabled'); - button.removeAttribute('disabled'); - } - else { - $(button).addClass('disabled'); - button.setAttribute('disabled', true); - } + button = document.getElementById(buttons[i]); + if (i>0 || buttons.length>1) { + $(button).removeClass('disabled'); + button.removeAttribute('disabled'); + } + else { + $(button).addClass('disabled'); + button.setAttribute('disabled', true); + } } }; @@ -429,8 +428,8 @@ rcube_webmail.prototype.managesieve_reset = function() for (var x=0; x * * Configuration (see config.inc.php.dist) @@ -17,1112 +17,1059 @@ class managesieve extends rcube_plugin { - public $task = 'settings'; - - private $rc; - private $sieve; - private $errors; - private $form; - private $script = array(); - private $exts = array(); - private $headers = array( - 'subject' => 'Subject', - 'sender' => 'From', - 'recipient' => 'To', - ); - - function init() - { - // add Tab label/title - $this->add_texts('localization/', array('filters','managefilters')); - - // register actions - $this->register_action('plugin.managesieve', array($this, 'managesieve_actions')); - $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save')); - - // include main js script - $this->include_script('managesieve.js'); - } - - function managesieve_start() - { - $this->rc = rcmail::get_instance(); - $this->load_config(); - - // 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'); - require_once($this->home . '/lib/rcube_sieve.php'); - - $host = str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')); - $port = $this->rc->config->get('managesieve_port', 2000); - - // try to connect to managesieve server and to fetch the script - $this->sieve = new rcube_sieve($_SESSION['username'], - $this->rc->decrypt($_SESSION['password']), - $host, $port, - $this->rc->config->get('managesieve_usetls', false), - $this->rc->config->get('managesieve_disabled_extensions'), - $this->rc->config->get('managesieve_debug', false) + public $task = 'settings'; + + private $rc; + private $sieve; + private $errors; + private $form; + private $script = array(); + private $exts = array(); + private $headers = array( + 'subject' => 'Subject', + 'sender' => 'From', + 'recipient' => 'To', ); - if (!($error = $this->sieve->error())) { - - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - $_SESSION['managesieve_active'] = $active; - - if (!empty($_GET['_set'])) { - $script_name = get_input_value('_set', 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 ($script_name) - $this->sieve->load($script_name); - - $error = $this->sieve->error(); - } - - // finally set script objects - if ($error) + + function init() { - switch ($error) { - case SIEVE_ERROR_CONNECTION: - case SIEVE_ERROR_LOGIN: - $this->rc->output->show_message('managesieve.filterconnerror', 'error'); - break; - default: - $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); - break; - } - - raise_error(array('code' => 403, 'type' => 'php', 'message' => "Unable to connect to managesieve on $host:$port"), true, false); - - // to disable 'Add filter' button set env variable - $this->rc->output->set_env('filterconnerror', true); - $this->script = array(); + // add Tab label/title + $this->add_texts('localization/', array('filters','managefilters')); + + // register actions + $this->register_action('plugin.managesieve', array($this, 'managesieve_actions')); + $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save')); + + // include main js script + $this->include_script('managesieve.js'); } - else + + function managesieve_start() { - $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; - } + $this->rc = rcmail::get_instance(); + $this->load_config(); + + // 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'); + require_once($this->home . '/lib/rcube_sieve.php'); + + $host = str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')); + $port = $this->rc->config->get('managesieve_port', 2000); + + // try to connect to managesieve server and to fetch the script + $this->sieve = new rcube_sieve($_SESSION['username'], + $this->rc->decrypt($_SESSION['password']), $host, $port, + $this->rc->config->get('managesieve_usetls', false), + $this->rc->config->get('managesieve_disabled_extensions'), + $this->rc->config->get('managesieve_debug', false) + ); + + if (!($error = $this->sieve->error())) { + + $list = $this->sieve->get_scripts(); + $active = $this->sieve->get_active(); + $_SESSION['managesieve_active'] = $active; + + if (!empty($_GET['_set'])) { + $script_name = get_input_value('_set', 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 ($script_name) + $this->sieve->load($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'); + break; + default: + $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); + break; + } + + raise_error(array('code' => 403, 'type' => 'php', + 'file' => __FILE__, 'line' => __LINE__, + 'message' => "Unable to connect to managesieve on $host:$port"), true, false); + + // to disable 'Add filter' button set env variable + $this->rc->output->set_env('filterconnerror', true); + $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; + } - function managesieve_actions() - { - // Init plugin and handle managesieve connection - $error = $this->managesieve_start(); + return $error; + } - // Handle user requests - if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) + function managesieve_actions() { - $fid = (int) get_input_value('_fid', RCUBE_INPUT_GET); - - if ($action=='up' && !$error) - { - if ($fid && isset($this->script[$fid]) && isset($this->script[$fid-1])) - { - if ($this->sieve->script->update_rule($fid, $this->script[$fid-1]) !== false - && $this->sieve->script->update_rule($fid-1, $this->script[$fid]) !== false) - $result = $this->sieve->save(); - - if ($result) { -// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->command('managesieve_updatelist', 'up', '', $fid); - } else - $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); + // Init plugin and handle managesieve connection + $error = $this->managesieve_start(); + + // Handle user requests + if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) { + $fid = (int) get_input_value('_fid', RCUBE_INPUT_GET); + + if ($action == 'up' && !$error) { + if ($fid && isset($this->script[$fid]) && isset($this->script[$fid-1])) { + if ($this->sieve->script->update_rule($fid, $this->script[$fid-1]) !== false + && $this->sieve->script->update_rule($fid-1, $this->script[$fid]) !== false) { + $result = $this->sieve->save(); + } + + if ($result) { +// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); + $this->rc->output->command('managesieve_updatelist', 'up', '', $fid); + } else + $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); + } + } + else if ($action == 'down' && !$error) { + if (isset($this->script[$fid]) && isset($this->script[$fid+1])) { + if ($this->sieve->script->update_rule($fid, $this->script[$fid+1]) !== false + && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false) { + $result = $this->sieve->save(); + } + + if ($result === true) { +// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); + $this->rc->output->command('managesieve_updatelist', 'down', '', $fid); + } else { + $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); + } + } + } + else if ($action == 'delete' && !$error) { + if (isset($this->script[$fid])) { + if ($this->sieve->script->delete_rule($fid)) + $result = $this->sieve->save(); + + 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('managesieve_reset'); + $_SESSION['managesieve_active'] = $script_name; + } else { + $this->rc->output->show_message('managesieve.setactivateerror', 'error'); + } + } + else if ($action == 'deact' && !$error) { + $result = $this->sieve->deactivate(); + + if ($result === true) { + $this->rc->output->set_env('active_set', ''); + $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation'); + $this->rc->output->command('managesieve_reset'); + $_SESSION['managesieve_active'] = ''; + } else { + $this->rc->output->show_message('managesieve.setdeactivateerror', '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'); + $this->rc->session->remove('managesieve_current'); + } else { + $this->rc->output->show_message('managesieve.setdeleteerror', 'error'); + } + } + else if ($action == 'setget') { + $script_name = get_input_value('_set', RCUBE_INPUT_GPC); + $script = $this->sieve->get_script($script_name); + + if (PEAR::isError($script)) + exit; + + $browser = new rcube_browser; + + // send download headers + header("Content-Type: application/octet-stream"); + header("Content-Length: ".strlen($script)); + + if ($browser->ie) + header("Content-Type: application/force-download"); + if ($browser->ie && $browser->ver < 7) + $filename = rawurlencode(abbreviate_string($script_name, 55)); + else if ($browser->ie) + $filename = rawurlencode($script_name); + else + $filename = addcslashes($script_name, '\\"'); + + header("Content-Disposition: attachment; filename=\"$filename.txt\""); + echo $script; + exit; + } + elseif ($action == 'ruleadd') { + $rid = get_input_value('_rid', RCUBE_INPUT_GPC); + $id = $this->genid(); + $content = $this->rule_div($fid, $id, false); + + $this->rc->output->command('managesieve_rulefill', $content, $id, $rid); + } + elseif ($action == 'actionadd') { + $aid = get_input_value('_aid', RCUBE_INPUT_GPC); + $id = $this->genid(); + $content = $this->action_div($fid, $id, false); + + $this->rc->output->command('managesieve_actionfill', $content, $id, $aid); + } + + $this->rc->output->send(); } - } - else if ($action=='down' && !$error) - { - if (isset($this->script[$fid]) && isset($this->script[$fid+1])) - { - if ($this->sieve->script->update_rule($fid, $this->script[$fid+1]) !== false - && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false) - $result = $this->sieve->save(); - - if ($result === true) { -// $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->command('managesieve_updatelist', 'down', '', $fid); - } else { - $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); - } - } - } - else if ($action=='delete' && !$error) - { - if (isset($this->script[$fid])) - { - if ($this->sieve->script->delete_rule($fid)) - $result = $this->sieve->save(); - - 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('managesieve_reset'); - $_SESSION['managesieve_active'] = $script_name; - } else { - $this->rc->output->show_message('managesieve.setactivateerror', 'error'); - } - } - else if ($action=='deact' && !$error) - { - $result = $this->sieve->deactivate(); - - if ($result === true) { - $this->rc->output->set_env('active_set', ''); - $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation'); - $this->rc->output->command('managesieve_reset'); - $_SESSION['managesieve_active'] = ''; - } else { - $this->rc->output->show_message('managesieve.setdeactivateerror', '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'); - $this->rc->session->remove('managesieve_current'); - } else { - $this->rc->output->show_message('managesieve.setdeleteerror', 'error'); - } - } - else if ($action=='setget') - { - $script_name = get_input_value('_set', RCUBE_INPUT_GPC); - $script = $this->sieve->get_script($script_name); - - if (PEAR::isError($script)) - exit; - - $browser = new rcube_browser; - - // send download headers - header("Content-Type: application/octet-stream"); - header("Content-Length: ".strlen($script)); - - if ($browser->ie) - header("Content-Type: application/force-download"); - if ($browser->ie && $browser->ver < 7) - $filename = rawurlencode(abbreviate_string($script_name, 55)); - else if ($browser->ie) - $filename = rawurlencode($script_name); - else - $filename = addcslashes($script_name, '\\"'); - - header("Content-Disposition: attachment; filename=\"$filename.txt\""); - echo $script; - exit; - } - elseif ($action=='ruleadd') - { - $rid = get_input_value('_rid', RCUBE_INPUT_GPC); - $id = $this->genid(); - $content = $this->rule_div($fid, $id, false); - - $this->rc->output->command('managesieve_rulefill', $content, $id, $rid); - } - elseif ($action=='actionadd') - { - $aid = get_input_value('_aid', RCUBE_INPUT_GPC); - $id = $this->genid(); - $content = $this->action_div($fid, $id, false); - - $this->rc->output->command('managesieve_actionfill', $content, $id, $aid); - } - - $this->rc->output->send(); + + $this->managesieve_send(); } - $this->managesieve_send(); - } + function managesieve_save() + { + // Init plugin and handle managesieve connection + $error = $this->managesieve_start(); + + // filters set add action + if (!empty($_POST['_newset'])) { + $name = get_input_value('_name', RCUBE_INPUT_POST); + $copy = get_input_value('_copy', RCUBE_INPUT_POST); + $from = get_input_value('_from', RCUBE_INPUT_POST); + + if (!$name) + $error = 'managesieve.emptyname'; + else if (mb_strlen($name)>128) + $error = 'managesieve.nametoolong'; + else if ($from == 'file') { + // from file + if (is_uploaded_file($_FILES['_file']['tmp_name'])) { + $file = file_get_contents($_FILES['_file']['tmp_name']); + $file = preg_replace('/\r/', '', $file); + // for security don't save script directly + // check syntax before, like this... + $this->sieve->load_script($file); + if (!$this->sieve->save($name)) { + $error = 'managesieve.setcreateerror'; + } + } + else { // upload failed + $err = $_FILES['_file']['error']; + $error = true; + + if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { + $msg = rcube_label(array('name' => 'filesizeerror', + 'vars' => array('size' => + show_bytes(parse_bytes(ini_get('upload_max_filesize')))))); + } + else { + $error = 'fileuploaderror'; + } + } + } + else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) { + $error = 'managesieve.setcreateerror'; + } + + if (!$error) { + $this->rc->output->show_message('managesieve.setcreated', 'confirmation'); + $this->rc->output->command('parent.managesieve_reload', $name); + } else if ($msg) { + $this->rc->output->command('display_message', $msg, 'error'); + } 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, true)); + $fid = trim(get_input_value('_fid', RCUBE_INPUT_POST)); + $join = trim(get_input_value('_join', RCUBE_INPUT_POST)); + + // and arrays + $headers = $_POST['_header']; + $cust_headers = $_POST['_custom_header']; + $ops = $_POST['_rule_op']; + $sizeops = $_POST['_rule_size_op']; + $sizeitems = $_POST['_rule_size_item']; + $sizetargets = $_POST['_rule_size_target']; + $targets = $_POST['_rule_target']; + $act_types = $_POST['_action_type']; + $mailboxes = $_POST['_action_mailbox']; + $act_targets = $_POST['_action_target']; + $area_targets = $_POST['_action_target_area']; + $reasons = $_POST['_action_reason']; + $addresses = $_POST['_action_addresses']; + $days = $_POST['_action_days']; + + // we need a "hack" for radiobuttons + foreach ($sizeitems as $item) + $items[] = $item; + + $this->form['disabled'] = $_POST['_disabled'] ? true : false; + $this->form['join'] = $join=='allof' ? true : false; + $this->form['name'] = $name; + $this->form['tests'] = array(); + $this->form['actions'] = array(); + + if ($name == '') + $this->errors['name'] = $this->gettext('cannotbeempty'); + else + foreach($this->script as $idx => $rule) + if($rule['name'] == $name && $idx != $fid) { + $this->errors['name'] = $this->gettext('ruleexist'); + break; + } + + $i = 0; + // rules + if ($join == 'any') { + $this->form['tests'][0]['test'] = 'true'; + } + else { + foreach($headers as $idx => $header) { + $header = $this->strip_value($header); + $target = $this->strip_value($targets[$idx], true); + $op = $this->strip_value($ops[$idx]); + + // normal header + if (in_array($header, $this->headers)) { + if(preg_match('/^not/', $op)) + $this->form['tests'][$i]['not'] = true; + $type = preg_replace('/^not/', '', $op); + + if ($type == 'exists') { + $this->form['tests'][$i]['test'] = 'exists'; + $this->form['tests'][$i]['arg'] = $header; + } + else { + $this->form['tests'][$i]['type'] = $type; + $this->form['tests'][$i]['test'] = 'header'; + $this->form['tests'][$i]['arg1'] = $header; + $this->form['tests'][$i]['arg2'] = $target; + + if ($target == '') + $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); + } + } + else + switch ($header) { + case 'size': + $sizeop = $this->strip_value($sizeops[$idx]); + $sizeitem = $this->strip_value($items[$idx]); + $sizetarget = $this->strip_value($sizetargets[$idx]); + + $this->form['tests'][$i]['test'] = 'size'; + $this->form['tests'][$i]['type'] = $sizeop; + $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem; + + if (!preg_match('/^[0-9]+(K|M|G)*$/i', $sizetarget)) + $this->errors['tests'][$i]['sizetarget'] = $this->gettext('wrongformat'); + break; + case '...': + $cust_header = $headers = $this->strip_value($cust_headers[$idx]); + + if(preg_match('/^not/', $op)) + $this->form['tests'][$i]['not'] = true; + $type = preg_replace('/^not/', '', $op); + + if ($cust_header == '') + $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); + else { + $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY); + + if (!count($headers)) + $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); + else { + foreach ($headers as $hr) + if (!preg_match('/^[a-z0-9-]+$/i', $hr)) + $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars'); + } + } + + if (empty($this->errors['tests'][$i]['header'])) + $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; + + if ($type == 'exists') { + $this->form['tests'][$i]['test'] = 'exists'; + $this->form['tests'][$i]['arg'] = $cust_header; + } + else { + $this->form['tests'][$i]['test'] = 'header'; + $this->form['tests'][$i]['type'] = $type; + $this->form['tests'][$i]['arg1'] = $cust_header; + $this->form['tests'][$i]['arg2'] = $target; + + if ($target == '') + $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); + } + break; + } + $i++; + } + } + + $i = 0; + // actions + foreach($act_types as $idx => $type) { + $type = $this->strip_value($type); + $target = $this->strip_value($act_targets[$idx]); + + $this->form['actions'][$i]['type'] = $type; + + switch ($type) { + case 'fileinto': + $mailbox = $this->strip_value($mailboxes[$idx]); + $this->form['actions'][$i]['target'] = $mailbox; + break; + case 'reject': + case 'ereject': + $target = $this->strip_value($area_targets[$idx]); + $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target); + + // if ($target == '') +// $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty'); + break; + case 'redirect': + $this->form['actions'][$i]['target'] = $target; + + if ($this->form['actions'][$i]['target'] == '') + $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty'); + else if (!$this->check_email($this->form['actions'][$i]['target'])) + $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning'); + break; + case 'vacation': + $reason = $this->strip_value($reasons[$idx]); + $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason); + $this->form['actions'][$i]['days'] = $days[$idx]; + $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]); +// @TODO: vacation :subject, :mime, :from, :handle - function managesieve_save() - { - // Init plugin and handle managesieve connection - $error = $this->managesieve_start(); + if ($this->form['actions'][$i]['addresses']) { + foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) { + $address = trim($address); + if (!$address) + unset($this->form['actions'][$i]['addresses'][$aidx]); + else if(!$this->check_email($address)) { + $this->errors['actions'][$i]['addresses'] = $this->gettext('noemailwarning'); + break; + } else + $this->form['actions'][$i]['addresses'][$aidx] = $address; + } + } + + if ($this->form['actions'][$i]['reason'] == '') + $this->errors['actions'][$i]['reason'] = $this->gettext('cannotbeempty'); + if ($this->form['actions'][$i]['days'] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i]['days'])) + $this->errors['actions'][$i]['days'] = $this->gettext('forbiddenchars'); + break; + } + + $i++; + } + + if (!$this->errors) { + // zapis skryptu + if (!isset($this->script[$fid])) { + $fid = $this->sieve->script->add_rule($this->form); + $new = true; + } else + $fid = $this->sieve->script->update_rule($fid, $this->form); + + if ($fid !== false) + $save = $this->sieve->save(); + + if ($save && $fid !== false) { + $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); + $this->rc->output->add_script( + sprintf("rcmail.managesieve_updatelist('%s', '%s', %d, %d);", + isset($new) ? 'add' : 'update', Q($this->form['name']), + $fid, $this->form['disabled']), + 'foot'); + } + else { + $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); +// $this->rc->output->send(); + } + } + } - // filters set add action - if (!empty($_POST['_newset'])) + $this->managesieve_send(); + } + + private function managesieve_send() { - $name = get_input_value('_name', RCUBE_INPUT_POST); - $copy = get_input_value('_copy', RCUBE_INPUT_POST); - $from = get_input_value('_from', RCUBE_INPUT_POST); - - if (!$name) - $error = 'managesieve.emptyname'; - else if (mb_strlen($name)>128) - $error = 'managesieve.nametoolong'; - else if ($from == 'file') { - // from file - if (is_uploaded_file($_FILES['_file']['tmp_name'])) { - $file = file_get_contents($_FILES['_file']['tmp_name']); - $file = preg_replace('/\r/', '', $file); - // for security don't save script directly - // check syntax before, like this... - $this->sieve->load_script($file); - if (!$this->sieve->save($name)) { - $error = 'managesieve.setcreateerror'; - } - } - else { // upload failed - $err = $_FILES['_file']['error']; - $error = true; - if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { - $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => - show_bytes(parse_bytes(ini_get('upload_max_filesize')))))); - } - else { - $error = 'fileuploaderror'; - } + // Handle form action + 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'); } - } - else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) { - $error = 'managesieve.setcreateerror'; - } - - if (!$error) { - $this->rc->output->show_message('managesieve.setcreated', 'confirmation'); - $this->rc->output->command('parent.managesieve_reload', $name); - } else if ($msg) { - $this->rc->output->command('display_message', $msg, 'error'); - } else { - $this->rc->output->show_message($error, 'error'); - } } - // filter add/edit action - else if (isset($_POST['_name'])) + + // return the filters list as HTML table + function filters_list($attrib) { - $name = trim(get_input_value('_name', RCUBE_INPUT_POST, true)); - $fid = trim(get_input_value('_fid', RCUBE_INPUT_POST)); - $join = trim(get_input_value('_join', RCUBE_INPUT_POST)); - - // and arrays - $headers = $_POST['_header']; - $cust_headers = $_POST['_custom_header']; - $ops = $_POST['_rule_op']; - $sizeops = $_POST['_rule_size_op']; - $sizeitems = $_POST['_rule_size_item']; - $sizetargets = $_POST['_rule_size_target']; - $targets = $_POST['_rule_target']; - $act_types = $_POST['_action_type']; - $mailboxes = $_POST['_action_mailbox']; - $act_targets = $_POST['_action_target']; - $area_targets = $_POST['_action_target_area']; - $reasons = $_POST['_action_reason']; - $addresses = $_POST['_action_addresses']; - $days = $_POST['_action_days']; - - // we need a "hack" for radiobuttons - foreach ($sizeitems as $item) - $items[] = $item; - - $this->form['disabled'] = $_POST['_disabled'] ? true : false; - $this->form['join'] = $join=='allof' ? true : false; - $this->form['name'] = $name; - $this->form['tests'] = array(); - $this->form['actions'] = array(); - - if ($name == '') - $this->errors['name'] = $this->gettext('cannotbeempty'); - else - foreach($this->script as $idx => $rule) - if($rule['name'] == $name && $idx != $fid) { - $this->errors['name'] = $this->gettext('ruleexist'); - break; - } - - $i = 0; - // rules - if ($join == 'any') - { - $this->form['tests'][0]['test'] = 'true'; - } - else foreach($headers as $idx => $header) - { - $header = $this->strip_value($header); - $target = $this->strip_value($targets[$idx], true); - $op = $this->strip_value($ops[$idx]); - - // normal header - if (in_array($header, $this->headers)) - { - if(preg_match('/^not/', $op)) - $this->form['tests'][$i]['not'] = true; - $type = preg_replace('/^not/', '', $op); - - if ($type == 'exists') - { - $this->form['tests'][$i]['test'] = 'exists'; - $this->form['tests'][$i]['arg'] = $header; - } - else - { - $this->form['tests'][$i]['type'] = $type; - $this->form['tests'][$i]['test'] = 'header'; - $this->form['tests'][$i]['arg1'] = $header; - $this->form['tests'][$i]['arg2'] = $target; - - if ($target == '') - $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); - } - } - else - switch ($header) - { - case 'size': - $sizeop = $this->strip_value($sizeops[$idx]); - $sizeitem = $this->strip_value($items[$idx]); - $sizetarget = $this->strip_value($sizetargets[$idx]); - - $this->form['tests'][$i]['test'] = 'size'; - $this->form['tests'][$i]['type'] = $sizeop; - $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem; - - if (!preg_match('/^[0-9]+(K|M|G)*$/i', $sizetarget)) - $this->errors['tests'][$i]['sizetarget'] = $this->gettext('wrongformat'); - break; - case '...': - $cust_header = $headers = $this->strip_value($cust_headers[$idx]); - - if(preg_match('/^not/', $op)) - $this->form['tests'][$i]['not'] = true; - $type = preg_replace('/^not/', '', $op); - - if ($cust_header == '') - $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); - else { - $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY); - - if (!count($headers)) - $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); - else { - foreach ($headers as $hr) - if (!preg_match('/^[a-z0-9-]+$/i', $hr)) - $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars'); - } - } - - if (empty($this->errors['tests'][$i]['header'])) - $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; - - if ($type == 'exists') - { - $this->form['tests'][$i]['test'] = 'exists'; - $this->form['tests'][$i]['arg'] = $cust_header; - } - else - { - $this->form['tests'][$i]['test'] = 'header'; - $this->form['tests'][$i]['type'] = $type; - $this->form['tests'][$i]['arg1'] = $cust_header; - $this->form['tests'][$i]['arg2'] = $target; - - if ($target == '') - $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); - } - break; - } - $i++; - } - - $i = 0; - // actions - foreach($act_types as $idx => $type) - { - $type = $this->strip_value($type); - $target = $this->strip_value($act_targets[$idx]); - - $this->form['actions'][$i]['type'] = $type; - - switch ($type) - { - case 'fileinto': - $mailbox = $this->strip_value($mailboxes[$idx]); - $this->form['actions'][$i]['target'] = $mailbox; - break; - case 'reject': - case 'ereject': - $target = $this->strip_value($area_targets[$idx]); - $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target); - - // if ($target == '') -// $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty'); - break; - case 'redirect': - $this->form['actions'][$i]['target'] = $target; - - if ($this->form['actions'][$i]['target'] == '') - $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty'); - else if (!$this->check_email($this->form['actions'][$i]['target'])) - $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning'); - break; - case 'vacation': - $reason = $this->strip_value($reasons[$idx]); - $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason); - $this->form['actions'][$i]['days'] = $days[$idx]; - $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]); -// @TODO: vacation :subject, :mime, :from, :handle + // add id to message list table if not specified + if (!strlen($attrib['id'])) + $attrib['id'] = 'rcmfilterslist'; + + // define list of cols to be displayed + $a_show_cols = array('managesieve.filtername'); + + foreach($this->script as $idx => $filter) + $result[] = array( + 'managesieve.filtername' => $filter['name'], + 'id' => $idx, + 'class' => $filter['disabled'] ? 'disabled' : '', + ); + + // create XHTML table + $out = rcube_table_output($attrib, $result, $a_show_cols, 'id'); + + // set client env + $this->rc->output->add_gui_object('filterslist', $attrib['id']); + $this->rc->output->include_script('list.js'); + + // add some labels to client + $this->rc->output->add_label('managesieve.filterdeleteconfirm'); - if ($this->form['actions'][$i]['addresses']) { - foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) { - $address = trim($address); - if (!$address) - unset($this->form['actions'][$i]['addresses'][$aidx]); - else if(!$this->check_email($address)) { - $this->errors['actions'][$i]['addresses'] = $this->gettext('noemailwarning'); - break; - } else - $this->form['actions'][$i]['addresses'][$aidx] = $address; - } - } - - if ($this->form['actions'][$i]['reason'] == '') - $this->errors['actions'][$i]['reason'] = $this->gettext('cannotbeempty'); - if ($this->form['actions'][$i]['days'] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i]['days'])) - $this->errors['actions'][$i]['days'] = $this->gettext('forbiddenchars'); - break; - } - - $i++; - } - - if (!$this->errors) - { - // zapis skryptu - if (!isset($this->script[$fid])) { - $fid = $this->sieve->script->add_rule($this->form); - $new = true; - } else - $fid = $this->sieve->script->update_rule($fid, $this->form); - - if ($fid !== false) - $save = $this->sieve->save(); - - if ($save && $fid !== false) - { - $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); - $this->rc->output->add_script( - sprintf("rcmail.managesieve_updatelist('%s', '%s', %d, %d);", - isset($new) ? 'add' : 'update', Q($this->form['name']), $fid, $this->form['disabled']), - 'foot'); - } - else - { - $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); -// $this->rc->output->send(); - } - } + return $out; } - $this->managesieve_send(); - } - - private function managesieve_send() - { - // Handle form action - 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'); + // return the filters list as - function filtersets_list($attrib) - { - // add id to message list table if not specified - if (!strlen($attrib['id'])) - $attrib['id'] = 'rcmfiltersetslist'; - - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - - $select = new html_select(array('name' => '_set', 'id' => $attrib['id'], 'onchange' => 'rcmail.managesieve_set()')); - - if ($list) { - asort($list, SORT_LOCALE_STRING); - - foreach($list as $set) - $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); + + 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/blank.gif'); + + return html::tag('iframe', $attrib); } - - $out = $select->show($this->sieve->current); - - // set client env - $this->rc->output->add_gui_object('filtersetslist', $attrib['id']); - $this->rc->output->add_label('managesieve.setdeleteconfirm', - 'managesieve.active', 'managesieve.filtersetact', 'managesieve.filtersetdeact'); - - return $out; - } - 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/blank.gif'); - - return html::tag('iframe', $attrib); - } - - - function filterset_form($attrib) - { - if (!$attrib['id']) - $attrib['id'] = 'rcmfiltersetform'; + function filterset_form($attrib) + { + if (!$attrib['id']) + $attrib['id'] = 'rcmfiltersetform'; + + $out = '
'."\n"; - $out = ''."\n"; - - $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task)); - $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save')); - $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); - $hiddenfields->add(array('name' => '_newset', 'value' => 1)); - - $out .= $hiddenfields->show(); - - $name = get_input_value('_name', RCUBE_INPUT_POST); - $copy = get_input_value('_copy', RCUBE_INPUT_POST); - $selected = get_input_value('_from', RCUBE_INPUT_POST); - - $table = new html_table(array('cols' => 2)); - - // filter set name input - $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30, - 'class' => ($this->errors['name'] ? 'error' : ''))); - - $table->add('title', sprintf('', '_name', Q($this->gettext('filtersetname')))); - $table->add(null, $input_name->show($name)); - - $from ='
'; - $from .= ''; - $from .= sprintf(' ', 'from_none', Q($this->gettext('none'))); - - // filters set list - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - - $select = new html_select(array('name' => '_copy', 'id' => '_copy')); + $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task)); + $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save')); + $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); + $hiddenfields->add(array('name' => '_newset', 'value' => 1)); - asort($list, SORT_LOCALE_STRING); + $out .= $hiddenfields->show(); - foreach($list as $set) - $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); - - $from .= '
'; - $from .= sprintf(' ', 'from_set', Q($this->gettext('fromset'))); - $from .= $select->show($copy); + $name = get_input_value('_name', RCUBE_INPUT_POST); + $copy = get_input_value('_copy', RCUBE_INPUT_POST); + $selected = get_input_value('_from', RCUBE_INPUT_POST); - // script upload box - $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30, - 'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : ''))); + $table = new html_table(array('cols' => 2)); - $from .= '
'; - $from .= sprintf(' ', 'from_file', Q($this->gettext('fromfile'))); - $from .= $upload->show(); - $from .= '
'; + // filter set name input + $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30, + 'class' => ($this->errors['name'] ? 'error' : ''))); - $table->add('title', ''); - $table->add(null, $from); + $table->add('title', sprintf('', + '_name', Q($this->gettext('filtersetname')))); + $table->add(null, $input_name->show($name)); - $out .= $table->show(); - - $this->rc->output->add_gui_object('sieveform', 'filtersetform'); + $from ='
'; + $from .= ''; + $from .= sprintf(' ', 'from_none', Q($this->gettext('none'))); - return $out; - } + // filters set list + $list = $this->sieve->get_scripts(); + $active = $this->sieve->get_active(); + $select = new html_select(array('name' => '_copy', 'id' => '_copy')); - function filter_form($attrib) - { - if (!$attrib['id']) - $attrib['id'] = 'rcmfilterform'; + if (is_array($list)) { + asort($list, SORT_LOCALE_STRING); - $fid = get_input_value('_fid', RCUBE_INPUT_GPC); - $scr = isset($this->form) ? $this->form : $this->script[$fid]; + foreach ($list as $set) + $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); - $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task)); - $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save')); - $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); - $hiddenfields->add(array('name' => '_fid', 'value' => $fid)); + $from .= '
'; + $from .= sprintf(' ', 'from_set', Q($this->gettext('fromset'))); + $from .= $select->show($copy); + } + + // script upload box + $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30, + 'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : ''))); + + $from .= '
'; + $from .= sprintf(' ', 'from_file', Q($this->gettext('fromfile'))); + $from .= $upload->show(); + $from .= '
'; + + $table->add('title', ''); + $table->add(null, $from); + + $out .= $table->show(); + + $this->rc->output->add_gui_object('sieveform', 'filtersetform'); + + return $out; + } - $out = ''."\n"; - $out .= $hiddenfields->show(); - // 'any' flag - if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) - $any = true; + function filter_form($attrib) + { + if (!$attrib['id']) + $attrib['id'] = 'rcmfilterform'; - // filter name input - $field_id = '_name'; - $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30, - 'class' => ($this->errors['name'] ? 'error' : ''))); + $fid = get_input_value('_fid', RCUBE_INPUT_GPC); + $scr = isset($this->form) ? $this->form : $this->script[$fid]; - if (isset($scr)) - $input_name = $input_name->show($scr['name']); - else - $input_name = $input_name->show(); + $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task)); + $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save')); + $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); + $hiddenfields->add(array('name' => '_fid', 'value' => $fid)); - $out .= sprintf("\n %s

\n", - $field_id, Q($this->gettext('filtername')), $input_name); + $out = ''."\n"; + $out .= $hiddenfields->show(); - $out .= '
' . Q($this->gettext('messagesrules')) . "\n"; + // 'any' flag + if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) + $any = true; - // any, allof, anyof radio buttons - $field_id = '_allof'; - $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof', - 'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio')); + // filter name input + $field_id = '_name'; + $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30, + 'class' => ($this->errors['name'] ? 'error' : ''))); - if (isset($scr) && !$any) - $input_join = $input_join->show($scr['join'] ? 'allof' : ''); - else - $input_join = $input_join->show(); + if (isset($scr)) + $input_name = $input_name->show($scr['name']); + else + $input_name = $input_name->show(); - $out .= sprintf("%s \n", - $input_join, $field_id, Q($this->gettext('filterallof'))); + $out .= sprintf("\n %s

\n", + $field_id, Q($this->gettext('filtername')), $input_name); - $field_id = '_anyof'; - $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof', - 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio')); + $out .= '
' . Q($this->gettext('messagesrules')) . "\n"; - if (isset($scr) && !$any) - $input_join = $input_join->show($scr['join'] ? '' : 'anyof'); - else - $input_join = $input_join->show('anyof'); // default + // any, allof, anyof radio buttons + $field_id = '_allof'; + $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof', + 'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio')); - $out .= sprintf("%s\n", - $input_join, $field_id, Q($this->gettext('filteranyof'))); + if (isset($scr) && !$any) + $input_join = $input_join->show($scr['join'] ? 'allof' : ''); + else + $input_join = $input_join->show(); - $field_id = '_any'; - $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any', - 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio')); + $out .= sprintf("%s \n", + $input_join, $field_id, Q($this->gettext('filterallof'))); - $input_join = $input_join->show($any ? 'any' : ''); + $field_id = '_anyof'; + $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof', + 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio')); - $out .= sprintf("%s\n", - $input_join, $field_id, Q($this->gettext('filterany'))); + if (isset($scr) && !$any) + $input_join = $input_join->show($scr['join'] ? '' : 'anyof'); + else + $input_join = $input_join->show('anyof'); // default - $rows_num = isset($scr) ? sizeof($scr['tests']) : 1; + $out .= sprintf("%s\n", + $input_join, $field_id, Q($this->gettext('filteranyof'))); - $out .= '\n"; + $field_id = '_any'; + $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any', + 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio')); - $out .= "
\n"; + $input_join = $input_join->show($any ? 'any' : ''); - // actions - $out .= '
' . Q($this->gettext('messagesactions')) . "\n"; + $out .= sprintf("%s\n", + $input_join, $field_id, Q($this->gettext('filterany'))); - $rows_num = isset($scr) ? sizeof($scr['actions']) : 1; + $rows_num = isset($scr) ? sizeof($scr['tests']) : 1; - $out .= '
'; - for ($x=0; $x<$rows_num; $x++) - $out .= $this->action_div($fid, $x); - $out .= "
\n"; + $out .= '\n"; - $out .= "
\n"; - - if ($scr['disabled']) { - $this->rc->output->set_env('rule_disabled', true); + $out .= "
\n"; + + // actions + $out .= '
' . Q($this->gettext('messagesactions')) . "\n"; + + $rows_num = isset($scr) ? sizeof($scr['actions']) : 1; + + $out .= '
'; + for ($x=0; $x<$rows_num; $x++) + $out .= $this->action_div($fid, $x); + $out .= "
\n"; + + $out .= "
\n"; + + if ($scr['disabled']) { + $this->rc->output->set_env('rule_disabled', true); + } + $this->rc->output->add_label( + 'managesieve.ruledeleteconfirm', + 'managesieve.actiondeleteconfirm' + ); + $this->rc->output->add_gui_object('sieveform', 'filterform'); + + return $out; } - $this->rc->output->add_label('managesieve.ruledeleteconfirm'); - $this->rc->output->add_label('managesieve.actiondeleteconfirm'); - $this->rc->output->add_gui_object('sieveform', 'filterform'); - - return $out; - } - - function rule_div($fid, $id, $div=true) - { - $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id]; - $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']); - - $out = $div ? '
'."\n" : ''; - - $out .= ''; - - // add/del buttons - $out .= '
'; - - // headers select - $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id, - 'onchange' => 'header_select(' .$id .')')); - foreach($this->headers as $name => $val) - $select_header->add(Q($this->gettext($name)), Q($val)); - $select_header->add(Q($this->gettext('size')), 'size'); - $select_header->add(Q($this->gettext('...')), '...'); - - // TODO: list arguments - - if ((isset($rule['test']) && $rule['test'] == 'header') - && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers)) - $out .= $select_header->show($rule['arg1']); - elseif ((isset($rule['test']) && $rule['test'] == 'exists') - && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers)) - $out .= $select_header->show($rule['arg']); - elseif (isset($rule['test']) && $rule['test'] == 'size') - $out .= $select_header->show('size'); - elseif (isset($rule['test']) && $rule['test'] != 'true') - $out .= $select_header->show('...'); - else - $out .= $select_header->show(); - - $out .= ''; - - if ((isset($rule['test']) && $rule['test'] == 'header') - && (is_array($rule['arg1']) || !in_array($rule['arg1'], $this->headers))) - $custom = is_array($rule['arg1']) ? implode(', ', $rule['arg1']) : $rule['arg1']; - elseif ((isset($rule['test']) && $rule['test'] == 'exists') - && (is_array($rule['arg']) || !in_array($rule['arg'], $this->headers))) - $custom = is_array($rule['arg']) ? implode(', ', $rule['arg']) : $rule['arg']; - - $out .= '
- error_class($id, 'test', 'header') - .' value="' .Q($custom). '" size="20" /> 
' . "\n"; - - // matching type select (operator) - $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id, - 'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'), 'onchange' => 'rule_op_select('.$id.')')); - $select_op->add(Q($this->gettext('filtercontains')), 'contains'); - $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains'); - $select_op->add(Q($this->gettext('filteris')), 'is'); - $select_op->add(Q($this->gettext('filterisnot')), 'notis'); - $select_op->add(Q($this->gettext('filterexists')), 'exists'); - $select_op->add(Q($this->gettext('filternotexists')), 'notexists'); -// $select_op->add(Q($this->gettext('filtermatches')), 'matches'); -// $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches'); - - // target input (TODO: lists) - - if ($rule['test'] == 'header') + + function rule_div($fid, $id, $div=true) { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']); - $target = $rule['arg2']; + $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id]; + $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']); + + $out = $div ? '
'."\n" : ''; + + $out .= ''; + + // add/del buttons + $out .= '
'; + + // headers select + $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id, + 'onchange' => 'header_select(' .$id .')')); + foreach($this->headers as $name => $val) + $select_header->add(Q($this->gettext($name)), Q($val)); + $select_header->add(Q($this->gettext('size')), 'size'); + $select_header->add(Q($this->gettext('...')), '...'); + + // TODO: list arguments + + if ((isset($rule['test']) && $rule['test'] == 'header') + && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers)) + $out .= $select_header->show($rule['arg1']); + else if ((isset($rule['test']) && $rule['test'] == 'exists') + && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers)) + $out .= $select_header->show($rule['arg']); + else if (isset($rule['test']) && $rule['test'] == 'size') + $out .= $select_header->show('size'); + else if (isset($rule['test']) && $rule['test'] != 'true') + $out .= $select_header->show('...'); + else + $out .= $select_header->show(); + + $out .= ''; + + if ((isset($rule['test']) && $rule['test'] == 'header') + && (is_array($rule['arg1']) || !in_array($rule['arg1'], $this->headers))) + $custom = is_array($rule['arg1']) ? implode(', ', $rule['arg1']) : $rule['arg1']; + else if ((isset($rule['test']) && $rule['test'] == 'exists') + && (is_array($rule['arg']) || !in_array($rule['arg'], $this->headers))) + $custom = is_array($rule['arg']) ? implode(', ', $rule['arg']) : $rule['arg']; + + $out .= '
+ error_class($id, 'test', 'header') + .' value="' .Q($custom). '" size="20" /> 
' . "\n"; + + // matching type select (operator) + $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id, + 'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'), + 'onchange' => 'rule_op_select('.$id.')')); + $select_op->add(Q($this->gettext('filtercontains')), 'contains'); + $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains'); + $select_op->add(Q($this->gettext('filteris')), 'is'); + $select_op->add(Q($this->gettext('filterisnot')), 'notis'); + $select_op->add(Q($this->gettext('filterexists')), 'exists'); + $select_op->add(Q($this->gettext('filternotexists')), 'notexists'); +// $select_op->add(Q($this->gettext('filtermatches')), 'matches'); +// $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches'); + + // target input (TODO: lists) + + if ($rule['test'] == 'header') { + $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']); + $target = $rule['arg2']; + } + else if ($rule['test'] == 'size') { + $out .= $select_op->show(); + if(preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches)) { + $sizetarget = $matches[1]; + $sizeitem = $matches[2]; + } + } + else { + $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']); + $target = ''; + } + + $out .= 'error_class($id, 'test', 'target') + . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n"; + + $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id)); + $select_size_op->add(Q($this->gettext('filterunder')), 'under'); + $select_size_op->add(Q($this->gettext('filterover')), 'over'); + + $out .= '
'; + $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); + $out .= 'error_class($id, 'test', 'sizetarget') .' /> + B + kB + MB + GB'; + $out .= '
'; + $out .= '
'; + $out .= ' '; + $out .= ''; + $out .= '
'; + + $out .= $div ? "
\n" : ''; + + return $out; + } + + function action_div($fid, $id, $div=true) + { + $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id]; + $rows_num = isset($this->form) ? sizeof($this->form['actions']) : sizeof($this->script[$fid]['actions']); + + $out = $div ? '
'."\n" : ''; + + $out .= ''; + + // actions target inputs + $out .= ''; + + // add/del buttons + $out .= ''; + + $out .= '
'; + + // action select + $select_action = new html_select(array('name' => "_action_type[]", 'id' => 'action_type'.$id, + 'onchange' => 'action_type_select(' .$id .')')); + if (in_array('fileinto', $this->exts)) + $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto'); + $select_action->add(Q($this->gettext('messageredirect')), 'redirect'); + if (in_array('reject', $this->exts)) + $select_action->add(Q($this->gettext('messagediscard')), 'reject'); + else if (in_array('ereject', $this->exts)) + $select_action->add(Q($this->gettext('messagediscard')), 'ereject'); + if (in_array('vacation', $this->exts)) + $select_action->add(Q($this->gettext('messagereply')), 'vacation'); + $select_action->add(Q($this->gettext('messagedelete')), 'discard'); + $select_action->add(Q($this->gettext('rulestop')), 'stop'); + + $out .= $select_action->show($action['type']); + $out .= ''; + // shared targets + $out .= 'error_class($id, 'action', 'target') .' />'; + $out .= '\n"; + + // vacation + $out .= '
'; + $out .= ''. Q($this->gettext('vacationreason')) .'
' + .'\n"; + $out .= '
' .Q($this->gettext('vacationaddresses')) . '
' + .'error_class($id, 'action', 'addresses') .' />'; + $out .= '
' . Q($this->gettext('vacationdays')) . '
' + .'error_class($id, 'action', 'days') .' />'; + $out .= '
'; + + // mailbox select + $out .= ''; + $out .= '
'; + $out .= ' '; + $out .= ''; + $out .= '
'; + + $out .= $div ? "
\n" : ''; + + return $out; } - elseif ($rule['test'] == 'size') + + private function genid() { - $out .= $select_op->show(); - if(preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches)) - { - $sizetarget = $matches[1]; - $sizeitem = $matches[2]; - } + $result = intval(rcube_timer()); + return $result; } - else + + private function strip_value($str, $allow_html=false) { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']); - $target = ''; + if (!$allow_html) + $str = strip_tags($str); + + return trim($str); } - $out .= 'error_class($id, 'test', 'target') - . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n"; - - $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id)); - $select_size_op->add(Q($this->gettext('filterunder')), 'under'); - $select_size_op->add(Q($this->gettext('filterover')), 'over'); - - $out .= '
'; - $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); - $out .= 'error_class($id, 'test', 'sizetarget') .' /> - B - kB - MB - GB'; - $out .= '
'; - $out .= '
'; - $out .= ' '; - $out .= ''; - $out .= '
'; - - $out .= $div ? "
\n" : ''; - - return $out; - } - - function action_div($fid, $id, $div=true) - { - $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id]; - $rows_num = isset($this->form) ? sizeof($this->form['actions']) : sizeof($this->script[$fid]['actions']); - - $out = $div ? '
'."\n" : ''; - - $out .= ''; - - // actions target inputs - $out .= ''; - - // add/del buttons - $out .= ''; - - $out .= '
'; - - // action select - $select_action = new html_select(array('name' => "_action_type[]", 'id' => 'action_type'.$id, - 'onchange' => 'action_type_select(' .$id .')')); - if (in_array('fileinto', $this->exts)) - $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto'); - $select_action->add(Q($this->gettext('messageredirect')), 'redirect'); - if (in_array('reject', $this->exts)) - $select_action->add(Q($this->gettext('messagediscard')), 'reject'); - elseif (in_array('ereject', $this->exts)) - $select_action->add(Q($this->gettext('messagediscard')), 'ereject'); - if (in_array('vacation', $this->exts)) - $select_action->add(Q($this->gettext('messagereply')), 'vacation'); - $select_action->add(Q($this->gettext('messagedelete')), 'discard'); - $select_action->add(Q($this->gettext('rulestop')), 'stop'); - - $out .= $select_action->show($action['type']); - $out .= ''; - // shared targets - $out .= 'error_class($id, 'action', 'target') .' />'; - $out .= '\n"; - - // vacation - $out .= '
'; - $out .= ''. Q($this->gettext('vacationreason')) .'
' - .'\n"; - $out .= '
' .Q($this->gettext('vacationaddresses')) . '
' - .'error_class($id, 'action', 'addresses') .' />'; - $out .= '
' . Q($this->gettext('vacationdays')) . '
' - .'error_class($id, 'action', 'days') .' />'; - $out .= '
'; - - // mailbox select - $out .= ''; - $out .= '
'; - $out .= ' '; - $out .= ''; - $out .= '
'; - - $out .= $div ? "
\n" : ''; - - return $out; - } - - private function genid() - { - $result = intval(rcube_timer()); - return $result; - } - - private function strip_value($str, $allow_html=false) - { - if (!$allow_html) - $str = strip_tags($str); - - return trim($str); - } - - private function error_class($id, $type, $target, $name_only=false) - { - // TODO: tooltips - if ($type == 'test' && isset($this->errors['tests'][$id][$target])) - return ($name_only ? 'error' : ' class="error"'); - elseif ($type == 'action' && isset($this->errors['actions'][$id][$target])) - return ($name_only ? 'error' : ' class="error"'); - - return ''; - } - - private function check_email($email) - { - if (function_exists('check_email')); - return check_email($email); - - // Check for invalid characters - if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) - return false; - - // Check that there's one @ symbol, and that the lengths are right - if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $email)) - return false; - - // Split it into sections to make life easier - $email_array = explode('@', $email); - - // Check local part - $local_array = explode('.', $email_array[0]); - foreach ($local_array as $local_part) - if (!preg_match('/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+)|("[^"]+"))$/', $local_part)) - return false; - - // Check domain part - if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/', $email_array[1]) - || preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/', $email_array[1])) - return true; // If an IP address - else - { // If not an IP address - $domain_array = explode('.', $email_array[1]); - if (sizeof($domain_array) < 2) - return false; // Not enough parts to be a valid domain - - foreach ($domain_array as $domain_part) - if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $domain_part)) - return false; - - return true; + + private function mbox_encode($text, $encoding) + { + return rcube_charset_convert($text, 'UTF7-IMAP', $encoding); } - - return false; - } - - private function mbox_encode($text, $encoding) - { - return rcube_charset_convert($text, 'UTF7-IMAP', $encoding); - } } ?> -- cgit v1.2.3