Code

a4c50e91aad3890d0ed2622b0f0642a538aa71fd
[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.";
7     var $view_logged = FALSE;
9     var $attributes = array(
10             "mailAddress",
11             "mailLocation",
12             "quotaUsage",
13             "quotaSize",
14             "alternateAddresses",
15             "forwardingAddresses",
16             "vacationEnabled",
17             "vacationStart",
18             "vacationStop",
19             "vacationMessage",
20             "mailBoxWarnLimitEnabled",
21             "mailBoxWarnLimitValue",
22             "mailBoxSendSizelimitEnabled",
23             "mailBoxSendSizelimitValue",
24             "mailBoxHardSizelimitEnabled",
25             "mailBoxHardSizelimitValue",
26             "mailBoxAutomaticRemovalEnabled",
27             "mailBoxAutomaticRemovalValue",
28             "localDeliveryOnly",
29             "dropOwnMails"
30             );
32     var $enabledFeatures = array(
33             "quotaUsage"                => TRUE,
34             "quotaSize"                 => TRUE,
35             "mailFilter"                => TRUE,
36             "alternateAddresses"        => TRUE,
37             "forwardingAddresses"       => TRUE,
38             "vacationMessage"           => TRUE,
39             "mailBoxWarnLimit"          => TRUE,
40             "mailBoxSendSizelimit"      => TRUE,
41             "mailBoxHardSizelimit"      => TRUE,
42             "mailBoxAutomaticRemoval"   => TRUE,
43             "localDeliveryOnly"         => TRUE,
44             "dropOwnMails"              => TRUE);
46     var $flagAttrinutes = array("vacationEnabled","mailBoxWarnLimitEnabled","mailBoxSendSizelimitEnabled",
47             "mailBoxHardSizelimitEnabled","mailBoxAutomaticRemovalEnabled","localDeliveryOnly","dropOwnMails");
49     var $mailAddressSelectDialog = NULL;
50     var $filterEditor = NULL;
51     var $filterRules = array();
52     var $vacationTemplates = array();
54     var $mailAddress = "";
55     var $mailLocation = "";
56     var $quotaUsage = 0;
57     var $quotaSize = 0;
58     var $alternateAddresses = array();
59     var $forwardingAddresses = array();
60     var $vacationEnabled = FALSE;
61     var $vacationStart = 0;
62     var $vacationStop = 0;
63     var $vacationMessage = "";
64     var $mailBoxWarnLimitEnabled = FALSE;
65     var $mailBoxWarnLimitValue = 100;
66     var $mailBoxSendSizelimitEnabled = FALSE;
67     var $mailBoxSendSizelimitValue = 100;
68     var $mailBoxHardSizelimitEnabled = FALSE;
69     var $mailBoxHardSizelimitValue = 100;
70     var $mailBoxAutomaticRemovalEnabled = FALSE;
71     var $mailBoxAutomaticRemovalValue = 100;
72     var $localDeliveryOnly = FALSE;
73     var $dropOwnMails = FALSE;
75     function __construct ($config, $dn= NULL)
76     {
77         plugin::plugin($config,$dn); 
79         // Get attributes from parent object 
80         foreach(array("uid","cn") as $attr){
81             if(isset($this->parent->by_object['group']) && isset($this->parent->by_object['group']->$attr)){
82                 $this->$attr = &$this->parent->by_object['group']->$attr;
83             }elseif(isset($this->attrs[$attr])){
84                 $this->$attr = $this->attrs[$attr][0];
85             }
86         }
88         // TODO: Detect if we've a valid groupware account here.
89         $this->is_account = FALSE;
91         // Set vacation start/stop if not set alreasy
92         $this->vacationStart = time();
93         $this->vacationStop = time() + (14 * 60*60*24);
95         // Prepare vacation start/stop time to be initially valid.  
96         $this->vacationStart= date('d.m.Y', $this->vacationStart);
97         $this->vacationStop= date('d.m.Y', $this->vacationStop);
98     }
101     function execute()
102     {
104         // Register plugin execution 
105         $display = plugin::execute();
107         // Log plugin execution.
108         if($this->is_account && !$this->view_logged){
109             $this->view_logged = TRUE;
110             new log("view","users/".get_class($this),$this->dn);
111         }
113         /****************
114           Filter editor
115          ****************/
117         if(isset($_POST['filterEditor_cancel'])) $this->filterEditor = NULL;
118         if(isset($_POST['configureFilter'])){
119             $this->filterEditor = new filterEditor($this->config, $this->filterRules);
120         }
121         $this->dialog = FALSE;
122         if($this->filterEditor instanceOf filterEditor){
123             $this->filterEditor->save_object();
124             $this->dialog = TRUE;
125             return($this->filterEditor->execute());
126         }
129         /****************
130           Account status
131          ****************/
133         if(isset($_POST['modify_state'])){
134             if($this->is_account && $this->acl_is_removeable()){
135                 $this->is_account= FALSE;
136             }elseif(!$this->is_account && $this->acl_is_createable()){
137                 $this->is_account= TRUE;
138             }
139         }
140         if(!$this->multiple_support_active){
141             if (!$this->is_account && $this->parent === NULL){
142                 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
143                     msgPool::noValidExtension(_("Mail"))."</b>";
144                 $display.= back_to_main();
145                 return ($display);
146             }
147             if ($this->parent !== NULL){
148                 if ($this->is_account){ 
149                     $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Mail")),msgPool::featuresEnabled(_("Mail")));
150                 } else {
151                     $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Mail")),msgPool::featuresDisabled(_("Mail")));
152                     return ($display);
153                 }
154             }
155         }
157         /****************
158           Forward addresses 
159          ****************/
161         // Display dialog to select a local fowarder 
162         if (isset($_POST['addLocalForwardingAddress'])){
163             $this->mailAddressSelectDialog=  new mailAddressSelect($this->config, get_userinfo());
164             $this->dialog= TRUE;
165         }
167         // Close dialogs, action was canceled 
168         if (isset($_POST['mailAddressSelect_cancel'])){
169             $this->mailAddressSelectDialog= FALSE;
170             $this->dialog= FALSE;
171         }
173         // Append selected forwarding addresses now.
174         if (isset($_POST['mailAddressSelect_save']) && $this->mailAddressSelectDialog instanceOf mailAddressSelect){
175             if($this->acl_is_writeable("forwardingAddresses")){
176                 $list = $this->mailAddressSelectDialog->save();
177                 foreach ($list as $entry){
178                     $val = $entry['mail'][0];
179                     if (!in_array ($val, $this->alternateAddresses) && $val != $this->mailAddress){
180                         $this->addForwarder($val);
181                         $this->is_modified= TRUE;
182                     }
183                 }
184                 $this->mailAddressSelectDialog= FALSE;
185                 $this->dialog= FALSE;
186             } else {
187                 msg_dialog::display(_("Error"), _("Please select an entry!"), ERROR_DIALOG);
188             }
189         }
191         // Display the address selection dialog.
192         if($this->mailAddressSelectDialog instanceOf mailAddressSelect){
193             $used  = array();
194             $used['mail'] = array_values($this->alternateAddresses);  
195             $used['mail'] = array_merge($used['mail'], array_values($this->forwardingAddresses));  
196             $used['mail'][] = $this->mailAddress;
198             // Build up blocklist
199             session::set('filterBlacklist', $used);
200             return($this->mailAddressSelectDialog->execute());
201         }
203         // Add manually inserted forwarding address.
204         if (isset($_POST['addForwardingAddress'])){
205             if ($_POST['forwardingAddressInput'] != ""){
206                 $address= get_post('forwardingAddressInput');
207                 $valid= FALSE;
208                 if (!tests::is_email($address)){
209                     if (!tests::is_email($address, TRUE)){
210                         if ($this->is_template){
211                             $valid= TRUE;
212                         } else {
213                             msg_dialog::display(_("Error"), msgPool::invalid(_("Mail address"),
214                                         "","","your-address@your-domain.com"),ERROR_DIALOG);
215                         }
216                     }
217                 } elseif ($address == $this->mailAddress || in_array($address, $this->alternateAddresses)) {
218                     msg_dialog::display(_("Error"),_("Cannot add primary address to the list of forwarders!") , ERROR_DIALOG);
219                 } else {
220                     $valid= TRUE;
221                 }
222                 if ($valid){
223                     if($this->acl_is_writeable("forwardingAddresses")){
224                         $this->addForwarder ($address);
225                         $this->is_modified= TRUE;
226                     }
227                 }
228             }
229         }
230         if (isset($_POST['deleteForwardingAddress'])){
231             $this->delForwarder ($_POST['forwardingAddressList']);
232         }
235         /****************
236           Alternate addresses 
237          ****************/
239         // Add manually inserted alternate mail address.
240         if (isset($_POST['addAlternateAddress'])){
241             $valid= FALSE;
242             if (!tests::is_email($_POST['alternateAddressInput'])){
243                 if ($this->is_template){
244                     if (!(tests::is_email($_POST['alternateAddressInput'], TRUE))){
245                         msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
246                                     "","","your-domain@your-domain.com"),ERROR_DIALOG);
247                     } else {
248                         $valid= TRUE;
249                     }
250                 } else {
251                     msg_dialog::display(_("Error"),msgPool::invalid(_("Mail address"),
252                                 "","","your-domain@your-domain.com"),ERROR_DIALOG);
253                 }
254             } else {
255                 $valid= TRUE;
256             }
257             if ($valid && ($user= $this->addAlternate (get_post('alternateAddressInput'))) != ""){
258                 $ui= get_userinfo();
259                 $addon= "";
260                 if ($user[0] == "!") {
261                     $addon= sprintf(_("Address is already in use by group '%s'."), mb_substr($user, 1));
262                 } else {
263                     $addon= sprintf(_("Address is already in use by user '%s'."), $user);
264                 }
265                 msg_dialog::display(_("Error"), msgPool::duplicated(_("Mail address"))."<br><br><i>".
266                         "$addon</i>", ERROR_DIALOG);
267             }
268         }
270         // Remove alternate mail address.
271         if (isset($_POST['deleteAlternateAddress']) && isset($_POST['alternateAddressList'])){
272             $this->delAlternate ($_POST['alternateAddressList']);
273         }
276         /****************
277           SMARTY- Assign smarty variables 
278          ****************/
280         $smarty = get_smarty();
281         foreach($this->attributes as $attr){
282             $smarty->assign($attr, $this->$attr);
283         }
285         $plInfo = $this->plInfo();
286         foreach($plInfo['plProvidedAcls'] as $acl => $name){
287             $smarty->assign($acl."ACL", $this->getacl($acl));
288         }
289         foreach($this->enabledFeatures as $feature => $state){
290             $smarty->assign($feature."_isActive", $state);
291         }
293         $smarty->assign("mailLocations", array("tester"));
294         if (count($this->vacationTemplates)){
295             $smarty->assign("displayTemplateSelector", "true");
296             $smarty->assign("vacationTemplate", set_post($this->vacationTemplate));
297             $smarty->assign("vacationTemplates", set_post($this->vacationTemplates));
298             $smarty->assign("template", set_post(get_post('vacation_template')));
299         } else {
300             $smarty->assign("displayTemplateSelector", "false");
301         }
303         return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
304     }
308     /* Save data to object */
309     function save_object()
310     {
311         if(isset($_POST['groupwarePluginPosted'])){
312             foreach($this->attributes as $attr){
313                 if(isset($_POST[$attr])){
314                     $this->$attr = get_post($attr);
315                 }
316             }
318             foreach($this->flagAttrinutes as $attr){
319                 $this->$attr = isset($_POST[$attr]);
320             }
321         }
322     }
325     /*! \brief  Parse vacation templates and build up an array
326       containing 'filename' => 'description'. 
327       Used to fill vacation dropdown box.
328       @return Array   All useable vacation templates.
329      */ 
330     function get_vacation_templates()
331     {
332         $vct = array();
333         if ($this->config->get_cfg_value("core","vacationTemplateDirectory") != ""){
334             $dir= $this->config->get_cfg_value("core","vacationTemplateDirectory");
335             if (is_dir($dir) && is_readable($dir)){
336                 $dh = opendir($dir);
337                 while($file = readdir($dh)){
338                     $description= "";
339                     if (is_file($dir."/".$file)){
340                         $fh = fopen($dir."/".$file, "r");
341                         $line= fgets($fh, 256);
342                         if (!preg_match('/^DESC:/', $line)){
343                             msg_dialog::display(_("Configuration error"), sprintf(_("No DESC tag in vacation template '%s'!"), $file), ERROR_DIALOG);
344                         }else{
345                             $description= trim(preg_replace('/^DESC:\s*/', '', $line));
346                         }
347                         fclose ($fh);
348                     }
349                     if ($description != ""){
350                         $vct["$dir/$file"]= $description;
351                     }
352                 }
353                 closedir($dh);
354             }
355         }
356         return($vct); 
357     }
360     /*! \brief  Adds the given mail address to the list of mail forwarders 
361      */ 
362     function addForwarder($address)
363     {
364         if(empty($address)) return;
365         if($this->acl_is_writeable("forwardingAddresses")){
366             $this->forwardingAddresses[]= $address;
367             $this->forwardingAddresses= array_unique ($this->forwardingAddresses);
368             sort ($this->forwardingAddresses);
369             reset ($this->forwardingAddresses);
370             $this->is_modified= TRUE;
371         }else{
372             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
373         }
374     }
377     /*! \brief  Removes the given mail address from the list of mail forwarders 
378      */ 
379     function delForwarder($addresses)
380     {
381         if($this->acl_is_writeable("forwardingAddresses")){
382             $this->forwardingAddresses= array_remove_entries ($addresses, $this->forwardingAddresses);
383             $this->is_modified= TRUE;
384         }else{
385             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
386         }
387     }
390     /*! \brief  Add given mail address to the list of alternate adresses ,
391       .          check if this mal address is used, skip adding in this case 
392      */ 
393     function addAlternate($address)
394     {
395         if(empty($address)) return;
396         if($this->acl_is_writeable("alternateAddresses")){
397             $ldap= $this->config->get_ldap_link();
398             $address= strtolower($address);
400             /* Is this address already assigned in LDAP? */
401             $ldap->cd ($this->config->current['BASE']);
402             $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=$address)".
403                     "(alias=$address)(gosaMailAlternateAddress=$address)))", array("uid", "cn"));
404             if ($ldap->count() > 0){
405                 $attrs= $ldap->fetch ();
406                 if (!isset($attrs["uid"])) {
407                     return ("!".$attrs["cn"][0]);
408                 }
409                 return ($attrs["uid"][0]);
410             }
411             if (!in_array($address, $this->alternateAddresses)){
412                 $this->alternateAddresses[]= $address;
413                 $this->is_modified= TRUE;
414             }
415             sort ($this->alternateAddresses);
416             reset ($this->alternateAddresses);
417             return ("");
418         }else{
419             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
420         }
421     }
424     /*! \brief  Removes the given mail address from the alternate addresses list 
425      */ 
426     function delAlternate($addresses)
427     {
428         if($this->acl_is_writeable("alternateAddresses")){
429             $this->alternateAddresses= array_remove_entries ($addresses,$this->alternateAddresses);
430             $this->is_modified= TRUE;
431         }else{
432             msg_dialog::display(_("Permission error"), _("You have no permission to modify these addresses!"), ERROR_DIALOG);
433         }
434     }
437     /*! \brief  Prepare importet vacation string. \
438       .         Replace placeholder like %givenName a.s.o.
439       @param  string  Vacation string
440       @return string  Completed vacation string
441      */
442     private function prepare_vacation_template($contents)
443     {
444         /* Replace attributes */
445         $attrs = array();
446         $obj   = NULL;
447         if(isset($this->parent->by_object['user'])){
448             $attrs  = $this->parent->by_object['user']->attributes;
449             $obj    = $this->parent->by_object['user'];
450         }else{
451             $obj    = new user($this->config,$this->dn);
452             $attrs  = $obj->attributes;
453         }
454         if($obj){
456             /* Replace vacation start and end time */
457             if($this->enabledFeatures['vacationMessage']){
458                 if(preg_match("/%start/",$contents)){
459                     $contents = preg_replace("/%start/",$this->vacationStart,$contents);
460                 }
461                 if(preg_match("/%end/",$contents)){
462                     $contents = preg_replace("/%end/",$this->vacationStop,$contents);
463                 }
464             }else{
465                 if(preg_match("/%start/",$contents)){
466                     $contents = preg_replace("/%start/", _("unknown"),$contents);
467                 }
468                 if(preg_match("/%end/",$contents)){
469                     $contents = preg_replace("/%end/", _("unknown"), $contents);
470                 }
471             }
473             foreach ($attrs as $val){
475                 // We can only replace strings here
476                 if(!is_string($obj->$val)) continue;
478                 if(preg_match("/dateOfBirth/",$val)){
479                     if($obj->use_dob){
480                         $contents= preg_replace("/%$val/",date("Y-d-m",$obj->dateOfBirth),$contents);
481                     }
482                 }else {
483                     $contents= preg_replace("/%$val/",
484                             $obj->$val, $contents);
485                 }
487             }
488         }
489         $contents = ltrim(preg_replace("/^DESC:.*$/m","",$contents),"\n ");
490         return($contents);
491     }
494     function remove_from_parent()
495     {
496         /* Cancel if there's nothing to do here */
497         if (!$this->initially_was_account){
498             return;
499         }
501     }
504     function save()
505     {
506     }
509     /*! \brief  Check given values 
510      */
511     function check()
512     {
513         $mssages = plugin::check();
514         return($message);
515     }
518     /*! \brief  Adapt from template, using 'dn' 
519      */
520     function adapt_from_template($dn, $skip= array())
521     {
522         plugin::adapt_from_template($dn, $skip);
524     }
527     /*! \brief  ACL settings 
528      */
529     static function plInfo()
530     {
531         return (array(
532                     "plShortName"     => _("Groupware"),
533                     "plDescription"   => _("Groupware settings"),
534                     "plSelfModify"    => TRUE,
535                     "plDepends"       => array("user"),                     // This plugin depends on
536                     "plPriority"      => 4,                                 // Position in tabs
537                     "plSection"     => array("personal" => _("My account")),
538                     "plCategory"    => array("users"),
539                     "plOptions"       => array(),
540                     "plProvidedAcls"  => array(
541                         "mailAddress"                   => _("Mail address"),
542                         "mailLocation"                  => _("Mail location"),
543                         "quotaUsage"                    => _("Quota usage"),
544                         "mailFilter"                    => _("Mail filter"),
545                         "quotaSize"                     => _("Quota size"),
546                         "alternateAddresses"            => _("Alternate mail addresses"),
547                         "forwardingAddresses"           => _("Forwarding mail addresses"),
548                         "vacationEnabled"               => _("Vaction switch"),
549                         "vacationStart"                 => _("Vacation start time"),
550                         "vacationStop"                  => _("Vacation stop time"),
551                         "vacationMessage"               => _("Vacation message"),
552                         "mailBoxWarnLimit"              => _("Warn sizelimit"),
553                         "mailBoxSendSizelimit"          => _("Send sizelimit"),
554                         "mailBoxHardSizelimit"          => _("Hard sizelimit"),
555                         "mailBoxAutomaticRemoval"       => _("Automatic mail removal"),
556                         "localDeliveryOnly"             => _("Local delivery only"),
557                         "dropOwnMails"                  => _("Drop own mails")
558                         )
559                     ));
560     }
563 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
564 ?>