Code

Added scalix group support
[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= "";
24   /* Indicates whether we have a scalix group or a sclix user account 
25    */
26         var $scalixGroup = FALSE;
27   
28   /* INDIRECT MUST, scalix will complain if no email was specified */
29   /* MAY */
30   var $scalixAdministrator= FALSE;
31   var $scalixMailboxAdministrator = FALSE;
32   var $scalixServerLanguage= "";
33   var $scalixEmailAddress= array();
34   var $scalixLimitMailboxSize= "";
35   var $scalixLimitOutboundMail= FALSE;
36   var $scalixLimitInboundMail= FALSE;
37   var $scalixLimitNotifyUser= FALSE;
38   var $scalixHideUserEntry= FALSE;
39   var $scalixMailboxClass= "";
41   var $uid  ="";
42   var $default_permissions= "none";
43   var $member_permissions= "post";
44   var $members= array();
45   var $admins= array();
46   var $vacations= array();
47   var $perms= array(  "lrs"       => "read", 
48                       "lrsp"      => "post", 
49                       "lrsip"     => "append",
50                       "lrswipcd"  => "write", 
51                       "lrswipcda" => "all" );
53   /* attribute list for save action */
54   var $attributes           = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
55   var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
56                                     "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
57   var $objectclasses        = array("scalixUserClass");
60   /* constructor, if 'dn' is set, the node loads the given
61      'dn' from LDAP */
62   function scalixAccount ($config, $dn= NULL)
63   {
64     /* dn shouldn't be NULL */
65     if($dn == NULL){
66       trigger_error("Initialising scalixAccount without valid dn.");
67     }
69     /* Load bases attributes */
70     plugin::plugin($config, $dn);
72     /* Load attributes */
73     if ($dn != "new"){
75       /* Set user id */
76       if(isset($this->attrs['uid'][0])){
77         $this->uid = $this->attrs['uid'][0];
78       }
79         
80       foreach ($this->fakeBooleanAttributes as $val){
81         if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
82           $this->$val = TRUE;
83         } else {
84           $this->$val = FALSE;
85         }
86       }
88       /* Load attributes containing arrays */
89       foreach (array("scalixEmailAddress") as $val){
90         if (isset($this->attrs["$val"]["count"])){
91           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
92             array_push($this->$val, $this->attrs["$val"][$i]);
93           }
94         }
95       }
96     }
98     /* Save initial account state */
99     $this->initially_was_account= $this->is_account;
100   }
103   function execute()
104   {
105     /* Call parent execute */
106     plugin::execute();
108     /* Load templating engine */
109     $smarty= get_smarty();
110     $smarty->assign("scalixGroup",$this->scalixGroup);
111     $display= "";
113     /* Do we need to flip is_account state? */
114     if (isset($_POST['modify_state'])){
115       $this->is_account= !$this->is_account;
116     }
118     /* Show main page */
119     $mailboxClasses = array("", "LIMITED", "FULL");
120     $serverLanguages= array("", "GERMAN", "ENGLISH");
122     /* Do we represent a valid account? */
123     if (!$this->is_account && $this->parent == NULL){
124       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
125         _("This account has no scalix extensions.")."</b>";
127       $display.= back_to_main();
128       return ($display);
129     }
131     /* Show tab dialog headers */
132     if ($this->parent != NULL){
133       if ($this->is_account){
134         $display= $this->show_header(_("Remove scalix account"),
135             _("This account has scalix synchronization enabled. You can disable it by clicking below."));
136       } else {
137         $display= $this->show_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
138         return ($display);
139       }
140     }
142     /* Trigger forward add dialog? */
143     if (isset($_POST['add_local_forwarder'])){
144       $this->forward_dialog= TRUE;
145       $this->dialog= TRUE;
146     }
148     /* Cancel forward add dialog? */
149     if (isset($_POST['add_locals_cancel'])){
150       $this->forward_dialog= FALSE;
151       $this->dialog= FALSE;
152     }
154     /* Add email addresses */
155     if (isset($_POST['add_email'])){
156       if ($_POST['email_address'] != "" &&
157           chkacl ($this->acl, "scalixEmailAddress") == ""){
159         $valid= FALSE;
160         if (!is_email($_POST['email_address'])){
161           if ($this->is_template){
162             if (!(is_email($_POST['email_address'], TRUE))){
163               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
164             } else {
165               $valid= TRUE;
166             }
167           } else {
168             print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
169           }
171         } else {
172           $valid= TRUE;
173         }
175         if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
176           $ui= get_userinfo();
177           if ($user != $ui->username){
178             print_red (_("The address you're trying to add is already used by user")." '$user'.");
179           }
180         }
181       }
182     }
184     /* Delete email addresses */
185     if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
186       if (count($_POST['emails_list']) &&
187           chkacl ($this->acl, "scalixEmailAddress") == ""){
189         $this->delEmail ($_POST['emails_list']);
190       }
191     }
193     $smarty->assign("mailboxClasses", $mailboxClasses);
194     $smarty->assign("serverLanguages", $serverLanguages);
195     foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
196       "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
197       "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
199       $smarty->assign("$val", $this->$val);
200       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
201     }
203     /* Fill checkboxes */
204     if ($this->scalixAdministrator) {
205       $smarty->assign("scalixAdministrator", "checked");
206     } else {
207       $smarty->assign("scalixAdministrator", "");
208     }
209     if ($this->scalixMailboxAdministrator) {
210       $smarty->assign("scalixMailboxAdministrator", "checked");
211     } else {
212       $smarty->assign("scalixMailboxAdministrator", "");
213     }
214     if ($this->scalixLimitOutboundMail) {
215       $smarty->assign("scalixLimitOutboundMail", "checked");
216     } else {
217       $smarty->assign("scalixLimitOutboundMail", "");
218     }
219     if ($this->scalixLimitInboundMail) {
220       $smarty->assign("scalixLimitInboundMail", "checked");
221     } else {
222       $smarty->assign("scalixLimitInboundMail", "");
223     }
224     if ($this->scalixLimitNotifyUser) {
225       $smarty->assign("scalixLimitNotifyUser", "checked");
226     } else {
227       $smarty->assign("scalixLimitNotifyUser", "");
228     }
229     if ($this->scalixHideUserEntry) {
230       $smarty->assign("scalixHideUserEntry", "checked");
231     } else {
232       $smarty->assign("scalixHideUserEntry", "");
233     }
235     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
236     return ($display);
237   }
240   /* remove object from parent */
241   function remove_from_parent()
242   {
243     /* Cancel if there's nothing to do here */
244     if (!$this->initially_was_account){
245       return;
246     }
247     
248     /* include global link_info */
249     $ldap= $this->config->get_ldap_link();
251     /* Remove and write to LDAP */
252     plugin::remove_from_parent();
254     /* Zero arrays */
255     $this->attrs['scalixEmailAddress']= array();
257     /* Unset fake boolean attributes from entry */
258     foreach ($this->fakeBooleanAttributes as $val){
259       $this->attrs["$val"]= array();
260     }
262     /*unset scalixScalixObject*/
263     $this->attrs['scalixScalixObject']=array();
265     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
266     $ldap->cd($this->dn);
267     $ldap->modify($this->attrs);
268     show_ldap_error($ldap->get_error());
270     /* Optionally execute a command after we're done */
271     $this->handle_post_events("remove");
272   }
275   /* Save data to object */
276   function save_object()
277   {
278     if (isset($_POST['scalixTab'])){
280       /* Save ldap attributes */
281       plugin::save_object();
283       /* Save fakeBooleanAttributes*/
284       foreach ($this->fakeBooleanAttributes as $val){
285         if (chkacl ($this->acl, "$val") == "") {
286           if( isset ($_POST["$val"])) {
287             $data = TRUE;
288           } else {
289             $data = FALSE;
290           }
291           if ($this->$val != $data){
292             $this->is_modified= TRUE;
293           }
295           $this->$val= $data;
296         }
297       }
298     }
299   }
302   /* Save data to LDAP, depending on is_account we save or delete */
303   function save()
304   {
305     $ldap= $this->config->get_ldap_link();
307     /* Call parents save to prepare $this->attrs */
308     plugin::save();
310     /* Save arrays */
311     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
312     
313     /* Save boolean vars ... the scalix schema expects them as strings */
314     if(!$this->scalixGroup){
315       $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
316       $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
317       $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
318       $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
319       $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
320     }
321     
322     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
323     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
325     /* Remove Mailquota if = "" */
326     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
327       $this->attrs['scalixLimitMailboxSize']=array();
328     }
330     /* Save data to LDAP */
331     $ldap->cd($this->dn);
332     $ldap->modify($this->attrs);
333     show_ldap_error($ldap->get_error());
335     /* Optionally execute a command after we're done */
336     if ($this->initially_was_account == $this->is_account){
337       if ($this->is_modified){
338         $this->handle_post_events("modify");
339       }
340     } else {
341       $this->handle_post_events("add");
342     }
344   }
346   /* Check formular input */
347   function check()
348   {
349     $ldap= $this->config->get_ldap_link();
351     $message= array();
353     if(empty($this->scalixMailnode)){
354       $message[]= _("There is no scalix mailnode specified.");
355     }
356     if(empty($this->scalixScalixObject)){
357       $message[]= _("scalixScalixObject must be set!");
358     }
360     $ldap->cd($this->config->current['BASE']);
361     foreach($this->scalixEmailAddress as $k => $mail) {         
362       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
363         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
364   
365       if ($ldap->count() != 0){
366         $message[]= _("The email address '$mail' you've entered is already in use.");
367       }
368     }
370     /* Check quota */
371     if ($this->scalixLimitMailboxSize != '' && chkacl ($this->acl, "scalixLimitMailboxSize") == ""){
372       if (!is_numeric($this->scalixLimitMailboxSize)) {
373         $message[]= _("Value in 'Limit Mailbox size' is not valid.");
374       } else {
375         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
376       }
377     }
379     return ($message);
380   }
382   /* Adapt from template, using 'dn' */
383   function adapt_from_template($dn)
384   {
385     plugin::adapt_from_template($dn);
387     foreach (array("scalixEmailAddress") as $val){
388       $this->$val= array();
389       if (isset($this->attrs["$val"]["count"])){
390         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
391           $value= $this->attrs["$val"][$i];
392           foreach (array("sn", "givenName", "uid") as $repl){
393             if (preg_match("/%$repl/i", $value)){
394               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
395             }
396           }
397           array_push($this->$val, strtolower(rewrite($value)));
398         }
399       }
400     }
401     $this->mail= strtolower(rewrite($this->mail));
402   }
404   function addEmail($address)
405   {
406     $ldap= $this->config->get_ldap_link();
408     $address= strtolower($address);
410     /* Is this address already assigned in LDAP? */
411     $ldap->cd ($this->config->current['BASE']);
412     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
413         "(scalixEmailAddress=$address)))", array("uid"));
415     if ($ldap->count() > 0){
416       $attrs= $ldap->fetch ();
417       return ($attrs["uid"][0]);
418     }
420     /* Add to list of alternates */
421     if (!in_array($address, $this->scalixEmailAddress)){
422       $this->scalixEmailAddress[]= $address;
423       $this->is_modified= TRUE;
424     }
426     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
427     reset ($this->scalixEmailAddress);
429     return ("");
430   }
433   function delEmail($addresses)
434   {
435     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
436     $this->is_modified= TRUE;
437   }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>