'Subject', 'from' => 'From', 'to' => 'To', ); protected $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", ); protected $notify_methods = array( 'mailto', // 'sms', // 'tel', ); protected $notify_importance_options = array( 3 => 'notifyimportancelow', 2 => 'notifyimportancenormal', 1 => 'notifyimportancehigh' ); const VERSION = '8.9'; const PROGNAME = 'Roundcube (Managesieve)'; const PORT = 4190; /** * Class constructor */ function __construct($plugin) { $this->rc = rcube::get_instance(); $this->plugin = $plugin; } /** * Loads configuration, initializes plugin (including sieve connection) */ function start($mode = null) { // 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'), 'filterseteditraw' => array($this, 'filterset_editraw'), )); // connect to managesieve server $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password'])); // load current/active script if (!$error) { // Get list of scripts $list = $this->list_scripts(); // reset current script when entering filters UI (#1489412) if ($this->rc->action == 'plugin.managesieve') { $this->rc->session->remove('managesieve_current'); } if ($mode != 'vacation') { if (!empty($_GET['_set']) || !empty($_POST['_set'])) { $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); } else if (!empty($_SESSION['managesieve_current'])) { $script_name = $_SESSION['managesieve_current']; } } $error = $this->load_script($script_name); } // finally set script objects if ($error) { switch ($error) { case rcube_sieve::ERROR_CONNECTION: case rcube_sieve::ERROR_LOGIN: $this->rc->output->show_message('managesieve.filterconnerror', 'error'); break; default: $this->rc->output->show_message('managesieve.filterunknownerror', 'error'); break; } // reload interface in case of possible error when specified script wasn't found (#1489412) if ($script_name !== null && !empty($list) && !in_array($script_name, $list)) { $this->rc->output->command('reload', 500); } // 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->init_script(); $this->rc->output->set_env('currentset', $this->sieve->current); $_SESSION['managesieve_current'] = $this->sieve->current; } $this->rc->output->set_env('raw_sieve_editor', $this->rc->config->get('managesieve_raw_editor', true)); return $error; } /** * Connect to configured managesieve server * * @param string $username User login * @param string $password User password * * @return int Connection status: 0 on success, >0 on failure */ public function connect($username, $password) { // Get connection parameters $host = $this->rc->config->get('managesieve_host', 'localhost'); $port = $this->rc->config->get('managesieve_port'); $tls = $this->rc->config->get('managesieve_usetls', false); $host = rcube_utils::parse_host($host); $host = rcube_utils::idn_to_ascii($host); // remove tls:// prefix, set TLS flag if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) { $tls = true; } if (empty($port)) { $port = getservbyname('sieve', 'tcp'); if (empty($port)) { $port = self::PORT; } } $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array( 'user' => $username, 'password' => $password, 'host' => $host, 'port' => $port, 'usetls' => $tls, 'auth_type' => $this->rc->config->get('managesieve_auth_type'), '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'), 'socket_options' => $this->rc->config->get('managesieve_conn_options'), )); // Handle per-host socket options rcube_utils::parse_socket_options($plugin['socket_options'], $plugin['host']); // 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'], $plugin['socket_options'] ); $error = $this->sieve->error(); if ($error) { rcube::raise_error(array( 'code' => 403, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Unable to connect to managesieve on $host:$port" ), true, false); } return $error; } /** * Load specified (or active) script * * @param string $script_name Optional script name * * @return int Connection status: 0 on success, >0 on failure */ protected function load_script($script_name = null) { // Get list of scripts $list = $this->list_scripts(); if ($script_name === null || $script_name === '') { // get (first) active script if (!empty($this->active)) { $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); } return $this->sieve->error(); } /** * User interface actions handler */ function actions() { $error = $this->start(); // Handle user requests if ($action = rcube_utils::get_input_value('_act', rcube_utils::INPUT_GPC)) { $fid = (int) rcube_utils::get_input_value('_fid', rcube_utils::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) rcube_utils::get_input_value('_to', rcube_utils::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 = !empty($rule['disabled']); $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 = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true); $result = $this->activate_script($script_name); $kep14 = $this->rc->config->get('managesieve_kolab_master'); 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 = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, 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 = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, 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') { $this->rc->request_security_check(rcube_utils::INPUT_GET); $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true); $script = $this->sieve->get_script($script_name); if ($script !== false) { $this->rc->output->download_headers($script_name . '.txt', array('length' => strlen($script))); 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 = rcube_utils::get_input_value('_rid', rcube_utils::INPUT_POST); $id = $this->genid(); $content = $this->rule_div($fid, $id, false); $this->rc->output->command('managesieve_rulefill', $content, $id, $rid); } else if ($action == 'actionadd') { $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST); $id = $this->genid(); $content = $this->action_div($fid, $id, false); $this->rc->output->command('managesieve_actionfill', $content, $id, $aid); } else if ($action == 'addresses') { $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST); $this->rc->output->command('managesieve_vacation_addresses_update', $aid, $this->user_emails()); } $this->rc->output->send(); } else if ($this->rc->task == 'mail') { // Initialize the form $rules = rcube_utils::get_input_value('r', rcube_utils::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->send(); } function saveraw() { // Init plugin and handle managesieve connection $error = $this->start(); $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST); $result = $this->sieve->save_script($script_name, $_POST['rawsetcontent']); if ($result === false) { $this->rc->output->show_message('managesieve.filtersaveerror', 'error'); $errorLines = $this->sieve->get_error_lines(); if (count($errorLines) > 0) { $this->rc->output->set_env("sieve_errors", $errorLines); } } 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 $error = $this->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 = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true); $copy = rcube_utils::get_input_value('_copy', rcube_utils::INPUT_POST, true); $from = rcube_utils::get_input_value('_from', rcube_utils::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->plugin->gettext('cannotbeempty'); } else if (mb_strlen($name) > 128) { $this->errors['name'] = $this->plugin->gettext('nametoolong'); } else if (!empty($exceptions) && in_array($name, (array)$exceptions)) { $this->errors['name'] = $this->plugin->gettext('namereserved'); } else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) { $this->errors['name'] = $this->plugin->gettext('namereserved'); } else if (in_array($name, $list)) { $this->errors['name'] = $this->plugin->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->plugin->gettext('setcreateerror'); } } else { // upload failed $err = $_FILES['_file']['error']; if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { $msg = $this->rc->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $this->rc->show_bytes(rcube_utils::max_upload_size())))); } else { $this->errors['file'] = $this->plugin->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(rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true)); $fid = trim(rcube_utils::get_input_value('_fid', rcube_utils::INPUT_POST)); $join = trim(rcube_utils::get_input_value('_join', rcube_utils::INPUT_POST)); // and arrays $headers = rcube_utils::get_input_value('_header', rcube_utils::INPUT_POST); $cust_headers = rcube_utils::get_input_value('_custom_header', rcube_utils::INPUT_POST); $cust_vars = rcube_utils::get_input_value('_custom_var', rcube_utils::INPUT_POST); $ops = rcube_utils::get_input_value('_rule_op', rcube_utils::INPUT_POST); $sizeops = rcube_utils::get_input_value('_rule_size_op', rcube_utils::INPUT_POST); $sizeitems = rcube_utils::get_input_value('_rule_size_item', rcube_utils::INPUT_POST); $sizetargets = rcube_utils::get_input_value('_rule_size_target', rcube_utils::INPUT_POST); $targets = rcube_utils::get_input_value('_rule_target', rcube_utils::INPUT_POST, true); $mods = rcube_utils::get_input_value('_rule_mod', rcube_utils::INPUT_POST); $mod_types = rcube_utils::get_input_value('_rule_mod_type', rcube_utils::INPUT_POST); $body_trans = rcube_utils::get_input_value('_rule_trans', rcube_utils::INPUT_POST); $body_types = rcube_utils::get_input_value('_rule_trans_type', rcube_utils::INPUT_POST, true); $comparators = rcube_utils::get_input_value('_rule_comp', rcube_utils::INPUT_POST); $indexes = rcube_utils::get_input_value('_rule_index', rcube_utils::INPUT_POST); $lastindexes = rcube_utils::get_input_value('_rule_index_last', rcube_utils::INPUT_POST); $dateheaders = rcube_utils::get_input_value('_rule_date_header', rcube_utils::INPUT_POST); $dateparts = rcube_utils::get_input_value('_rule_date_part', rcube_utils::INPUT_POST); $message = rcube_utils::get_input_value('_rule_message', rcube_utils::INPUT_POST); $dup_handles = rcube_utils::get_input_value('_rule_duplicate_handle', rcube_utils::INPUT_POST, true); $dup_headers = rcube_utils::get_input_value('_rule_duplicate_header', rcube_utils::INPUT_POST, true); $dup_uniqueids = rcube_utils::get_input_value('_rule_duplicate_uniqueid', rcube_utils::INPUT_POST, true); $dup_seconds = rcube_utils::get_input_value('_rule_duplicate_seconds', rcube_utils::INPUT_POST); $dup_lasts = rcube_utils::get_input_value('_rule_duplicate_last', rcube_utils::INPUT_POST); $act_types = rcube_utils::get_input_value('_action_type', rcube_utils::INPUT_POST, true); $mailboxes = rcube_utils::get_input_value('_action_mailbox', rcube_utils::INPUT_POST, true); $act_targets = rcube_utils::get_input_value('_action_target', rcube_utils::INPUT_POST, true); $domain_targets = rcube_utils::get_input_value('_action_target_domain', rcube_utils::INPUT_POST); $area_targets = rcube_utils::get_input_value('_action_target_area', rcube_utils::INPUT_POST, true); $reasons = rcube_utils::get_input_value('_action_reason', rcube_utils::INPUT_POST, true); $addresses = rcube_utils::get_input_value('_action_addresses', rcube_utils::INPUT_POST, true); $intervals = rcube_utils::get_input_value('_action_interval', rcube_utils::INPUT_POST); $interval_types = rcube_utils::get_input_value('_action_interval_type', rcube_utils::INPUT_POST); $from = rcube_utils::get_input_value('_action_from', rcube_utils::INPUT_POST); $subject = rcube_utils::get_input_value('_action_subject', rcube_utils::INPUT_POST, true); $flags = rcube_utils::get_input_value('_action_flags', rcube_utils::INPUT_POST); $varnames = rcube_utils::get_input_value('_action_varname', rcube_utils::INPUT_POST); $varvalues = rcube_utils::get_input_value('_action_varvalue', rcube_utils::INPUT_POST); $varmods = rcube_utils::get_input_value('_action_varmods', rcube_utils::INPUT_POST); $notifymethods = rcube_utils::get_input_value('_action_notifymethod', rcube_utils::INPUT_POST); $notifytargets = rcube_utils::get_input_value('_action_notifytarget', rcube_utils::INPUT_POST, true); $notifyoptions = rcube_utils::get_input_value('_action_notifyoption', rcube_utils::INPUT_POST, true); $notifymessages = rcube_utils::get_input_value('_action_notifymessage', rcube_utils::INPUT_POST, true); $notifyfrom = rcube_utils::get_input_value('_action_notifyfrom', rcube_utils::INPUT_POST); $notifyimp = rcube_utils::get_input_value('_action_notifyimportance', rcube_utils::INPUT_POST); // we need a "hack" for radiobuttons foreach ($sizeitems as $item) $items[] = $item; $this->form['disabled'] = !empty($_POST['_disabled']); $this->form['join'] = $join == 'allof'; $this->form['name'] = $name; $this->form['tests'] = array(); $this->form['actions'] = array(); if ($name == '') $this->errors['name'] = $this->plugin->gettext('cannotbeempty'); else { foreach ($this->script as $idx => $rule) if($rule['name'] == $name && $idx != $fid) { $this->errors['name'] = $this->plugin->gettext('ruleexist'); break; } } $i = 0; // rules if ($join == 'any') { $this->form['tests'][0]['test'] = 'true'; } else { foreach ($headers as $idx => $header) { // targets are indexed differently (assume form order) $target = $this->strip_value(array_shift($targets), true); $header = $this->strip_value($header); $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->plugin->gettext('cannotbeempty'); else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) { $this->errors['tests'][$i]['sizetarget'] = $this->plugin->gettext('forbiddenchars'); $this->form['tests'][$i]['item'] = $sizeitem; } else $this->form['tests'][$i]['arg'] .= $m[1]; } else if ($header == 'currentdate') { $datepart = $this->strip_value($dateparts[$idx]); 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'] = 'currentdate'; $this->form['tests'][$i]['type'] = $type; $this->form['tests'][$i]['part'] = $datepart; $this->form['tests'][$i]['arg'] = $target; if ($type != 'exists') { if (!count($target)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } else if (strpos($type, 'count-') === 0) { foreach ($target as $arg) { if (preg_match('/[^0-9]/', $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } } else if (strpos($type, 'value-') === 0) { // Some date/time formats do not support i;ascii-numeric comparator if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) { $comparator = ''; } } if (!preg_match('/^(regex|matches|count-)/', $type) && count($target)) { foreach ($target as $arg) { if (!$this->validate_date_part($datepart, $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat'); break; } } } } } else if ($header == 'date') { $datepart = $this->strip_value($dateparts[$idx]); $dateheader = $this->strip_value($dateheaders[$idx]); $index = $this->strip_value($indexes[$idx]); $indexlast = $this->strip_value($lastindexes[$idx]); 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; } if (!empty($index) && $mod != 'envelope') { $this->form['tests'][$i]['index'] = intval($index); $this->form['tests'][$i]['last'] = !empty($indexlast); } if (empty($dateheader)) { $dateheader = 'Date'; } else if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $dateheader)) { $this->errors['tests'][$i]['dateheader'] = $this->plugin->gettext('forbiddenchars'); } $this->form['tests'][$i]['test'] = 'date'; $this->form['tests'][$i]['type'] = $type; $this->form['tests'][$i]['part'] = $datepart; $this->form['tests'][$i]['arg'] = $target; $this->form['tests'][$i]['header'] = $dateheader; if ($type != 'exists') { if (!count($target)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } else if (strpos($type, 'count-') === 0) { foreach ($target as $arg) { if (preg_match('/[^0-9]/', $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } } else if (strpos($type, 'value-') === 0) { // Some date/time formats do not support i;ascii-numeric comparator if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) { $comparator = ''; } } if (count($target) && !preg_match('/^(regex|matches|count-)/', $type)) { foreach ($target as $arg) { if (!$this->validate_date_part($datepart, $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat'); break; } } } } } 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 (empty($target) && $type != 'exists') { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } else if (preg_match('/^(value|count)-/', $type)) { foreach ($target as $target_value) { if (preg_match('/[^0-9]/', $target_value)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } } $this->form['tests'][$i]['part'] = $trans; if ($trans == 'content') { $this->form['tests'][$i]['content'] = $trans_type; } } else if ($header == 'message') { $test = $this->strip_value($message[$idx]); if (preg_match('/^not/', $test)) { $this->form['tests'][$i]['not'] = true; $test = substr($test, 3); } $this->form['tests'][$i]['test'] = $test; if ($test == 'duplicate') { $this->form['tests'][$i]['last'] = !empty($dup_lasts[$idx]); $this->form['tests'][$i]['handle'] = trim($dup_handles[$idx]); $this->form['tests'][$i]['header'] = trim($dup_headers[$idx]); $this->form['tests'][$i]['uniqueid'] = trim($dup_uniqueids[$idx]); $this->form['tests'][$i]['seconds'] = trim($dup_seconds[$idx]); if ($this->form['tests'][$i]['seconds'] && preg_match('/[^0-9]/', $this->form['tests'][$i]['seconds']) ) { $this->errors['tests'][$i]['duplicate_seconds'] = $this->plugin->gettext('forbiddenchars'); } if ($this->form['tests'][$i]['header'] && $this->form['tests'][$i]['uniqueid']) { $this->errors['tests'][$i]['duplicate_uniqueid'] = $this->plugin->gettext('duplicate.conflict.err'); } } } else { $cust_header = $headers = $this->strip_value(array_shift($cust_headers)); $mod = $this->strip_value($mods[$idx]); $mod_type = $this->strip_value($mod_types[$idx]); $index = $this->strip_value($indexes[$idx]); $indexlast = $this->strip_value($lastindexes[$idx]); if ($header == 'string') { $cust_var = $headers = $this->strip_value(array_shift($cust_vars)); } if (preg_match('/^not/', $operator)) $this->form['tests'][$i]['not'] = true; $type = preg_replace('/^not/', '', $operator); if (!empty($index) && $mod != 'envelope') { $this->form['tests'][$i]['index'] = intval($index); $this->form['tests'][$i]['last'] = !empty($indexlast); } if ($header == '...' || $header == 'string') { if (!count($headers)) $this->errors['tests'][$i]['header'] = $this->plugin->gettext('cannotbeempty'); else if ($header == '...') { foreach ($headers as $hr) { // RFC2822: printable ASCII except colon if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $hr)) { $this->errors['tests'][$i]['header'] = $this->plugin->gettext('forbiddenchars'); } } } if (empty($this->errors['tests'][$i]['header'])) $cust_header = $cust_var = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers; } $test = $header == 'string' ? 'string' : 'header'; $header = $header == 'string' ? $cust_var : $header; $header = $header == '...' ? $cust_header : $header; if (is_array($header)) { foreach ($header as $h_index => $val) { if (isset($this->headers[$val])) { $header[$h_index] = $this->headers[$val]; } } } if ($type == 'exists') { $this->form['tests'][$i]['test'] = 'exists'; $this->form['tests'][$i]['arg'] = $header; } else { 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 (empty($target)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } else if (preg_match('/^(value|count)-/', $type)) { foreach ($target as $target_value) { if (preg_match('/[^0-9]/', $target_value)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } } if ($mod) { $this->form['tests'][$i]['part'] = $mod_type; } } } if ($header != 'size' && $comparator) { $this->form['tests'][$i]['comparator'] = $comparator; } $i++; } } $i = 0; // actions foreach ($act_types as $idx => $type) { $type = $this->strip_value($type); switch ($type) { case 'fileinto': case 'fileinto_copy': $mailbox = $this->strip_value($mailboxes[$idx], false, false); $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->plugin->gettext('cannotbeempty'); break; case 'redirect': case 'redirect_copy': $target = $this->strip_value($act_targets[$idx]); $domain = $this->strip_value($domain_targets[$idx]); // force one of the configured domains $domains = (array) $this->rc->config->get('managesieve_domains'); if (!empty($domains) && !empty($target)) { if (!$domain || !in_array($domain, $domains)) { $domain = $domains[0]; } $target .= '@' . $domain; } $this->form['actions'][$i]['target'] = $target; if ($target == '') $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); else if (!rcube_utils::check_email($target)) $this->errors['actions'][$i]['target'] = $this->plugin->gettext(!empty($domains) ? 'forbiddenchars' : '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->plugin->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]); $interval_type = $interval_types[$idx] == 'seconds' ? 'seconds' : 'days'; $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason); $this->form['actions'][$i]['from'] = $from[$idx]; $this->form['actions'][$i]['subject'] = $subject[$idx]; $this->form['actions'][$i]['addresses'] = array_shift($addresses); $this->form['actions'][$i][$interval_type] = $intervals[$idx]; // @TODO: vacation :mime, :handle foreach ((array)$this->form['actions'][$i]['addresses'] as $aidx => $address) { $this->form['actions'][$i]['addresses'][$aidx] = $address = trim($address); if (empty($address)) { unset($this->form['actions'][$i]['addresses'][$aidx]); } else if (!rcube_utils::check_email($address)) { $this->errors['actions'][$i]['addresses'] = $this->plugin->gettext('noemailwarning'); break; } } if (!empty($this->form['actions'][$i]['from']) && !rcube_utils::check_email($this->form['actions'][$i]['from'])) { $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning'); } if ($this->form['actions'][$i]['reason'] == '') $this->errors['actions'][$i]['reason'] = $this->plugin->gettext('cannotbeempty'); if ($this->form['actions'][$i][$interval_type] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i][$interval_type])) $this->errors['actions'][$i]['interval'] = $this->plugin->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->plugin->gettext('cannotbeempty'); } else if (!preg_match('/^[0-9a-z_]+$/i', $varnames[$idx])) { $this->errors['actions'][$i]['name'] = $this->plugin->gettext('forbiddenchars'); } if (!isset($varvalues[$idx]) || $varvalues[$idx] === '') { $this->errors['actions'][$i]['value'] = $this->plugin->gettext('cannotbeempty'); } break; case 'notify': if (empty($notifymethods[$idx])) { $this->errors['actions'][$i]['method'] = $this->plugin->gettext('cannotbeempty'); } if (empty($notifytargets[$idx])) { $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } if (!empty($notifyfrom[$idx]) && !rcube_utils::check_email($notifyfrom[$idx])) { $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning'); } // skip empty options foreach ((array)$notifyoptions[$idx] as $opt_idx => $opt) { if (!strlen(trim($opt))) { unset($notifyoptions[$idx][$opt_idx]); } } $this->form['actions'][$i]['method'] = $notifymethods[$idx] . ':' . $notifytargets[$idx]; $this->form['actions'][$i]['options'] = $notifyoptions[$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) { // save the script 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' => $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'); } } else { $this->rc->output->show_message('managesieve.filterformerror', 'warning'); } } $this->send(); } protected function 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 if (isset($_GET['_seteditraw']) || isset($_POST['_seteditraw'])) { $this->rc->output->send('managesieve.seteditraw'); } else { $this->rc->output->send('managesieve.filteredit'); } } else { $this->rc->output->set_pagetitle($this->plugin->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 = $this->rc->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