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", "uid", "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);
74 /**********************
75 Set mailMethod to the one defined in gosa.conf
76 **********************/
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 /**********************
89 Create the account prefix user. user/
90 **********************/
92 /* Preset folder prefix. Will change it later to respect
93 altnamespace. */
94 if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
95 $this->folder_prefix= "user/";
96 } else {
97 $this->folder_prefix= "user.";
98 }
101 /**********************
102 This is not a new account, parse additional attributes
103 **********************/
105 #FIXME i think is_account would be enough
106 if (($dn != NULL) && ($dn != "new") && $this->is_account){
108 /* Load attributes containing arrays */
109 foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
110 $this->$val= array();
111 if (isset($this->attrs["$val"]["count"])){
112 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
113 array_push($this->$val, $this->attrs["$val"][$i]);
114 }
115 }
116 }
118 if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
119 $this->gosaMailServer = $this->gosaMailServer[0];
120 }
122 /* Save initial account state */
123 $this->initially_was_account= $this->is_account;
126 /**********************
127 If there is a server defined, connect and get some more infos
128 **********************/
130 /* Only do IMAP actions if gosaMailServer attribute is set */
131 if (isset ($this->attrs["gosaMailServer"][0])){
133 $method = new $this->method($this->config);
134 $id = $method->uattrib;
136 /* Adapt attributes if needed */
137 $method->fixAttributesOnLoad($this);
138 if ($method->connect($this->attrs["gosaMailServer"][0])){
140 /* Update quota values */
141 $quota= $method->getQuota($this->folder_prefix.$this->$id);
143 if($quota){
145 if ($quota['gosaMailQuota'] == 2147483647){
146 $this->quotaUsage = "";
147 $this->gosaMailQuota = "";
148 } else {
149 $this->quotaUsage = $quota['quotaUsage'];
150 $this->gosaMailQuota = $quota['gosaMailQuota'];
151 }
152 }else{
153 print_red(sprintf(_("Can't get quota for for '%s'."),$this->folder_prefix.$this->$id));
154 }
157 /* Get mailboxes / folder like INBOX ..*/
158 $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
160 $method->disconnect();
161 }else{
162 /* Could not connect to ldap.
163 */
164 if (isset($this->attrs['gosaMailQuota'][0])){
165 $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
166 }
167 }
168 }
169 }
172 /**********************
173 Get vacation message
174 **********************/
176 /* Fill vacation array */
177 $this->vacation= array();
178 if (isset($this->config->current['VACATIONDIR'])){
179 $dir= $this->config->current['VACATIONDIR'];
180 if (is_dir($dir) && is_readable($dir)){
182 /* Look for files and build the vacation array */
183 $dh= opendir($dir);
184 while ($file = readdir($dh)){
185 $description= $this->parse_vacation("$dir/$file");
186 if ($description != ""){
187 $this->vacation["$dir/$file"]= $description;
188 }
189 }
190 closedir($dh);
191 }
192 }
195 /**********************
196 Create filter
197 **********************/
199 /* Get global filter config */
200 if (!is_global("mailfilter")){
201 $ui= get_userinfo();
202 $base= get_base_from_people($ui->dn);
203 $mailfilter= array( "depselect" => $base,
204 "muser" => "",
205 "regex" => "*");
206 register_global("mailfilter", $mailfilter);
207 }
208 }
211 function parse_vacation($file)
212 {
213 $desc= "";
215 if (is_file($file)){
216 $fh = fopen($file, "r");
217 $line= fgets($fh, 256);
219 if (!preg_match('/^DESC:/', $line)){
220 print_red (_("No DESC tag in vacation file:")." $file");
221 return $desc;
222 }
223 fclose ($fh);
225 $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
226 }
228 return $desc;
229 }
232 function execute()
233 {
234 /* Call parent execute */
235 plugin::execute();
237 /* Load templating engine */
238 $smarty= get_smarty();
239 $display= "";
241 $mailserver= array();
242 foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
243 $mailserver[]= $key;
244 }
246 /* Do we need to flip is_account state? */
247 if (isset($_POST['modify_state'])){
248 $this->is_account= !$this->is_account;
249 }
251 /* Show main page */
252 $mailserver= array();
253 foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
254 $mailserver[]= $key;
255 }
257 /* Do we represent a valid account? */
258 if (!$this->is_account && $this->parent == NULL){
259 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
260 _("This account has no mail extensions.")."</b>";
262 $display.= back_to_main();
263 return ($display);
264 }
266 /* Show tab dialog headers */
267 if ($this->parent != NULL){
268 if ($this->is_account){
269 $display= $this->show_header(_("Remove mail account"),
270 _("This account has mail features enabled. You can disable them by clicking below."));
271 } else {
272 $display= $this->show_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
273 return ($display);
274 }
275 }
277 /* Trigger forward add dialog? */
278 if (isset($_POST['add_local_forwarder'])){
279 $this->forward_dialog= TRUE;
280 $this->dialog= TRUE;
281 }
283 /* Cancel forward add dialog? */
284 if (isset($_POST['add_locals_cancel'])){
285 $this->forward_dialog= FALSE;
286 $this->dialog= FALSE;
287 }
289 /* Finished adding of locals? */
290 if (isset($_POST['add_locals_finish'])){
291 if (count ($_POST['local_list']) &&
292 chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
294 /* Walk through list of forwarders, ignore own addresses */
295 foreach ($_POST['local_list'] as $val){
296 if (!in_array ($val, $this->gosaMailAlternateAddress) &&
297 $val != $this->mail){
299 $this->addForwarder($val);
300 $this->is_modified= TRUE;
301 }
302 }
303 }
304 $this->forward_dialog= FALSE;
305 $this->dialog= FALSE;
306 }
308 /* Add forward email addresses */
309 if (isset($_POST['add_forwarder'])){
310 if ($_POST['forward_address'] != ""){
312 /* Valid email address specified? */
313 $address= $_POST['forward_address'];
314 $valid= FALSE;
315 if (!is_email($address)){
316 if (!is_email($address, TRUE)){
317 if ($this->is_template){
318 $valid= TRUE;
319 } else {
320 print_red (_("You're trying to add an invalid email address to the list of forwarders."));
321 }
322 }
323 } elseif ($address == $this->mail
324 || in_array($address, $this->gosaMailAlternateAddress)) {
326 print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
328 } else {
329 $valid= TRUE;
330 }
332 if ($valid){
333 /* Add it */
334 if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
335 $this->addForwarder ($address);
336 $this->is_modified= TRUE;
337 }
339 }
340 }
341 }
343 /* Delete forward email addresses */
344 if (isset($_POST['delete_forwarder'])){
345 if (count($_POST['forwarder_list'])
346 && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
348 $this->delForwarder ($_POST['forwarder_list']);
349 }
350 }
352 /* Add alternate email addresses */
353 if (isset($_POST['add_alternate'])){
354 if ($_POST['alternate_address'] != "" &&
355 chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
357 $valid= FALSE;
358 if (!is_email($_POST['alternate_address'])){
359 if ($this->is_template){
360 if (!(is_email($_POST['alternate_address'], TRUE))){
361 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
362 } else {
363 $valid= TRUE;
364 }
365 } else {
366 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
367 }
369 } else {
370 $valid= TRUE;
371 }
373 if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
374 $ui= get_userinfo();
375 if ($user != $ui->username){
376 print_red (_("The address you're trying to add is already used by user")." '$user'.");
377 }
378 }
379 }
380 }
382 /* Delete alternate email addresses */
383 if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
384 if (count($_POST['alternates_list']) &&
385 chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
387 $this->delAlternate ($_POST['alternates_list']);
388 }
389 }
391 /* Import vacation message? */
392 if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
393 $contents= "";
394 $lines= file($_POST["vacation_template"]);
395 foreach ($lines as $line){
396 if (!preg_match('/^DESC:/', $line)){
397 $contents.= $line;
398 }
399 }
401 /* Replace attributes */
402 $attrs= $this->parent->by_object['user']->attributes;
403 foreach ($attrs as $val){
404 $contents= preg_replace("/%$val/",
405 $this->parent->by_object['user']->$val, $contents);
406 }
408 /* Save message */
409 $this->gosaVacationMessage= htmlspecialchars($contents);
410 }
412 /* Show forward add dialog */
413 if ($this->forward_dialog){
414 $ldap= $this->config->get_ldap_link();
416 /* Save data */
417 $mailfilter= get_global("mailfilter");
418 foreach( array("depselect", "muser", "regex") as $type){
419 if (isset($_POST[$type])){
420 $mailfilter[$type]= $_POST[$type];
421 }
422 }
423 if (isset($_GET['search'])){
424 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
425 if ($s == "**"){
426 $s= "*";
427 }
428 $mailfilter['regex']= $s;
429 }
430 register_global("mailfilter", $mailfilter);
432 /* Get actual list */
433 $mailusers= array ();
434 if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
435 $regex= $mailfilter['regex'];
436 $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
437 } else {
438 $filter= "";
439 }
440 if ($mailfilter['muser'] != ""){
441 $user= $mailfilter['muser'];
442 $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
443 }
445 /* Add already present people to the filter */
446 $exclude= "";
447 foreach ($this->gosaMailForwardingAddress as $mail){
448 $exclude.= "(mail=$mail)";
449 }
450 if ($exclude != ""){
451 $filter.= "(!(|$exclude))";
452 }
454 $acl= array($this->config->current['BASE'] => ":all");
455 $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'],
456 array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
457 $ldap->cd($mailfilter['depselect']);
458 $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
459 error_reporting (0);
460 while ($attrs= $ldap->fetch()){
461 if(preg_match('/%/', $attrs['mail'][0])){
462 continue;
463 }
464 $name= $this->make_name($attrs);
465 $mailusers[$attrs['mail'][0]]= $name."<".
466 $attrs['mail'][0].">";
467 }
468 error_reporting (E_ALL);
469 natcasesort ($mailusers);
470 reset ($mailusers);
472 /* Show dialog */
473 $smarty->assign("search_image", get_template_path('images/search.png'));
474 $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
475 $smarty->assign("tree_image", get_template_path('images/tree.png'));
476 $smarty->assign("infoimage", get_template_path('images/info.png'));
477 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
478 $smarty->assign("mailusers", $mailusers);
479 if (isset($_POST['depselect'])){
480 $smarty->assign("depselect", $_POST['depselect']);
481 }
482 $smarty->assign("deplist", $this->config->idepartments);
483 $smarty->assign("apply", apply_filter());
484 $smarty->assign("alphabet", generate_alphabet());
485 $smarty->assign("hint", print_sizelimit_warning());
486 foreach( array("depselect", "muser", "regex") as $type){
487 $smarty->assign("$type", $mailfilter[$type]);
488 }
489 $smarty->assign("hint", print_sizelimit_warning());
491 $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
492 return ($display);
493 }
495 $smarty->assign("mailServers", $mailserver);
496 foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
497 "gosaMailAlternateAddress", "gosaMailForwardingAddress",
498 "gosaVacationMessage", "gosaMailDeliveryMode",
499 "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
501 $smarty->assign("$val", $this->$val);
502 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
503 }
505 if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
506 $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
507 $smarty->assign("quotadefined", "true");
508 } else {
509 $smarty->assign("quotadefined", "false");
510 }
512 /* Disable mail field if needed */
513 $method= new $this->method($this->config);
514 if ($method->uattrib == "mail" && $this->initially_was_account){
515 $smarty->assign("mailACL", "disabled");
516 }
518 /* Fill checkboxes */
519 if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
520 $smarty->assign("drop_own_mails", "checked");
521 } else {
522 $smarty->assign("drop_own_mails", "");
523 }
524 if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
525 $smarty->assign("use_vacation", "checked");
526 } else {
527 $smarty->assign("use_vacation", "");
528 }
529 if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
530 $smarty->assign("use_spam_filter", "checked");
531 } else {
532 $smarty->assign("use_spam_filter", "");
533 }
534 if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
535 $smarty->assign("use_mailsize_limit", "checked");
536 } else {
537 $smarty->assign("use_mailsize_limit", "");
538 }
539 if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
540 $smarty->assign("only_local", "checked");
541 } else {
542 $smarty->assign("only_local", "");
543 }
544 if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
545 $smarty->assign("own_script", "checked");
546 } else {
547 $smarty->assign("own_script", "");
548 }
550 /* Have vacation templates? */
551 $smarty->assign("template", "");
552 if (count($this->vacation)){
553 $smarty->assign("show_templates", "true");
554 $smarty->assign("vacationtemplates", $this->vacation);
555 if (isset($_POST['vacation_template'])){
556 $smarty->assign("template", $_POST['vacation_template']);
557 }
558 } else {
559 $smarty->assign("show_templates", "false");
560 }
562 /* Fill spam selector */
563 $spamlevel= array();
564 for ($i= 0; $i<21; $i++){
565 $spamlevel[]= $i;
566 }
567 $smarty->assign("spamlevel", $spamlevel);
568 $smarty->assign("spambox", $this->mailboxList);
569 $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));
570 $smarty->assign("only_localACL", chkacl($this->acl, "only_local"));
572 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
573 return ($display);
574 }
577 /* remove object from parent */
578 function remove_from_parent()
579 {
580 /* Cancel if there's nothing to do here */
581 if (!$this->initially_was_account){
582 return;
583 }
585 /* include global link_info */
586 $ldap= $this->config->get_ldap_link();
588 /* Remove and write to LDAP */
589 plugin::remove_from_parent();
591 /* Zero arrays */
592 $this->attrs['gosaMailAlternateAddress']= array();
593 $this->attrs['gosaMailForwardingAddress']= array();
595 /* Adapt attributes if needed */
596 $method= new $this->method($this->config);
597 $method->fixAttributesOnRemove($this);
599 /* Mailmethod wants us to remove the entry from LDAP. Keep uid! */
600 #fixme: || kolab || is differs here, you can't delete all attrs specified in this plugin ....
601 #fixme: there are some attributes we have to keep, i think.
602 unset ($this->attrs['uid']);
606 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
607 $this->attributes, "Save");
608 $ldap->cd($this->dn);
609 $this->cleanup();
610 $ldap->modify ($this->attrs);
612 show_ldap_error($ldap->get_error(), _("Removing mail account failed"));
614 /* Connect to IMAP server for account deletion */
615 if ($this->gosaMailServer != ""){
616 $method= new $this->method($this->config);
617 $id= $method->uattrib;
618 if ($method->connect($this->gosaMailServer)){
620 /* Remove account from IMAP server */
621 $method->deleteMailbox($this->folder_prefix.$this->$id);
622 $method->disconnect();
623 }
624 }
626 /* Optionally execute a command after we're done */
627 $this->handle_post_events("remove");
628 }
631 /* Save data to object */
632 function save_object()
633 {
634 if (isset($_POST['mailTab'])){
635 /* Save ldap attributes */
636 plugin::save_object();
638 /* Assemble mail delivery mode
639 The mode field in ldap consists of values between braces, this must
640 be called when 'mail' is set, because checkboxes may not be set when
641 we're in some other dialog.
643 Example for gosaMailDeliveryMode [LR ]
644 L: Local delivery
645 R: Reject when exceeding mailsize limit
646 S: Use spam filter
647 V: Use vacation message
648 C: Use custm sieve script
649 I: Only insider delivery */
651 $tmp= "";
652 if (!isset($_POST["drop_own_mails"])){
653 $tmp.= "L";
654 }
655 if (isset($_POST["use_mailsize_limit"])){
656 $tmp.= "R";
657 }
658 if (isset($_POST["use_spam_filter"])){
659 $tmp.= "S";
660 }
661 if (isset($_POST["use_vacation"])){
662 $tmp.= "V";
663 }
664 if (isset($_POST["own_script"])){
665 $tmp.= "C";
666 }
667 if (isset($_POST["only_local"])){
668 $tmp.= "I";
669 }
670 $tmp= "[$tmp]";
672 if (chkacl ($this->acl, "gosaMailDeliveryMode") == ""){
673 if ($this->gosaMailDeliveryMode != $tmp){
674 $this->is_modified= TRUE;
675 }
676 $this->gosaMailDeliveryMode= $tmp;
677 }
678 }
679 }
682 /* Save data to LDAP, depending on is_account we save or delete */
683 function save()
684 {
685 $ldap= $this->config->get_ldap_link();
687 /* Call parents save to prepare $this->attrs */
688 plugin::save();
690 /* Save arrays */
691 $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
692 $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
694 /* Adapt attributes if needed */
695 $method= new $this->method($this->config);
696 $id= $method->uattrib;
697 $method->fixAttributesOnStore($this);
699 /* Remove Mailquota if = "" */
700 if((isset($this->attrs['gosaMailQuota']))&&($this->attrs['gosaMailQuota']=="")) {
701 $this->attrs['gosaMailQuota']=array();
702 }
704 if(empty($this->attrs['gosaSpamMailbox'])){
705 unset($this->attrs['gosaSpamMailbox']);
706 }
708 $this->attrs['mail'] = strtolower($this->attrs['mail']);
710 /* Save data to LDAP */
711 $ldap->cd($this->dn);
712 $this->cleanup();
713 $ldap->modify ($this->attrs);
715 show_ldap_error($ldap->get_error(), _("Saving mail account failed"));
717 /* Only do IMAP actions if we are not a template */
718 if (!$this->is_template){
720 if ($method->connect($this->gosaMailServer)){
721 $method->updateMailbox($this->folder_prefix.$this->$id);
722 $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
723 $method->disconnect();
725 /* Write sieve information only if not in C mode */
726 if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
727 $method->configureFilter($this->$id,
728 $this->gosaMailDeliveryMode,
729 $this->mail,
730 $this->gosaMailAlternateAddress,
731 $this->gosaMailMaxSize,
732 $this->gosaSpamMailbox,
733 $this->gosaSpamSortLevel,
734 $this->gosaVacationMessage);
735 }
736 }
737 }
739 /* Optionally execute a command after we're done */
740 if ($this->initially_was_account == $this->is_account){
741 if ($this->is_modified){
742 $this->handle_post_events("modify");
743 }
744 } else {
745 $this->handle_post_events("add");
746 }
748 }
750 /* Check formular input */
751 function check()
752 {
753 if(!$this->is_account) return(array());
754 $ldap= $this->config->get_ldap_link();
756 /* Call common method to give check the hook */
757 $message= plugin::check();
759 if(empty($this->gosaMailServer)){
760 $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
761 }
763 /* must: mail */
764 if ($this->mail == ""){
765 $message[]= _("The required field 'Primary address' is not set.");
766 }
767 if ($this->is_template){
768 if (!is_email($this->mail, TRUE)){
769 $message[]= _("Please enter a valid email address in 'Primary address' field.");
770 }
771 } else {
772 if (!is_email($this->mail)){
773 $message[]= _("Please enter a valid email address in 'Primary address' field.");
774 }
775 }
776 $ldap->cd($this->config->current['BASE']);
777 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
778 if ($ldap->count() != 0){
779 $message[]= _("The primary address you've entered is already in use.");
780 }
782 /* Check quota */
783 if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
784 if (!is_numeric($this->gosaMailQuota)) {
785 $message[]= _("Value in 'Quota size' is not valid.");
786 } else {
787 $this->gosaMailQuota= (int) $this->gosaMailQuota;
788 }
789 }
791 /* Check rejectsize for integer */
792 if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
793 if (!is_numeric($this->gosaMailMaxSize)){
794 $message[]= _("Please specify a vaild mail size for mails to be rejected.");
795 } else {
796 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
797 }
798 }
800 /* Need gosaMailMaxSize if use_mailsize_limit is checked */
801 if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) &&
802 $this->gosaMailMaxSize == ""){
804 $message[]= _("You need to set the maximum mail size in order to reject anything.");
805 }
807 if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
808 $message[]= _("You specified Spam settings, but there is no Folder specified.");
809 }
811 return ($message);
812 }
814 /* Adapt from template, using 'dn' */
815 function adapt_from_template($dn)
816 {
817 plugin::adapt_from_template($dn);
819 foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
820 $this->$val= array();
821 if (isset($this->attrs["$val"]["count"])){
822 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
823 $value= $this->attrs["$val"][$i];
824 foreach (array("sn", "givenName", "uid") as $repl){
825 if (preg_match("/%$repl/i", $value)){
826 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
827 }
828 }
829 array_push($this->$val, strtolower(rewrite($value)));
830 }
831 }
832 }
833 $this->mail= strtolower(rewrite($this->mail));
834 }
836 /* Add entry to forwarder list */
837 function addForwarder($address)
838 {
839 $this->gosaMailForwardingAddress[]= $address;
840 $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
842 sort ($this->gosaMailForwardingAddress);
843 reset ($this->gosaMailForwardingAddress);
844 $this->is_modified= TRUE;
845 }
847 /* Remove list of addresses from forwarder list */
848 function delForwarder($addresses)
849 {
850 $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
851 $this->is_modified= TRUE;
852 }
856 function addAlternate($address)
857 {
858 $ldap= $this->config->get_ldap_link();
860 $address= strtolower($address);
862 /* Is this address already assigned in LDAP? */
863 $ldap->cd ($this->config->current['BASE']);
864 $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
866 if ($ldap->count() > 0){
867 $attrs= $ldap->fetch ();
868 return ($attrs["uid"][0]);
869 }
871 /* Add to list of alternates */
872 if (!in_array($address, $this->gosaMailAlternateAddress)){
873 $this->gosaMailAlternateAddress[]= $address;
874 $this->is_modified= TRUE;
875 }
877 sort ($this->gosaMailAlternateAddress);
878 reset ($this->gosaMailAlternateAddress);
880 return ("");
881 }
884 function delAlternate($addresses)
885 {
886 $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
887 $this->gosaMailAlternateAddress);
888 $this->is_modified= TRUE;
889 }
891 function make_name($attrs)
892 {
893 $name= "";
894 if (isset($attrs['sn'][0])){
895 $name= $attrs['sn'][0];
896 }
897 if (isset($attrs['givenName'][0])){
898 if ($name != ""){
899 $name.= ", ".$attrs['givenName'][0];
900 } else {
901 $name.= $attrs['givenName'][0];
902 }
903 }
904 if ($name != ""){
905 $name.= " ";
906 }
908 return ($name);
909 }
912 /* Create the mail part for the copy & paste dialog */
913 function getCopyDialog()
914 {
915 if(!$this->is_account) return("");
916 $smarty = get_smarty();
917 $smarty->assign("mail",$this->mail);
918 $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
919 $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
920 $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
922 $ret = array();
923 $ret['status'] = "";
924 $ret['string'] = $str;
925 return($ret);
926 }
928 function saveCopyDialog()
929 {
930 if(!$this->is_account) return;
932 /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
933 $this->execute();
935 if(isset($_POST['mail'])){
936 $this->mail = $_POST['mail'];
937 }
939 }
940 }
942 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
943 ?>