xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision d0c0a5c427963f7daba8ff28ee1abab3ef8156ba)
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();
34462e9e37SMichael Große    protected $_lastdisabled = false; // set to true if last user is unknown and last button is hence buggy
350440ff15Schris
360440ff15Schris    /**
370440ff15Schris     * Constructor
380440ff15Schris     */
3926e22ab8SChristopher Smith    public function __construct(){
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    /**
100462e9e37SMichael Große     * @param boolean $lastdisabled
101462e9e37SMichael Große     */
102462e9e37SMichael Große    public function setLastdisabled($lastdisabled) {
103462e9e37SMichael Große        $this->_lastdisabled = $lastdisabled;
104462e9e37SMichael Große    }
105462e9e37SMichael Große
106462e9e37SMichael 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>&#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>");
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\">");
225f23f9594SAndreas Gohr                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".hsc($user)."]\" ".$delete_disable." /></td>");
2262365d73dSAnika Henke                if ($editable) {
227f23f9594SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.$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\">");
244ae614416SAnika Henke        ptln("          <button type=\"submit\" name=\"fn[delete]\" id=\"usrmgr__del\" ".$delete_disable.">".$this->lang['delete_selected']."</button>");
24576c49356SMike Wilmes        ptln("        </span>");
24676c49356SMike Wilmes        ptln("        <span class=\"mediaright\">");
24776c49356SMike Wilmes        ptln("          <button type=\"submit\" name=\"fn[start]\" ".$page_buttons['start'].">".$this->lang['start']."</button>");
24876c49356SMike Wilmes        ptln("          <button type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev'].">".$this->lang['prev']."</button>");
24976c49356SMike Wilmes        ptln("          <button type=\"submit\" name=\"fn[next]\" ".$page_buttons['next'].">".$this->lang['next']."</button>");
25076c49356SMike Wilmes        ptln("          <button type=\"submit\" name=\"fn[last]\" ".$page_buttons['last'].">".$this->lang['last']."</button>");
25176c49356SMike Wilmes        ptln("        </span>");
2525c967d3dSChristopher Smith        if (!empty($this->_filter)) {
253ae614416SAnika Henke            ptln("    <button type=\"submit\" name=\"fn[search][clear]\">".$this->lang['clear']."</button>");
2545c967d3dSChristopher Smith        }
255ae614416SAnika Henke        ptln("        <button type=\"submit\" name=\"fn[export]\">".$export_label."</button>");
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
3329b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"),   true, $indent+6);
3339b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),   false, $indent+6);
3349b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),   false, $indent+6);
3359b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),    true, $indent+6);
3369b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),    true, $indent+6);
3379b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"), false, $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)
359f23f9594SAndreas Gohr          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".hsc($user)."\" />",$indent);
3600440ff15Schris
3610440ff15Schris        $this->_htmlFilterSettings($indent+10);
3620440ff15Schris
363ae614416SAnika Henke        ptln("          <button type=\"submit\" name=\"fn[".$cmd."]\">".$this->lang[$cmd]."</button>",$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) {
372ae614416SAnika Henke                ptln("      <li><span class=\"li\">".$note."</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
3889b82d43fSAndreas Gohr     * @param bool   $required is this field required?
389c5a7c0c6SGerrit Uitslag     * @param int $indent
390c5a7c0c6SGerrit Uitslag     */
3919b82d43fSAndreas Gohr    protected function _htmlInputField($id, $name, $label, $value, $cando, $required, $indent=0) {
3927de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3937de12fceSAndreas Gohr        echo str_pad('',$indent);
3947de12fceSAndreas Gohr
395359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
396d796a891SAndreas Gohr            $fieldtype = 'password';
397d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3987b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3997b3674bdSChristopher Smith            $fieldtype = 'email';
4007b3674bdSChristopher Smith            $autocomp  = '';
401d796a891SAndreas Gohr        }else{
402d796a891SAndreas Gohr            $fieldtype = 'text';
403d796a891SAndreas Gohr            $autocomp  = '';
404d796a891SAndreas Gohr        }
405f23f9594SAndreas Gohr        $value = hsc($value);
406d796a891SAndreas Gohr
4077de12fceSAndreas Gohr        echo "<tr $class>";
4087de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
4097de12fceSAndreas Gohr        echo "<td>";
4107de12fceSAndreas Gohr        if($cando){
4119b82d43fSAndreas Gohr            $req = '';
4129b82d43fSAndreas Gohr            if($required) $req = 'required="required"';
4139b82d43fSAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp $req />";
4147de12fceSAndreas Gohr        }else{
4157de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
416ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
4177de12fceSAndreas Gohr        }
4187de12fceSAndreas Gohr        echo "</td>";
4197de12fceSAndreas Gohr        echo "</tr>";
42026fb387bSchris    }
42126fb387bSchris
422c5a7c0c6SGerrit Uitslag    /**
423c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
424c5a7c0c6SGerrit Uitslag     *
425c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
426c5a7c0c6SGerrit Uitslag     * @return string html escaped value
427c5a7c0c6SGerrit Uitslag     */
4283712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
4290440ff15Schris        if (empty($this->_filter)) return '';
4300440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
4310440ff15Schris    }
4320440ff15Schris
433c5a7c0c6SGerrit Uitslag    /**
434c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
435c5a7c0c6SGerrit Uitslag     *
436c5a7c0c6SGerrit Uitslag     * @param int $indent
437c5a7c0c6SGerrit Uitslag     */
4383712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4390440ff15Schris
4400440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4410440ff15Schris
4420440ff15Schris        foreach ($this->_filter as $key => $filter) {
4430440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4440440ff15Schris        }
4450440ff15Schris    }
4460440ff15Schris
447c5a7c0c6SGerrit Uitslag    /**
448c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
449c5a7c0c6SGerrit Uitslag     *
450c5a7c0c6SGerrit Uitslag     * @param int $indent
451c5a7c0c6SGerrit Uitslag     */
4523712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
453ae1afd2fSChristopher Smith        global $ID;
454ae1afd2fSChristopher Smith
455ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
456ae1afd2fSChristopher Smith
457ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
458ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
459ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
460ae1afd2fSChristopher Smith        formSecurityToken();
461b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
462ae614416SAnika Henke        ptln('    <button type="submit" name="fn[import]">'.$this->lang['import'].'</button>',$indent);
463ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
464ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
465ae1afd2fSChristopher Smith
466ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
467ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
468ae1afd2fSChristopher Smith        ptln('</div>');
469ae1afd2fSChristopher Smith
470ae1afd2fSChristopher Smith        // list failures from the previous import
471ae1afd2fSChristopher Smith        if ($this->_import_failures) {
472ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
473ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
474b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
475ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
476ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
477ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
478ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
479ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
480ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
481ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
482ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
483ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
484ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
485ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
486ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
487ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
488ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
489ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
490ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
491ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
492ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
493ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
494ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
495ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
496ae1afd2fSChristopher Smith            }
497ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
498ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
499b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
500ae1afd2fSChristopher Smith            ptln('</div>');
501ae1afd2fSChristopher Smith        }
502ae1afd2fSChristopher Smith
503ae1afd2fSChristopher Smith    }
504ae1afd2fSChristopher Smith
505c5a7c0c6SGerrit Uitslag    /**
506c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
507c5a7c0c6SGerrit Uitslag     *
508c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
509c5a7c0c6SGerrit Uitslag     */
5103712ca9aSGerrit Uitslag    protected function _addUser(){
51100d58927SMichael Hamann        global $INPUT;
512634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
51382fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
5140440ff15Schris
515359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
5160440ff15Schris        if (empty($user)) return false;
5176733c4d7SChris Smith
5186733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
519c3f4fb63SGina Haeussge            if (empty($pass)){
52000d58927SMichael Hamann                if($INPUT->has('usernotify')){
5218a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
522c3f4fb63SGina Haeussge                } else {
52360b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
52409a5dcd6SMichael Große                    msg($this->lang['addUser_error_missing_pass'], -1);
52560b9901bSAndreas Gohr                    return false;
52660b9901bSAndreas Gohr                }
527359e9417SChristopher Smith            } else {
528359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
52909a5dcd6SMichael Große                    msg($this->lang['add_fail'], -1);
53009a5dcd6SMichael Große                    msg($this->lang['addUser_error_pass_not_identical'], -1);
531359e9417SChristopher Smith                    return false;
532359e9417SChristopher Smith                }
5336733c4d7SChris Smith            }
5346733c4d7SChris Smith        } else {
5356733c4d7SChris Smith            if (!empty($pass)){
5366733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
53709a5dcd6SMichael Große                msg($this->lang['addUser_error_modPass_disabled'], -1);
5386733c4d7SChris Smith                return false;
5396733c4d7SChris Smith            }
5406733c4d7SChris Smith        }
5416733c4d7SChris Smith
5426733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5436733c4d7SChris Smith            if (empty($name)){
5446733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
54509a5dcd6SMichael Große                msg($this->lang['addUser_error_name_missing'], -1);
5466733c4d7SChris Smith                return false;
5476733c4d7SChris Smith            }
5486733c4d7SChris Smith        } else {
5496733c4d7SChris Smith            if (!empty($name)){
55009a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
55109a5dcd6SMichael Große                msg($this->lang['addUser_error_modName_disabled'], -1);
5526733c4d7SChris Smith                return false;
5536733c4d7SChris Smith            }
5546733c4d7SChris Smith        }
5556733c4d7SChris Smith
5566733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5576733c4d7SChris Smith            if (empty($mail)){
5586733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
55909a5dcd6SMichael Große                msg($this->lang['addUser_error_mail_missing'], -1);
5606733c4d7SChris Smith                return false;
5616733c4d7SChris Smith            }
5626733c4d7SChris Smith        } else {
5636733c4d7SChris Smith            if (!empty($mail)){
56409a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
56509a5dcd6SMichael Große                msg($this->lang['addUser_error_modMail_disabled'], -1);
5666733c4d7SChris Smith                return false;
5676733c4d7SChris Smith            }
5686733c4d7SChris Smith        }
5690440ff15Schris
5707d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
571a6858c6aSchris
572a6858c6aSchris            msg($this->lang['add_ok'], 1);
573a6858c6aSchris
57400d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
575a6858c6aSchris                $this->_notifyUser($user,$pass);
576a6858c6aSchris            }
577a6858c6aSchris        } else {
57860b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
57909a5dcd6SMichael Große            msg($this->lang['addUser_error_create_event_failed'], -1);
580a6858c6aSchris        }
581a6858c6aSchris
582a6858c6aSchris        return $ok;
5830440ff15Schris    }
5840440ff15Schris
5850440ff15Schris    /**
586c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
587c5a7c0c6SGerrit Uitslag     *
588c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5890440ff15Schris     */
5903712ca9aSGerrit Uitslag    protected function _deleteUser(){
59100d58927SMichael Hamann        global $conf, $INPUT;
5929ec82636SAndreas Gohr
593634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
59482fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5950440ff15Schris
59600d58927SMichael Hamann        $selected = $INPUT->arr('delete');
59700d58927SMichael Hamann        if (empty($selected)) return false;
5980440ff15Schris        $selected = array_keys($selected);
5990440ff15Schris
600c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
601c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
602c9a8f912SMichael Klier            return false;
603c9a8f912SMichael Klier        }
604c9a8f912SMichael Klier
6057d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
6060440ff15Schris        if ($count == count($selected)) {
6070440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
6080440ff15Schris            msg("$text.", 1);
6090440ff15Schris        } else {
6100440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
6110440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
6120440ff15Schris            msg("$part1, $part2",-1);
6130440ff15Schris        }
61478c7c8c9Schris
6159ec82636SAndreas Gohr        // invalidate all sessions
6169ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
6179ec82636SAndreas Gohr
61878c7c8c9Schris        return true;
61978c7c8c9Schris    }
62078c7c8c9Schris
62178c7c8c9Schris    /**
62278c7c8c9Schris     * Edit user (a user has been selected for editing)
623c5a7c0c6SGerrit Uitslag     *
624c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
625c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
62678c7c8c9Schris     */
6273712ca9aSGerrit Uitslag    protected function _editUser($param) {
628634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
62978c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
630786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
63178c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
63278c7c8c9Schris
63378c7c8c9Schris        // no user found?
63478c7c8c9Schris        if (!$userdata) {
63578c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
63678c7c8c9Schris            return false;
63778c7c8c9Schris        }
63878c7c8c9Schris
63978c7c8c9Schris        $this->_edit_user = $user;
64078c7c8c9Schris        $this->_edit_userdata = $userdata;
64178c7c8c9Schris
64278c7c8c9Schris        return true;
6430440ff15Schris    }
6440440ff15Schris
6450440ff15Schris    /**
646c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
647c5a7c0c6SGerrit Uitslag     *
648c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6490440ff15Schris     */
6503712ca9aSGerrit Uitslag    protected function _modifyUser(){
65100d58927SMichael Hamann        global $conf, $INPUT;
6529ec82636SAndreas Gohr
653634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
65482fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6550440ff15Schris
65626fb387bSchris        // get currently valid  user data
657786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
658073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
659073766c6Smatthiasgrimm
66026fb387bSchris        // get new user data subject to change
661359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
662073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6630440ff15Schris
6640440ff15Schris        $changes = array();
665073766c6Smatthiasgrimm        if ($newuser != $olduser) {
66626fb387bSchris
66726fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
66826fb387bSchris                msg($this->lang['update_fail'],-1);
66926fb387bSchris                return false;
67026fb387bSchris            }
67126fb387bSchris
67226fb387bSchris            // check if $newuser already exists
673073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
674073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
675a6858c6aSchris                $re_edit = true;
6760440ff15Schris            } else {
677073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6780440ff15Schris            }
67993eefc2fSAndreas Gohr        }
680359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6812400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
682359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
683359e9417SChristopher Smith                    $changes['pass'] = $newpass;
684359e9417SChristopher Smith                } else {
685359e9417SChristopher Smith                    return false;
686359e9417SChristopher Smith                }
687359e9417SChristopher Smith            } else {
688359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
689359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
690359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
691359e9417SChristopher Smith                }
692359e9417SChristopher Smith            }
6930440ff15Schris        }
6940440ff15Schris
69540d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
696073766c6Smatthiasgrimm            $changes['name'] = $newname;
69740d72af6SChristopher Smith        }
69840d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
699073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
70040d72af6SChristopher Smith        }
70140d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
702073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
70340d72af6SChristopher Smith        }
7040440ff15Schris
7057d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
7060440ff15Schris            msg($this->lang['update_ok'],1);
707a6858c6aSchris
7086ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
709a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
7106ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
711a6858c6aSchris            }
712a6858c6aSchris
7139ec82636SAndreas Gohr            // invalidate all sessions
7149ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
7159ec82636SAndreas Gohr
7160440ff15Schris        } else {
7170440ff15Schris            msg($this->lang['update_fail'],-1);
7180440ff15Schris        }
71978c7c8c9Schris
720a6858c6aSchris        if (!empty($re_edit)) {
721a6858c6aSchris            $this->_editUser($olduser);
7220440ff15Schris        }
7230440ff15Schris
724a6858c6aSchris        return $ok;
725a6858c6aSchris    }
726a6858c6aSchris
727a6858c6aSchris    /**
728c5a7c0c6SGerrit Uitslag     * Send password change notification email
729c5a7c0c6SGerrit Uitslag     *
730c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
731c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
732c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
733c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
734a6858c6aSchris     */
7353712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
736a6858c6aSchris
737a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
738328143f8SChristopher Smith            if ($status_alert) {
739a6858c6aSchris                msg($this->lang['notify_ok'], 1);
740328143f8SChristopher Smith            }
741a6858c6aSchris        } else {
742328143f8SChristopher Smith            if ($status_alert) {
743a6858c6aSchris                msg($this->lang['notify_fail'], -1);
744a6858c6aSchris            }
745328143f8SChristopher Smith        }
746a6858c6aSchris
747a6858c6aSchris        return $sent;
748a6858c6aSchris    }
749a6858c6aSchris
750a6858c6aSchris    /**
751359e9417SChristopher Smith     * Verify password meets minimum requirements
752359e9417SChristopher Smith     * :TODO: extend to support password strength
753359e9417SChristopher Smith     *
754359e9417SChristopher Smith     * @param string  $password   candidate string for new password
755359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
756359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
757359e9417SChristopher Smith     */
758359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
759be9008d3SChristopher Smith        global $lang;
760359e9417SChristopher Smith
7612400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
762359e9417SChristopher Smith            return false;
763359e9417SChristopher Smith        }
764359e9417SChristopher Smith
765359e9417SChristopher Smith        if ($password !== $confirm) {
766be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
767359e9417SChristopher Smith            return false;
768359e9417SChristopher Smith        }
769359e9417SChristopher Smith
770359e9417SChristopher Smith        // :TODO: test password for required strength
771359e9417SChristopher Smith
772359e9417SChristopher Smith        // if we make it this far the password is good
773359e9417SChristopher Smith        return true;
774359e9417SChristopher Smith    }
775359e9417SChristopher Smith
776359e9417SChristopher Smith    /**
777c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
778a6858c6aSchris     *
779c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
780a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7810440ff15Schris     */
7823712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
783c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7847441e340SAndreas Gohr        global $auth;
785fbfbbe8aSHakan Sandell        global $INPUT;
7860440ff15Schris
78759bc3b48SGerrit Uitslag        $user = array();
788fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
789fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
790fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
791fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
792fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
793359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7940440ff15Schris
7957441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7967441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7977441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7987441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7997441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
8000440ff15Schris
8010440ff15Schris        return $user;
8020440ff15Schris    }
8030440ff15Schris
804c5a7c0c6SGerrit Uitslag    /**
805c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
806c5a7c0c6SGerrit Uitslag     *
807c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
808c5a7c0c6SGerrit Uitslag     */
8093712ca9aSGerrit Uitslag    protected function _setFilter($op) {
8100440ff15Schris
8110440ff15Schris        $this->_filter = array();
8120440ff15Schris
8130440ff15Schris        if ($op == 'new') {
81459bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
8150440ff15Schris
8160440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
8170440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
8180440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
8190440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
8200440ff15Schris        }
8210440ff15Schris    }
8220440ff15Schris
823c5a7c0c6SGerrit Uitslag    /**
824c5a7c0c6SGerrit Uitslag     * Get the current search terms
825c5a7c0c6SGerrit Uitslag     *
826c5a7c0c6SGerrit Uitslag     * @return array
827c5a7c0c6SGerrit Uitslag     */
8283712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
829fbfbbe8aSHakan Sandell        global $INPUT;
8300440ff15Schris
831fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
8320440ff15Schris
8330440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
8340440ff15Schris        $filter = array();
8350440ff15Schris
8360440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8370440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8380440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8390440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8400440ff15Schris
8410440ff15Schris        return $filter;
8420440ff15Schris    }
8430440ff15Schris
844c5a7c0c6SGerrit Uitslag    /**
845c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
846c5a7c0c6SGerrit Uitslag     */
8473712ca9aSGerrit Uitslag    protected function _validatePagination() {
8480440ff15Schris
8490440ff15Schris        if ($this->_start >= $this->_user_total) {
8500440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8510440ff15Schris        }
8520440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8530440ff15Schris
8540440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8550440ff15Schris    }
8560440ff15Schris
857c5a7c0c6SGerrit Uitslag    /**
858c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
859c5a7c0c6SGerrit Uitslag     *
860c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8610440ff15Schris     */
8623712ca9aSGerrit Uitslag    protected function _pagination() {
8630440ff15Schris
86451d94d49Schris        $disabled = 'disabled="disabled"';
86551d94d49Schris
86659bc3b48SGerrit Uitslag        $buttons = array();
86751d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
86851d94d49Schris
86951d94d49Schris        if ($this->_user_total == -1) {
87051d94d49Schris            $buttons['last'] = $disabled;
87151d94d49Schris            $buttons['next'] = '';
87251d94d49Schris        } else {
87351d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
87451d94d49Schris        }
8750440ff15Schris
876462e9e37SMichael Große        if ($this->_lastdisabled) {
877462e9e37SMichael Große            $buttons['last'] = $disabled;
878462e9e37SMichael Große        }
879462e9e37SMichael Große
8800440ff15Schris        return $buttons;
8810440ff15Schris    }
8825c967d3dSChristopher Smith
883c5a7c0c6SGerrit Uitslag    /**
884c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8855c967d3dSChristopher Smith     */
8863712ca9aSGerrit Uitslag    protected function _export() {
8875c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8885c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8895c967d3dSChristopher Smith        $column_headings = array(
8905c967d3dSChristopher Smith            $this->lang["user_id"],
8915c967d3dSChristopher Smith            $this->lang["user_name"],
8925c967d3dSChristopher Smith            $this->lang["user_mail"],
8935c967d3dSChristopher Smith            $this->lang["user_groups"]
8945c967d3dSChristopher Smith        );
8955c967d3dSChristopher Smith
8965c967d3dSChristopher Smith        // ==============================================================================================
8975c967d3dSChristopher Smith        // GENERATE OUTPUT
8985c967d3dSChristopher Smith        // normal headers for downloading...
8995c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
9005c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
9015c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
9025c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
9035c967d3dSChristopher Smith
9045c967d3dSChristopher Smith        // output the csv
9055c967d3dSChristopher Smith        $fd = fopen('php://output','w');
9065c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
9075c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
9085c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
9095c967d3dSChristopher Smith            fputcsv($fd, $line);
9105c967d3dSChristopher Smith        }
9115c967d3dSChristopher Smith        fclose($fd);
912b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
913b2c01466SChristopher Smith
9145c967d3dSChristopher Smith        die;
9155c967d3dSChristopher Smith    }
916ae1afd2fSChristopher Smith
917c5a7c0c6SGerrit Uitslag    /**
918c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
919ae1afd2fSChristopher Smith     *
920ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
921c5a7c0c6SGerrit Uitslag     *
9225ba64050SChristopher Smith     * @return bool whether successful
923ae1afd2fSChristopher Smith     */
9243712ca9aSGerrit Uitslag    protected function _import() {
925ae1afd2fSChristopher Smith        // check we are allowed to add users
926ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
927ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
928ae1afd2fSChristopher Smith
929ae1afd2fSChristopher Smith        // check file uploaded ok.
930b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
931ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
932ae1afd2fSChristopher Smith            return false;
933ae1afd2fSChristopher Smith        }
934ae1afd2fSChristopher Smith        // retrieve users from the file
935ae1afd2fSChristopher Smith        $this->_import_failures = array();
936ae1afd2fSChristopher Smith        $import_success_count = 0;
937ae1afd2fSChristopher Smith        $import_fail_count = 0;
938ae1afd2fSChristopher Smith        $line = 0;
939ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
940ae1afd2fSChristopher Smith        if ($fd) {
941ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
942efcec72bSChristopher Smith                if (!utf8_check($csv)) {
943efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
944efcec72bSChristopher Smith                }
945*d0c0a5c4SAnika Henke                $raw = str_getcsv($csv);
946ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
947ae1afd2fSChristopher Smith                // data checks...
948ae1afd2fSChristopher Smith                if (1 == ++$line) {
949ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
950ae1afd2fSChristopher Smith                }
951ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
952ae1afd2fSChristopher Smith                    $import_fail_count++;
953ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
954ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
955ae1afd2fSChristopher Smith                    continue;
956ae1afd2fSChristopher Smith                }
957ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
958ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
959ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
960328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
961328143f8SChristopher Smith                    if (!$sent){
962328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
963328143f8SChristopher Smith                    }
964ae1afd2fSChristopher Smith                    $import_success_count++;
965ae1afd2fSChristopher Smith                } else {
966ae1afd2fSChristopher Smith                    $import_fail_count++;
967e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
968ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
969ae1afd2fSChristopher Smith                }
970ae1afd2fSChristopher Smith            }
971ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
972ae1afd2fSChristopher Smith            if ($import_fail_count) {
973ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
974ae1afd2fSChristopher Smith            }
975ae1afd2fSChristopher Smith        } else {
976ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
977ae1afd2fSChristopher Smith        }
978ae1afd2fSChristopher Smith
979ae1afd2fSChristopher Smith        // save import failures into the session
980ae1afd2fSChristopher Smith        if (!headers_sent()) {
981ae1afd2fSChristopher Smith            session_start();
982ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
983ae1afd2fSChristopher Smith            session_write_close();
984ae1afd2fSChristopher Smith        }
985c5a7c0c6SGerrit Uitslag        return true;
986ae1afd2fSChristopher Smith    }
987ae1afd2fSChristopher Smith
988c5a7c0c6SGerrit Uitslag    /**
989786dfb0eSGerrit Uitslag     * Returns cleaned user data
990c5a7c0c6SGerrit Uitslag     *
991c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
992253d4b48SGerrit Uitslag     * @param string $error
993253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
994c5a7c0c6SGerrit Uitslag     */
9953712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
996ae1afd2fSChristopher Smith        global $INPUT;
997ae1afd2fSChristopher Smith
998ae1afd2fSChristopher Smith        // kludgy ....
999ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
1000ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
1001ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
1002ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
1003ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
1004ae1afd2fSChristopher Smith
1005ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
100659bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
1007ae1afd2fSChristopher Smith        if (empty($user)) {
1008ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
1009ae1afd2fSChristopher Smith            return false;
1010ae1afd2fSChristopher Smith        }
1011ae1afd2fSChristopher Smith
1012ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
1013ae1afd2fSChristopher Smith
1014ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
1015ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
1016ae1afd2fSChristopher Smith            return false;
1017ae1afd2fSChristopher Smith        }
1018ae1afd2fSChristopher Smith
1019328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
1020328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
1021ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
1022ae1afd2fSChristopher Smith                return false;
1023ae1afd2fSChristopher Smith            }
1024328143f8SChristopher Smith        } else {
1025328143f8SChristopher Smith            if (!empty($mail)) {
1026328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
1027328143f8SChristopher Smith                return false;
1028328143f8SChristopher Smith            }
1029328143f8SChristopher Smith        }
1030ae1afd2fSChristopher Smith
1031ae1afd2fSChristopher Smith        return $cleaned;
1032ae1afd2fSChristopher Smith    }
1033ae1afd2fSChristopher Smith
1034c5a7c0c6SGerrit Uitslag    /**
1035c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1036c5a7c0c6SGerrit Uitslag     *
1037c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1038c5a7c0c6SGerrit Uitslag     *
1039c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
1040c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10415ba64050SChristopher Smith     * @return bool whether successful
1042c5a7c0c6SGerrit Uitslag     */
10433712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1044ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1045ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1046ae1afd2fSChristopher Smith            return false;
1047ae1afd2fSChristopher Smith        }
1048ae1afd2fSChristopher Smith
1049ae1afd2fSChristopher Smith        return true;
1050ae1afd2fSChristopher Smith    }
1051ae1afd2fSChristopher Smith
1052c5a7c0c6SGerrit Uitslag    /**
1053c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1054c5a7c0c6SGerrit Uitslag     */
10553712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1056ae1afd2fSChristopher Smith
1057ae1afd2fSChristopher Smith        // ==============================================================================================
1058ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1059ae1afd2fSChristopher Smith        // normal headers for downloading...
1060ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1061ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1062ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1063ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1064ae1afd2fSChristopher Smith
1065ae1afd2fSChristopher Smith        // output the csv
1066ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1067c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1068ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1069ae1afd2fSChristopher Smith        }
1070ae1afd2fSChristopher Smith        fclose($fd);
1071ae1afd2fSChristopher Smith        die;
1072ae1afd2fSChristopher Smith    }
1073ae1afd2fSChristopher Smith
1074b2c01466SChristopher Smith    /**
1075b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1076253d4b48SGerrit Uitslag     *
1077253d4b48SGerrit Uitslag     * @param string $file filename
1078253d4b48SGerrit Uitslag     * @return bool
1079b2c01466SChristopher Smith     */
1080b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1081b2c01466SChristopher Smith        return is_uploaded_file($file);
1082b2c01466SChristopher Smith    }
10830440ff15Schris}
1084