Code

Some copy & paste fixes added some comments
[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("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'], 
416                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
417       $ldap->cd($mailfilter['depselect']);
418       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
419       error_reporting (0);
420       while ($attrs= $ldap->fetch()){
421         if(preg_match('/%/', $attrs['mail'][0])){
422           continue;
423         }
424         $name= $this->make_name($attrs);
425         $mailusers[$attrs['mail'][0]]= $name."&lt;".
426           $attrs['mail'][0]."&gt;";
427       }
428       error_reporting (E_ALL);
429       natcasesort ($mailusers);
430       reset ($mailusers);
432       /* Show dialog */
433       $smarty->assign("search_image", get_template_path('images/search.png'));
434       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
435       $smarty->assign("tree_image", get_template_path('images/tree.png'));
436       $smarty->assign("infoimage", get_template_path('images/info.png'));
437       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
438       $smarty->assign("mailusers", $mailusers);
439       if (isset($_POST['depselect'])){
440         $smarty->assign("depselect", $_POST['depselect']);
441       }
442       $smarty->assign("deplist", $this->config->idepartments);
443       $smarty->assign("apply", apply_filter());
444       $smarty->assign("alphabet", generate_alphabet());
445       $smarty->assign("hint", print_sizelimit_warning());
446       foreach( array("depselect", "muser", "regex") as $type){
447         $smarty->assign("$type", $mailfilter[$type]);
448       }
449       $smarty->assign("hint", print_sizelimit_warning());
451       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
452       return ($display);
453     }
455     $smarty->assign("mailServers", $mailserver);
456     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
457           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
458           "gosaVacationMessage", "gosaMailDeliveryMode",
459           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
461       $smarty->assign("$val", $this->$val);
462       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
463     }
465     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
466       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
467       $smarty->assign("quotadefined", "true");
468     } else {
469       $smarty->assign("quotadefined", "false");
470     }
472     /* Disable mail field if needed */
473     $method= new $this->method($this->config);
474     if ($method->uattrib == "mail" && $this->initially_was_account){
475       $smarty->assign("mailACL", "disabled");
476     }
478     /* Fill checkboxes */
479     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
480       $smarty->assign("drop_own_mails", "checked");
481     } else {
482       $smarty->assign("drop_own_mails", "");
483     }
484     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
485       $smarty->assign("use_vacation", "checked");
486     } else {
487       $smarty->assign("use_vacation", "");
488     }
489     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
490       $smarty->assign("use_spam_filter", "checked");
491     } else {
492       $smarty->assign("use_spam_filter", "");
493     }
494     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
495       $smarty->assign("use_mailsize_limit", "checked");
496     } else {
497       $smarty->assign("use_mailsize_limit", "");
498     }
499     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
500       $smarty->assign("only_local", "checked");
501     } else {
502       $smarty->assign("only_local", "");
503     }
504     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
505       $smarty->assign("own_script", "checked");
506     } else {
507       $smarty->assign("own_script", "");
508     }
510     /* Have vacation templates? */
511     $smarty->assign("template", "");
512     if (count($this->vacation)){
513       $smarty->assign("show_templates", "true");
514       $smarty->assign("vacationtemplates", $this->vacation);
515       if (isset($_POST['vacation_template'])){
516         $smarty->assign("template", $_POST['vacation_template']);
517       }
518     } else {
519       $smarty->assign("show_templates", "false");
520     }
522     /* Fill spam selector */
523     $spamlevel= array();
524     for ($i= 0; $i<21; $i++){
525       $spamlevel[]= $i;
526     }
527     $smarty->assign("spamlevel", $spamlevel);
528     $smarty->assign("spambox", $this->mailboxList);
529     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
530     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
532     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
533     return ($display);
534   }
537   /* remove object from parent */
538   function remove_from_parent()
539   {
540     /* Cancel if there's nothing to do here */
541     if (!$this->initially_was_account){
542       return;
543     }
544     
545     /* include global link_info */
546     $ldap= $this->config->get_ldap_link();
548     /* Remove and write to LDAP */
549     plugin::remove_from_parent();
551     /* Zero arrays */
552     $this->attrs['gosaMailAlternateAddress']= array();
553     $this->attrs['gosaMailForwardingAddress']= array();
555     /* Adapt attributes if needed */
556     $method= new $this->method($this->config);
557     $method->fixAttributesOnRemove($this);
559     /* Mailmethod wants us to remove the entry from LDAP. Keep uid! */
560     #fixme: || kolab || is differs here, you can't delete all attrs specified in this plugin .... 
561     #fixme: there are some attributes we have to keep, i think.
562     unset ($this->attrs['uid']);
566     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
567         $this->attributes, "Save");
568     $ldap->cd($this->dn);
569     $this->cleanup();
570 $ldap->modify ($this->attrs); 
572     show_ldap_error($ldap->get_error());
574     /* Connect to IMAP server for account deletion */
575     if ($this->gosaMailServer != ""){
576       $method= new $this->method($this->config);
577       $id= $method->uattrib;
578       if ($method->connect($this->gosaMailServer)){
580         /* Remove account from IMAP server */
581         $method->deleteMailbox($this->folder_prefix.$this->$id);
582         $method->disconnect();
583       }
584     }
586     /* Optionally execute a command after we're done */
587     $this->handle_post_events("remove");
588   }
591   /* Save data to object */
592   function save_object()
593   {
594     if (isset($_POST['mailTab'])){
595       /* Save ldap attributes */
596       plugin::save_object();
598       /* Assemble mail delivery mode
599          The mode field in ldap consists of values between braces, this must
600          be called when 'mail' is set, because checkboxes may not be set when
601          we're in some other dialog.
603          Example for gosaMailDeliveryMode [LR        ]
604          L: Local delivery
605          R: Reject when exceeding mailsize limit
606          S: Use spam filter
607          V: Use vacation message
608          C: Use custm sieve script
609          I: Only insider delivery */
611       $tmp= "";
612       if (!isset($_POST["drop_own_mails"])){
613         $tmp.= "L";
614       }
615       if (isset($_POST["use_mailsize_limit"])){
616         $tmp.= "R";
617       }
618       if (isset($_POST["use_spam_filter"])){
619         $tmp.= "S";
620       }
621       if (isset($_POST["use_vacation"])){
622         $tmp.= "V";
623       }
624       if (isset($_POST["own_script"])){
625         $tmp.= "C";
626       }
627       if (isset($_POST["only_local"])){
628         $tmp.= "I";
629       }
630       $tmp= "[$tmp]";
632       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
633         if ($this->gosaMailDeliveryMode != $tmp){
634           $this->is_modified= TRUE;
635         }
636         $this->gosaMailDeliveryMode= $tmp;
637       }
638     }
639   }
642   /* Save data to LDAP, depending on is_account we save or delete */
643   function save()
644   {
645     $ldap= $this->config->get_ldap_link();
647     /* Call parents save to prepare $this->attrs */
648     plugin::save();
650     /* Save arrays */
651     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
652     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
654     /* Adapt attributes if needed */
655     $method= new $this->method($this->config);
656     $id= $method->uattrib;
657     $method->fixAttributesOnStore($this);
659     /* Remove Mailquota if = "" */
660     if((isset($this->attrs['gosaMailQuota']))&&($this->attrs['gosaMailQuota']=="")) {
661       $this->attrs['gosaMailQuota']=array();
662     }
664     if(empty($this->attrs['gosaSpamMailbox'])){
665       unset($this->attrs['gosaSpamMailbox']);
666     }
668     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
670     /* Save data to LDAP */
671     $ldap->cd($this->dn);
672     $this->cleanup();
673     $ldap->modify ($this->attrs); 
674   
675     show_ldap_error($ldap->get_error());
677     /* Only do IMAP actions if we are not a template */
678     if (!$this->is_template){
680       if ($method->connect($this->gosaMailServer)){
681         $method->updateMailbox($this->folder_prefix.$this->$id);
682         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
683         $method->disconnect();
685         /* Write sieve information only if not in C mode */
686         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
687           $method->configureFilter($this->$id,
688               $this->gosaMailDeliveryMode,
689               $this->mail,
690               $this->gosaMailAlternateAddress,
691               $this->gosaMailMaxSize,
692               $this->gosaSpamMailbox,
693               $this->gosaSpamSortLevel,
694               $this->gosaVacationMessage);
695         }
696       }
697     }
699     /* Optionally execute a command after we're done */
700     if ($this->initially_was_account == $this->is_account){
701       if ($this->is_modified){
702         $this->handle_post_events("modify");
703       }
704     } else {
705       $this->handle_post_events("add");
706     }
708   }
710   /* Check formular input */
711   function check()
712   {
713     if(!$this->is_account) return(array());
714     $ldap= $this->config->get_ldap_link();
716     /* Call common method to give check the hook */
717     $message= plugin::check();
719     if(empty($this->gosaMailServer)){
720       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
721     }
723     /* must: mail */
724     if ($this->mail == ""){
725       $message[]= _("The required field 'Primary address' is not set.");
726     }
727     if ($this->is_template){
728       if (!is_email($this->mail, TRUE)){
729         $message[]= _("Please enter a valid email address in 'Primary address' field.");
730       }
731     } else {
732       if (!is_email($this->mail)){
733         $message[]= _("Please enter a valid email address in 'Primary address' field.");
734       }
735     }
736     $ldap->cd($this->config->current['BASE']);
737     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
738     if ($ldap->count() != 0){
739       $message[]= _("The primary address you've entered is already in use.");
740     }
742     /* Check quota */
743     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
744       if (!is_numeric($this->gosaMailQuota)) {
745         $message[]= _("Value in 'Quota size' is not valid.");
746       } else {
747         $this->gosaMailQuota= (int) $this->gosaMailQuota;
748       }
749     }
751     /* Check rejectsize for integer */
752     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
753       if (!is_numeric($this->gosaMailMaxSize)){
754         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
755       } else {
756         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
757       }
758     }
760     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
761     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
762         $this->gosaMailMaxSize == ""){
764       $message[]= _("You need to set the maximum mail size in order to reject anything.");
765     }
767     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
768       $message[]= _("You specified Spam settings, but there is no Folder specified.");
769     }
771     return ($message);
772   }
774   /* Adapt from template, using 'dn' */
775   function adapt_from_template($dn)
776   {
777     plugin::adapt_from_template($dn);
779     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
780       $this->$val= array();
781       if (isset($this->attrs["$val"]["count"])){
782         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
783           $value= $this->attrs["$val"][$i];
784           foreach (array("sn", "givenName", "uid") as $repl){
785             if (preg_match("/%$repl/i", $value)){
786               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
787             }
788           }
789           array_push($this->$val, strtolower(rewrite($value)));
790         }
791       }
792     }
793     $this->mail= strtolower(rewrite($this->mail));
794   }
796   /* Add entry to forwarder list */
797   function addForwarder($address)
798   {
799     $this->gosaMailForwardingAddress[]= $address;
800     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
802     sort ($this->gosaMailForwardingAddress);
803     reset ($this->gosaMailForwardingAddress);
804     $this->is_modified= TRUE;
805   }
807   /* Remove list of addresses from forwarder list */
808   function delForwarder($addresses)
809   {
810     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
811     $this->is_modified= TRUE;
812   }
816   function addAlternate($address)
817   {
818     $ldap= $this->config->get_ldap_link();
820     $address= strtolower($address);
822     /* Is this address already assigned in LDAP? */
823     $ldap->cd ($this->config->current['BASE']);
824     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
826     if ($ldap->count() > 0){
827       $attrs= $ldap->fetch ();
828       return ($attrs["uid"][0]);
829     }
831     /* Add to list of alternates */
832     if (!in_array($address, $this->gosaMailAlternateAddress)){
833       $this->gosaMailAlternateAddress[]= $address;
834       $this->is_modified= TRUE;
835     }
837     sort ($this->gosaMailAlternateAddress);
838     reset ($this->gosaMailAlternateAddress);
840     return ("");
841   }
844   function delAlternate($addresses)
845   {
846     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
847                                                            $this->gosaMailAlternateAddress);
848     $this->is_modified= TRUE;
849   }
851   function make_name($attrs)
852   {
853     $name= "";
854     if (isset($attrs['sn'][0])){
855       $name= $attrs['sn'][0];
856     }
857     if (isset($attrs['givenName'][0])){
858       if ($name != ""){
859         $name.= ", ".$attrs['givenName'][0];
860       } else {
861         $name.= $attrs['givenName'][0];
862       }
863     }
864     if ($name != ""){
865       $name.= " ";
866     }
868     return ($name);
869   }
871   
872   /* Create the mail part for the copy & paste dialog */
873   function getCopyDialog()
874   {
875     if(!$this->is_account) return("");
876     $smarty = get_smarty();
877     $smarty->assign("mail",$this->mail); 
878     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
879     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
880     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
882     $ret = array();
883     $ret['status'] = "";
884     $ret['string'] = $str;
885     return($ret);
886   }
888   function saveCopyDialog()
889   {
890     if(!$this->is_account) return;  
892     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
893     $this->execute();
894     
895     if(isset($_POST['mail'])){
896       $this->mail = $_POST['mail'];
897     }
899   }
902 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
903 ?>