Code

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