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