Code

bb8d82cfe0be3f8e25f0c44992ed9e0fd1ee4bb8
[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", set_post($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     /* Show main page */
120     $mailboxClasses = array("&nbsp;", "LIMITED", "FULL");
121     $serverLanguages= array("&nbsp;", "GERMAN", "ENGLISH");
123     /* Do we represent a valid account? */
124     if (!$this->is_account && $this->parent === NULL){
125       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
126         _("This account has no SCALIX extensions.")."</b>";
128       $display.= back_to_main();
129       return ($display);
130     }
132     /* Show tab dialog headers */
133     if ($this->parent !== NULL){
134       if ($this->is_account){
135         $display= $this->show_disable_header(_("Remove SCALIX account"),
136             _("This account has SCALIX synchronization enabled. You can disable it by clicking below."));
137       } else {
138         $display= $this->show_enable_header(_("Create SCALIX account"), _("This account has SCALIX synchronization disabled. You can enable it by clicking below."));
139         return ($display);
140       }
141     }
143     /* Trigger forward add dialog? */
144     if (isset($_POST['add_local_forwarder'])){
145       $this->forward_dialog= TRUE;
146       $this->dialog= TRUE;
147     }
149     /* Cancel forward add dialog? */
150     if (isset($_POST['add_locals_cancel'])){
151       $this->forward_dialog= FALSE;
152       $this->dialog= FALSE;
153     }
156     $smarty->assign("mailboxClasses", $mailboxClasses);
157     $smarty->assign("serverLanguages", $serverLanguages);
158     foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
159       "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
160       "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
162       $smarty->assign("$val", set_post($this->$val));
163     }
165     $tmp = $this->plInfo();
166     foreach($tmp['plProvidedAcls'] as $name => $desc){
167       $smarty->assign($name."ACL", $this->getacl($name));
168     }
170     /* Fill checkboxes */
171     if ($this->scalixAdministrator) {
172       $smarty->assign("scalixAdministrator", "checked");
173     } else {
174       $smarty->assign("scalixAdministrator", "");
175     }
176     if ($this->scalixMailboxAdministrator) {
177       $smarty->assign("scalixMailboxAdministrator", "checked");
178     } else {
179       $smarty->assign("scalixMailboxAdministrator", "");
180     }
181     if ($this->scalixLimitOutboundMail) {
182       $smarty->assign("scalixLimitOutboundMail", "checked");
183     } else {
184       $smarty->assign("scalixLimitOutboundMail", "");
185     }
186     if ($this->scalixLimitInboundMail) {
187       $smarty->assign("scalixLimitInboundMail", "checked");
188     } else {
189       $smarty->assign("scalixLimitInboundMail", "");
190     }
191     if ($this->scalixLimitNotifyUser) {
192       $smarty->assign("scalixLimitNotifyUser", "checked");
193     } else {
194       $smarty->assign("scalixLimitNotifyUser", "");
195     }
196     if ($this->scalixHideUserEntry) {
197       $smarty->assign("scalixHideUserEntry", "checked");
198     } else {
199       $smarty->assign("scalixHideUserEntry", "");
200     }
202     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
203     return ($display);
204   }
207   /* remove object from parent */
208   function remove_from_parent()
209   {
210     /* Cancel if there's nothing to do here */
211     if (!$this->initially_was_account){
212       return;
213     }
214     
215     /* include global link_info */
216     $ldap= $this->config->get_ldap_link();
218     /* Remove and write to LDAP */
219     plugin::remove_from_parent();
221     /* Zero arrays */
222     $this->attrs['scalixEmailAddress']= array();
224     /* Unset fake boolean attributes from entry */
225     foreach ($this->fakeBooleanAttributes as $val){
226       $this->attrs["$val"]= array();
227     }
229     /*unset scalixScalixObject*/
230     $this->attrs['scalixScalixObject']=array();
232     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
233     $ldap->cd($this->dn);
234     $ldap->modify($this->attrs);
235     if (!$ldap->success()){
236       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
237     }
239     /* Optionally execute a command after we're done */
240     $this->handle_post_events("remove");
241   }
244   /* Save data to object */
245   function save_object()
246   {
247     if (isset($_POST['scalixTab'])){
249       /* Save ldap attributes */
250       plugin::save_object();
252       /* Check if given value is not empty */
253       if (isset($_POST['email_address']) && !empty($_POST['email_address']) && isset($_POST['add_email'])){
254         $valid= FALSE;
256         /* Valid mail address */
257         if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
258             (!$this->is_template && !tests::is_email($_POST['email_address']))){
259           msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
260         }else{
261           $valid = TRUE;
262         }
264         /* Check write access */
265         if(!$this->acl_is_writeable("scalixEmailAddress")){
266           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
267         }else{
269           /* Mail address already in use ? */
270           if ($valid && ($user= $this->addEmail (get_post('email_address'))) != ""){
271             $ui= get_userinfo();
272             if ($user != $ui->username){
273               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user),
274                   ERROR_DIALOG);
275             }
276           }
277         }
278       }
280       /* Delete email addresses */
281       if (isset($_POST['delete_email']) && isset ($_POST['emails_list']) && $this->acl_is_writeable("scalixEmailAddress")){
282         if(!$this->acl_is_writeable("scalixEmailAddress")){
283           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
284         }else{
285           if (count($_POST['emails_list'])){
286             $this->delEmail ($_POST['emails_list']);
287           }
288         }
289       }
291       /* Save fakeBooleanAttributes*/
292       foreach ($this->fakeBooleanAttributes as $val){
293         if ($this->acl_is_writeable($val)) {
294           if( isset ($_POST["$val"])) {
295             $data = TRUE;
296           } else {
297             $data = FALSE;
298           }
299           if ($this->$val != $data){
300             $this->is_modified= TRUE;
301           }
303           $this->$val= $data;
304         }
305       }
306     }
307   }
310   /* Save data to LDAP, depending on is_account we save or delete */
311   function save()
312   {
313     $ldap= $this->config->get_ldap_link();
315     /* Call parents save to prepare $this->attrs */
316     plugin::save();
318     /* Save arrays */
319     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
320     
321     /* Save boolean vars ... the scalix schema expects them as strings */
322     if(!$this->scalixGroup){
323       $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
324       $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
325       $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
326       $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
327       $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
328     }
329     
330     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
331     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
333     /* Remove Mailquota if = "" */
334     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
335       $this->attrs['scalixLimitMailboxSize']=array();
336     }
338     /* Save data to LDAP */
339     $ldap->cd($this->dn);
340     $ldap->modify($this->attrs);
341     if (!$ldap->success()){
342       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
343     }
345     /* Optionally execute a command after we're done */
346     if ($this->initially_was_account == $this->is_account){
347       if ($this->is_modified){
348         $this->handle_post_events("modify");
349       }
350     } else {
351       $this->handle_post_events("add");
352     }
354   }
356   /* Check formular input */
357   function check()
358   {
359     $ldap= $this->config->get_ldap_link();
361     $message= array();
363     if(empty($this->scalixMailnode)){
364       $message[]= msgPool::required(_("Mail node"));
365     }
366     if(empty($this->scalixScalixObject)){
367       $message[]= msgPool::required(_("SCALIX object"));
368     }
370     $ldap->cd($this->config->current['BASE']);
371     foreach($this->scalixEmailAddress as $k => $mail) {         
372       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
373         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
374   
375       if ($ldap->count() != 0){
376         $message[]= msgPool::duplicated(_("Email address"));
377       }
378     }
380     /* Check quota */
381     if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
382       if (!is_numeric($this->scalixLimitMailboxSize)) {
383         $message[]= msgPool::invalid(_("Limit Mailbox"));
384       } else {
385         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
386       }
387     }
389     return ($message);
390   }
392   /* Adapt from template, using 'dn' */
393   function adapt_from_template($dn, $skip= array())
394   {
395     plugin::adapt_from_template($dn, $skip);
397     foreach (array("scalixEmailAddress") as $val){
399       if (in_array($val, $skip)){
400         continue;
401       }
403       $this->$val= array();
404       if (isset($this->attrs["$val"]["count"])){
405         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
406           $value= $this->attrs["$val"][$i];
407           foreach (array("sn", "givenName", "uid") as $repl){
408             if (preg_match("/%$repl/i", $value)){
409               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
410             }
411           }
412           array_push($this->$val, strtolower(rewrite($value)));
413         }
414       }
415     }
416     $this->mail= strtolower(rewrite($this->mail));
417   }
419   function addEmail($address)
420   {
421     $ldap= $this->config->get_ldap_link();
423     $address= strtolower($address);
425     /* Is this address already assigned in LDAP? */
426     $ldap->cd ($this->config->current['BASE']);
427     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
428         "(scalixEmailAddress=$address)))", array("uid"));
430     if ($ldap->count() > 0){
431       $attrs= $ldap->fetch ();
432       return ($attrs["uid"][0]);
433     }
435     /* Add to list of alternates */
436     if (!in_array($address, $this->scalixEmailAddress)){
437       $this->scalixEmailAddress[]= $address;
438       $this->is_modified= TRUE;
439     }
441     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
442     reset ($this->scalixEmailAddress);
444     return ("");
445   }
448   function delEmail($addresses)
449   {
450     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
451     $this->is_modified= TRUE;
452   }
455   static function plInfo()
456   {
457     return (array(
458           "plDescription"     => _("SCALIX account"),
459           "plSelfModify"      => TRUE,
460           "plDepends"         => array("user"),
461           "plPriority"        => 2,
462           "plSection"         => array("personal" => _("My account")),
463           "plCategory"        => array("users"),
464           "plOptions"         => array(),
466           "plRequirements"=> array(
467               'ldapSchema' => array('scalixUserClass' => ''),
468               'onFailureDisablePlugin' => array(get_class())
469               ),
471           "plProvidedAcls"  => array(
473             "scalixMailnode"            => _("SCALIX Mail node"),
474             "scalixMailboxClass"        => _("Mailbox class"),
475             "scalixServerLanguage"      => _("Server language"),
476             "scalixAdministrator"       => _("Administrator"),
477             "scalixMailboxAdministrator"=> _("Mailbox administrator"),
478             "scalixHideUserEntry"       => _("Hide user entry in SCALIX"),
479             "scalixLimitMailboxSize"    => _("Mailbox size limitations"),
480             "scalixLimitOutboundMail"   => _("Limit outbound"),
481             "scalixLimitInboundMail"    => _("Limit inbound"),
482             "scalixLimitNotifyUser"     => _("Notify user"),
483             "scalixEmailAddress"        => _("SCALIX email addresses"))
485             ));
486   }
489   function saveCopyDialog()
490   {
491     if (isset($_POST['scalixTab'])){
493       /* Add email addresses */
494       if (isset($_POST['add_email'])){
496         /* Check if given value is not empty */
497         if ($_POST['email_address'] != ""){
498           $valid= FALSE;
500           /* Valid mail address */
501           if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) || 
502               (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
503             msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
504           }else{
505             $valid = TRUE;
506           }
508           /* Mail address already in use ? */
509           if ($valid && ($user= $this->addEmail (get_post('email_address'))) != ""){
510             $ui= get_userinfo();
511             if ($user != $ui->username){
512               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user), ERROR_DIALOG);
513             }
514           }
515         }
516       }
518       /* Delete email addresses */
519       if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
520         if (count($_POST['emails_list'])){
521           $this->delEmail ($_POST['emails_list']);
522         }
523       }
524     }
525   }
528   function PrepareForCopyPaste($source)
529   {
530     plugin::PrepareForCopyPaste($source);
531     foreach ($this->fakeBooleanAttributes as $val){
532       if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
533         $this->$val = TRUE;
534       } else {
535         $this->$val = FALSE;
536       }
537     }
539     /* Load attributes containing arrays */
540     foreach (array("scalixEmailAddress") as $val){
541       if (isset($source["$val"]["count"])){
542         for ($i= 0; $i<$source["$val"]["count"]; $i++){
543           array_push($this->$val, $source["$val"][$i]);
544         }
545       }
546     }
547   }
550   /* Create the posix dialog part for copy & paste */
551   function getCopyDialog()
552   {
553     $smarty = get_smarty(); 
554     $smarty->assign("scalixEmailAddress",set_post($this->scalixEmailAddress));
555     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));    
556     $ret = array();
557     $ret['string'] = $str;
558     $ret['status'] = "";
559     return($ret);
560  
561   }
564 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler
565 ?>