1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
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 */
21 /* Include user tab class */
22 require "tabs_user.inc";
25 class userManagement extends plugin
26 {
27 /* Plugin definitions */
28 var $plHeadline= "Users";
29 var $plDescription= "This does something";
31 /* Dialog attributes */
32 var $usertab= NULL;
33 var $ui= NULL;
34 var $acl= "";
35 var $templates= array();
36 var $got_uid= false;
37 var $departments= array();
38 var $CopyPasteHandler= NULL;
41 function userManagement($config, $ui)
42 {
44 /* Save configuration for internal use */
45 $this->config= $config;
46 $this->ui= $ui;
48 /* Copy & Paste handler */
49 if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
50 $this->CopyPasteHandler= new CopyPasteHandler($this->config);
51 }
53 /* Get global filter config */
54 if (!isset($_SESSION["userfilter"])){
55 $base= get_base_from_people($ui->dn);
56 $userfilter= array( "mailusers" => "checked",
57 "unixusers" => "checked",
58 "templates" => "",
59 "subsearch" => "",
60 "proxyusers" => "checked",
61 "sambausers" => "checked",
62 "faxusers" => "checked",
63 "functionalusers" => "checked",
64 "regex" => "*");
65 $_SESSION["userfilter"]= $userfilter;
66 }
68 /* Transfer global base to dialogs people base */
69 if(!isset($_SESSION['CurrentMainBase'])){
70 $base= get_base_from_people($ui->dn);
71 $_SESSION['CurrentMainBase']= $base;
72 }
74 /* LOCK MESSAGE Vars */
75 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/");
76 }
79 function execute()
80 {
81 //FIXME: This function lacks structure! It's dammn spaghetti code.
83 /* Call parent execute */
84 plugin::execute();
86 $smarty= get_smarty(); // Smarty instance
87 $userfilter= get_global("userfilter"); // contains Filter Settings
88 $s_action= ""; // Contains the action to be taken
89 $s_entry= ""; // The value for s_action
90 $base_back= ""; // The Link for Backbutton
92 /* Department folder change requested? */
93 if( isset($_GET['act']) && $_GET['act'] == "dep_open" ){
94 $s_action= "open";
95 $s_entry= base64_decode($_GET['dep_id']);
97 /* Set current main base to the requested department DN */
98 $_SESSION['CurrentMainBase']= $this->config->departments[trim($s_entry)];
99 }
101 /* Edit entry button pressed? */
102 if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
103 $s_action= "edit";
104 $s_entry= validate($_GET['id']);
105 }
107 /* Test relevant POST values */
108 foreach($_POST as $key => $val){
110 /* Get every possible POST combination and set s_action/s_entry accordingly */
111 foreach(array("del" => "user_del", "edit" => "user_edit",
112 "back" => "dep_back", "new" => "user_new",
113 "home" => "dep_home", "new_tpl" => "user_tplnew",
114 "change_pw" => "user_chgpw", "root" => "dep_root",
115 "editpaste" => "editpaste", "copy" => "copy",
116 "cut" => "cut") as $act => $name){
118 if (preg_match("/".$name.".*/", $key)){
119 $s_action= $act;
120 $s_entry= preg_replace("/".$name."_/i", "", $key);
121 break;
122 }
123 }
125 } /* ...Test POST */
127 /* Remove coordinate prefix from POST, required by some browsers */
128 $s_entry= preg_replace("/_.$/", "", $s_entry);
130 /* Seperate possibly encoded tab and entry, default to tab "user" */
131 if(preg_match("/.*-.*/", $s_entry)){
132 $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
133 $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
134 }else{
135 $s_tab= "user";
136 }
138 /* Department has changed? */
139 if(isset($_POST['CurrentMainBase'])){
140 $_SESSION['CurrentMainBase']= validate($_POST['CurrentMainBase']);
141 }
143 /* Home-button is pressed, move to users home DN */
144 if($s_action == "home"){
145 $_SESSION['CurrentMainBase']= preg_replace('/^[^,]+,'.normalizePreg(get_people_ou()).'/', "", $this->ui->dn);
146 }
148 /* Root-button is pressed, move to root DN */
149 if($s_action == "root"){
150 $_SESSION['CurrentMainBase']= $this->config->current['BASE'];
151 }
153 /* If Back-button is pressed, move back one step in DN */
154 if($s_action == "back"){
155 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
156 // a step between. You'll stumble in a "hidden" department in this case.
157 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
159 /* The department array keeps non DN entries as index. We need to convert
160 it before checking the existance. */
161 $base_back= trim(convert_department_dn($base_back));
163 /* Check if the department exists, otherwise revert to the configure base DN */
164 if(isset($this->config->departments[$base_back])){
165 $_SESSION['CurrentMainBase']= $this->config->departments[$base_back];
166 }else{
167 $_SESSION['CurrentMainBase']= $this->config->departments['/'];
168 }
169 }
171 # |||| ----------------------- copy and paste - move it into a seperate function
172 //FIXME: I've mostly skipped reviewing this. Just a few comments have been
173 // added. Fact is, that we've an execute() function with more than 800
174 // lines. I personally tend to make this shorter. Even the complete new
175 // list management is a huge copy'n paste factor which can be sourced out
176 // to another function.
178 /* Only perform copy/paste if it is enabled */
179 if($this->CopyPasteHandler){
181 /* Paste copied/cutted object in here */
182 //FIXME: Sure? Does this comment describe what happens below?
183 if( $s_action == "editPaste" || $this->CopyPasteHandler->stillOpen()){
184 $this->CopyPasteHandler->save_object();
185 $this->CopyPasteHandler->SetVar("base", $_SESSION['CurrentMainBase']);
187 /* Execute copy and paste without showing message on success. */
188 //FIXME: It is not clear what really happens, or why we're stopping
189 // to process data here. See the FIXME in class_CopyAndPasteHandler.
190 // Normally we don't have something in $ret, because we've fired
191 // up with (false) <=> which seems to mean "no messages".
192 if( ($ret= $this->CopyPasteHandler->execute(false))){
193 return ($ret);
194 }
196 /* Reload to have freshly copied entries in the list */
197 $this->reload();
199 /* Use the last dn to search for it's ID in the newly generated list. */
200 $dn= $this->CopyPasteHandler->lastdn;
201 foreach($this->list as $id => $entry){
202 if($entry['dn'] == $dn){
203 $s_entry= $id;
204 break;
205 }
206 }
208 /* Change action to "change_pw" if user has requested to do so. */
209 if($_POST['passwordTodo'] == "new"){
210 $s_action= "change_pw";
211 }
212 }
214 /* Copy current object to CopyHandler */
215 if($s_action == "copy"){
216 $this->CopyPasteHandler->Clear();
218 $dn= $this->list[trim($s_entry)]['dn'];
219 $acl= get_permissions ($dn, $this->ui->subtreeACL);
221 $obj= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
222 $objNew= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], "new");
224 $obj->set_acl($acl);
225 $objNew->set_acl($acl);
227 foreach($obj->by_object as $name => $tab){
228 foreach(array("photoData","jpegPhoto","is_account") as $attr){
229 if(isset($obj->by_object[$name]->$attr)){
230 $objNew->by_object[$name]->$attr = $obj->by_object[$name]->$attr;
231 }
232 }
233 }
234 $this->CopyPasteHandler->Copy($obj,$objNew);
235 }
237 /* Copy current object to CopyHandler */
238 //FIXME: Again? This is cut. Hopefully it works without touching the special
239 // attributes from above.
240 if($s_action == "cut"){
241 $this->CopyPasteHandler->Clear();
242 $dn= $this->list[trim($s_entry)]['dn'];
243 $acl= get_permissions ($dn, $this->ui->subtreeACL);
245 $obj= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
246 $obj->set_acl($acl);
248 $this->CopyPasteHandler->Cut($obj);
249 }
250 }
251 # ^^^^^ ----------------------- copy and paste - move it into a seperate function
253 /* Save filter data if we are in the headpage */
254 if(!isset($this->usertab)){
255 $values= array("functionalusers", "unixusers", "mailusers", "sambausers",
256 "proxyusers", "faxusers", "templates", "subsearch");
257 $userfilter= saveFilter($userfilter, $values);
258 register_global("userfilter", $userfilter);
259 }
261 /* React on user interaction here */
262 if (($_SERVER["REQUEST_METHOD"] == "POST")||($_SERVER["REQUEST_METHOD"]== "GET")){
264 /* User wants to edit data? */
265 if (($s_action=="edit") && (!isset($this->usertab->config))){
267 /* Get 'dn' from posted 'uid', must be unique */
268 $this->dn= $this->list[trim($s_entry)]['dn'];
270 /* Check locking, save current plugin in 'back_plugin', so
271 the dialog knows where to return. */
272 if (($user= get_lock($this->dn)) != ""){
273 return(gen_locked_message ($user, $this->dn));
274 }
276 /* Lock the current entry, so everyone will get the
277 above dialog */
278 add_lock ($this->dn, $this->ui->dn);
280 /* Set up the users ACL's for this 'dn' */
281 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
283 /* Register usertab to trigger edit dialog */
284 $this->usertab= new usertabs($this->config,
285 $this->config->data['TABS']['USERTABS'], $this->dn);
287 /* Switch tab, in case that was requested by the user */
288 $this->usertab->current = $s_tab;
290 /* Set ACL and move DN to the headline */
291 $this->usertab->set_acl($acl);
292 $_SESSION['objectinfo']= $this->dn;
293 }
294 }
296 /* Reset all relevant data, if we get a _cancel request */
297 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
298 if (isset($this->usertab)){
299 del_lock ($this->usertab->dn);
300 unset ($this->usertab);
301 }
302 $this->usertab= NULL;
303 $this->lognames= array();;
304 $this->sn= "";
305 $this->givenName= "";
306 $this->uid= "";
307 unset ($_SESSION['objectinfo']);
308 }
310 /* Password change requested */
311 if ($s_action == "change_pw"){
313 /* Get 'dn' from posted 'uid' */
314 $this->dn= $this->list[trim($s_entry)]['dn'];
316 /* Load permissions for selected 'dn' and check if
317 we're allowed to remove this 'dn' */
318 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
319 $acl= get_module_permission($acl, "user", $this->dn);
320 if (chkacl($acl, "password") == ""){
322 /* User is allowed to change passwords, save 'dn' and 'acl' for next
323 dialog. */
324 $this->acl= $acl;
325 $_SESSION['objectinfo']= $this->dn;
326 return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
328 } else {
329 /* User is not allowed. Show message and cancel. */
330 print_red (_("You are not allowed to set this users password!"));
331 }
332 }
334 /* Perform password change */
335 if (isset($_POST['password_finish'])){
337 /* For security reasons, check if user is allowed to set password again */
338 if (chkacl($this->acl, "password") == "" || chkacl($this->acl, "create")){
340 /* Check input and feed errors into 'message' */
341 $message= array();
343 /* Sanity checks... */
344 if ($_POST['new_password'] != $_POST['repeated_password']){
346 /* Matching passwords in new and repeated? */
347 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
348 } else {
350 /* Empty password is not permitted by default. */
351 if ($_POST['new_password'] == ""){
352 $message[]= _("The password you've entered as 'New password' is empty.");
353 }
354 }
356 /* Errors, or password change? */
357 if (count($message) != 0){
359 /* Show error message and continue editing */
360 show_errors ($message);
361 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
362 }
364 $config= $this->config;
365 $ldap_ui= $this->config->get_ldap_link();
366 if(isset($this->usertab->dn)){
367 $user = ($ldap_ui->fetch($ldap_ui->cat($this->usertab->dn)));
368 }else{
369 $user = ($ldap_ui->fetch($ldap_ui->cat($this->dn)));
370 }
371 if((is_array($user))&&(isset($user['uid']))){
372 $username= $user['uid'][0];
373 }
375 /* Set password, perform required steps */
376 if ($this->usertab){
377 if ($this->usertab->password_change_needed()){
378 $obj= $this->usertab->by_object['user'];
379 change_password ($this->usertab->dn, $_POST['new_password'],
380 0, $obj->pw_storage);
381 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
382 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
383 }
385 gosa_log ("Password for '".$this->usertab->dn."' has been changed");
386 unset($this->usertab);
387 $this->usertab= NULL;
388 }
389 } else {
390 change_password ($this->dn, $_POST['new_password']);
391 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
392 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
393 }
395 gosa_log ("Password for '".$this->dn."' has been changed");
396 }
397 } else {
399 /* Missing permissions, show message */
400 print_red (_("You are not allowed to set this users password!"));
401 }
403 /* Clean session, delete lock */
404 del_lock ($this->dn);
405 $this->reload();
406 unset ($this->usertab);
407 $this->usertab= NULL;
408 $this->lognames= array();;
409 $this->sn= "";
410 $this->givenName= "";
411 $this->uid= "";
412 unset ($_SESSION['objectinfo']);
413 }
415 /* Remove user was requested */
416 if ($s_action=="del"){
418 /* Get 'dn' from posted 'uid' */
419 $this->dn= $this->list[trim($s_entry)]['dn'];
421 /* Load permissions for selected 'dn' and check if
422 we're allowed to remove this 'dn' */
423 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
424 $this->acl= get_module_permission($acl, "user", $this->dn);
425 if (chkacl($this->acl, "delete") == ""){
427 /* Check locking, save current plugin in 'back_plugin', so
428 the dialog knows where to return. */
429 if (($user= get_lock($this->dn)) != ""){
430 return(gen_locked_message ($user, $this->dn));
431 }
433 /* Lock the current entry, so nobody will edit it during deletion */
434 add_lock ($this->dn, $this->ui->dn);
435 $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
436 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
437 } else {
439 /* Obviously the user isn't allowed to delete. Show message and
440 clean session. */
441 print_red (_("You are not allowed to delete this user!"));
442 }
443 }
445 /* Confirmation for deletion has been passed. User should be deleted. */
446 if (isset($_POST['delete_user_confirm'])){
448 /* Some nice guy may send this as POST, so we've to check
449 for the permissions again. */
450 if (chkacl($this->acl, "delete") == ""){
452 /* Delete request is permitted, perform LDAP action */
453 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],
454 $this->dn);
455 $this->usertab->set_acl(array($this->acl));
456 $this->usertab->delete ();
457 gosa_log ("User object '".$this->dn."' has been removed");
458 unset ($this->usertab);
459 $this->usertab= NULL;
461 /* User list has changed, reload it. */
462 $this->reload ();
463 } else {
465 /* Normally this shouldn't be reached, send some extra
466 logs to notify the administrator */
467 print_red (_("You are not allowed to delete this user!"));
469 if(isset($this->ui->uid)){
470 gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
471 }
472 }
474 /* Remove lock file after successfull deletion */
475 del_lock ($this->dn);
476 }
479 /* Delete user canceled? */
480 if (isset($_POST['delete_cancel'])){
481 del_lock ($this->dn);
482 }
485 /* Finish user edit is triggered by the tabulator dialog, so
486 the user wants to save edited data. Check and save at this
487 point. */
488 if ((isset($_POST['edit_finish'])) && (isset($this->usertab->config))){
490 /* Check tabs, will feed message array */
491 $this->usertab->last= $this->usertab->current;
492 $this->usertab->save_object();
493 $message= $this->usertab->check();
495 /* Save, or display error message? */
496 if (count($message) == 0){
498 /* No errors. Go ahead and prepare to ask for a password
499 in case we're creating a new user. 'dn' will be 'new'
500 in this case. It is set to the correct value later. */
501 if ($this->dn == "new"){
502 $set_pass= 1;
503 } else {
504 $set_pass= 0;
505 }
507 /* Save user data to ldap */
508 if($this->usertab->save() == 1){
509 gosa_log ("User object '".$this->dn."' saving failed.");
510 return;
511 }
512 gosa_log ("User object '".$this->dn."' has been saved");
514 /* User has been saved successfully, remove lock from
515 LDAP. */
516 if ($this->dn != "new"){
517 del_lock ($this->dn);
518 }
520 /* In case of new users, ask for a password */
521 if (($set_pass || $this->usertab->password_change_needed()) &&
522 !$this->is_template){
524 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
525 }
527 /* There's no page reload so we have to read new users at
528 this point. */
529 $this->reload ();
530 unset ($this->usertab);
531 $this->usertab= NULL;
532 unset ($_SESSION['objectinfo']);
534 } else {
535 /* Ok. There seem to be errors regarding to the tab data,
536 show message and continue as usual. */
537 show_errors($message);
538 }
539 }
541 /* Generate template list */
542 if (($s_action=="new")||($s_action=="create_user_from_tpl")){
544 $this->templates= array();
546 $ldap= $this->config->get_ldap_link();
548 /* Create list of tempaltes */
549 foreach ($this->config->departments as $key => $value){
551 /* Get acls from different ou's */
552 $acl= get_permissions (get_people_ou().$value, $this->ui->subtreeACL);
553 $acl= get_module_permission($acl, "user", get_people_ou().$value);
555 /* If creation of a new user is allowed, append this template */
556 if (chkacl($acl, "create") == ""){
558 /* Search all templates from the current dn */
559 $ldap->cd (get_people_ou().$value);
560 $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
562 /* Append */
563 if ($ldap->count() != 0){
564 while ($attrs= $ldap->fetch()){
565 $this->templates[$ldap->getDN()]=
566 $attrs['uid'][0]." - ".@LDAP::fix($key);
567 }
568 $this->templates['none']= _("none");
569 }
570 }
571 }
573 /* Sort templates */
574 natcasesort ($this->templates);
575 reset ($this->templates);
576 }
578 /* New user/template request */
579 if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
580 /* By default we set 'dn' to 'new', all relevant plugins will
581 react on this. */
582 $this->dn= "new";
583 if (isset($this->config->current['IDGEN'])){
584 $this->got_uid= false;
585 } else {
586 $this->got_uid= true;
587 }
589 /* Create new usertab object */
590 $this->usertab= new usertabs($this->config,
591 $this->config->data['TABS']['USERTABS'], $this->dn);
592 $this->usertab->set_acl(array(':all'));
593 $this->usertab->by_object['user']->base= $_SESSION['CurrentMainBase'];
595 /* Take care about templates */
596 if ($s_action=="new_tpl"){
597 $this->is_template= TRUE;
598 $this->usertab->set_template_mode ();
599 } else {
600 $this->is_template= FALSE;
601 }
603 /* Use template if there are any of them */
604 if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
605 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
606 $smarty->assign("$attr", $this->$attr);
607 }
608 if ($s_action=="create_user_from_tpl"){
609 $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
610 } else {
611 $smarty->assign("template", "none");
612 }
613 $smarty->assign("edit_uid", "");
614 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
615 }
616 }
618 /* Continue template editing */
619 if ((isset($_POST['template_continue']) && $_POST['template'] != 'none' && !isset($_POST['uid']))){
620 $this->sn= $_POST['sn'];
621 $this->givenName= $_POST['givenName'];
623 /* Check for requred values */
624 $message= array();
625 if ($this->sn == "") {
626 $message[]= _("The required field 'Name' is not set.");
627 }
628 if ($this->givenName == "") {
629 $message[]= _("The required field 'Given name' is not set.");
630 }
632 /* Check if dn is used */
633 $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
634 $ldap= $this->config->get_ldap_link();
635 $ldap->cd ($dn);
636 $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
637 if ($ldap->count () != 0){
638 $message[]= _("A person with the choosen name is already used in this tree.");
639 }
641 /* Show error message / continue editing */
642 if (count($message) > 0){
643 show_errors ($message);
644 } else {
645 $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
646 if (isset($this->config->current['IDGEN']) &&
647 $this->config->current['IDGEN'] != ""){
648 $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
649 if (count($uids)){
650 $smarty->assign("edit_uid", "false");
651 $smarty->assign("uids", $uids);
652 $this->uid= current($uids);
653 }
654 } else {
655 $smarty->assign("edit_uid", "");
656 $this->uid= "";
657 }
658 $this->got_uid= true;
659 }
661 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
662 $smarty->assign("$attr", $this->$attr);
663 }
664 if (isset($_POST['template'])){
665 $smarty->assign("template", $_POST['template']);
666 }
667 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
668 }
670 /* No template. Ok. Lets fill data into the normal user dialog */
671 if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
672 foreach(array("sn", "givenName", "uid") as $attr){
673 if (isset($_POST[$attr])){
674 $this->usertab->by_object['user']->$attr= $_POST[$attr];
675 }
676 }
677 }
679 /* Finish template preamble */
680 if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
682 /* Might not be filled if IDGEN is unset */
683 $this->sn = $_POST['sn'];
684 $this->givenName = $_POST['givenName'];
686 /* Move user supplied data to sub plugins */
687 $this->uid = $_POST['uid'];
688 $this->usertab->uid = $this->uid;
689 $this->usertab->sn = $this->sn;
690 $this->usertab->givenName = $this->givenName;
691 $template_dn = $_POST['template'];
692 $this->usertab->adapt_from_template($template_dn);
693 $template_base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
694 $this->usertab->by_object['user']->base= $template_base;
696 /* Set up the users ACL's for this 'dn' */
697 $acl= get_permissions ($template_base, $this->ui->subtreeACL);
698 $this->usertab->set_acl($acl);
699 }
701 if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
702 $this->usertab->by_object['user']->base= $_SESSION['CurrentMainBase'];
703 }
705 /* Show tab dialog if object is present */
706 if(isset($this->usertab->config)){
707 $display= $this->usertab->execute();
709 /* Don't show buttons if tab dialog requests this */
710 if (!$this->usertab->by_object[$this->usertab->current]->dialog){
711 $display.= "<p style=\"text-align:right\">\n";
712 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
713 $display.= " \n";
714 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
715 $display.= "</p>";
716 }
717 return ($display);
718 }
720 /* Reload if we need a reload... */
721 if (!isset($this->usertab) &&
722 !isset($_POST['new_user']) &&
723 !isset($_POST['new_template']) &&
724 !isset($_POST['delete_user']) &&
725 !isset($_POST['setpass_user']) &&
726 !isset($_POST['select_user'])){
727 $this->reload();
728 }
730 /* Check for exeeded sizelimit */
731 if (($message= check_sizelimit()) != ""){
732 return($message);
733 }
735 /* Prepare departments */
736 $options= "";
737 foreach ($this->config->idepartments as $key => $value){
738 if ($_SESSION['CurrentMainBase'] == $key){
739 $options.= "<option selected value='$key'>$value</option>";
740 } else {
741 $options.= "<option value='$key'>$value</option>";
742 }
743 }
745 /* Choose correct paste icon */
746 if($this->CopyPasteHandler){
747 $Copy_Paste= $this->CopyPasteHandler->generatePasteIcon();
748 }else{
749 $Copy_Paste= "";
750 }
752 #------------------------------------------------------------------------ Review mark
753 //FIXME: Hmm. I spent some hours in the past to get rid of HTML inside the
754 // PHP code. The section from here until the end of the execute() function
755 // is not what I'd call a good solution. It works, but its somewhat ugly.
756 // Currently I've no idea to make it better. HERE docs can help to make
757 // the code more readable. Smarty templates would be the best solution.
758 // Just not sure how to do it right now.
760 /* NEW LIST MANAGMENT */
761 //FIXME: Who cares? The old one is not present, and the big letters don't
762 // make clear that it's still spaghetti code...
763 //FIXME: Variable naming - field1....
764 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
765 " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
766 " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
767 " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
768 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
769 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
770 " <input class='center' type='image' align='middle' src='images/list_new_user.png' title='"._("Create new user")."' alt='"._("New user")."' name='user_new'> ".
771 " <input class='center' type='image' align='middle' src='images/list_new.png' title='"._("Create new template")."' alt='"._("New template")."' name='user_tplnew'> ".
772 $Copy_Paste.
773 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
774 _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
775 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
776 "</div>";
778 if($this->CopyPasteHandler){
779 $action = "<input class='center' type='image'
780 src='images/editcut.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'> ";
781 $action.= "<input class='center' type='image'
782 src='images/editcopy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'> ";
783 $action.= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'
784 name='user_edit_%KEY%' title='"._("Edit user")."'>";
785 $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."'
786 name='user_chgpw_%KEY%' title='"._("Change password")."'>";
787 $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'
788 name='user_del_%KEY%' title='"._("Delete user")."'>";
789 }else{
790 $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'
791 name='user_edit_%KEY%' title='"._("Edit user")."'>";
792 $action.= "<input class='center' type='image' src='images/list_password.png' alt='"._("password")."'
793 name='user_chgpw_%KEY%' title='"._("Change password")."'>";
794 $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'
795 name='user_del_%KEY%' title='"._("Delete user")."'>";
796 }
798 $divlist = new divlist("userstab");
799 $divlist->SetHeader(array(
800 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
801 array("string" => _("Username")." / "._("Department"), "attach" => "style=''"),
802 array("string" => _("Properties"), "attach" => "style='width:152px;'"),
803 array("string" => _("Actions"), "attach" => "style='width:102px;border-right:0px;text-align:right;'")));
805 $divlist->SetSummary(_("This table displays all users, in the selected tree."));
806 $divlist->SetEntriesPerPage(0);
808 /* Defining Links */
809 $linkopen = "<a href='?plug=".validate($_GET['plug'])."&act=dep_open&dep_id=%s'>%s</a>";
811 /* Insert departments in divlist*/
812 foreach($this->departments as $key => $val){
814 if(!isset($this->config->departments[trim($key)])){
815 $this->config->departments[trim($key)]="";
816 }
818 $non_empty="";
819 $nkey= normalizePreg($key);
820 foreach($this->config->departments as $keyd => $vald ){
821 if(preg_match("/$nkey\/.*/", $keyd)){
822 $non_empty="full";
823 }
824 }
826 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
827 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
828 $field3 = array("string" => " ", "attach" => "style='width:152px;'");
829 $field4 = array("string" => " ", "attach" => "style='width:90px;border-right:0px;text-align:right;'");
831 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
832 }
834 /* Pictures for Extensions */
835 $usrimg ="<input class='center' type='image' src='images/penguin.png' alt='"._("GOsa")."'
836 name='user_edit_%KEY%-user' title='"._("Edit generic properties")."'>";
837 $posiximg = "<input class='center' type='image' src='images/select_user.png' alt='"._("Posix")."'
838 name='user_edit_%KEY%-posixAccount' title='"._("Edit UNIX properties")."'>";
839 $eviroimg = "<input class='center' type='image' src='images/smallenv.png' alt='"._("Environment")."'
840 name='user_edit_%KEY%-environment' title='"._("Edit environment properties")."'>";
841 $mailimg = "<input class='center' type='image' src='images/mailto.png' alt='"._("Mail")."'
842 name='user_edit_%KEY%-mailAccount' title='"._("Edit mail properties")."'>";
843 $fonimg = "<input class='center' type='image' src='images/%image%' alt='"._("Phone")."'
844 name='user_edit_%KEY%-phoneAccount' title='"._("Edit phone properties")."%title%'>";
845 $faximg = "<input class='center' type='image' src='images/fax_small.png' alt='"._("Fax")."'
846 name='user_edit_%KEY%-gofaxAccount' title='"._("Edit fax properies")."'>";
847 $sambaimg = "<input class='center' type='image' src='images/select_winstation.png' alt='"._("Samba")."'
848 name='user_edit_%KEY%-sambaAccount' title='"._("Edit samba properties")."'>";
849 $tplcreateuserimg = "<input type='image' class='center' src='images/list_new.png' alt='"._("Create user from template")."'
850 name='userfrom_tpl_%KEY%' title='"._("Create user with this template")."'>";
852 $empty = "<img class='center' src='images/empty.png' style='width:16px;height:16px;' alt=''>";
854 // User and Template Images
855 $userimg = "<img class='center' src='images/select_user.png' alt='User' title='%s'>";
856 $tplimg = "<img class='center' src='images/select_template.png' alt='Template' title='%s'>";
857 $editlink = "<a href='?plug=".validate($_GET['plug'])."&id=%s&act=edit_entry'>%s</a>";
859 // Defines Arrays to save User and Templates
860 $tpls = array();
861 $users= array();
863 /* Get Configuration for goFon DB (if it is set), to connect to the asterisk tables.
864 * Read Sip Table for specified account (for each user with phoneAccount).
865 * Check the attributes ip port and regseconds.
866 * If regseconds is set and >0 , the phone is logged in.
867 * Else the phone is currently not logged.
868 * If we can't read any Data from the DB or there is no goFon DB specified
869 * show old style without status icons.
870 */
871 $r_db =false;
872 $r_con =false;
873 if (isset($_SESSION['config']->data['SERVERS']['FON'])){
874 $a_SETUP= $_SESSION['config']->data['SERVERS']['FON'];
875 // Connect to DB server
877 /*
878 If you want to disable the status pictures, just comment out the next line
879 */
880 $r_con = false;
881 $r_db = false;
882 if(is_callable("mysql_pconnect")){
883 $r_con= @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
884 // Check if we are connected correctly
885 if($r_con){
886 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
887 }
888 }
889 }
891 // Test Every Entry and generate divlist Array
892 foreach($this->list as $key => $val){
893 $connected = ""; // This string represents timestamp or offline status
894 $ip_port = ""; // String that will represent ip : port of the connected phone
895 if((in_array("goFonAccount" ,$val['objectClass']))){
897 /* Set defaults */
898 $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
899 $fonac = preg_replace("/%KEY%/", "$key", $fonac);
900 $fonac = preg_replace("/%title%/", "", $fonac);
902 /* Database connection is ok ?*/
903 if(($r_db)&&(is_callable("mysql_query"))){
904 $res= @mysql_query("SELECT regseconds,name,port,ipaddr FROM ".$a_SETUP['SIP_TABLE']." WHERE (name='".$val['uid'][0]."')");
905 $mysql_entry = @mysql_fetch_row($res);
906 if(is_array($mysql_entry)){
907 if((isset($mysql_entry[0]))&&($mysql_entry[0]>1)){
908 $connected = " | "._("Online")." : ".gmdate("d.m.Y H:i:s",($mysql_entry[0]+(60*60)));
909 $fonac = preg_replace("/%image%/", "select_phone_connected.png", $fonimg);
910 $fonac = preg_replace("/%KEY%/", "$key", $fonac);
911 $fonac = preg_replace("/%title%/", $connected, $fonac);
912 $ip_port= " - ".$mysql_entry[3].":".$mysql_entry[2];
913 }
914 if((isset($mysql_entry[0]))&&($mysql_entry[0]==0)){
915 $connected = " | "._("Offline");
916 $fonac = preg_replace("/%image%/", "select_phone.png", $fonimg);
917 $fonac = preg_replace("/%KEY%/", "$key", $fonac);
918 $fonac = preg_replace("/%title%/", $connected, $fonac);
919 }
920 }
921 }
922 }else{
923 $fonac=$empty;
924 }
928 /* Specify Pics for Extensions */
929 //FIXME: Spaghetti code. Please replace by a simple loop.
930 if(in_array("posixAccount" ,$val['objectClass'])){
931 $posix = preg_replace("/%KEY%/", "$key", $posiximg);
932 }else{
933 $posix=$empty;
934 }
936 if(in_array("gotoEnvironment" ,$val['objectClass'])){
937 $enviro = preg_replace("/%KEY%/", "$key", $eviroimg);
938 }else{
939 $enviro =$empty;
940 }
942 if(in_array("gosaMailAccount" ,$val['objectClass'])){
943 $maila = preg_replace("/%KEY%/", "$key", $mailimg);
944 }else{
945 $maila=$empty;
946 }
948 if(in_array("goFaxAccount" ,$val['objectClass'])){
949 $faxac = preg_replace("/%KEY%/", "$key", $faximg);
950 }else{
951 $faxac=$empty;
952 }
954 if(in_array("sambaSamAccount" ,$val['objectClass'])){
955 $samba = preg_replace("/%KEY%/", "$key", $sambaimg);
956 }else{
957 $samba=$empty;
958 }
960 if(in_array("gosaUserTemplate",$val['objectClass'])){
961 $tpl = preg_replace("/%KEY%/", "$key", $tplimg);
962 $s_img_create_from_template = preg_replace("/%KEY%/", "$key", $tplcreateuserimg);
963 }else{
964 $s_img_create_from_template = "";
965 $tpl=$userimg;
966 }
968 $usrimg2 = preg_replace("/%KEY%/", "$key", $usrimg);
970 // Generate caption for rows
971 if (isset($val["sn"]) && isset($val["givenName"])){
972 $display= $val["sn"][0].", ".$val["givenName"][0]." [".$val["uid"][0]."]";
973 } else {
974 $display= "[".$val["uid"][0]."]";
975 }
977 /* Create each field */
978 $field1 = array("string" => sprintf($tpl,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
979 $field2 = array("string" => sprintf($editlink,$key,$display).$ip_port, "attach" => "style='' title='dn: ".@LDAP::fix($val['dn'])."'");
980 $field3 = array("string" => $usrimg2." ".$posix." ".$enviro." ".$maila." ".$fonac." ".$faxac." ".$samba, "attach" => "style='width:152px;'");
981 $field4 = array("string" => $s_img_create_from_template.preg_replace("/%KEY%/", "$key", $action),"attach" => "style='width:102px;border-right:0px;text-align:right;'");
983 $add = array($field1,$field2,$field3,$field4);
985 // Template or User
986 if(in_array("gosaUserTemplate",$val['objectClass'])){
987 $tpls[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
988 }else{
989 $users[strtolower( $val['sn']['0'].$val['uid']['0'])]=$add;
990 }
991 }
993 // Sort Entries
994 ksort($users);
995 ksort($tpls);
997 // Append Templates
998 foreach($tpls as $key => $val){
999 $divlist->AddEntry( $val, array(""));
1000 }
1001 // Append Users
1002 foreach($users as $key => $val){
1003 $divlist->AddEntry( $val, array(""));
1004 }
1005 $smarty->assign("userlist", $divlist->DrawList());
1006 $smarty->assign("userlisthead", $listhead);
1008 $smarty->assign("search_image", get_template_path('images/search.png'));
1009 $smarty->assign("tree_image", get_template_path('images/tree.png'));
1010 $smarty->assign("infoimage", get_template_path('images/info.png'));
1011 $smarty->assign("launchimage", get_template_path('images/launch.png'));
1012 $smarty->assign("deplist", $this->config->idepartments);
1014 foreach( array("functionalusers", "unixusers",
1015 "mailusers", "sambausers", "proxyusers",
1016 "faxusers", "templates", "subsearch") as $type){
1018 $smarty->assign("$type", $userfilter[$type]);
1019 }
1021 $smarty->assign("CurrentMainBase",$_SESSION['CurrentMainBase']);
1022 $smarty->assign("regex", $userfilter["regex"]);
1024 /* Extend if we are not using javascript */
1025 $smarty->assign("apply", apply_filter());
1026 $smarty->assign("alphabet", generate_alphabet());
1027 $smarty->assign("hint", print_sizelimit_warning());
1029 if((is_callable("mysql_close"))&&($r_con)){
1030 @mysql_close($r_con);
1031 }
1033 /* Return rendered main page */
1034 return ($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
1035 }
1036 #----------------------------------------------------------------- Reviewed after this mark
1039 function reload()
1040 {
1041 /* Get config */
1042 $userfilter= get_global('userfilter');
1044 /* Set base for all searches */
1045 $base= $_SESSION['CurrentMainBase'];
1047 /* Regex filter? */
1048 if ($userfilter['regex'] != ""){
1049 $regex= $userfilter['regex'];
1050 } else {
1051 $regex= "*";
1052 }
1054 /* Setup filter depending on selection */
1055 $filter="";
1056 if ($this->config->current['SAMBAVERSION'] == 3){
1057 $samba= "sambaSamAccount";
1058 } else {
1059 $samba= "sambaAccount";
1060 }
1061 if ($userfilter['functionalusers'] == "checked"){
1062 $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1063 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1064 "(objectClass=gosaProxyAccount))))";
1065 }
1066 if ($userfilter['unixusers'] == "checked"){
1067 $filter.= "(objectClass=posixAccount)";
1068 }
1069 if ($userfilter['mailusers'] == "checked"){
1070 $filter.= "(objectClass=gosaMailAccount)";
1071 }
1072 if ($userfilter['sambausers'] == "checked"){
1073 $filter.= "(objectClass=$samba)";
1074 }
1075 if ($userfilter['proxyusers'] == "checked"){
1076 $filter.= "(objectClass=gosaProxyAccount)";
1077 }
1078 if ($userfilter['templates'] == "checked"){
1079 $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1080 } else {
1081 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1082 }
1083 $filter= "(&(|(uid=".normalizePreg($regex).")(sn=".normalizePreg($regex).")(givenName=".normalizePreg($regex)."))$filter)";
1085 /* Generate userlist */
1086 $ldap= $this->config->get_ldap_link(TRUE);
1087 if ($userfilter['subsearch'] == "checked"){
1088 $this->list= get_list($filter, $this->ui->subtreeACL, $base,
1089 array("uid", "givenName", "sn", "objectClass"), GL_SUBSEARCH | GL_SIZELIMIT);
1090 } else {
1091 $base= get_people_ou().$base;
1092 $this->list= get_list($filter, $this->ui->subtreeACL, $base,
1093 array("uid", "givenName", "sn", "objectClass"), GL_SIZELIMIT);
1094 }
1096 /* Create base for department search */
1097 if(get_people_ou() == ""){
1098 $dep_base= $base;
1099 }else{
1100 $dep_base= preg_replace("/".normalizePreg(get_people_ou())."/i", "", $base);
1101 }
1103 /* Get all departments within this subtree */
1104 $deps= get_list("(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
1105 $dep_base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
1107 /* sort deparmtents */
1108 natcasesort($deps);
1110 /* Append deparments to class var and if available add description */
1111 foreach($deps as $value){
1112 if(isset($value['description'][0])){
1113 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
1114 }else{
1115 $this->departments[$value['dn']]= get_sub_department($value['dn']);
1116 }
1117 }
1119 }
1122 function remove_lock()
1123 {
1124 /* Remove user lock if a DN is marked as "currently edited" */
1125 if (isset($this->usertab->dn)){
1126 del_lock ($this->usertab->dn);
1127 }
1128 }
1131 /* A set of disabled and therefore overloaded functions. They are
1132 not needed in this class. */
1133 function remove_from_parent() { }
1134 function check() { }
1135 function save() { }
1136 function adapt_from_template($dn) { }
1137 function password_change_needed() { }
1138 function show_header($button_text, $text, $disabled= FALSE) { }
1140 } /* ... class userManagement */
1143 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1144 ?>