Code

Some group-application fixes
[gosa.git] / plugins / admin / groups / class_groupMail.inc
1 <?php
3 class mailgroup extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary        = "Manage mail groups/shared folders";
7   var $cli_description    = "Some longer text\nfor help";
8   var $cli_parameters     = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   var $uid                        = "";       // User id 
11   var $cn                         = "";       // cn
13   var $method                     = "mailMethod"; // Used Mail method 
14   var $mmethod                    = "";           // Contains the gosa.conf MAILMETHOD
15   var $mail                       = "";           // Default mail address 
17   var $gosaMailAlternateAddress   = array();  // Set default Alternate Mail Adresses to empty array
18   var $gosaMailForwardingAddress  = array();  // Forwarding also empty
20   var $gosaMailServer             = "";       // Selected mailserver 
21   var $gosaMailQuota              = "";       // Defined Quota 
22   var $quotaUsage                 = 0;        // Currently used quota
24   var $gosaVacationMessage        = "";       // Vocation message 
26   var $imapacl                    = array('anyone'    => 'p',     // Set acls for everyone
27       '%members%' => 'lrsp',  // %members% are all group-members
28       ''          => 'p');    // Every user added gets this right
31   var $gosaSpamSortLevel          = "";     
32   var $gosaSpamMailbox            = "";
33   var $gosaSharedFolderTarget     ;
35   var $forward_dialog             = FALSE;    
37   var $members                    = array();  // Group members
39   var $mailusers                  = array();
40   var $perms                      = array();
41   var $gosaMailDeliveryMode       = "[L        ]";   // 
42   var $gosaMailMaxSize            = "";       // 
45   /* Helper */
46   var $indexed_acl= array();
47   var $indexed_user= array();
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, $ui= NULL)
59   {
60     /* Initialise all available attributes ... if possible
61      */
62     plugin::plugin($config, $dn);
64     /* Set mailMethod to the one defined in gosa.conf 
65      */
66     if (isset($this->config->current['MAILMETHOD'])){
67       $this->mmethod= $this->config->current['MAILMETHOD'];
68     }
70     /* Check if selected mail method exists 
71      */
72     if (class_exists("mailMethod$this->mmethod")){
73       $this->method= "mailMethod$this->mmethod";
74     } else {
75       print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $this->mmethod));
76     }
78     /* Load Mailserver  
79      */
80     if(isset($this->attrs['gosaMailServer'][0])){
81       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
82     }
84     /* Convert cn to uid in case of existing entry
85      */
86     if (isset($this->attrs['cn'][0])){
87       $this->uid= $this->attrs['cn'][0];
88     }
91     /* If this ins't new mailgroup, read all required data from ldap
92      */
93     if (($dn != "new")&&($dn != NULL)){
95       /* Load attributes which represent multiple entries  
96        */
97       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
98         $this->$val = array();
99         if (isset($this->attrs["$val"]["count"])){
100           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
101             array_push($this->$val, $this->attrs["$val"][$i]);
102           }
103         }
104       }
106       /* Only do IMAP actions if gosaMailServer attribute is set 
107        */
108       if (isset ($this->attrs["gosaMailServer"][0])){
110         /* Create new instance of our defined mailclass
111          */
112         $method= new $this->method($this->config);
114         if ($method->connect($this->attrs["gosaMailServer"][0])){
115         
117           /* Maybe the entry is not saved in new style, get
118              permissions from IMAP and convert them to acl attributes */
119           if (!isset($this->attrs['acl'])){
120             $this->imapacl=  $method->getSharedFolderPermissions($this->uid);
122             /* Need to filter what a member acl could be... */
123             $vote= array();
124             $peak= 0;
125             $leader= "";
126             foreach ($this->imapacl as $user => $acl){
128               if ($user != "anyone" ){
129                 if (!isset($vote[$acl])){
130                   $vote[$acl]= 1;
131                 } else {
132                   $vote[$acl]++;
133                 }
134                 if ($vote[$acl] > $peak){
135                   $leader= $acl;
136                   $peek= $vote[$acl];
137                 }
138               }
140             }
142             /* Highest count wins as %members%, remove all members
143                with the same acl */
144             if(!empty($leader)){
145               $this->imapacl['%members%']= $leader;
146             }
147             foreach ($this->imapacl as $user => $acl){
148               if ($this->acl == $leader && in_array($user, $this->attrs['memberUid'])){
149                 unset($this->imapacl[$user]);
150               }
151             }
153           } // ENDE ! isset ($this->attrs['acl'])
154           
155           /* Adapt attributes if needed */
156           $method->fixAttributesOnLoad($this);
157           
158           /*  get Quota */
159           $quota= $method->getQuota($this->uid);
161           /* Update quota values */
162           if(is_array($quota)){
163             if ($quota['gosaMailQuota'] == 2147483647){
164               $this->quotaUsage= "";
165               $this->gosaMailQuota= "";
166             } else {
167               $this->quotaUsage= $quota['quotaUsage'];
168               $this->gosaMailQuota= $quota['gosaMailQuota'];
169             }
170           }else{
171             $this->quotaUsage     = "";
172             $this->gosaMailQuota  = "";
173 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->uid));
174           }
175           $method->disconnect();
176         }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
178       }   // ENDE gosaMailServer
180     }   // ENDE dn != "new"
183     /* Get global filter config */
184     if (!is_global("gmailfilter")){
185       $ui= get_userinfo();
186       $base= get_base_from_people($ui->dn);
187       $gmailfilter= array( "depselect"       => $base,
188           "muser"            => "",
189           "regex"           => "*");
190       register_global("gmailfilter", $gmailfilter);
191     }
193     /* Load permissions */
194     $tmp = array();
195     if(preg_match("/kolab/i",$this->mmethod)){
196       $ldap = $this->config->get_ldap_link();
198       if (isset($this->attrs['acl'])){
200         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
201           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
203           /* Add to list */
204           $this->imapacl[$user]= $permission;
206           /* Get all user permissions sorted by acl, to detect the most used acl
207              This acl is then used for %members%
208            */
209           if ($user != "anyone" && $user != "%members%"){
210             $tmp[$permission][] = $user;
211           }
213           /* There is an entry in $this->imapacl like this this ... 
214              $this->attrs['imapacl']['anyone'] = "p";
215              $this->attrs['imapacl']['%members%'] = "lprs";
216              $this->attrs['imapacl'][''] = ""; <------ This is used to diplay an empty 
217              Field for special acls in our template.
218              If there is at least one special acl in out imapacl,
219              we don't need this entry anymore, because it is already displayed. 
220            */
221           if ($user != "anyone" && $user != "%members%"){
222             unset($this->imapacl['']);
223           }
224         }
225       }
227       /* In this section we dectect which acl is tho most used 
228          This will be used as %members% acl  
229        */
230       $tmp2 = array(); 
231       foreach($tmp as $acl => $user){
232         $tmp2[count($tmp[$acl])]=$acl;
233       }
234       /* Most used at last 
235        */
236       ksort($tmp2);      
237   
238       /* Assign last (most used acl) to %members% acl 
239        */
240       $str = array_pop($tmp2);
241       if(!empty($str)) {
242         $this->imapacl['%members%']=$str;
243       }
245       /* Open ldap connection 
246        */
247       $ldap = $this->config->get_ldap_link();
248       $ldap->cd($this->config->current['BASE']);
250       /* Remove those users, that use %members% acl && are member of this group. */
251       foreach($this->imapacl as $mail => $permission){
252         $ldap->search("(&(objectClass=person)(mail=".$mail."))",array("uid"));
253         $atr = $ldap->fetch();
254         if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
255           if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
256             unset($this->imapacl[$mail]);
257           }
258         }
259       }
260       /* Append an empty entry, for special acl handling */
261       if(count($this->imapacl)==2){
262         $this->imapacl[''] ="";
263       }
264   
265     }else{ // Not kolab 
266       /* Load permissions */ 
267       if (isset($this->attrs['acl'])){
268         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
269           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
270           $this->imapacl[$user]= $permission;
271           if ($user != "anyone" && $user != "%members%"){
272             unset($this->imapacl['']);
273           }
274         }
275       }
276     }
278     /* Load Mailserver  
279      */
280     if(isset($this->attrs['gosaMailServer'][0])){
281       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
282     }
283     /* Fill translations */
284     $this->perms["lrs"]= _("read");
285     $this->perms["lrsp"]= _("post");
286     $this->perms["p"]= _("external post");
287     $this->perms["lrsip"]= _("append");
288     $this->perms["lrswipcd"]= _("write");
289   }
291   function execute()
292   {
293     /* Call parent execute */
294     //plugin::execute();
296     /* Load templating engine */
297     $smarty= get_smarty();
298     if ($_SESSION['js']==FALSE){
299       $smarty->assign("javascript", "false");
300     } else {
301       $smarty->assign("javascript", "true");
302     }
304     /* Do we need to flip is_account state? */
305     if (isset($_POST['modify_state'])){
306       $this->is_account= !$this->is_account;
307     }
309     /* Do we represent a valid account? */
310     if (!$this->is_account && $this->parent == NULL){
311       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
312         _("This 'dn' has no valid mail extensions.")."</b>";
313       return ($display);
314     }
316     /* Show tab dialog headers */
317     $display= "";
318     if ($this->parent != NULL){
319       if ($this->is_account){
320         $display= $this->show_disable_header(_("Remove mail account"),
321             _("This account has mail features enabled. You can disable them by clicking below."));
322       } else {
323         $display= $this->show_enable_header(_("Create mail account"),
324             _("This account has mail features disabled. You can enable them by clicking below."));
325         return ($display);
326       }
327     }
329     /* Add ACL? */
330     foreach ($this->indexed_user as $nr => $user){
331       if (isset($_POST["add_$nr"])){
332         $this->imapacl[""]= "l";
333       }
334       if (isset($_POST["del_$nr"])){
335         unset ($this->imapacl[$user]);
336       }
337     }
339     /* Trigger forward add dialog? */
340     if (isset($_POST['add_local_forwarder'])){
341       $this->forward_dialog= TRUE;
342       $this->dialog= TRUE;
343     }
345     /* Cancel forward add dialog? */
346     if (isset($_POST['add_locals_cancel'])){
347       $this->forward_dialog= FALSE;
348       $this->dialog= FALSE;
349     }
351     /* Finished adding of locals? */
352     if (isset($_POST['add_locals_finish'])){
353       if (count ($_POST['local_list']) &&
354           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
356         /* Walk through list of forwarders, ignore own addresses */
357         foreach ($_POST['local_list'] as $val){
358           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
359               $val != $this->mail){
361             $this->addForwarder($val);
362           }
363         }
364       }
365       $this->forward_dialog= FALSE;
366       $this->dialog= FALSE;
367     }
369     /* Add forward email addresses */
370     if (isset($_POST['add_forwarder'])){
371       if ($_POST['forward_address'] != ""){
373         /* Valid email address specified? */
374         $address= $_POST['forward_address'];
375         if (!is_email($address)){
377           print_red (_("You're trying to add an invalid email address ".
378                 "to the list of forwarders."));
380         } elseif ($address == $this->mail
381             || in_array($address, $this->gosaMailAlternateAddress)) {
383           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
385         } else {
387           /* Add it */
388           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
389             $this->addForwarder ($address);
390           }
392         }
393       }
394     }
396     /* Delete forward email addresses */
397     if (isset($_POST['delete_forwarder'])){
398       if (count($_POST['forwarder_list'])
399           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
401         $this->delForwarder ($_POST['forwarder_list']);
402       }
403     }
405     /* Add alternate email addresses */
406     if (isset($_POST['add_alternate'])){
407       if ($_POST['alternate_address'] != "" &&
408           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
410         if (!is_email($_POST['alternate_address'])){
411           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
413         } elseif (($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
414           $ui= get_userinfo();
415           if ($user != $ui->username){
416             print_red (_("The address you're trying to add is already used by user")." '$user'.");
417           }
418         }
419       }
420     }
422     /* Delete alternate email addresses */
423     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
424       if (count($_POST['alternates_list']) &&
425           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
427         $this->delAlternate ($_POST['alternates_list']);
428       }
429     }
431     /* Show forward add dialog */
432     if ($this->forward_dialog){
433       $ldap= $this->config->get_ldap_link();
435       /* Save data */
436       $gmailfilter= get_global("gmailfilter");
437       foreach( array("depselect", "muser", "regex") as $type){
438         if (isset($_POST[$type])){
439           $gmailfilter[$type]= $_POST[$type];
440         }
441       }
442       if (isset($_GET['search'])){
443         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
444         if ($s == "**"){
445           $s= "*";
446         }
447         $gmailfilter['regex']= $s;
448       }
449       register_global("gmailfilter", $gmailfilter);
451       /* Get actual list */
452       $mailusers= array ();
453       if ($gmailfilter['regex'] != '*' && $gmailfilter['regex'] != ""){
454         $regex= $gmailfilter['regex'];
455         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
456       } else {
457         $filter= "";
458       }
459       if ($gmailfilter['muser'] != ""){
460         $user= $gmailfilter['muser'];
461         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
462       }
464       /* Add already present people to the filter */
465       $exclude= "";
466       foreach ($this->gosaMailForwardingAddress as $mail){
467         $exclude.= "(mail=$mail)";
468       }
469       if ($exclude != ""){
470         $filter.= "(!(|$exclude))";
471       }
473       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $gmailfilter['depselect'],
474                      array("sn", "mail", "givenName"), GL_SUBSEARCH | GL_SIZELIMIT);
475       $ldap->cd($gmailfilter['depselect']);
476       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
477       error_reporting (0);
478       while ($attrs= $ldap->fetch()){
479         if(preg_match('/%/', $attrs['mail'][0])){
480           continue;
481         }
482         $name= $this->make_name($attrs);
483         $mailusers[$attrs['mail'][0]]= $name."&lt;".
484           $attrs['mail'][0]."&gt;";
485       }
486       error_reporting (E_ALL);
487       natcasesort ($mailusers);
488       reset ($mailusers);
490       /* Show dialog */
491       $smarty->assign("search_image", get_template_path('images/search.png'));
492       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
493       $smarty->assign("tree_image", get_template_path('images/tree.png'));
494       $smarty->assign("infoimage", get_template_path('images/info.png'));
495       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
496       $smarty->assign("mailusers", $mailusers);
497       $smarty->assign("deplist", $this->config->idepartments);
498       $smarty->assign("apply", apply_filter());
499       $smarty->assign("alphabet", generate_alphabet());
500       $smarty->assign("hint", print_sizelimit_warning());
501       foreach( array("depselect", "muser", "regex") as $type){
502         $smarty->assign("$type", $gmailfilter[$type]);
503       }
504       $smarty->assign("hint", print_sizelimit_warning());
505       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE));
506       return ($display);
507     }
509     /* Assemble normal permissions */
510     $smarty->assign("permissionsACL", chkacl($this->acl, "permissions"));
511     if (isset($this->imapacl['anyone'])){
512       $smarty->assign("default_permissions", $this->imapacl['anyone']);
513     }
514     $smarty->assign("member_permissions", "lrsp");
515     if (isset($this->imapacl['%members%'])){
516       $smarty->assign("member_permissions", $this->imapacl['%members%']);
517     }
519     /* Assemble extra attributes */
520     $perm= chkacl($this->acl, "permissions");
521     $tmp= "";
522     $nr= 0;
523     $count= count($this->imapacl);
524     $this->indexed_user= array();
525     $this->indexed_acl= array();
526     foreach($this->imapacl as $user => $acl){
527       if ($user != "anyone" && $user != "%members%"){
528         $tmp.= "<tr><td><input name=\"user_$nr\" size=20 maxlength=60 ".
529           "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
530         foreach ($this->perms as $key => $value){
531           if ($acl == $key){
532             $tmp.= "<option value=$key selected>$value</option>";
533           } else {
534             $tmp.= "<option value=$key>$value</option>";
535           }
536         }
537         $tmp.= "</select>&nbsp;";
538         if ($nr == $count - 1){
539           $tmp.= "<input type=submit value=\""._("Add")."\" ".
540             "name=\"add_$nr\" $perm>";
541         }
542         if ($count > 3){
543           $tmp.= "<input type=submit value=\""._("Remove")."\" ".
544             "name=\"del_$nr\" $perm></td></tr>";
545         }
546       }
547       $this->indexed_user[$nr]= $user;
548       $this->indexed_acl[$nr++]= $acl;
549     }
550     $smarty->assign("plusattributes", $tmp);
552     /* Show main page */
553     $mailserver= array();
554     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
555       $mailserver[]= $key;
556     }
557     $smarty->assign("mailServers", $mailserver);
558     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
559           "gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
560       $smarty->assign("$val", $this->$val);
561       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
562     }
563     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
564       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
565       $smarty->assign("quotadefined", "true");
566     } else {
567       $smarty->assign("quotadefined", "false");
568     }
570     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
571     return ($display);
572   }
575   /* remove object from parent */
576   function remove_from_parent()
577   {
578     /* Added these ObjectClass and Attributes, because they were not 
579        removed correctly, only in case of kolab ... 
580      */
581     if(isset($this->config->current['MAILMETHOD'])&&preg_match("/kolab/i",$this->config->current['MAILMETHOD'])){
582       $this->attributes[]="acl";
583       $this->objectclasses[] = "kolabSharedFolder";
584     }
585     /* include global link_info */
586     $ldap= $this->config->get_ldap_link();
588     /* Remove and write to LDAP */
589     plugin::remove_from_parent();
591     /* Zero arrays */
592     $this->attrs['gosaMailAlternateAddress']= array();
593     $this->attrs['gosaMailForwardingAddress']= array();
594     $this->attrs['gosaSharedFolderTarget']= array();
596     /* Connect to IMAP server for account deletion */
597     if ($this->initially_was_account){
598       $method= new $this->method($this->config);
599       if ($method->connect($this->gosaMailServer)){
600         /* Remove account from IMAP server */
601         $method->deleteMailbox($this->uid);
602         $method->disconnect();
603       }
604       $method->fixAttributesOnRemove($this);
605     }
606     /* Keep uid */
607     unset ($this->attrs['uid']);
610     $ldap->cd($this->dn);
611     $ldap->modify ($this->attrs); 
612     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/mail with dn '%s' failed."),$this->dn));
614     /* Optionally execute a command after we're done */
615     $this->handle_post_events("remove");
616   }
619   /* Save data to object */
620   function save_object()
621   {
622     /* Assemble mail delivery mode
623        The mode field in ldap consists of values between braces, this must
624        be called when 'mail' is set, because checkboxes may not be set when
625        we're in some other dialog.
627        Example for gosaMailDeliveryMode [LR        ]
628 L: Local delivery
629 R: Reject when exceeding mailsize limit
630 S: Use spam filter
631 V: Use vacation message
632 C: Use custom sieve script
633 I: Only insider delivery */
634     if (isset($_POST['mailedit'])){
636       /* Save ldap attributes */
637       plugin::save_object();
639       $tmp= "";
640       if (!isset($_POST["drop_own_mails"])){
641         $tmp.= "L";
642       }
643       if (isset($_POST["use_mailsize_limit"])){
644         $tmp.= "R";
645       }
646       if (isset($_POST["use_spam_filter"])){
647         $tmp.= "S";
648       }
649       if (isset($_POST["use_vacation"])){
650         $tmp.= "V";
651       }
652       if (isset($_POST["own_script"])){
653         $tmp.= "C";
654       }
655       if (isset($_POST["only_local"])){
656         $tmp.= "I";
657       }
658       $tmp= "[$tmp]";
660       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
661         $this->gosaMailDeliveryMode= $tmp;
662       }
664       /* Collect data and re-assign it to the imapacl array */
665       if (chkacl($this->acl, "permissions") == ""){
666         $this->imapacl= array();
667         $this->imapacl['%members%']= $_POST['member_permissions'];
668         $this->imapacl['anyone']= $_POST['default_permissions'];
669         foreach ($this->indexed_user as $nr => $user){
670           if (!isset($_POST["user_$nr"])){
671             continue;
672           }
673           if ($_POST["user_$nr"] != $user ||
674               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
675             $this->is_modified= TRUE;
676           }
677           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
678         }
679       }
680     }
682   }
686   /* Save data to LDAP, depending on is_account we save or delete */
687   function save()
688   {
689     $ldap= $this->config->get_ldap_link();
690     $ldap->cd($this->config->current['BASE']);
692     /* Call parents save to prepare $this->attrs */
693     plugin::save();
695     /* Save arrays */
696     $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
697     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
698     $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
700     if(preg_match("/kolab/i",$this->mmethod)){
701       /* Save acl's */
702       $this->attrs['acl']= array();
703       foreach ($this->imapacl as $user => $acl){
704         if ($user == ""){
705           continue;
706         }
707         $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
708         $mail = $ldap->fetch();
709         if($mail){
710           if(isset($mail['mail'][0])){
711             $this->attrs['acl'][]= $mail['mail'][0]." $acl";
712           }
713         }else{
714           $this->attrs['acl'][]= "$user $acl";
715         }
716       }
717     }else{
718       /* Save acl's */
719       $this->attrs['acl']= array();
720       foreach ($this->imapacl as $user => $acl){
721         if ($user == ""){
722           continue;
723         }
724         $this->attrs['acl'][]= "$user $acl";
725       }
726     }
728     /* Only do IMAP actions if we are not a template */
729     if(preg_match("/kolab/i",$this->mmethod)){
730       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
731         if(isset($this->attrs['gosaMailServer'][0])){
732           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
733         }
734       }
735     }  
738     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
739       $method= new $this->method($this->config);
740       $method->fixAttributesOnStore($this);
741       if (($method->connect($this->gosaMailServer))){
742         $method->updateMailbox($this->uid);
743         $method->setQuota($this->uid, $this->gosaMailQuota);
744         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
745         $method->disconnect();
746       }
747     }
749     /* Exchange '%member%' pseudo entry */
750     $memberacl= $this->imapacl['%members%'];
752     foreach ($this->members as $user){
753       if(preg_match("/kolab/i",$this->mmethod)){
754         $ldap->cd($this->config->current['BASE']);
755         $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
756         $at = $ldap->fetch();
757         if(isset($at['mail'][0])){
758           $user = $at['mail'][0];
759         }
760       }
761       if (!isset($this->imapacl[$user])){
762         $this->imapacl[$user]= $memberacl;
763       }
764     }
765     $this->attrs['acl'] = array();
766     foreach($this->imapacl as $user => $acl){
767       if(preg_match("/%members%/",$user) || empty($user)) continue;
768       
769       $this->attrs['acl'][] = $user." ".$acl;
770     }
772     /* Save data to LDAP */
773     $ldap->cd($this->dn);
774     $this->cleanup();
775     $ldap->modify ($this->attrs); 
776     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/mail with dn '%s' failed."),$this->dn));
778     /* Optionally execute a command after we're done */
779     if ($this->initially_was_account == $this->is_account){
780       if ($this->is_modified){
781         $this->handle_post_events("mofify");
782       }
783     } else {
784       $this->handle_post_events("add");
785     }
786   }
788   /* Check formular input */
789   function check()
790   {
791     $ldap= $this->config->get_ldap_link();
793     /* Call common method to give check the hook */
794     $message= plugin::check();
796     if(!$this->is_account) return array();
797     
798     //$message[] = $str;      
800     /* must: mail */
801     if ($this->mail == ""){
802       $message[]= _("The required field 'Primary address' is not set.");
803     }
804     if (!is_email($this->mail)){
805       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
806     }
807     $ldap->cd($this->config->current['BASE']);
808     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
809         $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
810     if ($ldap->count() != 0){
811       $message[]= _("The primary address you've entered is already in use.");
812     }
813   
814     /* Check quota */
815     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
816       if (!is_numeric($this->gosaMailQuota)) {
817         $message[]= _("Value in 'Quota size' is not valid.");
818       } else {
819         $this->gosaMailQuota= (int) $this->gosaMailQuota;
820       }
821     }
823     /* Check rejectsize for integer */
824     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
825       if (!is_numeric($this->gosaMailMaxSize)){
826         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
827       } else {
828         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
829       }
830     }
832     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
833     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
834       $message[]= _("You need to set the maximum mail size in order to reject anything.");
835     }
837     if(ord($this->imapacl['anyone'][0])==194){
838       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
839     }
841     if(empty($this->gosaMailServer)){
842       $message[] = _("Please select a valid mail server.");
843     }
845     return ($message);
846   }
848   /* Adapt from template, using 'dn' */
849   function adapt_from_template($dn)
850   {
851     plugin::adapt_from_template($dn);
853     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
854       $this->$val= array();
855       if (isset($this->attrs["$val"]["count"])){
856         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
857           $value= $this->attrs["$val"][$i];
858           foreach (array("sn", "givenName", "uid") as $repl){
859             if (preg_match("/%$repl/i", $value)){
860               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
861             }
862           }
863           array_push($this->$val, $value);
864         }
865       }
866     }
867   }
869   /* Add entry to forwarder list */
870   function addForwarder($address)
871   {
872     $this->gosaMailForwardingAddress[]= $address;
873     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
875     sort ($this->gosaMailForwardingAddress);
876     reset ($this->gosaMailForwardingAddress);
877     $this->is_modified= TRUE;
878   }
880   /* Remove list of addresses from forwarder list */
881   function delForwarder($addresses)
882   {
883     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
884         $this->gosaMailForwardingAddress);
885     $this->is_modified= TRUE;
886   }
890   function addAlternate($address)
891   {
892     $ldap= $this->config->get_ldap_link();
894     $address= strtolower($address);
896     /* Is this address already assigned in LDAP? */
897     $ldap->cd ($this->config->current['BASE']);
898     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
899         "(gosaMailAlternateAddress=$address)))");
901     if ($ldap->count() > 0){
902       $attrs= $ldap->fetch ();
903       return ($attrs["uid"][0]);
904     }
906     /* Add to list of alternates */
907     if (!in_array($address, $this->gosaMailAlternateAddress)){
908       $this->gosaMailAlternateAddress[]= $address;
909     }
911     sort ($this->gosaMailAlternateAddress);
912     reset ($this->gosaMailAlternateAddress);
913     $this->is_modified= TRUE;
915     return ("");
916   }
919   function delAlternate($addresses)
920   {
921     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
922         $this->gosaMailAlternateAddress);
923     $this->is_modified= TRUE;
924   }
927   function make_name($attrs)
928   {
929     $name= "";
930     if (isset($attrs['sn'][0])){
931       $name= $attrs['sn'][0];
932     }
933     if (isset($attrs['givenName'][0])){
934       if ($name != ""){
935         $name.= ", ".$attrs['givenName'][0];
936       } else {
937         $name.= $attrs['givenName'][0];
938       }
939     }
940     if ($name != ""){
941       $name.= " ";
942     }
944     return ($name);
945   }
947   function getCopyDialog()
948   {
949     if(!$this->is_account) return("");
951     $smarty = get_smarty();
952     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
953     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
954     $smarty->assign("mail",$this->mail);
955     $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE));
956     $ret = array();
957     $ret['string'] = $display;
958     $ret['status'] = "";
959     return($ret);
960   }
962   function saveCopyDialog()
963   {
964     if(!$this->is_account) return;
966     /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
967     */
968     $this->execute();
969     if(isset($_POST['mail'])){
970       $this->mail = $_POST['mail'];
971     }
972   }
975   /* Return plugin informations for acl handling  */
976   function plInfo()
977   {
978     return (array(
979           "plShortName"   => _("Mail"),
980           "plDescription" => _("Group mail"),
981           "plSelfModify"  => FALSE,
982           "plDepends"     => array(),
983           "plPriority"    => 0,
984           "plSection"     => array("administration"),
985           "plCategory"    => array("groups"), 
986           "plProvidedAcls"=> array(
987             "mail"                      => _("Mail address"),
988             "gosaMailAlternateAddress"  => _("Alternate addresses"),
989             "gosaMailForwardingAddress" => _("Forwarding addresses"),
990             "gosaMailQuota"             => _("Quota size"),
991             "gosaMailServer"            => _("Mail server"),
992             "acl"                       => _("Permissions"))
993           ));
994   }
997 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
998 ?>