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