Code

bbb0021dd347037113b74ca85a647b25a7bdd433
[gosa.git] / gosa-plugins / mail / personal / mail / class_mailAccount.inc
1 <?php
2 /*! 
3   \brief   mail plugin
4   \author  Fabian Hicker  <Fabian.Hickert@GONICUS.de>
5   \version 2.6.2
6   \date    03.12.2007
8   This class provides the functionality to read and write all attributes
9   relevant for gosaMailAccounts from/to the LDAP. 
10   It does syntax checking and displays the formulars required.
11   Special handling like sieve or imap actions will be implemented 
12   by the mailMethods.
15 Functions :
17  - mailAccount (&$config, $dn= NULL)
18  - execute()
19  - save_object()
20  - get_vacation_templates()
21  - addForwarder($address)
22  - delForwarder($addresses)
23  - addAlternate($address)
24  - delAlternate($addresses)
25  - prepare_vacation_template($contents)
26  - remove_from_parent()
27  - save()
28  - check()
29  - adapt_from_template($dn, $skip= array())
30  - getCopyDialog()
31  - saveCopyDialog()
32  - PrepareForCopyPaste($source)
33  - get_multi_edit_values()
34  - multiple_check()
35  - set_multi_edit_values($values)
36  - init_multiple_support($attrs,$all)
37  - get_multi_init_values()
38  - multiple_execute()
39  - multiple_save_object()
40  - make_name($attrs)
41  - plInfo()
44  */
46 /*
48     FLAG    POSTNAME                DESC
49     ####################################################################################################
50     L  (!)  only_local              Enables: "User is only allowed to send and receive local mails"
52                                     <REVERSED> If checked in the ui, the flag is not present in the 
53                                      gosaMailDeliveryMode attribute. If its unchecked in the ui, the 
54                                      character 'L' is added to the delivery flags.
56     R       use_mailsize_limit      Enables rule: "Reject mails bigger than [n] MB";
57     S       use_spam_filter         Enables rule: "Move mails tagged with SPAM level greater than [n] 
58                                      to folder [x]"
59     V       use_vacation            Enables: "Vacation message"
60     C       own_script              Enables: "Use custom sieve script (disables all Mail options!)"
61     I       drop_own_mails          Enables: "No delivery to own mailbox."
63 */
65 class mailAccount extends plugin
66 {
67   /* Definitions */
68   var $plHeadline     = "Mail";
69   var $plDescription  = "Manage personal mail settings";
70   var $view_logged    = FALSE;
71   var $is_account     = FALSE;
72   var $initially_was_account = FALSE;
74   /* GOsa mail attributes */
75   var $mail                               = "";
76   var $gosaVacationStart                  = "";
77   var $gosaVacationStop                   = "";
78   var $gosaMailAlternateAddress           = array();
79   var $gosaMailForwardingAddress          = array();
80   var $gosaMailDeliveryMode               = "[L        ]";
81   var $gosaMailServer                     = "";
82   var $gosaMailQuota                      = "";
83   var $gosaMailMaxSize                    = "";
84   var $gosaVacationMessage                = "";
85   var $gosaSpamSortLevel                  = "";
86   var $gosaSpamMailbox                    = "";
88   /* The methods defaults */
89   var $quotaUsage     = -1; // Means unknown
91   var $mailMethod      = NULL;
92   var $MailDomain      = "";
93   var $sieveManagementUsed = FALSE;
94   var $vacationTemplates = array();
95   var $sieve_management = NULL;
96   var $mailAddressSelect = FALSE;
97   var $initial_uid    = "";
98   var $mailDomainPart = "";
99   var $mailDomainParts = array();
100   var $MailBoxes = array("INBOX");
102   /* Used LDAP attributes && classes */
103   var $attributes= array(
104       "mail", "gosaMailServer","gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
105       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
106       "gosaVacationStart","gosaVacationStop", "gosaVacationMessage", "gosaMailAlternateAddress", 
107       "gosaMailForwardingAddress");
108   var $objectclasses= array("gosaMailAccount");
110   var $multiple_support = TRUE;
112   var $uid = "";
113   var $cn  = "";
116   /*! \brief  Initialize the mailAccount 
117    */
118   function __construct (&$config, $dn= NULL)
119   {
120     plugin::plugin($config,$dn); 
122     /* Get attributes from parent object 
123      */
124     foreach(array("uid","cn") as $attr){
125       if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
126         $this->$attr = &$this->parent->by_object['group']->$attr;
127       }elseif(isset($this->attrs[$attr])){
128         $this->$attr = $this->attrs[$attr][0];
129       }
130     }
132     /* Intialize the used mailMethod
133      */
134     $tmp = new mailMethod($config,$this);
135     $this->mailMethod       = $tmp->get_method();
136     $this->mailMethod->fixAttributesOnLoad();
137     $this->mailDomainParts  = $this->mailMethod->getMailDomains();
138     $this->SpamLevels = $this->mailMethod->getSpamLevels();
140     /* Remember account status 
141      */
142     $this->initially_was_account = $this->is_account;
144     /* Initialize vacation settings, if enabled.
145      */   
146     if(empty($this->gosaVacationStart) && $this->mailMethod->vacationRangeEnabled()){
147       $this->gosaVacationStart = time();
148       $this->gosaVacationStop = time();
149     }
151     /* Read vacation templates 
152      */
153     $this->vacationTemplates = $this->get_vacation_templates();
155     /* Initialize configured values 
156      */ 
157     if($this->is_account){
159       if($this->mailMethod->connect() && $this->mailMethod->account_exists()){
161         /* Read quota */
162         $this->gosaMailQuota = $this->mailMethod->getQuota($this->gosaMailQuota);
163         $this->quotaUsage    = $this->mailMethod->getQuotaUsage($this->quotaUsage);
164         if($this->mailMethod->is_error()){
165           msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings: %s"), 
166                 $this->mailMethod->get_error()), ERROR_DIALOG);
167         }
168         
169         /* Read mailboxes */
170         $this->MailBoxes = $this->mailMethod->getMailboxList($this->MailBoxes);
171         if($this->mailMethod->is_error()){
172           msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes: %s"), 
173                 $this->mailMethod->get_error()), ERROR_DIALOG);
174         }
175           
176       }elseif(!$this->mailMethod->is_connected()){
177         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
178               $this->mailMethod->get_error()), ERROR_DIALOG);
179       }elseif(!$this->mailMethod->account_exists()){
180         msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"), 
181               $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
182       }
184       /* If the doamin part is selectable, we have to split the mail address
185        */
186       if(!(!$this->mailMethod->isModifyableMail() && $this->is_account)){
187         if($this->mailMethod->domainSelectionEnabled()){
188           $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
189           $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
190           if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
191             $this->mailDomainParts[] = $this->mailDomainPart;
192           }
193         }
194       }
196       /* Load attributes containing arrays */
197       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
198         $this->$val= array();
199         if (isset($this->attrs["$val"]["count"])){
200           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
201             array_push($this->$val, $this->attrs["$val"][$i]);
202           }
203         }
204       }
205     }
207     /* Intialize sieveManagement if necessary */
208     if($this->mailMethod->allowSieveManagement()){
209       $this->mailboxList = &$this->MailBoxes;
210       $this->sieve_management = new sieveManagement($this->config,$this->dn,$this,$this->mailMethod->getUAttrib());
211     }
213     /* Disconnect mailMethod. Connect on demand later. 
214      */
215     $this->mailMethod->disconnect();
217     /* Convert start/stop dates */
218     #TODO: use date format
219     $this->gosaVacationStart= date('d.m.Y', $this->gosaVacationStart);
220     $this->gosaVacationStop= date('d.m.Y', $this->gosaVacationStop);
221   }
224   function execute()
225   {
227     /* Call parent execute */
228     $display = plugin::execute();
230     /* Log view */
231     if($this->is_account && !$this->view_logged){
232       $this->view_logged = TRUE;
233       new log("view","users/".get_class($this),$this->dn);
234     }
237     /****************
238       Account status
239      ****************/
241     if(isset($_POST['modify_state'])){
242       if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
243         $this->is_account= FALSE;
244       }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
245         $this->is_account= TRUE;
246       }
247     }
248     if(!$this->multiple_support_active){
249       if (!$this->is_account && $this->parent === NULL){
250         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
251           msgPool::noValidExtension(_("Mail"))."</b>";
252         $display.= back_to_main();
253         return ($display);
254       }
255       if ($this->parent !== NULL){
256         if ($this->is_account){ 
257           $reason = "";
258           if(!$this->mailMethod->accountRemoveable($reason)){
259             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
260           }else{
261             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::featuresEnabled(_("Mail")));
262           }
263         } else {
264           $reason = "";
265           if(!$this->mailMethod->accountCreateable($reason)){
266             $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
267           }else{
268             $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::featuresDisabled(_("Mail")));
269           }
270           return ($display);
271         }
272       }
273     }
275     /****************
276       Sieve Management Dialog
277      ****************/
278     if($this->mailMethod->allowSieveManagement()){
279       if(isset($_POST['sieveManagement'])
280           && preg_match("/C/",$this->gosaMailDeliveryMode)
281           && $this->acl_is_writeable("sieveManagement") 
282           && $this->mailMethod->allowSieveManagement()) {
283         $this->dialog = $this->sieve_management;
284       }
285       if(isset($_POST['sieve_cancel'])){
286         $this->dialog = FALSE;
287       }
288       if(isset($_POST['sieve_finish'])){
289         $this->sieve_management = $this->dialog;
290         $this->dialog = FALSE;
291       }
292       if(is_object($this->dialog)){
293         $this->dialog->save_object();
294         return($this->dialog->execute());
295       }
296     }
298     /****************
299       Forward addresses 
300      ****************/
302     if (isset($_POST['add_local_forwarder'])){
303       $this->mailAddressSelect=  new mailAddressSelect($this->config, get_userinfo());
304       $this->dialog= TRUE;
305     }
306     if (isset($_POST['mailAddressSelect_cancel'])){
307       $this->mailAddressSelect= FALSE;
308       $this->dialog= FALSE;
309     }
311     if (isset($_POST['mailAddressSelect_save']) && $this->mailAddressSelect instanceOf mailAddressSelect){
313       if($this->acl_is_writeable("gosaMailForwardingAddress")){
314         $list = $this->mailAddressSelect->save();
315         foreach ($list as $entry){
316           $val = $entry['mail'][0];
317           if (!in_array ($val, $this->gosaMailAlternateAddress) && $val != $this->mail){
318             $this->addForwarder($val);
319             $this->is_modified= TRUE;
320           }
321         }
322         $this->mailAddressSelect= FALSE;
323         $this->dialog= FALSE;
324       } else {
325         msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
326       }
327     }
329     if($this->mailAddressSelect instanceOf mailAddressSelect){
330       $used  = array();
331       $used['mail'] = array_values($this->gosaMailAlternateAddress);  
332       $used['mail'] = array_merge($used['mail'], array_values($this->gosaMailForwardingAddress));  
333       $used['mail'][] = $this->mail;
335       // Build up blocklist
336       session::set('filterBlacklist', $used);
337       return($this->mailAddressSelect->execute());
338     }
340     if (isset($_POST['add_forwarder'])){
341       if ($_POST['forward_address'] != ""){
342         $address= get_post('forward_address');
343         $valid= FALSE;
344         if (!tests::is_email($address)){
345           if (!tests::is_email($address, TRUE)){
346             if ($this->is_template){
347               $valid= TRUE;
348             } else {
349               msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
350                     "","","your-address@your-domain.com"),ERROR_DIALOG);
351             }
352           }
353         } elseif ($address == $this->mail
354             || in_array($address, $this->gosaMailAlternateAddress)) {
355           msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
356         } else {
357           $valid= TRUE;
358         }
359         if ($valid){
360           if($this->acl_is_writeable("gosaMailForwardingAddress")){
361             $this->addForwarder ($address);
362             $this->is_modified= TRUE;
363           }
364         }
365       }
366     }
367     if (isset($_POST['delete_forwarder'])){
368       $this->delForwarder ($_POST['forwarder_list']);
369     }
370     if ($this->mailAddressSelect instanceOf mailAddressSelect){
371     
372       return($this->mailAddressSelect->execute());
373     }
376     /****************
377       Alternate addresses 
378      ****************/
380     if (isset($_POST['add_alternate'])){
381       $valid= FALSE;
382       if (!tests::is_email($_POST['alternate_address'])){
383         if ($this->is_template){
384           if (!(tests::is_email($_POST['alternate_address'], TRUE))){
385             msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),"","","your-domain@your-domain.com"),ERROR_DIALOG);
386           } else {
387             $valid= TRUE;
388           }
389         } else {
390           msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),"","","your-domain@your-domain.com"),ERROR_DIALOG);
391         }
392       } else {
393         $valid= TRUE;
394       }
395       if ($valid && ($user= $this->addAlternate (get_post('alternate_address'))) != ""){
396         $ui= get_userinfo();
397         $addon= "";
398         if ($user[0] == "!") {
399           $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
400         } else {
401           $addon= sprintf(_("Address is already in use by user '%s'."), $user);
402         }
403         msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
404             "$addon</i>", ERROR_DIALOG);
405       }
406     }
407     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
408       $this->delAlternate ($_POST['alternates_list']);
409     }
411     /****************
412       SMARTY- Assign smarty variables 
413      ****************/
414     $smarty = get_smarty();
416     $smarty->assign("initially_was_account", $this->initially_was_account);
417     $smarty->assign("isModifyableMail"  , $this->mailMethod->isModifyableMail());
418     $smarty->assign("isModifyableServer", $this->mailMethod->isModifyableServer());
419     $smarty->assign("mailEqualsCN", $this->mailMethod->mailEqualsCN());
421     $tmp  = $this->plInfo();
422     foreach($tmp['plProvidedAcls'] as $name => $transl){
423       $smarty->assign("$name"."ACL", $this->getacl($name));
424     }
425     foreach($this->attributes as $attr){
426       $smarty->assign($attr,set_post($this->$attr));
427     }
428     $smarty->assign("quotaEnabled", $this->mailMethod->quotaEnabled());
429     if($this->mailMethod->quotaEnabled()){
430       $smarty->assign("quotaUsage",   mailMethod::quota_to_image($this->quotaUsage,$this->gosaMailQuota));
431       $smarty->assign("gosaMailQuota",set_post($this->gosaMailQuota));
432     }
433     $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
434     $smarty->assign("MailDomains", set_post($this->mailDomainParts));
435     $smarty->assign("MailDomain" , set_post($this->mailDomainPart));
436     $smarty->assign("MailServers", set_post($this->mailMethod->getMailServers()));
437     $smarty->assign("allowSieveManagement", $this->mailMethod->allowSieveManagement());
438     $smarty->assign("own_script",  $this->sieveManagementUsed);
440     /* _Multiple users vars_ */
441     foreach($this->attributes as $attr){
442       $u_attr = "use_".$attr;
443       $smarty->assign($u_attr,in_array($attr,$this->multi_boxes));
444     }
445     foreach(array("only_local","gosaMailForwardingAddress","use_mailsize_limit","drop_own_mails","use_vacation","use_spam_filter") as $attr){
446       $u_attr = "use_".$attr;
447       $smarty->assign($u_attr,in_array($attr,$this->multi_boxes));
448     }
451     /****************
452       SMARTY- Assign flags 
453      ****************/
455     $types = array(
456         "V"=>"use_vacation",
457         "S"=>"use_spam_filter",
458         "R"=>"use_mailsize_limit",
459         "I"=>"drop_own_mails",
460         "C"=>"own_script");
461     foreach($types as $option => $varname){
462       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
463         $smarty->assign($varname, "checked");
464       } else {
465         $smarty->assign($varname, "");
466       }
467     }
468     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
469       $smarty->assign("only_local", "checked");
470     } else {
471       $smarty->assign("only_local", "");
472     }
475     /****************
476       Smarty- Vacation settings 
477      ****************/
478     $smarty->assign("rangeEnabled", FALSE);
479     $smarty->assign("template", "");
480     if (count($this->vacationTemplates)){
481       $smarty->assign("show_templates", "true");
482       $smarty->assign("vacationtemplates", set_post($this->vacationTemplates));
483       if (isset($_POST['vacation_template'])){
484         $smarty->assign("template", set_post(get_post('vacation_template')));
485       }
486     } else {
487       $smarty->assign("show_templates", "false");
488     }
490     /* Vacation range assigments
491      */
492     if($this->mailMethod->vacationRangeEnabled()){
493       $smarty->assign("rangeEnabled", TRUE);
494     }
496     /* fill filter settings 
497      */
498     $smarty->assign("spamlevel", $this->SpamLevels);
499     $smarty->assign("spambox"  , $this->MailBoxes);
501     $smarty->assign("multiple_support",$this->multiple_support_active);  
502     return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
503   }
507   /* Save data to object */
508   function save_object()
509   {
510     if (isset($_POST['mailTab'])){
512       /* Save ldap attributes */
513       $mail   = $this->mail;
514       $server = $this->gosaMailServer;
515       plugin::save_object();
517       if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
518         $this->gosaMailServer = $server;
519       }
521       if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
522         $this->mail = $mail;
523       }else{
525         /* Get posted mail domain part, if necessary  
526          */
527         if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
528           if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
529             $this->mailDomainPart = get_post('MailDomain');
530           }
531         }
532       }
534       /* Import vacation message? 
535        */
536       if (isset($_POST["import_vacation"]) && isset($this->vacationTemplates[$_POST["vacation_template"]])){
537         if($this->multiple_support_active){
538           $contents = ltrim(preg_replace("/^DESC:.*$/m","",file_get_contents(get_post("vacation_template"))));
539         }else{
540           $contents = $this->prepare_vacation_template(file_get_contents(get_post("vacation_template")));
541         }
542         $this->gosaVacationMessage= htmlspecialchars($contents);
543       }
545       /* Handle flags 
546        */
547       if(isset($_POST['own_script'])){
548         if(!preg_match("/C/",$this->gosaMailDeliveryMode)){
549           $str= preg_replace("/[\[\]]/","",$this->gosaMailDeliveryMode);
550           $this->gosaMailDeliveryMode = "[".$str."C]";
551         }
552       }else{
554         /* Assemble mail delivery mode
555            The mode field in ldap consists of values between braces, this must
556            be called when 'mail' is set, because checkboxes may not be set when
557            we're in some other dialog.
559            Example for gosaMailDeliveryMode [LR        ]
560            L -  Local delivery
561            R -  Reject when exceeding mailsize limit
562            S -  Use spam filter
563            V -  Use vacation message
564            C -  Use custm sieve script
565            I -  Only insider delivery */
567         $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
570         /* Handle delivery flags */
571         if($this->acl_is_writeable("gosaMailDeliveryModeL")){
572           if(!preg_match("/L/",$tmp) && !isset($_POST['only_local'])){
573             $tmp.="L";
574           }elseif(preg_match("/L/",$tmp) && isset($_POST['only_local'])){
575             $tmp = preg_replace("/L/","",$tmp);
576           }
577         }
578         $opts = array(
579             "R"   => "use_mailsize_limit",
580             "S"   => "use_spam_filter",
581             "V"   => "use_vacation",
582             "C"   => "own_script",
583             "I"   => "drop_own_mails");
585         foreach($opts as $flag => $post){
586           if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
587             if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
588               $tmp.= $flag;
589             }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
590               $tmp = preg_replace("/".$flag."/","",$tmp);
591             }
592           }
593         }
595         $tmp= "[$tmp]";
596         if ($this->gosaMailDeliveryMode != $tmp){
597           $this->is_modified= TRUE;
598         }
599         $this->gosaMailDeliveryMode= $tmp;
601         /* Get start/stop values for vacation scope of application
602          */
603         if($this->mailMethod->vacationRangeEnabled()){
604           if($this->acl_is_writeable("gosaVacationMessage") && preg_match("/V/",$this->gosaMailDeliveryMode)){
605             if(isset($_POST['gosaVacationStart'])){
606               $this->gosaVacationStart = get_post('gosaVacationStart');
607             }
608             if(isset($_POST['gosaVacationStop'])){
609               $this->gosaVacationStop = get_post('gosaVacationStop');
610             }
611           }
612         }
613       }
614     }
615   }
618   /*! \brief  Parse vacation templates and build up an array
619     containing 'filename' => 'description'. 
620     Used to fill vacation dropdown box.
621     @return Array   All useable vacation templates.
622    */ 
623   function get_vacation_templates()
624   {
625     $vct = array();
626     if ($this->config->get_cfg_value("core","vacationTemplateDirectory") != ""){
627       $dir= $this->config->get_cfg_value("core","vacationTemplateDirectory");
628       if (is_dir($dir) && is_readable($dir)){
629         $dh = opendir($dir);
630         while($file = readdir($dh)){
631           $description= "";
632           if (is_file($dir."/".$file)){
633             $fh = fopen($dir."/".$file, "r");
634             $line= fgets($fh, 256);
635             if (!preg_match('/^DESC:/', $line)){
636               msg_dialog::display(_("Configuration error"), sprintf(_("No DESC tag in vacation template '%s'!"), $file), ERROR_DIALOG);
637             }else{
638               $description= trim(preg_replace('/^DESC:\s*/', '', $line));
639             }
640             fclose ($fh);
641           }
642           if ($description != ""){
643             $vct["$dir/$file"]= $description;
644           }
645         }
646         closedir($dh);
647       }
648     }
649     return($vct); 
650   }
653   /*! \brief  Adds the given mail address to the list of mail forwarders 
654    */ 
655   function addForwarder($address)
656   {
657     if(empty($address)) return;
658     if($this->acl_is_writeable("gosaMailForwardingAddress")){
659       $this->gosaMailForwardingAddress[]= $address;
660       $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
661       sort ($this->gosaMailForwardingAddress);
662       reset ($this->gosaMailForwardingAddress);
663       $this->is_modified= TRUE;
664     }else{
665       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
666     }
667   }
670   /*! \brief  Removes the given mail address from the list of mail forwarders 
671    */ 
672   function delForwarder($addresses)
673   {
674     if($this->acl_is_writeable("gosaMailForwardingAddress")){
675       $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
676       $this->is_modified= TRUE;
677     }else{
678       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
679     }
680   }
683   /*! \brief  Add given mail address to the list of alternate adresses ,
684     .          check if this mal address is used, skip adding in this case 
685    */ 
686   function addAlternate($address)
687   {
688     if(empty($address)) return;
689     if($this->acl_is_writeable("gosaMailAlternateAddress")){
690       $ldap= $this->config->get_ldap_link();
691       $address= strtolower($address);
693       /* Is this address already assigned in LDAP? */
694       $ldap->cd ($this->config->current['BASE']);
695       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)".
696           "(alias=$address)(gosaMailAlternateAddress=$address)))", array("uid", "cn"));
697       if ($ldap->count() > 0){
698         $attrs= $ldap->fetch ();
699         if (!isset($attrs["uid"])) {
700           return ("!".$attrs["cn"][0]);
701         }
702         return ($attrs["uid"][0]);
703       }
704       if (!in_array($address, $this->gosaMailAlternateAddress)){
705         $this->gosaMailAlternateAddress[]= $address;
706         $this->is_modified= TRUE;
707       }
708       sort ($this->gosaMailAlternateAddress);
709       reset ($this->gosaMailAlternateAddress);
710       return ("");
711     }else{
712       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
713     }
714   }
717   /*! \brief  Removes the given mail address from the alternate addresses list 
718    */ 
719   function delAlternate($addresses)
720   {
721     if($this->acl_is_writeable("gosaMailAlternateAddress")){
722       $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
723       $this->is_modified= TRUE;
724     }else{
725       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
726     }
727   }
730   /*! \brief  Prepare importet vacation string. \
731     .         Replace placeholder like %givenName a.s.o.
732     @param  string  Vacation string
733     @return string  Completed vacation string
734    */
735   private function prepare_vacation_template($contents)
736   {
737     /* Replace attributes */
738     $attrs = array();
739     $obj   = NULL;
740     if(isset($this->parent->by_object['user'])){
741       $attrs  = $this->parent->by_object['user']->attributes;
742       $obj    = $this->parent->by_object['user'];
743     }else{
744       $obj    = new user($this->config,$this->dn);
745       $attrs  = $obj->attributes;
746     }
747     if($obj){
749       /* Replace vacation start and end time */
750       if($this->mailMethod->vacationRangeEnabled()){
751         if(preg_match("/%start/",$contents)){
752           $contents = preg_replace("/%start/",$this->gosaVacationStart,$contents);
753         }
754         if(preg_match("/%end/",$contents)){
755           $contents = preg_replace("/%end/",$this->gosaVacationStop,$contents);
756         }
757       }else{
758         if(preg_match("/%start/",$contents)){
759           $contents = preg_replace("/%start/", _("unknown"),$contents);
760         }
761         if(preg_match("/%end/",$contents)){
762           $contents = preg_replace("/%end/", _("unknown"), $contents);
763         }
764       }
766       foreach ($attrs as $val){
768         // We can only replace strings here
769         if(!is_string($obj->$val)) continue;
771         if(preg_match("/dateOfBirth/",$val)){
772           if($obj->use_dob){
773             $contents= preg_replace("/%$val/",date("Y-d-m",$obj->dateOfBirth),$contents);
774           }
775         }else {
776           $contents= preg_replace("/%$val/",
777               $obj->$val, $contents);
778         }
780       }
781     }
782     $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
783     return($contents);
784   }
787   /*! \brief  Removes the mailAccount extension from ldap 
788    */  
789   function remove_from_parent()
790   {
791     /* Cancel if there's nothing to do here */
792     if (!$this->initially_was_account){
793       return;
794     }
796     /* If domain part was selectable, contruct mail address */
797     if($this->mailMethod->domainSelectionEnabled()){
798       $this->mail = $this->mail."@".$this->mailDomainPart;
799     }
801     /* Update sharedFolder dependencies. 
802        Open each shared folder and remove this account. 
803        Then Save the group to ensure that all necessary 
804         actions will be taken (imap acls updated aso.).
805      */
806     $ldap = $this->config->get_ldap_link();    
807     $ldap->cd($this->config->current['BASE']);
808     $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
809     while($attrs = $ldap->fetch()){
810       $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
811       if(isset($grp->by_object['mailgroup']) && isset($grp->by_object['group'])){
812         $grp->by_object['group']->removeUser($this->uid);
814         /* Do not save the complete group! This will quit the complete membership 
815          */
816         $grp->by_object['mailgroup']->save();
817       } 
818     }
820     // Do NOT remove the mail attribute while it is used in the Fax Account.
821     if(isset($this->parent->by_object['gofaxAccount'])){
822         $fax = $this->parent->by_object['gofaxAccount'];
824         // Fax delivery to the mail account is activated, keep the mail attribute.
825         if($fax->goFaxDeliveryMode & 32){
826            $this->attributes = array_remove_entries(array('mail'), $this->attributes)  ;
827         }
828     }
830     /* Remove GOsa attributes */
831     plugin::remove_from_parent();
833     /* Zero arrays */
834     $this->attrs['gosaMailAlternateAddress'] = array();
835     $this->attrs['gosaMailForwardingAddress']= array();
838     $this->mailMethod->fixAttributesOnRemove();
839     $this->cleanup();
841     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
842     $ldap= $this->config->get_ldap_link();
843     $ldap->cd($this->dn);
844     $ldap->modify ($this->attrs);
846     /* Add "view" to logging class */
847     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
848     if (!$ldap->success()){
849       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
850     }
851     
852     /* Let the mailMethod remove this mailbox, e.g. from imap and
853        update shared folder membership, ACL may need to be updated. 
854      */
855     if (!$this->is_template){
857       if(!$this->mailMethod->connect()){
858         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
859               $this->mailMethod->get_error()), ERROR_DIALOG);
860       }else{
861         if(!$this->mailMethod->deleteMailbox()){
862           msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox: %s"), 
863                 $this->mailMethod->get_error()), ERROR_DIALOG);
864         }
865       }
866     }
867     $this->mailMethod->disconnect();
869     /* Optionally execute a command after we're done */
870     $this->handle_post_events("remove",array("uid" => $this->uid));
871   }
874   /*! \brief  Save the mailAccount settings to the ldap database.
875    */
876   function save()
877   {
878     $ldap= $this->config->get_ldap_link();
880     /* If domain part was selectable, contruct mail address */
881     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
883       if($this->mailMethod->domainSelectionEnabled()){
884         $this->mail = $this->mail."@".$this->mailDomainPart;
885       }
887       /* Enforce lowercase mail address and trim whitespaces
888        */
889       $this->mail = trim(strtolower($this->mail));
890     }
893     /* Call parents save to prepare $this->attrs */
894     plugin::save();
896     /* Save arrays */
897     $this->attrs['gosaMailAlternateAddress'] = $this->gosaMailAlternateAddress;
898     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
900     if(!$this->mailMethod->vacationRangeEnabled()){
901       $this->attrs['gosaVacationStart'] = $this->attrs['gosaVacationStop'] = array();
902     }elseif (!preg_match('/V/', $this->gosaMailDeliveryMode)){
903       unset($this->attrs['gosaVacationStart']);
904       unset($this->attrs['gosaVacationStop']);
905     } else {
906       /* Adapt values to be timestamps */
907       list($day, $month, $year)= explode('.', $this->gosaVacationStart);
908       $this->attrs['gosaVacationStart']= mktime(0,0,0,$month, $day, $year);
909       list($day, $month, $year)= explode('.', $this->gosaVacationStop);
910       $this->attrs['gosaVacationStop']= mktime(0,0,0,$month, $day, $year);
911     }
913     /* Map method attributes */ 
914     $this->mailMethod->fixAttributesOnStore();
915     
916     /* Save data to LDAP */
917     $ldap->cd($this->dn);
918     $this->cleanup();
919     $ldap->modify ($this->attrs);
921     if (!$ldap->success()){
922       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
923     }
925     /* Log last action */
926     if($this->initially_was_account){
927       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
928     }else{
929       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
930     }
932     /* Only do IMAP actions if we are not a template */
933     if (!$this->is_template){
934       $this->mailMethod->connect();
935       if(!$this->mailMethod->is_connected()){
936         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
937               $this->mailMethod->get_error()), ERROR_DIALOG);
938       }else{
939         if(!$this->mailMethod->updateMailbox()){
940           msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"), 
941                 $this->mailMethod->get_error()), ERROR_DIALOG);
942         }
943         if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
944           msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"), 
945                 $this->mailMethod->get_error()), ERROR_DIALOG);
946         }
948         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
950           /* Do not write sieve settings if this account is new and 
951              doesn't seem to exist.
952            */
953           if(!$this->initially_was_account && !$this->mailMethod->account_exists()){
954             @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
955                 "Skipping sieve settings, the account doesn't seem to be created already.</b>","");
956           }else{
957             if(!$this->mailMethod->saveSieveSettings()){
958               msg_dialog::display(_("Mail error saving sieve settings"), $this->mailMethod->get_error(), ERROR_DIALOG);
959             }
960           }
961         }else{
962           if ($this->sieve_management) {
963             @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 
964                 "User uses an own sieve script, skipping sieve update.".$str."</b>","");
965             $this->sieve_management->save();
966           }
967         }
968       }
969     }
970     $this->mailMethod->disconnect();
972     /* Update sharedFolder dependencies.
973        Open each shared folder and remove this account.
974        Then Save the group to ensure that all necessary
975        actions will be taken (imap acls updated aso.).
976      */
977     if(!$this->initially_was_account){
978       $ldap = $this->config->get_ldap_link();
979       $ldap->cd($this->config->current['BASE']);
980       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
981       while($attrs = $ldap->fetch()){
982         $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
983         if(isset($grp->by_object['mailgroup'])){
984           /* Do not save the complete group! This will quit the complete membership
985            */
986           $grp->by_object['mailgroup']->save();
987         }
988       }
989     }
991     /* Optionally execute a command after we're done */
992     if ($this->initially_was_account == $this->is_account){
993       if ($this->is_modified){
994         $this->handle_post_events("modify", array("uid" => $this->uid));
995       }
996     } else {
997       $this->handle_post_events("add", array("uid" => $this->uid));
998     }
999   }
1002   /*! \brief  Check given values 
1003    */
1004   function check()
1005   {
1006     if(!$this->is_account){
1007       return(array());
1008     }
1010     $ldap= $this->config->get_ldap_link();
1012     /* Call common method to give check the hook */
1013     $message= plugin::check();
1015     if(empty($this->gosaMailServer)){
1016       $message[]= msgPool::noserver(_("Mail"));
1017     }
1019     /* Mail address checks */
1020     $mail = $this->mail;
1021     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
1023       if($this->mailMethod->domainSelectionEnabled()){
1024         $mail.= "@".$this->mailDomainPart;
1025       }
1027       if (empty($mail)){
1028         $message[]= msgPool::required(_("Primary address"));
1029       }
1030       if ($this->is_template){
1031         if (!tests::is_email($mail, TRUE)){
1032           $message[]= msgPool::invalid(_("Mail address"),"","","{%givenName}.{%sn}@your-domain.com");
1033         }
1034       } else {
1035         if (!tests::is_email($mail)){
1036           $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
1037         }
1038       }
1040       /* Check if this mail address is already in use */
1041       $ldap->cd($this->config->current['BASE']);
1042       $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
1043         "(objectClass=gosaMailAccount)".
1044         "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
1045       $ldap->search($filter,array("uid", "cn"));
1046       if ($ldap->count() != 0){
1047         $entry= $ldap->fetch();
1048         $addon= "";
1049         if (!isset($entry['uid'])) {
1050            $addon= sprintf(_("Address is already in use by group '%s'."), $entry['cn'][0]);
1051         } else {
1052            $addon= sprintf(_("Address is already in use by user '%s'."), $entry['uid'][0]);
1053         }
1054         $message[]= msgPool::duplicated(_("Mail address"))."<br><br><i>$addon</i>";
1055       }
1056     }
1059     /* Check quota */
1060     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
1061       if (!is_numeric($this->gosaMailQuota)) {
1062         $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
1063       } else {
1064         $this->gosaMailQuota= (int) $this->gosaMailQuota;
1065       }
1066     }
1068     /* Check rejectsize for integer */
1069     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
1070       if (!is_numeric($this->gosaMailMaxSize)){
1071         $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
1072       } else {
1073         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
1074       }
1075     }
1077     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
1078     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && $this->gosaMailMaxSize == ""){
1079       $message[]= msgPool::required(_("Mail reject size"));
1080     }
1082     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
1083       $message[]= msgPool::required(_("Spam folder"));
1084     }
1086     if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){ 
1088       /* Check date strings */
1089       $state= true;
1090       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStart)) {
1091         $message[]= msgPool::invalid(_("from"),$this->gosaVacationStart);
1092         $state= false;
1093       }
1094       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStop)) {
1095         $message[]= msgPool::invalid(_("to"),$this->gosaVacationStop);
1096         $state= false;
1097       }
1099       #TODO: take care of date format
1100       if ($state) {
1101         list($day, $month, $year)= explode('.', $this->gosaVacationStart);
1102         $start= mktime(0,0,0,$month, $day, $year);
1103         list($day, $month, $year)= explode('.', $this->gosaVacationStop);
1104         $stop= mktime(0,0,0,$month, $day, $year);
1105         if($start > $stop){
1106           $message[]= msgPool::invalid(_("Vacation interval"));
1107         }
1108       }
1109     }
1110     return($message);
1111   }
1114   /*! \brief  Adapt from template, using 'dn' 
1115    */
1116   function adapt_from_template($dn, $skip= array())
1117   {
1118     plugin::adapt_from_template($dn, $skip);
1120     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
1122       if (in_array($val, $skip)){
1123         continue;
1124       }
1126       $this->$val= array();
1127       if (isset($this->attrs["$val"]["count"])){
1128         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
1129           $value= $this->attrs["$val"][$i];
1130           foreach (array("sn", "givenName", "uid") as $repl){
1131             if (preg_match("/%$repl/i", $value)){
1132               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
1133             }
1134           }
1135             
1136           // Remove non ASCII charcters
1137           $value = iconv('UTF-8', 'US-ASCII//TRANSLIT', $value);
1138           $value = preg_replace('/[^(\x20-\x7F)]*/','',$value);
1140           // No spaces are allowed here
1141           $value = preg_replace("/[ ]/","", $value);
1142           array_push($this->$val, strtolower(rewrite($value)));
1143         }
1144       }
1145     }
1146     $this->mail= strtolower(rewrite($this->mail));
1148     // Remove non ASCII charcters
1149     $this->mail= iconv('UTF-8', 'US-ASCII//TRANSLIT', $this->mail);
1150     $this->mail= preg_replace('/[^(\x20-\x7F)]*/','',$this->mail);
1152     // No spaces are allowed here
1153     $this->mail = preg_replace("/[ ]/","", $this->mail);
1155     // Fix mail address when using templates
1156     if($this->is_account && $this->mailMethod->domainSelectionEnabled()){
1157       $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
1158       $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
1159       if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
1160         $this->mailDomainParts[] = $this->mailDomainPart;
1161       }
1162     }
1163   }
1166   /*! \brief  Creates the mail part for the copy & paste dialog 
1167    */ 
1168   function getCopyDialog()
1169   {
1170       if(!$this->is_account) return("");
1171       $smarty = get_smarty();
1172       $smarty->assign("mail",$this->mail);
1173       $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1174       $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1176       $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
1177       $smarty->assign("MailDomains", $this->mailDomainParts);
1178       $smarty->assign("MailDomain" , $this->mailDomainPart);
1179       $smarty->assign("MailServers", $this->mailMethod->getMailServers());
1180       $smarty->assign("isModifyableMail"  , $this->mailMethod->isModifyableMail());
1181       $smarty->assign("initially_was_account", $this->initially_was_account);
1183     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
1185       $ret = array();
1186       $ret['status'] = "";
1187       $ret['string'] = $str;
1188       return($ret);
1189   }
1192   /*! \brief  save_object for copy&paste vars 
1193    */  
1194   function saveCopyDialog()
1195   {
1196       if(!$this->is_account) return;
1198       /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
1199       $this->execute();
1200       if(isset($_POST['mail'])){
1201           $this->mail = get_post('mail');
1202       }
1203   }
1206   /*! \brief  Prepare this account to be copied 
1207    */
1208   function PrepareForCopyPaste($source)
1209   {
1210       plugin::PrepareForCopyPaste($source);
1212       /* Reset alternate mail addresses */
1213       $this->gosaMailAlternateAddress = array();
1216       /* If the doamin part is selectable, we have to split the mail address
1217        */
1218       if($this->mailMethod->domainSelectionEnabled()){
1219           $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
1220           $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
1221           if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
1222               $this->mailDomainParts[] = $this->mailDomainPart;
1223           }
1224       }
1226       // Initialize Vacation start/stop times 
1227       // This value is set to 'dd.mm.YYYY' if no times are set in the source object. 
1228       // But if they are set, then this values contain timestamps.
1229       if(isset($source['gosaVacationStart'][0])){
1230           $this->gosaVacationStart= date('d.m.Y', $source['gosaVacationStart'][0]);
1231           $this->gosaVacationStop= date('d.m.Y', $source['gosaVacationStop'][0]);
1232       }
1233   }
1236   /*! \brief  Prepare this class the be useable when editing multiple users at once 
1237    */
1238   function get_multi_edit_values()
1239   {
1240     $ret = plugin::get_multi_edit_values();
1241     if(in_array("gosaMailQuota",$this->multi_boxes)){
1242       $ret['gosaMailQuota'] = $this->gosaMailQuota;
1243     }
1244     $flag_add = $flag_remove = array();
1245     $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
1246     $opts = array(
1247         "R"   => "use_mailsize_limit",
1248         "S"   => "use_spam_filter",
1249         "L"   => "only_local",
1250         "V"   => "use_vacation",
1251         "C"   => "own_script",
1252         "I"   => "drop_own_mails");
1253     foreach($opts as $flag => $post){
1254       if(in_array($post, $this->multi_boxes)){
1255         if(preg_match("/".$flag."/",$tmp)){
1256           $flag_add[] = $flag;
1257         }else{
1258           $flag_remove[] = $flag;
1259         }
1260       }
1261     }
1262     $ret['flag_add'] = $flag_add;
1263     $ret['flag_remove'] = $flag_remove;
1265     if($this->mailMethod->vacationRangeEnabled()){
1266       if(in_array("V",$flag_add)){
1267         $ret['gosaVacationStart'] =  $this->gosaVacationStart = get_post('gosaVacationStart');
1268         $ret['gosaVacationStop'] =  $this->gosaVacationStop = get_post('gosaVacationStop');
1269       }
1270     }
1271     return($ret);
1272   }
1275   /*! \brief  Check given input for multiple user edit 
1276    */
1277   function multiple_check()
1278   {
1279     $message = plugin::multiple_check();
1281     if(empty($this->gosaMailServer) && in_array("gosaMailServer",$this->multi_boxes)){
1282       $message[]= msgPool::noserver(_("Mail"));
1283     }
1285     /* Check quota */
1286     if ($this->gosaMailQuota != ''  && in_array("gosaMailQuota",$this->multi_boxes)){
1287       if (!is_numeric($this->gosaMailQuota)) {
1288         $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
1289       } else {
1290         $this->gosaMailQuota= (int) $this->gosaMailQuota;
1291       }
1292     }
1294     /* Check rejectsize for integer */
1295     if ($this->gosaMailMaxSize != '' && in_array("gosaMailMaxSize",$this->multi_boxes)){
1296       if (!is_numeric($this->gosaMailMaxSize)){
1297         $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
1298       } else {
1299         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
1300       }
1301     }
1303     if(empty($this->gosaSpamMailbox) && in_array("gosaSpamMailbox",$this->multi_boxes)){
1304       $message[]= msgPool::required(_("Spam folder"));
1305     }
1307     if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){ 
1309       /* Check date strings */
1310       $state= true;
1311       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStart)) {
1312         $message[]= msgPool::invalid(_("from"),$this->gosaVacationStart);
1313         $state= false;
1314       }
1315       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStop)) {
1316         $message[]= msgPool::invalid(_("to"),$this->gosaVacationStop);
1317         $state= false;
1318       }
1320       #TODO: take care of date format
1321       if ($state) {
1322         list($day, $month, $year)= explode('.', $this->gosaVacationStart);
1323         $start= mktime(0,0,0,$month, $day, $year);
1324         list($day, $month, $year)= explode('.', $this->gosaVacationStop);
1325         $stop= mktime(0,0,0,$month, $day, $year);
1326         if($start > $stop){
1327           $message[]= msgPool::invalid(_("Vacation interval"));
1328         }
1329       }
1330     }
1331     return($message);
1332   }
1334   
1335   /*! \brief  ...
1336    */
1337   function set_multi_edit_values($values)
1338   {
1339     plugin::set_multi_edit_values($values);
1340     $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
1341     if(isset($values['flag_add'])){
1342       foreach($values['flag_add'] as $flag){
1343         if(!preg_match("/".$flag."/",$tmp)){
1344           $tmp .= $flag;
1345         }
1346       }
1347     }
1348     if(isset($values['flag_remove'])){
1349       foreach($values['flag_remove'] as $flag){
1350         if(preg_match("/".$flag."/",$tmp)){
1351           $tmp = preg_replace("/".$flag."/","",$tmp);
1352         }
1353       }
1354     }
1355     $this->gosaMailDeliveryMode = "[".$tmp."]";
1357     /* Set vacation message and replace placeholder like %givenName
1358      */
1359     if(isset($values['gosaVacationMessage'])){
1360       $this->gosaVacationMessage = $this->prepare_vacation_template($values['gosaVacationMessage']);
1361     }
1362   }
1365   /*! \brief  Initialize plugin to be used as multiple edit class. 
1366    */
1367   function init_multiple_support($attrs,$all)
1368   {
1369     plugin::init_multiple_support($attrs,$all);
1370     if(isset($this->multi_attrs['gosaMailQuota'])){
1371       $this->gosaMailQuota = $this->multi_attrs['gosaMailQuota'];
1372     }
1373   }
1375  
1376   /*! \brief
1377    */
1378   function get_multi_init_values()
1379   {
1380     $attrs = plugin::get_multi_init_values();
1381     $attrs['gosaMailQuota'] = $this->gosaMailQuota;
1382     return($attrs);
1383   }
1386   /*! \brief  Display multiple edit dialog 
1387    */
1388   function multiple_execute()
1389   {
1390     return($this->execute());
1391   }
1393   
1394   /*! \brief  Save posts from multiple edit dialog 
1395    */
1396   function multiple_save_object()
1397   {
1398     plugin::multiple_save_object();
1400     $this->save_object();
1401     foreach(array("only_local","gosaMailForwardingAddress","use_mailsize_limit","drop_own_mails","use_vacation","use_spam_filter") as $attr){
1402       if(isset($_POST["use_".$attr])){
1403         $this->multi_boxes[] = $attr;
1404       }
1405     }
1406   }
1409   /*! \brief  Creates the user names for the add_local_forward dialog
1410    */
1411   function make_name($attrs)
1412   {
1413     $name= "";
1414     if (isset($attrs['sn'][0])){
1415       $name= $attrs['sn'][0];
1416     }
1417     if (isset($attrs['givenName'][0])){
1418       if ($name != ""){
1419         $name.= ", ".$attrs['givenName'][0];
1420       } else {
1421         $name.= $attrs['givenName'][0];
1422       }
1423     }
1424     if ($name != ""){
1425       $name.= " ";
1426     }
1428     return ($name);
1429   }
1432   function allow_remove()
1433   {
1434     $resason = "";
1435     if(!$this->mailMethod->allow_remove($reason)){
1436       return($reason);
1437     }
1438     return("");
1439   }
1443   /*! \brief  ACL settings 
1444    */
1445   static function plInfo()
1446   {
1447       return (array(
1448                   "plShortName"     => _("Mail"),
1449                   "plDescription"   => _("Mail settings"),
1450                   "plSelfModify"    => TRUE,
1451                   "plDepends"       => array("user"),                     // This plugin depends on
1452                   "plPriority"      => 4,                                 // Position in tabs
1453                   "plSection"     => array("personal" => _("My account")),
1454                   "plCategory"    => array("users"),
1455                   "plOptions"       => array(),
1457                   "plRequirements"=> array(
1458                       'ldapSchema' => array('gosaMailAccount' => '>=2.7'),
1459                       'onFailureDisablePlugin' => array(get_class())
1460                       ),
1461     
1462                   "plProperties"  =>
1463                   array(
1464                       array(
1465                           "name"          => "cyrusDeleteMailbox",
1466                           "type"          => "bool",
1467                           "default"       => 'true',
1468                           "description"   => _("Remove mail boxes from the IMAP storage after they their user gets removed."),
1469                           "check"         => "gosaProperty::isBool",
1470                           "migrate"       => "",
1471                           "group"         => "mail",
1472                           "mandatory"     => FALSE
1473                           ),
1474                       array(
1475                           "name"          => "cyrusAutocreateFolders",
1476                           "type"          => "string",
1477                           "default"       => "",
1478                           "description"   => _("Comma separated list of folders to be automatically created on user creation."),
1479                           "check"         => "gosaProperty::isString",
1480                           "migrate"       => "",
1481                           "group"         => "mail",
1482                           "mandatory"     => FALSE
1483                           )
1484                           ),
1487                       "plProvidedAcls"  => array(
1488                               "mail"                      =>  _("Mail address"),
1489                               "gosaMailServer"            =>  _("Mail server"),
1490                               "gosaMailQuota"             =>  _("Quota size"),
1492                               "gosaMailDeliveryModeV"     =>  _("Add vacation information"),  // This is flag of gosaMailDeliveryMode
1493                               "gosaVacationMessage"       =>  _("Vacation message"),
1495                               "gosaMailDeliveryModeS"     =>  _("Use SPAM filter"),           // This is flag of gosaMailDeliveryMode
1496                               "gosaSpamSortLevel"         =>  _("SPAM level"),
1497                               "gosaSpamMailbox"           =>  _("SPAM mail box"),
1499                               "sieveManagement"           =>  _("Sieve management"),
1501                               "gosaMailDeliveryModeR"     =>  _("Reject due to mail size"),    // This is flag of gosaMailDeliveryMode
1502                               "gosaMailMaxSize"           =>  _("Mail max size"),
1504                               "gosaMailForwardingAddress" =>  _("Forwarding address"),
1505                               "gosaMailDeliveryModeL"     =>  _("Local delivery"),            // This is flag of gosaMailDeliveryMode
1506                               "gosaMailDeliveryModeI"     =>  _("No delivery to own mailbox "),     // This is flag of gosaMailDeliveryMode
1507                               "gosaMailAlternateAddress"  =>  _("Mail alternative addresses"),
1509                               "gosaMailForwardingAddress" =>  _("Forwarding address"),
1510                               "gosaMailDeliveryModeC"     =>  _("Use custom sieve script"))   // This is flag of gosaMailDeliveryMode
1511                                   ));
1512   }
1517 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1518 ?>