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 if (is_null($this->_auth)) return false; 77 78 // extract the command and any specific parameters 79 // submit button name is of the form - fn[cmd][param(s)] 80 $fn = $_REQUEST['fn']; 81 82 if (is_array($fn)) { 83 $cmd = key($fn); 84 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 85 } else { 86 $cmd = $fn; 87 $param = null; 88 } 89 90 if ($cmd != "search") { 91 if (!empty($_REQUEST['start'])) 92 $this->_start = $_REQUEST['start']; 93 $this->_filter = $this->_retrieveFilter(); 94 } 95 96 switch($cmd){ 97 case "add" : $this->_addUser(); break; 98 case "delete" : $this->_deleteUser(); break; 99 case "modify" : $this->_modifyUser(); break; 100 case "edit" : $this->_editUser($param); break; 101 case "search" : $this->_setFilter($param); 102 $this->_start = 0; 103 break; 104 } 105 106 $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1; 107 108 // page handling 109 switch($cmd){ 110 case 'start' : $this->_start = 0; break; 111 case 'prev' : $this->_start -= $this->_pagesize; break; 112 case 'next' : $this->_start += $this->_pagesize; break; 113 case 'last' : $this->_start = $this->_user_total; break; 114 } 115 $this->_validatePagination(); 116 } 117 118 /** 119 * output appropriate html 120 */ 121 function html() { 122 global $ID; 123 124 if(is_null($this->_auth)) { 125 print $this->lang['badauth']; 126 return false; 127 } 128 129 $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); 130 $users = array_keys($user_list); 131 132 $page_buttons = $this->_pagination(); 133 $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"'; 134 135 $editable = $this->_auth->canDo('UserMod'); 136 137 print $this->locale_xhtml('intro'); 138 print $this->locale_xhtml('list'); 139 140 ptln("<div id=\"user__manager\">"); 141 ptln("<div class=\"level2\">"); 142 143 if ($this->_user_total > 0) { 144 ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>"); 145 } else { 146 ptln("<p>".sprintf($this->lang['nonefound'],$this->_auth->getUserCount())."</p>"); 147 } 148 ptln("<form action=\"".wl($ID)."\" method=\"post\">"); 149 formSecurityToken(); 150 ptln(" <div class=\"table\">"); 151 ptln(" <table class=\"inline\">"); 152 ptln(" <thead>"); 153 ptln(" <tr>"); 154 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>"); 155 ptln(" </tr>"); 156 157 ptln(" <tr>"); 158 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>"); 159 ptln(" <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>"); 160 ptln(" <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>"); 161 ptln(" <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>"); 162 ptln(" <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>"); 163 ptln(" </tr>"); 164 ptln(" </thead>"); 165 166 if ($this->_user_total) { 167 ptln(" <tbody>"); 168 foreach ($user_list as $user => $userinfo) { 169 extract($userinfo); 170 $groups = join(', ',$grps); 171 ptln(" <tr class=\"user_info\">"); 172 ptln(" <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>"); 173 if ($editable) { 174 ptln(" <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1, 175 'do' => 'admin', 176 'page' => 'usermanager', 177 'sectok' => getSecurityToken())). 178 "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>"); 179 } else { 180 ptln(" <td>".hsc($user)."</td>"); 181 } 182 ptln(" <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>"); 183 ptln(" </tr>"); 184 } 185 ptln(" </tbody>"); 186 } 187 188 ptln(" <tbody>"); 189 ptln(" <tr><td colspan=\"5\" class=\"centeralign\">"); 190 ptln(" <span class=\"medialeft\">"); 191 ptln(" <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />"); 192 ptln(" </span>"); 193 ptln(" <span class=\"mediaright\">"); 194 ptln(" <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />"); 195 ptln(" <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />"); 196 ptln(" <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />"); 197 ptln(" <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />"); 198 ptln(" </span>"); 199 ptln(" <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />"); 200 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />"); 201 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />"); 202 203 $this->_htmlFilterSettings(2); 204 205 ptln(" </td></tr>"); 206 ptln(" </tbody>"); 207 ptln(" </table>"); 208 ptln(" </div>"); 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(" <div class=\"table\">",$indent); 260 ptln(" <table class=\"inline\">",$indent); 261 ptln(" <thead>",$indent); 262 ptln(" <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent); 263 ptln(" </thead>",$indent); 264 ptln(" <tbody>",$indent); 265 266 $this->_htmlInputField($cmd."_userid", "userid", $this->lang["user_id"], $user, $this->_auth->canDo("modLogin"), $indent+6); 267 $this->_htmlInputField($cmd."_userpass", "userpass", $this->lang["user_pass"], "", $this->_auth->canDo("modPass"), $indent+6); 268 $this->_htmlInputField($cmd."_username", "username", $this->lang["user_name"], $name, $this->_auth->canDo("modName"), $indent+6); 269 $this->_htmlInputField($cmd."_usermail", "usermail", $this->lang["user_mail"], $mail, $this->_auth->canDo("modMail"), $indent+6); 270 $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6); 271 272 if ($this->_auth->canDo("modPass")) { 273 $notes[] = $this->lang['note_pass']; 274 if ($user) { 275 $notes[] = $this->lang['note_notify']; 276 } 277 278 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); 279 } 280 281 ptln(" </tbody>",$indent); 282 ptln(" <tbody>",$indent); 283 ptln(" <tr>",$indent); 284 ptln(" <td colspan=\"2\">",$indent); 285 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />",$indent); 286 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />",$indent); 287 288 // save current $user, we need this to access details if the name is changed 289 if ($user) 290 ptln(" <input type=\"hidden\" name=\"userid_old\" value=\"".$user."\" />",$indent); 291 292 $this->_htmlFilterSettings($indent+10); 293 294 ptln(" <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent); 295 ptln(" </td>",$indent); 296 ptln(" </tr>",$indent); 297 ptln(" </tbody>",$indent); 298 ptln(" </table>",$indent); 299 ptln(" </div>",$indent); 300 301 foreach ($notes as $note) 302 ptln("<div class=\"fn\">".$note."</div>",$indent); 303 304 ptln("</form>",$indent); 305 } 306 307 function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) { 308 $class = $cando ? '' : ' class="disabled"'; 309 echo str_pad('',$indent); 310 311 if($name == 'userpass'){ 312 $fieldtype = 'password'; 313 $autocomp = 'autocomplete="off"'; 314 }else{ 315 $fieldtype = 'text'; 316 $autocomp = ''; 317 } 318 319 320 echo "<tr $class>"; 321 echo "<td><label for=\"$id\" >$label: </label></td>"; 322 echo "<td>"; 323 if($cando){ 324 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />"; 325 }else{ 326 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; 327 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />"; 328 } 329 echo "</td>"; 330 echo "</tr>"; 331 } 332 333 function _htmlFilter($key) { 334 if (empty($this->_filter)) return ''; 335 return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : ''); 336 } 337 338 function _htmlFilterSettings($indent=0) { 339 340 ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent); 341 342 foreach ($this->_filter as $key => $filter) { 343 ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent); 344 } 345 } 346 347 function _addUser(){ 348 if (!checkSecurityToken()) return false; 349 if (!$this->_auth->canDo('addUser')) return false; 350 351 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(); 352 if (empty($user)) return false; 353 354 if ($this->_auth->canDo('modPass')){ 355 if (empty($pass)){ 356 if(!empty($_REQUEST['usernotify'])){ 357 $pass = auth_pwgen(); 358 } else { 359 msg($this->lang['add_fail'], -1); 360 return false; 361 } 362 } 363 } else { 364 if (!empty($pass)){ 365 msg($this->lang['add_fail'], -1); 366 return false; 367 } 368 } 369 370 if ($this->_auth->canDo('modName')){ 371 if (empty($name)){ 372 msg($this->lang['add_fail'], -1); 373 return false; 374 } 375 } else { 376 if (!empty($name)){ 377 return false; 378 } 379 } 380 381 if ($this->_auth->canDo('modMail')){ 382 if (empty($mail)){ 383 msg($this->lang['add_fail'], -1); 384 return false; 385 } 386 } else { 387 if (!empty($mail)){ 388 return false; 389 } 390 } 391 392 if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) { 393 394 msg($this->lang['add_ok'], 1); 395 396 if (!empty($_REQUEST['usernotify']) && $pass) { 397 $this->_notifyUser($user,$pass); 398 } 399 } else { 400 msg($this->lang['add_fail'], -1); 401 } 402 403 return $ok; 404 } 405 406 /** 407 * Delete user 408 */ 409 function _deleteUser(){ 410 global $conf; 411 412 if (!checkSecurityToken()) return false; 413 if (!$this->_auth->canDo('delUser')) return false; 414 415 $selected = $_REQUEST['delete']; 416 if (!is_array($selected) || empty($selected)) return false; 417 $selected = array_keys($selected); 418 419 if(in_array($_SERVER['REMOTE_USER'], $selected)) { 420 msg("You can't delete yourself!", -1); 421 return false; 422 } 423 424 $count = $this->_auth->triggerUserMod('delete', array($selected)); 425 if ($count == count($selected)) { 426 $text = str_replace('%d', $count, $this->lang['delete_ok']); 427 msg("$text.", 1); 428 } else { 429 $part1 = str_replace('%d', $count, $this->lang['delete_ok']); 430 $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']); 431 msg("$part1, $part2",-1); 432 } 433 434 // invalidate all sessions 435 io_saveFile($conf['cachedir'].'/sessionpurge',time()); 436 437 return true; 438 } 439 440 /** 441 * Edit user (a user has been selected for editing) 442 */ 443 function _editUser($param) { 444 if (!checkSecurityToken()) return false; 445 if (!$this->_auth->canDo('UserMod')) return false; 446 447 $user = cleanID(preg_replace('/.*:/','',$param)); 448 $userdata = $this->_auth->getUserData($user); 449 450 // no user found? 451 if (!$userdata) { 452 msg($this->lang['edit_usermissing'],-1); 453 return false; 454 } 455 456 $this->_edit_user = $user; 457 $this->_edit_userdata = $userdata; 458 459 return true; 460 } 461 462 /** 463 * Modify user (modified user data has been recieved) 464 */ 465 function _modifyUser(){ 466 global $conf; 467 468 if (!checkSecurityToken()) return false; 469 if (!$this->_auth->canDo('UserMod')) return false; 470 471 // get currently valid user data 472 $olduser = cleanID(preg_replace('/.*:/','',$_REQUEST['userid_old'])); 473 $oldinfo = $this->_auth->getUserData($olduser); 474 475 // get new user data subject to change 476 list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser(); 477 if (empty($newuser)) return false; 478 479 $changes = array(); 480 if ($newuser != $olduser) { 481 482 if (!$this->_auth->canDo('modLogin')) { // sanity check, shouldn't be possible 483 msg($this->lang['update_fail'],-1); 484 return false; 485 } 486 487 // check if $newuser already exists 488 if ($this->_auth->getUserData($newuser)) { 489 msg(sprintf($this->lang['update_exists'],$newuser),-1); 490 $re_edit = true; 491 } else { 492 $changes['user'] = $newuser; 493 } 494 } 495 496 // generate password if left empty and notification is on 497 if(!empty($_REQUEST['usernotify']) && empty($newpass)){ 498 $newpass = auth_pwgen(); 499 } 500 501 if (!empty($newpass) && $this->_auth->canDo('modPass')) 502 $changes['pass'] = $newpass; 503 if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) 504 $changes['name'] = $newname; 505 if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) 506 $changes['mail'] = $newmail; 507 if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) 508 $changes['grps'] = $newgrps; 509 510 if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) { 511 msg($this->lang['update_ok'],1); 512 513 if (!empty($_REQUEST['usernotify']) && $newpass) { 514 $notify = empty($changes['user']) ? $olduser : $newuser; 515 $this->_notifyUser($notify,$newpass); 516 } 517 518 // invalidate all sessions 519 io_saveFile($conf['cachedir'].'/sessionpurge',time()); 520 521 } else { 522 msg($this->lang['update_fail'],-1); 523 } 524 525 if (!empty($re_edit)) { 526 $this->_editUser($olduser); 527 } 528 529 return $ok; 530 } 531 532 /** 533 * send password change notification email 534 */ 535 function _notifyUser($user, $password) { 536 537 if ($sent = auth_sendPassword($user,$password)) { 538 msg($this->lang['notify_ok'], 1); 539 } else { 540 msg($this->lang['notify_fail'], -1); 541 } 542 543 return $sent; 544 } 545 546 /** 547 * retrieve & clean user data from the form 548 * 549 * @return array (user, password, full name, email, array(groups)) 550 */ 551 function _retrieveUser($clean=true) { 552 global $auth; 553 global $INPUT; 554 555 $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid'); 556 $user[1] = $INPUT->str('userpass'); 557 $user[2] = $INPUT->str('username'); 558 $user[3] = $INPUT->str('usermail'); 559 $user[4] = explode(',',$INPUT->str('usergroups')); 560 561 $user[4] = array_map('trim',$user[4]); 562 if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]); 563 $user[4] = array_filter($user[4]); 564 $user[4] = array_unique($user[4]); 565 if(!count($user[4])) $user[4] = null; 566 567 return $user; 568 } 569 570 function _setFilter($op) { 571 572 $this->_filter = array(); 573 574 if ($op == 'new') { 575 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false); 576 577 if (!empty($user)) $this->_filter['user'] = $user; 578 if (!empty($name)) $this->_filter['name'] = $name; 579 if (!empty($mail)) $this->_filter['mail'] = $mail; 580 if (!empty($grps)) $this->_filter['grps'] = join('|',$grps); 581 } 582 } 583 584 function _retrieveFilter() { 585 global $INPUT; 586 587 $t_filter = $INPUT->arr('filter'); 588 589 // messy, but this way we ensure we aren't getting any additional crap from malicious users 590 $filter = array(); 591 592 if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; 593 if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; 594 if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; 595 if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; 596 597 return $filter; 598 } 599 600 function _validatePagination() { 601 602 if ($this->_start >= $this->_user_total) { 603 $this->_start = $this->_user_total - $this->_pagesize; 604 } 605 if ($this->_start < 0) $this->_start = 0; 606 607 $this->_last = min($this->_user_total, $this->_start + $this->_pagesize); 608 } 609 610 /* 611 * return an array of strings to enable/disable pagination buttons 612 */ 613 function _pagination() { 614 615 $disabled = 'disabled="disabled"'; 616 617 $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : ''; 618 619 if ($this->_user_total == -1) { 620 $buttons['last'] = $disabled; 621 $buttons['next'] = ''; 622 } else { 623 $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : ''; 624 } 625 626 return $buttons; 627 } 628} 629