Code

Backport from trunk
[gosa.git] / gosa-plugins / mail / admin / groups / mail / class_groupMail.inc
1 <?php
3 class mailgroup extends plugin
4 {
5     /* Multiple edit */
6     var $gosaMailForwardingAddress_Some  = array();  // Used in multiple edit 
8     /* Default values */
9     var $mail                       = "";           // Default mail address 
10     var $gosaMailAlternateAddress   = array();  // Set default Alternate Mail Adresses to empty array
11     var $gosaMailForwardingAddress  = array();  // Forwarding also empty
12     var $gosaMailServer             = "";       // Selected mailserver 
13     var $gosaMailQuota              = "";       // Defined Quota 
14     var $gosaVacationMessage        = "";       // Vocation message 
15     var $gosaSpamSortLevel          = "";     
16     var $gosaSpamMailbox            = "";
17     var $gosaSharedFolderTarget     ;
18     var $gosaMailDeliveryMode       = "[L        ]";   // 
19     var $gosaMailMaxSize            = "";       // 
20     var $FolderType                 = array("CAT" => '', "SUB_CAT" => '');
22     var $quotaUsage                 = -1;        // -1 Means undefined
24     /* Internal */
25     var $AclTypes                   = array();
26     var $members                    = array();  // Group members
27     var $mailusers                  = array();  // Group member with mail account
28     var $folder_acls                = array();
29     var $MailMethod = NULL; 
30     var $mailAddressSelect             = FALSE;    
31     var $remove_folder_from_imap    = true;
32     var $view_logged                = FALSE;
33     var $mailDomainPart             = "";
35     /* attribute list for save action */
36     var $attributes= array( "mail",   "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
37             "gosaMailAlternateAddress", "gosaMailForwardingAddress",
38             "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox",
39             "acl","gosaSharedFolderTarget", "gosaVacationMessage");
41     var $objectclasses= array("gosaMailAccount");
42     var $multiple_support = FALSE; // Not tested yet
44     var $uid = "";
45     var $cn ="";
46     var $orig_cn = "";
47     var $show_effective_memeber = FALSE;
49     var $aclPostToId = array();
51     function __construct (&$config, $dn= NULL, $base_object= NULL)
52     {
53         plugin::plugin($config, $dn);
55         /* Get attributes from parent object
56          */
57         foreach(array("uid","cn") as $attr){
58             if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
59                 $this->$attr = $this->parent->by_object['group']->$attr;
60             }elseif(isset($this->attrs[$attr])){
61                 $this->$attr = $this->attrs[$attr][0];
62             }
63         }
64         $this->orig_cn = $this->uid = $this->cn;
66         /* Intialize the used mailMethod
67          */
68         $tmp = new mailMethod($config,$this,"group");
69         $this->mailMethod           = $tmp->get_method();
70         $this->mailMethod->fixAttributesOnLoad();
71         $this->mailDomainParts      = $this->mailMethod->getMailDomains();
72         $this->AvailableFolderTypes = $this->mailMethod->getAvailableFolderTypes();
73         $this->MailBoxes = array();
75         /* Remember account status
76          */
77         $this->initially_was_account = $this->is_account;
79         /* While we are not not allowed to modify the mail address
80          *  and this is a new mail account, preset the user part of the 
81          *  mail address with the accounts cn.
82          */ 
83         if(!$this->mailMethod->isModifyableMail() && !$this->initially_was_account){
84             $this->mail = $base_object->cn;
85         }
87         /* Load folder_acls with defaults.
88            anyone -- The default acl, will be written to ldap.
89            member -- The ACL used for the members.
90          */ 
91         $this->folder_acls = $this->mailMethod->getDefaultACLs();
93         /* Load acls
94            The most used acl will be used as member acl, this
95            shortens the listed acls.        
96            This may be merged/overwritten by the mail methods.
97          */
98         $ldap = $this->config->get_ldap_link();
99         if(isset($this->attrs['acl'])){
100             for($i = 0; $i < $this->attrs['acl']['count'] ; $i++){
102                 /* Be carefull here, since kolab22 uses spaces in the acls (herbert read anon/post)
103                  */
104                 $str = $this->attrs['acl'][$i];
105                 list($name, $acl) = preg_split("/[ ]{1}/", $str, 2);
107                 if($name == "anyone") $name = "__anyone__";
108                 $this->folder_acls[$name] = $acl;
109             }
110         }
112         /* Initialize configured values
113          */
114         if($this->is_account){
115             if($this->mailMethod->connect() && $this->mailMethod->account_exists()){
117                 /* Read quota */
118                 $this->gosaMailQuota = $this->mailMethod->getQuota($this->gosaMailQuota);
119                 $this->quotaUsage = $this->mailMethod->getQuotaUsage($this->quotaUsage);
120                 if($this->mailMethod->is_error()){
121                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings: %s"),
122                                 $this->mailMethod->get_error()), ERROR_DIALOG);
123                 }
125                 /* Read mailboxes */
126                 $this->MailBoxes = $this->mailMethod->getMailboxList($this->MailBoxes);
127                 if($this->mailMethod->is_error()){
128                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes: %s"),
129                                 $this->mailMethod->get_error()), ERROR_DIALOG);
130                 }
132                 /* Receive folder types */
133                 $this->FolderType = $this->mailMethod->getFolderType($this->FolderType);
134                 if($this->mailMethod->is_error()){
135                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot receive folder types: %s"),
136                                 $this->mailMethod->get_error()), ERROR_DIALOG);
137                 }
139                 /* Receive permissions */  
140                 $this->folder_acls = $this->mailMethod->getFolderACLs($this->folder_acls);
141                 if($this->mailMethod->is_error()){
142                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot receive folder permissions: %s"),
143                                 $this->mailMethod->get_error()), ERROR_DIALOG);
144                 }
146             }elseif(!$this->mailMethod->is_connected()){
147                 msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
148                             $this->mailMethod->get_error()), ERROR_DIALOG);
149             }elseif(!$this->mailMethod->account_exists()){
150                 msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"),
151                             $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
152             }
154             /* If the doamin part is selectable, we have to split the mail address
155              */
156             if(!(!$this->mailMethod->isModifyableMail() && $this->is_account)){
157                 if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
158                     $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
159                     $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
160                     if(!in_array_strict($this->mailDomainPart,$this->mailDomainParts)){
161                         $this->mailDomainParts[] = $this->mailDomainPart;
162                     }
163                 }
164             }
166             /* Load attributes containing arrays */
167             foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
168                 $this->$val= array();
169                 if (isset($this->attrs["$val"]["count"])){
170                     for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
171                         array_push($this->$val, $this->attrs["$val"][$i]);
172                     }
173                 }
174             }
175         }
177         /* Disconnect mailMethod. Connect on demand later.
178          */
179         $this->mailMethod->disconnect();
180         $this->AclTypes = $this->mailMethod->getAclTypes();
182         /* Summarize most used ACLs as member acl 
183          */
184         if(count($this->folder_acls) > 2){
185             $acl_usage = array();
186             $most_acl = $this->folder_acls['__member__'];
187             $most_cnt = 0;
188             $member = $this->get_member();
189             foreach($this->folder_acls as $user => $acl){
190                 if(preg_match("/^__/",$user)) continue;
191                 if(!in_array_strict($user,$member['mail'])) continue; 
192                 if(!isset($acl_usage[$acl])) $acl_usage[$acl]=0;
193                 $acl_usage[$acl] ++;
194                 if($acl_usage[$acl] > $most_cnt){
195                     $most_cnt = $acl_usage[$acl];
196                     $most_acl = $acl;
197                 }
198             }
199             $this->folder_acls['__member__'] = $most_acl;  
200             foreach($this->folder_acls as $name => $acl){
201                 if(preg_match("/^__/",$name)) continue;
202                 if($acl == $most_acl && in_array_strict($name,$member['mail'])){
203                     unset($this->folder_acls[$name]);
204                 }
205             }
206         }
208         /* Get global filter config */
209         if (!session::is_set("gmailfilter")){
210             $ui= get_userinfo();
211             $base= get_base_from_people($ui->dn);
212             $gmailfilter= array( "depselect"       => $base,
213                     "muser"            => "",
214                     "regex"           => "*");
215             session::set("gmailfilter", $gmailfilter);
216         }
217     }
220     /*! \brief  Returns all group members once with 'dn' and once with 'mail'.
221       This function is used to summarize ACLs by member acls.
222       @return Array   Containing all members, with mail and dn
223      */ 
224     function get_member()
225     {
226         $member = array('all' => array(), 'mail' => array());
227         $ldap = $this->config->get_ldap_link();
228         $ldap->cd($this->config->current['BASE']);
229         if(isset($this->parent->by_object['group'])){
230             foreach($this->parent->by_object['group']->memberUid as $uid){
231                 if(!isset($this->parent->by_object['group']->dnMapping[$uid])) continue;
232                 $dn = $this->parent->by_object['group']->dnMapping[$uid];
233                 $member['all'][$uid] = $uid;
234                 if($ldap->object_match_filter($dn,"(&(objectClass=gosaMailAccount)(".$this->mailMethod->getUAttrib()."=*))")){
235                     $ldap->cat($dn);
236                     $attrs = $ldap->fetch();
237                     $member['mail'][$uid] = $attrs[$this->mailMethod->getUAttrib()][0]; 
238                 }
239             }
240         }else{
241             if(!isset($this->attrs['memberUid'])) return($member);
242             $uattrib = $this->mailMethod->getUAttrib();
243             $users = get_list("(&(objectClass=person)(objectClass=gosaAccount)(uid=*))",
244                     "users",$this->config->current['BASE'],
245                     array("uid","objectClass",$uattrib),GL_SUBSEARCH | GL_NO_ACL_CHECK);
246             foreach($users as $user){
247                 $member['all'][$user['uid'][0]] = $user['dn'];
248                 if(isset($user[$uattrib]) 
249                         && in_array_strict("gosaMailAccount",$user['objectClass']) 
250                         && (in_array_strict($user['uid'][0], $this->attrs['memberUid']))){
251                     $member['mail'][$user['uid'][0]] = $user[$uattrib][0];
252                 }
253             }
254         }
255         return($member);
256     }
259     function execute()
260     {
261         /* Call parent execute */
262         plugin::execute();
264         /* Log view */
265         if($this->is_account && !$this->view_logged){
266             $this->view_logged = TRUE;
267             new log("view","groups/".get_class($this),$this->dn);
268         }
270         /****************
271           Account status
272          ****************/
274         if(!$this->multiple_support_active){
276             if(isset($_POST['modify_state'])){
277                 if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
278                     $this->is_account= FALSE;
279                 }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
280                     $this->is_account= TRUE;
281                 }
282             }
284             if ($this->is_account){
285                 $reason = "";
286                 if(!$this->mailMethod->accountRemoveable($reason)){
287                     $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
288                 }else{
289                     $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::
290                             featuresEnabled(_("Mail")));
291                 }
292             } else {
293                 $reason = "";
294                 if(!$this->mailMethod->accountCreateable($reason)){
295                     $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
296                 }else{
297                     $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::
298                             featuresDisabled(_("Mail")));
300                     /* Show checkbox that allows us to remove imap entry too*/
301                     if($this->initially_was_account){
302                         $c = "";
303                         if($this->remove_folder_from_imap){
304                             $c= " checked ";
305                         }
306                         $display .= "<h3>Shared folder delete options</h3>
307                             <input class='center' type='checkbox' name='remove_folder_from_imap' value='1' ".$c."
308                             title='"._("Remove shared folder from mail server database when entry gets removed in LDAP")."'>";
309                         $display .= _("Remove the shared folder and all its contents after saving this account");
310                     }
311                 }
312                 return ($display);
313             }
314         }
317         /****************
318           Preset mail attribute
319          ****************/
320         if(empty($this->mail) && $this->mailMethod->mailEqualsCN() && !$this->initially_was_account){
321             if($this->mailMethod->domainSelectionEnabled()){
322                 $this->mail = &$this->parent->by_object['group']->cn;
323             }
324         }
327         /****************
328           Forward addresses
329          ****************/
331         if (isset($_POST['add_local_forwarder'])){
332             $this->mailAddressSelect= new mailAddressSelect($this->config, get_userinfo());
333             $this->dialog= TRUE;
334         }
336         if (isset($_POST['mailAddressSelect_cancel'])){
337             $this->mailAddressSelect= FALSE;
338             $this->dialog= FALSE;
339         }
341         if (isset($_POST['mailAddressSelect_save'])){
342             if($this->acl_is_writeable("gosaMailForwardingAddress")){
343                 $list = $this->mailAddressSelect->save();
344                 foreach ($list as $entry){
345                     $val = $entry['mail'][0];
346                     if (!in_array ($val, $this->gosaMailAlternateAddress) && $val != $this->mail){
347                         $this->addForwarder($val);
348                         $this->is_modified= TRUE;
349                     }
350                 }
351                 $this->mailAddressSelect= FALSE;
352                 $this->dialog= FALSE;
353             } else {
354                 msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
355             }
356         }
358         if($this->mailAddressSelect instanceOf mailAddressSelect){
359             $used  = array();
360             $used['mail'] = array_values($this->gosaMailAlternateAddress);
361             $used['mail'] = array_merge($used['mail'], array_values($this->gosaMailForwardingAddress));
362             $used['mail'][] = $this->mail;
364             // Build up blocklist
365             session::set('filterBlacklist', $used);
366             return($this->mailAddressSelect->execute());
367         }
369         if (isset($_POST['add_forwarder'])){
370             if ($_POST['forward_address'] != ""){
371                 $address= get_post('forward_address');
372                 $valid= FALSE;
373                 if (!tests::is_email($address)){
374                     if (!tests::is_email($address, TRUE)){
375                         if ($this->is_template){
376                             $valid= TRUE;
377                         } else {
378                             msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
379                                         "","","your-address@your-domain.com"),ERROR_DIALOG);
380                         }
381                     }
382                 } elseif ($address == $this->mail
383                         || in_array_strict($address, $this->gosaMailAlternateAddress)) {
384                     msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
385                 } else {
386                     $valid= TRUE;
387                 }
388                 if ($valid){
389                     if($this->acl_is_writeable("gosaMailForwardingAddress")){
390                         $this->addForwarder ($address);
391                         $this->is_modified= TRUE;
392                     }
393                 }
394             }
395         }
396         if (isset($_POST['delete_forwarder'])){
397             $this->delForwarder (get_post('forwarder_list'));
398         }
401         /****************
402           Alternate addresses
403          ****************/
405         if (isset($_POST['add_alternate'])){
406             $valid= FALSE;
407             if (!tests::is_email(get_post('alternate_address'))){
408                 if ($this->is_template){
409                     if (!(tests::is_email(get_post('alternate_address'), TRUE))){
410                         msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
411                                     "","","your-domain@your-domain.com"),     ERROR_DIALOG);
412                     } else {
413                         $valid= TRUE;
414                     }
415                 } else {
416                     msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
417                                 "","","your-domain@your-domain.com"),       ERROR_DIALOG);
418                 }
419             } else {
420                 $valid= TRUE;
421             }
422             if ($valid && ($user= $this->addAlternate (get_post('alternate_address'))) != ""){
423                 $ui= get_userinfo();
424                 $addon= "";
425                 if ($user[0] == "!") {
426                     $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
427                 } else {
428                     $addon= sprintf(_("Address is already in use by user '%s'."), $user);
429                 }
430                 msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
431                         "$addon</i>", ERROR_DIALOG);
432             }
433         }
434         if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
435             $this->delAlternate (get_post('alternates_list'));
436         }
439         /****************
440           SMARTY- Assign smarty variables
441          ****************/
443         /* Load templating engine */
444         $smarty= get_smarty();
445         $smarty->assign("initially_was_account", $this->initially_was_account);
446         $smarty->assign("isModifyableMail", $this->mailMethod->isModifyableMail());
447         $smarty->assign("isModifyableServer", $this->mailMethod->isModifyableServer());
448         $smarty->assign("mailEqualsCN", $this->mailMethod->mailEqualsCN());
449         $smarty->assign("folder_acls" , $this->postable_acls());
450         $smarty->assign("AclTypes" ,    set_post($this->AclTypes));
451         $smarty->assign("Effective",    $this->get_effective_member_acls());
452         $smarty->assign("show_effective_memeber",    $this->show_effective_memeber);
454         $smarty->assign("quotaEnabled", $this->mailMethod->quotaEnabled());
455         if($this->mailMethod->quotaEnabled()){
456             $smarty->assign("gosaMailQuota", set_post($this->gosaMailQuota));
457             $smarty->assign("quotaUsage",   mailMethod::quota_to_image($this->quotaUsage,$this->gosaMailQuota)); 
458         }
460         $smarty->assign("MailDomains",set_post($this->mailDomainParts));
461         $smarty->assign("MailDomain" ,set_post($this->mailDomainPart));
462         $smarty->assign("MailServers",set_post($this->mailMethod->getMailServers()));
463         $smarty->assign("allowSieveManagement", $this->mailMethod->allowSieveManagement());
465         $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
466         $smarty->assign("folderTypesEnabled",$this->mailMethod->folderTypesEnabled());
467         $smarty->assign("AvailableFolderTypes", set_post( $this->AvailableFolderTypes));
468         $smarty->assign("FolderType", set_post($this->FolderType));
470         if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
471             if($this->acl_is_readable("gosaMailQuota")){
472                 $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
473                 $smarty->assign("quotadefined", "true");
474             }else{
475                 $smarty->assign("quotadefined", "true");
476                 $smarty->assign("quotausage", "-");
477             }
478         } else {
479             $smarty->assign("quotadefined", "false");
480         }
482         /* Assign acls */
483         $tmp = $this->plInfo();
484         foreach($tmp['plProvidedAcls'] as $name => $translation) {
485             $smarty->assign($name."ACL",$this->getacl($name));
486         }
487         foreach($this->attributes as $name){
488             $smarty->assign($name, set_post($this->$name));
489         }
493         $smarty->assign("mailServers", set_post($this->mailMethod->getMailServers()));
494         if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
495             $smarty->assign("only_local", "checked");
496         }else{
497             $smarty->assign("only_local", "");
498         }
501         /******
502           Multi edit support 
503          ******/
504         foreach($this->attributes as $attr){
505             if(in_array_strict($attr,$this->multi_boxes)){
506                 $smarty->assign("use_".$attr,TRUE);
507             }else{
508                 $smarty->assign("use_".$attr,FALSE);
509             }
510         }
512         /* Multiple support handling */
513         foreach(array("kolabFolderType") as $attr){
514             if(in_array_strict($attr,$this->multi_boxes)){
515                 $smarty->assign("use_".$attr,TRUE);
516             }else{
517                 $smarty->assign("use_".$attr,FALSE);
518             }
519         }
521         $smarty->assign("Forward_all",     set_post($this->gosaMailForwardingAddress));
522         $smarty->assign("Forward_some",    set_post($this->gosaMailForwardingAddress_Some));
523         $smarty->assign("multiple_support",set_post($this->multiple_support_active));
525         $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE, dirname(__FILE__)));
526         return ($display);
527     }
530     /* remove object from parent */
531     function remove_from_parent()
532     {
533         if(!$this->initially_was_account){
534             return;
535         }
537         /* If domain part was selectable, contruct mail address */
538         if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
539             $this->mail = $this->mail."@".$this->mailDomainPart;
540         }
542         /* Remove GOsa attributes */
543         plugin::remove_from_parent();
545         /* Zero arrays */
546         $this->attrs['gosaMailAlternateAddress'] = array();
547         $this->attrs['gosaMailForwardingAddress']= array();
549         $this->mailMethod->fixAttributesOnRemove();
550         $this->cleanup();
551         $ldap = $this->config->get_ldap_link();
552         $ldap->cd($this->dn);
553         $ldap->modify ($this->attrs); 
554         if (!$ldap->success()){
555             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
556         }
558         new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
561         /* Let the mailMethod remove this mailbox, e.g. from imap and
562            update shared folder membership, ACL may need to be updated.
563          */
564         if (!$this->is_template && $this->remove_folder_from_imap){
566             if(!$this->mailMethod->connect()){
567                 msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
568                             $this->mailMethod->get_error()), ERROR_DIALOG);
569             }else{
570                 if(!$this->mailMethod->deleteMailbox()){
571                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox: %s"),
572                                 $this->mailMethod->get_error()), ERROR_DIALOG);
573                 }
574                 if(!$this->mailMethod->updateSharedFolder()){
575                     msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions: %s"),
576                                 $this->mailMethod->get_error()), ERROR_DIALOG);
577                 }
578             }
579         }
580         $this->mailMethod->disconnect();
583         /* Optionally execute a command after we're done */
584         $this->handle_post_events("remove");
585     }
588     /* Save data to object */
589     function save_object()
590     {
591         /* Check if user wants to remove the shared folder from imap too 
592          */
593         if($this->initially_was_account && !$this->is_account){
594             if(isset($_POST['remove_folder_from_imap'])){
595                 $this->remove_folder_from_imap = true;
596             }else{
597                 $this->remove_folder_from_imap = false;
598             }
599         }
600         if (isset($_POST['mailedit'])){
602             if(isset($_POST['show_effective_memeber'])){
603                 $this->show_effective_memeber = !$this->show_effective_memeber;
604             }
606             $mail = $this->mail;
607             $server = $this->gosaMailServer;
608             plugin::save_object();
610             if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
611                 $this->gosaMailServer = $server;
612             }
613             if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
614                 $this->mail = $mail;
615             }else{
617                 if($this->mailMethod->mailEqualsCN()){
618                     $this->mail = &$this->parent->by_object['group']->cn;
619                     if(isset($_POST['MailDomain'])){
620                         $this->mailDomainPart = get_post('MailDomain');
621                     }
622                 }
624                 /* Get posted mail domain part, if necessary
625                  */
626                 if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
627                     if(in_array_strict(get_post('MailDomain'), $this->mailDomainParts)){
628                         $this->mailDomainPart = get_post('MailDomain');
629                     }
630                 }
631             }
633             /* Get folder type 
634              */
635             if($this->mailMethod->folderTypesEnabled()){
636                 if(isset($_POST['FolderTypeCAT'])){
637                     $this->FolderType['CAT']     = get_post('FolderTypeCAT');
638                 }
639                 if(isset($_POST['FolderTypeSUB_CAT'])){
640                     $this->FolderType['SUB_CAT'] = get_post('FolderTypeSUB_CAT');
641                 }
642             }
644             /* Handle posted ACL changes. 
645                Add/del member acls.
646              */
647             if(isset($_POST['mail_acls_posted'])){
648                 $new_acls = array();
649                 foreach(array("__anyone__","__member__") as $attr){
650                     $id = (isset($this->aclPostToId[$attr])) ? $this->aclPostToId[$attr] : -1;
651                     if(isset($_POST['acl_value_'.$id])){ 
652                         $new_acls[$attr] = get_post('acl_value_'.$id);
653                     }else{
654                         $new_acls[$attr] = $this->folder_acls[$attr];
655                     }
656                 }
659                 foreach($this->folder_acls as $user => $acl){
660                     if($user == "__member__" || $user == "__anyone__") continue;
662                     $id = (isset($this->aclPostToId[$user])) ? $this->aclPostToId[$user] : -1;
664                     if(isset($_POST['remove_acl_user_'.$id])){
665                     }elseif(isset($_POST['acl_user_'.$id])){
666                         if($user != get_post('acl_user_'.$id)){
667                             $new_acls[get_post('acl_user_'.$id)] = get_post('acl_value_'.$id);
668                         }else{
669                             $new_acls[$user] = get_post('acl_value_'.$id);
670                         }
671                     }else{
672                         $new_acls[$user] = $acl;
673                     }
674                 }
675                 if(isset($_POST['add_acl_user'])){
676                     $new_acls[_('New')] = $this->folder_acls['__anyone__'];
677                 }
678                 $this->folder_acls = $new_acls;
679             }
681             /* Handle GOsa mail delivery flags.
682              */
684             /* Assemble mail delivery mode
685                The mode field in ldap consists of values between braces, this must
686                be called when 'mail' is set, because checkboxes may not be set when
687                we're in some other dialog.
689                Example for gosaMailDeliveryMode [LR        ]
690                L - Local delivery
691                R - Reject when exceeding mailsize limit
692                S - Use spam filter
693                V - Use vacation message
694                C - Use custom sieve script
695                I - Only insider delivery */
696             $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
698 #     if($this->acl_is_writeable("gosaMailDeliveryModeL")){
699 #       if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
700 #         $tmp.="L";
701 #       }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
702 #         $tmp = preg_replace("/L/","",$tmp);
703 #       }
704 #     }
706             $opts = array(
707                     "I"   => "only_local");
709             foreach($opts as $flag => $post){
711                 if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
712                     if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
713                         $tmp.= $flag;
714                     }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
715                         $tmp = preg_replace("/".$flag."/","",$tmp);
716                     }
717                 }
718             }
720             $tmp= "[$tmp]";
721             if ($this->gosaMailDeliveryMode != $tmp){
722                 $this->is_modified= TRUE;
723             }
724             $this->gosaMailDeliveryMode= $tmp;
725         }
726     }
729     /* Save data to LDAP, depending on is_account we save or delete */
730     function save()
731     {
732         $ldap= $this->config->get_ldap_link();
734         /* If domain part was selectable, contruct mail address */
735         if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
737             if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
738                 $this->mail = $this->mail."@".$this->mailDomainPart;
739             }
740         }
742         /* Enforce lowercase mail address and trim whitespaces
743          */
744         $this->mail = trim(strtolower($this->mail));
747         /* Create acls 
748          */
749         $this->acl = array("anyone ".$this->folder_acls['__anyone__']);
750         $member = $this->get_member();
751         $new_folder_acls = array("anyone" => $this->folder_acls['__anyone__']);
752         foreach($member['mail'] as $uid => $mail){
754             /* Do not save overridden acls */
755             if(isset($this->folder_acls[$mail])){
756                 continue;
757             }
759             $this->acl[] = $mail." ".$this->folder_acls['__member__'];
760             $new_folder_acls[$mail]=$this->folder_acls['__member__'];
761         }
762         foreach($this->folder_acls as $user => $acls){
763             if(preg_match("/^__/",$user)) continue;
764             $this->acl[] = $user." ".$acls;
765             $new_folder_acls[$user]=$acls;
766         }
767         $this->folder_acls = $new_folder_acls;
768         $this->acl = array_unique($this->acl);
770         /* Call parents save to prepare $this->attrs */
771         plugin::save();
773         /* Save arrays */
774         $this->attrs['gosaMailAlternateAddress'] = $this->gosaMailAlternateAddress;
775         $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
777         /* Map method attributes */
778         $this->mailMethod->fixAttributesOnStore();
780         /* Save data to LDAP */
781         $ldap->cd($this->dn);
782         $this->cleanup();
783         $ldap->modify ($this->attrs); 
784         if (!$ldap->success()){
785             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
786         }
788         if($this->initially_was_account){
789             new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
790         }else{
791             new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());  
792         }
794         /* Do imap/sieve actions,
795          */
796         $this->mailMethod->connect();
797         if(!$this->mailMethod->is_connected()){
798             msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
799                         $this->mailMethod->get_error()), ERROR_DIALOG);
800         }else{
801             if(!$this->mailMethod->updateMailbox()){
802                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"),
803                             $this->mailMethod->get_error()), ERROR_DIALOG);
804             }
805             if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
806                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"),
807                             $this->mailMethod->get_error()), ERROR_DIALOG);
808             }
809             /* Save Folder Types, if available 
810              */
811             if($this->mailMethod->folderTypesEnabled()){
812                 $this->mailMethod->setFolderType($this->FolderType);
813             }
814             if(!$this->mailMethod->setFolderACLs($this->folder_acls)){
815                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions: %s"),
816                             $this->mailMethod->get_error()), ERROR_DIALOG);
817             }
818         }
819         $this->mailMethod->disconnect();
821         /* Optionally execute a command after we're done */
822         if ($this->initially_was_account == $this->is_account){
823             if ($this->is_modified){
824                 $this->handle_post_events("modify");
825             }
826         } else {
827             $this->handle_post_events("add");
828         }
829     }
833     /* Check formular input */
834     function check()
835     {
836         if(!$this->is_account) return array();
837         $ldap= $this->config->get_ldap_link();
840         /* Call common method to give check the hook */
841         $message= plugin::check();
843         /* Ensure that this group isn't renamed if the mailMethod enforces cn mailAttributes 
844          */
845         if($this->mailMethod->mailEqualsCN() && $this->initially_was_account){
846             if($this->cn != $this->orig_cn){
847                 $message[] = sprintf(_("The group 'cn' has changed. It can't be changed due to the fact that mail method '%s' relies on it!")
848                         ,get_class($this->mailMethod));
849             }
850         }
852         if(empty($this->gosaMailServer)){
853             $message[]= msgPool::noserver(_("Mail"));
854         }
856         /* Mail address checks */
857         $mail = $this->mail;
858         if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
859             if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
860                 $mail.= "@".$this->mailDomainPart;
861             }
862             if (empty($mail)){
863                 $message[]= msgPool::required(_("Primary address"));
864             }elseif (!tests::is_email($mail)){
865                 $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
866             }
867         }
869         /* Check quota */
870         if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
871             if (!is_numeric($this->gosaMailQuota)) {
872                 $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/[0-9]/");
873             } else {
874                 $this->gosaMailQuota= (int) $this->gosaMailQuota;
875             }
876         }
878         /* Check if this mail address is already in use */
879         $ldap->cd($this->config->current['BASE']);
880         $filter = "(&(!(objectClass=gosaUserTemplate))(!(cn=".$this->cn."))".
881             "(objectClass=gosaMailAccount)".
882             "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
883         $ldap->search($filter,array("cn"));
884         if ($ldap->count() != 0){
885             $message[]= msgPool::duplicated(_("Mail address"));
886         }
888         /* Check rejectsize for integer */
889         if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailQuota")){
890             if (!is_numeric($this->gosaMailMaxSize)){
891                 $message[]= msgPool::invalid(_("Mail max size"));
892             } else {
893                 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
894             }
895         }
897         /* Need gosaMailMaxSize if use_mailsize_limit is checked */
898         if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
899             $message[]= _("You need to set the maximum mail size in order to reject anything.");
900         }
902         if(empty($this->gosaMailServer)){
903             $message[] = msgPool::required(_("Mail server"));
904         }
906         return ($message);
907     }
909     /* Adapt from template, using 'dn' */
910     function adapt_from_template($dn, $skip= array())
911     {
912         plugin::adapt_from_template($dn, $skip);
914         foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
916             if (in_array_strict($val, $skip)){
917                 continue;
918             }
920             $this->$val= array();
921             if (isset($this->attrs["$val"]["count"])){
922                 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
923                     $value= $this->attrs["$val"][$i];
924                     foreach (array("sn", "givenName", "uid") as $repl){
925                         if (preg_match("/%$repl/i", $value)){
926                             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
927                         }
928                     }
929                     array_push($this->$val, $value);
930                 }
931             }
932         }
933     }
937     function make_name($attrs)
938     {
939         $name= "";
940         if (isset($attrs['sn'][0])){
941             $name= $attrs['sn'][0];
942         }
943         if (isset($attrs['givenName'][0])){
944             if ($name != ""){
945                 $name.= ", ".$attrs['givenName'][0];
946             } else {
947                 $name.= $attrs['givenName'][0];
948             }
949         }
950         if ($name != ""){
951             $name.= " ";
952         }
954         return ($name);
955     }
957     function getCopyDialog()
958     {
959         if(!$this->is_account) return("");
961         $smarty = get_smarty();
962         $smarty->assign("gosaMailAlternateAddress",    set_post($this->gosaMailAlternateAddress));
963         $smarty->assign("gosaMailForwardingAddress",   set_post($this->gosaMailForwardingAddress));
964         $smarty->assign("mail", set_post($this->mail));
965         $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE, dirname(__FILE__)));
966         $ret = array();
967         $ret['string'] = $display;
968         $ret['status'] = "";
969         return($ret);
970     }
972     function saveCopyDialog()
973     {
974         if(!$this->is_account) return;
976         /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
977          */
978         $this->execute();
979         if(isset($_POST['mail'])){
980             $this->mail = get_post('mail');
981         }
982     }
985     function PrepareForCopyPaste($source)
986     {
987         plugin::PrepareForCopyPaste($source);
989         /* Reset alternate mail addresses */
990         $this->gosaMailAlternateAddress = array();
991     }
994     /* Return plugin informations for acl handling  */
995     static function plInfo()
996     {
997         return (array(
998                     "plShortName"   => _("Mail"),
999                     "plDescription" => _("Group mail"),
1000                     "plSelfModify"  => FALSE,
1001                     "plDepends"     => array(),
1002                     "plPriority"    => 10,
1003                     "plSection"     => array("administration"),
1004                     "plCategory"    => array("groups"), 
1005                     "plProvidedAcls"=> array(
1006                         "mail"                      => _("Mail address"),
1007                         "gosaMailQuota"             => _("Quota size"),
1008                         "gosaMailServer"            => _("Mail server"),
1009                         "kolabFolderType"           => _("Folder type")." ("._("Kolab").")",
1010                         "gosaMailAlternateAddress"  => _("Alternate addresses"),
1011                         "gosaMailForwardingAddress" => _("Forwarding addresses"),
1012                         "gosaMailDeliveryModeI"     => _("Only local"),
1013                         "acl"                       => _("Permissions"))
1014                     ));
1015     }
1018     /* Remove given ACL for given member (uid,mail) ..
1019      */
1020     function removeUserAcl($index )
1021     {
1022         if(isset($this->imapacl[$index])){
1023             unset($this->imapacl[$index]);
1024         }
1025     }
1027     function multiple_execute()
1028     {
1029         return($this->execute());
1030     }
1033     function init_multiple_support($attrs,$all)
1034     {
1035         plugin::init_multiple_support($attrs,$all);
1037         $this->gosaMailForwardingAddress = array();
1038         if(isset($attrs['gosaMailForwardingAddress'])){
1039             for($i = 0 ; $i < $attrs['gosaMailForwardingAddress']['count'] ; $i++){
1040                 $this->gosaMailForwardingAddress[] = $attrs['gosaMailForwardingAddress'][$i];
1041             }
1042         }
1044         $this->gosaMailForwardingAddress_Some = array();
1045         if(isset($all['gosaMailForwardingAddress'])){
1046             for($i = 0 ; $i < $all['gosaMailForwardingAddress']['count'] ; $i++){
1047                 if(!in_array_strict($all['gosaMailForwardingAddress'][$i],$this->gosaMailForwardingAddress)){
1048                     $this->gosaMailForwardingAddress_Some[] = $all['gosaMailForwardingAddress'][$i];
1049                 }
1050             }
1051         }
1052     }
1054     function multiple_save_object()
1055     {
1056         if(isset($_POST['multiple_mail_group_posted'])){
1057             plugin::multiple_save_object();
1059             foreach(array("kolabFolderType") as $attr){
1060                 if(isset($_POST['use_'.$attr])){
1061                     $this->multi_boxes[] = $attr;
1062                 }
1063             }
1065             /* Add special kolab attributes */
1066             if(preg_match("/olab/i",$this->config->get_cfg_value("core","mailMethod"))){
1067                 if(isset($_POST['kolabFolderTypeType']) && $this->acl_is_writeable("kolabFolderType")){
1068                     $this->kolabFolderTypeType = get_post("kolabFolderTypeType");
1069                     $this->kolabFolderTypeSubType = get_post("kolabFolderTypeSubType");
1070                 }
1071             }
1073             /* Collect data and re-assign it to the imapacl array */
1074             if ($this->acl_is_writeable("acl")){
1075                 $this->imapacl= array();
1076                 $this->imapacl['%members%']= $_POST['member_permissions'];
1077                 $this->imapacl['anyone']= $_POST['default_permissions'];
1078                 foreach ($this->indexed_user as $nr => $user){
1079                     if (!isset($_POST["user_$nr"])){
1080                         continue;
1081                     }
1082                     if ($_POST["user_$nr"] != $user ||
1083                             $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
1084                         $this->is_modified= TRUE;
1085                     }
1086                     $this->imapacl[get_post("user_$nr")]= get_post("perm_$nr");
1087                 }
1088             }
1089         }
1090     }
1093     /* Return selected values for multiple edit */
1094     function get_multi_edit_values()
1095     {
1096         $ret = plugin::get_multi_edit_values();
1097         $ret['Forward_some'] = $this->gosaMailForwardingAddress_Some;    
1098         $ret['Forward_all'] = $this->gosaMailForwardingAddress;    
1099         if(in_array_strict('kolabFolderType',$this->multi_boxes)){
1100             $ret['kolabFolderTypeType'] = $this->kolabFolderTypeType;
1101             $ret['kolabFolderTypeSubType'] = $this->kolabFolderTypeSubType;
1102         }
1103         if(in_array_strict("acl",$this->multi_boxes)){
1104             $ret['imapacl'] = $this->imapacl;
1105         }
1106         return($ret);
1107     }
1109     function set_multi_edit_values($attrs)
1110     {
1111         $forward = array();
1112         foreach($attrs['Forward_some'] as $addr){
1113             if(in_array_strict($addr,$this->gosaMailForwardingAddress)){
1114                 $forward[] = $addr;
1115             }
1116         }
1117         foreach($attrs['Forward_all'] as $addr){
1118             $forward[] = $addr;
1119         }
1120         plugin::set_multi_edit_values($attrs);
1121         $this->gosaMailForwardingAddress = $forward;
1122     }
1125     /*! \brief  Add given mail address to the list of forwarders.
1126      */
1127     function addForwarder($address)
1128     {
1129         if(empty($address)) return;
1130         $this->gosaMailForwardingAddress[]= $address;
1131         $this->gosaMailForwardingAddress= array_unique($this->gosaMailForwardingAddress);
1133         /* Update multiple edit values too */
1134         if($this->multiple_support_active){
1135             $this->gosaMailForwardingAddress_Some= 
1136                 array_remove_entries (array($address),$this->gosaMailForwardingAddress_Some);
1137         }
1139         sort ($this->gosaMailForwardingAddress);
1140         reset ($this->gosaMailForwardingAddress);
1141         $this->is_modified= TRUE;
1142     }
1145     /*! \brief  Removes the given mail address from the forwarders 
1146      */
1147     function delForwarder($addresses)
1148     {
1149         if(empty($addresses)) return;
1150         $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
1151                 $this->gosaMailForwardingAddress);
1153         /* Update multiple edit values too */
1154         if($this->multiple_support_active){
1155             $this->gosaMailForwardingAddress_Some = array_remove_entries ($addresses,
1156                     $this->gosaMailForwardingAddress_Some);
1157         }
1158         $this->is_modified= TRUE;
1159     }
1162     /*! \brief  Add given mail address to the list of alternate adresses ,
1163       .          check if this mal address is used, skip adding in this case
1164      */
1165     function addAlternate($address)
1166     {
1167         if(empty($address)) continue;
1168         $ldap= $this->config->get_ldap_link();
1170         $address= strtolower($address);
1172         /* Is this address already assigned in LDAP? */
1173         $ldap->cd ($this->config->current['BASE']);
1174         $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
1175                 "(gosaMailAlternateAddress=$address)))", array("cn", "uid"));
1177         if ($ldap->count() > 0){
1178             $attrs= $ldap->fetch ();
1179             if (!isset($attrs["uid"])) {
1180                 return ("!".$attrs["cn"][0]);
1181             }
1182             return ($attrs["uid"][0]);
1183         }
1185         /* Add to list of alternates */
1186         if (!in_array_strict($address, $this->gosaMailAlternateAddress)){
1187             $this->gosaMailAlternateAddress[]= $address;
1188         }
1190         sort ($this->gosaMailAlternateAddress);
1191         reset ($this->gosaMailAlternateAddress);
1192         $this->is_modified= TRUE;
1194         return ("");
1195     }
1198     /*! \brief  Removes the given mail address from the alternate addresses  
1199      */
1200     function delAlternate($addresses)
1201     {
1202         if(!count($addresses)) return;
1203         $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
1204                 $this->gosaMailAlternateAddress);
1205         $this->is_modified= TRUE;
1206     }
1209     function postable_acls()
1210     {
1211         $ret = array();
1212         $this->aclPostToId = array();
1213         foreach($this->folder_acls as $name => $acl){
1214             $id = count($this->aclPostToId);
1215             $this->aclPostToId[$name] = $id;
1216             $ret[set_post($name)] = array("name" => set_post($name),"acl" => set_post($acl),"post_name" => $id);
1217         }
1218         return($ret);
1219     }
1222     function get_effective_member_acls()
1223     {
1224         $tmp = array();
1225         $member = $this->get_member();
1226         foreach($member['mail'] as $uid => $mail){
1228             /* Do not save overridden acls */
1229             if(isset($this->folder_acls[$mail])){
1230                 continue;
1231             }
1234             $tmp[$mail]  = $this->folder_acls['__member__'];
1235         }
1236         return($tmp);
1237     }
1240     function allow_remove()
1241     {
1242         $resason = "";
1243         if(!$this->mailMethod->allow_remove($reason)){
1244             return($reason);
1245         }
1246         return("");
1247     }
1250 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1251 ?>