Code

0f8cc811ee932bbdbf50428a8a6d3bceb6f75090
[gosa.git] / gosa-plugins / scalix / 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 $mail ="";
42   var $uid  ="";
43   var $default_permissions= "none";
44   var $member_permissions= "post";
45   var $members= array();
46   var $admins= array();
47   var $vacations= array();
48   var $perms= array(  "lrs"       => "read", 
49                       "lrsp"      => "post", 
50                       "lrsip"     => "append",
51                       "lrswipcd"  => "write", 
52                       "lrswipcda" => "all" );
54   /* attribute list for save action */
55   var $attributes           = array("scalixMailnode", "scalixServerLanguage", "scalixLimitMailboxSize", "scalixMailboxClass");
56   var $fakeBooleanAttributes= array("scalixAdministrator", "scalixMailboxAdministrator", "scalixLimitOutboundMail",
57                                     "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry");
58   var $objectclasses        = array("scalixUserClass");
61   /* constructor, if 'dn' is set, the node loads the given
62      'dn' from LDAP */
63   function scalixAccount (&$config, $dn= NULL)
64   {
65     /* dn shouldn't be NULL */
66     if($dn === NULL){
67       trigger_error("Initialising scalixAccount without valid dn.");
68     }
70     /* Load bases attributes */
71     plugin::plugin($config, $dn);
73     /* Load attributes */
74     if ($dn != "new"){
76       /* Set user id */
77       if(isset($this->attrs['uid'][0])){
78         $this->uid = $this->attrs['uid'][0];
79       }
80         
81       foreach ($this->fakeBooleanAttributes as $val){
82         if (isset($this->attrs["$val"][0])&&$this->attrs["$val"][0]=="TRUE"){
83           $this->$val = TRUE;
84         } else {
85           $this->$val = FALSE;
86         }
87       }
89       /* Load attributes containing arrays */
90       foreach (array("scalixEmailAddress") as $val){
91         if (isset($this->attrs["$val"]["count"])){
92           for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
93             array_push($this->$val, $this->attrs["$val"][$i]);
94           }
95         }
96       }
97     }
99     /* Save initial account state */
100     $this->initially_was_account= $this->is_account;
101   }
104   function execute()
105   {
106     /* Call parent execute */
107     plugin::execute();
109     /* Load templating engine */
110     $smarty= get_smarty();
111     $smarty->assign("scalixGroup",$this->scalixGroup);
112     $display= "";
114     /* Do we need to flip is_account state? */
115     if (isset($_POST['modify_state'])){
116       $this->is_account= !$this->is_account;
117     }
119     $ReadOnly = !session::is_set("edit") && !isset($this->parent);
121     /* Show main page */
122     $mailboxClasses = array("", "LIMITED", "FULL");
123     $serverLanguages= array("", "GERMAN", "ENGLISH");
125     /* Do we represent a valid account? */
126     if (!$this->is_account && $this->parent === NULL){
127       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
128         _("This account has no scalix extensions.")."</b>";
130       $display.= back_to_main();
131       return ($display);
132     }
134     /* Show tab dialog headers */
135     if ($this->parent !== NULL){
136       if ($this->is_account){
137         $display= $this->show_disable_header(_("Remove scalix account"),
138             _("This account has scalix synchronization enabled. You can disable it by clicking below."));
139       } else {
140         $display= $this->show_enable_header(_("Create scalix account"), _("This account has scalix synchronization disabled. You can enable it by clicking below."));
141         return ($display);
142       }
143     }
145     /* Trigger forward add dialog? */
146     if (isset($_POST['add_local_forwarder'])){
147       $this->forward_dialog= TRUE;
148       $this->dialog= TRUE;
149     }
151     /* Cancel forward add dialog? */
152     if (isset($_POST['add_locals_cancel'])){
153       $this->forward_dialog= FALSE;
154       $this->dialog= FALSE;
155     }
158     $smarty->assign("mailboxClasses", $mailboxClasses);
159     $smarty->assign("serverLanguages", $serverLanguages);
160     foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
161       "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
162       "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
164       $smarty->assign("$val", $this->$val);
165       $smarty->assign("$val"."ACL", $this->getacl($val, $ReadOnly));
166     }
168     /* Fill checkboxes */
169     if ($this->scalixAdministrator) {
170       $smarty->assign("scalixAdministrator", "checked");
171     } else {
172       $smarty->assign("scalixAdministrator", "");
173     }
174     if ($this->scalixMailboxAdministrator) {
175       $smarty->assign("scalixMailboxAdministrator", "checked");
176     } else {
177       $smarty->assign("scalixMailboxAdministrator", "");
178     }
179     if ($this->scalixLimitOutboundMail) {
180       $smarty->assign("scalixLimitOutboundMail", "checked");
181     } else {
182       $smarty->assign("scalixLimitOutboundMail", "");
183     }
184     if ($this->scalixLimitInboundMail) {
185       $smarty->assign("scalixLimitInboundMail", "checked");
186     } else {
187       $smarty->assign("scalixLimitInboundMail", "");
188     }
189     if ($this->scalixLimitNotifyUser) {
190       $smarty->assign("scalixLimitNotifyUser", "checked");
191     } else {
192       $smarty->assign("scalixLimitNotifyUser", "");
193     }
194     if ($this->scalixHideUserEntry) {
195       $smarty->assign("scalixHideUserEntry", "checked");
196     } else {
197       $smarty->assign("scalixHideUserEntry", "");
198     }
200     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
201     return ($display);
202   }
205   /* remove object from parent */
206   function remove_from_parent()
207   {
208     /* Cancel if there's nothing to do here */
209     if (!$this->initially_was_account){
210       return;
211     }
212     
213     /* include global link_info */
214     $ldap= $this->config->get_ldap_link();
216     /* Remove and write to LDAP */
217     plugin::remove_from_parent();
219     /* Zero arrays */
220     $this->attrs['scalixEmailAddress']= array();
222     /* Unset fake boolean attributes from entry */
223     foreach ($this->fakeBooleanAttributes as $val){
224       $this->attrs["$val"]= array();
225     }
227     /*unset scalixScalixObject*/
228     $this->attrs['scalixScalixObject']=array();
230     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
231     $ldap->cd($this->dn);
232     $ldap->modify($this->attrs);
233     if (!$ldap->success()){
234       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
235     }
237     /* Optionally execute a command after we're done */
238     $this->handle_post_events("remove");
239   }
242   /* Save data to object */
243   function save_object()
244   {
245     if (isset($_POST['scalixTab'])){
247       /* Save ldap attributes */
248       plugin::save_object();
250       /* Check if given value is not empty */
251       if (isset($_POST['email_address']) && !empty($_POST['email_address'])){
252         $valid= FALSE;
254         /* Valid mail address */
255         if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
256             (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
257           msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
258         }else{
259           $valid = TRUE;
260         }
262         /* Check write access */
263         if(!$this->acl_is_writeable("scalixEmailAddress")){
264           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
265         }else{
267           /* Mail address already in use ? */
268           if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
269             $ui= get_userinfo();
270             if ($user != $ui->username){
271               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user),
272                   ERROR_DIALOG);
273             }
274           }
275         }
276       }
278       /* Delete email addresses */
279       if (isset($_POST['delete_email']) && isset ($_POST['emails_list']) && $this->acl_is_writeable("scalixEmailAddress")){
280         if(!$this->acl_is_writeable("scalixEmailAddress")){
281           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
282         }else{
283           if (count($_POST['emails_list'])){
284             $this->delEmail ($_POST['emails_list']);
285           }
286         }
287       }
289       /* Save fakeBooleanAttributes*/
290       foreach ($this->fakeBooleanAttributes as $val){
291         if ($this->acl_is_writeable($val)) {
292           if( isset ($_POST["$val"])) {
293             $data = TRUE;
294           } else {
295             $data = FALSE;
296           }
297           if ($this->$val != $data){
298             $this->is_modified= TRUE;
299           }
301           $this->$val= $data;
302         }
303       }
304     }
305   }
308   /* Save data to LDAP, depending on is_account we save or delete */
309   function save()
310   {
311     $ldap= $this->config->get_ldap_link();
313     /* Call parents save to prepare $this->attrs */
314     plugin::save();
316     /* Save arrays */
317     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
318     
319     /* Save boolean vars ... the scalix schema expects them as strings */
320     if(!$this->scalixGroup){
321       $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
322       $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
323       $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
324       $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
325       $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
326     }
327     
328     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
329     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
331     /* Remove Mailquota if = "" */
332     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
333       $this->attrs['scalixLimitMailboxSize']=array();
334     }
336     /* Save data to LDAP */
337     $ldap->cd($this->dn);
338     $ldap->modify($this->attrs);
339     if (!$ldap->success()){
340       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
341     }
343     /* Optionally execute a command after we're done */
344     if ($this->initially_was_account == $this->is_account){
345       if ($this->is_modified){
346         $this->handle_post_events("modify");
347       }
348     } else {
349       $this->handle_post_events("add");
350     }
352   }
354   /* Check formular input */
355   function check()
356   {
357     $ldap= $this->config->get_ldap_link();
359     $message= array();
361     if(empty($this->scalixMailnode)){
362       $message[]= msgPool::required(_("Mailnode"));
363     }
364     if(empty($this->scalixScalixObject)){
365       $message[]= msgPool::required(_("Scalix object"));
366     }
368     $ldap->cd($this->config->current['BASE']);
369     foreach($this->scalixEmailAddress as $k => $mail) {         
370       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
371         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
372   
373       if ($ldap->count() != 0){
374         $message[]= msgPool::duplicated(_("Email address"));
375       }
376     }
378     /* Check quota */
379     if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
380       if (!is_numeric($this->scalixLimitMailboxSize)) {
381         $message[]= msgPool::invalid(_("Limit Mailbox"));
382       } else {
383         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
384       }
385     }
387     return ($message);
388   }
390   /* Adapt from template, using 'dn' */
391   function adapt_from_template($dn, $skip= array())
392   {
393     plugin::adapt_from_template($dn, $skip);
395     foreach (array("scalixEmailAddress") as $val){
397       if (in_array($val, $skip)){
398         continue;
399       }
401       $this->$val= array();
402       if (isset($this->attrs["$val"]["count"])){
403         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
404           $value= $this->attrs["$val"][$i];
405           foreach (array("sn", "givenName", "uid") as $repl){
406             if (preg_match("/%$repl/i", $value)){
407               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
408             }
409           }
410           array_push($this->$val, strtolower(rewrite($value)));
411         }
412       }
413     }
414     $this->mail= strtolower(rewrite($this->mail));
415   }
417   function addEmail($address)
418   {
419     $ldap= $this->config->get_ldap_link();
421     $address= strtolower($address);
423     /* Is this address already assigned in LDAP? */
424     $ldap->cd ($this->config->current['BASE']);
425     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
426         "(scalixEmailAddress=$address)))", array("uid"));
428     if ($ldap->count() > 0){
429       $attrs= $ldap->fetch ();
430       return ($attrs["uid"][0]);
431     }
433     /* Add to list of alternates */
434     if (!in_array($address, $this->scalixEmailAddress)){
435       $this->scalixEmailAddress[]= $address;
436       $this->is_modified= TRUE;
437     }
439     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
440     reset ($this->scalixEmailAddress);
442     return ("");
443   }
446   function delEmail($addresses)
447   {
448     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
449     $this->is_modified= TRUE;
450   }
453   static function plInfo()
454   {
455     return (array(
456           "plDescription"     => _("Scalix account"),
457           "plSelfModify"      => TRUE,
458           "plDepends"         => array("user"),
459           "plPriority"        => 2,
460           "plSection"         => array("personal" => _("My account")),
461           "plCategory"        => array("users"),
462           "plOptions"         => array(),
464           "plProvidedAcls"  => array(
466             "scalixMailnode"            => _("Scalix Mail node"),
467             "scalixMailboxClass"        => _("Mailbox class"),
468             "scalixServerLanguage"      => _("Server language"),
469             "scalixAdministrator"       => _("Administrator"),
470             "scalixMailboxAdministrator"=> _("Mailbox administrator"),
471             "scalixHideUserEntry"       => _("Hide user entry in Scalix"),
472             "scalixLimitMailboxSize"    => _("Mailbox size limitations"),
473             "scalixLimitOutboundMail"   => _("Limit outbound"),
474             "scalixLimitInboundMail"    => _("Limit inbound"),
475             "scalixLimitNotifyUser"     => _("Notify user"),
476             "scalixEmailAddress"        => _("Scalix email addresses"))
478             ));
479   }
482   function saveCopyDialog()
483   {
484     if (isset($_POST['scalixTab'])){
486       /* Add email addresses */
487       if (isset($_POST['add_email'])){
489         /* Check if given value is not empty */
490         if ($_POST['email_address'] != ""){
491           $valid= FALSE;
493           /* Valid mail address */
494           if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) || 
495               (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
496             msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
497           }else{
498             $valid = TRUE;
499           }
501           /* Mail address already in use ? */
502           if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
503             $ui= get_userinfo();
504             if ($user != $ui->username){
505               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user), ERROR_DIALOG);
506             }
507           }
508         }
509       }
511       /* Delete email addresses */
512       if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
513         if (count($_POST['emails_list'])){
514           $this->delEmail ($_POST['emails_list']);
515         }
516       }
517     }
518   }
521   function PrepareForCopyPaste($source)
522   {
523     plugin::PrepareForCopyPaste($source);
524     foreach ($this->fakeBooleanAttributes as $val){
525       if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
526         $this->$val = TRUE;
527       } else {
528         $this->$val = FALSE;
529       }
530     }
532     /* Load attributes containing arrays */
533     foreach (array("scalixEmailAddress") as $val){
534       if (isset($source["$val"]["count"])){
535         for ($i= 0; $i<$source["$val"]["count"]; $i++){
536           array_push($this->$val, $source["$val"][$i]);
537         }
538       }
539     }
540   }
543   /* Create the posix dialog part for copy & paste */
544   function getCopyDialog()
545   {
546     $smarty = get_smarty(); 
547     $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
548     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));    
549     $ret = array();
550     $ret['string'] = $str;
551     $ret['status'] = "";
552     return($ret);
553  
554   }
557 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler
558 ?>