Code

Updated locales. Fixed small typos
[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     }
161     /* Get vacation message */
163     /* Fill vacation array */
164     $this->vacation= array();
165     if (isset($this->config->current['VACATIONDIR'])){
166       $dir= $this->config->current['VACATIONDIR'];
167       if (is_dir($dir) && is_readable($dir)){
169         /* Look for files and build the vacation array */
170         $dh= opendir($dir);
171         while ($file = readdir($dh)){
172           $description= $this->parse_vacation("$dir/$file");
173           if ($description != ""){
174             $this->vacation["$dir/$file"]= $description;
175           }
176         }
177         closedir($dh);
178       }
179     }
182   /* Create filter */
184     /* Get global filter config */
185     if (!is_global("mailfilter")){
186       $ui= get_userinfo();
187       $base= get_base_from_people($ui->dn);
188       $mailfilter= array( "depselect"       => $base,
189           "muser"            => "",
190           "regex"           => "*");
191       register_global("mailfilter", $mailfilter);
192     }
193   }
196   function parse_vacation($file)
197   {
198     $desc= "";
200     if (is_file($file)){
201       $fh = fopen($file, "r");
202       $line= fgets($fh, 256);
204       if (!preg_match('/^DESC:/', $line)){
205         print_red (_("No DESC tag in vacation file:")." $file");
206         return $desc;
207       }
208       fclose ($fh);
210       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
211     }
213     return $desc;
214   }
217   function execute()
218   {
219     /* Call parent execute */
220     plugin::execute();
222     /* Initialise vars */
224     /* Load templating engine */
225     $smarty= get_smarty();
226     $display= "";
228     /* Get available mailserver */
229     $mailserver= array();
230     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
231       $mailserver[]= $key;
232     }
234     /* Handle account state */
236     /* Do we need to flip is_account state? */
237     if (isset($_POST['modify_state'])){
238       $this->is_account= !$this->is_account;
239     }
241     /* Do we represent a valid account? */
242     if (!$this->is_account && $this->parent == NULL){
243       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
244         _("This account has no mail extensions.")."</b>";
246       $display.= back_to_main();
247       return ($display);
248     }
250     /* Show tab dialog headers */
251     if ($this->parent != NULL){
252       if ($this->is_account){
253         $display= $this->show_header(_("Remove mail account"),
254             _("This account has mail features enabled. You can disable them by clicking below."));
255       } else {
256         $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
257         return ($display);
258       }
259     }
262     /* Forwarder  subdialog */
264     /* Trigger forward add dialog? */
265     if (isset($_POST['add_local_forwarder'])){
266       $this->forward_dialog= TRUE;
267       $this->dialog= TRUE;
268     }
270     /* Cancel forward add dialog? */
271     if (isset($_POST['add_locals_cancel'])){
272       $this->forward_dialog= FALSE;
273       $this->dialog= FALSE;
274     }
276     /* Finished adding of locals? */
277     if (isset($_POST['add_locals_finish'])){
278       if (count ($_POST['local_list']) &&
279           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
281         /* Walk through list of forwarders, ignore own addresses */
282         foreach ($_POST['local_list'] as $val){
283           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
284               $val != $this->mail){
286             $this->addForwarder($val);
287             $this->is_modified= TRUE;
288           }
289         }
290       }
291       $this->forward_dialog= FALSE;
292       $this->dialog= FALSE;
293     }
295     /* Add forward email addresses */
296     if (isset($_POST['add_forwarder'])){
297       if ($_POST['forward_address'] != ""){
299         /* Valid email address specified? */
300         $address= $_POST['forward_address'];
301         $valid= FALSE;
302         if (!is_email($address)){
303           if (!is_email($address, TRUE)){
304             if ($this->is_template){
305               $valid= TRUE;
306             } else {
307               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
308             }
309           }
310         } elseif ($address == $this->mail
311             || in_array($address, $this->gosaMailAlternateAddress)) {
313           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
315         } else {
316           $valid= TRUE;
317         }
319         if ($valid){
320           /* Add it */
321           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
322             $this->addForwarder ($address);
323             $this->is_modified= TRUE;
324           }
326         }
327       }
328     }
330     /* Delete forward email addresses */
331     if (isset($_POST['delete_forwarder'])){
332       if (count($_POST['forwarder_list']) 
333           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
335         $this->delForwarder ($_POST['forwarder_list']);
336       }
337     }
339     
340     /* Alternate address handling */
342     /* Add alternate email addresses */
343     if (isset($_POST['add_alternate'])){
344       if ($_POST['alternate_address'] != "" &&
345           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
347         $valid= FALSE;
348         if (!is_email($_POST['alternate_address'])){
349           if ($this->is_template){
350             if (!(is_email($_POST['alternate_address'], TRUE))){
351               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
352             } else {
353               $valid= TRUE;
354             }
355           } else {
356             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
357           }
359         } else {
360           $valid= TRUE;
361         }
363         if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
364           $ui= get_userinfo();
365           if ($user != $ui->username){
366             print_red (_("The address you're trying to add is already used by user")." '$user'.");
367           }
368         }
369       }
370     }
372     /* Delete alternate email addresses */
373     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
374       if (count($_POST['alternates_list']) &&
375           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
377         $this->delAlternate ($_POST['alternates_list']);
378       }
379     }
381   
382     /* Vocation message */
383   
384     /* Import vacation message? */
385     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
386       $contents= "";
387       $lines= file($_POST["vacation_template"]);
388       foreach ($lines as $line){
389         if (!preg_match('/^DESC:/', $line)){
390           $contents.= $line;
391         }
392       }
394       /* Replace attributes */
395       $attrs= $this->parent->by_object['user']->attributes;
396       foreach ($attrs as $val){
397         $contents= preg_replace("/%$val/",
398             $this->parent->by_object['user']->$val, $contents);
399       }
401       /* Save message */
402       $this->gosaVacationMessage= htmlspecialchars($contents);
403     }
405   
406     /* Display forward dialog if requested above */
408     /* Show forward add dialog */
409     if ($this->forward_dialog){
410       $ldap= $this->config->get_ldap_link();
412       /* Save data */
413       $mailfilter= get_global("mailfilter");
414       foreach( array("depselect", "muser", "regex") as $type){
415         if (isset($_POST[$type])){
416           $mailfilter[$type]= $_POST[$type];
417         }
418       }
419       if (isset($_GET['search'])){
420         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
421         if ($s == "**"){
422           $s= "*";
423         }
424         $mailfilter['regex']= $s;
425       }
426       register_global("mailfilter", $mailfilter);
428       /* Get actual list */
429       $mailusers= array ();
430       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
431         $regex= $mailfilter['regex'];
432         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
433       } else {
434         $filter= "";
435       }
436       if ($mailfilter['muser'] != ""){
437         $user= $mailfilter['muser'];
438         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
439       }
441       /* Add already present people to the filter */
442       $exclude= "";
443       foreach ($this->gosaMailForwardingAddress as $mail){
444         $exclude.= "(mail=$mail)";
445       }
446       if ($exclude != ""){
447         $filter.= "(!(|$exclude))";
448       }
450       $acl= array($this->config->current['BASE'] => ":all");
451       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'], 
452                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
453       $ldap->cd($mailfilter['depselect']);
454       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
455       error_reporting (0);
456       while ($attrs= $ldap->fetch()){
457         if(preg_match('/%/', $attrs['mail'][0])){
458           continue;
459         }
460         $name= $this->make_name($attrs);
461         $mailusers[$attrs['mail'][0]]= $name."&lt;".
462           $attrs['mail'][0]."&gt;";
463       }
464       error_reporting (E_ALL);
465       natcasesort ($mailusers);
466       reset ($mailusers);
468       /* Show dialog */
469       $smarty->assign("search_image", get_template_path('images/search.png'));
470       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
471       $smarty->assign("tree_image", get_template_path('images/tree.png'));
472       $smarty->assign("infoimage", get_template_path('images/info.png'));
473       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
474       $smarty->assign("mailusers", $mailusers);
475       if (isset($_POST['depselect'])){
476         $smarty->assign("depselect", $_POST['depselect']);
477       }
478       $smarty->assign("deplist", $this->config->idepartments);
479       $smarty->assign("apply", apply_filter());
480       $smarty->assign("alphabet", generate_alphabet());
481       $smarty->assign("hint", print_sizelimit_warning());
482       foreach( array("depselect", "muser", "regex") as $type){
483         $smarty->assign("$type", $mailfilter[$type]);
484       }
485       $smarty->assign("hint", print_sizelimit_warning());
487       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
488       return ($display);
489     }
491     /* Display mail account tab */
493     $smarty->assign("mailServers", $mailserver);
494     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
495           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
496           "gosaVacationMessage", "gosaMailDeliveryMode",
497           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
499       $smarty->assign("$val", $this->$val);
500       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
501     }
503     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
504       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
505       $smarty->assign("quotadefined", "true");
506     } else {
507       $smarty->assign("quotadefined", "false");
508     }
510     /* Disable mail field if needed */
511     $method= new $this->method($this->config);
512     if ($method->uattrib == "mail" && $this->initially_was_account){
513       $smarty->assign("mailACL", "disabled");
514     }
517     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
518       $smarty->assign("drop_own_mails", "checked");
519     } else {
520       $smarty->assign("drop_own_mails", "");
521     }
523     $types = array(
524           "V"=>"use_vacation",
525           "S"=>"use_spam_filter",
526           "R"=>"use_mailsize_limit",
527           "I"=>"only_local",
528           "C"=>"own_script");
530     /* Fill checkboxes */
531     foreach($types as $option => $varname){
532       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
533         $smarty->assign($varname, "checked");
534       } else {
535         $smarty->assign($varname, "");
536       }
537     }
538     
539     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
540       $smarty->assign("use_vacation", "checked");
541     } else {
542       $smarty->assign("use_vacation", "");
543     }
544     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
545       $smarty->assign("use_spam_filter", "checked");
546     } else {
547       $smarty->assign("use_spam_filter", "");
548     }
549     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
550       $smarty->assign("use_mailsize_limit", "checked");
551     } else {
552       $smarty->assign("use_mailsize_limit", "");
553     }
554     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
555       $smarty->assign("only_local", "checked");
556     } else {
557       $smarty->assign("only_local", "");
558     }
559     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
560       $smarty->assign("own_script", "checked");
561     } else {
562       $smarty->assign("own_script", "");
563     }
565     /* Have vacation templates? */
566     $smarty->assign("template", "");
567     if (count($this->vacation)){
568       $smarty->assign("show_templates", "true");
569       $smarty->assign("vacationtemplates", $this->vacation);
570       if (isset($_POST['vacation_template'])){
571         $smarty->assign("template", $_POST['vacation_template']);
572       }
573     } else {
574       $smarty->assign("show_templates", "false");
575     }
577     /* Fill spam selector */
578     $spamlevel= array();
579     for ($i= 0; $i<21; $i++){
580       $spamlevel[]= $i;
581     }
582     $smarty->assign("spamlevel", $spamlevel);
583     $smarty->assign("spambox", $this->mailboxList);
584     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
585     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
587     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
588     return ($display);
589   }
592   /* remove object from parent */
593   function remove_from_parent()
594   {
595     /* Cancel if there's nothing to do here */
596     if (!$this->initially_was_account){
597       return;
598     }
599     
600     /* include global link_info */
601     $ldap= $this->config->get_ldap_link();
603     /* Remove and write to LDAP */
604     plugin::remove_from_parent();
606     /* Zero arrays */
607     $this->attrs['gosaMailAlternateAddress']= array();
608     $this->attrs['gosaMailForwardingAddress']= array();
610     /* Adapt attributes if needed */
611     $method= new $this->method($this->config);
612     $method->fixAttributesOnRemove($this);
614     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
615     $ldap->cd($this->dn);
616     $this->cleanup();
617     $ldap->modify ($this->attrs); 
619     show_ldap_error($ldap->get_error(), _("Removing mail account failed"));
621     /* Connect to IMAP server for account deletion */
622     if ($this->gosaMailServer != ""){
623       $method= new $this->method($this->config);
624       $id= $method->uattrib;
625       if ($method->connect($this->gosaMailServer)){
627         /* Remove account from IMAP server */
628         $method->deleteMailbox($this->folder_prefix.$this->$id);
629         $method->disconnect();
630       }
631     }
633     /* Optionally execute a command after we're done */
634     $this->handle_post_events("remove");
635   }
638   /* Save data to object */
639   function save_object()
640   {
641     if (isset($_POST['mailTab'])){
642       /* Save ldap attributes */
643       plugin::save_object();
645       /* Assemble mail delivery mode
646          The mode field in ldap consists of values between braces, this must
647          be called when 'mail' is set, because checkboxes may not be set when
648          we're in some other dialog.
650          Example for gosaMailDeliveryMode [LR        ]
651          L: Local delivery
652          R: Reject when exceeding mailsize limit
653          S: Use spam filter
654          V: Use vacation message
655          C: Use custm sieve script
656          I: Only insider delivery */
658       $tmp= "";
659       if (!isset($_POST["drop_own_mails"])){
660         $tmp.= "L";
661       }
662       if (isset($_POST["use_mailsize_limit"])){
663         $tmp.= "R";
664       }
665       if (isset($_POST["use_spam_filter"])){
666         $tmp.= "S";
667       }
668       if (isset($_POST["use_vacation"])){
669         $tmp.= "V";
670       }
671       if (isset($_POST["own_script"])){
672         $tmp.= "C";
673       }
674       if (isset($_POST["only_local"])){
675         $tmp.= "I";
676       }
677       $tmp= "[$tmp]";
679       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
680         if ($this->gosaMailDeliveryMode != $tmp){
681           $this->is_modified= TRUE;
682         }
683         $this->gosaMailDeliveryMode= $tmp;
684       }
685     }
686   }
689   /* Save data to LDAP, depending on is_account we save or delete */
690   function save()
691   {
692     $ldap= $this->config->get_ldap_link();
694     /* Call parents save to prepare $this->attrs */
695     plugin::save();
697     /* Save arrays */
698     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
699     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
701     /* Adapt attributes if needed */
702     $method= new $this->method($this->config);
703     $id= $method->uattrib;
705     $method->fixAttributesOnStore($this);
707     /* Remove Mailquota if = "" or "0"  */
708     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
709       $this->attrs['gosaMailQuota']=0;
710     }
712     if(empty($this->attrs['gosaSpamMailbox'])){
713       unset($this->attrs['gosaSpamMailbox']);
714     }
716     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
718     /* Save data to LDAP */
719     $ldap->cd($this->dn);
720     $this->cleanup();
721     $ldap->modify ($this->attrs); 
723     show_ldap_error($ldap->get_error(), _("Saving mail account failed"));
725     /* Only do IMAP actions if we are not a template */
726     if (!$this->is_template){
728       if ($method->connect($this->gosaMailServer)){
729         $method->updateMailbox($this->folder_prefix.$this->$id);
730         
731         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
732         $method->disconnect();
734         /* Write sieve information only if not in C mode */
735         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
736           $method->configureFilter($this->$id,
737               $this->gosaMailDeliveryMode,
738               $this->mail,
739               $this->gosaMailAlternateAddress,
740               $this->gosaMailMaxSize,
741               $this->gosaSpamMailbox,
742               $this->gosaSpamSortLevel,
743               $this->gosaVacationMessage);
744         }
745       }
746     }
748     /* Optionally execute a command after we're done */
749     if ($this->initially_was_account == $this->is_account){
750       if ($this->is_modified){
751         $this->handle_post_events("modify");
752       }
753     } else {
754       $this->handle_post_events("add");
755     }
757   }
760   /* Check formular input */
761   function check()
762   {
763     if(!$this->is_account) return(array());
765     $ldap= $this->config->get_ldap_link();
767     /* Call common method to give check the hook */
768     $message= plugin::check();
770     if(empty($this->gosaMailServer)){
771       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
772     }
774     /* must: mail */
775     if ($this->mail == ""){
776       $message[]= _("The required field 'Primary address' is not set.");
777     }
778     if ($this->is_template){
779       if (!is_email($this->mail, TRUE)){
780         $message[]= _("Please enter a valid email address in 'Primary address' field.");
781       }
782     } else {
783       if (!is_email($this->mail)){
784         $message[]= _("Please enter a valid email address in 'Primary address' field.");
785       }
786     }
787     $ldap->cd($this->config->current['BASE']);
788     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
789     if ($ldap->count() != 0){
790       $message[]= _("The primary address you've entered is already in use.");
791     }
793     /* Check quota */
794     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
795       if (!is_numeric($this->gosaMailQuota)) {
796         $message[]= _("Value in 'Quota size' is not valid.");
797       } else {
798         $this->gosaMailQuota= (int) $this->gosaMailQuota;
799       }
800     }
802     /* Check rejectsize for integer */
803     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
804       if (!is_numeric($this->gosaMailMaxSize)){
805         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
806       } else {
807         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
808       }
809     }
811     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
812     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
813         $this->gosaMailMaxSize == ""){
815       $message[]= _("You need to set the maximum mail size in order to reject anything.");
816     }
818     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
819       $message[]= _("You specified Spam settings, but there is no Folder specified.");
820     }
822     return ($message);
823   }
826   /* Adapt from template, using 'dn' */
827   function adapt_from_template($dn)
828   {
829     plugin::adapt_from_template($dn);
831     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
832       $this->$val= array();
833       if (isset($this->attrs["$val"]["count"])){
834         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
835           $value= $this->attrs["$val"][$i];
836           foreach (array("sn", "givenName", "uid") as $repl){
837             if (preg_match("/%$repl/i", $value)){
838               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
839             }
840           }
841           array_push($this->$val, strtolower(rewrite($value)));
842         }
843       }
844     }
845     $this->mail= strtolower(rewrite($this->mail));
846   }
849   /* Add entry to forwarder list */
850   function addForwarder($address)
851   {
852     $this->gosaMailForwardingAddress[]= $address;
853     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
854     sort ($this->gosaMailForwardingAddress);
855     reset ($this->gosaMailForwardingAddress);
856     $this->is_modified= TRUE;
857   }
860   /* Remove list of addresses from forwarder list */
861   function delForwarder($addresses)
862   {
863     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
864     $this->is_modified= TRUE;
865   }
868   /* Add given mail address to the list of alternate adresses , 
869       check if this mal address is used, skip adding in this case */
870   function addAlternate($address)
871   {
872     $ldap= $this->config->get_ldap_link();
873     $address= strtolower($address);
874       
875     /* Is this address already assigned in LDAP? */
876     $ldap->cd ($this->config->current['BASE']);
877     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
879     if ($ldap->count() > 0){
880       $attrs= $ldap->fetch ();
881       return ($attrs["uid"][0]);
882     }
884     /* Add to list of alternates */
885     if (!in_array($address, $this->gosaMailAlternateAddress)){
886       $this->gosaMailAlternateAddress[]= $address;
887       $this->is_modified= TRUE;
888     }
890     sort ($this->gosaMailAlternateAddress);
891     reset ($this->gosaMailAlternateAddress);
892     return ("");
893   }
896   function delAlternate($addresses)
897   {
898     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
899                                                            $this->gosaMailAlternateAddress);
900     $this->is_modified= TRUE;
901   }
903   function make_name($attrs)
904   {
905     $name= "";
906     if (isset($attrs['sn'][0])){
907       $name= $attrs['sn'][0];
908     }
909     if (isset($attrs['givenName'][0])){
910       if ($name != ""){
911         $name.= ", ".$attrs['givenName'][0];
912       } else {
913         $name.= $attrs['givenName'][0];
914       }
915     }
916     if ($name != ""){
917       $name.= " ";
918     }
920     return ($name);
921   }
923   
924   /* Create the mail part for the copy & paste dialog */
925   function getCopyDialog()
926   {
927     if(!$this->is_account) return("");
928     $smarty = get_smarty();
929     $smarty->assign("mail",$this->mail); 
930     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
931     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
932     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
934     $ret = array();
935     $ret['status'] = "";
936     $ret['string'] = $str;
937     return($ret);
938   }
940   function saveCopyDialog()
941   {
942     if(!$this->is_account) return;  
944     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
945     $this->execute();
946     
947     if(isset($_POST['mail'])){
948       $this->mail = $_POST['mail'];
949     }
951   }
953   function allow_remove()
954   {
955     if (isset($this->config->current['MAILMETHOD'])){
956       $method= $this->config->current['MAILMETHOD'];
957       if(preg_match("/kolab/i",$method)){
958         $ldap = $this->config->get_ldap_link();
959         $ldap->cd($this->config->current['BASE']);
960         $ldap->cat($this->dn);
961         if($ldap->count()){
962           $attrs = $ldap->fetch();
963           if(isset($attrs['kolabDeleteFlag'])){ 
964             return(_("Waiting for kolab to remove mail properties."));
965           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
966             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
967           }
968         }
969       }
970     }
971   }
974 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
975 ?>