comparison plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Managesieve (Sieve Filters) Engine
5 *
6 * Engine part of Managesieve plugin implementing UI and backend access.
7 *
8 * Copyright (C) 2008-2014, The Roundcube Dev Team
9 * Copyright (C) 2011-2014, Kolab Systems AG
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
23 */
24
25 class rcube_sieve_engine
26 {
27 protected $rc;
28 protected $sieve;
29 protected $errors;
30 protected $form;
31 protected $list;
32 protected $tips = array();
33 protected $script = array();
34 protected $exts = array();
35 protected $active = array();
36 protected $headers = array(
37 'subject' => 'Subject',
38 'from' => 'From',
39 'to' => 'To',
40 );
41 protected $addr_headers = array(
42 // Required
43 "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to",
44 // Additional (RFC 822 / RFC 2822)
45 "reply-to", "resent-reply-to", "resent-sender", "resent-cc", "resent-bcc",
46 // Non-standard (RFC 2076, draft-palme-mailext-headers-08.txt)
47 "for-approval", "for-handling", "for-comment", "apparently-to", "errors-to",
48 "delivered-to", "return-receipt-to", "x-admin", "read-receipt-to",
49 "x-confirm-reading-to", "return-receipt-requested",
50 "registered-mail-reply-requested-by", "mail-followup-to", "mail-reply-to",
51 "abuse-reports-to", "x-complaints-to", "x-report-abuse-to",
52 // Undocumented
53 "x-beenthere",
54 );
55 protected $notify_methods = array(
56 'mailto',
57 // 'sms',
58 // 'tel',
59 );
60 protected $notify_importance_options = array(
61 3 => 'notifyimportancelow',
62 2 => 'notifyimportancenormal',
63 1 => 'notifyimportancehigh'
64 );
65
66 const VERSION = '8.9';
67 const PROGNAME = 'Roundcube (Managesieve)';
68 const PORT = 4190;
69
70
71 /**
72 * Class constructor
73 */
74 function __construct($plugin)
75 {
76 $this->rc = rcube::get_instance();
77 $this->plugin = $plugin;
78 }
79
80 /**
81 * Loads configuration, initializes plugin (including sieve connection)
82 */
83 function start($mode = null)
84 {
85 // register UI objects
86 $this->rc->output->add_handlers(array(
87 'filterslist' => array($this, 'filters_list'),
88 'filtersetslist' => array($this, 'filtersets_list'),
89 'filterframe' => array($this, 'filter_frame'),
90 'filterform' => array($this, 'filter_form'),
91 'filtersetform' => array($this, 'filterset_form'),
92 'filterseteditraw' => array($this, 'filterset_editraw'),
93 ));
94
95 // connect to managesieve server
96 $error = $this->connect($_SESSION['username'], $this->rc->decrypt($_SESSION['password']));
97
98 // load current/active script
99 if (!$error) {
100 // Get list of scripts
101 $list = $this->list_scripts();
102
103 // reset current script when entering filters UI (#1489412)
104 if ($this->rc->action == 'plugin.managesieve') {
105 $this->rc->session->remove('managesieve_current');
106 }
107
108 if ($mode != 'vacation') {
109 if (!empty($_GET['_set']) || !empty($_POST['_set'])) {
110 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
111 }
112 else if (!empty($_SESSION['managesieve_current'])) {
113 $script_name = $_SESSION['managesieve_current'];
114 }
115 }
116
117 $error = $this->load_script($script_name);
118 }
119
120 // finally set script objects
121 if ($error) {
122 switch ($error) {
123 case rcube_sieve::ERROR_CONNECTION:
124 case rcube_sieve::ERROR_LOGIN:
125 $this->rc->output->show_message('managesieve.filterconnerror', 'error');
126 break;
127
128 default:
129 $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
130 break;
131 }
132
133 // reload interface in case of possible error when specified script wasn't found (#1489412)
134 if ($script_name !== null && !empty($list) && !in_array($script_name, $list)) {
135 $this->rc->output->command('reload', 500);
136 }
137
138 // to disable 'Add filter' button set env variable
139 $this->rc->output->set_env('filterconnerror', true);
140 $this->script = array();
141 }
142 else {
143 $this->exts = $this->sieve->get_extensions();
144 $this->init_script();
145 $this->rc->output->set_env('currentset', $this->sieve->current);
146 $_SESSION['managesieve_current'] = $this->sieve->current;
147 }
148
149 $this->rc->output->set_env('raw_sieve_editor', $this->rc->config->get('managesieve_raw_editor', true));
150
151 return $error;
152 }
153
154 /**
155 * Connect to configured managesieve server
156 *
157 * @param string $username User login
158 * @param string $password User password
159 *
160 * @return int Connection status: 0 on success, >0 on failure
161 */
162 public function connect($username, $password)
163 {
164 // Get connection parameters
165 $host = $this->rc->config->get('managesieve_host', 'localhost');
166 $port = $this->rc->config->get('managesieve_port');
167 $tls = $this->rc->config->get('managesieve_usetls', false);
168
169 $host = rcube_utils::parse_host($host);
170 $host = rcube_utils::idn_to_ascii($host);
171
172 // remove tls:// prefix, set TLS flag
173 if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) {
174 $tls = true;
175 }
176
177 if (empty($port)) {
178 $port = getservbyname('sieve', 'tcp');
179 if (empty($port)) {
180 $port = self::PORT;
181 }
182 }
183
184 $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
185 'user' => $username,
186 'password' => $password,
187 'host' => $host,
188 'port' => $port,
189 'usetls' => $tls,
190 'auth_type' => $this->rc->config->get('managesieve_auth_type'),
191 'disabled' => $this->rc->config->get('managesieve_disabled_extensions'),
192 'debug' => $this->rc->config->get('managesieve_debug', false),
193 'auth_cid' => $this->rc->config->get('managesieve_auth_cid'),
194 'auth_pw' => $this->rc->config->get('managesieve_auth_pw'),
195 'socket_options' => $this->rc->config->get('managesieve_conn_options'),
196 ));
197
198 // Handle per-host socket options
199 rcube_utils::parse_socket_options($plugin['socket_options'], $plugin['host']);
200
201 // try to connect to managesieve server and to fetch the script
202 $this->sieve = new rcube_sieve(
203 $plugin['user'],
204 $plugin['password'],
205 $plugin['host'],
206 $plugin['port'],
207 $plugin['auth_type'],
208 $plugin['usetls'],
209 $plugin['disabled'],
210 $plugin['debug'],
211 $plugin['auth_cid'],
212 $plugin['auth_pw'],
213 $plugin['socket_options']
214 );
215
216 $error = $this->sieve->error();
217
218 if ($error) {
219 rcube::raise_error(array(
220 'code' => 403,
221 'file' => __FILE__,
222 'line' => __LINE__,
223 'message' => "Unable to connect to managesieve on $host:$port"
224 ), true, false);
225 }
226
227 return $error;
228 }
229
230 /**
231 * Load specified (or active) script
232 *
233 * @param string $script_name Optional script name
234 *
235 * @return int Connection status: 0 on success, >0 on failure
236 */
237 protected function load_script($script_name = null)
238 {
239 // Get list of scripts
240 $list = $this->list_scripts();
241
242 if ($script_name === null || $script_name === '') {
243 // get (first) active script
244 if (!empty($this->active)) {
245 $script_name = $this->active[0];
246 }
247 else if ($list) {
248 $script_name = $list[0];
249 }
250 // create a new (initial) script
251 else {
252 // if script not exists build default script contents
253 $script_file = $this->rc->config->get('managesieve_default');
254 $script_name = $this->rc->config->get('managesieve_script_name');
255
256 if (empty($script_name)) {
257 $script_name = 'roundcube';
258 }
259
260 if ($script_file && is_readable($script_file)) {
261 $content = file_get_contents($script_file);
262 }
263
264 // add script and set it active
265 if ($this->sieve->save_script($script_name, $content)) {
266 $this->activate_script($script_name);
267 $this->list[] = $script_name;
268 }
269 }
270 }
271
272 if ($script_name) {
273 $this->sieve->load($script_name);
274 }
275
276 return $this->sieve->error();
277 }
278
279 /**
280 * User interface actions handler
281 */
282 function actions()
283 {
284 $error = $this->start();
285
286 // Handle user requests
287 if ($action = rcube_utils::get_input_value('_act', rcube_utils::INPUT_GPC)) {
288 $fid = (int) rcube_utils::get_input_value('_fid', rcube_utils::INPUT_POST);
289
290 if ($action == 'delete' && !$error) {
291 if (isset($this->script[$fid])) {
292 if ($this->sieve->script->delete_rule($fid))
293 $result = $this->save_script();
294
295 if ($result === true) {
296 $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
297 $this->rc->output->command('managesieve_updatelist', 'del', array('id' => $fid));
298 }
299 else {
300 $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
301 }
302 }
303 }
304 else if ($action == 'move' && !$error) {
305 if (isset($this->script[$fid])) {
306 $to = (int) rcube_utils::get_input_value('_to', rcube_utils::INPUT_POST);
307 $rule = $this->script[$fid];
308
309 // remove rule
310 unset($this->script[$fid]);
311 $this->script = array_values($this->script);
312
313 // add at target position
314 if ($to >= count($this->script)) {
315 $this->script[] = $rule;
316 }
317 else {
318 $script = array();
319 foreach ($this->script as $idx => $r) {
320 if ($idx == $to)
321 $script[] = $rule;
322 $script[] = $r;
323 }
324 $this->script = $script;
325 }
326
327 $this->sieve->script->content = $this->script;
328 $result = $this->save_script();
329
330 if ($result === true) {
331 $result = $this->list_rules();
332
333 $this->rc->output->show_message('managesieve.moved', 'confirmation');
334 $this->rc->output->command('managesieve_updatelist', 'list',
335 array('list' => $result, 'clear' => true, 'set' => $to));
336 }
337 else {
338 $this->rc->output->show_message('managesieve.moveerror', 'error');
339 }
340 }
341 }
342 else if ($action == 'act' && !$error) {
343 if (isset($this->script[$fid])) {
344 $rule = $this->script[$fid];
345 $disabled = !empty($rule['disabled']);
346 $rule['disabled'] = !$disabled;
347 $result = $this->sieve->script->update_rule($fid, $rule);
348
349 if ($result !== false)
350 $result = $this->save_script();
351
352 if ($result === true) {
353 if ($rule['disabled'])
354 $this->rc->output->show_message('managesieve.deactivated', 'confirmation');
355 else
356 $this->rc->output->show_message('managesieve.activated', 'confirmation');
357 $this->rc->output->command('managesieve_updatelist', 'update',
358 array('id' => $fid, 'disabled' => $rule['disabled']));
359 }
360 else {
361 if ($rule['disabled'])
362 $this->rc->output->show_message('managesieve.deactivateerror', 'error');
363 else
364 $this->rc->output->show_message('managesieve.activateerror', 'error');
365 }
366 }
367 }
368 else if ($action == 'setact' && !$error) {
369 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
370 $result = $this->activate_script($script_name);
371 $kep14 = $this->rc->config->get('managesieve_kolab_master');
372
373 if ($result === true) {
374 $this->rc->output->set_env('active_sets', $this->active);
375 $this->rc->output->show_message('managesieve.setactivated', 'confirmation');
376 $this->rc->output->command('managesieve_updatelist', 'setact',
377 array('name' => $script_name, 'active' => true, 'all' => !$kep14));
378 }
379 else {
380 $this->rc->output->show_message('managesieve.setactivateerror', 'error');
381 }
382 }
383 else if ($action == 'deact' && !$error) {
384 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
385 $result = $this->deactivate_script($script_name);
386
387 if ($result === true) {
388 $this->rc->output->set_env('active_sets', $this->active);
389 $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation');
390 $this->rc->output->command('managesieve_updatelist', 'setact',
391 array('name' => $script_name, 'active' => false));
392 }
393 else {
394 $this->rc->output->show_message('managesieve.setdeactivateerror', 'error');
395 }
396 }
397 else if ($action == 'setdel' && !$error) {
398 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST, true);
399 $result = $this->remove_script($script_name);
400
401 if ($result === true) {
402 $this->rc->output->show_message('managesieve.setdeleted', 'confirmation');
403 $this->rc->output->command('managesieve_updatelist', 'setdel',
404 array('name' => $script_name));
405 $this->rc->session->remove('managesieve_current');
406 }
407 else {
408 $this->rc->output->show_message('managesieve.setdeleteerror', 'error');
409 }
410 }
411 else if ($action == 'setget') {
412 $this->rc->request_security_check(rcube_utils::INPUT_GET);
413
414 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_GPC, true);
415 $script = $this->sieve->get_script($script_name);
416
417 if ($script === false) {
418 exit;
419 }
420
421 $browser = new rcube_browser;
422
423 // send download headers
424 header("Content-Type: application/octet-stream");
425 header("Content-Length: ".strlen($script));
426
427 if ($browser->ie) {
428 header("Content-Type: application/force-download");
429 $filename = rawurlencode($script_name);
430 }
431 else {
432 $filename = addcslashes($script_name, '\\"');
433 }
434
435 header("Content-Disposition: attachment; filename=\"$filename.txt\"");
436 echo $script;
437 exit;
438 }
439 else if ($action == 'list') {
440 $result = $this->list_rules();
441
442 $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result));
443 }
444 else if ($action == 'ruleadd') {
445 $rid = rcube_utils::get_input_value('_rid', rcube_utils::INPUT_POST);
446 $id = $this->genid();
447 $content = $this->rule_div($fid, $id, false);
448
449 $this->rc->output->command('managesieve_rulefill', $content, $id, $rid);
450 }
451 else if ($action == 'actionadd') {
452 $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST);
453 $id = $this->genid();
454 $content = $this->action_div($fid, $id, false);
455
456 $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
457 }
458 else if ($action == 'addresses') {
459 $aid = rcube_utils::get_input_value('_aid', rcube_utils::INPUT_POST);
460
461 $this->rc->output->command('managesieve_vacation_addresses_update', $aid, $this->user_emails());
462 }
463
464 $this->rc->output->send();
465 }
466 else if ($this->rc->task == 'mail') {
467 // Initialize the form
468 $rules = rcube_utils::get_input_value('r', rcube_utils::INPUT_GET);
469 if (!empty($rules)) {
470 $i = 0;
471 foreach ($rules as $rule) {
472 list($header, $value) = explode(':', $rule, 2);
473 $tests[$i] = array(
474 'type' => 'contains',
475 'test' => 'header',
476 'arg1' => $header,
477 'arg2' => $value,
478 );
479 $i++;
480 }
481
482 $this->form = array(
483 'join' => count($tests) > 1 ? 'allof' : 'anyof',
484 'name' => '',
485 'tests' => $tests,
486 'actions' => array(
487 0 => array('type' => 'fileinto'),
488 1 => array('type' => 'stop'),
489 ),
490 );
491 }
492 }
493
494 $this->send();
495 }
496
497 function saveraw()
498 {
499 // Init plugin and handle managesieve connection
500 $error = $this->start();
501
502 $script_name = rcube_utils::get_input_value('_set', rcube_utils::INPUT_POST);
503
504 $result = $this->sieve->save_script($script_name, $_POST['rawsetcontent']);
505
506 if ($result === false) {
507 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
508 $errorLines = $this->sieve->get_error_lines();
509 if (count($errorLines) > 0) {
510 $this->rc->output->set_env("sieve_errors", $errorLines);
511 }
512 }
513 else {
514 $this->rc->output->show_message('managesieve.setupdated', 'confirmation');
515 $this->rc->output->command('parent.managesieve_updatelist', 'refresh');
516 }
517
518 $this->send();
519 }
520
521 function save()
522 {
523 // Init plugin and handle managesieve connection
524 $error = $this->start();
525
526 // get request size limits (#1488648)
527 $max_post = max(array(
528 ini_get('max_input_vars'),
529 ini_get('suhosin.request.max_vars'),
530 ini_get('suhosin.post.max_vars'),
531 ));
532 $max_depth = max(array(
533 ini_get('suhosin.request.max_array_depth'),
534 ini_get('suhosin.post.max_array_depth'),
535 ));
536
537 // check request size limit
538 if ($max_post && count($_POST, COUNT_RECURSIVE) >= $max_post) {
539 rcube::raise_error(array(
540 'code' => 500, 'type' => 'php',
541 'file' => __FILE__, 'line' => __LINE__,
542 'message' => "Request size limit exceeded (one of max_input_vars/suhosin.request.max_vars/suhosin.post.max_vars)"
543 ), true, false);
544 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
545 }
546 // check request depth limits
547 else if ($max_depth && count($_POST['_header']) > $max_depth) {
548 rcube::raise_error(array(
549 'code' => 500, 'type' => 'php',
550 'file' => __FILE__, 'line' => __LINE__,
551 'message' => "Request size limit exceeded (one of suhosin.request.max_array_depth/suhosin.post.max_array_depth)"
552 ), true, false);
553 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
554 }
555 // filters set add action
556 else if (!empty($_POST['_newset'])) {
557 $name = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true);
558 $copy = rcube_utils::get_input_value('_copy', rcube_utils::INPUT_POST, true);
559 $from = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
560 $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
561 $kolab = $this->rc->config->get('managesieve_kolab_master');
562 $name_uc = mb_strtolower($name);
563 $list = $this->list_scripts();
564
565 if (!$name) {
566 $this->errors['name'] = $this->plugin->gettext('cannotbeempty');
567 }
568 else if (mb_strlen($name) > 128) {
569 $this->errors['name'] = $this->plugin->gettext('nametoolong');
570 }
571 else if (!empty($exceptions) && in_array($name, (array)$exceptions)) {
572 $this->errors['name'] = $this->plugin->gettext('namereserved');
573 }
574 else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) {
575 $this->errors['name'] = $this->plugin->gettext('namereserved');
576 }
577 else if (in_array($name, $list)) {
578 $this->errors['name'] = $this->plugin->gettext('setexist');
579 }
580 else if ($from == 'file') {
581 // from file
582 if (is_uploaded_file($_FILES['_file']['tmp_name'])) {
583 $file = file_get_contents($_FILES['_file']['tmp_name']);
584 $file = preg_replace('/\r/', '', $file);
585 // for security don't save script directly
586 // check syntax before, like this...
587 $this->sieve->load_script($file);
588 if (!$this->save_script($name)) {
589 $this->errors['file'] = $this->plugin->gettext('setcreateerror');
590 }
591 }
592 else { // upload failed
593 $err = $_FILES['_file']['error'];
594
595 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
596 $msg = $this->rc->gettext(array('name' => 'filesizeerror',
597 'vars' => array('size' =>
598 $this->rc->show_bytes(rcube_utils::max_upload_size()))));
599 }
600 else {
601 $this->errors['file'] = $this->plugin->gettext('fileuploaderror');
602 }
603 }
604 }
605 else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) {
606 $error = 'managesieve.setcreateerror';
607 }
608
609 if (!$error && empty($this->errors)) {
610 // Find position of the new script on the list
611 $list[] = $name;
612 asort($list, SORT_LOCALE_STRING);
613 $list = array_values($list);
614 $index = array_search($name, $list);
615
616 $this->rc->output->show_message('managesieve.setcreated', 'confirmation');
617 $this->rc->output->command('parent.managesieve_updatelist', 'setadd',
618 array('name' => $name, 'index' => $index));
619 }
620 else if ($msg) {
621 $this->rc->output->command('display_message', $msg, 'error');
622 }
623 else if ($error) {
624 $this->rc->output->show_message($error, 'error');
625 }
626 }
627 // filter add/edit action
628 else if (isset($_POST['_name'])) {
629 $name = trim(rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true));
630 $fid = trim(rcube_utils::get_input_value('_fid', rcube_utils::INPUT_POST));
631 $join = trim(rcube_utils::get_input_value('_join', rcube_utils::INPUT_POST));
632
633 // and arrays
634 $headers = rcube_utils::get_input_value('_header', rcube_utils::INPUT_POST);
635 $cust_headers = rcube_utils::get_input_value('_custom_header', rcube_utils::INPUT_POST);
636 $cust_vars = rcube_utils::get_input_value('_custom_var', rcube_utils::INPUT_POST);
637 $ops = rcube_utils::get_input_value('_rule_op', rcube_utils::INPUT_POST);
638 $sizeops = rcube_utils::get_input_value('_rule_size_op', rcube_utils::INPUT_POST);
639 $sizeitems = rcube_utils::get_input_value('_rule_size_item', rcube_utils::INPUT_POST);
640 $sizetargets = rcube_utils::get_input_value('_rule_size_target', rcube_utils::INPUT_POST);
641 $targets = rcube_utils::get_input_value('_rule_target', rcube_utils::INPUT_POST, true);
642 $mods = rcube_utils::get_input_value('_rule_mod', rcube_utils::INPUT_POST);
643 $mod_types = rcube_utils::get_input_value('_rule_mod_type', rcube_utils::INPUT_POST);
644 $body_trans = rcube_utils::get_input_value('_rule_trans', rcube_utils::INPUT_POST);
645 $body_types = rcube_utils::get_input_value('_rule_trans_type', rcube_utils::INPUT_POST, true);
646 $comparators = rcube_utils::get_input_value('_rule_comp', rcube_utils::INPUT_POST);
647 $indexes = rcube_utils::get_input_value('_rule_index', rcube_utils::INPUT_POST);
648 $lastindexes = rcube_utils::get_input_value('_rule_index_last', rcube_utils::INPUT_POST);
649 $dateheaders = rcube_utils::get_input_value('_rule_date_header', rcube_utils::INPUT_POST);
650 $dateparts = rcube_utils::get_input_value('_rule_date_part', rcube_utils::INPUT_POST);
651 $message = rcube_utils::get_input_value('_rule_message', rcube_utils::INPUT_POST);
652 $dup_handles = rcube_utils::get_input_value('_rule_duplicate_handle', rcube_utils::INPUT_POST, true);
653 $dup_headers = rcube_utils::get_input_value('_rule_duplicate_header', rcube_utils::INPUT_POST, true);
654 $dup_uniqueids = rcube_utils::get_input_value('_rule_duplicate_uniqueid', rcube_utils::INPUT_POST, true);
655 $dup_seconds = rcube_utils::get_input_value('_rule_duplicate_seconds', rcube_utils::INPUT_POST);
656 $dup_lasts = rcube_utils::get_input_value('_rule_duplicate_last', rcube_utils::INPUT_POST);
657 $act_types = rcube_utils::get_input_value('_action_type', rcube_utils::INPUT_POST, true);
658 $mailboxes = rcube_utils::get_input_value('_action_mailbox', rcube_utils::INPUT_POST, true);
659 $act_targets = rcube_utils::get_input_value('_action_target', rcube_utils::INPUT_POST, true);
660 $domain_targets = rcube_utils::get_input_value('_action_target_domain', rcube_utils::INPUT_POST);
661 $area_targets = rcube_utils::get_input_value('_action_target_area', rcube_utils::INPUT_POST, true);
662 $reasons = rcube_utils::get_input_value('_action_reason', rcube_utils::INPUT_POST, true);
663 $addresses = rcube_utils::get_input_value('_action_addresses', rcube_utils::INPUT_POST, true);
664 $intervals = rcube_utils::get_input_value('_action_interval', rcube_utils::INPUT_POST);
665 $interval_types = rcube_utils::get_input_value('_action_interval_type', rcube_utils::INPUT_POST);
666 $from = rcube_utils::get_input_value('_action_from', rcube_utils::INPUT_POST);
667 $subject = rcube_utils::get_input_value('_action_subject', rcube_utils::INPUT_POST, true);
668 $flags = rcube_utils::get_input_value('_action_flags', rcube_utils::INPUT_POST);
669 $varnames = rcube_utils::get_input_value('_action_varname', rcube_utils::INPUT_POST);
670 $varvalues = rcube_utils::get_input_value('_action_varvalue', rcube_utils::INPUT_POST);
671 $varmods = rcube_utils::get_input_value('_action_varmods', rcube_utils::INPUT_POST);
672 $notifymethods = rcube_utils::get_input_value('_action_notifymethod', rcube_utils::INPUT_POST);
673 $notifytargets = rcube_utils::get_input_value('_action_notifytarget', rcube_utils::INPUT_POST, true);
674 $notifyoptions = rcube_utils::get_input_value('_action_notifyoption', rcube_utils::INPUT_POST, true);
675 $notifymessages = rcube_utils::get_input_value('_action_notifymessage', rcube_utils::INPUT_POST, true);
676 $notifyfrom = rcube_utils::get_input_value('_action_notifyfrom', rcube_utils::INPUT_POST);
677 $notifyimp = rcube_utils::get_input_value('_action_notifyimportance', rcube_utils::INPUT_POST);
678
679 // we need a "hack" for radiobuttons
680 foreach ($sizeitems as $item)
681 $items[] = $item;
682
683 $this->form['disabled'] = !empty($_POST['_disabled']);
684 $this->form['join'] = $join == 'allof';
685 $this->form['name'] = $name;
686 $this->form['tests'] = array();
687 $this->form['actions'] = array();
688
689 if ($name == '')
690 $this->errors['name'] = $this->plugin->gettext('cannotbeempty');
691 else {
692 foreach ($this->script as $idx => $rule)
693 if($rule['name'] == $name && $idx != $fid) {
694 $this->errors['name'] = $this->plugin->gettext('ruleexist');
695 break;
696 }
697 }
698
699 $i = 0;
700 // rules
701 if ($join == 'any') {
702 $this->form['tests'][0]['test'] = 'true';
703 }
704 else {
705 foreach ($headers as $idx => $header) {
706 // targets are indexed differently (assume form order)
707 $target = $this->strip_value(array_shift($targets), true);
708 $header = $this->strip_value($header);
709 $operator = $this->strip_value($ops[$idx]);
710 $comparator = $this->strip_value($comparators[$idx]);
711
712 if ($header == 'size') {
713 $sizeop = $this->strip_value($sizeops[$idx]);
714 $sizeitem = $this->strip_value($items[$idx]);
715 $sizetarget = $this->strip_value($sizetargets[$idx]);
716
717 $this->form['tests'][$i]['test'] = 'size';
718 $this->form['tests'][$i]['type'] = $sizeop;
719 $this->form['tests'][$i]['arg'] = $sizetarget;
720
721 if ($sizetarget == '')
722 $this->errors['tests'][$i]['sizetarget'] = $this->plugin->gettext('cannotbeempty');
723 else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) {
724 $this->errors['tests'][$i]['sizetarget'] = $this->plugin->gettext('forbiddenchars');
725 $this->form['tests'][$i]['item'] = $sizeitem;
726 }
727 else
728 $this->form['tests'][$i]['arg'] .= $m[1];
729 }
730 else if ($header == 'currentdate') {
731 $datepart = $this->strip_value($dateparts[$idx]);
732
733 if (preg_match('/^not/', $operator))
734 $this->form['tests'][$i]['not'] = true;
735 $type = preg_replace('/^not/', '', $operator);
736
737 if ($type == 'exists') {
738 $this->errors['tests'][$i]['op'] = true;
739 }
740
741 $this->form['tests'][$i]['test'] = 'currentdate';
742 $this->form['tests'][$i]['type'] = $type;
743 $this->form['tests'][$i]['part'] = $datepart;
744 $this->form['tests'][$i]['arg'] = $target;
745
746 if ($type != 'exists') {
747 if (!count($target)) {
748 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
749 }
750 else if (strpos($type, 'count-') === 0) {
751 foreach ($target as $arg) {
752 if (preg_match('/[^0-9]/', $arg)) {
753 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
754 }
755 }
756 }
757 else if (strpos($type, 'value-') === 0) {
758 // Some date/time formats do not support i;ascii-numeric comparator
759 if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) {
760 $comparator = '';
761 }
762 }
763
764 if (!preg_match('/^(regex|matches|count-)/', $type) && count($target)) {
765 foreach ($target as $arg) {
766 if (!$this->validate_date_part($datepart, $arg)) {
767 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat');
768 break;
769 }
770 }
771 }
772 }
773 }
774 else if ($header == 'date') {
775 $datepart = $this->strip_value($dateparts[$idx]);
776 $dateheader = $this->strip_value($dateheaders[$idx]);
777 $index = $this->strip_value($indexes[$idx]);
778 $indexlast = $this->strip_value($lastindexes[$idx]);
779
780 if (preg_match('/^not/', $operator))
781 $this->form['tests'][$i]['not'] = true;
782 $type = preg_replace('/^not/', '', $operator);
783
784 if ($type == 'exists') {
785 $this->errors['tests'][$i]['op'] = true;
786 }
787
788 if (!empty($index) && $mod != 'envelope') {
789 $this->form['tests'][$i]['index'] = intval($index);
790 $this->form['tests'][$i]['last'] = !empty($indexlast);
791 }
792
793 if (empty($dateheader)) {
794 $dateheader = 'Date';
795 }
796 else if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $dateheader)) {
797 $this->errors['tests'][$i]['dateheader'] = $this->plugin->gettext('forbiddenchars');
798 }
799
800 $this->form['tests'][$i]['test'] = 'date';
801 $this->form['tests'][$i]['type'] = $type;
802 $this->form['tests'][$i]['part'] = $datepart;
803 $this->form['tests'][$i]['arg'] = $target;
804 $this->form['tests'][$i]['header'] = $dateheader;
805
806 if ($type != 'exists') {
807 if (!count($target)) {
808 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
809 }
810 else if (strpos($type, 'count-') === 0) {
811 foreach ($target as $arg) {
812 if (preg_match('/[^0-9]/', $arg)) {
813 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
814 }
815 }
816 }
817 else if (strpos($type, 'value-') === 0) {
818 // Some date/time formats do not support i;ascii-numeric comparator
819 if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) {
820 $comparator = '';
821 }
822 }
823
824 if (count($target) && !preg_match('/^(regex|matches|count-)/', $type)) {
825 foreach ($target as $arg) {
826 if (!$this->validate_date_part($datepart, $arg)) {
827 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat');
828 break;
829 }
830 }
831 }
832 }
833 }
834 else if ($header == 'body') {
835 $trans = $this->strip_value($body_trans[$idx]);
836 $trans_type = $this->strip_value($body_types[$idx], true);
837
838 if (preg_match('/^not/', $operator))
839 $this->form['tests'][$i]['not'] = true;
840 $type = preg_replace('/^not/', '', $operator);
841
842 if ($type == 'exists') {
843 $this->errors['tests'][$i]['op'] = true;
844 }
845
846 $this->form['tests'][$i]['test'] = 'body';
847 $this->form['tests'][$i]['type'] = $type;
848 $this->form['tests'][$i]['arg'] = $target;
849
850 if (empty($target) && $type != 'exists') {
851 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
852 }
853 else if (preg_match('/^(value|count)-/', $type)) {
854 foreach ($target as $target_value) {
855 if (preg_match('/[^0-9]/', $target_value)) {
856 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
857 }
858 }
859 }
860
861 $this->form['tests'][$i]['part'] = $trans;
862 if ($trans == 'content') {
863 $this->form['tests'][$i]['content'] = $trans_type;
864 }
865 }
866 else if ($header == 'message') {
867 $test = $this->strip_value($message[$idx]);
868
869 if (preg_match('/^not/', $test)) {
870 $this->form['tests'][$i]['not'] = true;
871 $test = substr($test, 3);
872 }
873
874 $this->form['tests'][$i]['test'] = $test;
875
876 if ($test == 'duplicate') {
877 $this->form['tests'][$i]['last'] = !empty($dup_lasts[$idx]);
878 $this->form['tests'][$i]['handle'] = trim($dup_handles[$idx]);
879 $this->form['tests'][$i]['header'] = trim($dup_headers[$idx]);
880 $this->form['tests'][$i]['uniqueid'] = trim($dup_uniqueids[$idx]);
881 $this->form['tests'][$i]['seconds'] = trim($dup_seconds[$idx]);
882
883 if ($this->form['tests'][$i]['seconds']
884 && preg_match('/[^0-9]/', $this->form['tests'][$i]['seconds'])
885 ) {
886 $this->errors['tests'][$i]['duplicate_seconds'] = $this->plugin->gettext('forbiddenchars');
887 }
888
889 if ($this->form['tests'][$i]['header'] && $this->form['tests'][$i]['uniqueid']) {
890 $this->errors['tests'][$i]['duplicate_uniqueid'] = $this->plugin->gettext('duplicate.conflict.err');
891 }
892 }
893 }
894 else {
895 $cust_header = $headers = $this->strip_value(array_shift($cust_headers));
896 $mod = $this->strip_value($mods[$idx]);
897 $mod_type = $this->strip_value($mod_types[$idx]);
898 $index = $this->strip_value($indexes[$idx]);
899 $indexlast = $this->strip_value($lastindexes[$idx]);
900
901 if ($header == 'string') {
902 $cust_var = $headers = $this->strip_value(array_shift($cust_vars));
903 }
904
905 if (preg_match('/^not/', $operator))
906 $this->form['tests'][$i]['not'] = true;
907 $type = preg_replace('/^not/', '', $operator);
908
909 if (!empty($index) && $mod != 'envelope') {
910 $this->form['tests'][$i]['index'] = intval($index);
911 $this->form['tests'][$i]['last'] = !empty($indexlast);
912 }
913
914 if ($header == '...' || $header == 'string') {
915 if (!count($headers))
916 $this->errors['tests'][$i]['header'] = $this->plugin->gettext('cannotbeempty');
917 else if ($header == '...') {
918 foreach ($headers as $hr) {
919 // RFC2822: printable ASCII except colon
920 if (!preg_match('/^[\x21-\x39\x41-\x7E]+$/i', $hr)) {
921 $this->errors['tests'][$i]['header'] = $this->plugin->gettext('forbiddenchars');
922 }
923 }
924 }
925
926 if (empty($this->errors['tests'][$i]['header']))
927 $cust_header = $cust_var = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers;
928 }
929
930 $test = $header == 'string' ? 'string' : 'header';
931 $header = $header == 'string' ? $cust_var : $header;
932 $header = $header == '...' ? $cust_header : $header;
933
934 if (is_array($header)) {
935 foreach ($header as $h_index => $val) {
936 if (isset($this->headers[$val])) {
937 $header[$h_index] = $this->headers[$val];
938 }
939 }
940 }
941
942 if ($type == 'exists') {
943 $this->form['tests'][$i]['test'] = 'exists';
944 $this->form['tests'][$i]['arg'] = $header;
945 }
946 else {
947 if ($mod == 'address' || $mod == 'envelope') {
948 $found = false;
949 if (empty($this->errors['tests'][$i]['header'])) {
950 foreach ((array)$header as $hdr) {
951 if (!in_array(strtolower(trim($hdr)), $this->addr_headers))
952 $found = true;
953 }
954 }
955 if (!$found)
956 $test = $mod;
957 }
958
959 $this->form['tests'][$i]['type'] = $type;
960 $this->form['tests'][$i]['test'] = $test;
961 $this->form['tests'][$i]['arg1'] = $header;
962 $this->form['tests'][$i]['arg2'] = $target;
963
964 if (empty($target)) {
965 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
966 }
967 else if (preg_match('/^(value|count)-/', $type)) {
968 foreach ($target as $target_value) {
969 if (preg_match('/[^0-9]/', $target_value)) {
970 $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars');
971 }
972 }
973 }
974
975 if ($mod) {
976 $this->form['tests'][$i]['part'] = $mod_type;
977 }
978 }
979 }
980
981 if ($header != 'size' && $comparator) {
982 $this->form['tests'][$i]['comparator'] = $comparator;
983 }
984
985 $i++;
986 }
987 }
988
989 $i = 0;
990 // actions
991 foreach ($act_types as $idx => $type) {
992 $type = $this->strip_value($type);
993
994 switch ($type) {
995 case 'fileinto':
996 case 'fileinto_copy':
997 $mailbox = $this->strip_value($mailboxes[$idx], false, false);
998 $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in');
999
1000 if ($type == 'fileinto_copy') {
1001 $type = 'fileinto';
1002 $this->form['actions'][$i]['copy'] = true;
1003 }
1004 break;
1005
1006 case 'reject':
1007 case 'ereject':
1008 $target = $this->strip_value($area_targets[$idx]);
1009 $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target);
1010
1011 // if ($target == '')
1012 // $this->errors['actions'][$i]['targetarea'] = $this->plugin->gettext('cannotbeempty');
1013 break;
1014
1015 case 'redirect':
1016 case 'redirect_copy':
1017 $target = $this->strip_value($act_targets[$idx]);
1018 $domain = $this->strip_value($domain_targets[$idx]);
1019
1020 // force one of the configured domains
1021 $domains = (array) $this->rc->config->get('managesieve_domains');
1022 if (!empty($domains) && !empty($target)) {
1023 if (!$domain || !in_array($domain, $domains)) {
1024 $domain = $domains[0];
1025 }
1026
1027 $target .= '@' . $domain;
1028 }
1029
1030 $this->form['actions'][$i]['target'] = $target;
1031
1032 if ($target == '')
1033 $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
1034 else if (!rcube_utils::check_email($target))
1035 $this->errors['actions'][$i]['target'] = $this->plugin->gettext(!empty($domains) ? 'forbiddenchars' : 'noemailwarning');
1036
1037 if ($type == 'redirect_copy') {
1038 $type = 'redirect';
1039 $this->form['actions'][$i]['copy'] = true;
1040 }
1041
1042 break;
1043
1044 case 'addflag':
1045 case 'setflag':
1046 case 'removeflag':
1047 $_target = array();
1048 if (empty($flags[$idx])) {
1049 $this->errors['actions'][$i]['target'] = $this->plugin->gettext('noflagset');
1050 }
1051 else {
1052 foreach ($flags[$idx] as $flag) {
1053 $_target[] = $this->strip_value($flag);
1054 }
1055 }
1056 $this->form['actions'][$i]['target'] = $_target;
1057 break;
1058
1059 case 'vacation':
1060 $reason = $this->strip_value($reasons[$idx]);
1061 $interval_type = $interval_types[$idx] == 'seconds' ? 'seconds' : 'days';
1062
1063 $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason);
1064 $this->form['actions'][$i]['from'] = $from[$idx];
1065 $this->form['actions'][$i]['subject'] = $subject[$idx];
1066 $this->form['actions'][$i]['addresses'] = array_shift($addresses);
1067 $this->form['actions'][$i][$interval_type] = $intervals[$idx];
1068 // @TODO: vacation :mime, :handle
1069
1070 foreach ((array)$this->form['actions'][$i]['addresses'] as $aidx => $address) {
1071 $this->form['actions'][$i]['addresses'][$aidx] = $address = trim($address);
1072
1073 if (empty($address)) {
1074 unset($this->form['actions'][$i]['addresses'][$aidx]);
1075 }
1076 else if (!rcube_utils::check_email($address)) {
1077 $this->errors['actions'][$i]['addresses'] = $this->plugin->gettext('noemailwarning');
1078 break;
1079 }
1080 }
1081
1082 if (!empty($this->form['actions'][$i]['from']) && !rcube_utils::check_email($this->form['actions'][$i]['from'])) {
1083 $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning');
1084 }
1085
1086 if ($this->form['actions'][$i]['reason'] == '')
1087 $this->errors['actions'][$i]['reason'] = $this->plugin->gettext('cannotbeempty');
1088 if ($this->form['actions'][$i][$interval_type] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i][$interval_type]))
1089 $this->errors['actions'][$i]['interval'] = $this->plugin->gettext('forbiddenchars');
1090 break;
1091
1092 case 'set':
1093 $this->form['actions'][$i]['name'] = $varnames[$idx];
1094 $this->form['actions'][$i]['value'] = $varvalues[$idx];
1095 foreach ((array)$varmods[$idx] as $v_m) {
1096 $this->form['actions'][$i][$v_m] = true;
1097 }
1098
1099 if (empty($varnames[$idx])) {
1100 $this->errors['actions'][$i]['name'] = $this->plugin->gettext('cannotbeempty');
1101 }
1102 else if (!preg_match('/^[0-9a-z_]+$/i', $varnames[$idx])) {
1103 $this->errors['actions'][$i]['name'] = $this->plugin->gettext('forbiddenchars');
1104 }
1105
1106 if (!isset($varvalues[$idx]) || $varvalues[$idx] === '') {
1107 $this->errors['actions'][$i]['value'] = $this->plugin->gettext('cannotbeempty');
1108 }
1109 break;
1110
1111 case 'notify':
1112 if (empty($notifymethods[$idx])) {
1113 $this->errors['actions'][$i]['method'] = $this->plugin->gettext('cannotbeempty');
1114 }
1115 if (empty($notifytargets[$idx])) {
1116 $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty');
1117 }
1118 if (!empty($notifyfrom[$idx]) && !rcube_utils::check_email($notifyfrom[$idx])) {
1119 $this->errors['actions'][$i]['from'] = $this->plugin->gettext('noemailwarning');
1120 }
1121
1122 // skip empty options
1123 foreach ((array)$notifyoptions[$idx] as $opt_idx => $opt) {
1124 if (!strlen(trim($opt))) {
1125 unset($notifyoptions[$idx][$opt_idx]);
1126 }
1127 }
1128
1129 $this->form['actions'][$i]['method'] = $notifymethods[$idx] . ':' . $notifytargets[$idx];
1130 $this->form['actions'][$i]['options'] = $notifyoptions[$idx];
1131 $this->form['actions'][$i]['message'] = $notifymessages[$idx];
1132 $this->form['actions'][$i]['from'] = $notifyfrom[$idx];
1133 $this->form['actions'][$i]['importance'] = $notifyimp[$idx];
1134 break;
1135 }
1136
1137 $this->form['actions'][$i]['type'] = $type;
1138 $i++;
1139 }
1140
1141 if (!$this->errors && !$error) {
1142 // save the script
1143 if (!isset($this->script[$fid])) {
1144 $fid = $this->sieve->script->add_rule($this->form);
1145 $new = true;
1146 }
1147 else {
1148 $fid = $this->sieve->script->update_rule($fid, $this->form);
1149 }
1150
1151 if ($fid !== false) {
1152 $save = $this->save_script();
1153 }
1154
1155 if ($save && $fid !== false) {
1156 $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
1157 if ($this->rc->task != 'mail') {
1158 $this->rc->output->command('parent.managesieve_updatelist',
1159 isset($new) ? 'add' : 'update',
1160 array(
1161 'name' => $this->form['name'],
1162 'id' => $fid,
1163 'disabled' => $this->form['disabled']
1164 ));
1165 }
1166 else {
1167 $this->rc->output->command('managesieve_dialog_close');
1168 $this->rc->output->send('iframe');
1169 }
1170 }
1171 else {
1172 $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
1173 }
1174 }
1175 else {
1176 $this->rc->output->show_message('managesieve.filterformerror', 'warning');
1177 }
1178 }
1179
1180 $this->send();
1181 }
1182
1183 protected function send()
1184 {
1185 // Handle form action
1186 if (isset($_GET['_framed']) || isset($_POST['_framed'])) {
1187 if (isset($_GET['_newset']) || isset($_POST['_newset'])) {
1188 $this->rc->output->send('managesieve.setedit');
1189 }
1190 else if (isset($_GET['_seteditraw']) || isset($_POST['_seteditraw'])) {
1191 $this->rc->output->send('managesieve.seteditraw');
1192 }
1193 else {
1194 $this->rc->output->send('managesieve.filteredit');
1195 }
1196 }
1197 else {
1198 $this->rc->output->set_pagetitle($this->plugin->gettext('filters'));
1199 $this->rc->output->send('managesieve.managesieve');
1200 }
1201 }
1202
1203 // return the filters list as HTML table
1204 function filters_list($attrib)
1205 {
1206 // add id to message list table if not specified
1207 if (!strlen($attrib['id']))
1208 $attrib['id'] = 'rcmfilterslist';
1209
1210 // define list of cols to be displayed
1211 $a_show_cols = array('name');
1212
1213 $result = $this->list_rules();
1214
1215 // create XHTML table
1216 $out = $this->rc->table_output($attrib, $result, $a_show_cols, 'id');
1217
1218 // set client env
1219 $this->rc->output->add_gui_object('filterslist', $attrib['id']);
1220 $this->rc->output->include_script('list.js');
1221
1222 // add some labels to client
1223 $this->rc->output->add_label('managesieve.filterdeleteconfirm');
1224
1225 return $out;
1226 }
1227
1228 // return the filters list as <SELECT>
1229 function filtersets_list($attrib, $no_env = false)
1230 {
1231 // add id to message list table if not specified
1232 if (!strlen($attrib['id'])) {
1233 $attrib['id'] = 'rcmfiltersetslist';
1234 }
1235
1236 $list = $this->list_scripts();
1237
1238 if ($list) {
1239 asort($list, SORT_LOCALE_STRING);
1240 }
1241
1242 if (!empty($attrib['type']) && $attrib['type'] == 'list') {
1243 // define list of cols to be displayed
1244 $a_show_cols = array('name');
1245
1246 if ($list) {
1247 foreach ($list as $idx => $set) {
1248 $scripts['S'.$idx] = $set;
1249 $result[] = array(
1250 'name' => $set,
1251 'id' => 'S'.$idx,
1252 'class' => !in_array($set, $this->active) ? 'disabled' : '',
1253 );
1254 }
1255 }
1256
1257 // create XHTML table
1258 $out = $this->rc->table_output($attrib, $result, $a_show_cols, 'id');
1259
1260 $this->rc->output->set_env('filtersets', $scripts);
1261 $this->rc->output->include_script('list.js');
1262 }
1263 else {
1264 $select = new html_select(array('name' => '_set', 'id' => $attrib['id'],
1265 'onchange' => $this->rc->task != 'mail' ? 'rcmail.managesieve_set()' : ''));
1266
1267 if ($list) {
1268 foreach ($list as $set)
1269 $select->add($set, $set);
1270 }
1271
1272 $out = $select->show($this->sieve->current);
1273 }
1274
1275 // set client env
1276 if (!$no_env) {
1277 $this->rc->output->add_gui_object('filtersetslist', $attrib['id']);
1278 $this->rc->output->add_label('managesieve.setdeleteconfirm');
1279 }
1280
1281 return $out;
1282 }
1283
1284 function filter_frame($attrib)
1285 {
1286 return $this->rc->output->frame($attrib, true);
1287 }
1288
1289 function filterset_editraw($attrib)
1290 {
1291 $script_name = isset($_GET['_set']) ? $_GET['_set'] : $_POST['_set'];
1292 $script = $this->sieve->get_script($script_name);
1293 $script_post = $_POST['rawsetcontent'];
1294
1295 $out = '<form name="filtersetrawform" action="./" method="post" enctype="multipart/form-data">'."\n";
1296
1297 $hiddenfields = new html_hiddenfield();
1298 $hiddenfields->add(array('name' => '_task', 'value' => $this->rc->task));
1299 $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-saveraw'));
1300 $hiddenfields->add(array('name' => '_set', 'value' => $script_name));
1301 $hiddenfields->add(array('name' => '_seteditraw', 'value' => 1));
1302 $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
1303
1304 $out .= $hiddenfields->show();
1305
1306 $txtarea = new html_textarea(array(
1307 'id' => 'rawfiltersettxt',
1308 'name' => 'rawsetcontent',
1309 'rows' => '15'
1310 ));
1311
1312 $out .= $txtarea->show($script_post !== null ? $script_post : ($script !== false ? rtrim($script) : ''));
1313
1314 $this->rc->output->add_gui_object('sievesetrawform', 'filtersetrawform');
1315 $this->plugin->include_stylesheet('codemirror/lib/codemirror.css');
1316 $this->plugin->include_script('codemirror/lib/codemirror.js');
1317 $this->plugin->include_script('codemirror/addon/selection/active-line.js');
1318 $this->plugin->include_script('codemirror/mode/sieve/sieve.js');
1319
1320 if ($script === false) {
1321 $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
1322 }
1323
1324 return $out;
1325 }
1326
1327 function filterset_form($attrib)
1328 {
1329 if (!$attrib['id'])
1330 $attrib['id'] = 'rcmfiltersetform';
1331
1332 $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n";
1333
1334 $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
1335 $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
1336 $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
1337 $hiddenfields->add(array('name' => '_newset', 'value' => 1));
1338
1339 $out .= $hiddenfields->show();
1340
1341 $name = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST);
1342 $copy = rcube_utils::get_input_value('_copy', rcube_utils::INPUT_POST);
1343 $selected = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
1344
1345 // filter set name input
1346 $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30,
1347 'class' => ($this->errors['name'] ? 'error' : '')));
1348
1349 $out .= sprintf('<label for="%s"><b>%s:</b></label> %s<br><br>',
1350 '_name', rcube::Q($this->plugin->gettext('filtersetname')), $input_name->show($name));
1351
1352 $out .="\n<fieldset class=\"itemlist\"><legend>" . $this->plugin->gettext('filters') . ":</legend>\n";
1353 $out .= html::tag('input', array(
1354 'type' => 'radio',
1355 'id' => 'from_none',
1356 'name' => '_from',
1357 'value' => 'none',
1358 'checked' => !$selected || $selected == 'none'
1359 ));
1360 $out .= html::label('from_none', rcube::Q($this->plugin->gettext('none')));
1361
1362 // filters set list
1363 $list = $this->list_scripts();
1364 $select = new html_select(array('name' => '_copy', 'id' => '_copy'));
1365
1366 if (is_array($list)) {
1367 asort($list, SORT_LOCALE_STRING);
1368
1369 if (!$copy)
1370 $copy = $_SESSION['managesieve_current'];
1371
1372 foreach ($list as $set) {
1373 $select->add($set, $set);
1374 }
1375
1376 $out .= '<br>';
1377 $out .= html::tag('input', array(
1378 'type' => 'radio',
1379 'id' => 'from_set',
1380 'name' => '_from',
1381 'value' => 'set',
1382 'checked' => $selected == 'set',
1383 ));
1384 $out .= html::label('from_set', rcube::Q($this->plugin->gettext('fromset')));
1385 $out .= $select->show($copy);
1386 }
1387
1388 // script upload box
1389 $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30,
1390 'type' => 'file', 'class' => ($this->errors['file'] ? 'error' : '')));
1391
1392 $out .= '<br>';
1393 $out .= html::tag('input', array(
1394 'type' => 'radio',
1395 'id' => 'from_file',
1396 'name' => '_from',
1397 'value' => 'file',
1398 'checked' => $selected == 'file',
1399 ));
1400 $out .= html::label('from_file', rcube::Q($this->plugin->gettext('fromfile')));
1401 $out .= $upload->show();
1402 $out .= '</fieldset>';
1403
1404 $this->rc->output->add_gui_object('sieveform', 'filtersetform');
1405
1406 if ($this->errors['name'])
1407 $this->add_tip('_name', $this->errors['name'], true);
1408 if ($this->errors['file'])
1409 $this->add_tip('_file', $this->errors['file'], true);
1410
1411 $this->print_tips();
1412
1413 return $out;
1414 }
1415
1416
1417 function filter_form($attrib)
1418 {
1419 if (!$attrib['id'])
1420 $attrib['id'] = 'rcmfilterform';
1421
1422 $fid = rcube_utils::get_input_value('_fid', rcube_utils::INPUT_GPC);
1423 $scr = isset($this->form) ? $this->form : $this->script[$fid];
1424
1425 $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
1426 $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
1427 $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
1428 $hiddenfields->add(array('name' => '_fid', 'value' => $fid));
1429
1430 $out = '<form name="filterform" action="./" method="post">'."\n";
1431 $out .= $hiddenfields->show();
1432
1433 // 'any' flag
1434 if ((!isset($this->form) && empty($scr['tests']) && !empty($scr))
1435 || (count($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not'])
1436 ) {
1437 $any = true;
1438 }
1439
1440 // filter name input
1441 $field_id = '_name';
1442 $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30,
1443 'class' => ($this->errors['name'] ? 'error' : '')));
1444
1445 if ($this->errors['name'])
1446 $this->add_tip($field_id, $this->errors['name'], true);
1447
1448 if (isset($scr))
1449 $input_name = $input_name->show($scr['name']);
1450 else
1451 $input_name = $input_name->show();
1452
1453 $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s\n",
1454 $field_id, rcube::Q($this->plugin->gettext('filtername')), $input_name);
1455
1456 // filter set selector
1457 if ($this->rc->task == 'mail') {
1458 $out .= sprintf("\n&nbsp;<label for=\"%s\"><b>%s:</b></label> %s\n",
1459 $field_id, rcube::Q($this->plugin->gettext('filterset')),
1460 $this->filtersets_list(array('id' => 'sievescriptname'), true));
1461 }
1462
1463 $out .= '<br><br><fieldset><legend>' . rcube::Q($this->plugin->gettext('messagesrules')) . "</legend>\n";
1464
1465 // any, allof, anyof radio buttons
1466 $field_id = '_allof';
1467 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof',
1468 'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio'));
1469
1470 if (isset($scr) && !$any)
1471 $input_join = $input_join->show($scr['join'] ? 'allof' : '');
1472 else
1473 $input_join = $input_join->show();
1474
1475 $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filterallof')));
1476
1477 $field_id = '_anyof';
1478 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof',
1479 'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio'));
1480
1481 if (isset($scr) && !$any)
1482 $input_join = $input_join->show($scr['join'] ? '' : 'anyof');
1483 else
1484 $input_join = $input_join->show('anyof'); // default
1485
1486 $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filteranyof')));
1487
1488 $field_id = '_any';
1489 $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any',
1490 'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio'));
1491
1492 $input_join = $input_join->show($any ? 'any' : '');
1493
1494 $out .= $input_join . html::label($field_id, rcube::Q($this->plugin->gettext('filterany')));
1495
1496 $rows_num = !empty($scr['tests']) ? count($scr['tests']) : 1;
1497
1498 $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>';
1499 for ($x=0; $x<$rows_num; $x++)
1500 $out .= $this->rule_div($fid, $x);
1501 $out .= "</div>\n";
1502
1503 $out .= "</fieldset>\n";
1504
1505 // actions
1506 $out .= '<fieldset><legend>' . rcube::Q($this->plugin->gettext('messagesactions')) . "</legend>\n";
1507
1508 $rows_num = isset($scr) ? count($scr['actions']) : 1;
1509
1510 $out .= '<div id="actions">';
1511 for ($x=0; $x<$rows_num; $x++)
1512 $out .= $this->action_div($fid, $x);
1513 $out .= "</div>\n";
1514
1515 $out .= "</fieldset>\n";
1516
1517 $this->print_tips();
1518
1519 if ($scr['disabled']) {
1520 $this->rc->output->set_env('rule_disabled', true);
1521 }
1522 $this->rc->output->add_label(
1523 'managesieve.ruledeleteconfirm',
1524 'managesieve.actiondeleteconfirm'
1525 );
1526 $this->rc->output->add_gui_object('sieveform', 'filterform');
1527
1528 return $out;
1529 }
1530
1531 function rule_div($fid, $id, $div=true)
1532 {
1533 $rule = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id];
1534 $rows_num = isset($this->form) ? count($this->form['tests']) : count($this->script[$fid]['tests']);
1535
1536 // headers select
1537 $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id,
1538 'onchange' => 'rule_header_select(' .$id .')'));
1539
1540 foreach ($this->headers as $index => $header) {
1541 $header = $this->rc->text_exists($index) ? $this->plugin->gettext($index) : $header;
1542 $select_header->add($header, $index);
1543 }
1544 $select_header->add($this->plugin->gettext('...'), '...');
1545 if (in_array('body', $this->exts)) {
1546 $select_header->add($this->plugin->gettext('body'), 'body');
1547 }
1548 $select_header->add($this->plugin->gettext('size'), 'size');
1549 if (in_array('date', $this->exts)) {
1550 $select_header->add($this->plugin->gettext('datetest'), 'date');
1551 $select_header->add($this->plugin->gettext('currdate'), 'currentdate');
1552 }
1553 if (in_array('variables', $this->exts)) {
1554 $select_header->add($this->plugin->gettext('string'), 'string');
1555 }
1556 if (in_array('duplicate', $this->exts)) {
1557 $select_header->add($this->plugin->gettext('message'), 'message');
1558 }
1559
1560 if (isset($rule['test'])) {
1561 if (in_array($rule['test'], array('header', 'address', 'envelope'))) {
1562 if (is_array($rule['arg1']) && count($rule['arg1']) == 1) {
1563 $rule['arg1'] = $rule['arg1'][0];
1564 }
1565
1566 $matches = ($header = strtolower($rule['arg1'])) && isset($this->headers[$header]);
1567 $test = $matches ? $header : '...';
1568 }
1569 else if ($rule['test'] == 'exists') {
1570 if (is_array($rule['arg']) && count($rule['arg']) == 1) {
1571 $rule['arg'] = $rule['arg'][0];
1572 }
1573
1574 $matches = ($header = strtolower($rule['arg'])) && isset($this->headers[$header]);
1575 $test = $matches ? $header : '...';
1576 }
1577 else if (in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'string'))) {
1578 $test = $rule['test'];
1579 }
1580 else if (in_array($rule['test'], array('duplicate'))) {
1581 $test = 'message';
1582 }
1583 else if ($rule['test'] != 'true') {
1584 $test = '...';
1585 }
1586 }
1587
1588 $aout = $select_header->show($test);
1589
1590 // custom headers input
1591 if (isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) {
1592 $custom = (array) $rule['arg1'];
1593 if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) {
1594 unset($custom);
1595 }
1596 }
1597 else if (isset($rule['test']) && $rule['test'] == 'string') {
1598 $customv = (array) $rule['arg1'];
1599 if (count($customv) == 1 && isset($this->headers[strtolower($customv[0])])) {
1600 unset($customv);
1601 }
1602 }
1603 else if (isset($rule['test']) && $rule['test'] == 'exists') {
1604 $custom = (array) $rule['arg'];
1605 if (count($custom) == 1 && isset($this->headers[strtolower($custom[0])])) {
1606 unset($custom);
1607 }
1608 }
1609
1610 // custom variable input
1611 $tout = $this->list_input($id, 'custom_header', $custom, isset($custom),
1612 $this->error_class($id, 'test', 'header', 'custom_header'), 15) . "\n";
1613
1614 $tout .= $this->list_input($id, 'custom_var', $customv, isset($customv),
1615 $this->error_class($id, 'test', 'header', 'custom_var'), 15) . "\n";
1616
1617 // matching type select (operator)
1618 $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id,
1619 'style' => 'display:' .(!in_array($rule['test'], array('size', 'duplicate')) ? 'inline' : 'none'),
1620 'class' => 'operator_selector',
1621 'onchange' => 'rule_op_select(this, '.$id.')'));
1622 $select_op->add(rcube::Q($this->plugin->gettext('filtercontains')), 'contains');
1623 $select_op->add(rcube::Q($this->plugin->gettext('filternotcontains')), 'notcontains');
1624 $select_op->add(rcube::Q($this->plugin->gettext('filteris')), 'is');
1625 $select_op->add(rcube::Q($this->plugin->gettext('filterisnot')), 'notis');
1626 $select_op->add(rcube::Q($this->plugin->gettext('filterexists')), 'exists');
1627 $select_op->add(rcube::Q($this->plugin->gettext('filternotexists')), 'notexists');
1628 $select_op->add(rcube::Q($this->plugin->gettext('filtermatches')), 'matches');
1629 $select_op->add(rcube::Q($this->plugin->gettext('filternotmatches')), 'notmatches');
1630 if (in_array('regex', $this->exts)) {
1631 $select_op->add(rcube::Q($this->plugin->gettext('filterregex')), 'regex');
1632 $select_op->add(rcube::Q($this->plugin->gettext('filternotregex')), 'notregex');
1633 }
1634 if (in_array('relational', $this->exts)) {
1635 $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthan')), 'count-gt');
1636 $select_op->add(rcube::Q($this->plugin->gettext('countisgreaterthanequal')), 'count-ge');
1637 $select_op->add(rcube::Q($this->plugin->gettext('countislessthan')), 'count-lt');
1638 $select_op->add(rcube::Q($this->plugin->gettext('countislessthanequal')), 'count-le');
1639 $select_op->add(rcube::Q($this->plugin->gettext('countequals')), 'count-eq');
1640 $select_op->add(rcube::Q($this->plugin->gettext('countnotequals')), 'count-ne');
1641 $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthan')), 'value-gt');
1642 $select_op->add(rcube::Q($this->plugin->gettext('valueisgreaterthanequal')), 'value-ge');
1643 $select_op->add(rcube::Q($this->plugin->gettext('valueislessthan')), 'value-lt');
1644 $select_op->add(rcube::Q($this->plugin->gettext('valueislessthanequal')), 'value-le');
1645 $select_op->add(rcube::Q($this->plugin->gettext('valueequals')), 'value-eq');
1646 $select_op->add(rcube::Q($this->plugin->gettext('valuenotequals')), 'value-ne');
1647 }
1648
1649 $test = self::rule_test($rule);
1650 $target = '';
1651
1652 // target(s) input
1653 if (in_array($rule['test'], array('header', 'address', 'envelope','string'))) {
1654 $target = $rule['arg2'];
1655 }
1656 else if (in_array($rule['test'], array('body', 'date', 'currentdate'))) {
1657 $target = $rule['arg'];
1658 }
1659 else if ($rule['test'] == 'size') {
1660 if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) {
1661 $sizetarget = $matches[1];
1662 $sizeitem = $matches[2];
1663 }
1664 else {
1665 $sizetarget = $rule['arg'];
1666 $sizeitem = $rule['item'];
1667 }
1668 }
1669
1670 // (current)date part select
1671 if (in_array('date', $this->exts) || in_array('currentdate', $this->exts)) {
1672 $date_parts = array('date', 'iso8601', 'std11', 'julian', 'time',
1673 'year', 'month', 'day', 'hour', 'minute', 'second', 'weekday', 'zone');
1674 $select_dp = new html_select(array('name' => "_rule_date_part[]", 'id' => 'rule_date_part'.$id,
1675 'style' => in_array($rule['test'], array('currentdate', 'date')) && !preg_match('/^(notcount|count)-/', $test) ? '' : 'display:none',
1676 'class' => 'datepart_selector',
1677 ));
1678
1679 foreach ($date_parts as $part) {
1680 $select_dp->add(rcube::Q($this->plugin->gettext($part)), $part);
1681 }
1682
1683 $tout .= $select_dp->show($rule['test'] == 'currentdate' || $rule['test'] == 'date' ? $rule['part'] : '');
1684 }
1685
1686 // message test select (e.g. duplicate)
1687 if (in_array('duplicate', $this->exts)) {
1688 $select_msg = new html_select(array('name' => "_rule_message[]", 'id' => 'rule_message'.$id,
1689 'style' => in_array($rule['test'], array('duplicate')) ? '' : 'display:none',
1690 'class' => 'message_selector',
1691 ));
1692
1693 $select_msg->add(rcube::Q($this->plugin->gettext('duplicate')), 'duplicate');
1694 $select_msg->add(rcube::Q($this->plugin->gettext('notduplicate')), 'notduplicate');
1695
1696 $tout .= $select_msg->show($test);
1697 }
1698
1699 $tout .= $select_op->show($test);
1700 $tout .= $this->list_input($id, 'rule_target', $target,
1701 $rule['test'] != 'size' && $rule['test'] != 'exists' && $rule['test'] != 'duplicate',
1702 $this->error_class($id, 'test', 'target', 'rule_target')) . "\n";
1703
1704 $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id));
1705 $select_size_op->add(rcube::Q($this->plugin->gettext('filterover')), 'over');
1706 $select_size_op->add(rcube::Q($this->plugin->gettext('filterunder')), 'under');
1707
1708 $tout .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">';
1709 $tout .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : '');
1710 $tout .= html::tag('input', array(
1711 'type' => 'text',
1712 'name' => '_rule_size_target[]',
1713 'id' => 'rule_size_i'.$id,
1714 'value' => $sizetarget,
1715 'size' => 10,
1716 'class' => $this->error_class($id, 'test', 'sizetarget', 'rule_size_i'),
1717 ));
1718 foreach (array('', 'K', 'M', 'G') as $unit) {
1719 $tout .= html::label(null, html::tag('input', array(
1720 'type' => 'radio',
1721 'name' => '_rule_size_item['.$id.']',
1722 'value' => $unit,
1723 'checked' => $sizeitem == $unit,
1724 'class' => 'radio',
1725 )) . $this->rc->gettext($unit . 'B'));
1726 }
1727 $tout .= '</div>';
1728
1729 // Advanced modifiers (address, envelope)
1730 $select_mod = new html_select(array('name' => "_rule_mod[]", 'id' => 'rule_mod_op'.$id,
1731 'onchange' => 'rule_mod_select(' .$id .')'));
1732 $select_mod->add(rcube::Q($this->plugin->gettext('none')), '');
1733 $select_mod->add(rcube::Q($this->plugin->gettext('address')), 'address');
1734 if (in_array('envelope', $this->exts)) {
1735 $select_mod->add(rcube::Q($this->plugin->gettext('envelope')), 'envelope');
1736 }
1737
1738 $select_type = new html_select(array('name' => "_rule_mod_type[]", 'id' => 'rule_mod_type'.$id));
1739 $select_type->add(rcube::Q($this->plugin->gettext('allparts')), 'all');
1740 $select_type->add(rcube::Q($this->plugin->gettext('domain')), 'domain');
1741 $select_type->add(rcube::Q($this->plugin->gettext('localpart')), 'localpart');
1742 if (in_array('subaddress', $this->exts)) {
1743 $select_type->add(rcube::Q($this->plugin->gettext('user')), 'user');
1744 $select_type->add(rcube::Q($this->plugin->gettext('detail')), 'detail');
1745 }
1746
1747 $need_mod = !in_array($rule['test'], array('size', 'body', 'date', 'currentdate', 'duplicate', 'string'));
1748 $mout = '<div id="rule_mod' .$id. '" class="adv"' . (!$need_mod ? ' style="display:none"' : '') . '>';
1749 $mout .= ' <span class="label">' . rcube::Q($this->plugin->gettext('modifier')) . ' </span>';
1750 $mout .= $select_mod->show($rule['test']);
1751 $mout .= ' <span id="rule_mod_type' . $id . '"';
1752 $mout .= ' style="display:' . (in_array($rule['test'], array('address', 'envelope')) ? 'inline' : 'none') .'">';
1753 $mout .= rcube::Q($this->plugin->gettext('modtype')) . ' ';
1754 $mout .= $select_type->show($rule['part']);
1755 $mout .= '</span>';
1756 $mout .= '</div>';
1757
1758 // Advanced modifiers (body transformations)
1759 $select_mod = new html_select(array('name' => "_rule_trans[]", 'id' => 'rule_trans_op'.$id,
1760 'onchange' => 'rule_trans_select(' .$id .')'));
1761 $select_mod->add(rcube::Q($this->plugin->gettext('text')), 'text');
1762 $select_mod->add(rcube::Q($this->plugin->gettext('undecoded')), 'raw');
1763 $select_mod->add(rcube::Q($this->plugin->gettext('contenttype')), 'content');
1764
1765 $mout .= '<div id="rule_trans' .$id. '" class="adv"' . ($rule['test'] != 'body' ? ' style="display:none"' : '') . '>';
1766 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('modifier')) . '</span>';
1767 $mout .= $select_mod->show($rule['part']);
1768 $mout .= html::tag('input', array(
1769 'type' => 'text',
1770 'name' => '_rule_trans_type[]',
1771 'id' => 'rule_trans_type'.$id,
1772 'value' => is_array($rule['content']) ? implode(',', $rule['content']) : $rule['content'],
1773 'size' => 20,
1774 'style' => $rule['part'] != 'content' ? 'display:none' : '',
1775 'class' => $this->error_class($id, 'test', 'part', 'rule_trans_type'),
1776 ));
1777 $mout .= '</div>';
1778
1779 // Advanced modifiers (body transformations)
1780 $select_comp = new html_select(array('name' => "_rule_comp[]", 'id' => 'rule_comp_op'.$id));
1781 $select_comp->add(rcube::Q($this->plugin->gettext('default')), '');
1782 $select_comp->add(rcube::Q($this->plugin->gettext('octet')), 'i;octet');
1783 $select_comp->add(rcube::Q($this->plugin->gettext('asciicasemap')), 'i;ascii-casemap');
1784 if (in_array('comparator-i;ascii-numeric', $this->exts)) {
1785 $select_comp->add(rcube::Q($this->plugin->gettext('asciinumeric')), 'i;ascii-numeric');
1786 }
1787
1788 // Comparators
1789 $need_comp = $rule['test'] != 'size' && $rule['test'] != 'duplicate';
1790 $mout .= '<div id="rule_comp' .$id. '" class="adv"' . (!$need_comp ? ' style="display:none"' : '') . '>';
1791 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('comparator')) . '</span>';
1792 $mout .= $select_comp->show($rule['comparator']);
1793 $mout .= '</div>';
1794
1795 // Date header
1796 if (in_array('date', $this->exts)) {
1797 $mout .= '<div id="rule_date_header_div' .$id. '" class="adv"'. ($rule['test'] != 'date' ? ' style="display:none"' : '') .'>';
1798 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('dateheader')) . '</span>';
1799 $mout .= html::tag('input', array(
1800 'type' => 'text',
1801 'name' => '_rule_date_header[]',
1802 'id' => 'rule_date_header' . $id,
1803 'value' => $rule['test'] == 'date' ? $rule['header'] : '',
1804 'size' => 15,
1805 'class' => $this->error_class($id, 'test', 'dateheader', 'rule_date_header'),
1806 ));
1807 $mout .= '</div>';
1808 }
1809
1810 // Index
1811 if (in_array('index', $this->exts)) {
1812 $need_index = in_array($rule['test'], array('header', ', address', 'date'));
1813 $mout .= '<div id="rule_index_div' .$id. '" class="adv"'. (!$need_index ? ' style="display:none"' : '') .'>';
1814 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('index')) . '</span>';
1815 $mout .= html::tag('input', array(
1816 'type' => 'text',
1817 'name' => '_rule_index[]',
1818 'id' => 'rule_index' . $id,
1819 'value' => $rule['index'] ? intval($rule['index']) : '',
1820 'size' => 3,
1821 'class' => $this->error_class($id, 'test', 'index', 'rule_index'),
1822 ));
1823 $mout .= '&nbsp;' . html::tag('input', array(
1824 'type' => 'checkbox',
1825 'name' => '_rule_index_last[]',
1826 'id' => 'rule_index_last' . $id,
1827 'value' => 1,
1828 'checked' => !empty($rule['last']),
1829 ))
1830 . html::label('rule_index_last' . $id, rcube::Q($this->plugin->gettext('indexlast')));
1831 $mout .= '</div>';
1832 }
1833
1834 // Duplicate
1835 if (in_array('duplicate', $this->exts)) {
1836 $need_duplicate = $rule['test'] == 'duplicate';
1837 $mout .= '<div id="rule_duplicate_div' .$id. '" class="adv"'. (!$need_duplicate ? ' style="display:none"' : '') .'>';
1838
1839 foreach (array('handle', 'header', 'uniqueid') as $unit) {
1840 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('duplicate.handle')) . '</span>';
1841 $mout .= html::tag('input', array(
1842 'type' => 'text',
1843 'name' => '_rule_duplicate_' . $unit . '[]',
1844 'id' => 'rule_duplicate_' . $unit . $id,
1845 'value' => $rule[$unit],
1846 'size' => 30,
1847 'class' => $this->error_class($id, 'test', 'duplicate_' . $unit, 'rule_duplicate_' . $unit),
1848 ));
1849 $mout .= '<br>';
1850 }
1851
1852 $mout .= '<span class="label">' . rcube::Q($this->plugin->gettext('duplicate.seconds')) . '</span>';
1853 $mout .= html::tag('input', array(
1854 'type' => 'text',
1855 'name' => '_rule_duplicate_seconds[]',
1856 'id' => 'rule_duplicate_seconds' . $id,
1857 'value' => $rule['seconds'],
1858 'size' => 6,
1859 'class' => $this->error_class($id, 'test', 'duplicate_seconds', 'rule_duplicate_seconds'),
1860 ));
1861 $mout .= '&nbsp;' . html::tag('input', array(
1862 'type' => 'checkbox',
1863 'name' => '_rule_duplicate_last[' . $id . ']',
1864 'id' => 'rule_duplicate_last' . $id,
1865 'value' => 1,
1866 'checked' => !empty($rule['last']),
1867 ));
1868 $mout .= html::label('rule_duplicate_last' . $id, rcube::Q($this->plugin->gettext('duplicate.last')));
1869 $mout .= '</div>';
1870 }
1871
1872 // Build output table
1873 $out = $div ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : '';
1874 $out .= '<table><tr>';
1875 $out .= '<td class="advbutton">';
1876 $out .= '<a href="#" id="ruleadv' . $id .'" title="'. rcube::Q($this->plugin->gettext('advancedopts')). '"
1877 onclick="rule_adv_switch(' . $id .', this)" class="show">&nbsp;&nbsp;</a>';
1878 $out .= '</td>';
1879 $out .= '<td class="rowactions">' . $aout . '</td>';
1880 $out .= '<td class="rowtargets">' . $tout . "\n";
1881 $out .= '<div id="rule_advanced' .$id. '" style="display:none">' . $mout . '</div>';
1882 $out .= '</td>';
1883
1884 // add/del buttons
1885 $out .= '<td class="rowbuttons">';
1886 $out .= '<a href="#" id="ruleadd' . $id .'" title="'. rcube::Q($this->plugin->gettext('add')). '"
1887 onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button add"></a>';
1888 $out .= '<a href="#" id="ruledel' . $id .'" title="'. rcube::Q($this->plugin->gettext('del')). '"
1889 onclick="rcmail.managesieve_ruledel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
1890 $out .= '</td>';
1891 $out .= '</tr></table>';
1892
1893 $out .= $div ? "</div>\n" : '';
1894
1895 return $out;
1896 }
1897
1898 private static function rule_test(&$rule)
1899 {
1900 // first modify value/count tests with 'not' keyword
1901 // we'll revert the meaning of operators
1902 if ($rule['not'] && preg_match('/^(count|value)-([gteqnl]{2})/', $rule['type'], $m)) {
1903 $rule['not'] = false;
1904
1905 switch ($m[2]) {
1906 case 'gt': $rule['type'] = $m[1] . '-le'; break;
1907 case 'ge': $rule['type'] = $m[1] . '-lt'; break;
1908 case 'lt': $rule['type'] = $m[1] . '-ge'; break;
1909 case 'le': $rule['type'] = $m[1] . '-gt'; break;
1910 case 'eq': $rule['type'] = $m[1] . '-ne'; break;
1911 case 'ne': $rule['type'] = $m[1] . '-eq'; break;
1912 }
1913 }
1914 else if ($rule['not'] && $rule['test'] == 'size') {
1915 $rule['not'] = false;
1916 $rule['type'] = $rule['type'] == 'over' ? 'under' : 'over';
1917 }
1918
1919 $set = array('header', 'address', 'envelope', 'body', 'date', 'currentdate', 'string');
1920
1921 // build test string supported by select element
1922 if ($rule['size']) {
1923 $test = $rule['type'];
1924 }
1925 else if (in_array($rule['test'], $set)) {
1926 $test = ($rule['not'] ? 'not' : '') . ($rule['type'] ?: 'is');
1927 }
1928 else {
1929 $test = ($rule['not'] ? 'not' : '') . $rule['test'];
1930 }
1931
1932 return $test;
1933 }
1934
1935 function action_div($fid, $id, $div=true)
1936 {
1937 $action = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id];
1938 $rows_num = isset($this->form) ? count($this->form['actions']) : count($this->script[$fid]['actions']);
1939
1940 $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : '';
1941
1942 $out .= '<table><tr><td class="rowactions">';
1943
1944 // action select
1945 $select_action = new html_select(array('name' => "_action_type[$id]", 'id' => 'action_type'.$id,
1946 'onchange' => 'action_type_select(' .$id .')'));
1947 if (in_array('fileinto', $this->exts))
1948 $select_action->add(rcube::Q($this->plugin->gettext('messagemoveto')), 'fileinto');
1949 if (in_array('fileinto', $this->exts) && in_array('copy', $this->exts))
1950 $select_action->add(rcube::Q($this->plugin->gettext('messagecopyto')), 'fileinto_copy');
1951 $select_action->add(rcube::Q($this->plugin->gettext('messageredirect')), 'redirect');
1952 if (in_array('copy', $this->exts))
1953 $select_action->add(rcube::Q($this->plugin->gettext('messagesendcopy')), 'redirect_copy');
1954 if (in_array('reject', $this->exts))
1955 $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'reject');
1956 else if (in_array('ereject', $this->exts))
1957 $select_action->add(rcube::Q($this->plugin->gettext('messagediscard')), 'ereject');
1958 if (in_array('vacation', $this->exts))
1959 $select_action->add(rcube::Q($this->plugin->gettext('messagereply')), 'vacation');
1960 $select_action->add(rcube::Q($this->plugin->gettext('messagedelete')), 'discard');
1961 if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) {
1962 $select_action->add(rcube::Q($this->plugin->gettext('setflags')), 'setflag');
1963 $select_action->add(rcube::Q($this->plugin->gettext('addflags')), 'addflag');
1964 $select_action->add(rcube::Q($this->plugin->gettext('removeflags')), 'removeflag');
1965 }
1966 if (in_array('variables', $this->exts)) {
1967 $select_action->add(rcube::Q($this->plugin->gettext('setvariable')), 'set');
1968 }
1969 if (in_array('enotify', $this->exts) || in_array('notify', $this->exts)) {
1970 $select_action->add(rcube::Q($this->plugin->gettext('notify')), 'notify');
1971 }
1972 $select_action->add(rcube::Q($this->plugin->gettext('messagekeep')), 'keep');
1973 $select_action->add(rcube::Q($this->plugin->gettext('rulestop')), 'stop');
1974
1975 $select_type = $action['type'];
1976 if (in_array($action['type'], array('fileinto', 'redirect')) && $action['copy']) {
1977 $select_type .= '_copy';
1978 }
1979
1980 $out .= $select_action->show($select_type);
1981 $out .= '</td>';
1982
1983 // actions target inputs
1984 $out .= '<td class="rowtargets">';
1985
1986 // force domain selection in redirect email input
1987 $domains = (array) $this->rc->config->get('managesieve_domains');
1988 if (!empty($domains)) {
1989 sort($domains);
1990
1991 $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id));
1992 $domain_select->add(array_combine($domains, $domains));
1993
1994 if ($action['type'] == 'redirect') {
1995 $parts = explode('@', $action['target']);
1996 if (!empty($parts)) {
1997 $action['domain'] = array_pop($parts);
1998 $action['target'] = implode('@', $parts);
1999 }
2000 }
2001 }
2002
2003 // redirect target
2004 $out .= '<span id="redirect_target' . $id . '" style="white-space:nowrap;'
2005 . ' display:' . ($action['type'] == 'redirect' ? 'inline' : 'none') . '">'
2006 . html::tag('input', array(
2007 'type' => 'text',
2008 'name' => '_action_target[' . $id . ']',
2009 'id' => 'action_target' . $id,
2010 'value' => $action['type'] == 'redirect' ? $action['target'] : '',
2011 'size' => !empty($domains) ? 20 : 35,
2012 'class' => $this->error_class($id, 'action', 'target', 'action_target'),
2013 ));
2014 $out .= !empty($domains) ? ' @ ' . $domain_select->show($action['domain']) : '';
2015 $out .= '</span>';
2016
2017 // (e)reject target
2018 $out .= html::tag('textarea', array(
2019 'name' => '_action_target_area[' . $id . ']',
2020 'id' => 'action_target_area' . $id,
2021 'rows' => 3,
2022 'cols' => 35,
2023 'class' => $this->error_class($id, 'action', 'targetarea', 'action_target_area'),
2024 'style' => 'display:' . (in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none'),
2025 ), (in_array($action['type'], array('reject', 'ereject')) ? rcube::Q($action['target'], 'strict', false) : ''));
2026
2027 // vacation
2028 $vsec = in_array('vacation-seconds', $this->exts);
2029 $auto_addr = $this->rc->config->get('managesieve_vacation_addresses_init');
2030 $from_addr = $this->rc->config->get('managesieve_vacation_from_init');
2031
2032 if (empty($action)) {
2033 if ($auto_addr) {
2034 $action['addresses'] = $this->user_emails();
2035 }
2036 if ($from_addr) {
2037 $default_identity = $this->rc->user->list_emails(true);
2038 $action['from'] = $default_identity['email'];
2039 }
2040 }
2041
2042 $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
2043 $out .= '<span class="label">'. rcube::Q($this->plugin->gettext('vacationreason')) .'</span><br>';
2044 $out .= html::tag('textarea', array(
2045 'name' => '_action_reason[' . $id . ']',
2046 'id' => 'action_reason' . $id,
2047 'rows' => 3,
2048 'cols' => 35,
2049 'class' => $this->error_class($id, 'action', 'reason', 'action_reason'),
2050 ), rcube::Q($action['reason'], 'strict', false));
2051 $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationsubject')) . '</span><br>';
2052 $out .= html::tag('input', array(
2053 'type' => 'text',
2054 'name' => '_action_subject[' . $id . ']',
2055 'id' => 'action_subject' . $id,
2056 'value' => is_array($action['subject']) ? implode(', ', $action['subject']) : $action['subject'],
2057 'size' => 35,
2058 'class' => $this->error_class($id, 'action', 'subject', 'action_subject'),
2059 ));
2060 $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationfrom')) . '</span><br>';
2061 $out .= html::tag('input', array(
2062 'type' => 'text',
2063 'name' => '_action_from[' . $id . ']',
2064 'id' => 'action_from' . $id,
2065 'value' => $action['from'],
2066 'size' => 35,
2067 'class' => $this->error_class($id, 'action', 'from', 'action_from'),
2068 ));
2069 $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('vacationaddr')) . '</span><br>';
2070 $out .= $this->list_input($id, 'action_addresses', $action['addresses'], true,
2071 $this->error_class($id, 'action', 'addresses', 'action_addresses'), 30)
2072 . html::a(array('href' => '#', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".managesieve_vacation_addresses($id)"),
2073 rcube::Q($this->plugin->gettext('filladdresses')));
2074 $out .= '<br><span class="label">' . rcube::Q($this->plugin->gettext($vsec ? 'vacationinterval' : 'vacationdays')) . '</span><br>';
2075 $out .= html::tag('input', array(
2076 'type' => 'text',
2077 'name' => '_action_interval[' . $id . ']',
2078 'id' => 'action_interval' . $id,
2079 'value' => rcube_sieve_vacation::vacation_interval($action),
2080 'size' => 2,
2081 'class' => $this->error_class($id, 'action', 'interval', 'action_interval'),
2082 ));
2083 if ($vsec) {
2084 foreach (array('days', 'seconds') as $unit) {
2085 $out .= '&nbsp;' . html::label(null, html::tag('input', array(
2086 'type' => 'radio',
2087 'name' => '_action_interval_type[' . $id . ']',
2088 'value' => $unit,
2089 'checked' => ($unit == 'seconds' && isset($action['seconds'])
2090 || $unit == 'deys' && !isset($action['seconds'])),
2091 'class' => 'radio',
2092 )) . $this->plugin->gettext($unit));
2093 }
2094 }
2095 $out .= '</div>';
2096
2097 // flags
2098 $flags = array(
2099 'read' => '\\Seen',
2100 'answered' => '\\Answered',
2101 'flagged' => '\\Flagged',
2102 'deleted' => '\\Deleted',
2103 'draft' => '\\Draft',
2104 );
2105 $flags_target = (array)$action['target'];
2106
2107 $flout = '';
2108 foreach ($flags as $fidx => $flag) {
2109 $flout .= html::tag('input', array(
2110 'type' => 'checkbox',
2111 'name' => '_action_flags[' .$id .'][]',
2112 'value' => $flag,
2113 'checked' => in_array_nocase($flag, $flags_target),
2114 ))
2115 . rcube::Q($this->plugin->gettext('flag'.$fidx)) .'<br>';
2116 }
2117 $out .= html::div(array(
2118 'id' => 'action_flags' . $id,
2119 'style' => 'display:' . (preg_match('/^(set|add|remove)flag$/', $action['type']) ? 'inline' : 'none'),
2120 'class' => $this->error_class($id, 'action', 'flags', 'action_flags'),
2121 ), $flout);
2122
2123 // set variable
2124 $set_modifiers = array(
2125 'lower',
2126 'upper',
2127 'lowerfirst',
2128 'upperfirst',
2129 'quotewildcard',
2130 'length'
2131 );
2132
2133 $out .= '<div id="action_set' .$id.'" style="display:' .($action['type']=='set' ? 'inline' : 'none') .'">';
2134 foreach (array('name', 'value') as $unit) {
2135 $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('setvar' . $unit)) . '</span><br>';
2136 $out .= html::tag('input', array(
2137 'type' => 'text',
2138 'name' => '_action_var' . $unit . '[' . $id . ']',
2139 'id' => 'action_var' . $unit . $id,
2140 'value' => $action[$unit],
2141 'size' => 35,
2142 'class' => $this->error_class($id, 'action', $unit, 'action_var' . $unit),
2143 ));
2144 $out .= '<br>';
2145 }
2146 $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('setvarmodifiers')) . '</span>';
2147 foreach ($set_modifiers as $s_m) {
2148 $s_m_id = 'action_varmods' . $id . $s_m;
2149 $out .= '<br>' . html::tag('input', array(
2150 'type' => 'checkbox',
2151 'name' => '_action_varmods[' . $id . '][]',
2152 'value' => $s_m,
2153 'id' => $s_m_id,
2154 'checked' => array_key_exists($s_m, (array)$action) && $action[$s_m],
2155 ))
2156 .rcube::Q($this->plugin->gettext('var' . $s_m));
2157 }
2158 $out .= '</div>';
2159
2160 // notify
2161 $notify_methods = (array) $this->rc->config->get('managesieve_notify_methods');
2162 $importance_options = $this->notify_importance_options;
2163
2164 if (empty($notify_methods)) {
2165 $notify_methods = $this->notify_methods;
2166 }
2167
2168 list($method, $target) = explode(':', $action['method'], 2);
2169 $method = strtolower($method);
2170
2171 if ($method && !in_array($method, $notify_methods)) {
2172 $notify_methods[] = $method;
2173 }
2174
2175 $select_method = new html_select(array(
2176 'name' => "_action_notifymethod[$id]",
2177 'id' => "_action_notifymethod$id",
2178 'class' => $this->error_class($id, 'action', 'method', 'action_notifymethod'),
2179 ));
2180 foreach ($notify_methods as $m_n) {
2181 $select_method->add(rcube::Q($this->rc->text_exists('managesieve.notifymethod'.$m_n) ? $this->plugin->gettext('managesieve.notifymethod'.$m_n) : $m_n), $m_n);
2182 }
2183
2184 $select_importance = new html_select(array(
2185 'name' => "_action_notifyimportance[$id]",
2186 'id' => "_action_notifyimportance$id",
2187 'class' => $this->error_class($id, 'action', 'importance', 'action_notifyimportance')
2188 ));
2189 foreach ($importance_options as $io_v => $io_n) {
2190 $select_importance->add(rcube::Q($this->plugin->gettext($io_n)), $io_v);
2191 }
2192
2193 // @TODO: nice UI for mailto: (other methods too) URI parameters
2194 $out .= '<div id="action_notify' .$id.'" style="display:' .($action['type'] == 'notify' ? 'inline' : 'none') .'">';
2195 $out .= '<span class="label">' .rcube::Q($this->plugin->gettext('notifytarget')) . '</span><br>';
2196 $out .= $select_method->show($method);
2197 $out .= html::tag('input', array(
2198 'type' => 'text',
2199 'name' => '_action_notifytarget[' . $id . ']',
2200 'id' => 'action_notifytarget' . $id,
2201 'value' => $target,
2202 'size' => 25,
2203 'class' => $this->error_class($id, 'action', 'target', 'action_notifytarget'),
2204 ));
2205 $out .= '<br><span class="label">'. rcube::Q($this->plugin->gettext('notifymessage')) .'</span><br>';
2206 $out .= html::tag('textarea', array(
2207 'name' => '_action_notifymessage[' . $id . ']',
2208 'id' => 'action_notifymessage' . $id,
2209 'rows' => 3,
2210 'cols' => 35,
2211 'class' => $this->error_class($id, 'action', 'message', 'action_notifymessage'),
2212 ), rcube::Q($action['message'], 'strict', false));
2213 if (in_array('enotify', $this->exts)) {
2214 $out .= '<br><span class="label">' .rcube::Q($this->plugin->gettext('notifyfrom')) . '</span><br>';
2215 $out .= html::tag('input', array(
2216 'type' => 'text',
2217 'name' => '_action_notifyfrom[' . $id . ']',
2218 'id' => 'action_notifyfrom' . $id,
2219 'value' => $action['from'],
2220 'size' => 35,
2221 'class' => $this->error_class($id, 'action', 'from', 'action_notifyfrom'),
2222 ));
2223 }
2224 $out .= '<br><span class="label">' . rcube::Q($this->plugin->gettext('notifyimportance')) . '</span><br>';
2225 $out .= $select_importance->show($action['importance'] ? (int) $action['importance'] : 2);
2226 $out .= '<div id="action_notifyoption_div' . $id . '">'
2227 .'<span class="label">' . rcube::Q($this->plugin->gettext('notifyoptions')) . '</span><br>'
2228 .$this->list_input($id, 'action_notifyoption', (array)$action['options'], true,
2229 $this->error_class($id, 'action', 'options', 'action_notifyoption'), 30) . '</div>';
2230 $out .= '</div>';
2231
2232 // mailbox select
2233 if ($action['type'] == 'fileinto') {
2234 $mailbox = $this->mod_mailbox($action['target'], 'out');
2235 // make sure non-existing (or unsubscribed) mailbox is listed (#1489956)
2236 $additional = array($mailbox);
2237 }
2238 else {
2239 $mailbox = '';
2240 }
2241
2242 $select = $this->rc->folder_selector(array(
2243 'realnames' => false,
2244 'maxlength' => 100,
2245 'id' => 'action_mailbox' . $id,
2246 'name' => "_action_mailbox[$id]",
2247 'style' => 'display:'.(empty($action['type']) || $action['type'] == 'fileinto' ? 'inline' : 'none'),
2248 'additional' => $additional,
2249 ));
2250 $out .= $select->show($mailbox);
2251 $out .= '</td>';
2252
2253 // add/del buttons
2254 $out .= '<td class="rowbuttons">';
2255 $out .= '<a href="#" id="actionadd' . $id .'" title="'. rcube::Q($this->plugin->gettext('add')). '"
2256 onclick="rcmail.managesieve_actionadd(' . $id .')" class="button add"></a>';
2257 $out .= '<a href="#" id="actiondel' . $id .'" title="'. rcube::Q($this->plugin->gettext('del')). '"
2258 onclick="rcmail.managesieve_actiondel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
2259 $out .= '</td>';
2260
2261 $out .= '</tr></table>';
2262
2263 $out .= $div ? "</div>\n" : '';
2264
2265 return $out;
2266 }
2267
2268 protected function genid()
2269 {
2270 return preg_replace('/[^0-9]/', '', microtime(true));
2271 }
2272
2273 protected function strip_value($str, $allow_html = false, $trim = true)
2274 {
2275 if (is_array($str)) {
2276 foreach ($str as $idx => $val) {
2277 $val = $this->strip_value($val, $allow_html, $trim);
2278
2279 if ($val === '') {
2280 unset($str[$idx]);
2281 }
2282 }
2283
2284 return $str;
2285 }
2286
2287 if (!$allow_html) {
2288 $str = strip_tags($str);
2289 }
2290
2291 return $trim ? trim($str) : $str;
2292 }
2293
2294 protected function error_class($id, $type, $target, $elem_prefix='')
2295 {
2296 // TODO: tooltips
2297 if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) ||
2298 ($type == 'action' && ($str = $this->errors['actions'][$id][$target]))
2299 ) {
2300 $this->add_tip($elem_prefix.$id, $str, true);
2301 return 'error';
2302 }
2303
2304 return '';
2305 }
2306
2307 protected function add_tip($id, $str, $error=false)
2308 {
2309 if ($error) {
2310 $str = html::span('sieve error', $str);
2311 }
2312
2313 $this->tips[] = array($id, $str);
2314 }
2315
2316 protected function print_tips()
2317 {
2318 if (empty($this->tips)) {
2319 return;
2320 }
2321
2322 $script = rcmail_output::JS_OBJECT_NAME.'.managesieve_tip_register('.json_encode($this->tips).');';
2323 $this->rc->output->add_script($script, 'foot');
2324 }
2325
2326 protected function list_input($id, $name, $value, $enabled, $class, $size=null)
2327 {
2328 $value = (array) $value;
2329 $value = array_map(array('rcube', 'Q'), $value);
2330 $value = implode("\n", $value);
2331
2332 return html::tag('textarea', array(
2333 'data-type' => 'list',
2334 'data-size' => $size,
2335 'name' => '_' . $name . '['. $id .']',
2336 'id' => $name.$id,
2337 'disabled' => !$enabled,
2338 'class' => $class,
2339 'style' => 'display:none',
2340 ), $value);
2341 }
2342
2343 /**
2344 * Validate input for date part elements
2345 */
2346 protected function validate_date_part($type, $value)
2347 {
2348 // we do simple validation of date/part format
2349 switch ($type) {
2350 case 'date': // yyyy-mm-dd
2351 return preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value);
2352 case 'iso8601':
2353 return preg_match('/^[0-9: .,ZWT+-]+$/', $value);
2354 case 'std11':
2355 return preg_match('/^((Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s+)?[0-9]{1,2}\s+'
2356 . '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+[0-9]{2,4}\s+'
2357 . '[0-9]{2}:[0-9]{2}(:[0-9]{2})?\s+([+-]*[0-9]{4}|[A-Z]{1,3})$', $value);
2358 case 'julian':
2359 return preg_match('/^[0-9]+$/', $value);
2360 case 'time': // hh:mm:ss
2361 return preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $value);
2362 case 'year':
2363 return preg_match('/^[0-9]{4}$/', $value);
2364 case 'month':
2365 return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 13;
2366 case 'day':
2367 return preg_match('/^[0-9]{2}$/', $value) && $value > 0 && $value < 32;
2368 case 'hour':
2369 return preg_match('/^[0-9]{2}$/', $value) && $value < 24;
2370 case 'minute':
2371 return preg_match('/^[0-9]{2}$/', $value) && $value < 60;
2372 case 'second':
2373 // According to RFC5260, seconds can be from 00 to 60
2374 return preg_match('/^[0-9]{2}$/', $value) && $value < 61;
2375 case 'weekday':
2376 return preg_match('/^[0-9]$/', $value) && $value < 7;
2377 case 'zone':
2378 return preg_match('/^[+-][0-9]{4}$/', $value);
2379 }
2380 }
2381
2382 /**
2383 * Converts mailbox name from/to UTF7-IMAP from/to internal Sieve encoding
2384 * with delimiter replacement.
2385 *
2386 * @param string $mailbox Mailbox name
2387 * @param string $mode Conversion direction ('in'|'out')
2388 *
2389 * @return string Mailbox name
2390 */
2391 protected function mod_mailbox($mailbox, $mode = 'out')
2392 {
2393 $delimiter = $_SESSION['imap_delimiter'];
2394 $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter');
2395 $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP');
2396
2397 if ($mode == 'out') {
2398 $mailbox = rcube_charset::convert($mailbox, $mbox_encoding, 'UTF7-IMAP');
2399 if ($replace_delimiter && $replace_delimiter != $delimiter)
2400 $mailbox = str_replace($replace_delimiter, $delimiter, $mailbox);
2401 }
2402 else {
2403 $mailbox = rcube_charset::convert($mailbox, 'UTF7-IMAP', $mbox_encoding);
2404 if ($replace_delimiter && $replace_delimiter != $delimiter)
2405 $mailbox = str_replace($delimiter, $replace_delimiter, $mailbox);
2406 }
2407
2408 return $mailbox;
2409 }
2410
2411 /**
2412 * List sieve scripts
2413 *
2414 * @return array Scripts list
2415 */
2416 public function list_scripts()
2417 {
2418 if ($this->list !== null) {
2419 return $this->list;
2420 }
2421
2422 $this->list = $this->sieve->get_scripts();
2423
2424 // Handle active script(s) and list of scripts according to Kolab's KEP:14
2425 if ($this->rc->config->get('managesieve_kolab_master')) {
2426 // Skip protected names
2427 foreach ((array)$this->list as $idx => $name) {
2428 $_name = strtoupper($name);
2429 if ($_name == 'MASTER')
2430 $master_script = $name;
2431 else if ($_name == 'MANAGEMENT')
2432 $management_script = $name;
2433 else if($_name == 'USER')
2434 $user_script = $name;
2435 else
2436 continue;
2437
2438 unset($this->list[$idx]);
2439 }
2440
2441 // get active script(s), read USER script
2442 if ($user_script) {
2443 $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
2444 $filename_regex = '/'.preg_quote($extension, '/').'$/';
2445 $_SESSION['managesieve_user_script'] = $user_script;
2446
2447 $this->sieve->load($user_script);
2448
2449 foreach ($this->sieve->script->as_array() as $rules) {
2450 foreach ($rules['actions'] as $action) {
2451 if ($action['type'] == 'include' && empty($action['global'])) {
2452 $name = preg_replace($filename_regex, '', $action['target']);
2453 // make sure the script exist
2454 if (in_array($name, $this->list)) {
2455 $this->active[] = $name;
2456 }
2457 }
2458 }
2459 }
2460 }
2461 // create USER script if it doesn't exist
2462 else {
2463 $content = "# USER Management Script\n"
2464 ."#\n"
2465 ."# This script includes the various active sieve scripts\n"
2466 ."# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n"
2467 ."#\n"
2468 ."# For more information, see http://wiki.kolab.org/KEP:14#USER\n"
2469 ."#\n";
2470 if ($this->sieve->save_script('USER', $content)) {
2471 $_SESSION['managesieve_user_script'] = 'USER';
2472 if (empty($this->master_file))
2473 $this->sieve->activate('USER');
2474 }
2475 }
2476 }
2477 else if (!empty($this->list)) {
2478 // Get active script name
2479 if ($active = $this->sieve->get_active()) {
2480 $this->active = array($active);
2481 }
2482
2483 // Hide scripts from config
2484 $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
2485 if (!empty($exceptions)) {
2486 $this->list = array_diff($this->list, (array)$exceptions);
2487 }
2488 }
2489
2490 // reindex
2491 if (!empty($this->list)) {
2492 $this->list = array_values($this->list);
2493 }
2494
2495 return $this->list;
2496 }
2497
2498 /**
2499 * Removes sieve script
2500 *
2501 * @param string $name Script name
2502 *
2503 * @return bool True on success, False on failure
2504 */
2505 public function remove_script($name)
2506 {
2507 $result = $this->sieve->remove($name);
2508
2509 // Kolab's KEP:14
2510 if ($result && $this->rc->config->get('managesieve_kolab_master')) {
2511 $this->deactivate_script($name);
2512 }
2513
2514 return $result;
2515 }
2516
2517 /**
2518 * Activates sieve script
2519 *
2520 * @param string $name Script name
2521 *
2522 * @return bool True on success, False on failure
2523 */
2524 public function activate_script($name)
2525 {
2526 // Kolab's KEP:14
2527 if ($this->rc->config->get('managesieve_kolab_master')) {
2528 $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
2529 $user_script = $_SESSION['managesieve_user_script'];
2530
2531 // if the script is not active...
2532 if ($user_script && array_search($name, $this->active) === false) {
2533 // ...rewrite USER file adding appropriate include command
2534 if ($this->sieve->load($user_script)) {
2535 $script = $this->sieve->script->as_array();
2536 $list = array();
2537 $regexp = '/' . preg_quote($extension, '/') . '$/';
2538
2539 // Create new include entry
2540 $rule = array(
2541 'actions' => array(
2542 0 => array(
2543 'target' => $name.$extension,
2544 'type' => 'include',
2545 'personal' => true,
2546 )));
2547
2548 // get all active scripts for sorting
2549 foreach ($script as $rid => $rules) {
2550 foreach ($rules['actions'] as $action) {
2551 if ($action['type'] == 'include' && empty($action['global'])) {
2552 $target = $extension ? preg_replace($regexp, '', $action['target']) : $action['target'];
2553 $list[] = $target;
2554 }
2555 }
2556 }
2557 $list[] = $name;
2558
2559 // Sort and find current script position
2560 asort($list, SORT_LOCALE_STRING);
2561 $list = array_values($list);
2562 $index = array_search($name, $list);
2563
2564 // add rule at the end of the script
2565 if ($index === false || $index == count($list)-1) {
2566 $this->sieve->script->add_rule($rule);
2567 }
2568 // add rule at index position
2569 else {
2570 $script2 = array();
2571 foreach ($script as $rid => $rules) {
2572 if ($rid == $index) {
2573 $script2[] = $rule;
2574 }
2575 $script2[] = $rules;
2576 }
2577 $this->sieve->script->content = $script2;
2578 }
2579
2580 $result = $this->sieve->save();
2581 if ($result) {
2582 $this->active[] = $name;
2583 }
2584 }
2585 }
2586 }
2587 else {
2588 $result = $this->sieve->activate($name);
2589 if ($result)
2590 $this->active = array($name);
2591 }
2592
2593 return $result;
2594 }
2595
2596 /**
2597 * Deactivates sieve script
2598 *
2599 * @param string $name Script name
2600 *
2601 * @return bool True on success, False on failure
2602 */
2603 public function deactivate_script($name)
2604 {
2605 // Kolab's KEP:14
2606 if ($this->rc->config->get('managesieve_kolab_master')) {
2607 $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
2608 $user_script = $_SESSION['managesieve_user_script'];
2609
2610 // if the script is active...
2611 if ($user_script && ($key = array_search($name, $this->active)) !== false) {
2612 // ...rewrite USER file removing appropriate include command
2613 if ($this->sieve->load($user_script)) {
2614 $script = $this->sieve->script->as_array();
2615 $name = $name.$extension;
2616
2617 foreach ($script as $rid => $rules) {
2618 foreach ($rules['actions'] as $action) {
2619 if ($action['type'] == 'include' && empty($action['global'])
2620 && $action['target'] == $name
2621 ) {
2622 break 2;
2623 }
2624 }
2625 }
2626
2627 // Entry found
2628 if ($rid < count($script)) {
2629 $this->sieve->script->delete_rule($rid);
2630 $result = $this->sieve->save();
2631 if ($result) {
2632 unset($this->active[$key]);
2633 }
2634 }
2635 }
2636 }
2637 }
2638 else {
2639 $result = $this->sieve->deactivate();
2640 if ($result)
2641 $this->active = array();
2642 }
2643
2644 return $result;
2645 }
2646
2647 /**
2648 * Saves current script (adding some variables)
2649 */
2650 public function save_script($name = null)
2651 {
2652 // Kolab's KEP:14
2653 if ($this->rc->config->get('managesieve_kolab_master')) {
2654 $this->sieve->script->set_var('EDITOR', self::PROGNAME);
2655 $this->sieve->script->set_var('EDITOR_VERSION', self::VERSION);
2656 }
2657
2658 return $this->sieve->save($name);
2659 }
2660
2661 /**
2662 * Returns list of rules from the current script
2663 *
2664 * @return array List of rules
2665 */
2666 public function list_rules()
2667 {
2668 $result = array();
2669 $i = 1;
2670
2671 foreach ($this->script as $idx => $filter) {
2672 if (empty($filter['actions'])) {
2673 continue;
2674 }
2675 $fname = $filter['name'] ?: "#$i";
2676 $result[] = array(
2677 'id' => $idx,
2678 'name' => $fname,
2679 'class' => $filter['disabled'] ? 'disabled' : '',
2680 );
2681 $i++;
2682 }
2683
2684 return $result;
2685 }
2686
2687 /**
2688 * Initializes internal script data
2689 */
2690 protected function init_script()
2691 {
2692 if (!$this->sieve->script) {
2693 return;
2694 }
2695
2696 $this->script = $this->sieve->script->as_array();
2697
2698 $headers = array();
2699 $exceptions = array('date', 'currentdate', 'size', 'body');
2700
2701 // find common headers used in script, will be added to the list
2702 // of available (predefined) headers (#1489271)
2703 foreach ($this->script as $rule) {
2704 foreach ((array) $rule['tests'] as $test) {
2705 if ($test['test'] == 'header') {
2706 foreach ((array) $test['arg1'] as $header) {
2707 $lc_header = strtolower($header);
2708
2709 // skip special names to not confuse UI
2710 if (in_array($lc_header, $exceptions)) {
2711 continue;
2712 }
2713
2714 if (!isset($this->headers[$lc_header]) && !isset($headers[$lc_header])) {
2715 $headers[$lc_header] = $header;
2716 }
2717 }
2718 }
2719 }
2720 }
2721
2722 ksort($headers);
2723
2724 $this->headers += $headers;
2725 }
2726
2727 /**
2728 * Get all e-mail addresses of the user
2729 */
2730 protected function user_emails()
2731 {
2732 $addresses = $this->rc->user->list_emails();
2733
2734 foreach ($addresses as $idx => $email) {
2735 $addresses[$idx] = $email['email'];
2736 }
2737
2738 $addresses = array_unique($addresses);
2739 sort($addresses);
2740
2741 return $addresses;
2742 }
2743 }