Code

d76a31871167c034c4be455e8eb5fd909e519c17
[gosa.git] / gosa-plugins / groupware / personal / groupware / class_groupware.inc
1 <?php
3 class groupware extends plugin
4 {
5     var $plHeadline     = "Mail";
6     var $plDescription  = "GOsa mail extension.";
8     var $attributes = array(
9             "mailAddress",
10             "mailLocation",
11             "quotaUsage",
12             "quotaSize",
13             "alternateAddresses",
14             "forwardingAddresses",
15             "vacationEnabled",
16             "vacationStart",
17             "vacationStop",
18             "vacationMessage",
19             "mailBoxWarnLimitEnabled",
20             "mailBoxWarnLimitValue",
21             "mailBoxSendSizelimitEnabled",
22             "mailBoxSendSizelimitValue",
23             "mailBoxHardSizelimitEnabled",
24             "mailBoxHardSizelimitValue",
25             "mailBoxAutomaticRemovalEnabled",
26             "mailBoxAutomaticRemovalValue",
27             );
29     var $mailAddressSelectDialog = NULL;
30     var $vacationTemplates = array();
32     var $mailAddress = "";
33     var $mailLocation = "";
34     var $quotaUsage = "";
35     var $quotaSize = "";
36     var $alternateAddresses = "";
37     var $forwardingAddresses = "";
38     var $vacationEnabled = FALSE;
39     var $vacationStart = "";
40     var $vacationStop = "";
41     var $vacationMessage = "";
42     var $mailBoxWarnLimitEnabled = FALSE;
43     var $mailBoxWarnLimitValue = "";
44     var $mailBoxSendSizelimitEnabled = FALSE;
45     var $mailBoxSendSizelimitValue = "";
46     var $mailBoxHardSizelimitEnabled = FALSE;
47     var $mailBoxHardSizelimitValue = "";
48     var $mailBoxAutomaticRemovalEnabled = FALSE;
49     var $mailBoxAutomaticRemovalValue = "";
51     function __construct ($config, $dn= NULL)
52     {
53         plugin::plugin($config,$dn); 
55         // Get attributes from parent object 
56         foreach(array("uid","cn") as $attr){
57             if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
58                 $this->$attr = &$this->parent->by_object['group']->$attr;
59             }elseif(isset($this->attrs[$attr])){
60                 $this->$attr = $this->attrs[$attr][0];
61             }
62         }
64         // Prepare vacation start/stop time to be initially valid.  
65         $this->gosaVacationStart= date('d.m.Y', $this->gosaVacationStart);
66         $this->gosaVacationStop= date('d.m.Y', $this->gosaVacationStop);
67     }
70     function execute()
71     {
73         // Register plugin execution 
74         $display = plugin::execute();
76         // Log plugin execution.
77         if($this->is_account && !$this->view_logged){
78             $this->view_logged = TRUE;
79             new log("view","users/".get_class($this),$this->dn);
80         }
83         /****************
84           Account status
85          ****************/
87         if(isset($_POST['modify_state'])){
88             if($this->is_account && $this->acl_is_removeable() && $this->mailMethod->accountRemoveAble()){
89                 $this->is_account= FALSE;
90             }elseif(!$this->is_account && $this->acl_is_createable() && $this->mailMethod->accountCreateable()){
91                 $this->is_account= TRUE;
92             }
93         }
94         if(!$this->multiple_support_active){
95             if (!$this->is_account && $this->parent === NULL){
96                 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
97                     msgPool::noValidExtension(_("Mail"))."</b>";
98                 $display.= back_to_main();
99                 return ($display);
100             }
101             if ($this->parent !== NULL){
102                 if ($this->is_account){ 
103                     $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::featuresEnabled(_("Mail")));
104                 } else {
105                     $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::featuresDisabled(_("Mail")));
106                     return ($display);
107                 }
108             }
109         }
111         /****************
112           Forward addresses 
113          ****************/
115         // Display dialog to select a local fowarder 
116         if (isset($_POST['addLocalForwardingAddress'])){
117             $this->mailAddressSelectDialog=  new mailAddressSelect($this->config, get_userinfo());
118             $this->dialog= TRUE;
119         }
121         // Close dialogs, action was canceled 
122         if (isset($_POST['mailAddressSelect_cancel'])){
123             $this->mailAddressSelectDialog= FALSE;
124             $this->dialog= FALSE;
125         }
127         // Append selected forwarding addresses now.
128         if (isset($_POST['mailAddressSelect_save']) && $this->mailAddressSelectDialog instanceOf mailAddressSelect){
129             if($this->acl_is_writeable("gosaMailForwardingAddress")){
130                 $list = $this->mailAddressSelectDialog->save();
131                 foreach ($list as $entry){
132                     $val = $entry['mail'][0];
133                     if (!in_array ($val, $this->gosaMailAlternateAddress) && $val != $this->mail){
134                         $this->addForwarder($val);
135                         $this->is_modified= TRUE;
136                     }
137                 }
138                 $this->mailAddressSelectDialog= FALSE;
139                 $this->dialog= FALSE;
140             } else {
141                 msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
142             }
143         }
145         // Display the address selection dialog.
146         if($this->mailAddressSelectDialog instanceOf mailAddressSelect){
147             $used  = array();
148             $used['mail'] = array_values($this->gosaMailAlternateAddress);  
149             $used['mail'] = array_merge($used['mail'], array_values($this->gosaMailForwardingAddress));  
150             $used['mail'][] = $this->mail;
152             // Build up blocklist
153             session::set('filterBlacklist', $used);
154             return($this->mailAddressSelectDialog->execute());
155         }
157         // Add manually inserted forwarding address.
158         if (isset($_POST['addForwardingAddress'])){
159             if ($_POST['forwardingAddressInput'] != ""){
160                 $address= get_post('forwardingAddressInput');
161                 $valid= FALSE;
162                 if (!tests::is_email($address)){
163                     if (!tests::is_email($address, TRUE)){
164                         if ($this->is_template){
165                             $valid= TRUE;
166                         } else {
167                             msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
168                                         "","","your-address@your-domain.com"),ERROR_DIALOG);
169                         }
170                     }
171                 } elseif ($address == $this->mail
172                         || in_array($address, $this->gosaMailAlternateAddress)) {
173                     msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
174                 } else {
175                     $valid= TRUE;
176                 }
177                 if ($valid){
178                     if($this->acl_is_writeable("gosaMailForwardingAddress")){
179                         $this->addForwarder ($address);
180                         $this->is_modified= TRUE;
181                     }
182                 }
183             }
184         }
185         if (isset($_POST['deleteForwardingAddress'])){
186             $this->delForwarder ($_POST['forwardingAddressList']);
187         }
190         /****************
191           Alternate addresses 
192          ****************/
194         // Add manually inserted alternate mail address.
195         if (isset($_POST['addAlternateAddress'])){
196             $valid= FALSE;
197             if (!tests::is_email($_POST['alternateAddressInput'])){
198                 if ($this->is_template){
199                     if (!(tests::is_email($_POST['alternateAddressInput'], TRUE))){
200                         msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
201                                     "","","your-domain@your-domain.com"),ERROR_DIALOG);
202                     } else {
203                         $valid= TRUE;
204                     }
205                 } else {
206                     msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
207                                 "","","your-domain@your-domain.com"),ERROR_DIALOG);
208                 }
209             } else {
210                 $valid= TRUE;
211             }
212             if ($valid && ($user= $this->addAlternate (get_post('alternateAddressInput'))) != ""){
213                 $ui= get_userinfo();
214                 $addon= "";
215                 if ($user[0] == "!") {
216                     $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
217                 } else {
218                     $addon= sprintf(_("Address is already in use by user '%s'."), $user);
219                 }
220                 msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
221                         "$addon</i>", ERROR_DIALOG);
222             }
223         }
225         // Remove alternate mail address.
226         if (isset($_POST['deleteAlternateAddress']) && isset($_POST['alternateAddressList'])){
227             $this->delAlternate ($_POST['alternateAddressList']);
228         }
231         /****************
232           SMARTY- Assign smarty variables 
233          ****************/
235         $smarty = get_smarty();
236         foreach($this->attributes as $attr){
237             $smarty->assign($attr, $this->$attr);
238         }
240         /****************
241           SMARTY- Assign flags 
242          ****************/
244         /****************
245           Smarty- Vacation settings 
246          ****************/
248         $smarty->assign("mailLocations", array("tester"));
249         if (count($this->vacationTemplates)){
250             $smarty->assign("displayTemplateSelector", "true");
251             $smarty->assign("vacationTemplate", set_post($this->vacationTemplate));
252             $smarty->assign("vacationTemplates", set_post($this->vacationTemplates));
253             $smarty->assign("template", set_post(get_post('vacation_template')));
254         } else {
255             $smarty->assign("displayTemplateSelector", "false");
256         }
258         return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
259     }
263     /* Save data to object */
264     function save_object()
265     {
266     }
269     /*! \brief  Parse vacation templates and build up an array
270       containing 'filename' => 'description'. 
271       Used to fill vacation dropdown box.
272       @return Array   All useable vacation templates.
273      */ 
274     function get_vacation_templates()
275     {
276         $vct = array();
277         if ($this->config->get_cfg_value("core","vacationTemplateDirectory") != ""){
278             $dir= $this->config->get_cfg_value("core","vacationTemplateDirectory");
279             if (is_dir($dir) && is_readable($dir)){
280                 $dh = opendir($dir);
281                 while($file = readdir($dh)){
282                     $description= "";
283                     if (is_file($dir."/".$file)){
284                         $fh = fopen($dir."/".$file, "r");
285                         $line= fgets($fh, 256);
286                         if (!preg_match('/^DESC:/', $line)){
287                             msg_dialog::display(_("Configuration error"), sprintf(_("No DESC tag in vacation template '%s'!"), $file), ERROR_DIALOG);
288                         }else{
289                             $description= trim(preg_replace('/^DESC:\s*/', '', $line));
290                         }
291                         fclose ($fh);
292                     }
293                     if ($description != ""){
294                         $vct["$dir/$file"]= $description;
295                     }
296                 }
297                 closedir($dh);
298             }
299         }
300         return($vct); 
301     }
304     /*! \brief  Adds the given mail address to the list of mail forwarders 
305      */ 
306     function addForwarder($address)
307     {
308         if(empty($address)) return;
309         if($this->acl_is_writeable("gosaMailForwardingAddress")){
310             $this->gosaMailForwardingAddress[]= $address;
311             $this->gosaMailForwardingAddress= array_unique ($this->gosaMailForwardingAddress);
312             sort ($this->gosaMailForwardingAddress);
313             reset ($this->gosaMailForwardingAddress);
314             $this->is_modified= TRUE;
315         }else{
316             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
317         }
318     }
321     /*! \brief  Removes the given mail address from the list of mail forwarders 
322      */ 
323     function delForwarder($addresses)
324     {
325         if($this->acl_is_writeable("gosaMailForwardingAddress")){
326             $this->gosaMailForwardingAddress= array_remove_entries ($addresses, $this->gosaMailForwardingAddress);
327             $this->is_modified= TRUE;
328         }else{
329             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
330         }
331     }
334     /*! \brief  Add given mail address to the list of alternate adresses ,
335       .          check if this mal address is used, skip adding in this case 
336      */ 
337     function addAlternate($address)
338     {
339         if(empty($address)) return;
340         if($this->acl_is_writeable("gosaMailAlternateAddress")){
341             $ldap= $this->config->get_ldap_link();
342             $address= strtolower($address);
344             /* Is this address already assigned in LDAP? */
345             $ldap->cd ($this->config->current['BASE']);
346             $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)".
347                     "(alias=$address)(gosaMailAlternateAddress=$address)))", array("uid", "cn"));
348             if ($ldap->count() > 0){
349                 $attrs= $ldap->fetch ();
350                 if (!isset($attrs["uid"])) {
351                     return ("!".$attrs["cn"][0]);
352                 }
353                 return ($attrs["uid"][0]);
354             }
355             if (!in_array($address, $this->gosaMailAlternateAddress)){
356                 $this->gosaMailAlternateAddress[]= $address;
357                 $this->is_modified= TRUE;
358             }
359             sort ($this->gosaMailAlternateAddress);
360             reset ($this->gosaMailAlternateAddress);
361             return ("");
362         }else{
363             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
364         }
365     }
368     /*! \brief  Removes the given mail address from the alternate addresses list 
369      */ 
370     function delAlternate($addresses)
371     {
372         if($this->acl_is_writeable("gosaMailAlternateAddress")){
373             $this->gosaMailAlternateAddress= array_remove_entries ($addresses,$this->gosaMailAlternateAddress);
374             $this->is_modified= TRUE;
375         }else{
376             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
377         }
378     }
381     /*! \brief  Prepare importet vacation string. \
382       .         Replace placeholder like %givenName a.s.o.
383       @param  string  Vacation string
384       @return string  Completed vacation string
385      */
386     private function prepare_vacation_template($contents)
387     {
388         /* Replace attributes */
389         $attrs = array();
390         $obj   = NULL;
391         if(isset($this->parent->by_object['user'])){
392             $attrs  = $this->parent->by_object['user']->attributes;
393             $obj    = $this->parent->by_object['user'];
394         }else{
395             $obj    = new user($this->config,$this->dn);
396             $attrs  = $obj->attributes;
397         }
398         if($obj){
400             /* Replace vacation start and end time */
401             if($this->mailMethod->vacationRangeEnabled()){
402                 if(preg_match("/%start/",$contents)){
403                     $contents = preg_replace("/%start/",$this->gosaVacationStart,$contents);
404                 }
405                 if(preg_match("/%end/",$contents)){
406                     $contents = preg_replace("/%end/",$this->gosaVacationStop,$contents);
407                 }
408             }else{
409                 if(preg_match("/%start/",$contents)){
410                     $contents = preg_replace("/%start/", _("unknown"),$contents);
411                 }
412                 if(preg_match("/%end/",$contents)){
413                     $contents = preg_replace("/%end/", _("unknown"), $contents);
414                 }
415             }
417             foreach ($attrs as $val){
419                 // We can only replace strings here
420                 if(!is_string($obj->$val)) continue;
422                 if(preg_match("/dateOfBirth/",$val)){
423                     if($obj->use_dob){
424                         $contents= preg_replace("/%$val/",date("Y-d-m",$obj->dateOfBirth),$contents);
425                     }
426                 }else {
427                     $contents= preg_replace("/%$val/",
428                             $obj->$val, $contents);
429                 }
431             }
432         }
433         $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
434         return($contents);
435     }
438     function remove_from_parent()
439     {
440         /* Cancel if there's nothing to do here */
441         if (!$this->initially_was_account){
442             return;
443         }
445     }
448     function save()
449     {
450     }
453     /*! \brief  Check given values 
454      */
455     function check()
456     {
457         if(!$this->is_account){
458             return(array());
459         }
461         $ldap= $this->config->get_ldap_link();
463         /* Call common method to give check the hook */
464         $message= plugin::check();
466         if(empty($this->gosaMailServer)){
467             $message[]= msgPool::noserver(_("Mail"));
468         }
470         /* Mail address checks */
471         $mail = $this->mail;
472         if(!(!$this->mailMethod->isModifyableMail() && $this->initially_was_account)){
474             if($this->mailMethod->domainSelectionEnabled()){
475                 $mail.= "@".$this->mailDomainPart;
476             }
478             if (empty($mail)){
479                 $message[]= msgPool::required(_("Primary address"));
480             }
481             if ($this->is_template){
482                 if (!tests::is_email($mail, TRUE)){
483                     $message[]= msgPool::invalid(_("Mail address"),"","","%givenName.%sn@your-domain.com");
484                 }
485             } else {
486                 if (!tests::is_email($mail)){
487                     $message[]= msgPool::invalid(_("Mail address"),"","","your-address@your-domain.com");
488                 }
489             }
491             /* Check if this mail address is already in use */
492             $ldap->cd($this->config->current['BASE']);
493             $filter = "(&(!(objectClass=gosaUserTemplate))(!(uid=".$this->uid."))".
494                 "(objectClass=gosaMailAccount)".
495                 "(|(mail=".$mail.")(alias=".$mail.")(gosaMailAlternateAddress=".$mail.")))";
496             $ldap->search($filter,array("uid", "cn"));
497             if ($ldap->count() != 0){
498                 $entry= $ldap->fetch();
499                 $addon= "";
500                 if (!isset($entry['uid'])) {
501                     $addon= sprintf(_("Address is already in use by group '%s'."), $entry['cn'][0]);
502                 } else {
503                     $addon= sprintf(_("Address is already in use by user '%s'."), $entry['uid'][0]);
504                 }
505                 $message[]= msgPool::duplicated(_("Mail address"))."<br><br><i>$addon</i>";
506             }
507         }
510         /* Check quota */
511         if ($this->gosaMailQuota != '' && $this->acl_is_writeable("gosaMailQuota")){
512             if (!is_numeric($this->gosaMailQuota)) {
513                 $message[]= msgPool::invalid(_("Quota size"),$this->gosaMailQuota,"/^[0-9]*/");
514             } else {
515                 $this->gosaMailQuota= (int) $this->gosaMailQuota;
516             }
517         }
519         /* Check rejectsize for integer */
520         if ($this->gosaMailMaxSize != '' && $this->acl_is_writeable("gosaMailMaxSize")){
521             if (!is_numeric($this->gosaMailMaxSize)){
522                 $message[]= msgPool::invalid(_("Mail reject size"),$this->gosaMailMaxSize,"/^[0-9]*/");
523             } else {
524                 $this->gosaMailMaxSize= (int) $this->gosaMailMaxSize;
525             }
526         }
528         /* Need gosaMailMaxSize if use_mailsize_limit is checked */
529         if (is_integer(strpos($this->gosaMailDeliveryMode, "R")) && $this->gosaMailMaxSize == ""){
530             $message[]= msgPool::required(_("Mail reject size"));
531         }
533         if((preg_match("/S/", $this->gosaMailDeliveryMode))&&(empty($this->gosaSpamMailbox))) {
534             $message[]= msgPool::required(_("Spam folder"));
535         }
537         if ($this->mailMethod->vacationRangeEnabled() && preg_match('/V/', $this->gosaMailDeliveryMode)){ 
539             /* Check date strings */
540             $state= true;
541             if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStart)) {
542                 $message[]= msgPool::invalid(_("from"),$this->gosaVacationStart);
543                 $state= false;
544             }
545             if ($this->gosaVacationStart == "" || !tests::is_date($this->gosaVacationStop)) {
546                 $message[]= msgPool::invalid(_("to"),$this->gosaVacationStop);
547                 $state= false;
548             }
550 #TODO: take care of date format
551             if ($state) {
552                 list($day, $month, $year)= explode('.', $this->gosaVacationStart);
553                 $start= mktime(0,0,0,$month, $day, $year);
554                 list($day, $month, $year)= explode('.', $this->gosaVacationStop);
555                 $stop= mktime(0,0,0,$month, $day, $year);
556                 if($start > $stop){
557                     $message[]= msgPool::invalid(_("Vacation interval"));
558                 }
559             }
560         }
561         return($message);
562     }
565     /*! \brief  Adapt from template, using 'dn' 
566      */
567     function adapt_from_template($dn, $skip= array())
568     {
569         plugin::adapt_from_template($dn, $skip);
571     }
574     /*! \brief  ACL settings 
575      */
576     static function plInfo()
577     {
578         return (array(
579                     "plShortName"     => _("Mail"),
580                     "plDescription"   => _("Mail settings"),
581                     "plSelfModify"    => TRUE,
582                     "plDepends"       => array("user"),                     // This plugin depends on
583                     "plPriority"      => 4,                                 // Position in tabs
584                     "plSection"     => array("personal" => _("My account")),
585                     "plCategory"    => array("users"),
586                     "plOptions"       => array(),
587                     "plProvidedAcls"  => array()
588                     ));
589     }
592 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
593 ?>