Code

Updated ldap error msgs to include the object dn
[gosa.git] / plugins / personal / mail / class_mailAccount.inc
1 <?php
2 /*! \brief   mail plugin
3   \author  Cajus Pollmeier <pollmeier@gonicus.de>
4   \version 2.00
5   \date    24.07.2003
7   This class provides the functionality to read and write all attributes
8   relevant for gosaMailAccounts from/to the LDAP. It does syntax checking
9   and displays the formulars required.
10  */
12 /* Load sieve support */
13 require_once ("class_sieve.inc");
15 /* Load mail methods */
16 global $BASE_DIR;
17 get_dir_list("$BASE_DIR/include");
19 class mailAccount extends plugin
20 {
21   /* Definitions */
22   var $plHeadline         = "Mail";
23   var $plDescription      = "This does something";
24   var $method             = "mailMethod";
26   /* CLI vars */
27   var $cli_summary        = "Manage users mail account";
28   var $cli_description    = "Some longer text\nfor help";
29   var $cli_parameters     = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
31   /* plugin specific values */
32   var $mail                               = "";
33   var $uid                                = "";
34   var $gosaMailAlternateAddress           = array();
35   var $gosaMailForwardingAddress          = array();
36   var $gosaMailDeliveryMode               = "[L        ]";
37   var $gosaMailServer                     = "";
38   var $gosaMailQuota                      = "";
39   var $gosaMailMaxSize                    = "";
40   var $gosaVacationMessage                = "";
41   var $gosaSpamSortLevel                  = "";
42   var $gosaSpamMailbox                    = "";
44   var $quotaUsage                         = 0;
45   var $forward_dialog                     = FALSE;
46   var $folder_prefix                      = "";
47   var $mailboxList                        = array();
48   var $default_permissions                = "none";
49   var $member_permissions                 = "post";
50   var $members                            = array();
51   var $admins                             = array();
52   var $vacations                          = array();
53   var $perms                              = array(  "lrs"       => "read", 
54                                                     "lrsp"      => "post", 
55                                                     "lrsip"     => "append",
56                                                     "lrswipcd"  => "write", 
57                                                     "lrswipcda" => "all" );
59   /* attribute list for save action */
60   var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
61       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
62       "gosaVacationMessage", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
63   var $objectclasses= array("gosaMailAccount");
66   /* constructor, if 'dn' is set, the node loads the given
67      'dn' from LDAP */
68   function mailAccount ($config, $dn= NULL)
69   {
70     /* Load bases attributes */
71     plugin::plugin($config, $dn);
73     if(isset($this->attrs['uid'])){
74       $this->uid = $this->attrs['uid'][0];
75     }
76  
77     if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
78       $this->gosaMailServer = $this->gosaMailServer[0];
79     }
81     /* Save initial account state */
82     $this->initially_was_account= $this->is_account;
84     /*  Set mailMethod to the one defined in gosa.conf */
85     if (isset($this->config->current['MAILMETHOD'])){
86       $method= $this->config->current['MAILMETHOD'];
87       if (class_exists("mailMethod$method")){
88         $this->method= "mailMethod$method";
89       } else {
90         print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
91       }
92     }
94     
95     /* Create the account prefix  user. user/ 
96        Preset folder prefix. Will change it later to respect
97        altnamespace. */
98     if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
99       $this->folder_prefix= "user/";
100     } else {
101       $this->folder_prefix= "user.";
102     }
104     
105     /* This is not a new account, parse additional attributes */
106     if (($dn != NULL) && ($dn != "new") && $this->is_account){
108       /* Load attributes containing arrays */
109       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
110         $this->$val= array();
111         if (isset($this->attrs["$val"]["count"])){
112           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
113             array_push($this->$val, $this->attrs["$val"][$i]);
114           }
115         }
116       }
119       /* Only do IMAP actions if gosaMailServer attribute is set */
120       if (isset ($this->attrs["gosaMailServer"][0])){
122         $method = new $this->method($this->config);
123         $id     = $method->uattrib;
125         /* Adapt attributes if needed */
126         $method->fixAttributesOnLoad($this);
127         if ($method->connect($this->attrs["gosaMailServer"][0])){
129           /* Update quota values */
130           $quota= $method->getQuota($this->folder_prefix.$this->$id);
131          
132           if($quota){
133             if ($quota['gosaMailQuota'] == 2147483647){
134               $this->quotaUsage     = "";
135               $this->gosaMailQuota  = "";
136             } else {
137               $this->quotaUsage     = $quota['quotaUsage'];
138               $this->gosaMailQuota  = $quota['gosaMailQuota'];
139             }
140           }else{
141             $this->quotaUsage     = "";
142             $this->gosaMailQuota  = "";
143             print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
144           }
146           /* Get mailboxes / folder like INBOX ..*/
147           $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
148           
149           $method->disconnect();
150         }else{
151           /* Could not connect to ldap.
152            */
153           if (isset($this->attrs['gosaMailQuota'][0])){
154             $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
155           }
156         }
157       }
158     }
160     /* Fill vacation array */
161     $this->vacation= array();
162     if (isset($this->config->current['VACATIONDIR'])){
163       $dir= $this->config->current['VACATIONDIR'];
164       if (is_dir($dir) && is_readable($dir)){
166         /* Look for files and build the vacation array */
167         $dh= opendir($dir);
168         while ($file = readdir($dh)){
169           $description= $this->parse_vacation("$dir/$file");
170           if ($description != ""){
171             $this->vacation["$dir/$file"]= $description;
172           }
173         }
174         closedir($dh);
175       }
176     }
179   /* Create filter */
181     /* Get global filter config */
182     if (!is_global("mailfilter")){
183       $ui= get_userinfo();
184       $base= get_base_from_people($ui->dn);
185       $mailfilter= array( "depselect"       => $base,
186           "muser"            => "",
187           "regex"           => "*");
188       register_global("mailfilter", $mailfilter);
189     }
190   }
193   function parse_vacation($file)
194   {
195     $desc= "";
197     if (is_file($file)){
198       $fh = fopen($file, "r");
199       $line= fgets($fh, 256);
201       if (!preg_match('/^DESC:/', $line)){
202         print_red (_("No DESC tag in vacation file:")." $file");
203         return $desc;
204       }
205       fclose ($fh);
207       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
208     }
210     return $desc;
211   }
214   function execute()
215   {
216     /* Call parent execute */
217     plugin::execute();
219     /* Initialise vars */
221     /* Load templating engine */
222     $smarty= get_smarty();
223     $display= "";
225     /* Get available mailserver */
226     $mailserver= array();
227     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
228       $mailserver[]= $key;
229     }
231     /* Handle account state */
233     /* Do we need to flip is_account state? */
234     if (isset($_POST['modify_state'])){
235       $this->is_account= !$this->is_account;
236     }
238     /* Do we represent a valid account? */
239     if (!$this->is_account && $this->parent == NULL){
240       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
241         _("This account has no mail extensions.")."</b>";
243       $display.= back_to_main();
244       return ($display);
245     }
247     /* Show tab dialog headers */
248     if ($this->parent != NULL){
249       if ($this->is_account){
250         $display= $this->show_header(_("Remove mail account"),
251             _("This account has mail features enabled. You can disable them by clicking below."));
252       } else {
253         $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
254         return ($display);
255       }
256     }
259     /* Forwarder  subdialog */
261     /* Trigger forward add dialog? */
262     if (isset($_POST['add_local_forwarder'])){
263       $this->forward_dialog= TRUE;
264       $this->dialog= TRUE;
265     }
267     /* Cancel forward add dialog? */
268     if (isset($_POST['add_locals_cancel'])){
269       $this->forward_dialog= FALSE;
270       $this->dialog= FALSE;
271     }
273     /* Finished adding of locals? */
274     if (isset($_POST['add_locals_finish'])){
275       if (count ($_POST['local_list']) &&
276           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
278         /* Walk through list of forwarders, ignore own addresses */
279         foreach ($_POST['local_list'] as $val){
280           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
281               $val != $this->mail){
283             $this->addForwarder($val);
284             $this->is_modified= TRUE;
285           }
286         }
287       }
288       $this->forward_dialog= FALSE;
289       $this->dialog= FALSE;
290     }
292     /* Add forward email addresses */
293     if (isset($_POST['add_forwarder'])){
294       if ($_POST['forward_address'] != ""){
296         /* Valid email address specified? */
297         $address= $_POST['forward_address'];
298         $valid= FALSE;
299         if (!is_email($address)){
300           if (!is_email($address, TRUE)){
301             if ($this->is_template){
302               $valid= TRUE;
303             } else {
304               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
305             }
306           }
307         } elseif ($address == $this->mail
308             || in_array($address, $this->gosaMailAlternateAddress)) {
310           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
312         } else {
313           $valid= TRUE;
314         }
316         if ($valid){
317           /* Add it */
318           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
319             $this->addForwarder ($address);
320             $this->is_modified= TRUE;
321           }
323         }
324       }
325     }
327     /* Delete forward email addresses */
328     if (isset($_POST['delete_forwarder'])){
329       if (count($_POST['forwarder_list']) 
330           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
332         $this->delForwarder ($_POST['forwarder_list']);
333       }
334     }
336     
337     /* Alternate address handling */
339     /* Add alternate email addresses */
340     if (isset($_POST['add_alternate'])){
341       if ($_POST['alternate_address'] != "" &&
342           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
344         $valid= FALSE;
345         if (!is_email($_POST['alternate_address'])){
346           if ($this->is_template){
347             if (!(is_email($_POST['alternate_address'], TRUE))){
348               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
349             } else {
350               $valid= TRUE;
351             }
352           } else {
353             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
354           }
356         } else {
357           $valid= TRUE;
358         }
360         if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
361           $ui= get_userinfo();
362           if ($user != $ui->username){
363             print_red (_("The address you're trying to add is already used by user")." '$user'.");
364           }
365         }
366       }
367     }
369     /* Delete alternate email addresses */
370     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
371       if (count($_POST['alternates_list']) &&
372           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
374         $this->delAlternate ($_POST['alternates_list']);
375       }
376     }
378   
379     /* Vacation message */
380   
381     /* Import vacation message? */
382     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
383       $contents= "";
384       $lines= file($_POST["vacation_template"]);
385       foreach ($lines as $line){
386         if (!preg_match('/^DESC:/', $line)){
387           $contents.= $line;
388         }
389       }
391       /* Replace attributes */
392       $attrs= $this->parent->by_object['user']->attributes;
393       foreach ($attrs as $val){
394         $contents= preg_replace("/%$val/",
395             $this->parent->by_object['user']->$val, $contents);
396       }
398       /* Save message */
399       $this->gosaVacationMessage= htmlspecialchars($contents);
400     }
402   
403     /* Display forward dialog if requested above */
405     /* Show forward add dialog */
406     if ($this->forward_dialog){
407       $ldap= $this->config->get_ldap_link();
409       /* Save data */
410       $mailfilter= get_global("mailfilter");
411       foreach( array("depselect", "muser", "regex") as $type){
412         if (isset($_POST[$type])){
413           $mailfilter[$type]= $_POST[$type];
414         }
415       }
416       if (isset($_GET['search'])){
417         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
418         if ($s == "**"){
419           $s= "*";
420         }
421         $mailfilter['regex']= $s;
422       }
423       register_global("mailfilter", $mailfilter);
425       /* Get actual list */
426       $mailusers= array ();
427       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
428         $regex= $mailfilter['regex'];
429         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
430       } else {
431         $filter= "";
432       }
433       if ($mailfilter['muser'] != ""){
434         $user= $mailfilter['muser'];
435         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
436       }
438       /* Add already present people to the filter */
439       $exclude= "";
440       foreach ($this->gosaMailForwardingAddress as $mail){
441         $exclude.= "(mail=$mail)";
442       }
443       if ($exclude != ""){
444         $filter.= "(!(|$exclude))";
445       }
447       $acl= array($this->config->current['BASE'] => ":all");
448       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'], 
449                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
450       $ldap->cd($mailfilter['depselect']);
451       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
452       error_reporting (0);
453       while ($attrs= $ldap->fetch()){
454         if(preg_match('/%/', $attrs['mail'][0])){
455           continue;
456         }
457         $name= $this->make_name($attrs);
458         $mailusers[$attrs['mail'][0]]= $name."&lt;".
459           $attrs['mail'][0]."&gt;";
460       }
461       error_reporting (E_ALL);
462       natcasesort ($mailusers);
463       reset ($mailusers);
465       /* Show dialog */
466       $smarty->assign("search_image", get_template_path('images/search.png'));
467       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
468       $smarty->assign("tree_image", get_template_path('images/tree.png'));
469       $smarty->assign("infoimage", get_template_path('images/info.png'));
470       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
471       $smarty->assign("mailusers", $mailusers);
472       if (isset($_POST['depselect'])){
473         $smarty->assign("depselect", $_POST['depselect']);
474       }
475       $smarty->assign("deplist", $this->config->idepartments);
476       $smarty->assign("apply", apply_filter());
477       $smarty->assign("alphabet", generate_alphabet());
478       $smarty->assign("hint", print_sizelimit_warning());
479       foreach( array("depselect", "muser", "regex") as $type){
480         $smarty->assign("$type", $mailfilter[$type]);
481       }
482       $smarty->assign("hint", print_sizelimit_warning());
484       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
485       return ($display);
486     }
488     /* Display mail account tab */
490     $smarty->assign("mailServers", $mailserver);
491     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
492           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
493           "gosaVacationMessage", "gosaMailDeliveryMode",
494           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
496       $smarty->assign("$val", $this->$val);
497       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
498     }
500     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
501       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
502       $smarty->assign("quotadefined", "true");
503     } else {
504       $smarty->assign("quotadefined", "false");
505     }
507     /* Disable mail field if needed */
508     $method= new $this->method($this->config);
509     if ($method->uattrib == "mail" && $this->initially_was_account){
510       $smarty->assign("mailACL", "disabled");
511     }
514     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
515       $smarty->assign("drop_own_mails", "checked");
516     } else {
517       $smarty->assign("drop_own_mails", "");
518     }
520     $types = array(
521           "V"=>"use_vacation",
522           "S"=>"use_spam_filter",
523           "R"=>"use_mailsize_limit",
524           "I"=>"only_local",
525           "C"=>"own_script");
527     /* Fill checkboxes */
528     foreach($types as $option => $varname){
529       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
530         $smarty->assign($varname, "checked");
531       } else {
532         $smarty->assign($varname, "");
533       }
534     }
535     
536     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
537       $smarty->assign("use_vacation", "checked");
538     } else {
539       $smarty->assign("use_vacation", "");
540     }
541     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
542       $smarty->assign("use_spam_filter", "checked");
543     } else {
544       $smarty->assign("use_spam_filter", "");
545     }
546     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
547       $smarty->assign("use_mailsize_limit", "checked");
548     } else {
549       $smarty->assign("use_mailsize_limit", "");
550     }
551     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
552       $smarty->assign("only_local", "checked");
553     } else {
554       $smarty->assign("only_local", "");
555     }
556     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
557       $smarty->assign("own_script", "checked");
558     } else {
559       $smarty->assign("own_script", "");
560     }
562     /* Have vacation templates? */
563     $smarty->assign("template", "");
564     if (count($this->vacation)){
565       $smarty->assign("show_templates", "true");
566       $smarty->assign("vacationtemplates", $this->vacation);
567       if (isset($_POST['vacation_template'])){
568         $smarty->assign("template", $_POST['vacation_template']);
569       }
570     } else {
571       $smarty->assign("show_templates", "false");
572     }
574     /* Fill spam selector */
575     $spamlevel= array();
576     for ($i= 0; $i<21; $i++){
577       $spamlevel[]= $i;
578     }
579     $smarty->assign("spamlevel", $spamlevel);
580     $smarty->assign("spambox", $this->mailboxList);
581     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
582     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
584     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
585     return ($display);
586   }
589   /* remove object from parent */
590   function remove_from_parent()
591   {
592     /* Cancel if there's nothing to do here */
593     if (!$this->initially_was_account){
594       return;
595     }
596     
597     /* include global link_info */
598     $ldap= $this->config->get_ldap_link();
600     /* Remove and write to LDAP */
601     plugin::remove_from_parent();
603     /* Zero arrays */
604     $this->attrs['gosaMailAlternateAddress']= array();
605     $this->attrs['gosaMailForwardingAddress']= array();
607     /* Adapt attributes if needed */
608     $method= new $this->method($this->config);
609     $method->fixAttributesOnRemove($this);
611     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
612     $ldap->cd($this->dn);
613     $this->cleanup();
614     $ldap->modify ($this->attrs); 
616     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/mail account with dn '%s' failed."),$this->dn));
618     /* Connect to IMAP server for account deletion */
619     if ($this->gosaMailServer != ""){
620       $method= new $this->method($this->config);
621       $id= $method->uattrib;
622       if ($method->connect($this->gosaMailServer)){
624         /* Remove account from IMAP server */
625         $method->deleteMailbox($this->folder_prefix.$this->$id);
626         $method->disconnect();
627       }
628     }
630     /* Optionally execute a command after we're done */
631     $this->handle_post_events("remove");
632   }
635   /* Save data to object */
636   function save_object()
637   {
638     if (isset($_POST['mailTab'])){
639       /* Save ldap attributes */
640       plugin::save_object();
642       /* Assemble mail delivery mode
643          The mode field in ldap consists of values between braces, this must
644          be called when 'mail' is set, because checkboxes may not be set when
645          we're in some other dialog.
647          Example for gosaMailDeliveryMode [LR        ]
648          L: Local delivery
649          R: Reject when exceeding mailsize limit
650          S: Use spam filter
651          V: Use vacation message
652          C: Use custm sieve script
653          I: Only insider delivery */
655       $tmp= "";
656       if (!isset($_POST["drop_own_mails"])){
657         $tmp.= "L";
658       }
659       if (isset($_POST["use_mailsize_limit"])){
660         $tmp.= "R";
661       }
662       if (isset($_POST["use_spam_filter"])){
663         $tmp.= "S";
664       }
665       if (isset($_POST["use_vacation"])){
666         $tmp.= "V";
667       }
668       if (isset($_POST["own_script"])){
669         $tmp.= "C";
670       }
671       if (isset($_POST["only_local"])){
672         $tmp.= "I";
673       }
674       $tmp= "[$tmp]";
676       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
677         if ($this->gosaMailDeliveryMode != $tmp){
678           $this->is_modified= TRUE;
679         }
680         $this->gosaMailDeliveryMode= $tmp;
681       }
682     }
683   }
686   /* Save data to LDAP, depending on is_account we save or delete */
687   function save()
688   {
689     $ldap= $this->config->get_ldap_link();
691     /* Call parents save to prepare $this->attrs */
692     plugin::save();
694     /* Save arrays */
695     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
696     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
698     /* Adapt attributes if needed */
699     $method= new $this->method($this->config);
700     $id= $method->uattrib;
702     $method->fixAttributesOnStore($this);
704     /* Remove Mailquota if = "" or "0"  */
705     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
706       $this->attrs['gosaMailQuota']=0;
707     }
709     if(empty($this->attrs['gosaSpamMailbox'])){
710       unset($this->attrs['gosaSpamMailbox']);
711     }
713     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
715     /* Save data to LDAP */
716     $ldap->cd($this->dn);
717     $this->cleanup();
718     $ldap->modify ($this->attrs); 
720     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/mail account with dn '%s' failed."),$this->dn));
722     /* Only do IMAP actions if we are not a template */
723     if (!$this->is_template){
725       if ($method->connect($this->gosaMailServer)){
726         $method->updateMailbox($this->folder_prefix.$this->$id);
727         
728         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
729         $method->disconnect();
731         /* Write sieve information only if not in C mode */
732         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
733           $method->configureFilter($this->$id,
734               $this->gosaMailDeliveryMode,
735               $this->mail,
736               $this->gosaMailAlternateAddress,
737               $this->gosaMailMaxSize,
738               $this->gosaSpamMailbox,
739               $this->gosaSpamSortLevel,
740               $this->gosaVacationMessage);
741         }
742       }
743     }
745     /* Optionally execute a command after we're done */
746     if ($this->initially_was_account == $this->is_account){
747       if ($this->is_modified){
748         $this->handle_post_events("modify");
749       }
750     } else {
751       $this->handle_post_events("add");
752     }
754   }
757   /* Check formular input */
758   function check()
759   {
760     if(!$this->is_account) return(array());
762     $ldap= $this->config->get_ldap_link();
764     /* Call common method to give check the hook */
765     $message= plugin::check();
767     if(empty($this->gosaMailServer)){
768       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
769     }
771     /* must: mail */
772     if ($this->mail == ""){
773       $message[]= _("The required field 'Primary address' is not set.");
774     }
775     if ($this->is_template){
776       if (!is_email($this->mail, TRUE)){
777         $message[]= _("Please enter a valid email address in 'Primary address' field.");
778       }
779     } else {
780       if (!is_email($this->mail)){
781         $message[]= _("Please enter a valid email address in 'Primary address' field.");
782       }
783     }
784     $ldap->cd($this->config->current['BASE']);
785     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
786     if ($ldap->count() != 0){
787       $message[]= _("The primary address you've entered is already in use.");
788     }
790     /* Check quota */
791     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
792       if (!is_numeric($this->gosaMailQuota)) {
793         $message[]= _("Value in 'Quota size' is not valid.");
794       } else {
795         $this->gosaMailQuota= (int) $this->gosaMailQuota;
796       }
797     }
799     /* Check rejectsize for integer */
800     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
801       if (!is_numeric($this->gosaMailMaxSize)){
802         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
803       } else {
804         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
805       }
806     }
808     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
809     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
810         $this->gosaMailMaxSize == ""){
812       $message[]= _("You need to set the maximum mail size in order to reject anything.");
813     }
815     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
816       $message[]= _("You specified Spam settings, but there is no Folder specified.");
817     }
819     return ($message);
820   }
823   /* Adapt from template, using 'dn' */
824   function adapt_from_template($dn)
825   {
826     plugin::adapt_from_template($dn);
828     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
829       $this->$val= array();
830       if (isset($this->attrs["$val"]["count"])){
831         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
832           $value= $this->attrs["$val"][$i];
833           foreach (array("sn", "givenName", "uid") as $repl){
834             if (preg_match("/%$repl/i", $value)){
835               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
836             }
837           }
838           array_push($this->$val, strtolower(rewrite($value)));
839         }
840       }
841     }
842     $this->mail= strtolower(rewrite($this->mail));
843   }
846   /* Add entry to forwarder list */
847   function addForwarder($address)
848   {
849     $this->gosaMailForwardingAddress[]= $address;
850     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
851     sort ($this->gosaMailForwardingAddress);
852     reset ($this->gosaMailForwardingAddress);
853     $this->is_modified= TRUE;
854   }
857   /* Remove list of addresses from forwarder list */
858   function delForwarder($addresses)
859   {
860     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
861     $this->is_modified= TRUE;
862   }
865   /* Add given mail address to the list of alternate adresses , 
866       check if this mal address is used, skip adding in this case */
867   function addAlternate($address)
868   {
869     $ldap= $this->config->get_ldap_link();
870     $address= strtolower($address);
871       
872     /* Is this address already assigned in LDAP? */
873     $ldap->cd ($this->config->current['BASE']);
874     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
876     if ($ldap->count() > 0){
877       $attrs= $ldap->fetch ();
878       return ($attrs["uid"][0]);
879     }
881     /* Add to list of alternates */
882     if (!in_array($address, $this->gosaMailAlternateAddress)){
883       $this->gosaMailAlternateAddress[]= $address;
884       $this->is_modified= TRUE;
885     }
887     sort ($this->gosaMailAlternateAddress);
888     reset ($this->gosaMailAlternateAddress);
889     return ("");
890   }
893   function delAlternate($addresses)
894   {
895     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
896                                                            $this->gosaMailAlternateAddress);
897     $this->is_modified= TRUE;
898   }
900   function make_name($attrs)
901   {
902     $name= "";
903     if (isset($attrs['sn'][0])){
904       $name= $attrs['sn'][0];
905     }
906     if (isset($attrs['givenName'][0])){
907       if ($name != ""){
908         $name.= ", ".$attrs['givenName'][0];
909       } else {
910         $name.= $attrs['givenName'][0];
911       }
912     }
913     if ($name != ""){
914       $name.= " ";
915     }
917     return ($name);
918   }
920   
921   /* Create the mail part for the copy & paste dialog */
922   function getCopyDialog()
923   {
924     if(!$this->is_account) return("");
925     $smarty = get_smarty();
926     $smarty->assign("mail",$this->mail); 
927     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
928     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
929     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
931     $ret = array();
932     $ret['status'] = "";
933     $ret['string'] = $str;
934     return($ret);
935   }
937   function saveCopyDialog()
938   {
939     if(!$this->is_account) return;  
941     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
942     $this->execute();
943     
944     if(isset($_POST['mail'])){
945       $this->mail = $_POST['mail'];
946     }
948   }
950   function allow_remove()
951   {
952     if (isset($this->config->current['MAILMETHOD'])){
953       $method= $this->config->current['MAILMETHOD'];
954       if(preg_match("/kolab/i",$method)){
955         $ldap = $this->config->get_ldap_link();
956         $ldap->cd($this->config->current['BASE']);
957         $ldap->cat($this->dn);
958         if($ldap->count()){
959           $attrs = $ldap->fetch();
960           if(isset($attrs['kolabDeleteFlag'])){ 
961             return(_("Waiting for kolab to remove mail properties."));
962           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
963             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
964           }
965         }
966       }
967     }
968   }
971 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
972 ?>