1<?php 2/* 3 * User Manager 4 * 5 * Dokuwiki Admin Plugin 6 * 7 * This version of the user manager has been modified to only work with 8 * objectified version of auth system 9 * 10 * @author neolao <neolao@neolao.com> 11 * @author Chris Smith <chris@jalakai.co.uk> 12 */ 13// must be run within Dokuwiki 14if(!defined('DOKU_INC')) die(); 15 16if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/'); 17 18/** 19 * All DokuWiki plugins to extend the admin function 20 * need to inherit from this class 21 */ 22class admin_plugin_usermanager extends DokuWiki_Admin_Plugin { 23 24 var $_auth = null; // auth object 25 var $_user_total = 0; // number of registered users 26 var $_filter = array(); // user selection filter(s) 27 var $_start = 0; // index of first user to be displayed 28 var $_last = 0; // index of the last user to be displayed 29 var $_pagesize = 20; // number of users to list on one page 30 var $_edit_user = ''; // set to user selected for editing 31 var $_edit_userdata = array(); 32 var $_disabled = ''; // if disabled set to explanatory string 33 34 /** 35 * Constructor 36 */ 37 function admin_plugin_usermanager(){ 38 global $auth; 39 40 $this->setupLocale(); 41 42 if (!isset($auth)) { 43 $this->disabled = $this->lang['noauth']; 44 } else if (!$auth->canDo('getUsers')) { 45 $this->disabled = $this->lang['nosupport']; 46 } else { 47 48 // we're good to go 49 $this->_auth = & $auth; 50 51 } 52 } 53 54 /** 55 * return prompt for admin menu 56 */ 57 function getMenuText($language) { 58 59 if (!is_null($this->_auth)) 60 return parent::getMenuText($language); 61 62 return $this->getLang('menu').' '.$this->disabled; 63 } 64 65 /** 66 * return sort order for position in admin menu 67 */ 68 function getMenuSort() { 69 return 2; 70 } 71 72 /** 73 * handle user request 74 */ 75 function handle() { 76 global $ID; 77 78 if (is_null($this->_auth)) return false; 79 80 // extract the command and any specific parameters 81 // submit button name is of the form - fn[cmd][param(s)] 82 $fn = $_REQUEST['fn']; 83 84 if (is_array($fn)) { 85 $cmd = key($fn); 86 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 87 } else { 88 $cmd = $fn; 89 $param = null; 90 } 91 92 if ($cmd != "search") { 93 if (!empty($_REQUEST['start'])) 94 $this->_start = $_REQUEST['start']; 95 $this->_filter = $this->_retrieveFilter(); 96 } 97 98 switch($cmd){ 99 case "add" : $this->_addUser(); break; 100 case "delete" : $this->_deleteUser(); break; 101 case "modify" : $this->_modifyUser(); break; 102 case "edit" : $this->_editUser($param); break; 103 case "search" : $this->_setFilter($param); 104 $this->_start = 0; 105 break; 106 } 107 108 $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1; 109 110 // page handling 111 switch($cmd){ 112 case 'start' : $this->_start = 0; break; 113 case 'prev' : $this->_start -= $this->_pagesize; break; 114 case 'next' : $this->_start += $this->_pagesize; break; 115 case 'last' : $this->_start = $this->_user_total; break; 116 } 117 $this->_validatePagination(); 118 } 119 120 /** 121 * output appropriate html 122 */ 123 function html() { 124 global $ID; 125 126 if(is_null($this->_auth)) { 127 print $this->lang['badauth']; 128 return false; 129 } 130 131 $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); 132 $users = array_keys($user_list); 133 134 $page_buttons = $this->_pagination(); 135 $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"'; 136 137 $editable = $this->_auth->canDo('UserMod'); 138 139 print $this->locale_xhtml('intro'); 140 print $this->locale_xhtml('list'); 141 142 ptln("<div id=\"user__manager\">"); 143 ptln("<div class=\"level2\">"); 144 145 if ($this->_user_total > 0) { 146 ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>"); 147 } else { 148 ptln("<p>".sprintf($this->lang['nonefound'],$this->_auth->getUserCount())."</p>"); 149 } 150 ptln("<form action=\"".wl($ID)."\" method=\"post\">"); 151 formSecurityToken(); 152 ptln(" <table class=\"inline\">"); 153 ptln(" <thead>"); 154 ptln(" <tr>"); 155 ptln(" <th> </th><th>".$this->lang["user_id"]."</th><th>".$this->lang["user_name"]."</th><th>".$this->lang["user_mail"]."</th><th>".$this->lang["user_groups"]."</th>"); 156 ptln(" </tr>"); 157 158 ptln(" <tr>"); 159 ptln(" <td class=\"rightalign\"><input type=\"image\" src=\"".DOKU_PLUGIN_IMAGES."search.png\" name=\"fn[search][new]\" title=\"".$this->lang['search_prompt']."\" alt=\"".$this->lang['search']."\" class=\"button\" /></td>"); 160 ptln(" <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>"); 161 ptln(" <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>"); 162 ptln(" <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>"); 163 ptln(" <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>"); 164 ptln(" </tr>"); 165 ptln(" </thead>"); 166 167 if ($this->_user_total) { 168 ptln(" <tbody>"); 169 foreach ($user_list as $user => $userinfo) { 170 extract($userinfo); 171 $groups = join(', ',$grps); 172 ptln(" <tr class=\"user_info\">"); 173 ptln(" <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>"); 174 if ($editable) { 175 ptln(" <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1, 176 'do' => 'admin', 177 'page' => 'usermanager', 178 'sectok' => getSecurityToken())). 179 "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>"); 180 } else { 181 ptln(" <td>".hsc($user)."</td>"); 182 } 183 ptln(" <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>"); 184 ptln(" </tr>"); 185 } 186 ptln(" </tbody>"); 187 } 188 189 ptln(" <tbody>"); 190 ptln(" <tr><td colspan=\"5\" class=\"centeralign\">"); 191 ptln(" <span class=\"medialeft\">"); 192 ptln(" <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />"); 193 ptln(" </span>"); 194 ptln(" <span class=\"mediaright\">"); 195 ptln(" <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />"); 196 ptln(" <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />"); 197 ptln(" <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />"); 198 ptln(" <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />"); 199 ptln(" </span>"); 200 ptln(" <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />"); 201 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />"); 202 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />"); 203 204 $this->_htmlFilterSettings(2); 205 206 ptln(" </td></tr>"); 207 ptln(" </tbody>"); 208 ptln(" </table>"); 209 210 ptln("</form>"); 211 ptln("</div>"); 212 213 $style = $this->_edit_user ? " class=\"edit_user\"" : ""; 214 215 if ($this->_auth->canDo('addUser')) { 216 ptln("<div".$style.">"); 217 print $this->locale_xhtml('add'); 218 ptln(" <div class=\"level2\">"); 219 220 $this->_htmlUserForm('add',null,array(),4); 221 222 ptln(" </div>"); 223 ptln("</div>"); 224 } 225 226 if($this->_edit_user && $this->_auth->canDo('UserMod')){ 227 ptln("<div".$style." id=\"scroll__here\">"); 228 print $this->locale_xhtml('edit'); 229 ptln(" <div class=\"level2\">"); 230 231 $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4); 232 233 ptln(" </div>"); 234 ptln("</div>"); 235 } 236 ptln("</div>"); 237 } 238 239 240 /** 241 * @todo disable fields which the backend can't change 242 */ 243 function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) { 244 global $conf; 245 global $ID; 246 247 $name = $mail = $groups = ''; 248 $notes = array(); 249 250 if ($user) { 251 extract($userdata); 252 if (!empty($grps)) $groups = join(',',$grps); 253 } else { 254 $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']); 255 } 256 257 ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent); 258 formSecurityToken(); 259 ptln(" <table class=\"inline\">",$indent); 260 ptln(" <thead>",$indent); 261 ptln(" <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent); 262 ptln(" </thead>",$indent); 263 ptln(" <tbody>",$indent); 264 265 $this->_htmlInputField($cmd."_userid", "userid", $this->lang["user_id"], $user, $this->_auth->canDo("modLogin"), $indent+6); 266 $this->_htmlInputField($cmd."_userpass", "userpass", $this->lang["user_pass"], "", $this->_auth->canDo("modPass"), $indent+6); 267 $this->_htmlInputField($cmd."_username", "username", $this->lang["user_name"], $name, $this->_auth->canDo("modName"), $indent+6); 268 $this->_htmlInputField($cmd."_usermail", "usermail", $this->lang["user_mail"], $mail, $this->_auth->canDo("modMail"), $indent+6); 269 $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6); 270 271 if ($this->_auth->canDo("modPass")) { 272 $notes[] = $this->lang['note_pass']; 273 if ($user) { 274 $notes[] = $this->lang['note_notify']; 275 } 276 277 ptln("<tr><td><label for=\"".$cmd."_usernotify\" >".$this->lang["user_notify"].": </label></td><td><input type=\"checkbox\" id=\"".$cmd."_usernotify\" name=\"usernotify\" value=\"1\" /></td></tr>", $indent); 278 } 279 280 ptln(" </tbody>",$indent); 281 ptln(" <tbody>",$indent); 282 ptln(" <tr>",$indent); 283 ptln(" <td colspan=\"2\">",$indent); 284 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />",$indent); 285 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />",$indent); 286 287 // save current $user, we need this to access details if the name is changed 288 if ($user) 289 ptln(" <input type=\"hidden\" name=\"userid_old\" value=\"".$user."\" />",$indent); 290 291 $this->_htmlFilterSettings($indent+10); 292 293 ptln(" <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent); 294 ptln(" </td>",$indent); 295 ptln(" </tr>",$indent); 296 ptln(" </tbody>",$indent); 297 ptln(" </table>",$indent); 298 299 foreach ($notes as $note) 300 ptln("<div class=\"fn\">".$note."</div>",$indent); 301 302 ptln("</form>",$indent); 303 } 304 305 function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) { 306 $class = $cando ? '' : ' class="disabled"'; 307 $disabled = $cando ? '' : ' disabled="disabled"'; 308 echo str_pad('',$indent); 309 310 if($name == 'userpass'){ 311 $fieldtype = 'password'; 312 $autocomp = 'autocomplete="off"'; 313 }else{ 314 $fieldtype = 'text'; 315 $autocomp = ''; 316 } 317 318 319 echo "<tr $class>"; 320 echo "<td><label for=\"$id\" >$label: </label></td>"; 321 echo "<td>"; 322 if($cando){ 323 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />"; 324 }else{ 325 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; 326 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />"; 327 } 328 echo "</td>"; 329 echo "</tr>"; 330 } 331 332 function _htmlFilter($key) { 333 if (empty($this->_filter)) return ''; 334 return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : ''); 335 } 336 337 function _htmlFilterSettings($indent=0) { 338 339 ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent); 340 341 foreach ($this->_filter as $key => $filter) { 342 ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent); 343 } 344 } 345 346 function _addUser(){ 347 if (!checkSecurityToken()) return false; 348 if (!$this->_auth->canDo('addUser')) return false; 349 350 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(); 351 if (empty($user)) return false; 352 353 if ($this->_auth->canDo('modPass')){ 354 if (empty($pass)){ 355 if(!empty($_REQUEST['usernotify'])){ 356 $pass = auth_pwgen(); 357 } else { 358 msg($this->lang['add_fail'], -1); 359 return false; 360 } 361 } 362 } else { 363 if (!empty($pass)){ 364 msg($this->lang['add_fail'], -1); 365 return false; 366 } 367 } 368 369 if ($this->_auth->canDo('modName')){ 370 if (empty($name)){ 371 msg($this->lang['add_fail'], -1); 372 return false; 373 } 374 } else { 375 if (!empty($name)){ 376 return false; 377 } 378 } 379 380 if ($this->_auth->canDo('modMail')){ 381 if (empty($mail)){ 382 msg($this->lang['add_fail'], -1); 383 return false; 384 } 385 } else { 386 if (!empty($mail)){ 387 return false; 388 } 389 } 390 391 if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) { 392 393 msg($this->lang['add_ok'], 1); 394 395 if (!empty($_REQUEST['usernotify']) && $pass) { 396 $this->_notifyUser($user,$pass); 397 } 398 } else { 399 msg($this->lang['add_fail'], -1); 400 } 401 402 return $ok; 403 } 404 405 /** 406 * Delete user 407 */ 408 function _deleteUser(){ 409 global $conf; 410 411 if (!checkSecurityToken()) return false; 412 if (!$this->_auth->canDo('delUser')) return false; 413 414 $selected = $_REQUEST['delete']; 415 if (!is_array($selected) || empty($selected)) return false; 416 $selected = array_keys($selected); 417 418 if(in_array($_SERVER['REMOTE_USER'], $selected)) { 419 msg("You can't delete yourself!", -1); 420 return false; 421 } 422 423 $count = $this->_auth->triggerUserMod('delete', array($selected)); 424 if ($count == count($selected)) { 425 $text = str_replace('%d', $count, $this->lang['delete_ok']); 426 msg("$text.", 1); 427 } else { 428 $part1 = str_replace('%d', $count, $this->lang['delete_ok']); 429 $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']); 430 msg("$part1, $part2",-1); 431 } 432 433 // invalidate all sessions 434 io_saveFile($conf['cachedir'].'/sessionpurge',time()); 435 436 return true; 437 } 438 439 /** 440 * Edit user (a user has been selected for editing) 441 */ 442 function _editUser($param) { 443 if (!checkSecurityToken()) return false; 444 if (!$this->_auth->canDo('UserMod')) return false; 445 446 $user = cleanID(preg_replace('/.*:/','',$param)); 447 $userdata = $this->_auth->getUserData($user); 448 449 // no user found? 450 if (!$userdata) { 451 msg($this->lang['edit_usermissing'],-1); 452 return false; 453 } 454 455 $this->_edit_user = $user; 456 $this->_edit_userdata = $userdata; 457 458 return true; 459 } 460 461 /** 462 * Modify user (modified user data has been recieved) 463 */ 464 function _modifyUser(){ 465 global $conf; 466 467 if (!checkSecurityToken()) return false; 468 if (!$this->_auth->canDo('UserMod')) return false; 469 470 // get currently valid user data 471 $olduser = cleanID(preg_replace('/.*:/','',$_REQUEST['userid_old'])); 472 $oldinfo = $this->_auth->getUserData($olduser); 473 474 // get new user data subject to change 475 list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser(); 476 if (empty($newuser)) return false; 477 478 $changes = array(); 479 if ($newuser != $olduser) { 480 481 if (!$this->_auth->canDo('modLogin')) { // sanity check, shouldn't be possible 482 msg($this->lang['update_fail'],-1); 483 return false; 484 } 485 486 // check if $newuser already exists 487 if ($this->_auth->getUserData($newuser)) { 488 msg(sprintf($this->lang['update_exists'],$newuser),-1); 489 $re_edit = true; 490 } else { 491 $changes['user'] = $newuser; 492 } 493 } 494 495 // generate password if left empty and notification is on 496 if(!empty($_REQUEST['usernotify']) && empty($newpass)){ 497 $newpass = auth_pwgen(); 498 } 499 500 if (!empty($newpass) && $this->_auth->canDo('modPass')) 501 $changes['pass'] = $newpass; 502 if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) 503 $changes['name'] = $newname; 504 if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) 505 $changes['mail'] = $newmail; 506 if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) 507 $changes['grps'] = $newgrps; 508 509 if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) { 510 msg($this->lang['update_ok'],1); 511 512 if (!empty($_REQUEST['usernotify']) && $newpass) { 513 $notify = empty($changes['user']) ? $olduser : $newuser; 514 $this->_notifyUser($notify,$newpass); 515 } 516 517 // invalidate all sessions 518 io_saveFile($conf['cachedir'].'/sessionpurge',time()); 519 520 } else { 521 msg($this->lang['update_fail'],-1); 522 } 523 524 if (!empty($re_edit)) { 525 $this->_editUser($olduser); 526 } 527 528 return $ok; 529 } 530 531 /** 532 * send password change notification email 533 */ 534 function _notifyUser($user, $password) { 535 536 if ($sent = auth_sendPassword($user,$password)) { 537 msg($this->lang['notify_ok'], 1); 538 } else { 539 msg($this->lang['notify_fail'], -1); 540 } 541 542 return $sent; 543 } 544 545 /** 546 * retrieve & clean user data from the form 547 * 548 * @return array(user, password, full name, email, array(groups)) 549 */ 550 function _retrieveUser($clean=true) { 551 global $auth; 552 553 $user[0] = ($clean) ? $auth->cleanUser($_REQUEST['userid']) : $_REQUEST['userid']; 554 $user[1] = $_REQUEST['userpass']; 555 $user[2] = $_REQUEST['username']; 556 $user[3] = $_REQUEST['usermail']; 557 $user[4] = explode(',',$_REQUEST['usergroups']); 558 559 $user[4] = array_map('trim',$user[4]); 560 if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]); 561 $user[4] = array_filter($user[4]); 562 $user[4] = array_unique($user[4]); 563 if(!count($user[4])) $user[4] = null; 564 565 return $user; 566 } 567 568 function _setFilter($op) { 569 570 $this->_filter = array(); 571 572 if ($op == 'new') { 573 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false); 574 575 if (!empty($user)) $this->_filter['user'] = $user; 576 if (!empty($name)) $this->_filter['name'] = $name; 577 if (!empty($mail)) $this->_filter['mail'] = $mail; 578 if (!empty($grps)) $this->_filter['grps'] = join('|',$grps); 579 } 580 } 581 582 function _retrieveFilter() { 583 584 $t_filter = $_REQUEST['filter']; 585 if (!is_array($t_filter)) return array(); 586 587 // messy, but this way we ensure we aren't getting any additional crap from malicious users 588 $filter = array(); 589 590 if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; 591 if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; 592 if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; 593 if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; 594 595 return $filter; 596 } 597 598 function _validatePagination() { 599 600 if ($this->_start >= $this->_user_total) { 601 $this->_start = $this->_user_total - $this->_pagesize; 602 } 603 if ($this->_start < 0) $this->_start = 0; 604 605 $this->_last = min($this->_user_total, $this->_start + $this->_pagesize); 606 } 607 608 /* 609 * return an array of strings to enable/disable pagination buttons 610 */ 611 function _pagination() { 612 613 $disabled = 'disabled="disabled"'; 614 615 $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : ''; 616 617 if ($this->_user_total == -1) { 618 $buttons['last'] = $disabled; 619 $buttons['next'] = ''; 620 } else { 621 $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : ''; 622 } 623 624 return $buttons; 625 } 626} 627