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