Code

Fixed replacing of dateOfBirth in vacation templates
[gosa.git] / plugins / personal / mail / class_mailAccount.inc
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   /* plugin specific values */
27   var $mail                               = "";
28   var $uid                                = "";
29   var $gosaMailAlternateAddress           = array();
30   var $gosaMailForwardingAddress          = array();
31   var $gosaMailDeliveryMode               = "[L        ]";
32   var $gosaMailServer                     = "";
33   var $gosaMailQuota                      = "";
34   var $gosaMailMaxSize                    = "";
35   var $gosaVacationMessage                = "";
36   var $gosaSpamSortLevel                  = "";
37   var $gosaSpamMailbox                    = "";
39   var $quotaUsage                         = 0;
40   var $forward_dialog                     = FALSE;
41   var $folder_prefix                      = "";
42   var $mailboxList                        = array("INBOX");
43   var $default_permissions                = "none";
44   var $member_permissions                 = "post";
45   var $members                            = array();
46   var $admins                             = array();
47   var $vacations                          = array();
48   var $perms                              = array(  "lrs"       => "read", 
49                                                     "lrsp"      => "post", 
50                                                     "lrsip"     => "append",
51                                                     "lrswipcd"  => "write", 
52                                                     "lrswipcda" => "all" );
54   /* attribute list for save action */
55   var $attributes= array("mail", "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
56       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
57       "gosaVacationMessage", "gosaMailAlternateAddress", "gosaMailForwardingAddress");
58   var $objectclasses= array("gosaMailAccount");
61   /* constructor, if 'dn' is set, the node loads the given
62      'dn' from LDAP */
63   function mailAccount ($config, $dn= NULL, $parent= NULL)
64   {
65     /* Load bases attributes */
66     plugin::plugin($config, $dn, $parent);
68     if(isset($this->attrs['uid'])){
69       $this->uid = $this->attrs['uid'][0];
70     }
71  
72     if(is_array($this->gosaMailServer) && isset($this->gosaMailServer[0])){
73       $this->gosaMailServer = $this->gosaMailServer[0];
74     }
76     /* Save initial account state */
77     $this->initially_was_account= $this->is_account;
79     /*  Set mailMethod to the one defined in gosa.conf */
80     if (isset($this->config->current['MAILMETHOD'])){
81       $method= $this->config->current['MAILMETHOD'];
82       if (class_exists("mailMethod$method")){
83         $this->method= "mailMethod$method";
84       } else {
85         print_red(sprintf(_("There is no mail method '%s' specified in your gosa.conf available."), $method));
86       }
87     }
89     /* 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     }elseif (isset($this->config->data['MAIN']['CYRUSUNIXSTYLE']) && $this->config->data['MAIN']['CYRUSUNIXSTYLE'] == "true"){
95       $this->folder_prefix= "user/";
96     } else {
97       $this->folder_prefix= "user.";
98     }
99     
100     /* This is not a new account, parse additional attributes */
101     if (($dn != NULL) && ($dn != "new") && $this->is_account){
103       /* Load attributes containing arrays */
104       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
105         $this->$val= array();
106         if (isset($this->attrs["$val"]["count"])){
107           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
108             array_push($this->$val, $this->attrs["$val"][$i]);
109           }
110         }
111       }
114       /* Only do IMAP actions if gosaMailServer attribute is set */
115       if (isset ($this->attrs["gosaMailServer"][0])){
117         $method = new $this->method($this->config);
118         $id     = $method->uattrib;
120         /* Adapt attributes if needed */
121         $method->fixAttributesOnLoad($this);
123         /* FixAttributesOnLoad possibly creates an array out of gosaMailServer.
124             If the mail tab wasn't opened once before saving, the account can't be saved */
125         if(is_array($this->gosaMailServer)){
126           $this->gosaMailServer = $this->gosaMailServer[0];
127         }
129         if ($method->connect($this->attrs["gosaMailServer"][0])){
131           /* Update quota values */
132           $quota= $method->getQuota($this->folder_prefix.$this->$id);
133          
134           if($quota){
135             if ($quota['gosaMailQuota'] == 2147483647){
136               $this->quotaUsage     = "";
137               $this->gosaMailQuota  = "";
138             } else {
139               $this->quotaUsage     = $quota['quotaUsage'];
140               $this->gosaMailQuota  = $quota['gosaMailQuota'];
141             }
142           }else{
143             $this->quotaUsage     = "";
144             $this->gosaMailQuota  = "";
145 //            print_red(sprintf(_("Can't get quota information for '%s'."),$this->folder_prefix.$this->$id));
146           }
148           /* Get mailboxes / folder like INBOX ..*/
149           $this->mailboxList= $method->getMailboxList($this->folder_prefix.$this->$id,$this->$id);
150           
151           $method->disconnect();
152         }else{
153           /* Could not connect to ldap.
154            */
155           if (isset($this->attrs['gosaMailQuota'][0])){
156             $this->gosaMailQuota = $this->attrs['gosaMailQuota'][0];
157           }
158         }
159       }
160     }
163     /* Get vacation message */
165     /* Fill vacation array */
166     $this->vacation= array();
167     if (isset($this->config->current['VACATIONDIR'])){
168       $dir= $this->config->current['VACATIONDIR'];
169       if (is_dir($dir) && is_readable($dir)){
171         /* Look for files and build the vacation array */
172         $dh= opendir($dir);
173         while ($file = readdir($dh)){
174           $description= $this->parse_vacation("$dir/$file");
175           if ($description != ""){
176             $this->vacation["$dir/$file"]= $description;
177           }
178         }
179         closedir($dh);
180       }
181     }
184   /* Create filter */
186     /* Get global filter config */
187     if (!is_global("mailfilter")){
188       $ui= get_userinfo();
189       $base= get_base_from_people($ui->dn);
190       $mailfilter= array( "depselect"       => $base,
191           "muser"            => "",
192           "regex"           => "*");
193       register_global("mailfilter", $mailfilter);
194     }
195   }
198   function parse_vacation($file)
199   {
200     $desc= "";
202     if (is_file($file)){
203       $fh = fopen($file, "r");
204       $line= fgets($fh, 256);
206       if (!preg_match('/^DESC:/', $line)){
207         print_red (_("No DESC tag in vacation file:")." $file");
208         return $desc;
209       }
210       fclose ($fh);
212       $desc= trim(preg_replace('/^DESC:\s*/', '', $line));
213     }
215     return $desc;
216   }
219   function execute()
220   {
221     /* Call parent execute */
222     plugin::execute();
224     /* Initialise vars */
226     /* Load templating engine */
227     $smarty= get_smarty();
228     $display= "";
230     /* Get available mailserver */
231     $mailserver= array();
232     foreach ($this->config->data['SERVERS']['IMAP'] as $key => $val){
233       $mailserver[]= $key;
234     }
236     /* Handle account state */
238     /* Do we need to flip is_account state? */
239     if (isset($_POST['modify_state'])){
241       /* Onyl change account state if allowed */
242       if($this->is_account && $this->acl == "#all#" && !$this->accountDelegationsConfigured()){
243         $this->is_account= !$this->is_account;
244       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
245         $this->is_account= !$this->is_account;
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\">&nbsp;<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_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_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_header(_("Create mail account"), _("This account has mail features disabled. You can enable them by clicking below."));
270         return ($display);
271       }
272     }
274     /* Forwarder  subdialog */
276     /* Trigger forward add dialog? */
277     if (isset($_POST['add_local_forwarder'])){
278       $this->forward_dialog= TRUE;
279       $this->dialog= TRUE;
280     }
282     /* Cancel forward add dialog? */
283     if (isset($_POST['add_locals_cancel'])){
284       $this->forward_dialog= FALSE;
285       $this->dialog= FALSE;
286     }
288     /* Finished adding of locals? */
289     if (isset($_POST['add_locals_finish'])){
290       if (count ($_POST['local_list']) &&
291           chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
293         /* Walk through list of forwarders, ignore own addresses */
294         foreach ($_POST['local_list'] as $val){
295           if (!in_array ($val, $this->gosaMailAlternateAddress) &&
296               $val != $this->mail){
298             $this->addForwarder($val);
299             $this->is_modified= TRUE;
300           }
301         }
302       }
303       $this->forward_dialog= FALSE;
304       $this->dialog= FALSE;
305     }
307     /* Add forward email addresses */
308     if (isset($_POST['add_forwarder'])){
309       if ($_POST['forward_address'] != ""){
311         /* Valid email address specified? */
312         $address= $_POST['forward_address'];
313         $valid= FALSE;
314         if (!is_email($address)){
315           if (!is_email($address, TRUE)){
316             if ($this->is_template){
317               $valid= TRUE;
318             } else {
319               print_red (_("You're trying to add an invalid email address to the list of forwarders."));
320             }
321           }
322         } elseif ($address == $this->mail
323             || in_array($address, $this->gosaMailAlternateAddress)) {
325           print_red (_("Adding your one of your own addresses to the forwarders makes no sense."));
327         } else {
328           $valid= TRUE;
329         }
331         if ($valid){
332           /* Add it */
333           if (chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
334             $this->addForwarder ($address);
335             $this->is_modified= TRUE;
336           }
338         }
339       }
340     }
342     /* Delete forward email addresses */
343     if (isset($_POST['delete_forwarder'])){
344       if (count($_POST['forwarder_list']) 
345           && chkacl ($this->acl, "gosaMailForwardingAddress") == ""){
347         $this->delForwarder ($_POST['forwarder_list']);
348       }
349     }
351     
352     /* Alternate address handling */
354     /* Add alternate email addresses */
355     if (isset($_POST['add_alternate'])){
356       if ($_POST['alternate_address'] != "" &&
357           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
359         $valid= FALSE;
360         if (!is_email($_POST['alternate_address'])){
361           if ($this->is_template){
362             if (!(is_email($_POST['alternate_address'], TRUE))){
363               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
364             } else {
365               $valid= TRUE;
366             }
367           } else {
368             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
369           }
371         } else {
372           $valid= TRUE;
373         }
375         if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
376           $ui= get_userinfo();
377           if ($user != $ui->username && !$this->is_template){
378             print_red (_("The address you're trying to add is already used by user")." '$user'.");
379           }
380         }
381       }
382     }
384     /* Delete alternate email addresses */
385     if (isset($_POST['delete_alternate']) && isset ($_POST['alternates_list'])){
386       if (count($_POST['alternates_list']) &&
387           chkacl ($this->acl, "gosaMailAlternateAddress") == ""){
389         $this->delAlternate ($_POST['alternates_list']);
390       }
391     }
393   
394     /* Vocation message */
395   
396     /* Import vacation message? */
397     if (isset($_POST["import_vacation"]) && isset($this->vacation[$_POST["vacation_template"]])){
398       $contents= "";
399       $lines= file($_POST["vacation_template"]);
400       foreach ($lines as $line){
401         if (!preg_match('/^DESC:/', $line)){
402           $contents.= $line;
403         }
404       }
406       /* Replace attributes */
407       $attrs= $this->parent->by_object['user']->attributes;
408       foreach ($attrs as $val){
409         
410         if(preg_match("/dateOfBirth/",$val)){
411           if($this->parent->by_object['user']->use_dob){
412             $contents= preg_replace("/%$val/",date("Y-d-m",$this->parent->by_object['user']->dateOfBirth),$contents);
413           }
414         }else {
415           $contents= preg_replace("/%$val/",
416               $this->parent->by_object['user']->$val, $contents);
417         }
418       }
420       /* Save message */
421       $this->gosaVacationMessage= htmlspecialchars($contents);
422     }
424   
425     /* Display forward dialog if requested above */
427     /* Show forward add dialog */
428     if ($this->forward_dialog){
429       $ldap= $this->config->get_ldap_link();
431       /* Save data */
432       $mailfilter= get_global("mailfilter");
433       foreach( array("depselect", "muser", "regex") as $type){
434         if (isset($_POST[$type])){
435           $mailfilter[$type]= $_POST[$type];
436         }
437       }
438       if (isset($_GET['search'])){
439         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
440         if ($s == "**"){
441           $s= "*";
442         }
443         $mailfilter['regex']= $s;
444       }
445       register_global("mailfilter", $mailfilter);
447       /* Get actual list */
448       $mailusers= array ();
449       if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
450         $regex= $mailfilter['regex'];
451         $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
452       } else {
453         $filter= "";
454       }
455       if ($mailfilter['muser'] != ""){
456         $user= $mailfilter['muser'];
457         $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
458       }
460       /* Add already present people to the filter */
461       $exclude= "";
462       foreach ($this->gosaMailForwardingAddress as $mail){
463         $exclude.= "(mail=$mail)";
464       }
465       if ($exclude != ""){
466         $filter.= "(!(|$exclude))";
467       }
469       $acl= array($this->config->current['BASE'] => ":all");
470       $res= get_list("(&(objectClass=gosaMailAccount)$filter)", $acl, $mailfilter['depselect'], 
471                      array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
472       $ldap->cd($mailfilter['depselect']);
473       $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
474       error_reporting (0);
475       while ($attrs= $ldap->fetch()){
476         if(preg_match('/%/', $attrs['mail'][0])){
477           continue;
478         }
479         $name= $this->make_name($attrs);
480         $mailusers[$attrs['mail'][0]]= $name."&lt;".
481           $attrs['mail'][0]."&gt;";
482       }
483       error_reporting (E_ALL);
484       natcasesort ($mailusers);
485       reset ($mailusers);
487       /* Show dialog */
488       $smarty->assign("search_image", get_template_path('images/search.png'));
489       $smarty->assign("usearch_image", get_template_path('images/search_user.png'));
490       $smarty->assign("tree_image", get_template_path('images/tree.png'));
491       $smarty->assign("infoimage", get_template_path('images/info.png'));
492       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
493       $smarty->assign("mailusers", $mailusers);
494       if (isset($_POST['depselect'])){
495         $smarty->assign("depselect", $_POST['depselect']);
496       }
497       $smarty->assign("deplist", $this->config->idepartments);
498       $smarty->assign("apply", apply_filter());
499       $smarty->assign("alphabet", generate_alphabet());
500       $smarty->assign("hint", print_sizelimit_warning());
501       foreach( array("depselect", "muser", "regex") as $type){
502         $smarty->assign("$type", $mailfilter[$type]);
503       }
504       $smarty->assign("hint", print_sizelimit_warning());
506       $display.= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
507       return ($display);
508     }
510     /* Display mail account tab */
512     $smarty->assign("mailServers", $mailserver);
513     foreach(array(
514           "gosaMailServer", 
515           "gosaMailQuota", 
516           "perms", 
517           "mail",
518           "gosaMailAlternateAddress", 
519           "gosaMailForwardingAddress",
521           // gosaMailDeliveryMode Flags 
522           "drop_own_mails",                       // No local delivery 
523           "gosaMailMaxSize",                      // Enable - Drop mails > max size
524           "gosaSpamSortLevel", "gosaSpamMailbox", // Enable - Spam sort options
525           "gosaVacationMessage",                  // Enable - Vacation message      
526           "custom_sieve",                         // Use custom sieve script
527           "only_local"                            // Send/receive local mails 
528                                         ) as $val){
529       if(isset($this->$val)){
530         $smarty->assign("$val", $this->$val);
531       }
532       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
533     }
535     if (is_numeric($this->gosaMailQuota) && $this->gosaMailQuota != 0){
536       $smarty->assign("quotausage", progressbar(round(($this->quotaUsage * 100)/ $this->gosaMailQuota),100,15,true));
537       $smarty->assign("quotadefined", "true");
538     } else {
539       $smarty->assign("quotadefined", "false");
540     }
542     /* Disable mail field if needed */
543     $method= new $this->method($this->config);
544     if ($method->uattrib == "mail" && $this->initially_was_account){
545       $smarty->assign("mailACL", "disabled");
546     }
549     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
550       $smarty->assign("drop_own_mails", "checked");
551     } else {
552       $smarty->assign("drop_own_mails", "");
553     }
555     $types = array(
556           "V"=>"use_vacation",
557           "S"=>"use_spam_filter",
558           "R"=>"use_mailsize_limit",
559           "I"=>"only_local",
560           "C"=>"own_script");
562     /* Fill checkboxes */
563     foreach($types as $option => $varname){
564       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
565         $smarty->assign($varname, "checked");
566       } else {
567         $smarty->assign($varname, "");
568       }
569     }
570     
571     if (preg_match("/V/", $this->gosaMailDeliveryMode)) {
572       $smarty->assign("use_vacation", "checked");
573     } else {
574       $smarty->assign("use_vacation", "");
575     }
576     if (preg_match("/S/", $this->gosaMailDeliveryMode)) {
577       $smarty->assign("use_spam_filter", "checked");
578     } else {
579       $smarty->assign("use_spam_filter", "");
580     }
581     if (preg_match("/R/", $this->gosaMailDeliveryMode)) {
582       $smarty->assign("use_mailsize_limit", "checked");
583     } else {
584       $smarty->assign("use_mailsize_limit", "");
585     }
586     if (preg_match("/I/", $this->gosaMailDeliveryMode)) {
587       $smarty->assign("only_local", "checked");
588     } else {
589       $smarty->assign("only_local", "");
590     }
591     if (preg_match("/C/", $this->gosaMailDeliveryMode)) {
592       $smarty->assign("own_script", "checked");
593     } else {
594       $smarty->assign("own_script", "");
595     }
597     /* Have vacation templates? */
598     $smarty->assign("template", "");
599     if (count($this->vacation)){
600       $smarty->assign("show_templates", "true");
601       $smarty->assign("vacationtemplates", $this->vacation);
602       if (isset($_POST['vacation_template'])){
603         $smarty->assign("template", $_POST['vacation_template']);
604       }
605     } else {
606       $smarty->assign("show_templates", "false");
607     }
609     /* Fill spam selector */
610     $spamlevel= array();
611     for ($i= 0; $i<21; $i++){
612       $spamlevel[]= $i;
613     }
614     $smarty->assign("spamlevel", $spamlevel);
615     $smarty->assign("spambox", $this->mailboxList);
616     $smarty->assign("custom_sieveACL", chkacl($this->acl, "custom_sieve"));     
617     $smarty->assign("only_localACL", chkacl($this->acl, "only_local")); 
619     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
620     return ($display);
621   }
623   /* check if we have some delegations configured, those delegations must be removed first */
624   function accountDelegationsConfigured()
625   { 
626     /* We are in administrational edit mode.
627         Check tab configurations directly */
628     if(isset($this->attrs)){
629       $checkArray  = array("kolabInvitationPolicy","unrestrictedMailSize", "calFBURL","kolabDelegate","kolabFreeBusyFuture");
630       foreach($checkArray as $index){
631         if(isset($this->attrs[$index])){
632            return(true);
633         }
634       }
635     }
636     if(isset($this->parent->by_object['connectivity']->plugin['kolabAccount'])){
637       if($this->parent->by_object['connectivity']->plugin['kolabAccount']->is_account){
638         return(true);
639       }
640     }
641     return(false); 
642   }
645   /* remove object from parent */
646   function remove_from_parent()
647   {
648     /* Cancel if there's nothing to do here */
649     if (!$this->initially_was_account){
650       return;
651     }
652     
653     /* include global link_info */
654     $ldap= $this->config->get_ldap_link();
656     /* Remove and write to LDAP */
657     plugin::remove_from_parent();
659     /* Zero arrays */
660     $this->attrs['gosaMailAlternateAddress']= array();
661     $this->attrs['gosaMailForwardingAddress']= array();
663     /* Adapt attributes if needed */
664     $method= new $this->method($this->config);
665     $method->fixAttributesOnRemove($this);
667     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
668     $ldap->cd($this->dn);
669     $this->cleanup();
670     $ldap->modify ($this->attrs); 
672     show_ldap_error($ldap->get_error(), _("Removing mail account failed"));
674     /* Connect to IMAP server for account deletion */
675     if ($this->gosaMailServer != ""){
676       $method= new $this->method($this->config);
677       $id= $method->uattrib;
678       if ($method->connect($this->gosaMailServer)){
680         /* Remove account from IMAP server */
681         $method->deleteMailbox($this->folder_prefix.$this->$id);
682         $method->disconnect();
683       }
684     }
686     /* Optionally execute a command after we're done */
687     $this->handle_post_events("remove", array('uid'=> $this->uid));
688   }
691   /* Save data to object */
692   function save_object()
693   {
694     if (isset($_POST['mailTab'])){
695       /* Save ldap attributes */
696       plugin::save_object();
698       /* Assemble mail delivery mode
699          The mode field in ldap consists of values between braces, this must
700          be called when 'mail' is set, because checkboxes may not be set when
701          we're in some other dialog.
703          Example for gosaMailDeliveryMode [LR        ]
704          L: Local delivery
705          R: Reject when exceeding mailsize limit
706          S: Use spam filter
707          V: Use vacation message
708          C: Use custm sieve script
709          I: Only insider delivery */
711       $tmp= "";
712       $Flags = array(
713                      "R"  => array("ACL" => "gosaMailMaxSize",    "POST" => "use_mailsize_limit"),
714                      "V"  => array("ACL" => "gosaVacationMessage","POST" => "use_vacation"),
715                      "C"  => array("ACL" => "custom_sieve",       "POST" => "own_script"),
716                      "I"  => array("ACL" => "only_local",         "POST" => "only_local"));
718       /* Check acls and set new value if allowed. 
719          If we do not have permission to change the value 
720           check for the old value and use this */
721       foreach($Flags as $flag => $val){
722         $acl  = $val['ACL'];
723         $post = $val['POST'];
724         if(chkacl($this->acl,$acl) ==""){
725           if (isset($_POST[$post])){
726             $tmp.= $flag;
727           }
728         }else{
729           if(preg_match("/".$flag."/",$this->gosaMailDeliveryMode)){
730             $tmp.= $flag ;
731           }
732         }
733       }
735       /* ! Inverse flag handling for drop_own_mails */
736       if(chkacl($this->acl,"drop_own_mails") == ""){
737         if (!isset($_POST['drop_own_mails'])){
738           $tmp.= "L";
739         }
740       }else{
741         if(preg_match("/L/",$this->gosaMailDeliveryMode)){
742           $tmp.= "L" ;
743         }
744       }
746       /* If one of these acls are given, we are allowed to enable disable the the spam settings */
747       if(chkacl($this->acl,"gosaSpamMailbox") == "" || chkacl($this->acl,"gosaSpamSortLevel") ==""){
748         if (isset($_POST["use_spam_filter"])){
749           $tmp.= "S";
750         }
751       }else{
752         if(preg_match("/S/",$this->gosaMailDeliveryMode)){
753           $tmp.= "S" ;
754         }
755       }
756       
757       /* Check if something has changed an assign new gosaMailDeliveryMode */
758       $tmp= "[$tmp]";
759       if ($this->gosaMailDeliveryMode != $tmp){
760         $this->is_modified= TRUE;
761       }
762       $this->gosaMailDeliveryMode= $tmp;
763     }
764   }
767   /* Save data to LDAP, depending on is_account we save or delete */
768   function save()
769   {
770     $ldap= $this->config->get_ldap_link();
772     /* Call parents save to prepare $this->attrs */
773     plugin::save();
775     /* Save arrays */
776     $this->attrs['gosaMailAlternateAddress']= $this->gosaMailAlternateAddress;
777     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
779     /* Adapt attributes if needed */
780     $method= new $this->method($this->config);
781     $id= $method->uattrib;
783     $method->fixAttributesOnStore($this);
785     /* Remove Mailquota if = "" or "0"  */
786     if((isset($this->attrs['gosaMailQuota']))&&(!$this->attrs['gosaMailQuota'])) {
787       $this->attrs['gosaMailQuota']=0;
788     }
790     if(empty($this->attrs['gosaSpamMailbox'])){
791       unset($this->attrs['gosaSpamMailbox']);
792     }
794     $this->attrs['mail'] = strtolower($this->attrs['mail']); 
796     /* Save data to LDAP */
797     $ldap->cd($this->dn);
798     $this->cleanup();
799     $ldap->modify ($this->attrs); 
801     show_ldap_error($ldap->get_error(), _("Saving mail account failed"));
803     /* Only do IMAP actions if we are not a template */
804     if (!$this->is_template){
806       if ($method->connect($this->gosaMailServer)){
807         $method->updateMailbox($this->folder_prefix.$this->$id);
808         
809         $method->setQuota($this->folder_prefix.$this->$id, $this->gosaMailQuota);
810         $method->disconnect();
812         /* Only talk with sieve if the mail account already exists */
813         if($this->initially_was_account){
814           
815           /* Write sieve information only if not in C mode */
816           if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
817             $method->configureFilter($this->$id,
818                 $this->gosaMailDeliveryMode,
819                 $this->mail,
820                 $this->gosaMailAlternateAddress,
821                 $this->gosaMailMaxSize,
822                 $this->gosaSpamMailbox,
823                 $this->gosaSpamSortLevel,
824                 $this->gosaVacationMessage);
825           }
826         }
827       }
828     }
830     /* Optionally execute a command after we're done */
831     if ($this->initially_was_account == $this->is_account){
832       if ($this->is_modified){
833         $this->handle_post_events("modify", array('uid'=> $this->uid));
834       }
835     } else {
836       $this->handle_post_events("add", array('uid'=>$this->uid));
837     }
839   }
842   /* Check formular input */
843   function check()
844   {
845     if(!$this->is_account) return(array());
847     $ldap= $this->config->get_ldap_link();
849     /* Call common method to give check the hook */
850     $message= plugin::check();
852     if(empty($this->gosaMailServer)){
853       $message[]= _("There is no valid mailserver specified, please add one in the system setup.");
854     }
856     /* must: mail */
857     if ($this->mail == ""){
858       $message[]= _("The required field 'Primary address' is not set.");
859     }
860     if ($this->is_template){
861       if (!is_email($this->mail, TRUE)){
862         $message[]= _("Please enter a valid email address in 'Primary address' field.");
863       }
864     } else {
865       if (!is_email($this->mail)){
866         $message[]= _("Please enter a valid email address in 'Primary address' field.");
867       }
868     }
869     $ldap->cd($this->config->current['BASE']);
870     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".$this->mail."))(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
871     if ($ldap->count() != 0){
872       $message[]= _("The primary address you've entered is already in use.");
873     }
875     /* Check quota */
876     if ($this->gosaMailQuota != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
877       if (!is_numeric($this->gosaMailQuota)) {
878         $message[]= _("Value in 'Quota size' is not valid.");
879       } else {
880         $this->gosaMailQuota= (int) $this->gosaMailQuota;
881       }
882     }
884     /* Check rejectsize for integer */
885     if ($this->gosaMailMaxSize != '' && chkacl ($this->acl, "gosaMailQuota") == ""){
886       if (!is_numeric($this->gosaMailMaxSize)){
887         $message[]= _("Please specify a vaild mail size for mails to be rejected.");
888       } else {
889         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
890       }
891     }
893     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
894     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && 
895         $this->gosaMailMaxSize == ""){
897       $message[]= _("You need to set the maximum mail size in order to reject anything.");
898     }
900     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
901       $message[]= _("You specified Spam settings, but there is no Folder specified.");
902     }
904     return ($message);
905   }
908   /* Adapt from template, using 'dn' */
909   function adapt_from_template($dn)
910   {
911     plugin::adapt_from_template($dn);
913     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
914       $this->$val= array();
915       if (isset($this->attrs["$val"]["count"])){
916         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
917           $value= $this->attrs["$val"][$i];
918           foreach (array("sn", "givenName", "uid") as $repl){
919             if (preg_match("/%$repl/i", $value)){
920               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
921             }
922           }
923           array_push($this->$val, strtolower(rewrite($value)));
924         }
925       }
926     }
927     $this->mail= strtolower(rewrite($this->mail));
928   }
931   /* Add entry to forwarder list */
932   function addForwarder($address)
933   {
934     $this->gosaMailForwardingAddress[]= $address;
935     $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
936     sort ($this->gosaMailForwardingAddress);
937     reset ($this->gosaMailForwardingAddress);
938     $this->is_modified= TRUE;
939   }
942   /* Remove list of addresses from forwarder list */
943   function delForwarder($addresses)
944   {
945     $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
946     $this->is_modified= TRUE;
947   }
950   /* Add given mail address to the list of alternate adresses , 
951       check if this mal address is used, skip adding in this case */
952   function addAlternate($address)
953   {
954     $ldap= $this->config->get_ldap_link();
955     $address= strtolower($address);
956       
957     /* Is this address already assigned in LDAP? */
958     $ldap->cd ($this->config->current['BASE']);
959     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)"."(gosaMailAlternateAddress=$address)))", array("uid"));
961     if ($ldap->count() > 0){
962       $attrs= $ldap->fetch ();
963       return ($attrs["uid"][0]);
964     }
966     /* Add to list of alternates */
967     if (!in_array($address, $this->gosaMailAlternateAddress)){
968       $this->gosaMailAlternateAddress[]= $address;
969       $this->is_modified= TRUE;
970     }
972     sort ($this->gosaMailAlternateAddress);
973     reset ($this->gosaMailAlternateAddress);
974     return ("");
975   }
978   function delAlternate($addresses)
979   {
980     $this->gosaMailAlternateAddress= array_remove_entries ($addresses,
981                                                            $this->gosaMailAlternateAddress);
982     $this->is_modified= TRUE;
983   }
985   function make_name($attrs)
986   {
987     $name= "";
988     if (isset($attrs['sn'][0])){
989       $name= $attrs['sn'][0];
990     }
991     if (isset($attrs['givenName'][0])){
992       if ($name != ""){
993         $name.= ", ".$attrs['givenName'][0];
994       } else {
995         $name.= $attrs['givenName'][0];
996       }
997     }
998     if ($name != ""){
999       $name.= " ";
1000     }
1002     return ($name);
1003   }
1005   
1006   /* Create the mail part for the copy & paste dialog */
1007   function getCopyDialog()
1008   {
1009     if(!$this->is_account) return("");
1010     $smarty = get_smarty();
1011     $smarty->assign("mail",$this->mail); 
1012     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1013     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1014     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
1016     $ret = array();
1017     $ret['status'] = "";
1018     $ret['string'] = $str;
1019     return($ret);
1020   }
1022   function saveCopyDialog()
1023   {
1024     if(!$this->is_account) return;  
1026     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
1027     $this->execute();
1028     
1029     if(isset($_POST['mail'])){
1030       $this->mail = $_POST['mail'];
1031     }
1032   }
1034    
1035   function PrepareForCopyPaste($source)
1036   {
1037     plugin::PrepareForCopyPaste($source);
1039     /* Reset alternate mail addresses */
1040     $this->gosaMailAlternateAddress = array();    
1041   }
1044   function allow_remove()
1045   {
1046     if (isset($this->config->current['MAILMETHOD'])){
1047       $method= $this->config->current['MAILMETHOD'];
1048       if(preg_match("/kolab/i",$method)){
1049         $ldap = $this->config->get_ldap_link();
1050         $ldap->cd($this->config->current['BASE']);
1051         $ldap->cat($this->dn);
1052         if($ldap->count()){
1053           $attrs = $ldap->fetch();
1054           if(isset($attrs['kolabDeleteFlag'])){ 
1055             return(_("Waiting for kolab to remove mail properties."));
1056           }elseif(in_array("gosaMailAccount",$attrs['objectClass'])){
1057             return(_("Please remove the mail account first, to allow kolab to call its remove methods."));
1058           }
1059         }
1060       }
1061     }
1062   }
1065 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1066 ?>