4 * Managesieve (Sieve Filters)
6 * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
7 * It's clickable interface which operates on text scripts and communicates
8 * with server using managesieve protocol. Adds Filters tab in Settings.
11 * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
13 * Configuration (see config.inc.php.dist):
16 class managesieve extends rcube_plugin
18 public $task = 'settings';
24 private $script = array();
25 private $exts = array();
26 private $headers = array(
27 'subject' => 'Subject',
34 // add Tab label/title
35 $this->add_texts('localization/', array('filters','managefilters'));
38 $this->register_action('plugin.managesieve', array($this, 'managesieve_init'));
39 $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
41 // include main js script
42 $this->include_script('managesieve.js');
45 function managesieve_start()
47 $rcmail = rcmail::get_instance();
52 // register UI objects
53 $this->rc->output->add_handlers(array(
54 'filterslist' => array($this, 'filters_list'),
55 'filterframe' => array($this, 'filter_frame'),
56 'filterform' => array($this, 'filter_form'),
59 require_once($this->home . '/lib/Net/Sieve.php');
60 require_once($this->home . '/lib/rcube_sieve.php');
62 // try to connect to managesieve server and to fetch the script
63 $this->sieve = new rcube_sieve($_SESSION['username'],
64 $this->rc->decrypt($_SESSION['password']),
65 str_replace('%h', $_SESSION['imap_host'], $this->rc->config->get('managesieve_host', 'localhost')),
66 $this->rc->config->get('managesieve_port', 2000),
67 $this->rc->config->get('managesieve_usetls', false),
68 $this->rc->config->get('managesieve_disabled_extensions'));
70 $error = $this->sieve->error();
72 if ($error == SIEVE_ERROR_NOT_EXISTS)
74 // if script not exists build default script contents
75 $script_file = $this->rc->config->get('managesieve_default');
76 if ($script_file && is_readable($script_file))
77 $this->sieve->script->add_text(file_get_contents($script_file));
78 // that's not exactly an error
85 case SIEVE_ERROR_CONNECTION:
86 case SIEVE_ERROR_LOGIN:
87 $this->rc->output->show_message('managesieve.filterconnerror', 'error');
90 $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
94 // to disable 'Add filter' button set env variable
95 $this->rc->output->set_env('filterconnerror', true);
98 // finally set script objects
101 $this->script = array();
105 $this->script = $this->sieve->script->as_array();
106 $this->exts = $this->sieve->get_extensions();
112 function managesieve_init()
114 // Init plugin and handle managesieve connection
115 $error = $this->managesieve_start();
117 // Handle user requests
118 if ($action = get_input_value('_act', RCUBE_INPUT_GPC))
120 $fid = (int) get_input_value('_fid', RCUBE_INPUT_GET);
122 if ($action=='up' && !$error)
124 if ($fid && isset($this->script[$fid]) && isset($this->script[$fid-1]))
126 if ($this->sieve->script->update_rule($fid, $this->script[$fid-1]) !== false
127 && $this->sieve->script->update_rule($fid-1, $this->script[$fid]) !== false)
128 $result = $this->sieve->save();
131 // $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
132 $this->rc->output->command('managesieve_updatelist', 'up', '', $fid);
134 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
137 elseif ($action=='down' && !$error)
139 if (isset($this->script[$fid]) && isset($this->script[$fid+1]))
141 if ($this->sieve->script->update_rule($fid, $this->script[$fid+1]) !== false
142 && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false)
143 $result = $this->sieve->save();
146 // $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
147 $this->rc->output->command('managesieve_updatelist', 'down', '', $fid);
149 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
152 elseif ($action=='delete' && !$error)
154 if (isset($this->script[$fid]))
156 if ($this->sieve->script->delete_rule($fid))
157 $result = $this->sieve->save();
160 $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
162 $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
163 $this->rc->output->command('managesieve_updatelist', 'delete', '', $fid);
167 elseif ($action=='ruleadd')
169 $rid = get_input_value('_rid', RCUBE_INPUT_GPC);
170 $id = $this->genid();
171 $content = $this->rule_div($fid, $id, false);
173 $this->rc->output->command('managesieve_rulefill', $content, $id, $rid);
175 elseif ($action=='actionadd')
177 $aid = get_input_value('_aid', RCUBE_INPUT_GPC);
178 $id = $this->genid();
179 $content = $this->action_div($fid, $id, false);
181 $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
184 $this->rc->output->send();
187 $this->managesieve_send();
190 function managesieve_save()
192 // Init plugin and handle managesieve connection
193 $error = $this->managesieve_start();
196 if (isset($_POST['_name']))
198 $name = trim(get_input_value('_name', RCUBE_INPUT_POST));
199 $fid = trim(get_input_value('_fid', RCUBE_INPUT_POST));
200 $join = trim(get_input_value('_join', RCUBE_INPUT_POST));
203 $headers = $_POST['_header'];
204 $cust_headers = $_POST['_custom_header'];
205 $ops = $_POST['_rule_op'];
206 $sizeops = $_POST['_rule_size_op'];
207 $sizeitems = $_POST['_rule_size_item'];
208 $sizetargets = $_POST['_rule_size_target'];
209 $targets = $_POST['_rule_target'];
210 $act_types = $_POST['_action_type'];
211 $mailboxes = $_POST['_action_mailbox'];
212 $act_targets = $_POST['_action_target'];
213 $area_targets = $_POST['_action_target_area'];
214 $reasons = $_POST['_action_reason'];
215 $addresses = $_POST['_action_addresses'];
216 $days = $_POST['_action_days'];
218 // we need a "hack" for radiobuttons
219 foreach ($sizeitems as $item)
222 $this->form['join'] = $join=='allof' ? true : false;
223 $this->form['name'] = $name;
224 $this->form['tests'] = array();
225 $this->form['actions'] = array();
228 $this->errors['name'] = $this->gettext('cannotbeempty');
230 foreach($this->script as $idx => $rule)
231 if($rule['name'] == $name && $idx != $fid) {
232 $this->errors['name'] = $this->gettext('ruleexist');
240 $this->form['tests'][0]['test'] = 'true';
242 else foreach($headers as $idx => $header)
244 $header = $this->strip_value($header);
245 $target = $this->strip_value($targets[$idx]);
246 $op = $this->strip_value($ops[$idx]);
249 if (in_array($header, $this->headers))
251 if(preg_match('/^not/', $op))
252 $this->form['tests'][$i]['not'] = true;
253 $type = preg_replace('/^not/', '', $op);
255 if ($type == 'exists')
257 $this->form['tests'][$i]['test'] = 'exists';
258 $this->form['tests'][$i]['arg'] = $header;
262 $this->form['tests'][$i]['type'] = $type;
263 $this->form['tests'][$i]['test'] = 'header';
264 $this->form['tests'][$i]['arg1'] = $header;
265 $this->form['tests'][$i]['arg2'] = $target;
268 $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
275 $sizeop = $this->strip_value($sizeops[$idx]);
276 $sizeitem = $this->strip_value($items[$idx]);
277 $sizetarget = $this->strip_value($sizetargets[$idx]);
279 $this->form['tests'][$i]['test'] = 'size';
280 $this->form['tests'][$i]['type'] = $sizeop;
281 $this->form['tests'][$i]['arg'] = $sizetarget.$sizeitem;
283 if (!preg_match('/^[0-9]+(K|M|G)*$/i', $sizetarget))
284 $this->errors['tests'][$i]['sizetarget'] = $this->gettext('wrongformat');
287 $cust_header = $headers = $this->strip_value($cust_headers[$idx]);
289 if(preg_match('/^not/', $op))
290 $this->form['tests'][$i]['not'] = true;
291 $type = preg_replace('/^not/', '', $op);
293 if ($cust_header == '')
294 $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty');
296 $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY);
298 if (!count($headers))
299 $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty');
301 foreach ($headers as $hr)
302 if (!preg_match('/^[a-z0-9-]+$/i', $hr))
303 $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars');
307 if (empty($this->errors['tests'][$i]['header']))
308 $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers;
310 if ($type == 'exists')
312 $this->form['tests'][$i]['test'] = 'exists';
313 $this->form['tests'][$i]['arg'] = $cust_header;
317 $this->form['tests'][$i]['test'] = 'header';
318 $this->form['tests'][$i]['type'] = $type;
319 $this->form['tests'][$i]['arg1'] = $cust_header;
320 $this->form['tests'][$i]['arg2'] = $target;
323 $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
332 foreach($act_types as $idx => $type)
334 $type = $this->strip_value($type);
335 $target = $this->strip_value($act_targets[$idx]);
337 $this->form['actions'][$i]['type'] = $type;
342 $mailbox = $this->strip_value($mailboxes[$idx]);
343 $this->form['actions'][$i]['target'] = $mailbox;
347 $target = $this->strip_value($area_targets[$idx]);
348 $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target);
350 // if ($target == '')
351 // $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty');
354 $this->form['actions'][$i]['target'] = $target;
356 if ($this->form['actions'][$i]['target'] == '')
357 $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty');
358 else if (!$this->check_email($this->form['actions'][$i]['target']))
359 $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning');
362 $reason = $this->strip_value($reasons[$idx]);
363 $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason);
364 $this->form['actions'][$i]['days'] = $days[$idx];
365 $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]);
366 // @TODO: vacation :subject, :mime, :from, :handle
368 if ($this->form['actions'][$i]['addresses']) {
369 foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) {
370 $address = trim($address);
372 unset($this->form['actions'][$i]['addresses'][$aidx]);
373 else if(!$this->check_email($address)) {
374 $this->errors['actions'][$i]['addresses'] = $this->gettext('noemailwarning');
377 $this->form['actions'][$i]['addresses'][$aidx] = $address;
381 if ($this->form['actions'][$i]['reason'] == '')
382 $this->errors['actions'][$i]['reason'] = $this->gettext('cannotbeempty');
383 if ($this->form['actions'][$i]['days'] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i]['days']))
384 $this->errors['actions'][$i]['days'] = $this->gettext('forbiddenchars');
394 if (!isset($this->script[$fid])) {
395 $fid = $this->sieve->script->add_rule($this->form);
398 $fid = $this->sieve->script->update_rule($fid, $this->form);
401 $save = $this->sieve->save();
403 if ($save && $fid !== false)
405 $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
406 $this->rc->output->add_script(sprintf("rcmail.managesieve_updatelist('%s', '%s', %d);",
407 isset($new) ? 'add' : 'update', $this->form['name'], $fid), 'foot');
408 // $this->rc->output->command('managesieve_updatelist', isset($new) ? 'add' : 'update', $this->form['name'], $fid);
409 // $this->rc->output->send();
413 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
414 // $this->rc->output->send();
419 $this->managesieve_send();
422 private function managesieve_send()
424 // Handle form action
425 if (isset($_GET['_framed']) || isset($_POST['_framed']))
426 $this->rc->output->send('managesieve.managesieveedit');
428 $this->rc->output->set_pagetitle($this->gettext('filters'));
429 $this->rc->output->send('managesieve.managesieve');
433 // return the filters list as HTML table
434 function filters_list($attrib)
436 // add id to message list table if not specified
437 if (!strlen($attrib['id']))
438 $attrib['id'] = 'rcmfilterslist';
440 // define list of cols to be displayed
441 $a_show_cols = array('managesieve.filtername');
443 foreach($this->script as $idx => $filter)
444 $result[] = array('managesieve.filtername' => $filter['name'], 'id' => $idx);
446 // create XHTML table
447 $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
450 $this->rc->output->add_gui_object('filterslist', $attrib['id']);
451 $this->rc->output->include_script('list.js');
453 // add some labels to client
454 $this->rc->output->add_label('managesieve.filterconfirmdelete');
459 function filter_frame($attrib)
462 $attrib['id'] = 'rcmfilterframe';
464 $attrib['name'] = $attrib['id'];
466 $this->rc->output->set_env('contentframe', $attrib['name']);
467 $this->rc->output->set_env('blankpage', $attrib['src'] ?
468 $this->rc->output->abs_url($attrib['src']) : 'program/blank.gif');
470 return html::tag('iframe', $attrib);
474 function filter_form($attrib)
477 $attrib['id'] = 'rcmfilterform';
479 $fid = get_input_value('_fid', RCUBE_INPUT_GPC);
480 $scr = isset($this->form) ? $this->form : $this->script[$fid];
482 $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
483 $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
484 $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
485 $hiddenfields->add(array('name' => '_fid', 'value' => $fid));
487 $out = '<form name="filterform" action="./" method="post">'."\n";
488 $out .= $hiddenfields->show();
491 if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not'])
496 $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30,
497 'class' => ($this->errors['name'] ? 'error' : '')));
500 $input_name = $input_name->show($scr['name']);
502 $input_name = $input_name->show();
504 $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s<br /><br />\n",
505 $field_id, Q($this->gettext('filtername')), $input_name);
507 $out .= '<fieldset><legend>' . Q($this->gettext('messagesrules')) . "</legend>\n";
509 // any, allof, anyof radio buttons
510 $field_id = '_allof';
511 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof',
512 'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio'));
514 if (isset($scr) && !$any)
515 $input_join = $input_join->show($scr['join'] ? 'allof' : '');
517 $input_join = $input_join->show();
519 $out .= sprintf("%s<label for=\"%s\">%s</label> \n",
520 $input_join, $field_id, Q($this->gettext('filterallof')));
522 $field_id = '_anyof';
523 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof',
524 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio'));
526 if (isset($scr) && !$any)
527 $input_join = $input_join->show($scr['join'] ? '' : 'anyof');
529 $input_join = $input_join->show('anyof'); // default
531 $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
532 $input_join, $field_id, Q($this->gettext('filteranyof')));
535 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any',
536 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio'));
538 $input_join = $input_join->show($any ? 'any' : '');
540 $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
541 $input_join, $field_id, Q($this->gettext('filterany')));
543 $rows_num = isset($scr) ? sizeof($scr['tests']) : 1;
545 $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>';
546 for ($x=0; $x<$rows_num; $x++)
547 $out .= $this->rule_div($fid, $x);
550 $out .= "</fieldset>\n";
553 $out .= '<fieldset><legend>' . Q($this->gettext('messagesactions')) . "</legend>\n";
555 $rows_num = isset($scr) ? sizeof($scr['actions']) : 1;
557 $out .= '<div id="actions">';
558 for ($x=0; $x<$rows_num; $x++)
559 $out .= $this->action_div($fid, $x);
562 $out .= "</fieldset>\n";
564 $this->rc->output->add_label('managesieve.ruledeleteconfirm');
565 $this->rc->output->add_label('managesieve.actiondeleteconfirm');
566 $this->rc->output->add_gui_object('sieveform', 'filterform');
571 function rule_div($fid, $id, $div=true)
573 $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id];
574 $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']);
576 $out = $div ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : '';
578 $out .= '<table><tr><td class="rowactions">';
581 $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id,
582 'onchange' => 'header_select(' .$id .')'));
583 foreach($this->headers as $name => $val)
584 $select_header->add(Q($this->gettext($name)), Q($val));
585 $select_header->add(Q($this->gettext('size')), 'size');
586 $select_header->add(Q($this->gettext('...')), '...');
588 // TODO: list arguments
590 if ((isset($rule['test']) && $rule['test'] == 'header')
591 && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers))
592 $out .= $select_header->show($rule['arg1']);
593 elseif ((isset($rule['test']) && $rule['test'] == 'exists')
594 && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers))
595 $out .= $select_header->show($rule['arg']);
596 elseif (isset($rule['test']) && $rule['test'] == 'size')
597 $out .= $select_header->show('size');
598 elseif (isset($rule['test']) && $rule['test'] != 'true')
599 $out .= $select_header->show('...');
601 $out .= $select_header->show();
603 $out .= '</td><td class="rowtargets">';
605 if ((isset($rule['test']) && $rule['test'] == 'header')
606 && (is_array($rule['arg1']) || !in_array($rule['arg1'], $this->headers)))
607 $custom = is_array($rule['arg1']) ? implode(', ', $rule['arg1']) : $rule['arg1'];
608 elseif ((isset($rule['test']) && $rule['test'] == 'exists')
609 && (is_array($rule['arg']) || !in_array($rule['arg'], $this->headers)))
610 $custom = is_array($rule['arg']) ? implode(', ', $rule['arg']) : $rule['arg'];
612 $out .= '<div id="custom_header' .$id. '" style="display:' .(isset($custom) ? 'inline' : 'none'). '">
613 <input type="text" name="_custom_header[]" '. $this->error_class($id, 'test', 'header')
614 .' value="' .Q($custom). '" size="20" /> </div>' . "\n";
616 // matching type select (operator)
617 $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id,
618 'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'), 'onchange' => 'rule_op_select('.$id.')'));
619 $select_op->add(Q($this->gettext('filtercontains')), 'contains');
620 $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains');
621 $select_op->add(Q($this->gettext('filteris')), 'is');
622 $select_op->add(Q($this->gettext('filterisnot')), 'notis');
623 $select_op->add(Q($this->gettext('filterexists')), 'exists');
624 $select_op->add(Q($this->gettext('filternotexists')), 'notexists');
625 // $select_op->add(Q($this->gettext('filtermatches')), 'matches');
626 // $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches');
628 // target input (TODO: lists)
630 if ($rule['test'] == 'header')
632 $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']);
633 $target = $rule['arg2'];
635 elseif ($rule['test'] == 'size')
637 $out .= $select_op->show();
638 if(preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches))
640 $sizetarget = $matches[1];
641 $sizeitem = $matches[2];
646 $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']);
650 $out .= '<input type="text" name="_rule_target[]" id="rule_target' .$id. '"
651 value="' .Q($target). '" size="20" ' . $this->error_class($id, 'test', 'target')
652 . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n";
654 $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id));
655 $select_size_op->add(Q($this->gettext('filterunder')), 'under');
656 $select_size_op->add(Q($this->gettext('filterover')), 'over');
658 $out .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">';
659 $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : '');
660 $out .= '<input type="text" name="_rule_size_target[]" value="'.$sizetarget.'" size="10" ' . $this->error_class($id, 'test', 'sizetarget') .' />
661 <input type="radio" name="_rule_size_item['.$id.']" value=""'. (!$sizeitem ? ' checked="checked"' : '') .' class="radio" />B
662 <input type="radio" name="_rule_size_item['.$id.']" value="K"'. ($sizeitem=='K' ? ' checked="checked"' : '') .' class="radio" />kB
663 <input type="radio" name="_rule_size_item['.$id.']" value="M"'. ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />MB
664 <input type="radio" name="_rule_size_item['.$id.']" value="G"'. ($sizeitem=='G' ? ' checked="checked"' : '') .' class="radio" />GB';
669 $out .= '<td class="rowbuttons">';
670 $out .= '<input type="button" id="ruleadd' . $id .'" value="'. Q($this->gettext('add')). '"
671 onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button" /> ';
672 $out .= '<input type="button" id="ruledel' . $id .'" value="'. Q($this->gettext('del')). '"
673 onclick="rcmail.managesieve_ruledel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"'
674 . ($rows_num<2 ? ' disabled="disabled"' : '') .' />';
675 $out .= '</td></tr></table>';
677 $out .= $div ? "</div>\n" : '';
682 function action_div($fid, $id, $div=true)
684 $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id];
685 $rows_num = isset($this->form) ? sizeof($this->form['actions']) : sizeof($this->script[$fid]['actions']);
687 $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : '';
689 $out .= '<table><tr><td class="rowactions">';
692 $select_action = new html_select(array('name' => "_action_type[]", 'id' => 'action_type'.$id,
693 'onchange' => 'action_type_select(' .$id .')'));
694 if (in_array('fileinto', $this->exts))
695 $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto');
696 $select_action->add(Q($this->gettext('messageredirect')), 'redirect');
697 if (in_array('reject', $this->exts))
698 $select_action->add(Q($this->gettext('messagediscard')), 'reject');
699 elseif (in_array('ereject', $this->exts))
700 $select_action->add(Q($this->gettext('messagediscard')), 'ereject');
701 if (in_array('vacation', $this->exts))
702 $select_action->add(Q($this->gettext('messagereply')), 'vacation');
703 $select_action->add(Q($this->gettext('messagedelete')), 'discard');
704 $select_action->add(Q($this->gettext('rulestop')), 'stop');
706 $out .= $select_action->show($action['type']);
709 // actions target inputs
710 $out .= '<td class="rowtargets">';
712 $out .= '<input type="text" name="_action_target[]" id="action_target' .$id. '" '
713 .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="40" '
714 .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" '
715 . $this->error_class($id, 'action', 'target') .' />';
716 $out .= '<textarea name="_action_target_area[]" id="action_target_area' .$id. '" '
717 .'rows="3" cols="40" '. $this->error_class($id, 'action', 'targetarea')
718 .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">'
719 . (in_array($action['type'], array('reject', 'ereject')) ? Q($action['target'], 'strict', false) : '')
723 $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
724 $out .= '<span class="label">'. Q($this->gettext('vacationreason')) .'</span><br />'
725 .'<textarea name="_action_reason[]" id="action_reason' .$id. '" '
726 .'rows="3" cols="40" '. $this->error_class($id, 'action', 'reason') . '>'
727 . Q($action['reason'], 'strict', false) . "</textarea>\n";
728 $out .= '<br /><span class="label">' .Q($this->gettext('vacationaddresses')) . '</span><br />'
729 .'<input type="text" name="_action_addresses[]" '
730 .'value="' . (is_array($action['addresses']) ? Q(implode(', ', $action['addresses']), 'strict', false) : $action['addresses']) . '" size="40" '
731 . $this->error_class($id, 'action', 'addresses') .' />';
732 $out .= '<br /><span class="label">' . Q($this->gettext('vacationdays')) . '</span><br />'
733 .'<input type="text" name="_action_days[]" '
734 .'value="' .Q($action['days'], 'strict', false) . '" size="2" '
735 . $this->error_class($id, 'action', 'days') .' />';
739 $out .= '<select id="action_mailbox' .$id. '" name="_action_mailbox[]" style="display:'
740 .(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none'). '">';
742 $this->rc->imap_init(true);
744 $a_folders = $this->rc->imap->list_mailboxes();
745 $delimiter = $this->rc->imap->get_hierarchy_delimiter();
748 $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP');
750 if ($action['type'] == 'fileinto')
751 $mailbox = $action['target'];
755 foreach ($a_folders as $folder)
757 $utf7folder = $this->rc->imap->mod_mailbox($folder);
758 $names = explode($delimiter, rcube_charset_convert($folder, 'UTF7-IMAP'));
759 $name = $names[sizeof($names)-1];
761 if ($replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter'))
762 $utf7folder = str_replace($delimiter, $replace_delimiter, $utf7folder);
764 // convert to Sieve implementation encoding
765 $utf7folder = $this->mbox_encode($utf7folder, $mbox_encoding);
767 if ($folder_class = rcmail_folder_classname($name))
768 $foldername = $this->gettext($folder_class);
772 $out .= sprintf('<option value="%s"%s>%s%s</option>'."\n",
773 htmlspecialchars($utf7folder),
774 ($mailbox == $utf7folder ? ' selected="selected"' : ''),
775 str_repeat(' ', 4 * (sizeof($names)-1)),
776 Q(abbreviate_string($foldername, 40 - (2 * sizeof($names)-1))));
782 $out .= '<td class="rowbuttons">';
783 $out .= '<input type="button" id="actionadd' . $id .'" value="'. Q($this->gettext('add')). '"
784 onclick="rcmail.managesieve_actionadd(' . $id .')" class="button" /> ';
785 $out .= '<input type="button" id="actiondel' . $id .'" value="'. Q($this->gettext('del')). '"
786 onclick="rcmail.managesieve_actiondel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"'
787 . ($rows_num<2 ? ' disabled="disabled"' : '') .' />';
790 $out .= '</tr></table>';
792 $out .= $div ? "</div>\n" : '';
797 private function genid()
799 $result = intval(rcube_timer());
803 private function strip_value($str)
805 return trim(strip_tags($str));
808 private function error_class($id, $type, $target, $name_only=false)
811 if ($type == 'test' && isset($this->errors['tests'][$id][$target]))
812 return ($name_only ? 'error' : ' class="error"');
813 elseif ($type == 'action' && isset($this->errors['actions'][$id][$target]))
814 return ($name_only ? 'error' : ' class="error"');
819 private function check_email($email)
821 if (function_exists('check_email'));
822 return check_email($email);
824 // Check for invalid characters
825 if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email))
828 // Check that there's one @ symbol, and that the lengths are right
829 if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $email))
832 // Split it into sections to make life easier
833 $email_array = explode('@', $email);
836 $local_array = explode('.', $email_array[0]);
837 foreach ($local_array as $local_part)
838 if (!preg_match('/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+)|("[^"]+"))$/', $local_part))
842 if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/', $email_array[1])
843 || preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/', $email_array[1]))
844 return true; // If an IP address
846 { // If not an IP address
847 $domain_array = explode('.', $email_array[1]);
848 if (sizeof($domain_array) < 2)
849 return false; // Not enough parts to be a valid domain
851 foreach ($domain_array as $domain_part)
852 if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $domain_part))
861 private function mbox_encode($text, $encoding)
863 return rcube_charset_convert($text, 'UTF7-IMAP', $encoding);