Code

Fixed group base detection.
[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            = "";       // 
43   
44   var $remove_folder_from_imap    = true;
46   /* Helper */
47   var $indexed_acl= array();
48   var $indexed_user= array();
50   /* attribute list for save action */
51   var $attributes= array( "mail",   "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
52       "gosaMailAlternateAddress", "gosaMailForwardingAddress",
53       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox",
54       "acl","gosaSharedFolderTarget", "gosaVacationMessage");
56   var $objectclasses= array("gosaMailAccount");
59   function mailgroup ($config, $dn= NULL, $ui= NULL)
60   {
61     /* Initialise all available attributes ... if possible
62      */
63     plugin::plugin($config, $dn);
65     /* Set mailMethod to the one defined in gosa.conf 
66      */
67     if (isset($this->config->current['MAILMETHOD'])){
68       $this->mmethod= $this->config->current['MAILMETHOD'];
69     }
71     /* Check if selected mail method exists 
72      */
73     if (class_exists("mailMethod$this->mmethod")){
74       $this->method= "mailMethod$this->mmethod";
75     } else {
76       print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $this->mmethod));
77     }
79     /* Load Mailserver  
80      */
81     if(isset($this->attrs['gosaMailServer'][0])){
82       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
83     }
85     /* Convert cn to uid in case of existing entry
86      */
87     if (isset($this->attrs['cn'][0])){
88       $this->uid= $this->attrs['cn'][0];
89     }
92     /* If this ins't new mailgroup, read all required data from ldap
93      */
94     if (($dn != "new")&&($dn != NULL)){
96       /* Load attributes which represent multiple entries  
97        */
98       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
99         $this->$val = array();
100         if (isset($this->attrs["$val"]["count"])){
101           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
102             array_push($this->$val, $this->attrs["$val"][$i]);
103           }
104         }
105       }
107       /* Only do IMAP actions if gosaMailServer attribute is set 
108        */
109       if (isset ($this->attrs["gosaMailServer"][0])){
111         /* Create new instance of our defined mailclass
112          */
113         $method= new $this->method($this->config);
115         if ($method->connect($this->attrs["gosaMailServer"][0])){
116         
118           /* Maybe the entry is not saved in new style, get
119              permissions from IMAP and convert them to acl attributes */
120           if (!isset($this->attrs['acl'])){
121             $this->imapacl=  $method->getSharedFolderPermissions($this->uid);
123             /* Need to filter what a member acl could be... */
124             $vote= array();
125             $peak= 0;
126             $leader= "";
127             foreach ($this->imapacl as $user => $acl){
129               if ($user != "anyone" ){
130                 if (!isset($vote[$acl])){
131                   $vote[$acl]= 1;
132                 } else {
133                   $vote[$acl]++;
134                 }
135                 if ($vote[$acl] > $peak){
136                   $leader= $acl;
137                   $peek= $vote[$acl];
138                 }
139               }
141             }
143             /* Highest count wins as %members%, remove all members
144                with the same acl */
145             if(!empty($leader)){
146               $this->imapacl['%members%']= $leader;
147             }
148             foreach ($this->imapacl as $user => $acl){
149               if ($this->acl == $leader && in_array($user, $this->attrs['memberUid'])){
150                 unset($this->imapacl[$user]);
151               }
152             }
154           } // ENDE ! isset ($this->attrs['acl'])
155           
156           /* Adapt attributes if needed */
157           $method->fixAttributesOnLoad($this);
158           
159           /*  get Quota */
160           $quota= $method->getQuota($this->uid);
162           /* Update quota values */
163           if(is_array($quota)){
164             if ($quota['gosaMailQuota'] == 2147483647){
165               $this->quotaUsage= "";
166               $this->gosaMailQuota= "";
167             } else {
168               $this->quotaUsage= $quota['quotaUsage'];
169               $this->gosaMailQuota= $quota['gosaMailQuota'];
170             }
171           }else{
172             $this->quotaUsage     = "";
173             $this->gosaMailQuota  = "";
174 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->uid));
175           }
176           $method->disconnect();
177         }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
179       }   // ENDE gosaMailServer
181     }   // ENDE dn != "new"
184     /* Get global filter config */
185     if (!is_global("gmailfilter")){
186       $ui= get_userinfo();
187       $base= get_base_from_people($ui->dn);
188       $gmailfilter= array( "depselect"       => $base,
189           "muser"            => "",
190           "regex"           => "*");
191       register_global("gmailfilter", $gmailfilter);
192     }
194     /* Load permissions */
195     $tmp = array();
196     if(preg_match("/kolab/i",$this->mmethod)){
197       $ldap = $this->config->get_ldap_link();
199       if (isset($this->attrs['acl'])){
201         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
202           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
204           /* Add to list */
205           $this->imapacl[$user]= $permission;
207           /* Get all user permissions sorted by acl, to detect the most used acl
208              This acl is then used for %members%
209            */
210           if ($user != "anyone" && $user != "%members%"){
211             $tmp[$permission][] = $user;
212           }
214           /* There is an entry in $this->imapacl like this this ... 
215              $this->attrs['imapacl']['anyone'] = "p";
216              $this->attrs['imapacl']['%members%'] = "lprs";
217              $this->attrs['imapacl'][''] = ""; <------ This is used to diplay an empty 
218              Field for special acls in our template.
219              If there is at least one special acl in out imapacl,
220              we don't need this entry anymore, because it is already displayed. 
221            */
222           if ($user != "anyone" && $user != "%members%"){
223             unset($this->imapacl['']);
224           }
225         }
226       }
228       /* In this section we dectect which acl is tho most used 
229          This will be used as %members% acl  
230        */
231       $tmp2 = array(); 
232       foreach($tmp as $acl => $user){
233         $tmp2[count($tmp[$acl])]=$acl;
234       }
235       /* Most used at last 
236        */
237       ksort($tmp2);      
238   
239       /* Assign last (most used acl) to %members% acl 
240        */
241       $str = array_pop($tmp2);
242       if(!empty($str)) {
243         $this->imapacl['%members%']=$str;
244       }
246       /* Open ldap connection 
247        */
248       $ldap = $this->config->get_ldap_link();
249       $ldap->cd($this->config->current['BASE']);
251       /* Remove those users, that use %members% acl && are member of this group. */
252       foreach($this->imapacl as $mail => $permission){
253         $ldap->search("(&(objectClass=person)(mail=".$mail."))",array("uid"));
254         $atr = $ldap->fetch();
255         if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
256           if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
257             unset($this->imapacl[$mail]);
258           }
259         }
260       }
261       /* Append an empty entry, for special acl handling */
262       if(count($this->imapacl)==2){
263         $this->imapacl[''] ="";
264       }
265   
266     }else{ // Not kolab 
267       /* Load permissions */ 
268       if (isset($this->attrs['acl'])){
269         for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
270           list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
271           $this->imapacl[$user]= $permission;
272           if ($user != "anyone" && $user != "%members%"){
273             unset($this->imapacl['']);
274           }
275         }
276       }
277     }
279     /* Load Mailserver  
280      */
281     if(isset($this->attrs['gosaMailServer'][0])){
282       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
283     }
284     /* Fill translations */
285     $this->perms["lrs"]= _("read");
286     $this->perms["lrsp"]= _("post");
287     $this->perms["p"]= _("external post");
288     $this->perms["lrsip"]= _("append");
289     $this->perms["lrswipcd"]= _("write");
290   }
292   function execute()
293   {
294     /* Call parent execute */
295     //plugin::execute();
297     /* Load templating engine */
298     $smarty= get_smarty();
300     /* Assign acls */
301     $tmp = $this->plInfo();
302     foreach($tmp['plProvidedAcls'] as $name => $translation) {
303       $smarty->assign($name."ACL",$this->getacl($name));
304     }
306     if ($_SESSION['js']==FALSE){
307       $smarty->assign("javascript", "false");
308     } else {
309       $smarty->assign("javascript", "true");
310     }
312     /* Do we need to flip is_account state? */
313     if(isset($_POST['modify_state'])){
314       if($this->is_account && $this->acl_is_removeable()){
315         $this->is_account= FALSE;
316       }elseif(!$this->is_account && $this->acl_is_createable()){
317         $this->is_account= TRUE;
318       }
319     }
321     $display = "";
323     /* Do we represent a valid account? */
324     if (!$this->is_account && $this->parent == NULL){
326       $display.= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
327         _("This 'dn' has no valid mail extensions.")."</b>";
328       return ($display);
329     }
331     /* Show tab dialog headers */
332     $display= "";
333     if ($this->parent != NULL){
334       if ($this->is_account){
335         $display.= $this->show_disable_header(_("Remove mail account"),
336             _("This account has mail features enabled. You can disable them by clicking below."));
337       } else {
338         $display.= $this->show_enable_header(_("Create mail account"),
339             _("This account has mail features disabled. You can enable them by clicking below."));
340   
341         /* Show checkbox that allows us to remove imap entry too*/
342         if($this->initially_was_account){
343           $c = "";
344           if($this->remove_folder_from_imap){
345             $c= " checked ";
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")."'>";
350           $display .= _("Remove the shared folder and all its contents from mail server.");
351         }
352         return ($display);
353       }
354     }
356     /* Add ACL? */
357     if($this->acl_is_writeable("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       }
366     }
368     /* Trigger forward add dialog? */
369     if($this->acl_is_writeable("gosaMailForwardingAddress")){
370       if (isset($_POST['add_local_forwarder'])){
371         $this->forward_dialog= TRUE;
372         $this->dialog= TRUE;
373       }
374     }
376     /* Cancel forward add dialog? */
377     if($this->acl_is_writeable("gosaMailForwardingAddress")){
378       if (isset($_POST['add_locals_cancel'])){
379         $this->forward_dialog= FALSE;
380         $this->dialog= FALSE;
381       }
382     }
384     /* Finished adding of locals? */
385     if ((isset($_POST['add_locals_finish'])) && ($this->acl_is_writeable("gosaMailForwardingAddress"))) {
386       if (count ($_POST['local_list']) && $this->acl_is_writeable("gosaMailForwardingAddress")){
388         /* Walk through list of forwarders, ignore own addresses */
389         foreach ($_POST['local_list'] as $val){
390           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
391               $val != $this->mail){
393             $this->addForwarder($val);
394           }
395         }
396       }
397       $this->forward_dialog= FALSE;
398       $this->dialog= FALSE;
399     }
401     /* Add forward email addresses */
402     if ((isset($_POST['add_forwarder'])) && ($this->acl_is_writeable("gosaMailForwardingAddress"))){
403       if ($_POST['forward_address'] != ""){
405         /* Valid email address specified? */
406         $address= $_POST['forward_address'];
407         if (!is_email($address)){
409           print_red (_("You're trying to add an invalid email address ".
410                 "to the list of forwarders."));
412         } elseif ($address == $this->mail
413             || in_array($address, $this->gosaMailAlternateAddress)) {
415           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
417         } else {
419           /* Add it */
420           if ($this->acl_is_writeable("gosaMailForwardingAddress")){
421             $this->addForwarder ($address);
422           }
424         }
425       }
426     }
428     /* Delete forward email addresses */
429     if (isset($_POST['delete_forwarder']) && ($this->acl_is_writeable("gosaMailForwardingAddress"))){
430       if (count($_POST['forwarder_list'])&& $this->acl_is_writeable("gosaMailForwardingAddress")){
432         $this->delForwarder ($_POST['forwarder_list']);
433       }
434     }
436     /* Add alternate email addresses */
437     if (isset($_POST['add_alternate'])){
438       if ($_POST['alternate_address'] != "" && $this->acl_is_writeable("gosaMailAlternateAddress")){
440         if (!is_email($_POST['alternate_address'])){
441           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
443         } elseif (($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
444           $ui= get_userinfo();
445           if ($user != $ui->username){
446             print_red (_("The address you're trying to add is already used by user")." '$user'.");
447           }
448         }
449       }
450     }
452     /* Delete alternate email addresses */
453     if($this->acl_is_writeable("gosaMailAlternateAddress")){
454       if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
455         if (count($_POST['alternates_list']) && $this->acl_is_writeable("gosaMailAlternateAddress")){
456           $this->delAlternate ($_POST['alternates_list']);
457         }
458       }
459     }
461     /* Show forward add dialog */
462     if ($this->forward_dialog){
463       $ldap= $this->config->get_ldap_link();
465       /* Save data */
466       $gmailfilter= get_global("gmailfilter");
467       foreach( array("depselect", "muser", "regex") as $type){
468         if (isset($_POST[$type])){
469           $gmailfilter[$type]= $_POST[$type];
470         }
471       }
472       if (isset($_GET['search'])){
473         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
474         if ($s == "**"){
475           $s= "*";
476         }
477         $gmailfilter['regex']= $s;
478       }
479       register_global("gmailfilter", $gmailfilter);
481       /* Get actual list */
482       $mailusers= array ();
483       if ($gmailfilter['regex'] != '*' && $gmailfilter['regex'] != ""){
484         $regex= $gmailfilter['regex'];
485         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
486       } else {
487         $filter= "";
488       }
489       if ($gmailfilter['muser'] != ""){
490         $user= $gmailfilter['muser'];
491         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
492       }
494       /* Add already present people to the filter */
495       $exclude= "";
496       foreach ($this->gosaMailForwardingAddress as $mail){
497         $exclude.= "(mail=$mail)";
498       }
499       if ($exclude != ""){
500         $filter.= "(!(|$exclude))";
501       }
503       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $gmailfilter['depselect'],
504                      array("sn", "mail", "givenName"), GL_SUBSEARCH | GL_SIZELIMIT);
505       $ldap->cd($gmailfilter['depselect']);
506       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
507       error_reporting (0);
508       while ($attrs= $ldap->fetch()){
509         if(preg_match('/%/', $attrs['mail'][0])){
510           continue;
511         }
512         $name= $this->make_name($attrs);
513         $mailusers[$attrs['mail'][0]]= $name."&lt;".
514           $attrs['mail'][0]."&gt;";
515       }
516       error_reporting (E_ALL);
517       natcasesort ($mailusers);
518       reset ($mailusers);
520       /* Show dialog */
521       $smarty->assign("search_image", get_template_path('images/search.png'));
522       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
523       $smarty->assign("tree_image", get_template_path('images/tree.png'));
524       $smarty->assign("infoimage", get_template_path('images/info.png'));
525       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
526       $smarty->assign("mailusers", $mailusers);
527       $smarty->assign("deplist", $this->config->idepartments);
528       $smarty->assign("apply", apply_filter());
529       $smarty->assign("alphabet", generate_alphabet());
530       $smarty->assign("hint", print_sizelimit_warning());
531       foreach( array("depselect", "muser", "regex") as $type){
532         $smarty->assign("$type", $gmailfilter[$type]);
533       }
534       $smarty->assign("hint", print_sizelimit_warning());
535       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE));
536       return ($display);
537     }
539     /* Assemble normal 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= $this->getacl( "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){
557       /* Add additional acl settings */
558       if ($user != "anyone" && $user != "%members%"){
560         $Dis  = "";
561         if(!preg_match("/w/",$perm)){
562           $Dis = " disabled ";
563         }
564   
565         $tmp.= "<tr>  
566                  <td>
567                   <input name=\"user_$nr\" size=20 maxlength=60 value=\"$user\" ".$Dis.">
568                  </td>
569                  <td>
570                  <select size=\"1\" name=\"perm_$nr\" ".$Dis.">";
572         /* Add acl options for this additional acl setting */
573         if(preg_match("/r/",$perm)){
574           foreach ($this->perms as $key => $value){
575             if ($acl == $key){
576               $tmp.= "<option value=$key selected>$value</option>";
577             } else {
578               $tmp.= "<option value=$key>$value</option>";
579             }
580           }
581         }
582         $tmp.= "</select>&nbsp;";
585         
586         if ($nr == $count - 1){
587           if($this->acl_is_writeable("acl")){
588             $tmp.= "<input type=submit value=\""._("Add")."\" ".
589               "name=\"add_$nr\" >";
590           }
591         }
592         if ($count > 3){
593           if($this->acl_is_writeable("acl")){
594             $tmp.= "<input type=submit value=\""._("Remove")."\" ".
595               "name=\"del_$nr\" ></td></tr>";        
596           }
597         }
598       }
599       $this->indexed_user[$nr]= $user;
600       $this->indexed_acl[$nr++]= $acl;
601     }
602     $smarty->assign("plusattributes", $tmp);
604     /* Show main page */
605     $mailserver= array();
606     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
607       $mailserver[]= $key;
608     }
609     $smarty->assign("mailServers", $mailserver);
610     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
611           "gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
612       $smarty->assign("$val", $this->$val);
613     }
614     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
615       if($this->acl_is_readable("gosaMailQuota")){
616         $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
617         $smarty->assign("quotadefined", "true");
618       }else{
619         $smarty->assign("quotadefined", "true");
620         $smarty->assign("quotausage", "-");
621       }
622     } else {
623       $smarty->assign("quotadefined", "false");
624     }
626     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
627     return ($display);
628   }
631   /* remove object from parent */
632   function remove_from_parent()
633   {
634     /* Added these ObjectClass and Attributes, because they were not 
635        removed correctly, only in case of kolab ... 
636      */
637     if(isset($this->config->current['MAILMETHOD'])&&preg_match("/kolab/i",$this->config->current['MAILMETHOD'])){
638       $this->attributes[]="acl";
639       $this->objectclasses[] = "kolabSharedFolder";
640     }
641     /* include global link_info */
642     $ldap= $this->config->get_ldap_link();
644     /* Remove and write to LDAP */
645     plugin::remove_from_parent();
647     /* Zero arrays */
648     $this->attrs['gosaMailAlternateAddress']= array();
649     $this->attrs['gosaMailForwardingAddress']= array();
650     $this->attrs['gosaSharedFolderTarget']= array();
652     /* Connect to IMAP server for account deletion */
653     if ($this->initially_was_account){
654  
655       $method= new $this->method($this->config);
656       $method->fixAttributesOnRemove($this);
657       if ($method->connect($this->gosaMailServer) && $this->remove_folder_from_imap){
659         /* Remove account from IMAP server */
660         $method->deleteMailbox($this->uid);
661         $method->disconnect();
662       }
663     }
664     /* Keep uid */
665     unset ($this->attrs['uid']);
667     $ldap->cd($this->dn);
668     $ldap->modify ($this->attrs); 
669     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/mail with dn '%s' failed."),$this->dn));
671     /* Optionally execute a command after we're done */
672     $this->handle_post_events("remove");
673   }
676   /* Save data to object */
677   function save_object()
678   {
680     /* Check if user wants to remove the shared folder from imap too */
681     if($this->initially_was_account && !$this->is_account){
682       if(isset($_POST['remove_folder_from_imap'])){
683         $this->remove_folder_from_imap = true;
684       }else{
685         $this->remove_folder_from_imap = false;
686       }
687     }
689     /* Assemble mail delivery mode
690        The mode field in ldap consists of values between braces, this must
691        be called when 'mail' is set, because checkboxes may not be set when
692        we're in some other dialog.
694        Example for gosaMailDeliveryMode [LR        ]
695 L: Local delivery
696 R: Reject when exceeding mailsize limit
697 S: Use spam filter
698 V: Use vacation message
699 C: Use custom sieve script
700 I: Only insider delivery */
701     if (isset($_POST['mailedit'])){
703       plugin::save_object();
705       $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
707       /* Handle delivery flags */
708       if($this->acl_is_writeable("gosaMailDeliveryModeL")){
709         if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
710           $tmp.="L";
711         }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
712           $tmp = preg_replace("/L/","",$tmp);
713         }
714       }
716       $opts = array(
717           "R"   => "use_mailsize_limit",
718           "S"   => "use_spam_filter",
719           "V"   => "use_vacation",
720           "C"   => "own_script",
721           "I"   => "only_local");
723       foreach($opts as $flag => $post){
724         if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
725           if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
726             $tmp.= $flag;
727           }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
728             $tmp = preg_replace("/".$flag."/","",$tmp);
729           }
730         }
731       }
733       $tmp= "[$tmp]";
734       if ($this->gosaMailDeliveryMode != $tmp){
735         $this->is_modified= TRUE;
736       }
737       $this->gosaMailDeliveryMode= $tmp;
739       /* Collect data and re-assign it to the imapacl array */
740       if ($this->acl_is_writeable("acl")){
741         $this->imapacl= array();
742         $this->imapacl['%members%']= $_POST['member_permissions'];
743         $this->imapacl['anyone']= $_POST['default_permissions'];
744         foreach ($this->indexed_user as $nr => $user){
745           if (!isset($_POST["user_$nr"])){
746             continue;
747           }
748           if ($_POST["user_$nr"] != $user ||
749               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
750             $this->is_modified= TRUE;
751           }
752           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
753         }
754       }
755     }
757   }
761   /* Save data to LDAP, depending on is_account we save or delete */
762   function save()
763   {
764     $ldap= $this->config->get_ldap_link();
765     $ldap->cd($this->config->current['BASE']);
767     /* Call parents save to prepare $this->attrs */
768     plugin::save();
770     /* Save arrays */
771     $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
772     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
773     $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
775     if(preg_match("/kolab/i",$this->mmethod)){
776       /* Save acl's */
777       $this->attrs['acl']= array();
778       foreach ($this->imapacl as $user => $acl){
779         if ($user == ""){
780           continue;
781         }
782         $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
783         $mail = $ldap->fetch();
784         if($mail){
785           if(isset($mail['mail'][0])){
786             $this->attrs['acl'][]= $mail['mail'][0]." $acl";
787           }
788         }else{
789           $this->attrs['acl'][]= "$user $acl";
790         }
791       }
792     }else{
793       /* Save acl's */
794       $this->attrs['acl']= array();
795       foreach ($this->imapacl as $user => $acl){
796         if ($user == ""){
797           continue;
798         }
799         $this->attrs['acl'][]= "$user $acl";
800       }
801     }
803     /* Only do IMAP actions if we are not a template */
804     if(preg_match("/kolab/i",$this->mmethod)){
805       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
806         if(isset($this->attrs['gosaMailServer'][0])){
807           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
808         }
809       }
810     }  
813     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
814       $method= new $this->method($this->config);
815       $method->fixAttributesOnStore($this);
816       if (($method->connect($this->gosaMailServer))){
817         $method->updateMailbox($this->uid);
818         $method->setQuota($this->uid, $this->gosaMailQuota);
819         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
820         $method->disconnect();
821       }
822     }
824     /* Exchange '%member%' pseudo entry */
825     $memberacl= $this->imapacl['%members%'];
827     foreach ($this->members as $user){
828       if(preg_match("/kolab/i",$this->mmethod)){
829         $ldap->cd($this->config->current['BASE']);
830         $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
831         $at = $ldap->fetch();
832         if(isset($at['mail'][0])){
833           $user = $at['mail'][0];
834         }
835       }
836       if (!isset($this->imapacl[$user])){
837         $this->imapacl[$user]= $memberacl;
838       }
839     }
840     $this->attrs['acl'] = array();
841     foreach($this->imapacl as $user => $acl){
842       if(preg_match("/%members%/",$user) || empty($user)) continue;
843       
844       $this->attrs['acl'][] = $user." ".$acl;
845     }
847     /* Save data to LDAP */
848     $ldap->cd($this->dn);
849     $this->cleanup();
850     $ldap->modify ($this->attrs); 
851     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/mail with dn '%s' failed."),$this->dn));
853     /* Optionally execute a command after we're done */
854     if ($this->initially_was_account == $this->is_account){
855       if ($this->is_modified){
856         $this->handle_post_events("modify");
857       }
858     } else {
859       $this->handle_post_events("add");
860     }
861   }
863   /* Check formular input */
864   function check()
865   {
866     $ldap= $this->config->get_ldap_link();
868     /* Call common method to give check the hook */
869     $message= plugin::check();
871     if(!$this->is_account) return array();
872     
873     //$message[] = $str;      
875     /* must: mail */
876     if ($this->mail == ""){
877       $message[]= _("The required field 'Primary address' is not set.");
878     }
879     if (!is_email($this->mail)){
880       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
881     }
882     $ldap->cd($this->config->current['BASE']);
883     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
884         $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
885     if ($ldap->count() != 0){
886       $message[]= _("The primary address you've entered is already in use.");
887     }
888   
889     /* Check quota */
890     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
891       if (!is_numeric($this->gosaMailQuota)) {
892         $message[]= _("Value in 'Quota size' is not valid.");
893       } else {
894         $this->gosaMailQuota= (int) $this->gosaMailQuota;
895       }
896     }
898     /* Check rejectsize for integer */
899     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailQuota")){
900       if (!is_numeric($this->gosaMailMaxSize)){
901         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
902       } else {
903         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
904       }
905     }
907     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
908     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
909       $message[]= _("You need to set the maximum mail size in order to reject anything.");
910     }
912     if(ord($this->imapacl['anyone'][0])==194){
913       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
914     }
916     if(empty($this->gosaMailServer)){
917       $message[] = _("Please select a valid mail server.");
918     }
920     return ($message);
921   }
923   /* Adapt from template, using 'dn' */
924   function adapt_from_template($dn)
925   {
926     plugin::adapt_from_template($dn);
928     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
929       $this->$val= array();
930       if (isset($this->attrs["$val"]["count"])){
931         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
932           $value= $this->attrs["$val"][$i];
933           foreach (array("sn", "givenName", "uid") as $repl){
934             if (preg_match("/%$repl/i", $value)){
935               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
936             }
937           }
938           array_push($this->$val, $value);
939         }
940       }
941     }
942   }
944   /* Add entry to forwarder list */
945   function addForwarder($address)
946   {
947     $this->gosaMailForwardingAddress[]= $address;
948     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
950     sort ($this->gosaMailForwardingAddress);
951     reset ($this->gosaMailForwardingAddress);
952     $this->is_modified= TRUE;
953   }
955   /* Remove list of addresses from forwarder list */
956   function delForwarder($addresses)
957   {
958     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
959         $this->gosaMailForwardingAddress);
960     $this->is_modified= TRUE;
961   }
965   function addAlternate($address)
966   {
967     $ldap= $this->config->get_ldap_link();
969     $address= strtolower($address);
971     /* Is this address already assigned in LDAP? */
972     $ldap->cd ($this->config->current['BASE']);
973     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
974         "(gosaMailAlternateAddress=$address)))");
976     if ($ldap->count() > 0){
977       $attrs= $ldap->fetch ();
978       return ($attrs["uid"][0]);
979     }
981     /* Add to list of alternates */
982     if (!in_array($address, $this->gosaMailAlternateAddress)){
983       $this->gosaMailAlternateAddress[]= $address;
984     }
986     sort ($this->gosaMailAlternateAddress);
987     reset ($this->gosaMailAlternateAddress);
988     $this->is_modified= TRUE;
990     return ("");
991   }
994   function delAlternate($addresses)
995   {
996     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
997         $this->gosaMailAlternateAddress);
998     $this->is_modified= TRUE;
999   }
1002   function make_name($attrs)
1003   {
1004     $name= "";
1005     if (isset($attrs['sn'][0])){
1006       $name= $attrs['sn'][0];
1007     }
1008     if (isset($attrs['givenName'][0])){
1009       if ($name != ""){
1010         $name.= ", ".$attrs['givenName'][0];
1011       } else {
1012         $name.= $attrs['givenName'][0];
1013       }
1014     }
1015     if ($name != ""){
1016       $name.= " ";
1017     }
1019     return ($name);
1020   }
1022   function getCopyDialog()
1023   {
1024     if(!$this->is_account) return("");
1026     $smarty = get_smarty();
1027     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1028     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1029     $smarty->assign("mail",$this->mail);
1030     $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE));
1031     $ret = array();
1032     $ret['string'] = $display;
1033     $ret['status'] = "";
1034     return($ret);
1035   }
1037   function saveCopyDialog()
1038   {
1039     if(!$this->is_account) return;
1041     /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
1042     */
1043     $this->execute();
1044     if(isset($_POST['mail'])){
1045       $this->mail = $_POST['mail'];
1046     }
1047   }
1050   /* Return plugin informations for acl handling  */
1051   function plInfo()
1052   {
1053     return (array(
1054           "plShortName"   => _("Mail"),
1055           "plDescription" => _("Group mail"),
1056           "plSelfModify"  => FALSE,
1057           "plDepends"     => array(),
1058           "plPriority"    => 0,
1059           "plSection"     => array("administration"),
1060           "plCategory"    => array("groups"), 
1061           "plProvidedAcls"=> array(
1062             "mail"                      => _("Mail address"),
1063             "gosaMailAlternateAddress"  => _("Alternate addresses"),
1064             "gosaMailForwardingAddress" => _("Forwarding addresses"),
1065             "gosaMailQuota"             => _("Quota size"),
1066             "gosaMailServer"            => _("Mail server"),
1067             "acl"                       => _("Permissions"))
1068           ));
1069   }
1072 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1073 ?>