Code

Added new base method
[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();
299     /* Assign acls */
300     $tmp = $this->plInfo();
301     foreach($tmp['plProvidedAcls'] as $name => $translation) {
302       $smarty->assign($name."ACL",$this->getacl($name));
303     }
305     if ($_SESSION['js']==FALSE){
306       $smarty->assign("javascript", "false");
307     } else {
308       $smarty->assign("javascript", "true");
309     }
311     /* Do we need to flip is_account state? */
312     if(isset($_POST['modify_state'])){
313       if($this->is_account && $this->acl_is_removeable()){
314         $this->is_account= FALSE;
315       }elseif(!$this->is_account && $this->acl_is_createable()){
316         $this->is_account= TRUE;
317       }
318     }
321     /* Do we represent a valid account? */
322     if (!$this->is_account && $this->parent == NULL){
323       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
324         _("This 'dn' has no valid mail extensions.")."</b>";
325       return ($display);
326     }
328     /* Show tab dialog headers */
329     $display= "";
330     if ($this->parent != NULL){
331       if ($this->is_account){
332         $display= $this->show_disable_header(_("Remove mail account"),
333             _("This account has mail features enabled. You can disable them by clicking below."));
334       } else {
335         $display= $this->show_enable_header(_("Create mail account"),
336             _("This account has mail features disabled. You can enable them by clicking below."));
337         return ($display);
338       }
339     }
341     /* Add ACL? */
342     if($this->acl_is_writeable("acl")){
343       foreach ($this->indexed_user as $nr => $user){
344         if (isset($_POST["add_$nr"])){
345           $this->imapacl[""]= "l";
346         }
347         if (isset($_POST["del_$nr"])){
348           unset ($this->imapacl[$user]);
349         }
350       }
351     }
353     /* Trigger forward add dialog? */
354     if($this->acl_is_writeable("gosaMailForwardingAddress")){
355       if (isset($_POST['add_local_forwarder'])){
356         $this->forward_dialog= TRUE;
357         $this->dialog= TRUE;
358       }
359     }
361     /* Cancel forward add dialog? */
362     if($this->acl_is_writeable("gosaMailForwardingAddress")){
363       if (isset($_POST['add_locals_cancel'])){
364         $this->forward_dialog= FALSE;
365         $this->dialog= FALSE;
366       }
367     }
369     /* Finished adding of locals? */
370     if ((isset($_POST['add_locals_finish'])) && ($this->acl_is_writeable("gosaMailForwardingAddress"))) {
371       if (count ($_POST['local_list']) && $this->acl_is_writeable("gosaMailForwardingAddress")){
373         /* Walk through list of forwarders, ignore own addresses */
374         foreach ($_POST['local_list'] as $val){
375           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
376               $val != $this->mail){
378             $this->addForwarder($val);
379           }
380         }
381       }
382       $this->forward_dialog= FALSE;
383       $this->dialog= FALSE;
384     }
386     /* Add forward email addresses */
387     if ((isset($_POST['add_forwarder'])) && ($this->acl_is_writeable("gosaMailForwardingAddress"))){
388       if ($_POST['forward_address'] != ""){
390         /* Valid email address specified? */
391         $address= $_POST['forward_address'];
392         if (!is_email($address)){
394           print_red (_("You're trying to add an invalid email address ".
395                 "to the list of forwarders."));
397         } elseif ($address == $this->mail
398             || in_array($address, $this->gosaMailAlternateAddress)) {
400           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
402         } else {
404           /* Add it */
405           if ($this->acl_is_writeable("gosaMailForwardingAddress")){
406             $this->addForwarder ($address);
407           }
409         }
410       }
411     }
413     /* Delete forward email addresses */
414     if (isset($_POST['delete_forwarder']) && ($this->acl_is_writeable("gosaMailForwardingAddress"))){
415       if (count($_POST['forwarder_list'])&& $this->acl_is_writeable("gosaMailForwardingAddress")){
417         $this->delForwarder ($_POST['forwarder_list']);
418       }
419     }
421     /* Add alternate email addresses */
422     if (isset($_POST['add_alternate'])){
423       if ($_POST['alternate_address'] != "" && $this->acl_is_writeable("gosaMailAlternateAddress")){
425         if (!is_email($_POST['alternate_address'])){
426           print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
428         } elseif (($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
429           $ui= get_userinfo();
430           if ($user != $ui->username){
431             print_red (_("The address you're trying to add is already used by user")." '$user'.");
432           }
433         }
434       }
435     }
437     /* Delete alternate email addresses */
438     if($this->acl_is_writeable("gosaMailAlternateAddress")){
439       if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
440         if (count($_POST['alternates_list']) && $this->acl_is_writeable("gosaMailAlternateAddress")){
441           $this->delAlternate ($_POST['alternates_list']);
442         }
443       }
444     }
446     /* Show forward add dialog */
447     if ($this->forward_dialog){
448       $ldap= $this->config->get_ldap_link();
450       /* Save data */
451       $gmailfilter= get_global("gmailfilter");
452       foreach( array("depselect", "muser", "regex") as $type){
453         if (isset($_POST[$type])){
454           $gmailfilter[$type]= $_POST[$type];
455         }
456       }
457       if (isset($_GET['search'])){
458         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
459         if ($s == "**"){
460           $s= "*";
461         }
462         $gmailfilter['regex']= $s;
463       }
464       register_global("gmailfilter", $gmailfilter);
466       /* Get actual list */
467       $mailusers= array ();
468       if ($gmailfilter['regex'] != '*' && $gmailfilter['regex'] != ""){
469         $regex= $gmailfilter['regex'];
470         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
471       } else {
472         $filter= "";
473       }
474       if ($gmailfilter['muser'] != ""){
475         $user= $gmailfilter['muser'];
476         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
477       }
479       /* Add already present people to the filter */
480       $exclude= "";
481       foreach ($this->gosaMailForwardingAddress as $mail){
482         $exclude.= "(mail=$mail)";
483       }
484       if ($exclude != ""){
485         $filter.= "(!(|$exclude))";
486       }
488       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $gmailfilter['depselect'],
489                      array("sn", "mail", "givenName"), GL_SUBSEARCH | GL_SIZELIMIT);
490       $ldap->cd($gmailfilter['depselect']);
491       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
492       error_reporting (0);
493       while ($attrs= $ldap->fetch()){
494         if(preg_match('/%/', $attrs['mail'][0])){
495           continue;
496         }
497         $name= $this->make_name($attrs);
498         $mailusers[$attrs['mail'][0]]= $name."&lt;".
499           $attrs['mail'][0]."&gt;";
500       }
501       error_reporting (E_ALL);
502       natcasesort ($mailusers);
503       reset ($mailusers);
505       /* Show dialog */
506       $smarty->assign("search_image", get_template_path('images/search.png'));
507       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
508       $smarty->assign("tree_image", get_template_path('images/tree.png'));
509       $smarty->assign("infoimage", get_template_path('images/info.png'));
510       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
511       $smarty->assign("mailusers", $mailusers);
512       $smarty->assign("deplist", $this->config->idepartments);
513       $smarty->assign("apply", apply_filter());
514       $smarty->assign("alphabet", generate_alphabet());
515       $smarty->assign("hint", print_sizelimit_warning());
516       foreach( array("depselect", "muser", "regex") as $type){
517         $smarty->assign("$type", $gmailfilter[$type]);
518       }
519       $smarty->assign("hint", print_sizelimit_warning());
520       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE));
521       return ($display);
522     }
524     /* Assemble normal permissions */
525     if (isset($this->imapacl['anyone'])){
526       $smarty->assign("default_permissions", $this->imapacl['anyone']);
527     }
528     $smarty->assign("member_permissions", "lrsp");
529     if (isset($this->imapacl['%members%'])){
530       $smarty->assign("member_permissions", $this->imapacl['%members%']);
531     }
533     /* Assemble extra attributes */
534     $perm= $this->getacl( "permissions");
535     $tmp= "";
536     $nr= 0;
537     $count= count($this->imapacl);
538     $this->indexed_user= array();
539     $this->indexed_acl= array();
540     foreach($this->imapacl as $user => $acl){
542       /* Add additional acl settings */
543       if ($user != "anyone" && $user != "%members%"){
545         $Dis  = "";
546         if(!preg_match("/w/",$perm)){
547           $Dis = " disabled ";
548         }
549   
550         $tmp.= "<tr>  
551                  <td>
552                   <input name=\"user_$nr\" size=20 maxlength=60 value=\"$user\" ".$Dis.">
553                  </td>
554                  <td>
555                  <select size=\"1\" name=\"perm_$nr\" ".$Dis.">";
557         /* Add acl options for this additional acl setting */
558         if(preg_match("/r/",$perm)){
559           foreach ($this->perms as $key => $value){
560             if ($acl == $key){
561               $tmp.= "<option value=$key selected>$value</option>";
562             } else {
563               $tmp.= "<option value=$key>$value</option>";
564             }
565           }
566         }
567         $tmp.= "</select>&nbsp;";
570         
571         if ($nr == $count - 1){
572           if($this->acl_is_writeable("acl")){
573             $tmp.= "<input type=submit value=\""._("Add")."\" ".
574               "name=\"add_$nr\" >";
575           }
576         }
577         if ($count > 3){
578           if($this->acl_is_writeable("acl")){
579             $tmp.= "<input type=submit value=\""._("Remove")."\" ".
580               "name=\"del_$nr\" ></td></tr>";        
581           }
582         }
583       }
584       $this->indexed_user[$nr]= $user;
585       $this->indexed_acl[$nr++]= $acl;
586     }
587     $smarty->assign("plusattributes", $tmp);
589     /* Show main page */
590     $mailserver= array();
591     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
592       $mailserver[]= $key;
593     }
594     $smarty->assign("mailServers", $mailserver);
595     foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
596           "gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
597       $smarty->assign("$val", $this->$val);
598     }
599     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
600       if($this->acl_is_readable("gosaMailQuota")){
601         $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
602         $smarty->assign("quotadefined", "true");
603       }else{
604         $smarty->assign("quotadefined", "true");
605         $smarty->assign("quotausage", "-");
606       }
607     } else {
608       $smarty->assign("quotadefined", "false");
609     }
611     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
612     return ($display);
613   }
616   /* remove object from parent */
617   function remove_from_parent()
618   {
619     /* Added these ObjectClass and Attributes, because they were not 
620        removed correctly, only in case of kolab ... 
621      */
622     if(isset($this->config->current['MAILMETHOD'])&&preg_match("/kolab/i",$this->config->current['MAILMETHOD'])){
623       $this->attributes[]="acl";
624       $this->objectclasses[] = "kolabSharedFolder";
625     }
626     /* include global link_info */
627     $ldap= $this->config->get_ldap_link();
629     /* Remove and write to LDAP */
630     plugin::remove_from_parent();
632     /* Zero arrays */
633     $this->attrs['gosaMailAlternateAddress']= array();
634     $this->attrs['gosaMailForwardingAddress']= array();
635     $this->attrs['gosaSharedFolderTarget']= array();
637     /* Connect to IMAP server for account deletion */
638     if ($this->initially_was_account){
639       $method= new $this->method($this->config);
640       if ($method->connect($this->gosaMailServer)){
641         /* Remove account from IMAP server */
642         $method->deleteMailbox($this->uid);
643         $method->disconnect();
644       }
645       $method->fixAttributesOnRemove($this);
646     }
647     /* Keep uid */
648     unset ($this->attrs['uid']);
651     $ldap->cd($this->dn);
652     $ldap->modify ($this->attrs); 
653     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/mail with dn '%s' failed."),$this->dn));
655     /* Optionally execute a command after we're done */
656     $this->handle_post_events("remove");
657   }
660   /* Save data to object */
661   function save_object()
662   {
663     /* Assemble mail delivery mode
664        The mode field in ldap consists of values between braces, this must
665        be called when 'mail' is set, because checkboxes may not be set when
666        we're in some other dialog.
668        Example for gosaMailDeliveryMode [LR        ]
669 L: Local delivery
670 R: Reject when exceeding mailsize limit
671 S: Use spam filter
672 V: Use vacation message
673 C: Use custom sieve script
674 I: Only insider delivery */
675     if (isset($_POST['mailedit'])){
677       plugin::save_object();
679       $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
681       /* Handle delivery flags */
682       if($this->acl_is_writeable("gosaMailDeliveryModeL")){
683         if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
684           $tmp.="L";
685         }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
686           $tmp = preg_replace("/L/","",$tmp);
687         }
688       }
690       $opts = array(
691           "R"   => "use_mailsize_limit",
692           "S"   => "use_spam_filter",
693           "V"   => "use_vacation",
694           "C"   => "own_script",
695           "I"   => "only_local");
697       foreach($opts as $flag => $post){
698         if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
699           if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
700             $tmp.= $flag;
701           }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
702             $tmp = preg_replace("/".$flag."/","",$tmp);
703           }
704         }
705       }
707       $tmp= "[$tmp]";
708       if ($this->gosaMailDeliveryMode != $tmp){
709         $this->is_modified= TRUE;
710       }
711       $this->gosaMailDeliveryMode= $tmp;
713       /* Collect data and re-assign it to the imapacl array */
714       if ($this->acl_is_writeable("acl")){
715         $this->imapacl= array();
716         $this->imapacl['%members%']= $_POST['member_permissions'];
717         $this->imapacl['anyone']= $_POST['default_permissions'];
718         foreach ($this->indexed_user as $nr => $user){
719           if (!isset($_POST["user_$nr"])){
720             continue;
721           }
722           if ($_POST["user_$nr"] != $user ||
723               $_POST["perm_$nr"] != $this->indexed_acl[$nr]){
724             $this->is_modified= TRUE;
725           }
726           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
727         }
728       }
729     }
731   }
735   /* Save data to LDAP, depending on is_account we save or delete */
736   function save()
737   {
738     $ldap= $this->config->get_ldap_link();
739     $ldap->cd($this->config->current['BASE']);
741     /* Call parents save to prepare $this->attrs */
742     plugin::save();
744     /* Save arrays */
745     $this->attrs['gosaMailAlternateAddress']  = $this->gosaMailAlternateAddress;
746     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
747     $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
749     if(preg_match("/kolab/i",$this->mmethod)){
750       /* Save acl's */
751       $this->attrs['acl']= array();
752       foreach ($this->imapacl as $user => $acl){
753         if ($user == ""){
754           continue;
755         }
756         $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
757         $mail = $ldap->fetch();
758         if($mail){
759           if(isset($mail['mail'][0])){
760             $this->attrs['acl'][]= $mail['mail'][0]." $acl";
761           }
762         }else{
763           $this->attrs['acl'][]= "$user $acl";
764         }
765       }
766     }else{
767       /* Save acl's */
768       $this->attrs['acl']= array();
769       foreach ($this->imapacl as $user => $acl){
770         if ($user == ""){
771           continue;
772         }
773         $this->attrs['acl'][]= "$user $acl";
774       }
775     }
777     /* Only do IMAP actions if we are not a template */
778     if(preg_match("/kolab/i",$this->mmethod)){
779       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
780         if(isset($this->attrs['gosaMailServer'][0])){
781           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
782         }
783       }
784     }  
787     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
788       $method= new $this->method($this->config);
789       $method->fixAttributesOnStore($this);
790       if (($method->connect($this->gosaMailServer))){
791         $method->updateMailbox($this->uid);
792         $method->setQuota($this->uid, $this->gosaMailQuota);
793         $method->setSharedFolderPermissions($this->uid, $this->imapacl);
794         $method->disconnect();
795       }
796     }
798     /* Exchange '%member%' pseudo entry */
799     $memberacl= $this->imapacl['%members%'];
801     foreach ($this->members as $user){
802       if(preg_match("/kolab/i",$this->mmethod)){
803         $ldap->cd($this->config->current['BASE']);
804         $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
805         $at = $ldap->fetch();
806         if(isset($at['mail'][0])){
807           $user = $at['mail'][0];
808         }
809       }
810       if (!isset($this->imapacl[$user])){
811         $this->imapacl[$user]= $memberacl;
812       }
813     }
814     $this->attrs['acl'] = array();
815     foreach($this->imapacl as $user => $acl){
816       if(preg_match("/%members%/",$user) || empty($user)) continue;
817       
818       $this->attrs['acl'][] = $user." ".$acl;
819     }
821     /* Save data to LDAP */
822     $ldap->cd($this->dn);
823     $this->cleanup();
824     $ldap->modify ($this->attrs); 
825     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/mail with dn '%s' failed."),$this->dn));
827     /* Optionally execute a command after we're done */
828     if ($this->initially_was_account == $this->is_account){
829       if ($this->is_modified){
830         $this->handle_post_events("modify");
831       }
832     } else {
833       $this->handle_post_events("add");
834     }
835   }
837   /* Check formular input */
838   function check()
839   {
840     $ldap= $this->config->get_ldap_link();
842     /* Call common method to give check the hook */
843     $message= plugin::check();
845     if(!$this->is_account) return array();
846     
847     //$message[] = $str;      
849     /* must: mail */
850     if ($this->mail == ""){
851       $message[]= _("The required field 'Primary address' is not set.");
852     }
853     if (!is_email($this->mail)){
854       $message[]= _("Please enter a valid email addres in 'Primary address' field.");
855     }
856     $ldap->cd($this->config->current['BASE']);
857     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
858         $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
859     if ($ldap->count() != 0){
860       $message[]= _("The primary address you've entered is already in use.");
861     }
862   
863     /* Check quota */
864     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
865       if (!is_numeric($this->gosaMailQuota)) {
866         $message[]= _("Value in 'Quota size' is not valid.");
867       } else {
868         $this->gosaMailQuota= (int) $this->gosaMailQuota;
869       }
870     }
872     /* Check rejectsize for integer */
873     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailQuota")){
874       if (!is_numeric($this->gosaMailMaxSize)){
875         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
876       } else {
877         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
878       }
879     }
881     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
882     if (is_integer(strpos($this->gosaMailDeliveryMode, "reject")) && $this->gosaMailMaxSize == ""){
883       $message[]= _("You need to set the maximum mail size in order to reject anything.");
884     }
886     if(ord($this->imapacl['anyone'][0])==194){
887       $message[] = _("Please choose valid permission settings. Default permission can't be emtpy.");
888     }
890     if(empty($this->gosaMailServer)){
891       $message[] = _("Please select a valid mail server.");
892     }
894     return ($message);
895   }
897   /* Adapt from template, using 'dn' */
898   function adapt_from_template($dn)
899   {
900     plugin::adapt_from_template($dn);
902     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
903       $this->$val= array();
904       if (isset($this->attrs["$val"]["count"])){
905         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
906           $value= $this->attrs["$val"][$i];
907           foreach (array("sn", "givenName", "uid") as $repl){
908             if (preg_match("/%$repl/i", $value)){
909               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
910             }
911           }
912           array_push($this->$val, $value);
913         }
914       }
915     }
916   }
918   /* Add entry to forwarder list */
919   function addForwarder($address)
920   {
921     $this->gosaMailForwardingAddress[]= $address;
922     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
924     sort ($this->gosaMailForwardingAddress);
925     reset ($this->gosaMailForwardingAddress);
926     $this->is_modified= TRUE;
927   }
929   /* Remove list of addresses from forwarder list */
930   function delForwarder($addresses)
931   {
932     $this->gosaMailForwardingAddress= array_remove_entries ($addresses,
933         $this->gosaMailForwardingAddress);
934     $this->is_modified= TRUE;
935   }
939   function addAlternate($address)
940   {
941     $ldap= $this->config->get_ldap_link();
943     $address= strtolower($address);
945     /* Is this address already assigned in LDAP? */
946     $ldap->cd ($this->config->current['BASE']);
947     $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)".
948         "(gosaMailAlternateAddress=$address)))");
950     if ($ldap->count() > 0){
951       $attrs= $ldap->fetch ();
952       return ($attrs["uid"][0]);
953     }
955     /* Add to list of alternates */
956     if (!in_array($address, $this->gosaMailAlternateAddress)){
957       $this->gosaMailAlternateAddress[]= $address;
958     }
960     sort ($this->gosaMailAlternateAddress);
961     reset ($this->gosaMailAlternateAddress);
962     $this->is_modified= TRUE;
964     return ("");
965   }
968   function delAlternate($addresses)
969   {
970     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
971         $this->gosaMailAlternateAddress);
972     $this->is_modified= TRUE;
973   }
976   function make_name($attrs)
977   {
978     $name= "";
979     if (isset($attrs['sn'][0])){
980       $name= $attrs['sn'][0];
981     }
982     if (isset($attrs['givenName'][0])){
983       if ($name != ""){
984         $name.= ", ".$attrs['givenName'][0];
985       } else {
986         $name.= $attrs['givenName'][0];
987       }
988     }
989     if ($name != ""){
990       $name.= " ";
991     }
993     return ($name);
994   }
996   function getCopyDialog()
997   {
998     if(!$this->is_account) return("");
1000     $smarty = get_smarty();
1001     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1002     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1003     $smarty->assign("mail",$this->mail);
1004     $display= $smarty->fetch (get_template_path('paste_mail.tpl', TRUE));
1005     $ret = array();
1006     $ret['string'] = $display;
1007     $ret['status'] = "";
1008     return($ret);
1009   }
1011   function saveCopyDialog()
1012   {
1013     if(!$this->is_account) return;
1015     /* Perform ADD / REMOVE ... for mail alternate / mail forwarding addresses 
1016     */
1017     $this->execute();
1018     if(isset($_POST['mail'])){
1019       $this->mail = $_POST['mail'];
1020     }
1021   }
1024   /* Return plugin informations for acl handling  */
1025   function plInfo()
1026   {
1027     return (array(
1028           "plShortName"   => _("Mail"),
1029           "plDescription" => _("Group mail"),
1030           "plSelfModify"  => FALSE,
1031           "plDepends"     => array(),
1032           "plPriority"    => 0,
1033           "plSection"     => array("administration"),
1034           "plCategory"    => array("groups"), 
1035           "plProvidedAcls"=> array(
1036             "mail"                      => _("Mail address"),
1037             "gosaMailAlternateAddress"  => _("Alternate addresses"),
1038             "gosaMailForwardingAddress" => _("Forwarding addresses"),
1039             "gosaMailQuota"             => _("Quota size"),
1040             "gosaMailServer"            => _("Mail server"),
1041             "acl"                       => _("Permissions"))
1042           ));
1043   }
1046 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1047 ?>