Code

Create user from template fixed, some Session vars wasn't initialised
[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   function userManagement ($config, $ui)
38   {
39     /* Save configuration for internal use */
40     $this->config= $config;
41     $this->ui= $ui;
43     /* Get global filter config */
44     if (!isset($_SESSION["userfilter"])){
45       $base= get_base_from_people($ui->dn);
46       $userfilter= array( "mailusers"       => "checked",
47           "unixusers"       => "checked",
48           "templates"       => "",
49           "subsearch"       => "",
50           "proxyusers"      => "checked",
51           "sambausers"      => "checked",
52           "faxusers"        => "checked",
53           "functionalusers" => "checked",
54           "depselect"       => $base,
55           "regex"           => "*");
56       $_SESSION["userfilter"] = $userfilter;
57     }
58   }
60   function execute()
61   {
62     $smarty       = get_smarty();             // Smarty instance
63     $userfilter   = get_global("userfilter"); // contains Filter Settings
64     $s_action     = "";                       // Contains the action to proceed
65     $s_entry      = "";                       // The value for s_action
66     $base_back    = "";                       // The Link for Backbutton
68     /* Start for New List Managment */
69     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
70       $s_action="open";
71       $s_entry = base64_decode($_GET['dep_id']);
72       $userfilter['depselect']= "".$this->config->departments[trim($s_entry)];
73     }
75     /* Test Posts */  
76     foreach($_POST as $key => $val){
77       // Post for delete
78       if(preg_match("/user_del.*/",$key)){
79         $s_action = "del";
80         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
81         // Post for edit
82       }elseif(preg_match("/user_edit_.*/",$key)){
83         $s_action="edit";
84         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
85         // Post for new
86       }elseif(preg_match("/dep_back.*/i",$key)){
87         $s_action="back";
88       }elseif(preg_match("/user_new.*/",$key)){
89         $s_action="new";
90       }elseif(preg_match("/dep_home.*/i",$key)){
91         $s_action="home";
92       }elseif(preg_match("/user_tplnew.*/i",$key)){
93         $s_action="new_tpl";
94       }elseif(preg_match("/user_chgpw.*/i",$key)){
95         $s_action="change_pw";
96         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
97       }elseif(preg_match("/dep_root.*/i",$key)){
98         $s_action="root";
99       }elseif(preg_match("/userfrom_tpl.*/i",$key)){
100         $s_action="create_user_from_tpl";
101         $s_entry  = preg_replace("/userfrom_tpl_/i","",$key);
102       }
103     }
105     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
106       $s_action = "edit";
107       $s_entry  = $_GET['id'];
108     }
110     $s_entry  = preg_replace("/_.$/","",$s_entry);
112     if(preg_match("/.*-.*/",$s_entry)){
113       $s_tab   = preg_replace("/^.*-/i","",$s_entry);
114       $s_entry = preg_replace("/-.*$/i","",$s_entry);
115     }else{
116       $s_tab   = "user";
117     };
119     /* Department changed? */
120     if(isset($_POST['depselect']) && $_POST['depselect']){
121       $userfilter['depselect']= $_POST['depselect'];
122     }
124     /* Homebutton is posted */
125     if($s_action=="home"){
126       $userfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
127       $userfilter['depselect']=(preg_replace("/^[^,]+,/","",$userfilter['depselect']));
128     }
130     if($s_action=="root"){
131       $userfilter['depselect']=($this->config->current['BASE']);
132     }
135     /* If Backbutton is Posted */
136     if($s_action=="back"){
137       $base_back          = preg_replace("/^[^,]+,/","",$userfilter['depselect']);
138       $base_back          = convert_department_dn($base_back);
140       if(isset($this->config->departments[trim($base_back)])){
141         $userfilter['depselect']= $this->config->departments[trim($base_back)];
142       }else{
143         $userfilter['depselect']= $this->config->departments["/"];
144       }
145     }
147     /* End for new List Managment */
149     /* Save filter data if we are in the headpage */
150     if (!isset($this->usertab)){
151       if (isset($_POST['regexit'])){
152         $userfilter["regex"]= $_POST['regexit'];
153       }
154     }
155     if(isset($_POST['regexit'])){
156       foreach( array("functionalusers", "unixusers", "mailusers","sambausers", "proxyusers", "faxusers", "templates", "subsearch") as $type){
157         if (isset($_POST[$type])) {
158           $userfilter[$type]= "checked";
159         } else {
160           $userfilter[$type]= "";
161         }
162       }
163     }
164     if (isset($_GET['search'])){
165       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
166       if ($s == "**"){
167         $s= "*";
168       }
169       $userfilter['regex']= $s;
170     }
171     register_global("userfilter", $userfilter);
174     /* React on user interaction here */
175     if (($_SERVER["REQUEST_METHOD"] == "POST")||($_SERVER["REQUEST_METHOD"]== "GET")){
177       /* User wants to edit data? */
178       if ($s_action=="edit"){
180         /* Get 'dn' from posted 'uid', must be unique */
182         $this->dn= $this->list[trim($s_entry)]['dn'];
184         /* Check locking, save current plugin in 'back_plugin', so
185            the dialog knows where to return. */
186         if (($user= get_lock($this->dn)) != ""){
187           return(gen_locked_message ($user, $this->dn));
188         }
190         /* Lock the current entry, so everyone will get the
191            above dialog */
192         add_lock ($this->dn, $this->ui->dn);
194         /* Set up the users ACL's for this 'dn' */
195         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
197         /* Register usertab to trigger edit dialog */
198         $this->usertab= new usertabs($this->config, 
199             $this->config->data['TABS']['USERTABS'], $this->dn);
200         //        $_POST[$s_tab]=$s_tab;
202         // This would be better
203         $this->usertab->current = $s_tab;
204         $this->usertab->set_acl($acl);
206         $_SESSION['objectinfo']= $this->dn;
207       }
208     }
210     /* Reset requested? */
211     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
212       if (isset($this->usertab)){
213         del_lock ($this->usertab->dn);
214         unset ($this->usertab);
215       }
216       $this->usertab= NULL;
217       $this->lognames= array();;
218       $this->sn= "";
219       $this->givenName= "";
220       $this->uid= "";
221       unset ($_SESSION['objectinfo']);
222     }
224     /* Password change requested */
225     if ($s_action=="change_pw"){
227       /* Get 'dn' from posted 'uid' */
228       $this->dn= $this->list[trim($s_entry)]['dn'];
230       /* Load permissions for selected 'dn' and check if
231          we're allowed to remove this 'dn' */
232       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
233       $acl= get_module_permission($acl, "user", $this->dn);
234       if (chkacl($acl, "password") == ""){
236         /* User is allowed to change passwords, save 'dn' and 'acl' for next
237            dialog. */
238         $this->acl= $acl;
239         $_SESSION['objectinfo']= $this->dn;
240         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
242       } else {
243         /* User is not allowed. Show message and cancel. */
244         print_red (_("You are not allowed to set this users password!"));
245       }
246     }
248     /* Perform password change */
249     if (isset($_POST['password_finish'])){
251       /* For security reasons, check if user is allowed to set password again */
252       if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
254         /* Check input and feed errors into 'message' */
255         $message= array();
257         /* Sanity checks... */
258         if ($_POST['new_password'] != $_POST['repeated_password']){
260           /* Matching passwords in new and repeated? */
261           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
262         } else {
264           /* Empty password is not permitted by default. */
265           if ($_POST['new_password'] == ""){
266             $message[]= _("The password you've entered as 'New password' is empty.");
267           }
268         }
270         /* Errors, or change password? */
271         if (count($message) != 0){
273           /* Show error message and continue editing */
274           show_errors ($message);
275           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
276         }
278         $config= $this->config;
279         $ldap_ui= $this->config->get_ldap_link();
280         if(isset($this->usertab->dn)){
281           $user = ($ldap_ui->fetch($ldap_ui->cat($this->usertab->dn)));
282         }else{
283           $user = ($ldap_ui->fetch($ldap_ui->cat($this->dn)));
284         }
285         if((is_array($user))&&(isset($user['uid']))){
286           $username= $user['uid'][0];
287         }
289         /* Set password, perform required steps */
290         if ($this->usertab){
291           if ($this->usertab->password_change_needed()){
292             $obj= $this->usertab->by_object['user'];
293             change_password ($this->usertab->dn, $_POST['new_password'],
294                 0, $obj->pw_storage);
295             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
296               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
297             }
299             gosa_log ("Password for '".$this->usertab->dn."' has been changed");
300             unset($this->usertab);
301             $this->usertab= NULL;
302           }
303         } else {
304           change_password ($this->dn, $_POST['new_password']);
305           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
306             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
307           }
309           gosa_log ("Password for '".$this->dn."' has been changed");
310         }
311       } else {
313         /* Missing permissions, show message */
314         print_red (_("You are not allowed to set this users password!"));
315       }
317       /* Clean session, delete lock */
318       del_lock ($this->dn);
319       $this->reload();
320       unset ($this->usertab);
321       $this->usertab= NULL;
322       $this->lognames= array();;
323       $this->sn= "";
324       $this->givenName= "";
325       $this->uid= "";
326       unset ($_SESSION['objectinfo']);
327     }
330     /* Remove user was requested */
331     if ($s_action=="del"){
333       /* Get 'dn' from posted 'uid' */
334       $this->dn= $this->list[trim($s_entry)]['dn'];
336       /* Load permissions for selected 'dn' and check if
337          we're allowed to remove this 'dn' */
338       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
339       $this->acl= get_module_permission($acl, "user", $this->dn);
340       if (chkacl($this->acl, "delete") == ""){
342         /* Check locking, save current plugin in 'back_plugin', so
343            the dialog knows where to return. */
344         if (($user= get_lock($this->dn)) != ""){
345           return(gen_locked_message ($user, $this->dn));
346         }
348         /* Lock the current entry, so nobody will edit it during deletion */
349         add_lock ($this->dn, $this->ui->dn);
350         $smarty->assign("info", sprintf(_("You're about to delete the user %s."), $this->dn));
351         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
352       } else {
354         /* Obviously the user isn't allowed to delete. Show message and
355            clean session. */
356         print_red (_("You are not allowed to delete this user!"));
357       }
358     }
362     /* Confirmation for deletion has been passed. User should be deleted. */
363     if (isset($_POST['delete_user_confirm'])){
365       /* Some nice guy may send this as POST, so we've to check
366          for the permissions again. */
367       if (chkacl($this->acl, "delete") == ""){
369         /* Delete request is permitted, perform LDAP action */
370         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
371             $this->dn);
372         $this->usertab->set_acl(array($this->acl));
373         $this->usertab->delete ();
374         gosa_log ("User object '".$this->dn."' has been removed");
375         unset ($this->usertab);
376         $this->usertab= NULL;
378         /* User list has changed, reload it. */
379         $this->reload ();
380       } else {
382         /* Normally this shouldn't be reached, send some extra
383            logs to notify the administrator */
384         print_red (_("You are not allowed to delete this user!"));
386         if(isset($this->ui->uid)){
387           gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
388         }
389       }
391       /* Remove lock file after successfull deletion */
392       del_lock ($this->dn);
393     }
396     /* Delete user canceled? */
397     if (isset($_POST['delete_cancel'])){
398       del_lock ($this->dn);
399     }
402     /* Finish user edit is triggered by the tabulator dialog, so
403        the user wants to save edited data. Check and save at this
404        point. */
405     if (isset($_POST['edit_finish'])){
407       /* Check tabs, will feed message array */
408       $this->usertab->last= $this->usertab->current;
409       $this->usertab->save_object();
410       $message= $this->usertab->check();
412       /* Save, or display error message? */
413       if (count($message) == 0){
415         /* No errors. Go ahead and prepare to ask for a password
416            in case we're creating a new user. 'dn' will be 'new'
417            in this case. It is set to the correct value later. */
418         if ($this->dn == "new"){
419           $set_pass= 1;
420         } else {
421           $set_pass= 0;
422         }
424         /* Save user data to ldap */
425         if($this->usertab->save() == 1){
426           gosa_log ("User object '".$this->dn."' saving failed.");
427           return;
428         }
429         gosa_log ("User object '".$this->dn."' has been saved");
431         /* User has been saved successfully, remove lock from
432            LDAP. */
433         if ($this->dn != "new"){
434           del_lock ($this->dn);
435         }
437         /* In case of new users, ask for a password */
438         if (($set_pass || $this->usertab->password_change_needed()) &&
439             !$this->is_template){
441           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
442         }
444         /* There's no page reload so we have to read new users at
445            this point. */
446         $this->reload ();
447         unset ($this->usertab);
448         $this->usertab= NULL;
449         unset ($_SESSION['objectinfo']);
451       } else {
452         /* Ok. There seem to be errors regarding to the tab data,
453            show message and continue as usual. */
454         show_errors($message);
455       }
456     }
458     /* Generate template list */
459     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
460       $this->templates= array();
461       $ldap= $this->config->get_ldap_link();
462       foreach ($this->config->departments as $key => $value){
463         $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
464         $acl= get_module_permission($acl, "user", get_people_ou().$value);
465         if (chkacl($acl, "create") == ""){
466           $ldap->cd (get_people_ou().$value);
467           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
469           if ($ldap->count() != 0){
470             while ($attrs= $ldap->fetch()){
471               $this->templates[$ldap->getDN()]=
472                 $attrs['uid'][0]." - $key";
473             }
474             $this->templates['none']= _("none");
475           }
476         }
477       }
478       natcasesort ($this->templates);
479       reset ($this->templates);
480     }
482     /* New user/template request */
483     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
484       /* By default we set 'dn' to 'new', all relevant plugins will
485          react on this. */
486       $this->dn= "new";
487       if (isset($this->config->current['IDGEN'])){
488         $this->got_uid= false;
489       } else {
490         $this->got_uid= true;
491       }
493       /* Create new usertab object */
494       $this->usertab= new usertabs($this->config,
495           $this->config->data['TABS']['USERTABS'], $this->dn);
496       $this->usertab->set_acl(array(':all'));
498       /* Take care about templates */
499       if ($s_action=="new_tpl"){
500         $this->is_template= TRUE;
501         $this->usertab->set_template_mode ();
502       } else {
503         $this->is_template= FALSE;
504       }
506       /* Use template if there are any of them */
507       if ((count($this->templates) && !isset($_POST['new_template']))||($s_action=="create_user_from_tpl")){
508         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
509           $smarty->assign("$attr", $this->$attr);
510         }
511         if ($s_action=="create_user_from_tpl"){
512           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
513         } else {
514           $smarty->assign("template", "");
515         }
516         $smarty->assign("edit_uid", "");
517         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
518       }
519     }
521     /* Continue template editing */
522     if ((isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid']))){
523       $this->sn= $_POST['sn'];
524       $this->givenName= $_POST['givenName'];
526       /* Check for requred values */
527       $message= array();
528       if ($this->sn == "") {
529         $message[]= _("The required field 'Name' is not set.");
530       }
531       if ($this->givenName == "") {
532         $message[]= _("The required field 'Given name' is not set.");
533       }
535       /* Check if dn is used */
536       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
537       $ldap= $this->config->get_ldap_link();
538       $ldap->cd ($dn);
539       $ldap->search ("(&(sn=".$this->sn.")(givenName=".$this->givenName."))", array("givenName"));
540       if ($ldap->count () != 0){
541         $message[]= _("A person with the choosen name is already used in this tree.");
542       }
544       /* Show error message / continue editing */
545       if (count($message) > 0){
546         show_errors ($message);
547       } else {
548         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
549         if (isset($this->config->current['IDGEN']) &&
550             $this->config->current['IDGEN'] != ""){
551           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
552           if (count($uids)){
553             $smarty->assign("edit_uid", "false");
554             $smarty->assign("uids", $uids);
555             $this->uid= current($uids);
556           }
557         } else {
558           $smarty->assign("edit_uid", "");
559           $this->uid= "";
560         }
561         $this->got_uid= true;
562       }
564       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
565         $smarty->assign("$attr", $this->$attr);
566       }
567       if (isset($_POST['template'])){
568         $smarty->assign("template", $_POST['template']);
569       }
570       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
571     }
573     /* No template. Ok. Lets fill data into the normal user dialog */
574     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
575       foreach(array("sn", "givenName", "uid") as $attr){
576         if (isset($_POST[$attr])){
577           $this->usertab->by_object['user']->$attr= $_POST[$attr];
578         }
579       }
580     }
582     /* Finish template preamble */
583     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
585       /* Might not be filled if IDGEN is unset */
586       $this->sn                 = $_POST['sn'];
587       $this->givenName          = $_POST['givenName'];
589       /* Move user supplied data to sub plugins */
590       $this->uid                = $_POST['uid'];
591       $this->usertab->uid       = $this->uid;
592       $this->usertab->sn        = $this->sn;
593       $this->usertab->givenName = $this->givenName;
594       $template_dn              = $_POST['template'];
595       $this->usertab->adapt_from_template($template_dn);
596       $template_base            = $userfilter['depselect'];//preg_replace("/^[^,]+,".get_people_ou()."/", '', $template_dn);
597       $this->usertab->by_object['user']->base= $template_base;
599       /* Set up the users ACL's for this 'dn' */
600       $acl= get_permissions ($template_base, $this->ui->subtreeACL);
601       $this->usertab->set_acl($acl);
602     }
604     /* Show tab dialog if object is present */
605     if ($this->usertab){
606       $display= $this->usertab->execute();
608       /* Don't show buttons if tab dialog requests this */
609       if (!$this->usertab->by_object[$this->usertab->current]->dialog){
610         $display.= "<p style=\"text-align:right\">\n";
611         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
612         $display.= "&nbsp;\n";
613         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
614         $display.= "</p>";
615       }
616       return ($display);
617     }
619     $smarty= get_smarty();
621     /* Check sorting variable */
622     if (!isset($this->usertab) &&
623         !isset($_POST['new_user']) &&
624         !isset($_POST['new_template']) &&
625         !isset($_POST['delete_user']) &&
626         !isset($_POST['setpass_user']) &&
627         !isset($_POST['select_user'])){
628       $this->reload();
629     }
631     /* Check for exeeded sizelimit */
632     if (($message= check_sizelimit()) != ""){
633       return($message);
634     }
636     /* Prepare departments */
637     $options= "";
638     foreach ($this->config->idepartments as $key => $value){
639       if ($userfilter['depselect'] == $key){
640         $options.= "<option selected value='$key'>$value</option>";
641       } else {
642         $options.= "<option value='$key'>$value</option>";
643       }
644     }
646     /* NEW LIST MANAGMENT */
647     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
648       " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
649       " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
650       " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
651       " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
652       " <input type='image' align='middle' src='images/list_new_user.png' title='"._("Create new user")."' alt='"._("New user")."' name='user_new'>&nbsp;".
653       " <input type='image' align='middle' src='images/list_new.png' title='"._("Create new template")."' alt='"._("New template")."' name='user_tplnew'>&nbsp;".
654       " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
655       _("Current base")."&nbsp;<select name='depselect' onChange='mainform.submit()'>$options</select>".
656       " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
657       "</div>";
659     $action= "<input type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
660     $action.= "<input type='image' src='images/list_password.png' alt='"._("password")."' name='user_chgpw_%KEY%' title='"._("Change password")."'>";
661     $action.= "<input type='image' src='images/editdelete.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
665     $divlist = new divlist("userstab");
666     $divlist->SetHeader(array(
667           array("string" => "&nbsp;"),
668           array("string" => _("Username")." / "._("Department")),
669           array("string" => _("Properties")),
670           array("string" => _("Actions"), "attach" => "style='border:none'" )));
673     $divlist->SetSummary(_("This table displays all users, in the selected tree."));
674     $divlist->SetEntriesPerPage(20);
676     // Defining Links 
677     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
679     /* Insert departments in divlist*/
680     foreach($this->departments as $key=> $val){
682       if(!isset($this->config->departments[trim($key)])){
683         $this->config->departments[trim($key)]="";
684       }
686       $non_empty="";
687       $keys= str_replace("/","\/",$key);
688       foreach($this->config->departments as $keyd=>$vald ){
689         if(preg_match("/".$keys."\/.*/",$keyd)){
690           $non_empty="full";
691         }
692       }
694       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>");
695       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
696       $field3 = array("string" => "&nbsp;");
697       $field4 = array("string" => "&nbsp;", "attach" => "style='border:none'");
700       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
701     }
703     /* Pictures for Extensions */
704     $usrimg   ="<input type='image' src='images/penguin.png' alt='"._("GOsa")."'   
705                     name='user_edit_%KEY%-user' title='"._("Edit generic properties")."'>";
706     $posiximg = "<input type='image' src='images/select_user.png' alt='"._("Posix")."'  
707                     name='user_edit_%KEY%-posixAccount' title='"._("Edit UNIX properties")."'>";
708     $mailimg  = "<input type='image' src='images/mailto.png' alt='"._("Mail")."'     
709                     name='user_edit_%KEY%-mailAccount' title='"._("Edit mail properties")."'>";
710     $fonimg   = "<input type='image' src='images/%image%' alt='"._("Phone")."'     
711                     name='user_edit_%KEY%-phoneAccount' title='"._("Edit phone properties")."%title%'>";
712     $faximg   = "<input type='image' src='images/fax_small.png' alt='"._("Fax")."'     
713                     name='user_edit_%KEY%-gofaxAccount' title='"._("Edit fax properies")."'>";
714     $sambaimg = "<input type='image' src='images/select_winstation.png' alt='"._("Samba")."'     
715                     name='user_edit_%KEY%-sambaAccount' title='"._("Edit samba properties")."'>"; 
716     $tplcreateuserimg  = "<input type='image' src='images/list_new.png' alt='"._("Create user from template")."'
717                     name='userfrom_tpl_%KEY%' title='"._("Create user with this template")."'>";   
718     
720     $empty    = "<img src='images/empty.png' style='width:16px;height:16px;' alt=''>"; 
722     // User and Template  Images  
723     $userimg  = "<img src='images/select_user.png' alt='User' title='%s'>";   
724     $tplimg   = "<img src='images/select_template.png' alt='Template' title='%s'>";   
725     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
727     // Defines Arrays to save User and Templates
728     $tpls = array();
729     $users= array();
731     /* Get Configuration for goFon DB (if it is set), to connect to the asterisk tables.
732      * Read Sip Table for specified account (for each user with phoneAccount).
733      * Check the attributes ip port and regseconds.
734      * If regseconds is set and >0 , the phone is logged in.
735      * Else the phone is currently not logged.
736      * If we can't read any Data from the DB or there is no goFon DB specified
737      * show old style without status icons.
738      */
739     $r_db   =false;
740     $r_con  =false;
741     if (isset($_SESSION['config']->data['SERVERS']['FON'])){
742       $a_SETUP= $_SESSION['config']->data['SERVERS']['FON'];
743       // Connect to DB server
744   
745       /* 
746         If you want to disable the status pictures, just comment out the next line
747       */
748       $r_con= @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
749       // Check if we are  connected correctly
750       if($r_con){
751         $r_db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
752       }
753     }
755     // Test Every Entry and generate divlist Array  
756     foreach($this->list as $key => $val){
757       $connected  = ""; // This string represents timestamp or offline status 
758       $ip_port    = ""; // String that will represent ip : port of the connected phone
759       if((in_array("goFonAccount"    ,$val['objectClass']))){
760   
761         /* Set defaults */
762         $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
763         $fonac = preg_replace("/%KEY%/", "$key", $fonac);
764         $fonac = preg_replace("/%title%/", "", $fonac);
766         /* Database connection is ok ?*/
767         if($r_db){
768           $res= @mysql_query("SELECT regseconds,name,port,ipaddr FROM ".$a_SETUP['SIP_TABLE']." WHERE (name='".$val['uid'][0]."')");
769           $mysql_entry = @mysql_fetch_row($res);
770           if(is_array($mysql_entry)){
771             if((isset($mysql_entry[0]))&&($mysql_entry[0]>1)){
772               $connected = " | "._("Online")." : ".gmdate("d.m.Y H:i:s",($mysql_entry[0]+(60*60)));
773               $fonac = preg_replace("/%image%/", "select_phone_connected.png", $fonimg);
774               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
775               $fonac = preg_replace("/%title%/", $connected, $fonac);
776               $ip_port= " - ".$mysql_entry[3].":".$mysql_entry[2];
777             }
778             if((isset($mysql_entry[0]))&&($mysql_entry[0]==0)){
779               $connected = " | "._("Offline");
780               $fonac = preg_replace("/%image%/", "select_phone_notconnected.png", $fonimg);
781               $fonac = preg_replace("/%KEY%/", "$key", $fonac);
782               $fonac = preg_replace("/%title%/", $connected, $fonac);
783             }
784           }
785         }
786       }else{
787         $fonac=$empty;
788       }
790       // Specify Pics for Extensions  
791       if(in_array("posixAccount"    ,$val['objectClass'])){ 
792         $posix = preg_replace("/%KEY%/", "$key", $posiximg);
793       }else{ 
794         $posix=$empty;
795       }
796    
797       if(in_array("gosaMailAccount" ,$val['objectClass'])){ 
798         $maila = preg_replace("/%KEY%/", "$key", $mailimg);   
799       }else{ 
800         $maila=$empty;
801       } 
802   
803       if(in_array("goFaxAccount"    ,$val['objectClass'])){ 
804         $faxac = preg_replace("/%KEY%/", "$key", $faximg);    
805       }else{ 
806         $faxac=$empty;
807       } 
809       if(in_array("sambaSamAccount" ,$val['objectClass'])){ 
810         $samba = preg_replace("/%KEY%/", "$key", $sambaimg);  
811       }else{ 
812         $samba=$empty;
813       } 
815       if(in_array("gosaUserTemplate",$val['objectClass'])){ 
816         $tpl                          = preg_replace("/%KEY%/", "$key", $tplimg);   
817         $s_img_create_from_template   = preg_replace("/%KEY%/", "$key", $tplcreateuserimg);
818       }else{ 
819         $s_img_create_from_template   = "";
820         $tpl=$userimg;
821       }
823       $usrimg = preg_replace("/%KEY%/", "$key", $usrimg);
825       // Generate caption for rows
826       if (isset($val["sn"]) && isset($val["givenName"])){
827         $display= $val["sn"][0].", ".$val["givenName"][0]." [".$val["uid"][0]."]";
828       } else {
829         $display= "[".$val["uid"][0]."]";
830       }
832       /* Create each field */
833       $field1 = array("string" => sprintf($tpl,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
834       $field2 = array("string" => sprintf($editlink,$key,$display).$ip_port,"attach"=>"title='".$val['dn']."'");
835       $field3 = array("string" => $usrimg."&nbsp;".$posix."&nbsp;".$maila."&nbsp;".$fonac."&nbsp;".$faxac."&nbsp;".$samba."&nbsp;".$s_img_create_from_template, "attach" => "style='width:138px;'");
836       $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none; text-align:right;width:48px'");
838       $add = array($field1,$field2,$field3,$field4);
840       // Template or User
841       if(in_array("gosaUserTemplate",$val['objectClass'])){
842         $tpls[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
843       }else{
844         $users[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
845       }
846     }
848     // Sort Entries 
849     ksort($users);
850     ksort($tpls);
852     // Append Templates
853     foreach($tpls as $key => $val){
854       $divlist->AddEntry( $val, array(""));
855     }
856     // Append Users 
857     foreach($users as $key => $val){
858       $divlist->AddEntry( $val, array(""));
859     }
860     /* Show main page */
861     //$smarty->assign("userlist",   nl2br(htmlentities($divlist->DrawList())));
862     $smarty->assign("userlist",   $divlist->DrawList());
863     $smarty->assign("userlisthead", $listhead);
865     /* END NEW LIST MANAGMENT
866      */  
867     $smarty->assign("search_image", get_template_path('images/search.png'));
868     $smarty->assign("tree_image", get_template_path('images/tree.png'));
869     $smarty->assign("infoimage", get_template_path('images/info.png'));
870     $smarty->assign("launchimage", get_template_path('images/launch.png'));
871     $smarty->assign("deplist", $this->config->idepartments);
872     foreach( array("depselect",  "functionalusers", "unixusers",
873           "mailusers", "sambausers", "proxyusers", 
874           "faxusers", "templates", "subsearch") as $type){
876       $smarty->assign("$type", $userfilter[$type]);
877     }
879     $smarty->assign("regex", $userfilter["regex"]);
880     /* Extend if we are not using javascript */
881     $smarty->assign("apply", apply_filter());
882     $smarty->assign("alphabet", generate_alphabet());
883     $smarty->assign("hint", print_sizelimit_warning());
885     return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
886   }
889   function reload()
890   {
891     /* Get config */
892     $userfilter= get_global('userfilter');
894     /* Set base for all searches */
895     $base= $userfilter['depselect'];
897     /* Regex filter? */
898     if ($userfilter['regex'] != ""){
899       $regex= $userfilter['regex'];
900     } else {
901       $regex= "*";
902     }
904     /* Set filter depending on selection */
905     if ($this->config->current['SAMBAVERSION'] == 3){
906       $samba= "sambaSamAccount";
907     } else {
908       $samba= "sambaAccount";
909     }
911     $filter="";
912     if ($userfilter['functionalusers'] == "checked"){
913       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)(objectClass=gosaMailAccount)(objectClass=$samba)(objectClass=goFaxAccount)(objectClass=gosaProxyAccount))))";
914     }
915     if ($userfilter['unixusers'] == "checked"){
916       $filter.= "(objectClass=posixAccount)";
917     }
918     if ($userfilter['mailusers'] == "checked"){
919       $filter.= "(objectClass=gosaMailAccount)";
920     }
921     if ($userfilter['sambausers'] == "checked"){
922       $filter.= "(objectClass=$samba)";
923     }
924     if ($userfilter['proxyusers'] == "checked"){
925       $filter.= "(objectClass=gosaProxyAccount)";
926     }
927     if ($userfilter['faxusers'] == "checked"){
928       $filter.= "(objectClass=goFaxAccount)";
929     }
931     if ($userfilter['templates'] == "checked"){
932       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
933     } else {
934       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
935     }
936     $filter= "(&(|(uid=$regex)(sn=$regex)(givenName=$regex))$filter)";
938     /* Generate userlist */
939     $ldap= $this->config->get_ldap_link(TRUE);
940     if ($userfilter['subsearch'] == "checked"){
941       $this->list= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
942     } else {
943       $base= get_people_ou().$base;
944       $this->list= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("uid", "givenName", "sn", "objectClass"), TRUE);
945     }
949     /* NEW LIST MANAGMENT
950      * We also need to search for the departments
951      * So we are able to navigate like in konquerer 
952      */
953     $base2 = preg_replace("/ou=people,/i","",$base);
955     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
956         TRUE, $base2, array("ou", "description"), TRUE);
958     $this->departments= array();
960     $tmp = array();
961     foreach ($res3 as $value){
962       $tmp[strtolower($value['dn']).$value['dn']]=$value;
963     }
964     ksort($tmp);
965     foreach($tmp as $value){
966       if($value["description"][0]!=".."){
967         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
968       }else{
969         $this->departments[$value['dn']]=$value["description"][0];
970       }
971     }
972     /* END NEW LIST MANAGMENT 
973      */
976     $this->userlist= array();
978     foreach ($this->list as $value){
979       if (isset($value["uid"][0]) && !preg_match('/\$$/', $value["uid"][0])){
980         if (in_array_ics('gosaUserTemplate', $value['objectClass'])){
981           $this->userlist[$value["dn"]]= "* ".$value["uid"][0]." ("._("Template").")";
982           continue;
983         }
984         if (isset($value["givenName"][0]) && isset($value["sn"][0])){
985           $this->userlist[$value["dn"]]= $value["sn"][0].", ".
986             $value["givenName"][0].
987             " [".$value["uid"][0]."]";
988         } else {
989           $this->userlist[$value["dn"]]= "[".$value["uid"][0]."]";
990         }
991       }
992     }
994     natcasesort ($this->userlist);
995     reset ($this->userlist);
996   }
998   function remove_from_parent()
999   {
1000   }
1003   /* Check values */
1004   function check()
1005   {
1006   }
1009   /* Save to LDAP */
1010   function save()
1011   {
1012   }
1014   function adapt_from_template($dn)
1015   {
1016   }
1018   function password_change_needed()
1019   {
1020   }
1022   function show_header($button_text, $text, $disabled= FALSE)
1023   {
1024   }
1026   function remove_lock()
1027   {
1028     if (isset($this->usertab->dn)){
1029       del_lock ($this->usertab->dn);
1030     }
1031   }
1035 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1036 ?>