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