Code

Fixed errors
[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 $gosaMailAlternateAddress           = array();
34   var $gosaMailForwardingAddress          = array();
35   var $gosaMailDeliveryMode               = "[L        ]";
36   var $gosaMailServer                     = "";
37   var $gosaMailQuota                      = "";
38   var $gosaMailMaxSize                    = "";
39   var $gosaVacationMessage                = "";
40   var $gosaSpamSortLevel                  = "";
41   var $gosaSpamMailbox                    = "";
43   var $quotaUsage                         = 0;
44   var $forward_dialog                     = FALSE;
45   var $folder_prefix                      = "";
46   var $mailboxList                        = array();
47   var $default_permissions                = "none";
48   var $member_permissions                 = "post";
49   var $members                            = array();
50   var $admins                             = array();
51   var $vacations                          = array();
52   var $perms                              = array(  "lrs"       => "read", 
53                                                     "lrsp"      => "post", 
54                                                     "lrsip"     => "append",
55                                                     "lrswipcd"  => "write", 
56                                                     "lrswipcda" => "all" );
58   /* attribute list for save action */
59   var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
60       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
61       "gosaVacationMessage", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
62   var $objectclasses= array("gosaMailAccount");
63   var $uid;
65   /* constructor, if 'dn' is set, the node loads the given
66      'dn' from LDAP */
67   function mailAccount ($config, $dn= NULL)
68   {
69     /* Load bases attributes */
70     plugin::plugin($config, $dn);
72     /* Set uid */
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     }elseif (isset($this->config->data['MAIN']['CYRUSUNIXSTYLE']) && $this->config->data['MAIN']['CYRUSUNIXSTYLE'] == "true"){
101       $this->folder_prefix= "user/";
102     } else {
103       $this->folder_prefix= "user.";
104     }
105     
106     /* This is not a new account, parse additional attributes */
107     if (($dn != NULL) && ($dn != "new") && $this->is_account){
109       /* Load attributes containing arrays */
110       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
111         $this->$val= array();
112         if (isset($this->attrs["$val"]["count"])){
113           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
114             array_push($this->$val, $this->attrs["$val"][$i]);
115           }
116         }
117       }
120       /* Only do IMAP actions if gosaMailServer attribute is set */
121       if (isset ($this->attrs["gosaMailServer"][0])){
123         $method = new $this->method($this->config);
124         $id     = $method->uattrib;
126         /* Adapt attributes if needed */
127         $method->fixAttributesOnLoad($this);
129         /* FixAttributesOnLoad possibly creates an array out of gosaMailServer.
130             If the mail tab wasn't opened once before saving, the account can't be saved */
131         if(is_array($this->gosaMailServer)){
132           $this->gosaMailServer = $this->gosaMailServer[0];
133         }
135         if ($method->connect($this->attrs["gosaMailServer"][0])){
137           /* Update quota values */
138           $quota= $method->getQuota($this->folder_prefix.$this->$id);
139          
140           if($quota){
141             if ($quota['gosaMailQuota'] == 2147483647){
142               $this->quotaUsage     = "";
143               $this->gosaMailQuota  = "";
144             } else {
145               $this->quotaUsage     = $quota['quotaUsage'];
146               $this->gosaMailQuota  = $quota['gosaMailQuota'];
147             }
148           }else{
149             $this->quotaUsage     = "";
150             $this->gosaMailQuota  = "";
151 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
152           }
154           /* Get mailboxes / folder like INBOX ..*/
155           $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
156           
157           $method->disconnect();
158         }else{
159           /* Could not connect to ldap.
160            */
161           if (isset($this->attrs['gosaMailQuota'][0])){
162             $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
163           }
164         }
165       }
166     }
168     /* Fill vacation array */
169     $this->vacation= array();
170     if (isset($this->config->current['VACATIONDIR'])){
171       $dir= $this->config->current['VACATIONDIR'];
172       if (is_dir($dir) && is_readable($dir)){
174         /* Look for files and build the vacation array */
175         $dh= opendir($dir);
176         while ($file = readdir($dh)){
177           $description= $this->parse_vacation("$dir/$file");
178           if ($description != ""){
179             $this->vacation["$dir/$file"]= $description;
180           }
181         }
182         closedir($dh);
183       }
184     }
187   /* Create filter */
189     /* Get global filter config */
190     if (!is_global("mailfilter")){
191       $ui= get_userinfo();
192       $base= get_base_from_people($ui->dn);
193       $mailfilter= array( "depselect"       => $base,
194           "muser"            => "",
195           "regex"           => "*");
196       register_global("mailfilter", $mailfilter);
197     }
198   }
201   function parse_vacation($file)
202   {
203     $desc= "";
205     if (is_file($file)){
206       $fh = fopen($file, "r");
207       $line= fgets($fh, 256);
209       if (!preg_match('/^DESC:/', $line)){
210         print_red (_("No DESC tag in vacation file:")." $file");
211         return $desc;
212       }
213       fclose ($fh);
215       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
216     }
218     return $desc;
219   }
222   function execute()
223   {
224     /* Call parent execute */
225     plugin::execute();
227     /* Initialise vars */
229     /* Load templating engine */
230     $smarty= get_smarty();
231     $display= "";
233     /* Get available mailserver */
234     $mailserver= array();
235     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
236       $mailserver[]= $key;
237     }
239     /* Handle account state */
240     /* Do we need to flip is_account state? */
241     if(isset($_POST['modify_state'])){
242       if($this->is_account && $this->acl_is_removeable()){
243         $this->is_account= FALSE;
244       }elseif(!$this->is_account && $this->acl_is_createable()){
245         $this->is_account= TRUE;
246       }
247     }
249     /* Do we represent a valid account? */
250     if (!$this->is_account && $this->parent == NULL){
251       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
252         _("This account has no mail extensions.")."</b>";
254       $display.= back_to_main();
255       return ($display);
256     }
258     /* Show tab dialog headers */
259     if ($this->parent != NULL){
260       if ($this->is_account){
261         if($this->accountDelegationsConfigured()){
262           $display= $this->show_disable_header(_("Remove mail account"),
263               _("This account can't be removed while there are delegations configured. Remove those delegations first."),TRUE,TRUE);
264         }else{
265           $display= $this->show_disable_header(_("Remove mail account"),
266               _("This account has mail features enabled. You can disable them by clicking below."));
267         }
268       } else {
269         $display= $this->show_enable_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
270         return ($display);
271       }
272     }
275     /* Forwarder  subdialog */
277     /* Trigger forward add dialog? */
278     if (isset($_POST['add_local_forwarder'])){
279       $this->forward_dialog= TRUE;
280       $this->dialog= TRUE;
281     }
283     /* Cancel forward add dialog? */
284     if (isset($_POST['add_locals_cancel'])){
285       $this->forward_dialog= FALSE;
286       $this->dialog= FALSE;
287     }
289     /* Finished adding of locals? */
290     if (isset($_POST['add_locals_finish'])){
292       /* Check if we are able to write gosaMailForwardingAddress */
293       if($this->acl_is_writeable("gosaMailForwardingAddress")){
295         /* Walk through list of forwarders, ignore own addresses */
296         foreach ($_POST['local_list'] as $val){
297           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
298               $val != $this->mail){
300             $this->addForwarder($val);
301             $this->is_modified= TRUE;
302           }
303         }
304       }
305       $this->forward_dialog= FALSE;
306       $this->dialog= FALSE;
307     }
309     /* Add forward email addresses */
310     if (isset($_POST['add_forwarder'])){
311       if ($_POST['forward_address'] != ""){
313         /* Valid email address specified? */
314         $address= $_POST['forward_address'];
315         $valid= FALSE;
316         if (!is_email($address)){
317           if (!is_email($address, TRUE)){
318             if ($this->is_template){
319               $valid= TRUE;
320             } else {
321               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
322             }
323           }
324         } elseif ($address == $this->mail
325             || in_array($address, $this->gosaMailAlternateAddress)) {
327           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
329         } else {
330           $valid= TRUE;
331         }
333         if ($valid){
335           /* Add it, if we are able to write gosaMailForwardingAddress */
336           if($this->acl_is_writeable("gosaMailForwardingAddress")){
337             $this->addForwarder ($address);
338             $this->is_modified= TRUE;
339           }
340         }
341       }
342     }
344     /* Delete forward email addresses */
345     if (isset($_POST['delete_forwarder'])){
346       $this->delForwarder ($_POST['forwarder_list']);
347     }
350     /* Add alternate email addresses */
351     if (isset($_POST['add_alternate'])){
353       $valid= FALSE;
354       if (!is_email($_POST['alternate_address'])){
355         if ($this->is_template){
356           if (!(is_email($_POST['alternate_address'], TRUE))){
357             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
358           } else {
359             $valid= TRUE;
360           }
361         } else {
362           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
363         }
365       } else {
366         $valid= TRUE;
367       }
369       if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
370         $ui= get_userinfo();
371         if ($user != $ui->username){
372           print_red (_("The address you're trying to add is already used by user")." '$user'.");
373         }
374       }
375     }
377     /* Delete alternate email addresses */
378     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
379       $this->delAlternate ($_POST['alternates_list']);
380     }
382   
383     /* Vacation message */
384   
385     /* Import vacation message? */
386     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
387       $contents= "";
388       $lines= file($_POST["vacation_template"]);
389       foreach ($lines as $line){
390         if (!preg_match('/^DESC:/', $line)){
391           $contents.= $line;
392         }
393       }
395       /* Replace attributes */
396       $attrs= $this->parent->by_object['user']->attributes;
397       foreach ($attrs as $val){
398         $contents= preg_replace("/%$val/",
399             $this->parent->by_object['user']->$val, $contents);
400       }
402       /* Save message */
403       $this->gosaVacationMessage= htmlspecialchars($contents);
404     }
406   
407     /* Display forward dialog if requested above */
409     /* Show forward add dialog */
410     if ($this->forward_dialog){
411       $ldap= $this->config->get_ldap_link();
413       /* Save data */
414       $mailfilter= get_global("mailfilter");
415       foreach( array("depselect", "muser", "regex") as $type){
416         if (isset($_POST[$type])){
417           $mailfilter[$type]= $_POST[$type];
418         }
419       }
420       if (isset($_GET['search'])){
421         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
422         if ($s == "**"){
423           $s= "*";
424         }
425         $mailfilter['regex']= $s;
426       }
427       register_global("mailfilter", $mailfilter);
429       /* Get actual list */
430       $mailusers= array ();
431       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
432         $regex= $mailfilter['regex'];
433         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
434       } else {
435         $filter= "";
436       }
437       if ($mailfilter['muser'] != ""){
438         $user= $mailfilter['muser'];
439         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
440       }
442       /* Add already present people to the filter */
443       $exclude= "";
444       foreach ($this->gosaMailForwardingAddress as $mail){
445         $exclude.= "(mail=$mail)";
446       }
447       if ($exclude != ""){
448         $filter.= "(!(|$exclude))";
449       }
451       $acl= array($this->config->current['BASE'] => ":all");
452       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'], 
453                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
454       $ldap->cd($mailfilter['depselect']);
455       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
456       error_reporting (0);
457       while ($attrs= $ldap->fetch()){
458         if(preg_match('/%/', $attrs['mail'][0])){
459           continue;
460         }
461         $name= $this->make_name($attrs);
462         $mailusers[$attrs['mail'][0]]= $name."&lt;".
463           $attrs['mail'][0]."&gt;";
464       }
465       error_reporting (E_ALL);
466       natcasesort ($mailusers);
467       reset ($mailusers);
469       /* Show dialog */
470       $smarty->assign("search_image", get_template_path('images/search.png'));
471       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
472       $smarty->assign("tree_image", get_template_path('images/tree.png'));
473       $smarty->assign("infoimage", get_template_path('images/info.png'));
474       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
475       $smarty->assign("mailusers", $mailusers);
476       if (isset($_POST['depselect'])){
477         $smarty->assign("depselect", $_POST['depselect']);
478       }
479       $smarty->assign("deplist", $this->config->idepartments);
480       $smarty->assign("apply", apply_filter());
481       $smarty->assign("alphabet", generate_alphabet());
482       $smarty->assign("hint", print_sizelimit_warning());
483       foreach( array("depselect", "muser", "regex") as $type){
484         $smarty->assign("$type", $mailfilter[$type]);
485       }
486       $smarty->assign("hint", print_sizelimit_warning());
488       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
489       return ($display);
490     }
492     /* Display mail account tab */
494     $smarty->assign("mailServers", $mailserver);
495     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
497     $tmp  = $this->plInfo();
498     foreach($tmp['plProvidedAcls'] as $name => $transl){
499       $smarty->assign("$name"."ACL", $this->getacl($name,$SkipWrite));
500     }
502     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
503           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
504           "gosaVacationMessage", "gosaMailDeliveryMode",
505           "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
506       $smarty->assign("$val", $this->$val);
507     }
509     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
510       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
511       $smarty->assign("quotadefined", "true");
512     } else {
513       $smarty->assign("quotadefined", "false");
514     }
516     /* Disable mail field if needed */
517     $method= new $this->method($this->config);
518     if ($method->uattrib == "mail" && $this->initially_was_account){
519       $smarty->assign("mailACL", preg_replace("/w/","",$this->getacl("mail",$SkipWrite)));
520     }
523     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
524       $smarty->assign("drop_own_mails", "checked");
525     } else {
526       $smarty->assign("drop_own_mails", "");
527     }
529     $types = array(
530           "V"=>"use_vacation",
531           "S"=>"use_spam_filter",
532           "R"=>"use_mailsize_limit",
533           "I"=>"only_local",
534           "C"=>"own_script");
536     /* Fill checkboxes */
537     foreach($types as $option => $varname){
538       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
539         $smarty->assign($varname, "checked");
540       } else {
541         $smarty->assign($varname, "");
542       }
543     }
544     
545     /* Have vacation templates? */
546     $smarty->assign("template", "");
547     if (count($this->vacation)){
548       $smarty->assign("show_templates", "true");
549       $smarty->assign("vacationtemplates", $this->vacation);
550       if (isset($_POST['vacation_template'])){
551         $smarty->assign("template", $_POST['vacation_template']);
552       }
553     } else {
554       $smarty->assign("show_templates", "false");
555     }
557     /* Fill spam selector */
558     $spamlevel= array();
559     for ($i= 0; $i<21; $i++){
560       $spamlevel[]= $i;
561     }
562     $smarty->assign("spamlevel", $spamlevel);
563     $smarty->assign("spambox", $this->mailboxList);
565     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
566     return ($display);
567   }
570   /* remove object from parent */
571   function remove_from_parent()
572   {
573     /* Cancel if there's nothing to do here */
574     if (!$this->initially_was_account){
575       return;
576     }
577     
578     /* include global link_info */
579     $ldap= $this->config->get_ldap_link();
581     /* Remove and write to LDAP */
582     plugin::remove_from_parent();
584     /* Zero arrays */
585     $this->attrs['gosaMailAlternateAddress']= array();
586     $this->attrs['gosaMailForwardingAddress']= array();
588     /* Adapt attributes if needed */
589     $method= new $this->method($this->config);
590     $method->fixAttributesOnRemove($this);
592     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
593     $ldap->cd($this->dn);
594     $this->cleanup();
595     $ldap->modify ($this->attrs); 
597     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/mail account with dn '%s' failed."),$this->dn));
599     /* Connect to IMAP server for account deletion */
600     if ($this->gosaMailServer != ""){
601       $method= new $this->method($this->config);
602       $id= $method->uattrib;
603       if ($method->connect($this->gosaMailServer)){
605         /* Remove account from IMAP server */
606         $method->deleteMailbox($this->folder_prefix.$this->$id);
607         $method->disconnect();
608       }
609     }
611     /* Optionally execute a command after we're done */
612     $this->handle_post_events("remove",array("uid" => $this->uid));
613   }
615   
616   /* check if we have some delegations configured, those delegations must be removed first */
617   function accountDelegationsConfigured()
618   { 
619     /* We are in administrational edit mode.
620         Check tab configurations directly */
621     if(isset($this->attrs)){
622       $checkArray  = array("kolabInvitationPolicy","unrestrictedMailSize", "calFBURL","kolabDelegate","kolabFreeBusyFuture");
623       foreach($checkArray as $index){
624         if(isset($this->attrs[$index])){
625            return(true);
626         }
627       }
628     }
629     return(false); 
630   }
631  
633   /* Save data to object */
634   function save_object()
635   {
636     if (isset($_POST['mailTab'])){
637       /* Save ldap attributes */
638       plugin::save_object();
640       /* Assemble mail delivery mode
641          The mode field in ldap consists of values between braces, this must
642          be called when 'mail' is set, because checkboxes may not be set when
643          we're in some other dialog.
645          Example for gosaMailDeliveryMode [LR        ]
646          L: Local delivery
647          R: Reject when exceeding mailsize limit
648          S: Use spam filter
649          V: Use vacation message
650          C: Use custm sieve script
651          I: Only insider delivery */
653       $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
656       /* Handle delivery flags */ 
657       if($this->acl_is_writeable("gosaMailDeliveryModeL")){
658         if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
659           $tmp.="L";
660         }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
661           $tmp = preg_replace("/L/","",$tmp);
662         }
663       }
665       $opts = array(     
666           "R"   => "use_mailsize_limit",
667           "S"   => "use_spam_filter",
668           "V"   => "use_vacation",
669           "C"   => "own_script",
670           "I"   => "only_local");
672       foreach($opts as $flag => $post){
673         if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
674           if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
675             $tmp.= $flag;
676           }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
677             $tmp = preg_replace("/".$flag."/","",$tmp);
678           }
679         }
680       }
682       $tmp= "[$tmp]";
683       if ($this->gosaMailDeliveryMode != $tmp){
684         $this->is_modified= TRUE;
685       }
686       $this->gosaMailDeliveryMode= $tmp;
687     }
688   }
691   /* Save data to LDAP, depending on is_account we save or delete */
692   function save()
693   {
694     $ldap= $this->config->get_ldap_link();
696     /* Call parents save to prepare $this->attrs */
697     plugin::save();
699     /* Save arrays */
700     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
701     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
703     /* Adapt attributes if needed */
704     $method= new $this->method($this->config);
705     $id= $method->uattrib;
707     $method->fixAttributesOnStore($this);
709     /* Remove Mailquota if = "" or "0"  */
710     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
711       $this->attrs['gosaMailQuota']=0;
712     }
714     if(empty($this->attrs['gosaSpamMailbox'])){
715       unset($this->attrs['gosaSpamMailbox']);
716     }
718     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
720     /* Save data to LDAP */
721     $ldap->cd($this->dn);
722     $this->cleanup();
723     $ldap->modify ($this->attrs); 
725     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/mail account with dn '%s' failed."),$this->dn));
727     /* Only do IMAP actions if we are not a template */
728     if (!$this->is_template){
730       if ($method->connect($this->gosaMailServer)){
731         $method->updateMailbox($this->folder_prefix.$this->$id);
732         
733         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
734         $method->disconnect();
736         /* Ensure that this is an existing account */
737         if($this->initially_was_account){
739           /* Write sieve information only if not in C mode */
740           if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
741             $method->configureFilter($this->$id,
742                 $this->gosaMailDeliveryMode,
743                 $this->mail,
744                 $this->gosaMailAlternateAddress,
745                 $this->gosaMailMaxSize,
746                 $this->gosaSpamMailbox,
747                 $this->gosaSpamSortLevel,
748                 $this->gosaVacationMessage);
749           }
750         }
751       }
752     }
754     /* Optionally execute a command after we're done */
755     if ($this->initially_was_account == $this->is_account){
756       if ($this->is_modified){
757         $this->handle_post_events("modify", array("uid" => $this->uid));
758       }
759     } else {
760       $this->handle_post_events("add", array("uid" => $this->uid));
761     }
763   }
766   /* Check formular input */
767   function check()
768   {
769     if(!$this->is_account) return(array());
771     $ldap= $this->config->get_ldap_link();
773     /* Call common method to give check the hook */
774     $message= plugin::check();
776     if(empty($this->gosaMailServer)){
777       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
778     }
780     /* must: mail */
781     if ($this->mail == ""){
782       $message[]= _("The required field 'Primary address' is not set.");
783     }
784     if ($this->is_template){
785       if (!is_email($this->mail, TRUE)){
786         $message[]= _("Please enter a valid email address in 'Primary address' field.");
787       }
788     } else {
789       if (!is_email($this->mail)){
790         $message[]= _("Please enter a valid email address in 'Primary address' field.");
791       }
792     }
793     $ldap->cd($this->config->current['BASE']);
794     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
795     if ($ldap->count() != 0){
796       $message[]= _("The primary address you've entered is already in use.");
797     }
799     /* Check quota */
800     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
801       if (!is_numeric($this->gosaMailQuota)) {
802         $message[]= _("Value in 'Quota size' is not valid.");
803       } else {
804         $this->gosaMailQuota= (int) $this->gosaMailQuota;
805       }
806     }
808     /* Check rejectsize for integer */
809     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
810       if (!is_numeric($this->gosaMailMaxSize)){
811         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
812       } else {
813         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
814       }
815     }
817     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
818     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
819         $this->gosaMailMaxSize == ""){
821       $message[]= _("You need to set the maximum mail size in order to reject anything.");
822     }
824     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
825       $message[]= _("You specified Spam settings, but there is no Folder specified.");
826     }
828     return ($message);
829   }
832   /* Adapt from template, using 'dn' */
833   function adapt_from_template($dn)
834   {
835     plugin::adapt_from_template($dn);
837     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
838       $this->$val= array();
839       if (isset($this->attrs["$val"]["count"])){
840         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
841           $value= $this->attrs["$val"][$i];
842           foreach (array("sn", "givenName", "uid") as $repl){
843             if (preg_match("/%$repl/i", $value)){
844               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
845             }
846           }
847           array_push($this->$val, strtolower(rewrite($value)));
848         }
849       }
850     }
851     $this->mail= strtolower(rewrite($this->mail));
852   }
855   /* Add entry to forwarder list */
856   function addForwarder($address)
857   {
858     if($this->acl_is_writeable("gosaMailForwardingAddress")){
859       $this->gosaMailForwardingAddress[]= $address;
860       $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
861       sort ($this->gosaMailForwardingAddress);
862       reset ($this->gosaMailForwardingAddress);
863       $this->is_modified= TRUE;
864     }else{
865       print_red(_("You are not allowed to write mail forwarding."));
866     }
867   }
870   /* Remove list of addresses from forwarder list */
871   function delForwarder($addresses)
872   {
873     if($this->acl_is_writeable("gosaMailForwardingAddress")){
874       $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
875       $this->is_modified= TRUE;
876     }else{
877       print_red(_("You are not allowed to write mail forwarding."));
878     }
879   }
882   /* Add given mail address to the list of alternate adresses , 
883      check if this mal address is used, skip adding in this case */
884   function addAlternate($address)
885   {
886     if($this->acl_is_writeable("gosaMailAlternateAddress")){
887       $ldap= $this->config->get_ldap_link();
888       $address= strtolower($address);
890       /* Is this address already assigned in LDAP? */
891       $ldap->cd ($this->config->current['BASE']);
892       $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
894       if ($ldap->count() > 0){
895         $attrs= $ldap->fetch ();
896         return ($attrs["uid"][0]);
897       }
899       /* Add to list of alternates */
900       if (!in_array($address, $this->gosaMailAlternateAddress)){
901         $this->gosaMailAlternateAddress[]= $address;
902         $this->is_modified= TRUE;
903       }
905       sort ($this->gosaMailAlternateAddress);
906       reset ($this->gosaMailAlternateAddress);
907       return ("");
908     }else{
909       print_red(_("You are not allowed to write mail alternate address."));
910     }
911   }
914   function delAlternate($addresses)
915   {
916     if($this->acl_is_writeable("gosaMailAlternateAddress")){
917       $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
918       $this->is_modified= TRUE;
919     }else{
920       print_red(_("You are not allowed to write mail alternate address."));
921     }
922   }
924   function make_name($attrs)
925   {
926     $name= "";
927     if (isset($attrs['sn'][0])){
928       $name= $attrs['sn'][0];
929     }
930     if (isset($attrs['givenName'][0])){
931       if ($name != ""){
932         $name.= ", ".$attrs['givenName'][0];
933       } else {
934         $name.= $attrs['givenName'][0];
935       }
936     }
937     if ($name != ""){
938       $name.= " ";
939     }
941     return ($name);
942   }
944   
945   /* Create the mail part for the copy & paste dialog */
946   function getCopyDialog()
947   {
948     if(!$this->is_account) return("");
949     $smarty = get_smarty();
950     $smarty->assign("mail",$this->mail); 
951     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
952     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
953     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
955     $ret = array();
956     $ret['status'] = "";
957     $ret['string'] = $str;
958     return($ret);
959   }
961   function saveCopyDialog()
962   {
963     if(!$this->is_account) return;  
965     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
966     $this->execute();
967     
968     if(isset($_POST['mail'])){
969       $this->mail = $_POST['mail'];
970     }
972   }
974   function allow_remove()
975   {
976     if (isset($this->config->current['MAILMETHOD'])){
977       $method= $this->config->current['MAILMETHOD'];
978       if(preg_match("/kolab/i",$method)){
979         $ldap = $this->config->get_ldap_link();
980         $ldap->cd($this->config->current['BASE']);
981         $ldap->cat($this->dn);
982         if($ldap->count()){
983           $attrs = $ldap->fetch();
984           if(isset($attrs['kolabDeleteFlag'])){ 
985             return(_("Waiting for kolab to remove mail properties."));
986           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
987             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
988           }
989         }
990       }
991     }
992   }
995   function plInfo()
996   {
997     return (array(
998           "plShortName"     => _("Mail"),
999           "plDescription"   => _("Mail settings"),
1000           "plSelfModify"    => TRUE,
1001           "plDepends"       => array("user"),                     // This plugin depends on
1002           "plPriority"      => 4,                                 // Position in tabs
1003           "plSection"       => "personal",                        // This belongs to personal
1004           "plCategory"      => array("users"),                    // Add to following categories
1005           "plOptions"       => array(),
1006   
1007           "plProvidedAcls"  => array(
1008             "mail"                      =>  _("Mail address"),
1009             "gosaMailServer"            =>  _("Mail server"),
1010             "gosaMailQuota"             =>  _("Quota size"),
1012             "gosaMailDeliveryModeV"     =>  _("Add vacation information"),  // This is flag of gosaMailDeliveryMode
1013             "gosaVacationMessage"       =>  _("Vacation message"),
1015             "gosaMailDeliveryModes"     =>  _("Use spam filter"),           // This is flag of gosaMailDeliveryMode
1016             "gosaSpamSortLevel"         =>  _("Spam level"),
1017             "gosaSpamMailbox"           =>  _("Spam mail box"),
1019             "gosaMailDeliveryModeR"     =>  _("Reject due to mailsize"),    // This is flag of gosaMailDeliveryMode
1020             "gosaMailMaxSize"           =>  _("Mail max size"),
1022             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1023             "gosaMailDeliveryModeL"     =>  _("Local delivery"),            // This is flag of gosaMailDeliveryMode
1024             "gosaMailDeliveryModeI"     =>  _("Only insider delivery"),     // This is flag of gosaMailDeliveryMode
1025             "gosaMailAlternateAddress"  =>  _("Mail alternative addresses"),
1027             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1028             "gosaMailDeliveryModeC"     =>  _("Use custom sieve script"))   // This is flag of gosaMailDeliveryMode
1029         ));
1030   }
1033 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1034 ?>