xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 09a5dcd6ff9498dd0dc9adfd5a2dfd50771ae97b)
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
33226fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
33326fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
334be9008d3SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),  $indent+6);
33526fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
33626fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
33726fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
33826fb387bSchris
339a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
340ee9498f5SChristopher Smith            if ($cmd == 'add') {
341c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
342ee9498f5SChristopher Smith            }
343a6858c6aSchris            if ($user) {
344a6858c6aSchris                $notes[] = $this->lang['note_notify'];
345a6858c6aSchris            }
346a6858c6aSchris
347a6858c6aSchris            ptln("<tr><td><label for=\"".$cmd."_usernotify\" >".$this->lang["user_notify"].": </label></td><td><input type=\"checkbox\" id=\"".$cmd."_usernotify\" name=\"usernotify\" value=\"1\" /></td></tr>", $indent);
348a6858c6aSchris        }
349a6858c6aSchris
3500440ff15Schris        ptln("    </tbody>",$indent);
3510440ff15Schris        ptln("    <tbody>",$indent);
3520440ff15Schris        ptln("      <tr>",$indent);
3530440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3540440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3550440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3560440ff15Schris
3570440ff15Schris        // save current $user, we need this to access details if the name is changed
3580440ff15Schris        if ($user)
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
388c5a7c0c6SGerrit Uitslag     * @param int $indent
389c5a7c0c6SGerrit Uitslag     */
3903712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3917de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3927de12fceSAndreas Gohr        echo str_pad('',$indent);
3937de12fceSAndreas Gohr
394359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
395d796a891SAndreas Gohr            $fieldtype = 'password';
396d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3977b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3987b3674bdSChristopher Smith            $fieldtype = 'email';
3997b3674bdSChristopher Smith            $autocomp  = '';
400d796a891SAndreas Gohr        }else{
401d796a891SAndreas Gohr            $fieldtype = 'text';
402d796a891SAndreas Gohr            $autocomp  = '';
403d796a891SAndreas Gohr        }
404f23f9594SAndreas Gohr        $value = hsc($value);
405d796a891SAndreas Gohr
4067de12fceSAndreas Gohr        echo "<tr $class>";
4077de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
4087de12fceSAndreas Gohr        echo "<td>";
4097de12fceSAndreas Gohr        if($cando){
410d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
4117de12fceSAndreas Gohr        }else{
4127de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
413ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
4147de12fceSAndreas Gohr        }
4157de12fceSAndreas Gohr        echo "</td>";
4167de12fceSAndreas Gohr        echo "</tr>";
41726fb387bSchris    }
41826fb387bSchris
419c5a7c0c6SGerrit Uitslag    /**
420c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
421c5a7c0c6SGerrit Uitslag     *
422c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
423c5a7c0c6SGerrit Uitslag     * @return string html escaped value
424c5a7c0c6SGerrit Uitslag     */
4253712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
4260440ff15Schris        if (empty($this->_filter)) return '';
4270440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
4280440ff15Schris    }
4290440ff15Schris
430c5a7c0c6SGerrit Uitslag    /**
431c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
432c5a7c0c6SGerrit Uitslag     *
433c5a7c0c6SGerrit Uitslag     * @param int $indent
434c5a7c0c6SGerrit Uitslag     */
4353712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4360440ff15Schris
4370440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4380440ff15Schris
4390440ff15Schris        foreach ($this->_filter as $key => $filter) {
4400440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4410440ff15Schris        }
4420440ff15Schris    }
4430440ff15Schris
444c5a7c0c6SGerrit Uitslag    /**
445c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
446c5a7c0c6SGerrit Uitslag     *
447c5a7c0c6SGerrit Uitslag     * @param int $indent
448c5a7c0c6SGerrit Uitslag     */
4493712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
450ae1afd2fSChristopher Smith        global $ID;
451ae1afd2fSChristopher Smith
452ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
453ae1afd2fSChristopher Smith
454ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
455ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
456ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
457ae1afd2fSChristopher Smith        formSecurityToken();
458b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
459ae614416SAnika Henke        ptln('    <button type="submit" name="fn[import]">'.$this->lang['import'].'</button>',$indent);
460ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
461ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
462ae1afd2fSChristopher Smith
463ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
464ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
465ae1afd2fSChristopher Smith        ptln('</div>');
466ae1afd2fSChristopher Smith
467ae1afd2fSChristopher Smith        // list failures from the previous import
468ae1afd2fSChristopher Smith        if ($this->_import_failures) {
469ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
470ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
471b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
472ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
473ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
474ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
475ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
476ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
477ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
478ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
479ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
480ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
481ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
482ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
483ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
484ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
485ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
486ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
487ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
488ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
489ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
490ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
491ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
492ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
493ae1afd2fSChristopher Smith            }
494ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
495ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
496b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
497ae1afd2fSChristopher Smith            ptln('</div>');
498ae1afd2fSChristopher Smith        }
499ae1afd2fSChristopher Smith
500ae1afd2fSChristopher Smith    }
501ae1afd2fSChristopher Smith
502c5a7c0c6SGerrit Uitslag    /**
503c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
504c5a7c0c6SGerrit Uitslag     *
505c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
506c5a7c0c6SGerrit Uitslag     */
5073712ca9aSGerrit Uitslag    protected function _addUser(){
50800d58927SMichael Hamann        global $INPUT;
509634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
51082fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
5110440ff15Schris
512359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
5130440ff15Schris        if (empty($user)) return false;
5146733c4d7SChris Smith
5156733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
516c3f4fb63SGina Haeussge            if (empty($pass)){
51700d58927SMichael Hamann                if($INPUT->has('usernotify')){
5188a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
519c3f4fb63SGina Haeussge                } else {
52060b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
521*09a5dcd6SMichael Große                    msg($this->lang['addUser_error_missing_pass'], -1);
52260b9901bSAndreas Gohr                    return false;
52360b9901bSAndreas Gohr                }
524359e9417SChristopher Smith            } else {
525359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
526*09a5dcd6SMichael Große                    msg($this->lang['add_fail'], -1);
527*09a5dcd6SMichael Große                    msg($this->lang['addUser_error_pass_not_identical'], -1);
528359e9417SChristopher Smith                    return false;
529359e9417SChristopher Smith                }
5306733c4d7SChris Smith            }
5316733c4d7SChris Smith        } else {
5326733c4d7SChris Smith            if (!empty($pass)){
5336733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
534*09a5dcd6SMichael Große                msg($this->lang['addUser_error_modPass_disabled'], -1);
5356733c4d7SChris Smith                return false;
5366733c4d7SChris Smith            }
5376733c4d7SChris Smith        }
5386733c4d7SChris Smith
5396733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5406733c4d7SChris Smith            if (empty($name)){
5416733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
542*09a5dcd6SMichael Große                msg($this->lang['addUser_error_name_missing'], -1);
5436733c4d7SChris Smith                return false;
5446733c4d7SChris Smith            }
5456733c4d7SChris Smith        } else {
5466733c4d7SChris Smith            if (!empty($name)){
547*09a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
548*09a5dcd6SMichael Große                msg($this->lang['addUser_error_modName_disabled'], -1);
5496733c4d7SChris Smith                return false;
5506733c4d7SChris Smith            }
5516733c4d7SChris Smith        }
5526733c4d7SChris Smith
5536733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5546733c4d7SChris Smith            if (empty($mail)){
5556733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
556*09a5dcd6SMichael Große                msg($this->lang['addUser_error_mail_missing'], -1);
5576733c4d7SChris Smith                return false;
5586733c4d7SChris Smith            }
5596733c4d7SChris Smith        } else {
5606733c4d7SChris Smith            if (!empty($mail)){
561*09a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
562*09a5dcd6SMichael Große                msg($this->lang['addUser_error_modMail_disabled'], -1);
5636733c4d7SChris Smith                return false;
5646733c4d7SChris Smith            }
5656733c4d7SChris Smith        }
5660440ff15Schris
5677d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
568a6858c6aSchris
569a6858c6aSchris            msg($this->lang['add_ok'], 1);
570a6858c6aSchris
57100d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
572a6858c6aSchris                $this->_notifyUser($user,$pass);
573a6858c6aSchris            }
574a6858c6aSchris        } else {
57560b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
576*09a5dcd6SMichael Große            msg($this->lang['addUser_error_create_event_failed'], -1);
577a6858c6aSchris        }
578a6858c6aSchris
579a6858c6aSchris        return $ok;
5800440ff15Schris    }
5810440ff15Schris
5820440ff15Schris    /**
583c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
584c5a7c0c6SGerrit Uitslag     *
585c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5860440ff15Schris     */
5873712ca9aSGerrit Uitslag    protected function _deleteUser(){
58800d58927SMichael Hamann        global $conf, $INPUT;
5899ec82636SAndreas Gohr
590634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
59182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5920440ff15Schris
59300d58927SMichael Hamann        $selected = $INPUT->arr('delete');
59400d58927SMichael Hamann        if (empty($selected)) return false;
5950440ff15Schris        $selected = array_keys($selected);
5960440ff15Schris
597c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
598c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
599c9a8f912SMichael Klier            return false;
600c9a8f912SMichael Klier        }
601c9a8f912SMichael Klier
6027d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
6030440ff15Schris        if ($count == count($selected)) {
6040440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
6050440ff15Schris            msg("$text.", 1);
6060440ff15Schris        } else {
6070440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
6080440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
6090440ff15Schris            msg("$part1, $part2",-1);
6100440ff15Schris        }
61178c7c8c9Schris
6129ec82636SAndreas Gohr        // invalidate all sessions
6139ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
6149ec82636SAndreas Gohr
61578c7c8c9Schris        return true;
61678c7c8c9Schris    }
61778c7c8c9Schris
61878c7c8c9Schris    /**
61978c7c8c9Schris     * Edit user (a user has been selected for editing)
620c5a7c0c6SGerrit Uitslag     *
621c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
622c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
62378c7c8c9Schris     */
6243712ca9aSGerrit Uitslag    protected function _editUser($param) {
625634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
62678c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
627786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
62878c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
62978c7c8c9Schris
63078c7c8c9Schris        // no user found?
63178c7c8c9Schris        if (!$userdata) {
63278c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
63378c7c8c9Schris            return false;
63478c7c8c9Schris        }
63578c7c8c9Schris
63678c7c8c9Schris        $this->_edit_user = $user;
63778c7c8c9Schris        $this->_edit_userdata = $userdata;
63878c7c8c9Schris
63978c7c8c9Schris        return true;
6400440ff15Schris    }
6410440ff15Schris
6420440ff15Schris    /**
643c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
644c5a7c0c6SGerrit Uitslag     *
645c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6460440ff15Schris     */
6473712ca9aSGerrit Uitslag    protected function _modifyUser(){
64800d58927SMichael Hamann        global $conf, $INPUT;
6499ec82636SAndreas Gohr
650634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
65182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6520440ff15Schris
65326fb387bSchris        // get currently valid  user data
654786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
655073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
656073766c6Smatthiasgrimm
65726fb387bSchris        // get new user data subject to change
658359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
659073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6600440ff15Schris
6610440ff15Schris        $changes = array();
662073766c6Smatthiasgrimm        if ($newuser != $olduser) {
66326fb387bSchris
66426fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
66526fb387bSchris                msg($this->lang['update_fail'],-1);
66626fb387bSchris                return false;
66726fb387bSchris            }
66826fb387bSchris
66926fb387bSchris            // check if $newuser already exists
670073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
671073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
672a6858c6aSchris                $re_edit = true;
6730440ff15Schris            } else {
674073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6750440ff15Schris            }
67693eefc2fSAndreas Gohr        }
677359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6782400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
679359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
680359e9417SChristopher Smith                    $changes['pass'] = $newpass;
681359e9417SChristopher Smith                } else {
682359e9417SChristopher Smith                    return false;
683359e9417SChristopher Smith                }
684359e9417SChristopher Smith            } else {
685359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
686359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
687359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
688359e9417SChristopher Smith                }
689359e9417SChristopher Smith            }
6900440ff15Schris        }
6910440ff15Schris
69240d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
693073766c6Smatthiasgrimm            $changes['name'] = $newname;
69440d72af6SChristopher Smith        }
69540d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
696073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
69740d72af6SChristopher Smith        }
69840d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
699073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
70040d72af6SChristopher Smith        }
7010440ff15Schris
7027d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
7030440ff15Schris            msg($this->lang['update_ok'],1);
704a6858c6aSchris
7056ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
706a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
7076ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
708a6858c6aSchris            }
709a6858c6aSchris
7109ec82636SAndreas Gohr            // invalidate all sessions
7119ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
7129ec82636SAndreas Gohr
7130440ff15Schris        } else {
7140440ff15Schris            msg($this->lang['update_fail'],-1);
7150440ff15Schris        }
71678c7c8c9Schris
717a6858c6aSchris        if (!empty($re_edit)) {
718a6858c6aSchris            $this->_editUser($olduser);
7190440ff15Schris        }
7200440ff15Schris
721a6858c6aSchris        return $ok;
722a6858c6aSchris    }
723a6858c6aSchris
724a6858c6aSchris    /**
725c5a7c0c6SGerrit Uitslag     * Send password change notification email
726c5a7c0c6SGerrit Uitslag     *
727c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
728c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
729c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
730c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
731a6858c6aSchris     */
7323712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
733a6858c6aSchris
734a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
735328143f8SChristopher Smith            if ($status_alert) {
736a6858c6aSchris                msg($this->lang['notify_ok'], 1);
737328143f8SChristopher Smith            }
738a6858c6aSchris        } else {
739328143f8SChristopher Smith            if ($status_alert) {
740a6858c6aSchris                msg($this->lang['notify_fail'], -1);
741a6858c6aSchris            }
742328143f8SChristopher Smith        }
743a6858c6aSchris
744a6858c6aSchris        return $sent;
745a6858c6aSchris    }
746a6858c6aSchris
747a6858c6aSchris    /**
748359e9417SChristopher Smith     * Verify password meets minimum requirements
749359e9417SChristopher Smith     * :TODO: extend to support password strength
750359e9417SChristopher Smith     *
751359e9417SChristopher Smith     * @param string  $password   candidate string for new password
752359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
753359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
754359e9417SChristopher Smith     */
755359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
756be9008d3SChristopher Smith        global $lang;
757359e9417SChristopher Smith
7582400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
759359e9417SChristopher Smith            return false;
760359e9417SChristopher Smith        }
761359e9417SChristopher Smith
762359e9417SChristopher Smith        if ($password !== $confirm) {
763be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
764359e9417SChristopher Smith            return false;
765359e9417SChristopher Smith        }
766359e9417SChristopher Smith
767359e9417SChristopher Smith        // :TODO: test password for required strength
768359e9417SChristopher Smith
769359e9417SChristopher Smith        // if we make it this far the password is good
770359e9417SChristopher Smith        return true;
771359e9417SChristopher Smith    }
772359e9417SChristopher Smith
773359e9417SChristopher Smith    /**
774c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
775a6858c6aSchris     *
776c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
777a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7780440ff15Schris     */
7793712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
780c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7817441e340SAndreas Gohr        global $auth;
782fbfbbe8aSHakan Sandell        global $INPUT;
7830440ff15Schris
78459bc3b48SGerrit Uitslag        $user = array();
785fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
786fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
787fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
788fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
789fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
790359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7910440ff15Schris
7927441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7937441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7947441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7957441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7967441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7970440ff15Schris
7980440ff15Schris        return $user;
7990440ff15Schris    }
8000440ff15Schris
801c5a7c0c6SGerrit Uitslag    /**
802c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
803c5a7c0c6SGerrit Uitslag     *
804c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
805c5a7c0c6SGerrit Uitslag     */
8063712ca9aSGerrit Uitslag    protected function _setFilter($op) {
8070440ff15Schris
8080440ff15Schris        $this->_filter = array();
8090440ff15Schris
8100440ff15Schris        if ($op == 'new') {
81159bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
8120440ff15Schris
8130440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
8140440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
8150440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
8160440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
8170440ff15Schris        }
8180440ff15Schris    }
8190440ff15Schris
820c5a7c0c6SGerrit Uitslag    /**
821c5a7c0c6SGerrit Uitslag     * Get the current search terms
822c5a7c0c6SGerrit Uitslag     *
823c5a7c0c6SGerrit Uitslag     * @return array
824c5a7c0c6SGerrit Uitslag     */
8253712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
826fbfbbe8aSHakan Sandell        global $INPUT;
8270440ff15Schris
828fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
8290440ff15Schris
8300440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
8310440ff15Schris        $filter = array();
8320440ff15Schris
8330440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8340440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8350440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8360440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8370440ff15Schris
8380440ff15Schris        return $filter;
8390440ff15Schris    }
8400440ff15Schris
841c5a7c0c6SGerrit Uitslag    /**
842c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
843c5a7c0c6SGerrit Uitslag     */
8443712ca9aSGerrit Uitslag    protected function _validatePagination() {
8450440ff15Schris
8460440ff15Schris        if ($this->_start >= $this->_user_total) {
8470440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8480440ff15Schris        }
8490440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8500440ff15Schris
8510440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8520440ff15Schris    }
8530440ff15Schris
854c5a7c0c6SGerrit Uitslag    /**
855c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
856c5a7c0c6SGerrit Uitslag     *
857c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8580440ff15Schris     */
8593712ca9aSGerrit Uitslag    protected function _pagination() {
8600440ff15Schris
86151d94d49Schris        $disabled = 'disabled="disabled"';
86251d94d49Schris
86359bc3b48SGerrit Uitslag        $buttons = array();
86451d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
86551d94d49Schris
86651d94d49Schris        if ($this->_user_total == -1) {
86751d94d49Schris            $buttons['last'] = $disabled;
86851d94d49Schris            $buttons['next'] = '';
86951d94d49Schris        } else {
87051d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
87151d94d49Schris        }
8720440ff15Schris
873462e9e37SMichael Große        if ($this->_lastdisabled) {
874462e9e37SMichael Große            $buttons['last'] = $disabled;
875462e9e37SMichael Große        }
876462e9e37SMichael Große
8770440ff15Schris        return $buttons;
8780440ff15Schris    }
8795c967d3dSChristopher Smith
880c5a7c0c6SGerrit Uitslag    /**
881c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8825c967d3dSChristopher Smith     */
8833712ca9aSGerrit Uitslag    protected function _export() {
8845c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8855c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8865c967d3dSChristopher Smith        $column_headings = array(
8875c967d3dSChristopher Smith            $this->lang["user_id"],
8885c967d3dSChristopher Smith            $this->lang["user_name"],
8895c967d3dSChristopher Smith            $this->lang["user_mail"],
8905c967d3dSChristopher Smith            $this->lang["user_groups"]
8915c967d3dSChristopher Smith        );
8925c967d3dSChristopher Smith
8935c967d3dSChristopher Smith        // ==============================================================================================
8945c967d3dSChristopher Smith        // GENERATE OUTPUT
8955c967d3dSChristopher Smith        // normal headers for downloading...
8965c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8975c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8985c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8995c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
9005c967d3dSChristopher Smith
9015c967d3dSChristopher Smith        // output the csv
9025c967d3dSChristopher Smith        $fd = fopen('php://output','w');
9035c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
9045c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
9055c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
9065c967d3dSChristopher Smith            fputcsv($fd, $line);
9075c967d3dSChristopher Smith        }
9085c967d3dSChristopher Smith        fclose($fd);
909b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
910b2c01466SChristopher Smith
9115c967d3dSChristopher Smith        die;
9125c967d3dSChristopher Smith    }
913ae1afd2fSChristopher Smith
914c5a7c0c6SGerrit Uitslag    /**
915c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
916ae1afd2fSChristopher Smith     *
917ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
918c5a7c0c6SGerrit Uitslag     *
9195ba64050SChristopher Smith     * @return bool whether successful
920ae1afd2fSChristopher Smith     */
9213712ca9aSGerrit Uitslag    protected function _import() {
922ae1afd2fSChristopher Smith        // check we are allowed to add users
923ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
924ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
925ae1afd2fSChristopher Smith
926ae1afd2fSChristopher Smith        // check file uploaded ok.
927b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
928ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
929ae1afd2fSChristopher Smith            return false;
930ae1afd2fSChristopher Smith        }
931ae1afd2fSChristopher Smith        // retrieve users from the file
932ae1afd2fSChristopher Smith        $this->_import_failures = array();
933ae1afd2fSChristopher Smith        $import_success_count = 0;
934ae1afd2fSChristopher Smith        $import_fail_count = 0;
935ae1afd2fSChristopher Smith        $line = 0;
936ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
937ae1afd2fSChristopher Smith        if ($fd) {
938ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
939efcec72bSChristopher Smith                if (!utf8_check($csv)) {
940efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
941efcec72bSChristopher Smith                }
942c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
943ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
944ae1afd2fSChristopher Smith                // data checks...
945ae1afd2fSChristopher Smith                if (1 == ++$line) {
946ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
947ae1afd2fSChristopher Smith                }
948ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
949ae1afd2fSChristopher Smith                    $import_fail_count++;
950ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
951ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
952ae1afd2fSChristopher Smith                    continue;
953ae1afd2fSChristopher Smith                }
954ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
955ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
956ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
957328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
958328143f8SChristopher Smith                    if (!$sent){
959328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
960328143f8SChristopher Smith                    }
961ae1afd2fSChristopher Smith                    $import_success_count++;
962ae1afd2fSChristopher Smith                } else {
963ae1afd2fSChristopher Smith                    $import_fail_count++;
964e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
965ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
966ae1afd2fSChristopher Smith                }
967ae1afd2fSChristopher Smith            }
968ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
969ae1afd2fSChristopher Smith            if ($import_fail_count) {
970ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
971ae1afd2fSChristopher Smith            }
972ae1afd2fSChristopher Smith        } else {
973ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
974ae1afd2fSChristopher Smith        }
975ae1afd2fSChristopher Smith
976ae1afd2fSChristopher Smith        // save import failures into the session
977ae1afd2fSChristopher Smith        if (!headers_sent()) {
978ae1afd2fSChristopher Smith            session_start();
979ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
980ae1afd2fSChristopher Smith            session_write_close();
981ae1afd2fSChristopher Smith        }
982c5a7c0c6SGerrit Uitslag        return true;
983ae1afd2fSChristopher Smith    }
984ae1afd2fSChristopher Smith
985c5a7c0c6SGerrit Uitslag    /**
986786dfb0eSGerrit Uitslag     * Returns cleaned user data
987c5a7c0c6SGerrit Uitslag     *
988c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
989253d4b48SGerrit Uitslag     * @param string $error
990253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
991c5a7c0c6SGerrit Uitslag     */
9923712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
993ae1afd2fSChristopher Smith        global $INPUT;
994ae1afd2fSChristopher Smith
995ae1afd2fSChristopher Smith        // kludgy ....
996ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
997ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
998ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
999ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
1000ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
1001ae1afd2fSChristopher Smith
1002ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
100359bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
1004ae1afd2fSChristopher Smith        if (empty($user)) {
1005ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
1006ae1afd2fSChristopher Smith            return false;
1007ae1afd2fSChristopher Smith        }
1008ae1afd2fSChristopher Smith
1009ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
1010ae1afd2fSChristopher Smith
1011ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
1012ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
1013ae1afd2fSChristopher Smith            return false;
1014ae1afd2fSChristopher Smith        }
1015ae1afd2fSChristopher Smith
1016328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
1017328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
1018ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
1019ae1afd2fSChristopher Smith                return false;
1020ae1afd2fSChristopher Smith            }
1021328143f8SChristopher Smith        } else {
1022328143f8SChristopher Smith            if (!empty($mail)) {
1023328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
1024328143f8SChristopher Smith                return false;
1025328143f8SChristopher Smith            }
1026328143f8SChristopher Smith        }
1027ae1afd2fSChristopher Smith
1028ae1afd2fSChristopher Smith        return $cleaned;
1029ae1afd2fSChristopher Smith    }
1030ae1afd2fSChristopher Smith
1031c5a7c0c6SGerrit Uitslag    /**
1032c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1033c5a7c0c6SGerrit Uitslag     *
1034c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1035c5a7c0c6SGerrit Uitslag     *
1036c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
1037c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10385ba64050SChristopher Smith     * @return bool whether successful
1039c5a7c0c6SGerrit Uitslag     */
10403712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1041ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1042ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1043ae1afd2fSChristopher Smith            return false;
1044ae1afd2fSChristopher Smith        }
1045ae1afd2fSChristopher Smith
1046ae1afd2fSChristopher Smith        return true;
1047ae1afd2fSChristopher Smith    }
1048ae1afd2fSChristopher Smith
1049c5a7c0c6SGerrit Uitslag    /**
1050c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1051c5a7c0c6SGerrit Uitslag     */
10523712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1053ae1afd2fSChristopher Smith
1054ae1afd2fSChristopher Smith        // ==============================================================================================
1055ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1056ae1afd2fSChristopher Smith        // normal headers for downloading...
1057ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1058ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1059ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1060ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1061ae1afd2fSChristopher Smith
1062ae1afd2fSChristopher Smith        // output the csv
1063ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1064c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1065ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1066ae1afd2fSChristopher Smith        }
1067ae1afd2fSChristopher Smith        fclose($fd);
1068ae1afd2fSChristopher Smith        die;
1069ae1afd2fSChristopher Smith    }
1070ae1afd2fSChristopher Smith
1071b2c01466SChristopher Smith    /**
1072b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1073253d4b48SGerrit Uitslag     *
1074253d4b48SGerrit Uitslag     * @param string $file filename
1075253d4b48SGerrit Uitslag     * @return bool
1076b2c01466SChristopher Smith     */
1077b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1078b2c01466SChristopher Smith        return is_uploaded_file($file);
1079b2c01466SChristopher Smith    }
1080b2c01466SChristopher Smith
1081c9454ee3SChristopher Smith    /**
1082c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1083c9454ee3SChristopher Smith     *
1084c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1085c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1086253d4b48SGerrit Uitslag     *
1087253d4b48SGerrit Uitslag     * @param string $csv string to parse
1088253d4b48SGerrit Uitslag     * @return array
1089c9454ee3SChristopher Smith     */
1090c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1091c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1092c9454ee3SChristopher Smith    }
1093c9454ee3SChristopher Smith
1094c9454ee3SChristopher Smith    /**
1095c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1096c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1097c9454ee3SChristopher Smith     *
1098c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1099253d4b48SGerrit Uitslag     *
1100253d4b48SGerrit Uitslag     * @param string $str string to parse
1101253d4b48SGerrit Uitslag     * @return array
1102c9454ee3SChristopher Smith     */
1103c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1104c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1105c9454ee3SChristopher Smith        fputs($fp, $str);
1106c9454ee3SChristopher Smith        rewind($fp);
1107c9454ee3SChristopher Smith
1108c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1109c9454ee3SChristopher Smith
1110c9454ee3SChristopher Smith        fclose($fp);
1111c9454ee3SChristopher Smith        return $data;
1112c9454ee3SChristopher Smith    }
11130440ff15Schris}
1114