diff options
| author | alec <alec@208e9e7b-5314-0410-a742-e7e81cd9613c> | 2010-05-04 13:27:13 +0000 |
|---|---|---|
| committer | alec <alec@208e9e7b-5314-0410-a742-e7e81cd9613c> | 2010-05-04 13:27:13 +0000 |
| commit | 6601c9e127d61ec04a62c452a9f611e9d15ba171 (patch) | |
| tree | b079c5fbef39f169c990030f67500c0abb56270d /plugins | |
| parent | bf3f51e74d660e3d0efcff2739c58e932fdec72b (diff) | |
- 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
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/managesieve/Changelog | 4 | ||||
| -rw-r--r-- | plugins/managesieve/config.inc.php.dist | 1 | ||||
| -rw-r--r-- | plugins/managesieve/lib/rcube_sieve.php | 929 | ||||
| -rw-r--r-- | plugins/managesieve/managesieve.js | 272 | ||||
| -rw-r--r-- | plugins/managesieve/managesieve.php | 1849 |
5 files changed, 1504 insertions, 1551 deletions
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; + $extension = ''; + $tests = array(); + $i = 0; + + // header + $script .= '# rule:[' . $rule['name'] . "]\n"; - // 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++; - } + // 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 ('); +// $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; - } + $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"; - if ($extension && !isset($exts[$extension])) - $exts[$extension] = $extension; - } + // 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 .= "}\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'); + foreach ($mm as $m) { + $content = trim($m[0]); - 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); - } + 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'); - $content = preg_replace('/^vacation/', '', $content); - $content = preg_replace('/;$/', '', $content); - $content = trim($content); + 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); + } - if (preg_match('/^:(mime)/', $content, $vm)) { - $vacation['mime'] = true; - $content = preg_replace('/^:mime/', '', $content); - } + $content = preg_replace('/^vacation/', '', $content); + $content = preg_replace('/;$/', '', $content); + $content = trim($content); - $vacation['reason'] = $this->_parse_string($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 = $('<span>').attr('id', 'settingstabpluginmanagesieve').addClass('tablink'); var button = $('<a>').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<rows.length; i++) if (rows[i] != null && rows[i].uid == id) - - return i; + return i; } rcube_webmail.prototype.managesieve_updatelist = function(action, name, id, disabled) @@ -103,114 +102,114 @@ rcube_webmail.prototype.managesieve_updatelist = function(action, name, id, disa switch (action) { case 'delete': this.filters_list.remove_row(this.managesieve_rowid(id)); - this.filters_list.clear_selection(); - this.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false); - this.show_contentframe(false); + this.filters_list.clear_selection(); + this.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false); + this.show_contentframe(false); - // re-numbering filters + // re-numbering filters var i, rows = this.filters_list.rows; for (i=0; i<rows.length; i++) { - if (rows[i] != null && rows[i].uid > 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; i<rows.length; i++) { - if (rows[i]==null) { // removed row - continue; + if (rows[i]==null) { // removed row + continue; } else if (rows[i].uid == id) { - from = rows[i].obj; + from = rows[i].obj; fromstatus = $(from).hasClass('disabled'); - } + } else if (rows[i].uid == id+1) { - name = rows[i].obj.cells[0].innerHTML; + name = rows[i].obj.cells[0].innerHTML; status = $(rows[i].obj).hasClass('disabled'); - rows[i].obj.cells[0].innerHTML = from.cells[0].innerHTML; - from.cells[0].innerHTML = name; + rows[i].obj.cells[0].innerHTML = from.cells[0].innerHTML; + from.cells[0].innerHTML = name; $(from)[status?'addClass':'removeClass']('disabled'); $(rows[i].obj)[fromstatus?'addClass':'removeClass']('disabled'); - this.filters_list.highlight_row(i); - break; - } - } - // ... and disable/enable Down button - this.filters_listbuttons(); + this.filters_list.highlight_row(i); + break; + } + } + // ... and disable/enable Down button + this.filters_listbuttons(); break; case 'up': var from, status, fromstatus, rows = this.filters_list.rows; - // we need only to replace filter names... + // we need only to replace filter names... for (var i=0; i<rows.length; i++) { - if (rows[i] == null) { // removed row - continue; + if (rows[i] == null) { // removed row + continue; } else if (rows[i].uid == id-1) { - from = rows[i].obj; + from = rows[i].obj; fromstatus = $(from).hasClass('disabled'); - this.filters_list.highlight_row(i); - } + this.filters_list.highlight_row(i); + } else if (rows[i].uid == id) { - name = rows[i].obj.cells[0].innerHTML; + name = rows[i].obj.cells[0].innerHTML; status = $(rows[i].obj).hasClass('disabled'); - rows[i].obj.cells[0].innerHTML = from.cells[0].innerHTML; - from.cells[0].innerHTML = name; + rows[i].obj.cells[0].innerHTML = from.cells[0].innerHTML; + from.cells[0].innerHTML = name; $(from)[status?'addClass':'removeClass']('disabled'); $(rows[i].obj)[fromstatus?'addClass':'removeClass']('disabled'); - break; - } - } - // ... and disable/enable Up button - this.filters_listbuttons(); + break; + } + } + // ... and disable/enable Up button + this.filters_listbuttons(); break; - + case 'update': var rows = parent.rcmail.filters_list.rows; for (var i=0; i<rows.length; i++) - if (rows[i] && rows[i].uid == id) { - rows[i].obj.cells[0].innerHTML = name; + if (rows[i] && rows[i].uid == id) { + rows[i].obj.cells[0].innerHTML = name; if (disabled) $(rows[i].obj).addClass('disabled'); else $(rows[i].obj).removeClass('disabled'); - break; - } + break; + } break; - + case 'add': var row, new_row, td, list = parent.rcmail.filters_list; - if (!list) + if (!list) break; - for (var i=0; i<list.rows.length; i++) - if (list.rows[i] != null && String(list.rows[i].obj.id).match(/^rcmrow/)) - row = list.rows[i].obj; + for (var i=0; i<list.rows.length; i++) + if (list.rows[i] != null && String(list.rows[i].obj.id).match(/^rcmrow/)) + row = list.rows[i].obj; if (row) { - new_row = parent.document.createElement('tr'); - new_row.id = 'rcmrow'+id; - td = parent.document.createElement('td'); - new_row.appendChild(td); - list.insert_row(new_row, false); + new_row = parent.document.createElement('tr'); + new_row.id = 'rcmrow'+id; + td = parent.document.createElement('td'); + new_row.appendChild(td); + list.insert_row(new_row, false); if (disabled) $(new_row).addClass('disabled'); - if (row.cells[0].className) - td.className = row.cells[0].className; - - td.innerHTML = name; - list.highlight_row(id); + if (row.cells[0].className) + td.className = row.cells[0].className; + + td.innerHTML = name; + list.highlight_row(id); - parent.rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', true); - } + parent.rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', true); + } else // refresh whole page - parent.rcmail.goto_url('plugin.managesieve'); - break; + parent.rcmail.goto_url('plugin.managesieve'); + break; } this.set_busy(false); @@ -227,8 +226,8 @@ rcube_webmail.prototype.managesieve_save = function() { if (parent.rcmail && parent.rcmail.filters_list && this.gui_objects.sieveform.name != 'filtersetform') { var id = parent.rcmail.filters_list.get_single_selection(); - if (id != null) - this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid; + if (id != null) + this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid; } this.gui_objects.sieveform.submit(); }; @@ -260,26 +259,26 @@ rcube_webmail.prototype.filters_listbuttons = function() if (rows[i] == null) { // removed row } else if (i == id) { - this.enable_command('plugin.managesieve-up', false); - break; + this.enable_command('plugin.managesieve-up', false); + break; } else { - this.enable_command('plugin.managesieve-up', true); - break; + this.enable_command('plugin.managesieve-up', true); + break; } } for (var i=rows.length-1; i>0; 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'); + // 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'; + 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; i<div.childNodes.length; i++) { if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow') + after) - break; + break; } if (div.childNodes[i+1]) @@ -366,26 +365,26 @@ rcube_webmail.prototype.managesieve_formbuttons = function(div) // count and get buttons for (i=0; i<div.childNodes.length; i++) { - if (div.id == 'rules' && div.childNodes[i].id) { - if (/rulerow/.test(div.childNodes[i].id)) - buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, '')); - } - else if (div.childNodes[i].id) { - if (/actionrow/.test(div.childNodes[i].id)) - buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, '')); - } + if (div.id == 'rules' && div.childNodes[i].id) { + if (/rulerow/.test(div.childNodes[i].id)) + buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, '')); + } + else if (div.childNodes[i].id) { + if (/actionrow/.test(div.childNodes[i].id)) + buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, '')); + } } for (i=0; i<buttons.length; i++) { - 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); - } + 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<opts.length; x++) { if (opts[x].value != rcmail.env.active_set && opts[x].innerHTML.match(regx)) opts[x].innerHTML = opts[x].innerHTML.replace(regx, ''); - else if (opts[x].value == rcmail.env.active_set) - opts[x].innerHTML = opts[x].innerHTML + label; + else if (opts[x].value == rcmail.env.active_set) + opts[x].innerHTML = opts[x].innerHTML + label; } // change title of setact button @@ -472,3 +471,4 @@ rcube_webmail.prototype.managesieve_reload = function(set) + (rcmail.env.reload_set ? '&_set=' + rcmail.env.reload_set : '') }, 500); }; + diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php index 0d9042eff..9257e586b 100644 --- a/plugins/managesieve/managesieve.php +++ b/plugins/managesieve/managesieve.php @@ -7,7 +7,7 @@ * It's clickable interface which operates on text scripts and communicates * with server using managesieve protocol. Adds Filters tab in Settings. * - * @version 2.4 + * @version 2.5 * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl> * * Configuration (see config.inc.php.dist) @@ -17,1112 +17,1059 @@ class managesieve extends rcube_plugin { - public $task = 'settings'; + 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', - ); + 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')); + function init() + { + // add Tab label/title + $this->add_texts('localization/', array('filters','managefilters')); - // include main js script - $this->include_script('managesieve.js'); - } - - function managesieve_start() - { - $this->rc = rcmail::get_instance(); - $this->load_config(); + // register actions + $this->register_action('plugin.managesieve', array($this, 'managesieve_actions')); + $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save')); - // 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'), - )); + // include main js script + $this->include_script('managesieve.js'); + } - require_once($this->home . '/lib/Net/Sieve.php'); - require_once($this->home . '/lib/rcube_sieve.php'); + function managesieve_start() + { + $this->rc = rcmail::get_instance(); + $this->load_config(); - $host = str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')); - $port = $this->rc->config->get('managesieve_port', 2000); + // 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'), + )); - // 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) - ); + require_once($this->home . '/lib/Net/Sieve.php'); + require_once($this->home . '/lib/rcube_sieve.php'); - 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); + $host = str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')); + $port = $this->rc->config->get('managesieve_port', 2000); - // 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; - } - } + // 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 ($script_name) - $this->sieve->load($script_name); + if (!($error = $this->sieve->error())) { - $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; - } + $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(); + } - raise_error(array('code' => 403, 'type' => 'php', 'message' => "Unable to connect to managesieve on $host:$port"), true, false); + // 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; + } - // to disable 'Add filter' button set env variable - $this->rc->output->set_env('filterconnerror', true); - $this->script = array(); + 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; + } + + return $error; } - else + + function managesieve_actions() { - $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; - } + // Init plugin and handle managesieve connection + $error = $this->managesieve_start(); - function managesieve_actions() - { - // 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); - // 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 ($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) { +// $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.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 ($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 (PEAR::isError($script)) - exit; + 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); - $browser = new rcube_browser; - - // send download headers - header("Content-Type: application/octet-stream"); - header("Content-Length: ".strlen($script)); + 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 ($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); + 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); - $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); - } + 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); - $this->rc->output->send(); - } + if (PEAR::isError($script)) + exit; - $this->managesieve_send(); - } + $browser = new rcube_browser; - function managesieve_save() - { - // Init plugin and handle managesieve connection - $error = $this->managesieve_start(); + // send download headers + header("Content-Type: application/octet-stream"); + header("Content-Length: ".strlen($script)); - // 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 ($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, '\\"'); - 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'; - } + 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 (!$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'); - } + $this->managesieve_send(); } - // filter add/edit action - else if (isset($_POST['_name'])) + + function managesieve_save() { - $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']; + // 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; + } - // we need a "hack" for radiobuttons - foreach ($sizeitems as $item) - $items[] = $item; + $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]); - $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(); + // 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 ($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]); + 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; - // 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 ($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]); - 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; + $this->form['tests'][$i]['test'] = 'size'; + $this->form['tests'][$i]['type'] = $sizeop; + $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem; - 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]); + 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]); - $this->form['tests'][$i]['test'] = 'size'; - $this->form['tests'][$i]['type'] = $sizeop; - $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem; + if(preg_match('/^not/', $op)) + $this->form['tests'][$i]['not'] = true; + $type = preg_replace('/^not/', '', $op); - 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 ($cust_header == '') + $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty'); + else { + $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY); - if(preg_match('/^not/', $op)) - $this->form['tests'][$i]['not'] = true; - $type = preg_replace('/^not/', '', $op); + 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 ($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 (empty($this->errors['tests'][$i]['header'])) + $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; - 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 ($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['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty'); - break; - case 'redirect': - $this->form['actions'][$i]['target'] = $target; + if ($target == '') + $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty'); + } + break; + } + $i++; + } + } - 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]); + $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 - 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->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->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 ($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; + } - if ($fid !== false) - $save = $this->sieve->save(); + $i++; + } - 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(); - } - } - } + 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(); - $this->managesieve_send(); - } + 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(); + } + } + } + + $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'); + 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 HTML table - function filters_list($attrib) - { - // 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' : '', - ); + // return the filters list as HTML table + function filters_list($attrib) + { + // add id to message list table if not specified + if (!strlen($attrib['id'])) + $attrib['id'] = 'rcmfilterslist'; - // create XHTML table - $out = rcube_table_output($attrib, $result, $a_show_cols, 'id'); + // define list of cols to be displayed + $a_show_cols = array('managesieve.filtername'); - // 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'); - - return $out; - } + foreach($this->script as $idx => $filter) + $result[] = array( + 'managesieve.filtername' => $filter['name'], + 'id' => $idx, + 'class' => $filter['disabled'] ? 'disabled' : '', + ); - // return the filters list as <SELECT> - function filtersets_list($attrib) - { - // add id to message list table if not specified - if (!strlen($attrib['id'])) - $attrib['id'] = 'rcmfiltersetslist'; + // create XHTML table + $out = rcube_table_output($attrib, $result, $a_show_cols, 'id'); - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - - $select = new html_select(array('name' => '_set', 'id' => $attrib['id'], 'onchange' => 'rcmail.managesieve_set()')); + // set client env + $this->rc->output->add_gui_object('filterslist', $attrib['id']); + $this->rc->output->include_script('list.js'); - if ($list) { - asort($list, SORT_LOCALE_STRING); + // add some labels to client + $this->rc->output->add_label('managesieve.filterdeleteconfirm'); - foreach($list as $set) - $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); + return $out; } - - $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']; + // return the filters list as <SELECT> + function filtersets_list($attrib) + { + // add id to message list table if not specified + if (!strlen($attrib['id'])) + $attrib['id'] = 'rcmfiltersetslist'; - $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'); + $list = $this->sieve->get_scripts(); + $active = $this->sieve->get_active(); - return html::tag('iframe', $attrib); - } + $select = new html_select(array('name' => '_set', 'id' => $attrib['id'], + 'onchange' => 'rcmail.managesieve_set()')); + if ($list) { + asort($list, SORT_LOCALE_STRING); - function filterset_form($attrib) - { - if (!$attrib['id']) - $attrib['id'] = 'rcmfiltersetform'; + foreach ($list as $set) + $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); + } - $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n"; + $out = $select->show($this->sieve->current); - $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)); + // 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' + ); - $out .= $hiddenfields->show(); + return $out; + } - $name = get_input_value('_name', RCUBE_INPUT_POST); - $copy = get_input_value('_copy', RCUBE_INPUT_POST); - $selected = get_input_value('_from', RCUBE_INPUT_POST); + function filter_frame($attrib) + { + if (!$attrib['id']) + $attrib['id'] = 'rcmfilterframe'; - $table = new html_table(array('cols' => 2)); + $attrib['name'] = $attrib['id']; - // filter set name input - $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30, - 'class' => ($this->errors['name'] ? 'error' : ''))); + $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'); - $table->add('title', sprintf('<label for="%s"><b>%s:</b></label>', '_name', Q($this->gettext('filtersetname')))); - $table->add(null, $input_name->show($name)); + return html::tag('iframe', $attrib); + } - $from ='<div class="itemlist">'; - $from .= '<input type="radio" id="from_none" name="_from" value="none"' - .(!$selected || $selected=='none' ? ' checked="checked"' : '').'></input>'; - $from .= sprintf('<label for="%s">%s</label> ', 'from_none', Q($this->gettext('none'))); + function filterset_form($attrib) + { + if (!$attrib['id']) + $attrib['id'] = 'rcmfiltersetform'; - // filters set list - $list = $this->sieve->get_scripts(); - $active = $this->sieve->get_active(); - - $select = new html_select(array('name' => '_copy', 'id' => '_copy')); + $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n"; - asort($list, SORT_LOCALE_STRING); + $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)); - foreach($list as $set) - $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); - - $from .= '<br /><input type="radio" id="from_set" name="_from" value="set"' - .($selected=='set' ? ' checked="checked"' : '').'></input>'; - $from .= sprintf('<label for="%s">%s:</label> ', 'from_set', Q($this->gettext('fromset'))); - $from .= $select->show($copy); + $out .= $hiddenfields->show(); - // script upload box - $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30, - 'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : ''))); + $name = get_input_value('_name', RCUBE_INPUT_POST); + $copy = get_input_value('_copy', RCUBE_INPUT_POST); + $selected = get_input_value('_from', RCUBE_INPUT_POST); - $from .= '<br /><input type="radio" id="from_file" name="_from" value="file"' - .($selected=='file' ? ' checked="checked"' : '').'></input>'; - $from .= sprintf('<label for="%s">%s:</label> ', 'from_file', Q($this->gettext('fromfile'))); - $from .= $upload->show(); - $from .= '</div>'; + $table = new html_table(array('cols' => 2)); - $table->add('title', '<label>'.$this->gettext('filters').':</label>'); - $table->add(null, $from); + // filter set name input + $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30, + 'class' => ($this->errors['name'] ? 'error' : ''))); - $out .= $table->show(); - - $this->rc->output->add_gui_object('sieveform', 'filtersetform'); + $table->add('title', sprintf('<label for="%s"><b>%s:</b></label>', + '_name', Q($this->gettext('filtersetname')))); + $table->add(null, $input_name->show($name)); - return $out; - } + $from ='<div class="itemlist">'; + $from .= '<input type="radio" id="from_none" name="_from" value="none"' + .(!$selected || $selected=='none' ? ' checked="checked"' : '').'></input>'; + $from .= sprintf('<label for="%s">%s</label> ', 'from_none', Q($this->gettext('none'))); + // filters set list + $list = $this->sieve->get_scripts(); + $active = $this->sieve->get_active(); - function filter_form($attrib) - { - if (!$attrib['id']) - $attrib['id'] = 'rcmfilterform'; + $select = new html_select(array('name' => '_copy', 'id' => '_copy')); - $fid = get_input_value('_fid', RCUBE_INPUT_GPC); - $scr = isset($this->form) ? $this->form : $this->script[$fid]; + if (is_array($list)) { + asort($list, SORT_LOCALE_STRING); - $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)); + foreach ($list as $set) + $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set); - $out = '<form name="filterform" action="./" method="post">'."\n"; - $out .= $hiddenfields->show(); + $from .= '<br /><input type="radio" id="from_set" name="_from" value="set"' + .($selected=='set' ? ' checked="checked"' : '').'></input>'; + $from .= sprintf('<label for="%s">%s:</label> ', 'from_set', Q($this->gettext('fromset'))); + $from .= $select->show($copy); + } - // 'any' flag - if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) - $any = true; + // script upload box + $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30, + 'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : ''))); - // filter name input - $field_id = '_name'; - $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30, - 'class' => ($this->errors['name'] ? 'error' : ''))); + $from .= '<br /><input type="radio" id="from_file" name="_from" value="file"' + .($selected=='file' ? ' checked="checked"' : '').'></input>'; + $from .= sprintf('<label for="%s">%s:</label> ', 'from_file', Q($this->gettext('fromfile'))); + $from .= $upload->show(); + $from .= '</div>'; - if (isset($scr)) - $input_name = $input_name->show($scr['name']); - else - $input_name = $input_name->show(); + $table->add('title', '<label>'.$this->gettext('filters').':</label>'); + $table->add(null, $from); - $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s<br /><br />\n", - $field_id, Q($this->gettext('filtername')), $input_name); + $out .= $table->show(); - $out .= '<fieldset><legend>' . Q($this->gettext('messagesrules')) . "</legend>\n"; + $this->rc->output->add_gui_object('sieveform', 'filtersetform'); - // 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')); + return $out; + } - if (isset($scr) && !$any) - $input_join = $input_join->show($scr['join'] ? 'allof' : ''); - else - $input_join = $input_join->show(); - $out .= sprintf("%s<label for=\"%s\">%s</label> \n", - $input_join, $field_id, Q($this->gettext('filterallof'))); + function filter_form($attrib) + { + if (!$attrib['id']) + $attrib['id'] = 'rcmfilterform'; - $field_id = '_anyof'; - $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof', - 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio')); + $fid = get_input_value('_fid', RCUBE_INPUT_GPC); + $scr = isset($this->form) ? $this->form : $this->script[$fid]; - if (isset($scr) && !$any) - $input_join = $input_join->show($scr['join'] ? '' : 'anyof'); - else - $input_join = $input_join->show('anyof'); // default + $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("%s<label for=\"%s\">%s</label>\n", - $input_join, $field_id, Q($this->gettext('filteranyof'))); + $out = '<form name="filterform" action="./" method="post">'."\n"; + $out .= $hiddenfields->show(); - $field_id = '_any'; - $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any', - 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio')); + // 'any' flag + if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not']) + $any = true; - $input_join = $input_join->show($any ? 'any' : ''); + // filter name input + $field_id = '_name'; + $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30, + 'class' => ($this->errors['name'] ? 'error' : ''))); - $out .= sprintf("%s<label for=\"%s\">%s</label>\n", - $input_join, $field_id, Q($this->gettext('filterany'))); + if (isset($scr)) + $input_name = $input_name->show($scr['name']); + else + $input_name = $input_name->show(); - $rows_num = isset($scr) ? sizeof($scr['tests']) : 1; + $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s<br /><br />\n", + $field_id, Q($this->gettext('filtername')), $input_name); - $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>'; - for ($x=0; $x<$rows_num; $x++) - $out .= $this->rule_div($fid, $x); - $out .= "</div>\n"; + $out .= '<fieldset><legend>' . Q($this->gettext('messagesrules')) . "</legend>\n"; - $out .= "</fieldset>\n"; + // 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')); - // actions - $out .= '<fieldset><legend>' . Q($this->gettext('messagesactions')) . "</legend>\n"; + if (isset($scr) && !$any) + $input_join = $input_join->show($scr['join'] ? 'allof' : ''); + else + $input_join = $input_join->show(); - $rows_num = isset($scr) ? sizeof($scr['actions']) : 1; + $out .= sprintf("%s<label for=\"%s\">%s</label> \n", + $input_join, $field_id, Q($this->gettext('filterallof'))); - $out .= '<div id="actions">'; - for ($x=0; $x<$rows_num; $x++) - $out .= $this->action_div($fid, $x); - $out .= "</div>\n"; + $field_id = '_anyof'; + $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof', + 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio')); - $out .= "</fieldset>\n"; - - if ($scr['disabled']) { - $this->rc->output->set_env('rule_disabled', true); - } - $this->rc->output->add_label('managesieve.ruledeleteconfirm'); - $this->rc->output->add_label('managesieve.actiondeleteconfirm'); - $this->rc->output->add_gui_object('sieveform', 'filterform'); + if (isset($scr) && !$any) + $input_join = $input_join->show($scr['join'] ? '' : 'anyof'); + else + $input_join = $input_join->show('anyof'); // default - return $out; - } + $out .= sprintf("%s<label for=\"%s\">%s</label>\n", + $input_join, $field_id, Q($this->gettext('filteranyof'))); - 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 ? '<div class="rulerow" id="rulerow' .$id .'">'."\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 .= '<table><tr><td class="rowactions">'; + $input_join = $input_join->show($any ? 'any' : ''); - // 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('...')), '...'); + $out .= sprintf("%s<label for=\"%s\">%s</label>\n", + $input_join, $field_id, Q($this->gettext('filterany'))); - // TODO: list arguments + $rows_num = isset($scr) ? sizeof($scr['tests']) : 1; - 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 .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>'; + for ($x=0; $x<$rows_num; $x++) + $out .= $this->rule_div($fid, $x); + $out .= "</div>\n"; - $out .= '</td><td class="rowtargets">'; + $out .= "</fieldset>\n"; - 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 .= '<div id="custom_header' .$id. '" style="display:' .(isset($custom) ? 'inline' : 'none'). '"> - <input type="text" name="_custom_header[]" '. $this->error_class($id, 'test', 'header') - .' value="' .Q($custom). '" size="20" /> </div>' . "\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'); + // actions + $out .= '<fieldset><legend>' . Q($this->gettext('messagesactions')) . "</legend>\n"; - // target input (TODO: lists) + $rows_num = isset($scr) ? sizeof($scr['actions']) : 1; - if ($rule['test'] == 'header') - { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']); - $target = $rule['arg2']; - } - elseif ($rule['test'] == 'size') - { - $out .= $select_op->show(); - if(preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches)) - { - $sizetarget = $matches[1]; - $sizeitem = $matches[2]; - } + $out .= '<div id="actions">'; + for ($x=0; $x<$rows_num; $x++) + $out .= $this->action_div($fid, $x); + $out .= "</div>\n"; + + $out .= "</fieldset>\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; } - else + + function rule_div($fid, $id, $div=true) { - $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']); - $target = ''; - } + $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 ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : ''; - $out .= '<input type="text" name="_rule_target[]" id="rule_target' .$id. '" - value="' .Q($target). '" size="20" ' . $this->error_class($id, 'test', 'target') - . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n"; + $out .= '<table><tr><td class="rowactions">'; - $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'); + // 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('...')), '...'); - $out .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">'; - $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); - $out .= '<input type="text" name="_rule_size_target[]" value="'.$sizetarget.'" size="10" ' . $this->error_class($id, 'test', 'sizetarget') .' /> - <input type="radio" name="_rule_size_item['.$id.']" value=""'. (!$sizeitem ? ' checked="checked"' : '') .' class="radio" />B - <input type="radio" name="_rule_size_item['.$id.']" value="K"'. ($sizeitem=='K' ? ' checked="checked"' : '') .' class="radio" />kB - <input type="radio" name="_rule_size_item['.$id.']" value="M"'. ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />MB - <input type="radio" name="_rule_size_item['.$id.']" value="G"'. ($sizeitem=='G' ? ' checked="checked"' : '') .' class="radio" />GB'; - $out .= '</div>'; - $out .= '</td>'; - - // add/del buttons - $out .= '<td class="rowbuttons">'; - $out .= '<input type="button" id="ruleadd' . $id .'" value="'. Q($this->gettext('add')). '" - onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button" /> '; - $out .= '<input type="button" id="ruledel' . $id .'" value="'. Q($this->gettext('del')). '" - onclick="rcmail.managesieve_ruledel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"' - . ($rows_num<2 ? ' disabled="disabled"' : '') .' />'; - $out .= '</td></tr></table>'; + // TODO: list arguments - $out .= $div ? "</div>\n" : ''; - - return $out; - } + 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(); - 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 .= '</td><td class="rowtargets">'; - $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : ''; + 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 .= '<table><tr><td class="rowactions">'; + $out .= '<div id="custom_header' .$id. '" style="display:' .(isset($custom) ? 'inline' : 'none'). '"> + <input type="text" name="_custom_header[]" '. $this->error_class($id, 'test', 'header') + .' value="' .Q($custom). '" size="20" /> </div>' . "\n"; - // 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'); + // 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'); - $out .= $select_action->show($action['type']); - $out .= '</td>'; + // target input (TODO: lists) - // actions target inputs - $out .= '<td class="rowtargets">'; - // shared targets - $out .= '<input type="text" name="_action_target[]" id="action_target' .$id. '" ' - .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="40" ' - .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" ' - . $this->error_class($id, 'action', 'target') .' />'; - $out .= '<textarea name="_action_target_area[]" id="action_target_area' .$id. '" ' - .'rows="3" cols="40" '. $this->error_class($id, 'action', 'targetarea') - .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">' - . (in_array($action['type'], array('reject', 'ereject')) ? Q($action['target'], 'strict', false) : '') - . "</textarea>\n"; + 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 = ''; + } - // vacation - $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">'; - $out .= '<span class="label">'. Q($this->gettext('vacationreason')) .'</span><br />' - .'<textarea name="_action_reason[]" id="action_reason' .$id. '" ' - .'rows="3" cols="40" '. $this->error_class($id, 'action', 'reason') . '>' - . Q($action['reason'], 'strict', false) . "</textarea>\n"; - $out .= '<br /><span class="label">' .Q($this->gettext('vacationaddresses')) . '</span><br />' - .'<input type="text" name="_action_addresses[]" ' - .'value="' . (is_array($action['addresses']) ? Q(implode(', ', $action['addresses']), 'strict', false) : $action['addresses']) . '" size="40" ' - . $this->error_class($id, 'action', 'addresses') .' />'; - $out .= '<br /><span class="label">' . Q($this->gettext('vacationdays')) . '</span><br />' - .'<input type="text" name="_action_days[]" ' - .'value="' .Q($action['days'], 'strict', false) . '" size="2" ' - . $this->error_class($id, 'action', 'days') .' />'; - $out .= '</div>'; + $out .= '<input type="text" name="_rule_target[]" id="rule_target' .$id. '" + value="' .Q($target). '" size="20" ' . $this->error_class($id, 'test', 'target') + . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n"; - // mailbox select - $out .= '<select id="action_mailbox' .$id. '" name="_action_mailbox[]" style="display:' - .(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none'). '">'; + $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'); - $this->rc->imap_connect(); + $out .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">'; + $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); + $out .= '<input type="text" name="_rule_size_target[]" value="'.$sizetarget.'" size="10" ' + . $this->error_class($id, 'test', 'sizetarget') .' /> + <input type="radio" name="_rule_size_item['.$id.']" value=""'. (!$sizeitem ? ' checked="checked"' : '') .' class="radio" />B + <input type="radio" name="_rule_size_item['.$id.']" value="K"'. ($sizeitem=='K' ? ' checked="checked"' : '') .' class="radio" />kB + <input type="radio" name="_rule_size_item['.$id.']" value="M"'. ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />MB + <input type="radio" name="_rule_size_item['.$id.']" value="G"'. ($sizeitem=='G' ? ' checked="checked"' : '') .' class="radio" />GB'; + $out .= '</div>'; + $out .= '</td>'; - $a_folders = $this->rc->imap->list_mailboxes(); - $delimiter = $this->rc->imap->get_hierarchy_delimiter(); + // add/del buttons + $out .= '<td class="rowbuttons">'; + $out .= '<input type="button" id="ruleadd' . $id .'" value="'. Q($this->gettext('add')). '" + onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button" /> '; + $out .= '<input type="button" id="ruledel' . $id .'" value="'. Q($this->gettext('del')). '" + onclick="rcmail.managesieve_ruledel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"' + . ($rows_num<2 ? ' disabled="disabled"' : '') .' />'; + $out .= '</td></tr></table>'; - // set mbox encoding - $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP'); + $out .= $div ? "</div>\n" : ''; - if ($action['type'] == 'fileinto') - $mailbox = $action['target']; - else - $mailbox = ''; + return $out; + } - foreach ($a_folders as $folder) + function action_div($fid, $id, $div=true) { - $utf7folder = $this->rc->imap->mod_mailbox($folder); - $names = explode($delimiter, rcube_charset_convert($folder, 'UTF7-IMAP')); - $name = $names[sizeof($names)-1]; - - if ($replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter')) - $utf7folder = str_replace($delimiter, $replace_delimiter, $utf7folder); - - // convert to Sieve implementation encoding - $utf7folder = $this->mbox_encode($utf7folder, $mbox_encoding); - - if ($folder_class = rcmail_folder_classname($name)) - $foldername = $this->gettext($folder_class); - else - $foldername = $name; + $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 .= sprintf('<option value="%s"%s>%s%s</option>'."\n", - htmlspecialchars($utf7folder), - ($mailbox == $utf7folder ? ' selected="selected"' : ''), - str_repeat(' ', 4 * (sizeof($names)-1)), - Q(abbreviate_string($foldername, 40 - (2 * sizeof($names)-1)))); - } - $out .= '</select>'; - $out .= '</td>'; + $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : ''; + + $out .= '<table><tr><td class="rowactions">'; - // add/del buttons - $out .= '<td class="rowbuttons">'; - $out .= '<input type="button" id="actionadd' . $id .'" value="'. Q($this->gettext('add')). '" - onclick="rcmail.managesieve_actionadd(' . $id .')" class="button" /> '; - $out .= '<input type="button" id="actiondel' . $id .'" value="'. Q($this->gettext('del')). '" - onclick="rcmail.managesieve_actiondel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"' - . ($rows_num<2 ? ' disabled="disabled"' : '') .' />'; - $out .= '</td>'; - - $out .= '</tr></table>'; + // 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 .= $div ? "</div>\n" : ''; + $out .= $select_action->show($action['type']); + $out .= '</td>'; - return $out; - } + // actions target inputs + $out .= '<td class="rowtargets">'; + // shared targets + $out .= '<input type="text" name="_action_target[]" id="action_target' .$id. '" ' + .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="40" ' + .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" ' + . $this->error_class($id, 'action', 'target') .' />'; + $out .= '<textarea name="_action_target_area[]" id="action_target_area' .$id. '" ' + .'rows="3" cols="40" '. $this->error_class($id, 'action', 'targetarea') + .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">' + . (in_array($action['type'], array('reject', 'ereject')) ? Q($action['target'], 'strict', false) : '') + . "</textarea>\n"; - private function genid() - { - $result = intval(rcube_timer()); - return $result; - } + // vacation + $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">'; + $out .= '<span class="label">'. Q($this->gettext('vacationreason')) .'</span><br />' + .'<textarea name="_action_reason[]" id="action_reason' .$id. '" ' + .'rows="3" cols="40" '. $this->error_class($id, 'action', 'reason') . '>' + . Q($action['reason'], 'strict', false) . "</textarea>\n"; + $out .= '<br /><span class="label">' .Q($this->gettext('vacationaddresses')) . '</span><br />' + .'<input type="text" name="_action_addresses[]" ' + .'value="' . (is_array($action['addresses']) ? Q(implode(', ', $action['addresses']), 'strict', false) : $action['addresses']) . '" size="40" ' + . $this->error_class($id, 'action', 'addresses') .' />'; + $out .= '<br /><span class="label">' . Q($this->gettext('vacationdays')) . '</span><br />' + .'<input type="text" name="_action_days[]" ' + .'value="' .Q($action['days'], 'strict', false) . '" size="2" ' + . $this->error_class($id, 'action', 'days') .' />'; + $out .= '</div>'; - private function strip_value($str, $allow_html=false) - { - if (!$allow_html) - $str = strip_tags($str); - - return trim($str); - } + // mailbox select + $out .= '<select id="action_mailbox' .$id. '" name="_action_mailbox[]" style="display:' + .(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none'). '">'; - 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"'); + $this->rc->imap_connect(); - return ''; - } + $a_folders = $this->rc->imap->list_mailboxes(); + $delimiter = $this->rc->imap->get_hierarchy_delimiter(); - private function check_email($email) - { - if (function_exists('check_email')); - return check_email($email); + // set mbox encoding + $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP'); - // Check for invalid characters - if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) - return false; + if ($action['type'] == 'fileinto') + $mailbox = $action['target']; + else + $mailbox = ''; - // Check that there's one @ symbol, and that the lengths are right - if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $email)) - return false; + foreach ($a_folders as $folder) { + $utf7folder = $this->rc->imap->mod_mailbox($folder); + $names = explode($delimiter, rcube_charset_convert($folder, 'UTF7-IMAP')); + $name = $names[sizeof($names)-1]; - // Split it into sections to make life easier - $email_array = explode('@', $email); + if ($replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter')) + $utf7folder = str_replace($delimiter, $replace_delimiter, $utf7folder); - // 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; + // convert to Sieve implementation encoding + $utf7folder = $this->mbox_encode($utf7folder, $mbox_encoding); - // 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 + if ($folder_class = rcmail_folder_classname($name)) + $foldername = $this->gettext($folder_class); + else + $foldername = $name; - 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; + $out .= sprintf('<option value="%s"%s>%s%s</option>'."\n", + htmlspecialchars($utf7folder), + ($mailbox == $utf7folder ? ' selected="selected"' : ''), + str_repeat(' ', 4 * (sizeof($names)-1)), + Q(abbreviate_string($foldername, 40 - (2 * sizeof($names)-1)))); + } + $out .= '</select>'; + $out .= '</td>'; + + // add/del buttons + $out .= '<td class="rowbuttons">'; + $out .= '<input type="button" id="actionadd' . $id .'" value="'. Q($this->gettext('add')). '" + onclick="rcmail.managesieve_actionadd(' . $id .')" class="button" /> '; + $out .= '<input type="button" id="actiondel' . $id .'" value="'. Q($this->gettext('del')). '" + onclick="rcmail.managesieve_actiondel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"' + . ($rows_num<2 ? ' disabled="disabled"' : '') .' />'; + $out .= '</td>'; + + $out .= '</tr></table>'; + + $out .= $div ? "</div>\n" : ''; + + return $out; + } - return true; + 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"'); + else if ($type == 'action' && isset($this->errors['actions'][$id][$target])) + return ($name_only ? 'error' : ' class="error"'); + + return ''; + } + + 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); - } } ?> |
