Code

aa453044596b6348131cc59e17e809657f1c74a5
[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++){
101                 $str = trim($this->attrs['acl'][$i]);
103                 /* Be carefull here, since kolab22 uses spaces in the acls (herbert read anon/post)
104                  */
105                 $name = trim(preg_replace("/^([^\s]*).*$/","\\1",$str));
106                 $acl  = trim(preg_replace("/^[^\s]*+\s/","",$str));
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($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($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($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("gosaMailAccount",$user['objectClass']) 
250                         && (in_array($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($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($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($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(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){
651                     if(isset($_POST['acl_value_'.$attr])){ 
652                         $new_acls[$attr] = get_post('acl_value_'.$attr);
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;
661                     $id = $this->aclPostToId[$user];
662                     if(isset($_POST['remove_acl_user_'.$id])){
663                     }elseif(isset($_POST['acl_user_'.$id])){
664                         if($user != get_post('acl_user_'.$id)){
665                             $new_acls[get_post('acl_user_'.$id)] = get_post('acl_value_'.$id);
666                         }else{
667                             $new_acls[$user] = get_post('acl_value_'.$id);
668                         }
669                     }else{
670                         $new_acls[$user] = $acl;
671                     }
672                 }
673                 if(isset($_POST['add_acl_user'])){
674                     $new_acls[_('New')] = $this->folder_acls['__anyone__'];
675                 }
676                 $this->folder_acls = $new_acls;
677             }
679             /* Handle GOsa mail delivery flags.
680              */
682             /* Assemble mail delivery mode
683                The mode field in ldap consists of values between braces, this must
684                be called when 'mail' is set, because checkboxes may not be set when
685                we're in some other dialog.
687                Example for gosaMailDeliveryMode [LR        ]
688                L - Local delivery
689                R - Reject when exceeding mailsize limit
690                S - Use spam filter
691                V - Use vacation message
692                C - Use custom sieve script
693                I - Only insider delivery */
694             $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
696 #     if($this->acl_is_writeable("gosaMailDeliveryModeL")){
697 #       if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
698 #         $tmp.="L";
699 #       }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
700 #         $tmp = preg_replace("/L/","",$tmp);
701 #       }
702 #     }
704             $opts = array(
705                     "I"   => "only_local");
707             foreach($opts as $flag => $post){
709                 if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
710                     if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
711                         $tmp.= $flag;
712                     }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
713                         $tmp = preg_replace("/".$flag."/","",$tmp);
714                     }
715                 }
716             }
718             $tmp= "[$tmp]";
719             if ($this->gosaMailDeliveryMode != $tmp){
720                 $this->is_modified= TRUE;
721             }
722             $this->gosaMailDeliveryMode= $tmp;
723         }
724     }
727     /* Save data to LDAP, depending on is_account we save or delete */
728     function save()
729     {
730         $ldap= $this->config->get_ldap_link();
732         /* If domain part was selectable, contruct mail address */
733         if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
735             if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
736                 $this->mail = $this->mail."@".$this->mailDomainPart;
737             }
738         }
740         /* Enforce lowercase mail address and trim whitespaces
741          */
742         $this->mail = trim(strtolower($this->mail));
745         /* Create acls 
746          */
747         $this->acl = array("anyone ".$this->folder_acls['__anyone__']);
748         $member = $this->get_member();
749         $new_folder_acls = array("anyone" => $this->folder_acls['__anyone__']);
750         foreach($member['mail'] as $uid => $mail){
752             /* Do not save overridden acls */
753             if(isset($this->folder_acls[$mail])){
754                 continue;
755             }
757             $this->acl[] = $mail." ".$this->folder_acls['__member__'];
758             $new_folder_acls[$mail]=$this->folder_acls['__member__'];
759         }
760         foreach($this->folder_acls as $user => $acls){
761             if(preg_match("/^__/",$user)) continue;
762             $this->acl[] = $user." ".$acls;
763             $new_folder_acls[$user]=$acls;
764         }
765         $this->folder_acls = $new_folder_acls;
766         $this->acl = array_unique($this->acl);
768         /* Call parents save to prepare $this->attrs */
769         plugin::save();
771         /* Save arrays */
772         $this->attrs['gosaMailAlternateAddress'] = $this->gosaMailAlternateAddress;
773         $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
775         /* Map method attributes */
776         $this->mailMethod->fixAttributesOnStore();
778         /* Save data to LDAP */
779         $ldap->cd($this->dn);
780         $this->cleanup();
781         $ldap->modify ($this->attrs); 
782         if (!$ldap->success()){
783             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
784         }
786         if($this->initially_was_account){
787             new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
788         }else{
789             new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());  
790         }
792         /* Do imap/sieve actions,
793          */
794         $this->mailMethod->connect();
795         if(!$this->mailMethod->is_connected()){
796             msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
797                         $this->mailMethod->get_error()), ERROR_DIALOG);
798         }else{
799             if(!$this->mailMethod->updateMailbox()){
800                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"),
801                             $this->mailMethod->get_error()), ERROR_DIALOG);
802             }
803             if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
804                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"),
805                             $this->mailMethod->get_error()), ERROR_DIALOG);
806             }
807             /* Save Folder Types, if available 
808              */
809             if($this->mailMethod->folderTypesEnabled()){
810                 $this->mailMethod->setFolderType($this->FolderType);
811             }
812             if(!$this->mailMethod->setFolderACLs($this->folder_acls)){
813                 msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions: %s"),
814                             $this->mailMethod->get_error()), ERROR_DIALOG);
815             }
816         }
817         $this->mailMethod->disconnect();
819         /* Optionally execute a command after we're done */
820         if ($this->initially_was_account == $this->is_account){
821             if ($this->is_modified){
822                 $this->handle_post_events("modify");
823             }
824         } else {
825             $this->handle_post_events("add");
826         }
827     }
831     /* Check formular input */
832     function check()
833     {
834         if(!$this->is_account) return array();
835         $ldap= $this->config->get_ldap_link();
838         /* Call common method to give check the hook */
839         $message= plugin::check();
841         /* Ensure that this group isn't renamed if the mailMethod enforces cn mailAttributes 
842          */
843         if($this->mailMethod->mailEqualsCN() && $this->initially_was_account){
844             if($this->cn != $this->orig_cn){
845                 $message[] = sprintf(_("The group 'cn' has changed. It can't be changed due to the fact that mail method '%s' relies on it!")
846                         ,get_class($this->mailMethod));
847             }
848         }
850         if(empty($this->gosaMailServer)){
851             $message[]= msgPool::noserver(_("Mail"));
852         }
854         /* Mail address checks */
855         $mail = $this->mail;
856         if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
857             if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
858                 $mail.= "@".$this->mailDomainPart;
859             }
860             if (empty($mail)){
861                 $message[]= msgPool::required(_("Primary address"));
862             }elseif (!tests::is_email($mail)){
863                 $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
864             }
865         }
867         /* Check quota */
868         if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
869             if (!is_numeric($this->gosaMailQuota)) {
870                 $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/[0-9]/");
871             } else {
872                 $this->gosaMailQuota= (int) $this->gosaMailQuota;
873             }
874         }
876         /* Check if this mail address is already in use */
877         $ldap->cd($this->config->current['BASE']);
878         $filter = "(&(!(objectClass=gosaUserTemplate))(!(cn=".$this->cn."))".
879             "(objectClass=gosaMailAccount)".
880             "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
881         $ldap->search($filter,array("cn"));
882         if ($ldap->count() != 0){
883             $message[]= msgPool::duplicated(_("Mail address"));
884         }
886         /* Check rejectsize for integer */
887         if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailQuota")){
888             if (!is_numeric($this->gosaMailMaxSize)){
889                 $message[]= msgPool::invalid(_("Mail max size"));
890             } else {
891                 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
892             }
893         }
895         /* Need gosaMailMaxSize if use_mailsize_limit is checked */
896         if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
897             $message[]= _("You need to set the maximum mail size in order to reject anything.");
898         }
900         if(empty($this->gosaMailServer)){
901             $message[] = msgPool::required(_("Mail server"));
902         }
904         return ($message);
905     }
907     /* Adapt from template, using 'dn' */
908     function adapt_from_template($dn, $skip= array())
909     {
910         plugin::adapt_from_template($dn, $skip);
912         foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
914             if (in_array($val, $skip)){
915                 continue;
916             }
918             $this->$val= array();
919             if (isset($this->attrs["$val"]["count"])){
920                 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
921                     $value= $this->attrs["$val"][$i];
922                     foreach (array("sn", "givenName", "uid") as $repl){
923                         if (preg_match("/%$repl/i", $value)){
924                             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
925                         }
926                     }
927                     array_push($this->$val, $value);
928                 }
929             }
930         }
931     }
935     function make_name($attrs)
936     {
937         $name= "";
938         if (isset($attrs['sn'][0])){
939             $name= $attrs['sn'][0];
940         }
941         if (isset($attrs['givenName'][0])){
942             if ($name != ""){
943                 $name.= ", ".$attrs['givenName'][0];
944             } else {
945                 $name.= $attrs['givenName'][0];
946             }
947         }
948         if ($name != ""){
949             $name.= " ";
950         }
952         return ($name);
953     }
955     function getCopyDialog()
956     {
957         if(!$this->is_account) return("");
959         $smarty = get_smarty();
960         $smarty->assign("gosaMailAlternateAddress",    set_post($this->gosaMailAlternateAddress));
961         $smarty->assign("gosaMailForwardingAddress",   set_post($this->gosaMailForwardingAddress));
962         $smarty->assign("mail", set_post($this->mail));
963         $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE, dirname(__FILE__)));
964         $ret = array();
965         $ret['string'] = $display;
966         $ret['status'] = "";
967         return($ret);
968     }
970     function saveCopyDialog()
971     {
972         if(!$this->is_account) return;
974         /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
975          */
976         $this->execute();
977         if(isset($_POST['mail'])){
978             $this->mail = get_post('mail');
979         }
980     }
983     function PrepareForCopyPaste($source)
984     {
985         plugin::PrepareForCopyPaste($source);
987         /* Reset alternate mail addresses */
988         $this->gosaMailAlternateAddress = array();
989     }
992     /* Return plugin informations for acl handling  */
993     static function plInfo()
994     {
995         return (array(
996                     "plShortName"   => _("Mail"),
997                     "plDescription" => _("Group mail"),
998                     "plSelfModify"  => FALSE,
999                     "plDepends"     => array(),
1000                     "plPriority"    => 10,
1001                     "plSection"     => array("administration"),
1002                     "plCategory"    => array("groups"), 
1003                     "plProvidedAcls"=> array(
1004                         "mail"                      => _("Mail address"),
1005                         "gosaMailQuota"             => _("Quota size"),
1006                         "gosaMailServer"            => _("Mail server"),
1007                         "kolabFolderType"           => _("Folder type")." ("._("Kolab").")",
1008                         "gosaMailAlternateAddress"  => _("Alternate addresses"),
1009                         "gosaMailForwardingAddress" => _("Forwarding addresses"),
1010                         "gosaMailDeliveryModeI"     => _("Only local"),
1011                         "acl"                       => _("Permissions"))
1012                     ));
1013     }
1016     /* Remove given ACL for given member (uid,mail) ..
1017      */
1018     function removeUserAcl($index )
1019     {
1020         if(isset($this->imapacl[$index])){
1021             unset($this->imapacl[$index]);
1022         }
1023     }
1025     function multiple_execute()
1026     {
1027         return($this->execute());
1028     }
1031     function init_multiple_support($attrs,$all)
1032     {
1033         plugin::init_multiple_support($attrs,$all);
1035         $this->gosaMailForwardingAddress = array();
1036         if(isset($attrs['gosaMailForwardingAddress'])){
1037             for($i = 0 ; $i < $attrs['gosaMailForwardingAddress']['count'] ; $i++){
1038                 $this->gosaMailForwardingAddress[] = $attrs['gosaMailForwardingAddress'][$i];
1039             }
1040         }
1042         $this->gosaMailForwardingAddress_Some = array();
1043         if(isset($all['gosaMailForwardingAddress'])){
1044             for($i = 0 ; $i < $all['gosaMailForwardingAddress']['count'] ; $i++){
1045                 if(!in_array($all['gosaMailForwardingAddress'][$i],$this->gosaMailForwardingAddress)){
1046                     $this->gosaMailForwardingAddress_Some[] = $all['gosaMailForwardingAddress'][$i];
1047                 }
1048             }
1049         }
1050     }
1052     function multiple_save_object()
1053     {
1054         if(isset($_POST['multiple_mail_group_posted'])){
1055             plugin::multiple_save_object();
1057             foreach(array("kolabFolderType") as $attr){
1058                 if(isset($_POST['use_'.$attr])){
1059                     $this->multi_boxes[] = $attr;
1060                 }
1061             }
1063             /* Add special kolab attributes */
1064             if(preg_match("/olab/i",$this->config->get_cfg_value("core","mailMethod"))){
1065                 if(isset($_POST['kolabFolderTypeType']) && $this->acl_is_writeable("kolabFolderType")){
1066                     $this->kolabFolderTypeType = get_post("kolabFolderTypeType");
1067                     $this->kolabFolderTypeSubType = get_post("kolabFolderTypeSubType");
1068                 }
1069             }
1071             /* Collect data and re-assign it to the imapacl array */
1072             if ($this->acl_is_writeable("acl")){
1073                 $this->imapacl= array();
1074                 $this->imapacl['%members%']= $_POST['member_permissions'];
1075                 $this->imapacl['anyone']= $_POST['default_permissions'];
1076                 foreach ($this->indexed_user as $nr => $user){
1077                     if (!isset($_POST["user_$nr"])){
1078                         continue;
1079                     }
1080                     if ($_POST["user_$nr"] != $user ||
1081                             $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
1082                         $this->is_modified= TRUE;
1083                     }
1084                     $this->imapacl[get_post("user_$nr")]= get_post("perm_$nr");
1085                 }
1086             }
1087         }
1088     }
1091     /* Return selected values for multiple edit */
1092     function get_multi_edit_values()
1093     {
1094         $ret = plugin::get_multi_edit_values();
1095         $ret['Forward_some'] = $this->gosaMailForwardingAddress_Some;    
1096         $ret['Forward_all'] = $this->gosaMailForwardingAddress;    
1097         if(in_array('kolabFolderType',$this->multi_boxes)){
1098             $ret['kolabFolderTypeType'] = $this->kolabFolderTypeType;
1099             $ret['kolabFolderTypeSubType'] = $this->kolabFolderTypeSubType;
1100         }
1101         if(in_array("acl",$this->multi_boxes)){
1102             $ret['imapacl'] = $this->imapacl;
1103         }
1104         return($ret);
1105     }
1107     function set_multi_edit_values($attrs)
1108     {
1109         $forward = array();
1110         foreach($attrs['Forward_some'] as $addr){
1111             if(in_array($addr,$this->gosaMailForwardingAddress)){
1112                 $forward[] = $addr;
1113             }
1114         }
1115         foreach($attrs['Forward_all'] as $addr){
1116             $forward[] = $addr;
1117         }
1118         plugin::set_multi_edit_values($attrs);
1119         $this->gosaMailForwardingAddress = $forward;
1120     }
1123     /*! \brief  Add given mail address to the list of forwarders.
1124      */
1125     function addForwarder($address)
1126     {
1127         if(empty($address)) return;
1128         $this->gosaMailForwardingAddress[]= $address;
1129         $this->gosaMailForwardingAddress= array_unique($this->gosaMailForwardingAddress);
1131         /* Update multiple edit values too */
1132         if($this->multiple_support_active){
1133             $this->gosaMailForwardingAddress_Some= 
1134                 array_remove_entries (array($address),$this->gosaMailForwardingAddress_Some);
1135         }
1137         sort ($this->gosaMailForwardingAddress);
1138         reset ($this->gosaMailForwardingAddress);
1139         $this->is_modified= TRUE;
1140     }
1143     /*! \brief  Removes the given mail address from the forwarders 
1144      */
1145     function delForwarder($addresses)
1146     {
1147         if(empty($addresses)) return;
1148         $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
1149                 $this->gosaMailForwardingAddress);
1151         /* Update multiple edit values too */
1152         if($this->multiple_support_active){
1153             $this->gosaMailForwardingAddress_Some = array_remove_entries ($addresses,
1154                     $this->gosaMailForwardingAddress_Some);
1155         }
1156         $this->is_modified= TRUE;
1157     }
1160     /*! \brief  Add given mail address to the list of alternate adresses ,
1161       .          check if this mal address is used, skip adding in this case
1162      */
1163     function addAlternate($address)
1164     {
1165         if(empty($address)) continue;
1166         $ldap= $this->config->get_ldap_link();
1168         $address= strtolower($address);
1170         /* Is this address already assigned in LDAP? */
1171         $ldap->cd ($this->config->current['BASE']);
1172         $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
1173                 "(gosaMailAlternateAddress=$address)))", array("cn", "uid"));
1175         if ($ldap->count() > 0){
1176             $attrs= $ldap->fetch ();
1177             if (!isset($attrs["uid"])) {
1178                 return ("!".$attrs["cn"][0]);
1179             }
1180             return ($attrs["uid"][0]);
1181         }
1183         /* Add to list of alternates */
1184         if (!in_array($address, $this->gosaMailAlternateAddress)){
1185             $this->gosaMailAlternateAddress[]= $address;
1186         }
1188         sort ($this->gosaMailAlternateAddress);
1189         reset ($this->gosaMailAlternateAddress);
1190         $this->is_modified= TRUE;
1192         return ("");
1193     }
1196     /*! \brief  Removes the given mail address from the alternate addresses  
1197      */
1198     function delAlternate($addresses)
1199     {
1200         if(!count($addresses)) return;
1201         $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
1202                 $this->gosaMailAlternateAddress);
1203         $this->is_modified= TRUE;
1204     }
1207     function postable_acls()
1208     {
1209         $ret = array();
1210         $this->aclPostToId = array();
1211         foreach($this->folder_acls as $name => $acl){
1212             $id = count($this->aclPostToId);
1213             $this->aclPostToId[$name] = $id;
1214             $ret[set_post($name)] = array("name" => set_post($name),"acl" => set_post($acl),"post_name" => $id);
1215         }
1216         return($ret);
1217     }
1220     function get_effective_member_acls()
1221     {
1222         $tmp = array();
1223         $member = $this->get_member();
1224         foreach($member['mail'] as $uid => $mail){
1226             /* Do not save overridden acls */
1227             if(isset($this->folder_acls[$mail])){
1228                 continue;
1229             }
1232             $tmp[$mail]  = $this->folder_acls['__member__'];
1233         }
1234         return($tmp);
1235     }
1238     function allow_remove()
1239     {
1240         $resason = "";
1241         if(!$this->mailMethod->allow_remove($reason)){
1242             return($reason);
1243         }
1244         return("");
1245     }
1248 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1249 ?>