Code

Added scalix copy & paste
[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   /* CLI vars */
20   var $cli_summary    = "Manage users scalix account";
21   var $cli_description= "Some longer text\nfor help";
22   var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24   /* plugin specific values - scalixUserClass */
25   /* MUST */
26   var $scalixScalixObject= TRUE; //TRUE or FALSE
27   var $scalixMailnode= "";
28   /* INDIRECT MUST, scalix will complain if no email was specified */
29   
30   /* MAY */
31   var $scalixAdministrator= FALSE;
32   var $scalixMailboxAdministrator = FALSE;
33   var $scalixServerLanguage= "";
34   var $scalixEmailAddress= array();
35   var $scalixLimitMailboxSize= "";
36   var $scalixLimitOutboundMail= FALSE;
37   var $scalixLimitInboundMail= FALSE;
38   var $scalixLimitNotifyUser= FALSE;
39   var $scalixHideUserEntry= FALSE;
40   var $scalixMailboxClass= "";
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     $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_disable_header(_("Remove scalix account"),
135             _("This account has scalix synchronization enabled. You can disable it by clicking below."));
136       } else {
137         $display= $this->show_enable_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     }
155     $smarty->assign("mailboxClasses", $mailboxClasses);
156     $smarty->assign("serverLanguages", $serverLanguages);
157     foreach(array("perms", "scalixScalixObject", "scalixMailnode", "scalixAdministrator", "scalixMailboxAdministrator",
158       "scalixServerLanguage", "scalixLimitMailboxSize", "scalixLimitOutboundMail", "scalixEmailAddress",
159       "scalixLimitInboundMail", "scalixLimitNotifyUser", "scalixHideUserEntry", "scalixMailboxClass") as $val){
161       $smarty->assign("$val", $this->$val);
162       $smarty->assign("$val"."ACL", $this->getacl($val));
163     }
165     /* Fill checkboxes */
166     if ($this->scalixAdministrator) {
167       $smarty->assign("scalixAdministrator", "checked");
168     } else {
169       $smarty->assign("scalixAdministrator", "");
170     }
171     if ($this->scalixMailboxAdministrator) {
172       $smarty->assign("scalixMailboxAdministrator", "checked");
173     } else {
174       $smarty->assign("scalixMailboxAdministrator", "");
175     }
176     if ($this->scalixLimitOutboundMail) {
177       $smarty->assign("scalixLimitOutboundMail", "checked");
178     } else {
179       $smarty->assign("scalixLimitOutboundMail", "");
180     }
181     if ($this->scalixLimitInboundMail) {
182       $smarty->assign("scalixLimitInboundMail", "checked");
183     } else {
184       $smarty->assign("scalixLimitInboundMail", "");
185     }
186     if ($this->scalixLimitNotifyUser) {
187       $smarty->assign("scalixLimitNotifyUser", "checked");
188     } else {
189       $smarty->assign("scalixLimitNotifyUser", "");
190     }
191     if ($this->scalixHideUserEntry) {
192       $smarty->assign("scalixHideUserEntry", "checked");
193     } else {
194       $smarty->assign("scalixHideUserEntry", "");
195     }
197     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
198     return ($display);
199   }
202   /* remove object from parent */
203   function remove_from_parent()
204   {
205     /* Cancel if there's nothing to do here */
206     if (!$this->initially_was_account){
207       return;
208     }
209     
210     /* include global link_info */
211     $ldap= $this->config->get_ldap_link();
213     /* Remove and write to LDAP */
214     plugin::remove_from_parent();
216     /* Zero arrays */
217     $this->attrs['scalixEmailAddress']= array();
219     /* Unset fake boolean attributes from entry */
220     foreach ($this->fakeBooleanAttributes as $val){
221       $this->attrs["$val"]= array();
222     }
224     /*unset scalixScalixObject*/
225     $this->attrs['scalixScalixObject']=array();
227     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
228     $ldap->cd($this->dn);
229     $ldap->modify($this->attrs);
230     show_ldap_error($ldap->get_error());
232     /* Optionally execute a command after we're done */
233     $this->handle_post_events("remove");
234   }
237   /* Save data to object */
238   function save_object()
239   {
240     if (isset($_POST['scalixTab'])){
242       /* Save ldap attributes */
243       plugin::save_object();
245       /* Save fakeBooleanAttributes*/
246       foreach ($this->fakeBooleanAttributes as $val){
247         if ($this->acl_is_writeable($val)) {
248           if( isset ($_POST["$val"])) {
249             $data = TRUE;
250           } else {
251             $data = FALSE;
252           }
253           if ($this->$val != $data){
254             $this->is_modified= TRUE;
255           }
257           $this->$val= $data;
258         }
259       }
260     }
261   }
264   /* Save data to LDAP, depending on is_account we save or delete */
265   function save()
266   {
267     $ldap= $this->config->get_ldap_link();
269     /* Call parents save to prepare $this->attrs */
270     plugin::save();
272     /* Save arrays */
273     $this->attrs['scalixEmailAddress']= $this->scalixEmailAddress;    
274     
275     /* Save boolean vars ... the scalix schema expects them as strings */
276     $this->attrs['scalixScalixObject'] = $this->scalixScalixObject?"TRUE":"FALSE";
277     $this->attrs['scalixAdministrator'] = $this->scalixAdministrator?"TRUE":"FALSE";
278     $this->attrs['scalixMailboxAdministrator'] = $this->scalixMailboxAdministrator?"TRUE":"FALSE";
279     $this->attrs['scalixLimitOutboundMail'] = $this->scalixLimitOutboundMail?"TRUE":"FALSE";
280     $this->attrs['scalixLimitInboundMail'] = $this->scalixLimitInboundMail?"TRUE":"FALSE";
281     $this->attrs['scalixLimitNotifyUser'] = $this->scalixLimitNotifyUser?"TRUE":"FALSE";
282     $this->attrs['scalixHideUserEntry'] = $this->scalixHideUserEntry?"TRUE":"FALSE";
284     /* Remove Mailquota if = "" */
285     if((isset($this->attrs['scalixLimitMailboxSize']))&&($this->attrs['scalixLimitMailboxSize']=="")) {
286       $this->attrs['scalixLimitMailboxSize']=array();
287     }
289     /* Save data to LDAP */
290     $ldap->cd($this->dn);
291     $ldap->modify($this->attrs);
292     show_ldap_error($ldap->get_error());
294     /* Optionally execute a command after we're done */
295     if ($this->initially_was_account == $this->is_account){
296       if ($this->is_modified){
297         $this->handle_post_events("modify");
298       }
299     } else {
300       $this->handle_post_events("add");
301     }
303   }
305   /* Check formular input */
306   function check()
307   {
308     $ldap= $this->config->get_ldap_link();
310     $message= array();
312     if(empty($this->scalixMailnode)){
313       $message[]= _("There is no scalix mailnode specified.");
314     }
315     if(empty($this->scalixScalixObject)){
316       $message[]= _("scalixScalixObject must be set!");
317     }
319     $ldap->cd($this->config->current['BASE']);
320     foreach($this->scalixEmailAddress as $k => $mail) {         
321       $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=scalixUserClass)(scalixEmailAddress=".
322         $mail.")(!(uid=".$this->uid."))(!(cn=".$this->uid.")))", array("uid"));
323   
324       if ($ldap->count() != 0){
325         $message[]= _("The email address '$mail' you've entered is already in use.");
326       }
327     }
329     /* Check quota */
330     if ($this->scalixLimitMailboxSize != '' && $this->acl_is_writeable("scalixLimitMailboxSize")){
331       if (!is_numeric($this->scalixLimitMailboxSize)) {
332         $message[]= _("Value in 'Limit Mailbox size' is not valid.");
333       } else {
334         $this->scalixLimitMailboxSize= (int) $this->scalixLimitMailboxSize;
335       }
336     }
338     return ($message);
339   }
341   /* Adapt from template, using 'dn' */
342   function adapt_from_template($dn)
343   {
344     plugin::adapt_from_template($dn);
346     foreach (array("scalixEmailAddress") as $val){
347       $this->$val= array();
348       if (isset($this->attrs["$val"]["count"])){
349         for ($i= 0; $i<$this->attrs["$val"]["count"]; $i++){
350           $value= $this->attrs["$val"][$i];
351           foreach (array("sn", "givenName", "uid") as $repl){
352             if (preg_match("/%$repl/i", $value)){
353               $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
354             }
355           }
356           array_push($this->$val, strtolower(rewrite($value)));
357         }
358       }
359     }
360     $this->mail= strtolower(rewrite($this->mail));
361   }
363   function addEmail($address)
364   {
365     $ldap= $this->config->get_ldap_link();
367     $address= strtolower($address);
369     /* Is this address already assigned in LDAP? */
370     $ldap->cd ($this->config->current['BASE']);
371     $ldap->search ("(&(objectClass=scalixUserClass)(|(scalixEmailAddress=$address)".
372         "(scalixEmailAddress=$address)))", array("uid"));
374     if ($ldap->count() > 0){
375       $attrs= $ldap->fetch ();
376       return ($attrs["uid"][0]);
377     }
379     /* Add to list of alternates */
380     if (!in_array($address, $this->scalixEmailAddress)){
381       $this->scalixEmailAddress[]= $address;
382       $this->is_modified= TRUE;
383     }
385     //sort ($this->scalixEmailAddress); //scalix makes the first email the destination, all others can receive
386     reset ($this->scalixEmailAddress);
388     return ("");
389   }
392   function delEmail($addresses)
393   {
394     $this->scalixEmailAddress= array_remove_entries ($addresses, $this->scalixEmailAddress);
395     $this->is_modified= TRUE;
396   }
399   function plInfo()
400   {
401     return (array(
402           "plDescription"     => _("Scalix account"),
403           "plSelfModify"      => TRUE,
404           "plDepends"         => array("user"),
405           "plPriority"        => 2,
406           "plSection"         => array("personal" => _("My account")),
407           "plCategory"        => array("users"),
408           "plOptions"         => array(),
410           "plProvidedAcls"  => array(
412             "scalixMailnode"            => _("Scalix Mail node"),
413             "scalixMailboxClass"        => _("Mailbox class"),
414             "scalixServerLanguage"      => _("Server language"),
415             "scalixAdministrator"       => _("Administrator"),
416             "scalixMailboxAdministrator"=> _("Mailbox administrator"),
417             "scalixHideUserEntry"       => _("Hide user entry in Scalix"),
418             "scalixLimitMailboxSize"    => _("Mailbox size limitations"),
419             "scalixLimitOutboundMail"   => _("Limit outbound"),
420             "scalixLimitInboundMail"    => _("Limit inbound"),
421             "scalixLimitNotifyUser"     => _("Notify user"),
422             "scalixEmailAddress"        => _("Scalix email addresses"))
424             ));
425   }
428   function saveCopyDialog()
429   {
430     if (isset($_POST['scalixTab'])){
432       /* Add email addresses */
433       if (isset($_POST['add_email'])){
435         /* Check write access */
436 #        if(!$this->acl_is_writeable("scalixEmailAddress")){
437 #          print_red(_("You are not allowed to modify alternate addresses"));
438 #        }else{
440           /* Check if given value is not empty */
441           if ($_POST['email_address'] != ""){
442             $valid= FALSE;
444             /* Valid mail address */
445             if( ($this->is_template && !is_email($_POST['email_address'], TRUE)) || 
446                 (!$this->is_template && !is_email($_POST['email_address'])) ){
447               print_red (_("You're trying to add an invalid email address to the list of alternate addresses."));
448             }else{
449               $valid = TRUE;
450             }
452             /* Mail address already in use ? */
453             if ($valid && ($user= $this->addEmail ($_POST['email_address'])) != ""){
454               $ui= get_userinfo();
455               if ($user != $ui->username){
456                 print_red (_("The address you're trying to add is already used by user")." '$user'.");
457               }
458             }
459 #          }
460         }
461       }
462   
463       /* Delete email addresses */
464       if (isset($_POST['delete_email']) && isset ($_POST['emails_list'])){
465 #        if(!$this->acl_is_writeable("scalixEmailAddress")){
466 #          print_red(_("You are not allowed to modify alternate addresses"));
467 #        }else{
468           if (count($_POST['emails_list'])){
469             $this->delEmail ($_POST['emails_list']);
470 #          }
471         }
472       }
473     }
474   }
477   function PrepareForCopyPaste($source)
478   {
479     plugin::PrepareForCopyPaste($source);
480     foreach ($this->fakeBooleanAttributes as $val){
481       if (isset($source["$val"][0])&&$source["$val"][0]=="TRUE"){
482         $this->$val = TRUE;
483       } else {
484         $this->$val = FALSE;
485       }
486     }
488     /* Load attributes containing arrays */
489     foreach (array("scalixEmailAddress") as $val){
490       if (isset($source["$val"]["count"])){
491         for ($i= 0; $i<$source["$val"]["count"]; $i++){
492           array_push($this->$val, $source["$val"][$i]);
493         }
494       }
495     }
496   }
499   /* Create the posix dialog part for copy & paste */
500   function getCopyDialog()
501   {
502     $smarty = get_smarty(); 
503     $smarty->assign("scalixEmailAddress",$this->scalixEmailAddress);
504     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));    
505     $ret = array();
506     $ret['string'] = $str;
507     $ret['status'] = "";
508     return($ret);
509  
510   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:rulere
514 ?>