Code

234a373b27fea45a67fabba0ca14792aeb9baaa8
[gosa.git] / plugins / personal / scalix / class_scalixAccount.inc
1 <?php
2 /*! \brief   scalix plugin
3   \author  Jörn Dreyer <gosa.jfd@butonic.de>,
4            based on work by Cajus Pollmeier <pollmeier@gonicus.de>
5   \version 0.01
6   \date    20.10.2006
8   This class provides the functionality to read and write all attributes
9   relevant for scalixAccounts from/to the LDAP. It does syntax checking
10   and displays the formulars required.
11  */
13 class scalixAccount extends plugin
14 {
15   /* Definitions */
16   var $plHeadline   = "Scalix";
17   var $plDescription= "This does something";
19   /* plugin specific values - scalixUserClass */
20   /* MUST */
21   var $scalixScalixObject= TRUE; //TRUE or FALSE
22   var $scalixMailnode= "";
23   /* INDIRECT MUST, scalix will complain if no email was specified */
24   
25   /* MAY */
26   var $scalixAdministrator= FALSE;
27   var $scalixMailboxAdministrator = FALSE;
28   var $scalixServerLanguage= "";
29   var $scalixEmailAddress= array();
30   var $scalixLimitMailboxSize= "";
31   var $scalixLimitOutboundMail= FALSE;
32   var $scalixLimitInboundMail= FALSE;
33   var $scalixLimitNotifyUser= FALSE;
34   var $scalixHideUserEntry= FALSE;
35   var $scalixMailboxClass= "";
37   var $uid  ="";
38   var $default_permissions= "none";
39   var $member_permissions= "post";
40   var $members= array();
41   var $admins= array();
42   var $vacations= array();
43   var $perms= array(  "lrs"       => "read", 
44                       "lrsp"      => "post", 
45                       "lrsip"     => "append",
46                       "lrswipcd"  => "write", 
47                       "lrswipcda" => "all" );
49   /* attribute list for save action */
50   var $attributes           = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
51   var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
52                                     "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
53   var $objectclasses        = array("scalixUserClass");
56   /* constructor, if 'dn' is set, the node loads the given
57      'dn' from LDAP */
58   function scalixAccount ($config, $dn= NULL)
59   {
60     /* dn shouldn't be NULL */
61     if($dn == NULL){
62       trigger_error("Initialising scalixAccount without valid dn.");
63     }
65     /* Load bases attributes */
66     plugin::plugin($config, $dn);
68     /* Load attributes */
69     if ($dn != "new"){
71       /* Set user id */
72       if(isset($this->attrs['uid'][0])){
73         $this->uid = $this->attrs['uid'][0];
74       }
75         
76       foreach ($this->fakeBooleanAttributes as $val){
77         if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
78           $this->$val = TRUE;
79         } else {
80           $this->$val = FALSE;
81         }
82       }
84       /* Load attributes containing arrays */
85       foreach (array("scalixEmailAddress") as $val){
86         if (isset($this->attrs["$val"]["count"])){
87           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
88             array_push($this->$val, $this->attrs["$val"][$i]);
89           }
90         }
91       }
92     }
94     /* Save initial account state */
95     $this->initially_was_account= $this->is_account;
96   }
99   function execute()
100   {
101     /* Call parent execute */
102     plugin::execute();
104     /* Load templating engine */
105     $smarty= get_smarty();
106     $display= "";
108     /* Do we need to flip is_account state? */
109     if (isset($_POST['modify_state'])){
110       $this->is_account= !$this->is_account;
111     }
113     /* Show main page */
114     $mailboxClasses = array("", "LIMITED", "FULL");
115     $serverLanguages= array("", "GERMAN", "ENGLISH");
117     /* Do we represent a valid account? */
118     if (!$this->is_account && $this->parent == NULL){
119       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
120         _("This account has no scalix extensions.")."</b>";
122       $display.= back_to_main();
123       return ($display);
124     }
126     /* Show tab dialog headers */
127     if ($this->parent != NULL){
128       if ($this->is_account){
129         $display= $this->show_disable_header(_("Remove scalix account"),
130             _("This account has scalix synchronization enabled. You can disable it by clicking below."));
131       } else {
132         $display= $this->show_enable_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
133         return ($display);
134       }
135     }
137     /* Trigger forward add dialog? */
138     if (isset($_POST['add_local_forwarder'])){
139       $this->forward_dialog= TRUE;
140       $this->dialog= TRUE;
141     }
143     /* Cancel forward add dialog? */
144     if (isset($_POST['add_locals_cancel'])){
145       $this->forward_dialog= FALSE;
146       $this->dialog= FALSE;
147     }
150     $smarty->assign("mailboxClasses", $mailboxClasses);
151     $smarty->assign("serverLanguages", $serverLanguages);
152     foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
153       "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
154       "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
156       $smarty->assign("$val", $this->$val);
157       $smarty->assign("$val"."ACL", $this->getacl($val));
158     }
160     /* Fill checkboxes */
161     if ($this->scalixAdministrator) {
162       $smarty->assign("scalixAdministrator", "checked");
163     } else {
164       $smarty->assign("scalixAdministrator", "");
165     }
166     if ($this->scalixMailboxAdministrator) {
167       $smarty->assign("scalixMailboxAdministrator", "checked");
168     } else {
169       $smarty->assign("scalixMailboxAdministrator", "");
170     }
171     if ($this->scalixLimitOutboundMail) {
172       $smarty->assign("scalixLimitOutboundMail", "checked");
173     } else {
174       $smarty->assign("scalixLimitOutboundMail", "");
175     }
176     if ($this->scalixLimitInboundMail) {
177       $smarty->assign("scalixLimitInboundMail", "checked");
178     } else {
179       $smarty->assign("scalixLimitInboundMail", "");
180     }
181     if ($this->scalixLimitNotifyUser) {
182       $smarty->assign("scalixLimitNotifyUser", "checked");
183     } else {
184       $smarty->assign("scalixLimitNotifyUser", "");
185     }
186     if ($this->scalixHideUserEntry) {
187       $smarty->assign("scalixHideUserEntry", "checked");
188     } else {
189       $smarty->assign("scalixHideUserEntry", "");
190     }
192     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
193     return ($display);
194   }
197   /* remove object from parent */
198   function remove_from_parent()
199   {
200     /* Cancel if there's nothing to do here */
201     if (!$this->initially_was_account){
202       return;
203     }
204     
205     /* include global link_info */
206     $ldap= $this->config->get_ldap_link();
208     /* Remove and write to LDAP */
209     plugin::remove_from_parent();
211     /* Zero arrays */
212     $this->attrs['scalixEmailAddress']= array();
214     /* Unset fake boolean attributes from entry */
215     foreach ($this->fakeBooleanAttributes as $val){
216       $this->attrs["$val"]= array();
217     }
219     /*unset scalixScalixObject*/
220     $this->attrs['scalixScalixObject']=array();
222     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
223     $ldap->cd($this->dn);
224     $ldap->modify($this->attrs);
225     show_ldap_error($ldap->get_error());
227     /* Optionally execute a command after we're done */
228     $this->handle_post_events("remove");
229   }
232   /* Save data to object */
233   function save_object()
234   {
235     if (isset($_POST['scalixTab'])){
237       /* Save ldap attributes */
238       plugin::save_object();
240       /* Save fakeBooleanAttributes*/
241       foreach ($this->fakeBooleanAttributes as $val){
242         if ($this->acl_is_writeable($val)) {
243           if( isset ($_POST["$val"])) {
244             $data = TRUE;
245           } else {
246             $data = FALSE;
247           }
248           if ($this->$val != $data){
249             $this->is_modified= TRUE;
250           }
252           $this->$val= $data;
253         }
254       }
255     }
256   }
259   /* Save data to LDAP, depending on is_account we save or delete */
260   function save()
261   {
262     $ldap= $this->config->get_ldap_link();
264     /* Call parents save to prepare $this->attrs */
265     plugin::save();
267     /* Save arrays */
268     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
269     
270     /* Save boolean vars ... the scalix schema expects them as strings */
271     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
272     $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
273     $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
274     $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
275     $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
276     $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
277     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
279     /* Remove Mailquota if = "" */
280     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
281       $this->attrs['scalixLimitMailboxSize']=array();
282     }
284     /* Save data to LDAP */
285     $ldap->cd($this->dn);
286     $ldap->modify($this->attrs);
287     show_ldap_error($ldap->get_error());
289     /* Optionally execute a command after we're done */
290     if ($this->initially_was_account == $this->is_account){
291       if ($this->is_modified){
292         $this->handle_post_events("modify");
293       }
294     } else {
295       $this->handle_post_events("add");
296     }
298   }
300   /* Check formular input */
301   function check()
302   {
303     $ldap= $this->config->get_ldap_link();
305     $message= array();
307     if(empty($this->scalixMailnode)){
308       $message[]= _("There is no scalix mailnode specified.");
309     }
310     if(empty($this->scalixScalixObject)){
311       $message[]= _("scalixScalixObject must be set!");
312     }
314     $ldap->cd($this->config->current['BASE']);
315     foreach($this->scalixEmailAddress as $k => $mail) {         
316       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
317         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
318   
319       if ($ldap->count() != 0){
320         $message[]= _("The email address '$mail' you've entered is already in use.");
321       }
322     }
324     /* Check quota */
325     if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
326       if (!is_numeric($this->scalixLimitMailboxSize)) {
327         $message[]= _("Value in 'Limit Mailbox size' is not valid.");
328       } else {
329         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
330       }
331     }
333     return ($message);
334   }
336   /* Adapt from template, using 'dn' */
337   function adapt_from_template($dn)
338   {
339     plugin::adapt_from_template($dn);
341     foreach (array("scalixEmailAddress") as $val){
342       $this->$val= array();
343       if (isset($this->attrs["$val"]["count"])){
344         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
345           $value= $this->attrs["$val"][$i];
346           foreach (array("sn", "givenName", "uid") as $repl){
347             if (preg_match("/%$repl/i", $value)){
348               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
349             }
350           }
351           array_push($this->$val, strtolower(rewrite($value)));
352         }
353       }
354     }
355     $this->mail= strtolower(rewrite($this->mail));
356   }
358   function addEmail($address)
359   {
360     $ldap= $this->config->get_ldap_link();
362     $address= strtolower($address);
364     /* Is this address already assigned in LDAP? */
365     $ldap->cd ($this->config->current['BASE']);
366     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
367         "(scalixEmailAddress=$address)))", array("uid"));
369     if ($ldap->count() > 0){
370       $attrs= $ldap->fetch ();
371       return ($attrs["uid"][0]);
372     }
374     /* Add to list of alternates */
375     if (!in_array($address, $this->scalixEmailAddress)){
376       $this->scalixEmailAddress[]= $address;
377       $this->is_modified= TRUE;
378     }
380     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
381     reset ($this->scalixEmailAddress);
383     return ("");
384   }
387   function delEmail($addresses)
388   {
389     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
390     $this->is_modified= TRUE;
391   }
394   function plInfo()
395   {
396     return (array(
397           "plDescription"     => _("Scalix account"),
398           "plSelfModify"      => TRUE,
399           "plDepends"         => array("user"),
400           "plPriority"        => 2,
401           "plSection"         => array("personal" => _("My account")),
402           "plCategory"        => array("users"),
403           "plOptions"         => array(),
405           "plProvidedAcls"  => array(
407             "scalixMailnode"            => _("Scalix Mail node"),
408             "scalixMailboxClass"        => _("Mailbox class"),
409             "scalixServerLanguage"      => _("Server language"),
410             "scalixAdministrator"       => _("Administrator"),
411             "scalixMailboxAdministrator"=> _("Mailbox administrator"),
412             "scalixHideUserEntry"       => _("Hide user entry in Scalix"),
413             "scalixLimitMailboxSize"    => _("Mailbox size limitations"),
414             "scalixLimitOutboundMail"   => _("Limit outbound"),
415             "scalixLimitInboundMail"    => _("Limit inbound"),
416             "scalixLimitNotifyUser"     => _("Notify user"),
417             "scalixEmailAddress"        => _("Scalix email addresses"))
419             ));
420   }
423   function saveCopyDialog()
424   {
425     if (isset($_POST['scalixTab'])){
427       /* Add email addresses */
428       if (isset($_POST['add_email'])){
430         /* Check write access */
431 #        if(!$this->acl_is_writeable("scalixEmailAddress")){
432 #          print_red(_("You are not allowed to modify alternate addresses"));
433 #        }else{
435           /* Check if given value is not empty */
436           if ($_POST['email_address'] != ""){
437             $valid= FALSE;
439             /* Valid mail address */
440             if( ($this->is_template && !is_email($_POST['email_address'], TRUE)) || 
441                 (!$this->is_template && !is_email($_POST['email_address'])) ){
442               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
443             }else{
444               $valid = TRUE;
445             }
447             /* Mail address already in use ? */
448             if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
449               $ui= get_userinfo();
450               if ($user != $ui->username){
451                 print_red (_("The address you're trying to add is already used by user")." '$user'.");
452               }
453             }
454 #          }
455         }
456       }
457   
458       /* Delete email addresses */
459       if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
460 #        if(!$this->acl_is_writeable("scalixEmailAddress")){
461 #          print_red(_("You are not allowed to modify alternate addresses"));
462 #        }else{
463           if (count($_POST['emails_list'])){
464             $this->delEmail ($_POST['emails_list']);
465 #          }
466         }
467       }
468     }
469   }
472   function PrepareForCopyPaste($source)
473   {
474     plugin::PrepareForCopyPaste($source);
475     foreach ($this->fakeBooleanAttributes as $val){
476       if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
477         $this->$val = TRUE;
478       } else {
479         $this->$val = FALSE;
480       }
481     }
483     /* Load attributes containing arrays */
484     foreach (array("scalixEmailAddress") as $val){
485       if (isset($source["$val"]["count"])){
486         for ($i= 0; $i<$source["$val"]["count"]; $i++){
487           array_push($this->$val, $source["$val"][$i]);
488         }
489       }
490     }
491   }
494   /* Create the posix dialog part for copy & paste */
495   function getCopyDialog()
496   {
497     $smarty = get_smarty(); 
498     $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
499     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));    
500     $ret = array();
501     $ret['string'] = $str;
502     $ret['status'] = "";
503     return($ret);
504  
505   }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:rulere
509 ?>