From 39cceb2f89a26a69db16c615c96885c238c4ba67 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 10 Nov 2016 12:05:28 +0100 Subject: [PATCH] Managesieve: Various improvement to raw script editor --- CHANGELOG | 1 + plugins/managesieve/Changelog | 1 + plugins/managesieve/config.inc.php.dist | 3 + .../lib/Roundcube/rcube_sieve_engine.php | 91 ++++++++++--------- plugins/managesieve/managesieve.js | 73 +++++++++------ plugins/managesieve/managesieve.php | 13 ++- .../skins/classic/templates/managesieve.html | 4 +- .../skins/larry/templates/managesieve.html | 2 + .../skins/larry/templates/seteditraw.html | 13 +-- 9 files changed, 117 insertions(+), 84 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index de90b8863..39be19cce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -51,6 +51,7 @@ CHANGELOG Roundcube Webmail - Indicate that a collapsed thread has flagged children (#5013) - Implemented message/rfc822 attachment preview - Update to jsTimezoneDetect 1.0.6 +- Managesieve: Add (optional) RAW script editor (#5414) - Managesieve: Add option to automatically set vacation :from address (#5428) - Managesieve: Support 'string' test from variables extension [RFC 5229] (#5248) - Managesieve: Support 'duplicate' extension [RFC 7352] diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog index adee09261..8280e04d3 100644 --- a/plugins/managesieve/Changelog +++ b/plugins/managesieve/Changelog @@ -2,6 +2,7 @@ - Fix regression where js error is thrown if server does not support 'duplicate' extension - Add option to automatically set vacation :from address (#5428) - Support per-host managesieve_conn_options (#5136) +- Added (optional) RAW script editor (#5414) * version 8.7 [2016-07-19] ----------------------------------------------------------- diff --git a/plugins/managesieve/config.inc.php.dist b/plugins/managesieve/config.inc.php.dist index a7315d508..96fffb3f2 100644 --- a/plugins/managesieve/config.inc.php.dist +++ b/plugins/managesieve/config.inc.php.dist @@ -103,3 +103,6 @@ $config['managesieve_vacation_from_init'] = false; // Supported methods of notify extension. Default: 'mailto' $config['managesieve_notify_methods'] = array('mailto'); + +// Enables scripts RAW editor feature +$config['managesieve_raw_editor'] = true; diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php index 452f9a24d..0f3f047ea 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php @@ -146,6 +146,8 @@ class rcube_sieve_engine $_SESSION['managesieve_current'] = $this->sieve->current; } + $this->rc->output->set_env('raw_sieve_editor', $this->rc->config->get('managesieve_raw_editor', true)); + return $error; } @@ -488,7 +490,7 @@ class rcube_sieve_engine ); } } - + $this->send(); } @@ -496,22 +498,22 @@ class rcube_sieve_engine { // Init plugin and handle managesieve connection $error = $this->start(); - - $this->rc->request_security_check(rcube_utils::INPUT_POST); - + $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST); - + $result = $this->sieve->save_script($script_name, $_POST['rawsetcontent']); - - if ($result == true) { - $this->rc->output->show_message('managesieve.setupdated', 'confirmation'); - $this->send(); - } else { + + if ($result === false) { $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); - $this->send(); } + else { + $this->rc->output->show_message('managesieve.setupdated', 'confirmation'); + $this->rc->output->command('parent.managesieve_updatelist', 'refresh'); + } + + $this->send(); } - + function save() { // Init plugin and handle managesieve connection @@ -1142,8 +1144,9 @@ class rcube_sieve_engine $fid = $this->sieve->script->update_rule($fid, $this->form); } - if ($fid !== false) + if ($fid !== false) { $save = $this->save_script(); + } if ($save && $fid !== false) { $this->rc->output->show_message('managesieve.filtersaved', 'confirmation'); @@ -1163,7 +1166,6 @@ class rcube_sieve_engine } else { $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); -// $this->rc->output->send(); } } else { @@ -1223,8 +1225,9 @@ class rcube_sieve_engine function filtersets_list($attrib, $no_env = false) { // add id to message list table if not specified - if (!strlen($attrib['id'])) + if (!strlen($attrib['id'])) { $attrib['id'] = 'rcmfiltersetslist'; + } $list = $this->list_scripts(); @@ -1278,37 +1281,39 @@ class rcube_sieve_engine { return $this->rc->output->frame($attrib, true); } - - function filterset_editraw($attrib) + + function filterset_editraw($attrib) { - $scriptName = isset($_GET['_set']) ? $_GET['_set'] : $_POST['_set']; - $script = $this->sieve->get_script($scriptName); - - if ($script != false) { - $out = '
'."\n"; - - $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task)); - $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-saveraw')); - $hiddenfields->add(array('name' => '_set', 'value' => $scriptName)); - $hiddenfields->add(array('name' => '_seteditraw', 'value' => 1)); - $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); - - $out .= $hiddenfields->show(); - - $txtarea = new html_textarea(array( - 'id' => 'rawfiltersettxt', - 'name' => 'rawsetcontent', - 'rows' => '15' - )); - $out .= $txtarea->show($script); - //$out .= ''; - - $this->rc->output->add_gui_object('sievesetrawform', 'filtersetrawform'); - return $out; - } else { + $script_name = isset($_GET['_set']) ? $_GET['_set'] : $_POST['_set']; + $script = $this->sieve->get_script($script_name); + $script_post = $_POST['rawsetcontent']; + + $out = ''."\n"; + + $hiddenfields = new html_hiddenfield(); + $hiddenfields->add(array('name' => '_task', 'value' => $this->rc->task)); + $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-saveraw')); + $hiddenfields->add(array('name' => '_set', 'value' => $script_name)); + $hiddenfields->add(array('name' => '_seteditraw', 'value' => 1)); + $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0))); + + $out .= $hiddenfields->show(); + + $txtarea = new html_textarea(array( + 'id' => 'rawfiltersettxt', + 'name' => 'rawsetcontent', + 'rows' => '15' + )); + + $out .= $txtarea->show($script_post !== null ? $script_post : ($script !== false ? $script : '')); + + $this->rc->output->add_gui_object('sievesetrawform', 'filtersetrawform'); + + if ($script === false) { $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); - return ''; } + + return $out; } function filterset_form($attrib) diff --git a/plugins/managesieve/managesieve.js b/plugins/managesieve/managesieve.js index 33c4d320e..2ab8df4b2 100644 --- a/plugins/managesieve/managesieve.js +++ b/plugins/managesieve/managesieve.js @@ -91,9 +91,9 @@ if (window.rcmail) { setcnt = rcmail.filtersets_list.rowcount; rcmail.enable_command('plugin.managesieve-set', true); - rcmail.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', setcnt); + rcmail.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', setcnt > 0); rcmail.enable_command('plugin.managesieve-setdel', setcnt > 1); - rcmail.enable_command('plugin.managesieve-seteditraw', true); + rcmail.enable_command('plugin.managesieve-seteditraw', setcnt > 0 && rcmail.env.raw_sieve_editor); // Fix dragging filters over sets list $('tr', rcmail.gui_objects.filtersetslist).each(function (i, e) { rcmail.managesieve_fixdragend(e); }); @@ -111,8 +111,7 @@ if (window.rcmail) { rcube_webmail.prototype.managesieve_add = function() { - this.load_managesieveframe(); - this.filters_list.clear_selection(); + this.load_managesieveframe('', true); }; rcube_webmail.prototype.managesieve_del = function() @@ -138,8 +137,14 @@ rcube_webmail.prototype.managesieve_act = function() rcube_webmail.prototype.managesieve_select = function(list) { var id = list.get_single_selection(); - if (id != null) - this.load_managesieveframe(list.rows[id].uid); + + if (id != null) { + id = list.rows[id].uid; + this.load_managesieveframe('_fid=' + id); + } + + var has_id = typeof(id) != 'undefined' && id != null; + this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', has_id); }; // Set selection @@ -148,7 +153,8 @@ rcube_webmail.prototype.managesieve_setselect = function(list) this.show_contentframe(false); this.filters_list.clear(true); this.enable_command('plugin.managesieve-setdel', list.rowcount > 1); - this.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', true); + this.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', list.rowcount > 0); + this.enable_command('plugin.managesieve-seteditraw', list.rowcount > 0 && this.env.raw_sieve_editor); var id = list.get_single_selection(); if (id != null) @@ -219,24 +225,13 @@ rcube_webmail.prototype.managesieve_seteditraw = function() var id = this.filtersets_list.get_single_selection(), script = this.env.filtersets[id]; - if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) { - var lock = this.set_busy(true, 'loading'); - target = window.frames[this.env.contentframe]; - target.location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_framed=1&_seteditraw=1&_unlock='+lock+'&_set='+urlencode(script); - } + this.load_managesieveframe('_seteditraw=1&_set=' + urlencode(script), true); } // Set add request rcube_webmail.prototype.managesieve_setadd = function() { - this.filters_list.clear_selection(); - this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', false); - - if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) { - var lock = this.set_busy(true, 'loading'); - target = window.frames[this.env.contentframe]; - target.location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_framed=1&_newset=1&_unlock='+lock; - } + this.load_managesieveframe('_newset=1', true); }; rcube_webmail.prototype.managesieve_updatelist = function(action, o) @@ -249,9 +244,8 @@ rcube_webmail.prototype.managesieve_updatelist = function(action, o) var id = o.id, list = this.filters_list; list.remove_row(this.managesieve_rowid(o.id)); - list.clear_selection(); this.show_contentframe(false); - this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false); + this.reset_filters_list(); // filter identifiers changed, fix the list $('tr', this.filters_list.list).each(function() { @@ -388,22 +382,42 @@ rcube_webmail.prototype.managesieve_updatelist = function(action, o) this.managesieve_fixdragend(row); break; + + case 'refresh': + this.reset_filters_list(true); + break; } this.set_busy(false); }; +// Resets filters list state +rcube_webmail.prototype.reset_filters_list = function(reload) +{ + this.filters_list.clear_selection(); + this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', false); + + if (reload) { + var id = this.filtersets_list.get_single_selection(); + + this.filters_list.clear(true); + this.managesieve_list(this.env.filtersets[id]); + } +}; + // load filter frame -rcube_webmail.prototype.load_managesieveframe = function(id) +rcube_webmail.prototype.load_managesieveframe = function(add_url, reset) { - var has_id = typeof(id) != 'undefined' && id != null; - this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', has_id); + if (reset) + this.reset_filters_list(); if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) { + var lock = this.set_busy(true, 'loading'); + target = window.frames[this.env.contentframe]; - var msgid = this.set_busy(true, 'loading'); - target.location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_framed=1' - +(has_id ? '&_fid='+id : '')+'&_unlock='+msgid; + target.location.href = this.env.comm_path + + '&_action=plugin.managesieve-action&_framed=1&_unlock=' + lock + + (add_url ? ('&' + add_url) : ''); } }; @@ -479,8 +493,7 @@ rcube_webmail.prototype.managesieve_save = function() } this.gui_objects.sieveform.submit(); } - - if (this.gui_objects.sievesetrawform) { + else if (this.gui_objects.sievesetrawform) { this.gui_objects.sievesetrawform.submit(); } }; diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php index 43615afa3..d2f1a9c8f 100644 --- a/plugins/managesieve/managesieve.php +++ b/plugins/managesieve/managesieve.php @@ -225,19 +225,24 @@ class managesieve extends rcube_plugin $engine = $this->get_engine(); $engine->save(); } - + /** * Raw form save action handler */ function managesieve_saveraw() { + $engine = $this->get_engine(); + + if (!$this->rc->config->get('managesieve_raw_editor', true)) { + return; + } + // load localization $this->add_texts('localization/', array('filters','managefilters')); - + // include main js script $this->include_script('managesieve.js'); - - $engine = $this->get_engine(); + $engine->saveraw(); } diff --git a/plugins/managesieve/skins/classic/templates/managesieve.html b/plugins/managesieve/skins/classic/templates/managesieve.html index 2f76cfdb7..dbc25fb5f 100644 --- a/plugins/managesieve/skins/classic/templates/managesieve.html +++ b/plugins/managesieve/skins/classic/templates/managesieve.html @@ -64,8 +64,10 @@ diff --git a/plugins/managesieve/skins/larry/templates/managesieve.html b/plugins/managesieve/skins/larry/templates/managesieve.html index b3e1f0f4e..de2c2f4eb 100644 --- a/plugins/managesieve/skins/larry/templates/managesieve.html +++ b/plugins/managesieve/skins/larry/templates/managesieve.html @@ -52,7 +52,9 @@ diff --git a/plugins/managesieve/skins/larry/templates/seteditraw.html b/plugins/managesieve/skins/larry/templates/seteditraw.html index 7e6fa47ac..ba1e91ba5 100644 --- a/plugins/managesieve/skins/larry/templates/seteditraw.html +++ b/plugins/managesieve/skins/larry/templates/seteditraw.html @@ -1,23 +1,24 @@ - - + + <roundcube:object name="pagetitle" /> - +
-

+

-