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