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');
     } 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');
         } 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');
                         }
                     }
                 }
             } 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();
 }
示例#2
0
 /**
  * Key import (page) handler
  */
 private function key_import()
 {
     // Import process
     if ($data = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST)) {
         $this->enigma->load_engine();
         $this->enigma->engine->password_handler();
         $result = $this->enigma->engine->import_key($data);
         if (is_array($result)) {
             if (rcube_utils::get_input_value('_generated', rcube_utils::INPUT_POST)) {
                 $this->rc->output->command('enigma_key_create_success');
                 $this->rc->output->show_message('enigma.keygeneratesuccess', 'confirmation');
             } else {
                 $this->rc->output->show_message('enigma.keysimportsuccess', 'confirmation', array('new' => $result['imported'], 'old' => $result['unchanged']));
                 if ($result['imported'] && !empty($_POST['_refresh'])) {
                     $this->rc->output->command('enigma_list', 1, false);
                 }
             }
         } else {
             $this->rc->output->show_message('enigma.keysimportfailed', 'error');
         }
         $this->rc->output->send();
     } else {
         if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
             $this->enigma->load_engine();
             $result = $this->enigma->engine->import_key($_FILES['_file']['tmp_name'], true);
             if (is_array($result)) {
                 // reload list if any keys has been added
                 if ($result['imported']) {
                     $this->rc->output->command('parent.enigma_list', 1);
                 } else {
                     $this->rc->output->command('parent.enigma_loadframe');
                 }
                 $this->rc->output->show_message('enigma.keysimportsuccess', 'confirmation', array('new' => $result['imported'], 'old' => $result['unchanged']));
             } else {
                 if ($result instanceof enigma_error && $result->getCode() == enigma_error::BADPASS) {
                     $this->password_prompt($result);
                 } else {
                     $this->rc->output->show_message('enigma.keysimportfailed', 'error');
                 }
             }
             $this->rc->output->send('iframe');
         } else {
             if ($err = $_FILES['_file']['error']) {
                 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
                     $this->rc->output->show_message('filesizeerror', 'error', array('size' => $this->rc->show_bytes(rcube_utils::max_upload_size())));
                 } else {
                     $this->rc->output->show_message('fileuploaderror', 'error');
                 }
                 $this->rc->output->send('iframe');
             }
         }
     }
     $this->rc->output->add_handlers(array('importform' => array($this, 'tpl_key_import_form')));
     $this->rc->output->set_pagetitle($this->enigma->gettext('keyimport'));
     $this->rc->output->send('enigma.keyimport');
 }
示例#3
0
 /**
  * Initializes file uploading interface.
  *
  * @param int $max_size Optional maximum file size in bytes
  *
  * @return string Human-readable file size limit
  */
 public function upload_init($max_size = null)
 {
     // Enable upload progress bar
     if ($seconds = $this->config->get('upload_progress')) {
         if (function_exists('uploadprogress_get_info')) {
             $field_name = 'UPLOAD_IDENTIFIER';
         }
         if (!$field_name && filter_var(ini_get('apc.rfc1867'), FILTER_VALIDATE_BOOLEAN)) {
             $field_name = ini_get('apc.rfc1867_name');
         }
         if (!$field_name && filter_var(ini_get('session.upload_progress.enabled'), FILTER_VALIDATE_BOOLEAN)) {
             $field_name = ini_get('session.upload_progress.name');
         }
         if ($field_name) {
             $this->output->set_env('upload_progress_name', $field_name);
             $this->output->set_env('upload_progress_time', (int) $seconds);
         }
     }
     // find max filesize value
     $max_filesize = rcube_utils::max_upload_size();
     if ($max_size && $max_size < $max_filesize) {
         $max_filesize = $max_size;
     }
     $max_filesize_txt = $this->show_bytes($max_filesize);
     $this->output->set_env('max_filesize', $max_filesize);
     $this->output->set_env('filesizeerror', $this->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $max_filesize_txt))));
     if ($max_filecount = ini_get('max_file_uploads')) {
         $this->output->set_env('max_filecount', $max_filecount);
         $this->output->set_env('filecounterror', $this->gettext(array('name' => 'filecounterror', 'vars' => array('count' => $max_filecount))));
     }
     return $max_filesize_txt;
 }