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