Code

updated groupMail to use methods ACL defaults
[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 $forward_dialog             = 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 ="";
47   function __construct (&$config, $dn= NULL, $ui= NULL)
48   {
49     plugin::plugin($config, $dn);
51     /* Get attributes from parent object
52      */
53     foreach(array("uid","cn") as $attr){
54       if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
55         $this->$attr = &$this->parent->by_object['group']->$attr;
56       }elseif(isset($this->attrs[$attr])){
57         $this->$attr = $this->attrs[$attr][0];
58       }
59     }
61     /* Intialize the used mailMethod
62      */
63     $tmp = new mailMethod($config,$this,"group");
64     $this->mailMethod           = $tmp->get_method();
65     $this->mailMethod->fixAttributesOnLoad();
66     $this->mailDomainParts      = $this->mailMethod->getMailDomains();
67     $this->AvailableFolderTypes = $this->mailMethod->getAvailableFolderTypes();
68     $this->MailBoxes = array();
70     /* Remember account status
71      */
72     $this->initially_was_account = $this->is_account;
74     /* Load folder_acls with defaults.
75       anyone -- The default acl, will be written to ldap.
76       member -- The ACL used for the members.
77      */ 
78     $this->folder_acls = $this->mailMethod->getDefaultACLs();
80     /* Load acls
81        The most used acl will be used as member acl, this
82         shortens the listed acls.        
83        This may be merged/overwritten by the mail methods.
84      */
85     $ldap = $this->config->get_ldap_link();
86     if(isset($this->attrs['acl'])){
87       for($i = 0; $i < $this->attrs['acl']['count'] ; $i++){
88         $str = trim($this->attrs['acl'][$i]);
90         /* Be carefull here, since kolab22 uses spaces in the acls (herbert read anon/post)
91          */
92         $name = trim(preg_replace("/^([^\s]*).*$/","\\1",$str));
93         $acl  = trim(preg_replace("/^[^\s]*+\s/","",$str));
94         if($name == "anyone") $name = "__anyone__";
95         $this->folder_acls[$name] = $acl;
96       }
97     }
99     /* Initialize configured values
100      */
101     if($this->is_account){
102       if($this->mailMethod->connect() && $this->mailMethod->account_exists()){
104         /* Read quota */
105         $this->gosaMailQuota = $this->mailMethod->getQuota($this->gosaMailQuota);
106         $this->quotaUsage = $this->mailMethod->getQuotaUsage($this->quotaUsage);
107         if($this->mailMethod->is_error()){
108           msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings: %s"),
109                 $this->mailMethod->get_error()), ERROR_DIALOG);
110         }
112         /* Read mailboxes */
113         $this->MailBoxes = $this->mailMethod->getMailboxList($this->MailBoxes);
114         if($this->mailMethod->is_error()){
115           msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes: %s"),
116                 $this->mailMethod->get_error()), ERROR_DIALOG);
117         }
119         /* Receive folder types */
120         $this->FolderType = $this->mailMethod->getFolderType($this->FolderType);
121         if($this->mailMethod->is_error()){
122           msg_dialog::display(_("Mail error"), sprintf(_("Cannot receive folder types: %s"),
123                 $this->mailMethod->get_error()), ERROR_DIALOG);
124         }
126         /* Receive permissions */  
127         $this->folder_acls = $this->mailMethod->getFolderACLs($this->folder_acls);
128         if($this->mailMethod->is_error()){
129           msg_dialog::display(_("Mail error"), sprintf(_("Cannot receive folder permissions: %s"),
130                 $this->mailMethod->get_error()), ERROR_DIALOG);
131         }
133       }elseif(!$this->mailMethod->is_connected()){
134         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
135               $this->mailMethod->get_error()), ERROR_DIALOG);
136       }elseif(!$this->mailMethod->account_exists()){
137         msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"),
138               $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
139       }
141       /* If the doamin part is selectable, we have to split the mail address
142        */
143       if(!(!$this->mailMethod->isModifyableMail() && $this->is_account)){
144         if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
145           $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
146           $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
147           if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
148             $this->mailDomainParts[] = $this->mailDomainPart;
149           }
150         }
151       }
153       /* Load attributes containing arrays */
154       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
155         $this->$val= array();
156         if (isset($this->attrs["$val"]["count"])){
157           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
158             array_push($this->$val, $this->attrs["$val"][$i]);
159           }
160         }
161       }
162     }
164     /* Disconnect mailMethod. Connect on demand later.
165      */
166     $this->mailMethod->disconnect();
167     $this->AclTypes = $this->mailMethod->getAclTypes();
169     /* Summarize most used ACLs as member acl 
170      */
171     if(count($this->folder_acls) > 2){
172       $acl_usage = array();
173       $most_acl = "";
174       $most_cnt = 0;
175       $member = $this->get_member();
176       foreach($this->folder_acls as $user => $acl){
177         if(!isset($acl_usage[$acl])) $acl_usage[$acl]=0;
178         $acl_usage[$acl] ++;
179         if($acl_usage[$acl] > $most_cnt){
180           $most_cnt = $acl_usage[$acl];
181           $most_acl = $acl;
182         }
183       }
184       $this->folder_acls['__member__'] = $most_acl;  
185       foreach($this->folder_acls as $name => $acl){
186         if(preg_match("/^__/",$name)) continue;
187         if($acl == $most_acl && in_array($name,$member['mail'])){
188           unset($this->folder_acls[$name]);
189         }
190       }
191     }
193     /* Get global filter config */
194     if (!session::is_set("gmailfilter")){
195       $ui= get_userinfo();
196       $base= get_base_from_people($ui->dn);
197       $gmailfilter= array( "depselect"       => $base,
198           "muser"            => "",
199           "regex"           => "*");
200       session::set("gmailfilter", $gmailfilter);
201     }
202   }
204  
205   /*! \brief  Returns all group members once with 'dn' and once with 'mail'.
206               This function is used to summarize ACLs by member acls.
207       @return Array   Containing all members, with mail and dn
208    */ 
209   function get_member()
210   {
211     $member = array('all' => array(), 'mail' => array());
212     $ldap = $this->config->get_ldap_link();
213     $ldap->cd($this->config->current['BASE']);
214     if(isset($this->parent->by_object['group'])){
215       foreach($this->parent->by_object['group']->memberUid as $uid){
216         $dn = $this->parent->by_object['group']->dnMapping[$uid];
217         $member['all'][$uid] = $uid;
218         if($ldap->object_match_filter($dn,"(&(objectClass=gosaMailAccount)(".$this->mailMethod->getUAttrib()."=*))")){
219           $ldap->cat($dn);
220           $attrs = $ldap->fetch();
221           $member['mail'][$uid] = $attrs[$this->mailMethod->getUAttrib()][0]; 
222         }
223       }
224     }else{
225       if(!isset($this->attrs['memberUid'])) return($member);
226       $uattrib = $this->mailMethod->getUAttrib();
227       $users = get_list("(&(objectClass=person)(objectClass=gosaAccount)(uid=*))",
228               "users",$this->config->current['BASE'],
229               array("uid","objectClass",$uattrib),GL_SUBSEARCH | GL_NO_ACL_CHECK);
230       foreach($users as $user){
231         $member['all'][$user['uid'][0]] = $user['dn'];
232         if(isset($user[$uattrib]) 
233             && in_array("gosaMailAccount",$user['objectClass']) 
234             && (in_array($user['uid'][0], $this->attrs['memberUid']))){
235           $member['mail'][$user['uid'][0]] = $user[$uattrib][0];
236         }
237       }
238     }
239     return($member);
240   }
243   function execute()
244   {
245     /* Call parent execute */
246     plugin::execute();
248     /* Log view */
249     if($this->is_account && !$this->view_logged){
250       $this->view_logged = TRUE;
251       new log("view","groups/".get_class($this),$this->dn);
252     }
254     $this->get_member();
255  
256     /****************
257       Account status
258      ****************/
260     if(!$this->multiple_support_active){
262       if(isset($_POST['modify_state'])){
263         if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
264           $this->is_account= FALSE;
265         }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
266           $this->is_account= TRUE;
267         }
268       }
270       if ($this->is_account){
271         $reason = "";
272         if(!$this->mailMethod->accountRemoveable($reason)){
273           $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
274         }else{
275           $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::
276               featuresEnabled(_("Mail")));
277         }
278       } else {
279         $reason = "";
280         if(!$this->mailMethod->accountCreateable($reason)){
281           $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
282         }else{
283           $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::
284               featuresDisabled(_("Mail")));
286           /* Show checkbox that allows us to remove imap entry too*/
287           if($this->initially_was_account){
288             $c = "";
289             if($this->remove_folder_from_imap){
290               $c= " checked ";
291             }
292             $display .= "<h2>Shared folder delete options</h2>
293               <input class='center' type='checkbox' name='remove_folder_from_imap' value='1' ".$c."
294               title='"._("Remove shared folder from mail server database when entry gets removed in LDAP")."'>";
295             $display .= _("Remove the shared folder and all its contents after saving this account");
296           }
297         }
298         return ($display);
299       }
300     }
303     /****************
304       Forward addresses
305      ****************/
306     if (isset($_POST['add_local_forwarder'])){
307       $this->forward_dialog= TRUE;
308       $this->dialog= TRUE;
309     }
310     if (isset($_POST['add_locals_cancel'])){
311       $this->forward_dialog= FALSE;
312       $this->dialog= FALSE;
313     }
314     if (isset($_POST['add_locals_finish'])){
315       if (isset($_POST['local_list'])){
316         if($this->acl_is_writeable("gosaMailForwardingAddress")){
317           foreach ($_POST['local_list'] as $val){
318             if (!in_array ($val, $this->gosaMailAlternateAddress) &&
319                 $val != $this->mail){
320               $this->addForwarder($val);
321               $this->is_modified= TRUE;
322             }
323           }
324         }
325         $this->forward_dialog= FALSE;
326         $this->dialog= FALSE;
327       } else {
328         msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
329       }
330     }
332     if (isset($_POST['add_forwarder'])){
333       if ($_POST['forward_address'] != ""){
334         $address= $_POST['forward_address'];
335         $valid= FALSE;
336         if (!tests::is_email($address)){
337           if (!tests::is_email($address, TRUE)){
338             if ($this->is_template){
339               $valid= TRUE;
340             } else {
341               msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
342                     "","","your-address@your-domain.com"),ERROR_DIALOG);
343             }
344           }
345         } elseif ($address == $this->mail
346             || in_array($address, $this->gosaMailAlternateAddress)) {
347           msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
348         } else {
349           $valid= TRUE;
350         }
351         if ($valid){
352           if($this->acl_is_writeable("gosaMailForwardingAddress")){
353             $this->addForwarder ($address);
354             $this->is_modified= TRUE;
355           }
356         }
357       }
358     }
359     if (isset($_POST['delete_forwarder'])){
360       $this->delForwarder ($_POST['forwarder_list']);
361     }
362     if ($this->forward_dialog){
363       return($this->display_forward_dialog());
364     } 
366  
367     /****************
368       Alternate addresses
369      ****************/
371     if (isset($_POST['add_alternate'])){
372       $valid= FALSE;
373       if (!tests::is_email($_POST['alternate_address'])){
374         if ($this->is_template){
375           if (!(tests::is_email($_POST['alternate_address'], TRUE))){
376             msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
377                   "","","your-domain@your-domain.com"),     ERROR_DIALOG);
378           } else {
379             $valid= TRUE;
380           }
381         } else {
382           msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
383                 "","","your-domain@your-domain.com"),       ERROR_DIALOG);
384         }
385       } else {
386         $valid= TRUE;
387       }
388       if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
389         $ui= get_userinfo();
390         if ($user != $ui->username){
391           msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."&nbsp;".
392               sprintf(_("Address is already in use by user '%s'."), $user), ERROR_DIALOG);
393         }
394       }
395     }
396     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
397       $this->delAlternate ($_POST['alternates_list']);
398     }
401     /****************
402       SMARTY- Assign smarty variables
403      ****************/
405     /* Load templating engine */
406     $smarty= get_smarty();
407     $smarty->assign("initially_was_account", $this->initially_was_account);
408     $smarty->assign("isModifyableMail", $this->mailMethod->isModifyableMail());
409     $smarty->assign("isModifyableServer", $this->mailMethod->isModifyableServer());
410     $smarty->assign("mailEqualsCN", $this->mailMethod->mailEqualsCN());
411     $smarty->assign("folder_acls" , $this->postable_acls());
412     $smarty->assign("AclTypes" ,    $this->AclTypes);
413   
414     $smarty->assign("quotaEnabled", $this->mailMethod->quotaEnabled());
415     if($this->mailMethod->quotaEnabled()){
416       $smarty->assign("gosaMailQuota",$this->gosaMailQuota);
417       $smarty->assign("quotaUsage",   mailMethod::quota_to_image($this->quotaUsage,$this->gosaMailQuota)); 
418     }
420     $smarty->assign("MailDomains", $this->mailDomainParts);
421     $smarty->assign("MailDomain" , $this->mailDomainPart);
422     $smarty->assign("MailServers", $this->mailMethod->getMailServers());
423     $smarty->assign("allowSieveManagement", $this->mailMethod->allowSieveManagement());
425     $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
426     $smarty->assign("folderTypesEnabled",$this->mailMethod->folderTypesEnabled());
427     $smarty->assign("AvailableFolderTypes", $this->AvailableFolderTypes);
428     $smarty->assign("FolderType", $this->FolderType);
429  
430     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
431       if($this->acl_is_readable("gosaMailQuota")){
432         $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
433         $smarty->assign("quotadefined", "true");
434       }else{
435         $smarty->assign("quotadefined", "true");
436         $smarty->assign("quotausage", "-");
437       }
438     } else {
439       $smarty->assign("quotadefined", "false");
440     }
442     /* Assign acls */
443     $tmp = $this->plInfo();
444     foreach($tmp['plProvidedAcls'] as $name => $translation) {
445       $smarty->assign($name."ACL",$this->getacl($name));
446     }
447     foreach($this->attributes as $name){
448       $smarty->assign($name,$this->$name);
449     }
453     $smarty->assign("mailServers", $this->mailMethod->getMailServers());
454     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
455       $smarty->assign("only_local", "checked");
456     }else{
457       $smarty->assign("only_local", "");
458     }
461     /******
462       Multi edit support 
463      ******/
464     foreach($this->attributes as $attr){
465       if(in_array($attr,$this->multi_boxes)){
466         $smarty->assign("use_".$attr,TRUE);
467       }else{
468         $smarty->assign("use_".$attr,FALSE);
469       }
470     }
472     /* Multiple support handling */
473     foreach(array("kolabFolderType") as $attr){
474       if(in_array($attr,$this->multi_boxes)){
475         $smarty->assign("use_".$attr,TRUE);
476       }else{
477         $smarty->assign("use_".$attr,FALSE);
478       }
479     }
481     $smarty->assign("Forward_all",$this->gosaMailForwardingAddress);
482     $smarty->assign("Forward_some",$this->gosaMailForwardingAddress_Some);
483     $smarty->assign("multiple_support",$this->multiple_support_active);
485     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE, dirname(__FILE__)));
486     return ($display);
487   }
490   /* remove object from parent */
491   function remove_from_parent()
492   {
493     if(!$this->initially_was_account){
494       return;
495     }
496  
497     /* If domain part was selectable, contruct mail address */
498     if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
499       $this->mail = $this->mail."@".$this->mailDomainPart;
500     }
502     /* Remove GOsa attributes */
503     plugin::remove_from_parent();
505     /* Zero arrays */
506     $this->attrs['gosaMailAlternateAddress'] = array();
507     $this->attrs['gosaMailForwardingAddress']= array();
508     $this->attrs['gosaSharedFolderTarget']= array();
510     $this->mailMethod->fixAttributesOnRemove();
511     $this->cleanup();
512     $ldap = $this->config->get_ldap_link();
513     $ldap->cd($this->dn);
514     $ldap->modify ($this->attrs); 
515     if (!$ldap->success()){
516       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
517     }
519     new log("remove","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
522     /* Let the mailMethod remove this mailbox, e.g. from imap and
523        update shared folder membership, ACL may need to be updated.
524      */
525     if (!$this->is_template && $this->remove_folder_from_imap){
527       if(!$this->mailMethod->connect()){
528         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
529               $this->mailMethod->get_error()), ERROR_DIALOG);
530       }else{
531         if(!$this->mailMethod->deleteMailbox()){
532           msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox: %s"),
533                 $this->mailMethod->get_error()), ERROR_DIALOG);
534         }
535         if(!$this->mailMethod->updateSharedFolder()){
536           msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions: %s"),
537                 $this->mailMethod->get_error()), ERROR_DIALOG);
538         }
539       }
540     }
541     $this->mailMethod->disconnect();
544     /* Optionally execute a command after we're done */
545     $this->handle_post_events("remove");
546   }
549   /* Save data to object */
550   function save_object()
551   {
552     /* Check if user wants to remove the shared folder from imap too 
553      */
554     if($this->initially_was_account && !$this->is_account){
555       if(isset($_POST['remove_folder_from_imap'])){
556         $this->remove_folder_from_imap = true;
557       }else{
558         $this->remove_folder_from_imap = false;
559       }
560     }
561     if (isset($_POST['mailedit'])){
563       $mail = $this->mail;
564       $server = $this->gosaMailServer;
565       plugin::save_object();
567       if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
568         $this->gosaMailServer = $server;
569       }
570       if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
571         $this->mail = $mail;
572       }else{
574         if($this->mailMethod->mailEqualsCN()){
575           $this->mail = &$this->parent->by_object['group']->cn;
576           if(isset($_POST['MailDomain'])){
577             $this->mailDomainPart = get_post('MailDomain');
578           }
579         }
581       /* Get posted mail domain part, if necessary
582        */
583       if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
584         if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
585           $this->mailDomainPart = get_post('MailDomain');
586         }
587       }
588     }
590       /* Get folder type 
591        */
592       if($this->mailMethod->folderTypesEnabled()){
593         if(isset($_POST['FolderTypeCAT'])){
594         $this->FolderType['CAT']     = get_post('FolderTypeCAT');
595         }
596         if(isset($_POST['FolderTypeSUB_CAT'])){
597           $this->FolderType['SUB_CAT'] = get_post('FolderTypeSUB_CAT');
598         }
599       }
601       /* Handle posted ACL changes. 
602          Add/del member acls.
603        */
604       if(isset($_POST['mail_acls_posted'])){
605         $new_acls = array();
606         foreach(array("__anyone__","__member__") as $attr){
607           $pname = base64_encode($attr);
608           if(get_post('acl_value_'.$pname)){ 
609             $new_acls[$attr] = get_post('acl_value_'.$pname);
610           }else{
611             $new_acls[$attr] = $this->folder_acls[$attr];
612           }
613         }
615         foreach($this->folder_acls as $user => $acl){
616           $pname = base64_encode($user);
617           if($user == "__member__" || $user == "__anyone__") continue;
618           if(isset($_POST['remove_acl_user_'.$pname])){
619           }elseif(isset($_POST['acl_user_'.$pname])){
620             if($user != get_post('acl_user_'.$pname)){
621               $new_acls[get_post('acl_user_'.$pname)] = get_post('acl_value_'.$pname);
622             }else{
623               $new_acls[$user] = get_post('acl_value_'.$pname);
624             }
625           }else{
626             $new_acls[$user] = $acl;
627           }
628         }
629         if(isset($_POST['add_acl_user'])){
630           $new_acls[_('New')] = $this->folder_acls['__anyone__'];
631         }
632         $this->folder_acls = $new_acls;
633       }
635       /* Handle GOsa mail delivery flags.
636        */
638     /* Assemble mail delivery mode
639        The mode field in ldap consists of values between braces, this must
640        be called when 'mail' is set, because checkboxes may not be set when
641        we're in some other dialog.
643        Example for gosaMailDeliveryMode [LR        ]
644        L - Local delivery
645        R - Reject when exceeding mailsize limit
646        S - Use spam filter
647        V - Use vacation message
648        C - Use custom sieve script
649        I - Only insider delivery */
650       $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
651       if($this->acl_is_writeable("gosaMailDeliveryModeL")){
652         if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
653           $tmp.="L";
654         }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
655           $tmp = preg_replace("/L/","",$tmp);
656         }
657       }
659       $opts = array(
660           "R"   => "use_mailsize_limit",
661           "S"   => "use_spam_filter",
662           "V"   => "use_vacation",
663           "C"   => "own_script",
664           "I"   => "only_local");
666       foreach($opts as $flag => $post){
667         if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
668           if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
669             $tmp.= $flag;
670           }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
671             $tmp = preg_replace("/".$flag."/","",$tmp);
672           }
673         }
674       }
676       $tmp= "[$tmp]";
677       if ($this->gosaMailDeliveryMode != $tmp){
678         $this->is_modified= TRUE;
679       }
680       $this->gosaMailDeliveryMode= $tmp;
681     }
682   }
685   /* Save data to LDAP, depending on is_account we save or delete */
686   function save()
687   {
688     $ldap= $this->config->get_ldap_link();
690     /* If domain part was selectable, contruct mail address */
691     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
693       if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
694         $this->mail = $this->mail."@".$this->mailDomainPart;
695       }
696     }
698     /* Enforce lowercase mail address and trim whitespaces
699      */
700     $this->mail = trim(strtolower($this->mail));
701     
703     /* Create acls 
704      */
705     $this->acl = array("anyone ".$this->folder_acls['__anyone__']);
706     $member = $this->get_member();
707     $new_folder_acls = array("anyone" => $this->folder_acls['__anyone__']);
708     foreach($member['mail'] as $uid => $mail){
709       $this->acl[] = $mail." ".$this->folder_acls['__member__'];
710       $new_folder_acls[$mail]=$this->folder_acls['__member__'];
711     }
712     foreach($this->folder_acls as $user => $acls){
713       if(preg_match("/^__/",$user)) continue;
714       $this->acl[] = $user." ".$acls;
715       $new_folder_acls[$user]=$acls;
716     }
717     $this->folder_acls = $new_folder_acls;
718     $this->acl = array_unique($this->acl);
719       
720     /* Call parents save to prepare $this->attrs */
721     plugin::save();
723     /* Save arrays */
724     $this->attrs['gosaMailAlternateAddress'] = $this->gosaMailAlternateAddress;
725     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
727     /* Map method attributes */
728     $this->mailMethod->fixAttributesOnStore();
730     /* Save data to LDAP */
731     $ldap->cd($this->dn);
732     $this->cleanup();
733     $ldap->modify ($this->attrs); 
734     if (!$ldap->success()){
735       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
736     }
737     
738     if($this->initially_was_account){
739       new log("modify","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
740     }else{
741       new log("create","groups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());  
742     }
743     
744     /* Do imap/sieve actions,
745      */
746     $this->mailMethod->connect();
747     if(!$this->mailMethod->is_connected()){
748       msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"),
749             $this->mailMethod->get_error()), ERROR_DIALOG);
750     }else{
751       if(!$this->mailMethod->updateMailbox()){
752         msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"),
753               $this->mailMethod->get_error()), ERROR_DIALOG);
754       }
755       if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
756         msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"),
757               $this->mailMethod->get_error()), ERROR_DIALOG);
758       }
759       /* Save Folder Types, if available 
760        */
761       if($this->mailMethod->folderTypesEnabled()){
762         $this->mailMethod->setFolderType($this->FolderType);
763       }
764       if(!$this->mailMethod->setFolderACLs($this->folder_acls)){
765         msg_dialog::display(_("Mail error"), sprintf(_("Cannot update shared folder permissions: %s"),
766               $this->mailMethod->get_error()), ERROR_DIALOG);
767       }
768     }
769     $this->mailMethod->disconnect();
771     /* Optionally execute a command after we're done */
772     if ($this->initially_was_account == $this->is_account){
773       if ($this->is_modified){
774         $this->handle_post_events("modify");
775       }
776     } else {
777       $this->handle_post_events("add");
778     }
779   }
783   /* Check formular input */
784   function check()
785   {
786     if(!$this->is_account) return array();
787     $ldap= $this->config->get_ldap_link();
789     /* Call common method to give check the hook */
790     $message= plugin::check();
792     if(empty($this->gosaMailServer)){
793       $message[]= msgPool::noserver(_("Mail"));
794     }
796     /* Mail address checks */
797     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
798       $mail = $this->mail;
799       if($this->mailMethod->domainSelectionEnabled() || $this->mailMethod->mailEqualsCN()){
800         $mail.= "@".$this->mailDomainPart;
801       }
802       if (empty($mail)){
803         $message[]= msgPool::required(_("Primary address"));
804       }elseif (!tests::is_email($mail)){
805         $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
806       }
807     }
808     
809     /* Check quota */
810     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
811       if (!is_numeric($this->gosaMailQuota)) {
812         $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/[0-9]/");
813       } else {
814         $this->gosaMailQuota= (int) $this->gosaMailQuota;
815       }
816     }
818     /* Check rejectsize for integer */
819     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailQuota")){
820       if (!is_numeric($this->gosaMailMaxSize)){
821         $message[]= msgPool::invalid(_("Mail max size"));
822       } else {
823         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
824       }
825     }
827     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
828     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
829       $message[]= _("You need to set the maximum mail size in order to reject anything.");
830     }
832     if(empty($this->gosaMailServer)){
833       $message[] = msgPool::required(_("Mail server"));
834     }
836     return ($message);
837   }
839   /* Adapt from template, using 'dn' */
840   function adapt_from_template($dn, $skip= array())
841   {
842     plugin::adapt_from_template($dn, $skip);
844     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
845  
846       if (in_array($val, $skip)){
847         continue;
848       }
850       $this->$val= array();
851       if (isset($this->attrs["$val"]["count"])){
852         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
853           $value= $this->attrs["$val"][$i];
854           foreach (array("sn", "givenName", "uid") as $repl){
855             if (preg_match("/%$repl/i", $value)){
856               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
857             }
858           }
859           array_push($this->$val, $value);
860         }
861       }
862     }
863   }
867   function make_name($attrs)
868   {
869     $name= "";
870     if (isset($attrs['sn'][0])){
871       $name= $attrs['sn'][0];
872     }
873     if (isset($attrs['givenName'][0])){
874       if ($name != ""){
875         $name.= ", ".$attrs['givenName'][0];
876       } else {
877         $name.= $attrs['givenName'][0];
878       }
879     }
880     if ($name != ""){
881       $name.= " ";
882     }
884     return ($name);
885   }
887   function getCopyDialog()
888   {
889     if(!$this->is_account) return("");
891     $smarty = get_smarty();
892     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
893     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
894     $smarty->assign("mail",$this->mail);
895     $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE, dirname(__FILE__)));
896     $ret = array();
897     $ret['string'] = $display;
898     $ret['status'] = "";
899     return($ret);
900   }
902   function saveCopyDialog()
903   {
904     if(!$this->is_account) return;
906     /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
907     */
908     $this->execute();
909     if(isset($_POST['mail'])){
910       $this->mail = $_POST['mail'];
911     }
912   }
915   function PrepareForCopyPaste($source)
916   {
917     plugin::PrepareForCopyPaste($source);
918  
919     /* Reset alternate mail addresses */
920     $this->gosaMailAlternateAddress = array();
921   }
924   /* Return plugin informations for acl handling  */
925   static function plInfo()
926   {
927     return (array(
928           "plShortName"   => _("Mail"),
929           "plDescription" => _("Group mail"),
930           "plSelfModify"  => FALSE,
931           "plDepends"     => array(),
932           "plPriority"    => 10,
933           "plSection"     => array("administration"),
934           "plCategory"    => array("groups"), 
935           "plProvidedAcls"=> array(
936             "mail"                      => _("Mail address"),
937             "gosaMailQuota"             => _("Quota size"),
938             "gosaMailServer"            => _("Mail server"),
939             "kolabFolderType"           => _("Folder type")." ("._("Kolab").")",
940             "gosaMailAlternateAddress"  => _("Alternate addresses"),
941             "gosaMailForwardingAddress" => _("Forwarding addresses"),
942             "gosaMailDeliveryModeI"     => _("Only local"),
943             "acl"                       => _("Permissions"))
944           ));
945   }
947   
948   /* Remove given ACL for given member (uid,mail) ..
949    */
950   function removeUserAcl($index )
951   {
952     if(isset($this->imapacl[$index])){
953       unset($this->imapacl[$index]);
954     }
955   }
957   function multiple_execute()
958   {
959     return($this->execute());
960   }
963   function init_multiple_support($attrs,$all)
964   {
965     plugin::init_multiple_support($attrs,$all);
967     $this->gosaMailForwardingAddress = array();
968     if(isset($attrs['gosaMailForwardingAddress'])){
969       for($i = 0 ; $i < $attrs['gosaMailForwardingAddress']['count'] ; $i++){
970         $this->gosaMailForwardingAddress[] = $attrs['gosaMailForwardingAddress'][$i];
971       }
972     }
974     $this->gosaMailForwardingAddress_Some = array();
975     if(isset($all['gosaMailForwardingAddress'])){
976       for($i = 0 ; $i < $all['gosaMailForwardingAddress']['count'] ; $i++){
977         if(!in_array($all['gosaMailForwardingAddress'][$i],$this->gosaMailForwardingAddress)){
978           $this->gosaMailForwardingAddress_Some[] = $all['gosaMailForwardingAddress'][$i];
979         }
980       }
981     }
982   }
984   function multiple_save_object()
985   {
986     if(isset($_POST['multiple_mail_group_posted'])){
987       plugin::multiple_save_object();
988       
989       foreach(array("kolabFolderType") as $attr){
990         if(isset($_POST['use_'.$attr])){
991           $this->multi_boxes[] = $attr;
992         }
993       }
995       /* Add special kolab attributes */
996       if(preg_match("/olab/i",$this->config->get_cfg_value("mailmethod"))){
997         if(isset($_POST['kolabFolderTypeType']) && $this->acl_is_writeable("kolabFolderType")){
998           $this->kolabFolderTypeType = get_post("kolabFolderTypeType");
999           $this->kolabFolderTypeSubType = get_post("kolabFolderTypeSubType");
1000         }
1001       }
1003       /* Collect data and re-assign it to the imapacl array */
1004       if ($this->acl_is_writeable("acl")){
1005         $this->imapacl= array();
1006         $this->imapacl['%members%']= $_POST['member_permissions'];
1007         $this->imapacl['anyone']= $_POST['default_permissions'];
1008         foreach ($this->indexed_user as $nr => $user){
1009           if (!isset($_POST["user_$nr"])){
1010             continue;
1011           }
1012           if ($_POST["user_$nr"] != $user ||
1013               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
1014             $this->is_modified= TRUE;
1015           }
1016           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
1017         }
1018       }
1019     }
1020   }
1021   
1022   
1023   /* Return selected values for multiple edit */
1024   function get_multi_edit_values()
1025   {
1026     $ret = plugin::get_multi_edit_values();
1027     $ret['Forward_some'] = $this->gosaMailForwardingAddress_Some;    
1028     $ret['Forward_all'] = $this->gosaMailForwardingAddress;    
1029     if(in_array('kolabFolderType',$this->multi_boxes)){
1030       $ret['kolabFolderTypeType'] = $this->kolabFolderTypeType;
1031       $ret['kolabFolderTypeSubType'] = $this->kolabFolderTypeSubType;
1032     }
1033     if(in_array("acl",$this->multi_boxes)){
1034       $ret['imapacl'] = $this->imapacl;
1035     }
1036     return($ret);
1037   }
1039   function set_multi_edit_values($attrs)
1040   {
1041     $forward = array();
1042     foreach($attrs['Forward_some'] as $addr){
1043       if(in_array($addr,$this->gosaMailForwardingAddress)){
1044         $forward[] = $addr;
1045       }
1046     }
1047     foreach($attrs['Forward_all'] as $addr){
1048       $forward[] = $addr;
1049     }
1050     plugin::set_multi_edit_values($attrs);
1051     $this->gosaMailForwardingAddress = $forward;
1052   }
1055   /*! \brief  Displays a dialog that allows mail address selection.
1056    */
1057   function display_forward_dialog()
1058   {
1059     restore_error_handler();
1061     $smarty = get_smarty();
1062     $ldap= $this->config->get_ldap_link();
1064     /* Save data */
1065     $mailfilter= session::get("mailfilter");
1066     foreach( array("depselect", "muser", "regex") as $type){
1067       if (isset($_POST[$type])){
1068         $mailfilter[$type]= $_POST[$type];
1069       }
1070     }
1071     if (isset($_GET['search'])){
1072       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
1073       if ($s == "**"){
1074         $s= "*";
1075       }
1076       $mailfilter['regex']= $s;
1077     }
1078     session::set("mailfilter", $mailfilter);
1080     /* Get actual list */
1081     $mailusers= array ();
1082     if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
1083       $regex= $mailfilter['regex'];
1084       $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
1085     } else {
1086       $filter= "";
1087     }
1088     if ($mailfilter['muser'] != ""){
1089       $user= $mailfilter['muser'];
1090       $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
1091     }
1093     /* Add already present people to the filter */
1094     $exclude= "";
1095     foreach ($this->gosaMailForwardingAddress as $mail){
1096       $exclude.= "(mail=$mail)";
1097     }
1098     if ($exclude != ""){
1099       $filter.= "(!(|$exclude))";
1100     }
1101     $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'],
1102         array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
1103     $ldap->cd($mailfilter['depselect']);
1104     $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
1105     while ($attrs= $ldap->fetch()){
1106       if(preg_match('/%/', $attrs['mail'][0])){
1107         continue;
1108       }
1109       $name= $this->make_name($attrs);
1110       $mailusers[$attrs['mail'][0]]= $name."&lt;".
1111         $attrs['mail'][0]."&gt;";
1112     }
1113     natcasesort ($mailusers);
1114     reset ($mailusers);
1116     /* Show dialog */
1117     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
1118     $smarty->assign("usearch_image", get_template_path('images/lists/search-user.png'));
1119     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
1120     $smarty->assign("infoimage", get_template_path('images/info.png'));
1121     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
1122     $smarty->assign("mailusers", $mailusers);
1123     if (isset($_POST['depselect'])){
1124       $smarty->assign("depselect", $_POST['depselect']);
1125     }
1126     $smarty->assign("deplist", $this->config->idepartments);
1127     $smarty->assign("apply", apply_filter());
1128     $smarty->assign("alphabet", generate_alphabet());
1129     $smarty->assign("hint", print_sizelimit_warning());
1130     foreach( array("depselect", "muser", "regex") as $type){
1131       $smarty->assign("$type", $mailfilter[$type]);
1132     }
1133     $smarty->assign("hint", print_sizelimit_warning());
1134     $display= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
1135     return ($display);
1136   }
1139   /*! \brief  Add given mail address to the list of forwarders.
1140    */
1141   function addForwarder($address)
1142   {
1143     if(empty($address)) continue;
1144     $this->gosaMailForwardingAddress[]= $address;
1145     $this->gosaMailForwardingAddress= array_unique($this->gosaMailForwardingAddress);
1147     /* Update multiple edit values too */
1148     if($this->multiple_support_active){
1149       $this->gosaMailForwardingAddress_Some= 
1150         array_remove_entries (array($address),$this->gosaMailForwardingAddress_Some);
1151     }
1153     sort ($this->gosaMailForwardingAddress);
1154     reset ($this->gosaMailForwardingAddress);
1155     $this->is_modified= TRUE;
1156   }
1159   /*! \brief  Removes the given mail address from the forwarders 
1160    */
1161   function delForwarder($addresses)
1162   {
1163     if(empty($address)) continue;
1164     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
1165         $this->gosaMailForwardingAddress);
1167     /* Update multiple edit values too */
1168     if($this->multiple_support_active){
1169       $this->gosaMailForwardingAddress_Some = array_remove_entries ($addresses,
1170           $this->gosaMailForwardingAddress_Some);
1171     }
1172     $this->is_modified= TRUE;
1173   }
1176   /*! \brief  Add given mail address to the list of alternate adresses ,
1177     .          check if this mal address is used, skip adding in this case
1178    */
1179   function addAlternate($address)
1180   {
1181     if(empty($address)) continue;
1182     $ldap= $this->config->get_ldap_link();
1184     $address= strtolower($address);
1186     /* Is this address already assigned in LDAP? */
1187     $ldap->cd ($this->config->current['BASE']);
1188     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
1189         "(gosaMailAlternateAddress=$address)))");
1191     if ($ldap->count() > 0){
1192       $attrs= $ldap->fetch ();
1193       return ($attrs["uid"][0]);
1194     }
1196     /* Add to list of alternates */
1197     if (!in_array($address, $this->gosaMailAlternateAddress)){
1198       $this->gosaMailAlternateAddress[]= $address;
1199     }
1201     sort ($this->gosaMailAlternateAddress);
1202     reset ($this->gosaMailAlternateAddress);
1203     $this->is_modified= TRUE;
1205     return ("");
1206   }
1209   /*! \brief  Removes the given mail address from the alternate addresses  
1210    */
1211   function delAlternate($addresses)
1212   {
1213     if(empty($address)) continue;
1214     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
1215         $this->gosaMailAlternateAddress);
1216     $this->is_modified= TRUE;
1217   }
1220   function postable_acls()
1221   {
1222     $ret = array();
1223     foreach($this->folder_acls as $name => $acl){
1224       $ret[$name] = array("name" => $name,"acl" => $acl,"post_name" => base64_encode($name));
1225     }
1226     return($ret);
1227   }
1229   
1230   function allow_remove()
1231   {
1232     $resason = "";
1233     if(!$this->mailMethod->allow_remove($reason)){
1234       return($reason);
1235     }
1236     return("");
1237   }
1240   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1241 ?>