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'][0];
75 }
77 if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
78 $this->gosaMailServer = $this->gosaMailServer[0];
79 }
81 /* Save initial account state */
82 $this->initially_was_account= $this->is_account;
84 /* Set mailMethod to the one defined in gosa.conf */
85 if (isset($this->config->current['MAILMETHOD'])){
86 $method= $this->config->current['MAILMETHOD'];
87 if (class_exists("mailMethod$method")){
88 $this->method= "mailMethod$method";
89 } else {
90 print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
91 }
92 }
95 /* Create the account prefix user. user/
96 Preset folder prefix. Will change it later to respect
97 altnamespace. */
98 if (isset($this->config->current['CYRUSUNIXSTYLE']) && $this->config->current['CYRUSUNIXSTYLE'] == "true"){
99 $this->folder_prefix= "user/";
100 }elseif (isset($this->config->data['MAIN']['CYRUSUNIXSTYLE']) && $this->config->data['MAIN']['CYRUSUNIXSTYLE'] == "true"){
101 $this->folder_prefix= "user/";
102 } else {
103 $this->folder_prefix= "user.";
104 }
106 /* This is not a new account, parse additional attributes */
107 if (($dn != NULL) && ($dn != "new") && $this->is_account){
109 /* Load attributes containing arrays */
110 foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
111 $this->$val= array();
112 if (isset($this->attrs["$val"]["count"])){
113 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
114 array_push($this->$val, $this->attrs["$val"][$i]);
115 }
116 }
117 }
120 /* Only do IMAP actions if gosaMailServer attribute is set */
121 if (isset ($this->attrs["gosaMailServer"][0])){
123 $method = new $this->method($this->config);
124 $id = $method->uattrib;
126 /* Adapt attributes if needed */
127 $method->fixAttributesOnLoad($this);
129 /* FixAttributesOnLoad possibly creates an array out of gosaMailServer.
130 If the mail tab wasn't opened once before saving, the account can't be saved */
131 if(is_array($this->gosaMailServer)){
132 $this->gosaMailServer = $this->gosaMailServer[0];
133 }
135 if ($method->connect($this->attrs["gosaMailServer"][0])){
137 /* Update quota values */
138 $quota= $method->getQuota($this->folder_prefix.$this->$id);
140 if($quota){
141 if ($quota['gosaMailQuota'] == 2147483647){
142 $this->quotaUsage = "";
143 $this->gosaMailQuota = "";
144 } else {
145 $this->quotaUsage = $quota['quotaUsage'];
146 $this->gosaMailQuota = $quota['gosaMailQuota'];
147 }
148 }else{
149 $this->quotaUsage = "";
150 $this->gosaMailQuota = "";
151 // print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
152 }
154 /* Get mailboxes / folder like INBOX ..*/
155 $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
157 $method->disconnect();
158 }else{
159 /* Could not connect to ldap.
160 */
161 if (isset($this->attrs['gosaMailQuota'][0])){
162 $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
163 }
164 }
165 }
166 }
168 /* Fill vacation array */
169 $this->vacation= array();
170 if (isset($this->config->current['VACATIONDIR'])){
171 $dir= $this->config->current['VACATIONDIR'];
172 if (is_dir($dir) && is_readable($dir)){
174 /* Look for files and build the vacation array */
175 $dh= opendir($dir);
176 while ($file = readdir($dh)){
177 $description= $this->parse_vacation("$dir/$file");
178 if ($description != ""){
179 $this->vacation["$dir/$file"]= $description;
180 }
181 }
182 closedir($dh);
183 }
184 }
187 /* Create filter */
189 /* Get global filter config */
190 if (!is_global("mailfilter")){
191 $ui= get_userinfo();
192 $base= get_base_from_people($ui->dn);
193 $mailfilter= array( "depselect" => $base,
194 "muser" => "",
195 "regex" => "*");
196 register_global("mailfilter", $mailfilter);
197 }
198 }
201 function parse_vacation($file)
202 {
203 $desc= "";
205 if (is_file($file)){
206 $fh = fopen($file, "r");
207 $line= fgets($fh, 256);
209 if (!preg_match('/^DESC:/', $line)){
210 print_red (_("No DESC tag in vacation file:")." $file");
211 return $desc;
212 }
213 fclose ($fh);
215 $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
216 }
218 return $desc;
219 }
222 function execute()
223 {
224 /* Call parent execute */
225 plugin::execute();
227 /* Initialise vars */
229 /* Load templating engine */
230 $smarty= get_smarty();
231 $display= "";
233 /* Get available mailserver */
234 $mailserver= array();
235 foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
236 $mailserver[]= $key;
237 }
239 /* Handle account state */
240 /* Do we need to flip is_account state? */
241 if(isset($_POST['modify_state'])){
242 if($this->is_account && $this->acl_is_removeable()){
243 $this->is_account= FALSE;
244 }elseif(!$this->is_account && $this->acl_is_createable()){
245 $this->is_account= TRUE;
246 }
247 }
249 /* Do we represent a valid account? */
250 if (!$this->is_account && $this->parent == NULL){
251 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
252 _("This account has no mail extensions.")."</b>";
254 $display.= back_to_main();
255 return ($display);
256 }
258 /* Show tab dialog headers */
259 if ($this->parent != NULL){
260 if ($this->is_account){
261 if($this->accountDelegationsConfigured()){
262 $display= $this->show_disable_header(_("Remove mail account"),
263 _("This account can't be removed while there are delegations configured. Remove those delegations first."),TRUE,TRUE);
264 }else{
265 $display= $this->show_disable_header(_("Remove mail account"),
266 _("This account has mail features enabled. You can disable them by clicking below."));
267 }
268 } else {
269 $display= $this->show_enable_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
270 return ($display);
271 }
272 }
275 /* Forwarder subdialog */
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'])){
292 /* Check if we are able to write gosaMailForwardingAddress */
293 if($this->acl_is_writeable("gosaMailForwardingAddress")){
295 /* Walk through list of forwarders, ignore own addresses */
296 foreach ($_POST['local_list'] as $val){
297 if (!in_array ($val, $this->gosaMailAlternateAddress) &&
298 $val != $this->mail){
300 $this->addForwarder($val);
301 $this->is_modified= TRUE;
302 }
303 }
304 }
305 $this->forward_dialog= FALSE;
306 $this->dialog= FALSE;
307 }
309 /* Add forward email addresses */
310 if (isset($_POST['add_forwarder'])){
311 if ($_POST['forward_address'] != ""){
313 /* Valid email address specified? */
314 $address= $_POST['forward_address'];
315 $valid= FALSE;
316 if (!is_email($address)){
317 if (!is_email($address, TRUE)){
318 if ($this->is_template){
319 $valid= TRUE;
320 } else {
321 print_red (_("You're trying to add an invalid email address to the list of forwarders."));
322 }
323 }
324 } elseif ($address == $this->mail
325 || in_array($address, $this->gosaMailAlternateAddress)) {
327 print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
329 } else {
330 $valid= TRUE;
331 }
333 if ($valid){
335 /* Add it, if we are able to write gosaMailForwardingAddress */
336 if($this->acl_is_writeable("gosaMailForwardingAddress")){
337 $this->addForwarder ($address);
338 $this->is_modified= TRUE;
339 }
340 }
341 }
342 }
344 /* Delete forward email addresses */
345 if (isset($_POST['delete_forwarder'])){
346 $this->delForwarder ($_POST['forwarder_list']);
347 }
350 /* Add alternate email addresses */
351 if (isset($_POST['add_alternate'])){
353 $valid= FALSE;
354 if (!is_email($_POST['alternate_address'])){
355 if ($this->is_template){
356 if (!(is_email($_POST['alternate_address'], TRUE))){
357 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
358 } else {
359 $valid= TRUE;
360 }
361 } else {
362 print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
363 }
365 } else {
366 $valid= TRUE;
367 }
369 if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
370 $ui= get_userinfo();
371 if ($user != $ui->username){
372 print_red (_("The address you're trying to add is already used by user")." '$user'.");
373 }
374 }
375 }
377 /* Delete alternate email addresses */
378 if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
379 $this->delAlternate ($_POST['alternates_list']);
380 }
383 /* Vacation message */
385 /* Import vacation message? */
386 if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
387 $contents= "";
388 $lines= file($_POST["vacation_template"]);
389 foreach ($lines as $line){
390 if (!preg_match('/^DESC:/', $line)){
391 $contents.= $line;
392 }
393 }
395 /* Replace attributes */
396 $attrs= $this->parent->by_object['user']->attributes;
397 foreach ($attrs as $val){
398 $contents= preg_replace("/%$val/",
399 $this->parent->by_object['user']->$val, $contents);
400 }
402 /* Save message */
403 $this->gosaVacationMessage= htmlspecialchars($contents);
404 }
407 /* Display forward dialog if requested above */
409 /* Show forward add dialog */
410 if ($this->forward_dialog){
411 $ldap= $this->config->get_ldap_link();
413 /* Save data */
414 $mailfilter= get_global("mailfilter");
415 foreach( array("depselect", "muser", "regex") as $type){
416 if (isset($_POST[$type])){
417 $mailfilter[$type]= $_POST[$type];
418 }
419 }
420 if (isset($_GET['search'])){
421 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
422 if ($s == "**"){
423 $s= "*";
424 }
425 $mailfilter['regex']= $s;
426 }
427 register_global("mailfilter", $mailfilter);
429 /* Get actual list */
430 $mailusers= array ();
431 if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
432 $regex= $mailfilter['regex'];
433 $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
434 } else {
435 $filter= "";
436 }
437 if ($mailfilter['muser'] != ""){
438 $user= $mailfilter['muser'];
439 $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
440 }
442 /* Add already present people to the filter */
443 $exclude= "";
444 foreach ($this->gosaMailForwardingAddress as $mail){
445 $exclude.= "(mail=$mail)";
446 }
447 if ($exclude != ""){
448 $filter.= "(!(|$exclude))";
449 }
451 $acl= array($this->config->current['BASE'] => ":all");
452 $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'],
453 array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
454 $ldap->cd($mailfilter['depselect']);
455 $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
456 error_reporting (0);
457 while ($attrs= $ldap->fetch()){
458 if(preg_match('/%/', $attrs['mail'][0])){
459 continue;
460 }
461 $name= $this->make_name($attrs);
462 $mailusers[$attrs['mail'][0]]= $name."<".
463 $attrs['mail'][0].">";
464 }
465 error_reporting (E_ALL);
466 natcasesort ($mailusers);
467 reset ($mailusers);
469 /* Show dialog */
470 $smarty->assign("search_image", get_template_path('images/search.png'));
471 $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
472 $smarty->assign("tree_image", get_template_path('images/tree.png'));
473 $smarty->assign("infoimage", get_template_path('images/info.png'));
474 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
475 $smarty->assign("mailusers", $mailusers);
476 if (isset($_POST['depselect'])){
477 $smarty->assign("depselect", $_POST['depselect']);
478 }
479 $smarty->assign("deplist", $this->config->idepartments);
480 $smarty->assign("apply", apply_filter());
481 $smarty->assign("alphabet", generate_alphabet());
482 $smarty->assign("hint", print_sizelimit_warning());
483 foreach( array("depselect", "muser", "regex") as $type){
484 $smarty->assign("$type", $mailfilter[$type]);
485 }
486 $smarty->assign("hint", print_sizelimit_warning());
488 $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
489 return ($display);
490 }
492 /* Display mail account tab */
494 $smarty->assign("mailServers", $mailserver);
495 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
497 $tmp = $this->plInfo();
498 foreach($tmp['plProvidedAcls'] as $name => $transl){
499 $smarty->assign("$name"."ACL", $this->getacl($name,$SkipWrite));
500 }
502 foreach(array("gosaMailServer", "gosaMailQuota", "perms", "mail",
503 "gosaMailAlternateAddress", "gosaMailForwardingAddress",
504 "gosaVacationMessage", "gosaMailDeliveryMode",
505 "gosaMailMaxSize", "gosaSpamSortLevel", "gosaSpamMailbox") as $val){
506 $smarty->assign("$val", $this->$val);
507 }
509 if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
510 $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
511 $smarty->assign("quotadefined", "true");
512 } else {
513 $smarty->assign("quotadefined", "false");
514 }
516 /* Disable mail field if needed */
517 $method= new $this->method($this->config);
518 if ($method->uattrib == "mail" && $this->initially_was_account){
519 $smarty->assign("mailACL", preg_replace("/w/","",$this->getacl("mail",$SkipWrite)));
520 }
523 if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
524 $smarty->assign("drop_own_mails", "checked");
525 } else {
526 $smarty->assign("drop_own_mails", "");
527 }
529 $types = array(
530 "V"=>"use_vacation",
531 "S"=>"use_spam_filter",
532 "R"=>"use_mailsize_limit",
533 "I"=>"only_local",
534 "C"=>"own_script");
536 /* Fill checkboxes */
537 foreach($types as $option => $varname){
538 if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
539 $smarty->assign($varname, "checked");
540 } else {
541 $smarty->assign($varname, "");
542 }
543 }
545 /* Have vacation templates? */
546 $smarty->assign("template", "");
547 if (count($this->vacation)){
548 $smarty->assign("show_templates", "true");
549 $smarty->assign("vacationtemplates", $this->vacation);
550 if (isset($_POST['vacation_template'])){
551 $smarty->assign("template", $_POST['vacation_template']);
552 }
553 } else {
554 $smarty->assign("show_templates", "false");
555 }
557 /* Fill spam selector */
558 $spamlevel= array();
559 for ($i= 0; $i<21; $i++){
560 $spamlevel[]= $i;
561 }
562 $smarty->assign("spamlevel", $spamlevel);
563 $smarty->assign("spambox", $this->mailboxList);
565 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
566 return ($display);
567 }
570 /* remove object from parent */
571 function remove_from_parent()
572 {
573 /* Cancel if there's nothing to do here */
574 if (!$this->initially_was_account){
575 return;
576 }
578 /* include global link_info */
579 $ldap= $this->config->get_ldap_link();
581 /* Remove and write to LDAP */
582 plugin::remove_from_parent();
584 /* Zero arrays */
585 $this->attrs['gosaMailAlternateAddress']= array();
586 $this->attrs['gosaMailForwardingAddress']= array();
588 /* Adapt attributes if needed */
589 $method= new $this->method($this->config);
590 $method->fixAttributesOnRemove($this);
592 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
593 $ldap->cd($this->dn);
594 $this->cleanup();
595 $ldap->modify ($this->attrs);
597 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/mail account with dn '%s' failed."),$this->dn));
599 /* Connect to IMAP server for account deletion */
600 if ($this->gosaMailServer != ""){
601 $method= new $this->method($this->config);
602 $id= $method->uattrib;
603 if ($method->connect($this->gosaMailServer)){
605 /* Remove account from IMAP server */
606 $method->deleteMailbox($this->folder_prefix.$this->$id);
607 $method->disconnect();
608 }
609 }
611 /* Optionally execute a command after we're done */
612 $this->handle_post_events("remove");
613 }
616 /* check if we have some delegations configured, those delegations must be removed first */
617 function accountDelegationsConfigured()
618 {
619 /* We are in administrational edit mode.
620 Check tab configurations directly */
621 if(isset($this->attrs)){
622 $checkArray = array("kolabInvitationPolicy","unrestrictedMailSize", "calFBURL","kolabDelegate","kolabFreeBusyFuture");
623 foreach($checkArray as $index){
624 if(isset($this->attrs[$index])){
625 return(true);
626 }
627 }
628 }
629 return(false);
630 }
633 /* Save data to object */
634 function save_object()
635 {
636 if (isset($_POST['mailTab'])){
637 /* Save ldap attributes */
638 plugin::save_object();
640 /* Assemble mail delivery mode
641 The mode field in ldap consists of values between braces, this must
642 be called when 'mail' is set, because checkboxes may not be set when
643 we're in some other dialog.
645 Example for gosaMailDeliveryMode [LR ]
646 L: Local delivery
647 R: Reject when exceeding mailsize limit
648 S: Use spam filter
649 V: Use vacation message
650 C: Use custm sieve script
651 I: Only insider delivery */
653 $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
656 /* Handle delivery flags */
657 if($this->acl_is_writeable("gosaMailDeliveryModeL")){
658 if(!preg_match("/L/",$tmp) && !isset($_POST['drop_own_mails'])){
659 $tmp.="L";
660 }elseif(preg_match("/L/",$tmp) && isset($_POST['drop_own_mails'])){
661 $tmp = preg_replace("/L/","",$tmp);
662 }
663 }
665 $opts = array(
666 "R" => "use_mailsize_limit",
667 "S" => "use_spam_filter",
668 "V" => "use_vacation",
669 "C" => "own_script",
670 "I" => "only_local");
672 foreach($opts as $flag => $post){
673 if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
674 if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
675 $tmp.= $flag;
676 }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
677 $tmp = preg_replace("/".$flag."/","",$tmp);
678 }
679 }
680 }
682 $tmp= "[$tmp]";
683 if ($this->gosaMailDeliveryMode != $tmp){
684 $this->is_modified= TRUE;
685 }
686 $this->gosaMailDeliveryMode= $tmp;
687 }
688 }
691 /* Save data to LDAP, depending on is_account we save or delete */
692 function save()
693 {
694 $ldap= $this->config->get_ldap_link();
696 /* Call parents save to prepare $this->attrs */
697 plugin::save();
699 /* Save arrays */
700 $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
701 $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
703 /* Adapt attributes if needed */
704 $method= new $this->method($this->config);
705 $id= $method->uattrib;
707 $method->fixAttributesOnStore($this);
709 /* Remove Mailquota if = "" or "0" */
710 if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
711 $this->attrs['gosaMailQuota']=0;
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(), sprintf(_("Saving of user/mail account with dn '%s' failed."),$this->dn));
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);
733 $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
734 $method->disconnect();
736 /* Write sieve information only if not in C mode */
737 if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
738 $method->configureFilter($this->$id,
739 $this->gosaMailDeliveryMode,
740 $this->mail,
741 $this->gosaMailAlternateAddress,
742 $this->gosaMailMaxSize,
743 $this->gosaSpamMailbox,
744 $this->gosaSpamSortLevel,
745 $this->gosaVacationMessage);
746 }
747 }
748 }
750 /* Optionally execute a command after we're done */
751 if ($this->initially_was_account == $this->is_account){
752 if ($this->is_modified){
753 $this->handle_post_events("modify");
754 }
755 } else {
756 $this->handle_post_events("add");
757 }
759 }
762 /* Check formular input */
763 function check()
764 {
765 if(!$this->is_account) return(array());
767 $ldap= $this->config->get_ldap_link();
769 /* Call common method to give check the hook */
770 $message= plugin::check();
772 if(empty($this->gosaMailServer)){
773 $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
774 }
776 /* must: mail */
777 if ($this->mail == ""){
778 $message[]= _("The required field 'Primary address' is not set.");
779 }
780 if ($this->is_template){
781 if (!is_email($this->mail, TRUE)){
782 $message[]= _("Please enter a valid email address in 'Primary address' field.");
783 }
784 } else {
785 if (!is_email($this->mail)){
786 $message[]= _("Please enter a valid email address in 'Primary address' field.");
787 }
788 }
789 $ldap->cd($this->config->current['BASE']);
790 $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
791 if ($ldap->count() != 0){
792 $message[]= _("The primary address you've entered is already in use.");
793 }
795 /* Check quota */
796 if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
797 if (!is_numeric($this->gosaMailQuota)) {
798 $message[]= _("Value in 'Quota size' is not valid.");
799 } else {
800 $this->gosaMailQuota= (int) $this->gosaMailQuota;
801 }
802 }
804 /* Check rejectsize for integer */
805 if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
806 if (!is_numeric($this->gosaMailMaxSize)){
807 $message[]= _("Please specify a vaild mail size for mails to be rejected.");
808 } else {
809 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
810 }
811 }
813 /* Need gosaMailMaxSize if use_mailsize_limit is checked */
814 if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) &&
815 $this->gosaMailMaxSize == ""){
817 $message[]= _("You need to set the maximum mail size in order to reject anything.");
818 }
820 if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
821 $message[]= _("You specified Spam settings, but there is no Folder specified.");
822 }
824 return ($message);
825 }
828 /* Adapt from template, using 'dn' */
829 function adapt_from_template($dn)
830 {
831 plugin::adapt_from_template($dn);
833 foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
834 $this->$val= array();
835 if (isset($this->attrs["$val"]["count"])){
836 for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
837 $value= $this->attrs["$val"][$i];
838 foreach (array("sn", "givenName", "uid") as $repl){
839 if (preg_match("/%$repl/i", $value)){
840 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
841 }
842 }
843 array_push($this->$val, strtolower(rewrite($value)));
844 }
845 }
846 }
847 $this->mail= strtolower(rewrite($this->mail));
848 }
851 /* Add entry to forwarder list */
852 function addForwarder($address)
853 {
854 if($this->acl_is_writeable("gosaMailForwardingAddress")){
855 $this->gosaMailForwardingAddress[]= $address;
856 $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
857 sort ($this->gosaMailForwardingAddress);
858 reset ($this->gosaMailForwardingAddress);
859 $this->is_modified= TRUE;
860 }else{
861 print_red(_("You are not allowed to write mail forwarding."));
862 }
863 }
866 /* Remove list of addresses from forwarder list */
867 function delForwarder($addresses)
868 {
869 if($this->acl_is_writeable("gosaMailForwardingAddress")){
870 $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
871 $this->is_modified= TRUE;
872 }else{
873 print_red(_("You are not allowed to write mail forwarding."));
874 }
875 }
878 /* Add given mail address to the list of alternate adresses ,
879 check if this mal address is used, skip adding in this case */
880 function addAlternate($address)
881 {
882 if($this->acl_is_writeable("gosaMailAlternateAddress")){
883 $ldap= $this->config->get_ldap_link();
884 $address= strtolower($address);
886 /* Is this address already assigned in LDAP? */
887 $ldap->cd ($this->config->current['BASE']);
888 $ldap->search ("(&(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
890 if ($ldap->count() > 0){
891 $attrs= $ldap->fetch ();
892 return ($attrs["uid"][0]);
893 }
895 /* Add to list of alternates */
896 if (!in_array($address, $this->gosaMailAlternateAddress)){
897 $this->gosaMailAlternateAddress[]= $address;
898 $this->is_modified= TRUE;
899 }
901 sort ($this->gosaMailAlternateAddress);
902 reset ($this->gosaMailAlternateAddress);
903 return ("");
904 }else{
905 print_red(_("You are not allowed to write mail alternate address."));
906 }
907 }
910 function delAlternate($addresses)
911 {
912 if($this->acl_is_writeable("gosaMailAlternateAddress")){
913 $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
914 $this->is_modified= TRUE;
915 }else{
916 print_red(_("You are not allowed to write mail alternate address."));
917 }
918 }
920 function make_name($attrs)
921 {
922 $name= "";
923 if (isset($attrs['sn'][0])){
924 $name= $attrs['sn'][0];
925 }
926 if (isset($attrs['givenName'][0])){
927 if ($name != ""){
928 $name.= ", ".$attrs['givenName'][0];
929 } else {
930 $name.= $attrs['givenName'][0];
931 }
932 }
933 if ($name != ""){
934 $name.= " ";
935 }
937 return ($name);
938 }
941 /* Create the mail part for the copy & paste dialog */
942 function getCopyDialog()
943 {
944 if(!$this->is_account) return("");
945 $smarty = get_smarty();
946 $smarty->assign("mail",$this->mail);
947 $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
948 $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
949 $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
951 $ret = array();
952 $ret['status'] = "";
953 $ret['string'] = $str;
954 return($ret);
955 }
957 function saveCopyDialog()
958 {
959 if(!$this->is_account) return;
961 /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
962 $this->execute();
964 if(isset($_POST['mail'])){
965 $this->mail = $_POST['mail'];
966 }
968 }
970 function allow_remove()
971 {
972 if (isset($this->config->current['MAILMETHOD'])){
973 $method= $this->config->current['MAILMETHOD'];
974 if(preg_match("/kolab/i",$method)){
975 $ldap = $this->config->get_ldap_link();
976 $ldap->cd($this->config->current['BASE']);
977 $ldap->cat($this->dn);
978 if($ldap->count()){
979 $attrs = $ldap->fetch();
980 if(isset($attrs['kolabDeleteFlag'])){
981 return(_("Waiting for kolab to remove mail properties."));
982 }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
983 return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
984 }
985 }
986 }
987 }
988 }
991 function plInfo()
992 {
993 return (array(
994 "plShortName" => _("Mail"),
995 "plDescription" => _("Mail settings"),
996 "plSelfModify" => TRUE,
997 "plDepends" => array("user"), // This plugin depends on
998 "plPriority" => 4, // Position in tabs
999 "plSection" => "personal", // This belongs to personal
1000 "plCategory" => array("users"), // Add to following categories
1001 "plOptions" => array(),
1003 "plProvidedAcls" => array(
1004 "mail" => _("Mail address"),
1005 "gosaMailServer" => _("Mail server"),
1006 "gosaMailQuota" => _("Quota size"),
1008 "gosaMailDeliveryModeV" => _("Add vacation information"), // This is flag of gosaMailDeliveryMode
1009 "gosaVacationMessage" => _("Vacation message"),
1011 "gosaMailDeliveryModes" => _("Use spam filter"), // This is flag of gosaMailDeliveryMode
1012 "gosaSpamSortLevel" => _("Spam level"),
1013 "gosaSpamMailbox" => _("Spam mail box"),
1015 "gosaMailDeliveryModeR" => _("Reject due to mailsize"), // This is flag of gosaMailDeliveryMode
1016 "gosaMailMaxSize" => _("Mail max size"),
1018 "gosaMailForwardingAddress" => _("Forwarding address"),
1019 "gosaMailDeliveryModeL" => _("Local delivery"), // This is flag of gosaMailDeliveryMode
1020 "gosaMailDeliveryModeI" => _("Only insider delivery"), // This is flag of gosaMailDeliveryMode
1021 "gosaMailAlternateAddress" => _("Mail alternative addresses"),
1023 "gosaMailForwardingAddress" => _("Forwarding address"),
1024 "gosaMailDeliveryModeC" => _("Use custom sieve script")) // This is flag of gosaMailDeliveryMode
1025 ));
1026 }
1027 }
1029 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1030 ?>