1<?php 2 3use dokuwiki\Extension\AdminPlugin; 4use dokuwiki\Extension\AuthPlugin; 5use dokuwiki\Utf8\Clean; 6 7/* 8 * User Manager 9 * 10 * Dokuwiki Admin Plugin 11 * 12 * This version of the user manager has been modified to only work with 13 * objectified version of auth system 14 * 15 * @author neolao <neolao@neolao.com> 16 * @author Chris Smith <chris@jalakai.co.uk> 17 */ 18 19/** 20 * All DokuWiki plugins to extend the admin function 21 * need to inherit from this class 22 */ 23class admin_plugin_usermanager extends AdminPlugin 24{ 25 protected const IMAGE_DIR = DOKU_BASE . 'lib/plugins/usermanager/images/'; 26 27 protected $auth; // auth object 28 protected $users_total = 0; // number of registered users 29 protected $filter = []; // user selection filter(s) 30 protected $start = 0; // index of first user to be displayed 31 protected $last = 0; // index of the last user to be displayed 32 protected $pagesize = 20; // number of users to list on one page 33 protected $edit_user = ''; // set to user selected for editing 34 protected $edit_userdata = []; 35 protected $disabled = ''; // if disabled set to explanatory string 36 protected $import_failures = []; 37 protected $lastdisabled = false; // set to true if last user is unknown and last button is hence buggy 38 39 /** 40 * Constructor 41 */ 42 public function __construct() 43 { 44 /** @var AuthPlugin $auth */ 45 global $auth; 46 47 $this->setupLocale(); 48 49 if (!isset($auth)) { 50 $this->disabled = $this->lang['noauth']; 51 } elseif (!$auth->canDo('getUsers')) { 52 $this->disabled = $this->lang['nosupport']; 53 } else { 54 // we're good to go 55 $this->auth = &$auth; 56 } 57 58 // attempt to retrieve any import failures from the session 59 if (!empty($_SESSION['import_failures'])) { 60 $this->import_failures = $_SESSION['import_failures']; 61 } 62 } 63 64 /** 65 * Return prompt for admin menu 66 * 67 * @param string $language 68 * @return string 69 */ 70 public function getMenuText($language) 71 { 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 * @return int 83 */ 84 public function getMenuSort() 85 { 86 return 2; 87 } 88 89 /** 90 * @return int current start value for pageination 91 */ 92 public function getStart() 93 { 94 return $this->start; 95 } 96 97 /** 98 * @return int number of users per page 99 */ 100 public function getPagesize() 101 { 102 return $this->pagesize; 103 } 104 105 /** 106 * @param boolean $lastdisabled 107 */ 108 public function setLastdisabled($lastdisabled) 109 { 110 $this->lastdisabled = $lastdisabled; 111 } 112 113 /** 114 * Handle user request 115 * 116 * @return bool 117 */ 118 public function handle() 119 { 120 global $INPUT; 121 if (is_null($this->auth)) return false; 122 123 // extract the command and any specific parameters 124 // submit button name is of the form - fn[cmd][param(s)] 125 $fn = $INPUT->param('fn'); 126 127 if (is_array($fn)) { 128 $cmd = key($fn); 129 $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 130 } else { 131 $cmd = $fn; 132 $param = null; 133 } 134 135 if ($cmd != "search") { 136 $this->start = $INPUT->int('start', 0); 137 $this->filter = $this->retrieveFilter(); 138 } 139 140 switch ($cmd) { 141 case "add": 142 $this->addUser(); 143 break; 144 case "delete": 145 $this->deleteUser(); 146 break; 147 case "modify": 148 $this->modifyUser(); 149 break; 150 case "edit": 151 $this->editUser($param); 152 break; 153 case "search": 154 $this->setFilter($param); 155 $this->start = 0; 156 break; 157 case "export": 158 $this->exportCSV(); 159 break; 160 case "import": 161 $this->importCSV(); 162 break; 163 case "importfails": 164 $this->downloadImportFailures(); 165 break; 166 } 167 168 $this->users_total = $this->auth->canDo('getUserCount') ? $this->auth->getUserCount($this->filter) : -1; 169 170 // page handling 171 switch ($cmd) { 172 case 'start': 173 $this->start = 0; 174 break; 175 case 'prev': 176 $this->start -= $this->pagesize; 177 break; 178 case 'next': 179 $this->start += $this->pagesize; 180 break; 181 case 'last': 182 $this->start = $this->users_total; 183 break; 184 } 185 $this->validatePagination(); 186 return true; 187 } 188 189 /** 190 * Output appropriate html 191 * 192 * @return bool 193 * @todo split into smaller functions, use Form class 194 */ 195 public function html() 196 { 197 global $ID; 198 199 if (is_null($this->auth)) { 200 print $this->lang['badauth']; 201 return false; 202 } 203 204 $user_list = $this->auth->retrieveUsers($this->start, $this->pagesize, $this->filter); 205 206 $page_buttons = $this->pagination(); 207 $delete_disable = $this->auth->canDo('delUser') ? '' : 'disabled="disabled"'; 208 209 $editable = $this->auth->canDo('UserMod'); 210 $export_label = empty($this->filter) ? $this->lang['export_all'] : $this->lang['export_filtered']; 211 212 print $this->locale_xhtml('intro'); 213 print $this->locale_xhtml('list'); 214 215 echo '<div id="user__manager">'; 216 echo '<div class="level2">'; 217 218 if ($this->users_total > 0) { 219 printf( 220 '<p>' . $this->lang['summary'] . '</p>', 221 $this->start + 1, 222 $this->last, 223 $this->users_total, 224 $this->auth->getUserCount() 225 ); 226 } else { 227 if ($this->users_total < 0) { 228 $allUserTotal = 0; 229 } else { 230 $allUserTotal = $this->auth->getUserCount(); 231 } 232 printf('<p>%s</p>', sprintf($this->lang['nonefound'], $allUserTotal)); 233 } 234 printf('<form action="%s" method="post">', wl($ID)); 235 formSecurityToken(); 236 echo '<div class="table">'; 237 echo '<table class="inline">'; 238 echo '<thead>'; 239 echo '<tr>'; 240 echo '<th> </th>'; 241 echo '<th>' . $this->lang["user_id"] . '</th>'; 242 echo '<th>' . $this->lang["user_name"] . '</th>'; 243 echo '<th>' . $this->lang["user_mail"] . '</th>'; 244 echo '<th>' . $this->lang["user_groups"] . '</th>'; 245 echo '</tr>'; 246 247 echo '<tr>'; 248 echo '<td class="rightalign"><input type="image" src="' . 249 self::IMAGE_DIR . 'search.png" name="fn[search][new]" title="' . 250 $this->lang['search_prompt'] . '" alt="' . $this->lang['search'] . '" class="button" /></td>'; 251 echo '<td><input type="text" name="userid" class="edit" value="' . $this->htmlFilter('user') . '" /></td>'; 252 echo '<td><input type="text" name="username" class="edit" value="' . $this->htmlFilter('name') . '" /></td>'; 253 echo '<td><input type="text" name="usermail" class="edit" value="' . $this->htmlFilter('mail') . '" /></td>'; 254 echo '<td><input type="text" name="usergroups" class="edit" value="' . $this->htmlFilter('grps') . '" /></td>'; 255 echo '</tr>'; 256 echo '</thead>'; 257 258 if ($this->users_total) { 259 echo '<tbody>'; 260 foreach ($user_list as $user => $userinfo) { 261 extract($userinfo); 262 /** 263 * @var string $name 264 * @var string $pass 265 * @var string $mail 266 * @var array $grps 267 */ 268 $groups = implode(', ', $grps); 269 echo '<tr class="user_info">'; 270 echo '<td class="centeralign"><input type="checkbox" name="delete[' . hsc($user) . 271 ']" ' . $delete_disable . ' /></td>'; 272 if ($editable) { 273 echo '<td><a href="' . wl($ID, ['fn[edit][' . $user . ']' => 1, 274 'do' => 'admin', 275 'page' => 'usermanager', 276 'sectok' => getSecurityToken()]) . 277 '" title="' . $this->lang['edit_prompt'] . '">' . hsc($user) . '</a></td>'; 278 } else { 279 echo '<td>' . hsc($user) . '</td>'; 280 } 281 echo '<td>' . hsc($name) . '</td><td>' . hsc($mail) . '</td><td>' . hsc($groups) . '</td>'; 282 echo '</tr>'; 283 } 284 echo '</tbody>'; 285 } 286 287 echo '<tbody>'; 288 echo '<tr><td colspan="5" class="centeralign">'; 289 echo '<span class="medialeft">'; 290 echo '<button type="submit" name="fn[delete]" id="usrmgr__del" ' . $delete_disable . '>' . 291 $this->lang['delete_selected'] . '</button>'; 292 echo '</span>'; 293 echo '<span class="mediaright">'; 294 echo '<button type="submit" name="fn[start]" ' . $page_buttons['start'] . '>' . 295 $this->lang['start'] . '</button>'; 296 echo '<button type="submit" name="fn[prev]" ' . $page_buttons['prev'] . '>' . 297 $this->lang['prev'] . "</button>"; 298 echo '<button type="submit" name="fn[next]" ' . $page_buttons['next'] . '>' . 299 $this->lang['next'] . '</button>'; 300 echo '<button type="submit" name="fn[last]" ' . $page_buttons['last'] . '>' . 301 $this->lang['last'] . '</button>'; 302 echo '</span>'; 303 if (!empty($this->filter)) { 304 echo '<button type="submit" name="fn[search][clear]">' . $this->lang['clear'] . '</button>'; 305 } 306 echo '<button type="submit" name="fn[export]">' . $export_label . '</button>'; 307 echo '<input type="hidden" name="do" value="admin" />'; 308 echo '<input type="hidden" name="page" value="usermanager" />'; 309 310 $this->htmlFilterSettings(2); 311 312 echo '</td></tr>'; 313 echo '</tbody>'; 314 echo '</table>'; 315 echo '</div>'; 316 317 echo '</form>'; 318 echo '</div>'; 319 320 $style = $this->edit_user ? ' class="edit_user"' : ''; 321 322 if ($this->auth->canDo('addUser')) { 323 echo '<div' . $style . '>'; 324 print $this->locale_xhtml('add'); 325 echo '<div class="level2">'; 326 327 $this->htmlUserForm('add', null, [], 4); 328 329 echo '</div>'; 330 echo '</div>'; 331 } 332 333 if ($this->edit_user && $this->auth->canDo('UserMod')) { 334 echo '<div' . $style . ' id="scroll__here">'; 335 print $this->locale_xhtml('edit'); 336 echo '<div class="level2">'; 337 338 $this->htmlUserForm('modify', $this->edit_user, $this->edit_userdata, 4); 339 340 echo '</div>'; 341 echo '</div>'; 342 } 343 344 if ($this->auth->canDo('addUser')) { 345 $this->htmlImportForm(); 346 } 347 echo '</div>'; 348 return true; 349 } 350 351 /** 352 * User Manager is only available if the auth backend supports it 353 * 354 * @inheritdoc 355 * @return bool 356 */ 357 public function isAccessibleByCurrentUser() 358 { 359 /** @var AuthPlugin $auth */ 360 global $auth; 361 if (!$auth || !$auth->canDo('getUsers')) { 362 return false; 363 } 364 365 return parent::isAccessibleByCurrentUser(); 366 } 367 368 369 /** 370 * Display form to add or modify a user 371 * 372 * @param string $cmd 'add' or 'modify' 373 * @param string $user id of user 374 * @param array $userdata array with name, mail, pass and grps 375 * @param int $indent 376 * @todo use Form class 377 */ 378 protected function htmlUserForm($cmd, $user = '', $userdata = [], $indent = 0) 379 { 380 global $conf; 381 global $ID; 382 global $lang; 383 $name = ''; 384 $mail = ''; 385 $groups = ''; 386 $notes = []; 387 388 if ($user) { 389 extract($userdata); 390 if (!empty($grps)) $groups = implode(',', $grps); 391 } else { 392 $notes[] = sprintf($this->lang['note_group'], $conf['defaultgroup']); 393 } 394 395 printf('<form action="%s" method="post">', wl($ID)); 396 formSecurityToken(); 397 echo '<div class="table">'; 398 echo '<table class="inline">'; 399 echo '<thead>'; 400 echo '<tr><th>' . $this->lang["field"] . "</th><th>" . $this->lang["value"] . "</th></tr>"; 401 echo '</thead>'; 402 echo '<tbody>'; 403 404 $this->htmlInputField( 405 $cmd . "_userid", 406 "userid", 407 $this->lang["user_id"], 408 $user, 409 $this->auth->canDo("modLogin"), 410 true, 411 $indent + 6 412 ); 413 $this->htmlInputField( 414 $cmd . "_userpass", 415 "userpass", 416 $this->lang["user_pass"], 417 "", 418 $this->auth->canDo("modPass"), 419 false, 420 $indent + 6 421 ); 422 $this->htmlInputField( 423 $cmd . "_userpass2", 424 "userpass2", 425 $lang["passchk"], 426 "", 427 $this->auth->canDo("modPass"), 428 false, 429 $indent + 6 430 ); 431 $this->htmlInputField( 432 $cmd . "_username", 433 "username", 434 $this->lang["user_name"], 435 $name, 436 $this->auth->canDo("modName"), 437 true, 438 $indent + 6 439 ); 440 $this->htmlInputField( 441 $cmd . "_usermail", 442 "usermail", 443 $this->lang["user_mail"], 444 $mail, 445 $this->auth->canDo("modMail"), 446 true, 447 $indent + 6 448 ); 449 $this->htmlInputField( 450 $cmd . "_usergroups", 451 "usergroups", 452 $this->lang["user_groups"], 453 $groups, 454 $this->auth->canDo("modGroups"), 455 false, 456 $indent + 6 457 ); 458 459 if ($this->auth->canDo("modPass")) { 460 if ($cmd == 'add') { 461 $notes[] = $this->lang['note_pass']; 462 } 463 if ($user) { 464 $notes[] = $this->lang['note_notify']; 465 } 466 467 echo '<tr><td><label for="' . $cmd . "_usernotify\" >" . 468 $this->lang["user_notify"] . ': </label></td> 469 <td><input type="checkbox" id="' . $cmd . '_usernotify" name="usernotify" value="1" /> 470 </td></tr>'; 471 } 472 473 echo '</tbody>'; 474 echo '<tbody>'; 475 echo '<tr>'; 476 echo '<td colspan="2">'; 477 echo '<input type="hidden" name="do" value="admin" />'; 478 echo '<input type="hidden" name="page" value="usermanager" />'; 479 480 // save current $user, we need this to access details if the name is changed 481 if ($user) { 482 echo '<input type="hidden" name="userid_old" value="' . hsc($user) . "\" />"; 483 } 484 485 $this->htmlFilterSettings($indent + 10); 486 487 echo '<button type="submit" name="fn[' . $cmd . ']">' . $this->lang[$cmd] . '</button>'; 488 echo '</td>'; 489 echo '</tr>'; 490 echo '</tbody>'; 491 echo '</table>'; 492 493 if ($notes) { 494 echo '<ul class="notes">'; 495 foreach ($notes as $note) { 496 echo '<li><span class="li">' . $note . '</li>'; 497 } 498 echo '</ul>'; 499 } 500 echo '</div>'; 501 echo '</form>'; 502 } 503 504 /** 505 * Prints a inputfield 506 * 507 * @param string $id 508 * @param string $name 509 * @param string $label 510 * @param string $value 511 * @param bool $cando whether auth backend is capable to do this action 512 * @param bool $required is this field required? 513 * @param int $indent 514 * @todo obsolete when Form class is used 515 */ 516 protected function htmlInputField($id, $name, $label, $value, $cando, $required, $indent = 0) 517 { 518 $class = $cando ? '' : ' class="disabled"'; 519 echo str_pad('', $indent); 520 521 if ($name == 'userpass' || $name == 'userpass2') { 522 $fieldtype = 'password'; 523 $autocomp = 'autocomplete="off"'; 524 } elseif ($name == 'usermail') { 525 $fieldtype = 'email'; 526 $autocomp = ''; 527 } else { 528 $fieldtype = 'text'; 529 $autocomp = ''; 530 } 531 $value = hsc($value); 532 533 echo "<tr $class>"; 534 echo "<td><label for=\"$id\" >$label: </label></td>"; 535 echo '<td>'; 536 if ($cando) { 537 $req = ''; 538 if ($required) $req = 'required="required"'; 539 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" 540 value=\"$value\" class=\"edit\" $autocomp $req />"; 541 } else { 542 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; 543 echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" 544 value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />"; 545 } 546 echo '</td>'; 547 echo '</tr>'; 548 } 549 550 /** 551 * Returns htmlescaped filter value 552 * 553 * @param string $key name of search field 554 * @return string html escaped value 555 */ 556 protected function htmlFilter($key) 557 { 558 if (empty($this->filter)) return ''; 559 return (isset($this->filter[$key]) ? hsc($this->filter[$key]) : ''); 560 } 561 562 /** 563 * Print hidden inputs with the current filter values 564 * 565 * @param int $indent 566 */ 567 protected function htmlFilterSettings($indent = 0) 568 { 569 570 echo '<input type="hidden" name="start" value="' . $this->start . '" />'; 571 572 foreach ($this->filter as $key => $filter) { 573 echo '<input type="hidden" name="filter[' . $key . ']" value="' . hsc($filter) . '" />'; 574 } 575 } 576 577 /** 578 * Print import form and summary of previous import 579 * 580 * @param int $indent 581 */ 582 protected function htmlImportForm($indent = 0) 583 { 584 global $ID; 585 586 $failure_download_link = wl($ID, ['do' => 'admin', 'page' => 'usermanager', 'fn[importfails]' => 1]); 587 588 echo '<div class="level2 import_users">'; 589 print $this->locale_xhtml('import'); 590 echo '<form action="' . wl($ID) . '" method="post" enctype="multipart/form-data">'; 591 formSecurityToken(); 592 echo '<label>' . $this->lang['import_userlistcsv'] . '<input type="file" name="import" /></label>'; 593 echo '<button type="submit" name="fn[import]">' . $this->lang['import'] . '</button>'; 594 echo '<input type="hidden" name="do" value="admin" />'; 595 echo '<input type="hidden" name="page" value="usermanager" />'; 596 597 $this->htmlFilterSettings($indent + 4); 598 echo '</form>'; 599 echo '</div>'; 600 601 // list failures from the previous import 602 if ($this->import_failures) { 603 $digits = strlen(count($this->import_failures)); 604 echo '<div class="level3 import_failures">'; 605 echo '<h3>' . $this->lang['import_header'] . '</h3>'; 606 echo '<table class="import_failures">'; 607 echo '<thead>'; 608 echo '<tr>'; 609 echo '<th class="line">' . $this->lang['line'] . '</th>'; 610 echo '<th class="error">' . $this->lang['error'] . '</th>'; 611 echo '<th class="userid">' . $this->lang['user_id'] . '</th>'; 612 echo '<th class="username">' . $this->lang['user_name'] . '</th>'; 613 echo '<th class="usermail">' . $this->lang['user_mail'] . '</th>'; 614 echo '<th class="usergroups">' . $this->lang['user_groups'] . '</th>'; 615 echo '</tr>'; 616 echo '</thead>'; 617 echo '<tbody>'; 618 foreach ($this->import_failures as $line => $failure) { 619 echo '<tr>'; 620 echo '<td class="lineno"> ' . sprintf('%0' . $digits . 'd', $line) . ' </td>'; 621 echo '<td class="error">' . $failure['error'] . ' </td>'; 622 echo '<td class="field userid"> ' . hsc($failure['user'][0]) . ' </td>'; 623 echo '<td class="field username"> ' . hsc($failure['user'][2]) . ' </td>'; 624 echo '<td class="field usermail"> ' . hsc($failure['user'][3]) . ' </td>'; 625 echo '<td class="field usergroups"> ' . hsc($failure['user'][4]) . ' </td>'; 626 echo '</tr>'; 627 } 628 echo '</tbody>'; 629 echo '</table>'; 630 echo '<p><a href="' . $failure_download_link . '">' . $this->lang['import_downloadfailures'] . '</a></p>'; 631 echo '</div>'; 632 } 633 } 634 635 /** 636 * Add an user to auth backend 637 * 638 * @return bool whether succesful 639 */ 640 protected function addUser() 641 { 642 global $INPUT; 643 if (!checkSecurityToken()) return false; 644 if (!$this->auth->canDo('addUser')) return false; 645 646 [$user, $pass, $name, $mail, $grps, $passconfirm] = $this->retrieveUser(); 647 if (empty($user)) return false; 648 649 if ($this->auth->canDo('modPass')) { 650 if (empty($pass)) { 651 if ($INPUT->has('usernotify')) { 652 $pass = auth_pwgen($user); 653 } else { 654 msg($this->lang['add_fail'], -1); 655 msg($this->lang['addUser_error_missing_pass'], -1); 656 return false; 657 } 658 } elseif (!$this->verifyPassword($pass, $passconfirm)) { 659 msg($this->lang['add_fail'], -1); 660 msg($this->lang['addUser_error_pass_not_identical'], -1); 661 return false; 662 } 663 } elseif (!empty($pass)) { 664 msg($this->lang['add_fail'], -1); 665 msg($this->lang['addUser_error_modPass_disabled'], -1); 666 return false; 667 } 668 669 if ($this->auth->canDo('modName')) { 670 if (empty($name)) { 671 msg($this->lang['add_fail'], -1); 672 msg($this->lang['addUser_error_name_missing'], -1); 673 return false; 674 } 675 } elseif (!empty($name)) { 676 msg($this->lang['add_fail'], -1); 677 msg($this->lang['addUser_error_modName_disabled'], -1); 678 return false; 679 } 680 681 if ($this->auth->canDo('modMail')) { 682 if (empty($mail)) { 683 msg($this->lang['add_fail'], -1); 684 msg($this->lang['addUser_error_mail_missing'], -1); 685 return false; 686 } 687 } elseif (!empty($mail)) { 688 msg($this->lang['add_fail'], -1); 689 msg($this->lang['addUser_error_modMail_disabled'], -1); 690 return false; 691 } 692 693 if ($ok = $this->auth->triggerUserMod('create', [$user, $pass, $name, $mail, $grps])) { 694 msg($this->lang['add_ok'], 1); 695 696 if ($INPUT->has('usernotify') && $pass) { 697 $this->notifyUser($user, $pass); 698 } 699 } else { 700 msg($this->lang['add_fail'], -1); 701 msg($this->lang['addUser_error_create_event_failed'], -1); 702 } 703 704 return $ok; 705 } 706 707 /** 708 * Delete user from auth backend 709 * 710 * @return bool whether succesful 711 */ 712 protected function deleteUser() 713 { 714 global $conf, $INPUT; 715 716 if (!checkSecurityToken()) return false; 717 if (!$this->auth->canDo('delUser')) return false; 718 719 $selected = $INPUT->arr('delete'); 720 if (empty($selected)) return false; 721 $selected = array_keys($selected); 722 723 if (in_array($_SERVER['REMOTE_USER'], $selected)) { 724 msg("You can't delete yourself!", -1); 725 return false; 726 } 727 728 $count = $this->auth->triggerUserMod('delete', [$selected]); 729 if ($count == count($selected)) { 730 $text = str_replace('%d', $count, $this->lang['delete_ok']); 731 msg("$text.", 1); 732 } else { 733 $part1 = str_replace('%d', $count, $this->lang['delete_ok']); 734 $part2 = str_replace('%d', (count($selected) - $count), $this->lang['delete_fail']); 735 msg("$part1, $part2", -1); 736 } 737 738 // invalidate all sessions 739 io_saveFile($conf['cachedir'] . '/sessionpurge', time()); 740 741 return true; 742 } 743 744 /** 745 * Edit user (a user has been selected for editing) 746 * 747 * @param string $param id of the user 748 * @return bool whether succesful 749 */ 750 protected function editUser($param) 751 { 752 if (!checkSecurityToken()) return false; 753 if (!$this->auth->canDo('UserMod')) return false; 754 $user = $this->auth->cleanUser(preg_replace('/.*[:\/]/', '', $param)); 755 $userdata = $this->auth->getUserData($user); 756 757 // no user found? 758 if (!$userdata) { 759 msg($this->lang['edit_usermissing'], -1); 760 return false; 761 } 762 763 $this->edit_user = $user; 764 $this->edit_userdata = $userdata; 765 766 return true; 767 } 768 769 /** 770 * Modify user in the auth backend (modified user data has been recieved) 771 * 772 * @return bool whether succesful 773 */ 774 protected function modifyUser() 775 { 776 global $conf, $INPUT; 777 778 if (!checkSecurityToken()) return false; 779 if (!$this->auth->canDo('UserMod')) return false; 780 781 // get currently valid user data 782 $olduser = $this->auth->cleanUser(preg_replace('/.*[:\/]/', '', $INPUT->str('userid_old'))); 783 $oldinfo = $this->auth->getUserData($olduser); 784 785 // get new user data subject to change 786 [$newuser, $newpass, $newname, $newmail, $newgrps, $passconfirm] = $this->retrieveUser(); 787 if (empty($newuser)) return false; 788 789 $changes = []; 790 if ($newuser != $olduser) { 791 if (!$this->auth->canDo('modLogin')) { // sanity check, shouldn't be possible 792 msg($this->lang['update_fail'], -1); 793 return false; 794 } 795 796 // check if $newuser already exists 797 if ($this->auth->getUserData($newuser)) { 798 msg(sprintf($this->lang['update_exists'], $newuser), -1); 799 $re_edit = true; 800 } else { 801 $changes['user'] = $newuser; 802 } 803 } 804 if ($this->auth->canDo('modPass')) { 805 if ($newpass || $passconfirm) { 806 if ($this->verifyPassword($newpass, $passconfirm)) { 807 $changes['pass'] = $newpass; 808 } else { 809 return false; 810 } 811 } elseif ($INPUT->has('usernotify')) { 812 // no new password supplied, check if we need to generate one (or it stays unchanged) 813 $changes['pass'] = auth_pwgen($olduser); 814 } 815 } 816 817 if (!empty($newname) && $this->auth->canDo('modName') && $newname != $oldinfo['name']) { 818 $changes['name'] = $newname; 819 } 820 if (!empty($newmail) && $this->auth->canDo('modMail') && $newmail != $oldinfo['mail']) { 821 $changes['mail'] = $newmail; 822 } 823 if (!empty($newgrps) && $this->auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) { 824 $changes['grps'] = $newgrps; 825 } 826 827 if ($ok = $this->auth->triggerUserMod('modify', [$olduser, $changes])) { 828 msg($this->lang['update_ok'], 1); 829 830 if ($INPUT->has('usernotify') && !empty($changes['pass'])) { 831 $notify = empty($changes['user']) ? $olduser : $newuser; 832 $this->notifyUser($notify, $changes['pass']); 833 } 834 835 // invalidate all sessions 836 io_saveFile($conf['cachedir'] . '/sessionpurge', time()); 837 } else { 838 msg($this->lang['update_fail'], -1); 839 } 840 841 if (!empty($re_edit)) { 842 $this->editUser($olduser); 843 } 844 845 return $ok; 846 } 847 848 /** 849 * Send password change notification email 850 * 851 * @param string $user id of user 852 * @param string $password plain text 853 * @param bool $status_alert whether status alert should be shown 854 * @return bool whether succesful 855 */ 856 protected function notifyUser($user, $password, $status_alert = true) 857 { 858 859 if ($sent = auth_sendPassword($user, $password)) { 860 if ($status_alert) { 861 msg($this->lang['notify_ok'], 1); 862 } 863 } elseif ($status_alert) { 864 msg($this->lang['notify_fail'], -1); 865 } 866 867 return $sent; 868 } 869 870 /** 871 * Verify password meets minimum requirements 872 * :TODO: extend to support password strength 873 * 874 * @param string $password candidate string for new password 875 * @param string $confirm repeated password for confirmation 876 * @return bool true if meets requirements, false otherwise 877 */ 878 protected function verifyPassword($password, $confirm) 879 { 880 global $lang; 881 882 if (empty($password) && empty($confirm)) { 883 return false; 884 } 885 886 if ($password !== $confirm) { 887 msg($lang['regbadpass'], -1); 888 return false; 889 } 890 891 // :TODO: test password for required strength 892 893 // if we make it this far the password is good 894 return true; 895 } 896 897 /** 898 * Retrieve & clean user data from the form 899 * 900 * @param bool $clean whether the cleanUser method of the authentication backend is applied 901 * @return array (user, password, full name, email, array(groups)) 902 */ 903 protected function retrieveUser($clean = true) 904 { 905 /** @var AuthPlugin $auth */ 906 global $auth; 907 global $INPUT; 908 909 $user = []; 910 $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid'); 911 $user[1] = $INPUT->str('userpass'); 912 $user[2] = $INPUT->str('username'); 913 $user[3] = $INPUT->str('usermail'); 914 $user[4] = explode(',', $INPUT->str('usergroups')); 915 $user[5] = $INPUT->str('userpass2'); // repeated password for confirmation 916 917 $user[4] = array_map('trim', $user[4]); 918 if ($clean) $user[4] = array_map([$auth, 'cleanGroup'], $user[4]); 919 $user[4] = array_filter($user[4]); 920 $user[4] = array_unique($user[4]); 921 if ($user[4] === []) $user[4] = null; 922 923 return $user; 924 } 925 926 /** 927 * Set the filter with the current search terms or clear the filter 928 * 929 * @param string $op 'new' or 'clear' 930 */ 931 protected function setFilter($op) 932 { 933 934 $this->filter = []; 935 936 if ($op == 'new') { 937 [$user, /* pass */, $name, $mail, $grps] = $this->retrieveUser(false); 938 939 if (!empty($user)) $this->filter['user'] = $user; 940 if (!empty($name)) $this->filter['name'] = $name; 941 if (!empty($mail)) $this->filter['mail'] = $mail; 942 if (!empty($grps)) $this->filter['grps'] = implode('|', $grps); 943 } 944 } 945 946 /** 947 * Get the current search terms 948 * 949 * @return array 950 */ 951 protected function retrieveFilter() 952 { 953 global $INPUT; 954 955 $t_filter = $INPUT->arr('filter'); 956 957 // messy, but this way we ensure we aren't getting any additional crap from malicious users 958 $filter = []; 959 960 if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; 961 if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; 962 if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; 963 if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; 964 965 return $filter; 966 } 967 968 /** 969 * Validate and improve the pagination values 970 */ 971 protected function validatePagination() 972 { 973 974 if ($this->start >= $this->users_total) { 975 $this->start = $this->users_total - $this->pagesize; 976 } 977 if ($this->start < 0) $this->start = 0; 978 979 $this->last = min($this->users_total, $this->start + $this->pagesize); 980 } 981 982 /** 983 * Return an array of strings to enable/disable pagination buttons 984 * 985 * @return array with enable/disable attributes 986 */ 987 protected function pagination() 988 { 989 990 $disabled = 'disabled="disabled"'; 991 992 $buttons = []; 993 $buttons['start'] = $buttons['prev'] = ($this->start == 0) ? $disabled : ''; 994 995 if ($this->users_total == -1) { 996 $buttons['last'] = $disabled; 997 $buttons['next'] = ''; 998 } else { 999 $buttons['last'] = $buttons['next'] = 1000 (($this->start + $this->pagesize) >= $this->users_total) ? $disabled : ''; 1001 } 1002 1003 if ($this->lastdisabled) { 1004 $buttons['last'] = $disabled; 1005 } 1006 1007 return $buttons; 1008 } 1009 1010 /** 1011 * Export a list of users in csv format using the current filter criteria 1012 */ 1013 protected function exportCSV() 1014 { 1015 // list of users for export - based on current filter criteria 1016 $user_list = $this->auth->retrieveUsers(0, 0, $this->filter); 1017 $column_headings = [ 1018 $this->lang["user_id"], 1019 $this->lang["user_name"], 1020 $this->lang["user_mail"], 1021 $this->lang["user_groups"] 1022 ]; 1023 1024 // ============================================================================================== 1025 // GENERATE OUTPUT 1026 // normal headers for downloading... 1027 header('Content-type: text/csv;charset=utf-8'); 1028 header('Content-Disposition: attachment; filename="wikiusers.csv"'); 1029# // for debugging assistance, send as text plain to the browser 1030# header('Content-type: text/plain;charset=utf-8'); 1031 1032 // output the csv 1033 $fd = fopen('php://output', 'w'); 1034 fputcsv($fd, $column_headings); 1035 foreach ($user_list as $user => $info) { 1036 $line = [$user, $info['name'], $info['mail'], implode(',', $info['grps'])]; 1037 fputcsv($fd, $line); 1038 } 1039 fclose($fd); 1040 if (defined('DOKU_UNITTEST')) { 1041 return; 1042 } 1043 1044 die; 1045 } 1046 1047 /** 1048 * Import a file of users in csv format 1049 * 1050 * csv file should have 4 columns, user_id, full name, email, groups (comma separated) 1051 * 1052 * @return bool whether successful 1053 */ 1054 protected function importCSV() 1055 { 1056 // check we are allowed to add users 1057 if (!checkSecurityToken()) return false; 1058 if (!$this->auth->canDo('addUser')) return false; 1059 1060 // check file uploaded ok. 1061 if ( 1062 empty($_FILES['import']['size']) || 1063 !empty($_FILES['import']['error']) && $this->isUploadedFile($_FILES['import']['tmp_name']) 1064 ) { 1065 msg($this->lang['import_error_upload'], -1); 1066 return false; 1067 } 1068 // retrieve users from the file 1069 $this->import_failures = []; 1070 $import_success_count = 0; 1071 $import_fail_count = 0; 1072 $line = 0; 1073 $fd = fopen($_FILES['import']['tmp_name'], 'r'); 1074 if ($fd) { 1075 while ($csv = fgets($fd)) { 1076 if (!Clean::isUtf8($csv)) { 1077 $csv = utf8_encode($csv); 1078 } 1079 $raw = str_getcsv($csv); 1080 $error = ''; // clean out any errors from the previous line 1081 // data checks... 1082 if (1 == ++$line) { 1083 if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue; // skip headers 1084 } 1085 if (count($raw) < 4) { // need at least four fields 1086 $import_fail_count++; 1087 $error = sprintf($this->lang['import_error_fields'], count($raw)); 1088 $this->import_failures[$line] = ['error' => $error, 'user' => $raw, 'orig' => $csv]; 1089 continue; 1090 } 1091 array_splice($raw, 1, 0, auth_pwgen()); // splice in a generated password 1092 $clean = $this->cleanImportUser($raw, $error); 1093 if ($clean && $this->importUser($clean, $error)) { 1094 $sent = $this->notifyUser($clean[0], $clean[1], false); 1095 if (!$sent) { 1096 msg(sprintf($this->lang['import_notify_fail'], $clean[0], $clean[3]), -1); 1097 } 1098 $import_success_count++; 1099 } else { 1100 $import_fail_count++; 1101 array_splice($raw, 1, 1); // remove the spliced in password 1102 $this->import_failures[$line] = ['error' => $error, 'user' => $raw, 'orig' => $csv]; 1103 } 1104 } 1105 msg( 1106 sprintf( 1107 $this->lang['import_success_count'], 1108 ($import_success_count + $import_fail_count), 1109 $import_success_count 1110 ), 1111 ($import_success_count ? 1 : -1) 1112 ); 1113 if ($import_fail_count) { 1114 msg(sprintf($this->lang['import_failure_count'], $import_fail_count), -1); 1115 } 1116 } else { 1117 msg($this->lang['import_error_readfail'], -1); 1118 } 1119 1120 // save import failures into the session 1121 if (!headers_sent()) { 1122 session_start(); 1123 $_SESSION['import_failures'] = $this->import_failures; 1124 session_write_close(); 1125 } 1126 return true; 1127 } 1128 1129 /** 1130 * Returns cleaned user data 1131 * 1132 * @param array $candidate raw values of line from input file 1133 * @param string $error 1134 * @return array|false cleaned data or false 1135 */ 1136 protected function cleanImportUser($candidate, &$error) 1137 { 1138 global $INPUT; 1139 1140 // FIXME kludgy .... 1141 $INPUT->set('userid', $candidate[0]); 1142 $INPUT->set('userpass', $candidate[1]); 1143 $INPUT->set('username', $candidate[2]); 1144 $INPUT->set('usermail', $candidate[3]); 1145 $INPUT->set('usergroups', $candidate[4]); 1146 1147 $cleaned = $this->retrieveUser(); 1148 [$user, /* pass */, $name, $mail, /* grps */] = $cleaned; 1149 if (empty($user)) { 1150 $error = $this->lang['import_error_baduserid']; 1151 return false; 1152 } 1153 1154 // no need to check password, handled elsewhere 1155 1156 if (!($this->auth->canDo('modName') xor empty($name))) { 1157 $error = $this->lang['import_error_badname']; 1158 return false; 1159 } 1160 1161 if ($this->auth->canDo('modMail')) { 1162 if (empty($mail) || !mail_isvalid($mail)) { 1163 $error = $this->lang['import_error_badmail']; 1164 return false; 1165 } 1166 } elseif (!empty($mail)) { 1167 $error = $this->lang['import_error_badmail']; 1168 return false; 1169 } 1170 1171 return $cleaned; 1172 } 1173 1174 /** 1175 * Adds imported user to auth backend 1176 * 1177 * Required a check of canDo('addUser') before 1178 * 1179 * @param array $user data of user 1180 * @param string &$error reference catched error message 1181 * @return bool whether successful 1182 */ 1183 protected function importUser($user, &$error) 1184 { 1185 if (!$this->auth->triggerUserMod('create', $user)) { 1186 $error = $this->lang['import_error_create']; 1187 return false; 1188 } 1189 1190 return true; 1191 } 1192 1193 /** 1194 * Downloads failures as csv file 1195 */ 1196 protected function downloadImportFailures() 1197 { 1198 1199 // ============================================================================================== 1200 // GENERATE OUTPUT 1201 // normal headers for downloading... 1202 header('Content-type: text/csv;charset=utf-8'); 1203 header('Content-Disposition: attachment; filename="importfails.csv"'); 1204# // for debugging assistance, send as text plain to the browser 1205# header('Content-type: text/plain;charset=utf-8'); 1206 1207 // output the csv 1208 $fd = fopen('php://output', 'w'); 1209 foreach ($this->import_failures as $fail) { 1210 fwrite($fd, $fail['orig']); 1211 } 1212 fclose($fd); 1213 die; 1214 } 1215 1216 /** 1217 * wrapper for is_uploaded_file to facilitate overriding by test suite 1218 * 1219 * @param string $file filename 1220 * @return bool 1221 */ 1222 protected function isUploadedFile($file) 1223 { 1224 return is_uploaded_file($file); 1225 } 1226} 1227