source: subversion/trunk/plugins/managesieve/managesieve.php @ 4525

Last change on this file since 4525 was 4525, checked in by alec, 2 years ago
  • Moved javascript code from skin templates into managesieve.js file
  • Property svn:keywords set to Id
File size: 52.2 KB
Line 
1<?php
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 3.0
11 * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
12 *
13 * Configuration (see config.inc.php.dist)
14 *
15 * $Id$
16 */
17
18class managesieve extends rcube_plugin
19{
20    public $task = 'settings';
21
22    private $rc;
23    private $sieve;
24    private $errors;
25    private $form;
26    private $tips = array();
27    private $script = array();
28    private $exts = array();
29    private $headers = array(
30        'subject'   => 'Subject',
31        'sender'    => 'From',
32        'recipient' => 'To',
33    );
34
35
36    function init()
37    {
38        // add Tab label/title
39        $this->add_texts('localization/', array('filters','managefilters'));
40
41        // register actions
42        $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
43        $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
44
45        // include main js script
46        $this->include_script('managesieve.js');
47    }
48
49    function managesieve_start()
50    {
51        $this->rc = rcmail::get_instance();
52        $this->load_config();
53
54        // register UI objects
55        $this->rc->output->add_handlers(array(
56            'filterslist'    => array($this, 'filters_list'),
57            'filtersetslist' => array($this, 'filtersets_list'),
58            'filterframe'    => array($this, 'filter_frame'),
59            'filterform'     => array($this, 'filter_form'),
60            'filtersetform'  => array($this, 'filterset_form'),
61        ));
62
63        // Add include path for internal classes
64        $include_path = $this->home . '/lib' . PATH_SEPARATOR;
65        $include_path .= ini_get('include_path');
66        set_include_path($include_path);
67
68        $host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost'));
69        $port = $this->rc->config->get('managesieve_port', 2000);
70
71        $host = rcube_idn_to_ascii($host);
72
73        // try to connect to managesieve server and to fetch the script
74        $this->sieve = new rcube_sieve($_SESSION['username'],
75            $this->rc->decrypt($_SESSION['password']), $host, $port,
76            $this->rc->config->get('managesieve_auth_type'),
77            $this->rc->config->get('managesieve_usetls', false),
78            $this->rc->config->get('managesieve_disabled_extensions'),
79            $this->rc->config->get('managesieve_debug', false),
80            $this->rc->config->get('managesieve_auth_cid'), 
81            $this->rc->config->get('managesieve_auth_pw')
82        );
83
84        if (!($error = $this->sieve->error())) {
85
86            $list = $this->sieve->get_scripts();
87            $active = $this->sieve->get_active();
88            $_SESSION['managesieve_active'] = $active;
89
90            if (!empty($_GET['_set'])) {
91                $script_name = get_input_value('_set', RCUBE_INPUT_GET);
92            }
93            else if (!empty($_SESSION['managesieve_current'])) {
94                $script_name = $_SESSION['managesieve_current'];
95            }
96            else {
97                // get active script
98                if ($active) {
99                    $script_name = $active;
100                }
101                else if ($list) {
102                    $script_name = $list[0];
103                }
104                // create a new (initial) script
105                else {
106                    // if script not exists build default script contents
107                    $script_file = $this->rc->config->get('managesieve_default');
108                    $script_name = 'roundcube';
109                    if ($script_file && is_readable($script_file))
110                        $content = file_get_contents($script_file);
111
112                // add script and set it active
113                if ($this->sieve->save_script($script_name, $content))
114                    if ($this->sieve->activate($script_name))
115                        $_SESSION['managesieve_active'] = $script_name;
116                }
117            }
118
119            if ($script_name)
120                $this->sieve->load($script_name);
121
122            $error = $this->sieve->error();
123        }
124
125        // finally set script objects
126        if ($error) {
127            switch ($error) {
128                case SIEVE_ERROR_CONNECTION:
129                case SIEVE_ERROR_LOGIN:
130                    $this->rc->output->show_message('managesieve.filterconnerror', 'error');
131                    break;
132                default:
133                    $this->rc->output->show_message('managesieve.filterunknownerror', 'error');
134                    break;
135            }
136
137            raise_error(array('code' => 403, 'type' => 'php',
138                'file' => __FILE__, 'line' => __LINE__,
139                'message' => "Unable to connect to managesieve on $host:$port"), true, false);
140
141            // to disable 'Add filter' button set env variable
142            $this->rc->output->set_env('filterconnerror', true);
143            $this->script = array();
144        }
145        else {
146            $this->script = $this->sieve->script->as_array();
147            $this->exts = $this->sieve->get_extensions();
148            $this->rc->output->set_env('active_set', $_SESSION['managesieve_active']);
149            $_SESSION['managesieve_current'] = $this->sieve->current;
150        }
151
152        return $error;
153    }
154
155    function managesieve_actions()
156    {
157        // Init plugin and handle managesieve connection
158        $error = $this->managesieve_start();
159
160        // Handle user requests
161        if ($action = get_input_value('_act', RCUBE_INPUT_GPC)) {
162            $fid = (int) get_input_value('_fid', RCUBE_INPUT_GET);
163
164            if ($action == 'up' && !$error) {
165                if ($fid && isset($this->script[$fid]) && isset($this->script[$fid-1])) {
166                    if ($this->sieve->script->update_rule($fid, $this->script[$fid-1]) !== false
167                        && $this->sieve->script->update_rule($fid-1, $this->script[$fid]) !== false) {
168                        $result = $this->sieve->save();
169                    }
170
171                    if ($result) {
172//                      $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
173                        $this->rc->output->command('managesieve_updatelist', 'up', '', $fid);
174                    } else
175                        $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
176                }
177            }
178            else if ($action == 'down' && !$error) {
179                if (isset($this->script[$fid]) && isset($this->script[$fid+1])) {
180                    if ($this->sieve->script->update_rule($fid, $this->script[$fid+1]) !== false
181                        && $this->sieve->script->update_rule($fid+1, $this->script[$fid]) !== false) {
182                        $result = $this->sieve->save();
183                    }
184
185                    if ($result === true) {
186//                      $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
187                        $this->rc->output->command('managesieve_updatelist', 'down', '', $fid);
188                    } else {
189                        $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
190                    }
191                }
192            }
193            else if ($action == 'delete' && !$error) {
194                if (isset($this->script[$fid])) {
195                    if ($this->sieve->script->delete_rule($fid))
196                        $result = $this->sieve->save();
197
198                    if ($result === true) {
199                        $this->rc->output->show_message('managesieve.filterdeleted', 'confirmation');
200                        $this->rc->output->command('managesieve_updatelist', 'delete', '', $fid);
201                    } else {
202                        $this->rc->output->show_message('managesieve.filterdeleteerror', 'error');
203                    }
204                }
205            }
206            else if ($action == 'setact' && !$error) {
207                $script_name = get_input_value('_set', RCUBE_INPUT_GPC);
208                $result = $this->sieve->activate($script_name);
209
210                if ($result === true) {
211                    $this->rc->output->set_env('active_set', $script_name);
212                    $this->rc->output->show_message('managesieve.setactivated', 'confirmation');
213                    $this->rc->output->command('managesieve_reset');
214                    $_SESSION['managesieve_active'] = $script_name;
215                } else {
216                    $this->rc->output->show_message('managesieve.setactivateerror', 'error');
217                }
218            }
219            else if ($action == 'deact' && !$error) {
220                $result = $this->sieve->deactivate();
221
222                if ($result === true) {
223                    $this->rc->output->set_env('active_set', '');
224                    $this->rc->output->show_message('managesieve.setdeactivated', 'confirmation');
225                    $this->rc->output->command('managesieve_reset');
226                    $_SESSION['managesieve_active'] = '';
227                } else {
228                    $this->rc->output->show_message('managesieve.setdeactivateerror', 'error');
229                }
230            }
231            else if ($action == 'setdel' && !$error) {
232                $script_name = get_input_value('_set', RCUBE_INPUT_GPC);
233                $result = $this->sieve->remove($script_name);
234
235                if ($result === true) {
236                    $this->rc->output->show_message('managesieve.setdeleted', 'confirmation');
237                    $this->rc->output->command('managesieve_reload');
238                    $this->rc->session->remove('managesieve_current');
239                } else {
240                    $this->rc->output->show_message('managesieve.setdeleteerror', 'error');
241                }
242            }
243            else if ($action == 'setget') {
244                $script_name = get_input_value('_set', RCUBE_INPUT_GPC);
245                $script = $this->sieve->get_script($script_name);
246
247                if (PEAR::isError($script))
248                    exit;
249
250                $browser = new rcube_browser;
251
252                // send download headers
253                header("Content-Type: application/octet-stream");
254                header("Content-Length: ".strlen($script));
255
256                if ($browser->ie)
257                    header("Content-Type: application/force-download");
258                if ($browser->ie && $browser->ver < 7)
259                    $filename = rawurlencode(abbreviate_string($script_name, 55));
260                else if ($browser->ie)
261                    $filename = rawurlencode($script_name);
262                else
263                    $filename = addcslashes($script_name, '\\"');
264
265                header("Content-Disposition: attachment; filename=\"$filename.txt\"");
266                echo $script;
267                exit;
268            }
269            elseif ($action == 'ruleadd') {
270                $rid = get_input_value('_rid', RCUBE_INPUT_GPC);
271                $id = $this->genid();
272                $content = $this->rule_div($fid, $id, false);
273
274                $this->rc->output->command('managesieve_rulefill', $content, $id, $rid);
275            }
276            elseif ($action == 'actionadd') {
277                $aid = get_input_value('_aid', RCUBE_INPUT_GPC);
278                $id = $this->genid();
279                $content = $this->action_div($fid, $id, false);
280
281                $this->rc->output->command('managesieve_actionfill', $content, $id, $aid);
282            }
283
284            $this->rc->output->send();
285        }
286
287        $this->managesieve_send();
288    }
289
290    function managesieve_save()
291    {
292        // Init plugin and handle managesieve connection
293        $error = $this->managesieve_start();
294
295        // filters set add action
296        if (!empty($_POST['_newset'])) {
297            $name = get_input_value('_name', RCUBE_INPUT_POST);
298            $copy = get_input_value('_copy', RCUBE_INPUT_POST);
299            $from = get_input_value('_from', RCUBE_INPUT_POST);
300
301            if (!$name)
302                $error = 'managesieve.emptyname';
303            else if (mb_strlen($name)>128)
304                $error = 'managesieve.nametoolong';
305            else if ($from == 'file') {
306                // from file
307                if (is_uploaded_file($_FILES['_file']['tmp_name'])) {
308                    $file = file_get_contents($_FILES['_file']['tmp_name']);
309                    $file = preg_replace('/\r/', '', $file);
310                    // for security don't save script directly
311                    // check syntax before, like this...
312                    $this->sieve->load_script($file);
313                    if (!$this->sieve->save($name)) {
314                        $error = 'managesieve.setcreateerror';
315                    }
316                }
317                else {  // upload failed
318                    $err = $_FILES['_file']['error'];
319                    $error = true;
320
321                    if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
322                        $msg = rcube_label(array('name' => 'filesizeerror',
323                            'vars' => array('size' =>
324                                show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
325                    }
326                    else {
327                        $error = 'fileuploaderror';
328                    }
329                }
330            }
331            else if (!$this->sieve->copy($name, $from == 'set' ? $copy : '')) {
332                $error = 'managesieve.setcreateerror';
333            }
334
335            if (!$error) {
336                $this->rc->output->show_message('managesieve.setcreated', 'confirmation');
337                $this->rc->output->command('parent.managesieve_reload', $name);
338            } else if ($msg) {
339                $this->rc->output->command('display_message', $msg, 'error');
340            } else {
341                $this->rc->output->show_message($error, 'error');
342            }
343        }
344        // filter add/edit action
345        else if (isset($_POST['_name'])) {
346            $name = trim(get_input_value('_name', RCUBE_INPUT_POST, true));
347            $fid  = trim(get_input_value('_fid', RCUBE_INPUT_POST));
348            $join = trim(get_input_value('_join', RCUBE_INPUT_POST));
349
350            // and arrays
351            $headers = $_POST['_header'];
352            $cust_headers = $_POST['_custom_header'];
353            $ops = $_POST['_rule_op'];
354            $sizeops = $_POST['_rule_size_op'];
355            $sizeitems = $_POST['_rule_size_item'];
356            $sizetargets = $_POST['_rule_size_target'];
357            $targets = $_POST['_rule_target'];
358            $act_types = $_POST['_action_type'];
359            $mailboxes = $_POST['_action_mailbox'];
360            $act_targets = $_POST['_action_target'];
361            $area_targets = $_POST['_action_target_area'];
362            $reasons = $_POST['_action_reason'];
363            $addresses = $_POST['_action_addresses'];
364            $days = $_POST['_action_days'];
365            $subject = $_POST['_action_subject'];
366            $flags = $_POST['_action_flags'];
367
368            // we need a "hack" for radiobuttons
369            foreach ($sizeitems as $item)
370                $items[] = $item;
371
372            $this->form['disabled'] = $_POST['_disabled'] ? true : false;
373            $this->form['join']     = $join=='allof' ? true : false;
374            $this->form['name']     = $name;
375            $this->form['tests']    = array();
376            $this->form['actions']  = array();
377
378            if ($name == '')
379                $this->errors['name'] = $this->gettext('cannotbeempty');
380            else {
381                foreach($this->script as $idx => $rule)
382                    if($rule['name'] == $name && $idx != $fid) {
383                        $this->errors['name'] = $this->gettext('ruleexist');
384                        break;
385                    }
386            }
387
388            $i = 0;
389            // rules
390            if ($join == 'any') {
391                $this->form['tests'][0]['test'] = 'true';
392            }
393            else {
394                foreach ($headers as $idx => $header) {
395                    $header = $this->strip_value($header);
396                    $target = $this->strip_value($targets[$idx], true);
397                    $op     = $this->strip_value($ops[$idx]);
398
399                    // normal header
400                    if (in_array($header, $this->headers)) {
401                        if (preg_match('/^not/', $op))
402                            $this->form['tests'][$i]['not'] = true;
403                        $type = preg_replace('/^not/', '', $op);
404
405                        if ($type == 'exists') {
406                            $this->form['tests'][$i]['test'] = 'exists';
407                            $this->form['tests'][$i]['arg'] = $header;
408                        }
409                        else {
410                            $this->form['tests'][$i]['type'] = $type;
411                            $this->form['tests'][$i]['test'] = 'header';
412                            $this->form['tests'][$i]['arg1'] = $header;
413                            $this->form['tests'][$i]['arg2'] = $target;
414
415                            if ($target == '')
416                                $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
417                            else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
418                                $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
419                        }
420                    }
421                    else
422                        switch ($header) {
423                        case 'size':
424                            $sizeop     = $this->strip_value($sizeops[$idx]);
425                            $sizeitem   = $this->strip_value($items[$idx]);
426                            $sizetarget = $this->strip_value($sizetargets[$idx]);
427
428                            $this->form['tests'][$i]['test'] = 'size';
429                            $this->form['tests'][$i]['type'] = $sizeop;
430                            $this->form['tests'][$i]['arg']  = $sizetarget.$sizeitem;
431
432                            if ($sizetarget == '')
433                                $this->errors['tests'][$i]['sizetarget'] = $this->gettext('cannotbeempty');
434                            else if (!preg_match('/^[0-9]+(K|M|G)*$/i', $sizetarget))
435                                $this->errors['tests'][$i]['sizetarget'] = $this->gettext('forbiddenchars');
436                            break;
437                        case '...':
438                            $cust_header = $headers = $this->strip_value($cust_headers[$idx]);
439
440                            if (preg_match('/^not/', $op))
441                                $this->form['tests'][$i]['not'] = true;
442                            $type = preg_replace('/^not/', '', $op);
443
444                            if ($cust_header == '')
445                                $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty');
446                            else {
447                                $headers = preg_split('/[\s,]+/', $cust_header, -1, PREG_SPLIT_NO_EMPTY);
448
449                                if (!count($headers))
450                                    $this->errors['tests'][$i]['header'] = $this->gettext('cannotbeempty');
451                                else {
452                                    foreach ($headers as $hr)
453                                        if (!preg_match('/^[a-z0-9-]+$/i', $hr))
454                                            $this->errors['tests'][$i]['header'] = $this->gettext('forbiddenchars');
455                                }
456                            }
457
458                            if (empty($this->errors['tests'][$i]['header']))
459                                $cust_header = (is_array($headers) && count($headers) == 1) ? $headers[0] : $headers;
460
461                            if ($type == 'exists') {
462                                $this->form['tests'][$i]['test'] = 'exists';
463                                $this->form['tests'][$i]['arg']  = $cust_header;
464                            }
465                            else {
466                                $this->form['tests'][$i]['test'] = 'header';
467                                $this->form['tests'][$i]['type'] = $type;
468                                $this->form['tests'][$i]['arg1'] = $cust_header;
469                                $this->form['tests'][$i]['arg2'] = $target;
470
471                                if ($target == '')
472                                    $this->errors['tests'][$i]['target'] = $this->gettext('cannotbeempty');
473                                else if (preg_match('/^(value|count)-/', $type) && !preg_match('/[0-9]+/', $target))
474                                    $this->errors['tests'][$i]['target'] = $this->gettext('forbiddenchars');
475                            }
476                            break;
477                        }
478                    $i++;
479                }
480            }
481
482            $i = 0;
483            // actions
484            foreach($act_types as $idx => $type) {
485                $type   = $this->strip_value($type);
486                $target = $this->strip_value($act_targets[$idx]);
487
488                switch ($type) {
489
490                case 'fileinto':
491                case 'fileinto_copy':
492                    $mailbox = $this->strip_value($mailboxes[$idx]);
493                    $this->form['actions'][$i]['target'] = $mailbox;
494                    if ($type == 'fileinto_copy') {
495                        $type = 'fileinto';
496                        $this->form['actions'][$i]['copy'] = true;
497                    }
498                    break;
499
500                case 'reject':
501                case 'ereject':
502                    $target = $this->strip_value($area_targets[$idx]);
503                    $this->form['actions'][$i]['target'] = str_replace("\r\n", "\n", $target);
504
505 //                 if ($target == '')
506//                      $this->errors['actions'][$i]['targetarea'] = $this->gettext('cannotbeempty');
507                    break;
508
509                case 'redirect':
510                case 'redirect_copy':
511                    $this->form['actions'][$i]['target'] = $target;
512
513                    if ($this->form['actions'][$i]['target'] == '')
514                        $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty');
515                    else if (!check_email($this->form['actions'][$i]['target']))
516                        $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning');
517
518                    if ($type == 'redirect_copy') {
519                        $type = 'redirect';
520                        $this->form['actions'][$i]['copy'] = true;
521                    }
522                    break;
523
524                case 'addflag':
525                case 'setflag':
526                case 'removeflag':
527                    $_target = array();
528                    if (empty($flags[$idx])) {
529                        $this->errors['actions'][$i]['target'] = $this->gettext('noflagset');
530                    }
531                    else {
532                        foreach ($flags[$idx] as $flag) {
533                            $_target[] = $this->strip_value($flag);
534                        }
535                    }
536                    $this->form['actions'][$i]['target'] = $_target;
537                    if (in_array('imap4flags', $this->exts)) {
538                        $this->form['actions'][$i]['mode'] = 'imap4flags';
539                    }
540                    break;
541
542                case 'vacation':
543                    $reason = $this->strip_value($reasons[$idx]);
544                    $this->form['actions'][$i]['reason']    = str_replace("\r\n", "\n", $reason);
545                    $this->form['actions'][$i]['days']      = $days[$idx];
546                    $this->form['actions'][$i]['subject']   = $subject[$idx];
547                    $this->form['actions'][$i]['addresses'] = explode(',', $addresses[$idx]);
548// @TODO: vacation :mime, :from, :handle
549
550                    if ($this->form['actions'][$i]['addresses']) {
551                        foreach($this->form['actions'][$i]['addresses'] as $aidx => $address) {
552                            $address = trim($address);
553                            if (!$address)
554                                unset($this->form['actions'][$i]['addresses'][$aidx]);
555                            else if(!check_email($address)) {
556                                $this->errors['actions'][$i]['addresses'] = $this->gettext('noemailwarning');
557                                break;
558                            } else
559                                $this->form['actions'][$i]['addresses'][$aidx] = $address;
560                        }
561                    }
562
563                    if ($this->form['actions'][$i]['reason'] == '')
564                        $this->errors['actions'][$i]['reason'] = $this->gettext('cannotbeempty');
565                    if ($this->form['actions'][$i]['days'] && !preg_match('/^[0-9]+$/', $this->form['actions'][$i]['days']))
566                        $this->errors['actions'][$i]['days'] = $this->gettext('forbiddenchars');
567                    break;
568                }
569
570                $this->form['actions'][$i]['type'] = $type;
571                $i++;
572            }
573
574            if (!$this->errors) {
575                // zapis skryptu
576                if (!isset($this->script[$fid])) {
577                    $fid = $this->sieve->script->add_rule($this->form);
578                    $new = true;
579                } else
580                    $fid = $this->sieve->script->update_rule($fid, $this->form);
581
582                if ($fid !== false)
583                    $save = $this->sieve->save();
584
585                if ($save && $fid !== false) {
586                    $this->rc->output->show_message('managesieve.filtersaved', 'confirmation');
587                    $this->rc->output->add_script(
588                        sprintf("rcmail.managesieve_updatelist('%s', '%s', %d, %d);",
589                            isset($new) ? 'add' : 'update', Q($this->form['name']),
590                            $fid, $this->form['disabled']),
591                        'foot');
592                }
593                else {
594                    $this->rc->output->show_message('managesieve.filtersaveerror', 'error');
595//                  $this->rc->output->send();
596                }
597            }
598        }
599
600        $this->managesieve_send();
601    }
602
603    private function managesieve_send()
604    {
605        // Handle form action
606        if (isset($_GET['_framed']) || isset($_POST['_framed'])) {
607            if (isset($_GET['_newset']) || isset($_POST['_newset'])) {
608                $this->rc->output->send('managesieve.setedit');
609            }
610            else {
611                $this->rc->output->send('managesieve.filteredit');
612            }
613        } else {
614            $this->rc->output->set_pagetitle($this->gettext('filters'));
615            $this->rc->output->send('managesieve.managesieve');
616        }
617    }
618
619    // return the filters list as HTML table
620    function filters_list($attrib)
621    {
622        // add id to message list table if not specified
623        if (!strlen($attrib['id']))
624            $attrib['id'] = 'rcmfilterslist';
625
626        // define list of cols to be displayed
627        $a_show_cols = array('managesieve.filtername');
628
629        foreach($this->script as $idx => $filter)
630            $result[] = array(
631                'managesieve.filtername' => $filter['name'],
632                'id' => $idx,
633                'class' => $filter['disabled'] ? 'disabled' : '',
634            );
635
636        // create XHTML table
637        $out = rcube_table_output($attrib, $result, $a_show_cols, 'id');
638
639        // set client env
640        $this->rc->output->add_gui_object('filterslist', $attrib['id']);
641        $this->rc->output->include_script('list.js');
642
643        // add some labels to client
644        $this->rc->output->add_label('managesieve.filterdeleteconfirm');
645
646        return $out;
647    }
648
649    // return the filters list as <SELECT>
650    function filtersets_list($attrib)
651    {
652        // add id to message list table if not specified
653        if (!strlen($attrib['id']))
654            $attrib['id'] = 'rcmfiltersetslist';
655
656        $list = $this->sieve->get_scripts();
657        $active = $this->sieve->get_active();
658
659        $select = new html_select(array('name' => '_set', 'id' => $attrib['id'],
660            'onchange' => 'rcmail.managesieve_set()'));
661
662        if ($list) {
663            asort($list, SORT_LOCALE_STRING);
664
665            foreach ($list as $set)
666                $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set);
667        }
668
669        $out = $select->show($this->sieve->current);
670
671        // set client env
672        $this->rc->output->add_gui_object('filtersetslist', $attrib['id']);
673        $this->rc->output->add_label(
674            'managesieve.setdeleteconfirm',
675            'managesieve.active',
676            'managesieve.filtersetact',
677            'managesieve.filtersetdeact'
678        );
679
680        return $out;
681    }
682
683    function filter_frame($attrib)
684    {
685        if (!$attrib['id'])
686            $attrib['id'] = 'rcmfilterframe';
687
688        $attrib['name'] = $attrib['id'];
689
690        $this->rc->output->set_env('contentframe', $attrib['name']);
691        $this->rc->output->set_env('blankpage', $attrib['src'] ?
692        $this->rc->output->abs_url($attrib['src']) : 'program/blank.gif');
693
694        return html::tag('iframe', $attrib);
695    }
696
697    function filterset_form($attrib)
698    {
699        if (!$attrib['id'])
700            $attrib['id'] = 'rcmfiltersetform';
701
702        $out = '<form name="filtersetform" action="./" method="post" enctype="multipart/form-data">'."\n";
703
704        $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
705        $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
706        $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
707        $hiddenfields->add(array('name' => '_newset', 'value' => 1));
708
709        $out .= $hiddenfields->show();
710
711        $name     = get_input_value('_name', RCUBE_INPUT_POST);
712        $copy     = get_input_value('_copy', RCUBE_INPUT_POST);
713        $selected = get_input_value('_from', RCUBE_INPUT_POST);
714
715        // filter set name input
716        $input_name = new html_inputfield(array('name' => '_name', 'id' => '_name', 'size' => 30,
717            'class' => ($this->errors['name'] ? 'error' : '')));
718
719        $out .= sprintf('<label for="%s"><b>%s:</b></label> %s<br /><br />',
720            '_name', Q($this->gettext('filtersetname')), $input_name->show($name));
721
722        $out .="\n<fieldset class=\"itemlist\"><legend>" . $this->gettext('filters') . ":</legend>\n";
723        $out .= '<input type="radio" id="from_none" name="_from" value="none"'
724            .(!$selected || $selected=='none' ? ' checked="checked"' : '').'></input>';
725        $out .= sprintf('<label for="%s">%s</label> ', 'from_none', Q($this->gettext('none')));
726
727        // filters set list
728        $list   = $this->sieve->get_scripts();
729        $active = $this->sieve->get_active();
730
731        $select = new html_select(array('name' => '_copy', 'id' => '_copy'));
732
733        if (is_array($list)) {
734            asort($list, SORT_LOCALE_STRING);
735
736            foreach ($list as $set)
737                $select->add($set . ($set == $active ? ' ('.$this->gettext('active').')' : ''), $set);
738
739            $out .= '<br /><input type="radio" id="from_set" name="_from" value="set"'
740                .($selected=='set' ? ' checked="checked"' : '').'></input>';
741            $out .= sprintf('<label for="%s">%s:</label> ', 'from_set', Q($this->gettext('fromset')));
742            $out .= $select->show($copy);
743        }
744
745        // script upload box
746        $upload = new html_inputfield(array('name' => '_file', 'id' => '_file', 'size' => 30,
747            'type' => 'file', 'class' => ($this->errors['name'] ? 'error' : '')));
748
749        $out .= '<br /><input type="radio" id="from_file" name="_from" value="file"'
750            .($selected=='file' ? ' checked="checked"' : '').'></input>';
751        $out .= sprintf('<label for="%s">%s:</label> ', 'from_file', Q($this->gettext('fromfile')));
752        $out .= $upload->show();
753        $out .= '</fieldset>';
754
755        $this->rc->output->add_gui_object('sieveform', 'filtersetform');
756
757        return $out;
758    }
759
760
761    function filter_form($attrib)
762    {
763        if (!$attrib['id'])
764            $attrib['id'] = 'rcmfilterform';
765
766        $fid = get_input_value('_fid', RCUBE_INPUT_GPC);
767        $scr = isset($this->form) ? $this->form : $this->script[$fid];
768
769        $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $this->rc->task));
770        $hiddenfields->add(array('name' => '_action', 'value' => 'plugin.managesieve-save'));
771        $hiddenfields->add(array('name' => '_framed', 'value' => ($_POST['_framed'] || $_GET['_framed'] ? 1 : 0)));
772        $hiddenfields->add(array('name' => '_fid', 'value' => $fid));
773
774        $out = '<form name="filterform" action="./" method="post">'."\n";
775        $out .= $hiddenfields->show();
776
777        // 'any' flag
778        if (sizeof($scr['tests']) == 1 && $scr['tests'][0]['test'] == 'true' && !$scr['tests'][0]['not'])
779            $any = true;
780
781        // filter name input
782        $field_id = '_name';
783        $input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'size' => 30,
784            'class' => ($this->errors['name'] ? 'error' : '')));
785
786        if ($this->errors['name'])
787            $this->add_tip($field_id, $this->errors['name'], true);
788
789        if (isset($scr))
790            $input_name = $input_name->show($scr['name']);
791        else
792            $input_name = $input_name->show();
793
794        $out .= sprintf("\n<label for=\"%s\"><b>%s:</b></label> %s<br /><br />\n",
795            $field_id, Q($this->gettext('filtername')), $input_name);
796
797        $out .= '<fieldset><legend>' . Q($this->gettext('messagesrules')) . "</legend>\n";
798
799        // any, allof, anyof radio buttons
800        $field_id = '_allof';
801        $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'allof',
802            'onclick' => 'rule_join_radio(\'allof\')', 'class' => 'radio'));
803
804        if (isset($scr) && !$any)
805            $input_join = $input_join->show($scr['join'] ? 'allof' : '');
806        else
807            $input_join = $input_join->show();
808
809        $out .= sprintf("%s<label for=\"%s\">%s</label>&nbsp;\n",
810            $input_join, $field_id, Q($this->gettext('filterallof')));
811
812        $field_id = '_anyof';
813        $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'anyof',
814            'onclick' => 'rule_join_radio(\'anyof\')', 'class' => 'radio'));
815
816        if (isset($scr) && !$any)
817            $input_join = $input_join->show($scr['join'] ? '' : 'anyof');
818        else
819            $input_join = $input_join->show('anyof'); // default
820
821        $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
822            $input_join, $field_id, Q($this->gettext('filteranyof')));
823
824        $field_id = '_any';
825        $input_join = new html_radiobutton(array('name' => '_join', 'id' => $field_id, 'value' => 'any',
826            'onclick' => 'rule_join_radio(\'any\')', 'class' => 'radio'));
827
828        $input_join = $input_join->show($any ? 'any' : '');
829
830        $out .= sprintf("%s<label for=\"%s\">%s</label>\n",
831            $input_join, $field_id, Q($this->gettext('filterany')));
832
833        $rows_num = isset($scr) ? sizeof($scr['tests']) : 1;
834
835        $out .= '<div id="rules"'.($any ? ' style="display: none"' : '').'>';
836        for ($x=0; $x<$rows_num; $x++)
837            $out .= $this->rule_div($fid, $x);
838        $out .= "</div>\n";
839
840        $out .= "</fieldset>\n";
841
842        // actions
843        $out .= '<fieldset><legend>' . Q($this->gettext('messagesactions')) . "</legend>\n";
844
845        $rows_num = isset($scr) ? sizeof($scr['actions']) : 1;
846
847        $out .= '<div id="actions">';
848        for ($x=0; $x<$rows_num; $x++)
849            $out .= $this->action_div($fid, $x);
850        $out .= "</div>\n";
851
852        $out .= "</fieldset>\n";
853
854        $this->print_tips();
855
856        if ($scr['disabled']) {
857            $this->rc->output->set_env('rule_disabled', true);
858        }
859        $this->rc->output->add_label(
860            'managesieve.ruledeleteconfirm',
861            'managesieve.actiondeleteconfirm'
862        );
863        $this->rc->output->add_gui_object('sieveform', 'filterform');
864
865        return $out;
866    }
867
868    function rule_div($fid, $id, $div=true)
869    {
870        $rule     = isset($this->form) ? $this->form['tests'][$id] : $this->script[$fid]['tests'][$id];
871        $rows_num = isset($this->form) ? sizeof($this->form['tests']) : sizeof($this->script[$fid]['tests']);
872
873        $out = $div ? '<div class="rulerow" id="rulerow' .$id .'">'."\n" : '';
874
875        $out .= '<table><tr><td class="rowactions">';
876
877        // headers select
878        $select_header = new html_select(array('name' => "_header[]", 'id' => 'header'.$id,
879            'onchange' => 'rule_header_select(' .$id .')'));
880        foreach($this->headers as $name => $val)
881            $select_header->add(Q($this->gettext($name)), Q($val));
882        $select_header->add(Q($this->gettext('size')), 'size');
883        $select_header->add(Q($this->gettext('...')), '...');
884
885        // TODO: list arguments
886
887        if ((isset($rule['test']) && $rule['test'] == 'header')
888            && !is_array($rule['arg1']) && in_array($rule['arg1'], $this->headers))
889            $out .= $select_header->show($rule['arg1']);
890        else if ((isset($rule['test']) && $rule['test'] == 'exists')
891            && !is_array($rule['arg']) && in_array($rule['arg'], $this->headers))
892            $out .= $select_header->show($rule['arg']);
893        else if (isset($rule['test']) && $rule['test'] == 'size')
894            $out .= $select_header->show('size');
895        else if (isset($rule['test']) && $rule['test'] != 'true')
896            $out .= $select_header->show('...');
897        else
898            $out .= $select_header->show();
899
900        $out .= '</td><td class="rowtargets">';
901
902        if ((isset($rule['test']) && $rule['test'] == 'header')
903            && (is_array($rule['arg1']) || !in_array($rule['arg1'], $this->headers)))
904            $custom = is_array($rule['arg1']) ? implode(', ', $rule['arg1']) : $rule['arg1'];
905        else if ((isset($rule['test']) && $rule['test'] == 'exists')
906            && (is_array($rule['arg']) || !in_array($rule['arg'], $this->headers)))
907            $custom = is_array($rule['arg']) ? implode(', ', $rule['arg']) : $rule['arg'];
908
909        $out .= '<div id="custom_header' .$id. '" style="display:' .(isset($custom) ? 'inline' : 'none'). '">
910            <input type="text" name="_custom_header[]" id="custom_header_i'.$id.'" '
911            . $this->error_class($id, 'test', 'header', 'custom_header_i')
912            .' value="' .Q($custom). '" size="20" />&nbsp;</div>' . "\n";
913
914        // matching type select (operator)
915        $select_op = new html_select(array('name' => "_rule_op[]", 'id' => 'rule_op'.$id,
916            'style' => 'display:' .($rule['test']!='size' ? 'inline' : 'none'),
917            'onchange' => 'rule_op_select('.$id.')'));
918        $select_op->add(Q($this->gettext('filtercontains')), 'contains');
919        $select_op->add(Q($this->gettext('filternotcontains')), 'notcontains');
920        $select_op->add(Q($this->gettext('filteris')), 'is');
921        $select_op->add(Q($this->gettext('filterisnot')), 'notis');
922        $select_op->add(Q($this->gettext('filterexists')), 'exists');
923        $select_op->add(Q($this->gettext('filternotexists')), 'notexists');
924        $select_op->add(Q($this->gettext('filtermatches')), 'matches');
925        $select_op->add(Q($this->gettext('filternotmatches')), 'notmatches');
926                if (in_array('regex', $this->exts)) {
927            $select_op->add(Q($this->gettext('filterregex')), 'regex');
928            $select_op->add(Q($this->gettext('filternotregex')), 'notregex');
929        }
930                if (in_array('relational', $this->exts)) {
931                        $select_op->add(Q($this->gettext('countisgreaterthan')), 'count-gt');
932                        $select_op->add(Q($this->gettext('countisgreaterthanequal')), 'count-ge');
933                        $select_op->add(Q($this->gettext('countislessthan')), 'count-lt');
934                        $select_op->add(Q($this->gettext('countislessthanequal')), 'count-le');
935                        $select_op->add(Q($this->gettext('countequals')), 'count-eq');
936                        $select_op->add(Q($this->gettext('countnotequals')), 'count-ne');
937                        $select_op->add(Q($this->gettext('valueisgreaterthan')), 'value-gt');
938                        $select_op->add(Q($this->gettext('valueisgreaterthanequal')), 'value-ge');
939                        $select_op->add(Q($this->gettext('valueislessthan')), 'value-lt');
940                        $select_op->add(Q($this->gettext('valueislessthanequal')), 'value-le');
941                        $select_op->add(Q($this->gettext('valueequals')), 'value-eq');
942                        $select_op->add(Q($this->gettext('valuenotequals')), 'value-ne');
943                }
944
945        // target input (TODO: lists)
946
947        if ($rule['test'] == 'header') {
948            $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['type']);
949            $target = $rule['arg2'];
950        }
951        else if ($rule['test'] == 'size') {
952            $out .= $select_op->show();
953            if (preg_match('/^([0-9]+)(K|M|G)*$/', $rule['arg'], $matches)) {
954                $sizetarget = $matches[1];
955                $sizeitem = $matches[2];
956            }
957        }
958        else {
959            $out .= $select_op->show(($rule['not'] ? 'not' : '').$rule['test']);
960            $target = '';
961        }
962
963        $out .= '<input type="text" name="_rule_target[]" id="rule_target' .$id. '"
964            value="' .Q($target). '" size="20" ' . $this->error_class($id, 'test', 'target', 'rule_target')
965            . ' style="display:' . ($rule['test']!='size' && $rule['test'] != 'exists' ? 'inline' : 'none') . '" />'."\n";
966
967        $select_size_op = new html_select(array('name' => "_rule_size_op[]", 'id' => 'rule_size_op'.$id));
968        $select_size_op->add(Q($this->gettext('filterunder')), 'under');
969        $select_size_op->add(Q($this->gettext('filterover')), 'over');
970
971        $out .= '<div id="rule_size' .$id. '" style="display:' . ($rule['test']=='size' ? 'inline' : 'none') .'">';
972        $out .= $select_size_op->show($rule['test']=='size' ? $rule['type'] : '');
973        $out .= '<input type="text" name="_rule_size_target[]" id="rule_size_i'.$id.'" value="'.$sizetarget.'" size="10" ' 
974            . $this->error_class($id, 'test', 'sizetarget', 'rule_size_i') .' />
975            <input type="radio" name="_rule_size_item['.$id.']" value=""'
976                . (!$sizeitem ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('B').'
977            <input type="radio" name="_rule_size_item['.$id.']" value="K"'
978                . ($sizeitem=='K' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('KB').'
979            <input type="radio" name="_rule_size_item['.$id.']" value="M"'
980                . ($sizeitem=='M' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('MB').'
981            <input type="radio" name="_rule_size_item['.$id.']" value="G"'
982                . ($sizeitem=='G' ? ' checked="checked"' : '') .' class="radio" />'.rcube_label('GB');
983        $out .= '</div>';
984        $out .= '</td>';
985
986        // add/del buttons
987        $out .= '<td class="rowbuttons">';
988        $out .= '<input type="button" id="ruleadd' . $id .'" value="'. Q($this->gettext('add')). '"
989            onclick="rcmail.managesieve_ruleadd(' . $id .')" class="button" /> ';
990        $out .= '<input type="button" id="ruledel' . $id .'" value="'. Q($this->gettext('del')). '"
991            onclick="rcmail.managesieve_ruledel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"'
992            . ($rows_num<2 ? ' disabled="disabled"' : '') .' />';
993        $out .= '</td></tr></table>';
994
995        $out .= $div ? "</div>\n" : '';
996
997        return $out;
998    }
999
1000    function action_div($fid, $id, $div=true)
1001    {
1002        $action   = isset($this->form) ? $this->form['actions'][$id] : $this->script[$fid]['actions'][$id];
1003        $rows_num = isset($this->form) ? sizeof($this->form['actions']) : sizeof($this->script[$fid]['actions']);
1004
1005        $out = $div ? '<div class="actionrow" id="actionrow' .$id .'">'."\n" : '';
1006
1007        $out .= '<table><tr><td class="rowactions">';
1008
1009        // action select
1010        $select_action = new html_select(array('name' => "_action_type[]", 'id' => 'action_type'.$id,
1011            'onchange' => 'action_type_select(' .$id .')'));
1012        if (in_array('fileinto', $this->exts))
1013            $select_action->add(Q($this->gettext('messagemoveto')), 'fileinto');
1014        if (in_array('fileinto', $this->exts) && in_array('copy', $this->exts))
1015            $select_action->add(Q($this->gettext('messagecopyto')), 'fileinto_copy');
1016        $select_action->add(Q($this->gettext('messageredirect')), 'redirect');
1017        if (in_array('copy', $this->exts))
1018            $select_action->add(Q($this->gettext('messagesendcopy')), 'redirect_copy');
1019        if (in_array('reject', $this->exts))
1020            $select_action->add(Q($this->gettext('messagediscard')), 'reject');
1021        else if (in_array('ereject', $this->exts))
1022            $select_action->add(Q($this->gettext('messagediscard')), 'ereject');
1023        if (in_array('vacation', $this->exts))
1024            $select_action->add(Q($this->gettext('messagereply')), 'vacation');
1025        $select_action->add(Q($this->gettext('messagedelete')), 'discard');
1026        if (in_array('imapflags', $this->exts) || in_array('imap4flags', $this->exts)) {
1027            $select_action->add(Q($this->gettext('setflags')), 'setflag');
1028            $select_action->add(Q($this->gettext('addflags')), 'addflag');
1029            $select_action->add(Q($this->gettext('removeflags')), 'removeflag');
1030        }
1031        $select_action->add(Q($this->gettext('rulestop')), 'stop');
1032
1033        $select_type = $action['type'];
1034        if (in_array($action['type'], array('fileinto', 'redirect')) && $action['copy']) {
1035            $select_type .= '_copy';
1036        }
1037
1038        $out .= $select_action->show($select_type);
1039        $out .= '</td>';
1040
1041        // actions target inputs
1042        $out .= '<td class="rowtargets">';
1043        // shared targets
1044        $out .= '<input type="text" name="_action_target[]" id="action_target' .$id. '" '
1045            .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="40" '
1046            .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" '
1047            . $this->error_class($id, 'action', 'target', 'action_target') .' />';
1048        $out .= '<textarea name="_action_target_area[]" id="action_target_area' .$id. '" '
1049            .'rows="3" cols="40" '. $this->error_class($id, 'action', 'targetarea', 'action_target_area')
1050            .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">'
1051            . (in_array($action['type'], array('reject', 'ereject')) ? Q($action['target'], 'strict', false) : '')
1052            . "</textarea>\n";
1053
1054        // vacation
1055        $out .= '<div id="action_vacation' .$id.'" style="display:' .($action['type']=='vacation' ? 'inline' : 'none') .'">';
1056        $out .= '<span class="label">'. Q($this->gettext('vacationreason')) .'</span><br />'
1057            .'<textarea name="_action_reason[]" id="action_reason' .$id. '" '
1058            .'rows="3" cols="45" '. $this->error_class($id, 'action', 'reason', 'action_reason') . '>'
1059            . Q($action['reason'], 'strict', false) . "</textarea>\n";
1060        $out .= '<br /><span class="label">' .Q($this->gettext('vacationsubject')) . '</span><br />'
1061            .'<input type="text" name="_action_subject[]" id="action_subject'.$id.'" '
1062            .'value="' . (is_array($action['subject']) ? Q(implode(', ', $action['subject']), 'strict', false) : $action['subject']) . '" size="50" '
1063            . $this->error_class($id, 'action', 'subject', 'action_subject') .' />';
1064        $out .= '<br /><span class="label">' .Q($this->gettext('vacationaddresses')) . '</span><br />'
1065            .'<input type="text" name="_action_addresses[]" id="action_addr'.$id.'" '
1066            .'value="' . (is_array($action['addresses']) ? Q(implode(', ', $action['addresses']), 'strict', false) : $action['addresses']) . '" size="50" '
1067            . $this->error_class($id, 'action', 'addresses', 'action_addr') .' />';
1068        $out .= '<br /><span class="label">' . Q($this->gettext('vacationdays')) . '</span><br />'
1069            .'<input type="text" name="_action_days[]" id="action_days'.$id.'" '
1070            .'value="' .Q($action['days'], 'strict', false) . '" size="2" '
1071            . $this->error_class($id, 'action', 'days', 'action_days') .' />';
1072        $out .= '</div>';
1073
1074        // flags
1075        $flags = array(
1076            'read'      => '\\\\Seen',
1077            'answered'  => '\\\\Answered',
1078            'flagged'   => '\\\\Flagged',
1079            'deleted'   => '\\\\Deleted',
1080            'draft'     => '\\\\Draft',
1081        );
1082        $action['target'] = (array)$action['target'];
1083        $out .= '<div id="action_flags' .$id.'" style="display:' 
1084            . (preg_match('/^(set|add|remove)flag$/', $action['type']) ? 'inline' : 'none') . '"'
1085            . $this->error_class($id, 'action', 'flags', 'action_flags') . '>';
1086        foreach ($flags as $fidx => $flag) {
1087            $out .= '<nobr><input type="checkbox" name="_action_flags[' .$id .'][]" value="' . $flag . '"'
1088                . (in_array_nocase($flag, $action['target']) ? 'checked="checked"' : '') . ' />'
1089                . Q($this->gettext('flag'.$fidx)) .'</nobr> ';
1090        }
1091        $out .= '</div>';
1092
1093        // mailbox select
1094        $out .= '<select id="action_mailbox' .$id. '" name="_action_mailbox[]" style="display:'
1095            .(!isset($action) || $action['type']=='fileinto' ? 'inline' : 'none'). '">';
1096
1097        $this->rc->imap_connect();
1098
1099        $a_folders = $this->rc->imap->list_mailboxes();
1100        $delimiter = $this->rc->imap->get_hierarchy_delimiter();
1101
1102        // set mbox encoding
1103        $mbox_encoding = $this->rc->config->get('managesieve_mbox_encoding', 'UTF7-IMAP');
1104
1105        if ($action['type'] == 'fileinto')
1106            $mailbox = $action['target'];
1107        else
1108            $mailbox = '';
1109
1110        foreach ($a_folders as $folder) {
1111            $utf7folder = $this->rc->imap->mod_mailbox($folder);
1112            $names = explode($delimiter, rcube_charset_convert($folder, 'UTF7-IMAP'));
1113            $name  = $names[sizeof($names)-1];
1114
1115            if ($replace_delimiter = $this->rc->config->get('managesieve_replace_delimiter'))
1116                $utf7folder = str_replace($delimiter, $replace_delimiter, $utf7folder);
1117
1118            // convert to Sieve implementation encoding
1119            $utf7folder = $this->mbox_encode($utf7folder, $mbox_encoding);
1120
1121            if ($folder_class = rcmail_folder_classname($name))
1122                $foldername = $this->gettext($folder_class);
1123            else
1124                $foldername = $name;
1125
1126            $out .= sprintf('<option value="%s"%s>%s%s</option>'."\n",
1127                htmlspecialchars($utf7folder),
1128                ($mailbox == $utf7folder ? ' selected="selected"' : ''),
1129                str_repeat('&nbsp;', 4 * (sizeof($names)-1)),
1130                Q(abbreviate_string($foldername, 40 - (2 * sizeof($names)-1))));
1131        }
1132        $out .= '</select>';
1133        $out .= '</td>';
1134
1135        // add/del buttons
1136        $out .= '<td class="rowbuttons">';
1137        $out .= '<input type="button" id="actionadd' . $id .'" value="'. Q($this->gettext('add')). '"
1138            onclick="rcmail.managesieve_actionadd(' . $id .')" class="button" /> ';
1139        $out .= '<input type="button" id="actiondel' . $id .'" value="'. Q($this->gettext('del')). '"
1140            onclick="rcmail.managesieve_actiondel(' . $id .')" class="button' . ($rows_num<2 ? ' disabled' : '') .'"'
1141            . ($rows_num<2 ? ' disabled="disabled"' : '') .' />';
1142        $out .= '</td>';
1143
1144        $out .= '</tr></table>';
1145
1146        $out .= $div ? "</div>\n" : '';
1147
1148        return $out;
1149    }
1150
1151    private function genid()
1152    {
1153        $result = intval(rcube_timer());
1154        return $result;
1155    }
1156
1157    private function strip_value($str, $allow_html=false)
1158    {
1159        if (!$allow_html)
1160            $str = strip_tags($str);
1161
1162        return trim($str);
1163    }
1164
1165    private function error_class($id, $type, $target, $elem_prefix='')
1166    {
1167        // TODO: tooltips
1168        if (($type == 'test' && ($str = $this->errors['tests'][$id][$target])) ||
1169            ($type == 'action' && ($str = $this->errors['actions'][$id][$target]))
1170        ) {
1171            $this->add_tip($elem_prefix.$id, $str, true);
1172            return ' class="error"';
1173        }
1174
1175        return '';
1176    }
1177
1178    private function mbox_encode($text, $encoding)
1179    {
1180        return rcube_charset_convert($text, 'UTF7-IMAP', $encoding);
1181    }
1182
1183    private function add_tip($id, $str, $error=false)
1184    {
1185        if ($error)
1186            $str = html::span('sieve error', $str);
1187
1188        $this->tips[] = array($id, $str);
1189    }
1190
1191    private function print_tips()
1192    {
1193        if (empty($this->tips))
1194            return;
1195
1196        $script = JS_OBJECT_NAME.'.managesieve_tip_register('.json_encode($this->tips).');';
1197        $this->rc->output->add_script($script, 'foot');
1198    }
1199
1200}
Note: See TracBrowser for help on using the repository browser.