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