File: class_mail-methods-cyrus.inc

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (666 lines) | stat: -rw-r--r-- 23,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?php

/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003-2010  Cajus Pollmeier
  Copyright (C) 2011-2013  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

class mailMethodCyrus extends mailMethod
{

  protected $ServerList   = array();
  protected $imap_handle  = NULL;
  protected $quota_loaded = FALSE;

  /* Allow modification of account_ids for existing mail accounts */
  protected $modifyableMail   = FALSE;

  /* Allow modification of the mail server attribute existing mail accounts */
  protected $modifyableServer = FALSE;

  /* Enforces same value for 'mail' as used for 'cn' */
  protected $mailEqualsCN   = FALSE;

  protected $enableDomainSelection  = FALSE;
  protected $enableQuota            = TRUE;
  protected $enableSieveManager     = TRUE;
  protected $enableVacationRange    = TRUE;
  protected $enableFolderTypes      = FALSE;

  protected function init()
  {
    mailMethod::init();
    if (isset($this->config->data['SERVERS']['IMAP'])) {
      $this->ServerList = $this->config->data['SERVERS']['IMAP'];
    }
  }


  public function connect()
  {
    mailMethod::connect();

    if (!count($this->ServerList)) {
      $this->error = _("There are no IMAP compatible mail servers defined!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
          "<b>IMAP: No mail servers configured, check systems->server->service->imap.</b>", "");
      return FALSE;
    } elseif (!isset($this->ServerList[$this->parent->gosaMailServer])) {
      $this->error = _("Mail server for this account is invalid!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
          "<b>IMAP: The selected mail server '".$this->parent->gosaMailServer."' is invalid.</b>", "");
      return FALSE;
    } else {
      $cfg = $this->ServerList[$this->parent->gosaMailServer];
    }

    /* For some reason, hiding errors with @ does not wor here... */
    if (!isset($cfg['connect'])) {
      $cfg['connect']   = "";
    }
    if (!isset($cfg['admin'])) {
      $cfg['admin']     = "";
    }
    if (!isset($cfg['password'])) {
      $cfg['password']  = "";
    }

    /* Setting connect timeout to 10 seconds,
        else the FusionDirectory UI may freeze for 60 seconds.
       (PHP default is 'default_socket_timeout = 60') */
    $timeout = $this->config->get_cfg_value("imapTimeout", 10);
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $timeout,
          "<b>IMAP: Setting imap connect timeout to</b> (seconds)");
    imap_timeout(1, $timeout);

    $this->imap_handle = @imap_open($cfg['connect'], $cfg['admin'], $cfg['password'], OP_HALFOPEN);

