Code

Just use the -n option from dh_installinit and the world is fine again.
[gosa.git] / 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("INBOX");
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, $parent= NULL)
69   {
70     /* Load bases attributes */
71     plugin::plugin($config, $dn, $parent);
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);
128         /* FixAttributesOnLoad possibly creates an array out of gosaMailServer.
129             If the mail tab wasn't opened once before saving, the account can't be saved */
130         if(is_array($this->gosaMailServer)){
131           $this->gosaMailServer = $this->gosaMailServer[0];
132         }
134         if ($method->connect($this->attrs["gosaMailServer"][0])){
136           /* Update quota values */
137           $quota= $method->getQuota($this->folder_prefix.$this->$id);
138          
139           if($quota){
140             if ($quota['gosaMailQuota'] == 2147483647){
141               $this->quotaUsage     = "";
142               $this->gosaMailQuota  = "";
143             } else {
144               $this->quotaUsage     = $quota['quotaUsage'];
145               $this->gosaMailQuota  = $quota['gosaMailQuota'];
146             }
147           }else{
148             $this->quotaUsage     = "";
149             $this->gosaMailQuota  = "";
150 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
151           }
153           /* Get mailboxes / folder like INBOX ..*/
154           $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
155           
156           $method->disconnect();
157         }else{
158           /* Could not connect to ldap.
159            */
160           if (isset($this->attrs['gosaMailQuota'][0])){
161             $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
162           }
163         }
164       }
165     }
168     /* Get vacation message */
170     /* Fill vacation array */
171     $this->vacation= array();
172     if (isset($this->config->current['VACATIONDIR'])){
173       $dir= $this->config->current['VACATIONDIR'];
174       if (is_dir($dir) && is_readable($dir)){
176         /* Look for files and build the vacation array */
177         $dh= opendir($dir);
178         while ($file = readdir($dh)){
179           $description= $this->parse_vacation("$dir/$file");
180           if ($description != ""){
181             $this->vacation["$dir/$file"]= $description;
182           }
183         }
184         closedir($dh);
185       }
186     }
189   /* Create filter */
191     /* Get global filter config */
192     if (!is_global("mailfilter")){
193       $ui= get_userinfo();
194       $base= get_base_from_people($ui->dn);
195       $mailfilter= array( "depselect"       => $base,
196           "muser"            => "",
197           "regex"           => "*");
198       register_global("mailfilter", $mailfilter);
199     }
200   }
203   function parse_vacation($file)
204   {
205     $desc= "";
207     if (is_file($file)){
208       $fh = fopen($file, "r");
209       $line= fgets($fh, 256);
211       if (!preg_match('/^DESC:/', $line)){
212         print_red (_("No DESC tag in vacation file:")." $file");
213         return $desc;
214       }
215       fclose ($fh);
217       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
218     }
220     return $desc;
221   }
224   function execute()
225   {
226     /* Call parent execute */
227     plugin::execute();
229     /* Initialise vars */
231     /* Load templating engine */
232     $smarty= get_smarty();
233     $display= "";
235     /* Get available mailserver */
236     $mailserver= array();
237     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
238       $mailserver[]= $key;
239     }
241     /* Handle account state */
243     /* Do we need to flip is_account state? */
244     if (isset($_POST['modify_state'])){
245       $this->is_account= !$this->is_account;
246     }
248     /* Do we represent a valid account? */
249     if (!$this->is_account && $this->parent == NULL){
250       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
251         _("This account has no mail extensions.")."</b>";
253       $display.= back_to_main();
254       return ($display);
255     }
257     /* Show tab dialog headers */
258     if ($this->parent != NULL){
259       if ($this->is_account){
260         if($this->accountDelegationsConfigured()){
261           $display= $this->show_header(_("Remove mail account"),
262               _("This account can't be removed while there are delegations configured. Remove those delegations first."),TRUE,TRUE);
263         }else{
264           $display= $this->show_header(_("Remove mail account"),
265               _("This account has mail features enabled. You can disable them by clicking below."));
266         }
267       } else {
268         $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
269         return ($display);
270       }
271     }
273     /* Forwarder  subdialog */
275     /* Trigger forward add dialog? */
276     if (isset($_POST['add_local_forwarder'])){
277       $this->forward_dialog= TRUE;
278       $this->dialog= TRUE;
279     }
281     /* Cancel forward add dialog? */
282     if (isset($_POST['add_locals_cancel'])){
283       $this->forward_dialog= FALSE;
284       $this->dialog= FALSE;
285     }
287     /* Finished adding of locals? */
288     if (isset($_POST['add_locals_finish'])){
289       if (count ($_POST['local_list']) &&
290           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
292         /* Walk through list of forwarders, ignore own addresses */
293         foreach ($_POST['local_list'] as $val){
294           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
295               $val != $this->mail){
297             $this->addForwarder($val);
298             $this->is_modified= TRUE;
299           }
300         }
301       }
302       $this->forward_dialog= FALSE;
303       $this->dialog= FALSE;
304     }
306     /* Add forward email addresses */
307     if (isset($_POST['add_forwarder'])){
308       if ($_POST['forward_address'] != ""){
310         /* Valid email address specified? */
311         $address= $_POST['forward_address'];
312         $valid= FALSE;
313         if (!is_email($address)){
314           if (!is_email($address, TRUE)){
315             if ($this->is_template){
316               $valid= TRUE;
317             } else {
318               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
319             }
320           }
321         } elseif ($address == $this->mail
322             || in_array($address, $this->gosaMailAlternateAddress)) {
324           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
326         } else {
327           $valid= TRUE;
328         }
330         if ($valid){
331           /* Add it */
332           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
333             $this->addForwarder ($address);
334             $this->is_modified= TRUE;
335           }
337         }
338       }
339     }
341     /* Delete forward email addresses */
342     if (isset($_POST['delete_forwarder'])){
343       if (count($_POST['forwarder_list']) 
344           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
346         $this->delForwarder ($_POST['forwarder_list']);
347       }
348     }
350     
351     /* Alternate address handling */
353     /* Add alternate email addresses */
354     if (isset($_POST['add_alternate'])){
355       if ($_POST['alternate_address'] != "" &&
356           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
358         $valid= FALSE;
359         if (!is_email($_POST['alternate_address'])){
360           if ($this->is_template){
361             if (!(is_email($_POST['alternate_address'], TRUE))){
362               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
363             } else {
364               $valid= TRUE;
365             }
366           } else {
367             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
368           }
370         } else {
371           $valid= TRUE;
372         }
374         if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
375           $ui= get_userinfo();
376           if ($user != $ui->username){
377             print_red (_("The address you're trying to add is already used by user")." '$user'.");
378           }
379         }
380       }
381     }
383     /* Delete alternate email addresses */
384     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
385       if (count($_POST['alternates_list']) &&
386           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
388         $this->delAlternate ($_POST['alternates_list']);
389       }
390     }
392   
393     /* Vocation message */
394   
395     /* Import vacation message? */
396     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
397       $contents= "";
398       $lines= file($_POST["vacation_template"]);
399       foreach ($lines as $line){
400         if (!preg_match('/^DESC:/', $line)){
401           $contents.= $line;
402         }
403       }
405       /* Replace attributes */
406       $attrs= $this->parent->by_object['user']->attributes;
407       foreach ($attrs as $val){
408         $contents= preg_replace("/%$val/",
409             $this->parent->by_object['user']->$val, $contents);
410       }
412       /* Save message */
413       $this->gosaVacationMessage= htmlspecialchars($contents);
414     }
416   
417     /* Display forward dialog if requested above */
419     /* Show forward add dialog */
420     if ($this->forward_dialog){
421       $ldap= $this->config->get_ldap_link();
423       /* Save data */
424       $mailfilter= get_global("mailfilter");
425       foreach( array("depselect", "muser", "regex") as $type){
426         if (isset($_POST[$type])){
427           $mailfilter[$type]= $_POST[$type];
428         }
429       }
430       if (isset($_GET['search'])){
431         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
432         if ($s == "**"){
433           $s= "*";
434         }
435         $mailfilter['regex']= $s;
436       }
437       register_global("mailfilter", $mailfilter);
439       /* Get actual list */
440       $mailusers= array ();
441       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
442         $regex= $mailfilter['regex'];
443         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
444       } else {
445         $filter= "";
446       }
447       if ($mailfilter['muser'] != ""){
448         $user= $mailfilter['muser'];
449         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
450       }
452       /* Add already present people to the filter */
453       $exclude= "";
454       foreach ($this->gosaMailForwardingAddress as $mail){
455         $exclude.= "(mail=$mail)";
456       }
457       if ($exclude != ""){
458         $filter.= "(!(|$exclude))";
459       }
461       $acl= array($this->config->current['BASE'] => ":all");
462       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'], 
463                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
464       $ldap->cd($mailfilter['depselect']);
465       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
466       error_reporting (0);
467       while ($attrs= $ldap->fetch()){
468         if(preg_match('/%/', $attrs['mail'][0])){
469           continue;
470         }
471         $name= $this->make_name($attrs);
472         $mailusers[$attrs['mail'][0]]= $name."&lt;".
473           $attrs['mail'][0]."&gt;";
474       }
475       error_reporting (E_ALL);
476       natcasesort ($mailusers);
477       reset ($mailusers);
479       /* Show dialog */
480       $smarty->assign("search_image", get_template_path('images/search.png'));
481       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
482       $smarty->assign("tree_image", get_template_path('images/tree.png'));
483       $smarty->assign("infoimage", get_template_path('images/info.png'));
484       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
485       $smarty->assign("mailusers", $mailusers);
486       if (isset($_POST['depselect'])){
487         $smarty->assign("depselect", $_POST['depselect']);
488       }
489       $smarty->assign("deplist", $this->config->idepartments);
490       $smarty->assign("apply", apply_filter());
491       $smarty->assign("alphabet", generate_alphabet());
492       $smarty->assign("hint", print_sizelimit_warning());
493       foreach( array("depselect", "muser", "regex") as $type){
494         $smarty->assign("$type", $mailfilter[$type]);
495       }
496       $smarty->assign("hint", print_sizelimit_warning());
498       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
499       return ($display);
500     }
502     /* Display mail account tab */
504     $smarty->assign("mailServers", $mailserver);
505     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
506           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
507           "gosaVacationMessage", "gosaMailDeliveryMode",
508           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
510       $smarty->assign("$val", $this->$val);
511       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
512     }
514     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
515       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
516       $smarty->assign("quotadefined", "true");
517     } else {
518       $smarty->assign("quotadefined", "false");
519     }
521     /* Disable mail field if needed */
522     $method= new $this->method($this->config);
523     if ($method->uattrib == "mail" && $this->initially_was_account){
524       $smarty->assign("mailACL", "disabled");
525     }
528     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
529       $smarty->assign("drop_own_mails", "checked");
530     } else {
531       $smarty->assign("drop_own_mails", "");
532     }
534     $types = array(
535           "V"=>"use_vacation",
536           "S"=>"use_spam_filter",
537           "R"=>"use_mailsize_limit",
538           "I"=>"only_local",
539           "C"=>"own_script");
541     /* Fill checkboxes */
542     foreach($types as $option => $varname){
543       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
544         $smarty->assign($varname, "checked");
545       } else {
546         $smarty->assign($varname, "");
547       }
548     }
549     
550     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
551       $smarty->assign("use_vacation", "checked");
552     } else {
553       $smarty->assign("use_vacation", "");
554     }
555     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
556       $smarty->assign("use_spam_filter", "checked");
557     } else {
558       $smarty->assign("use_spam_filter", "");
559     }
560     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
561       $smarty->assign("use_mailsize_limit", "checked");
562     } else {
563       $smarty->assign("use_mailsize_limit", "");
564     }
565     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
566       $smarty->assign("only_local", "checked");
567     } else {
568       $smarty->assign("only_local", "");
569     }
570     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
571       $smarty->assign("own_script", "checked");
572     } else {
573       $smarty->assign("own_script", "");
574     }
576     /* Have vacation templates? */
577     $smarty->assign("template", "");
578     if (count($this->vacation)){
579       $smarty->assign("show_templates", "true");
580       $smarty->assign("vacationtemplates", $this->vacation);
581       if (isset($_POST['vacation_template'])){
582         $smarty->assign("template", $_POST['vacation_template']);
583       }
584     } else {
585       $smarty->assign("show_templates", "false");
586     }
588     /* Fill spam selector */
589     $spamlevel= array();
590     for ($i= 0; $i<21; $i++){
591       $spamlevel[]= $i;
592     }
593     $smarty->assign("spamlevel", $spamlevel);
594     $smarty->assign("spambox", $this->mailboxList);
595     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
596     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
598     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
599     return ($display);
600   }
602   /* check if we have some delegations configured, those delegations must be removed first */
603   function accountDelegationsConfigured()
604   { 
605     /* We are in administrational edit mode.
606         Check tab configurations directly */
607     if(isset($this->attrs)){
608       $checkArray  = array("kolabInvitationPolicy","unrestrictedMailSize", "calFBURL","kolabDelegate","kolabFreeBusyFuture");
609       foreach($checkArray as $index){
610         if(isset($this->attrs[$index])){
611            return(true);
612         }
613       }
614     }
615     return(false); 
616   }
619   /* remove object from parent */
620   function remove_from_parent()
621   {
622     /* Cancel if there's nothing to do here */
623     if (!$this->initially_was_account){
624       return;
625     }
626     
627     /* include global link_info */
628     $ldap= $this->config->get_ldap_link();
630     /* Remove and write to LDAP */
631     plugin::remove_from_parent();
633     /* Zero arrays */
634     $this->attrs['gosaMailAlternateAddress']= array();
635     $this->attrs['gosaMailForwardingAddress']= array();
637     /* Adapt attributes if needed */
638     $method= new $this->method($this->config);
639     $method->fixAttributesOnRemove($this);
641     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
642     $ldap->cd($this->dn);
643     $this->cleanup();
644     $ldap->modify ($this->attrs); 
646     show_ldap_error($ldap->get_error(), _("Removing mail account failed"));
648     /* Connect to IMAP server for account deletion */
649     if ($this->gosaMailServer != ""){
650       $method= new $this->method($this->config);
651       $id= $method->uattrib;
652       if ($method->connect($this->gosaMailServer)){
654         /* Remove account from IMAP server */
655         $method->deleteMailbox($this->folder_prefix.$this->$id);
656         $method->disconnect();
657       }
658     }
660     /* Optionally execute a command after we're done */
661     $this->handle_post_events("remove", array('uid'=> $this->uid));
662   }
665   /* Save data to object */
666   function save_object()
667   {
668     if (isset($_POST['mailTab'])){
669       /* Save ldap attributes */
670       plugin::save_object();
672       /* Assemble mail delivery mode
673          The mode field in ldap consists of values between braces, this must
674          be called when 'mail' is set, because checkboxes may not be set when
675          we're in some other dialog.
677          Example for gosaMailDeliveryMode [LR        ]
678          L: Local delivery
679          R: Reject when exceeding mailsize limit
680          S: Use spam filter
681          V: Use vacation message
682          C: Use custm sieve script
683          I: Only insider delivery */
685       $tmp= "";
686       if (!isset($_POST["drop_own_mails"])){
687         $tmp.= "L";
688       }
689       if (isset($_POST["use_mailsize_limit"])){
690         $tmp.= "R";
691       }
692       if (isset($_POST["use_spam_filter"])){
693         $tmp.= "S";
694       }
695       if (isset($_POST["use_vacation"])){
696         $tmp.= "V";
697       }
698       if (isset($_POST["own_script"])){
699         $tmp.= "C";
700       }
701       if (isset($_POST["only_local"])){
702         $tmp.= "I";
703       }
704       $tmp= "[$tmp]";
706       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
707         if ($this->gosaMailDeliveryMode != $tmp){
708           $this->is_modified= TRUE;
709         }
710         $this->gosaMailDeliveryMode= $tmp;
711       }
712     }
713   }
716   /* Save data to LDAP, depending on is_account we save or delete */
717   function save()
718   {
719     $ldap= $this->config->get_ldap_link();
721     /* Call parents save to prepare $this->attrs */
722     plugin::save();
724     /* Save arrays */
725     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
726     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
728     /* Adapt attributes if needed */
729     $method= new $this->method($this->config);
730     $id= $method->uattrib;
732     $method->fixAttributesOnStore($this);
734     /* Remove Mailquota if = "" or "0"  */
735     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
736       $this->attrs['gosaMailQuota']=0;
737     }
739     if(empty($this->attrs['gosaSpamMailbox'])){
740       unset($this->attrs['gosaSpamMailbox']);
741     }
743     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
745     /* Save data to LDAP */
746     $ldap->cd($this->dn);
747     $this->cleanup();
748     $ldap->modify ($this->attrs); 
750     show_ldap_error($ldap->get_error(), _("Saving mail account failed"));
752     /* Only do IMAP actions if we are not a template */
753     if (!$this->is_template){
755       if ($method->connect($this->gosaMailServer)){
756         $method->updateMailbox($this->folder_prefix.$this->$id);
757         
758         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
759         $method->disconnect();
761         /* Only talk with sieve if the mail account already exists */
762         if($this->initially_was_account){
763           
764           /* Write sieve information only if not in C mode */
765           if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
766             $method->configureFilter($this->$id,
767                 $this->gosaMailDeliveryMode,
768                 $this->mail,
769                 $this->gosaMailAlternateAddress,
770                 $this->gosaMailMaxSize,
771                 $this->gosaSpamMailbox,
772                 $this->gosaSpamSortLevel,
773                 $this->gosaVacationMessage);
774           }
775         }
776       }
777     }
779     /* Optionally execute a command after we're done */
780     if ($this->initially_was_account == $this->is_account){
781       if ($this->is_modified){
782         $this->handle_post_events("modify", array('uid'=> $this->uid));
783       }
784     } else {
785       $this->handle_post_events("add", array('uid'=>$this->uid));
786     }
788   }
791   /* Check formular input */
792   function check()
793   {
794     if(!$this->is_account) return(array());
796     $ldap= $this->config->get_ldap_link();
798     /* Call common method to give check the hook */
799     $message= plugin::check();
801     if(empty($this->gosaMailServer)){
802       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
803     }
805     /* must: mail */
806     if ($this->mail == ""){
807       $message[]= _("The required field 'Primary address' is not set.");
808     }
809     if ($this->is_template){
810       if (!is_email($this->mail, TRUE)){
811         $message[]= _("Please enter a valid email address in 'Primary address' field.");
812       }
813     } else {
814       if (!is_email($this->mail)){
815         $message[]= _("Please enter a valid email address in 'Primary address' field.");
816       }
817     }
818     $ldap->cd($this->config->current['BASE']);
819     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
820     if ($ldap->count() != 0){
821       $message[]= _("The primary address you've entered is already in use.");
822     }
824     /* Check quota */
825     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
826       if (!is_numeric($this->gosaMailQuota)) {
827         $message[]= _("Value in 'Quota size' is not valid.");
828       } else {
829         $this->gosaMailQuota= (int) $this->gosaMailQuota;
830       }
831     }
833     /* Check rejectsize for integer */
834     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
835       if (!is_numeric($this->gosaMailMaxSize)){
836         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
837       } else {
838         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
839       }
840     }
842     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
843     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
844         $this->gosaMailMaxSize == ""){
846       $message[]= _("You need to set the maximum mail size in order to reject anything.");
847     }
849     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
850       $message[]= _("You specified Spam settings, but there is no Folder specified.");
851     }
853     return ($message);
854   }
857   /* Adapt from template, using 'dn' */
858   function adapt_from_template($dn)
859   {
860     plugin::adapt_from_template($dn);
862     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
863       $this->$val= array();
864       if (isset($this->attrs["$val"]["count"])){
865         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
866           $value= $this->attrs["$val"][$i];
867           foreach (array("sn", "givenName", "uid") as $repl){
868             if (preg_match("/%$repl/i", $value)){
869               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
870             }
871           }
872           array_push($this->$val, strtolower(rewrite($value)));
873         }
874       }
875     }
876     $this->mail= strtolower(rewrite($this->mail));
877   }
880   /* Add entry to forwarder list */
881   function addForwarder($address)
882   {
883     $this->gosaMailForwardingAddress[]= $address;
884     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
885     sort ($this->gosaMailForwardingAddress);
886     reset ($this->gosaMailForwardingAddress);
887     $this->is_modified= TRUE;
888   }
891   /* Remove list of addresses from forwarder list */
892   function delForwarder($addresses)
893   {
894     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
895     $this->is_modified= TRUE;
896   }
899   /* Add given mail address to the list of alternate adresses , 
900       check if this mal address is used, skip adding in this case */
901   function addAlternate($address)
902   {
903     $ldap= $this->config->get_ldap_link();
904     $address= strtolower($address);
905       
906     /* Is this address already assigned in LDAP? */
907     $ldap->cd ($this->config->current['BASE']);
908     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
910     if ($ldap->count() > 0){
911       $attrs= $ldap->fetch ();
912       return ($attrs["uid"][0]);
913     }
915     /* Add to list of alternates */
916     if (!in_array($address, $this->gosaMailAlternateAddress)){
917       $this->gosaMailAlternateAddress[]= $address;
918       $this->is_modified= TRUE;
919     }
921     sort ($this->gosaMailAlternateAddress);
922     reset ($this->gosaMailAlternateAddress);
923     return ("");
924   }
927   function delAlternate($addresses)
928   {
929     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
930                                                            $this->gosaMailAlternateAddress);
931     $this->is_modified= TRUE;
932   }
934   function make_name($attrs)
935   {
936     $name= "";
937     if (isset($attrs['sn'][0])){
938       $name= $attrs['sn'][0];
939     }
940     if (isset($attrs['givenName'][0])){
941       if ($name != ""){
942         $name.= ", ".$attrs['givenName'][0];
943       } else {
944         $name.= $attrs['givenName'][0];
945       }
946     }
947     if ($name != ""){
948       $name.= " ";
949     }
951     return ($name);
952   }
954   
955   /* Create the mail part for the copy & paste dialog */
956   function getCopyDialog()
957   {
958     if(!$this->is_account) return("");
959     $smarty = get_smarty();
960     $smarty->assign("mail",$this->mail); 
961     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
962     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
963     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
965     $ret = array();
966     $ret['status'] = "";
967     $ret['string'] = $str;
968     return($ret);
969   }
971   function saveCopyDialog()
972   {
973     if(!$this->is_account) return;  
975     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
976     $this->execute();
977     
978     if(isset($_POST['mail'])){
979       $this->mail = $_POST['mail'];
980     }
982   }
984   function allow_remove()
985   {
986     if (isset($this->config->current['MAILMETHOD'])){
987       $method= $this->config->current['MAILMETHOD'];
988       if(preg_match("/kolab/i",$method)){
989         $ldap = $this->config->get_ldap_link();
990         $ldap->cd($this->config->current['BASE']);
991         $ldap->cat($this->dn);
992         if($ldap->count()){
993           $attrs = $ldap->fetch();
994           if(isset($attrs['kolabDeleteFlag'])){ 
995             return(_("Waiting for kolab to remove mail properties."));
996           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
997             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
998           }
999         }
1000       }
1001     }
1002   }
1005 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1006 ?>