Code

Added support for general check hooks
[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", "lrsp" => "post", "lrsip" => "append",
54       "lrswipcd" => "write", "lrswipcda" => "all" );
56   /* attribute list for save action */
57   var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
58       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
59       "gosaVacationMessage", "uid", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
60   var $objectclasses= array("gosaMailAccount");
63   /* constructor, if 'dn' is set, the node loads the given
64      'dn' from LDAP */
65   function mailAccount ($config, $dn= NULL)
66   {
67     /* Configuration is fine, allways */
68     $this->config= $config;
70     /* Load bases attributes */
71     plugin::plugin($config, $dn);
73     /* Set mailMethod to the one defined in gosa.conf */
74     if (isset($this->config->current['MAILMETHOD'])){
75       $method= $this->config->current['MAILMETHOD'];
76       if (class_exists("mailMethod$method")){
77         $this->method= "mailMethod$method";
78       } else {
79         print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
80       }
81     }
83     /* Preset folder prefix. Will change it later to respect
84        altnamespace. */
85     if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
86       $this->folder_prefix= "user/";
87     } else {
88       $this->folder_prefix= "user.";
89     }
91     if ($dn != NULL){
93       /* Load attributes containing arrays */
94       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
95         $this->$val= array();
96         if (isset($this->attrs["$val"]["count"])){
97           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
98             array_push($this->$val, $this->attrs["$val"][$i]);
99           }
100         }
101       }
103       /* Only do IMAP actions if gosaMailServer attribute is set */
104       if (isset ($this->attrs["gosaMailServer"][0])){
106         $method= new $this->method($this->config);
107         $id= $method->uattrib;
109         /* Adapt attributes if needed */
110         $method->fixAttributesOnLoad($this);
111         if ($method->connect($this->attrs["gosaMailServer"][0])){
112           $quota= $method->getQuota($this->folder_prefix.$this->$id);
114           /* Update quota values */
115           if ($quota['gosaMailQuota'] == 2147483647){
116             $this->quotaUsage= "";
117             $this->gosaMailQuota= "";
118           } else {
119             $this->quotaUsage= $quota['quotaUsage'];
120             $this->gosaMailQuota= $quota['gosaMailQuota'];
121           }
122           $this->mailboxList= $method->getMailboxList(
123               $this->folder_prefix.$this->$id,
124               $this->$id);
125           $method->disconnect();
126         }else{
127           /* Could not connect to ldap.
128            */
129           $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
130         }
131       }
132     }
134     /* Fill vacation array */
135     $this->vacation= array();
136     if (isset($this->config->current['VACATIONDIR'])){
137       $dir= $this->config->current['VACATIONDIR'];
138       if (is_dir($dir) && is_readable($dir)){
140         /* Look for files and build the vacation array */
141         $dh= opendir($dir);
142         while ($file = readdir($dh)){
143           $description= $this->parse_vacation("$dir/$file");
144           if ($description != ""){
145             $this->vacation["$dir/$file"]= $description;
146           }
147         }
148         closedir($dh);
149       }
150     }
152     /* Get global filter config */
153     if (!is_global("mailfilter")){
154       $ui= get_userinfo();
155       $base= get_base_from_people($ui->dn);
156       $mailfilter= array( "depselect"       => $base,
157           "muser"            => "",
158           "regex"           => "*");
159       register_global("mailfilter", $mailfilter);
160     }
162     if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
163       $this->gosaMailServer = $this->gosaMailServer[0];
164     }
166     /* Save initial account state */
167     $this->initially_was_account= $this->is_account;
168   }
171   function parse_vacation($file)
172   {
173     $desc= "";
175     if (is_file($file)){
176       $fh = fopen($file, "r");
177       $line= fgets($fh, 256);
179       if (!preg_match('/^DESC:/', $line)){
180         print_red (_("No DESC tag in vacation file:")." $file");
181         return $desc;
182       }
183       fclose ($fh);
185       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
186     }
188     return $desc;
189   }
192   function execute()
193   {
194         /* Call parent execute */
195         plugin::execute();
197     /* Load templating engine */
198     $smarty= get_smarty();
199     $display= "";
201     $mailserver= array();
202     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
203       $mailserver[]= $key;
204     }
206     /* Do we need to flip is_account state? */
207     if (isset($_POST['modify_state'])){
208       $this->is_account= !$this->is_account;
209     }
211     /* Show main page */
212     $mailserver= array();
213     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
214       $mailserver[]= $key;
215     }
217     /* Do we represent a valid account? */
218     if (!$this->is_account && $this->parent == NULL){
219       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
220         _("This account has no mail extensions.")."</b>";
222       $display.= back_to_main();
223       return ($display);
224     }
226     /* Show tab dialog headers */
227     if ($this->parent != NULL){
228       if ($this->is_account){
229         $display= $this->show_header(_("Remove mail account"),
230             _("This account has mail features enabled. You can disable them by clicking below."));
231       } else {
232         $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
233         return ($display);
234       }
235     }
237     /* Trigger forward add dialog? */
238     if (isset($_POST['add_local_forwarder'])){
239       $this->forward_dialog= TRUE;
240       $this->dialog= TRUE;
241     }
243     /* Cancel forward add dialog? */
244     if (isset($_POST['add_locals_cancel'])){
245       $this->forward_dialog= FALSE;
246       $this->dialog= FALSE;
247     }
249     /* Finished adding of locals? */
250     if (isset($_POST['add_locals_finish'])){
251       if (count ($_POST['local_list']) &&
252           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
254         /* Walk through list of forwarders, ignore own addresses */
255         foreach ($_POST['local_list'] as $val){
256           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
257               $val != $this->mail){
259             $this->addForwarder($val);
260             $this->is_modified= TRUE;
261           }
262         }
263       }
264       $this->forward_dialog= FALSE;
265       $this->dialog= FALSE;
266     }
268     /* Add forward email addresses */
269     if (isset($_POST['add_forwarder'])){
270       if ($_POST['forward_address'] != ""){
272         /* Valid email address specified? */
273         $address= $_POST['forward_address'];
274         $valid= FALSE;
275         if (!is_email($address)){
276           if (!is_email($address, TRUE)){
277             if ($this->is_template){
278               $valid= TRUE;
279             } else {
280               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
281             }
282           }
283         } elseif ($address == $this->mail
284             || in_array($address, $this->gosaMailAlternateAddress)) {
286           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
288         } else {
289           $valid= TRUE;
290         }
292         if ($valid){
293           /* Add it */
294           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
295             $this->addForwarder ($address);
296             $this->is_modified= TRUE;
297           }
299         }
300       }
301     }
303     /* Delete forward email addresses */
304     if (isset($_POST['delete_forwarder'])){
305       if (count($_POST['forwarder_list']) 
306           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
308         $this->delForwarder ($_POST['forwarder_list']);
309       }
310     }
312     /* Add alternate email addresses */
313     if (isset($_POST['add_alternate'])){
314       if ($_POST['alternate_address'] != "" &&
315           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
317         $valid= FALSE;
318         if (!is_email($_POST['alternate_address'])){
319           if ($this->is_template){
320             if (!(is_email($_POST['alternate_address'], TRUE))){
321               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
322             } else {
323               $valid= TRUE;
324             }
325           } else {
326             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
327           }
329         } else {
330           $valid= TRUE;
331         }
333         if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
334           $ui= get_userinfo();
335           if ($user != $ui->username){
336             print_red (_("The address you're trying to add is already used by user")." '$user'.");
337           }
338         }
339       }
340     }
342     /* Delete alternate email addresses */
343     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
344       if (count($_POST['alternates_list']) &&
345           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
347         $this->delAlternate ($_POST['alternates_list']);
348       }
349     }
351     /* Import vacation message? */
352     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
353       $contents= "";
354       $lines= file($_POST["vacation_template"]);
355       foreach ($lines as $line){
356         if (!preg_match('/^DESC:/', $line)){
357           $contents.= $line;
358         }
359       }
361       /* Replace attributes */
362       $attrs= $this->parent->by_object['user']->attributes;
363       foreach ($attrs as $val){
364         $contents= preg_replace("/%$val/",
365             $this->parent->by_object['user']->$val, $contents);
366       }
368       /* Save message */
369       $this->gosaVacationMessage= htmlspecialchars($contents);
370     }
372     /* Show forward add dialog */
373     if ($this->forward_dialog){
374       $ldap= $this->config->get_ldap_link();
376       /* Save data */
377       $mailfilter= get_global("mailfilter");
378       foreach( array("depselect", "muser", "regex") as $type){
379         if (isset($_POST[$type])){
380           $mailfilter[$type]= $_POST[$type];
381         }
382       }
383       if (isset($_GET['search'])){
384         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
385         if ($s == "**"){
386           $s= "*";
387         }
388         $mailfilter['regex']= $s;
389       }
390       register_global("mailfilter", $mailfilter);
392       /* Get actual list */
393       $mailusers= array ();
394       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
395         $regex= $mailfilter['regex'];
396         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
397       } else {
398         $filter= "";
399       }
400       if ($mailfilter['muser'] != ""){
401         $user= $mailfilter['muser'];
402         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
403       }
405       /* Add already present people to the filter */
406       $exclude= "";
407       foreach ($this->gosaMailForwardingAddress as $mail){
408         $exclude.= "(mail=$mail)";
409       }
410       if ($exclude != ""){
411         $filter.= "(!(|$exclude))";
412       }
414       $acl= array($this->config->current['BASE'] => ":all");
415       $res= get_list($acl, "(&(objectClass=gosaMailAccount)$filter)", TRUE, $mailfilter['depselect'], array("sn", "mail", "givenName"), TRUE);
416       $ldap->cd($mailfilter['depselect']);
417       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
418       error_reporting (0);
419       while ($attrs= $ldap->fetch()){
420         if(preg_match('/%/', $attrs['mail'][0])){
421           continue;
422         }
423         $name= $this->make_name($attrs);
424         $mailusers[$attrs['mail'][0]]= $name."&lt;".
425           $attrs['mail'][0]."&gt;";
426       }
427       error_reporting (E_ALL);
428       natcasesort ($mailusers);
429       reset ($mailusers);
431       /* Show dialog */
432       $smarty->assign("search_image", get_template_path('images/search.png'));
433       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
434       $smarty->assign("tree_image", get_template_path('images/tree.png'));
435       $smarty->assign("infoimage", get_template_path('images/info.png'));
436       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
437       $smarty->assign("mailusers", $mailusers);
438       if (isset($_POST['depselect'])){
439         $smarty->assign("depselect", $_POST['depselect']);
440       }
441       $smarty->assign("deplist", $this->config->idepartments);
442       $smarty->assign("apply", apply_filter());
443       $smarty->assign("alphabet", generate_alphabet());
444       $smarty->assign("hint", print_sizelimit_warning());
445       foreach( array("depselect", "muser", "regex") as $type){
446         $smarty->assign("$type", $mailfilter[$type]);
447       }
448       $smarty->assign("hint", print_sizelimit_warning());
450       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
451       return ($display);
452     }
454     $smarty->assign("mailServers", $mailserver);
455     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
456           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
457           "gosaVacationMessage", "gosaMailDeliveryMode",
458           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
460       $smarty->assign("$val", $this->$val);
461       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
462     }
464     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
465       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
466       $smarty->assign("quotadefined", "true");
467     } else {
468       $smarty->assign("quotadefined", "false");
469     }
471     /* Disable mail field if needed */
472     $method= new $this->method($this->config);
473     if ($method->uattrib == "mail" && $this->initially_was_account){
474       $smarty->assign("mailACL", "disabled");
475     }
477     /* Fill checkboxes */
478     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
479       $smarty->assign("drop_own_mails", "checked");
480     } else {
481       $smarty->assign("drop_own_mails", "");
482     }
483     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
484       $smarty->assign("use_vacation", "checked");
485     } else {
486       $smarty->assign("use_vacation", "");
487     }
488     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
489       $smarty->assign("use_spam_filter", "checked");
490     } else {
491       $smarty->assign("use_spam_filter", "");
492     }
493     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
494       $smarty->assign("use_mailsize_limit", "checked");
495     } else {
496       $smarty->assign("use_mailsize_limit", "");
497     }
498     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
499       $smarty->assign("only_local", "checked");
500     } else {
501       $smarty->assign("only_local", "");
502     }
503     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
504       $smarty->assign("own_script", "checked");
505     } else {
506       $smarty->assign("own_script", "");
507     }
509     /* Have vacation templates? */
510     $smarty->assign("template", "");
511     if (count($this->vacation)){
512       $smarty->assign("show_templates", "true");
513       $smarty->assign("vacationtemplates", $this->vacation);
514       if (isset($_POST['vacation_template'])){
515         $smarty->assign("template", $_POST['vacation_template']);
516       }
517     } else {
518       $smarty->assign("show_templates", "false");
519     }
521     /* Fill spam selector */
522     $spamlevel= array();
523     for ($i= 0; $i<21; $i++){
524       $spamlevel[]= $i;
525     }
526     $smarty->assign("spamlevel", $spamlevel);
527     $smarty->assign("spambox", $this->mailboxList);
528     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
529     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
531     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
532     return ($display);
533   }
536   /* remove object from parent */
537   function remove_from_parent()
538   {
539     /* Cancel if there's nothing to do here */
540     if (!$this->initially_was_account){
541       return;
542     }
543     
544     /* include global link_info */
545     $ldap= $this->config->get_ldap_link();
547     /* Remove and write to LDAP */
548     plugin::remove_from_parent();
550     /* Zero arrays */
551     $this->attrs['gosaMailAlternateAddress']= array();
552     $this->attrs['gosaMailForwardingAddress']= array();
554     /* Adapt attributes if needed */
555     $method= new $this->method($this->config);
556     $method->fixAttributesOnRemove($this);
558     /* Mailmethod wants us to remove the entry from LDAP. Keep uid! */
559     #fixme: || kolab || is differs here, you can't delete all attrs specified in this plugin .... 
560     #fixme: there are some attributes we have to keep, i think.
561     unset ($this->attrs['uid']);
565     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
566         $this->attributes, "Save");
567     $ldap->cd($this->dn);
568     $this->cleanup();
569 $ldap->modify ($this->attrs); 
571     show_ldap_error($ldap->get_error());
573     /* Connect to IMAP server for account deletion */
574     if ($this->gosaMailServer != ""){
575       $method= new $this->method($this->config);
576       $id= $method->uattrib;
577       if ($method->connect($this->gosaMailServer)){
579         /* Remove account from IMAP server */
580         $method->deleteMailbox($this->folder_prefix.$this->$id);
581         $method->disconnect();
582       }
583     }
585     /* Optionally execute a command after we're done */
586     $this->handle_post_events("remove");
587   }
590   /* Save data to object */
591   function save_object()
592   {
593     if (isset($_POST['mailTab'])){
594       /* Save ldap attributes */
595       plugin::save_object();
597       /* Assemble mail delivery mode
598          The mode field in ldap consists of values between braces, this must
599          be called when 'mail' is set, because checkboxes may not be set when
600          we're in some other dialog.
602          Example for gosaMailDeliveryMode [LR        ]
603          L: Local delivery
604          R: Reject when exceeding mailsize limit
605          S: Use spam filter
606          V: Use vacation message
607          C: Use custm sieve script
608          I: Only insider delivery */
610       $tmp= "";
611       if (!isset($_POST["drop_own_mails"])){
612         $tmp.= "L";
613       }
614       if (isset($_POST["use_mailsize_limit"])){
615         $tmp.= "R";
616       }
617       if (isset($_POST["use_spam_filter"])){
618         $tmp.= "S";
619       }
620       if (isset($_POST["use_vacation"])){
621         $tmp.= "V";
622       }
623       if (isset($_POST["own_script"])){
624         $tmp.= "C";
625       }
626       if (isset($_POST["only_local"])){
627         $tmp.= "I";
628       }
629       $tmp= "[$tmp]";
631       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
632         if ($this->gosaMailDeliveryMode != $tmp){
633           $this->is_modified= TRUE;
634         }
635         $this->gosaMailDeliveryMode= $tmp;
636       }
637     }
638   }
641   /* Save data to LDAP, depending on is_account we save or delete */
642   function save()
643   {
644     $ldap= $this->config->get_ldap_link();
646     /* Call parents save to prepare $this->attrs */
647     plugin::save();
649     /* Save arrays */
650     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
651     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
653     /* Adapt attributes if needed */
654     $method= new $this->method($this->config);
655     $id= $method->uattrib;
656     $method->fixAttributesOnStore($this);
658     /* Remove Mailquota if = "" */
659     if((isset($this->attrs['gosaMailQuota']))&&($this->attrs['gosaMailQuota']=="")) {
660       $this->attrs['gosaMailQuota']=array();
661     }
663     if(empty($this->attrs['gosaSpamMailbox'])){
664       unset($this->attrs['gosaSpamMailbox']);
665     }
667     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
669     /* Save data to LDAP */
670     $ldap->cd($this->dn);
671     $this->cleanup();
672     $ldap->modify ($this->attrs); 
673   
674     show_ldap_error($ldap->get_error());
676     /* Only do IMAP actions if we are not a template */
677     if (!$this->is_template){
679       if ($method->connect($this->gosaMailServer)){
680         $method->updateMailbox($this->folder_prefix.$this->$id);
681         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
682         $method->disconnect();
684         /* Write sieve information only if not in C mode */
685         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
686           $method->configureFilter($this->$id,
687               $this->gosaMailDeliveryMode,
688               $this->mail,
689               $this->gosaMailAlternateAddress,
690               $this->gosaMailMaxSize,
691               $this->gosaSpamMailbox,
692               $this->gosaSpamSortLevel,
693               $this->gosaVacationMessage);
694         }
695       }
696     }
698     /* Optionally execute a command after we're done */
699     if ($this->initially_was_account == $this->is_account){
700       if ($this->is_modified){
701         $this->handle_post_events("modify");
702       }
703     } else {
704       $this->handle_post_events("add");
705     }
707   }
709   /* Check formular input */
710   function check()
711   {
712     if(!$this->is_account) return(array());
713     $ldap= $this->config->get_ldap_link();
715     /* Call common method to give check the hook */
716     $message= plugin::check();
718     if(empty($this->gosaMailServer)){
719       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
720     }
722     /* must: mail */
723     if ($this->mail == ""){
724       $message[]= _("The required field 'Primary address' is not set.");
725     }
726     if ($this->is_template){
727       if (!is_email($this->mail, TRUE)){
728         $message[]= _("Please enter a valid email address in 'Primary address' field.");
729       }
730     } else {
731       if (!is_email($this->mail)){
732         $message[]= _("Please enter a valid email address in 'Primary address' field.");
733       }
734     }
735     $ldap->cd($this->config->current['BASE']);
736     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
737     if ($ldap->count() != 0){
738       $message[]= _("The primary address you've entered is already in use.");
739     }
741     /* Check quota */
742     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
743       if (!is_numeric($this->gosaMailQuota)) {
744         $message[]= _("Value in 'Quota size' is not valid.");
745       } else {
746         $this->gosaMailQuota= (int) $this->gosaMailQuota;
747       }
748     }
750     /* Check rejectsize for integer */
751     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
752       if (!is_numeric($this->gosaMailMaxSize)){
753         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
754       } else {
755         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
756       }
757     }
759     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
760     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
761         $this->gosaMailMaxSize == ""){
763       $message[]= _("You need to set the maximum mail size in order to reject anything.");
764     }
766     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
767       $message[]= _("You specified Spam settings, but there is no Folder specified.");
768     }
770     return ($message);
771   }
773   /* Adapt from template, using 'dn' */
774   function adapt_from_template($dn)
775   {
776     plugin::adapt_from_template($dn);
778     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
779       $this->$val= array();
780       if (isset($this->attrs["$val"]["count"])){
781         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
782           $value= $this->attrs["$val"][$i];
783           foreach (array("sn", "givenName", "uid") as $repl){
784             if (preg_match("/%$repl/i", $value)){
785               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
786             }
787           }
788           array_push($this->$val, strtolower(rewrite($value)));
789         }
790       }
791     }
792     $this->mail= strtolower(rewrite($this->mail));
793   }
795   /* Add entry to forwarder list */
796   function addForwarder($address)
797   {
798     $this->gosaMailForwardingAddress[]= $address;
799     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
801     sort ($this->gosaMailForwardingAddress);
802     reset ($this->gosaMailForwardingAddress);
803     $this->is_modified= TRUE;
804   }
806   /* Remove list of addresses from forwarder list */
807   function delForwarder($addresses)
808   {
809     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
810     $this->is_modified= TRUE;
811   }
815   function addAlternate($address)
816   {
817     $ldap= $this->config->get_ldap_link();
819     $address= strtolower($address);
821     /* Is this address already assigned in LDAP? */
822     $ldap->cd ($this->config->current['BASE']);
823     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
825     if ($ldap->count() > 0){
826       $attrs= $ldap->fetch ();
827       return ($attrs["uid"][0]);
828     }
830     /* Add to list of alternates */
831     if (!in_array($address, $this->gosaMailAlternateAddress)){
832       $this->gosaMailAlternateAddress[]= $address;
833       $this->is_modified= TRUE;
834     }
836     sort ($this->gosaMailAlternateAddress);
837     reset ($this->gosaMailAlternateAddress);
839     return ("");
840   }
843   function delAlternate($addresses)
844   {
845     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
846                                                            $this->gosaMailAlternateAddress);
847     $this->is_modified= TRUE;
848   }
850   function make_name($attrs)
851   {
852     $name= "";
853     if (isset($attrs['sn'][0])){
854       $name= $attrs['sn'][0];
855     }
856     if (isset($attrs['givenName'][0])){
857       if ($name != ""){
858         $name.= ", ".$attrs['givenName'][0];
859       } else {
860         $name.= $attrs['givenName'][0];
861       }
862     }
863     if ($name != ""){
864       $name.= " ";
865     }
867     return ($name);
868   }
870   function getCopyDialog()
871   {
872     if(!$this->is_account) return("");
873     $smarty = get_smarty();
874     $smarty->assign("mail",$this->mail); 
875     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
876     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
877     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
879     $ret = array();
880     $ret['status'] = "";
881     $ret['string'] = $str;
882     return($ret);
883   }
885   function saveCopyDialog()
886   {
887     if(!$this->is_account) return;  
889     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
890     $this->execute();
891     
892     if(isset($_POST['mail'])){
893       $this->mail = $_POST['mail'];
894     }
896   }
899 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
900 ?>