xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 253d4b48ec708eb42033862dc15c8576f44a48ed)
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();
340440ff15Schris
350440ff15Schris    /**
360440ff15Schris     * Constructor
370440ff15Schris     */
38c5a7c0c6SGerrit Uitslag    public function admin_plugin_usermanager(){
39c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
400440ff15Schris        global $auth;
410440ff15Schris
420440ff15Schris        $this->setupLocale();
4351d94d49Schris
4451d94d49Schris        if (!isset($auth)) {
45c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['noauth'];
4682fd59b6SAndreas Gohr        } else if (!$auth->canDo('getUsers')) {
47c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['nosupport'];
4851d94d49Schris        } else {
4951d94d49Schris
5051d94d49Schris            // we're good to go
5151d94d49Schris            $this->_auth = & $auth;
5251d94d49Schris
5351d94d49Schris        }
54ae1afd2fSChristopher Smith
55ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
560e80bb5eSChristopher Smith        if (!empty($_SESSION['import_failures'])){
57ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
58ae1afd2fSChristopher Smith        }
590440ff15Schris    }
600440ff15Schris
610440ff15Schris    /**
62c5a7c0c6SGerrit Uitslag     * Return prompt for admin menu
63*253d4b48SGerrit Uitslag     *
64*253d4b48SGerrit Uitslag     * @param string $language
65*253d4b48SGerrit Uitslag     * @return string
660440ff15Schris     */
67c5a7c0c6SGerrit Uitslag    public function getMenuText($language) {
680440ff15Schris
690440ff15Schris        if (!is_null($this->_auth))
700440ff15Schris          return parent::getMenuText($language);
710440ff15Schris
72c5a7c0c6SGerrit Uitslag        return $this->getLang('menu').' '.$this->_disabled;
730440ff15Schris    }
740440ff15Schris
750440ff15Schris    /**
760440ff15Schris     * return sort order for position in admin menu
77*253d4b48SGerrit Uitslag     *
78*253d4b48SGerrit Uitslag     * @return int
790440ff15Schris     */
80c5a7c0c6SGerrit Uitslag    public function getMenuSort() {
810440ff15Schris        return 2;
820440ff15Schris    }
830440ff15Schris
840440ff15Schris    /**
85c5a7c0c6SGerrit Uitslag     * Handle user request
86*253d4b48SGerrit Uitslag     *
87*253d4b48SGerrit Uitslag     * @return bool
880440ff15Schris     */
89c5a7c0c6SGerrit Uitslag    public function handle() {
9000d58927SMichael Hamann        global $INPUT;
910440ff15Schris        if (is_null($this->_auth)) return false;
920440ff15Schris
930440ff15Schris        // extract the command and any specific parameters
940440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
9500d58927SMichael Hamann        $fn   = $INPUT->param('fn');
960440ff15Schris
970440ff15Schris        if (is_array($fn)) {
980440ff15Schris            $cmd = key($fn);
990440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
1000440ff15Schris        } else {
1010440ff15Schris            $cmd = $fn;
1020440ff15Schris            $param = null;
1030440ff15Schris        }
1040440ff15Schris
1050440ff15Schris        if ($cmd != "search") {
10600d58927SMichael Hamann            $this->_start = $INPUT->int('start', 0);
1070440ff15Schris            $this->_filter = $this->_retrieveFilter();
1080440ff15Schris        }
1090440ff15Schris
1100440ff15Schris        switch($cmd){
1110440ff15Schris            case "add"    : $this->_addUser(); break;
1120440ff15Schris            case "delete" : $this->_deleteUser(); break;
1130440ff15Schris            case "modify" : $this->_modifyUser(); break;
11478c7c8c9Schris            case "edit"   : $this->_editUser($param); break;
1150440ff15Schris            case "search" : $this->_setFilter($param);
1160440ff15Schris                            $this->_start = 0;
1170440ff15Schris                            break;
1185c967d3dSChristopher Smith            case "export" : $this->_export(); break;
119ae1afd2fSChristopher Smith            case "import" : $this->_import(); break;
120ae1afd2fSChristopher Smith            case "importfails" : $this->_downloadImportFailures(); break;
1210440ff15Schris        }
1220440ff15Schris
12351d94d49Schris        $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
1240440ff15Schris
1250440ff15Schris        // page handling
1260440ff15Schris        switch($cmd){
1270440ff15Schris            case 'start' : $this->_start = 0; break;
1280440ff15Schris            case 'prev'  : $this->_start -= $this->_pagesize; break;
1290440ff15Schris            case 'next'  : $this->_start += $this->_pagesize; break;
1300440ff15Schris            case 'last'  : $this->_start = $this->_user_total; break;
1310440ff15Schris        }
1320440ff15Schris        $this->_validatePagination();
133c5a7c0c6SGerrit Uitslag        return true;
1340440ff15Schris    }
1350440ff15Schris
1360440ff15Schris    /**
137c5a7c0c6SGerrit Uitslag     * Output appropriate html
138*253d4b48SGerrit Uitslag     *
139*253d4b48SGerrit Uitslag     * @return bool
1400440ff15Schris     */
141c5a7c0c6SGerrit Uitslag    public function html() {
1420440ff15Schris        global $ID;
1430440ff15Schris
1440440ff15Schris        if(is_null($this->_auth)) {
1450440ff15Schris            print $this->lang['badauth'];
1460440ff15Schris            return false;
1470440ff15Schris        }
1480440ff15Schris
1490440ff15Schris        $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
1500440ff15Schris
1510440ff15Schris        $page_buttons = $this->_pagination();
15282fd59b6SAndreas Gohr        $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
1530440ff15Schris
15477d19185SAndreas Gohr        $editable = $this->_auth->canDo('UserMod');
155b59cff8bSGerrit Uitslag        $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang['export_filtered'];
1566154103cSmatthiasgrimm
1570440ff15Schris        print $this->locale_xhtml('intro');
1580440ff15Schris        print $this->locale_xhtml('list');
1590440ff15Schris
16058dde80dSAnika Henke        ptln("<div id=\"user__manager\">");
16158dde80dSAnika Henke        ptln("<div class=\"level2\">");
1620440ff15Schris
16367019d15Schris        if ($this->_user_total > 0) {
1640440ff15Schris            ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
1650440ff15Schris        } else {
166a102b175SGerrit Uitslag            if($this->_user_total < 0) {
167a102b175SGerrit Uitslag                $allUserTotal = 0;
168a102b175SGerrit Uitslag            } else {
169a102b175SGerrit Uitslag                $allUserTotal = $this->_auth->getUserCount();
170a102b175SGerrit Uitslag            }
171a102b175SGerrit Uitslag            ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>");
1720440ff15Schris        }
1730440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
174634d7150SAndreas Gohr        formSecurityToken();
175c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1760440ff15Schris        ptln("  <table class=\"inline\">");
1770440ff15Schris        ptln("    <thead>");
1780440ff15Schris        ptln("      <tr>");
179e260f93bSAnika Henke        ptln("        <th>&#160;</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>");
1800440ff15Schris        ptln("      </tr>");
1810440ff15Schris
1820440ff15Schris        ptln("      <tr>");
1832365d73dSAnika 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>");
184a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
185a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
186a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
187a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
1880440ff15Schris        ptln("      </tr>");
1890440ff15Schris        ptln("    </thead>");
1900440ff15Schris
1910440ff15Schris        if ($this->_user_total) {
1920440ff15Schris            ptln("    <tbody>");
1930440ff15Schris            foreach ($user_list as $user => $userinfo) {
1940440ff15Schris                extract($userinfo);
195c5a7c0c6SGerrit Uitslag                /**
196c5a7c0c6SGerrit Uitslag                 * @var string $name
197c5a7c0c6SGerrit Uitslag                 * @var string $pass
198c5a7c0c6SGerrit Uitslag                 * @var string $mail
199c5a7c0c6SGerrit Uitslag                 * @var array  $grps
200c5a7c0c6SGerrit Uitslag                 */
2010440ff15Schris                $groups = join(', ',$grps);
202a2c0246eSAnika Henke                ptln("    <tr class=\"user_info\">");
2030440ff15Schris                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
2042365d73dSAnika Henke                if ($editable) {
20577d19185SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
20677d19185SAndreas Gohr                                                           'do' => 'admin',
20777d19185SAndreas Gohr                                                           'page' => 'usermanager',
20877d19185SAndreas Gohr                                                           'sectok' => getSecurityToken())).
20977d19185SAndreas Gohr                         "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
2102365d73dSAnika Henke                } else {
2112365d73dSAnika Henke                    ptln("    <td>".hsc($user)."</td>");
2122365d73dSAnika Henke                }
2132365d73dSAnika Henke                ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
2140440ff15Schris                ptln("    </tr>");
2150440ff15Schris            }
2160440ff15Schris            ptln("    </tbody>");
2170440ff15Schris        }
2180440ff15Schris
2190440ff15Schris        ptln("    <tbody>");
2202365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
221a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
222a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2230440ff15Schris        ptln("        </span>");
224a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
225a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
226a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
227a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
228a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2290440ff15Schris        ptln("        </span>");
2305c967d3dSChristopher Smith        if (!empty($this->_filter)) {
231a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2325c967d3dSChristopher Smith        }
2335c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2345164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2355164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
236daf4ca4eSAnika Henke
237daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
238daf4ca4eSAnika Henke
2390440ff15Schris        ptln("      </td></tr>");
2400440ff15Schris        ptln("    </tbody>");
2410440ff15Schris        ptln("  </table>");
242c7b28ffdSAnika Henke        ptln("  </div>");
2430440ff15Schris
2440440ff15Schris        ptln("</form>");
2450440ff15Schris        ptln("</div>");
2460440ff15Schris
247a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2480440ff15Schris
24982fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2500440ff15Schris            ptln("<div".$style.">");
2510440ff15Schris            print $this->locale_xhtml('add');
2520440ff15Schris            ptln("  <div class=\"level2\">");
2530440ff15Schris
25478c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2550440ff15Schris
2560440ff15Schris            ptln("  </div>");
2570440ff15Schris            ptln("</div>");
2580440ff15Schris        }
2590440ff15Schris
26082fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
261c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2620440ff15Schris            print $this->locale_xhtml('edit');
2630440ff15Schris            ptln("  <div class=\"level2\">");
2640440ff15Schris
26578c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2660440ff15Schris
2670440ff15Schris            ptln("  </div>");
2680440ff15Schris            ptln("</div>");
2690440ff15Schris        }
270ae1afd2fSChristopher Smith
271ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
272ae1afd2fSChristopher Smith            $this->_htmlImportForm();
273ae1afd2fSChristopher Smith        }
27458dde80dSAnika Henke        ptln("</div>");
275c5a7c0c6SGerrit Uitslag        return true;
2760440ff15Schris    }
2770440ff15Schris
27882fd59b6SAndreas Gohr    /**
279c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
280c5a7c0c6SGerrit Uitslag     *
281c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
282c5a7c0c6SGerrit Uitslag     * @param string $user id of user
283c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
284c5a7c0c6SGerrit Uitslag     * @param int    $indent
28582fd59b6SAndreas Gohr     */
2863712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
287a6858c6aSchris        global $conf;
288bb4866bdSchris        global $ID;
289be9008d3SChristopher Smith        global $lang;
29078c7c8c9Schris
29178c7c8c9Schris        $name = $mail = $groups = '';
292a6858c6aSchris        $notes = array();
2930440ff15Schris
2940440ff15Schris        if ($user) {
29578c7c8c9Schris            extract($userdata);
29678c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
297a6858c6aSchris        } else {
298a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2990440ff15Schris        }
3000440ff15Schris
3010440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
302634d7150SAndreas Gohr        formSecurityToken();
303c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
3040440ff15Schris        ptln("  <table class=\"inline\">",$indent);
3050440ff15Schris        ptln("    <thead>",$indent);
3060440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
3070440ff15Schris        ptln("    </thead>",$indent);
3080440ff15Schris        ptln("    <tbody>",$indent);
30926fb387bSchris
31026fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
31126fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
312be9008d3SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),  $indent+6);
31326fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
31426fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
31526fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
31626fb387bSchris
317a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
318ee9498f5SChristopher Smith            if ($cmd == 'add') {
319c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
320ee9498f5SChristopher Smith            }
321a6858c6aSchris            if ($user) {
322a6858c6aSchris                $notes[] = $this->lang['note_notify'];
323a6858c6aSchris            }
324a6858c6aSchris
325a6858c6aSchris            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);
326a6858c6aSchris        }
327a6858c6aSchris
3280440ff15Schris        ptln("    </tbody>",$indent);
3290440ff15Schris        ptln("    <tbody>",$indent);
3300440ff15Schris        ptln("      <tr>",$indent);
3310440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3320440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3330440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3340440ff15Schris
3350440ff15Schris        // save current $user, we need this to access details if the name is changed
3360440ff15Schris        if ($user)
3370440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3380440ff15Schris
3390440ff15Schris        $this->_htmlFilterSettings($indent+10);
3400440ff15Schris
341a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3420440ff15Schris        ptln("        </td>",$indent);
3430440ff15Schris        ptln("      </tr>",$indent);
3440440ff15Schris        ptln("    </tbody>",$indent);
3450440ff15Schris        ptln("  </table>",$indent);
34645c19902SChristopher Smith
34745c19902SChristopher Smith        if ($notes) {
34845c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
34945c19902SChristopher Smith            foreach ($notes as $note) {
35045c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
35145c19902SChristopher Smith            }
35245c19902SChristopher Smith            ptln("    </ul>");
35345c19902SChristopher Smith        }
354c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3550440ff15Schris        ptln("</form>",$indent);
3560440ff15Schris    }
3570440ff15Schris
358c5a7c0c6SGerrit Uitslag    /**
359c5a7c0c6SGerrit Uitslag     * Prints a inputfield
360c5a7c0c6SGerrit Uitslag     *
361c5a7c0c6SGerrit Uitslag     * @param string $id
362c5a7c0c6SGerrit Uitslag     * @param string $name
363c5a7c0c6SGerrit Uitslag     * @param string $label
364c5a7c0c6SGerrit Uitslag     * @param string $value
365c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
366c5a7c0c6SGerrit Uitslag     * @param int $indent
367c5a7c0c6SGerrit Uitslag     */
3683712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3697de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3707de12fceSAndreas Gohr        echo str_pad('',$indent);
3717de12fceSAndreas Gohr
372359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
373d796a891SAndreas Gohr            $fieldtype = 'password';
374d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3757b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3767b3674bdSChristopher Smith            $fieldtype = 'email';
3777b3674bdSChristopher Smith            $autocomp  = '';
378d796a891SAndreas Gohr        }else{
379d796a891SAndreas Gohr            $fieldtype = 'text';
380d796a891SAndreas Gohr            $autocomp  = '';
381d796a891SAndreas Gohr        }
382d796a891SAndreas Gohr
3837de12fceSAndreas Gohr        echo "<tr $class>";
3847de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3857de12fceSAndreas Gohr        echo "<td>";
3867de12fceSAndreas Gohr        if($cando){
387d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3887de12fceSAndreas Gohr        }else{
3897de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
390ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3917de12fceSAndreas Gohr        }
3927de12fceSAndreas Gohr        echo "</td>";
3937de12fceSAndreas Gohr        echo "</tr>";
39426fb387bSchris    }
39526fb387bSchris
396c5a7c0c6SGerrit Uitslag    /**
397c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
398c5a7c0c6SGerrit Uitslag     *
399c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
400c5a7c0c6SGerrit Uitslag     * @return string html escaped value
401c5a7c0c6SGerrit Uitslag     */
4023712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
4030440ff15Schris        if (empty($this->_filter)) return '';
4040440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
4050440ff15Schris    }
4060440ff15Schris
407c5a7c0c6SGerrit Uitslag    /**
408c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
409c5a7c0c6SGerrit Uitslag     *
410c5a7c0c6SGerrit Uitslag     * @param int $indent
411c5a7c0c6SGerrit Uitslag     */
4123712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4130440ff15Schris
4140440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4150440ff15Schris
4160440ff15Schris        foreach ($this->_filter as $key => $filter) {
4170440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4180440ff15Schris        }
4190440ff15Schris    }
4200440ff15Schris
421c5a7c0c6SGerrit Uitslag    /**
422c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
423c5a7c0c6SGerrit Uitslag     *
424c5a7c0c6SGerrit Uitslag     * @param int $indent
425c5a7c0c6SGerrit Uitslag     */
4263712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
427ae1afd2fSChristopher Smith        global $ID;
428ae1afd2fSChristopher Smith
429ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
430ae1afd2fSChristopher Smith
431ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
432ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
433ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
434ae1afd2fSChristopher Smith        formSecurityToken();
435b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
436ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
437ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
438ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
439ae1afd2fSChristopher Smith
440ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
441ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
442ae1afd2fSChristopher Smith        ptln('</div>');
443ae1afd2fSChristopher Smith
444ae1afd2fSChristopher Smith        // list failures from the previous import
445ae1afd2fSChristopher Smith        if ($this->_import_failures) {
446ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
447ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
448b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
449ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
450ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
451ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
452ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
453ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
454ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
455ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
456ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
457ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
458ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
459ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
460ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
461ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
462ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
463ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
464ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
465ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
466ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
467ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
468ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
469ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
470ae1afd2fSChristopher Smith            }
471ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
472ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
473b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
474ae1afd2fSChristopher Smith            ptln('</div>');
475ae1afd2fSChristopher Smith        }
476ae1afd2fSChristopher Smith
477ae1afd2fSChristopher Smith    }
478ae1afd2fSChristopher Smith
479c5a7c0c6SGerrit Uitslag    /**
480c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
481c5a7c0c6SGerrit Uitslag     *
482c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
483c5a7c0c6SGerrit Uitslag     */
4843712ca9aSGerrit Uitslag    protected function _addUser(){
48500d58927SMichael Hamann        global $INPUT;
486634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
48782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4880440ff15Schris
489359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
4900440ff15Schris        if (empty($user)) return false;
4916733c4d7SChris Smith
4926733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
493c3f4fb63SGina Haeussge            if (empty($pass)){
49400d58927SMichael Hamann                if($INPUT->has('usernotify')){
4958a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
496c3f4fb63SGina Haeussge                } else {
49760b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
49860b9901bSAndreas Gohr                    return false;
49960b9901bSAndreas Gohr                }
500359e9417SChristopher Smith            } else {
501359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
502359e9417SChristopher Smith                    return false;
503359e9417SChristopher Smith                }
5046733c4d7SChris Smith            }
5056733c4d7SChris Smith        } else {
5066733c4d7SChris Smith            if (!empty($pass)){
5076733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5086733c4d7SChris Smith                return false;
5096733c4d7SChris Smith            }
5106733c4d7SChris Smith        }
5116733c4d7SChris Smith
5126733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5136733c4d7SChris Smith            if (empty($name)){
5146733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5156733c4d7SChris Smith                return false;
5166733c4d7SChris Smith            }
5176733c4d7SChris Smith        } else {
5186733c4d7SChris Smith            if (!empty($name)){
5196733c4d7SChris Smith                return false;
5206733c4d7SChris Smith            }
5216733c4d7SChris Smith        }
5226733c4d7SChris Smith
5236733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5246733c4d7SChris Smith            if (empty($mail)){
5256733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5266733c4d7SChris Smith                return false;
5276733c4d7SChris Smith            }
5286733c4d7SChris Smith        } else {
5296733c4d7SChris Smith            if (!empty($mail)){
5306733c4d7SChris Smith                return false;
5316733c4d7SChris Smith            }
5326733c4d7SChris Smith        }
5330440ff15Schris
5347d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
535a6858c6aSchris
536a6858c6aSchris            msg($this->lang['add_ok'], 1);
537a6858c6aSchris
53800d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
539a6858c6aSchris                $this->_notifyUser($user,$pass);
540a6858c6aSchris            }
541a6858c6aSchris        } else {
54260b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
543a6858c6aSchris        }
544a6858c6aSchris
545a6858c6aSchris        return $ok;
5460440ff15Schris    }
5470440ff15Schris
5480440ff15Schris    /**
549c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
550c5a7c0c6SGerrit Uitslag     *
551c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5520440ff15Schris     */
5533712ca9aSGerrit Uitslag    protected function _deleteUser(){
55400d58927SMichael Hamann        global $conf, $INPUT;
5559ec82636SAndreas Gohr
556634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
55782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5580440ff15Schris
55900d58927SMichael Hamann        $selected = $INPUT->arr('delete');
56000d58927SMichael Hamann        if (empty($selected)) return false;
5610440ff15Schris        $selected = array_keys($selected);
5620440ff15Schris
563c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
564c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
565c9a8f912SMichael Klier            return false;
566c9a8f912SMichael Klier        }
567c9a8f912SMichael Klier
5687d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5690440ff15Schris        if ($count == count($selected)) {
5700440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5710440ff15Schris            msg("$text.", 1);
5720440ff15Schris        } else {
5730440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5740440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5750440ff15Schris            msg("$part1, $part2",-1);
5760440ff15Schris        }
57778c7c8c9Schris
5789ec82636SAndreas Gohr        // invalidate all sessions
5799ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5809ec82636SAndreas Gohr
58178c7c8c9Schris        return true;
58278c7c8c9Schris    }
58378c7c8c9Schris
58478c7c8c9Schris    /**
58578c7c8c9Schris     * Edit user (a user has been selected for editing)
586c5a7c0c6SGerrit Uitslag     *
587c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
588c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
58978c7c8c9Schris     */
5903712ca9aSGerrit Uitslag    protected function _editUser($param) {
591634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
59278c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
593786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
59478c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
59578c7c8c9Schris
59678c7c8c9Schris        // no user found?
59778c7c8c9Schris        if (!$userdata) {
59878c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
59978c7c8c9Schris            return false;
60078c7c8c9Schris        }
60178c7c8c9Schris
60278c7c8c9Schris        $this->_edit_user = $user;
60378c7c8c9Schris        $this->_edit_userdata = $userdata;
60478c7c8c9Schris
60578c7c8c9Schris        return true;
6060440ff15Schris    }
6070440ff15Schris
6080440ff15Schris    /**
609c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
610c5a7c0c6SGerrit Uitslag     *
611c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6120440ff15Schris     */
6133712ca9aSGerrit Uitslag    protected function _modifyUser(){
61400d58927SMichael Hamann        global $conf, $INPUT;
6159ec82636SAndreas Gohr
616634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
61782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6180440ff15Schris
61926fb387bSchris        // get currently valid  user data
620786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
621073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
622073766c6Smatthiasgrimm
62326fb387bSchris        // get new user data subject to change
624359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
625073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6260440ff15Schris
6270440ff15Schris        $changes = array();
628073766c6Smatthiasgrimm        if ($newuser != $olduser) {
62926fb387bSchris
63026fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
63126fb387bSchris                msg($this->lang['update_fail'],-1);
63226fb387bSchris                return false;
63326fb387bSchris            }
63426fb387bSchris
63526fb387bSchris            // check if $newuser already exists
636073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
637073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
638a6858c6aSchris                $re_edit = true;
6390440ff15Schris            } else {
640073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6410440ff15Schris            }
64293eefc2fSAndreas Gohr        }
643359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6442400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
645359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
646359e9417SChristopher Smith                    $changes['pass'] = $newpass;
647359e9417SChristopher Smith                } else {
648359e9417SChristopher Smith                    return false;
649359e9417SChristopher Smith                }
650359e9417SChristopher Smith            } else {
651359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
652359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
653359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
654359e9417SChristopher Smith                }
655359e9417SChristopher Smith            }
6560440ff15Schris        }
6570440ff15Schris
65840d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
659073766c6Smatthiasgrimm            $changes['name'] = $newname;
66040d72af6SChristopher Smith        }
66140d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
662073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
66340d72af6SChristopher Smith        }
66440d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
665073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
66640d72af6SChristopher Smith        }
6670440ff15Schris
6687d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6690440ff15Schris            msg($this->lang['update_ok'],1);
670a6858c6aSchris
6716ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
672a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
6736ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
674a6858c6aSchris            }
675a6858c6aSchris
6769ec82636SAndreas Gohr            // invalidate all sessions
6779ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6789ec82636SAndreas Gohr
6790440ff15Schris        } else {
6800440ff15Schris            msg($this->lang['update_fail'],-1);
6810440ff15Schris        }
68278c7c8c9Schris
683a6858c6aSchris        if (!empty($re_edit)) {
684a6858c6aSchris            $this->_editUser($olduser);
6850440ff15Schris        }
6860440ff15Schris
687a6858c6aSchris        return $ok;
688a6858c6aSchris    }
689a6858c6aSchris
690a6858c6aSchris    /**
691c5a7c0c6SGerrit Uitslag     * Send password change notification email
692c5a7c0c6SGerrit Uitslag     *
693c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
694c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
695c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
696c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
697a6858c6aSchris     */
6983712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
699a6858c6aSchris
700a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
701328143f8SChristopher Smith            if ($status_alert) {
702a6858c6aSchris                msg($this->lang['notify_ok'], 1);
703328143f8SChristopher Smith            }
704a6858c6aSchris        } else {
705328143f8SChristopher Smith            if ($status_alert) {
706a6858c6aSchris                msg($this->lang['notify_fail'], -1);
707a6858c6aSchris            }
708328143f8SChristopher Smith        }
709a6858c6aSchris
710a6858c6aSchris        return $sent;
711a6858c6aSchris    }
712a6858c6aSchris
713a6858c6aSchris    /**
714359e9417SChristopher Smith     * Verify password meets minimum requirements
715359e9417SChristopher Smith     * :TODO: extend to support password strength
716359e9417SChristopher Smith     *
717359e9417SChristopher Smith     * @param string  $password   candidate string for new password
718359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
719359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
720359e9417SChristopher Smith     */
721359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
722be9008d3SChristopher Smith        global $lang;
723359e9417SChristopher Smith
7242400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
725359e9417SChristopher Smith            return false;
726359e9417SChristopher Smith        }
727359e9417SChristopher Smith
728359e9417SChristopher Smith        if ($password !== $confirm) {
729be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
730359e9417SChristopher Smith            return false;
731359e9417SChristopher Smith        }
732359e9417SChristopher Smith
733359e9417SChristopher Smith        // :TODO: test password for required strength
734359e9417SChristopher Smith
735359e9417SChristopher Smith        // if we make it this far the password is good
736359e9417SChristopher Smith        return true;
737359e9417SChristopher Smith    }
738359e9417SChristopher Smith
739359e9417SChristopher Smith    /**
740c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
741a6858c6aSchris     *
742c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
743a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7440440ff15Schris     */
7453712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
746c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7477441e340SAndreas Gohr        global $auth;
748fbfbbe8aSHakan Sandell        global $INPUT;
7490440ff15Schris
75059bc3b48SGerrit Uitslag        $user = array();
751fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
752fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
753fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
754fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
755fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
756359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7570440ff15Schris
7587441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7597441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7607441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7617441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7627441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7630440ff15Schris
7640440ff15Schris        return $user;
7650440ff15Schris    }
7660440ff15Schris
767c5a7c0c6SGerrit Uitslag    /**
768c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
769c5a7c0c6SGerrit Uitslag     *
770c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
771c5a7c0c6SGerrit Uitslag     */
7723712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7730440ff15Schris
7740440ff15Schris        $this->_filter = array();
7750440ff15Schris
7760440ff15Schris        if ($op == 'new') {
77759bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
7780440ff15Schris
7790440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7800440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7810440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7820440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7830440ff15Schris        }
7840440ff15Schris    }
7850440ff15Schris
786c5a7c0c6SGerrit Uitslag    /**
787c5a7c0c6SGerrit Uitslag     * Get the current search terms
788c5a7c0c6SGerrit Uitslag     *
789c5a7c0c6SGerrit Uitslag     * @return array
790c5a7c0c6SGerrit Uitslag     */
7913712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
792fbfbbe8aSHakan Sandell        global $INPUT;
7930440ff15Schris
794fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
7950440ff15Schris
7960440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
7970440ff15Schris        $filter = array();
7980440ff15Schris
7990440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8000440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8010440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8020440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8030440ff15Schris
8040440ff15Schris        return $filter;
8050440ff15Schris    }
8060440ff15Schris
807c5a7c0c6SGerrit Uitslag    /**
808c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
809c5a7c0c6SGerrit Uitslag     */
8103712ca9aSGerrit Uitslag    protected function _validatePagination() {
8110440ff15Schris
8120440ff15Schris        if ($this->_start >= $this->_user_total) {
8130440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8140440ff15Schris        }
8150440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8160440ff15Schris
8170440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8180440ff15Schris    }
8190440ff15Schris
820c5a7c0c6SGerrit Uitslag    /**
821c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
822c5a7c0c6SGerrit Uitslag     *
823c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8240440ff15Schris     */
8253712ca9aSGerrit Uitslag    protected function _pagination() {
8260440ff15Schris
82751d94d49Schris        $disabled = 'disabled="disabled"';
82851d94d49Schris
82959bc3b48SGerrit Uitslag        $buttons = array();
83051d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
83151d94d49Schris
83251d94d49Schris        if ($this->_user_total == -1) {
83351d94d49Schris            $buttons['last'] = $disabled;
83451d94d49Schris            $buttons['next'] = '';
83551d94d49Schris        } else {
83651d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
83751d94d49Schris        }
8380440ff15Schris
8390440ff15Schris        return $buttons;
8400440ff15Schris    }
8415c967d3dSChristopher Smith
842c5a7c0c6SGerrit Uitslag    /**
843c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8445c967d3dSChristopher Smith     */
8453712ca9aSGerrit Uitslag    protected function _export() {
8465c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8475c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8485c967d3dSChristopher Smith        $column_headings = array(
8495c967d3dSChristopher Smith            $this->lang["user_id"],
8505c967d3dSChristopher Smith            $this->lang["user_name"],
8515c967d3dSChristopher Smith            $this->lang["user_mail"],
8525c967d3dSChristopher Smith            $this->lang["user_groups"]
8535c967d3dSChristopher Smith        );
8545c967d3dSChristopher Smith
8555c967d3dSChristopher Smith        // ==============================================================================================
8565c967d3dSChristopher Smith        // GENERATE OUTPUT
8575c967d3dSChristopher Smith        // normal headers for downloading...
8585c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8595c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8605c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8615c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8625c967d3dSChristopher Smith
8635c967d3dSChristopher Smith        // output the csv
8645c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8655c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8665c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8675c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8685c967d3dSChristopher Smith            fputcsv($fd, $line);
8695c967d3dSChristopher Smith        }
8705c967d3dSChristopher Smith        fclose($fd);
871b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
872b2c01466SChristopher Smith
8735c967d3dSChristopher Smith        die;
8745c967d3dSChristopher Smith    }
875ae1afd2fSChristopher Smith
876c5a7c0c6SGerrit Uitslag    /**
877c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
878ae1afd2fSChristopher Smith     *
879ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
880c5a7c0c6SGerrit Uitslag     *
8815ba64050SChristopher Smith     * @return bool whether successful
882ae1afd2fSChristopher Smith     */
8833712ca9aSGerrit Uitslag    protected function _import() {
884ae1afd2fSChristopher Smith        // check we are allowed to add users
885ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
886ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
887ae1afd2fSChristopher Smith
888ae1afd2fSChristopher Smith        // check file uploaded ok.
889b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
890ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
891ae1afd2fSChristopher Smith            return false;
892ae1afd2fSChristopher Smith        }
893ae1afd2fSChristopher Smith        // retrieve users from the file
894ae1afd2fSChristopher Smith        $this->_import_failures = array();
895ae1afd2fSChristopher Smith        $import_success_count = 0;
896ae1afd2fSChristopher Smith        $import_fail_count = 0;
897ae1afd2fSChristopher Smith        $line = 0;
898ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
899ae1afd2fSChristopher Smith        if ($fd) {
900ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
901efcec72bSChristopher Smith                if (!utf8_check($csv)) {
902efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
903efcec72bSChristopher Smith                }
904c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
905ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
906ae1afd2fSChristopher Smith                // data checks...
907ae1afd2fSChristopher Smith                if (1 == ++$line) {
908ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
909ae1afd2fSChristopher Smith                }
910ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
911ae1afd2fSChristopher Smith                    $import_fail_count++;
912ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
913ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
914ae1afd2fSChristopher Smith                    continue;
915ae1afd2fSChristopher Smith                }
916ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
917ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
918ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
919328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
920328143f8SChristopher Smith                    if (!$sent){
921328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
922328143f8SChristopher Smith                    }
923ae1afd2fSChristopher Smith                    $import_success_count++;
924ae1afd2fSChristopher Smith                } else {
925ae1afd2fSChristopher Smith                    $import_fail_count++;
926e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
927ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
928ae1afd2fSChristopher Smith                }
929ae1afd2fSChristopher Smith            }
930ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
931ae1afd2fSChristopher Smith            if ($import_fail_count) {
932ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
933ae1afd2fSChristopher Smith            }
934ae1afd2fSChristopher Smith        } else {
935ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
936ae1afd2fSChristopher Smith        }
937ae1afd2fSChristopher Smith
938ae1afd2fSChristopher Smith        // save import failures into the session
939ae1afd2fSChristopher Smith        if (!headers_sent()) {
940ae1afd2fSChristopher Smith            session_start();
941ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
942ae1afd2fSChristopher Smith            session_write_close();
943ae1afd2fSChristopher Smith        }
944c5a7c0c6SGerrit Uitslag        return true;
945ae1afd2fSChristopher Smith    }
946ae1afd2fSChristopher Smith
947c5a7c0c6SGerrit Uitslag    /**
948786dfb0eSGerrit Uitslag     * Returns cleaned user data
949c5a7c0c6SGerrit Uitslag     *
950c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
951*253d4b48SGerrit Uitslag     * @param string $error
952*253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
953c5a7c0c6SGerrit Uitslag     */
9543712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
955ae1afd2fSChristopher Smith        global $INPUT;
956ae1afd2fSChristopher Smith
957ae1afd2fSChristopher Smith        // kludgy ....
958ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
959ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
960ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
961ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
962ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
963ae1afd2fSChristopher Smith
964ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
96559bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
966ae1afd2fSChristopher Smith        if (empty($user)) {
967ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
968ae1afd2fSChristopher Smith            return false;
969ae1afd2fSChristopher Smith        }
970ae1afd2fSChristopher Smith
971ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
972ae1afd2fSChristopher Smith
973ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
974ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
975ae1afd2fSChristopher Smith            return false;
976ae1afd2fSChristopher Smith        }
977ae1afd2fSChristopher Smith
978328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
979328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
980ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
981ae1afd2fSChristopher Smith                return false;
982ae1afd2fSChristopher Smith            }
983328143f8SChristopher Smith        } else {
984328143f8SChristopher Smith            if (!empty($mail)) {
985328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
986328143f8SChristopher Smith                return false;
987328143f8SChristopher Smith            }
988328143f8SChristopher Smith        }
989ae1afd2fSChristopher Smith
990ae1afd2fSChristopher Smith        return $cleaned;
991ae1afd2fSChristopher Smith    }
992ae1afd2fSChristopher Smith
993c5a7c0c6SGerrit Uitslag    /**
994c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
995c5a7c0c6SGerrit Uitslag     *
996c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
997c5a7c0c6SGerrit Uitslag     *
998c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
999c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10005ba64050SChristopher Smith     * @return bool whether successful
1001c5a7c0c6SGerrit Uitslag     */
10023712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1003ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1004ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1005ae1afd2fSChristopher Smith            return false;
1006ae1afd2fSChristopher Smith        }
1007ae1afd2fSChristopher Smith
1008ae1afd2fSChristopher Smith        return true;
1009ae1afd2fSChristopher Smith    }
1010ae1afd2fSChristopher Smith
1011c5a7c0c6SGerrit Uitslag    /**
1012c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1013c5a7c0c6SGerrit Uitslag     */
10143712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1015ae1afd2fSChristopher Smith
1016ae1afd2fSChristopher Smith        // ==============================================================================================
1017ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1018ae1afd2fSChristopher Smith        // normal headers for downloading...
1019ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1020ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1021ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1022ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1023ae1afd2fSChristopher Smith
1024ae1afd2fSChristopher Smith        // output the csv
1025ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1026c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1027ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1028ae1afd2fSChristopher Smith        }
1029ae1afd2fSChristopher Smith        fclose($fd);
1030ae1afd2fSChristopher Smith        die;
1031ae1afd2fSChristopher Smith    }
1032ae1afd2fSChristopher Smith
1033b2c01466SChristopher Smith    /**
1034b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1035*253d4b48SGerrit Uitslag     *
1036*253d4b48SGerrit Uitslag     * @param string $file filename
1037*253d4b48SGerrit Uitslag     * @return bool
1038b2c01466SChristopher Smith     */
1039b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1040b2c01466SChristopher Smith        return is_uploaded_file($file);
1041b2c01466SChristopher Smith    }
1042b2c01466SChristopher Smith
1043c9454ee3SChristopher Smith    /**
1044c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1045c9454ee3SChristopher Smith     *
1046c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1047c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1048*253d4b48SGerrit Uitslag     *
1049*253d4b48SGerrit Uitslag     * @param string $csv string to parse
1050*253d4b48SGerrit Uitslag     * @return array
1051c9454ee3SChristopher Smith     */
1052c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1053c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1054c9454ee3SChristopher Smith    }
1055c9454ee3SChristopher Smith
1056c9454ee3SChristopher Smith    /**
1057c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1058c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1059c9454ee3SChristopher Smith     *
1060c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1061*253d4b48SGerrit Uitslag     *
1062*253d4b48SGerrit Uitslag     * @param string $str string to parse
1063*253d4b48SGerrit Uitslag     * @return array
1064c9454ee3SChristopher Smith     */
1065c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1066c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1067c9454ee3SChristopher Smith        fputs($fp, $str);
1068c9454ee3SChristopher Smith        rewind($fp);
1069c9454ee3SChristopher Smith
1070c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1071c9454ee3SChristopher Smith
1072c9454ee3SChristopher Smith        fclose($fp);
1073c9454ee3SChristopher Smith        return $data;
1074c9454ee3SChristopher Smith    }
10750440ff15Schris}
1076