Code

removed old style edit method
[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     /* Show main page */
120     $mailboxClasses = array("", "LIMITED", "FULL");
121     $serverLanguages= array("", "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", $this->$val);
163       $smarty->assign("$val"."ACL", $this->getacl($val));
164     }
166     /* Fill checkboxes */
167     if ($this->scalixAdministrator) {
168       $smarty->assign("scalixAdministrator", "checked");
169     } else {
170       $smarty->assign("scalixAdministrator", "");
171     }
172     if ($this->scalixMailboxAdministrator) {
173       $smarty->assign("scalixMailboxAdministrator", "checked");
174     } else {
175       $smarty->assign("scalixMailboxAdministrator", "");
176     }
177     if ($this->scalixLimitOutboundMail) {
178       $smarty->assign("scalixLimitOutboundMail", "checked");
179     } else {
180       $smarty->assign("scalixLimitOutboundMail", "");
181     }
182     if ($this->scalixLimitInboundMail) {
183       $smarty->assign("scalixLimitInboundMail", "checked");
184     } else {
185       $smarty->assign("scalixLimitInboundMail", "");
186     }
187     if ($this->scalixLimitNotifyUser) {
188       $smarty->assign("scalixLimitNotifyUser", "checked");
189     } else {
190       $smarty->assign("scalixLimitNotifyUser", "");
191     }
192     if ($this->scalixHideUserEntry) {
193       $smarty->assign("scalixHideUserEntry", "checked");
194     } else {
195       $smarty->assign("scalixHideUserEntry", "");
196     }
198     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
199     return ($display);
200   }
203   /* remove object from parent */
204   function remove_from_parent()
205   {
206     /* Cancel if there's nothing to do here */
207     if (!$this->initially_was_account){
208       return;
209     }
210     
211     /* include global link_info */
212     $ldap= $this->config->get_ldap_link();
214     /* Remove and write to LDAP */
215     plugin::remove_from_parent();
217     /* Zero arrays */
218     $this->attrs['scalixEmailAddress']= array();
220     /* Unset fake boolean attributes from entry */
221     foreach ($this->fakeBooleanAttributes as $val){
222       $this->attrs["$val"]= array();
223     }
225     /*unset scalixScalixObject*/
226     $this->attrs['scalixScalixObject']=array();
228     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
229     $ldap->cd($this->dn);
230     $ldap->modify($this->attrs);
231     if (!$ldap->success()){
232       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
233     }
235     /* Optionally execute a command after we're done */
236     $this->handle_post_events("remove");
237   }
240   /* Save data to object */
241   function save_object()
242   {
243     if (isset($_POST['scalixTab'])){
245       /* Save ldap attributes */
246       plugin::save_object();
248       /* Check if given value is not empty */
249       if (isset($_POST['email_address']) && !empty($_POST['email_address'])){
250         $valid= FALSE;
252         /* Valid mail address */
253         if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) ||
254             (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
255           msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
256         }else{
257           $valid = TRUE;
258         }
260         /* Check write access */
261         if(!$this->acl_is_writeable("scalixEmailAddress")){
262           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
263         }else{
265           /* Mail address already in use ? */
266           if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
267             $ui= get_userinfo();
268             if ($user != $ui->username){
269               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user),
270                   ERROR_DIALOG);
271             }
272           }
273         }
274       }
276       /* Delete email addresses */
277       if (isset($_POST['delete_email']) && isset ($_POST['emails_list']) && $this->acl_is_writeable("scalixEmailAddress")){
278         if(!$this->acl_is_writeable("scalixEmailAddress")){
279           msg_dialog::display(_("Error"), _("You have no permission to modify these addresses."), ERROR_DIALOG);
280         }else{
281           if (count($_POST['emails_list'])){
282             $this->delEmail ($_POST['emails_list']);
283           }
284         }
285       }
287       /* Save fakeBooleanAttributes*/
288       foreach ($this->fakeBooleanAttributes as $val){
289         if ($this->acl_is_writeable($val)) {
290           if( isset ($_POST["$val"])) {
291             $data = TRUE;
292           } else {
293             $data = FALSE;
294           }
295           if ($this->$val != $data){
296             $this->is_modified= TRUE;
297           }
299           $this->$val= $data;
300         }
301       }
302     }
303   }
306   /* Save data to LDAP, depending on is_account we save or delete */
307   function save()
308   {
309     $ldap= $this->config->get_ldap_link();
311     /* Call parents save to prepare $this->attrs */
312     plugin::save();
314     /* Save arrays */
315     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
316     
317     /* Save boolean vars ... the scalix schema expects them as strings */
318     if(!$this->scalixGroup){
319       $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
320       $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
321       $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
322       $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
323       $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
324     }
325     
326     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
327     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
329     /* Remove Mailquota if = "" */
330     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
331       $this->attrs['scalixLimitMailboxSize']=array();
332     }
334     /* Save data to LDAP */
335     $ldap->cd($this->dn);
336     $ldap->modify($this->attrs);
337     if (!$ldap->success()){
338       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
339     }
341     /* Optionally execute a command after we're done */
342     if ($this->initially_was_account == $this->is_account){
343       if ($this->is_modified){
344         $this->handle_post_events("modify");
345       }
346     } else {
347       $this->handle_post_events("add");
348     }
350   }
352   /* Check formular input */
353   function check()
354   {
355     $ldap= $this->config->get_ldap_link();
357     $message= array();
359     if(empty($this->scalixMailnode)){
360       $message[]= msgPool::required(_("Mailnode"));
361     }
362     if(empty($this->scalixScalixObject)){
363       $message[]= msgPool::required(_("Scalix object"));
364     }
366     $ldap->cd($this->config->current['BASE']);
367     foreach($this->scalixEmailAddress as $k => $mail) {         
368       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
369         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
370   
371       if ($ldap->count() != 0){
372         $message[]= msgPool::duplicated(_("Email address"));
373       }
374     }
376     /* Check quota */
377     if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
378       if (!is_numeric($this->scalixLimitMailboxSize)) {
379         $message[]= msgPool::invalid(_("Limit Mailbox"));
380       } else {
381         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
382       }
383     }
385     return ($message);
386   }
388   /* Adapt from template, using 'dn' */
389   function adapt_from_template($dn, $skip= array())
390   {
391     plugin::adapt_from_template($dn, $skip);
393     foreach (array("scalixEmailAddress") as $val){
395       if (in_array($val, $skip)){
396         continue;
397       }
399       $this->$val= array();
400       if (isset($this->attrs["$val"]["count"])){
401         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
402           $value= $this->attrs["$val"][$i];
403           foreach (array("sn", "givenName", "uid") as $repl){
404             if (preg_match("/%$repl/i", $value)){
405               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
406             }
407           }
408           array_push($this->$val, strtolower(rewrite($value)));
409         }
410       }
411     }
412     $this->mail= strtolower(rewrite($this->mail));
413   }
415   function addEmail($address)
416   {
417     $ldap= $this->config->get_ldap_link();
419     $address= strtolower($address);
421     /* Is this address already assigned in LDAP? */
422     $ldap->cd ($this->config->current['BASE']);
423     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
424         "(scalixEmailAddress=$address)))", array("uid"));
426     if ($ldap->count() > 0){
427       $attrs= $ldap->fetch ();
428       return ($attrs["uid"][0]);
429     }
431     /* Add to list of alternates */
432     if (!in_array($address, $this->scalixEmailAddress)){
433       $this->scalixEmailAddress[]= $address;
434       $this->is_modified= TRUE;
435     }
437     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
438     reset ($this->scalixEmailAddress);
440     return ("");
441   }
444   function delEmail($addresses)
445   {
446     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
447     $this->is_modified= TRUE;
448   }
451   static function plInfo()
452   {
453     return (array(
454           "plDescription"     => _("Scalix account"),
455           "plSelfModify"      => TRUE,
456           "plDepends"         => array("user"),
457           "plPriority"        => 2,
458           "plSection"         => array("personal" => _("My account")),
459           "plCategory"        => array("users"),
460           "plOptions"         => array(),
462           "plProvidedAcls"  => array(
464             "scalixMailnode"            => _("Scalix Mail node"),
465             "scalixMailboxClass"        => _("Mailbox class"),
466             "scalixServerLanguage"      => _("Server language"),
467             "scalixAdministrator"       => _("Administrator"),
468             "scalixMailboxAdministrator"=> _("Mailbox administrator"),
469             "scalixHideUserEntry"       => _("Hide user entry in Scalix"),
470             "scalixLimitMailboxSize"    => _("Mailbox size limitations"),
471             "scalixLimitOutboundMail"   => _("Limit outbound"),
472             "scalixLimitInboundMail"    => _("Limit inbound"),
473             "scalixLimitNotifyUser"     => _("Notify user"),
474             "scalixEmailAddress"        => _("Scalix email addresses"))
476             ));
477   }
480   function saveCopyDialog()
481   {
482     if (isset($_POST['scalixTab'])){
484       /* Add email addresses */
485       if (isset($_POST['add_email'])){
487         /* Check if given value is not empty */
488         if ($_POST['email_address'] != ""){
489           $valid= FALSE;
491           /* Valid mail address */
492           if( ($this->is_template && !tests::is_email($_POST['email_address'], TRUE)) || 
493               (!$this->is_template && !tests::is_email($_POST['email_address'])) ){
494             msg_dialog::display(_("Error"), _("Cannot add invalid mail address!"), ERROR_DIALOG);
495           }else{
496             $valid = TRUE;
497           }
499           /* Mail address already in use ? */
500           if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
501             $ui= get_userinfo();
502             if ($user != $ui->username){
503               msg_dialog::display(_("Error"), sprintf(_("Cannot add mail address: it is already used by user '%s'."), $user), ERROR_DIALOG);
504             }
505           }
506         }
507       }
509       /* Delete email addresses */
510       if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
511         if (count($_POST['emails_list'])){
512           $this->delEmail ($_POST['emails_list']);
513         }
514       }
515     }
516   }
519   function PrepareForCopyPaste($source)
520   {
521     plugin::PrepareForCopyPaste($source);
522     foreach ($this->fakeBooleanAttributes as $val){
523       if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
524         $this->$val = TRUE;
525       } else {
526         $this->$val = FALSE;
527       }
528     }
530     /* Load attributes containing arrays */
531     foreach (array("scalixEmailAddress") as $val){
532       if (isset($source["$val"]["count"])){
533         for ($i= 0; $i<$source["$val"]["count"]; $i++){
534           array_push($this->$val, $source["$val"][$i]);
535         }
536       }
537     }
538   }
541   /* Create the posix dialog part for copy & paste */
542   function getCopyDialog()
543   {
544     $smarty = get_smarty(); 
545     $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
546     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));    
547     $ret = array();
548     $ret['string'] = $str;
549     $ret['status'] = "";
550     return($ret);
551  
552   }
555 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler
556 ?>