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