10440ff15Schris<?php 20440ff15Schris/* 30440ff15Schris * User Manager 40440ff15Schris * 50440ff15Schris * Dokuwiki Admin Plugin 60440ff15Schris * 70440ff15Schris * This version of the user manager has been modified to only work with 80440ff15Schris * objectified version of auth system 90440ff15Schris * 100440ff15Schris * @author neolao <neolao@neolao.com> 110440ff15Schris * @author Chris Smith <chris@jalakai.co.uk> 120440ff15Schris */ 13e04f1f16Schris// must be run within Dokuwiki 14e04f1f16Schrisif(!defined('DOKU_INC')) die(); 15e04f1f16Schris 160440ff15Schrisif(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/'); 170440ff15Schris 180440ff15Schris/** 190440ff15Schris * All DokuWiki plugins to extend the admin function 200440ff15Schris * need to inherit from this class 210440ff15Schris */ 220440ff15Schrisclass admin_plugin_usermanager extends DokuWiki_Admin_Plugin { 230440ff15Schris 243712ca9aSGerrit Uitslag protected $_auth = null; // auth object 253712ca9aSGerrit Uitslag protected $_user_total = 0; // number of registered users 263712ca9aSGerrit Uitslag protected $_filter = array(); // user selection filter(s) 273712ca9aSGerrit Uitslag protected $_start = 0; // index of first user to be displayed 283712ca9aSGerrit Uitslag protected $_last = 0; // index of the last user to be displayed 293712ca9aSGerrit Uitslag protected $_pagesize = 20; // number of users to list on one page 303712ca9aSGerrit Uitslag protected $_edit_user = ''; // set to user selected for editing 313712ca9aSGerrit Uitslag protected $_edit_userdata = array(); 323712ca9aSGerrit Uitslag protected $_disabled = ''; // if disabled set to explanatory string 333712ca9aSGerrit Uitslag protected $_import_failures = array(); 34*462e9e37SMichael Große protected $_lastdisabled = false; // set to true if last user is unknown and last button is hence buggy 350440ff15Schris 360440ff15Schris /** 370440ff15Schris * Constructor 380440ff15Schris */ 39c5a7c0c6SGerrit Uitslag public function admin_plugin_usermanager(){ 40c5a7c0c6SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 410440ff15Schris global $auth; 420440ff15Schris 430440ff15Schris $this->setupLocale(); 4451d94d49Schris 4551d94d49Schris if (!isset($auth)) { 46c5a7c0c6SGerrit Uitslag $this->_disabled = $this->lang['noauth']; 4782fd59b6SAndreas Gohr } else if (!$auth->canDo('getUsers')) { 48c5a7c0c6SGerrit Uitslag $this->_disabled = $this->lang['nosupport']; 4951d94d49Schris } else { 5051d94d49Schris 5151d94d49Schris // we're good to go 5251d94d49Schris $this->_auth = & $auth; 5351d94d49Schris 5451d94d49Schris } 55ae1afd2fSChristopher Smith 56ae1afd2fSChristopher Smith // attempt to retrieve any import failures from the session 570e80bb5eSChristopher Smith if (!empty($_SESSION['import_failures'])){ 58ae1afd2fSChristopher Smith $this->_import_failures = $_SESSION['import_failures']; 59ae1afd2fSChristopher Smith } 600440ff15Schris } 610440ff15Schris 620440ff15Schris /** 63c5a7c0c6SGerrit Uitslag * Return prompt for admin menu 64253d4b48SGerrit Uitslag * 65253d4b48SGerrit Uitslag * @param string $language 66253d4b48SGerrit Uitslag * @return string 670440ff15Schris */ 68c5a7c0c6SGerrit Uitslag public function getMenuText($language) { 690440ff15Schris 700440ff15Schris if (!is_null($this->_auth)) 710440ff15Schris return parent::getMenuText($language); 720440ff15Schris 73c5a7c0c6SGerrit Uitslag return $this->getLang('menu').' '.$this->_disabled; 740440ff15Schris } 750440ff15Schris 760440ff15Schris /** 770440ff15Schris * return sort order for position in admin menu 78253d4b48SGerrit Uitslag * 79253d4b48SGerrit Uitslag * @return int 800440ff15Schris */ 81c5a7c0c6SGerrit Uitslag public function getMenuSort() { 820440ff15Schris return 2; 830440ff15Schris } 840440ff15Schris 850440ff15Schris /** 8667a31a83SMichael Große * @return int current start value for pageination 8767a31a83SMichael Große */ 8867a31a83SMichael Große public function getStart() { 8967a31a83SMichael Große return $this->_start; 9067a31a83SMichael Große } 9167a31a83SMichael Große 9267a31a83SMichael Große /** 9367a31a83SMichael Große * @return int number of users per page 9467a31a83SMichael Große */ 9567a31a83SMichael Große public function getPagesize() { 9667a31a83SMichael Große return $this->_pagesize; 9767a31a83SMichael Große } 9867a31a83SMichael Große 9967a31a83SMichael Große /** 100*462e9e37SMichael Große * @param boolean $lastdisabled 101*462e9e37SMichael Große */ 102*462e9e37SMichael Große public function setLastdisabled($lastdisabled) { 103*462e9e37SMichael Große $this->_lastdisabled = $lastdisabled; 104*462e9e37SMichael Große } 105*462e9e37SMichael Große 106*462e9e37SMichael Große /** 107c5a7c0c6SGerrit Uitslag * Handle user request 108253d4b48SGerrit Uitslag * 109253d4b48SGerrit Uitslag * @return bool 1100440ff15Schris */ 111c5a7c0c6SGerrit Uitslag public function handle() { 11200d58927SMichael Hamann global $INPUT; 1130440ff15Schris if (is_null($this->_auth)) return false; 1140440ff15Schris 1150440ff15Schris // extract the command and any specific parameters 1160440ff15Schris // submit button name is of the form - fn[cmd][param(s)] 11700d58927SMichael Hamann $fn = $INPUT->param('fn'); 1180440ff15Schris 1190440ff15Schris if (is_array($fn)) { 1200440ff15Schris $cmd = key($fn); 1210440ff15Schris $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; 1220440ff15Schris } else { 1230440ff15Schris $cmd = $fn; 1240440ff15Schris $param = null; 1250440ff15Schris } 1260440ff15Schris 1270440ff15Schris if ($cmd != "search") { 12800d58927SMichael Hamann $this->_start = $INPUT->int('start', 0); 1290440ff15Schris $this->_filter = $this->_retrieveFilter(); 1300440ff15Schris } 1310440ff15Schris 1320440ff15Schris switch($cmd){ 1330440ff15Schris case "add" : $this->_addUser(); break; 1340440ff15Schris case "delete" : $this->_deleteUser(); break; 1350440ff15Schris case "modify" : $this->_modifyUser(); break; 13678c7c8c9Schris case "edit" : $this->_editUser($param); break; 1370440ff15Schris case "search" : $this->_setFilter($param); 1380440ff15Schris $this->_start = 0; 1390440ff15Schris break; 1405c967d3dSChristopher Smith case "export" : $this->_export(); break; 141ae1afd2fSChristopher Smith case "import" : $this->_import(); break; 142ae1afd2fSChristopher Smith case "importfails" : $this->_downloadImportFailures(); break; 1430440ff15Schris } 1440440ff15Schris 14551d94d49Schris $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1; 1460440ff15Schris 1470440ff15Schris // page handling 1480440ff15Schris switch($cmd){ 1490440ff15Schris case 'start' : $this->_start = 0; break; 1500440ff15Schris case 'prev' : $this->_start -= $this->_pagesize; break; 1510440ff15Schris case 'next' : $this->_start += $this->_pagesize; break; 1520440ff15Schris case 'last' : $this->_start = $this->_user_total; break; 1530440ff15Schris } 1540440ff15Schris $this->_validatePagination(); 155c5a7c0c6SGerrit Uitslag return true; 1560440ff15Schris } 1570440ff15Schris 1580440ff15Schris /** 159c5a7c0c6SGerrit Uitslag * Output appropriate html 160253d4b48SGerrit Uitslag * 161253d4b48SGerrit Uitslag * @return bool 1620440ff15Schris */ 163c5a7c0c6SGerrit Uitslag public function html() { 1640440ff15Schris global $ID; 1650440ff15Schris 1660440ff15Schris if(is_null($this->_auth)) { 1670440ff15Schris print $this->lang['badauth']; 1680440ff15Schris return false; 1690440ff15Schris } 1700440ff15Schris 1710440ff15Schris $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); 1720440ff15Schris 1730440ff15Schris $page_buttons = $this->_pagination(); 17482fd59b6SAndreas Gohr $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"'; 1750440ff15Schris 17677d19185SAndreas Gohr $editable = $this->_auth->canDo('UserMod'); 177b59cff8bSGerrit Uitslag $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang['export_filtered']; 1786154103cSmatthiasgrimm 1790440ff15Schris print $this->locale_xhtml('intro'); 1800440ff15Schris print $this->locale_xhtml('list'); 1810440ff15Schris 18258dde80dSAnika Henke ptln("<div id=\"user__manager\">"); 18358dde80dSAnika Henke ptln("<div class=\"level2\">"); 1840440ff15Schris 18567019d15Schris if ($this->_user_total > 0) { 1860440ff15Schris ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>"); 1870440ff15Schris } else { 188a102b175SGerrit Uitslag if($this->_user_total < 0) { 189a102b175SGerrit Uitslag $allUserTotal = 0; 190a102b175SGerrit Uitslag } else { 191a102b175SGerrit Uitslag $allUserTotal = $this->_auth->getUserCount(); 192a102b175SGerrit Uitslag } 193a102b175SGerrit Uitslag ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>"); 1940440ff15Schris } 1950440ff15Schris ptln("<form action=\"".wl($ID)."\" method=\"post\">"); 196634d7150SAndreas Gohr formSecurityToken(); 197c7b28ffdSAnika Henke ptln(" <div class=\"table\">"); 1980440ff15Schris ptln(" <table class=\"inline\">"); 1990440ff15Schris ptln(" <thead>"); 2000440ff15Schris ptln(" <tr>"); 201e260f93bSAnika Henke 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>"); 2020440ff15Schris ptln(" </tr>"); 2030440ff15Schris 2040440ff15Schris ptln(" <tr>"); 2052365d73dSAnika Henke 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>"); 206a2c0246eSAnika Henke ptln(" <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>"); 207a2c0246eSAnika Henke ptln(" <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>"); 208a2c0246eSAnika Henke ptln(" <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>"); 209a2c0246eSAnika Henke ptln(" <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>"); 2100440ff15Schris ptln(" </tr>"); 2110440ff15Schris ptln(" </thead>"); 2120440ff15Schris 2130440ff15Schris if ($this->_user_total) { 2140440ff15Schris ptln(" <tbody>"); 2150440ff15Schris foreach ($user_list as $user => $userinfo) { 2160440ff15Schris extract($userinfo); 217c5a7c0c6SGerrit Uitslag /** 218c5a7c0c6SGerrit Uitslag * @var string $name 219c5a7c0c6SGerrit Uitslag * @var string $pass 220c5a7c0c6SGerrit Uitslag * @var string $mail 221c5a7c0c6SGerrit Uitslag * @var array $grps 222c5a7c0c6SGerrit Uitslag */ 2230440ff15Schris $groups = join(', ',$grps); 224a2c0246eSAnika Henke ptln(" <tr class=\"user_info\">"); 2250440ff15Schris ptln(" <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>"); 2262365d73dSAnika Henke if ($editable) { 22777d19185SAndreas Gohr ptln(" <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1, 22877d19185SAndreas Gohr 'do' => 'admin', 22977d19185SAndreas Gohr 'page' => 'usermanager', 23077d19185SAndreas Gohr 'sectok' => getSecurityToken())). 23177d19185SAndreas Gohr "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>"); 2322365d73dSAnika Henke } else { 2332365d73dSAnika Henke ptln(" <td>".hsc($user)."</td>"); 2342365d73dSAnika Henke } 2352365d73dSAnika Henke ptln(" <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>"); 2360440ff15Schris ptln(" </tr>"); 2370440ff15Schris } 2380440ff15Schris ptln(" </tbody>"); 2390440ff15Schris } 2400440ff15Schris 2410440ff15Schris ptln(" <tbody>"); 2422365d73dSAnika Henke ptln(" <tr><td colspan=\"5\" class=\"centeralign\">"); 243a2c0246eSAnika Henke ptln(" <span class=\"medialeft\">"); 244a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />"); 2450440ff15Schris ptln(" </span>"); 246a2c0246eSAnika Henke ptln(" <span class=\"mediaright\">"); 247a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />"); 248a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />"); 249a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />"); 250a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />"); 2510440ff15Schris ptln(" </span>"); 2525c967d3dSChristopher Smith if (!empty($this->_filter)) { 253a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />"); 2545c967d3dSChristopher Smith } 2555c967d3dSChristopher Smith ptln(" <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />"); 2565164d9c9SAnika Henke ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />"); 2575164d9c9SAnika Henke ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />"); 258daf4ca4eSAnika Henke 259daf4ca4eSAnika Henke $this->_htmlFilterSettings(2); 260daf4ca4eSAnika Henke 2610440ff15Schris ptln(" </td></tr>"); 2620440ff15Schris ptln(" </tbody>"); 2630440ff15Schris ptln(" </table>"); 264c7b28ffdSAnika Henke ptln(" </div>"); 2650440ff15Schris 2660440ff15Schris ptln("</form>"); 2670440ff15Schris ptln("</div>"); 2680440ff15Schris 269a2c0246eSAnika Henke $style = $this->_edit_user ? " class=\"edit_user\"" : ""; 2700440ff15Schris 27182fd59b6SAndreas Gohr if ($this->_auth->canDo('addUser')) { 2720440ff15Schris ptln("<div".$style.">"); 2730440ff15Schris print $this->locale_xhtml('add'); 2740440ff15Schris ptln(" <div class=\"level2\">"); 2750440ff15Schris 27678c7c8c9Schris $this->_htmlUserForm('add',null,array(),4); 2770440ff15Schris 2780440ff15Schris ptln(" </div>"); 2790440ff15Schris ptln("</div>"); 2800440ff15Schris } 2810440ff15Schris 28282fd59b6SAndreas Gohr if($this->_edit_user && $this->_auth->canDo('UserMod')){ 283c632fc69SAndreas Gohr ptln("<div".$style." id=\"scroll__here\">"); 2840440ff15Schris print $this->locale_xhtml('edit'); 2850440ff15Schris ptln(" <div class=\"level2\">"); 2860440ff15Schris 28778c7c8c9Schris $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4); 2880440ff15Schris 2890440ff15Schris ptln(" </div>"); 2900440ff15Schris ptln("</div>"); 2910440ff15Schris } 292ae1afd2fSChristopher Smith 293ae1afd2fSChristopher Smith if ($this->_auth->canDo('addUser')) { 294ae1afd2fSChristopher Smith $this->_htmlImportForm(); 295ae1afd2fSChristopher Smith } 29658dde80dSAnika Henke ptln("</div>"); 297c5a7c0c6SGerrit Uitslag return true; 2980440ff15Schris } 2990440ff15Schris 30082fd59b6SAndreas Gohr /** 301c5a7c0c6SGerrit Uitslag * Display form to add or modify a user 302c5a7c0c6SGerrit Uitslag * 303c5a7c0c6SGerrit Uitslag * @param string $cmd 'add' or 'modify' 304c5a7c0c6SGerrit Uitslag * @param string $user id of user 305c5a7c0c6SGerrit Uitslag * @param array $userdata array with name, mail, pass and grps 306c5a7c0c6SGerrit Uitslag * @param int $indent 30782fd59b6SAndreas Gohr */ 3083712ca9aSGerrit Uitslag protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) { 309a6858c6aSchris global $conf; 310bb4866bdSchris global $ID; 311be9008d3SChristopher Smith global $lang; 31278c7c8c9Schris 31378c7c8c9Schris $name = $mail = $groups = ''; 314a6858c6aSchris $notes = array(); 3150440ff15Schris 3160440ff15Schris if ($user) { 31778c7c8c9Schris extract($userdata); 31878c7c8c9Schris if (!empty($grps)) $groups = join(',',$grps); 319a6858c6aSchris } else { 320a6858c6aSchris $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']); 3210440ff15Schris } 3220440ff15Schris 3230440ff15Schris ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent); 324634d7150SAndreas Gohr formSecurityToken(); 325c7b28ffdSAnika Henke ptln(" <div class=\"table\">",$indent); 3260440ff15Schris ptln(" <table class=\"inline\">",$indent); 3270440ff15Schris ptln(" <thead>",$indent); 3280440ff15Schris ptln(" <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent); 3290440ff15Schris ptln(" </thead>",$indent); 3300440ff15Schris ptln(" <tbody>",$indent); 33126fb387bSchris 33226fb387bSchris $this->_htmlInputField($cmd."_userid", "userid", $this->lang["user_id"], $user, $this->_auth->canDo("modLogin"), $indent+6); 33326fb387bSchris $this->_htmlInputField($cmd."_userpass", "userpass", $this->lang["user_pass"], "", $this->_auth->canDo("modPass"), $indent+6); 334be9008d3SChristopher Smith $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"], "", $this->_auth->canDo("modPass"), $indent+6); 33526fb387bSchris $this->_htmlInputField($cmd."_username", "username", $this->lang["user_name"], $name, $this->_auth->canDo("modName"), $indent+6); 33626fb387bSchris $this->_htmlInputField($cmd."_usermail", "usermail", $this->lang["user_mail"], $mail, $this->_auth->canDo("modMail"), $indent+6); 33726fb387bSchris $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6); 33826fb387bSchris 339a6858c6aSchris if ($this->_auth->canDo("modPass")) { 340ee9498f5SChristopher Smith if ($cmd == 'add') { 341c3f4fb63SGina Haeussge $notes[] = $this->lang['note_pass']; 342ee9498f5SChristopher Smith } 343a6858c6aSchris if ($user) { 344a6858c6aSchris $notes[] = $this->lang['note_notify']; 345a6858c6aSchris } 346a6858c6aSchris 347a6858c6aSchris 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); 348a6858c6aSchris } 349a6858c6aSchris 3500440ff15Schris ptln(" </tbody>",$indent); 3510440ff15Schris ptln(" <tbody>",$indent); 3520440ff15Schris ptln(" <tr>",$indent); 3530440ff15Schris ptln(" <td colspan=\"2\">",$indent); 3540440ff15Schris ptln(" <input type=\"hidden\" name=\"do\" value=\"admin\" />",$indent); 3550440ff15Schris ptln(" <input type=\"hidden\" name=\"page\" value=\"usermanager\" />",$indent); 3560440ff15Schris 3570440ff15Schris // save current $user, we need this to access details if the name is changed 3580440ff15Schris if ($user) 3590440ff15Schris ptln(" <input type=\"hidden\" name=\"userid_old\" value=\"".$user."\" />",$indent); 3600440ff15Schris 3610440ff15Schris $this->_htmlFilterSettings($indent+10); 3620440ff15Schris 363a2c0246eSAnika Henke ptln(" <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent); 3640440ff15Schris ptln(" </td>",$indent); 3650440ff15Schris ptln(" </tr>",$indent); 3660440ff15Schris ptln(" </tbody>",$indent); 3670440ff15Schris ptln(" </table>",$indent); 36845c19902SChristopher Smith 36945c19902SChristopher Smith if ($notes) { 37045c19902SChristopher Smith ptln(" <ul class=\"notes\">"); 37145c19902SChristopher Smith foreach ($notes as $note) { 37245c19902SChristopher Smith ptln(" <li><span class=\"li\">".$note."</span></li>",$indent); 37345c19902SChristopher Smith } 37445c19902SChristopher Smith ptln(" </ul>"); 37545c19902SChristopher Smith } 376c7b28ffdSAnika Henke ptln(" </div>",$indent); 3770440ff15Schris ptln("</form>",$indent); 3780440ff15Schris } 3790440ff15Schris 380c5a7c0c6SGerrit Uitslag /** 381c5a7c0c6SGerrit Uitslag * Prints a inputfield 382c5a7c0c6SGerrit Uitslag * 383c5a7c0c6SGerrit Uitslag * @param string $id 384c5a7c0c6SGerrit Uitslag * @param string $name 385c5a7c0c6SGerrit Uitslag * @param string $label 386c5a7c0c6SGerrit Uitslag * @param string $value 387c5a7c0c6SGerrit Uitslag * @param bool $cando whether auth backend is capable to do this action 388c5a7c0c6SGerrit Uitslag * @param int $indent 389c5a7c0c6SGerrit Uitslag */ 3903712ca9aSGerrit Uitslag protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) { 3917de12fceSAndreas Gohr $class = $cando ? '' : ' class="disabled"'; 3927de12fceSAndreas Gohr echo str_pad('',$indent); 3937de12fceSAndreas Gohr 394359e9417SChristopher Smith if($name == 'userpass' || $name == 'userpass2'){ 395d796a891SAndreas Gohr $fieldtype = 'password'; 396d796a891SAndreas Gohr $autocomp = 'autocomplete="off"'; 3977b3674bdSChristopher Smith }elseif($name == 'usermail'){ 3987b3674bdSChristopher Smith $fieldtype = 'email'; 3997b3674bdSChristopher Smith $autocomp = ''; 400d796a891SAndreas Gohr }else{ 401d796a891SAndreas Gohr $fieldtype = 'text'; 402d796a891SAndreas Gohr $autocomp = ''; 403d796a891SAndreas Gohr } 404d796a891SAndreas Gohr 4057de12fceSAndreas Gohr echo "<tr $class>"; 4067de12fceSAndreas Gohr echo "<td><label for=\"$id\" >$label: </label></td>"; 4077de12fceSAndreas Gohr echo "<td>"; 4087de12fceSAndreas Gohr if($cando){ 409d796a891SAndreas Gohr echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />"; 4107de12fceSAndreas Gohr }else{ 4117de12fceSAndreas Gohr echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; 412ee54059bSTimo Voipio echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />"; 4137de12fceSAndreas Gohr } 4147de12fceSAndreas Gohr echo "</td>"; 4157de12fceSAndreas Gohr echo "</tr>"; 41626fb387bSchris } 41726fb387bSchris 418c5a7c0c6SGerrit Uitslag /** 419c5a7c0c6SGerrit Uitslag * Returns htmlescaped filter value 420c5a7c0c6SGerrit Uitslag * 421c5a7c0c6SGerrit Uitslag * @param string $key name of search field 422c5a7c0c6SGerrit Uitslag * @return string html escaped value 423c5a7c0c6SGerrit Uitslag */ 4243712ca9aSGerrit Uitslag protected function _htmlFilter($key) { 4250440ff15Schris if (empty($this->_filter)) return ''; 4260440ff15Schris return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : ''); 4270440ff15Schris } 4280440ff15Schris 429c5a7c0c6SGerrit Uitslag /** 430c5a7c0c6SGerrit Uitslag * Print hidden inputs with the current filter values 431c5a7c0c6SGerrit Uitslag * 432c5a7c0c6SGerrit Uitslag * @param int $indent 433c5a7c0c6SGerrit Uitslag */ 4343712ca9aSGerrit Uitslag protected function _htmlFilterSettings($indent=0) { 4350440ff15Schris 4360440ff15Schris ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent); 4370440ff15Schris 4380440ff15Schris foreach ($this->_filter as $key => $filter) { 4390440ff15Schris ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent); 4400440ff15Schris } 4410440ff15Schris } 4420440ff15Schris 443c5a7c0c6SGerrit Uitslag /** 444c5a7c0c6SGerrit Uitslag * Print import form and summary of previous import 445c5a7c0c6SGerrit Uitslag * 446c5a7c0c6SGerrit Uitslag * @param int $indent 447c5a7c0c6SGerrit Uitslag */ 4483712ca9aSGerrit Uitslag protected function _htmlImportForm($indent=0) { 449ae1afd2fSChristopher Smith global $ID; 450ae1afd2fSChristopher Smith 451ae1afd2fSChristopher Smith $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1)); 452ae1afd2fSChristopher Smith 453ae1afd2fSChristopher Smith ptln('<div class="level2 import_users">',$indent); 454ae1afd2fSChristopher Smith print $this->locale_xhtml('import'); 455ae1afd2fSChristopher Smith ptln(' <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent); 456ae1afd2fSChristopher Smith formSecurityToken(); 457b59cff8bSGerrit Uitslag ptln(' <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent); 458ae1afd2fSChristopher Smith ptln(' <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent); 459ae1afd2fSChristopher Smith ptln(' <input type="hidden" name="do" value="admin" />',$indent); 460ae1afd2fSChristopher Smith ptln(' <input type="hidden" name="page" value="usermanager" />',$indent); 461ae1afd2fSChristopher Smith 462ae1afd2fSChristopher Smith $this->_htmlFilterSettings($indent+4); 463ae1afd2fSChristopher Smith ptln(' </form>',$indent); 464ae1afd2fSChristopher Smith ptln('</div>'); 465ae1afd2fSChristopher Smith 466ae1afd2fSChristopher Smith // list failures from the previous import 467ae1afd2fSChristopher Smith if ($this->_import_failures) { 468ae1afd2fSChristopher Smith $digits = strlen(count($this->_import_failures)); 469ae1afd2fSChristopher Smith ptln('<div class="level3 import_failures">',$indent); 470b59cff8bSGerrit Uitslag ptln(' <h3>'.$this->lang['import_header'].'</h3>'); 471ae1afd2fSChristopher Smith ptln(' <table class="import_failures">',$indent); 472ae1afd2fSChristopher Smith ptln(' <thead>',$indent); 473ae1afd2fSChristopher Smith ptln(' <tr>',$indent); 474ae1afd2fSChristopher Smith ptln(' <th class="line">'.$this->lang['line'].'</th>',$indent); 475ae1afd2fSChristopher Smith ptln(' <th class="error">'.$this->lang['error'].'</th>',$indent); 476ae1afd2fSChristopher Smith ptln(' <th class="userid">'.$this->lang['user_id'].'</th>',$indent); 477ae1afd2fSChristopher Smith ptln(' <th class="username">'.$this->lang['user_name'].'</th>',$indent); 478ae1afd2fSChristopher Smith ptln(' <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent); 479ae1afd2fSChristopher Smith ptln(' <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent); 480ae1afd2fSChristopher Smith ptln(' </tr>',$indent); 481ae1afd2fSChristopher Smith ptln(' </thead>',$indent); 482ae1afd2fSChristopher Smith ptln(' <tbody>',$indent); 483ae1afd2fSChristopher Smith foreach ($this->_import_failures as $line => $failure) { 484ae1afd2fSChristopher Smith ptln(' <tr>',$indent); 485ae1afd2fSChristopher Smith ptln(' <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent); 486ae1afd2fSChristopher Smith ptln(' <td class="error">' .$failure['error'].' </td>', $indent); 487ae1afd2fSChristopher Smith ptln(' <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent); 488ae1afd2fSChristopher Smith ptln(' <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent); 489ae1afd2fSChristopher Smith ptln(' <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent); 490ae1afd2fSChristopher Smith ptln(' <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent); 491ae1afd2fSChristopher Smith ptln(' </tr>',$indent); 492ae1afd2fSChristopher Smith } 493ae1afd2fSChristopher Smith ptln(' </tbody>',$indent); 494ae1afd2fSChristopher Smith ptln(' </table>',$indent); 495b59cff8bSGerrit Uitslag ptln(' <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>'); 496ae1afd2fSChristopher Smith ptln('</div>'); 497ae1afd2fSChristopher Smith } 498ae1afd2fSChristopher Smith 499ae1afd2fSChristopher Smith } 500ae1afd2fSChristopher Smith 501c5a7c0c6SGerrit Uitslag /** 502c5a7c0c6SGerrit Uitslag * Add an user to auth backend 503c5a7c0c6SGerrit Uitslag * 504c5a7c0c6SGerrit Uitslag * @return bool whether succesful 505c5a7c0c6SGerrit Uitslag */ 5063712ca9aSGerrit Uitslag protected function _addUser(){ 50700d58927SMichael Hamann global $INPUT; 508634d7150SAndreas Gohr if (!checkSecurityToken()) return false; 50982fd59b6SAndreas Gohr if (!$this->_auth->canDo('addUser')) return false; 5100440ff15Schris 511359e9417SChristopher Smith list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser(); 5120440ff15Schris if (empty($user)) return false; 5136733c4d7SChris Smith 5146733c4d7SChris Smith if ($this->_auth->canDo('modPass')){ 515c3f4fb63SGina Haeussge if (empty($pass)){ 51600d58927SMichael Hamann if($INPUT->has('usernotify')){ 5178a285f7fSAndreas Gohr $pass = auth_pwgen($user); 518c3f4fb63SGina Haeussge } else { 51960b9901bSAndreas Gohr msg($this->lang['add_fail'], -1); 52060b9901bSAndreas Gohr return false; 52160b9901bSAndreas Gohr } 522359e9417SChristopher Smith } else { 523359e9417SChristopher Smith if (!$this->_verifyPassword($pass,$passconfirm)) { 524359e9417SChristopher Smith return false; 525359e9417SChristopher Smith } 5266733c4d7SChris Smith } 5276733c4d7SChris Smith } else { 5286733c4d7SChris Smith if (!empty($pass)){ 5296733c4d7SChris Smith msg($this->lang['add_fail'], -1); 5306733c4d7SChris Smith return false; 5316733c4d7SChris Smith } 5326733c4d7SChris Smith } 5336733c4d7SChris Smith 5346733c4d7SChris Smith if ($this->_auth->canDo('modName')){ 5356733c4d7SChris Smith if (empty($name)){ 5366733c4d7SChris Smith msg($this->lang['add_fail'], -1); 5376733c4d7SChris Smith return false; 5386733c4d7SChris Smith } 5396733c4d7SChris Smith } else { 5406733c4d7SChris Smith if (!empty($name)){ 5416733c4d7SChris Smith return false; 5426733c4d7SChris Smith } 5436733c4d7SChris Smith } 5446733c4d7SChris Smith 5456733c4d7SChris Smith if ($this->_auth->canDo('modMail')){ 5466733c4d7SChris Smith if (empty($mail)){ 5476733c4d7SChris Smith msg($this->lang['add_fail'], -1); 5486733c4d7SChris Smith return false; 5496733c4d7SChris Smith } 5506733c4d7SChris Smith } else { 5516733c4d7SChris Smith if (!empty($mail)){ 5526733c4d7SChris Smith return false; 5536733c4d7SChris Smith } 5546733c4d7SChris Smith } 5550440ff15Schris 5567d3c8d42SGabriel Birke if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) { 557a6858c6aSchris 558a6858c6aSchris msg($this->lang['add_ok'], 1); 559a6858c6aSchris 56000d58927SMichael Hamann if ($INPUT->has('usernotify') && $pass) { 561a6858c6aSchris $this->_notifyUser($user,$pass); 562a6858c6aSchris } 563a6858c6aSchris } else { 56460b9901bSAndreas Gohr msg($this->lang['add_fail'], -1); 565a6858c6aSchris } 566a6858c6aSchris 567a6858c6aSchris return $ok; 5680440ff15Schris } 5690440ff15Schris 5700440ff15Schris /** 571c5a7c0c6SGerrit Uitslag * Delete user from auth backend 572c5a7c0c6SGerrit Uitslag * 573c5a7c0c6SGerrit Uitslag * @return bool whether succesful 5740440ff15Schris */ 5753712ca9aSGerrit Uitslag protected function _deleteUser(){ 57600d58927SMichael Hamann global $conf, $INPUT; 5779ec82636SAndreas Gohr 578634d7150SAndreas Gohr if (!checkSecurityToken()) return false; 57982fd59b6SAndreas Gohr if (!$this->_auth->canDo('delUser')) return false; 5800440ff15Schris 58100d58927SMichael Hamann $selected = $INPUT->arr('delete'); 58200d58927SMichael Hamann if (empty($selected)) return false; 5830440ff15Schris $selected = array_keys($selected); 5840440ff15Schris 585c9a8f912SMichael Klier if(in_array($_SERVER['REMOTE_USER'], $selected)) { 586c9a8f912SMichael Klier msg("You can't delete yourself!", -1); 587c9a8f912SMichael Klier return false; 588c9a8f912SMichael Klier } 589c9a8f912SMichael Klier 5907d3c8d42SGabriel Birke $count = $this->_auth->triggerUserMod('delete', array($selected)); 5910440ff15Schris if ($count == count($selected)) { 5920440ff15Schris $text = str_replace('%d', $count, $this->lang['delete_ok']); 5930440ff15Schris msg("$text.", 1); 5940440ff15Schris } else { 5950440ff15Schris $part1 = str_replace('%d', $count, $this->lang['delete_ok']); 5960440ff15Schris $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']); 5970440ff15Schris msg("$part1, $part2",-1); 5980440ff15Schris } 59978c7c8c9Schris 6009ec82636SAndreas Gohr // invalidate all sessions 6019ec82636SAndreas Gohr io_saveFile($conf['cachedir'].'/sessionpurge',time()); 6029ec82636SAndreas Gohr 60378c7c8c9Schris return true; 60478c7c8c9Schris } 60578c7c8c9Schris 60678c7c8c9Schris /** 60778c7c8c9Schris * Edit user (a user has been selected for editing) 608c5a7c0c6SGerrit Uitslag * 609c5a7c0c6SGerrit Uitslag * @param string $param id of the user 610c5a7c0c6SGerrit Uitslag * @return bool whether succesful 61178c7c8c9Schris */ 6123712ca9aSGerrit Uitslag protected function _editUser($param) { 613634d7150SAndreas Gohr if (!checkSecurityToken()) return false; 61478c7c8c9Schris if (!$this->_auth->canDo('UserMod')) return false; 615786dfb0eSGerrit Uitslag $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param)); 61678c7c8c9Schris $userdata = $this->_auth->getUserData($user); 61778c7c8c9Schris 61878c7c8c9Schris // no user found? 61978c7c8c9Schris if (!$userdata) { 62078c7c8c9Schris msg($this->lang['edit_usermissing'],-1); 62178c7c8c9Schris return false; 62278c7c8c9Schris } 62378c7c8c9Schris 62478c7c8c9Schris $this->_edit_user = $user; 62578c7c8c9Schris $this->_edit_userdata = $userdata; 62678c7c8c9Schris 62778c7c8c9Schris return true; 6280440ff15Schris } 6290440ff15Schris 6300440ff15Schris /** 631c5a7c0c6SGerrit Uitslag * Modify user in the auth backend (modified user data has been recieved) 632c5a7c0c6SGerrit Uitslag * 633c5a7c0c6SGerrit Uitslag * @return bool whether succesful 6340440ff15Schris */ 6353712ca9aSGerrit Uitslag protected function _modifyUser(){ 63600d58927SMichael Hamann global $conf, $INPUT; 6379ec82636SAndreas Gohr 638634d7150SAndreas Gohr if (!checkSecurityToken()) return false; 63982fd59b6SAndreas Gohr if (!$this->_auth->canDo('UserMod')) return false; 6400440ff15Schris 64126fb387bSchris // get currently valid user data 642786dfb0eSGerrit Uitslag $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old'))); 643073766c6Smatthiasgrimm $oldinfo = $this->_auth->getUserData($olduser); 644073766c6Smatthiasgrimm 64526fb387bSchris // get new user data subject to change 646359e9417SChristopher Smith list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser(); 647073766c6Smatthiasgrimm if (empty($newuser)) return false; 6480440ff15Schris 6490440ff15Schris $changes = array(); 650073766c6Smatthiasgrimm if ($newuser != $olduser) { 65126fb387bSchris 65226fb387bSchris if (!$this->_auth->canDo('modLogin')) { // sanity check, shouldn't be possible 65326fb387bSchris msg($this->lang['update_fail'],-1); 65426fb387bSchris return false; 65526fb387bSchris } 65626fb387bSchris 65726fb387bSchris // check if $newuser already exists 658073766c6Smatthiasgrimm if ($this->_auth->getUserData($newuser)) { 659073766c6Smatthiasgrimm msg(sprintf($this->lang['update_exists'],$newuser),-1); 660a6858c6aSchris $re_edit = true; 6610440ff15Schris } else { 662073766c6Smatthiasgrimm $changes['user'] = $newuser; 6630440ff15Schris } 66493eefc2fSAndreas Gohr } 665359e9417SChristopher Smith if ($this->_auth->canDo('modPass')) { 6662400ddcbSChristopher Smith if ($newpass || $passconfirm) { 667359e9417SChristopher Smith if ($this->_verifyPassword($newpass,$passconfirm)) { 668359e9417SChristopher Smith $changes['pass'] = $newpass; 669359e9417SChristopher Smith } else { 670359e9417SChristopher Smith return false; 671359e9417SChristopher Smith } 672359e9417SChristopher Smith } else { 673359e9417SChristopher Smith // no new password supplied, check if we need to generate one (or it stays unchanged) 674359e9417SChristopher Smith if ($INPUT->has('usernotify')) { 675359e9417SChristopher Smith $changes['pass'] = auth_pwgen($olduser); 676359e9417SChristopher Smith } 677359e9417SChristopher Smith } 6780440ff15Schris } 6790440ff15Schris 68040d72af6SChristopher Smith if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) { 681073766c6Smatthiasgrimm $changes['name'] = $newname; 68240d72af6SChristopher Smith } 68340d72af6SChristopher Smith if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) { 684073766c6Smatthiasgrimm $changes['mail'] = $newmail; 68540d72af6SChristopher Smith } 68640d72af6SChristopher Smith if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) { 687073766c6Smatthiasgrimm $changes['grps'] = $newgrps; 68840d72af6SChristopher Smith } 6890440ff15Schris 6907d3c8d42SGabriel Birke if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) { 6910440ff15Schris msg($this->lang['update_ok'],1); 692a6858c6aSchris 6936ed3476bSChristopher Smith if ($INPUT->has('usernotify') && !empty($changes['pass'])) { 694a6858c6aSchris $notify = empty($changes['user']) ? $olduser : $newuser; 6956ed3476bSChristopher Smith $this->_notifyUser($notify,$changes['pass']); 696a6858c6aSchris } 697a6858c6aSchris 6989ec82636SAndreas Gohr // invalidate all sessions 6999ec82636SAndreas Gohr io_saveFile($conf['cachedir'].'/sessionpurge',time()); 7009ec82636SAndreas Gohr 7010440ff15Schris } else { 7020440ff15Schris msg($this->lang['update_fail'],-1); 7030440ff15Schris } 70478c7c8c9Schris 705a6858c6aSchris if (!empty($re_edit)) { 706a6858c6aSchris $this->_editUser($olduser); 7070440ff15Schris } 7080440ff15Schris 709a6858c6aSchris return $ok; 710a6858c6aSchris } 711a6858c6aSchris 712a6858c6aSchris /** 713c5a7c0c6SGerrit Uitslag * Send password change notification email 714c5a7c0c6SGerrit Uitslag * 715c5a7c0c6SGerrit Uitslag * @param string $user id of user 716c5a7c0c6SGerrit Uitslag * @param string $password plain text 717c5a7c0c6SGerrit Uitslag * @param bool $status_alert whether status alert should be shown 718c5a7c0c6SGerrit Uitslag * @return bool whether succesful 719a6858c6aSchris */ 7203712ca9aSGerrit Uitslag protected function _notifyUser($user, $password, $status_alert=true) { 721a6858c6aSchris 722a6858c6aSchris if ($sent = auth_sendPassword($user,$password)) { 723328143f8SChristopher Smith if ($status_alert) { 724a6858c6aSchris msg($this->lang['notify_ok'], 1); 725328143f8SChristopher Smith } 726a6858c6aSchris } else { 727328143f8SChristopher Smith if ($status_alert) { 728a6858c6aSchris msg($this->lang['notify_fail'], -1); 729a6858c6aSchris } 730328143f8SChristopher Smith } 731a6858c6aSchris 732a6858c6aSchris return $sent; 733a6858c6aSchris } 734a6858c6aSchris 735a6858c6aSchris /** 736359e9417SChristopher Smith * Verify password meets minimum requirements 737359e9417SChristopher Smith * :TODO: extend to support password strength 738359e9417SChristopher Smith * 739359e9417SChristopher Smith * @param string $password candidate string for new password 740359e9417SChristopher Smith * @param string $confirm repeated password for confirmation 741359e9417SChristopher Smith * @return bool true if meets requirements, false otherwise 742359e9417SChristopher Smith */ 743359e9417SChristopher Smith protected function _verifyPassword($password, $confirm) { 744be9008d3SChristopher Smith global $lang; 745359e9417SChristopher Smith 7462400ddcbSChristopher Smith if (empty($password) && empty($confirm)) { 747359e9417SChristopher Smith return false; 748359e9417SChristopher Smith } 749359e9417SChristopher Smith 750359e9417SChristopher Smith if ($password !== $confirm) { 751be9008d3SChristopher Smith msg($lang['regbadpass'], -1); 752359e9417SChristopher Smith return false; 753359e9417SChristopher Smith } 754359e9417SChristopher Smith 755359e9417SChristopher Smith // :TODO: test password for required strength 756359e9417SChristopher Smith 757359e9417SChristopher Smith // if we make it this far the password is good 758359e9417SChristopher Smith return true; 759359e9417SChristopher Smith } 760359e9417SChristopher Smith 761359e9417SChristopher Smith /** 762c5a7c0c6SGerrit Uitslag * Retrieve & clean user data from the form 763a6858c6aSchris * 764c5a7c0c6SGerrit Uitslag * @param bool $clean whether the cleanUser method of the authentication backend is applied 765a6858c6aSchris * @return array (user, password, full name, email, array(groups)) 7660440ff15Schris */ 7673712ca9aSGerrit Uitslag protected function _retrieveUser($clean=true) { 768c5a7c0c6SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 7697441e340SAndreas Gohr global $auth; 770fbfbbe8aSHakan Sandell global $INPUT; 7710440ff15Schris 77259bc3b48SGerrit Uitslag $user = array(); 773fbfbbe8aSHakan Sandell $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid'); 774fbfbbe8aSHakan Sandell $user[1] = $INPUT->str('userpass'); 775fbfbbe8aSHakan Sandell $user[2] = $INPUT->str('username'); 776fbfbbe8aSHakan Sandell $user[3] = $INPUT->str('usermail'); 777fbfbbe8aSHakan Sandell $user[4] = explode(',',$INPUT->str('usergroups')); 778359e9417SChristopher Smith $user[5] = $INPUT->str('userpass2'); // repeated password for confirmation 7790440ff15Schris 7807441e340SAndreas Gohr $user[4] = array_map('trim',$user[4]); 7817441e340SAndreas Gohr if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]); 7827441e340SAndreas Gohr $user[4] = array_filter($user[4]); 7837441e340SAndreas Gohr $user[4] = array_unique($user[4]); 7847441e340SAndreas Gohr if(!count($user[4])) $user[4] = null; 7850440ff15Schris 7860440ff15Schris return $user; 7870440ff15Schris } 7880440ff15Schris 789c5a7c0c6SGerrit Uitslag /** 790c5a7c0c6SGerrit Uitslag * Set the filter with the current search terms or clear the filter 791c5a7c0c6SGerrit Uitslag * 792c5a7c0c6SGerrit Uitslag * @param string $op 'new' or 'clear' 793c5a7c0c6SGerrit Uitslag */ 7943712ca9aSGerrit Uitslag protected function _setFilter($op) { 7950440ff15Schris 7960440ff15Schris $this->_filter = array(); 7970440ff15Schris 7980440ff15Schris if ($op == 'new') { 79959bc3b48SGerrit Uitslag list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false); 8000440ff15Schris 8010440ff15Schris if (!empty($user)) $this->_filter['user'] = $user; 8020440ff15Schris if (!empty($name)) $this->_filter['name'] = $name; 8030440ff15Schris if (!empty($mail)) $this->_filter['mail'] = $mail; 8040440ff15Schris if (!empty($grps)) $this->_filter['grps'] = join('|',$grps); 8050440ff15Schris } 8060440ff15Schris } 8070440ff15Schris 808c5a7c0c6SGerrit Uitslag /** 809c5a7c0c6SGerrit Uitslag * Get the current search terms 810c5a7c0c6SGerrit Uitslag * 811c5a7c0c6SGerrit Uitslag * @return array 812c5a7c0c6SGerrit Uitslag */ 8133712ca9aSGerrit Uitslag protected function _retrieveFilter() { 814fbfbbe8aSHakan Sandell global $INPUT; 8150440ff15Schris 816fbfbbe8aSHakan Sandell $t_filter = $INPUT->arr('filter'); 8170440ff15Schris 8180440ff15Schris // messy, but this way we ensure we aren't getting any additional crap from malicious users 8190440ff15Schris $filter = array(); 8200440ff15Schris 8210440ff15Schris if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; 8220440ff15Schris if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; 8230440ff15Schris if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; 8240440ff15Schris if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; 8250440ff15Schris 8260440ff15Schris return $filter; 8270440ff15Schris } 8280440ff15Schris 829c5a7c0c6SGerrit Uitslag /** 830c5a7c0c6SGerrit Uitslag * Validate and improve the pagination values 831c5a7c0c6SGerrit Uitslag */ 8323712ca9aSGerrit Uitslag protected function _validatePagination() { 8330440ff15Schris 8340440ff15Schris if ($this->_start >= $this->_user_total) { 8350440ff15Schris $this->_start = $this->_user_total - $this->_pagesize; 8360440ff15Schris } 8370440ff15Schris if ($this->_start < 0) $this->_start = 0; 8380440ff15Schris 8390440ff15Schris $this->_last = min($this->_user_total, $this->_start + $this->_pagesize); 8400440ff15Schris } 8410440ff15Schris 842c5a7c0c6SGerrit Uitslag /** 843c5a7c0c6SGerrit Uitslag * Return an array of strings to enable/disable pagination buttons 844c5a7c0c6SGerrit Uitslag * 845c5a7c0c6SGerrit Uitslag * @return array with enable/disable attributes 8460440ff15Schris */ 8473712ca9aSGerrit Uitslag protected function _pagination() { 8480440ff15Schris 84951d94d49Schris $disabled = 'disabled="disabled"'; 85051d94d49Schris 85159bc3b48SGerrit Uitslag $buttons = array(); 85251d94d49Schris $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : ''; 85351d94d49Schris 85451d94d49Schris if ($this->_user_total == -1) { 85551d94d49Schris $buttons['last'] = $disabled; 85651d94d49Schris $buttons['next'] = ''; 85751d94d49Schris } else { 85851d94d49Schris $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : ''; 85951d94d49Schris } 8600440ff15Schris 861*462e9e37SMichael Große if ($this->_lastdisabled) { 862*462e9e37SMichael Große $buttons['last'] = $disabled; 863*462e9e37SMichael Große } 864*462e9e37SMichael Große 8650440ff15Schris return $buttons; 8660440ff15Schris } 8675c967d3dSChristopher Smith 868c5a7c0c6SGerrit Uitslag /** 869c5a7c0c6SGerrit Uitslag * Export a list of users in csv format using the current filter criteria 8705c967d3dSChristopher Smith */ 8713712ca9aSGerrit Uitslag protected function _export() { 8725c967d3dSChristopher Smith // list of users for export - based on current filter criteria 8735c967d3dSChristopher Smith $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter); 8745c967d3dSChristopher Smith $column_headings = array( 8755c967d3dSChristopher Smith $this->lang["user_id"], 8765c967d3dSChristopher Smith $this->lang["user_name"], 8775c967d3dSChristopher Smith $this->lang["user_mail"], 8785c967d3dSChristopher Smith $this->lang["user_groups"] 8795c967d3dSChristopher Smith ); 8805c967d3dSChristopher Smith 8815c967d3dSChristopher Smith // ============================================================================================== 8825c967d3dSChristopher Smith // GENERATE OUTPUT 8835c967d3dSChristopher Smith // normal headers for downloading... 8845c967d3dSChristopher Smith header('Content-type: text/csv;charset=utf-8'); 8855c967d3dSChristopher Smith header('Content-Disposition: attachment; filename="wikiusers.csv"'); 8865c967d3dSChristopher Smith# // for debugging assistance, send as text plain to the browser 8875c967d3dSChristopher Smith# header('Content-type: text/plain;charset=utf-8'); 8885c967d3dSChristopher Smith 8895c967d3dSChristopher Smith // output the csv 8905c967d3dSChristopher Smith $fd = fopen('php://output','w'); 8915c967d3dSChristopher Smith fputcsv($fd, $column_headings); 8925c967d3dSChristopher Smith foreach ($user_list as $user => $info) { 8935c967d3dSChristopher Smith $line = array($user, $info['name'], $info['mail'], join(',',$info['grps'])); 8945c967d3dSChristopher Smith fputcsv($fd, $line); 8955c967d3dSChristopher Smith } 8965c967d3dSChristopher Smith fclose($fd); 897b2c01466SChristopher Smith if (defined('DOKU_UNITTEST')){ return; } 898b2c01466SChristopher Smith 8995c967d3dSChristopher Smith die; 9005c967d3dSChristopher Smith } 901ae1afd2fSChristopher Smith 902c5a7c0c6SGerrit Uitslag /** 903c5a7c0c6SGerrit Uitslag * Import a file of users in csv format 904ae1afd2fSChristopher Smith * 905ae1afd2fSChristopher Smith * csv file should have 4 columns, user_id, full name, email, groups (comma separated) 906c5a7c0c6SGerrit Uitslag * 9075ba64050SChristopher Smith * @return bool whether successful 908ae1afd2fSChristopher Smith */ 9093712ca9aSGerrit Uitslag protected function _import() { 910ae1afd2fSChristopher Smith // check we are allowed to add users 911ae1afd2fSChristopher Smith if (!checkSecurityToken()) return false; 912ae1afd2fSChristopher Smith if (!$this->_auth->canDo('addUser')) return false; 913ae1afd2fSChristopher Smith 914ae1afd2fSChristopher Smith // check file uploaded ok. 915b2c01466SChristopher Smith if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) { 916ae1afd2fSChristopher Smith msg($this->lang['import_error_upload'],-1); 917ae1afd2fSChristopher Smith return false; 918ae1afd2fSChristopher Smith } 919ae1afd2fSChristopher Smith // retrieve users from the file 920ae1afd2fSChristopher Smith $this->_import_failures = array(); 921ae1afd2fSChristopher Smith $import_success_count = 0; 922ae1afd2fSChristopher Smith $import_fail_count = 0; 923ae1afd2fSChristopher Smith $line = 0; 924ae1afd2fSChristopher Smith $fd = fopen($_FILES['import']['tmp_name'],'r'); 925ae1afd2fSChristopher Smith if ($fd) { 926ae1afd2fSChristopher Smith while($csv = fgets($fd)){ 927efcec72bSChristopher Smith if (!utf8_check($csv)) { 928efcec72bSChristopher Smith $csv = utf8_encode($csv); 929efcec72bSChristopher Smith } 930c9454ee3SChristopher Smith $raw = $this->_getcsv($csv); 931ae1afd2fSChristopher Smith $error = ''; // clean out any errors from the previous line 932ae1afd2fSChristopher Smith // data checks... 933ae1afd2fSChristopher Smith if (1 == ++$line) { 934ae1afd2fSChristopher Smith if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue; // skip headers 935ae1afd2fSChristopher Smith } 936ae1afd2fSChristopher Smith if (count($raw) < 4) { // need at least four fields 937ae1afd2fSChristopher Smith $import_fail_count++; 938ae1afd2fSChristopher Smith $error = sprintf($this->lang['import_error_fields'], count($raw)); 939ae1afd2fSChristopher Smith $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv); 940ae1afd2fSChristopher Smith continue; 941ae1afd2fSChristopher Smith } 942ae1afd2fSChristopher Smith array_splice($raw,1,0,auth_pwgen()); // splice in a generated password 943ae1afd2fSChristopher Smith $clean = $this->_cleanImportUser($raw, $error); 944ae1afd2fSChristopher Smith if ($clean && $this->_addImportUser($clean, $error)) { 945328143f8SChristopher Smith $sent = $this->_notifyUser($clean[0],$clean[1],false); 946328143f8SChristopher Smith if (!$sent){ 947328143f8SChristopher Smith msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1); 948328143f8SChristopher Smith } 949ae1afd2fSChristopher Smith $import_success_count++; 950ae1afd2fSChristopher Smith } else { 951ae1afd2fSChristopher Smith $import_fail_count++; 952e73725baSChristopher Smith array_splice($raw, 1, 1); // remove the spliced in password 953ae1afd2fSChristopher Smith $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv); 954ae1afd2fSChristopher Smith } 955ae1afd2fSChristopher Smith } 956ae1afd2fSChristopher Smith msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1)); 957ae1afd2fSChristopher Smith if ($import_fail_count) { 958ae1afd2fSChristopher Smith msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1); 959ae1afd2fSChristopher Smith } 960ae1afd2fSChristopher Smith } else { 961ae1afd2fSChristopher Smith msg($this->lang['import_error_readfail'],-1); 962ae1afd2fSChristopher Smith } 963ae1afd2fSChristopher Smith 964ae1afd2fSChristopher Smith // save import failures into the session 965ae1afd2fSChristopher Smith if (!headers_sent()) { 966ae1afd2fSChristopher Smith session_start(); 967ae1afd2fSChristopher Smith $_SESSION['import_failures'] = $this->_import_failures; 968ae1afd2fSChristopher Smith session_write_close(); 969ae1afd2fSChristopher Smith } 970c5a7c0c6SGerrit Uitslag return true; 971ae1afd2fSChristopher Smith } 972ae1afd2fSChristopher Smith 973c5a7c0c6SGerrit Uitslag /** 974786dfb0eSGerrit Uitslag * Returns cleaned user data 975c5a7c0c6SGerrit Uitslag * 976c5a7c0c6SGerrit Uitslag * @param array $candidate raw values of line from input file 977253d4b48SGerrit Uitslag * @param string $error 978253d4b48SGerrit Uitslag * @return array|false cleaned data or false 979c5a7c0c6SGerrit Uitslag */ 9803712ca9aSGerrit Uitslag protected function _cleanImportUser($candidate, & $error){ 981ae1afd2fSChristopher Smith global $INPUT; 982ae1afd2fSChristopher Smith 983ae1afd2fSChristopher Smith // kludgy .... 984ae1afd2fSChristopher Smith $INPUT->set('userid', $candidate[0]); 985ae1afd2fSChristopher Smith $INPUT->set('userpass', $candidate[1]); 986ae1afd2fSChristopher Smith $INPUT->set('username', $candidate[2]); 987ae1afd2fSChristopher Smith $INPUT->set('usermail', $candidate[3]); 988ae1afd2fSChristopher Smith $INPUT->set('usergroups', $candidate[4]); 989ae1afd2fSChristopher Smith 990ae1afd2fSChristopher Smith $cleaned = $this->_retrieveUser(); 99159bc3b48SGerrit Uitslag list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned; 992ae1afd2fSChristopher Smith if (empty($user)) { 993ae1afd2fSChristopher Smith $error = $this->lang['import_error_baduserid']; 994ae1afd2fSChristopher Smith return false; 995ae1afd2fSChristopher Smith } 996ae1afd2fSChristopher Smith 997ae1afd2fSChristopher Smith // no need to check password, handled elsewhere 998ae1afd2fSChristopher Smith 999ae1afd2fSChristopher Smith if (!($this->_auth->canDo('modName') xor empty($name))){ 1000ae1afd2fSChristopher Smith $error = $this->lang['import_error_badname']; 1001ae1afd2fSChristopher Smith return false; 1002ae1afd2fSChristopher Smith } 1003ae1afd2fSChristopher Smith 1004328143f8SChristopher Smith if ($this->_auth->canDo('modMail')) { 1005328143f8SChristopher Smith if (empty($mail) || !mail_isvalid($mail)) { 1006ae1afd2fSChristopher Smith $error = $this->lang['import_error_badmail']; 1007ae1afd2fSChristopher Smith return false; 1008ae1afd2fSChristopher Smith } 1009328143f8SChristopher Smith } else { 1010328143f8SChristopher Smith if (!empty($mail)) { 1011328143f8SChristopher Smith $error = $this->lang['import_error_badmail']; 1012328143f8SChristopher Smith return false; 1013328143f8SChristopher Smith } 1014328143f8SChristopher Smith } 1015ae1afd2fSChristopher Smith 1016ae1afd2fSChristopher Smith return $cleaned; 1017ae1afd2fSChristopher Smith } 1018ae1afd2fSChristopher Smith 1019c5a7c0c6SGerrit Uitslag /** 1020c5a7c0c6SGerrit Uitslag * Adds imported user to auth backend 1021c5a7c0c6SGerrit Uitslag * 1022c5a7c0c6SGerrit Uitslag * Required a check of canDo('addUser') before 1023c5a7c0c6SGerrit Uitslag * 1024c5a7c0c6SGerrit Uitslag * @param array $user data of user 1025c5a7c0c6SGerrit Uitslag * @param string &$error reference catched error message 10265ba64050SChristopher Smith * @return bool whether successful 1027c5a7c0c6SGerrit Uitslag */ 10283712ca9aSGerrit Uitslag protected function _addImportUser($user, & $error){ 1029ae1afd2fSChristopher Smith if (!$this->_auth->triggerUserMod('create', $user)) { 1030ae1afd2fSChristopher Smith $error = $this->lang['import_error_create']; 1031ae1afd2fSChristopher Smith return false; 1032ae1afd2fSChristopher Smith } 1033ae1afd2fSChristopher Smith 1034ae1afd2fSChristopher Smith return true; 1035ae1afd2fSChristopher Smith } 1036ae1afd2fSChristopher Smith 1037c5a7c0c6SGerrit Uitslag /** 1038c5a7c0c6SGerrit Uitslag * Downloads failures as csv file 1039c5a7c0c6SGerrit Uitslag */ 10403712ca9aSGerrit Uitslag protected function _downloadImportFailures(){ 1041ae1afd2fSChristopher Smith 1042ae1afd2fSChristopher Smith // ============================================================================================== 1043ae1afd2fSChristopher Smith // GENERATE OUTPUT 1044ae1afd2fSChristopher Smith // normal headers for downloading... 1045ae1afd2fSChristopher Smith header('Content-type: text/csv;charset=utf-8'); 1046ae1afd2fSChristopher Smith header('Content-Disposition: attachment; filename="importfails.csv"'); 1047ae1afd2fSChristopher Smith# // for debugging assistance, send as text plain to the browser 1048ae1afd2fSChristopher Smith# header('Content-type: text/plain;charset=utf-8'); 1049ae1afd2fSChristopher Smith 1050ae1afd2fSChristopher Smith // output the csv 1051ae1afd2fSChristopher Smith $fd = fopen('php://output','w'); 1052c5a7c0c6SGerrit Uitslag foreach ($this->_import_failures as $fail) { 1053ae1afd2fSChristopher Smith fputs($fd, $fail['orig']); 1054ae1afd2fSChristopher Smith } 1055ae1afd2fSChristopher Smith fclose($fd); 1056ae1afd2fSChristopher Smith die; 1057ae1afd2fSChristopher Smith } 1058ae1afd2fSChristopher Smith 1059b2c01466SChristopher Smith /** 1060b2c01466SChristopher Smith * wrapper for is_uploaded_file to facilitate overriding by test suite 1061253d4b48SGerrit Uitslag * 1062253d4b48SGerrit Uitslag * @param string $file filename 1063253d4b48SGerrit Uitslag * @return bool 1064b2c01466SChristopher Smith */ 1065b2c01466SChristopher Smith protected function _isUploadedFile($file) { 1066b2c01466SChristopher Smith return is_uploaded_file($file); 1067b2c01466SChristopher Smith } 1068b2c01466SChristopher Smith 1069c9454ee3SChristopher Smith /** 1070c9454ee3SChristopher Smith * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2 1071c9454ee3SChristopher Smith * 1072c9454ee3SChristopher Smith * @deprecated remove when dokuwiki php requirement increases to 5.3+ 1073c9454ee3SChristopher Smith * also associated unit test & mock access method 1074253d4b48SGerrit Uitslag * 1075253d4b48SGerrit Uitslag * @param string $csv string to parse 1076253d4b48SGerrit Uitslag * @return array 1077c9454ee3SChristopher Smith */ 1078c9454ee3SChristopher Smith protected function _getcsv($csv) { 1079c9454ee3SChristopher Smith return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv); 1080c9454ee3SChristopher Smith } 1081c9454ee3SChristopher Smith 1082c9454ee3SChristopher Smith /** 1083c9454ee3SChristopher Smith * replacement str_getcsv() function for php < 5.3 1084c9454ee3SChristopher Smith * loosely based on www.php.net/str_getcsv#88311 1085c9454ee3SChristopher Smith * 1086c9454ee3SChristopher Smith * @deprecated remove when dokuwiki php requirement increases to 5.3+ 1087253d4b48SGerrit Uitslag * 1088253d4b48SGerrit Uitslag * @param string $str string to parse 1089253d4b48SGerrit Uitslag * @return array 1090c9454ee3SChristopher Smith */ 1091c9454ee3SChristopher Smith protected function str_getcsv($str) { 1092c9454ee3SChristopher Smith $fp = fopen("php://temp/maxmemory:1048576", 'r+'); // 1MiB 1093c9454ee3SChristopher Smith fputs($fp, $str); 1094c9454ee3SChristopher Smith rewind($fp); 1095c9454ee3SChristopher Smith 1096c9454ee3SChristopher Smith $data = fgetcsv($fp); 1097c9454ee3SChristopher Smith 1098c9454ee3SChristopher Smith fclose($fp); 1099c9454ee3SChristopher Smith return $data; 1100c9454ee3SChristopher Smith } 11010440ff15Schris} 1102