Code

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