    /* Mailbox reachable? */
    if ($this->imap_handle === FALSE) {
      $this->error = imap_last_error();

      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>Failed</b> :".imap_last_error(),
        "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);

      return FALSE;
      $this->connected = FALSE;
    }
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>successful</b>",
        "<b>IMAP:</b> ".$cfg['admin']."@".$cfg['connect']);
    $this->connected = TRUE;

    return TRUE;
  }


  public function account_exists()
  {
    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return array();
    }

    /* Get server config */
    $cfg  = $this->ServerList[$this->parent->gosaMailServer];
    $list = @imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
    $res  = (is_array($list) && count($list));
    if ($res) {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "", "<b>IMAP: Account exists in imap server.</b>");
    } else {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "", "<b>IMAP: Account seems NOT to exists in imap server.</b>");
    }
    return $res;
  }

  public function disconnect()
  {
    mailMethod::disconnect();
    if ($this->is_connected()) {
      @imap_close ($this->imap_handle);
    }
  }


  public function is_connected()
  {
    $ret = mailMethod::is_connected();
    return ($ret && $this->imap_handle);
  }

  protected function loadQuota()
  {
    if (!$this->quotaEnabled()) {
      return TRUE;
    }
    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return FALSE;
    }

    $this->reset_error();

    /* Load quota settings */
    $result = array("quotaUsage" => "","gosaMailQuota" => "");
    $quota_value = @imap_get_quota($this->imap_handle, $this->account_id);

    /* Reset error queue, imap_qet_quota() will fail if the quota wasn't set yet.
     */
    imap_errors();

    if (is_array($quota_value) && count($quota_value)) {
      if (isset($quota_value["STORAGE"]) && is_array($quota_value["STORAGE"])) {

        /* use for PHP >= 4.3 */
        if ($quota_value["STORAGE"]['limit'] == 2147483647) {
          $result['quotaUsage']     = (int) ($quota_value["STORAGE"]['usage'] / 1024);
          $result['gosaMailQuota']  = "";
        } else {
          $result['quotaUsage']     = (int) ($quota_value["STORAGE"]['usage'] / 1024);
          $result['gosaMailQuota']  = (int) ($quota_value["STORAGE"]['limit'] / 1024);
        }
      } else {

        /* backward icompatible */
        if ($quota_value['usage'] == 2147483647) {
          $result['quotaUsage']     = (int) ($quota_value['usage'] / 1024);
          $result['gosaMailQuota']  = "";
        } else {
          $result['quotaUsage']     = (int) ($quota_value['usage'] / 1024);
          $result['gosaMailQuota']  = (int) ($quota_value['limit'] / 1024);
        }
      }
    }
    $this->quotaValue = $result['gosaMailQuota'];
    $this->quotaUsage = $result['quotaUsage'];

    /* Write debug output */
    if (is_array($quota_value)) {
      if ($this->quotaValue == "") {
        $quota = "(".$this->quotaUsage." / unlimited)";
      } else {
        $quota = "(".$this->quotaUsage." / ".$this->quotaValue.")";
      }
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $quota,
          "<b>IMAP: Successfully received account quota</b>");
    } else {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, imap_last_error(),
          "<b>IMAP: Failed to receive account quota</b>");
    }
  }

  public function getQuota($quota)
  {
    mailMethod::getQuota($quota);
    if (!$this->quota_loaded) {
      $this->quota_loaded = TRUE;
      $this->loadQuota();
    }
    return $this->quotaValue;
  }

  public function getQuotaUsage()
  {
    mailMethod::getQuotaUsage();
    if (!$this->quota_loaded) {
      $this->quota_loaded = TRUE;
      $this->loadQuota();
    }
    return $this->quotaUsage;
  }

  public function setQuota($number)
  {
    mailMethod::setQuota($number);

    if (!$this->quotaEnabled()) {
      return TRUE;
    }
    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return FALSE;
    }

    $this->build_account_id();

    /* Workaround for the php imap extension */
    if (($this->quotaValue == "") || ($this->quotaValue == "2147483647")) {
      $this->quotaValue = "2147483647";
    } elseif ($this->quotaValue > 0) {
      $this->quotaValue = $this->quotaValue * 1024;
    }
    $debug_number = $this->quotaValue." KB";
    if ($this->quotaValue == "2147483647") {
      $debug_number .= "<i>Unlimited</i>";
    }

    if (!imap_set_quota($this->imap_handle, $this->account_id, $this->quotaValue)) {
      msg_dialog::display(_("IMAP error"), sprintf(_("Cannot modify IMAP mailbox quota: %s"),
            '<br><br><i>'.imap_last_error().'</i>'), ERROR_DIALOG);
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id.": (".$debug_number.")</b>",
          "<b>IMAP: Set account quota</b> on server '".$this->parent->gosaMailServer."' <b>".imap_last_error()."</b>");
      return FALSE;
    }
    @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id.": (".$debug_number.")</b>",
        "<b>IMAP: Set account quota</b> on server :".$this->parent->gosaMailServer);
    return TRUE;
  }


  public function updateMailbox()
  {
    mailMethod::updateMailbox();

    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return FALSE;
    }

    $this->build_account_id ();
    if ($this->is_connected()) {
      $cfg  = $this->ServerList[$this->parent->gosaMailServer];
      $list = imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
      if ($list === FALSE) {
        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>",
          "<b>IMAP: Add/Update account</b> on server :".$this->parent->gosaMailServer);
        if (!imap_createmailbox($this->imap_handle, $cfg["connect"].$this->account_id)) {
          $this->error = imap_last_error();
          return FALSE;
        }

        /* Autocreate configured default folders */
        $folders = $this->config->get_cfg_value("cyrusAutocreateFolders", NULL);
        if ($folders !== NULL) {
          $cyrus_delim      = ($this->cyrusUseSlashes?"/":".");

          // Walk thru list of specified folders
          foreach ($folders as $folder) {
            @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "<b>".$this->account_id."</b>",
              "<b>IMAP: Add/Update account folder ".$folder."</b> on server :".$this->parent->gosaMailServer);
            if (!imap_createmailbox($this->imap_handle, $cfg["connect"].$this->account_id.$cyrus_delim.$folder)) {
              $this->error = imap_last_error();
              return FALSE;
            }
          }

        }
      }
    }
    return TRUE;
  }


  public function deleteMailbox()
  {
    mailMethod::deleteMailbox();

    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return FALSE;
    }

    $this->build_account_id ();

    $cfg = $this->ServerList[$this->parent->gosaMailServer];
    @imap_setacl ($this->imap_handle, $this->account_id, $cfg["admin"], "lrswipcda");

    if ($this->config->get_cfg_value("cyrusDeleteMailbox", "TRUE") == "TRUE") {
      if (!imap_deletemailbox($this->imap_handle, $cfg["connect"].$this->account_id)) {
        $this->error = imap_last_error();
        return FALSE;
      }
    } else {
      msg_dialog::display(_("Mail info"),
        sprintf(_("LDAP entry has been removed but cyrus mailbox (%s) is kept.\nPlease delete it manually!"),
        $this->account_id), INFO_DIALOG);
    }
    return TRUE;
  }


  public function getMailboxList()
  {
    mailMethod::getMailboxList();

    if (!$this->is_connected() || !$this->imap_handle) {
      trigger_error("Method not connected, catch error.");
      return array();
    }

    $result = array();

    /* Get server config */
    $cfg = $this->ServerList[$this->parent->gosaMailServer];

    /* Create search string
       And prepare replacements
     */
    if (preg_match("/\@/", $this->account_id)) {
      if ($this->cyrusUseSlashes) {
        $search = preg_replace("/\@/", "/*@", $this->account_id);
      } else {
        $search = preg_replace("/\@/", ".*@", $this->account_id);
      }
      $with_domain = TRUE;
    } else {
      if ($this->cyrusUseSlashes) {
        $search = $this->account_id."/*";
      } else {
        $search = $this->account_id.".*";
      }
      $with_domain = FALSE;
    }
    $folder = $this->account_id;
    if (preg_match("/\@/", $folder)) {
      $folder = preg_replace("/\@.*$/", "", $folder);
    }

    /* Contact imap server */
    $list   = @imap_listmailbox($this->imap_handle, $cfg["connect"], $this->account_id);
    $list2  = @imap_listmailbox($this->imap_handle, $cfg["connect"], $search);

    /* Create list of returned folder names */
    if (is_array($list)) {

      /* Merge in subfolders */
      if (is_array($list2)) {
        $list = array_merge($list, $list2);
      }

      foreach ($list as $val) {
        $str = trim(preg_replace("/^\{[^\}]*+\}/", "", $val));
        if ($with_domain) {
          $str = trim(preg_replace("/\@.*$/", "", $str));
        }
        $str = preg_replace ("/^.*".preg_quote($folder, '/')."/", "INBOX",
          mb_convert_encoding($str, "UTF-8", "UTF7-IMAP"));
        $result[] = $str;
      }
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, trim(implode($result, ", "), ", "),
          "<b>IMAP: Received mailbox folders.</b>");
      $this->error = imap_last_error();
    } else {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, imap_last_error(),
          "<b>IMAP: Cannot receive mailbox folders.</b>");
      $this->error = imap_last_error();
      return array();
    }

    /* Append "INBOX" to the folder array if result is empty and request comes from user dialog */
    if (!count($result)) {
      $result[] = "INBOX";
    }

    return $result;
  }


  /*! \brief  Returns configured acls
   */
  public function  getFolderACLs($folder_acls)
  {
    $this->reset_error();

    /* imap_getacl available? */
    if (!function_exists('imap_getacl')) {
      $this->error = _("The module imap_getacl is not implemented!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "The imap_getacl module is missing!",
          "<b>IMAP: Cannot set folder acls.</b>");
      return $folder_acls;
    }

    /* Get ACLs and merge them with the already given acls (ldap)
     */
    $this->build_account_id();
    $acls = imap_getacl ($this->imap_handle, $this->account_id);
    foreach ($acls as $user => $acl) {
      if ($user == "anyone") {
        $user = "__anyone__"; // Map to internal placeholder
      }
      $folder_acls[$user] = $acl;
    }

    /* Merge given ACL with acl mapping
       This ensures that no ACL will accidentally overwritten by fusiondirectory.
     */
    foreach ($folder_acls as $user => $acl) {
      if (!isset($this->acl_mapping[$acl])) {
        $this->acl_mapping[$acl] = $acl;
      }
    }

    return $folder_acls;
  }



  /*! \brief  Write ACLs back to imap or what ever
   */
  public function  setFolderACLs($permissions)
  {
    $this->reset_error();

    /* imap_getacl available? */
    if (!function_exists('imap_getacl')) {
      $this->error = _("The module imap_getacl is not implemented!");
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "The imap_getacl module is missing!",
          "<b>IMAP: Cannot set folder acls.</b>");
      return FALSE;
    }

    /* Get list of subfolders */
    $folders = $this->getMailboxList();
    foreach ($folders as $subfolder) {
      $folder_id = $this->create_folder_id($subfolder);

      /* Remove all acl's for this folder */
      $users = @imap_getacl ($this->imap_handle, $folder_id);

      if (is_array($users)) {
        foreach ($users as $userid => $perms) {
          $userid = strtolower($userid);
          imap_setacl ($this->imap_handle, $folder_id, $userid, "");
          @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $folder_id." -> ".$userid,
              "<b>IMAP: Removing folder permissions.</b>");
        }
      }
    }

    /* Set permissions for this folder */
    foreach ($folders as $subfolder) {
      $folder_id = $this->create_folder_id($subfolder);

      foreach ($permissions as $user => $acl) {
        imap_setacl ($this->imap_handle, $folder_id, $user, $acl);
        @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $folder_id." -> ".$user.": ".$acl,
            "<b>IMAP: Setting new folder permissions.</b>");
      }
    }
    return(TRUE);
  }


  public function saveSieveSettings()
  {
    mailMethod::saveSieveSettings();

    /* Map attribute from parent class
     */
    $mail                     = $this->parent->mail;
    $gosaMailDeliveryMode     = $this->parent->gosaMailDeliveryMode;
    $gosaMailAlternateAddress = $this->parent->gosaMailAlternateAddress;
    $gosaMailMaxSize          = $this->parent->gosaMailMaxSize;
    $gosaSpamMailbox          = $this->parent->gosaSpamMailbox;
    $gosaSpamSortLevel        = $this->parent->gosaSpamSortLevel;
    $gosaVacationMessage      = $this->parent->gosaVacationMessage;

    /* Try to login into sieve
     */
    $cfg    = $this->ServerList[$this->parent->gosaMailServer];
    $sieve  = new sieve($cfg["sieve_server"], $cfg["sieve_port"], $this->getUAttribValue(),
                        $cfg["password"], $cfg["admin"], $cfg["sieve_option"]);
    if (!$sieve->sieve_login()) {
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $sieve->error_raw, "<b>SIEVE: login failed.</b>");
      $this->error = $sieve->error_raw;
      return FALSE;
    }

    /* Build spamlevel. Spamassassin tags mails with "*" for each integer
       point of spam. So a spam level of 5.3 gets "*****" which can be
       checked easily by spam filters */
    $spamlevel = str_pad("", (int) $gosaSpamSortLevel, "*");

    /* Get current sieve script named 'fusiondirectory'.
        Check if it valid ("###FUSIONDIRECTORY" must be the first string).
        If it is valid just replace it, if it is NOT valid
         create a backup of the old
     */
    if ($sieve->sieve_listscripts()) {
      if (in_array('fusiondirectory', $sieve->response)) {
        $script = "";
        if (!$sieve->sieve_getscript('fusiondirectory')) {
          $this->error = sprintf(_('Cannot retrieve SIEVE script: %s'), to_string($sieve->error_raw));
          @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $sieve->error_raw,
              "<b>SIEVE: Connot read 'fusiondirectory' sieve script.</b>");
          $this->error = $sieve->error_raw;
          return FALSE;
        }

        $is_valid_script = FALSE;
        foreach ($sieve->response as $line) {
          if (empty($line)) {
            continue;
          }
          if (preg_match ('/^###FUSIONDIRECTORY/', $line) && strlen($script) == 0) {
            $is_valid_script = TRUE;
          }
          $line   = rtrim($line);
          $script .= $line;
        }

        if ($is_valid_script || strlen($script) == 0 || empty($script)) {
          @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "",
              "<b>SIEVE</b>: Sieve script 'fusiondirectory' was a valid FusionDirectory script and will be replaced.");
        } else {
          $new_name = "non_fusiondirectory_".date("Ymd_H-i-s");
          $sieve->sieve_sendscript($new_name, $script);
          @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, $this->sieve->error_raw,
              "<b>SIEVE</b>: Non FusionDirectory sieve script. <b>Creating backup of the current sieve script '".$new_name."'.</b>");
        }
      }
    }


    /*****
      Build up new sieve script here.
     *****/

    /* Only create a new one, if it is not empty */
    $smarty = get_smarty();
    if (is_integer(strpos($gosaMailDeliveryMode, "R")) ||
        is_integer(strpos($gosaMailDeliveryMode, "C")) ||
        is_integer(strpos($gosaMailDeliveryMode, "I")) ||
        is_integer(strpos($gosaMailDeliveryMode, "V")) ||
        is_integer(strpos($gosaMailDeliveryMode, "S"))) {
      /* Add anti-spam code */
      $smarty->assign('spamfilter', is_integer(strpos($gosaMailDeliveryMode, "S")));
      $smarty->assign('spambox', $gosaSpamMailbox);
      $smarty->assign('spamlevel', $spamlevel);

      /* Add "reject due to mailsize" code, message is currently not
         adjustable through FusionDirectory. */
      $smarty->assign('sizefilter', is_integer(strpos($gosaMailDeliveryMode, "R")));
      $smarty->assign('maxsize', $gosaMailMaxSize);

      /* Add vacation information */
      $smarty->assign('vacation', is_integer(strpos($gosaMailDeliveryMode, "V")));
      if (is_integer(strpos($gosaMailDeliveryMode, "V"))) {
        /* Sieve wants all destination addresses for the
           vacation message, so we've to assemble them from
           mail and mailAlternateAddress */
        $addrlist = '"'.$mail.'"';
        foreach ($gosaMailAlternateAddress as $val) {
          $addrlist .= ", \"$val\"";
        }
        $smarty->assign('addrlist', $addrlist);
        $smarty->assign('vacmsg', addslashes(addslashes($gosaVacationMessage)));
      }

      /* If no local delivery is wanted, tell the script to discard the mail */
      $smarty->assign('dropownmail', is_integer(strpos($gosaMailDeliveryMode, "I")));

      $script = $smarty->fetch(get_template_path('sieve_script.tpl', TRUE, dirname(__FILE__)));
    } else {
      $script = '';
    }
    /****
      Sieve script build complete
     ****/

    /* Upload script and make it the default one */
    if (!$sieve->sieve_sendscript("fusiondirectory", $script)) {
      $this->error = sprintf(_("Cannot store SIEVE script: %s"), to_string($sieve->error_raw));
      @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, "Error was: ".to_string($sieve->error_raw),
        "<b>SIEVE: Writing new Sieve script failed!</b>");
      return FALSE;
    }

    if (!$sieve->sieve_setactivescript("fusiondirectory")) {
      $this->error = sprintf(_("Cannot activate SIEVE script: %s"), to_string($sieve->error_raw));
      return FALSE;
    }

    $sieve->sieve_logout();
  }

  static public function get_server_list($config)
  {
    $serverList = array();

    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    $ldap->search ("(objectClass=fdCyrusServer)",
                  array('cn', 'fdCyrusConnect', 'fdCyrusAdmin', 'fdCyrusPassword', 'fdCyrusSieveServer'));
    while ($attrs = $ldap->fetch()) {

      /* Check if the given fdCyrusSieveServer is in the new style "{cn:port/option}"
         or the old style just "cn".
       */
      if (preg_match("/\{/", $attrs['fdCyrusSieveServer'][0])) {
        sscanf($attrs['fdCyrusSieveServer'][0], '{%[^{}:]:%d%[^{}]}', $sieve_server, $sieve_port, $sieve_option);
      } else {
        $sieve_server = $attrs['fdCyrusSieveServer'][0];
        $sieve_option = "";
        $sieve_port   = "";
      }

      $serverList[$attrs['cn'][0]] = array(
        "server_dn"     => $attrs['dn'],
        "connect"       => $attrs['fdCyrusConnect'][0],
        "admin"         => $attrs['fdCyrusAdmin'][0],
        "password"      => $attrs['fdCyrusPassword'][0],
        "sieve_server"  => $sieve_server,
        "sieve_option"  => $sieve_option,
        "sieve_port"    => $sieve_port
      );
    }

    return $serverList;
  }
}

?>