Code

Fixed groupmail acl stuff
[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])){
116           /*  get Quota
117            */
118           $quota= $method->getQuota($this->uid);
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'])
158           /* Update quota values */
159           if ($quota['gosaMailQuota'] == 2147483647){
160             $this->quotaUsage= "";
161             $this->gosaMailQuota= "";
162           } else {
163             $this->quotaUsage= $quota['quotaUsage'];
164             $this->gosaMailQuota= $quota['gosaMailQuota'];
165           }
166           $method->disconnect();
167         }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
169         /* Adapt attributes if needed */
170         $method->fixAttributesOnLoad($this);
172       }   // ENDE gosaMailServer
174     }   // ENDE dn != "new"
177     /* Get global filter config */
178     if (!is_global("gmailfilter")){
179       $ui= get_userinfo();
180       $base= get_base_from_people($ui->dn);
181       $gmailfilter= array( "depselect"       => $base,
182           "muser"            => "",
183           "regex"           => "*");
184       register_global("gmailfilter", $gmailfilter);
185     }
187     /* Load permissions */
188     $tmp = array();
189     if(preg_match("/kolab/i",$this->mmethod)){
190       $ldap = $this->config->get_ldap_link();
192       if (isset($this->attrs['acl'])){
194         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
195           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
197           /* Add to list */
198           $this->imapacl[$user]= $permission;
200           /* Get all user permissions sorted by acl, to detect the most used acl
201              This acl is then used for %members%
202            */
203           if ($user != "anyone" && $user != "%members%"){
204             $tmp[$permission][] = $user;
205           }
207           /* There is an entry in $this->imapacl like this this ... 
208              $this->attrs['imapacl']['anyone'] = "p";
209              $this->attrs['imapacl']['%members%'] = "lprs";
210              $this->attrs['imapacl'][''] = ""; <------ This is used to diplay an empty 
211              Field for special acls in our template.
212              If there is at least one special acl in out imapacl,
213              we don't need this entry anymore, because it is already displayed. 
214            */
215           if ($user != "anyone" && $user != "%members%"){
216             unset($this->imapacl['']);
217           }
218         }
219       }
221       /* In this section we dectect which acl is tho most used 
222          This will be used as %members% acl  
223        */
224       $tmp2 = array(); 
225       foreach($tmp as $acl => $user){
226         $tmp2[count($tmp[$acl])]=$acl;
227       }
228       /* Most used at last 
229        */
230       ksort($tmp2);      
231   
232       /* Assign last (most used acl) to %members% acl 
233        */
234       $str = array_pop($tmp2);
235       if(!empty($str)) {
236         $this->imapacl['%members%']=$str;
237       }
239       /* Open ldap connection 
240        */
241       $ldap = $this->config->get_ldap_link();
242       $ldap->cd($this->config->current['BASE']);
244       /* Remove those users, that use %members% acl && are member of this group. */
245       foreach($this->imapacl as $mail => $permission){
246         $ldap->search("(&(objectClass=person)(mail=".$mail."))",array("uid"));
247         $atr = $ldap->fetch();
248         if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
249           if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
250             unset($this->imapacl[$mail]);
251           }
252         }
253       }
254       /* Append an empty entry, for special acl handling */
255       if(count($this->imapacl)==2){
256         $this->imapacl[''] ="";
257       }
258   
259     }else{ // Not kolab 
260       /* Load permissions */ 
261       if (isset($this->attrs['acl'])){
262         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
263           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
264           $this->imapacl[$user]= $permission;
265           if ($user != "anyone" && $user != "%members%"){
266             unset($this->imapacl['']);
267           }
268         }
269       }
270     }
272     /* Fill translations */
273     $this->perms["lrs"]= _("read");
274     $this->perms["lrsp"]= _("post");
275     $this->perms["p"]= _("external post");
276     $this->perms["lrsip"]= _("append");
277     $this->perms["lrswipcd"]= _("write");
278   }
280   function execute()
281   {
282     /* Call parent execute */
283     //plugin::execute();
285     /* Load templating engine */
286     $smarty= get_smarty();
287     if ($_SESSION['js']==FALSE){
288       $smarty->assign("javascript", "false");
289     } else {
290       $smarty->assign("javascript", "true");
291     }
293     /* Do we need to flip is_account state? */
294     if (isset($_POST['modify_state'])){
295       $this->is_account= !$this->is_account;
296     }
298     /* Do we represent a valid account? */
299     if (!$this->is_account && $this->parent == NULL){
300       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
301         _("This 'dn' has no valid mail extensions.")."</b>";
302       return ($display);
303     }
305     /* Show tab dialog headers */
306     $display= "";
307     if ($this->parent != NULL){
308       if ($this->is_account){
309         $display= $this->show_header(_("Remove mail account"),
310             _("This account has mail features enabled. You can disable them by clicking below."));
311       } else {
312         $display= $this->show_header(_("Create mail account"),
313             _("This account has mail features disabled. You can enable them by clicking below."));
314         return ($display);
315       }
316     }
318     /* Add ACL? */
319     foreach ($this->indexed_user as $nr => $user){
320       if (isset($_POST["add_$nr"])){
321         $this->imapacl[""]= "l";
322       }
323       if (isset($_POST["del_$nr"])){
324         unset ($this->imapacl[$user]);
325       }
326     }
328     /* Trigger forward add dialog? */
329     if (isset($_POST['add_local_forwarder'])){
330       $this->forward_dialog= TRUE;
331       $this->dialog= TRUE;
332     }
334     /* Cancel forward add dialog? */
335     if (isset($_POST['add_locals_cancel'])){
336       $this->forward_dialog= FALSE;
337       $this->dialog= FALSE;
338     }
340     /* Finished adding of locals? */
341     if (isset($_POST['add_locals_finish'])){
342       if (count ($_POST['local_list']) &&
343           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
345         /* Walk through list of forwarders, ignore own addresses */
346         foreach ($_POST['local_list'] as $val){
347           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
348               $val != $this->mail){
350             $this->addForwarder($val);
351           }
352         }
353       }
354       $this->forward_dialog= FALSE;
355       $this->dialog= FALSE;
356     }
358     /* Add forward email addresses */
359     if (isset($_POST['add_forwarder'])){
360       if ($_POST['forward_address'] != ""){
362         /* Valid email address specified? */
363         $address= $_POST['forward_address'];
364         if (!is_email($address)){
366           print_red (_("You're trying to add an invalid email address ".
367                 "to the list of forwarders."));
369         } elseif ($address == $this->mail
370             || in_array($address, $this->gosaMailAlternateAddress)) {
372           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
374         } else {
376           /* Add it */
377           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
378             $this->addForwarder ($address);
379           }
381         }
382       }
383     }
385     /* Delete forward email addresses */
386     if (isset($_POST['delete_forwarder'])){
387       if (count($_POST['forwarder_list'])
388           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
390         $this->delForwarder ($_POST['forwarder_list']);
391       }
392     }
394     /* Add alternate email addresses */
395     if (isset($_POST['add_alternate'])){
396       if ($_POST['alternate_address'] != "" &&
397           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
399         if (!is_email($_POST['alternate_address'])){
400           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
402         } elseif (($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
403           $ui= get_userinfo();
404           if ($user != $ui->username){
405             print_red (_("The address you're trying to add is already used by user")." '$user'.");
406           }
407         }
408       }
409     }
411     /* Delete alternate email addresses */
412     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
413       if (count($_POST['alternates_list']) &&
414           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
416         $this->delAlternate ($_POST['alternates_list']);
417       }
418     }
420     /* Show forward add dialog */
421     if ($this->forward_dialog){
422       $ldap= $this->config->get_ldap_link();
424       /* Save data */
425       $gmailfilter= get_global("gmailfilter");
426       foreach( array("depselect", "muser", "regex") as $type){
427         if (isset($_POST[$type])){
428           $gmailfilter[$type]= $_POST[$type];
429         }
430       }
431       if (isset($_GET['search'])){
432         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
433         if ($s == "**"){
434           $s= "*";
435         }
436         $gmailfilter['regex']= $s;
437       }
438       register_global("gmailfilter", $gmailfilter);
440       /* Get actual list */
441       $mailusers= array ();
442       if ($gmailfilter['regex'] != '*' && $gmailfilter['regex'] != ""){
443         $regex= $gmailfilter['regex'];
444         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
445       } else {
446         $filter= "";
447       }
448       if ($gmailfilter['muser'] != ""){
449         $user= $gmailfilter['muser'];
450         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
451       }
453       /* Add already present people to the filter */
454       $exclude= "";
455       foreach ($this->gosaMailForwardingAddress as $mail){
456         $exclude.= "(mail=$mail)";
457       }
458       if ($exclude != ""){
459         $filter.= "(!(|$exclude))";
460       }
462       $acl= array($this->config->current['BASE'] => ":all");
463       $res= get_list($acl, "(&(objectClass=gosaMailAccount)$filter)", TRUE, $gmailfilter['depselect'], array("sn", "mail", "givenName"), TRUE);
464       $ldap->cd($gmailfilter['depselect']);
465       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
466       error_reporting (0);
467       while ($attrs= $ldap->fetch()){
468         if(preg_match('/%/', $attrs['mail'][0])){
469           continue;
470         }
471         $name= $this->make_name($attrs);
472         $mailusers[$attrs['mail'][0]]= $name."&lt;".
473           $attrs['mail'][0]."&gt;";
474       }
475       error_reporting (E_ALL);
476       natcasesort ($mailusers);
477       reset ($mailusers);
479       /* Show dialog */
480       $smarty->assign("search_image", get_template_path('images/search.png'));
481       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
482       $smarty->assign("tree_image", get_template_path('images/tree.png'));
483       $smarty->assign("infoimage", get_template_path('images/info.png'));
484       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
485       $smarty->assign("mailusers", $mailusers);
486       $smarty->assign("deplist", $this->config->idepartments);
487       $smarty->assign("apply", apply_filter());
488       $smarty->assign("alphabet", generate_alphabet());
489       $smarty->assign("hint", print_sizelimit_warning());
490       foreach( array("depselect", "muser", "regex") as $type){
491         $smarty->assign("$type", $gmailfilter[$type]);
492       }
493       $smarty->assign("hint", print_sizelimit_warning());
494       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE));
495       return ($display);
496     }
498     /* Assemble normal permissions */
499     $smarty->assign("permissionsACL", chkacl($this->acl, "permissions"));
500     if (isset($this->imapacl['anyone'])){
501       $smarty->assign("default_permissions", $this->imapacl['anyone']);
502     }
503     $smarty->assign("member_permissions", "lrsp");
504     if (isset($this->imapacl['%members%'])){
505       $smarty->assign("member_permissions", $this->imapacl['%members%']);
506     }
508     /* Assemble extra attributes */
509     $perm= chkacl($this->acl, "permissions");
510     $tmp= "";
511     $nr= 0;
512     $count= count($this->imapacl);
513     $this->indexed_user= array();
514     $this->indexed_acl= array();
515     foreach($this->imapacl as $user => $acl){
516       if ($user != "anyone" && $user != "%members%"){
517         $tmp.= "<tr><td><input name=\"user_$nr\" size=20 maxlength=60 ".
518           "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
519         foreach ($this->perms as $key => $value){
520           if ($acl == $key){
521             $tmp.= "<option value=$key selected>$value</option>";
522           } else {
523             $tmp.= "<option value=$key>$value</option>";
524           }
525         }
526         $tmp.= "</select>&nbsp;";
527         if ($nr == $count - 1){
528           $tmp.= "<input type=submit value=\""._("Add")."\" ".
529             "name=\"add_$nr\" $perm>";
530         }
531         if ($count > 3){
532           $tmp.= "<input type=submit value=\""._("Remove")."\" ".
533             "name=\"del_$nr\" $perm></td></tr>";
534         }
535       }
536       $this->indexed_user[$nr]= $user;
537       $this->indexed_acl[$nr++]= $acl;
538     }
539     $smarty->assign("plusattributes", $tmp);
541     /* Show main page */
542     $mailserver= array();
543     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
544       $mailserver[]= $key;
545     }
546     $smarty->assign("mailServers", $mailserver);
547     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
548           "gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
549       $smarty->assign("$val", $this->$val);
550       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
551     }
552     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
553       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
554       $smarty->assign("quotadefined", "true");
555     } else {
556       $smarty->assign("quotadefined", "false");
557     }
559     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
560     return ($display);
561   }
564   /* remove object from parent */
565   function remove_from_parent()
566   {
567     /* Added these ObjectClass and Attributes, because they were not 
568        removed correctly, only in case of kolab ... 
569      */
570     if(isset($this->config->current['MAILMETHOD'])&&preg_match("/kolab/i",$this->config->current['MAILMETHOD'])){
571       $this->attributes[]="acl";
572       $this->attributes[]="kolabHomeServer";
573       $this->objectclasses[] = "kolabSharedFolder";
574     }
575     /* include global link_info */
576     $ldap= $this->config->get_ldap_link();
578     /* Remove and write to LDAP */
579     plugin::remove_from_parent();
581     /* Zero arrays */
582     $this->attrs['gosaMailAlternateAddress']= array();
583     $this->attrs['gosaMailForwardingAddress']= array();
584     $this->attrs['gosaSharedFolderTarget']= array();
586     /* Keep uid */
587     unset ($this->attrs['uid']);
588     $ldap->cd($this->dn);
589     $this->cleanup();
590     $ldap->modify ($this->attrs); 
592     show_ldap_error($ldap->get_error());
594     /* Connect to IMAP server for account deletion */
595     if ($this->is_account){
596       $method= new $this->method($this->config);
597       if ($method->connect($this->attrs["gosaMailServer"][0])){
598         /* Remove account from IMAP server */
599         $method->deleteMailbox($this->uid);
600         $method->disconnect();
601       }
602       $method->fixAttributesOnRemove($this);
603     }
605     /* Optionally execute a command after we're done */
606     $this->handle_post_events("remove");
607   }
610   /* Save data to object */
611   function save_object()
612   {
613     /* Assemble mail delivery mode
614        The mode field in ldap consists of values between braces, this must
615        be called when 'mail' is set, because checkboxes may not be set when
616        we're in some other dialog.
618        Example for gosaMailDeliveryMode [LR        ]
619 L: Local delivery
620 R: Reject when exceeding mailsize limit
621 S: Use spam filter
622 V: Use vacation message
623 C: Use custom sieve script
624 I: Only insider delivery */
625     if (isset($_POST['mailedit'])){
627       /* Save ldap attributes */
628       plugin::save_object();
630       $tmp= "";
631       if (!isset($_POST["drop_own_mails"])){
632         $tmp.= "L";
633       }
634       if (isset($_POST["use_mailsize_limit"])){
635         $tmp.= "R";
636       }
637       if (isset($_POST["use_spam_filter"])){
638         $tmp.= "S";
639       }
640       if (isset($_POST["use_vacation"])){
641         $tmp.= "V";
642       }
643       if (isset($_POST["own_script"])){
644         $tmp.= "C";
645       }
646       if (isset($_POST["only_local"])){
647         $tmp.= "I";
648       }
649       $tmp= "[$tmp]";
651       if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
652         $this->gosaMailDeliveryMode= $tmp;
653       }
655       /* Collect data and re-assign it to the imapacl array */
656       if (chkacl($this->acl, "permissions") == ""){
657         $this->imapacl= array();
658         $this->imapacl['%members%']= $_POST['member_permissions'];
659         $this->imapacl['anyone']= $_POST['default_permissions'];
660         foreach ($this->indexed_user as $nr => $user){
661           if (!isset($_POST["user_$nr"])){
662             continue;
663           }
664           if ($_POST["user_$nr"] != $user ||
665               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
666             $this->is_modified= TRUE;
667           }
668           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
669         }
670       }
671     }
673   }
677   /* Save data to LDAP, depending on is_account we save or delete */
678   function save()
679   {
680     $ldap= $this->config->get_ldap_link();
681     $ldap->cd($this->config->current['BASE']);
683     /* Call parents save to prepare $this->attrs */
684     plugin::save();
686     /* Save arrays */
687     $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
688     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
690     /* Save shared folder target */
691     $this->attrs['gosaSharedFolderTarget']= "share+".$this->uid;
693     if(preg_match("/kolab/i",$this->mmethod)){
694       /* Save acl's */
695       $this->attrs['acl']= array();
696       foreach ($this->imapacl as $user => $acl){
697         if ($user == ""){
698           continue;
699         }
700         $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
701         $mail = $ldap->fetch();
702         if($mail){
703           if(isset($mail['mail'][0])){
704             $this->attrs['acl'][]= $mail['mail'][0]." $acl";
705           }
706         }else{
707           $this->attrs['acl'][]= "$user $acl";
708         }
709       }
710     }else{
711       /* Save acl's */
712       $this->attrs['acl']= array();
713       foreach ($this->imapacl as $user => $acl){
714         if ($user == ""){
715           continue;
716         }
717         $this->attrs['acl'][]= "$user $acl";
718       }
719     }
721     /* Only do IMAP actions if we are not a template */
722     if(preg_match("/kolab/i",$this->mmethod)){
723       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
724         if(isset($this->attrs['gosaMailServer'][0])){
725           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
726         }
727       }
728     }  
731     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
732       $method= new $this->method($this->config);
733       $method->fixAttributesOnStore($this);
734       if (($method->connect($this->gosaMailServer))){
735         $method->updateMailbox($this->uid);
736         $method->setQuota($this->uid, $this->gosaMailQuota);
737         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
738         $method->disconnect();
739       }
740     }
742     /* Exchange '%member%' pseudo entry */
743     $memberacl= $this->imapacl['%members%'];
745     if(empty($memberacl)){
746       print_a($this);
747       exit();
748     }
750     foreach ($this->members as $user){
751       if(preg_match("/kolab/i",$this->mmethod)){
752         $ldap->cd($this->config->current['BASE']);
753         $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
754         $at = $ldap->fetch();
755         if(isset($at['mail'][0])){
756           $user = $at['mail'][0];
757         }
758       }
759       if (!isset($this->imapacl[$user])){
760         $this->imapacl[$user]= $memberacl;
761       }
762     }
763     $this->attrs['acl'] = array();
764     foreach($this->imapacl as $user => $acl){
765       if(preg_match("/%members%/",$user) || empty($user)) continue;
766       
767       $this->attrs['acl'][] = $user." ".$acl;
768     }
770     /* Save data to LDAP */
771     $ldap->cd($this->dn);
772     $this->cleanup();
773     $ldap->modify ($this->attrs); 
775     show_ldap_error($ldap->get_error());
777     /* Optionally execute a command after we're done */
778     if ($this->initially_was_account == $this->is_account){
779       if ($this->is_modified){
780         $this->handle_post_events("mofify");
781       }
782     } else {
783       $this->handle_post_events("add");
784     }
785   }
787   /* Check formular input */
788   function check()
789   {
790     $ldap= $this->config->get_ldap_link();
792     $message= array();
794     /* must: mail */
795     if ($this->mail == ""){
796       $message[]= _("The required field 'Primary address' is not set.");
797     }
798     if (!is_email($this->mail)){
799       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
800     }
801     $ldap->cd($this->config->current['BASE']);
802     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
803         $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
804     if ($ldap->count() != 0){
805       $message[]= _("The primary address you've entered is already in use.");
806     }
808     /* Check quota */
809     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
810       if (!is_numeric($this->gosaMailQuota)) {
811         $message[]= _("Value in 'Quota size' is not valid.");
812       } else {
813         $this->gosaMailQuota= (int) $this->gosaMailQuota;
814       }
815     }
817     /* Check rejectsize for integer */
818     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
819       if (!is_numeric($this->gosaMailMaxSize)){
820         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
821       } else {
822         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
823       }
824     }
826     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
827     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
828       $message[]= _("You need to set the maximum mail size in order to reject anything.");
829     }
831     if(ord($this->imapacl['anyone'][0])==194){
832       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
833     }
835     if(empty($this->gosaMailServer)){
836       $message[] = _("Please select a valid mail server.");
837     }
839     return ($message);
840   }
842   /* Adapt from template, using 'dn' */
843   function adapt_from_template($dn)
844   {
845     plugin::adapt_from_template($dn);
847     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
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   }
863   /* Add entry to forwarder list */
864   function addForwarder($address)
865   {
866     $this->gosaMailForwardingAddress[]= $address;
867     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
869     sort ($this->gosaMailForwardingAddress);
870     reset ($this->gosaMailForwardingAddress);
871     $this->is_modified= TRUE;
872   }
874   /* Remove list of addresses from forwarder list */
875   function delForwarder($addresses)
876   {
877     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
878         $this->gosaMailForwardingAddress);
879     $this->is_modified= TRUE;
880   }
884   function addAlternate($address)
885   {
886     $ldap= $this->config->get_ldap_link();
888     $address= strtolower($address);
890     /* Is this address already assigned in LDAP? */
891     $ldap->cd ($this->config->current['BASE']);
892     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
893         "(gosaMailAlternateAddress=$address)))");
895     if ($ldap->count() > 0){
896       $attrs= $ldap->fetch ();
897       return ($attrs["uid"][0]);
898     }
900     /* Add to list of alternates */
901     if (!in_array($address, $this->gosaMailAlternateAddress)){
902       $this->gosaMailAlternateAddress[]= $address;
903     }
905     sort ($this->gosaMailAlternateAddress);
906     reset ($this->gosaMailAlternateAddress);
907     $this->is_modified= TRUE;
909     return ("");
910   }
913   function delAlternate($addresses)
914   {
915     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
916         $this->gosaMailAlternateAddress);
917     $this->is_modified= TRUE;
918   }
921   function make_name($attrs)
922   {
923     $name= "";
924     if (isset($attrs['sn'][0])){
925       $name= $attrs['sn'][0];
926     }
927     if (isset($attrs['givenName'][0])){
928       if ($name != ""){
929         $name.= ", ".$attrs['givenName'][0];
930       } else {
931         $name.= $attrs['givenName'][0];
932       }
933     }
934     if ($name != ""){
935       $name.= " ";
936     }
938     return ($name);
939   }
943 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
944 ?>