Code

Corrected (some) of the cyrus ACLs
[gosa.git] / plugins / admin / groups / class_groupMail.inc
1 <?php
3 class mailgroup extends plugin
4 {
5   var $uid                        = "";       // User id 
6   var $cn                         = "";       // cn
7   var $orig_cn                    = "";       // cn
9   var $method                     = "mailMethod"; // Used Mail method 
10   var $mmethod                    = "";           // Contains the gosa.conf MAILMETHOD
11   var $mail                       = "";           // Default mail address 
13   var $gosaMailAlternateAddress   = array();  // Set default Alternate Mail Adresses to empty array
14   var $gosaMailForwardingAddress  = array();  // Forwarding also empty
16   var $gosaMailServer             = "";       // Selected mailserver 
17   var $gosaMailQuota              = "";       // Defined Quota 
18   var $quotaUsage                 = 0;        // Currently used quota
20   var $gosaVacationMessage        = "";       // Vocation message 
22   var $imapacl                    = array('anyone'    => 'p',     // Set acls for everyone
23       '%members%' => 'lrspw',  // %members% are all group-members
24       ''          => 'p');    // Every user added gets this right
27   var $gosaSpamSortLevel          = "";     
28   var $gosaSpamMailbox            = "";
29   var $gosaSharedFolderTarget     ;
31   var $forward_dialog             = FALSE;    
33   var $members                    = array();  // Group members
35   var $mailusers                  = array();
36   var $perms                      = array();
37   var $gosaMailDeliveryMode       = "[L        ]";   // 
38   var $gosaMailMaxSize            = "";       // 
39   
40   var $remove_folder_from_imap    = true;
42   /* Helper */
43   var $indexed_acl= array();
44   var $indexed_user= array();
46   /* Copy & paste */
47   var $CopyPasteVars          = array("quotaUsage","imapacl");
49   /* attribute list for save action */
50   var $attributes= array( "mail",   "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
51       "gosaMailAlternateAddress", "gosaMailForwardingAddress",
52       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox",
53       "acl","gosaSharedFolderTarget", "gosaVacationMessage");
55   var $objectclasses= array("gosaMailAccount");
58   function mailgroup ($config, $dn= NULL, $parent= NULL)
59   {
60     /* Initialise all available attributes ... if possible
61      */
62     plugin::plugin($config, $dn, $parent);
64     /* Save initial cn */
65     $this->orig_cn = $this->cn;
67     /* Set mailMethod to the one defined in gosa.conf 
68      */
69     if (isset($this->config->current['MAILMETHOD'])){
70       $this->mmethod= $this->config->current['MAILMETHOD'];
71     }
73     /* Check if selected mail method exists 
74      */
75     if (class_exists("mailMethod$this->mmethod")){
76       $this->method= "mailMethod$this->mmethod";
77     } else {
78       print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $this->mmethod));
79     }
81     /* Load Mailserver  
82      */
83     if(isset($this->attrs['gosaMailServer'][0])){
84       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
85     }
87     /* Convert cn to uid in case of existing entry
88      */
89     if (isset($this->attrs['cn'][0])){
90       $this->uid= $this->attrs['cn'][0];
91     }
94     /* If this ins't new mailgroup, read all required data from ldap
95      */
96     if (($dn != "new")&&($dn != NULL)){
98       /* Load attributes which represent multiple entries  
99        */
100       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
101         $this->$val = array();
102         if (isset($this->attrs["$val"]["count"])){
103           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
104             array_push($this->$val, $this->attrs["$val"][$i]);
105           }
106         }
107       }
109       /* Only do IMAP actions if gosaMailServer attribute is set 
110        */
111       if (isset ($this->attrs["gosaMailServer"][0])){
113         /* Create new instance of our defined mailclass
114          */
115         $method= new $this->method($this->config);
117         if ($method->connect($this->attrs["gosaMailServer"][0])){
118         
120           /* Maybe the entry is not saved in new style, get
121              permissions from IMAP and convert them to acl attributes */
122           if (!isset($this->attrs['acl'])){
123             $this->imapacl=  $method->getSharedFolderPermissions($this->uid);
125             /* Need to filter what a member acl could be... */
126             $vote= array();
127             $peak= 0;
128             $leader= "";
129             foreach ($this->imapacl as $user => $acl){
131               if ($user != "anyone" ){
132                 if (!isset($vote[$acl])){
133                   $vote[$acl]= 1;
134                 } else {
135                   $vote[$acl]++;
136                 }
137                 if ($vote[$acl] > $peak){
138                   $leader= $acl;
139                   $peek= $vote[$acl];
140                 }
141               }
143             }
145             /* Highest count wins as %members%, remove all members
146                with the same acl */
147             if(!empty($leader)){
148               $this->imapacl['%members%']= $leader;
149             }
150             foreach ($this->imapacl as $user => $acl){
151               if ($this->acl == $leader && in_array($user, $this->attrs['memberUid'])){
152                 unset($this->imapacl[$user]);
153               }
154             }
156           } // ENDE ! isset ($this->attrs['acl'])
157           
158           /* Adapt attributes if needed */
159           $method->fixAttributesOnLoad($this);
160           
161           /*  get Quota */
162           $quota= $method->getQuota($this->uid);
164           /* Update quota values */
165           if(is_array($quota)){
166             if ($quota['gosaMailQuota'] == 2147483647){
167               $this->quotaUsage= "";
168               $this->gosaMailQuota= "";
169             } else {
170               $this->quotaUsage= $quota['quotaUsage'];
171               $this->gosaMailQuota= $quota['gosaMailQuota'];
172             }
173           }else{
174             $this->quotaUsage     = "";
175             $this->gosaMailQuota  = "";
176 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->uid));
177           }
178           $method->disconnect();
179         }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
181       }   // ENDE gosaMailServer
183     }   // ENDE dn != "new"
186     /* Get global filter config */
187     if (!is_global("gmailfilter")){
188       $ui= get_userinfo();
189       $base= get_base_from_people($ui->dn);
190       $gmailfilter= array( "depselect"       => $base,
191           "muser"            => "",
192           "regex"           => "*");
193       register_global("gmailfilter", $gmailfilter);
194     }
196     /* Load permissions */
197     $tmp = array();
198     if(preg_match("/olab/i",$this->mmethod)){
199       $ldap = $this->config->get_ldap_link();
201       if (isset($this->attrs['acl'])){
203         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
204           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
206           /* Add to list */
207           $this->imapacl[$user]= $permission;
209           /* Get all user permissions sorted by acl, to detect the most used acl
210              This acl is then used for %members%
211            */
212           if ($user != "anyone" && $user != "%members%"){
213             $tmp[$permission][] = $user;
214           }
216           /* There is an entry in $this->imapacl like this this ... 
217              $this->attrs['imapacl']['anyone'] = "p";
218              $this->attrs['imapacl']['%members%'] = "lprs";
219              $this->attrs['imapacl'][''] = ""; <------ This is used to diplay an empty 
220              Field for special acls in our template.
221              If there is at least one special acl in out imapacl,
222              we don't need this entry anymore, because it is already displayed. 
223            */
224           if ($user != "anyone" && $user != "%members%"){
225             unset($this->imapacl['']);
226           }
227         }
228       }
230       /* In this section we dectect which acl is tho most used 
231          This will be used as %members% acl  
232        */
233       $tmp2 = array(); 
234       foreach($tmp as $acl => $user){
235         $tmp2[count($tmp[$acl])]=$acl;
236       }
237       /* Most used at last 
238        */
239       ksort($tmp2);      
240   
241       /* Assign last (most used acl) to %members% acl 
242        */
243       $str = array_pop($tmp2);
244       if(!empty($str)) {
245         $this->imapacl['%members%']=$str;
246       }
248       /* Open ldap connection 
249        */
250       $ldap = $this->config->get_ldap_link();
251       $ldap->cd($this->config->current['BASE']);
253       /* Remove those users, that use %members% acl && are member of this group. */
254       foreach($this->imapacl as $mail => $permission){
255         $ldap->search("(&(objectClass=person)(mail=".$mail."))",array("uid"));
256         $atr = $ldap->fetch();
257         if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
258           if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
259             unset($this->imapacl[$mail]);
260           }
261         }
262       }
263       /* Append an empty entry, for special acl handling */
264       if(count($this->imapacl)==2){
265         $this->imapacl[''] ="";
266       }
267   
268     }else{ // Not kolab 
269       /* Load permissions */ 
270       if (isset($this->attrs['acl'])){
271         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
272           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
273           $this->imapacl[$user]= $permission;
274           if ($user != "anyone" && $user != "%members%"){
275             unset($this->imapacl['']);
276           }
277         }
278       }
279     }
281     /* Load Mailserver  
282      */
283     if(isset($this->attrs['gosaMailServer'][0])){
284       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
285     }
286     /* Fill translations */
287     $this->perms["lrsw"]= _("read");
288     $this->perms["lrswp"]= _("post");
289     $this->perms["p"]= _("external post");
290     $this->perms["lrswip"]= _("append");
291     $this->perms["lrswipcd"]= _("write");
292     $this->perms["lrswipcda"]= _("admin");
293     $this->perms[""]= _("none");
294   }
296   function execute()
297   {
298     /* Call parent execute */
299     //plugin::execute();
301     /* Load templating engine */
302     $smarty= get_smarty();
303     $display = "";
304     if ($_SESSION['js']==FALSE){
305       $smarty->assign("javascript", "false");
306     } else {
307       $smarty->assign("javascript", "true");
308     }
310     /* Do we need to flip is_account state? */
311     if (isset($_POST['modify_state'])){
313       /* Onyl change account state if allowed */
314       if($this->is_account && $this->acl == "#all#"){
315         $this->is_account= !$this->is_account;
316       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
317         $this->is_account= !$this->is_account;
318       }
319     }
320     
321     /* Do we represent a valid account? */
322     if (!$this->is_account && $this->parent == NULL){
324       $display.= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
325         _("This 'dn' has no valid mail extensions.")."</b>";
326       return ($display);
327     }
329     /* Show tab dialog headers */
330     $display= "";
331     if ($this->parent != NULL){
332       if ($this->is_account){
333         $display= $this->show_header(_("Remove mail account"),
334             _("This account has mail features enabled. You can disable them by clicking below."));
335       } else {
336         $display.= $this->show_header(_("Create mail account"),
337             _("This account has mail features disabled. You can enable them by clicking below."));
339         /* Show checkbox that allows us to remove imap entry too*/
340         if($this->initially_was_account){
341         
342           $c = "";
343           if($this->remove_folder_from_imap){
344             $c= " checked ";
345           }
346   
347           $display .= "<h2>Shared folder delete options</h2>
348                        <input class='center' type='checkbox' name='remove_folder_from_imap' value='1' ".$c."  
349                           title='"._("Remove shared folder from mail server database when entry gets removed in LDAP")."'>";
350           $display .= _("Remove the shared folder and all its contents after saving this account"); 
351         }
353         return ($display);
354       }
355     }
357     /* Add ACL? */
358     foreach ($this->indexed_user as $nr => $user){
359       if (isset($_POST["add_$nr"])){
360         $this->imapacl[""]= "l";
361       }
362       if (isset($_POST["del_$nr"])){
363         unset ($this->imapacl[$user]);
364       }
365     }
367     /* Trigger forward add dialog? */
368     if (isset($_POST['add_local_forwarder'])){
369       $this->forward_dialog= TRUE;
370       $this->dialog= TRUE;
371     }
373     /* Cancel forward add dialog? */
374     if (isset($_POST['add_locals_cancel'])){
375       $this->forward_dialog= FALSE;
376       $this->dialog= FALSE;
377     }
379     /* Finished adding of locals? */
380     if (isset($_POST['add_locals_finish'])){
381       if (count ($_POST['local_list']) &&
382           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
384         /* Walk through list of forwarders, ignore own addresses */
385         foreach ($_POST['local_list'] as $val){
386           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
387               $val != $this->mail){
389             $this->addForwarder($val);
390           }
391         }
392       }
393       $this->forward_dialog= FALSE;
394       $this->dialog= FALSE;
395     }
397     /* Add forward email addresses */
398     if (isset($_POST['add_forwarder'])){
399       if ($_POST['forward_address'] != ""){
401         /* Valid email address specified? */
402         $address= $_POST['forward_address'];
403         if (!is_email($address)){
405           print_red (_("You're trying to add an invalid email address ".
406                 "to the list of forwarders."));
408         } elseif ($address == $this->mail
409             || in_array($address, $this->gosaMailAlternateAddress)) {
411           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
413         } else {
415           /* Add it */
416           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
417             $this->addForwarder ($address);
418           }
420         }
421       }
422     }
424     /* Delete forward email addresses */
425     if (isset($_POST['delete_forwarder'])){
426       if (count($_POST['forwarder_list'])
427           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
429         $this->delForwarder ($_POST['forwarder_list']);
430       }
431     }
433     /* Add alternate email addresses */
434     if (isset($_POST['add_alternate'])){
435       if ($_POST['alternate_address'] != "" &&
436           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
438         if (!is_email($_POST['alternate_address'])){
439           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
441         } elseif (($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
442           $ui= get_userinfo();
443           if ($user != $ui->username){
444             print_red (_("The address you're trying to add is already used by user")." '$user'.");
445           }
446         }
447       }
448     }
450     /* Delete alternate email addresses */
451     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
452       if (count($_POST['alternates_list']) &&
453           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
455         $this->delAlternate ($_POST['alternates_list']);
456       }
457     }
459     /* Show forward add dialog */
460     if ($this->forward_dialog){
461       $ldap= $this->config->get_ldap_link();
463       /* Save data */
464       $gmailfilter= get_global("gmailfilter");
465       foreach( array("depselect", "muser", "regex") as $type){
466         if (isset($_POST[$type])){
467           $gmailfilter[$type]= $_POST[$type];
468         }
469       }
470       if (isset($_GET['search'])){
471         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
472         if ($s == "**"){
473           $s= "*";
474         }
475         $gmailfilter['regex']= $s;
476       }
477       register_global("gmailfilter", $gmailfilter);
479       /* Get actual list */
480       $mailusers= array ();
481       if ($gmailfilter['regex'] != '*' && $gmailfilter['regex'] != ""){
482         $regex= $gmailfilter['regex'];
483         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
484       } else {
485         $filter= "";
486       }
487       if ($gmailfilter['muser'] != ""){
488         $user= $gmailfilter['muser'];
489         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
490       }
492       /* Add already present people to the filter */
493       $exclude= "";
494       foreach ($this->gosaMailForwardingAddress as $mail){
495         $exclude.= "(mail=$mail)";
496       }
497       if ($exclude != ""){
498         $filter.= "(!(|$exclude))";
499       }
501       $acl= array($this->config->current['BASE'] => ":all");
502       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $gmailfilter['depselect'],
503                      array("sn", "mail", "givenName"), GL_SUBSEARCH | GL_SIZELIMIT);
504       $ldap->cd($gmailfilter['depselect']);
505       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
506       error_reporting (0);
507       while ($attrs= $ldap->fetch()){
508         if(preg_match('/%/', $attrs['mail'][0])){
509           continue;
510         }
511         $name= $this->make_name($attrs);
512         $mailusers[$attrs['mail'][0]]= $name."&lt;".
513           $attrs['mail'][0]."&gt;";
514       }
515       error_reporting (E_ALL);
516       natcasesort ($mailusers);
517       reset ($mailusers);
519       /* Show dialog */
520       $smarty->assign("search_image", get_template_path('images/search.png'));
521       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
522       $smarty->assign("tree_image", get_template_path('images/tree.png'));
523       $smarty->assign("infoimage", get_template_path('images/info.png'));
524       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
525       $smarty->assign("mailusers", $mailusers);
526       $smarty->assign("deplist", $this->config->idepartments);
527       $smarty->assign("apply", apply_filter());
528       $smarty->assign("alphabet", generate_alphabet());
529       $smarty->assign("hint", print_sizelimit_warning());
530       foreach( array("depselect", "muser", "regex") as $type){
531         $smarty->assign("$type", $gmailfilter[$type]);
532       }
533       $smarty->assign("hint", print_sizelimit_warning());
534       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE));
535       return ($display);
536     }
538     /* Assemble normal permissions */
539     $smarty->assign("permissionsACL", chkacl($this->acl, "permissions"));
540     if (isset($this->imapacl['anyone'])){
541       $smarty->assign("default_permissions", $this->imapacl['anyone']);
542     }
543     $smarty->assign("member_permissions", "lrsp");
544     if (isset($this->imapacl['%members%'])){
545       $smarty->assign("member_permissions", $this->imapacl['%members%']);
546     }
548     /* Assemble extra attributes */
549     $perm= chkacl($this->acl, "permissions");
550     $tmp= "";
551     $nr= 0;
552     $count= count($this->imapacl);
553     $this->indexed_user= array();
554     $this->indexed_acl= array();
555     foreach($this->imapacl as $user => $acl){
556       if ($user != "anyone" && $user != "%members%"){
557         $tmp.= "<tr><td><input name=\"user_$nr\" size=20 maxlength=60 ".
558           "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
559         foreach ($this->perms as $key => $value){
560           if ($acl == $key){
561             $tmp.= "<option value=\"$key\" selected>$value</option>";
562           } else {
563             $tmp.= "<option value=\"$key\">$value</option>";
564           }
565         }
566         $tmp.= "</select>&nbsp;";
567         if ($nr == $count - 1){
568           $tmp.= "<input type=submit value=\""._("Add")."\" ".
569             "name=\"add_$nr\" $perm>";
570         }
571         if ($count > 3){
572           $tmp.= "<input type=submit value=\""._("Remove")."\" ".
573             "name=\"del_$nr\" $perm></td></tr>";
574         }
575       }
576       $this->indexed_user[$nr]= $user;
577       $this->indexed_acl[$nr++]= $acl;
578     }
579     $smarty->assign("plusattributes", $tmp);
581     /* Show main page */
582     $mailserver= array();
583     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
584       $mailserver[]= $key;
585     }
586     $smarty->assign("mailServers", $mailserver);
587     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
588           "gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
589       $smarty->assign("$val", $this->$val);
590       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
591     }
592     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
593       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
594       $smarty->assign("quotadefined", "true");
595     } else {
596       $smarty->assign("quotadefined", "false");
597     }
599     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
600     return ($display);
601   }
604   /* remove object from parent */
605   function remove_from_parent()
606   {
607     /* Added these ObjectClass and Attributes, because they were not 
608        removed correctly, only in case of kolab ... 
609      */
610     if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
611       $this->attributes[]="acl";
612       $this->objectclasses[] = "kolabSharedFolder";
613     }
614     /* include global link_info */
615     $ldap= $this->config->get_ldap_link();
617     /* Remove and write to LDAP */
618     plugin::remove_from_parent();
620     /* Zero arrays */
621     $this->attrs['gosaMailAlternateAddress']= array();
622     $this->attrs['gosaMailForwardingAddress']= array();
623     $this->attrs['gosaSharedFolderTarget']= array();
625     /* Connect to IMAP server for account deletion */
626     if ($this->initially_was_account){
627  
628       $method= new $this->method($this->config);
629       $method->fixAttributesOnRemove($this);
630       if ($method->connect($this->gosaMailServer) && $this->remove_folder_from_imap){
632         /* Remove account from IMAP server */
633         $method->deleteMailbox($this->uid);
634         $method->disconnect();
635       }
636     }
637     /* Keep uid */
638     unset ($this->attrs['uid']);
640     $ldap->cd($this->dn);
641     $ldap->modify ($this->attrs); 
642     show_ldap_error($ldap->get_error(), _("Removing group mail settings failed"));
644     /* Optionally execute a command after we're done */
645     $this->handle_post_events("remove");
646   }
649   /* Save data to object */
650   function save_object()
651   {
653     /* Check if user wants to remove the shared folder from imap too */
654     if($this->initially_was_account && !$this->is_account){
655       if(isset($_POST['remove_folder_from_imap'])){
656         $this->remove_folder_from_imap = true;
657       }else{
658         $this->remove_folder_from_imap = false;
659       }
660     }
662     /* Assemble mail delivery mode
663        The mode field in ldap consists of values between braces, this must
664        be called when 'mail' is set, because checkboxes may not be set when
665        we're in some other dialog.
667        Example for gosaMailDeliveryMode [LR        ]
668 L: Local delivery
669 R: Reject when exceeding mailsize limit
670 S: Use spam filter
671 V: Use vacation message
672 C: Use custom sieve script
673 I: Only insider delivery */
674     if (isset($_POST['mailedit'])){
676       /* Save ldap attributes */
677       plugin::save_object();
679       $tmp= "";
680       if (!isset($_POST["drop_own_mails"])){
681         $tmp.= "L";
682       }
683       if (isset($_POST["use_mailsize_limit"])){
684         $tmp.= "R";
685       }
686       if (isset($_POST["use_spam_filter"])){
687         $tmp.= "S";
688       }
689       if (isset($_POST["use_vacation"])){
690         $tmp.= "V";
691       }
692       if (isset($_POST["own_script"])){
693         $tmp.= "C";
694       }
695       if (isset($_POST["only_local"])){
696         $tmp.= "I";
697       }
698       $tmp= "[$tmp]";
700       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
701         $this->gosaMailDeliveryMode= $tmp;
702       }
704       /* Collect data and re-assign it to the imapacl array */
705       if (chkacl($this->acl, "permissions") == ""){
706         $this->imapacl= array();
707         $this->imapacl['%members%']= $_POST['member_permissions'];
708         $this->imapacl['anyone']= $_POST['default_permissions'];
709         foreach ($this->indexed_user as $nr => $user){
710           if (!isset($_POST["user_$nr"])){
711             continue;
712           }
713           if ($_POST["user_$nr"] != $user ||
714               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
715             $this->is_modified= TRUE;
716           }
717           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
718         }
719       }
720     }
722   }
726   /* Save data to LDAP, depending on is_account we save or delete */
727   function save()
728   {
729     $ldap= $this->config->get_ldap_link();
730     $ldap->cd($this->config->current['BASE']);
732     /* Call parents save to prepare $this->attrs */
733     plugin::save();
735     /* Save arrays */
736     $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
737     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
738     $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
740     if(preg_match("/olab/i",$this->mmethod)){
741       /* Save acl's */
742       $this->attrs['acl']= array();
743       foreach ($this->imapacl as $user => $acl){
744         if ($user == ""){
745           continue;
746         }
747         $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
748         $mail = $ldap->fetch();
749         if($mail){
750           if(isset($mail['mail'][0])){
751             $this->attrs['acl'][]= $mail['mail'][0]." $acl";
752           }
753         }else{
754           $this->attrs['acl'][]= "$user $acl";
755         }
756       }
757     }else{
758       /* Save acl's */
759       $this->attrs['acl']= array();
760       foreach ($this->imapacl as $user => $acl){
761         if ($user == ""){
762           continue;
763         }
764         $this->attrs['acl'][]= "$user $acl";
765       }
766     }
768     /* Only do IMAP actions if we are not a template */
769     if(preg_match("/olab/i",$this->mmethod)){
770       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
771         if(isset($this->attrs['gosaMailServer'][0])){
772           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
773         }
774       }
775     }  
778     /* Exchange '%member%' pseudo entry */
779     $memberacl= $this->imapacl['%members%'];
781     foreach ($this->members as $user){
782       if(preg_match("/olab/i",$this->mmethod)){
783         $ldap->cd($this->config->current['BASE']);
784         $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
785         $at = $ldap->fetch();
786         if(isset($at['mail'][0])){
787           $user = $at['mail'][0];
788         }
789       }
790       if (!isset($this->imapacl[$user])){
791         $this->imapacl[$user]= $memberacl;
792       }
793     }
794     $this->attrs['acl'] = array();
795     
796     
797     foreach($this->imapacl as $user => $acl){
799       /* Remove empty user entry, to avoid entry like this im imap 
800        *   "" lrs
801        */
802       if(empty($user)){
803         unset($this->imapacl[$user]);
804       }
805    
806       /* Skip invalid values */
807       if(preg_match("/%members%/",$user) || empty($user)){
808         continue;
809       }
811       /* Append ldap acl entries */
812       $this->attrs['acl'][] = $user." ".$acl;
813     }
815     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
816       $method= new $this->method($this->config);
817       $method->fixAttributesOnStore($this);
818       if (($method->connect($this->gosaMailServer))){
819         $method->updateMailbox($this->uid);
820         $method->setQuota($this->uid, $this->gosaMailQuota);
821         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
822         $method->disconnect();
823       }
824     }
826     /* Save data to LDAP */
827     $ldap->cd($this->dn);
828     $this->cleanup();
829     $ldap->modify ($this->attrs); 
830     show_ldap_error($ldap->get_error(), _("Saving group mail settings failed"));
832     /* Optionally execute a command after we're done */
833     if ($this->initially_was_account == $this->is_account){
834       if ($this->is_modified){
835         $this->handle_post_events("modify");
836       }
837     } else {
838       $this->handle_post_events("add");
839     }
840   }
842   /* Check formular input */
843   function check()
844   {
845     $ldap= $this->config->get_ldap_link();
847     /* Call common method to give check the hook */
848     $message= plugin::check();
850     if(!$this->is_account) return array();
851     
852     //$message[] = $str;      
854     /* must: mail */
855     if ($this->mail == ""){
856       $message[]= _("The required field 'Primary address' is not set.");
857     }
858     if (!is_email($this->mail)){
859       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
860     }
861     $ldap->cd($this->config->current['BASE']);
862     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
863         $this->mail."))(!(uid=".$this->orig_cn."))(!(cn=".$this->orig_cn.")))");
864     if ($ldap->count() != 0){
865       $message[]= _("The primary address you've entered is already in use.");
866     }
867   
868     /* Check quota */
869     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
870       if (!is_numeric($this->gosaMailQuota)) {
871         $message[]= _("Value in 'Quota size' is not valid.");
872       } else {
873         $this->gosaMailQuota= (int) $this->gosaMailQuota;
874       }
875     }
877     /* Check rejectsize for integer */
878     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
879       if (!is_numeric($this->gosaMailMaxSize)){
880         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
881       } else {
882         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
883       }
884     }
886     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
887     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
888       $message[]= _("You need to set the maximum mail size in order to reject anything.");
889     }
891     if(ord($this->imapacl['anyone'][0])==194){
892       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
893     }
895     if(empty($this->gosaMailServer)){
896       $message[] = _("Please select a valid mail server.");
897     }
899     return ($message);
900   }
902   /* Adapt from template, using 'dn' */
903   function adapt_from_template($dn)
904   {
905     plugin::adapt_from_template($dn);
907     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
908       $this->$val= array();
909       if (isset($this->attrs["$val"]["count"])){
910         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
911           $value= $this->attrs["$val"][$i];
912           foreach (array("sn", "givenName", "uid") as $repl){
913             if (preg_match("/%$repl/i", $value)){
914               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
915             }
916           }
917           array_push($this->$val, $value);
918         }
919       }
920     }
921   }
923   /* Add entry to forwarder list */
924   function addForwarder($address)
925   {
926     $this->gosaMailForwardingAddress[]= $address;
927     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
929     sort ($this->gosaMailForwardingAddress);
930     reset ($this->gosaMailForwardingAddress);
931     $this->is_modified= TRUE;
932   }
934   /* Remove list of addresses from forwarder list */
935   function delForwarder($addresses)
936   {
937     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
938         $this->gosaMailForwardingAddress);
939     $this->is_modified= TRUE;
940   }
944   function addAlternate($address)
945   {
946     $ldap= $this->config->get_ldap_link();
948     $address= strtolower($address);
950     /* Is this address already assigned in LDAP? */
951     $ldap->cd ($this->config->current['BASE']);
952     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
953         "(gosaMailAlternateAddress=$address)))");
955     if ($ldap->count() > 0){
956       $attrs= $ldap->fetch ();
957       return ($attrs["uid"][0]);
958     }
960     /* Add to list of alternates */
961     if (!in_array($address, $this->gosaMailAlternateAddress)){
962       $this->gosaMailAlternateAddress[]= $address;
963     }
965     sort ($this->gosaMailAlternateAddress);
966     reset ($this->gosaMailAlternateAddress);
967     $this->is_modified= TRUE;
969     return ("");
970   }
973   function delAlternate($addresses)
974   {
975     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
976         $this->gosaMailAlternateAddress);
977     $this->is_modified= TRUE;
978   }
981   function make_name($attrs)
982   {
983     $name= "";
984     if (isset($attrs['sn'][0])){
985       $name= $attrs['sn'][0];
986     }
987     if (isset($attrs['givenName'][0])){
988       if ($name != ""){
989         $name.= ", ".$attrs['givenName'][0];
990       } else {
991         $name.= $attrs['givenName'][0];
992       }
993     }
994     if ($name != ""){
995       $name.= " ";
996     }
998     return ($name);
999   }
1001   function getCopyDialog()
1002   {
1003     if(!$this->is_account) return("");
1005     $smarty = get_smarty();
1006     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1007     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1008     $smarty->assign("mail",$this->mail);
1009     $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE));
1010     $ret = array();
1011     $ret['string'] = $display;
1012     $ret['status'] = "";
1013     return($ret);
1014   }
1016   function saveCopyDialog()
1017   {
1018     if(!$this->is_account) return;
1020     /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
1021     */
1022     $this->execute();
1023     if(isset($_POST['mail'])){
1024       $this->mail = $_POST['mail'];
1025     }
1026   }
1028   function PrepareForCopyPaste($source)
1029   {
1030     plugin::PrepareForCopyPaste($source);
1032     /* Reset alternate mail addresses */
1033     $this->gosaMailAlternateAddress = array();
1034   }
1036 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1037 ?>