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 */ 13if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/'); 14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 15if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/'); 16require_once(DOKU_PLUGIN.'admin.php'); 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 $_user_edit = null; // set to user selected for editing 31 var $_disabled = ''; // if disabled set to explanatory string 32 33 /** 34 * Constructor 35 */ 36 function admin_plugin_usermanager(){ 37 global $auth; 38 39 $this->setupLocale(); 40 41 if (!isset($auth)) { 42 $this->disabled = $this->lang['noauth']; 43 } else if (!$auth->canDo('retrieveUsers')) { 44 $this->disabled = $this->lang['notsupported']; 45 } else { 46 47 // we're good to go 48 $this->_auth = & $auth; 49 50 } 51 } 52 53 /** 54 * return some info 55 */ 56 function getInfo(){ 57 58 return array( 59 'author' => 'Chris Smith', 60 'email' => 'chris@jalakai.co.uk', 61 'date' => '2005-11-24', 62 'name' => 'User Manager', 63 'desc' => 'Manage users '.$this->disabled, 64 'url' => 'http://wiki.splitbrain.org/plugin:user_manager', 65 ); 66 } 67 /** 68 * return prompt for admin menu 69 */ 70 function getMenuText($language) { 71 72 if (!is_null($this->_auth)) 73 return parent::getMenuText($language); 74 75 return $this->getLang["menu"].' '.$this->disabled; 76 } 77 78 /** 79 * return sort order for position in admin menu 80 */ 81 function getMenuSort() { 82 return 2; 83 } 84 85 /** 86 * handle user request 87 */ 88 function handle() { 89 global $ID; 90 91 if (is_null($this->_auth)) return false; 92 93 // extract the command and any specific parameters 94 // submit button name is of the form - fn[cmd][param(s)] 95 $fn = $_REQUEST['fn']; 96 97 if (is_array($fn)) { 98 $cmd = key($fn); 99 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 100 } else { 101 $cmd = $fn; 102 $param = null; 103 } 104 105 if ($cmd != "search") { 106 if (!empty($_REQUEST['start'])) 107 $this->_start = $_REQUEST['start']; 108 $this->_filter = $this->_retrieveFilter(); 109 } 110 111 switch($cmd){ 112 case "add" : $this->_addUser(); break; 113 case "delete" : $this->_deleteUser(); break; 114 case "modify" : $this->_modifyUser(); break; 115 case "edit" : $this->_edit_user = $param; break; // no extra handling required - only html 116 case "search" : $this->_setFilter($param); 117 $this->_start = 0; 118 break; 119 } 120 121 $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1; 122 123 // page handling 124 switch($cmd){ 125 case 'start' : $this->_start = 0; break; 126 case 'prev' : $this->_start -= $this->_pagesize; break; 127 case 'next' : $this->_start += $this->_pagesize; break; 128 case 'last' : $this->_start = $this->_user_total; break; 129 } 130 $this->_validatePagination(); 131 } 132 133 /** 134 * output appropriate html 135 */ 136 function html() { 137 global $ID; 138 139 if(is_null($this->_auth)) { 140 print $this->lang['badauth']; 141 return false; 142 } 143 144 $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); 145 $users = array_keys($user_list); 146 147 $page_buttons = $this->_pagination(); 148 $edit_disable = $this->_auth->canDo('modifyUser') ? '' : 'disabled="disabled"'; 149 $delete_disable = $this->_auth->canDo('deleteUsers') ? '' : 'disabled="disabled"'; 150 151 print $this->locale_xhtml('intro'); 152 print $this->locale_xhtml('list'); 153 154 ptln("<div class=\"level2\" style=\"margin-bottom: 2em;\">"); 155 156 if ($this->_user_total) { 157 ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>"); 158 } else { 159 ptln("<p>".sprintf($this->lang['nonefound'],$this->_auth->getUserCount())."</p>"); 160 } 161 ptln("<form action=\"".wl($ID)."\" method=\"post\">"); 162 ptln(" <table class=\"inline\">"); 163 ptln(" <thead>"); 164 ptln(" <tr>"); 165 ptln(" <th colspan=\"2\"> </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>"); 166 ptln(" </tr>"); 167 168 ptln(" <tr>"); 169// ptln(" <td colspan=\"2\"><input type=\"submit\" name=\"fn[search][new]\" value=\"".$this->lang['search']."\" /></td>"); 170 ptln(" <td colspan=\"2\" style=\"vertical-align:middle; text-align:right;\"><input type=\"image\" src=\"".DOKU_PLUGIN_IMAGES."search.png\" name=\"fn[search][new]\" title=\"".$this->lang['search_prompt']."\" alt=\"".$this->lang['search']."\" /></td>"); 171 ptln(" <td><input type=\"text\" name=\"userid\" value=\"".$this->_htmlFilter('user')."\" /></td>"); 172 ptln(" <td><input type=\"text\" name=\"username\" value=\"".$this->_htmlFilter('name')."\" /></td>"); 173 ptln(" <td><input type=\"text\" name=\"usermail\" value=\"".$this->_htmlFilter('mail')."\" /></td>"); 174 ptln(" <td><input type=\"text\" name=\"usergroups\" value=\"".$this->_htmlFilter('grps')."\" /></td>"); 175 ptln(" </tr>"); 176 ptln(" </thead>"); 177 178 if ($this->_user_total) { 179 ptln(" <tbody>"); 180 foreach ($user_list as $user => $userinfo) { 181 extract($userinfo); 182 $groups = join(', ',$grps); 183 ptln(" <tr valign=\"top\" align=\"left\">"); 184 ptln(" <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>"); 185// ptln(" <td class=\"centeralign\"><input type=\"submit\" name=\"fn[edit][".$user."]\" ".$edit_disable." value=\"".$this->lang['edit']."\"/></td>"); 186 ptln(" <td class=\"centeralign\"><input type=\"image\" name=\"fn[edit][".$user."]\" ".$edit_disable." src=\"".DOKU_PLUGIN_IMAGES."user_edit.png\" title=\"".$this->lang['edit_prompt']."\" alt=\"".$this->lang['edit']."\"/></td>"); 187 ptln(" <td>".hsc($user)."</td><td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>"); 188 ptln(" </tr>"); 189 } 190 ptln(" </tbody>"); 191 } 192 193 ptln(" <tbody>"); 194 ptln(" <tr><td colspan=\"6\" style=\"text-align:center\">"); 195 ptln(" <span style=\"float:left\">"); 196 ptln(" <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />"); 197 ptln(" </span>"); 198 ptln(" <span style=\"float:right\">"); 199 ptln(" <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." value=\"".$this->lang['start']."\" />"); 200 ptln(" <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." value=\"".$this->lang['prev']."\" />"); 201 ptln(" <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." value=\"".$this->lang['next']."\" />"); 202 ptln(" <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." value=\"".$this->lang['last']."\" />"); 203 ptln(" </span>"); 204 ptln(" <input type=\"submit\" name=\"fn[search][clear]\" value=\"".$this->lang['clear']."\" />"); 205 ptln(" </td></tr>"); 206 ptln(" </tbody>"); 207 ptln(" </table>"); 208 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />"); 209 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />"); 210 211 $this->_htmlFilterSettings(2); 212 213 ptln("</form>"); 214 ptln("</div>"); 215 216 $style = $this->_edit_user ? " style=\"width: 46%; float: left;\"" : ""; 217 218 if ($this->_auth->canDo('createUser')) { 219 ptln("<div".$style.">"); 220 print $this->locale_xhtml('add'); 221 ptln(" <div class=\"level2\">"); 222 223 $this->_htmlUserForm('add',null,4); 224 225 ptln(" </div>"); 226 ptln("</div>"); 227 } 228 229 if($this->_edit_user && $this->_auth->canDo('modifyUser')){ 230 ptln("<div".$style." id=\"scroll__here\">"); 231 print $this->locale_xhtml('edit'); 232 ptln(" <div class=\"level2\">"); 233 234 $this->_htmlUserForm('modify',$this->_edit_user,4); 235 236 ptln(" </div>"); 237 ptln("</div>"); 238 } 239 } 240 241 function _htmlUserForm($cmd,$user=null,$indent=0) { 242 243 if ($user) { 244 extract($this->_auth->getUserData($user)); 245 $groups = join(',',$grps); 246 } else { 247 $user = $name = $mail = $groups = ''; 248 } 249 250 ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent); 251 ptln(" <table class=\"inline\">",$indent); 252 ptln(" <thead>",$indent); 253 ptln(" <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent); 254 ptln(" </thead>",$indent); 255 ptln(" <tbody>",$indent); 256 ptln(" <tr><td><label for=\"".$cmd."_userid\" >".$this->lang["user_id"]." : </label></td><td><input type=\"text\" id=\"".$cmd."_userid\" name=\"userid\" value=\"".$user."\" /></td></tr>",$indent); 257 ptln(" <tr><td><label for=\"".$cmd."_userpass\" >".$this->lang["user_pass"]." : </label></td><td><input type=\"text\" id=\"".$cmd."_userpass\" name=\"userpass\" value=\"\" /></td></tr>",$indent); 258 ptln(" <tr><td><label for=\"".$cmd."_username\" >".$this->lang["user_name"]." : </label></td><td><input type=\"text\" id=\"".$cmd."_username\" name=\"username\" value=\"".$name."\" /></td></tr>",$indent); 259 ptln(" <tr><td><label for=\"".$cmd."_usermail\" >".$this->lang["user_mail"]." : </label></td><td><input type=\"text\" id=\"".$cmd."_usermail\" name=\"usermail\" value=\"".$mail."\" /></td></tr>",$indent); 260 ptln(" <tr><td><label for=\"".$cmd."_usergroups\" >".$this->lang["user_groups"]." : </label></td><td><input type=\"text\" id=\"".$cmd."_usergroups\" name=\"usergroups\" value=\"".$groups."\" /></td></tr>",$indent); 261 ptln(" </tbody>",$indent); 262 ptln(" <tbody>",$indent); 263 ptln(" <tr>",$indent); 264 ptln(" <td colspan=\"2\">",$indent); 265 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />",$indent); 266 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />",$indent); 267 268 // save current $user, we need this to access details if the name is changed 269 if ($user) 270 ptln(" <input type=\"hidden\" name=\"userid_old\" value=\"".$user."\" />",$indent); 271 272 $this->_htmlFilterSettings($indent+10); 273 274 ptln(" <input type=\"submit\" name=\"fn[".$cmd."]\" value=\"".$this->lang[$cmd]."\" />",$indent); 275 ptln(" </td>",$indent); 276 ptln(" </tr>",$indent); 277 ptln(" </tbody>",$indent); 278 ptln(" </table>",$indent); 279 ptln("</form>",$indent); 280 } 281 282 function _htmlFilter($key) { 283 if (empty($this->_filter)) return ''; 284 return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : ''); 285 } 286 287 function _htmlFilterSettings($indent=0) { 288 289 ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent); 290 291 foreach ($this->_filter as $key => $filter) { 292 ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent); 293 } 294 } 295 296 function _addUser(){ 297 298 if (!$this->_auth->canDo('createUser')) return false; 299 300 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(); 301 if (empty($user)) return false; 302 303 return $this->_auth->createUser($user,$pass,$name,$mail,$grps); 304 } 305 306 /** 307 * Delete user 308 */ 309 function _deleteUser(){ 310 311 if (!$this->_auth->canDo('deleteUsers')) return false; 312 313 $selected = $_REQUEST['delete']; 314 if (!is_array($selected) || empty($selected)) return false; 315 $selected = array_keys($selected); 316 317 $count = $this->_auth->deleteUsers($selected); 318 if ($count == count($selected)) { 319 $text = str_replace('%d', $count, $this->lang['delete_ok']); 320 msg("$text.", 1); 321 } else { 322 $part1 = str_replace('%d', $count, $this->lang['delete_ok']); 323 $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']); 324 msg("$part1, $part2",-1); 325 } 326 } 327 328 /** 329 * Modify user 330 */ 331 function _modifyUser(){ 332 if (!$this->_auth->canDo('modifyUser')) return false; 333 334 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(); 335 if (empty($user)) return false; 336 337 $changes = array(); 338 $user_old = cleanID(preg_replace('/.*:/','',$_REQUEST['userid_old'])); 339 if ($user != $user_old) { 340 // check $user doesn't already exist 341 if ($this->_auth->getUserData($user)) { 342 msg(sprintf($this->lang['update_exists'],$user),-1); 343 $this->_edit_user = $user = $user_old; 344 } else { 345 $changes['user'] = $user; 346 $user = $user_old; 347 } 348 } 349 350 if (!empty($pass)) $changes['pass'] = $pass; 351 if (!empty($name)) $changes['name'] = $name; 352 if (!empty($mail)) $changes['mail'] = $mail; 353 if (!empty($grps)) $changes['grps'] = $grps; 354 355 if ($this->_auth->modifyUser($user, $changes)) { 356 msg($this->lang['update_ok'],1); 357 } else { 358 msg($this->lang['update_fail'],-1); 359 } 360 } 361 362 /* 363 * retrieve & clean user data from the form 364 * return an array(user, password, full name, email, array(groups)) 365 */ 366 function _retrieveUser($clean=true) { 367 368 $user[0] = ($clean) ? cleanID(preg_replace('/.*:/','',$_REQUEST['userid'])) : $_REQUEST['userid']; 369 $user[1] = $_REQUEST['userpass']; 370 $user[2] = $_REQUEST['username']; 371 $user[3] = $_REQUEST['usermail']; 372 $user[4] = preg_split('/\s*,\s*/',$_REQUEST['usergroups'],-1,PREG_SPLIT_NO_EMPTY); 373 374 if (is_array($user[4]) && (count($user[4]) == 1) && (trim($user[4][0]) == '')) { 375 $user[4] = null; 376 } 377 378 return $user; 379 } 380 381 function _setFilter($op) { 382 383 $this->_filter = array(); 384 385 if ($op == 'new') { 386 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false); 387 388 if (!empty($user)) $this->_filter['user'] = $user; 389 if (!empty($name)) $this->_filter['name'] = $name; 390 if (!empty($mail)) $this->_filter['mail'] = $mail; 391 if (!empty($grps)) $this->_filter['grps'] = join('|',$grps); 392 } 393 } 394 395 function _retrieveFilter() { 396 397 $t_filter = $_REQUEST['filter']; 398 if (!is_array($t_filter)) return array(); 399 400 // messy, but this way we ensure we aren't getting any additional crap from malicious users 401 $filter = array(); 402 403 if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; 404 if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; 405 if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; 406 if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; 407 408 return $filter; 409 } 410 411 function _validatePagination() { 412 413 if ($this->_start >= $this->_user_total) { 414 $this->_start = $this->_user_total - $this->_pagesize; 415 } 416 if ($this->_start < 0) $this->_start = 0; 417 418 $this->_last = min($this->_user_total, $this->_start + $this->_pagesize); 419 } 420 421 /* 422 * return an array of strings to enable/disable pagination buttons 423 */ 424 function _pagination() { 425 426 $disabled = 'disabled="disabled"'; 427 428 $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : ''; 429 430 if ($this->_user_total == -1) { 431 $buttons['last'] = $disabled; 432 $buttons['next'] = ''; 433 } else { 434 $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : ''; 435 } 436 437 return $buttons; 438 } 439} 440