/plugins/managesieve/managesieve.php
PHP | 2041 lines | 1563 code | 283 blank | 195 comment | 441 complexity | 6051ba91a7f4dbb90a2b58cbc8eea8ff MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Managesieve (Sieve Filters)
- *
- * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
- * It's clickable interface which operates on text scripts and communicates
- * with server using managesieve protocol. Adds Filters tab in Settings.
- *
- * @version @package_version@
- * @author Aleksander Machniak <alec@alec.pl>
- *
- * Configuration (see config.inc.php.dist)
- *
- * Copyright (C) 2008-2012, The Roundcube Dev Team
- * Copyright (C) 2011-2012, Kolab Systems AG
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- class managesieve extends rcube_plugin
- {
- public $task = 'mail|settings';
- private $rc;
- private $sieve;
- private $errors;
- private $form;
- private $tips = array();
- private $script = array();
- private $exts = array();
- private $list;
- private $active = array();
- private $headers = array(
- 'subject' => 'Subject',
- 'from' => 'From',
- 'to' => 'To',
- );
- private $addr_headers = array(
- // Required
- "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to",
- // Additional (RFC 822 / RFC 2822)
- "reply-to", "resent-reply-to", "resent-sender", "resent-cc", "resent-bcc",
- // Non-standard (RFC 2076, draft-palme-mailext-headers-08.txt)
- "for-approval", "for-handling", "for-comment", "apparently-to", "errors-to",
- "delivered-to", "return-receipt-to", "x-admin", "read-receipt-to",
- "x-confirm-reading-to", "return-receipt-requested",
- "registered-mail-reply-requested-by", "mail-followup-to", "mail-reply-to",
- "abuse-reports-to", "x-complaints-to", "x-report-abuse-to",
- // Undocumented
- "x-beenthere",
- );
- const VERSION = '6.0';
- const PROGNAME = 'Roundcube (Managesieve)';
- const PORT = 4190;
- function init()
- {
- $this->rc = rcmail::get_instance();
- // register actions
- $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
- $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
- if ($this->rc->task == 'settings') {
- $this->init_ui();
- }
- else if ($this->rc->task == 'mail') {
- // register message hook
- $this->add_hook('message_headers_output', array($this, 'mail_headers'));
- // inject Create Filter popup stuff
- if (empty($this->rc->action) || $this->rc->action == 'show') {
- $this->mail_task_handler();
- }
- }
- }
- /**
- * Initializes plugin's UI (localization, js script)
- */
- private function init_ui()
- {
- if ($this->ui_initialized)
- return;
- // load localization
- $this->add_texts('localization/', array('filters','managefilters'));
- $this->include_script('managesieve.js');
- $this->ui_initialized = true;
- }
- /**
- * Add UI elements to the 'mailbox view' and 'show message' UI.
- */
- function mail_task_handler()
- {
- // use jQuery for popup window
- $this->require_plugin('jqueryui');
- // include js script and localization
- $this->init_ui();
- // include styles
- $skin_path = $this->local_skin_path();
- if (is_file($this->home . "/$skin_path/managesieve_mail.css")) {
- $this->include_stylesheet("$skin_path/managesieve_mail.css");
- }
- // add 'Create filter' item to message menu
- $this->api->add_content(html::tag('li', null,
- $this->api->output->button(array(
- 'command' => 'managesieve-create',
- 'label' => 'managesieve.filtercreate',
- 'type' => 'link',
- 'classact' => 'icon filterlink active',
- 'class' => 'icon filterlink',
- 'innerclass' => 'icon filterlink',
- ))), 'messagemenu');
- // register some labels/messages
- $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
- 'managesieve.nodata', 'managesieve.nextstep', 'save');
- $this->rc->session->remove('managesieve_current');
- }
- /**
- * Get message headers for popup window
- */
- function mail_headers($args)
- {
- // this hook can be executed many times
- if ($this->mail_headers_done) {
- return $args;
- }
- $this->mail_headers_done = true;
- $headers = $args['headers'];
- $ret = array();
- if ($headers->subject)
- $ret[] = array('Subject', rcube_mime::decode_header($headers->subject));
- // @TODO: List-Id, others?
- foreach (array('From', 'To') as $h) {
- $hl = strtolower($h);
- if ($headers->$hl) {
- $list = rcube_mime::decode_address_list($headers->$hl);
- foreach ($list as $item) {
- if ($item['mailto']) {
- $ret[] = array($h, $item['mailto']);
- }
- }
- }
- }
- if ($this->rc->action == 'preview')
- $this->rc->output->command('parent.set_env', array('sieve_headers' => $ret));
- else
- $this->rc->output->set_env('sieve_headers', $ret);
- return $args;
- }
- /**
- * Loads configuration, initializes plugin (including sieve connection)
- */
- function managesieve_start()
- {
- $this->load_config();
- // register UI objects
- $this->rc->output->add_handlers(array(
- 'filterslist' => array($this, 'filters_list'),
- 'filtersetslist' => array($this, 'filtersets_list'),
- 'filterframe' => array($this, 'filter_frame'),
- 'filterform' => array($this, 'filter_form'),
- 'filtersetform' => array($this, 'filterset_form'),
- ));
- // Add include path for internal classes
- $include_path = $this->home . '/lib' . PATH_SEPARATOR;
- $include_path .= ini_get('include_path');
- set_include_path($include_path);
- $host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost'));
- $host = rcube_idn_to_ascii($host);
- $port = $this->rc->config->get('managesieve_port');
- if (empty($port)) {
- $port = getservbyname('sieve', 'tcp');
- if (empty($port)) {
- $port = self::PORT;
- }
- }
- $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
- 'user' => $_SESSION['username'],
- 'password' => $this->rc->decrypt($_SESSION['password']),
- 'host' => $host,
- 'port' => $port,
- 'auth_type' => $this->rc->config->get('managesieve_auth_type'),
- 'usetls' => $this->rc->config->get('managesieve_usetls', false),
- 'disabled' => $this->rc->config->get('managesieve_disabled_extensions'),
- 'debug' => $this->rc->config->get('managesieve_debug', false),
- 'auth_cid' => $this->rc->config->get('managesieve_auth_cid'),
- 'auth_pw' => $this->rc->config->get('managesieve_auth_pw'),
- ));
- // try to connect to managesieve server and to fetch the script
- $this->sieve = new rcube_sieve(
- $plugin['user'],
- $plugin['password'],
- $plugin['host'],
- $plugin['port'],
- $plugin['auth_type'],
- $plugin['usetls'],
- $plugin['disabled'],
- $plugin['debug'],
- $plugin['auth_cid'],
- $plugin['auth_pw']
- );
- if (!($error = $this->sieve->error())) {
- // Get list of scripts
- $list = $this->list_scripts();
- if (!empty($_GET['_set']) || !empty($_POST['_set'])) {
- $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
- }
- else if (!empty($_SESSION['managesieve_current'])) {
- $script_name = $_SESSION['managesieve_current'];
- }
- else {
- // get (first) active script
- if (!empty($this->active[0])) {
- $script_name = $this->active[0];
- }
- else if ($list) {
- $script_name = $list[0];
- }
- // create a new (initial) script
- else {
- // if script not exists build default script contents
- $script_file = $this->rc->config->get('managesieve_default');
- $script_name = $this->rc->config->get('managesieve_script_name');
- if (empty($script_name))
- $script_name = 'roundcube';
- if ($script_file && is_readable($script_file))
- $content = file_get_contents($script_file);
- // add script and set it active
- if ($this->sieve->save_script($script_name, $content)) {
- $this->activate_script($script_name);
- $this->list[] = $script_name;
- }
- }
- }
- if ($script_name) {
- $this->sieve->load($script_name);
- }
- $error = $this->sieve->error();
- }
- // finally set script objects
- if ($error) {
- switch ($error) {
- case SIEVE_ERROR_CONNECTION:
- case SIEVE_ERROR_LOGIN:
- $this->rc->output->show_message('managesieve.filterconnerror', 'error');
- break;
- default:
- $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
- break;
- }
- raise_error(array('code' => 403, 'type' => 'php',
- 'file' => __FILE__, 'line' => __LINE__,
- 'message' => "Unable to connect to managesieve on $host:$port"), true, false);
- // to disable 'Add filter' button set env variable
- $this->rc->output->set_env('filterconnerror', true);
- $this->script = array();
- }
- else {
- $this->exts = $this->sieve->get_extensions();
- $this->script = $this->sieve->script->as_array();
- $this->rc->output->set_env('currentset', $this->sieve->current);
- $_SESSION['managesieve_current'] = $this->sieve->current;
- }
- return $error;
- }
- function managesieve_actions()
- {
- $this->init_ui();
- $error = $this->managesieve_start();
- // Handle user requests
- if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) {
- $fid = (int) get_input_value('_fid', RCUBE_INPUT_POST);
- if ($action == 'delete' && !$error) {
- if (isset($this->script[$fid])) {
- if ($this->sieve->script->delete_rule($fid))
- $result = $this->save_script();
- if ($result === true) {
- $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'del', array('id' => $fid));
- } else {
- $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
- }
- }
- }
- else if ($action == 'move' && !$error) {
- if (isset($this->script[$fid])) {
- $to = (int) get_input_value('_to', RCUBE_INPUT_POST);
- $rule = $this->script[$fid];
- // remove rule
- unset($this->script[$fid]);
- $this->script = array_values($this->script);
- // add at target position
- if ($to >= count($this->script)) {
- $this->script[] = $rule;
- }
- else {
- $script = array();
- foreach ($this->script as $idx => $r) {
- if ($idx == $to)
- $script[] = $rule;
- $script[] = $r;
- }
- $this->script = $script;
- }
- $this->sieve->script->content = $this->script;
- $result = $this->save_script();
- if ($result === true) {
- $result = $this->list_rules();
- $this->rc->output->show_message('managesieve.moved', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'list',
- array('list' => $result, 'clear' => true, 'set' => $to));
- } else {
- $this->rc->output->show_message('managesieve.moveerror', 'error');
- }
- }
- }
- else if ($action == 'act' && !$error) {
- if (isset($this->script[$fid])) {
- $rule = $this->script[$fid];
- $disabled = $rule['disabled'] ? true : false;
- $rule['disabled'] = !$disabled;
- $result = $this->sieve->script->update_rule($fid, $rule);
- if ($result !== false)
- $result = $this->save_script();
- if ($result === true) {
- if ($rule['disabled'])
- $this->rc->output->show_message('managesieve.deactivated', 'confirmation');
- else
- $this->rc->output->show_message('managesieve.activated', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'update',
- array('id' => $fid, 'disabled' => $rule['disabled']));
- } else {
- if ($rule['disabled'])
- $this->rc->output->show_message('managesieve.deactivateerror', 'error');
- else
- $this->rc->output->show_message('managesieve.activateerror', 'error');
- }
- }
- }
- else if ($action == 'setact' && !$error) {
- $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
- $result = $this->activate_script($script_name);
- $kep14 = $this->rc->config->get('managesieve_kolab_master');
- if ($result === true) {
- $this->rc->output->set_env('active_sets', $this->active);
- $this->rc->output->show_message('managesieve.setactivated', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'setact',
- array('name' => $script_name, 'active' => true, 'all' => !$kep14));
- } else {
- $this->rc->output->show_message('managesieve.setactivateerror', 'error');
- }
- }
- else if ($action == 'deact' && !$error) {
- $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
- $result = $this->deactivate_script($script_name);
- if ($result === true) {
- $this->rc->output->set_env('active_sets', $this->active);
- $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'setact',
- array('name' => $script_name, 'active' => false));
- } else {
- $this->rc->output->show_message('managesieve.setdeactivateerror', 'error');
- }
- }
- else if ($action == 'setdel' && !$error) {
- $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
- $result = $this->remove_script($script_name);
- if ($result === true) {
- $this->rc->output->show_message('managesieve.setdeleted', 'confirmation');
- $this->rc->output->command('managesieve_updatelist', 'setdel',
- array('name' => $script_name));
- $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, true);
- $script = $this->sieve->get_script($script_name);
- if (PEAR::isError($script))
- exit;
- $browser = new rcube_browser;
- // send download headers
- header("Content-Type: application/octet-stream");
- header("Content-Length: ".strlen($script));
- if ($browser->ie)
- header("Content-Type: application/force-download");
- if ($browser->ie && $browser->ver < 7)
- $filename = rawurlencode(abbreviate_string($script_name, 55));
- else if ($browser->ie)
- $filename = rawurlencode($script_name);
- else
- $filename = addcslashes($script_name, '\\"');
- header("Content-Disposition: attachment; filename=\"$filename.txt\"");
- echo $script;
- exit;
- }
- else if ($action == 'list') {
- $result = $this->list_rules();
- $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result));
- }
- else if ($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);
- }
- else if ($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->rc->task == 'mail') {
- // Initialize the form
- $rules = get_input_value('r', RCUBE_INPUT_GET);
- if (!empty($rules)) {
- $i = 0;
- foreach ($rules as $rule) {
- list($header, $value) = explode(':', $rule, 2);
- $tests[$i] = array(
- 'type' => 'contains',
- 'test' => 'header',
- 'arg1' => $header,
- 'arg2' => $value,
- );
- $i++;
- }
- $this->form = array(
- 'join' => count($tests) > 1 ? 'allof' : 'anyof',
- 'name' => '',
- 'tests' => $tests,
- 'actions' => array(
- 0 => array('type' => 'fileinto'),
- 1 => array('type' => 'stop'),
- ),
- );
- }
- }
- $this->managesieve_send();
- }
- function managesieve_save()
- {
- // load localization
- $this->add_texts('localization/', array('filters','managefilters'));
- // include main js script
- if ($this->api->output->type == 'html') {
- $this->include_script('managesieve.js');
- }
- // Init plugin and handle managesieve connection
- $error = $this->managesieve_start();
- // get request size limits (#1488648)
- $max_post = max(array(
- ini_get('max_input_vars'),
- ini_get('suhosin.request.max_vars'),
- ini_get('suhosin.post.max_vars'),
- ));
- $max_depth = max(array(
- ini_get('suhosin.request.max_array_depth'),
- ini_get('suhosin.post.max_array_depth'),
- ));
- // check request size limit
- if ($max_post && count($_POST, COUNT_RECURSIVE) >= $max_post) {
- rcube::raise_error(array(
- 'code' => 500, 'type' => 'php',
- 'file' => __FILE__, 'line' => __LINE__,
- 'message' => "Request size limit exceeded (one of max_input_vars/suhosin.request.max_vars/suhosin.post.max_vars)"
- ), true, false);
- $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
- }
- // check request depth limits
- else if ($max_depth && count($_POST['_header']) > $max_depth) {
- rcube::raise_error(array(
- 'code' => 500, 'type' => 'php',
- 'file' => __FILE__, 'line' => __LINE__,
- 'message' => "Request size limit exceeded (one of suhosin.request.max_array_depth/suhosin.post.max_array_depth)"
- ), true, false);
- $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
- }
- // filters set add action
- else if (!empty($_POST['_newset'])) {
- $name = get_input_value('_name', RCUBE_INPUT_POST, true);
- $copy = get_input_value('_copy', RCUBE_INPUT_POST, true);
- $from = get_input_value('_from', RCUBE_INPUT_POST);
- $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
- $kolab = $this->rc->config->get('managesieve_kolab_master');
- $name_uc = mb_strtolower($name);
- $list = $this->list_scripts();
- if (!$name) {
- $this->errors['name'] = $this->gettext('cannotbeempty');
- }
- else if (mb_strlen($name) > 128) {
- $this->errors['name'] = $this->gettext('nametoolong');
- }
- else if (!empty($exceptions) && in_array($name, (array)$exceptions)) {
- $this->errors['name'] = $this->gettext('namereserved');
- }
- else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) {
- $this->errors['name'] = $this->gettext('namereserved');
- }
- else if (in_array($name, $list)) {
- $this->errors['name'] = $this->gettext('setexist');
- }
- 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->save_script($name)) {
- $this->errors['file'] = $this->gettext('setcreateerror');
- }
- }
- else { // upload failed
- $err = $_FILES['_file']['error'];
- 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 {
- $this->errors['file'] = $this->gettext('fileuploaderror');
- }
- }
- }
- else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) {
- $error = 'managesieve.setcreateerror';
- }
- if (!$error && empty($this->errors)) {
- // Find position of the new script on the list
- $list[] = $name;
- asort($list, SORT_LOCALE_STRING);
- $list = array_values($list);
- $index = array_search($name, $list);
- $this->rc->output->show_message('managesieve.setcreated', 'confirmation');
- $this->rc->output->command('parent.managesieve_updatelist', 'setadd',
- array('name' => $name, 'index' => $index));
- } else if ($msg) {
- $this->rc->output->command('display_message', $msg, 'error');
- } else if ($error) {
- $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 = get_input_value('_header', RCUBE_INPUT_POST);
- $cust_headers = get_input_value('_custom_header', RCUBE_INPUT_POST);
- $ops = get_input_value('_rule_op', RCUBE_INPUT_POST);
- $sizeops = get_input_value('_rule_size_op', RCUBE_INPUT_POST);
- $sizeitems = get_input_value('_rule_size_item', RCUBE_INPUT_POST);
- $sizetargets = get_input_value('_rule_size_target', RCUBE_INPUT_POST);
- $targets = get_input_value('_rule_target', RCUBE_INPUT_POST, true);
- $mods = get_input_value('_rule_mod', RCUBE_INPUT_POST);
- $mod_types = get_input_value('_rule_mod_type', RCUBE_INPUT_POST);
- $body_trans = get_input_value('_rule_trans', RCUBE_INPUT_POST);
- $body_types = get_input_value('_rule_trans_type', RCUBE_INPUT_POST, true);
- $comparators = get_input_value('_rule_comp', RCUBE_INPUT_POST);
- $act_types = get_input_value('_action_type', RCUBE_INPUT_POST, true);
- $mailboxes = get_input_value('_action_mailbox', RCUBE_INPUT_POST, true);
- $act_targets = get_input_value('_action_target', RCUBE_INPUT_POST, true);
- $area_targets = get_input_value('_action_target_area', RCUBE_INPUT_POST, true);
- $reasons = get_input_value('_action_reason', RCUBE_INPUT_POST, true);
- $addresses = get_input_value('_action_addresses', RCUBE_INPUT_POST, true);
- $days = get_input_value('_action_days', RCUBE_INPUT_POST);
- $subject = get_input_value('_action_subject', RCUBE_INPUT_POST, true);
- $flags = get_input_value('_action_flags', RCUBE_INPUT_POST);
- $varnames = get_input_value('_action_varname', RCUBE_INPUT_POST);
- $varvalues = get_input_value('_action_varvalue', RCUBE_INPUT_POST);
- $varmods = get_input_value('_action_varmods', RCUBE_INPUT_POST);
- $notifyaddrs = get_input_value('_action_notifyaddress', RCUBE_INPUT_POST);
- $notifybodies = get_input_value('_action_notifybody', RCUBE_INPUT_POST);
- $notifymessages = get_input_value('_action_notifymessage', RCUBE_INPUT_POST);
- $notifyfrom = get_input_value('_action_notifyfrom', RCUBE_INPUT_POST);
- $notifyimp = get_input_value('_action_notifyimportance', RCUBE_INPUT_POST);
- // we need a "hack" for radiobuttons
- foreach ($sizeitems as $item)
- $items[] = $item;
- $this->form['disabled'] = $_POST['_disabled'] ? true : false;
- $this->form['join'] = $join=='allof' ? true : false;
- $this->form['name'] = $name;
- $this->form['tests'] = array();
- $this->form['actions'] = array();
- if ($name == '')
- $this->errors['name'] = $this->gettext('cannotbeempty');
- else {
- foreach($this->script as $idx => $rule)
- if($rule['name'] == $name && $idx != $fid) {
- $this->errors['name'] = $this->gettext('ruleexist');
- break;
- }
- }
- $i = 0;
- // rules
- if ($join == 'any') {
- $this->form['tests'][0]['test'] = 'true';
- }
- else {
- foreach ($headers as $idx => $header) {
- $header = $this->strip_value($header);
- $target = $this->strip_value($targets[$idx], true);
- $operator = $this->strip_value($ops[$idx]);
- $comparator = $this->strip_value($comparators[$idx]);
- if ($header == 'size') {
- $sizeop = $this->strip_value($sizeops[$idx]);
- $sizeitem = $this->strip_value($items[$idx]);
- $sizetarget = $this->strip_value($sizetargets[$idx]);
- $this->form['tests'][$i]['test'] = 'size';
- $this->form['tests'][$i]['type'] = $sizeop;
- $this->form['tests'][$i]['arg'] = $sizetarget;
- if ($sizetarget == '')
- $this->errors['tests'][$i]['sizetarget'] = $this->gettext('cannotbeempty');
- else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) {
- $this->errors['tests'][$i]['sizetarget'] = $this->gettext('forbiddenchars');
- $this->form['tests'][$i]['item'] = $sizeitem;
- }
- else
- $this->form['tests'][$i]['arg'] .= $m[1];
- }
- else if ($header == 'body') {
- $trans = $this->strip_value($body_trans[$idx]);
- $trans_type = $this->strip_value($body_types[$idx], true);
- if (preg_match('/^not/', $operator))
- $this->form['tests'][$i]['not'] = true;
- $type = preg_replace('/^not/', '', $operator);
- if ($type == 'exists') {
- $this->errors['tests'][$i]['op'] = true;
- }
- $this->form['tests'][$i]['test'] = 'body';
- $this->form['tests'][$i]['type'] = $type;
- $this->form['tests'][$i]['arg'] = $target;
- if ($target == '' && $type != 'exists')
- $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
- else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
- $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
- $this->form['tests'][$i]['part'] = $trans;
- if ($trans == 'content') {
- $this->form['tests'][$i]['content'] = $trans_type;
- }
- }
- else {
- $cust_header = $headers = $this->strip_value($cust_headers[$idx]);
- $mod = $this->strip_value($mods[$idx]);
- $mod_type = $this->strip_value($mod_types[$idx]);
- if (preg_match('/^not/', $operator))
- $this->form['tests'][$i]['not'] = true;
- $type = preg_replace('/^not/', '', $operator);
- if ($header == '...') {
- $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) {
- // RFC2822: printable ASCII except colon
- if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/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'] = $header == '...' ? $cust_header : $header;
- }
- else {
- $test = 'header';
- $header = $header == '...' ? $cust_header : $header;
- if ($mod == 'address' || $mod == 'envelope') {
- $found = false;
- if (empty($this->errors['tests'][$i]['header'])) {
- foreach ((array)$header as $hdr) {
- if (!in_array(strtolower(trim($hdr)), $this->addr_headers))
- $found = true;
- }
- }
- if (!$found)
- $test = $mod;
- }
- $this->form['tests'][$i]['type'] = $type;
- $this->form['tests'][$i]['test'] = $test;
- $this->form['tests'][$i]['arg1'] = $header;
- $this->form['tests'][$i]['arg2'] = $target;
- if ($target == '')
- $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
- else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
- $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
- if ($mod) {
- $this->form['tests'][$i]['part'] = $mod_type;
- }
- }
- }
- if ($header != 'size' && $comparator) {
- if (preg_match('/^(value|count)/', $this->form['tests'][$i]['type']))
- $comparator = 'i;ascii-numeric';
- $this->form['tests'][$i]['comparator'] = $comparator;
- }
- $i++;
- }
- }
- $i = 0;
- // actions
- foreach($act_types as $idx => $type) {
- $type = $this->strip_value($type);
- $target = $this->strip_value($act_targets[$idx]);
- switch ($type) {
- case 'fileinto':
- case 'fileinto_copy':
- $mailbox = $this->strip_value($mailboxes[$idx]);
- $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in');
- if ($type == 'fileinto_copy') {
- $type = 'fileinto';
- $this->form['actions'][$i]['copy'] = true;
- }
- 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':
- case 'redirect_copy':
- $this->form['actions'][$i]['target'] = $target;
- if ($this->form['actions'][$i]['target'] == '')
- $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty');
- else if (!check_email($this->form['actions'][$i]['target']))
- $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning');
- if ($type == 'redirect_copy') {
- $type = 'redirect';
- $this->form['actions'][$i]['copy'] = true;
- }
- break;
- case 'addflag':
- case 'setflag':
- case 'removeflag':
- $_target = array();
- if (empty($flags[$idx])) {
- $this->errors['actions'][$i]['target'] = $this->gettext('noflagset');
- }
- else {
- foreach ($flags[$idx] as $flag) {
- $_target[] = $this->strip_value($flag);
- }
- }
- $this->form['actions'][$i]['target'] = $_target;
- 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]['subject'] = $subject[$idx];
- $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]);
- // @TODO: vacation :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(!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;
- case 'set':
- $this->form['actions'][$i]['name'] = $varnames[$idx];
- $this->form['actions'][$i]['value'] = $varvalues[$idx];
- foreach ((array)$varmods[$idx] as $v_m) {
- $this->form['actions'][$i][$v_m] = true;
- }
- if (empty($varnames[$idx])) {
- $this->errors['actions'][$i]['name'] = $this->gettext('cannotbeempty');
- }
- else if (!preg_match('/^[0-9a-z_]+$/i', $varnames[$idx])) {
- $this->errors['actions'][$i]['name'] = $this->gettext('forbiddenchars');
- }
- if (!isset($varvalues[$idx]) || $varvalues[$idx] === '') {
- $this->errors['actions'][$i]['value'] = $this->gettext('cannotbeempty');
- }
- break;
- case 'notify':
- if (empty($notifyaddrs[$idx])) {
- $this->errors['actions'][$i]['address'] = $this->gettext('cannotbeempty');
- }
- else if (!check_email($notifyaddrs[$idx])) {
- $this->errors['actions'][$i]['address'] = $this->gettext('noemailwarning');
- }
- if (!empty($notifyfrom[$idx]) && !check_email($notifyfrom[$idx])) {
- $this->errors['actions'][$i]['from'] = $this->gettext('noemailwarning');
- }
- $this->form['actions'][$i]['address'] = $notifyaddrs[$idx];
- $this->form['actions'][$i]['body'] = $notifybodies[$idx];
- $this->form['actions'][$i]['message'] = $notifymessages[$idx];
- $this->form['actions'][$i]['from'] = $notifyfrom[$idx];
- $this->form['actions'][$i]['importance'] = $notifyimp[$idx];
- break;
- }
- $this->form['actions'][$i]['type'] = $type;
- $i++;
- }
- if (!$this->errors && !$error) {
- // 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->save_script();
- if ($save && $fid !== false) {
- $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
- if ($this->rc->task != 'mail') {
- $this->rc->output->command('parent.managesieve_updatelist',
- isset($new) ? 'add' : 'update',
- array(
- 'name' => Q($this->form['name']),
- 'id' => $fid,
- 'disabled' => $this->form['disabled']
- ));
- }
- else {
- $this->rc->output->command('managesieve_dialog_close');
- $this->rc->output->send('iframe');
- }
- }
- 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');
- }
- }
- // 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('name');
- $result = $this->list_rules();
- // create XHTML table
- $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
- // set client env
- $this->rc->output->add_gui_object('filterslist', $attrib['id']);
- $this->rc->output->include_script('list.js');
- // add some labels to client
- $this->rc->output->add_label('managesieve.filterdeleteconfirm');
- return $out;
- }
- // return the filters list as <SELECT>
- function filtersets_list($attrib, $no_env = false)
- {
- // add id to message list table if not specified
- if (!strlen($attrib['id']))
- $attrib['id'] = 'rcmfiltersetslist';
- $list = $this->list_scripts();
- if ($list) {
- asort($list, SORT_LOCALE_STRING);
- }
- if (!empty($attrib['type']) && $attrib['type'] == 'list') {
- // define list of cols to be displayed
- $a_show_cols = array('name');
- if ($list) {
- foreach ($list as $idx => $set) {
- $scripts['S'.$idx] = $set;
- $result[] = array(
- 'name' => Q($set),
- 'id' => 'S'.$idx,
- 'class' => !in_array($set, $this->active) ? 'disabled' : '',
- );
- }
- }
- // create XHTML table
- $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
- $this->rc->output->set_env('filtersets', $scripts);
- $this->rc->output->include_script('list.js');
- }
- else {
- $select = new html_select(array('name' => '_set', 'id' => $attrib['id'],
- 'onchange' => $this->rc->task != 'mail' ? 'rcmail.managesieve_set()' : ''));
- if ($list) {
- foreach ($list as $set)
- $select->add($set, $set);
- }
- $out = $select->show($this->sieve->current);
- }
- // set client env
- if (!$no_env) {
- $this->rc->output->add_gui_object('filtersetslist', $attrib['id']);
- $this->rc->output->add_label('managesieve.setdeleteconfirm');
- }
- return $out;
- }
- function filter_frame($attrib)
- {
- if (!$attrib['id'])
- $attrib['id'] = 'rcmfilterframe';
- $attrib['name'] = $attrib['id'];
- $this->rc->output->set_env('contentframe', $attrib['name']);
- $this->rc->output->set_env('blankpage', $attrib['src'] ?
- $this->rc->output->abs_url($attrib['src']) : 'program/resources/blank.gif');
- return $this->rc->output->frame($attrib);
- }
- function filterset_form($attrib)
- {
- if (!$attrib['id'])
- $attrib['id'] = 'rcmfiltersetform';
- $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n";
- $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
- $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
- $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
- $hiddenfields->add(array('name' => '_newset', 'value' => 1));
- $out .= $hiddenfields->show();
- $name = get_input_value('_name', RCUBE_INPUT_POST);
- $copy = get_input_value('_copy', RCUBE_INPUT_POST);
- $selected = get_input_value('_from', RCUBE_INPUT_POST);
- // filter set name input
- $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30,
- 'class' => ($this->errors['name'] ? 'error' : '')));
- $out .= sprintf('<label for="%s"><b>%s:</b></label> %s<br /><br />',
- '_name', Q($this->gettext('filtersetname')), $input_name->show($name));
- $out .="\n<fieldset class=\"itemlist\"><legend>" . $this->gettext('filters') . ":</legend>\n";
- $out .= '<input type="radio" id="from_none" name="_from" value="none"'
- .(!$selected || $selected=='none' ? ' checked="checked"' : '').'></input>';
- $out .= sprintf('<label for="%s">%s</label> ', 'from_none', Q($this->gettext('none')));
- // filters set list
- $list = $this->list_scripts();
- $select = new html_select(array('name' => '_copy', 'id' => '_copy'));
- if (is_array($list)) {
- asort($list, SORT_LOCALE_STRING);
- if (!$copy)
- $copy = $_SESSION['managesieve_current'];
- foreach ($list as $set) {
- $select->add($set, $set);
- }
- $out .= '<br /><input type="radio" id="from_set" name="_from" value="set"'
- .($selected=='set' ? ' checked="checked"' : '').'></input>';
- $out .= sprintf('<label for="%s">%s:</label> ', 'from_set', Q($this->gettext('fromset')));
- $out .= $select->show($copy);
- }
- // script upload box
- $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30,
- 'type' => 'file', 'class' => ($this->errors['file'] ? 'error' : '')));
- $out .= '<br /><input type="radio" id="from_file" name="_from" value="file"'
- .($selected=='file' ? ' checked="checked"' : '').'></input>';
- $out .= sprintf('<label for="%s">%s:</label> ', 'from_file', Q($this->gettext('fromfile')));
- $out .= $upload->show();
- $out .= '</fieldset>';
- $this->rc->output->add_gui_object('sieveform', 'filtersetform');
- if ($this->errors['name'])
- $this->add_tip('_name', $this->errors['name'], true);
- if ($this->errors['file'])
- $this->add_tip('_file', $this->errors['file'], true);
- $this->print_tips();
- return $out;
- }
- function filter_form($attrib)
- {
- if (!$…
Large files files are truncated, but you can click here to view the full file