xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 64cdf7793cbaf7c5b5e6cc05ef184827b5efa5ec)
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    /**
301*64cdf779SAndreas Gohr     * User Manager is only available if the auth backend supports it
302*64cdf779SAndreas Gohr     *
303*64cdf779SAndreas Gohr     * @inheritdoc
304*64cdf779SAndreas Gohr     * @return bool
305*64cdf779SAndreas Gohr     */
306*64cdf779SAndreas Gohr    public function isAccessibleByCurrentUser()
307*64cdf779SAndreas Gohr    {
308*64cdf779SAndreas Gohr        /** @var DokuWiki_Auth_Plugin $auth */
309*64cdf779SAndreas Gohr        global $auth;
310*64cdf779SAndreas Gohr        if(!$auth || !$auth->canDo('getUsers') ) {
311*64cdf779SAndreas Gohr            return false;
312*64cdf779SAndreas Gohr        }
313*64cdf779SAndreas Gohr
314*64cdf779SAndreas Gohr        return parent::isAccessibleByCurrentUser();
315*64cdf779SAndreas Gohr    }
316*64cdf779SAndreas Gohr
317*64cdf779SAndreas Gohr
318*64cdf779SAndreas Gohr    /**
319c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
320c5a7c0c6SGerrit Uitslag     *
321c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
322c5a7c0c6SGerrit Uitslag     * @param string $user id of user
323c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
324c5a7c0c6SGerrit Uitslag     * @param int    $indent
32582fd59b6SAndreas Gohr     */
3263712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
327a6858c6aSchris        global $conf;
328bb4866bdSchris        global $ID;
329be9008d3SChristopher Smith        global $lang;
33078c7c8c9Schris
33178c7c8c9Schris        $name = $mail = $groups = '';
332a6858c6aSchris        $notes = array();
3330440ff15Schris
3340440ff15Schris        if ($user) {
33578c7c8c9Schris            extract($userdata);
33678c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
337a6858c6aSchris        } else {
338a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
3390440ff15Schris        }
3400440ff15Schris
3410440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
342634d7150SAndreas Gohr        formSecurityToken();
343c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
3440440ff15Schris        ptln("  <table class=\"inline\">",$indent);
3450440ff15Schris        ptln("    <thead>",$indent);
3460440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
3470440ff15Schris        ptln("    </thead>",$indent);
3480440ff15Schris        ptln("    <tbody>",$indent);
34926fb387bSchris
3509b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"),   true, $indent+6);
3519b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),   false, $indent+6);
3529b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),   false, $indent+6);
3539b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),    true, $indent+6);
3549b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),    true, $indent+6);
3559b82d43fSAndreas Gohr        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"), false, $indent+6);
35626fb387bSchris
357a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
358ee9498f5SChristopher Smith            if ($cmd == 'add') {
359c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
360ee9498f5SChristopher Smith            }
361a6858c6aSchris            if ($user) {
362a6858c6aSchris                $notes[] = $this->lang['note_notify'];
363a6858c6aSchris            }
364a6858c6aSchris
365a6858c6aSchris            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);
366a6858c6aSchris        }
367a6858c6aSchris
3680440ff15Schris        ptln("    </tbody>",$indent);
3690440ff15Schris        ptln("    <tbody>",$indent);
3700440ff15Schris        ptln("      <tr>",$indent);
3710440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3720440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3730440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3740440ff15Schris
3750440ff15Schris        // save current $user, we need this to access details if the name is changed
3760440ff15Schris        if ($user)
377f23f9594SAndreas Gohr          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".hsc($user)."\" />",$indent);
3780440ff15Schris
3790440ff15Schris        $this->_htmlFilterSettings($indent+10);
3800440ff15Schris
381ae614416SAnika Henke        ptln("          <button type=\"submit\" name=\"fn[".$cmd."]\">".$this->lang[$cmd]."</button>",$indent);
3820440ff15Schris        ptln("        </td>",$indent);
3830440ff15Schris        ptln("      </tr>",$indent);
3840440ff15Schris        ptln("    </tbody>",$indent);
3850440ff15Schris        ptln("  </table>",$indent);
38645c19902SChristopher Smith
38745c19902SChristopher Smith        if ($notes) {
38845c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
38945c19902SChristopher Smith            foreach ($notes as $note) {
390ae614416SAnika Henke                ptln("      <li><span class=\"li\">".$note."</li>",$indent);
39145c19902SChristopher Smith            }
39245c19902SChristopher Smith            ptln("    </ul>");
39345c19902SChristopher Smith        }
394c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3950440ff15Schris        ptln("</form>",$indent);
3960440ff15Schris    }
3970440ff15Schris
398c5a7c0c6SGerrit Uitslag    /**
399c5a7c0c6SGerrit Uitslag     * Prints a inputfield
400c5a7c0c6SGerrit Uitslag     *
401c5a7c0c6SGerrit Uitslag     * @param string $id
402c5a7c0c6SGerrit Uitslag     * @param string $name
403c5a7c0c6SGerrit Uitslag     * @param string $label
404c5a7c0c6SGerrit Uitslag     * @param string $value
405c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
4069b82d43fSAndreas Gohr     * @param bool   $required is this field required?
407c5a7c0c6SGerrit Uitslag     * @param int $indent
408c5a7c0c6SGerrit Uitslag     */
4099b82d43fSAndreas Gohr    protected function _htmlInputField($id, $name, $label, $value, $cando, $required, $indent=0) {
4107de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
4117de12fceSAndreas Gohr        echo str_pad('',$indent);
4127de12fceSAndreas Gohr
413359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
414d796a891SAndreas Gohr            $fieldtype = 'password';
415d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
4167b3674bdSChristopher Smith        }elseif($name == 'usermail'){
4177b3674bdSChristopher Smith            $fieldtype = 'email';
4187b3674bdSChristopher Smith            $autocomp  = '';
419d796a891SAndreas Gohr        }else{
420d796a891SAndreas Gohr            $fieldtype = 'text';
421d796a891SAndreas Gohr            $autocomp  = '';
422d796a891SAndreas Gohr        }
423f23f9594SAndreas Gohr        $value = hsc($value);
424d796a891SAndreas Gohr
4257de12fceSAndreas Gohr        echo "<tr $class>";
4267de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
4277de12fceSAndreas Gohr        echo "<td>";
4287de12fceSAndreas Gohr        if($cando){
4299b82d43fSAndreas Gohr            $req = '';
4309b82d43fSAndreas Gohr            if($required) $req = 'required="required"';
4319b82d43fSAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp $req />";
4327de12fceSAndreas Gohr        }else{
4337de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
434ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
4357de12fceSAndreas Gohr        }
4367de12fceSAndreas Gohr        echo "</td>";
4377de12fceSAndreas Gohr        echo "</tr>";
43826fb387bSchris    }
43926fb387bSchris
440c5a7c0c6SGerrit Uitslag    /**
441c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
442c5a7c0c6SGerrit Uitslag     *
443c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
444c5a7c0c6SGerrit Uitslag     * @return string html escaped value
445c5a7c0c6SGerrit Uitslag     */
4463712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
4470440ff15Schris        if (empty($this->_filter)) return '';
4480440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
4490440ff15Schris    }
4500440ff15Schris
451c5a7c0c6SGerrit Uitslag    /**
452c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
453c5a7c0c6SGerrit Uitslag     *
454c5a7c0c6SGerrit Uitslag     * @param int $indent
455c5a7c0c6SGerrit Uitslag     */
4563712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4570440ff15Schris
4580440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4590440ff15Schris
4600440ff15Schris        foreach ($this->_filter as $key => $filter) {
4610440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4620440ff15Schris        }
4630440ff15Schris    }
4640440ff15Schris
465c5a7c0c6SGerrit Uitslag    /**
466c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
467c5a7c0c6SGerrit Uitslag     *
468c5a7c0c6SGerrit Uitslag     * @param int $indent
469c5a7c0c6SGerrit Uitslag     */
4703712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
471ae1afd2fSChristopher Smith        global $ID;
472ae1afd2fSChristopher Smith
473ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
474ae1afd2fSChristopher Smith
475ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
476ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
477ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
478ae1afd2fSChristopher Smith        formSecurityToken();
479b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
480ae614416SAnika Henke        ptln('    <button type="submit" name="fn[import]">'.$this->lang['import'].'</button>',$indent);
481ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
482ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
483ae1afd2fSChristopher Smith
484ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
485ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
486ae1afd2fSChristopher Smith        ptln('</div>');
487ae1afd2fSChristopher Smith
488ae1afd2fSChristopher Smith        // list failures from the previous import
489ae1afd2fSChristopher Smith        if ($this->_import_failures) {
490ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
491ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
492b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
493ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
494ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
495ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
496ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
497ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
498ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
499ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
500ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
501ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
502ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
503ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
504ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
505ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
506ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
507ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
508ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
509ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
510ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
511ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
512ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
513ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
514ae1afd2fSChristopher Smith            }
515ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
516ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
517b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
518ae1afd2fSChristopher Smith            ptln('</div>');
519ae1afd2fSChristopher Smith        }
520ae1afd2fSChristopher Smith
521ae1afd2fSChristopher Smith    }
522ae1afd2fSChristopher Smith
523c5a7c0c6SGerrit Uitslag    /**
524c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
525c5a7c0c6SGerrit Uitslag     *
526c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
527c5a7c0c6SGerrit Uitslag     */
5283712ca9aSGerrit Uitslag    protected function _addUser(){
52900d58927SMichael Hamann        global $INPUT;
530634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
53182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
5320440ff15Schris
533359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
5340440ff15Schris        if (empty($user)) return false;
5356733c4d7SChris Smith
5366733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
537c3f4fb63SGina Haeussge            if (empty($pass)){
53800d58927SMichael Hamann                if($INPUT->has('usernotify')){
5398a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
540c3f4fb63SGina Haeussge                } else {
54160b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
54209a5dcd6SMichael Große                    msg($this->lang['addUser_error_missing_pass'], -1);
54360b9901bSAndreas Gohr                    return false;
54460b9901bSAndreas Gohr                }
545359e9417SChristopher Smith            } else {
546359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
54709a5dcd6SMichael Große                    msg($this->lang['add_fail'], -1);
54809a5dcd6SMichael Große                    msg($this->lang['addUser_error_pass_not_identical'], -1);
549359e9417SChristopher Smith                    return false;
550359e9417SChristopher Smith                }
5516733c4d7SChris Smith            }
5526733c4d7SChris Smith        } else {
5536733c4d7SChris Smith            if (!empty($pass)){
5546733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
55509a5dcd6SMichael Große                msg($this->lang['addUser_error_modPass_disabled'], -1);
5566733c4d7SChris Smith                return false;
5576733c4d7SChris Smith            }
5586733c4d7SChris Smith        }
5596733c4d7SChris Smith
5606733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5616733c4d7SChris Smith            if (empty($name)){
5626733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
56309a5dcd6SMichael Große                msg($this->lang['addUser_error_name_missing'], -1);
5646733c4d7SChris Smith                return false;
5656733c4d7SChris Smith            }
5666733c4d7SChris Smith        } else {
5676733c4d7SChris Smith            if (!empty($name)){
56809a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
56909a5dcd6SMichael Große                msg($this->lang['addUser_error_modName_disabled'], -1);
5706733c4d7SChris Smith                return false;
5716733c4d7SChris Smith            }
5726733c4d7SChris Smith        }
5736733c4d7SChris Smith
5746733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5756733c4d7SChris Smith            if (empty($mail)){
5766733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
57709a5dcd6SMichael Große                msg($this->lang['addUser_error_mail_missing'], -1);
5786733c4d7SChris Smith                return false;
5796733c4d7SChris Smith            }
5806733c4d7SChris Smith        } else {
5816733c4d7SChris Smith            if (!empty($mail)){
58209a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
58309a5dcd6SMichael Große                msg($this->lang['addUser_error_modMail_disabled'], -1);
5846733c4d7SChris Smith                return false;
5856733c4d7SChris Smith            }
5866733c4d7SChris Smith        }
5870440ff15Schris
5887d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
589a6858c6aSchris
590a6858c6aSchris            msg($this->lang['add_ok'], 1);
591a6858c6aSchris
59200d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
593a6858c6aSchris                $this->_notifyUser($user,$pass);
594a6858c6aSchris            }
595a6858c6aSchris        } else {
59660b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
59709a5dcd6SMichael Große            msg($this->lang['addUser_error_create_event_failed'], -1);
598a6858c6aSchris        }
599a6858c6aSchris
600a6858c6aSchris        return $ok;
6010440ff15Schris    }
6020440ff15Schris
6030440ff15Schris    /**
604c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
605c5a7c0c6SGerrit Uitslag     *
606c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6070440ff15Schris     */
6083712ca9aSGerrit Uitslag    protected function _deleteUser(){
60900d58927SMichael Hamann        global $conf, $INPUT;
6109ec82636SAndreas Gohr
611634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
61282fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
6130440ff15Schris
61400d58927SMichael Hamann        $selected = $INPUT->arr('delete');
61500d58927SMichael Hamann        if (empty($selected)) return false;
6160440ff15Schris        $selected = array_keys($selected);
6170440ff15Schris
618c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
619c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
620c9a8f912SMichael Klier            return false;
621c9a8f912SMichael Klier        }
622c9a8f912SMichael Klier
6237d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
6240440ff15Schris        if ($count == count($selected)) {
6250440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
6260440ff15Schris            msg("$text.", 1);
6270440ff15Schris        } else {
6280440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
6290440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
6300440ff15Schris            msg("$part1, $part2",-1);
6310440ff15Schris        }
63278c7c8c9Schris
6339ec82636SAndreas Gohr        // invalidate all sessions
6349ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
6359ec82636SAndreas Gohr
63678c7c8c9Schris        return true;
63778c7c8c9Schris    }
63878c7c8c9Schris
63978c7c8c9Schris    /**
64078c7c8c9Schris     * Edit user (a user has been selected for editing)
641c5a7c0c6SGerrit Uitslag     *
642c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
643c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
64478c7c8c9Schris     */
6453712ca9aSGerrit Uitslag    protected function _editUser($param) {
646634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
64778c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
648786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
64978c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
65078c7c8c9Schris
65178c7c8c9Schris        // no user found?
65278c7c8c9Schris        if (!$userdata) {
65378c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
65478c7c8c9Schris            return false;
65578c7c8c9Schris        }
65678c7c8c9Schris
65778c7c8c9Schris        $this->_edit_user = $user;
65878c7c8c9Schris        $this->_edit_userdata = $userdata;
65978c7c8c9Schris
66078c7c8c9Schris        return true;
6610440ff15Schris    }
6620440ff15Schris
6630440ff15Schris    /**
664c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
665c5a7c0c6SGerrit Uitslag     *
666c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6670440ff15Schris     */
6683712ca9aSGerrit Uitslag    protected function _modifyUser(){
66900d58927SMichael Hamann        global $conf, $INPUT;
6709ec82636SAndreas Gohr
671634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
67282fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6730440ff15Schris
67426fb387bSchris        // get currently valid  user data
675786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
676073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
677073766c6Smatthiasgrimm
67826fb387bSchris        // get new user data subject to change
679359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
680073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6810440ff15Schris
6820440ff15Schris        $changes = array();
683073766c6Smatthiasgrimm        if ($newuser != $olduser) {
68426fb387bSchris
68526fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
68626fb387bSchris                msg($this->lang['update_fail'],-1);
68726fb387bSchris                return false;
68826fb387bSchris            }
68926fb387bSchris
69026fb387bSchris            // check if $newuser already exists
691073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
692073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
693a6858c6aSchris                $re_edit = true;
6940440ff15Schris            } else {
695073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6960440ff15Schris            }
69793eefc2fSAndreas Gohr        }
698359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6992400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
700359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
701359e9417SChristopher Smith                    $changes['pass'] = $newpass;
702359e9417SChristopher Smith                } else {
703359e9417SChristopher Smith                    return false;
704359e9417SChristopher Smith                }
705359e9417SChristopher Smith            } else {
706359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
707359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
708359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
709359e9417SChristopher Smith                }
710359e9417SChristopher Smith            }
7110440ff15Schris        }
7120440ff15Schris
71340d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
714073766c6Smatthiasgrimm            $changes['name'] = $newname;
71540d72af6SChristopher Smith        }
71640d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
717073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
71840d72af6SChristopher Smith        }
71940d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
720073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
72140d72af6SChristopher Smith        }
7220440ff15Schris
7237d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
7240440ff15Schris            msg($this->lang['update_ok'],1);
725a6858c6aSchris
7266ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
727a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
7286ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
729a6858c6aSchris            }
730a6858c6aSchris
7319ec82636SAndreas Gohr            // invalidate all sessions
7329ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
7339ec82636SAndreas Gohr
7340440ff15Schris        } else {
7350440ff15Schris            msg($this->lang['update_fail'],-1);
7360440ff15Schris        }
73778c7c8c9Schris
738a6858c6aSchris        if (!empty($re_edit)) {
739a6858c6aSchris            $this->_editUser($olduser);
7400440ff15Schris        }
7410440ff15Schris
742a6858c6aSchris        return $ok;
743a6858c6aSchris    }
744a6858c6aSchris
745a6858c6aSchris    /**
746c5a7c0c6SGerrit Uitslag     * Send password change notification email
747c5a7c0c6SGerrit Uitslag     *
748c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
749c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
750c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
751c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
752a6858c6aSchris     */
7533712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
754a6858c6aSchris
755a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
756328143f8SChristopher Smith            if ($status_alert) {
757a6858c6aSchris                msg($this->lang['notify_ok'], 1);
758328143f8SChristopher Smith            }
759a6858c6aSchris        } else {
760328143f8SChristopher Smith            if ($status_alert) {
761a6858c6aSchris                msg($this->lang['notify_fail'], -1);
762a6858c6aSchris            }
763328143f8SChristopher Smith        }
764a6858c6aSchris
765a6858c6aSchris        return $sent;
766a6858c6aSchris    }
767a6858c6aSchris
768a6858c6aSchris    /**
769359e9417SChristopher Smith     * Verify password meets minimum requirements
770359e9417SChristopher Smith     * :TODO: extend to support password strength
771359e9417SChristopher Smith     *
772359e9417SChristopher Smith     * @param string  $password   candidate string for new password
773359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
774359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
775359e9417SChristopher Smith     */
776359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
777be9008d3SChristopher Smith        global $lang;
778359e9417SChristopher Smith
7792400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
780359e9417SChristopher Smith            return false;
781359e9417SChristopher Smith        }
782359e9417SChristopher Smith
783359e9417SChristopher Smith        if ($password !== $confirm) {
784be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
785359e9417SChristopher Smith            return false;
786359e9417SChristopher Smith        }
787359e9417SChristopher Smith
788359e9417SChristopher Smith        // :TODO: test password for required strength
789359e9417SChristopher Smith
790359e9417SChristopher Smith        // if we make it this far the password is good
791359e9417SChristopher Smith        return true;
792359e9417SChristopher Smith    }
793359e9417SChristopher Smith
794359e9417SChristopher Smith    /**
795c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
796a6858c6aSchris     *
797c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
798a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7990440ff15Schris     */
8003712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
801c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
8027441e340SAndreas Gohr        global $auth;
803fbfbbe8aSHakan Sandell        global $INPUT;
8040440ff15Schris
80559bc3b48SGerrit Uitslag        $user = array();
806fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
807fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
808fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
809fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
810fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
811359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
8120440ff15Schris
8137441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
8147441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
8157441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
8167441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
8177441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
8180440ff15Schris
8190440ff15Schris        return $user;
8200440ff15Schris    }
8210440ff15Schris
822c5a7c0c6SGerrit Uitslag    /**
823c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
824c5a7c0c6SGerrit Uitslag     *
825c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
826c5a7c0c6SGerrit Uitslag     */
8273712ca9aSGerrit Uitslag    protected function _setFilter($op) {
8280440ff15Schris
8290440ff15Schris        $this->_filter = array();
8300440ff15Schris
8310440ff15Schris        if ($op == 'new') {
83259bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
8330440ff15Schris
8340440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
8350440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
8360440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
8370440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
8380440ff15Schris        }
8390440ff15Schris    }
8400440ff15Schris
841c5a7c0c6SGerrit Uitslag    /**
842c5a7c0c6SGerrit Uitslag     * Get the current search terms
843c5a7c0c6SGerrit Uitslag     *
844c5a7c0c6SGerrit Uitslag     * @return array
845c5a7c0c6SGerrit Uitslag     */
8463712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
847fbfbbe8aSHakan Sandell        global $INPUT;
8480440ff15Schris
849fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
8500440ff15Schris
8510440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
8520440ff15Schris        $filter = array();
8530440ff15Schris
8540440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8550440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8560440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8570440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8580440ff15Schris
8590440ff15Schris        return $filter;
8600440ff15Schris    }
8610440ff15Schris
862c5a7c0c6SGerrit Uitslag    /**
863c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
864c5a7c0c6SGerrit Uitslag     */
8653712ca9aSGerrit Uitslag    protected function _validatePagination() {
8660440ff15Schris
8670440ff15Schris        if ($this->_start >= $this->_user_total) {
8680440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8690440ff15Schris        }
8700440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8710440ff15Schris
8720440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8730440ff15Schris    }
8740440ff15Schris
875c5a7c0c6SGerrit Uitslag    /**
876c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
877c5a7c0c6SGerrit Uitslag     *
878c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8790440ff15Schris     */
8803712ca9aSGerrit Uitslag    protected function _pagination() {
8810440ff15Schris
88251d94d49Schris        $disabled = 'disabled="disabled"';
88351d94d49Schris
88459bc3b48SGerrit Uitslag        $buttons = array();
88551d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
88651d94d49Schris
88751d94d49Schris        if ($this->_user_total == -1) {
88851d94d49Schris            $buttons['last'] = $disabled;
88951d94d49Schris            $buttons['next'] = '';
89051d94d49Schris        } else {
89151d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
89251d94d49Schris        }
8930440ff15Schris
894462e9e37SMichael Große        if ($this->_lastdisabled) {
895462e9e37SMichael Große            $buttons['last'] = $disabled;
896462e9e37SMichael Große        }
897462e9e37SMichael Große
8980440ff15Schris        return $buttons;
8990440ff15Schris    }
9005c967d3dSChristopher Smith
901c5a7c0c6SGerrit Uitslag    /**
902c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
9035c967d3dSChristopher Smith     */
9043712ca9aSGerrit Uitslag    protected function _export() {
9055c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
9065c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
9075c967d3dSChristopher Smith        $column_headings = array(
9085c967d3dSChristopher Smith            $this->lang["user_id"],
9095c967d3dSChristopher Smith            $this->lang["user_name"],
9105c967d3dSChristopher Smith            $this->lang["user_mail"],
9115c967d3dSChristopher Smith            $this->lang["user_groups"]
9125c967d3dSChristopher Smith        );
9135c967d3dSChristopher Smith
9145c967d3dSChristopher Smith        // ==============================================================================================
9155c967d3dSChristopher Smith        // GENERATE OUTPUT
9165c967d3dSChristopher Smith        // normal headers for downloading...
9175c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
9185c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
9195c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
9205c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
9215c967d3dSChristopher Smith
9225c967d3dSChristopher Smith        // output the csv
9235c967d3dSChristopher Smith        $fd = fopen('php://output','w');
9245c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
9255c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
9265c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
9275c967d3dSChristopher Smith            fputcsv($fd, $line);
9285c967d3dSChristopher Smith        }
9295c967d3dSChristopher Smith        fclose($fd);
930b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
931b2c01466SChristopher Smith
9325c967d3dSChristopher Smith        die;
9335c967d3dSChristopher Smith    }
934ae1afd2fSChristopher Smith
935c5a7c0c6SGerrit Uitslag    /**
936c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
937ae1afd2fSChristopher Smith     *
938ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
939c5a7c0c6SGerrit Uitslag     *
9405ba64050SChristopher Smith     * @return bool whether successful
941ae1afd2fSChristopher Smith     */
9423712ca9aSGerrit Uitslag    protected function _import() {
943ae1afd2fSChristopher Smith        // check we are allowed to add users
944ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
945ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
946ae1afd2fSChristopher Smith
947ae1afd2fSChristopher Smith        // check file uploaded ok.
948b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
949ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
950ae1afd2fSChristopher Smith            return false;
951ae1afd2fSChristopher Smith        }
952ae1afd2fSChristopher Smith        // retrieve users from the file
953ae1afd2fSChristopher Smith        $this->_import_failures = array();
954ae1afd2fSChristopher Smith        $import_success_count = 0;
955ae1afd2fSChristopher Smith        $import_fail_count = 0;
956ae1afd2fSChristopher Smith        $line = 0;
957ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
958ae1afd2fSChristopher Smith        if ($fd) {
959ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
960efcec72bSChristopher Smith                if (!utf8_check($csv)) {
961efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
962efcec72bSChristopher Smith                }
963d0c0a5c4SAnika Henke                $raw = str_getcsv($csv);
964ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
965ae1afd2fSChristopher Smith                // data checks...
966ae1afd2fSChristopher Smith                if (1 == ++$line) {
967ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
968ae1afd2fSChristopher Smith                }
969ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
970ae1afd2fSChristopher Smith                    $import_fail_count++;
971ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
972ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
973ae1afd2fSChristopher Smith                    continue;
974ae1afd2fSChristopher Smith                }
975ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
976ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
977ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
978328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
979328143f8SChristopher Smith                    if (!$sent){
980328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
981328143f8SChristopher Smith                    }
982ae1afd2fSChristopher Smith                    $import_success_count++;
983ae1afd2fSChristopher Smith                } else {
984ae1afd2fSChristopher Smith                    $import_fail_count++;
985e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
986ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
987ae1afd2fSChristopher Smith                }
988ae1afd2fSChristopher Smith            }
989ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
990ae1afd2fSChristopher Smith            if ($import_fail_count) {
991ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
992ae1afd2fSChristopher Smith            }
993ae1afd2fSChristopher Smith        } else {
994ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
995ae1afd2fSChristopher Smith        }
996ae1afd2fSChristopher Smith
997ae1afd2fSChristopher Smith        // save import failures into the session
998ae1afd2fSChristopher Smith        if (!headers_sent()) {
999ae1afd2fSChristopher Smith            session_start();
1000ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
1001ae1afd2fSChristopher Smith            session_write_close();
1002ae1afd2fSChristopher Smith        }
1003c5a7c0c6SGerrit Uitslag        return true;
1004ae1afd2fSChristopher Smith    }
1005ae1afd2fSChristopher Smith
1006c5a7c0c6SGerrit Uitslag    /**
1007786dfb0eSGerrit Uitslag     * Returns cleaned user data
1008c5a7c0c6SGerrit Uitslag     *
1009c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
1010253d4b48SGerrit Uitslag     * @param string $error
1011253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
1012c5a7c0c6SGerrit Uitslag     */
10133712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
1014ae1afd2fSChristopher Smith        global $INPUT;
1015ae1afd2fSChristopher Smith
1016ae1afd2fSChristopher Smith        // kludgy ....
1017ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
1018ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
1019ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
1020ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
1021ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
1022ae1afd2fSChristopher Smith
1023ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
102459bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
1025ae1afd2fSChristopher Smith        if (empty($user)) {
1026ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
1027ae1afd2fSChristopher Smith            return false;
1028ae1afd2fSChristopher Smith        }
1029ae1afd2fSChristopher Smith
1030ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
1031ae1afd2fSChristopher Smith
1032ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
1033ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
1034ae1afd2fSChristopher Smith            return false;
1035ae1afd2fSChristopher Smith        }
1036ae1afd2fSChristopher Smith
1037328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
1038328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
1039ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
1040ae1afd2fSChristopher Smith                return false;
1041ae1afd2fSChristopher Smith            }
1042328143f8SChristopher Smith        } else {
1043328143f8SChristopher Smith            if (!empty($mail)) {
1044328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
1045328143f8SChristopher Smith                return false;
1046328143f8SChristopher Smith            }
1047328143f8SChristopher Smith        }
1048ae1afd2fSChristopher Smith
1049ae1afd2fSChristopher Smith        return $cleaned;
1050ae1afd2fSChristopher Smith    }
1051ae1afd2fSChristopher Smith
1052c5a7c0c6SGerrit Uitslag    /**
1053c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1054c5a7c0c6SGerrit Uitslag     *
1055c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1056c5a7c0c6SGerrit Uitslag     *
1057c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
1058c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10595ba64050SChristopher Smith     * @return bool whether successful
1060c5a7c0c6SGerrit Uitslag     */
10613712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1062ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1063ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1064ae1afd2fSChristopher Smith            return false;
1065ae1afd2fSChristopher Smith        }
1066ae1afd2fSChristopher Smith
1067ae1afd2fSChristopher Smith        return true;
1068ae1afd2fSChristopher Smith    }
1069ae1afd2fSChristopher Smith
1070c5a7c0c6SGerrit Uitslag    /**
1071c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1072c5a7c0c6SGerrit Uitslag     */
10733712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1074ae1afd2fSChristopher Smith
1075ae1afd2fSChristopher Smith        // ==============================================================================================
1076ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1077ae1afd2fSChristopher Smith        // normal headers for downloading...
1078ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1079ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1080ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1081ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1082ae1afd2fSChristopher Smith
1083ae1afd2fSChristopher Smith        // output the csv
1084ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1085c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1086ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1087ae1afd2fSChristopher Smith        }
1088ae1afd2fSChristopher Smith        fclose($fd);
1089ae1afd2fSChristopher Smith        die;
1090ae1afd2fSChristopher Smith    }
1091ae1afd2fSChristopher Smith
1092b2c01466SChristopher Smith    /**
1093b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1094253d4b48SGerrit Uitslag     *
1095253d4b48SGerrit Uitslag     * @param string $file filename
1096253d4b48SGerrit Uitslag     * @return bool
1097b2c01466SChristopher Smith     */
1098b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1099b2c01466SChristopher Smith        return is_uploaded_file($file);
1100b2c01466SChristopher Smith    }
11010440ff15Schris}
1102