Code

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