Code

1a4d6ba7ce6b10fba69368e171c3e67e79a3d231
[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           "depselect"       => $base,
64           "regex"           => "*");
65       $_SESSION["userfilter"] = $userfilter;
66     }
67   }
69   function execute()
70   {
71     /* Call parent execute */
72     plugin::execute();
74     /* LOCK  MESSAGE Vars */
75     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/");
78     $smarty       = get_smarty();             // Smarty instance
79     $userfilter   = get_global("userfilter"); // contains Filter Settings
80     $s_action     = "";                       // Contains the action to proceed
81     $s_entry      = "";                       // The value for s_action
82     $base_back    = "";                       // The Link for Backbutton
84     /* Start for New List Managment */
85     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
86       $s_action="open";
87       $s_entry = base64_decode($_GET['dep_id']);
88       $userfilter['depselect']= "".$this->config->departments[trim($s_entry)];
89     }
91     /* Test Posts */  
92     foreach($_POST as $key => $val){
93       // Post for delete
94       if(preg_match("/user_del.*/",$key)){
95         $s_action = "del";
96         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
97         // Post for edit
98       }elseif(preg_match("/user_edit_.*/",$key)){
99         $s_action="edit";
100         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
101         // Post for new
102       }elseif(preg_match("/dep_back.*/i",$key)){
103         $s_action="back";
104       }elseif(preg_match("/user_new.*/",$key)){
105         $s_action="new";
106       }elseif(preg_match("/dep_home.*/i",$key)){
107         $s_action="home";
108       }elseif(preg_match("/user_tplnew.*/i",$key)){
109         $s_action="new_tpl";
110       }elseif(preg_match("/user_chgpw.*/i",$key)){
111         $s_action="change_pw";
112         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
113       }elseif(preg_match("/dep_root.*/i",$key)){
114         $s_action="root";
115       }elseif(preg_match("/userfrom_tpl.*/i",$key)){
116         $s_action="create_user_from_tpl";
117         $s_entry  = preg_replace("/userfrom_tpl_/i","",$key);
118       }elseif(preg_match("/^editPaste.*/i",$key)){
119         $s_action="editPaste";
120       }elseif(preg_match("/^copy_.*/",$key)){
121         $s_action="copy";
122         $s_entry  = preg_replace("/^copy_/i","",$key);
123       }elseif(preg_match("/^cut_.*/",$key)){
124         $s_action="cut";
125         $s_entry  = preg_replace("/^cut_/i","",$key);
126       }
127     }
129     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
130       $s_action = "edit";
131       $s_entry  = $_GET['id'];
132     }
134     $s_entry  = preg_replace("/_.$/","",$s_entry);
136     if(preg_match("/.*-.*/",$s_entry)){
137       $s_tab   = preg_replace("/^.*-/i","",$s_entry);
138       $s_entry = preg_replace("/-.*$/i","",$s_entry);
139     }else{
140       $s_tab   = "user";
141     };
143     /* Department changed? */
144     if(isset($_POST['depselect']) && $_POST['depselect']){
145       $userfilter['depselect']= $_POST['depselect'];
146     }
148     /* Homebutton is posted */
149     if($s_action=="home"){
150       $userfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
151       $userfilter['depselect']=(preg_replace("/^[^,]+,/","",$userfilter['depselect']));
152     }
154     if($s_action=="root"){
155       $userfilter['depselect']=($this->config->current['BASE']);
156     }
159     /* If Backbutton is Posted */
160     if($s_action=="back"){
161       $base_back          = preg_replace("/^[^,]+,/","",$userfilter['depselect']);
162       $base_back          = convert_department_dn($base_back);
164       if(isset($this->config->departments[trim($base_back)])){
165         $userfilter['depselect']= $this->config->departments[trim($base_back)];
166       }else{
167         $userfilter['depselect']= $this->config->departments["/"];
168       }
169     }
172     /* Only perform copy / paste if it is enabled
173      */
174     if($this->enableCopyPaste){
176       /* Paste copied/cutted object in here
177        */
178       if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
179         $this->CopyPasteHandler->save_object();
180         $this->CopyPasteHandler->SetVar("base",$userfilter['depselect']);
181         $ret    = $this->CopyPasteHandler->execute(false);
182         if($ret) return($ret);
183         $this->reload();
184         $dn = $this->CopyPasteHandler->lastdn;
186         foreach($this->list as $id => $entry){
187           if($entry['dn'] == $dn){
188             $s_entry = $id;
189           }
190         }
191         if($_POST['passwordTodo'] == "new"){
192           $s_action = "change_pw";
193         }
194       }
197       /* Copy current object to CopyHandler
198        */
199       if($s_action == "copy"){
200         $this->CopyPasteHandler->Clear();
202         $dn     = $this->list[trim($s_entry)]['dn'];
203         $acl    = get_permissions ($dn, $this->ui->subtreeACL);
205         $obj    = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
206         $objNew = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], "new");
208         $obj->set_acl($acl);
209         $objNew->set_acl($acl);
211         foreach($obj->by_object as $name => $tab){
212           foreach(array("photoData","jpegPhoto","is_account") as $attr){
213             if(isset($obj->by_object[$name]->$attr)){
214               $objNew->by_object[$name]->$attr = $obj->by_object[$name]->$attr;
215             }
216           } 
217         }
218         $this->CopyPasteHandler->Copy($obj,$objNew);
219       }
222       /* Copy current object to CopyHandler
223        */
224       if($s_action == "cut"){
225         $this->CopyPasteHandler->Clear();
226         $dn     = $this->list[trim($s_entry)]['dn'];
227         $acl= get_permissions ($dn, $this->ui->subtreeACL);
229         $obj    = new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
230         $obj->set_acl($acl);
232         $this->CopyPasteHandler->Cut($obj);
233       }
234     }
236     /* End for new List Managment */
238     /* Save filter data if we are in the headpage */
239     if (!isset($this->usertab)){
240       if (isset($_POST['regexit'])){
241         $userfilter["regex"]= $_POST['regexit'];
242       }
243     }
244     if(isset($_POST['regexit'])){
245       foreach( array("functionalusers", "unixusers", "mailusers","sambausers", "proxyusers", "faxusers", "templates", "subsearch") as $type){
246         if (isset($_POST[$type])) {
247           $userfilter[$type]= "checked";
248         } else {
249           $userfilter[$type]= "";
250         }
251       }
252     }
253     if (isset($_GET['search'])){
254       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
255       if ($s == "**"){
256         $s= "*";
257       }
258       $userfilter['regex']= $s;
259     }
260     register_global("userfilter", $userfilter);
263     /* React on user interaction here */
264     if (($_SERVER["REQUEST_METHOD"] == "POST")||($_SERVER["REQUEST_METHOD"]== "GET")){
266       /* User wants to edit data? */
267       
268       /* Check for config to ensure that there is currently no user open */
269       if (($s_action=="edit") && (!isset($this->usertab->config))){
271         /* Get 'dn' from posted 'uid', must be unique */
273         $this->dn= $this->list[trim($s_entry)]['dn'];
275         /* Check locking, save current plugin in 'back_plugin', so
276            the dialog knows where to return. */
277         if (($user= get_lock($this->dn)) != ""){
278           return(gen_locked_message ($user, $this->dn));
279         }
281         /* Lock the current entry, so everyone will get the
282            above dialog */
283         add_lock ($this->dn, $this->ui->dn);
285         /* Set up the users ACL's for this 'dn' */
286         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
288         /* Register usertab to trigger edit dialog */
289         $this->usertab= new usertabs($this->config, 
290             $this->config->data['TABS']['USERTABS'], $this->dn);
291         //        $_POST[$s_tab]=$s_tab;
293         // This would be better
294         $this->usertab->current = $s_tab;
295         $this->usertab->set_acl($acl);
297         $_SESSION['objectinfo']= $this->dn;
298       }
299     }
301     /* Reset requested? */
302     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
303       if (isset($this->usertab)){
304         del_lock ($this->usertab->dn);
305         unset ($this->usertab);
306       }
307       $this->usertab= NULL;
308       $this->lognames= array();;
309       $this->sn= "";
310       $this->givenName= "";
311       $this->uid= "";
312       unset ($_SESSION['objectinfo']);
313     }
315     /* Password change requested */
316     if ($s_action=="change_pw"){
318       /* Get 'dn' from posted 'uid' */
319       $this->dn= $this->list[trim($s_entry)]['dn'];
321       /* Load permissions for selected 'dn' and check if
322          we're allowed to remove this 'dn' */
323       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
324       $acl= get_module_permission($acl, "user", $this->dn);
325       if (chkacl($acl, "password") == ""){
327         /* User is allowed to change passwords, save 'dn' and 'acl' for next
328            dialog. */
329         $this->acl= $acl;
330         $_SESSION['objectinfo']= $this->dn;
331         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
333       } else {
334         /* User is not allowed. Show message and cancel. */
335         print_red (_("You are not allowed to set this users password!"));
336       }
337     }
339     /* Perform password change */
340     if (isset($_POST['password_finish'])){
342       /* For security reasons, check if user is allowed to set password again */
343       if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
345         /* Check input and feed errors into 'message' */
346         $message= array();
348         /* Sanity checks... */
349         if ($_POST['new_password'] != $_POST['repeated_password']){
351           /* Matching passwords in new and repeated? */
352           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
353         } else {
355           /* Empty password is not permitted by default. */
356           if ($_POST['new_password'] == ""){
357             $message[]= _("The password you've entered as 'New password' is empty.");
358           }
359         }
361         /* Errors, or change password? */
362         if (count($message) != 0){
364           /* Show error message and continue editing */
365           show_errors ($message);
366           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
367         }
369         $config= $this->config;
370         $ldap_ui= $this->config->get_ldap_link();
371         if(isset($this->usertab->dn)){
372           $user = ($ldap_ui->fetch($ldap_ui->cat($this->usertab->dn)));
373         }else{
374           $user = ($ldap_ui->fetch($ldap_ui->cat($this->dn)));
375         }
376         if((is_array($user))&&(isset($user['uid']))){
377           $username= $user['uid'][0];
378         }
380         /* Set password, perform required steps */
381         if ($this->usertab){
382           if ($this->usertab->password_change_needed()){
383             $obj= $this->usertab->by_object['user'];
384             change_password ($this->usertab->dn, $_POST['new_password'],
385                 0, $obj->pw_storage);
386             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
387               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
388             }
390             gosa_log ("Password for '".$this->usertab->dn."' has been changed");
391             unset($this->usertab);
392             $this->usertab= NULL;
393           }
394         } else {
395           change_password ($this->dn, $_POST['new_password']);
396           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
397             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
398           }
400           gosa_log ("Password for '".$this->dn."' has been changed");
401         }
402       } else {
404         /* Missing permissions, show message */
405         print_red (_("You are not allowed to set this users password!"));
406       }
408       /* Clean session, delete lock */
409       del_lock ($this->dn);
410       $this->reload();
411       unset ($this->usertab);
412       $this->usertab= NULL;
413       $this->lognames= array();;
414       $this->sn= "";
415       $this->givenName= "";
416       $this->uid= "";
417       unset ($_SESSION['objectinfo']);
418     }
421     /* Remove user was requested */
422     if ($s_action=="del"){
424       /* Get 'dn' from posted 'uid' */
425       $this->dn= $this->list[trim($s_entry)]['dn'];
427       /* Load permissions for selected 'dn' and check if
428          we're allowed to remove this 'dn' */
429       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
430       $this->acl= get_module_permission($acl, "user", $this->dn);
431       if (chkacl($this->acl, "delete") == ""){
433         /* Check locking, save current plugin in 'back_plugin', so
434            the dialog knows where to return. */
435         if (($user= get_lock($this->dn)) != ""){
436           return(gen_locked_message ($user, $this->dn));
437         }
439         /* Lock the current entry, so nobody will edit it during deletion */
440         add_lock ($this->dn, $this->ui->dn);
441         $smarty->assign("info", sprintf(_("You're about to delete the user %s."), LDAP::fix($this->dn)));
442         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
443       } else {
445         /* Obviously the user isn't allowed to delete. Show message and
446            clean session. */
447         print_red (_("You are not allowed to delete this user!"));
448       }
449     }
453     /* Confirmation for deletion has been passed. User should be deleted. */
454     if (isset($_POST['delete_user_confirm'])){
456       /* Some nice guy may send this as POST, so we've to check
457          for the permissions again. */
458       if (chkacl($this->acl, "delete") == ""){
460         /* Delete request is permitted, perform LDAP action */
461         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
462             $this->dn);
463         $this->usertab->set_acl(array($this->acl));
464         $this->usertab->delete ();
465         gosa_log ("User object '".$this->dn."' has been removed");
466         unset ($this->usertab);
467         $this->usertab= NULL;
469         /* User list has changed, reload it. */
470         $this->reload ();
471       } else {
473         /* Normally this shouldn't be reached, send some extra
474            logs to notify the administrator */
475         print_red (_("You are not allowed to delete this user!"));
477         if(isset($this->ui->uid)){
478           gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
479         }
480       }
482       /* Remove lock file after successfull deletion */
483       del_lock ($this->dn);
484     }
487     /* Delete user canceled? */
488     if (isset($_POST['delete_cancel'])){
489       del_lock ($this->dn);
490     }
493     /* Finish user edit is triggered by the tabulator dialog, so
494        the user wants to save edited data. Check and save at this
495        point. */
496     if ((isset($_POST['edit_finish'])) && (isset($this->usertab->config))){
498       /* Check tabs, will feed message array */
499       $this->usertab->last= $this->usertab->current;
500       $this->usertab->save_object();
501       $message= $this->usertab->check();
503       /* Save, or display error message? */
504       if (count($message) == 0){
506         /* No errors. Go ahead and prepare to ask for a password
507            in case we're creating a new user. 'dn' will be 'new'
508            in this case. It is set to the correct value later. */
509         if ($this->dn == "new"){
510           $set_pass= 1;
511         } else {
512           $set_pass= 0;
513         }
515         /* Save user data to ldap */
516         if($this->usertab->save() == 1){
517           gosa_log ("User object '".$this->dn."' saving failed.");
518           return;
519         }
520         gosa_log ("User object '".$this->dn."' has been saved");
522         /* User has been saved successfully, remove lock from
523            LDAP. */
524         if ($this->dn != "new"){
525           del_lock ($this->dn);
526         }
528         /* In case of new users, ask for a password */
529         if (($set_pass || $this->usertab->password_change_needed()) &&
530             !$this->is_template){
532           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
533         }
535         /* There's no page reload so we have to read new users at
536            this point. */
537         $this->reload ();
538         unset ($this->usertab);
539         $this->usertab= NULL;
540         unset ($_SESSION['objectinfo']);
542       } else {
543         /* Ok. There seem to be errors regarding to the tab data,
544            show message and continue as usual. */
545         show_errors($message);
546       }
547     }
549     /* Generate template list */
550     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
552       $this->templates= array();
554       $ldap= $this->config->get_ldap_link();
556       /* Create list of tempaltes */
557       foreach ($this->config->departments as $key => $value){
558     
559         /* Get acls from different ou's */
560         $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
561         $acl= get_module_permission($acl, "user", get_people_ou().$value);
562   
563         /* If creation of a new user is allowed, append this template */
564         if (chkacl($acl, "create") == ""){
565           
566           /* Search all templates from the current dn */
567           $ldap->cd (get_people_ou().$value);
568           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
570           /* Append */
571           if ($ldap->count() != 0){
572             while ($attrs= $ldap->fetch()){
573               $this->templates[$ldap->getDN()]=
574                 $attrs['uid'][0]." - $key";
575             }
576             $this->templates['none']= _("none");
577           }
578         }
579       }
581       /* Sort templates */
582       natcasesort ($this->templates);
583       reset ($this->templates);
584     }
586     /* New user/template request */
587     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
588       /* By default we set 'dn' to 'new', all relevant plugins will
589          react on this. */
590       $this->dn= "new";
591       if (isset($this->config->current['IDGEN'])){
592         $this->got_uid= false;
593       } else {
594         $this->got_uid= true;
595       }
597       /* Create new usertab object */
598       $this->usertab= new usertabs($this->config,
599           $this->config->data['TABS']['USERTABS'], $this->dn);
600       $this->usertab->set_acl(array(':all'));
601       $this->usertab->by_object['user']->base= $userfilter['depselect'];
603       /* Take care about templates */
604       if ($s_action=="new_tpl"){
605         $this->is_template= TRUE;
606         $this->usertab->set_template_mode ();
607       } else {
608         $this->is_template= FALSE;
609       }
611       /* Use template if there are any of them */
612   
613       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
614         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
615           $smarty->assign("$attr", $this->$attr);
616         }
617         if ($s_action=="create_user_from_tpl"){
618           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
619         } else {
620           $smarty->assign("template", "none");
621         }
622         $smarty->assign("edit_uid", "");
623         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
624       }
625     }
627     /* Continue template editing */
628     if ((isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid']))){
629       $this->sn= $_POST['sn'];
630       $this->givenName= $_POST['givenName'];
632       /* Check for requred values */
633       $message= array();
634       if ($this->sn == "") {
635         $message[]= _("The required field 'Name' is not set.");
636       }
637       if ($this->givenName == "") {
638         $message[]= _("The required field 'Given name' is not set.");
639       }
641       /* Check if dn is used */
642       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
643       $ldap= $this->config->get_ldap_link();
644       $ldap->cd ($dn);
645       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
646       if ($ldap->count () != 0){
647         $message[]= _("A person with the choosen name is already used in this tree.");
648       }
650       /* Show error message / continue editing */
651       if (count($message) > 0){
652         show_errors ($message);
653       } else {
654         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
655         if (isset($this->config->current['IDGEN']) &&
656             $this->config->current['IDGEN'] != ""){
657           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
658           if (count($uids)){
659             $smarty->assign("edit_uid", "false");
660             $smarty->assign("uids", $uids);
661             $this->uid= current($uids);
662           }
663         } else {
664           $smarty->assign("edit_uid", "");
665           $this->uid= "";
666         }
667         $this->got_uid= true;
668       }
670       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
671         $smarty->assign("$attr", $this->$attr);
672       }
673       if (isset($_POST['template'])){
674         $smarty->assign("template", $_POST['template']);
675       }
676       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
677     }
679     /* No template. Ok. Lets fill data into the normal user dialog */
680     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
681       foreach(array("sn", "givenName", "uid") as $attr){
682         if (isset($_POST[$attr])){
683           $this->usertab->by_object['user']->$attr= $_POST[$attr];
684         }
685       }
686     }
688     /* Finish template preamble */
689     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
691       /* Might not be filled if IDGEN is unset */
692       $this->sn                 = $_POST['sn'];
693       $this->givenName          = $_POST['givenName'];
695       /* Move user supplied data to sub plugins */
696       $this->uid                = $_POST['uid'];
697       $this->usertab->uid       = $this->uid;
698       $this->usertab->sn        = $this->sn;
699       $this->usertab->givenName = $this->givenName;
700       $template_dn              = $_POST['template'];
701       $this->usertab->adapt_from_template($template_dn);
702       $template_base            = preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
703       $this->usertab->by_object['user']->base= $template_base;
705       /* Set up the users ACL's for this 'dn' */
706       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
707       $this->usertab->set_acl($acl);
708     }
709     
710     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
711       $this->usertab->by_object['user']->base= $userfilter['depselect'];
712     }
714     /* Show tab dialog if object is present */
715     if(isset($this->usertab->config)){
716       $display= $this->usertab->execute();
718       /* Don't show buttons if tab dialog requests this */
719       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
720         $display.= "<p style=\"text-align:right\">\n";
721         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
722         $display.= "&nbsp;\n";
723         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
724         $display.= "</p>";
725       }
726       return ($display);
727     }
729     $smarty= get_smarty();
731     /* Check sorting variable */
732     if (!isset($this->usertab) &&
733         !isset($_POST['new_user']) &&
734         !isset($_POST['new_template']) &&
735         !isset($_POST['delete_user']) &&
736         !isset($_POST['setpass_user']) &&
737         !isset($_POST['select_user'])){
738       $this->reload();
739     }
741     /* Check for exeeded sizelimit */
742     if (($message= check_sizelimit()) != ""){
743       return($message);
744     }
746     /* Prepare departments */
747     $options= "";
748     foreach ($this->config->idepartments as $key => $value){
749       if ($userfilter['depselect'] == $key){
750         $options.= "<option selected value='$key'>$value</option>";
751       } else {
752         $options.= "<option value='$key'>$value</option>";
753       }
754     }
757     /* Create paste icon
758      */
759     if($this->enableCopyPaste){
760       $Copy_Paste = " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
761       if($this->CopyPasteHandler->isCurrentObjectPastAble()){
762         if($this->CopyPasteHandler->isCurrentCutted()){
763           $img = "images/cutpaste.png";
764         }else{
765           $img = "images/copypaste.png";
766         }
767         $Copy_Paste .= "<input type='image' name='editPaste' class='center'
768           src='".$img."' alt='"._("Paste")."' title='".$this->CopyPasteHandler->GetCurrentDn()."'>&nbsp;";
769       }else{
770         $Copy_Paste .= "<img class='center' src='images/cant_editpaste.png' alt='"._("Can't paste")."'>&nbsp;";
771       }
772     }else{
773       $Copy_Paste ="";
774     }
777     /* NEW LIST MANAGMENT */
778     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
779       " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
780       " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
781       " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
782       " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
783       " <img   class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
784       " <input class='center' type='image' align='middle' src='images/list_new_user.png' title='"._("Create new user")."' alt='"._("New user")."' name='user_new'>&nbsp;".
785       " <input class='center' type='image' align='middle' src='images/list_new.png' title='"._("Create new template")."' alt='"._("New template")."' name='user_tplnew'>&nbsp;".
786       $Copy_Paste.
787       " <img   class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
788       _("Base")."&nbsp;<select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
789       " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
790       "</div>";
794     
795     if($this->enableCopyPaste){
796       $action = "<input class='center' type='image'
797         src='images/editcut.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'>&nbsp;";
798       $action.= "<input class='center' type='image'
799         src='images/editcopy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'>&nbsp;";
800       $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'     
801         name='user_edit_%KEY%' title='"._("Edit user")."'>";
802       $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."' 
803         name='user_chgpw_%KEY%' title='"._("Change password")."'>";
804       $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   
805         name='user_del_%KEY%' title='"._("Delete user")."'>";
806     }else{
807       $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'     
808         name='user_edit_%KEY%' title='"._("Edit user")."'>";
809       $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."' 
810         name='user_chgpw_%KEY%' title='"._("Change password")."'>";
811       $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   
812         name='user_del_%KEY%' title='"._("Delete user")."'>";
813     }
817     $divlist = new divlist("userstab");
818     $divlist->SetHeader(array(
819           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
820           array("string" => _("Username")." / "._("Department"), "attach" => "style=''"),
821           array("string" => _("Properties"), "attach" => "style='width:152px;'"),
822           array("string" => _("Actions"), "attach" => "style='width:90px;border-right:0px;text-align:right;'")));
825     $divlist->SetSummary(_("This table displays all users, in the selected tree."));
826     $divlist->SetEntriesPerPage(0);
828     // Defining Links 
829     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
831     /* Insert departments in divlist*/
832     foreach($this->departments as $key=> $val){
834       if(!isset($this->config->departments[trim($key)])){
835         $this->config->departments[trim($key)]="";
836       }
838       $non_empty="";
839       $keys= str_replace("/","\/",$key);
840       foreach($this->config->departments as $keyd=>$vald ){
841         if(preg_match("/".$keys."\/.*/",$keyd)){
842           $non_empty="full";
843         }
844       }
846       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
847       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
848       $field3 = array("string" => "&nbsp;", "attach" => "style='width:152px;'");
849       $field4 = array("string" => "&nbsp;", "attach" => "style='width:90px;border-right:0px;text-align:right;'");
852       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
853     }
855     /* Pictures for Extensions */
856     $usrimg   ="<input class='center' type='image' src='images/penguin.png' alt='"._("GOsa")."'   
857                     name='user_edit_%KEY%-user' title='"._("Edit generic properties")."'>";
858     $posiximg = "<input class='center' type='image' src='images/select_user.png' alt='"._("Posix")."'  
859                     name='user_edit_%KEY%-posixAccount' title='"._("Edit UNIX properties")."'>";
860     $eviroimg = "<input class='center' type='image' src='images/smallenv.png' alt='"._("Environment")."'  
861                     name='user_edit_%KEY%-environment' title='"._("Edit environment properties")."'>";
862     $mailimg  = "<input class='center' type='image' src='images/mailto.png' alt='"._("Mail")."'     
863                     name='user_edit_%KEY%-mailAccount' title='"._("Edit mail properties")."'>";
864     $fonimg   = "<input class='center' type='image' src='images/%image%' alt='"._("Phone")."'     
865                     name='user_edit_%KEY%-phoneAccount' title='"._("Edit phone properties")."%title%'>";
866     $faximg   = "<input class='center' type='image' src='images/fax_small.png' alt='"._("Fax")."'     
867                     name='user_edit_%KEY%-gofaxAccount' title='"._("Edit fax properies")."'>";
868     $sambaimg = "<input class='center' type='image' src='images/select_winstation.png' alt='"._("Samba")."'     
869                     name='user_edit_%KEY%-sambaAccount' title='"._("Edit samba properties")."'>"; 
870     $tplcreateuserimg  = "<input type='image' class='center' src='images/list_new.png' alt='"._("Create user from template")."'
871                     name='userfrom_tpl_%KEY%' title='"._("Create user with this template")."'>";   
872     
874     $empty    = "<img class='center' src='images/empty.png' style='width:16px;height:16px;' alt=''>"; 
876     // User and Template  Images  
877     $userimg  = "<img class='center' src='images/select_user.png' alt='User' title='%s'>";   
878     $tplimg   = "<img class='center' src='images/select_template.png' alt='Template' title='%s'>";   
879     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
881     // Defines Arrays to save User and Templates
882     $tpls = array();
883     $users= array();
885     /* Get Configuration for goFon DB (if it is set), to connect to the asterisk tables.
886      * Read Sip Table for specified account (for each user with phoneAccount).
887      * Check the attributes ip port and regseconds.
888      * If regseconds is set and >0 , the phone is logged in.
889      * Else the phone is currently not logged.
890      * If we can't read any Data from the DB or there is no goFon DB specified
891      * show old style without status icons.
892      */
893     $r_db   =false;
894     $r_con  =false;
895     if (isset($_SESSION['config']->data['SERVERS']['FON'])){
896       $a_SETUP= $_SESSION['config']->data['SERVERS']['FON'];
897       // Connect to DB server
898   
899       /* 
900         If you want to disable the status pictures, just comment out the next line
901       */
902       $r_con = false;
903       $r_db  = false;
904       if(is_callable("mysql_pconnect")){
905         $r_con= @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
906         // Check if we are  connected correctly
907         if($r_con){
908           $r_db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
909         }
910       }
911     }
913     // Test Every Entry and generate divlist Array  
914     foreach($this->list as $key => $val){
915       $connected  = ""; // This string represents timestamp or offline status 
916       $ip_port    = ""; // String that will represent ip : port of the connected phone
917       if((in_array("goFonAccount"    ,$val['objectClass']))){
918   
919         /* Set defaults */
920         $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
921         $fonac = preg_replace("/%KEY%/", "$key", $fonac);
922         $fonac = preg_replace("/%title%/", "", $fonac);
924         /* Database connection is ok ?*/
925         if(($r_db)&&(is_callable("mysql_query"))){
926           $res= @mysql_query("SELECT regseconds,name,port,ipaddr FROM ".$a_SETUP['SIP_TABLE']." WHERE (name='".$val['uid'][0]."')");
927           $mysql_entry = @mysql_fetch_row($res);
928           if(is_array($mysql_entry)){
929             if((isset($mysql_entry[0]))&&($mysql_entry[0]>1)){
930               $connected = " | "._("Online")." : ".gmdate("d.m.Y H:i:s",($mysql_entry[0]+(60*60)));
931               $fonac = preg_replace("/%image%/", "select_phone_connected.png", $fonimg);
932               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
933               $fonac = preg_replace("/%title%/", $connected, $fonac);
934               $ip_port= " - ".$mysql_entry[3].":".$mysql_entry[2];
935             }
936             if((isset($mysql_entry[0]))&&($mysql_entry[0]==0)){
937               $connected = " | "._("Offline");
938               $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
939               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
940               $fonac = preg_replace("/%title%/", $connected, $fonac);
941             }
942           }
943         }
944       }else{
945         $fonac=$empty;
946       }
948     
950       // Specify Pics for Extensions  
951       if(in_array("posixAccount"    ,$val['objectClass'])){ 
952         $posix = preg_replace("/%KEY%/", "$key", $posiximg);
953       }else{ 
954         $posix=$empty;
955       }
956       
957       if(in_array("gotoEnvironment"    ,$val['objectClass'])){ 
958         $enviro = preg_replace("/%KEY%/", "$key", $eviroimg);
959       }else{ 
960         $enviro =$empty;
961       }
962    
963       if(in_array("gosaMailAccount" ,$val['objectClass'])){ 
964         $maila = preg_replace("/%KEY%/", "$key", $mailimg);   
965       }else{ 
966         $maila=$empty;
967       } 
968   
969       if(in_array("goFaxAccount"    ,$val['objectClass'])){ 
970         $faxac = preg_replace("/%KEY%/", "$key", $faximg);    
971       }else{ 
972         $faxac=$empty;
973       } 
975       if(in_array("sambaSamAccount" ,$val['objectClass'])){ 
976         $samba = preg_replace("/%KEY%/", "$key", $sambaimg);  
977       }else{ 
978         $samba=$empty;
979       } 
981       if(in_array("gosaUserTemplate",$val['objectClass'])){ 
982         $tpl                          = preg_replace("/%KEY%/", "$key", $tplimg);   
983         $s_img_create_from_template   = preg_replace("/%KEY%/", "$key", $tplcreateuserimg);
984       }else{ 
985         $s_img_create_from_template   = "";
986         $tpl=$userimg;
987       }
989       $usrimg2 = preg_replace("/%KEY%/", "$key", $usrimg);
991       // Generate caption for rows
992       if (isset($val["sn"]) && isset($val["givenName"])){
993         $display= $val["sn"][0].", ".$val["givenName"][0]." [".$val["uid"][0]."]";
994       } else {
995         $display= "[".$val["uid"][0]."]";
996       }
998       /* Create each field */
999       $field1 = array("string" => sprintf($tpl,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
1000       $field2 = array("string" => sprintf($editlink,$key,$display).$ip_port, "attach" => "style='' title='".$val['dn']."'");
1001       $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;'");
1002       $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='width:90px;border-right:0px;text-align:right;'");
1004       $add = array($field1,$field2,$field3,$field4);
1006       // Template or User
1007       if(in_array("gosaUserTemplate",$val['objectClass'])){
1008         $tpls[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
1009       }else{
1010         $users[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
1011       }
1012     }
1014     // Sort Entries 
1015     ksort($users);
1016     ksort($tpls);
1018     // Append Templates
1019     foreach($tpls as $key => $val){
1020       $divlist->AddEntry( $val, array(""));
1021     }
1022     // Append Users 
1023     foreach($users as $key => $val){
1024       $divlist->AddEntry( $val, array(""));
1025     }
1026     /* Show main page */
1027     //$smarty->assign("userlist",   nl2br(htmlentities($divlist->DrawList())));
1028     $smarty->assign("userlist",   $divlist->DrawList());
1029     $smarty->assign("userlisthead", $listhead);
1031     /* END NEW LIST MANAGMENT
1032      */  
1033     $smarty->assign("search_image", get_template_path('images/search.png'));
1034     $smarty->assign("tree_image", get_template_path('images/tree.png'));
1035     $smarty->assign("infoimage", get_template_path('images/info.png'));
1036     $smarty->assign("launchimage", get_template_path('images/launch.png'));
1037     $smarty->assign("deplist", $this->config->idepartments);
1038     foreach( array("depselect",  "functionalusers", "unixusers",
1039           "mailusers", "sambausers", "proxyusers", 
1040           "faxusers", "templates", "subsearch") as $type){
1042       $smarty->assign("$type", $userfilter[$type]);
1043     }
1045     $smarty->assign("regex", $userfilter["regex"]);
1046     /* Extend if we are not using javascript */
1047     $smarty->assign("apply", apply_filter());
1048     $smarty->assign("alphabet", generate_alphabet());
1049     $smarty->assign("hint", print_sizelimit_warning());
1051     if((is_callable("mysql_close"))&&($r_con)){
1052       @mysql_close($r_con);
1053     }
1054     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
1055   }
1058   function reload()
1059   {
1060     /* Get config */
1061     $userfilter= get_global('userfilter');
1063     /* Set base for all searches */
1064     $base= $userfilter['depselect'];
1066     /* Regex filter? */
1067     if ($userfilter['regex'] != ""){
1068       $regex= $userfilter['regex'];
1069     } else {
1070       $regex= "*";
1071     }
1073     /* Set filter depending on selection */
1074     if ($this->config->current['SAMBAVERSION'] == 3){
1075       $samba= "sambaSamAccount";
1076     } else {
1077       $samba= "sambaAccount";
1078     }
1081     /* Sometimes not all users were displayed in the users list.
1082        The problem was, that we search for an objectClass
1083         which isn't used in every ldap-schema configuration.
1084        Here we remove this class, if there is no result returned from 
1085         our test search.
1086     */
1087     $ldap = $this->config->get_ldap_link();
1088     $ldap->cd($this->config->current['BASE']);
1090     $ldap->search("(objectClass=goFaxAccount)",array("cn","uid"));
1091     if($ldap->fetch()){
1092       $useFaxInFilter = true;
1093       $FaxFilter = "(objectClass=goFaxAccount)";
1094     } else {
1095       $useFaxInFilter = false;
1096       $FaxFilter = "";
1097     }
1098    
1099  
1100     $filter="";
1101     if ($userfilter['functionalusers'] == "checked"){
1102       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=$samba)".$FaxFilter."(objectClass=gosaProxyAccount))))";
1103     }
1104     if ($userfilter['unixusers'] == "checked"){
1105       $filter.= "(objectClass=posixAccount)";
1106     }
1107     if ($userfilter['mailusers'] == "checked"){
1108       $filter.= "(objectClass=gosaMailAccount)";
1109     }
1110     if ($userfilter['sambausers'] == "checked"){
1111       $filter.= "(objectClass=$samba)";
1112     }
1113     if ($userfilter['proxyusers'] == "checked"){
1114       $filter.= "(objectClass=gosaProxyAccount)";
1115     }
1116     if (($userfilter['faxusers'] == "checked") && ($useFaxInFilter)){
1117       $filter.= "(objectClass=goFaxAccount)";
1118     }
1119     if ($userfilter['templates'] == "checked"){
1120       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1121     } else {
1122       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1123     }
1124     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
1126     /* Generate userlist */
1127     $ldap= $this->config->get_ldap_link(TRUE);
1128     if ($userfilter['subsearch'] == "checked"){
1129       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
1130     } else {
1131       $base= get_people_ou().$base;
1132       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
1133     }
1136     /* NEW LIST MANAGMENT
1137      * We also need to search for the departments
1138      * So we are able to navigate like in konquerer 
1139      */
1141     /* Create base to search in */
1143     $peopleOU = get_people_ou();
1144     if(empty($peopleOU))  {
1145       $base2 = $base;
1146     }else{
1147       $base2 = preg_replace("/".$peopleOU."/i","",$base);
1148     }
1150     /* Get all departments within this subtree */
1151     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
1152         TRUE, $base2, array("ou", "description"), TRUE);
1154     $this->departments= array();
1156     /* Create array with results */
1157     $tmp = array();
1158     foreach ($res3 as $value){
1159       $tmp[strtolower($value['dn']).$value['dn']]=$value;
1160     }
1162     /* Sort array */
1163     ksort($tmp);
1165     /* Create result array */
1166     foreach($tmp as $value){
1167       if(isset($value['description'][0])){
1168         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
1169       }else{
1170         $this->departments[$value['dn']]=convert_department_dn2($value['dn']);
1171       }
1172     }
1173     /* END NEW LIST MANAGMENT 
1174      */
1177     $this->userlist= array();
1179     foreach ($this->list as $value){
1180       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
1181         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
1182           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
1183           continue;
1184         }
1185         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
1186           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
1187             $value["givenName"][0].
1188             " [".$value["uid"][0]."]";
1189         } else {
1190           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
1191         }
1192       }
1193     }
1195     natcasesort ($this->userlist);
1196     reset ($this->userlist);
1197   }
1199   function remove_from_parent()
1200   {
1201   }
1204   /* Check values */
1205   function check()
1206   {
1207   }
1210   /* Save to LDAP */
1211   function save()
1212   {
1213   }
1215   function adapt_from_template($dn)
1216   {
1217   }
1219   function password_change_needed()
1220   {
1221   }
1223   function show_header($button_text, $text, $disabled= FALSE)
1224   {
1225   }
1227   function remove_lock()
1228   {
1229     if (isset($this->usertab->dn)){
1230       del_lock ($this->usertab->dn);
1231     }
1232   }
1236 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1237 ?>