Code

Update posixAccount check hook.
[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 ("sieve/libsieve.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 $gosaVacationStart                  = 0;
28   var $gosaVacationStop                   = 0;
29   var $view_logged = FALSE;
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("INBOX");
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","gosaVacationStart","gosaVacationStop",
61       "gosaVacationMessage", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
62   var $objectclasses= array("gosaMailAccount");
63   var $uid;
65   var $sieve_management = NULL;
67   /* constructor, if 'dn' is set, the node loads the given
68      'dn' from LDAP */
69   function mailAccount ($config, $dn= NULL)
70   {
71     /* Load bases attributes */
72     plugin::plugin($config, $dn);
74     /* Set uid */
75     if(isset($this->attrs['uid'])){
76       $this->uid = $this->attrs['uid'][0];
77     }
78  
79     if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
80       $this->gosaMailServer = $this->gosaMailServer[0];
81     }
83     /* Save initial account state */
84     $this->initially_was_account= $this->is_account;
86     /*  Set mailMethod to the one defined in gosa.conf */
87     if (isset($this->config->current['MAILMETHOD'])){
88       $method= $this->config->current['MAILMETHOD'];
89       if (class_exists("mailMethod$method")){
90         $this->method= "mailMethod$method";
91       } else {
92         print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
93       }
94     }
96     
97     /* Create the account prefix  user. user/ 
98        Preset folder prefix. Will change it later to respect
99        altnamespace. */
100     if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
101       $this->folder_prefix= "user/";
102     }elseif (isset($this->config->data['MAIN']['CYRUSUNIXSTYLE']) && $this->config->data['MAIN']['CYRUSUNIXSTYLE'] == "true"){
103       $this->folder_prefix= "user/";
104     } else {
105       $this->folder_prefix= "user.";
106     }
107     
108     /* This is not a new account, parse additional attributes */
109     if (($dn != NULL) && ($dn != "new") && $this->is_account){
111       /* Load attributes containing arrays */
112       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
113         $this->$val= array();
114         if (isset($this->attrs["$val"]["count"])){
115           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
116             array_push($this->$val, $this->attrs["$val"][$i]);
117           }
118         }
119       }
122       /* Only do IMAP actions if gosaMailServer attribute is set */
123       if (isset ($this->attrs["gosaMailServer"][0])){
125         $method = new $this->method($this->config);
126         $id     = $method->uattrib;
128         /* Adapt attributes if needed */
129         $method->fixAttributesOnLoad($this);
131         /* FixAttributesOnLoad possibly creates an array out of gosaMailServer.
132             If the mail tab wasn't opened once before saving, the account can't be saved */
133         if(is_array($this->gosaMailServer)){
134           $this->gosaMailServer = $this->gosaMailServer[0];
135         }
137         if ($method->connect($this->attrs["gosaMailServer"][0])){
139           /* Update quota values */
140           $quota= $method->getQuota($this->folder_prefix.$this->$id);
141          
142           if($quota){
143             if ($quota['gosaMailQuota'] == 2147483647){
144               $this->quotaUsage     = "";
145               $this->gosaMailQuota  = "";
146             } else {
147               $this->quotaUsage     = $quota['quotaUsage'];
148               $this->gosaMailQuota  = $quota['gosaMailQuota'];
149             }
150           }else{
151             $this->quotaUsage     = "";
152             $this->gosaMailQuota  = "";
153 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
154           }
156           /* Get mailboxes / folder like INBOX ..*/
157           $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
158           
159           $method->disconnect();
160         }else{
161           /* Could not connect to ldap.
162            */
163           if (isset($this->attrs['gosaMailQuota'][0])){
164             $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
165           }
166         }
167       }
168     }
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     }
188     /* Create sieve management class */
189     $method = new $this->method($this->config);
190     $id     = $method->uattrib;
191     $this->sieve_management = new sieveManagement($this->config,$this->dn,$this,$id);
193     /* Get global filter config */
194     if (!is_global("mailfilter")){
195       $ui= get_userinfo();
196       $base= get_base_from_people($ui->dn);
197       $mailfilter= array( "depselect"       => $base,
198           "muser"            => "",
199           "regex"           => "*");
200       register_global("mailfilter", $mailfilter);
201     }
202   }
205   function parse_vacation($file)
206   {
207     $desc= "";
209     if (is_file($file)){
210       $fh = fopen($file, "r");
211       $line= fgets($fh, 256);
213       if (!preg_match('/^DESC:/', $line)){
214         print_red (_("No DESC tag in vacation file:")." $file");
215         return $desc;
216       }
217       fclose ($fh);
219       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
220     }
222     return $desc;
223   }
226   function execute()
227   {
228     /* Call parent execute */
229     plugin::execute();
231     /* Log view */
232     if($this->is_account && !$this->view_logged){
233       $this->view_logged = TRUE;
234       new log("view","users/".get_class($this),$this->dn);
235     }
237     /* Initialise vars */
239     /* Load templating engine */
240     $smarty= get_smarty();
241     $display= "";
243     /* Get available mailserver */
244     $mailserver= array();
245     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
246       $mailserver[]= $key;
247     }
249     /* 
250      * Sieve Management 
251      */
252     if(isset($_POST['sieveManagement']) 
253         && preg_match("/C/",$this->gosaMailDeliveryMode)
254         && $this->acl_is_writeable("sieveManagement")) {
256       $this->dialog = $this->sieve_management;
257     }
258    
259     /* Cancel sieve edit */
260     if(isset($_POST['sieve_cancel'])){
261       $this->dialog = NULL;
262     }
263  
264     /* Save sieve changes */
265     if(isset($_POST['sieve_finish'])){
266       $this->sieve_management = $this->dialog;
267       $this->dialog = NULL;
268     }
269  
270     if(is_object($this->dialog)){
271       $this->dialog->save_object();
272       return($this->dialog->execute());
273     } 
276     /* Handle account state */
277     /* Do we need to flip is_account state? */
278     if(isset($_POST['modify_state'])){
279       if($this->is_account && $this->acl_is_removeable()){
280         $this->is_account= FALSE;
281       }elseif(!$this->is_account && $this->acl_is_createable()){
282         $this->is_account= TRUE;
283       }
284     }
286     /* Do we represent a valid account? */
287     if (!$this->is_account && $this->parent == NULL){
288       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
289         _("This account has no mail extensions.")."</b>";
291       $display.= back_to_main();
292       return ($display);
293     }
295     /* Show tab dialog headers */
296     if ($this->parent != NULL){
297       if ($this->is_account){
298         if($this->accountDelegationsConfigured()){
299           $display= $this->show_disable_header(_("Remove mail account"),
300               _("This account can't be removed while there are delegations configured. Remove those delegations first."),TRUE,TRUE);
301         }else{
302           $display= $this->show_disable_header(_("Remove mail account"),
303               _("This account has mail features enabled. You can disable them by clicking below."));
304         }
305       } else {
306         $display= $this->show_enable_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
307         return ($display);
308       }
309     }
312     /* Forwarder  subdialog */
314     /* Trigger forward add dialog? */
315     if (isset($_POST['add_local_forwarder'])){
316       $this->forward_dialog= TRUE;
317       $this->dialog= TRUE;
318     }
320     /* Cancel forward add dialog? */
321     if (isset($_POST['add_locals_cancel'])){
322       $this->forward_dialog= FALSE;
323       $this->dialog= FALSE;
324     }
326     /* Finished adding of locals? */
327     if (isset($_POST['add_locals_finish'])){
329       /* Check if we are able to write gosaMailForwardingAddress */
330       if($this->acl_is_writeable("gosaMailForwardingAddress")){
332         /* Walk through list of forwarders, ignore own addresses */
333         foreach ($_POST['local_list'] as $val){
334           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
335               $val != $this->mail){
337             $this->addForwarder($val);
338             $this->is_modified= TRUE;
339           }
340         }
341       }
342       $this->forward_dialog= FALSE;
343       $this->dialog= FALSE;
344     }
346     /* Add forward email addresses */
347     if (isset($_POST['add_forwarder'])){
348       if ($_POST['forward_address'] != ""){
350         /* Valid email address specified? */
351         $address= $_POST['forward_address'];
352         $valid= FALSE;
353         if (!is_email($address)){
354           if (!is_email($address, TRUE)){
355             if ($this->is_template){
356               $valid= TRUE;
357             } else {
358               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
359             }
360           }
361         } elseif ($address == $this->mail
362             || in_array($address, $this->gosaMailAlternateAddress)) {
364           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
366         } else {
367           $valid= TRUE;
368         }
370         if ($valid){
372           /* Add it, if we are able to write gosaMailForwardingAddress */
373           if($this->acl_is_writeable("gosaMailForwardingAddress")){
374             $this->addForwarder ($address);
375             $this->is_modified= TRUE;
376           }
377         }
378       }
379     }
381     /* Delete forward email addresses */
382     if (isset($_POST['delete_forwarder'])){
383       $this->delForwarder ($_POST['forwarder_list']);
384     }
387     /* Add alternate email addresses */
388     if (isset($_POST['add_alternate'])){
390       $valid= FALSE;
391       if (!is_email($_POST['alternate_address'])){
392         if ($this->is_template){
393           if (!(is_email($_POST['alternate_address'], TRUE))){
394             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
395           } else {
396             $valid= TRUE;
397           }
398         } else {
399           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
400         }
402       } else {
403         $valid= TRUE;
404       }
406       if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
407         $ui= get_userinfo();
408         if ($user != $ui->username){
409           print_red (_("The address you're trying to add is already used by user")." '$user'.");
410         }
411       }
412     }
414     /* Delete alternate email addresses */
415     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
416       $this->delAlternate ($_POST['alternates_list']);
417     }
419   
420     /* Vacation message */
421   
422     /* Import vacation message? */
423     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
424       $contents= "";
425       $lines= file($_POST["vacation_template"]);
426       foreach ($lines as $line){
427         if (!preg_match('/^DESC:/', $line)){
428           $contents.= $line;
429         }
430       }
432       /* Replace attributes */
433       $attrs= $this->parent->by_object['user']->attributes;
434       foreach ($attrs as $val){
435         
436         if(preg_match("/dateOfBirth/",$val)){
437           if($this->parent->by_object['user']->use_dob){
438             $contents= preg_replace("/%$val/",date("Y-d-m",$this->parent->by_object['user']->dateOfBirth),$contents);
439           }
440         }else {
441           $contents= preg_replace("/%$val/",
442               $this->parent->by_object['user']->$val, $contents);
443         }
444       }
446       /* Save message */
447       $this->gosaVacationMessage= htmlspecialchars($contents);
448     }
450   
451     /* Display forward dialog if requested above */
453     /* Show forward add dialog */
454     if ($this->forward_dialog){
455       $ldap= $this->config->get_ldap_link();
457       /* Save data */
458       $mailfilter= get_global("mailfilter");
459       foreach( array("depselect", "muser", "regex") as $type){
460         if (isset($_POST[$type])){
461           $mailfilter[$type]= $_POST[$type];
462         }
463       }
464       if (isset($_GET['search'])){
465         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
466         if ($s == "**"){
467           $s= "*";
468         }
469         $mailfilter['regex']= $s;
470       }
471       register_global("mailfilter", $mailfilter);
473       /* Get actual list */
474       $mailusers= array ();
475       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
476         $regex= $mailfilter['regex'];
477         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
478       } else {
479         $filter= "";
480       }
481       if ($mailfilter['muser'] != ""){
482         $user= $mailfilter['muser'];
483         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
484       }
486       /* Add already present people to the filter */
487       $exclude= "";
488       foreach ($this->gosaMailForwardingAddress as $mail){
489         $exclude.= "(mail=$mail)";
490       }
491       if ($exclude != ""){
492         $filter.= "(!(|$exclude))";
493       }
495       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'], 
496                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
497       $ldap->cd($mailfilter['depselect']);
498       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
499       error_reporting (0);
500       while ($attrs= $ldap->fetch()){
501         if(preg_match('/%/', $attrs['mail'][0])){
502           continue;
503         }
504         $name= $this->make_name($attrs);
505         $mailusers[$attrs['mail'][0]]= $name."&lt;".
506           $attrs['mail'][0]."&gt;";
507       }
508       error_reporting (E_ALL);
509       natcasesort ($mailusers);
510       reset ($mailusers);
512       /* Show dialog */
513       $smarty->assign("search_image", get_template_path('images/search.png'));
514       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
515       $smarty->assign("tree_image", get_template_path('images/tree.png'));
516       $smarty->assign("infoimage", get_template_path('images/info.png'));
517       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
518       $smarty->assign("mailusers", $mailusers);
519       if (isset($_POST['depselect'])){
520         $smarty->assign("depselect", $_POST['depselect']);
521       }
522       $smarty->assign("deplist", $this->config->idepartments);
523       $smarty->assign("apply", apply_filter());
524       $smarty->assign("alphabet", generate_alphabet());
525       $smarty->assign("hint", print_sizelimit_warning());
526       foreach( array("depselect", "muser", "regex") as $type){
527         $smarty->assign("$type", $mailfilter[$type]);
528       }
529       $smarty->assign("hint", print_sizelimit_warning());
531       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
532       return ($display);
533     }
535     /* Display mail account tab */
537     $smarty->assign("mailServers", $mailserver);
538     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
540     $tmp  = $this->plInfo();
541     foreach($tmp['plProvidedAcls'] as $name => $transl){
542       $smarty->assign("$name"."ACL", $this->getacl($name,$SkipWrite));
543     }
545     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
546           "gosaMailAlternateAddress", "gosaMailForwardingAddress",
547           "gosaVacationMessage", "gosaMailDeliveryMode", "gosaVacationStart",
548           "gosaVacationStop", "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
549       $smarty->assign("$val", $this->$val);
550     }
552     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
553       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
554       $smarty->assign("quotadefined", "true");
555     } else {
556       $smarty->assign("quotadefined", "false");
557     }
559     /* Disable mail field if needed */
560     $method= new $this->method($this->config);
561     if ($method->uattrib == "mail" && $this->initially_was_account){
562       $smarty->assign("mailACL", preg_replace("/w/","",$this->getacl("mail",$SkipWrite)));
563     }
565     /* Disable/Enable range select, but do not disable them twice 
566      *  if they are already diabled by "use own sieve script"
567      */
568     if (preg_match('/V/', $this->gosaMailDeliveryMode) || preg_match("/C/",$this->gosaMailDeliveryMode)){
569       $smarty->assign('rangeEnabled', "");
570     } else {
571       $smarty->assign('rangeEnabled', "disabled");
572     }
574     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
575       $smarty->assign("only_local", "checked");
576     } else {
577       $smarty->assign("only_local", "");
578     }
580     $types = array(
581           "V"=>"use_vacation",
582           "S"=>"use_spam_filter",
583           "R"=>"use_mailsize_limit",
584           "I"=>"drop_own_mails",
585           "C"=>"own_script");
587     /* Fill checkboxes */
588     foreach($types as $option => $varname){
589       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
590         $smarty->assign($varname, "checked");
591       } else {
592         $smarty->assign($varname, "");
593       }
594     }
595    
596     /* Display mail account tab */
597     if($this->gosaVacationStart ==0){
598       $date= getdate(time());
599     }else{
600       $date= getdate($this->gosaVacationStart);
601     }
602     $days= array();
603     for($d= 1; $d<32; $d++){
604       $days[$d]= $d;
605     }
606     $years= array();
607     for($y= $date['year']-10; $y<$date['year']+10; $y++){
608       $years[]= $y;
609     }
610     $months= array(_("January"), _("February"), _("March"), _("April"),
611         _("May"), _("June"), _("July"), _("August"), _("September"),
612         _("October"), _("November"), _("December"));
613     $smarty->assign("start_day", $date["mday"]);
614     $smarty->assign("days", $days);
615     $smarty->assign("months", $months);
616     $smarty->assign("start_month", $date["mon"]-1);
617     $smarty->assign("years", $years);
618     $smarty->assign("start_year", $date["year"]);
620     if($this->gosaVacationStop ==0){
621       $date= getdate(time());
622       $date["mday"]++;
623     }else{
624       $date= getdate($this->gosaVacationStop);
625     }
626     $smarty->assign("end_day", $date["mday"]);
627     $smarty->assign("end_month", $date["mon"]-1);
628     $smarty->assign("end_year", $date["year"]);
631  
632     /* Have vacation templates? */
633     $smarty->assign("template", "");
634     if (count($this->vacation)){
635       $smarty->assign("show_templates", "true");
636       $smarty->assign("vacationtemplates", $this->vacation);
637       if (isset($_POST['vacation_template'])){
638         $smarty->assign("template", $_POST['vacation_template']);
639       }
640     } else {
641       $smarty->assign("show_templates", "false");
642     }
644     /* Fill spam selector */
645     $spamlevel= array();
646     for ($i= 0; $i<21; $i++){
647       $spamlevel[]= $i;
648     }
649     $smarty->assign("spamlevel", $spamlevel);
650     $smarty->assign("spambox", $this->mailboxList);
652     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
653     return ($display);
654   }
657   /* remove object from parent */
658   function remove_from_parent()
659   {
660     /* Cancel if there's nothing to do here */
661     if (!$this->initially_was_account){
662       return;
663     }
664     
665     /* include global link_info */
666     $ldap= $this->config->get_ldap_link();
668     /* Remove and write to LDAP */
669     plugin::remove_from_parent();
671     /* Zero arrays */
672     $this->attrs['gosaMailAlternateAddress']= array();
673     $this->attrs['gosaMailForwardingAddress']= array();
675     /* Adapt attributes if needed */
676     $method= new $this->method($this->config);
677     $method->fixAttributesOnRemove($this);
679     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
680     $ldap->cd($this->dn);
681     $this->cleanup();
683     $ldap->modify ($this->attrs); 
685     /* Add "view" to logging class */ 
686     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
688     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/mail account with dn '%s' failed."),$this->dn));
690     /* Connect to IMAP server for account deletion */
691     if ($this->gosaMailServer != ""){
692       $method= new $this->method($this->config);
693       $id= $method->uattrib;
694       if ($method->connect($this->gosaMailServer)){
696         /* Remove account from IMAP server */
697         $method->deleteMailbox($this->folder_prefix.$this->$id);
698         $method->disconnect();
699       }
700     }
702     /* Optionally execute a command after we're done */
703     $this->handle_post_events("remove",array("uid" => $this->uid));
704   }
706   
707   /* check if we have some delegations configured, those delegations must be removed first */
708   function accountDelegationsConfigured()
709   { 
710     /* We are in administrational edit mode.
711         Check tab configurations directly */
712     if(isset($this->attrs)){
713       $checkArray  = array("kolabInvitationPolicy","unrestrictedMailSize", "calFBURL","kolabDelegate","kolabFreeBusyFuture");
714       foreach($checkArray as $index){
715         if(isset($this->attrs[$index])){
716            return(true);
717         }
718       }
719     }
720     return(false); 
721   }
722  
724   /* Save data to object */
725   function save_object()
726   {
727     if (isset($_POST['mailTab'])){
729       /* Save ldap attributes */
730       plugin::save_object();
733       if(isset($_POST['own_script'])){
735         if(!preg_match("/C/",$this->gosaMailDeliveryMode)){
736           $str= preg_replace("/[\[\]]/","",$this->gosaMailDeliveryMode);
737           $this->gosaMailDeliveryMode = "[".$str."C]";
738         }
740       }else{
742         /* Assemble mail delivery mode
743            The mode field in ldap consists of values between braces, this must
744            be called when 'mail' is set, because checkboxes may not be set when
745            we're in some other dialog.
747           Example for gosaMailDeliveryMode [LR        ]
748           L: Local delivery
749           R: Reject when exceeding mailsize limit
750           S: Use spam filter
751           V: Use vacation message
752           C: Use custm sieve script
753           I: Only insider delivery */
755         $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
758         /* Handle delivery flags */
759         if($this->acl_is_writeable("gosaMailDeliveryModeL")){
760           if(!preg_match("/L/",$tmp) && !isset($_POST['only_local'])){
761             $tmp.="L";
762           }elseif(preg_match("/L/",$tmp) && isset($_POST['only_local'])){
763             $tmp = preg_replace("/L/","",$tmp);
764           }
765         }
767         $opts = array(     
768             "R"   => "use_mailsize_limit",
769             "S"   => "use_spam_filter",
770             "V"   => "use_vacation",
771             "C"   => "own_script",
772             "I"   => "drop_own_mails");
774         foreach($opts as $flag => $post){
775           if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
776             if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
777               $tmp.= $flag;
778             }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
779               $tmp = preg_replace("/".$flag."/","",$tmp);
780             }
781           }
782         }
784         $tmp= "[$tmp]";
785         if ($this->gosaMailDeliveryMode != $tmp){
786           $this->is_modified= TRUE;
787         }
788         $this->gosaMailDeliveryMode= $tmp;
789       }
790     }
792   }
795   /* Save data to LDAP, depending on is_account we save or delete */
796   function save()
797   {
798     $ldap= $this->config->get_ldap_link();
800     /* Call parents save to prepare $this->attrs */
801     plugin::save();
803     /* Save arrays */
804     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
805     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
807     /* Adapt attributes if needed */
808     $method= new $this->method($this->config);
809     $id= $method->uattrib;
811     $method->fixAttributesOnStore($this);
813     /* Remove Mailquota if = "" or "0"  */
814     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
815       $this->attrs['gosaMailQuota']=0;
816     }
818     if(empty($this->attrs['gosaSpamMailbox'])){
819       unset($this->attrs['gosaSpamMailbox']);
820     }
822     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
824         /* Remove attributes - if not needed */
825     if (!preg_match('/V/', $this->gosaMailDeliveryMode)){
826       unset($this->attrs['gosaVacationStart']);
827       unset($this->attrs['gosaVacationStop']);
828     }
831     /* Remove attributes - if not needed */
832     if (!preg_match('/V/', $this->gosaMailDeliveryMode)){
833       unset($this->attrs['gosaVacationStart']);
834       unset($this->attrs['gosaVacationStop']);
835     }
837     /* Save data to LDAP */
838     $ldap->cd($this->dn);
839     $this->cleanup();
840     $ldap->modify ($this->attrs); 
842     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/mail account with dn '%s' failed."),$this->dn));
844     /* Log last action */ 
845     if($this->initially_was_account){
846       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
847     }else{
848       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
849     }
851     /* Only do IMAP actions if we are not a template */
852     if (!$this->is_template){
854       if ($method->connect($this->gosaMailServer)){
855         $method->updateMailbox($this->folder_prefix.$this->$id);
856         
857         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
858         $method->disconnect();
860         /* Ensure that this is an existing account */
861         if(1==1 || $this->initially_was_account){
863           /* Write sieve information only if not in C mode */
864           if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
865             $method->configureFilter($this->$id,
866                 $this->gosaMailDeliveryMode,
867                 $this->mail,
868                 $this->gosaMailAlternateAddress,
869                 $this->gosaMailMaxSize,
870                 $this->gosaSpamMailbox,
871                 $this->gosaSpamSortLevel,
872                 $this->gosaVacationMessage);
873           }else{
874             $this->sieve_management->save();
875           }
876         }
877       }
878     }
880     /* Optionally execute a command after we're done */
881     if ($this->initially_was_account == $this->is_account){
882       if ($this->is_modified){
883         $this->handle_post_events("modify", array("uid" => $this->uid));
884       }
885     } else {
886       $this->handle_post_events("add", array("uid" => $this->uid));
887     }
889   }
892   /* Check formular input */
893   function check()
894   {
895     if(!$this->is_account) return(array());
897     $ldap= $this->config->get_ldap_link();
899     /* Call common method to give check the hook */
900     $message= plugin::check();
902     if(empty($this->gosaMailServer)){
903       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
904     }
906     /* must: mail */
907     if ($this->mail == ""){
908       $message[]= _("The required field 'Primary address' is not set.");
909     }
910     if ($this->is_template){
911       if (!is_email($this->mail, TRUE)){
912         $message[]= _("Please enter a valid email address in 'Primary address' field.");
913       }
914     } else {
915       if (!is_email($this->mail)){
916         $message[]= _("Please enter a valid email address in 'Primary address' field.");
917       }
918     }
919     $ldap->cd($this->config->current['BASE']);
920     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
921     if ($ldap->count() != 0){
922       $message[]= _("The primary address you've entered is already in use.");
923     }
925     /* Check quota */
926     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
927       if (!is_numeric($this->gosaMailQuota)) {
928         $message[]= _("Value in 'Quota size' is not valid.");
929       } else {
930         $this->gosaMailQuota= (int) $this->gosaMailQuota;
931       }
932     }
934     /* Check rejectsize for integer */
935     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
936       if (!is_numeric($this->gosaMailMaxSize)){
937         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
938       } else {
939         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
940       }
941     }
943     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
944     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
945         $this->gosaMailMaxSize == ""){
947       $message[]= _("You need to set the maximum mail size in order to reject anything.");
948     }
950     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
951       $message[]= _("You specified Spam settings, but there is no Folder specified.");
952     }
954     if (preg_match('/V/', $this->gosaMailDeliveryMode) && $this->gosaVacationStart >= $this->gosaVacationStop){
955       $message[]= _("Time interval to show vacation message is not valid.");
956     }
958     return ($message);
959   }
962   /* Adapt from template, using 'dn' */
963   function adapt_from_template($dn)
964   {
965     plugin::adapt_from_template($dn);
967     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
968       $this->$val= array();
969       if (isset($this->attrs["$val"]["count"])){
970         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
971           $value= $this->attrs["$val"][$i];
972           foreach (array("sn", "givenName", "uid") as $repl){
973             if (preg_match("/%$repl/i", $value)){
974               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
975             }
976           }
977           array_push($this->$val, strtolower(rewrite($value)));
978         }
979       }
980     }
981     $this->mail= strtolower(rewrite($this->mail));
982   }
985   /* Add entry to forwarder list */
986   function addForwarder($address)
987   {
988     if($this->acl_is_writeable("gosaMailForwardingAddress")){
989       $this->gosaMailForwardingAddress[]= $address;
990       $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
991       sort ($this->gosaMailForwardingAddress);
992       reset ($this->gosaMailForwardingAddress);
993       $this->is_modified= TRUE;
994     }else{
995       print_red(_("You are not allowed to write mail forwarding."));
996     }
997   }
1000   /* Remove list of addresses from forwarder list */
1001   function delForwarder($addresses)
1002   {
1003     if($this->acl_is_writeable("gosaMailForwardingAddress")){
1004       $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
1005       $this->is_modified= TRUE;
1006     }else{
1007       print_red(_("You are not allowed to write mail forwarding."));
1008     }
1009   }
1012   /* Add given mail address to the list of alternate adresses , 
1013      check if this mal address is used, skip adding in this case */
1014   function addAlternate($address)
1015   {
1016     if($this->acl_is_writeable("gosaMailAlternateAddress")){
1017       $ldap= $this->config->get_ldap_link();
1018       $address= strtolower($address);
1020       /* Is this address already assigned in LDAP? */
1021       $ldap->cd ($this->config->current['BASE']);
1022       $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
1023       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
1025       if ($ldap->count() > 0){
1026         $attrs= $ldap->fetch ();
1027         return ($attrs["uid"][0]);
1028       }
1030       /* Add to list of alternates */
1031       if (!in_array($address, $this->gosaMailAlternateAddress)){
1032         $this->gosaMailAlternateAddress[]= $address;
1033         $this->is_modified= TRUE;
1034       }
1036       sort ($this->gosaMailAlternateAddress);
1037       reset ($this->gosaMailAlternateAddress);
1038       return ("");
1039     }else{
1040       print_red(_("You are not allowed to write mail alternate address."));
1041     }
1042   }
1045   function delAlternate($addresses)
1046   {
1047     if($this->acl_is_writeable("gosaMailAlternateAddress")){
1048       $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
1049       $this->is_modified= TRUE;
1050     }else{
1051       print_red(_("You are not allowed to write mail alternate address."));
1052     }
1053   }
1055   function make_name($attrs)
1056   {
1057     $name= "";
1058     if (isset($attrs['sn'][0])){
1059       $name= $attrs['sn'][0];
1060     }
1061     if (isset($attrs['givenName'][0])){
1062       if ($name != ""){
1063         $name.= ", ".$attrs['givenName'][0];
1064       } else {
1065         $name.= $attrs['givenName'][0];
1066       }
1067     }
1068     if ($name != ""){
1069       $name.= " ";
1070     }
1072     return ($name);
1073   }
1075   
1076   /* Create the mail part for the copy & paste dialog */
1077   function getCopyDialog()
1078   {
1079     if(!$this->is_account) return("");
1080     $smarty = get_smarty();
1081     $smarty->assign("mail",$this->mail); 
1082     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1083     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1084     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
1086     $ret = array();
1087     $ret['status'] = "";
1088     $ret['string'] = $str;
1089     return($ret);
1090   }
1092   function saveCopyDialog()
1093   {
1094     if(!$this->is_account) return;  
1096     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
1097     $this->execute();
1098     
1099     if(isset($_POST['mail'])){
1100       $this->mail = $_POST['mail'];
1101     }
1103   }
1105   function allow_remove()
1106   {
1107     if (isset($this->config->current['MAILMETHOD'])){
1108       $method= $this->config->current['MAILMETHOD'];
1109       if(preg_match("/olab/i",$method)){
1110         $ldap = $this->config->get_ldap_link();
1111         $ldap->cd($this->config->current['BASE']);
1112         $ldap->cat($this->dn);
1113         if($ldap->count()){
1114           $attrs = $ldap->fetch();
1115           if(isset($attrs['kolabDeleteFlag'])){ 
1116             return(_("Waiting for kolab to remove mail properties."));
1117           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
1118             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
1119           }
1120         }
1121       }
1122     }
1123   }
1125    
1126   function PrepareForCopyPaste($source)
1127   {
1128     plugin::PrepareForCopyPaste($source);
1130     /* Reset alternate mail addresses */
1131     $this->gosaMailAlternateAddress = array();    
1132    }
1135   function plInfo()
1136   {
1137     return (array(
1138           "plShortName"     => _("Mail"),
1139           "plDescription"   => _("Mail settings"),
1140           "plSelfModify"    => TRUE,
1141           "plDepends"       => array("user"),                     // This plugin depends on
1142           "plPriority"      => 4,                                 // Position in tabs
1143           "plSection"     => array("personal" => _("My account")),
1144           "plCategory"    => array("users"),
1145           "plOptions"       => array(),
1146   
1147           "plProvidedAcls"  => array(
1148             "mail"                      =>  _("Mail address"),
1149             "gosaMailServer"            =>  _("Mail server"),
1150             "gosaMailQuota"             =>  _("Quota size"),
1152             "gosaMailDeliveryModeV"     =>  _("Add vacation information"),  // This is flag of gosaMailDeliveryMode
1153             "gosaVacationMessage"       =>  _("Vacation message"),
1155             "gosaMailDeliveryModeS"     =>  _("Use spam filter"),           // This is flag of gosaMailDeliveryMode
1156             "gosaSpamSortLevel"         =>  _("Spam level"),
1157             "gosaSpamMailbox"           =>  _("Spam mail box"),
1159             "sieveManagement"           =>  _("Sieve management"),
1161             "gosaMailDeliveryModeR"     =>  _("Reject due to mailsize"),    // This is flag of gosaMailDeliveryMode
1162             "gosaMailMaxSize"           =>  _("Mail max size"),
1164             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1165             "gosaMailDeliveryModeL"     =>  _("Local delivery"),            // This is flag of gosaMailDeliveryMode
1166             "gosaMailDeliveryModeI"     =>  _("No delivery to own mailbox "),     // This is flag of gosaMailDeliveryMode
1167             "gosaMailAlternateAddress"  =>  _("Mail alternative addresses"),
1169             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1170             "gosaMailDeliveryModeC"     =>  _("Use custom sieve script"))   // This is flag of gosaMailDeliveryMode
1171         ));
1172   }
1175 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1176 ?>