alecpl
2012-04-22 390959bb323679f7611ee1585d8e1f55007c7773
commit | author | age
48e9c1 1 <?php
T 2
3 /**
4  * Managesieve (Sieve Filters)
5  *
6  * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
7  * It's clickable interface which operates on text scripts and communicates
8  * with server using managesieve protocol. Adds Filters tab in Settings.
9  *
10  * @version 5.0
11  * @author Aleksander Machniak <alec@alec.pl>
12  *
13  * Configuration (see config.inc.php.dist)
14  *
15  * Copyright (C) 2008-2011, The Roundcube Dev Team
16  * Copyright (C) 2011, Kolab Systems AG
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, write to the Free Software Foundation, Inc.,
29  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30  *
31  * $Id$
32  */
33
34 class managesieve extends rcube_plugin
35 {
36     public $task = 'mail|settings';
37
38     private $rc;
39     private $sieve;
40     private $errors;
41     private $form;
42     private $tips = array();
43     private $script = array();
44     private $exts = array();
45     private $list;
46     private $active = array();
47     private $headers = array(
48         'subject' => 'Subject',
49         'from'    => 'From',
50         'to'      => 'To',
51     );
52     private $addr_headers = array(
53         // Required
54         "from", "to", "cc", "bcc", "sender", "resent-from", "resent-to",
55         // Additional (RFC 822 / RFC 2822)
56         "reply-to", "resent-reply-to", "resent-sender", "resent-cc", "resent-bcc",
57         // Non-standard (RFC 2076, draft-palme-mailext-headers-08.txt)
58         "for-approval", "for-handling", "for-comment", "apparently-to", "errors-to",
59         "delivered-to", "return-receipt-to", "x-admin", "read-receipt-to",
60         "x-confirm-reading-to", "return-receipt-requested",
61         "registered-mail-reply-requested-by", "mail-followup-to", "mail-reply-to",
62         "abuse-reports-to", "x-complaints-to", "x-report-abuse-to",
63         // Undocumented
64         "x-beenthere",
65     );
66
67     const VERSION = '5.0';
68     const PROGNAME = 'Roundcube (Managesieve)';
69
70
71     function init()
72     {
73         $this->rc = rcmail::get_instance();
74
75         // register actions
76         $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
77         $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
78
79         if ($this->rc->task == 'settings') {
80             $this->init_ui();
81         }
82         else if ($this->rc->task == 'mail') {
83             // register message hook
84             $this->add_hook('message_headers_output', array($this, 'mail_headers'));
85
86             // inject Create Filter popup stuff
87             if (empty($this->rc->action) || $this->rc->action == 'show') {
88                 $this->mail_task_handler();
89             }
90         }
91     }
92
93     /**
94      * Initializes plugin's UI (localization, js script)
95      */
96     private function init_ui()
97     {
98         if ($this->ui_initialized)
99             return;
100
101         // load localization
102         $this->add_texts('localization/', array('filters','managefilters'));
103         $this->include_script('managesieve.js');
104
105         $this->ui_initialized = true;
106     }
107
108     /**
109      * Add UI elements to the 'mailbox view' and 'show message' UI.
110      */
111     function mail_task_handler()
112     {
113         // use jQuery for popup window
114         $this->require_plugin('jqueryui'); 
115
116         // include js script and localization
117         $this->init_ui();
118
119         // include styles
120         $skin = $this->rc->config->get('skin');
121         if (!file_exists($this->home."/skins/$skin/managesieve_mail.css"))
122             $skin = 'default';
123         $this->include_stylesheet("skins/$skin/managesieve_mail.css");
124
125         // add 'Create filter' item to message menu
126         $this->api->add_content(html::tag('li', null, 
127             $this->api->output->button(array(
128                 'command'  => 'managesieve-create',
129                 'label'    => 'managesieve.filtercreate',
130                 'type'     => 'link',
8d8f7a 131                 'classact' => 'icon filterlink active',
A 132                 'class'    => 'icon filterlink',
133                 'innerclass' => 'icon filterlink',
48e9c1 134             ))), 'messagemenu');
T 135
136         // register some labels/messages
137         $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
138             'managesieve.nodata', 'managesieve.nextstep', 'save');
139
140         $this->rc->session->remove('managesieve_current');
141     }
142
143     /**
144      * Get message headers for popup window
145      */
146     function mail_headers($args)
147     {
148         $headers = $args['headers'];
149         $ret     = array();
150
151         if ($headers->subject)
152             $ret[] = array('Subject', rcube_mime::decode_header($headers->subject));
153
154         // @TODO: List-Id, others?
155         foreach (array('From', 'To') as $h) {
156             $hl = strtolower($h);
157             if ($headers->$hl) {
158                 $list = rcube_mime::decode_address_list($headers->$hl);
159                 foreach ($list as $item) {
160                     if ($item['mailto']) {
161                         $ret[] = array($h, $item['mailto']);
162                     }
163                 }
164             }
165         }
166
167         if ($this->rc->action == 'preview')
168             $this->rc->output->command('parent.set_env', array('sieve_headers' => $ret));
169         else
170             $this->rc->output->set_env('sieve_headers', $ret);
171
172
173         return $args;
174     }
175
176     /**
177      * Loads configuration, initializes plugin (including sieve connection)
178      */
179     function managesieve_start()
180     {
181         $this->load_config();
182
183         // register UI objects
184         $this->rc->output->add_handlers(array(
185             'filterslist'    => array($this, 'filters_list'),
186             'filtersetslist' => array($this, 'filtersets_list'),
187             'filterframe'    => array($this, 'filter_frame'),
188             'filterform'     => array($this, 'filter_form'),
189             'filtersetform'  => array($this, 'filterset_form'),
190         ));
191
192         // Add include path for internal classes
193         $include_path = $this->home . '/lib' . PATH_SEPARATOR;
194         $include_path .= ini_get('include_path');
195         set_include_path($include_path);
196
197         $host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost'));
198         $port = $this->rc->config->get('managesieve_port', 2000);
199
200         $host = rcube_idn_to_ascii($host);
201
202         $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array(
203             'user'      => $_SESSION['username'],
204             'password'  => $this->rc->decrypt($_SESSION['password']),
205             'host'      => $host,
206             'port'      => $port,
207             'auth_type' => $this->rc->config->get('managesieve_auth_type'),
208             'usetls'    => $this->rc->config->get('managesieve_usetls', false),
209             'disabled'  => $this->rc->config->get('managesieve_disabled_extensions'),
210             'debug'     => $this->rc->config->get('managesieve_debug', false),
211             'auth_cid'  => $this->rc->config->get('managesieve_auth_cid'),
212             'auth_pw'   => $this->rc->config->get('managesieve_auth_pw'),
213         ));
214
215         // try to connect to managesieve server and to fetch the script
216         $this->sieve = new rcube_sieve(
217             $plugin['user'],
218             $plugin['password'],
219             $plugin['host'],
220             $plugin['port'],
221             $plugin['auth_type'],
222             $plugin['usetls'],
223             $plugin['disabled'],
224             $plugin['debug'],
225             $plugin['auth_cid'],
226             $plugin['auth_pw']
227         );
228
229         if (!($error = $this->sieve->error())) {
230             // Get list of scripts
231             $list = $this->list_scripts();
232
233             if (!empty($_GET['_set']) || !empty($_POST['_set'])) {
234                 $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
235             }
236             else if (!empty($_SESSION['managesieve_current'])) {
237                 $script_name = $_SESSION['managesieve_current'];
238             }
239             else {
240                 // get (first) active script
241                 if (!empty($this->active[0])) {
242                     $script_name = $this->active[0];
243                 }
244                 else if ($list) {
245                     $script_name = $list[0];
246                 }
247                 // create a new (initial) script
248                 else {
249                     // if script not exists build default script contents
250                     $script_file = $this->rc->config->get('managesieve_default');
251                     $script_name = $this->rc->config->get('managesieve_script_name');
252
253                     if (empty($script_name))
254                         $script_name = 'roundcube';
255
256                     if ($script_file && is_readable($script_file))
257                         $content = file_get_contents($script_file);
258
259                     // add script and set it active
260                     if ($this->sieve->save_script($script_name, $content)) {
261                         $this->activate_script($script_name);
262                         $this->list[] = $script_name;
263                     }
264                 }
265             }
266
267             if ($script_name) {
268                 $this->sieve->load($script_name);
269             }
270
271             $error = $this->sieve->error();
272         }
273
274         // finally set script objects
275         if ($error) {
276             switch ($error) {
277                 case SIEVE_ERROR_CONNECTION:
278                 case SIEVE_ERROR_LOGIN:
279                     $this->rc->output->show_message('managesieve.filterconnerror', 'error');
280                     break;
281                 default:
282                     $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
283                     break;
284             }
285
286             raise_error(array('code' => 403, 'type' => 'php',
287                 'file' => __FILE__, 'line' => __LINE__,
288                 'message' => "Unable to connect to managesieve on $host:$port"), true, false);
289
290             // to disable 'Add filter' button set env variable
291             $this->rc->output->set_env('filterconnerror', true);
292             $this->script = array();
293         }
294         else {
295             $this->exts = $this->sieve->get_extensions();
296             $this->script = $this->sieve->script->as_array();
297             $this->rc->output->set_env('currentset', $this->sieve->current);
298             $_SESSION['managesieve_current'] = $this->sieve->current;
299         }
300
301         return $error;
302     }
303
304     function managesieve_actions()
305     {
306         $this->init_ui();
307
308         $error = $this->managesieve_start();
309
310         // Handle user requests
311         if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) {
312             $fid = (int) get_input_value('_fid', RCUBE_INPUT_POST);
313
314             if ($action == 'delete' && !$error) {
315                 if (isset($this->script[$fid])) {
316                     if ($this->sieve->script->delete_rule($fid))
317                         $result = $this->save_script();
318
319                     if ($result === true) {
320                         $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
321                         $this->rc->output->command('managesieve_updatelist', 'del', array('id' => $fid));
322                     } else {
323                         $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
324                     }
325                 }
326             }
327             else if ($action == 'move' && !$error) {
328                 if (isset($this->script[$fid])) {
329                     $to   = (int) get_input_value('_to', RCUBE_INPUT_POST);
330                     $rule = $this->script[$fid];
331
332                     // remove rule
333                     unset($this->script[$fid]);
334                     $this->script = array_values($this->script);
335
336                     // add at target position
337                     if ($to >= count($this->script)) {
338                         $this->script[] = $rule;
339                     }
340                     else {
341                         $script = array();
342                         foreach ($this->script as $idx => $r) {
343                             if ($idx == $to)
344                                 $script[] = $rule;
345                             $script[] = $r;
346                         }
347                         $this->script = $script;
348                     }
349
350                     $this->sieve->script->content = $this->script;
351                     $result = $this->save_script();
352
353                     if ($result === true) {
354                         $result = $this->list_rules();
355
356                         $this->rc->output->show_message('managesieve.moved', 'confirmation');
357                         $this->rc->output->command('managesieve_updatelist', 'list',
358                             array('list' => $result, 'clear' => true, 'set' => $to));
359                     } else {
360                         $this->rc->output->show_message('managesieve.moveerror', 'error');
361                     }
362                 }
363             }
364             else if ($action == 'act' && !$error) {
365                 if (isset($this->script[$fid])) {
366                     $rule     = $this->script[$fid];
367                     $disabled = $rule['disabled'] ? true : false;
368                     $rule['disabled'] = !$disabled;
369                     $result = $this->sieve->script->update_rule($fid, $rule);
370
371                     if ($result !== false)
372                         $result = $this->save_script();
373
374                     if ($result === true) {
375                         if ($rule['disabled'])
376                             $this->rc->output->show_message('managesieve.deactivated', 'confirmation');
377                         else
378                             $this->rc->output->show_message('managesieve.activated', 'confirmation');
379                         $this->rc->output->command('managesieve_updatelist', 'update',
380                             array('id' => $fid, 'disabled' => $rule['disabled']));
381                     } else {
382                         if ($rule['disabled'])
383                             $this->rc->output->show_message('managesieve.deactivateerror', 'error');
384                         else
385                             $this->rc->output->show_message('managesieve.activateerror', 'error');
386                     }
387                 }
388             }
389             else if ($action == 'setact' && !$error) {
390                 $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
391                 $result = $this->activate_script($script_name);
392                 $kep14  = $this->rc->config->get('managesieve_kolab_master');
393
394                 if ($result === true) {
395                     $this->rc->output->set_env('active_sets', $this->active);
396                     $this->rc->output->show_message('managesieve.setactivated', 'confirmation');
397                     $this->rc->output->command('managesieve_updatelist', 'setact',
398                         array('name' => $script_name, 'active' => true, 'all' => !$kep14));
399                 } else {
400                     $this->rc->output->show_message('managesieve.setactivateerror', 'error');
401                 }
402             }
403             else if ($action == 'deact' && !$error) {
404                 $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
405                 $result = $this->deactivate_script($script_name);
406
407                 if ($result === true) {
408                     $this->rc->output->set_env('active_sets', $this->active);
409                     $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation');
410                     $this->rc->output->command('managesieve_updatelist', 'setact',
411                         array('name' => $script_name, 'active' => false));
412                 } else {
413                     $this->rc->output->show_message('managesieve.setdeactivateerror', 'error');
414                 }
415             }
416             else if ($action == 'setdel' && !$error) {
417                 $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
418                 $result = $this->remove_script($script_name);
419
420                 if ($result === true) {
421                     $this->rc->output->show_message('managesieve.setdeleted', 'confirmation');
422                     $this->rc->output->command('managesieve_updatelist', 'setdel',
423                         array('name' => $script_name));
424                     $this->rc->session->remove('managesieve_current');
425                 } else {
426                     $this->rc->output->show_message('managesieve.setdeleteerror', 'error');
427                 }
428             }
429             else if ($action == 'setget') {
430                 $script_name = get_input_value('_set', RCUBE_INPUT_GPC, true);
431                 $script = $this->sieve->get_script($script_name);
432
433                 if (PEAR::isError($script))
434                     exit;
435
436                 $browser = new rcube_browser;
437
438                 // send download headers
439                 header("Content-Type: application/octet-stream");
440                 header("Content-Length: ".strlen($script));
441
442                 if ($browser->ie)
443                     header("Content-Type: application/force-download");
444                 if ($browser->ie && $browser->ver < 7)
445                     $filename = rawurlencode(abbreviate_string($script_name, 55));
446                 else if ($browser->ie)
447                     $filename = rawurlencode($script_name);
448                 else
449                     $filename = addcslashes($script_name, '\\"');
450
451                 header("Content-Disposition: attachment; filename=\"$filename.txt\"");
452                 echo $script;
453                 exit;
454             }
455             else if ($action == 'list') {
456                 $result = $this->list_rules();
457
458                 $this->rc->output->command('managesieve_updatelist', 'list', array('list' => $result));
459             }
460             else if ($action == 'ruleadd') {
461                 $rid = get_input_value('_rid', RCUBE_INPUT_GPC);
462                 $id = $this->genid();
463                 $content = $this->rule_div($fid, $id, false);
464
465                 $this->rc->output->command('managesieve_rulefill', $content, $id, $rid);
466             }
467             else if ($action == 'actionadd') {
468                 $aid = get_input_value('_aid', RCUBE_INPUT_GPC);
469                 $id = $this->genid();
470                 $content = $this->action_div($fid, $id, false);
471
472                 $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
473             }
474
475             $this->rc->output->send();
476         }
477         else if ($this->rc->task == 'mail') {
478             // Initialize the form
479             $rules = get_input_value('r', RCUBE_INPUT_GET);
480             if (!empty($rules)) {
481                 $i = 0;
482                 foreach ($rules as $rule) {
483                     list($header, $value) = explode(':', $rule, 2);
484                     $tests[$i] = array(
485                         'type' => 'contains',
486                         'test' => 'header',
487                         'arg1' => $header,
488                         'arg2' => $value,
489                     );
490                     $i++;
491                 }
492
493                 $this->form = array(
494                     'join'  => count($tests) > 1 ? 'allof' : 'anyof',
495                     'name'  => '',
496                     'tests' => $tests,
497                     'actions' => array(
498                         0 => array('type' => 'fileinto'),
499                         1 => array('type' => 'stop'),
500                     ),
501                 );
502             }
503         }
504
505         $this->managesieve_send();
506     }
507
508     function managesieve_save()
509     {
510         // load localization
511         $this->add_texts('localization/', array('filters','managefilters'));
512
513         // include main js script
514         if ($this->api->output->type == 'html') {
515             $this->include_script('managesieve.js');
516         }
517
518         // Init plugin and handle managesieve connection
519         $error = $this->managesieve_start();
520
521         // filters set add action
522         if (!empty($_POST['_newset'])) {
523
524             $name       = get_input_value('_name', RCUBE_INPUT_POST, true);
525             $copy       = get_input_value('_copy', RCUBE_INPUT_POST, true);
526             $from       = get_input_value('_from', RCUBE_INPUT_POST);
527             $exceptions = $this->rc->config->get('managesieve_filename_exceptions');
528             $kolab      = $this->rc->config->get('managesieve_kolab_master');
529             $name_uc    = mb_strtolower($name);
530             $list       = $this->list_scripts();
531
532             if (!$name) {
533                 $this->errors['name'] = $this->gettext('cannotbeempty');
534             }
535             else if (mb_strlen($name) > 128) {
536                 $this->errors['name'] = $this->gettext('nametoolong');
537             }
538             else if (!empty($exceptions) && in_array($name, (array)$exceptions)) {
539                 $this->errors['name'] = $this->gettext('namereserved');
540             }
541             else if (!empty($kolab) && in_array($name_uc, array('MASTER', 'USER', 'MANAGEMENT'))) {
542                 $this->errors['name'] = $this->gettext('namereserved');
543             }
544             else if (in_array($name, $list)) {
545                 $this->errors['name'] = $this->gettext('setexist');
546             }
547             else if ($from == 'file') {
548                 // from file
549                 if (is_uploaded_file($_FILES['_file']['tmp_name'])) {
550                     $file = file_get_contents($_FILES['_file']['tmp_name']);
551                     $file = preg_replace('/\r/', '', $file);
552                     // for security don't save script directly
553                     // check syntax before, like this...
554                     $this->sieve->load_script($file);
555                     if (!$this->save_script($name)) {
556                         $this->errors['file'] = $this->gettext('setcreateerror');
557                     }
558                 }
559                 else {  // upload failed
560                     $err = $_FILES['_file']['error'];
561
562                     if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
563                         $msg = rcube_label(array('name' => 'filesizeerror',
564                             'vars' => array('size' =>
565                                 show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
566                     }
567                     else {
568                         $this->errors['file'] = $this->gettext('fileuploaderror');
569                     }
570                 }
571             }
572             else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) {
573                 $error = 'managesieve.setcreateerror';
574             }
575
576             if (!$error && empty($this->errors)) {
577                 // Find position of the new script on the list
578                 $list[] = $name;
579                 asort($list, SORT_LOCALE_STRING);
580                 $list  = array_values($list);
581                 $index = array_search($name, $list);
582
583                 $this->rc->output->show_message('managesieve.setcreated', 'confirmation');
584                 $this->rc->output->command('parent.managesieve_updatelist', 'setadd',
585                     array('name' => $name, 'index' => $index));
586             } else if ($msg) {
587                 $this->rc->output->command('display_message', $msg, 'error');
588             } else if ($error) {
589                 $this->rc->output->show_message($error, 'error');
590             }
591         }
592         // filter add/edit action
593         else if (isset($_POST['_name'])) {
594             $name = trim(get_input_value('_name', RCUBE_INPUT_POST, true));
595             $fid  = trim(get_input_value('_fid', RCUBE_INPUT_POST));
596             $join = trim(get_input_value('_join', RCUBE_INPUT_POST));
597
598             // and arrays
599             $headers        = get_input_value('_header', RCUBE_INPUT_POST);
600             $cust_headers   = get_input_value('_custom_header', RCUBE_INPUT_POST);
601             $ops            = get_input_value('_rule_op', RCUBE_INPUT_POST);
602             $sizeops        = get_input_value('_rule_size_op', RCUBE_INPUT_POST);
603             $sizeitems      = get_input_value('_rule_size_item', RCUBE_INPUT_POST);
604             $sizetargets    = get_input_value('_rule_size_target', RCUBE_INPUT_POST);
605             $targets        = get_input_value('_rule_target', RCUBE_INPUT_POST, true);
606             $mods           = get_input_value('_rule_mod', RCUBE_INPUT_POST);
607             $mod_types      = get_input_value('_rule_mod_type', RCUBE_INPUT_POST);
608             $body_trans     = get_input_value('_rule_trans', RCUBE_INPUT_POST);
609             $body_types     = get_input_value('_rule_trans_type', RCUBE_INPUT_POST, true);
610             $comparators    = get_input_value('_rule_comp', RCUBE_INPUT_POST);
611             $act_types      = get_input_value('_action_type', RCUBE_INPUT_POST, true);
612             $mailboxes      = get_input_value('_action_mailbox', RCUBE_INPUT_POST, true);
613             $act_targets    = get_input_value('_action_target', RCUBE_INPUT_POST, true);
614             $area_targets   = get_input_value('_action_target_area', RCUBE_INPUT_POST, true);
615             $reasons        = get_input_value('_action_reason', RCUBE_INPUT_POST, true);
616             $addresses      = get_input_value('_action_addresses', RCUBE_INPUT_POST, true);
617             $days           = get_input_value('_action_days', RCUBE_INPUT_POST);
618             $subject        = get_input_value('_action_subject', RCUBE_INPUT_POST, true);
619             $flags          = get_input_value('_action_flags', RCUBE_INPUT_POST);
620
621             // we need a "hack" for radiobuttons
622             foreach ($sizeitems as $item)
623                 $items[] = $item;
624
625             $this->form['disabled'] = $_POST['_disabled'] ? true : false;
626             $this->form['join']     = $join=='allof' ? true : false;
627             $this->form['name']     = $name;
628             $this->form['tests']    = array();
629             $this->form['actions']  = array();
630
631             if ($name == '')
632                 $this->errors['name'] = $this->gettext('cannotbeempty');
633             else {
634                 foreach($this->script as $idx => $rule)
635                     if($rule['name'] == $name && $idx != $fid) {
636                         $this->errors['name'] = $this->gettext('ruleexist');
637                         break;
638                     }
639             }
640
641             $i = 0;
642             // rules
643             if ($join == 'any') {
644                 $this->form['tests'][0]['test'] = 'true';
645             }
646             else {
647                 foreach ($headers as $idx => $header) {
648                     $header     = $this->strip_value($header);
649                     $target     = $this->strip_value($targets[$idx], true);
650                     $operator   = $this->strip_value($ops[$idx]);
651                     $comparator = $this->strip_value($comparators[$idx]);
652
653                     if ($header == 'size') {
654                         $sizeop     = $this->strip_value($sizeops[$idx]);
655                         $sizeitem   = $this->strip_value($items[$idx]);
656                         $sizetarget = $this->strip_value($sizetargets[$idx]);
657
658                         $this->form['tests'][$i]['test'] = 'size';
659                         $this->form['tests'][$i]['type'] = $sizeop;
660                         $this->form['tests'][$i]['arg']  = $sizetarget;
661
662                         if ($sizetarget == '')
663                             $this->errors['tests'][$i]['sizetarget'] = $this->gettext('cannotbeempty');
664                         else if (!preg_match('/^[0-9]+(K|M|G)?$/i', $sizetarget.$sizeitem, $m)) {
665                             $this->errors['tests'][$i]['sizetarget'] = $this->gettext('forbiddenchars');
666                             $this->form['tests'][$i]['item'] = $sizeitem;
667                         }
668                         else
669                             $this->form['tests'][$i]['arg'] .= $m[1];
670                     }
671                     else if ($header == 'body') {
672                         $trans      = $this->strip_value($body_trans[$idx]);
673                         $trans_type = $this->strip_value($body_types[$idx], true);
674
675                         if (preg_match('/^not/', $operator))
676                             $this->form['tests'][$i]['not'] = true;
677                         $type = preg_replace('/^not/', '', $operator);
678
679                         if ($type == 'exists') {
680                             $this->errors['tests'][$i]['op'] = true;
681                         }
682
683                         $this->form['tests'][$i]['test'] = 'body';
684                         $this->form['tests'][$i]['type'] = $type;
685                         $this->form['tests'][$i]['arg']  = $target;
686
687                         if ($target == '' && $type != 'exists')
688                             $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
689                         else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
690                             $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
691
692                         $this->form['tests'][$i]['part'] = $trans;
693                         if ($trans == 'content') {
694                             $this->form['tests'][$i]['content'] = $trans_type;
695                         }
696                     }
697                     else {
698                         $cust_header = $headers = $this->strip_value($cust_headers[$idx]);
699                         $mod      = $this->strip_value($mods[$idx]);
700                         $mod_type = $this->strip_value($mod_types[$idx]);
701
702                         if (preg_match('/^not/', $operator))
703                             $this->form['tests'][$i]['not'] = true;
704                         $type = preg_replace('/^not/', '', $operator);
705
706                         if ($header == '...') {
707                             $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY);
708
709                             if (!count($headers))
710                                 $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty');
711                             else {
712                                 foreach ($headers as $hr)
713                                     if (!preg_match('/^[a-z0-9-]+$/i', $hr))
714                                         $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars');
715                             }
716
717                             if (empty($this->errors['tests'][$i]['header']))
718                                 $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers;
719                         }
720
721                         if ($type == 'exists') {
722                             $this->form['tests'][$i]['test'] = 'exists';
723                             $this->form['tests'][$i]['arg'] = $header == '...' ? $cust_header : $header;
724                         }
725                         else {
726                             $test   = 'header';
727                             $header = $header == '...' ? $cust_header : $header;
728
729                             if ($mod == 'address' || $mod == 'envelope') {
730                                 $found = false;
731                                 if (empty($this->errors['tests'][$i]['header'])) {
732                                     foreach ((array)$header as $hdr) {
733                                         if (!in_array(strtolower(trim($hdr)), $this->addr_headers))
734                                             $found = true;
735                                     }
736                                 }
737                                 if (!$found)
738                                     $test = $mod;
739                             }
740
741                             $this->form['tests'][$i]['type'] = $type;
742                             $this->form['tests'][$i]['test'] = $test;
743                             $this->form['tests'][$i]['arg1'] = $header;
744                             $this->form['tests'][$i]['arg2'] = $target;
745
746                             if ($target == '')
747                                 $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
748                             else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
749                                 $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
750
751                             if ($mod) {
752                                 $this->form['tests'][$i]['part'] = $mod_type;
753                             }
754                         }
755                     }
756
757                     if ($header != 'size' && $comparator) {
758                         if (preg_match('/^(value|count)/', $this->form['tests'][$i]['type']))
759                             $comparator = 'i;ascii-numeric';
760
761                         $this->form['tests'][$i]['comparator'] = $comparator;
762                     }
763
764                     $i++;
765                 }
766             }
767
768             $i = 0;
769             // actions
770             foreach($act_types as $idx => $type) {
771                 $type   = $this->strip_value($type);
772                 $target = $this->strip_value($act_targets[$idx]);
773
774                 switch ($type) {
775
776                 case 'fileinto':
777                 case 'fileinto_copy':
778                     $mailbox = $this->strip_value($mailboxes[$idx]);
779                     $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in');
780                     if ($type == 'fileinto_copy') {
781                         $type = 'fileinto';
782                         $this->form['actions'][$i]['copy'] = true;
783                     }
784                     break;
785
786                 case 'reject':
787                 case 'ereject':
788                     $target = $this->strip_value($area_targets[$idx]);
789                     $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target);
790
791  //                 if ($target == '')
792 //                      $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty');
793                     break;
794
795                 case 'redirect':
796                 case 'redirect_copy':
797                     $this->form['actions'][$i]['target'] = $target;
798
799                     if ($this->form['actions'][$i]['target'] == '')
800                         $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty');
801                     else if (!check_email($this->form['actions'][$i]['target']))
802                         $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning');
803
804                     if ($type == 'redirect_copy') {
805                         $type = 'redirect';
806                         $this->form['actions'][$i]['copy'] = true;
807                     }
808                     break;
809
810                 case 'addflag':
811                 case 'setflag':
812                 case 'removeflag':
813                     $_target = array();
814                     if (empty($flags[$idx])) {
815                         $this->errors['actions'][$i]['target'] = $this->gettext('noflagset');
816                     }
817                     else {
818                         foreach ($flags[$idx] as $flag) {
819                             $_target[] = $this->strip_value($flag);
820                         }
821                     }
822                     $this->form['actions'][$i]['target'] = $_target;
823                     break;
824
825                 case 'vacation':
826                     $reason = $this->strip_value($reasons[$idx]);
827                     $this->form['actions'][$i]['reason']    = str_replace("\r\n", "\n", $reason);
828                     $this->form['actions'][$i]['days']      = $days[$idx];
829                     $this->form['actions'][$i]['subject']   = $subject[$idx];
830                     $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]);
831 // @TODO: vacation :mime, :from, :handle
832
833                     if ($this->form['actions'][$i]['addresses']) {
834                         foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) {
835                             $address = trim($address);
836                             if (!$address)
837                                 unset($this->form['actions'][$i]['addresses'][$aidx]);
838                             else if(!check_email($address)) {
839                                 $this->errors['actions'][$i]['addresses'] = $this->gettext('noemailwarning');
840                                 break;
841                             } else
842                                 $this->form['actions'][$i]['addresses'][$aidx] = $address;
843                         }
844                     }
845
846                     if ($this->form['actions'][$i]['reason'] == '')
847                         $this->errors['actions'][$i]['reason'] = $this->gettext('cannotbeempty');
848                     if ($this->form['actions'][$i]['days'] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i]['days']))
849                         $this->errors['actions'][$i]['days'] = $this->gettext('forbiddenchars');
850                     break;
851                 }
852
853                 $this->form['actions'][$i]['type'] = $type;
854                 $i++;
855             }
856
857             if (!$this->errors && !$error) {
858                 // zapis skryptu
859                 if (!isset($this->script[$fid])) {
860                     $fid = $this->sieve->script->add_rule($this->form);
861                     $new = true;
862                 } else
863                     $fid = $this->sieve->script->update_rule($fid, $this->form);
864
865                 if ($fid !== false)
866                     $save = $this->save_script();
867
868                 if ($save && $fid !== false) {
869                     $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
870                     if ($this->rc->task != 'mail') {
871                         $this->rc->output->command('parent.managesieve_updatelist',
872                             isset($new) ? 'add' : 'update',
873                             array(
874                                 'name' => Q($this->form['name']),
875                                 'id' => $fid,
876                                 'disabled' => $this->form['disabled']
877                         ));
878                     }
879                     else {
880                         $this->rc->output->command('managesieve_dialog_close');
881                         $this->rc->output->send('iframe');
882                     }
883                 }
884                 else {
885                     $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
886 //                  $this->rc->output->send();
887                 }
888             }
889         }
890
891         $this->managesieve_send();
892     }
893
894     private function managesieve_send()
895     {
896         // Handle form action
897         if (isset($_GET['_framed']) || isset($_POST['_framed'])) {
898             if (isset($_GET['_newset']) || isset($_POST['_newset'])) {
899                 $this->rc->output->send('managesieve.setedit');
900             }
901             else {
902                 $this->rc->output->send('managesieve.filteredit');
903             }
904         } else {
905             $this->rc->output->set_pagetitle($this->gettext('filters'));
906             $this->rc->output->send('managesieve.managesieve');
907         }
908     }
909
910     // return the filters list as HTML table
911     function filters_list($attrib)
912     {
913         // add id to message list table if not specified
914         if (!strlen($attrib['id']))
915             $attrib['id'] = 'rcmfilterslist';
916
917         // define list of cols to be displayed
918         $a_show_cols = array('name');
919
920         $result = $this->list_rules();
921
922         // create XHTML table
923         $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
924
925         // set client env
926         $this->rc->output->add_gui_object('filterslist', $attrib['id']);
927         $this->rc->output->include_script('list.js');
928
929         // add some labels to client
930         $this->rc->output->add_label('managesieve.filterdeleteconfirm');
931
932         return $out;
933     }
934
935     // return the filters list as <SELECT>
936     function filtersets_list($attrib, $no_env = false)
937     {
938         // add id to message list table if not specified
939         if (!strlen($attrib['id']))
940             $attrib['id'] = 'rcmfiltersetslist';
941
942         $list = $this->list_scripts();
943
944         if ($list) {
945             asort($list, SORT_LOCALE_STRING);
946         }
947
948         if (!empty($attrib['type']) && $attrib['type'] == 'list') {
949             // define list of cols to be displayed
950             $a_show_cols = array('name');
951
952             if ($list) {
953                 foreach ($list as $idx => $set) {
954                     $scripts['S'.$idx] = $set;
955                     $result[] = array(
956                         'name' => Q($set),
957                         'id' => 'S'.$idx,
958                         'class' => !in_array($set, $this->active) ? 'disabled' : '',
959                     );
960                 }
961             }
962
963             // create XHTML table
964             $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
965
966             $this->rc->output->set_env('filtersets', $scripts);
967             $this->rc->output->include_script('list.js');
968         }
969         else {
970             $select = new html_select(array('name' => '_set', 'id' => $attrib['id'],
971                 'onchange' => $this->rc->task != 'mail' ? 'rcmail.managesieve_set()' : ''));
972
973             if ($list) {
974                 foreach ($list as $set)
975                     $select->add($set, $set);
976             }
977
978             $out = $select->show($this->sieve->current);
979         }
980
981         // set client env
982         if (!$no_env) {
983             $this->rc->output->add_gui_object('filtersetslist', $attrib['id']);
984             $this->rc->output->add_label('managesieve.setdeleteconfirm');
985         }
986
987         return $out;
988     }
989
990     function filter_frame($attrib)
991     {
992         if (!$attrib['id'])
993             $attrib['id'] = 'rcmfilterframe';
994
995         $attrib['name'] = $attrib['id'];
996
997         $this->rc->output->set_env('contentframe', $attrib['name']);
998         $this->rc->output->set_env('blankpage', $attrib['src'] ?
999         $this->rc->output->abs_url($attrib['src']) : 'program/blank.gif');
1000
1001         return html::tag('iframe', $attrib);
1002     }
1003
1004     function filterset_form($attrib)
1005     {
1006         if (!$attrib['id'])
1007             $attrib['id'] = 'rcmfiltersetform';
1008
1009         $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n";
1010
1011         $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
1012         $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
1013         $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
1014         $hiddenfields->add(array('name' => '_newset', 'value' => 1));
1015
1016         $out .= $hiddenfields->show();
1017
1018         $name     = get_input_value('_name', RCUBE_INPUT_POST);
1019         $copy     = get_input_value('_copy', RCUBE_INPUT_POST);
1020         $selected = get_input_value('_from', RCUBE_INPUT_POST);
1021
1022         // filter set name input
1023         $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30,
1024             'class' => ($this->errors['name'] ? 'error' : '')));
1025
1026         $out .= sprintf('<label for="%s"><b>%s:</b></label> %s<br /><br />',
1027             '_name', Q($this->gettext('filtersetname')), $input_name->show($name));
1028
1029         $out .="\n<fieldset class=\"itemlist\"><legend>" . $this->gettext('filters') . ":</legend>\n";
1030         $out .= '<input type="radio" id="from_none" name="_from" value="none"'
1031             .(!$selected || $selected=='none' ? ' checked="checked"' : '').'></input>';
1032         $out .= sprintf('<label for="%s">%s</label> ', 'from_none', Q($this->gettext('none')));
1033
1034         // filters set list
1035         $list   = $this->list_scripts();
1036         $select = new html_select(array('name' => '_copy', 'id' => '_copy'));
1037
1038         if (is_array($list)) {
1039             asort($list, SORT_LOCALE_STRING);
1040
1041             if (!$copy)
1042                 $copy = $_SESSION['managesieve_current'];
1043
1044             foreach ($list as $set) {
1045                 $select->add($set, $set);
1046             }
1047
1048             $out .= '<br /><input type="radio" id="from_set" name="_from" value="set"'
1049                 .($selected=='set' ? ' checked="checked"' : '').'></input>';
1050             $out .= sprintf('<label for="%s">%s:</label> ', 'from_set', Q($this->gettext('fromset')));
1051             $out .= $select->show($copy);
1052         }
1053
1054         // script upload box
1055         $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30,
1056             'type' => 'file', 'class' => ($this->errors['file'] ? 'error' : '')));
1057
1058         $out .= '<br /><input type="radio" id="from_file" name="_from" value="file"'
1059             .($selected=='file' ? ' checked="checked"' : '').'></input>';
1060         $out .= sprintf('<label for="%s">%s:</label> ', 'from_file', Q($this->gettext('fromfile')));
1061         $out .= $upload->show();
1062         $out .= '</fieldset>';
1063
1064         $this->rc->output->add_gui_object('sieveform', 'filtersetform');
1065
1066         if ($this->errors['name'])
1067             $this->add_tip('_name', $this->errors['name'], true);
1068         if ($this->errors['file'])
1069             $this->add_tip('_file', $this->errors['file'], true);
1070
1071         $this->print_tips();
1072
1073         return $out;
1074     }
1075
1076
1077     function filter_form($attrib)
1078     {
1079         if (!$attrib['id'])
1080             $attrib['id'] = 'rcmfilterform';
1081
1082         $fid = get_input_value('_fid', RCUBE_INPUT_GPC);
1083         $scr = isset($this->form) ? $this->form : $this->script[$fid];
1084
1085         $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
1086         $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
1087         $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
1088         $hiddenfields->add(array('name' => '_fid', 'value' => $fid));
1089
1090         $out = '<form name="filterform" action="./" method="post">'."\n";
1091         $out .= $hiddenfields->show();
1092
1093         // 'any' flag
1094         if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not'])
1095             $any = true;
1096
1097         // filter name input
1098         $field_id = '_name';
1099         $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30,
1100             'class' => ($this->errors['name'] ? 'error' : '')));
1101
1102         if ($this->errors['name'])
1103             $this->add_tip($field_id, $this->errors['name'], true);
1104
1105         if (isset($scr))
1106             $input_name = $input_name->show($scr['name']);
1107         else
1108             $input_name = $input_name->show();
1109
1110         $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s\n",
1111             $field_id, Q($this->gettext('filtername')), $input_name);
1112
1113         // filter set selector
1114         if ($this->rc->task == 'mail') {
1115             $out .= sprintf("\n&nbsp;<label for=\"%s\"><b>%s:</b></label> %s\n",
1116                 $field_id, Q($this->gettext('filterset')),
1117                 $this->filtersets_list(array('id' => 'sievescriptname'), true));
1118         }
1119
1120         $out .= '<br /><br /><fieldset><legend>' . Q($this->gettext('messagesrules')) . "</legend>\n";
1121
1122         // any, allof, anyof radio buttons
1123         $field_id = '_allof';
1124         $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof',
1125             'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio'));
1126
1127         if (isset($scr) && !$any)
1128             $input_join = $input_join->show($scr['join'] ? 'allof' : '');
1129         else
1130             $input_join = $input_join->show();
1131
1132         $out .= sprintf("%s<label for=\"%s\">%s</label>&nbsp;\n",
1133             $input_join, $field_id, Q($this->gettext('filterallof')));
1134
1135         $field_id = '_anyof';
1136         $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof',
1137             'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio'));
1138
1139         if (isset($scr) && !$any)
1140             $input_join = $input_join->show($scr['join'] ? '' : 'anyof');
1141         else
1142             $input_join = $input_join->show('anyof'); // default
1143
1144         $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
1145             $input_join, $field_id, Q($this->gettext('filteranyof')));
1146
1147         $field_id = '_any';
1148         $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any',
1149             'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio'));
1150
1151         $input_join = $input_join->show($any ? 'any' : '');
1152
1153         $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
1154             $input_join, $field_id, Q($this->gettext('filterany')));
1155
1156         $rows_num = isset($scr) ? sizeof($scr['tests']) : 1;
1157
1158         $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>';
1159         for ($x=0; $x<$rows_num; $x++)
1160             $out .= $this->rule_div($fid, $x);
1161         $out .= "</div>\n";
1162
1163         $out .= "</fieldset>\n";
1164
1165         // actions
1166         $out .= '<fieldset><legend>' . Q($this->gettext('messagesactions')) . "</legend>\n";
1167
1168         $rows_num = isset($scr) ? sizeof($scr['actions']) : 1;
1169
1170         $out .= '<div id="actions">';
1171         for ($x=0; $x<$rows_num; $x++)
1172             $out .= $this->action_div($fid, $x);
1173         $out .= "</div>\n";
1174
1175         $out .= "</fieldset>\n";
1176
1177         $this->print_tips();
1178
1179         if ($scr['disabled']) {
1180             $this->rc->output->set_env('rule_disabled', true);
1181         }
1182         $this->rc->output->add_label(
1183             'managesieve.ruledeleteconfirm',
1184             'managesieve.actiondeleteconfirm'
1185         );
1186         $this->rc->output->add_gui_object('sieveform', 'filterform');
1187
1188         return $out;
1189     }
1190
1191     function rule_div($fid, $id, $div=true)
1192     {
1193         $rule     = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id];
1194         $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']);
1195
1196         // headers select
1197         $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id,
1198             'onchange' => 'rule_header_select(' .$id .')'));
1199         foreach($this->headers as $name => $val)
1200             $select_header->add(Q($this->gettext($name)), Q($val));
1201         if (in_array('body', $this->exts))
1202             $select_header->add(Q($this->gettext('body')), 'body');
1203         $select_header->add(Q($this->gettext('size')), 'size');
1204         $select_header->add(Q($this->gettext('...')), '...');
1205
1206         // TODO: list arguments
1207         $aout = '';
1208
1209         if ((isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope')))
1210             && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers)
1211         ) {
1212             $aout .= $select_header->show($rule['arg1']);
1213         }
1214         else if ((isset($rule['test']) && $rule['test'] == 'exists')
1215             && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers)
1216         ) {
1217             $aout .= $select_header->show($rule['arg']);
1218         }
1219         else if (isset($rule['test']) && $rule['test'] == 'size')
1220             $aout .= $select_header->show('size');
1221         else if (isset($rule['test']) && $rule['test'] == 'body')
1222             $aout .= $select_header->show('body');
1223         else if (isset($rule['test']) && $rule['test'] != 'true')
1224             $aout .= $select_header->show('...');
1225         else
1226             $aout .= $select_header->show();
1227
1228         if (isset($rule['test']) && in_array($rule['test'], array('header', 'address', 'envelope'))) {
1229             if (is_array($rule['arg1']))
1230                 $custom = implode(', ', $rule['arg1']);
1231             else if (!in_array($rule['arg1'], $this->headers))
1232                 $custom = $rule['arg1'];
1233         }
1234         else if (isset($rule['test']) && $rule['test'] == 'exists') {
1235             if (is_array($rule['arg']))
1236                 $custom = implode(', ', $rule['arg']);
1237             else if (!in_array($rule['arg'], $this->headers))
1238                 $custom = $rule['arg'];
1239         }
1240
1241         $tout = '<div id="custom_header' .$id. '" style="display:' .(isset($custom) ? 'inline' : 'none'). '">
1242             <input type="text" name="_custom_header[]" id="custom_header_i'.$id.'" '
1243             . $this->error_class($id, 'test', 'header', 'custom_header_i')
1244             .' value="' .Q($custom). '" size="15" />&nbsp;</div>' . "\n";
1245
1246         // matching type select (operator)
1247         $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id,
1248             'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'),
1249             'class' => 'operator_selector',
1250             'onchange' => 'rule_op_select('.$id.')'));
1251         $select_op->add(Q($this->gettext('filtercontains')), 'contains');
1252         $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains');
1253         $select_op->add(Q($this->gettext('filteris')), 'is');
1254         $select_op->add(Q($this->gettext('filterisnot')), 'notis');
1255         $select_op->add(Q($this->gettext('filterexists')), 'exists');
1256         $select_op->add(Q($this->gettext('filternotexists')), 'notexists');
1257         $select_op->add(Q($this->gettext('filtermatches')), 'matches');
1258         $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches');
1259         if (in_array('regex', $this->exts)) {
1260             $select_op->add(Q($this->gettext('filterregex')), 'regex');
1261             $select_op->add(Q($this->gettext('filternotregex')), 'notregex');
1262         }
1263         if (in_array('relational', $this->exts)) {
1264             $select_op->add(Q($this->gettext('countisgreaterthan')), 'count-gt');
1265             $select_op->add(Q($this->gettext('countisgreaterthanequal')), 'count-ge');
1266             $select_op->add(Q($this->gettext('countislessthan')), 'count-lt');
1267             $select_op->add(Q($this->gettext('countislessthanequal')), 'count-le');
1268             $select_op->add(Q($this->gettext('countequals')), 'count-eq');
1269             $select_op->add(Q($this->gettext('countnotequals')), 'count-ne');
1270             $select_op->add(Q($this->gettext('valueisgreaterthan')), 'value-gt');
1271             $select_op->add(Q($this->gettext('valueisgreaterthanequal')), 'value-ge');
1272             $select_op->add(Q($this->gettext('valueislessthan')), 'value-lt');
1273             $select_op->add(Q($this->gettext('valueislessthanequal')), 'value-le');
1274             $select_op->add(Q($this->gettext('valueequals')), 'value-eq');
1275             $select_op->add(Q($this->gettext('valuenotequals')), 'value-ne');
1276         }
1277
1278         // target input (TODO: lists)
1279
1280         if (in_array($rule['test'], array('header', 'address', 'envelope'))) {
1281             $test   = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is');
1282             $target = $rule['arg2'];
1283         }
1284         else if ($rule['test'] == 'body') {
1285             $test   = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is');
1286             $target = $rule['arg'];
1287         }
1288         else if ($rule['test'] == 'size') {
1289             $test   = '';
1290             $target = '';
1291             if (preg_match('/^([0-9]+)(K|M|G)?$/', $rule['arg'], $matches)) {
1292                 $sizetarget = $matches[1];
1293                 $sizeitem = $matches[2];
1294             }
1295             else {
1296                 $sizetarget = $rule['arg'];
1297                 $sizeitem = $rule['item'];
1298             }
1299         }
1300         else {
1301             $test   = ($rule['not'] ? 'not' : '').$rule['test'];
1302             $target =  '';
1303         }
1304
1305         $tout .= $select_op->show($test);
1306         $tout .= '<input type="text" name="_rule_target[]" id="rule_target' .$id. '"
1307             value="' .Q($target). '" size="20" ' . $this->error_class($id, 'test', 'target', 'rule_target')
1308             . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n";
1309
1310         $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id));
1311         $select_size_op->add(Q($this->gettext('filterover')), 'over');
1312         $select_size_op->add(Q($this->gettext('filterunder')), 'under');
1313
1314         $tout .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">';
1315         $tout .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : '');
1316         $tout .= '<input type="text" name="_rule_size_target[]" id="rule_size_i'.$id.'" value="'.$sizetarget.'" size="10" ' 
1317             . $this->error_class($id, 'test', 'sizetarget', 'rule_size_i') .' />
1318             <input type="radio" name="_rule_size_item['.$id.']" value=""'
1319                 . (!$sizeitem ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('B').'
1320             <input type="radio" name="_rule_size_item['.$id.']" value="K"'
1321                 . ($sizeitem=='K' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('KB').'
1322             <input type="radio" name="_rule_size_item['.$id.']" value="M"'
1323                 . ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('MB').'
1324             <input type="radio" name="_rule_size_item['.$id.']" value="G"'
1325                 . ($sizeitem=='G' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('GB');
1326         $tout .= '</div>';
1327
1328         // Advanced modifiers (address, envelope)
1329         $select_mod = new html_select(array('name' => "_rule_mod[]", 'id' => 'rule_mod_op'.$id,
1330             'onchange' => 'rule_mod_select(' .$id .')'));
1331         $select_mod->add(Q($this->gettext('none')), '');
1332         $select_mod->add(Q($this->gettext('address')), 'address');
1333         if (in_array('envelope', $this->exts))
1334             $select_mod->add(Q($this->gettext('envelope')), 'envelope');
1335
1336         $select_type = new html_select(array('name' => "_rule_mod_type[]", 'id' => 'rule_mod_type'.$id));
1337         $select_type->add(Q($this->gettext('allparts')), 'all');
1338         $select_type->add(Q($this->gettext('domain')), 'domain');
1339         $select_type->add(Q($this->gettext('localpart')), 'localpart');
1340         if (in_array('subaddress', $this->exts)) {
1341             $select_type->add(Q($this->gettext('user')), 'user');
1342             $select_type->add(Q($this->gettext('detail')), 'detail');
1343         }
1344
1345         $need_mod = $rule['test'] != 'size' && $rule['test'] != 'body';
1346         $mout = '<div id="rule_mod' .$id. '" class="adv" style="display:' . ($need_mod ? 'block' : 'none') .'">';
1347         $mout .= ' <span>';
1348         $mout .= Q($this->gettext('modifier')) . ' ';
1349         $mout .= $select_mod->show($rule['test']);
1350         $mout .= '</span>';
1351         $mout .= ' <span id="rule_mod_type' . $id . '"';
1352         $mout .= ' style="display:' . (in_array($rule['test'], array('address', 'envelope')) ? 'inline' : 'none') .'">';
1353         $mout .= Q($this->gettext('modtype')) . ' ';
1354         $mout .= $select_type->show($rule['part']);
1355         $mout .= '</span>';
1356         $mout .= '</div>';
1357
1358         // Advanced modifiers (body transformations)
1359         $select_mod = new html_select(array('name' => "_rule_trans[]", 'id' => 'rule_trans_op'.$id,
1360             'onchange' => 'rule_trans_select(' .$id .')'));
1361         $select_mod->add(Q($this->gettext('text')), 'text');
1362         $select_mod->add(Q($this->gettext('undecoded')), 'raw');
1363         $select_mod->add(Q($this->gettext('contenttype')), 'content');
1364
1365         $mout .= '<div id="rule_trans' .$id. '" class="adv" style="display:' . ($rule['test'] == 'body' ? 'block' : 'none') .'">';
1366         $mout .= ' <span>';
1367         $mout .= Q($this->gettext('modifier')) . ' ';
1368         $mout .= $select_mod->show($rule['part']);
1369         $mout .= '<input type="text" name="_rule_trans_type[]" id="rule_trans_type'.$id
1370             . '" value="'.(is_array($rule['content']) ? implode(',', $rule['content']) : $rule['content'])
1371             .'" size="20" style="display:' . ($rule['part'] == 'content' ? 'inline' : 'none') .'"'
1372             . $this->error_class($id, 'test', 'part', 'rule_trans_type') .' />';
1373         $mout .= '</span>';
1374         $mout .= '</div>';
1375
1376         // Advanced modifiers (body transformations)
1377         $select_comp = new html_select(array('name' => "_rule_comp[]", 'id' => 'rule_comp_op'.$id));
1378         $select_comp->add(Q($this->gettext('default')), '');
1379         $select_comp->add(Q($this->gettext('octet')), 'i;octet');
1380         $select_comp->add(Q($this->gettext('asciicasemap')), 'i;ascii-casemap');
1381         if (in_array('comparator-i;ascii-numeric', $this->exts)) {
1382             $select_comp->add(Q($this->gettext('asciinumeric')), 'i;ascii-numeric');
1383         }
1384
1385         $mout .= '<div id="rule_comp' .$id. '" class="adv" style="display:' . ($rule['test'] != 'size' ? 'block' : 'none') .'">';
1386         $mout .= ' <span>';
1387         $mout .= Q($this->gettext('comparator')) . ' ';
1388         $mout .= $select_comp->show($rule['comparator']);
1389         $mout .= '</span>';
1390         $mout .= '</div>';
1391
1392         // Build output table
1393         $out = $div ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : '';
1394         $out .= '<table><tr>';
1395         $out .= '<td class="advbutton">';
1396         $out .= '<a href="#" id="ruleadv' . $id .'" title="'. Q($this->gettext('advancedopts')). '"
1397             onclick="rule_adv_switch(' . $id .', this)" class="show">&nbsp;&nbsp;</a>';
1398         $out .= '</td>';
1399         $out .= '<td class="rowactions">' . $aout . '</td>';
1400         $out .= '<td class="rowtargets">' . $tout . "\n";
1401         $out .= '<div id="rule_advanced' .$id. '" style="display:none">' . $mout . '</div>';
1402         $out .= '</td>';
1403
1404         // add/del buttons
1405         $out .= '<td class="rowbuttons">';
1406         $out .= '<a href="#" id="ruleadd' . $id .'" title="'. Q($this->gettext('add')). '"
1407             onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button add"></a>';
1408         $out .= '<a href="#" id="ruledel' . $id .'" title="'. Q($this->gettext('del')). '"
1409             onclick="rcmail.managesieve_ruledel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
1410         $out .= '</td>';
1411         $out .= '</tr></table>';
1412
1413         $out .= $div ? "</div>\n" : '';
1414
1415         return $out;
1416     }
1417
1418     function action_div($fid, $id, $div=true)
1419     {
1420         $action   = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id];
1421         $rows_num = isset($this->form) ? sizeof($this->form['actions']) : sizeof($this->script[$fid]['actions']);
1422
1423         $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : '';
1424
1425         $out .= '<table><tr><td class="rowactions">';
1426
1427         // action select
1428         $select_action = new html_select(array('name' => "_action_type[$id]", 'id' => 'action_type'.$id,
1429             'onchange' => 'action_type_select(' .$id .')'));
1430         if (in_array('fileinto', $this->exts))
1431             $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto');
1432         if (in_array('fileinto', $this->exts) && in_array('copy', $this->exts))
1433             $select_action->add(Q($this->gettext('messagecopyto')), 'fileinto_copy');
1434         $select_action->add(Q($this->gettext('messageredirect')), 'redirect');
1435         if (in_array('copy', $this->exts))
1436             $select_action->add(Q($this->gettext('messagesendcopy')), 'redirect_copy');
1437         if (in_array('reject', $this->exts))
1438             $select_action->add(Q($this->gettext('messagediscard')), 'reject');
1439         else if (in_array('ereject', $this->exts))
1440             $select_action->add(Q($this->gettext('messagediscard')), 'ereject');
1441         if (in_array('vacation', $this->exts))
1442             $select_action->add(Q($this->gettext('messagereply')), 'vacation');
1443         $select_action->add(Q($this->gettext('messagedelete')), 'discard');
1444         if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) {
1445             $select_action->add(Q($this->gettext('setflags')), 'setflag');
1446             $select_action->add(Q($this->gettext('addflags')), 'addflag');
1447             $select_action->add(Q($this->gettext('removeflags')), 'removeflag');
1448         }
1449         $select_action->add(Q($this->gettext('rulestop')), 'stop');
1450
1451         $select_type = $action['type'];
1452         if (in_array($action['type'], array('fileinto', 'redirect')) && $action['copy']) {
1453             $select_type .= '_copy';
1454         }
1455
1456         $out .= $select_action->show($select_type);
1457         $out .= '</td>';
1458
1459         // actions target inputs
1460         $out .= '<td class="rowtargets">';
1461         // shared targets
1462         $out .= '<input type="text" name="_action_target['.$id.']" id="action_target' .$id. '" '
1463             .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="35" '
1464             .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" '
1465             . $this->error_class($id, 'action', 'target', 'action_target') .' />';
1466         $out .= '<textarea name="_action_target_area['.$id.']" id="action_target_area' .$id. '" '
1467             .'rows="3" cols="35" '. $this->error_class($id, 'action', 'targetarea', 'action_target_area')
1468             .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">'
1469             . (in_array($action['type'], array('reject', 'ereject')) ? Q($action['target'], 'strict', false) : '')
1470             . "</textarea>\n";
1471
1472         // vacation
1473         $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
1474         $out .= '<span class="label">'. Q($this->gettext('vacationreason')) .'</span><br />'
1475             .'<textarea name="_action_reason['.$id.']" id="action_reason' .$id. '" '
1476             .'rows="3" cols="35" '. $this->error_class($id, 'action', 'reason', 'action_reason') . '>'
1477             . Q($action['reason'], 'strict', false) . "</textarea>\n";
1478         $out .= '<br /><span class="label">' .Q($this->gettext('vacationsubject')) . '</span><br />'
1479             .'<input type="text" name="_action_subject['.$id.']" id="action_subject'.$id.'" '
1480             .'value="' . (is_array($action['subject']) ? Q(implode(', ', $action['subject']), 'strict', false) : $action['subject']) . '" size="35" '
1481             . $this->error_class($id, 'action', 'subject', 'action_subject') .' />';
1482         $out .= '<br /><span class="label">' .Q($this->gettext('vacationaddresses')) . '</span><br />'
1483             .'<input type="text" name="_action_addresses['.$id.']" id="action_addr'.$id.'" '
1484             .'value="' . (is_array($action['addresses']) ? Q(implode(', ', $action['addresses']), 'strict', false) : $action['addresses']) . '" size="35" '
1485             . $this->error_class($id, 'action', 'addresses', 'action_addr') .' />';
1486         $out .= '<br /><span class="label">' . Q($this->gettext('vacationdays')) . '</span><br />'
1487             .'<input type="text" name="_action_days['.$id.']" id="action_days'.$id.'" '
1488             .'value="' .Q($action['days'], 'strict', false) . '" size="2" '
1489             . $this->error_class($id, 'action', 'days', 'action_days') .' />';
1490         $out .= '</div>';
1491
1492         // flags
1493         $flags = array(
1494             'read'      => '\\Seen',
1495             'answered'  => '\\Answered',
1496             'flagged'   => '\\Flagged',
1497             'deleted'   => '\\Deleted',
1498             'draft'     => '\\Draft',
1499         );
1500         $flags_target = (array)$action['target'];
1501
1502         $out .= '<div id="action_flags' .$id.'" style="display:' 
1503             . (preg_match('/^(set|add|remove)flag$/', $action['type']) ? 'inline' : 'none') . '"'
1504             . $this->error_class($id, 'action', 'flags', 'action_flags') . '>';
1505         foreach ($flags as $fidx => $flag) {
1506             $out .= '<input type="checkbox" name="_action_flags[' .$id .'][]" value="' . $flag . '"'
1507                 . (in_array_nocase($flag, $flags_target) ? 'checked="checked"' : '') . ' />'
1508                 . Q($this->gettext('flag'.$fidx)) .'<br>';
1509         }
1510         $out .= '</div>';
1511
1512         // mailbox select
1513         if ($action['type'] == 'fileinto')
1514             $mailbox = $this->mod_mailbox($action['target'], 'out');
1515         else
1516             $mailbox = '';
1517
1518         $select = rcmail_mailbox_select(array(
1519             'realnames' => false,
1520             'maxlength' => 100,
1521             'id' => 'action_mailbox' . $id,
1522             'name' => "_action_mailbox[$id]",
1523             'style' => 'display:'.(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none')
1524         ));
1525         $out .= $select->show($mailbox);
1526         $out .= '</td>';
1527
1528         // add/del buttons
1529         $out .= '<td class="rowbuttons">';
1530         $out .= '<a href="#" id="actionadd' . $id .'" title="'. Q($this->gettext('add')). '"
1531             onclick="rcmail.managesieve_actionadd(' . $id .')" class="button add"></a>';
1532         $out .= '<a href="#" id="actiondel' . $id .'" title="'. Q($this->gettext('del')). '"
1533             onclick="rcmail.managesieve_actiondel(' . $id .')" class="button del' . ($rows_num<2 ? ' disabled' : '') .'"></a>';
1534         $out .= '</td>';
1535
1536         $out .= '</tr></table>';
1537
1538         $out .= $div ? "</div>\n" : '';
1539
1540         return $out;
1541     }
1542
1543     private function genid()
1544     {
1545         $result = preg_replace('/[^0-9]/', '', microtime(true));
1546         return $result;
1547     }
1548
1549     private function strip_value($str, $allow_html=false)
1550     {
1551         if (!$allow_html)
1552             $str = strip_tags($str);
1553
1554         return trim($str);
1555     }
1556
1557     private function error_class($id, $type, $target, $elem_prefix='')
1558     {
1559         // TODO: tooltips
1560         if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) ||
1561             ($type == 'action' && ($str = $this->errors['actions'][$id][$target]))
1562         ) {
1563             $this->add_tip($elem_prefix.$id, $str, true);
1564             return ' class="error"';
1565         }
1566
1567         return '';
1568     }
1569
1570     private function add_tip($id, $str, $error=false)
1571     {
1572         if ($error)
1573             $str = html::span('sieve error', $str);
1574
1575         $this->tips[] = array($id, $str);
1576     }
1577
1578     private function print_tips()
1579     {
1580         if (empty($this->tips))
1581             return;
1582
1583         $script = JS_OBJECT_NAME.'.managesieve_tip_register('.json_encode($this->tips).');';
1584         $this->rc->output->add_script($script, 'foot');
1585     }
1586
1587     /**
1588      * Converts mailbox name from/to UTF7-IMAP from/to internal Sieve encoding
1589      * with delimiter replacement.
1590      *
1591      * @param string $mailbox Mailbox name
1592      * @param string $mode    Conversion direction ('in'|'out')
1593      *
1594      * @return string Mailbox name
1595      */
1596     private function mod_mailbox($mailbox, $mode = 'out')
1597     {
1598         $delimiter         = $_SESSION['imap_delimiter'];
1599         $replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter');
1600         $mbox_encoding     = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP');
1601
1602         if ($mode == 'out') {
1603             $mailbox = rcube_charset_convert($mailbox, $mbox_encoding, 'UTF7-IMAP');
1604             if ($replace_delimiter && $replace_delimiter != $delimiter)
1605                 $mailbox = str_replace($replace_delimiter, $delimiter, $mailbox);
1606         }
1607         else {
1608             $mailbox = rcube_charset_convert($mailbox, 'UTF7-IMAP', $mbox_encoding);
1609             if ($replace_delimiter && $replace_delimiter != $delimiter)
1610                 $mailbox = str_replace($delimiter, $replace_delimiter, $mailbox);
1611         }
1612
1613         return $mailbox;
1614     }
1615
1616     /**
1617      * List sieve scripts
1618      *
1619      * @return array Scripts list
1620      */
1621     public function list_scripts()
1622     {
1623         if ($this->list !== null) {
1624             return $this->list;
1625         }
1626
1627         $this->list = $this->sieve->get_scripts();
1628
1629         // Handle active script(s) and list of scripts according to Kolab's KEP:14
1630         if ($this->rc->config->get('managesieve_kolab_master')) {
1631
1632             // Skip protected names
1633             foreach ((array)$this->list as $idx => $name) {
1634                 $_name = strtoupper($name);
1635                 if ($_name == 'MASTER')
1636                     $master_script = $name;
1637                 else if ($_name == 'MANAGEMENT')
1638                     $management_script = $name;
1639                 else if($_name == 'USER')
1640                     $user_script = $name;
1641                 else
1642                     continue;
1643
1644                 unset($this->list[$idx]);
1645             }
1646
1647             // get active script(s), read USER script
1648             if ($user_script) {
1649                 $extension = $this->rc->config->get('managesieve_filename_extension', '.sieve');
1650                 $filename_regex = '/'.preg_quote($extension, '/').'$/';
1651                 $_SESSION['managesieve_user_script'] = $user_script;
1652
1653                 $this->sieve->load($user_script);
1654
1655                 foreach ($this->sieve->script->as_array() as $rules) {
1656                     foreach ($rules['actions'] as $action) {
1657                         if ($action['type'] == 'include' && empty($action['global'])) {
1658                             $name = preg_replace($filename_regex, '', $action['target']);
1659                             $this->active[] = $name;
1660                         }
1661                     }
1662                 }
1663             }
1664             // create USER script if it doesn't exist
1665             else {
1666                 $content = "# USER Management Script\n"
1667                     ."#\n"
1668                     ."# This script includes the various active sieve scripts\n"
1669                     ."# it is AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY!\n"
1670                     ."#\n"
1671                     ."# For more information, see http://wiki.kolab.org/KEP:14#USER\n"
1672                     ."#\n";
1673                 if ($this->sieve->save_script('USER', $content)) {
1674                     $_SESSION['managesieve_user_script'] = 'USER';
1675                     if (empty($this->master_file))
1676                         $this->sieve->activate('USER');
1677                 }
1678             }
1679         }
1680         else if (!empty($this->list)) {
1681             // Get active script name
1682             if ($active = $this->sieve->get_active()) {
1683                 $this->active = array($active);
1684             }
1685         }
1686
1687         return $this->list;
1688     }
1689
1690     /**
1691      * Removes sieve script
1692      *
1693      * @param string $name Script name
1694      *
1695      * @return bool True on success, False on failure
1696      */
1697     public function remove_script($name)
1698     {
1699         $result = $this->sieve->remove($name);
1700
1701         // Kolab's KEP:14
1702         if ($result && $this->rc->config->get('managesieve_kolab_master')) {
1703             $this->deactivate_script($name);
1704         }
1705
1706         return $result;
1707     }
1708
1709     /**
1710      * Activates sieve script
1711      *
1712      * @param string $name Script name
1713      *
1714      * @return bool True on success, False on failure
1715      */
1716     public function activate_script($name)
1717     {
1718         // Kolab's KEP:14
1719         if ($this->rc->config->get('managesieve_kolab_master')) {
1720             $extension   = $this->rc->config->get('managesieve_filename_extension', '.sieve');
1721             $user_script = $_SESSION['managesieve_user_script'];
1722
1723             // if the script is not active...
1724             if ($user_script && ($key = array_search($name, $this->active)) === false) {
1725                 // ...rewrite USER file adding appropriate include command
1726                 if ($this->sieve->load($user_script)) {
1727                     $script = $this->sieve->script->as_array();
1728                     $list   = array();
1729                     $regexp = '/' . preg_quote($extension, '/') . '$/';
1730
1731                     // Create new include entry
1732                     $rule = array(
1733                         'actions' => array(
1734                             0 => array(
1735                                 'target'   => $name.$extension,
1736                                 'type'     => 'include',
1737                                 'personal' => true,
1738                     )));
1739
1740                     // get all active scripts for sorting
1741                     foreach ($script as $rid => $rules) {
1742                         foreach ($rules['actions'] as $aid => $action) {
1743                             if ($action['type'] == 'include' && empty($action['global'])) {
1744                                 $target = $extension ? preg_replace($regexp, '', $action['target']) : $action['target'];
1745                                 $list[] = $target;
1746                             }
1747                         }
1748                     }
1749                     $list[] = $name;
1750
1751                     // Sort and find current script position
1752                     asort($list, SORT_LOCALE_STRING);
1753                     $list = array_values($list);
1754                     $index = array_search($name, $list);
1755
1756                     // add rule at the end of the script
1757                     if ($index === false || $index == count($list)-1) {
1758                         $this->sieve->script->add_rule($rule);
1759                     }
1760                     // add rule at index position
1761                     else {
1762                         $script2 = array();
1763                         foreach ($script as $rid => $rules) {
1764                             if ($rid == $index) {
1765                                 $script2[] = $rule;
1766                             }
1767                             $script2[] = $rules;
1768                         }
1769                         $this->sieve->script->content = $script2;
1770                     }
1771
1772                     $result = $this->sieve->save();
1773                     if ($result) {
1774                         $this->active[] = $name;
1775                     }
1776                 }
1777             }
1778         }
1779         else {
1780             $result = $this->sieve->activate($name);
1781             if ($result)
1782                 $this->active = array($name);
1783         }
1784
1785         return $result;
1786     }
1787
1788     /**
1789      * Deactivates sieve script
1790      *
1791      * @param string $name Script name
1792      *
1793      * @return bool True on success, False on failure
1794      */
1795     public function deactivate_script($name)
1796     {
1797         // Kolab's KEP:14
1798         if ($this->rc->config->get('managesieve_kolab_master')) {
1799             $extension   = $this->rc->config->get('managesieve_filename_extension', '.sieve');
1800             $user_script = $_SESSION['managesieve_user_script'];
1801
1802             // if the script is active...
1803             if ($user_script && ($key = array_search($name, $this->active)) !== false) {
1804                 // ...rewrite USER file removing appropriate include command
1805                 if ($this->sieve->load($user_script)) {
1806                     $script = $this->sieve->script->as_array();
1807                     $name   = $name.$extension;
1808
1809                     foreach ($script as $rid => $rules) {
1810                         foreach ($rules['actions'] as $aid => $action) {
1811                             if ($action['type'] == 'include' && empty($action['global'])
1812                                 && $action['target'] == $name
1813                             ) {
1814                                 break 2;
1815                             }
1816                         }
1817                     }
1818
1819                     // Entry found
1820                     if ($rid < count($script)) {
1821                         $this->sieve->script->delete_rule($rid);
1822                         $result = $this->sieve->save();
1823                         if ($result) {
1824                             unset($this->active[$key]);
1825                         }
1826                     }
1827                 }
1828             }
1829         }
1830         else {
1831             $result = $this->sieve->deactivate();
1832             if ($result)
1833                 $this->active = array();
1834         }
1835
1836         return $result;
1837     }
1838
1839     /**
1840      * Saves current script (adding some variables)
1841      */
1842     public function save_script($name = null)
1843     {
1844         // Kolab's KEP:14
1845         if ($this->rc->config->get('managesieve_kolab_master')) {
1846             $this->sieve->script->set_var('EDITOR', self::PROGNAME);
1847             $this->sieve->script->set_var('EDITOR_VERSION', self::VERSION);
1848         }
1849
1850         return $this->sieve->save($name);
1851     }
1852
1853     /**
1854      * Returns list of rules from the current script
1855      *
1856      * @return array List of rules
1857      */
1858     public function list_rules()
1859     {
1860         $result = array();
1861         $i      = 1;
1862
1863         foreach ($this->script as $idx => $filter) {
1864             if ($filter['type'] != 'if') {
1865                 continue;
1866             }
1867             $fname = $filter['name'] ? $filter['name'] : "#$i";
1868             $result[] = array(
1869                 'id'    => $idx,
1870                 'name'  => Q($fname),
1871                 'class' => $filter['disabled'] ? 'disabled' : '',
1872             );
1873             $i++;
1874         }
1875
1876         return $result;
1877     }
1878 }