'notifyimportancelow', 2 => 'notifyimportancenormal', 1 => 'notifyimportancehigh' ); private $disabled_actions; const VERSION = '9.0'; const PROGNAME = 'Roundcube (Managesieve)'; const PORT = 4190; /** * Class constructor */ function __construct($plugin) { $this->rc = rcube::get_instance(); $this->plugin = $plugin; $this->headers = $this->get_default_headers(); } /** * 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'), 'filterform' => array($this, 'filter_form'), 'filtersetform' => array($this, 'filterset_form'), 'filterseteditraw' => array($this, 'filterset_editraw'), )); $this->disabled_actions = $this->rc->config->get('managesieve_disabled_actions', array()); // 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' && $mode != 'forward') { 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)); $this->rc->output->set_env('managesieve_disabled_actions', $this->disabled_actions); if (in_array('list_sets', $this->disabled_actions)) $this->rc->output->set_env('managesieve_no_set_list', 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'], $plugin['gssapi_context'], $plugin['gssapi_cn'] ); $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 (!in_array('delete_rule', $this->disabled_actions)) { 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 { $this->rc->output->show_message('managesieve.disabledaction', '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) { if (!in_array('enable_disable_set', $this->disabled_actions)) { $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 { $this->rc->output->show_message('managesieve.disabledaction', 'error'); } } else if ($action == 'deact' && !$error) { if (!in_array('enable_disable_set', $this->disabled_actions)) { $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 { $this->rc->output->show_message('managesieve.disabledaction', 'error'); } } else if ($action == 'setdel' && !$error) { if (!in_array('delete_set', $this->disabled_actions)) { $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 { $this->rc->output->show_message('managesieve.disabledaction', 'error'); } } else if ($action == 'setget') { if (!in_array('download_set', $this->disabled_actions)) { $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, $_SESSION['managesieve-compact-form']); $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)) { $tests = array(); foreach ($rules as $rule) { list($header, $value) = explode(':', $rule, 2); $tests[] = array( 'type' => 'contains', 'test' => 'header', 'arg1' => $header, 'arg2' => $value, ); } $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 (in_array('new_set', $this->disabled_actions)) { $error = 'managesieve.disabledaction'; } else 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); $mime_parts = rcube_utils::get_input_value('_rule_mime_part', rcube_utils::INPUT_POST); $mime_types = rcube_utils::get_input_value('_rule_mime_type', rcube_utils::INPUT_POST); $mime_params = rcube_utils::get_input_value('_rule_mime_param', rcube_utils::INPUT_POST, true); $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 (empty($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) && !empty($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 (empty($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 (!empty($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]); $mime_param = $this->strip_value($mime_params[$idx]); $mime_type = $mime_types[$idx]; $mime_part = $mime_parts[$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 ($test == 'header') { if (in_array($mime_type, array('type', 'subtype', 'contenttype', 'param'))) { $this->form['tests'][$i]['mime-' . $mime_type] = true; if ($mime_type == 'param') { if (empty($mime_param)) { $this->errors['tests'][$i]['mime-param'] = $this->plugin->gettext('cannotbeempty'); } $this->form['tests'][$i]['mime-param'] = $mime_param; } } if ($mime_part == 'anychild') { $this->form['tests'][$i]['mime-anychild'] = true; } } } 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 ', rcube::Q($this->plugin->gettext('filterdisabled'))); } if ($compact) { $select = new html_select(array('name' => '_join', 'id' => '_join' . $id, 'onchange' => 'rule_join_radio(this.value)')); foreach (array('allof', 'anyof', 'any') as $val) { $select->add($this->plugin->gettext('filter' . $val), $val); } $join = $any ? 'any' : 'allof'; if (isset($scr) && !$any) { $join = $scr['join'] ? 'allof' : 'anyof'; } $out .= sprintf("\n" . '
' . '' . '
%s
', rcube::Q($this->plugin->gettext('scope')), $select->show($join)); $out .= '\n" : "\n\n"; // actions $label = $this->plugin->gettext($compact ? 'actions' : 'messagesactions'); $out .= '
' . rcube::Q($label) . "\n"; $rows_num = isset($scr) ? count($scr['actions']) : 1; $out .= '
'; for ($x=0; $x<$rows_num; $x++) $out .= $this->action_div($fid, $x); $out .= "
\n"; $out .= "
\n"; $this->print_tips(); 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'); $attrib['name'] = 'filterform'; $attrib['action'] = './'; $attrib['method'] = 'post'; $out = html::tag('form', $attrib, $out, array('name', 'action', 'method', 'class')); if (!$compact) { $out = str_replace('', '', $out); } return $out; } function rule_div($fid, $id, $div = true, $compact = false) { $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id]; if (isset($this->form['tests'])) { $rows_num = count($this->form['tests']); } else if (isset($this->script[$fid]['tests'])) { $rows_num = count($this->script[$fid]['tests']); } else { $rows_num = 0; } // headers select $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id, 'onchange' => 'rule_header_select(' .$id .')')); foreach ($this->headers as $index => $header) { $header = $this->rc->text_exists($index) ? $this->plugin->gettext($index) : $header; $select_header->add($header, $index); } $select_header->add($this->plugin->gettext('...'), '...'); if (in_array('body', $this->exts)) { $select_header->add($this->plugin->gettext('body'), 'body'); } $select_header->add($this->plugin->gettext('size'), 'size'); if (in_array('date', $this->exts)) { $select_header->add($this->plugin->gettext('datetest'), 'date'); $select_header->add($this->plugin->gettext('currdate'), 'currentdate'); } if (in_array('variables', $this->exts)) { $select_header->add($this->plugin->gettext('string'), 'string'); } if (in_array('duplicate', $this->exts)) { $select_header->add($this->plugin->gettext('message'), 'message'); } if (isset($rule['test'])) { if (in_array($rule['test'], array('header', 'address', 'envelope'))) { if (is_array($rule['arg1']) && count($rule['arg1']) == 1) { $rule['arg1'] = $rule['arg1'][0]; } $matches = !is_array($rule['arg1']) && ($header = strtolower($rule['arg1'])) && isset($this->headers[$header]); $test = $matches ? $header : '...'; } else if ($rule['test'] == 'exists') { if (is_array($rule['arg']) && count($rule['arg']) == 1) { $rule['arg'] = $rule['arg'][0]; } $matches = !is_array($rule['arg']) && ($header = strtolower($rule['arg'])) && isset($this->headers[$header]); $test = $matches ? $header : '...'; } else if (in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'string'))) { $test = $rule['test']; } else if (in_array($rule['test'], array('duplicate'))) { $test = 'message'; } else if ($rule['test'] != 'true') { $test = '...'; } } $tout = '
'; $aout = $select_header->show($test); // custom headers input if (isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) { $custom = (array) $rule['arg1']; if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) { unset($custom); } } else if (isset($rule['test']) && $rule['test'] == 'string') { $customv = (array) $rule['arg1']; if (count($customv) == 1 && isset($this->headers[strtolower($customv[0])])) { unset($customv); } } else if (isset($rule['test']) && $rule['test'] == 'exists') { $custom = (array) $rule['arg']; if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) { unset($custom); } } // custom header and variable inputs $aout .= $this->list_input($id, 'custom_header', $custom, isset($custom), $this->error_class($id, 'test', 'header', 'custom_header'), 15) . "\n"; $aout .= $this->list_input($id, 'custom_var', $customv, isset($customv), $this->error_class($id, 'test', 'header', 'custom_var'), 15) . "\n"; // matching type select (operator) $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id, 'style' => 'display:' .(!in_array($rule['test'], array('size', 'duplicate')) ? 'inline' : 'none'), 'class' => 'operator_selector', 'onchange' => 'rule_op_select(this, '.$id.')')); $select_op->add(rcube::Q($this->plugin->gettext('filtercontains')), 'contains'); $select_op->add(rcube::Q($this->plugin->gettext('filternotcontains')), 'notcontains'); $select_op->add(rcube::Q($this->plugin->gettext('filteris')), 'is'); $select_op->add(rcube::Q($this->plugin->gettext('filterisnot')), 'notis'); $select_op->add(rcube::Q($this->plugin->gettext('filterexists')), 'exists'); $select_op->add(rcube::Q($this->plugin->gettext('filternotexists')), 'notexists'); $select_op->add(rcube::Q($this->plugin->gettext('filtermatches')), 'matches'); $select_op->add(rcube::Q($this->plugin->gettext('filternotmatches')), 'notmatches'); if (in_array('regex', $this->exts)) { $select_op->add(rcube::Q($this->plugin->gettext('filterregex')), 'regex'); $select_op->add(rcube::Q($this->plugin->gettext('filternotregex')), 'notregex'); } if (in_array('relational', $this->exts)) { $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthan')), 'count-gt'); $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthanequal')), 'count-ge'); $select_op->add(rcube::Q($this->plugin->gettext('countislessthan')), 'count-lt'); $select_op->add(rcube::Q($this->plugin->gettext('countislessthanequal')), 'count-le'); $select_op->add(rcube::Q($this->plugin->gettext('countequals')), 'count-eq'); $select_op->add(rcube::Q($this->plugin->gettext('countnotequals')), 'count-ne'); $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthan')), 'value-gt'); $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthanequal')), 'value-ge'); $select_op->add(rcube::Q($this->plugin->gettext('valueislessthan')), 'value-lt'); $select_op->add(rcube::Q($this->plugin->gettext('valueislessthanequal')), 'value-le'); $select_op->add(rcube::Q($this->plugin->gettext('valueequals')), 'value-eq'); $select_op->add(rcube::Q($this->plugin->gettext('valuenotequals')), 'value-ne'); } $test = self::rule_test($rule); $target = ''; // target(s) input if (in_array($rule['test'], array('header', 'address', 'envelope','string'))) { $target = $rule['arg2']; } else if (in_array($rule['test'], array('body', 'date', 'currentdate'))) { $target = $rule['arg']; } else if ($rule['test'] == 'size') { if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) { $sizetarget = $matches[1]; $sizeitem = $matches[2]; } else { $sizetarget = $rule['arg']; $sizeitem = $rule['item']; } } // (current)date part select if (in_array('date', $this->exts) || in_array('currentdate', $this->exts)) { $date_parts = array('date', 'iso8601', 'std11', 'julian', 'time', 'year', 'month', 'day', 'hour', 'minute', 'second', 'weekday', 'zone'); $select_dp = new html_select(array('name' => "_rule_date_part[]", 'id' => 'rule_date_part'.$id, 'style' => in_array($rule['test'], array('currentdate', 'date')) && !preg_match('/^(notcount|count)-/', $test) ? '' : 'display:none', 'class' => 'datepart_selector', )); foreach ($date_parts as $part) { $select_dp->add(rcube::Q($this->plugin->gettext($part)), $part); } $aout .= $select_dp->show($rule['test'] == 'currentdate' || $rule['test'] == 'date' ? $rule['part'] : ''); } // message test select (e.g. duplicate) if (in_array('duplicate', $this->exts)) { $select_msg = new html_select(array('name' => "_rule_message[]", 'id' => 'rule_message'.$id, 'style' => in_array($rule['test'], array('duplicate')) ? '' : 'display:none', 'class' => 'message_selector', )); $select_msg->add(rcube::Q($this->plugin->gettext('duplicate')), 'duplicate'); $select_msg->add(rcube::Q($this->plugin->gettext('notduplicate')), 'notduplicate'); $tout .= $select_msg->show($test); } $tout .= $select_op->show($test); $tout .= $this->list_input($id, 'rule_target', $target, $rule['test'] != 'size' && $rule['test'] != 'exists' && $rule['test'] != 'duplicate', $this->error_class($id, 'test', 'target', 'rule_target')) . "\n"; $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id, 'class' => 'input-group-prepend')); $select_size_op->add(rcube::Q($this->plugin->gettext('filterover')), 'over'); $select_size_op->add(rcube::Q($this->plugin->gettext('filterunder')), 'under'); $select_size_item = new html_select(array('name' => "_rule_size_item[]", 'id' => 'rule_size_item'.$id, 'class' => 'input-group-append')); foreach (array('', 'K', 'M', 'G') as $unit) { $select_size_item->add($this->plugin->gettext($unit . 'B'), $unit); } $tout .= '
'; $tout .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : ''); $tout .= html::tag('input', array( 'type' => 'text', 'name' => '_rule_size_target[]', 'id' => 'rule_size_i'.$id, 'value' => $sizetarget, 'size' => 10, 'class' => $this->error_class($id, 'test', 'sizetarget', 'rule_size_i'), )); $tout .= "\n" . $select_size_item->show($sizeitem); $tout .= '
'; $tout .= '
'; // Advanced modifiers (address, envelope) $select_mod = new html_select(array('name' => "_rule_mod[]", 'id' => 'rule_mod_op'.$id, 'onchange' => 'rule_mod_select(' .$id .')')); $select_mod->add(rcube::Q($this->plugin->gettext('none')), ''); $select_mod->add(rcube::Q($this->plugin->gettext('address')), 'address'); if (in_array('envelope', $this->exts)) { $select_mod->add(rcube::Q($this->plugin->gettext('envelope')), 'envelope'); } $select_type = new html_select(array('name' => "_rule_mod_type[]", 'id' => 'rule_mod_type'.$id)); $select_type->add(rcube::Q($this->plugin->gettext('allparts')), 'all'); $select_type->add(rcube::Q($this->plugin->gettext('domain')), 'domain'); $select_type->add(rcube::Q($this->plugin->gettext('localpart')), 'localpart'); if (in_array('subaddress', $this->exts)) { $select_type->add(rcube::Q($this->plugin->gettext('user')), 'user'); $select_type->add(rcube::Q($this->plugin->gettext('detail')), 'detail'); } $need_mod = !in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'duplicate', 'string')); $mout = ''; $mout .= ''; // Advanced modifiers (comparators) $select_comp = new html_select(array('name' => "_rule_comp[]", 'id' => 'rule_comp_op'.$id)); $select_comp->add(rcube::Q($this->plugin->gettext('default')), ''); $select_comp->add(rcube::Q($this->plugin->gettext('octet')), 'i;octet'); $select_comp->add(rcube::Q($this->plugin->gettext('asciicasemap')), 'i;ascii-casemap'); if (in_array('comparator-i;ascii-numeric', $this->exts)) { $select_comp->add(rcube::Q($this->plugin->gettext('asciinumeric')), 'i;ascii-numeric'); } $need_comp = $rule['test'] != 'size' && $rule['test'] != 'duplicate'; $mout .= ''; // Advanced modifiers (mime) if (in_array('mime', $this->exts)) { $need_mime = !$rule || in_array($rule['test'], array('header', 'address', 'exists')); $mime_type = ''; $select_mime = new html_select(array('name' => '_rule_mime_type[]', 'id' => 'rule_mime_type' . $id, 'style' => 'min-width:8em', 'onchange' => 'rule_mime_select(' . $id . ')')); $select_mime->add('-', ''); foreach (array('contenttype', 'type', 'subtype', 'param') as $val) { if (isset($rule['mime-' . $val])) { $mime_type = $val; } $select_mime->add(rcube::Q($this->plugin->gettext('mime-' . $val)), $val); } $select_mime_part = new html_select(array('name' => '_rule_mime_part[]', 'id' => 'rule_mime_part' . $id)); $select_mime_part->add(rcube::Q($this->plugin->gettext('mime-message')), ''); $select_mime_part->add(rcube::Q($this->plugin->gettext('mime-anychild')), 'anychild'); $mout .= ''; $mout .= ''; } // Advanced modifiers (body transformations) $select_mod = new html_select(array('name' => "_rule_trans[]", 'id' => 'rule_trans_op'.$id, 'onchange' => 'rule_trans_select(' .$id .')')); $select_mod->add(rcube::Q($this->plugin->gettext('text')), 'text'); $select_mod->add(rcube::Q($this->plugin->gettext('undecoded')), 'raw'); $select_mod->add(rcube::Q($this->plugin->gettext('contenttype')), 'content'); $mout .= ''; // Date header if (in_array('date', $this->exts)) { $mout .= ''; } // Index if (in_array('index', $this->exts)) { $need_index = in_array($rule['test'], array('header', ', address', 'date')); $mout .= ''; } // Duplicate if (in_array('duplicate', $this->exts)) { $need_duplicate = $rule['test'] == 'duplicate'; $mout .= ''; } $add_title = rcube::Q($this->plugin->gettext('add')); $del_title = rcube::Q($this->plugin->gettext('del')); $adv_title = rcube::Q($this->plugin->gettext('advancedopts')); // Build output table $out = $div ? '
'."\n" : ''; $out .= ''; if (!$compact) { $out .= ''; } $out .= ''; $out .= ''; $out .= ''; $out .= '
'; $out .= sprintf('' . '%s', $id, $adv_title, $id, $adv_title); $out .= '
' . $aout . '
' . $tout . "\n"; $out .= ''; $out .= ''; if ($compact) { $out .= sprintf('' . '%s', $id, $adv_title, $id, $adv_title); } $out .= sprintf('' . '%s', $id, $add_title, $id, $add_title); $out .= sprintf('' . '%s', $id, $del_title, $id, ($rows_num < 2 ? ' disabled' : ''), $del_title); $out .= '
'; $out .= $div ? "
\n" : ''; return $out; } private static function rule_test(&$rule) { // first modify value/count tests with 'not' keyword // we'll revert the meaning of operators if ($rule['not'] && preg_match('/^(count|value)-([gteqnl]{2})/', $rule['type'], $m)) { $rule['not'] = false; switch ($m[2]) { case 'gt': $rule['type'] = $m[1] . '-le'; break; case 'ge': $rule['type'] = $m[1] . '-lt'; break; case 'lt': $rule['type'] = $m[1] . '-ge'; break; case 'le': $rule['type'] = $m[1] . '-gt'; break; case 'eq': $rule['type'] = $m[1] . '-ne'; break; case 'ne': $rule['type'] = $m[1] . '-eq'; break; } } else if ($rule['not'] && $rule['test'] == 'size') { $rule['not'] = false; $rule['type'] = $rule['type'] == 'over' ? 'under' : 'over'; } $set = array('header', 'address', 'envelope', 'body', 'date', 'currentdate', 'string'); // build test string supported by select element if ($rule['size']) { $test = $rule['type']; } else if (in_array($rule['test'], $set)) { $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ?: 'is'); } else { $test = ($rule['not'] ? 'not' : '') . $rule['test']; } return $test; } function action_div($fid, $id, $div=true) { $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id]; if (isset($this->form['actions'])) { $rows_num = count($this->form['actions']); } else if (isset($this->script[$fid]['actions'])) { $rows_num = count($this->script[$fid]['actions']); } else { $rows_num = 0; } $out = $div ? '
'."\n" : ''; $out .= ''; // actions target inputs $out .= ''; // add/del buttons $add_label = rcube::Q($this->plugin->gettext('add')); $del_label = rcube::Q($this->plugin->gettext('del')); $out .= ''; $out .= '
'; // action select $select_action = new html_select(array('name' => "_action_type[$id]", 'id' => 'action_type'.$id, 'onchange' => 'action_type_select(' .$id .')')); if (in_array('fileinto', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagemoveto')), 'fileinto'); if (in_array('fileinto', $this->exts) && in_array('copy', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagecopyto')), 'fileinto_copy'); $select_action->add(rcube::Q($this->plugin->gettext('messageredirect')), 'redirect'); if (in_array('copy', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagesendcopy')), 'redirect_copy'); if (in_array('reject', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'reject'); else if (in_array('ereject', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'ereject'); if (in_array('vacation', $this->exts)) $select_action->add(rcube::Q($this->plugin->gettext('messagereply')), 'vacation'); $select_action->add(rcube::Q($this->plugin->gettext('messagedelete')), 'discard'); if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) { $select_action->add(rcube::Q($this->plugin->gettext('setflags')), 'setflag'); $select_action->add(rcube::Q($this->plugin->gettext('addflags')), 'addflag'); $select_action->add(rcube::Q($this->plugin->gettext('removeflags')), 'removeflag'); } if (in_array('variables', $this->exts)) { $select_action->add(rcube::Q($this->plugin->gettext('setvariable')), 'set'); } if (in_array('enotify', $this->exts) || in_array('notify', $this->exts)) { $select_action->add(rcube::Q($this->plugin->gettext('notify')), 'notify'); } $select_action->add(rcube::Q($this->plugin->gettext('messagekeep')), 'keep'); $select_action->add(rcube::Q($this->plugin->gettext('rulestop')), 'stop'); $select_type = $action['type']; if (in_array($action['type'], array('fileinto', 'redirect')) && $action['copy']) { $select_type .= '_copy'; } $out .= $select_action->show($select_type); $out .= ''; // force domain selection in redirect email input $domains = (array) $this->rc->config->get('managesieve_domains'); if (!empty($domains)) { sort($domains); $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id)); $domain_select->add(array_combine($domains, $domains)); if ($action['type'] == 'redirect') { $parts = explode('@', $action['target']); if (!empty($parts)) { $action['domain'] = array_pop($parts); $action['target'] = implode('@', $parts); } } } // redirect target $out .= '' . html::tag('input', array( 'type' => 'text', 'name' => '_action_target[' . $id . ']', 'id' => 'action_target' . $id, 'value' => $action['type'] == 'redirect' ? $action['target'] : '', 'size' => !empty($domains) ? 20 : 35, 'class' => $this->error_class($id, 'action', 'target', 'action_target'), )); $out .= !empty($domains) ? ' @ ' . $domain_select->show($action['domain']) : ''; $out .= ''; // (e)reject target $out .= html::tag('textarea', array( 'name' => '_action_target_area[' . $id . ']', 'id' => 'action_target_area' . $id, 'rows' => 3, 'cols' => 35, 'class' => $this->error_class($id, 'action', 'targetarea', 'action_target_area'), 'style' => 'display:' . (in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none'), ), (in_array($action['type'], array('reject', 'ereject')) ? rcube::Q($action['target'], 'strict', false) : '')); // vacation $vsec = in_array('vacation-seconds', $this->exts); $auto_addr = $this->rc->config->get('managesieve_vacation_addresses_init'); $from_addr = $this->rc->config->get('managesieve_vacation_from_init'); if (empty($action)) { if ($auto_addr) { $action['addresses'] = $this->user_emails(); } if ($from_addr) { $default_identity = $this->rc->user->list_emails(true); $action['from'] = $default_identity['email']; } } $out .= '
'; $out .= ''. rcube::Q($this->plugin->gettext('vacationreason')) .'
'; $out .= html::tag('textarea', array( 'name' => '_action_reason[' . $id . ']', 'id' => 'action_reason' . $id, 'rows' => 3, 'cols' => 35, 'class' => $this->error_class($id, 'action', 'reason', 'action_reason'), ), rcube::Q($action['reason'], 'strict', false)); $out .= '
' .rcube::Q($this->plugin->gettext('vacationsubject')) . '
'; $out .= html::tag('input', array( 'type' => 'text', 'name' => '_action_subject[' . $id . ']', 'id' => 'action_subject' . $id, 'value' => is_array($action['subject']) ? implode(', ', $action['subject']) : $action['subject'], 'size' => 35, 'class' => $this->error_class($id, 'action', 'subject', 'action_subject'), )); $out .= '
' .rcube::Q($this->plugin->gettext('vacationfrom')) . '
'; $out .= html::tag('input', array( 'type' => 'text', 'name' => '_action_from[' . $id . ']', 'id' => 'action_from' . $id, 'value' => $action['from'], 'size' => 35, 'class' => $this->error_class($id, 'action', 'from', 'action_from'), )); $out .= '
' .rcube::Q($this->plugin->gettext('vacationaddr')) . '
'; $out .= $this->list_input($id, 'action_addresses', $action['addresses'], true, $this->error_class($id, 'action', 'addresses', 'action_addresses'), 30) . html::a(array('href' => '#', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".managesieve_vacation_addresses($id)"), rcube::Q($this->plugin->gettext('filladdresses'))); $out .= '
' . rcube::Q($this->plugin->gettext('vacationinterval')) . '
'; $out .= '
' . html::tag('input', array( 'type' => 'text', 'name' => '_action_interval[' . $id . ']', 'id' => 'action_interval' . $id, 'value' => rcube_sieve_vacation::vacation_interval($action), 'size' => 2, 'class' => $this->error_class($id, 'action', 'interval', 'action_interval'), )); if ($vsec) { $interval_select = new html_select(array('name' => '_action_interval_type[' . $id . ']', 'class' => 'input-group-append')); $interval_select->add($this->plugin->gettext('days'), 'days'); $interval_select->add($this->plugin->gettext('seconds'), 'seconds'); $out .= $interval_select->show(isset($action['seconds']) ? 'seconds' : 'days'); } else { $out .= "\n" . html::span('input-group-append', html::span('input-group-text', $this->plugin->gettext('days'))); } $out .= '
'; // flags $flags = array( 'read' => '\\Seen', 'answered' => '\\Answered', 'flagged' => '\\Flagged', 'deleted' => '\\Deleted', 'draft' => '\\Draft', ); $flags_target = (array) $action['target']; $custom_flags = array(); $is_flag_action = preg_match('/^(set|add|remove)flag$/', $action['type']); if ($is_flag_action) { $custom_flags = array_filter($flags_target, function($v) use($flags) { return !in_array_nocase($v, $flags); }); } $flout = ''; foreach ($flags as $fidx => $flag) { $flout .= html::label(null, html::tag('input', array( 'type' => 'checkbox', 'name' => '_action_flags[' .$id .'][]', 'value' => $flag, 'checked' => $is_flag_action && in_array_nocase($flag, $flags_target), )) . rcube::Q($this->plugin->gettext('flag'.$fidx))) . '
'; } $flout .= $this->list_input($id, 'action_flags', $custom_flags, true, $this->error_class($id, 'action', 'flags', 'action_flags')); $out .= html::div(array( 'id' => 'action_flags' . $id, 'style' => 'display:' . ($is_flag_action ? 'inline' : 'none'), 'class' => trim('checklist ' . $this->error_class($id, 'action', 'flags', 'action_flags')), ), $flout); // set variable $set_modifiers = array( 'lower', 'upper', 'lowerfirst', 'upperfirst', 'quotewildcard', 'length' ); $out .= '
'; foreach (array('name', 'value') as $unit) { $out .= '' .rcube::Q($this->plugin->gettext('setvar' . $unit)) . '
'; $out .= html::tag('input', array( 'type' => 'text', 'name' => '_action_var' . $unit . '[' . $id . ']', 'id' => 'action_var' . $unit . $id, 'value' => $action[$unit], 'size' => 35, 'class' => $this->error_class($id, 'action', $unit, 'action_var' . $unit), )); $out .= '
'; } $out .= '' .rcube::Q($this->plugin->gettext('setvarmodifiers')) . ''; foreach ($set_modifiers as $s_m) { $s_m_id = 'action_varmods' . $id . $s_m; $out .= '
' . html::tag('input', array( 'type' => 'checkbox', 'name' => '_action_varmods[' . $id . '][]', 'value' => $s_m, 'id' => $s_m_id, 'checked' => array_key_exists($s_m, (array)$action) && $action[$s_m], )) .rcube::Q($this->plugin->gettext('var' . $s_m)); } $out .= '
'; // notify $notify_methods = (array) $this->rc->config->get('managesieve_notify_methods'); $importance_options = $this->notify_importance_options; if (empty($notify_methods)) { $notify_methods = $this->notify_methods; } list($method, $target) = explode(':', $action['method'], 2); $method = strtolower($method); if ($method && !in_array($method, $notify_methods)) { $notify_methods[] = $method; } $select_method = new html_select(array( 'name' => "_action_notifymethod[$id]", 'id' => "_action_notifymethod$id", 'class' => 'input-group-prepend ' . $this->error_class($id, 'action', 'method', 'action_notifymethod'), )); foreach ($notify_methods as $m_n) { $select_method->add(rcube::Q($this->rc->text_exists('managesieve.notifymethod'.$m_n) ? $this->plugin->gettext('managesieve.notifymethod'.$m_n) : $m_n), $m_n); } $select_importance = new html_select(array( 'name' => "_action_notifyimportance[$id]", 'id' => "_action_notifyimportance$id", 'class' => $this->error_class($id, 'action', 'importance', 'action_notifyimportance') )); foreach ($importance_options as $io_v => $io_n) { $select_importance->add(rcube::Q($this->plugin->gettext($io_n)), $io_v); } // @TODO: nice UI for mailto: (other methods too) URI parameters $out .= '
'; $out .= '' .rcube::Q($this->plugin->gettext('notifytarget')) . '
'; $out .= '
'; $out .= $select_method->show($method); $out .= html::tag('input', array( 'type' => 'text', 'name' => '_action_notifytarget[' . $id . ']', 'id' => 'action_notifytarget' . $id, 'value' => $target, 'size' => 25, 'class' => $this->error_class($id, 'action', 'target', 'action_notifytarget'), )); $out .= '
'; $out .= '
'. rcube::Q($this->plugin->gettext('notifymessage')) .'
'; $out .= html::tag('textarea', array( 'name' => '_action_notifymessage[' . $id . ']', 'id' => 'action_notifymessage' . $id, 'rows' => 3, 'cols' => 35, 'class' => $this->error_class($id, 'action', 'message', 'action_notifymessage'), ), rcube::Q($action['message'], 'strict', false)); if (in_array('enotify', $this->exts)) { $out .= '
' .rcube::Q($this->plugin->gettext('notifyfrom')) . '
'; $out .= html::tag('input', array( 'type' => 'text', 'name' => '_action_notifyfrom[' . $id . ']', 'id' => 'action_notifyfrom' . $id, 'value' => $action['from'], 'size' => 35, 'class' => $this->error_class($id, 'action', 'from', 'action_notifyfrom'), )); } $out .= '
' . rcube::Q($this->plugin->gettext('notifyimportance')) . '
'; $out .= $select_importance->show($action['importance'] ? (int) $action['importance'] : 2); $out .= '
' .'' . rcube::Q($this->plugin->gettext('notifyoptions')) . '
' .$this->list_input($id, 'action_notifyoption', (array)$action['options'], true, $this->error_class($id, 'action', 'options', 'action_notifyoption'), 30) . '
'; $out .= '
'; // mailbox select if ($action['type'] == 'fileinto') { $mailbox = $this->mod_mailbox($action['target'], 'out'); // make sure non-existing (or unsubscribed) mailbox is listed (#1489956) $additional = array($mailbox); } else { $mailbox = ''; } $select = $this->rc->folder_selector(array( 'realnames' => false, 'maxlength' => 100, 'id' => 'action_mailbox' . $id, 'name' => "_action_mailbox[$id]", 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none'), 'additional' => $additional, )); $out .= $select->show($mailbox); $out .= '
'; $out .= sprintf('' . '%s', $id, $add_label, $id, $add_label); $out .= sprintf('' . '%s', $id, $del_label, $id, ($rows_num < 2 ? ' disabled' : ''), $del_label); $out .= '
'; $out .= $div ? "
\n" : ''; return $out; } protected function genid() { return preg_replace('/[^0-9]/', '', microtime(true)); } protected function strip_value($str, $allow_html = false, $trim = true) { if (is_array($str)) { foreach ($str as $idx => $val) { $val = $this->strip_value($val, $allow_html, $trim); if ($val === '') { unset($str[$idx]); } } return $str; } if (!$allow_html) { $str = strip_tags($str); } return $trim ? trim($str) : $str; } protected function error_class($id, $type, $target, $elem_prefix='') { // TODO: tooltips if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) || ($type == 'action' && ($str = $this->errors['actions'][$id][$target])) ) { $this->add_tip($elem_prefix.$id, $str, true); return 'error'; } return ''; } protected function add_tip($id, $str, $error=false) { if ($error) { $class = 'sieve error'; } $this->tips[] = array($id, $class ?: '', $str); } protected function print_tips() { if (empty($this->tips)) { return; } $script = rcmail_output::JS_OBJECT_NAME.'.managesieve_tip_register('.json_encode($this->tips).');'; $this->rc->output->add_script($script, 'foot'); } protected function list_input($id, $name, $value, $enabled, $class, $size = null, $hidden = false) { $value = (array) $value; $value = array_map(array('rcube', 'Q'), $value); $value = implode("\n", $value); return html::tag('textarea', array( 'data-type' => 'list', 'data-size' => $size, 'data-hidden' => $hidden ?: null, 'name' => '_' . $name . '['. $id .']', 'id' => $name.$id, 'disabled' => !$enabled, 'class' => $class, 'style' => 'display:none', ), $value); } /** * Validate input for date part elements */ protected function validate_date_part($type, $value) { // we do simple validation of date/part format switch ($type) { case 'date': // yyyy-mm-dd return preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value); case 'iso8601': return preg_match('/^[0-9: .,ZWT+-]+$/', $value); case 'std11': return preg_match('/^((Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+)?[0-9]{1,2}\s+' . '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+[0-9]{2,4}\s+' . '[0-9]{2}:[0-9]{2}(:[0-9]{2})?\s+([+-]*[0-9]{4}|[A-Z]{1,3})$', $value); case 'julian': return preg_match('/^[0-9]+$/', $value); case 'time': // hh:mm:ss return preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $value); case 'year': return preg_match('/^[0-9]{4}$/', $value); case 'month': return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 13; case 'day': return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 32; case 'hour': return preg_match('/^[0-9]{2}$/', $value) && $value < 24; case 'minute': return preg_match('/^[0-9]{2}$/', $value) && $value < 60; case 'second': // According to RFC5260, seconds can be from 00 to 60 return preg_match('/^[0-9]{2}$/', $value) && $value < 61; case 'weekday': return preg_match('/^[0-9]$/', $value) && $value < 7; case 'zone': return preg_match('/^[+-][0-9]{4}$/', $value); } } /** * Converts mailbox name from/to UTF7-IMAP from/to internal Sieve encoding * with delimiter replacement. * * @param string $mailbox Mailbox name * @param string $mode Conversion direction ('in'|'out') * * @return string Mailbox name */ protected function mod_mailbox($mailbox, $mode = 'out') { $delimiter = $_SESSION['imap_delimiter']; $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter'); $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP'); if ($mode == 'out') { $mailbox = rcube_charset::convert($mailbox, $mbox_encoding, 'UTF7-IMAP'); if ($replace_delimiter && $replace_delimiter != $delimiter) $mailbox = str_replace($replace_delimiter, $delimiter, $mailbox); } else { $mailbox = rcube_charset::convert($mailbox, 'UTF7-IMAP', $mbox_encoding); if ($replace_delimiter && $replace_delimiter != $delimiter) $mailbox = str_replace($delimiter, $replace_delimiter, $mailbox); } return $mailbox; } /** * List sieve scripts * * @return array Scripts list */ public function list_scripts() { if ($this->list !== null) { return $this->list; } $this->list = $this->sieve->get_scripts(); // Handle active script(s) and list of scripts according to Kolab's KEP:14 if ($this->rc->config->get('managesieve_kolab_master')) { // Skip protected names foreach ((array)$this->list as $idx => $name) { $_name = strtoupper($name); if ($_name == 'MASTER') $master_script = $name; else if ($_name == 'MANAGEMENT') $management_script = $name; else if($_name == 'USER') $user_script = $name; else continue; unset($this->list[$idx]); } // get active script(s), read USER script if ($user_script) { $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); $filename_regex = '/'.preg_quote($extension, '/').'$/'; $_SESSION['managesieve_user_script'] = $user_script; $this->sieve->load($user_script); foreach ($this->sieve->script->as_array() as $rules) { foreach ($rules['actions'] as $action) { if ($action['type'] == 'include' && empty($action['global'])) { $name = preg_replace($filename_regex, '', $action['target']); // make sure the script exist if (in_array($name, $this->list)) { $this->active[] = $name; } } } } } // create USER script if it doesn't exist else { $content = "# USER Management Script\n" ."#\n" ."# This script includes the various active sieve scripts\n" ."# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n" ."#\n" ."# For more information, see http://wiki.kolab.org/KEP:14#USER\n" ."#\n"; if ($this->sieve->save_script('USER', $content)) { $_SESSION['managesieve_user_script'] = 'USER'; if (empty($this->master_file)) $this->sieve->activate('USER'); } } } else if (!empty($this->list)) { // Get active script name if ($active = $this->sieve->get_active()) { $this->active = array($active); } // Hide scripts from config $exceptions = $this->rc->config->get('managesieve_filename_exceptions'); if (!empty($exceptions)) { $this->list = array_diff($this->list, (array)$exceptions); } } // When no script listing allowed limit the list to the defined script if (in_array('list_sets', $this->disabled_actions)) { $script_name = $this->rc->config->get('managesieve_script_name', 'roundcube'); $this->list = array_intersect($this->list, array($script_name)); $this->active = null; if (in_array($script_name, $this->list)) { // Because its the only allowed script make sure its active $this->activate_script($script_name); } } // reindex if (!empty($this->list)) { $this->list = array_values($this->list); } return $this->list; } /** * Removes sieve script * * @param string $name Script name * * @return bool True on success, False on failure */ public function remove_script($name) { $result = $this->sieve->remove($name); // Kolab's KEP:14 if ($result && $this->rc->config->get('managesieve_kolab_master')) { $this->deactivate_script($name); } return $result; } /** * Activates sieve script * * @param string $name Script name * * @return bool True on success, False on failure */ public function activate_script($name) { // Kolab's KEP:14 if ($this->rc->config->get('managesieve_kolab_master')) { $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); $user_script = $_SESSION['managesieve_user_script']; // if the script is not active... if ($user_script && array_search($name, (array) $this->active) === false) { // ...rewrite USER file adding appropriate include command if ($this->sieve->load($user_script)) { $script = $this->sieve->script->as_array(); $list = array(); $regexp = '/' . preg_quote($extension, '/') . '$/'; // Create new include entry $rule = array( 'actions' => array( 0 => array( 'target' => $name.$extension, 'type' => 'include', 'personal' => true, ))); // get all active scripts for sorting foreach ($script as $rid => $rules) { foreach ($rules['actions'] as $action) { if ($action['type'] == 'include' && empty($action['global'])) { $target = $extension ? preg_replace($regexp, '', $action['target']) : $action['target']; $list[] = $target; } } } $list[] = $name; // Sort and find current script position asort($list, SORT_LOCALE_STRING); $list = array_values($list); $index = array_search($name, $list); // add rule at the end of the script if ($index === false || $index == count($list)-1) { $this->sieve->script->add_rule($rule); } // add rule at index position else { $script2 = array(); foreach ($script as $rid => $rules) { if ($rid == $index) { $script2[] = $rule; } $script2[] = $rules; } $this->sieve->script->content = $script2; } $result = $this->sieve->save(); if ($result) { $this->active[] = $name; } } } } else { $result = $this->sieve->activate($name); if ($result) $this->active = array($name); } return $result; } /** * Deactivates sieve script * * @param string $name Script name * * @return bool True on success, False on failure */ public function deactivate_script($name) { // Kolab's KEP:14 if ($this->rc->config->get('managesieve_kolab_master')) { $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve'); $user_script = $_SESSION['managesieve_user_script']; // if the script is active... if ($user_script && ($key = array_search($name, $this->active)) !== false) { // ...rewrite USER file removing appropriate include command if ($this->sieve->load($user_script)) { $script = $this->sieve->script->as_array(); $name = $name.$extension; foreach ($script as $rid => $rules) { foreach ($rules['actions'] as $action) { if ($action['type'] == 'include' && empty($action['global']) && $action['target'] == $name ) { break 2; } } } // Entry found if ($rid < count($script)) { $this->sieve->script->delete_rule($rid); $result = $this->sieve->save(); if ($result) { unset($this->active[$key]); } } } } } else { $result = $this->sieve->deactivate(); if ($result) $this->active = array(); } return $result; } /** * Saves current script (adding some variables) */ public function save_script($name = null) { // Kolab's KEP:14 if ($this->rc->config->get('managesieve_kolab_master')) { $this->sieve->script->set_var('EDITOR', self::PROGNAME); $this->sieve->script->set_var('EDITOR_VERSION', self::VERSION); } return $this->sieve->save($name); } /** * Returns list of rules from the current script * * @return array List of rules */ public function list_rules() { $result = array(); $i = 1; foreach ($this->script as $idx => $filter) { if (empty($filter['actions'])) { continue; } $fname = $filter['name'] ?: "#$i"; $result[] = array( 'id' => $idx, 'name' => $fname, 'class' => $filter['disabled'] ? 'disabled' : '', ); $i++; } return $result; } /** * Initializes internal script data */ protected function init_script() { if (!$this->sieve->script) { return; } $this->script = $this->sieve->script->as_array(); $headers = array(); $exceptions = array('date', 'currentdate', 'size', 'body'); // find common headers used in script, will be added to the list // of available (predefined) headers (#1489271) foreach ($this->script as $rule) { foreach ((array) $rule['tests'] as $test) { if ($test['test'] == 'header') { foreach ((array) $test['arg1'] as $header) { $lc_header = strtolower($header); // skip special names to not confuse UI if (in_array($lc_header, $exceptions)) { continue; } if (!isset($this->headers[$lc_header]) && !isset($headers[$lc_header])) { $headers[$lc_header] = $header; } } } } } ksort($headers); $this->headers += $headers; } /** * Get all e-mail addresses of the user */ protected function user_emails() { $addresses = $this->rc->user->list_emails(); foreach ($addresses as $idx => $email) { $addresses[$idx] = $email['email']; } $addresses = array_unique($addresses); sort($addresses); return $addresses; } /** * Convert configured default headers into internal format */ protected function get_default_headers() { $default = array('Subject', 'From', 'To'); $headers = (array) $this->rc->config->get('managesieve_default_headers', $default); $keys = array_map('strtolower', $headers); $headers = array_combine($keys, $headers); // make sure there's no Date header unset($headers['date']); return $headers; } }