1 <?php
2 /*! \brief mail plugin
3 \author Cajus Pollmeier <pollmeier@gonicus.de>
4 \version 2.00
5 \date 24.07.2003
7 This class provides the functionality to read and write all attributes
8 relevant for gosaMailAccounts from/to the LDAP. It does syntax checking
9 and displays the formulars required.
10 */
12 /* Load sieve support */
13 require_once ("class_sieve.inc");
15 /* Load mail methods */
16 global $BASE_DIR;
17 get_dir_list("$BASE_DIR/include");
19 class mailAccount extends plugin
20 {
21 /* Definitions */
22 var $plHeadline = "Mail";
23 var $plDescription = "This does something";
24 var $method = "mailMethod";
26 /* CLI vars */
27 var $cli_summary = "Manage users mail account";
28 var $cli_description = "Some longer text\nfor help";
29 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
31 /* plugin specific values */
32 var $mail = "";
33 var $uid = "";
34 var $gosaMailAlternateAddress = array();
35 var $gosaMailForwardingAddress = array();
36 var $gosaMailDeliveryMode = "[L ]";
37 var $gosaMailServer = "";
38 var $gosaMailQuota = "";
39 var $gosaMailMaxSize = "";
40 var $gosaVacationMessage = "";
41 var $gosaSpamSortLevel = "";
42 var $gosaSpamMailbox = "";
44 var $quotaUsage = 0;
45 var $forward_dialog = FALSE;
46 var $folder_prefix = "";
47 var $mailboxList = array();
48 var $default_permissions = "none";
49 var $member_permissions = "post";
50 var $members = array();
51 var $admins = array();
52 var $vacations = array();
53 var $perms = array( "lrs" => "read",
54 "lrsp" => "post",
55 "lrsip" => "append",
56 "lrswipcd" => "write",
57 "lrswipcda" => "all" );
59 /* attribute list for save action */
60 var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
61 "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
62 "gosaVacationMessage", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
63 var $objectclasses= array("gosaMailAccount");
66 /* constructor, if 'dn' is set, the node loads the given
67 'dn' from LDAP */
68 function mailAccount ($config, $dn= NULL)
69 {
70 /* Load bases attributes */
71 plugin::plugin($config, $dn);
73 if(isset($this->attrs['uid'])){
74 $this->uid = $this->attrs['uid'];
75 }
77 /* Set mailMethod to the one defined in gosa.conf */
78 if (isset($this->config->current['MAILMETHOD'])){
79 $method= $this->config->current['MAILMETHOD'];
80 if (class_exists("mailMethod$method")){
81 $this->method= "mailMethod$method";
82 } else {
83 print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
84 }
85 }
88 /* Create the account prefix user. user/ */
90 /* Preset folder prefix. Will change it later to respect
91 altnamespace. */
92 if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
93 $this->folder_prefix= "user/";
94 } else {
95 $this->folder_prefix= "user.";
96 }
99 /* This is not a new account, parse additional attributes */
101 #FIXME i think is_account would be enough
102 if (($dn != NULL) && ($dn != "new") && $this->is_account){
104 /* Load attributes containing arrays */
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 if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
115 $this->gosaMailServer = $this->gosaMailServer[0];
116 }
118 /* Save initial account state */
119 $this->initially_was_account= $this->is_account;
122 /* If there is a server defined, connect and get some more infos */
124 /* Only do IMAP actions if gosaMailServer attribute is set */
125 if (isset ($this->attrs["gosaMailServer"][0])){
127 $method = new $this->method($this->config);
128 $id = $method->uattrib;
130 /* Adapt attributes if needed */
131 $method->fixAttributesOnLoad($this);
132 if ($method->connect($this->attrs["gosaMailServer"][0])){
134 /* Update quota values */
135 $quota= $method->getQuota($this->folder_prefix.$this->$id);
137 if($quota){
138 if ($quota['gosaMailQuota'] == 2147483647){
139 $this->quotaUsage = "";
140 $this->gosaMailQuota = "";
141 } else {
142 $this->quotaUsage = $quota['quotaUsage'];
143 $this->gosaMailQuota = $quota['gosaMailQuota'];
144 }
145 }else{
146 print_red(sprintf(_("Can't get quota for for '%s'."),$this->folder_prefix.$this->$id));
147 }
149 /* Get mailboxes / folder like INBOX ..*/
150 $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
152 $method->disconnect();
153 }else{
154 /* Could not connect to ldap.
155 */
156 if (isset($this->attrs['gosaMailQuota'][0])){
157 $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
158 }
159 }
160 }
161 }
164 /* Get vacation message */
166 /* Fill vacation array */
167 $this->vacation= array();
168 if (isset($this->config->current['VACATIONDIR'])){
169 $dir= $this->config->current['VACATIONDIR'];
170 if (is_dir($dir) && is_readable($dir)){
172 /* Look for files and build the vacation array */
173 $dh= opendir($dir);
174 while ($file = readdir($dh)){
175 $description= $this->parse_vacation("$dir/$file");
176 if ($description != ""){
177 $this->vacation["$dir/$file"]= $description;
178 }
179 }
180 closedir($dh);
181 }
182 }
185 /* Create filter */
187 /* Get global filter config */
188 if (!is_global("mailfilter")){
189 $ui= get_userinfo();
190 $base= get_base_from_people($ui->dn);
191 $mailfilter= array( "depselect" => $base,
192 "muser" => "",
193 "regex" => "*");
194 register_global("mailfilter", $mailfilter);
195 }
196 }
199 function parse_vacation($file)
200 {
201 $desc= "";
203 if (is_file($file)){
204 $fh = fopen($file, "r");
205 $line= fgets($fh, 256);
207 if (!preg_match('/^DESC:/', $line)){
208 print_red (_("No DESC tag in vacation file:")." $file");
209 return $desc;
210 }
211 fclose ($fh);
213 $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
214 }
216 return $desc;
217 }
220 function execute()
221 {
222 /* Call parent execute */
223 plugin::execute();
225 /* Initialise vars */
227 /* Load templating engine */
228 $smarty= get_smarty();
229 $display= "";
231 /* Get available mailserver */
232 $mailserver= array();
233 foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
234 $mailserver[]= $key;
235 }
237 /* Handle account state */
239 /* Do we need to flip is_account state? */
240 if (isset($_POST['modify_state'])){
241 $this->is_account= !$this->is_account;
242 }
244 /* Do we represent a valid account? */
245 if (!$this->is_account && $this->parent == NULL){
246 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
247 _("This account has no mail extensions.")."</b>";
249 $display.= back_to_main();
250 return ($display);
251 }
253 /* Show tab dialog headers */
254 if ($this->parent != NULL){
255 if ($this->is_account){
256 $display= $this->show_header(_("Remove mail account"),
257 _("This account has mail features enabled. You can disable them by clicking below."));
258 } else {
259 $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
260 return ($display);
261 }
262 }
265 /* Forwarder subdialog */
267 /* Trigger forward add dialog? */
268 if (isset($_POST['add_local_forwarder'])){
269 $this->forward_dialog= TRUE;
270 $this->dialog= TRUE;
271 }
273 /* Cancel forward add dialog? */
274 if (isset($_POST['add_locals_cancel'])){
275 $this->forward_dialog= FALSE;
276 $this->dialog= FALSE;
277 }
279 /* Finished adding of locals? */
280 if (isset($_POST['add_locals_finish'])){
281 if (count ($_POST['local_list']) &&
282 chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
284 /* Walk through list of forwarders, ignore own addresses */
285 foreach ($_POST['local_list'] as $val){
286 if (!in_array ($val, $this->gosaMailAlternateAddress) &&
287 $val != $this->mail){
289 $this->addForwarder($val);
290 $this->is_modified= TRUE;
291 }
292 }
293 }
294 $this->forward_dialog= FALSE;
295 $this->dialog= FALSE;
296 }
298 /* Add forward email addresses */
299 if (isset($_POST['add_forwarder'])){
300 if ($_POST['forward_address'] != ""){
302 /* Valid email address specified? */
303 $address= $_POST['forward_address'];
304 $valid= FALSE;
305 if (!is_email($address)){
306 if (!is_email($address, TRUE)){
307 if ($this->is_template){
308 $valid= TRUE;
309 } else {
310 print_red (_("You're trying to add an invalid email address to the list of forwarders."));
311 }
312 }
313 } elseif ($address == $this->mail
314 || in_array($address, $this->gosaMailAlternateAddress)) {
316 print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
318 } else {
319 $valid= TRUE;
320 }
322 if ($valid){
323 /* Add it */
324 if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
325 $this->addForwarder ($address);
326 $this->is_modified= TRUE;
327 }
329 }
330 }
331 }
333 /* Delete forward email addresses */
334 if (isset($_POST['delete_forwarder'])){
335 if (count($_POST['forwarder_list'])
336 && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
338 $this->delForwarder ($_POST['forwarder_list']);
339 }
340 }
343 /* Alternate address handling */
345 /* Add alternate email addresses */
346 if (isset($_POST['add_alternate'])){
347 if ($_POST['alternate_address'] != "" &&
348 chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
350 $valid= FALSE;
351 if (!is_email($_POST['alternate_address'])){
352 if ($this->is_template){
353 if (!(is_email($_POST['alternate_address'], TRUE))){
354 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
355 } else {
356 $valid= TRUE;
357 }
358 } else {
359 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
360 }
362 } else {
363 $valid= TRUE;
364 }
366 if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
367 $ui= get_userinfo();
368 if ($user != $ui->username){
369 print_red (_("The address you're trying to add is already used by user")." '$user'.");
370 }
371 }
372 }
373 }
375 /* Delete alternate email addresses */
376 if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
377 if (count($_POST['alternates_list']) &&
378 chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
380 $this->delAlternate ($_POST['alternates_list']);
381 }
382 }
385 /* Vocation message */
387 /* Import vacation message? */
388 if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
389 $contents= "";
390 $lines= file($_POST["vacation_template"]);
391 foreach ($lines as $line){
392 if (!preg_match('/^DESC:/', $line)){
393 $contents.= $line;
394 }
395 }
397 /* Replace attributes */
398 $attrs= $this->parent->by_object['user']->attributes;
399 foreach ($attrs as $val){
400 $contents= preg_replace("/%$val/",
401 $this->parent->by_object['user']->$val, $contents);
402 }
404 /* Save message */
405 $this->gosaVacationMessage= htmlspecialchars($contents);
406 }
409 /* Display forward dialog if requested above */
411 /* Show forward add dialog */
412 if ($this->forward_dialog){
413 $ldap= $this->config->get_ldap_link();
415 /* Save data */
416 $mailfilter= get_global("mailfilter");
417 foreach( array("depselect", "muser", "regex") as $type){
418 if (isset($_POST[$type])){
419 $mailfilter[$type]= $_POST[$type];
420 }
421 }
422 if (isset($_GET['search'])){
423 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
424 if ($s == "**"){
425 $s= "*";
426 }
427 $mailfilter['regex']= $s;
428 }
429 register_global("mailfilter", $mailfilter);
431 /* Get actual list */
432 $mailusers= array ();
433 if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
434 $regex= $mailfilter['regex'];
435 $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
436 } else {
437 $filter= "";
438 }
439 if ($mailfilter['muser'] != ""){
440 $user= $mailfilter['muser'];
441 $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
442 }
444 /* Add already present people to the filter */
445 $exclude= "";
446 foreach ($this->gosaMailForwardingAddress as $mail){
447 $exclude.= "(mail=$mail)";
448 }
449 if ($exclude != ""){
450 $filter.= "(!(|$exclude))";
451 }
453 $acl= array($this->config->current['BASE'] => ":all");
454 $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'],
455 array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
456 $ldap->cd($mailfilter['depselect']);
457 $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
458 error_reporting (0);
459 while ($attrs= $ldap->fetch()){
460 if(preg_match('/%/', $attrs['mail'][0])){
461 continue;
462 }
463 $name= $this->make_name($attrs);
464 $mailusers[$attrs['mail'][0]]= $name."<".
465 $attrs['mail'][0].">";
466 }
467 error_reporting (E_ALL);
468 natcasesort ($mailusers);
469 reset ($mailusers);
471 /* Show dialog */
472 $smarty->assign("search_image", get_template_path('images/search.png'));
473 $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
474 $smarty->assign("tree_image", get_template_path('images/tree.png'));
475 $smarty->assign("infoimage", get_template_path('images/info.png'));
476 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
477 $smarty->assign("mailusers", $mailusers);
478 if (isset($_POST['depselect'])){
479 $smarty->assign("depselect", $_POST['depselect']);
480 }
481 $smarty->assign("deplist", $this->config->idepartments);
482 $smarty->assign("apply", apply_filter());
483 $smarty->assign("alphabet", generate_alphabet());
484 $smarty->assign("hint", print_sizelimit_warning());
485 foreach( array("depselect", "muser", "regex") as $type){
486 $smarty->assign("$type", $mailfilter[$type]);
487 }
488 $smarty->assign("hint", print_sizelimit_warning());
490 $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
491 return ($display);
492 }
494 /* Display mail account tab */
496 $smarty->assign("mailServers", $mailserver);
497 foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
498 "gosaMailAlternateAddress", "gosaMailForwardingAddress",
499 "gosaVacationMessage", "gosaMailDeliveryMode",
500 "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
502 $smarty->assign("$val", $this->$val);
503 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
504 }
506 if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
507 $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
508 $smarty->assign("quotadefined", "true");
509 } else {
510 $smarty->assign("quotadefined", "false");
511 }
513 /* Disable mail field if needed */
514 $method= new $this->method($this->config);
515 if ($method->uattrib == "mail" && $this->initially_was_account){
516 $smarty->assign("mailACL", "disabled");
517 }
520 if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
521 $smarty->assign("drop_own_mails", "checked");
522 } else {
523 $smarty->assign("drop_own_mails", "");
524 }
526 $types = array(
527 "V"=>"use_vacation",
528 "S"=>"use_spam_filter",
529 "R"=>"use_mailsize_limit",
530 "I"=>"only_local",
531 "C"=>"own_script");
533 /* Fill checkboxes */
534 foreach($types as $option => $varname){
535 if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
536 $smarty->assign($varname, "checked");
537 } else {
538 $smarty->assign($varname, "");
539 }
540 }
542 if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
543 $smarty->assign("use_vacation", "checked");
544 } else {
545 $smarty->assign("use_vacation", "");
546 }
547 if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
548 $smarty->assign("use_spam_filter", "checked");
549 } else {
550 $smarty->assign("use_spam_filter", "");
551 }
552 if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
553 $smarty->assign("use_mailsize_limit", "checked");
554 } else {
555 $smarty->assign("use_mailsize_limit", "");
556 }
557 if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
558 $smarty->assign("only_local", "checked");
559 } else {
560 $smarty->assign("only_local", "");
561 }
562 if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
563 $smarty->assign("own_script", "checked");
564 } else {
565 $smarty->assign("own_script", "");
566 }
568 /* Have vacation templates? */
569 $smarty->assign("template", "");
570 if (count($this->vacation)){
571 $smarty->assign("show_templates", "true");
572 $smarty->assign("vacationtemplates", $this->vacation);
573 if (isset($_POST['vacation_template'])){
574 $smarty->assign("template", $_POST['vacation_template']);
575 }
576 } else {
577 $smarty->assign("show_templates", "false");
578 }
580 /* Fill spam selector */
581 $spamlevel= array();
582 for ($i= 0; $i<21; $i++){
583 $spamlevel[]= $i;
584 }
585 $smarty->assign("spamlevel", $spamlevel);
586 $smarty->assign("spambox", $this->mailboxList);
587 $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));
588 $smarty->assign("only_localACL", chkacl($this->acl, "only_local"));
590 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
591 return ($display);
592 }
595 /* remove object from parent */
596 function remove_from_parent()
597 {
598 /* Cancel if there's nothing to do here */
599 if (!$this->initially_was_account){
600 return;
601 }
603 /* include global link_info */
604 $ldap= $this->config->get_ldap_link();
606 /* Remove and write to LDAP */
607 plugin::remove_from_parent();
609 /* Zero arrays */
610 $this->attrs['gosaMailAlternateAddress']= array();
611 $this->attrs['gosaMailForwardingAddress']= array();
613 /* Adapt attributes if needed */
614 $method= new $this->method($this->config);
615 $method->fixAttributesOnRemove($this);
617 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
618 $ldap->cd($this->dn);
619 $this->cleanup();
620 $ldap->modify ($this->attrs);
622 show_ldap_error($ldap->get_error(), _("Removing mail account failed"));
624 /* Connect to IMAP server for account deletion */
625 if ($this->gosaMailServer != ""){
626 $method= new $this->method($this->config);
627 $id= $method->uattrib;
628 if ($method->connect($this->gosaMailServer)){
630 /* Remove account from IMAP server */
631 $method->deleteMailbox($this->folder_prefix.$this->$id);
632 $method->disconnect();
633 }
634 }
636 /* Optionally execute a command after we're done */
637 $this->handle_post_events("remove");
638 }
641 /* Save data to object */
642 function save_object()
643 {
644 if (isset($_POST['mailTab'])){
645 /* Save ldap attributes */
646 plugin::save_object();
648 /* Assemble mail delivery mode
649 The mode field in ldap consists of values between braces, this must
650 be called when 'mail' is set, because checkboxes may not be set when
651 we're in some other dialog.
653 Example for gosaMailDeliveryMode [LR ]
654 L: Local delivery
655 R: Reject when exceeding mailsize limit
656 S: Use spam filter
657 V: Use vacation message
658 C: Use custm sieve script
659 I: Only insider delivery */
661 $tmp= "";
662 if (!isset($_POST["drop_own_mails"])){
663 $tmp.= "L";
664 }
665 if (isset($_POST["use_mailsize_limit"])){
666 $tmp.= "R";
667 }
668 if (isset($_POST["use_spam_filter"])){
669 $tmp.= "S";
670 }
671 if (isset($_POST["use_vacation"])){
672 $tmp.= "V";
673 }
674 if (isset($_POST["own_script"])){
675 $tmp.= "C";
676 }
677 if (isset($_POST["only_local"])){
678 $tmp.= "I";
679 }
680 $tmp= "[$tmp]";
682 if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
683 if ($this->gosaMailDeliveryMode != $tmp){
684 $this->is_modified= TRUE;
685 }
686 $this->gosaMailDeliveryMode= $tmp;
687 }
688 }
689 }
692 /* Save data to LDAP, depending on is_account we save or delete */
693 function save()
694 {
695 $ldap= $this->config->get_ldap_link();
697 /* Call parents save to prepare $this->attrs */
698 plugin::save();
700 /* Save arrays */
701 $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
702 $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
704 /* Adapt attributes if needed */
705 $method= new $this->method($this->config);
706 $id= $method->uattrib;
707 $method->fixAttributesOnStore($this);
709 /* Remove Mailquota if = "" */
710 if((isset($this->attrs['gosaMailQuota']))&&($this->attrs['gosaMailQuota']=="")) {
711 $this->attrs['gosaMailQuota']=array();
712 }
714 if(empty($this->attrs['gosaSpamMailbox'])){
715 unset($this->attrs['gosaSpamMailbox']);
716 }
718 $this->attrs['mail'] = strtolower($this->attrs['mail']);
720 /* Save data to LDAP */
721 $ldap->cd($this->dn);
722 $this->cleanup();
723 $ldap->modify ($this->attrs);
725 show_ldap_error($ldap->get_error(), _("Saving mail account failed"));
727 /* Only do IMAP actions if we are not a template */
728 if (!$this->is_template){
730 if ($method->connect($this->gosaMailServer)){
731 $method->updateMailbox($this->folder_prefix.$this->$id);
732 $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
733 $method->disconnect();
735 /* Write sieve information only if not in C mode */
736 if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
737 $method->configureFilter($this->$id,
738 $this->gosaMailDeliveryMode,
739 $this->mail,
740 $this->gosaMailAlternateAddress,
741 $this->gosaMailMaxSize,
742 $this->gosaSpamMailbox,
743 $this->gosaSpamSortLevel,
744 $this->gosaVacationMessage);
745 }
746 }
747 }
749 /* Optionally execute a command after we're done */
750 if ($this->initially_was_account == $this->is_account){
751 if ($this->is_modified){
752 $this->handle_post_events("modify");
753 }
754 } else {
755 $this->handle_post_events("add");
756 }
758 }
760 /* Check formular input */
761 function check()
762 {
763 if(!$this->is_account) return(array());
764 $ldap= $this->config->get_ldap_link();
766 /* Call common method to give check the hook */
767 $message= plugin::check();
769 if(empty($this->gosaMailServer)){
770 $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
771 }
773 /* must: mail */
774 if ($this->mail == ""){
775 $message[]= _("The required field 'Primary address' is not set.");
776 }
777 if ($this->is_template){
778 if (!is_email($this->mail, TRUE)){
779 $message[]= _("Please enter a valid email address in 'Primary address' field.");
780 }
781 } else {
782 if (!is_email($this->mail)){
783 $message[]= _("Please enter a valid email address in 'Primary address' field.");
784 }
785 }
786 $ldap->cd($this->config->current['BASE']);
787 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
788 if ($ldap->count() != 0){
789 $message[]= _("The primary address you've entered is already in use.");
790 }
792 /* Check quota */
793 if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
794 if (!is_numeric($this->gosaMailQuota)) {
795 $message[]= _("Value in 'Quota size' is not valid.");
796 } else {
797 $this->gosaMailQuota= (int) $this->gosaMailQuota;
798 }
799 }
801 /* Check rejectsize for integer */
802 if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
803 if (!is_numeric($this->gosaMailMaxSize)){
804 $message[]= _("Please specify a vaild mail size for mails to be rejected.");
805 } else {
806 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
807 }
808 }
810 /* Need gosaMailMaxSize if use_mailsize_limit is checked */
811 if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) &&
812 $this->gosaMailMaxSize == ""){
814 $message[]= _("You need to set the maximum mail size in order to reject anything.");
815 }
817 if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
818 $message[]= _("You specified Spam settings, but there is no Folder specified.");
819 }
821 return ($message);
822 }
824 /* Adapt from template, using 'dn' */
825 function adapt_from_template($dn)
826 {
827 plugin::adapt_from_template($dn);
829 foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
830 $this->$val= array();
831 if (isset($this->attrs["$val"]["count"])){
832 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
833 $value= $this->attrs["$val"][$i];
834 foreach (array("sn", "givenName", "uid") as $repl){
835 if (preg_match("/%$repl/i", $value)){
836 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
837 }
838 }
839 array_push($this->$val, strtolower(rewrite($value)));
840 }
841 }
842 }
843 $this->mail= strtolower(rewrite($this->mail));
844 }
846 /* Add entry to forwarder list */
847 function addForwarder($address)
848 {
849 $this->gosaMailForwardingAddress[]= $address;
850 $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
852 sort ($this->gosaMailForwardingAddress);
853 reset ($this->gosaMailForwardingAddress);
854 $this->is_modified= TRUE;
855 }
857 /* Remove list of addresses from forwarder list */
858 function delForwarder($addresses)
859 {
860 $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
861 $this->is_modified= TRUE;
862 }
866 function addAlternate($address)
867 {
868 $ldap= $this->config->get_ldap_link();
870 $address= strtolower($address);
872 /* Is this address already assigned in LDAP? */
873 $ldap->cd ($this->config->current['BASE']);
874 $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
876 if ($ldap->count() > 0){
877 $attrs= $ldap->fetch ();
878 return ($attrs["uid"][0]);
879 }
881 /* Add to list of alternates */
882 if (!in_array($address, $this->gosaMailAlternateAddress)){
883 $this->gosaMailAlternateAddress[]= $address;
884 $this->is_modified= TRUE;
885 }
887 sort ($this->gosaMailAlternateAddress);
888 reset ($this->gosaMailAlternateAddress);
890 return ("");
891 }
894 function delAlternate($addresses)
895 {
896 $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
897 $this->gosaMailAlternateAddress);
898 $this->is_modified= TRUE;
899 }
901 function make_name($attrs)
902 {
903 $name= "";
904 if (isset($attrs['sn'][0])){
905 $name= $attrs['sn'][0];
906 }
907 if (isset($attrs['givenName'][0])){
908 if ($name != ""){
909 $name.= ", ".$attrs['givenName'][0];
910 } else {
911 $name.= $attrs['givenName'][0];
912 }
913 }
914 if ($name != ""){
915 $name.= " ";
916 }
918 return ($name);
919 }
922 /* Create the mail part for the copy & paste dialog */
923 function getCopyDialog()
924 {
925 if(!$this->is_account) return("");
926 $smarty = get_smarty();
927 $smarty->assign("mail",$this->mail);
928 $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
929 $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
930 $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
932 $ret = array();
933 $ret['status'] = "";
934 $ret['string'] = $str;
935 return($ret);
936 }
938 function saveCopyDialog()
939 {
940 if(!$this->is_account) return;
942 /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
943 $this->execute();
945 if(isset($_POST['mail'])){
946 $this->mail = $_POST['mail'];
947 }
949 }
950 }
952 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
953 ?>