first commit. dokuwiki.
[h2N7SspZmY.git] / lib / plugins / usermanager / admin.php
blobda6029bbf4d65301a1677e94e61c196171ba549a
1 <?php
2 /*
3 * User Manager
5 * Dokuwiki Admin Plugin
7 * This version of the user manager has been modified to only work with
8 * objectified version of auth system
10 * @author neolao <neolao@neolao.com>
11 * @author Chris Smith <chris@jalakai.co.uk>
13 // must be run within Dokuwiki
14 if(!defined('DOKU_INC')) die();
16 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17 if(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/');
18 require_once(DOKU_PLUGIN.'admin.php');
20 /**
21 * All DokuWiki plugins to extend the admin function
22 * need to inherit from this class
24 class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
26 var $_auth = null; // auth object
27 var $_user_total = 0; // number of registered users
28 var $_filter = array(); // user selection filter(s)
29 var $_start = 0; // index of first user to be displayed
30 var $_last = 0; // index of the last user to be displayed
31 var $_pagesize = 20; // number of users to list on one page
32 var $_edit_user = ''; // set to user selected for editing
33 var $_edit_userdata = array();
34 var $_disabled = ''; // if disabled set to explanatory string
36 /**
37 * Constructor
39 function admin_plugin_usermanager(){
40 global $auth;
42 $this->setupLocale();
44 if (!isset($auth)) {
45 $this->disabled = $this->lang['noauth'];
46 } else if (!$auth->canDo('getUsers')) {
47 $this->disabled = $this->lang['nosupport'];
48 } else {
50 // we're good to go
51 $this->_auth = & $auth;
56 /**
57 * return some info
59 function getInfo(){
61 return array(
62 'author' => 'Chris Smith',
63 'email' => 'chris@jalakai.co.uk',
64 'date' => '2008-09-17',
65 'name' => 'User Manager',
66 'desc' => 'Manage users '.$this->disabled,
67 'url' => 'http://dokuwiki.org/plugin:usermanager',
70 /**
71 * return prompt for admin menu
73 function getMenuText($language) {
75 if (!is_null($this->_auth))
76 return parent::getMenuText($language);
78 return $this->getLang('menu').' '.$this->disabled;
81 /**
82 * return sort order for position in admin menu
84 function getMenuSort() {
85 return 2;
88 /**
89 * handle user request
91 function handle() {
92 global $ID;
94 if (is_null($this->_auth)) return false;
96 // extract the command and any specific parameters
97 // submit button name is of the form - fn[cmd][param(s)]
98 $fn = $_REQUEST['fn'];
100 if (is_array($fn)) {
101 $cmd = key($fn);
102 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
103 } else {
104 $cmd = $fn;
105 $param = null;
108 if ($cmd != "search") {
109 if (!empty($_REQUEST['start']))
110 $this->_start = $_REQUEST['start'];
111 $this->_filter = $this->_retrieveFilter();
114 switch($cmd){
115 case "add" : $this->_addUser(); break;
116 case "delete" : $this->_deleteUser(); break;
117 case "modify" : $this->_modifyUser(); break;
118 case "edit" : $this->_editUser($param); break;
119 case "search" : $this->_setFilter($param);
120 $this->_start = 0;
121 break;
124 $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
126 // page handling
127 switch($cmd){
128 case 'start' : $this->_start = 0; break;
129 case 'prev' : $this->_start -= $this->_pagesize; break;
130 case 'next' : $this->_start += $this->_pagesize; break;
131 case 'last' : $this->_start = $this->_user_total; break;
133 $this->_validatePagination();
137 * output appropriate html
139 function html() {
140 global $ID;
142 if(is_null($this->_auth)) {
143 print $this->lang['badauth'];
144 return false;
147 $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
148 $users = array_keys($user_list);
150 $page_buttons = $this->_pagination();
151 $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
153 $editable = $this->_auth->canDo('UserMod');
155 print $this->locale_xhtml('intro');
156 print $this->locale_xhtml('list');
158 ptln("<div id=\"user__manager\">");
159 ptln("<div class=\"level2\">");
161 if ($this->_user_total > 0) {
162 ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
163 } else {
164 ptln("<p>".sprintf($this->lang['nonefound'],$this->_auth->getUserCount())."</p>");
166 ptln("<form action=\"".wl($ID)."\" method=\"post\">");
167 formSecurityToken();
168 ptln(" <table class=\"inline\">");
169 ptln(" <thead>");
170 ptln(" <tr>");
171 ptln(" <th>&nbsp;</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>");
172 ptln(" </tr>");
174 ptln(" <tr>");
175 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>");
176 ptln(" <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
177 ptln(" <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
178 ptln(" <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
179 ptln(" <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
180 ptln(" </tr>");
181 ptln(" </thead>");
183 if ($this->_user_total) {
184 ptln(" <tbody>");
185 foreach ($user_list as $user => $userinfo) {
186 extract($userinfo);
187 $groups = join(', ',$grps);
188 ptln(" <tr class=\"user_info\">");
189 ptln(" <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
190 if ($editable) {
191 ptln(" <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
192 'do' => 'admin',
193 'page' => 'usermanager',
194 'sectok' => getSecurityToken())).
195 "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
196 } else {
197 ptln(" <td>".hsc($user)."</td>");
199 ptln(" <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
200 ptln(" </tr>");
202 ptln(" </tbody>");
205 ptln(" <tbody>");
206 ptln(" <tr><td colspan=\"5\" class=\"centeralign\">");
207 ptln(" <span class=\"medialeft\">");
208 ptln(" <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
209 ptln(" </span>");
210 ptln(" <span class=\"mediaright\">");
211 ptln(" <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
212 ptln(" <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
213 ptln(" <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
214 ptln(" <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
215 ptln(" </span>");
216 ptln(" <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
217 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />");
218 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />");
220 $this->_htmlFilterSettings(2);
222 ptln(" </td></tr>");
223 ptln(" </tbody>");
224 ptln(" </table>");
226 ptln("</form>");
227 ptln("</div>");
229 $style = $this->_edit_user ? " class=\"edit_user\"" : "";
231 if ($this->_auth->canDo('addUser')) {
232 ptln("<div".$style.">");
233 print $this->locale_xhtml('add');
234 ptln(" <div class=\"level2\">");
236 $this->_htmlUserForm('add',null,array(),4);
238 ptln(" </div>");
239 ptln("</div>");
242 if($this->_edit_user && $this->_auth->canDo('UserMod')){
243 ptln("<div".$style." id=\"scroll__here\">");
244 print $this->locale_xhtml('edit');
245 ptln(" <div class=\"level2\">");
247 $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
249 ptln(" </div>");
250 ptln("</div>");
252 ptln("</div>");
257 * @todo disable fields which the backend can't change
259 function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
260 global $conf;
261 global $ID;
263 $name = $mail = $groups = '';
264 $notes = array();
266 if ($user) {
267 extract($userdata);
268 if (!empty($grps)) $groups = join(',',$grps);
269 } else {
270 $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
273 ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
274 formSecurityToken();
275 ptln(" <table class=\"inline\">",$indent);
276 ptln(" <thead>",$indent);
277 ptln(" <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
278 ptln(" </thead>",$indent);
279 ptln(" <tbody>",$indent);
281 $this->_htmlInputField($cmd."_userid", "userid", $this->lang["user_id"], $user, $this->_auth->canDo("modLogin"), $indent+6);
282 $this->_htmlInputField($cmd."_userpass", "userpass", $this->lang["user_pass"], "", $this->_auth->canDo("modPass"), $indent+6);
283 $this->_htmlInputField($cmd."_username", "username", $this->lang["user_name"], $name, $this->_auth->canDo("modName"), $indent+6);
284 $this->_htmlInputField($cmd."_usermail", "usermail", $this->lang["user_mail"], $mail, $this->_auth->canDo("modMail"), $indent+6);
285 $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
287 if ($this->_auth->canDo("modPass")) {
288 $notes[] = $this->lang['note_pass'];
289 if ($user) {
290 $notes[] = $this->lang['note_notify'];
293 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);
296 ptln(" </tbody>",$indent);
297 ptln(" <tbody>",$indent);
298 ptln(" <tr>",$indent);
299 ptln(" <td colspan=\"2\">",$indent);
300 ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />",$indent);
301 ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />",$indent);
303 // save current $user, we need this to access details if the name is changed
304 if ($user)
305 ptln(" <input type=\"hidden\" name=\"userid_old\" value=\"".$user."\" />",$indent);
307 $this->_htmlFilterSettings($indent+10);
309 ptln(" <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
310 ptln(" </td>",$indent);
311 ptln(" </tr>",$indent);
312 ptln(" </tbody>",$indent);
313 ptln(" </table>",$indent);
315 foreach ($notes as $note)
316 ptln("<div class=\"fn\">".$note."</div>",$indent);
318 ptln("</form>",$indent);
321 function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
322 $class = $cando ? '' : ' class="disabled"';
323 $disabled = $cando ? '' : ' disabled="disabled"';
324 echo str_pad('',$indent);
326 $fieldtype = ($name == "userpass") ? 'password' : 'text';
328 echo "<tr $class>";
329 echo "<td><label for=\"$id\" >$label: </label></td>";
330 echo "<td>";
331 if($cando){
332 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" />";
333 }else{
334 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
335 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
337 echo "</td>";
338 echo "</tr>";
341 function _htmlFilter($key) {
342 if (empty($this->_filter)) return '';
343 return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
346 function _htmlFilterSettings($indent=0) {
348 ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
350 foreach ($this->_filter as $key => $filter) {
351 ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
355 function _addUser(){
356 if (!checkSecurityToken()) return false;
357 if (!$this->_auth->canDo('addUser')) return false;
359 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser();
360 if (empty($user)) return false;
362 if ($this->_auth->canDo('modPass')){
363 if (empty($pass)){
364 if(!empty($_REQUEST['usernotify'])){
365 $pass = auth_pwgen();
366 } else {
367 msg($this->lang['add_fail'], -1);
368 return false;
371 } else {
372 if (!empty($pass)){
373 msg($this->lang['add_fail'], -1);
374 return false;
378 if ($this->_auth->canDo('modName')){
379 if (empty($name)){
380 msg($this->lang['add_fail'], -1);
381 return false;
383 } else {
384 if (!empty($name)){
385 return false;
389 if ($this->_auth->canDo('modMail')){
390 if (empty($mail)){
391 msg($this->lang['add_fail'], -1);
392 return false;
394 } else {
395 if (!empty($mail)){
396 return false;
400 if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
402 msg($this->lang['add_ok'], 1);
404 if (!empty($_REQUEST['usernotify']) && $pass) {
405 $this->_notifyUser($user,$pass);
407 } else {
408 msg($this->lang['add_fail'], -1);
411 return $ok;
415 * Delete user
417 function _deleteUser(){
418 global $conf;
420 if (!checkSecurityToken()) return false;
421 if (!$this->_auth->canDo('delUser')) return false;
423 $selected = $_REQUEST['delete'];
424 if (!is_array($selected) || empty($selected)) return false;
425 $selected = array_keys($selected);
427 if(in_array($_SERVER['REMOTE_USER'], $selected)) {
428 msg("You can't delete yourself!", -1);
429 return false;
432 $count = $this->_auth->triggerUserMod('delete', array($selected));
433 if ($count == count($selected)) {
434 $text = str_replace('%d', $count, $this->lang['delete_ok']);
435 msg("$text.", 1);
436 } else {
437 $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
438 $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
439 msg("$part1, $part2",-1);
442 // invalidate all sessions
443 io_saveFile($conf['cachedir'].'/sessionpurge',time());
445 return true;
449 * Edit user (a user has been selected for editing)
451 function _editUser($param) {
452 if (!checkSecurityToken()) return false;
453 if (!$this->_auth->canDo('UserMod')) return false;
455 $user = cleanID(preg_replace('/.*:/','',$param));
456 $userdata = $this->_auth->getUserData($user);
458 // no user found?
459 if (!$userdata) {
460 msg($this->lang['edit_usermissing'],-1);
461 return false;
464 $this->_edit_user = $user;
465 $this->_edit_userdata = $userdata;
467 return true;
471 * Modify user (modified user data has been recieved)
473 function _modifyUser(){
474 global $conf;
476 if (!checkSecurityToken()) return false;
477 if (!$this->_auth->canDo('UserMod')) return false;
479 // get currently valid user data
480 $olduser = cleanID(preg_replace('/.*:/','',$_REQUEST['userid_old']));
481 $oldinfo = $this->_auth->getUserData($olduser);
483 // get new user data subject to change
484 list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser();
485 if (empty($newuser)) return false;
487 $changes = array();
488 if ($newuser != $olduser) {
490 if (!$this->_auth->canDo('modLogin')) { // sanity check, shouldn't be possible
491 msg($this->lang['update_fail'],-1);
492 return false;
495 // check if $newuser already exists
496 if ($this->_auth->getUserData($newuser)) {
497 msg(sprintf($this->lang['update_exists'],$newuser),-1);
498 $re_edit = true;
499 } else {
500 $changes['user'] = $newuser;
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;
513 if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
514 msg($this->lang['update_ok'],1);
516 if (!empty($_REQUEST['usernotify']) && $newpass) {
517 $notify = empty($changes['user']) ? $olduser : $newuser;
518 $this->_notifyUser($notify,$newpass);
521 // invalidate all sessions
522 io_saveFile($conf['cachedir'].'/sessionpurge',time());
524 } else {
525 msg($this->lang['update_fail'],-1);
528 if (!empty($re_edit)) {
529 $this->_editUser($olduser);
532 return $ok;
536 * send password change notification email
538 function _notifyUser($user, $password) {
540 if ($sent = auth_sendPassword($user,$password)) {
541 msg($this->lang['notify_ok'], 1);
542 } else {
543 msg($this->lang['notify_fail'], -1);
546 return $sent;
550 * retrieve & clean user data from the form
552 * @return array(user, password, full name, email, array(groups))
554 function _retrieveUser($clean=true) {
556 $user[0] = ($clean) ? cleanID(preg_replace('/.*:/','',$_REQUEST['userid'])) : $_REQUEST['userid'];
557 $user[1] = $_REQUEST['userpass'];
558 $user[2] = $_REQUEST['username'];
559 $user[3] = $_REQUEST['usermail'];
560 $user[4] = preg_split('/\s*,\s*/',$_REQUEST['usergroups'],-1,PREG_SPLIT_NO_EMPTY);
562 if (empty($user[4]) || (is_array($user[4]) && (count($user[4]) == 1) && (trim($user[4][0]) == ''))) {
563 $user[4] = null;
566 return $user;
569 function _setFilter($op) {
571 $this->_filter = array();
573 if ($op == 'new') {
574 list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
576 if (!empty($user)) $this->_filter['user'] = $user;
577 if (!empty($name)) $this->_filter['name'] = $name;
578 if (!empty($mail)) $this->_filter['mail'] = $mail;
579 if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
583 function _retrieveFilter() {
585 $t_filter = $_REQUEST['filter'];
586 if (!is_array($t_filter)) return array();
588 // messy, but this way we ensure we aren't getting any additional crap from malicious users
589 $filter = array();
591 if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
592 if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
593 if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
594 if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
596 return $filter;
599 function _validatePagination() {
601 if ($this->_start >= $this->_user_total) {
602 $this->_start = $this->_user_total - $this->_pagesize;
604 if ($this->_start < 0) $this->_start = 0;
606 $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
610 * return an array of strings to enable/disable pagination buttons
612 function _pagination() {
614 $disabled = 'disabled="disabled"';
616 $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
618 if ($this->_user_total == -1) {
619 $buttons['last'] = $disabled;
620 $buttons['next'] = '';
621 } else {
622 $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
625 return $buttons;