Code

Added global base
[gosa.git] / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabs_user.inc";
22 class userManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Users";
26   var $plDescription= "This does something";
28   /* Dialog attributes */
29   var $usertab= NULL;
30   var $userlist= array();
31   var $ui= NULL;
32   var $acl= "";
33   var $templates= array();
34   var $got_uid= false;
35   var $departments=array();
37   var $CopyPasteHandler ;
38   var $enableCopyPaste = false;
40   function userManagement ($config, $ui)
41   {
42     /* Save configuration for internal use */
43     $this->config= $config;
44     $this->ui= $ui;
46     /* Copy & Paste handler */
47     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
48       $this->enableCopyPaste = true;
49     }
50     $this->CopyPasteHandler = new CopyPasteHandler($this->config);
52     /* Get global filter config */
53     if (!isset($_SESSION["userfilter"])){
54       $base= get_base_from_people($ui->dn);
55       $userfilter= array( "mailusers"       => "checked",
56           "unixusers"       => "checked",
57           "templates"       => "",
58           "subsearch"       => "",
59           "proxyusers"      => "checked",
60           "sambausers"      => "checked",
61           "faxusers"        => "checked",
62           "functionalusers" => "checked",
63           "regex"           => "*");
64       $_SESSION["userfilter"] = $userfilter;
65     }
66     if(!isset($_SESSION['CurrentMainBase']))  {
67       $base= get_base_from_people($ui->dn);
68       $_SESSION['CurrentMainBase'] = $base;
69     }
70   }
72   function execute()
73   {
74     /* Call parent execute */
75     plugin::execute();
77     /* LOCK  MESSAGE Vars */
78     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/");
81     $smarty       = get_smarty();             // Smarty instance
82     $userfilter   = get_global("userfilter"); // contains Filter Settings
83     $s_action     = "";                       // Contains the action to proceed
84     $s_entry      = "";                       // The value for s_action
85     $base_back    = "";                       // The Link for Backbutton
87     /* Start for New List Managment */
88     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
89       $s_action="open";
90       $s_entry = base64_decode($_GET['dep_id']);
91       $_SESSION['CurrentMainBase']= $this->config->departments[trim($s_entry)];
92     }
94     /* Test Posts */  
95     foreach($_POST as $key => $val){
96       // Post for delete
97       if(preg_match("/user_del.*/",$key)){
98         $s_action = "del";
99         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
100         // Post for edit
101       }elseif(preg_match("/user_edit_.*/",$key)){
102         $s_action="edit";
103         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
104         // Post for new
105       }elseif(preg_match("/dep_back.*/i",$key)){
106         $s_action="back";
107       }elseif(preg_match("/user_new.*/",$key)){
108         $s_action="new";
109       }elseif(preg_match("/dep_home.*/i",$key)){
110         $s_action="home";
111       }elseif(preg_match("/user_tplnew.*/i",$key)){
112         $s_action="new_tpl";
113       }elseif(preg_match("/user_chgpw.*/i",$key)){
114         $s_action="change_pw";
115         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
116       }elseif(preg_match("/dep_root.*/i",$key)){
117         $s_action="root";
118       }elseif(preg_match("/userfrom_tpl.*/i",$key)){
119         $s_action="create_user_from_tpl";
120         $s_entry  = preg_replace("/userfrom_tpl_/i","",$key);
121       }elseif(preg_match("/^editPaste.*/i",$key)){
122         $s_action="editPaste";
123       }elseif(preg_match("/^copy_.*/",$key)){
124         $s_action="copy";
125         $s_entry  = preg_replace("/^copy_/i","",$key);
126       }elseif(preg_match("/^cut_.*/",$key)){
127         $s_action="cut";
128         $s_entry  = preg_replace("/^cut_/i","",$key);
129       }
130     }
132     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
133       $s_action = "edit";
134       $s_entry  = $_GET['id'];
135     }
137     $s_entry  = preg_replace("/_.$/","",$s_entry);
139     if(preg_match("/.*-.*/",$s_entry)){
140       $s_tab   = preg_replace("/^.*-/i","",$s_entry);
141       $s_entry = preg_replace("/-.*$/i","",$s_entry);
142     }else{
143       $s_tab   = "user";
144     };
146     /* Department changed? */
147     if(isset($_POST['CurrentMainBase']) && $_POST['CurrentMainBase']){
148       $_SESSION['CurrentMainBase']= $_POST['CurrentMainBase'];
149     }
151     /* Homebutton is posted */
152     if($s_action=="home"){
153       $_SESSION['CurrentMainBase']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
154       $_SESSION['CurrentMainBase']=(preg_replace("/^[^,]+,/","",$_SESSION['CurrentMainBase']));
155     }
157     if($s_action=="root"){
158       $_SESSION['CurrentMainBase']=($this->config->current['BASE']);
159     }
162     /* If Backbutton is Posted */
163     if($s_action=="back"){
164       $base_back          = preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase']);
165       $base_back          = convert_department_dn($base_back);
167       if(isset($this->config->departments[trim($base_back)])){
168         $_SESSION['CurrentMainBase']= $this->config->departments[trim($base_back)];
169       }else{
170         $_SESSION['CurrentMainBase']= $this->config->departments["/"];
171       }
172     }
175     /* Only perform copy / paste if it is enabled
176      */
177     if($this->enableCopyPaste){
179       /* Paste copied/cutted object in here
180        */
181       if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
182         $this->CopyPasteHandler->save_object();
183         $this->CopyPasteHandler->SetVar("base",$_SESSION['CurrentMainBase']);
184         $ret    = $this->CopyPasteHandler->execute(false);
185         if($ret) return($ret);
186         $this->reload();
187         $dn = $this->CopyPasteHandler->lastdn;
189         foreach($this->list as $id => $entry){
190           if($entry['dn'] == $dn){
191             $s_entry = $id;
192           }
193         }
194         if($_POST['passwordTodo'] == "new"){
195           $s_action = "change_pw";
196         }
197       }
200       /* Copy current object to CopyHandler
201        */
202       if($s_action == "copy"){
203         $this->CopyPasteHandler->Clear();
205         $dn     = $this->list[trim($s_entry)]['dn'];
206         $acl    = get_permissions ($dn, $this->ui->subtreeACL);
208         $obj    = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
209         $objNew = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], "new");
211         $obj->set_acl($acl);
212         $objNew->set_acl($acl);
214         foreach($obj->by_object as $name => $tab){
215           foreach(array("photoData","jpegPhoto","is_account") as $attr){
216             if(isset($obj->by_object[$name]->$attr)){
217               $objNew->by_object[$name]->$attr = $obj->by_object[$name]->$attr;
218             }
219           } 
220         }
221         $this->CopyPasteHandler->Copy($obj,$objNew);
222       }
225       /* Copy current object to CopyHandler
226        */
227       if($s_action == "cut"){
228         $this->CopyPasteHandler->Clear();
229         $dn     = $this->list[trim($s_entry)]['dn'];
230         $acl= get_permissions ($dn, $this->ui->subtreeACL);
232         $obj    = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
233         $obj->set_acl($acl);
235         $this->CopyPasteHandler->Cut($obj);
236       }
237     }
239     /* End for new List Managment */
241     /* Save filter data if we are in the headpage */
242     if (!isset($this->usertab)){
243       if (isset($_POST['regexit'])){
244         $userfilter["regex"]= $_POST['regexit'];
245       }
246     }
247     if(isset($_POST['regexit'])){
248       foreach( array("functionalusers", "unixusers", "mailusers","sambausers", "proxyusers", "faxusers", "templates", "subsearch") as $type){
249         if (isset($_POST[$type])) {
250           $userfilter[$type]= "checked";
251         } else {
252           $userfilter[$type]= "";
253         }
254       }
255     }
256     if (isset($_GET['search'])){
257       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
258       if ($s == "**"){
259         $s= "*";
260       }
261       $userfilter['regex']= $s;
262     }
263     register_global("userfilter", $userfilter);
266     /* React on user interaction here */
267     if (($_SERVER["REQUEST_METHOD"] == "POST")||($_SERVER["REQUEST_METHOD"]== "GET")){
269       /* User wants to edit data? */
270       
271       /* Check for config to ensure that there is currently no user open */
272       if (($s_action=="edit") && (!isset($this->usertab->config))){
274         /* Get 'dn' from posted 'uid', must be unique */
276         $this->dn= $this->list[trim($s_entry)]['dn'];
278         /* Check locking, save current plugin in 'back_plugin', so
279            the dialog knows where to return. */
280         if (($user= get_lock($this->dn)) != ""){
281           return(gen_locked_message ($user, $this->dn));
282         }
284         /* Lock the current entry, so everyone will get the
285            above dialog */
286         add_lock ($this->dn, $this->ui->dn);
288         /* Set up the users ACL's for this 'dn' */
289         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
291         /* Register usertab to trigger edit dialog */
292         $this->usertab= new usertabs($this->config, 
293             $this->config->data['TABS']['USERTABS'], $this->dn);
294         //        $_POST[$s_tab]=$s_tab;
296         // This would be better
297         $this->usertab->current = $s_tab;
298         $this->usertab->set_acl($acl);
300         $_SESSION['objectinfo']= $this->dn;
301       }
302     }
304     /* Reset requested? */
305     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
306       if (isset($this->usertab)){
307         del_lock ($this->usertab->dn);
308         unset ($this->usertab);
309       }
310       $this->usertab= NULL;
311       $this->lognames= array();;
312       $this->sn= "";
313       $this->givenName= "";
314       $this->uid= "";
315       unset ($_SESSION['objectinfo']);
316     }
318     /* Password change requested */
319     if ($s_action=="change_pw"){
321       /* Get 'dn' from posted 'uid' */
322       $this->dn= $this->list[trim($s_entry)]['dn'];
324       /* Load permissions for selected 'dn' and check if
325          we're allowed to remove this 'dn' */
326       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
327       $acl= get_module_permission($acl, "user", $this->dn);
328       if (chkacl($acl, "password") == ""){
330         /* User is allowed to change passwords, save 'dn' and 'acl' for next
331            dialog. */
332         $this->acl= $acl;
333         $_SESSION['objectinfo']= $this->dn;
334         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
336       } else {
337         /* User is not allowed. Show message and cancel. */
338         print_red (_("You are not allowed to set this users password!"));
339       }
340     }
342     /* Perform password change */
343     if (isset($_POST['password_finish'])){
345       /* For security reasons, check if user is allowed to set password again */
346       if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
348         /* Check input and feed errors into 'message' */
349         $message= array();
351         /* Sanity checks... */
352         if ($_POST['new_password'] != $_POST['repeated_password']){
354           /* Matching passwords in new and repeated? */
355           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
356         } else {
358           /* Empty password is not permitted by default. */
359           if ($_POST['new_password'] == ""){
360             $message[]= _("The password you've entered as 'New password' is empty.");
361           }
362         }
364         /* Errors, or change password? */
365         if (count($message) != 0){
367           /* Show error message and continue editing */
368           show_errors ($message);
369           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
370         }
372         $config= $this->config;
373         $ldap_ui= $this->config->get_ldap_link();
374         if(isset($this->usertab->dn)){
375           $user = ($ldap_ui->fetch($ldap_ui->cat($this->usertab->dn)));
376         }else{
377           $user = ($ldap_ui->fetch($ldap_ui->cat($this->dn)));
378         }
379         if((is_array($user))&&(isset($user['uid']))){
380           $username= $user['uid'][0];
381         }
383         /* Set password, perform required steps */
384         if ($this->usertab){
385           if ($this->usertab->password_change_needed()){
386             $obj= $this->usertab->by_object['user'];
387             change_password ($this->usertab->dn, $_POST['new_password'],
388                 0, $obj->pw_storage);
389             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
390               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
391             }
393             gosa_log ("Password for '".$this->usertab->dn."' has been changed");
394             unset($this->usertab);
395             $this->usertab= NULL;
396           }
397         } else {
398           change_password ($this->dn, $_POST['new_password']);
399           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
400             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
401           }
403           gosa_log ("Password for '".$this->dn."' has been changed");
404         }
405       } else {
407         /* Missing permissions, show message */
408         print_red (_("You are not allowed to set this users password!"));
409       }
411       /* Clean session, delete lock */
412       del_lock ($this->dn);
413       $this->reload();
414       unset ($this->usertab);
415       $this->usertab= NULL;
416       $this->lognames= array();;
417       $this->sn= "";
418       $this->givenName= "";
419       $this->uid= "";
420       unset ($_SESSION['objectinfo']);
421     }
424     /* Remove user was requested */
425     if ($s_action=="del"){
427       /* Get 'dn' from posted 'uid' */
428       $this->dn= $this->list[trim($s_entry)]['dn'];
430       /* Load permissions for selected 'dn' and check if
431          we're allowed to remove this 'dn' */
432       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
433       $this->acl= get_module_permission($acl, "user", $this->dn);
434       if (chkacl($this->acl, "delete") == ""){
436         /* Check locking, save current plugin in 'back_plugin', so
437            the dialog knows where to return. */
438         if (($user= get_lock($this->dn)) != ""){
439           return(gen_locked_message ($user, $this->dn));
440         }
442         /* Lock the current entry, so nobody will edit it during deletion */
443         add_lock ($this->dn, $this->ui->dn);
444         $smarty->assign("info", sprintf(_("You're about to delete the user %s."), LDAP::fix($this->dn)));
445         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
446       } else {
448         /* Obviously the user isn't allowed to delete. Show message and
449            clean session. */
450         print_red (_("You are not allowed to delete this user!"));
451       }
452     }
456     /* Confirmation for deletion has been passed. User should be deleted. */
457     if (isset($_POST['delete_user_confirm'])){
459       /* Some nice guy may send this as POST, so we've to check
460          for the permissions again. */
461       if (chkacl($this->acl, "delete") == ""){
463         /* Delete request is permitted, perform LDAP action */
464         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
465             $this->dn);
466         $this->usertab->set_acl(array($this->acl));
467         $this->usertab->delete ();
468         gosa_log ("User object '".$this->dn."' has been removed");
469         unset ($this->usertab);
470         $this->usertab= NULL;
472         /* User list has changed, reload it. */
473         $this->reload ();
474       } else {
476         /* Normally this shouldn't be reached, send some extra
477            logs to notify the administrator */
478         print_red (_("You are not allowed to delete this user!"));
480         if(isset($this->ui->uid)){
481           gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
482         }
483       }
485       /* Remove lock file after successfull deletion */
486       del_lock ($this->dn);
487     }
490     /* Delete user canceled? */
491     if (isset($_POST['delete_cancel'])){
492       del_lock ($this->dn);
493     }
496     /* Finish user edit is triggered by the tabulator dialog, so
497        the user wants to save edited data. Check and save at this
498        point. */
499     if ((isset($_POST['edit_finish'])) && (isset($this->usertab->config))){
501       /* Check tabs, will feed message array */
502       $this->usertab->last= $this->usertab->current;
503       $this->usertab->save_object();
504       $message= $this->usertab->check();
506       /* Save, or display error message? */
507       if (count($message) == 0){
509         /* No errors. Go ahead and prepare to ask for a password
510            in case we're creating a new user. 'dn' will be 'new'
511            in this case. It is set to the correct value later. */
512         if ($this->dn == "new"){
513           $set_pass= 1;
514         } else {
515           $set_pass= 0;
516         }
518         /* Save user data to ldap */
519         if($this->usertab->save() == 1){
520           gosa_log ("User object '".$this->dn."' saving failed.");
521           return;
522         }
523         gosa_log ("User object '".$this->dn."' has been saved");
525         /* User has been saved successfully, remove lock from
526            LDAP. */
527         if ($this->dn != "new"){
528           del_lock ($this->dn);
529         }
531         /* In case of new users, ask for a password */
532         if (($set_pass || $this->usertab->password_change_needed()) &&
533             !$this->is_template){
535           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
536         }
538         /* There's no page reload so we have to read new users at
539            this point. */
540         $this->reload ();
541         unset ($this->usertab);
542         $this->usertab= NULL;
543         unset ($_SESSION['objectinfo']);
545       } else {
546         /* Ok. There seem to be errors regarding to the tab data,
547            show message and continue as usual. */
548         show_errors($message);
549       }
550     }
552     /* Generate template list */
553     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
555       $this->templates= array();
557       $ldap= $this->config->get_ldap_link();
559       /* Create list of tempaltes */
560       foreach ($this->config->departments as $key => $value){
561     
562         /* Get acls from different ou's */
563         $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
564         $acl= get_module_permission($acl, "user", get_people_ou().$value);
565   
566         /* If creation of a new user is allowed, append this template */
567         if (chkacl($acl, "create") == ""){
568           
569           /* Search all templates from the current dn */
570           $ldap->cd (get_people_ou().$value);
571           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
573           /* Append */
574           if ($ldap->count() != 0){
575             while ($attrs= $ldap->fetch()){
576               $this->templates[$ldap->getDN()]=
577                 $attrs['uid'][0]." - $key";
578             }
579             $this->templates['none']= _("none");
580           }
581         }
582       }
584       /* Sort templates */
585       natcasesort ($this->templates);
586       reset ($this->templates);
587     }
589     /* New user/template request */
590     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
591       /* By default we set 'dn' to 'new', all relevant plugins will
592          react on this. */
593       $this->dn= "new";
594       if (isset($this->config->current['IDGEN'])){
595         $this->got_uid= false;
596       } else {
597         $this->got_uid= true;
598       }
600       /* Create new usertab object */
601       $this->usertab= new usertabs($this->config,
602           $this->config->data['TABS']['USERTABS'], $this->dn);
603       $this->usertab->set_acl(array(':all'));
604       $this->usertab->by_object['user']->base= $_SESSION['CurrentMainBase'];
606       /* Take care about templates */
607       if ($s_action=="new_tpl"){
608         $this->is_template= TRUE;
609         $this->usertab->set_template_mode ();
610       } else {
611         $this->is_template= FALSE;
612       }
614       /* Use template if there are any of them */
615   
616       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
617         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
618           $smarty->assign("$attr", $this->$attr);
619         }
620         if ($s_action=="create_user_from_tpl"){
621           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
622         } else {
623           $smarty->assign("template", "none");
624         }
625         $smarty->assign("edit_uid", "");
626         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
627       }
628     }
630     /* Continue template editing */
631     if ((isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid']))){
632       $this->sn= $_POST['sn'];
633       $this->givenName= $_POST['givenName'];
635       /* Check for requred values */
636       $message= array();
637       if ($this->sn == "") {
638         $message[]= _("The required field 'Name' is not set.");
639       }
640       if ($this->givenName == "") {
641         $message[]= _("The required field 'Given name' is not set.");
642       }
644       /* Check if dn is used */
645       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
646       $ldap= $this->config->get_ldap_link();
647       $ldap->cd ($dn);
648       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
649       if ($ldap->count () != 0){
650         $message[]= _("A person with the choosen name is already used in this tree.");
651       }
653       /* Show error message / continue editing */
654       if (count($message) > 0){
655         show_errors ($message);
656       } else {
657         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
658         if (isset($this->config->current['IDGEN']) &&
659             $this->config->current['IDGEN'] != ""){
660           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
661           if (count($uids)){
662             $smarty->assign("edit_uid", "false");
663             $smarty->assign("uids", $uids);
664             $this->uid= current($uids);
665           }
666         } else {
667           $smarty->assign("edit_uid", "");
668           $this->uid= "";
669         }
670         $this->got_uid= true;
671       }
673       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
674         $smarty->assign("$attr", $this->$attr);
675       }
676       if (isset($_POST['template'])){
677         $smarty->assign("template", $_POST['template']);
678       }
679       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
680     }
682     /* No template. Ok. Lets fill data into the normal user dialog */
683     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
684       foreach(array("sn", "givenName", "uid") as $attr){
685         if (isset($_POST[$attr])){
686           $this->usertab->by_object['user']->$attr= $_POST[$attr];
687         }
688       }
689     }
691     /* Finish template preamble */
692     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
694       /* Might not be filled if IDGEN is unset */
695       $this->sn                 = $_POST['sn'];
696       $this->givenName          = $_POST['givenName'];
698       /* Move user supplied data to sub plugins */
699       $this->uid                = $_POST['uid'];
700       $this->usertab->uid       = $this->uid;
701       $this->usertab->sn        = $this->sn;
702       $this->usertab->givenName = $this->givenName;
703       $template_dn              = $_POST['template'];
704       $this->usertab->adapt_from_template($template_dn);
705       $template_base            = preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
706       $this->usertab->by_object['user']->base= $template_base;
708       /* Set up the users ACL's for this 'dn' */
709       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
710       $this->usertab->set_acl($acl);
711     }
712     
713     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
714       $this->usertab->by_object['user']->base= $_SESSION['CurrentMainBase'];
715     }
717     /* Show tab dialog if object is present */
718     if(isset($this->usertab->config)){
719       $display= $this->usertab->execute();
721       /* Don't show buttons if tab dialog requests this */
722       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
723         $display.= "<p style=\"text-align:right\">\n";
724         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
725         $display.= "&nbsp;\n";
726         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
727         $display.= "</p>";
728       }
729       return ($display);
730     }
732     $smarty= get_smarty();
734     /* Check sorting variable */
735     if (!isset($this->usertab) &&
736         !isset($_POST['new_user']) &&
737         !isset($_POST['new_template']) &&
738         !isset($_POST['delete_user']) &&
739         !isset($_POST['setpass_user']) &&
740         !isset($_POST['select_user'])){
741       $this->reload();
742     }
744     /* Check for exeeded sizelimit */
745     if (($message= check_sizelimit()) != ""){
746       return($message);
747     }
749     /* Prepare departments */
750     $options= "";
751     foreach ($this->config->idepartments as $key => $value){
752       if ($_SESSION['CurrentMainBase'] == $key){
753         $options.= "<option selected value='$key'>$value</option>";
754       } else {
755         $options.= "<option value='$key'>$value</option>";
756       }
757     }
760     /* Create paste icon
761      */
762     if($this->enableCopyPaste){
763       $Copy_Paste = " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
764       if($this->CopyPasteHandler->isCurrentObjectPastAble()){
765         if($this->CopyPasteHandler->isCurrentCutted()){
766           $img = "images/cutpaste.png";
767         }else{
768           $img = "images/copypaste.png";
769         }
770         $Copy_Paste .= "<input type='image' name='editPaste' class='center'
771           src='".$img."' alt='"._("Paste")."' title='".$this->CopyPasteHandler->GetCurrentDn()."'>&nbsp;";
772       }else{
773         $Copy_Paste .= "<img class='center' src='images/cant_editpaste.png' alt='"._("Can't paste")."'>&nbsp;";
774       }
775     }else{
776       $Copy_Paste ="";
777     }
780     /* NEW LIST MANAGMENT */
781     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
782       " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
783       " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
784       " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
785       " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
786       " <img   class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
787       " <input class='center' type='image' align='middle' src='images/list_new_user.png' title='"._("Create new user")."' alt='"._("New user")."' name='user_new'>&nbsp;".
788       " <input class='center' type='image' align='middle' src='images/list_new.png' title='"._("Create new template")."' alt='"._("New template")."' name='user_tplnew'>&nbsp;".
789       $Copy_Paste.
790       " <img   class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
791       _("Base")."&nbsp;<select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
792       " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
793       "</div>";
797     
798     if($this->enableCopyPaste){
799       $action = "<input class='center' type='image'
800         src='images/editcut.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'>&nbsp;";
801       $action.= "<input class='center' type='image'
802         src='images/editcopy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'>&nbsp;";
803       $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'     
804         name='user_edit_%KEY%' title='"._("Edit user")."'>";
805       $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."' 
806         name='user_chgpw_%KEY%' title='"._("Change password")."'>";
807       $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   
808         name='user_del_%KEY%' title='"._("Delete user")."'>";
809     }else{
810       $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'     
811         name='user_edit_%KEY%' title='"._("Edit user")."'>";
812       $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."' 
813         name='user_chgpw_%KEY%' title='"._("Change password")."'>";
814       $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   
815         name='user_del_%KEY%' title='"._("Delete user")."'>";
816     }
820     $divlist = new divlist("userstab");
821     $divlist->SetHeader(array(
822           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
823           array("string" => _("Username")." / "._("Department"), "attach" => "style=''"),
824           array("string" => _("Properties"), "attach" => "style='width:152px;'"),
825           array("string" => _("Actions"), "attach" => "style='width:90px;border-right:0px;text-align:right;'")));
828     $divlist->SetSummary(_("This table displays all users, in the selected tree."));
829     $divlist->SetEntriesPerPage(0);
831     // Defining Links 
832     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
834     /* Insert departments in divlist*/
835     foreach($this->departments as $key=> $val){
837       if(!isset($this->config->departments[trim($key)])){
838         $this->config->departments[trim($key)]="";
839       }
841       $non_empty="";
842       $keys= str_replace("/","\/",$key);
843       foreach($this->config->departments as $keyd=>$vald ){
844         if(preg_match("/".$keys."\/.*/",$keyd)){
845           $non_empty="full";
846         }
847       }
849       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
850       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
851       $field3 = array("string" => "&nbsp;", "attach" => "style='width:152px;'");
852       $field4 = array("string" => "&nbsp;", "attach" => "style='width:90px;border-right:0px;text-align:right;'");
855       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
856     }
858     /* Pictures for Extensions */
859     $usrimg   ="<input class='center' type='image' src='images/penguin.png' alt='"._("GOsa")."'   
860                     name='user_edit_%KEY%-user' title='"._("Edit generic properties")."'>";
861     $posiximg = "<input class='center' type='image' src='images/select_user.png' alt='"._("Posix")."'  
862                     name='user_edit_%KEY%-posixAccount' title='"._("Edit UNIX properties")."'>";
863     $eviroimg = "<input class='center' type='image' src='images/smallenv.png' alt='"._("Environment")."'  
864                     name='user_edit_%KEY%-environment' title='"._("Edit environment properties")."'>";
865     $mailimg  = "<input class='center' type='image' src='images/mailto.png' alt='"._("Mail")."'     
866                     name='user_edit_%KEY%-mailAccount' title='"._("Edit mail properties")."'>";
867     $fonimg   = "<input class='center' type='image' src='images/%image%' alt='"._("Phone")."'     
868                     name='user_edit_%KEY%-phoneAccount' title='"._("Edit phone properties")."%title%'>";
869     $faximg   = "<input class='center' type='image' src='images/fax_small.png' alt='"._("Fax")."'     
870                     name='user_edit_%KEY%-gofaxAccount' title='"._("Edit fax properies")."'>";
871     $sambaimg = "<input class='center' type='image' src='images/select_winstation.png' alt='"._("Samba")."'     
872                     name='user_edit_%KEY%-sambaAccount' title='"._("Edit samba properties")."'>"; 
873     $tplcreateuserimg  = "<input type='image' class='center' src='images/list_new.png' alt='"._("Create user from template")."'
874                     name='userfrom_tpl_%KEY%' title='"._("Create user with this template")."'>";   
875     
877     $empty    = "<img class='center' src='images/empty.png' style='width:16px;height:16px;' alt=''>"; 
879     // User and Template  Images  
880     $userimg  = "<img class='center' src='images/select_user.png' alt='User' title='%s'>";   
881     $tplimg   = "<img class='center' src='images/select_template.png' alt='Template' title='%s'>";   
882     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
884     // Defines Arrays to save User and Templates
885     $tpls = array();
886     $users= array();
888     /* Get Configuration for goFon DB (if it is set), to connect to the asterisk tables.
889      * Read Sip Table for specified account (for each user with phoneAccount).
890      * Check the attributes ip port and regseconds.
891      * If regseconds is set and >0 , the phone is logged in.
892      * Else the phone is currently not logged.
893      * If we can't read any Data from the DB or there is no goFon DB specified
894      * show old style without status icons.
895      */
896     $r_db   =false;
897     $r_con  =false;
898     if (isset($_SESSION['config']->data['SERVERS']['FON'])){
899       $a_SETUP= $_SESSION['config']->data['SERVERS']['FON'];
900       // Connect to DB server
901   
902       /* 
903         If you want to disable the status pictures, just comment out the next line
904       */
905       $r_con = false;
906       $r_db  = false;
907       if(is_callable("mysql_pconnect")){
908         $r_con= @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
909         // Check if we are  connected correctly
910         if($r_con){
911           $r_db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
912         }
913       }
914     }
916     // Test Every Entry and generate divlist Array  
917     foreach($this->list as $key => $val){
918       $connected  = ""; // This string represents timestamp or offline status 
919       $ip_port    = ""; // String that will represent ip : port of the connected phone
920       if((in_array("goFonAccount"    ,$val['objectClass']))){
921   
922         /* Set defaults */
923         $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
924         $fonac = preg_replace("/%KEY%/", "$key", $fonac);
925         $fonac = preg_replace("/%title%/", "", $fonac);
927         /* Database connection is ok ?*/
928         if(($r_db)&&(is_callable("mysql_query"))){
929           $res= @mysql_query("SELECT regseconds,name,port,ipaddr FROM ".$a_SETUP['SIP_TABLE']." WHERE (name='".$val['uid'][0]."')");
930           $mysql_entry = @mysql_fetch_row($res);
931           if(is_array($mysql_entry)){
932             if((isset($mysql_entry[0]))&&($mysql_entry[0]>1)){
933               $connected = " | "._("Online")." : ".gmdate("d.m.Y H:i:s",($mysql_entry[0]+(60*60)));
934               $fonac = preg_replace("/%image%/", "select_phone_connected.png", $fonimg);
935               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
936               $fonac = preg_replace("/%title%/", $connected, $fonac);
937               $ip_port= " - ".$mysql_entry[3].":".$mysql_entry[2];
938             }
939             if((isset($mysql_entry[0]))&&($mysql_entry[0]==0)){
940               $connected = " | "._("Offline");
941               $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
942               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
943               $fonac = preg_replace("/%title%/", $connected, $fonac);
944             }
945           }
946         }
947       }else{
948         $fonac=$empty;
949       }
951     
953       // Specify Pics for Extensions  
954       if(in_array("posixAccount"    ,$val['objectClass'])){ 
955         $posix = preg_replace("/%KEY%/", "$key", $posiximg);
956       }else{ 
957         $posix=$empty;
958       }
959       
960       if(in_array("gotoEnvironment"    ,$val['objectClass'])){ 
961         $enviro = preg_replace("/%KEY%/", "$key", $eviroimg);
962       }else{ 
963         $enviro =$empty;
964       }
965    
966       if(in_array("gosaMailAccount" ,$val['objectClass'])){ 
967         $maila = preg_replace("/%KEY%/", "$key", $mailimg);   
968       }else{ 
969         $maila=$empty;
970       } 
971   
972       if(in_array("goFaxAccount"    ,$val['objectClass'])){ 
973         $faxac = preg_replace("/%KEY%/", "$key", $faximg);    
974       }else{ 
975         $faxac=$empty;
976       } 
978       if(in_array("sambaSamAccount" ,$val['objectClass'])){ 
979         $samba = preg_replace("/%KEY%/", "$key", $sambaimg);  
980       }else{ 
981         $samba=$empty;
982       } 
984       if(in_array("gosaUserTemplate",$val['objectClass'])){ 
985         $tpl                          = preg_replace("/%KEY%/", "$key", $tplimg);   
986         $s_img_create_from_template   = preg_replace("/%KEY%/", "$key", $tplcreateuserimg);
987       }else{ 
988         $s_img_create_from_template   = "";
989         $tpl=$userimg;
990       }
992       $usrimg2 = preg_replace("/%KEY%/", "$key", $usrimg);
994       // Generate caption for rows
995       if (isset($val["sn"]) && isset($val["givenName"])){
996         $display= $val["sn"][0].", ".$val["givenName"][0]." [".$val["uid"][0]."]";
997       } else {
998         $display= "[".$val["uid"][0]."]";
999       }
1001       /* Create each field */
1002       $field1 = array("string" => sprintf($tpl,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
1003       $field2 = array("string" => sprintf($editlink,$key,$display).$ip_port, "attach" => "style='' title='".$val['dn']."'");
1004       $field3 = array("string" => $usrimg2."&nbsp;".$posix."&nbsp;".$enviro."&nbsp;".$maila."&nbsp;".$fonac."&nbsp;".$faxac."&nbsp;".$samba."&nbsp;".$s_img_create_from_template, "attach" => "style='width:152px;'");
1005       $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='width:90px;border-right:0px;text-align:right;'");
1007       $add = array($field1,$field2,$field3,$field4);
1009       // Template or User
1010       if(in_array("gosaUserTemplate",$val['objectClass'])){
1011         $tpls[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
1012       }else{
1013         $users[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
1014       }
1015     }
1017     // Sort Entries 
1018     ksort($users);
1019     ksort($tpls);
1021     // Append Templates
1022     foreach($tpls as $key => $val){
1023       $divlist->AddEntry( $val, array(""));
1024     }
1025     // Append Users 
1026     foreach($users as $key => $val){
1027       $divlist->AddEntry( $val, array(""));
1028     }
1029     /* Show main page */
1030     //$smarty->assign("userlist",   nl2br(htmlentities($divlist->DrawList())));
1031     $smarty->assign("userlist",   $divlist->DrawList());
1032     $smarty->assign("userlisthead", $listhead);
1034     /* END NEW LIST MANAGMENT
1035      */  
1036     $smarty->assign("search_image", get_template_path('images/search.png'));
1037     $smarty->assign("tree_image", get_template_path('images/tree.png'));
1038     $smarty->assign("infoimage", get_template_path('images/info.png'));
1039     $smarty->assign("launchimage", get_template_path('images/launch.png'));
1040     $smarty->assign("deplist", $this->config->idepartments);
1041     foreach( array("functionalusers", "unixusers",
1042           "mailusers", "sambausers", "proxyusers", 
1043           "faxusers", "templates", "subsearch") as $type){
1045       $smarty->assign("$type", $userfilter[$type]);
1046     }
1047     $smarty->assign("CurrentMainBase",$_SESSION['CurrentMainBase']);
1049     $smarty->assign("regex", $userfilter["regex"]);
1050     /* Extend if we are not using javascript */
1051     $smarty->assign("apply", apply_filter());
1052     $smarty->assign("alphabet", generate_alphabet());
1053     $smarty->assign("hint", print_sizelimit_warning());
1055     if((is_callable("mysql_close"))&&($r_con)){
1056       @mysql_close($r_con);
1057     }
1058     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
1059   }
1062   function reload()
1063   {
1064     /* Get config */
1065     $userfilter= get_global('userfilter');
1067     /* Set base for all searches */
1068     $base= $_SESSION['CurrentMainBase'];
1070     /* Regex filter? */
1071     if ($userfilter['regex'] != ""){
1072       $regex= $userfilter['regex'];
1073     } else {
1074       $regex= "*";
1075     }
1077     /* Set filter depending on selection */
1078     if ($this->config->current['SAMBAVERSION'] == 3){
1079       $samba= "sambaSamAccount";
1080     } else {
1081       $samba= "sambaAccount";
1082     }
1085     /* Sometimes not all users were displayed in the users list.
1086        The problem was, that we search for an objectClass
1087         which isn't used in every ldap-schema configuration.
1088        Here we remove this class, if there is no result returned from 
1089         our test search.
1090     */
1091     $ldap = $this->config->get_ldap_link();
1092     $ldap->cd($this->config->current['BASE']);
1094     $ldap->search("(objectClass=goFaxAccount)",array("cn","uid"));
1095     if($ldap->fetch()){
1096       $useFaxInFilter = true;
1097       $FaxFilter = "(objectClass=goFaxAccount)";
1098     } else {
1099       $useFaxInFilter = false;
1100       $FaxFilter = "";
1101     }
1102    
1103  
1104     $filter="";
1105     if ($userfilter['functionalusers'] == "checked"){
1106       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=$samba)".$FaxFilter."(objectClass=gosaProxyAccount))))";
1107     }
1108     if ($userfilter['unixusers'] == "checked"){
1109       $filter.= "(objectClass=posixAccount)";
1110     }
1111     if ($userfilter['mailusers'] == "checked"){
1112       $filter.= "(objectClass=gosaMailAccount)";
1113     }
1114     if ($userfilter['sambausers'] == "checked"){
1115       $filter.= "(objectClass=$samba)";
1116     }
1117     if ($userfilter['proxyusers'] == "checked"){
1118       $filter.= "(objectClass=gosaProxyAccount)";
1119     }
1120     if (($userfilter['faxusers'] == "checked") && ($useFaxInFilter)){
1121       $filter.= "(objectClass=goFaxAccount)";
1122     }
1123     if ($userfilter['templates'] == "checked"){
1124       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1125     } else {
1126       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1127     }
1128     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
1130     /* Generate userlist */
1131     $ldap= $this->config->get_ldap_link(TRUE);
1132     if ($userfilter['subsearch'] == "checked"){
1133       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
1134     } else {
1135       $base= get_people_ou().$base;
1136       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
1137     }
1140     /* NEW LIST MANAGMENT
1141      * We also need to search for the departments
1142      * So we are able to navigate like in konquerer 
1143      */
1145     /* Create base to search in */
1147     $peopleOU = get_people_ou();
1148     if(empty($peopleOU))  {
1149       $base2 = $base;
1150     }else{
1151       $base2 = preg_replace("/".$peopleOU."/i","",$base);
1152     }
1154     /* Get all departments within this subtree */
1155     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
1156         TRUE, $base2, array("ou", "description"), TRUE);
1158     $this->departments= array();
1160     /* Create array with results */
1161     $tmp = array();
1162     foreach ($res3 as $value){
1163       $tmp[strtolower($value['dn']).$value['dn']]=$value;
1164     }
1166     /* Sort array */
1167     ksort($tmp);
1169     /* Create result array */
1170     foreach($tmp as $value){
1171       if(isset($value['description'][0])){
1172         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
1173       }else{
1174         $this->departments[$value['dn']]=convert_department_dn2($value['dn']);
1175       }
1176     }
1177     /* END NEW LIST MANAGMENT 
1178      */
1181     $this->userlist= array();
1183     foreach ($this->list as $value){
1184       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
1185         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
1186           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
1187           continue;
1188         }
1189         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
1190           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
1191             $value["givenName"][0].
1192             " [".$value["uid"][0]."]";
1193         } else {
1194           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
1195         }
1196       }
1197     }
1199     natcasesort ($this->userlist);
1200     reset ($this->userlist);
1201   }
1203   function remove_from_parent()
1204   {
1205   }
1208   /* Check values */
1209   function check()
1210   {
1211   }
1214   /* Save to LDAP */
1215   function save()
1216   {
1217   }
1219   function adapt_from_template($dn)
1220   {
1221   }
1223   function password_change_needed()
1224   {
1225   }
1227   function show_header($button_text, $text, $disabled= FALSE)
1228   {
1229   }
1231   function remove_lock()
1232   {
1233     if (isset($this->usertab->dn)){
1234       del_lock ($this->usertab->dn);
1235     }
1236   }
1240 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1241 ?>