Code

Added template adaption for mail domain parts. Closes #776
[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  - display_forward_dialog()
27  - remove_from_parent()
28  - save()
29  - check()
30  - adapt_from_template($dn, $skip= array())
31  - getCopyDialog()
32  - saveCopyDialog()
33  - PrepareForCopyPaste($source)
34  - get_multi_edit_values()
35  - multiple_check()
36  - set_multi_edit_values($values)
37  - init_multiple_support($attrs,$all)
38  - get_multi_init_values()
39  - multiple_execute()
40  - multiple_save_object()
41  - make_name($attrs)
42  - plInfo()
45  */
47 class mailAccount extends plugin
48 {
49   /* Definitions */
50   var $plHeadline     = "Mail";
51   var $plDescription  = "This does something";
52   var $view_logged    = FALSE;
53   var $is_account     = FALSE;
54   var $initially_was_account = FALSE;
56   /* GOsa mail attributes */
57   var $mail                               = "";
58   var $gosaVacationStart                  = "";
59   var $gosaVacationStop                   = "";
60   var $gosaMailAlternateAddress           = array();
61   var $gosaMailForwardingAddress          = array();
62   var $gosaMailDeliveryMode               = "[L        ]";
63   var $gosaMailServer                     = "";
64   var $gosaMailQuota                      = "";
65   var $gosaMailMaxSize                    = "";
66   var $gosaVacationMessage                = "";
67   var $gosaSpamSortLevel                  = "";
68   var $gosaSpamMailbox                    = "";
70   /* The methods defaults */
71   var $quotaUsage     = -1; // Means unknown
73   var $mailMethod      = NULL;
74   var $MailDomain      = "";
75   var $sieveManagementUsed = FALSE;
76   var $vacationTemplates = array();
77   var $sieve_management = NULL;
78   var $forward_dialog = FALSE;
79   var $initial_uid    = "";
80   var $mailDomainPart = "";
81   var $mailDomainParts = array();
82   var $MailBoxes = array("INBOX");
84   /* Used LDAP attributes && classes */
85   var $attributes= array(
86       "mail", "gosaMailServer","gosaMailQuota", "gosaMailMaxSize","gosaMailForwardingAddress",
87       "gosaMailDeliveryMode", "gosaSpamSortLevel", "gosaSpamMailbox","gosaMailAlternateAddress",
88       "gosaVacationStart","gosaVacationStop", "gosaVacationMessage", "gosaMailAlternateAddress", 
89       "gosaMailForwardingAddress");
90   var $objectclasses= array("gosaMailAccount");
92   var $multiple_support = TRUE;
94   var $uid = "";
95   var $cn  = "";
98   /*! \brief  Initialize the mailAccount 
99    */
100   function __construct (&$config, $dn= NULL)
101   {
102     plugin::plugin($config,$dn); 
104     /* Get attributes from parent object 
105      */
106     foreach(array("uid","cn") as $attr){
107       if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
108         $this->$attr = &$this->parent->by_object['group']->$attr;
109       }elseif(isset($this->attrs[$attr])){
110         $this->$attr = $this->attrs[$attr][0];
111       }
112     }
114     /* Intialize the used mailMethod
115      */
116     $tmp = new mailMethod($config,$this);
117     $this->mailMethod       = $tmp->get_method();
118     $this->mailMethod->fixAttributesOnLoad();
119     $this->mailDomainParts  = $this->mailMethod->getMailDomains();
120     $this->SpamLevels = $this->mailMethod->getSpamLevels();
122     /* Remember account status 
123      */
124     $this->initially_was_account = $this->is_account;
126     /* Initialize vacation settings, if enabled.
127      */   
128     if(empty($this->gosaVacationStart) && $this->mailMethod->vacationRangeEnabled()){
129       $this->gosaVacationStart = time();
130       $this->gosaVacationStop = time();
131     }
133     /* Read vacation templates 
134      */
135     $this->vacationTemplates = $this->get_vacation_templates();
137     /* Initialize configured values 
138      */ 
139     if($this->is_account){
141       if($this->mailMethod->connect() && $this->mailMethod->account_exists()){
143         /* Read quota */
144         $this->gosaMailQuota = $this->mailMethod->getQuota($this->gosaMailQuota);
145         $this->quotaUsage    = $this->mailMethod->getQuotaUsage($this->quotaUsage);
146         if($this->mailMethod->is_error()){
147           msg_dialog::display(_("Mail error"), sprintf(_("Cannot read quota settings: %s"), 
148                 $this->mailMethod->get_error()), ERROR_DIALOG);
149         }
150         
151         /* Read mailboxes */
152         $this->MailBoxes = $this->mailMethod->getMailboxList($this->MailBoxes);
153         if($this->mailMethod->is_error()){
154           msg_dialog::display(_("Mail error"), sprintf(_("Cannot get list of mailboxes: %s"), 
155                 $this->mailMethod->get_error()), ERROR_DIALOG);
156         }
157           
158       }elseif(!$this->mailMethod->is_connected()){
159         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
160               $this->mailMethod->get_error()), ERROR_DIALOG);
161       }elseif(!$this->mailMethod->account_exists()){
162         msg_dialog::display(_("Mail error"), sprintf(_("Mailbox '%s' doesn't exists on mail server: %s"), 
163               $this->mailMethod->get_account_id(),$this->gosaMailServer), ERROR_DIALOG);
164       }
166       /* If the doamin part is selectable, we have to split the mail address
167        */
168       if(!(!$this->mailMethod->isModifyableMail() && $this->is_account)){
169         if($this->mailMethod->domainSelectionEnabled()){
170           $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
171           $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
172           if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
173             $this->mailDomainParts[] = $this->mailDomainPart;
174           }
175         }
176       }
178       /* Load attributes containing arrays */
179       foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
180         $this->$val= array();
181         if (isset($this->attrs["$val"]["count"])){
182           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
183             array_push($this->$val, $this->attrs["$val"][$i]);
184           }
185         }
186       }
187     }
189     /* Intialize sieveManagement if necessary */
190     if($this->mailMethod->allowSieveManagement()){
191       $this->mailboxList = &$this->MailBoxes;
192       $this->sieve_management = new sieveManagement($this->config,$this->dn,$this,$this->mailMethod->getUAttrib());
193     }
195     /* Get global filter config used in add local forward
196      */
197     if (!session::is_set("mailfilter")){
198       $ui= get_userinfo();
199       $base= get_base_from_people($ui->dn);
200       $mailfilter= array( "depselect"       => $base,
201           "muser"            => "",
202           "regex"           => "*");
203       session::set("mailfilter", $mailfilter);
204     }
206     /* Disconnect mailMethod. Connect on demand later. 
207      */
208     $this->mailMethod->disconnect();
210     /* Convert start/stop dates */
211     #TODO: use date format
212     $this->gosaVacationStart= date('d.m.Y', $this->gosaVacationStart);
213     $this->gosaVacationStop= date('d.m.Y', $this->gosaVacationStop);
214   }
217   function execute()
218   {
220     /* Call parent execute */
221     $display = plugin::execute();
223     /* Log view */
224     if($this->is_account && !$this->view_logged){
225       $this->view_logged = TRUE;
226       new log("view","users/".get_class($this),$this->dn);
227     }
230     /****************
231       Account status
232      ****************/
234     if(isset($_POST['modify_state'])){
235       if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
236         $this->is_account= FALSE;
237       }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
238         $this->is_account= TRUE;
239       }
240     }
241     if(!$this->multiple_support_active){
242       if (!$this->is_account && $this->parent === NULL){
243         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
244           msgPool::noValidExtension(_("Mail"))."</b>";
245         $display.= back_to_main();
246         return ($display);
247       }
248       if ($this->parent !== NULL){
249         if ($this->is_account){ 
250           $reason = "";
251           if(!$this->mailMethod->accountRemoveable($reason)){
252             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
253           }else{
254             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::featuresEnabled(_("Mail")));
255           }
256         } else {
257           $reason = "";
258           if(!$this->mailMethod->accountCreateable($reason)){
259             $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),$reason ,TRUE,TRUE);
260           }else{
261             $display= $this->show_disable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::featuresDisabled(_("Mail")));
262           }
263           return ($display);
264         }
265       }
266     }
268     /****************
269       Sieve Management Dialog
270      ****************/
271     if($this->mailMethod->allowSieveManagement()){
272       if(isset($_POST['sieveManagement'])
273           && preg_match("/C/",$this->gosaMailDeliveryMode)
274           && $this->acl_is_writeable("sieveManagement") 
275           && $this->mailMethod->allowSieveManagement()) {
276         $this->dialog = $this->sieve_management;
277       }
278       if(isset($_POST['sieve_cancel'])){
279         $this->dialog = FALSE;
280       }
281       if(isset($_POST['sieve_finish'])){
282         $this->sieve_management = $this->dialog;
283         $this->dialog = FALSE;
284       }
285       if(is_object($this->dialog)){
286         $this->dialog->save_object();
287         return($this->dialog->execute());
288       }
289     }
291     /****************
292       Forward addresses 
293      ****************/
294     if (isset($_POST['add_local_forwarder'])){
295       $this->forward_dialog= TRUE;
296       $this->dialog= TRUE;
297     }
298     if (isset($_POST['add_locals_cancel'])){
299       $this->forward_dialog= FALSE;
300       $this->dialog= FALSE;
301     }
302     if (isset($_POST['add_locals_finish'])){
303       if (isset($_POST['local_list'])){
304         if($this->acl_is_writeable("gosaMailForwardingAddress")){
305           foreach ($_POST['local_list'] as $val){
306             if (!in_array ($val, $this->gosaMailAlternateAddress) &&
307                 $val != $this->mail){
308               $this->addForwarder($val);
309               $this->is_modified= TRUE;
310             }
311           }
312         }
313         $this->forward_dialog= FALSE;
314         $this->dialog= FALSE;
315       } else {
316         msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
317       }
318     }
319     if (isset($_POST['add_forwarder'])){
320       if ($_POST['forward_address'] != ""){
321         $address= $_POST['forward_address'];
322         $valid= FALSE;
323         if (!tests::is_email($address)){
324           if (!tests::is_email($address, TRUE)){
325             if ($this->is_template){
326               $valid= TRUE;
327             } else {
328               msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
329                     "","","your-address@your-domain.com"),ERROR_DIALOG);
330             }
331           }
332         } elseif ($address == $this->mail
333             || in_array($address, $this->gosaMailAlternateAddress)) {
334           msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
335         } else {
336           $valid= TRUE;
337         }
338         if ($valid){
339           if($this->acl_is_writeable("gosaMailForwardingAddress")){
340             $this->addForwarder ($address);
341             $this->is_modified= TRUE;
342           }
343         }
344       }
345     }
346     if (isset($_POST['delete_forwarder'])){
347       $this->delForwarder ($_POST['forwarder_list']);
348     }
349     if ($this->forward_dialog){
350       return($this->display_forward_dialog());
351     }
354     /****************
355       Alternate addresses 
356      ****************/
358     if (isset($_POST['add_alternate'])){
359       $valid= FALSE;
360       if (!tests::is_email($_POST['alternate_address'])){
361         if ($this->is_template){
362           if (!(tests::is_email($_POST['alternate_address'], TRUE))){
363             msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),"","","your-domain@your-domain.com"),ERROR_DIALOG);
364           } else {
365             $valid= TRUE;
366           }
367         } else {
368           msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),"","","your-domain@your-domain.com"),ERROR_DIALOG);
369         }
370       } else {
371         $valid= TRUE;
372       }
373       if ($valid && ($user= $this->addAlternate ($_POST['alternate_address'])) != ""){
374         $ui= get_userinfo();
375         $addon= "";
376         if ($user[0] == "!") {
377           $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
378         } else {
379           $addon= sprintf(_("Address is already in use by user '%s'."), $user);
380         }
381         msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
382             "$addon</i>", ERROR_DIALOG);
383       }
384     }
385     if (isset($_POST['delete_alternate']) && isset($_POST['alternates_list'])){
386       $this->delAlternate ($_POST['alternates_list']);
387     }
389     /****************
390       SMARTY- Assign smarty variables 
391      ****************/
392     $smarty = get_smarty();
393     $smarty->assign("usePrototype", "true");
394     $smarty->assign("initially_was_account", $this->initially_was_account);
395     $smarty->assign("isModifyableMail"  , $this->mailMethod->isModifyableMail());
396     $smarty->assign("isModifyableServer", $this->mailMethod->isModifyableServer());
397     $smarty->assign("mailEqualsCN", $this->mailMethod->mailEqualsCN());
399     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
400     $tmp  = $this->plInfo();
401     foreach($tmp['plProvidedAcls'] as $name => $transl){
402       $smarty->assign("$name"."ACL", $this->getacl($name,$SkipWrite));
403     }
404     foreach($this->attributes as $attr){
405       $smarty->assign($attr,$this->$attr);
406     }
407     $smarty->assign("quotaEnabled", $this->mailMethod->quotaEnabled());
408     if($this->mailMethod->quotaEnabled()){
409       $smarty->assign("quotaUsage",   mailMethod::quota_to_image($this->quotaUsage,$this->gosaMailQuota));
410       $smarty->assign("gosaMailQuota",$this->gosaMailQuota);
411     }
412     $smarty->assign("domainSelectionEnabled", $this->mailMethod->domainSelectionEnabled());
413     $smarty->assign("MailDomains", $this->mailDomainParts);
414     $smarty->assign("MailDomain" , $this->mailDomainPart);
415     $smarty->assign("MailServers", $this->mailMethod->getMailServers());
416     $smarty->assign("allowSieveManagement", $this->mailMethod->allowSieveManagement());
417     $smarty->assign("own_script",  $this->sieveManagementUsed);
419     /* _Multiple users vars_ */
420     foreach($this->attributes as $attr){
421       $u_attr = "use_".$attr;
422       $smarty->assign($u_attr,in_array($attr,$this->multi_boxes));
423     }
424     foreach(array("only_local","gosaMailForwardingAddress","use_mailsize_limit","drop_own_mails","use_vacation","use_spam_filter") as $attr){
425       $u_attr = "use_".$attr;
426       $smarty->assign($u_attr,in_array($attr,$this->multi_boxes));
427     }
430     /****************
431       SMARTY- Assign flags 
432      ****************/
434     $types = array(
435         "V"=>"use_vacation",
436         "S"=>"use_spam_filter",
437         "R"=>"use_mailsize_limit",
438         "I"=>"drop_own_mails",
439         "C"=>"own_script");
440     foreach($types as $option => $varname){
441       if (preg_match("/".$option."/", $this->gosaMailDeliveryMode)) {
442         $smarty->assign($varname, "checked");
443       } else {
444         $smarty->assign($varname, "");
445       }
446     }
447     if (!preg_match("/L/", $this->gosaMailDeliveryMode)) {
448       $smarty->assign("only_local", "checked");
449     } else {
450       $smarty->assign("only_local", "");
451     }
454     /****************
455       Smarty- Vacation settings 
456      ****************/
457     $smarty->assign("rangeEnabled", FALSE);
458     $smarty->assign("template", "");
459     if (count($this->vacationTemplates)){
460       $smarty->assign("show_templates", "true");
461       $smarty->assign("vacationtemplates", $this->vacationTemplates);
462       if (isset($_POST['vacation_template'])){
463         $smarty->assign("template", $_POST['vacation_template']);
464       }
465     } else {
466       $smarty->assign("show_templates", "false");
467     }
469     /* Vacation range assigments
470      */
471     if($this->mailMethod->vacationRangeEnabled()){
472       $smarty->assign("rangeEnabled", TRUE);
473     }
475     /* fill filter settings 
476      */
477     $smarty->assign("spamlevel", $this->SpamLevels);
478     $smarty->assign("spambox"  , $this->MailBoxes);
480     $smarty->assign("multiple_support",$this->multiple_support_active);  
481     return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
482   }
486   /* Save data to object */
487   function save_object()
488   {
489     if (isset($_POST['mailTab'])){
491       /* Save ldap attributes */
492       $mail   = $this->mail;
493       $server = $this->gosaMailServer;
494       plugin::save_object();
496       if(!$this->mailMethod->isModifyableServer() && $this->initially_was_account){
497         $this->gosaMailServer = $server;
498       }
500       if(!$this->mailMethod->isModifyableMail() && $this->initially_was_account){
501         $this->mail = $mail;
502       }else{
504         /* Get posted mail domain part, if necessary  
505          */
506         if($this->mailMethod->domainSelectionEnabled() && isset($_POST['MailDomain'])){
507           if(in_array(get_post('MailDomain'), $this->mailDomainParts)){
508             $this->mailDomainPart = get_post('MailDomain');
509           }
510         }
511       }
513       /* Import vacation message? 
514        */
515       if (isset($_POST["import_vacation"]) && isset($this->vacationTemplates[$_POST["vacation_template"]])){
516         if($this->multiple_support_active){
517           $contents = ltrim(preg_replace("/^DESC:.*$/m","",file_get_contents($_POST["vacation_template"])));
518         }else{
519           $contents = $this->prepare_vacation_template(file_get_contents($_POST["vacation_template"]));
520         }
521         $this->gosaVacationMessage= htmlspecialchars($contents);
522       }
524       /* Handle flags 
525        */
526       if(isset($_POST['own_script'])){
527         if(!preg_match("/C/",$this->gosaMailDeliveryMode)){
528           $str= preg_replace("/[\[\]]/","",$this->gosaMailDeliveryMode);
529           $this->gosaMailDeliveryMode = "[".$str."C]";
530         }
531       }else{
533         /* Assemble mail delivery mode
534            The mode field in ldap consists of values between braces, this must
535            be called when 'mail' is set, because checkboxes may not be set when
536            we're in some other dialog.
538            Example for gosaMailDeliveryMode [LR        ]
539            L -  Local delivery
540            R -  Reject when exceeding mailsize limit
541            S -  Use spam filter
542            V -  Use vacation message
543            C -  Use custm sieve script
544            I -  Only insider delivery */
546         $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
549         /* Handle delivery flags */
550         if($this->acl_is_writeable("gosaMailDeliveryModeL")){
551           if(!preg_match("/L/",$tmp) && !isset($_POST['only_local'])){
552             $tmp.="L";
553           }elseif(preg_match("/L/",$tmp) && isset($_POST['only_local'])){
554             $tmp = preg_replace("/L/","",$tmp);
555           }
556         }
557         $opts = array(
558             "R"   => "use_mailsize_limit",
559             "S"   => "use_spam_filter",
560             "V"   => "use_vacation",
561             "C"   => "own_script",
562             "I"   => "drop_own_mails");
564         foreach($opts as $flag => $post){
565           if($this->acl_is_writeable("gosaMailDeliveryMode".$flag)){
566             if(!preg_match("/".$flag."/",$tmp) && isset($_POST[$post])){
567               $tmp.= $flag;
568             }elseif(preg_match("/".$flag."/",$tmp) && !isset($_POST[$post])){
569               $tmp = preg_replace("/".$flag."/","",$tmp);
570             }
571           }
572         }
574         $tmp= "[$tmp]";
575         if ($this->gosaMailDeliveryMode != $tmp){
576           $this->is_modified= TRUE;
577         }
578         $this->gosaMailDeliveryMode= $tmp;
580         /* Get start/stop values for vacation scope of application
581          */
582         if($this->mailMethod->vacationRangeEnabled()){
583           if($this->acl_is_writeable("gosaVacationMessage") && preg_match("/V/",$this->gosaMailDeliveryMode)){
584             if(isset($_POST['gosaVacationStart'])){
585               $this->gosaVacationStart = $_POST['gosaVacationStart'];
586             }
587             if(isset($_POST['gosaVacationStop'])){
588               $this->gosaVacationStop = $_POST['gosaVacationStop'];
589             }
590           }
591         }
592       }
593     }
594   }
597   /*! \brief  Parse vacation templates and build up an array
598     containing 'filename' => 'description'. 
599     Used to fill vacation dropdown box.
600     @return Array   All useable vacation templates.
601    */ 
602   function get_vacation_templates()
603   {
604     $vct = array();
605     if ($this->config->get_cfg_value("vacationTemplateDirectory") != ""){
606       $dir= $this->config->get_cfg_value("vacationTemplateDirectory");
607       if (is_dir($dir) && is_readable($dir)){
608         $dh = opendir($dir);
609         while($file = readdir($dh)){
610           $description= "";
611           if (is_file($dir."/".$file)){
612             $fh = fopen($dir."/".$file, "r");
613             $line= fgets($fh, 256);
614             if (!preg_match('/^DESC:/', $line)){
615               msg_dialog::display(_("Configuration error"), sprintf(_("No DESC tag in vacation template '%s'!"), $file), ERROR_DIALOG);
616             }else{
617               $description= trim(preg_replace('/^DESC:\s*/', '', $line));
618             }
619             fclose ($fh);
620           }
621           if ($description != ""){
622             $vct["$dir/$file"]= $description;
623           }
624         }
625         closedir($dh);
626       }
627     }
628     return($vct); 
629   }
632   /*! \brief  Adds the given mail address to the list of mail forwarders 
633    */ 
634   function addForwarder($address)
635   {
636     if(empty($address)) return;
637     if($this->acl_is_writeable("gosaMailForwardingAddress")){
638       $this->gosaMailForwardingAddress[]= $address;
639       $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
640       sort ($this->gosaMailForwardingAddress);
641       reset ($this->gosaMailForwardingAddress);
642       $this->is_modified= TRUE;
643     }else{
644       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
645     }
646   }
649   /*! \brief  Removes the given mail address from the list of mail forwarders 
650    */ 
651   function delForwarder($addresses)
652   {
653     if($this->acl_is_writeable("gosaMailForwardingAddress")){
654       $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
655       $this->is_modified= TRUE;
656     }else{
657       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
658     }
659   }
662   /*! \brief  Add given mail address to the list of alternate adresses ,
663     .          check if this mal address is used, skip adding in this case 
664    */ 
665   function addAlternate($address)
666   {
667     if(empty($address)) return;
668     if($this->acl_is_writeable("gosaMailAlternateAddress")){
669       $ldap= $this->config->get_ldap_link();
670       $address= strtolower($address);
672       /* Is this address already assigned in LDAP? */
673       $ldap->cd ($this->config->current['BASE']);
674       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)".
675           "(alias=$address)(gosaMailAlternateAddress=$address)))", array("uid", "cn"));
676       if ($ldap->count() > 0){
677         $attrs= $ldap->fetch ();
678         if (!isset($attrs["uid"])) {
679           return ("!".$attrs["cn"][0]);
680         }
681         return ($attrs["uid"][0]);
682       }
683       if (!in_array($address, $this->gosaMailAlternateAddress)){
684         $this->gosaMailAlternateAddress[]= $address;
685         $this->is_modified= TRUE;
686       }
687       sort ($this->gosaMailAlternateAddress);
688       reset ($this->gosaMailAlternateAddress);
689       return ("");
690     }else{
691       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
692     }
693   }
696   /*! \brief  Removes the given mail address from the alternate addresses list 
697    */ 
698   function delAlternate($addresses)
699   {
700     if($this->acl_is_writeable("gosaMailAlternateAddress")){
701       $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
702       $this->is_modified= TRUE;
703     }else{
704       msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
705     }
706   }
709   /*! \brief  Prepare importet vacation string. \
710     .         Replace placeholder like %givenName a.s.o.
711     @param  string  Vacation string
712     @return string  Completed vacation string
713    */
714   private function prepare_vacation_template($contents)
715   {
716     /* Replace attributes */
717     $attrs = array();
718     $obj   = NULL;
719     if(isset($this->parent->by_object['user'])){
720       $attrs  = $this->parent->by_object['user']->attributes;
721       $obj    = $this->parent->by_object['user'];
722     }else{
723       $obj    = new user($this->config,$this->dn);
724       $attrs  = $obj->attributes;
725     }
726     if($obj){
728       /* Replace vacation start and end time */
729       if($this->mailMethod->vacationRangeEnabled()){
730         if(preg_match("/%start/",$contents)){
731           $contents = preg_replace("/%start/",$this->gosaVacationStart,$contents);
732         }
733         if(preg_match("/%end/",$contents)){
734           $contents = preg_replace("/%end/",$this->gosaVacationStop,$contents);
735         }
736       }else{
737         if(preg_match("/%start/",$contents)){
738           $contents = preg_replace("/%start/", _("unknown"),$contents);
739         }
740         if(preg_match("/%end/",$contents)){
741           $contents = preg_replace("/%end/", _("unknown"), $contents);
742         }
743       }
745       foreach ($attrs as $val){
746         if(preg_match("/dateOfBirth/",$val)){
747           if($obj->use_dob){
748             $contents= preg_replace("/%$val/",date("Y-d-m",$obj->dateOfBirth),$contents);
749           }
750         }else {
751           $contents= preg_replace("/%$val/",
752               $obj->$val, $contents);
753         }
755       }
756     }
757     $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
758     return($contents);
759   }
762   /*! \brief  Displays a dialog that allows mail address selection.
763    */ 
764   function display_forward_dialog()
765   {
766     restore_error_handler();
768     $smarty = get_smarty();
769     $ldap= $this->config->get_ldap_link();
771     /* Save data */
772     $mailfilter= session::get("mailfilter");
773     foreach( array("depselect", "muser", "regex") as $type){
774       if (isset($_POST[$type])){
775         $mailfilter[$type]= $_POST[$type];
776       }
777     }
778     if (isset($_GET['search'])){
779       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
780       if ($s == "**"){
781         $s= "*";
782       }
783       $mailfilter['regex']= $s;
784     }
785     session::set("mailfilter", $mailfilter);
787     /* Get actual list */
788     $mailusers= array ();
789     if ($mailfilter['regex'] != '*' && $mailfilter['regex'] != ""){
790       $regex= $mailfilter['regex'];
791       $filter= "(|(mail=$regex)(gosaMailAlternateAddress=$regex))";
792     } else {
793       $filter= "";
794     }
795     if ($mailfilter['muser'] != ""){
796       $user= $mailfilter['muser'];
797       $filter= "$filter(|(uid=$user)(cn=$user)(givenName=$user)(sn=$user))";
798     }
800     /* Add already present people to the filter */
801     $exclude= "";
802     foreach ($this->gosaMailForwardingAddress as $mail){
803       $exclude.= "(mail=$mail)";
804     }
805     if ($exclude != ""){
806       $filter.= "(!(|$exclude))";
807     }
808     $res= get_list("(&(objectClass=gosaMailAccount)$filter)", "users", $mailfilter['depselect'],
809         array("sn", "mail", "givenName"), GL_SIZELIMIT | GL_SUBSEARCH);
810     $ldap->cd($mailfilter['depselect']);
811     $ldap->search ("(&(objectClass=gosaMailAccount)$filter)", array("sn", "mail", "givenName"));
812     while ($attrs= $ldap->fetch()){
813       if(preg_match('/%/', $attrs['mail'][0])){
814         continue;
815       }
816       $name= $this->make_name($attrs);
817       $mailusers[$attrs['mail'][0]]= $name."&lt;".
818         $attrs['mail'][0]."&gt;";
819     }
820     natcasesort ($mailusers);
821     reset ($mailusers);
823     /* Show dialog */
824     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
825     $smarty->assign("usearch_image", get_template_path('images/lists/search-user.png'));
826     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
827     $smarty->assign("infoimage", get_template_path('images/info.png'));
828     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
829     $smarty->assign("mailusers", $mailusers);
830     if (isset($_POST['depselect'])){
831       $smarty->assign("depselect", $_POST['depselect']);
832     }
833     $smarty->assign("deplist", $this->config->idepartments);
834     $smarty->assign("apply", apply_filter());
835     $smarty->assign("alphabet", generate_alphabet());
836     $smarty->assign("hint", print_sizelimit_warning());
837     foreach( array("depselect", "muser", "regex") as $type){
838       $smarty->assign("$type", $mailfilter[$type]);
839     }
840     $smarty->assign("hint", print_sizelimit_warning());
841     $display= $smarty->fetch (get_template_path('mail_locals.tpl', TRUE, dirname(__FILE__)));
842     return ($display);
843   }
846   /*! \brief  Removes the mailAccount extension from ldap 
847    */  
848   function remove_from_parent()
849   {
850     /* Cancel if there's nothing to do here */
851     if (!$this->initially_was_account){
852       return;
853     }
855     /* If domain part was selectable, contruct mail address */
856     if($this->mailMethod->domainSelectionEnabled()){
857       $this->mail = $this->mail."@".$this->mailDomainPart;
858     }
860     /* Update sharedFolder dependencies. 
861        Open each shared folder and remove this account. 
862        Then Save the group to ensure that all necessary 
863         actions will be taken (imap acls updated aso.).
864      */
865     $ldap = $this->config->get_ldap_link();    
866     $ldap->cd($this->config->current['BASE']);
867     $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
868     while($attrs = $ldap->fetch()){
869       $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
870       if(isset($grp->by_object['mailgroup']) && isset($grp->by_object['group'])){
871         $grp->by_object['group']->removeUser($this->uid);
873         /* Do not save the complete group! This will quit the complete membership 
874          */
875         $grp->by_object['mailgroup']->save();
876       } 
877     }
879     /* Remove GOsa attributes */
880     plugin::remove_from_parent();
882     /* Zero arrays */
883     $this->attrs['gosaMailAlternateAddress'] = array();
884     $this->attrs['gosaMailForwardingAddress']= array();
887     $this->mailMethod->fixAttributesOnRemove();
888     $this->cleanup();
890     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
891     $ldap= $this->config->get_ldap_link();
892     $ldap->cd($this->dn);
893     $ldap->modify ($this->attrs);
895     /* Add "view" to logging class */
896     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
897     if (!$ldap->success()){
898       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
899     }
900     
901     /* Let the mailMethod remove this mailbox, e.g. from imap and
902        update shared folder membership, ACL may need to be updated. 
903      */
904     if (!$this->is_template){
906       if(!$this->mailMethod->connect()){
907         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
908               $this->mailMethod->get_error()), ERROR_DIALOG);
909       }else{
910         if(!$this->mailMethod->deleteMailbox()){
911           msg_dialog::display(_("Mail error"), sprintf(_("Cannot remove mailbox: %s"), 
912                 $this->mailMethod->get_error()), ERROR_DIALOG);
913         }
914       }
915     }
916     $this->mailMethod->disconnect();
918     /* Optionally execute a command after we're done */
919     $this->handle_post_events("remove",array("uid" => $this->uid));
920   }
923   /*! \brief  Save the mailAccount settings to the ldap database.
924    */
925   function save()
926   {
927     $ldap= $this->config->get_ldap_link();
929     /* If domain part was selectable, contruct mail address */
930     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
932       if($this->mailMethod->domainSelectionEnabled()){
933         $this->mail = $this->mail."@".$this->mailDomainPart;
934       }
936       /* Enforce lowercase mail address and trim whitespaces
937        */
938       $this->mail = trim(strtolower($this->mail));
939     }
942     /* Call parents save to prepare $this->attrs */
943     plugin::save();
945     /* Save arrays */
946     $this->attrs['gosaMailAlternateAddress'] = $this->gosaMailAlternateAddress;
947     $this->attrs['gosaMailForwardingAddress']= $this->gosaMailForwardingAddress;
949     if(!$this->mailMethod->vacationRangeEnabled()){
950       $this->attrs['gosaVacationStart'] = $this->attrs['gosaVacationStop'] = array();
951     }elseif (!preg_match('/V/', $this->gosaMailDeliveryMode)){
952       unset($this->attrs['gosaVacationStart']);
953       unset($this->attrs['gosaVacationStop']);
954     } else {
955       /* Adapt values to be timestamps */
956       list($day, $month, $year)= split('\.', $this->gosaVacationStart);
957       $this->attrs['gosaVacationStart']= mktime(0,0,0,$month, $day, $year);
958       list($day, $month, $year)= split('\.', $this->gosaVacationStop);
959       $this->attrs['gosaVacationStop']= mktime(0,0,0,$month, $day, $year);
960     }
962     /* Map method attributes */ 
963     $this->mailMethod->fixAttributesOnStore();
964     
965     /* Save data to LDAP */
966     $ldap->cd($this->dn);
967     $this->cleanup();
968     $ldap->modify ($this->attrs);
970     if (!$ldap->success()){
971       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
972     }
974     /* Log last action */
975     if($this->initially_was_account){
976       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
977     }else{
978       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
979     }
981     /* Only do IMAP actions if we are not a template */
982     if (!$this->is_template){
983       $this->mailMethod->connect();
984       if(!$this->mailMethod->is_connected()){
985         msg_dialog::display(_("Mail error"), sprintf(_("Mail method cannot connect: %s"), 
986               $this->mailMethod->get_error()), ERROR_DIALOG);
987       }else{
988         if(!$this->mailMethod->updateMailbox()){
989           msg_dialog::display(_("Mail error"), sprintf(_("Cannot update mailbox: %s"), 
990                 $this->mailMethod->get_error()), ERROR_DIALOG);
991         }
992         if(!$this->mailMethod->setQuota($this->gosaMailQuota)){
993           msg_dialog::display(_("Mail error"), sprintf(_("Cannot write quota settings: %s"), 
994                 $this->mailMethod->get_error()), ERROR_DIALOG);
995         }
997         if (!is_integer(strpos($this->gosaMailDeliveryMode, "C"))){
999           /* Do not write sieve settings if this account is new and 
1000              doesn't seem to exist.
1001            */
1002           if(!$this->initially_was_account && !$this->mailMethod->account_exists()){
1003             @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__,
1004                 "Skipping sieve settings, the account doesn't seem to be created already.</b>","");
1005           }else{
1006             if(!$this->mailMethod->saveSieveSettings()){
1007               msg_dialog::display(_("Mail error saving sieve settings"), $this->mailMethod->get_error(), ERROR_DIALOG);
1008             }
1009           }
1010         }else{
1011           if ($this->sieve_management) {
1012             @DEBUG (DEBUG_MAIL, __LINE__, __FUNCTION__, __FILE__, 
1013                 "User uses an own sieve script, skipping sieve update.".$str."</b>","");
1014             $this->sieve_management->save();
1015           }
1016         }
1017       }
1018     }
1019     $this->mailMethod->disconnect();
1021     /* Update sharedFolder dependencies.
1022        Open each shared folder and remove this account.
1023        Then Save the group to ensure that all necessary
1024        actions will be taken (imap acls updated aso.).
1025      */
1026     if(!$this->initially_was_account){
1027       $ldap = $this->config->get_ldap_link();
1028       $ldap->cd($this->config->current['BASE']);
1029       $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaMailAccount)(memberUid=".$this->uid."))",array("dn"));
1030       while($attrs = $ldap->fetch()){
1031         $grp = new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $attrs['dn']);
1032         if(isset($grp->by_object['mailgroup'])){
1033           /* Do not save the complete group! This will quit the complete membership
1034            */
1035           $grp->by_object['mailgroup']->save();
1036         }
1037       }
1038     }
1040     /* Optionally execute a command after we're done */
1041     if ($this->initially_was_account == $this->is_account){
1042       if ($this->is_modified){
1043         $this->handle_post_events("modify", array("uid" => $this->uid));
1044       }
1045     } else {
1046       $this->handle_post_events("add", array("uid" => $this->uid));
1047     }
1048   }
1051   /*! \brief  Check given values 
1052    */
1053   function check()
1054   {
1055     if(!$this->is_account){
1056       return(array());
1057     }
1059     $ldap= $this->config->get_ldap_link();
1061     /* Call common method to give check the hook */
1062     $message= plugin::check();
1064     if(empty($this->gosaMailServer)){
1065       $message[]= msgPool::noserver(_("Mail"));
1066     }
1068     /* Mail address checks */
1069     $mail = $this->mail;
1070     if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
1072       if($this->mailMethod->domainSelectionEnabled()){
1073         $mail.= "@".$this->mailDomainPart;
1074       }
1076       if (empty($mail)){
1077         $message[]= msgPool::required(_("Primary address"));
1078       }
1079       if ($this->is_template){
1080         if (!tests::is_email($mail, TRUE)){
1081           $message[]= msgPool::invalid(_("Mail address"),"","","%givenName.%sn@your-domain.com");
1082         }
1083       } else {
1084         if (!tests::is_email($mail)){
1085           $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
1086         }
1087       }
1089       /* Check if this mail address is already in use */
1090       $ldap->cd($this->config->current['BASE']);
1091       $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
1092         "(objectClass=gosaMailAccount)".
1093         "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
1094       $ldap->search($filter,array("uid", "cn"));
1095       if ($ldap->count() != 0){
1096         $entry= $ldap->fetch();
1097         $addon= "";
1098         if (!isset($entry['uid'])) {
1099            $addon= sprintf(_("Address is already in use by group '%s'."), $entry['cn'][0]);
1100         } else {
1101            $addon= sprintf(_("Address is already in use by user '%s'."), $entry['uid'][0]);
1102         }
1103         $message[]= msgPool::duplicated(_("Mail address"))."<br><br><i>$addon</i>";
1104       }
1105     }
1108     /* Check quota */
1109     if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
1110       if (!is_numeric($this->gosaMailQuota)) {
1111         $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
1112       } else {
1113         $this->gosaMailQuota= (int) $this->gosaMailQuota;
1114       }
1115     }
1117     /* Check rejectsize for integer */
1118     if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
1119       if (!is_numeric($this->gosaMailMaxSize)){
1120         $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
1121       } else {
1122         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
1123       }
1124     }
1126     /* Need gosaMailMaxSize if use_mailsize_limit is checked */
1127     if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && $this->gosaMailMaxSize == ""){
1128       $message[]= msgPool::required(_("Mail reject size"));
1129     }
1131     if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
1132       $message[]= msgPool::required(_("Spam folder"));
1133     }
1135     if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){ 
1137       /* Check date strings */
1138       $state= true;
1139       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStart)) {
1140         $message[]= msgPool::invalid(_("from"),$this->gosaVacationStart);
1141         $state= false;
1142       }
1143       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStop)) {
1144         $message[]= msgPool::invalid(_("to"),$this->gosaVacationStop);
1145         $state= false;
1146       }
1148       #TODO: take care of date format
1149       if ($state) {
1150         list($day, $month, $year)= split('\.', $this->gosaVacationStart);
1151         $start= mktime(0,0,0,$month, $day, $year);
1152         list($day, $month, $year)= split('\.', $this->gosaVacationStop);
1153         $stop= mktime(0,0,0,$month, $day, $year);
1154         if($start > $stop){
1155           $message[]= msgPool::invalid(_("Vacation interval"));
1156         }
1157       }
1158     }
1159     return($message);
1160   }
1163   /*! \brief  Adapt from template, using 'dn' 
1164    */
1165   function adapt_from_template($dn, $skip= array())
1166   {
1167     plugin::adapt_from_template($dn, $skip);
1169     foreach (array("gosaMailAlternateAddress", "gosaMailForwardingAddress") as $val){
1171       if (in_array($val, $skip)){
1172         continue;
1173       }
1175       $this->$val= array();
1176       if (isset($this->attrs["$val"]["count"])){
1177         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
1178           $value= $this->attrs["$val"][$i];
1179           foreach (array("sn", "givenName", "uid") as $repl){
1180             if (preg_match("/%$repl/i", $value)){
1181               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
1182             }
1183           }
1184           array_push($this->$val, strtolower(rewrite($value)));
1185         }
1186       }
1187     }
1188     $this->mail= strtolower(rewrite($this->mail));
1190     // Fix mail address when using templates
1191     if($this->is_account && $this->mailMethod->domainSelectionEnabled()){
1192       $this->mailDomainPart = preg_replace("/^[^@]*+@/","",$this->mail);
1193       $this->mail = preg_replace("/@.*$/","\\1",$this->mail);
1194       if(!in_array($this->mailDomainPart,$this->mailDomainParts)){
1195         $this->mailDomainParts[] = $this->mailDomainPart;
1196       }
1197     }
1198   }
1201   /*! \brief  Creates the mail part for the copy & paste dialog 
1202    */ 
1203   function getCopyDialog()
1204   {
1205     if(!$this->is_account) return("");
1206     $smarty = get_smarty();
1207     $smarty->assign("mail",$this->mail);
1208     $smarty->assign("gosaMailAlternateAddress",$this->gosaMailAlternateAddress);
1209     $smarty->assign("gosaMailForwardingAddress",$this->gosaMailForwardingAddress);
1210     $str = $smarty->fetch(get_template_path("copypaste.tpl",TRUE, dirname(__FILE__)));
1212     $ret = array();
1213     $ret['status'] = "";
1214     $ret['string'] = $str;
1215     return($ret);
1216   }
1218     
1219   /*! \brief  save_object for copy&paste vars 
1220    */  
1221   function saveCopyDialog()
1222   {
1223     if(!$this->is_account) return;
1225     /* Execute to save mailAlternateAddress && gosaMailForwardingAddress */
1226     $this->execute();
1227     if(isset($_POST['mail'])){
1228       $this->mail = $_POST['mail'];
1229     }
1230   }
1232   
1233   /*! \brief  Prepare this account to be copied 
1234    */
1235   function PrepareForCopyPaste($source)
1236   {
1237     plugin::PrepareForCopyPaste($source);
1239     /* Reset alternate mail addresses */
1240     $this->gosaMailAlternateAddress = array();
1241   }
1244   /*! \brief  Prepare this class the be useable when editing multiple users at once 
1245    */
1246   function get_multi_edit_values()
1247   {
1248     $ret = plugin::get_multi_edit_values();
1249     if(in_array("gosaMailQuota",$this->multi_boxes)){
1250       $ret['gosaMailQuota'] = $this->gosaMailQuota;
1251     }
1252     $flag_add = $flag_remove = array();
1253     $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
1254     $opts = array(
1255         "R"   => "use_mailsize_limit",
1256         "S"   => "use_spam_filter",
1257         "L"   => "only_local",
1258         "V"   => "use_vacation",
1259         "C"   => "own_script",
1260         "I"   => "drop_own_mails");
1261     foreach($opts as $flag => $post){
1262       if(in_array($post, $this->multi_boxes)){
1263         if(preg_match("/".$flag."/",$tmp)){
1264           $flag_add[] = $flag;
1265         }else{
1266           $flag_remove[] = $flag;
1267         }
1268       }
1269     }
1270     $ret['flag_add'] = $flag_add;
1271     $ret['flag_remove'] = $flag_remove;
1273     if($this->mailMethod->vacationRangeEnabled()){
1274       if(in_array("V",$flag_add)){
1275         $ret['gosaVacationStart'] =  $this->gosaVacationStart = $_POST['gosaVacationStart'];
1276         $ret['gosaVacationStop'] =  $this->gosaVacationStop = $_POST['gosaVacationStop'];
1277       }
1278     }
1279     return($ret);
1280   }
1283   /*! \brief  Check given input for multiple user edit 
1284    */
1285   function multiple_check()
1286   {
1287     $message = plugin::multiple_check();
1289     if(empty($this->gosaMailServer) && in_array("gosaMailServer",$this->multi_boxes)){
1290       $message[]= msgPool::noserver(_("Mail"));
1291     }
1293     /* Check quota */
1294     if ($this->gosaMailQuota != ''  && in_array("gosaMailQuota",$this->multi_boxes)){
1295       if (!is_numeric($this->gosaMailQuota)) {
1296         $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
1297       } else {
1298         $this->gosaMailQuota= (int) $this->gosaMailQuota;
1299       }
1300     }
1302     /* Check rejectsize for integer */
1303     if ($this->gosaMailMaxSize != '' && in_array("gosaMailMaxSize",$this->multi_boxes)){
1304       if (!is_numeric($this->gosaMailMaxSize)){
1305         $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
1306       } else {
1307         $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
1308       }
1309     }
1311     if(empty($this->gosaSpamMailbox) && in_array("gosaSpamMailbox",$this->multi_boxes)){
1312       $message[]= msgPool::required(_("Spam folder"));
1313     }
1315     if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){ 
1317       /* Check date strings */
1318       $state= true;
1319       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStart)) {
1320         $message[]= msgPool::invalid(_("from"),$this->gosaVacationStart);
1321         $state= false;
1322       }
1323       if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStop)) {
1324         $message[]= msgPool::invalid(_("to"),$this->gosaVacationStop);
1325         $state= false;
1326       }
1328       #TODO: take care of date format
1329       if ($state) {
1330         list($day, $month, $year)= split('\.', $this->gosaVacationStart);
1331         $start= mktime(0,0,0,$month, $day, $year);
1332         list($day, $month, $year)= split('\.', $this->gosaVacationStop);
1333         $stop= mktime(0,0,0,$month, $day, $year);
1334         if($start > $stop){
1335           $message[]= msgPool::invalid(_("Vacation interval"));
1336         }
1337       }
1338     }
1339     return($message);
1340   }
1342   
1343   /*! \brief  ...
1344    */
1345   function set_multi_edit_values($values)
1346   {
1347     plugin::set_multi_edit_values($values);
1348     $tmp= preg_replace("/[^a-z]/i","",$this->gosaMailDeliveryMode);
1349     if(isset($values['flag_add'])){
1350       foreach($values['flag_add'] as $flag){
1351         if(!preg_match("/".$flag."/",$tmp)){
1352           $tmp .= $flag;
1353         }
1354       }
1355     }
1356     if(isset($values['flag_remove'])){
1357       foreach($values['flag_remove'] as $flag){
1358         if(preg_match("/".$flag."/",$tmp)){
1359           $tmp = preg_replace("/".$flag."/","",$tmp);
1360         }
1361       }
1362     }
1363     $this->gosaMailDeliveryMode = "[".$tmp."]";
1365     /* Set vacation message and replace placeholder like %givenName
1366      */
1367     if(isset($values['gosaVacationMessage'])){
1368       $this->gosaVacationMessage = $this->prepare_vacation_template($values['gosaVacationMessage']);
1369     }
1370   }
1373   /*! \brief  Initialize plugin to be used as multiple edit class. 
1374    */
1375   function init_multiple_support($attrs,$all)
1376   {
1377     plugin::init_multiple_support($attrs,$all);
1378     if(isset($this->multi_attrs['gosaMailQuota'])){
1379       $this->gosaMailQuota = $this->multi_attrs['gosaMailQuota'];
1380     }
1381   }
1383  
1384   /*! \brief
1385    */
1386   function get_multi_init_values()
1387   {
1388     $attrs = plugin::get_multi_init_values();
1389     $attrs['gosaMailQuota'] = $this->gosaMailQuota;
1390     return($attrs);
1391   }
1394   /*! \brief  Display multiple edit dialog 
1395    */
1396   function multiple_execute()
1397   {
1398     return($this->execute());
1399   }
1401   
1402   /*! \brief  Save posts from multiple edit dialog 
1403    */
1404   function multiple_save_object()
1405   {
1406     plugin::multiple_save_object();
1408     $this->save_object();
1409     foreach(array("only_local","gosaMailForwardingAddress","use_mailsize_limit","drop_own_mails","use_vacation","use_spam_filter") as $attr){
1410       if(isset($_POST["use_".$attr])){
1411         $this->multi_boxes[] = $attr;
1412       }
1413     }
1414   }
1417   /*! \brief  Creates the user names for the add_local_forward dialog
1418    */
1419   function make_name($attrs)
1420   {
1421     $name= "";
1422     if (isset($attrs['sn'][0])){
1423       $name= $attrs['sn'][0];
1424     }
1425     if (isset($attrs['givenName'][0])){
1426       if ($name != ""){
1427         $name.= ", ".$attrs['givenName'][0];
1428       } else {
1429         $name.= $attrs['givenName'][0];
1430       }
1431     }
1432     if ($name != ""){
1433       $name.= " ";
1434     }
1436     return ($name);
1437   }
1440   function allow_remove()
1441   {
1442     $resason = "";
1443     if(!$this->mailMethod->allow_remove($reason)){
1444       return($reason);
1445     }
1446     return("");
1447   }
1451   /*! \brief  ACL settings 
1452    */
1453   static function plInfo()
1454   {
1455     return (array(
1456           "plShortName"     => _("Mail"),
1457           "plDescription"   => _("Mail settings"),
1458           "plSelfModify"    => TRUE,
1459           "plDepends"       => array("user"),                     // This plugin depends on
1460           "plPriority"      => 4,                                 // Position in tabs
1461           "plSection"     => array("personal" => _("My account")),
1462           "plCategory"    => array("users"),
1463           "plOptions"       => array(),
1465           "plProvidedAcls"  => array(
1466             "mail"                      =>  _("Mail address"),
1467             "gosaMailServer"            =>  _("Mail server"),
1468             "gosaMailQuota"             =>  _("Quota size"),
1470             "gosaMailDeliveryModeV"     =>  _("Add vacation information"),  // This is flag of gosaMailDeliveryMode
1471             "gosaVacationMessage"       =>  _("Vacation message"),
1473             "gosaMailDeliveryModeS"     =>  _("Use spam filter"),           // This is flag of gosaMailDeliveryMode
1474             "gosaSpamSortLevel"         =>  _("Spam level"),
1475             "gosaSpamMailbox"           =>  _("Spam mail box"),
1477             "sieveManagement"           =>  _("Sieve management"),
1479             "gosaMailDeliveryModeR"     =>  _("Reject due to mailsize"),    // This is flag of gosaMailDeliveryMode
1480             "gosaMailMaxSize"           =>  _("Mail max size"),
1482             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1483             "gosaMailDeliveryModeL"     =>  _("Local delivery"),            // This is flag of gosaMailDeliveryMode
1484             "gosaMailDeliveryModeI"     =>  _("No delivery to own mailbox "),     // This is flag of gosaMailDeliveryMode
1485             "gosaMailAlternateAddress"  =>  _("Mail alternative addresses"),
1487             "gosaMailForwardingAddress" =>  _("Forwarding address"),
1488             "gosaMailDeliveryModeC"     =>  _("Use custom sieve script"))   // This is flag of gosaMailDeliveryMode
1489               ));
1490   }
1495 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1496 ?>