Code

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