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