xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision f23f95941a400702f525923973f3612df6da82cb)
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     */
39c5a7c0c6SGerrit Uitslag    public function admin_plugin_usermanager(){
40c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
410440ff15Schris        global $auth;
420440ff15Schris
430440ff15Schris        $this->setupLocale();
4451d94d49Schris
4551d94d49Schris        if (!isset($auth)) {
46c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['noauth'];
4782fd59b6SAndreas Gohr        } else if (!$auth->canDo('getUsers')) {
48c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['nosupport'];
4951d94d49Schris        } else {
5051d94d49Schris
5151d94d49Schris            // we're good to go
5251d94d49Schris            $this->_auth = & $auth;
5351d94d49Schris
5451d94d49Schris        }
55ae1afd2fSChristopher Smith
56ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
570e80bb5eSChristopher Smith        if (!empty($_SESSION['import_failures'])){
58ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
59ae1afd2fSChristopher Smith        }
600440ff15Schris    }
610440ff15Schris
620440ff15Schris    /**
63c5a7c0c6SGerrit Uitslag     * Return prompt for admin menu
64253d4b48SGerrit Uitslag     *
65253d4b48SGerrit Uitslag     * @param string $language
66253d4b48SGerrit Uitslag     * @return string
670440ff15Schris     */
68c5a7c0c6SGerrit Uitslag    public function getMenuText($language) {
690440ff15Schris
700440ff15Schris        if (!is_null($this->_auth))
710440ff15Schris          return parent::getMenuText($language);
720440ff15Schris
73c5a7c0c6SGerrit Uitslag        return $this->getLang('menu').' '.$this->_disabled;
740440ff15Schris    }
750440ff15Schris
760440ff15Schris    /**
770440ff15Schris     * return sort order for position in admin menu
78253d4b48SGerrit Uitslag     *
79253d4b48SGerrit Uitslag     * @return int
800440ff15Schris     */
81c5a7c0c6SGerrit Uitslag    public function getMenuSort() {
820440ff15Schris        return 2;
830440ff15Schris    }
840440ff15Schris
850440ff15Schris    /**
8667a31a83SMichael Große     * @return int current start value for pageination
8767a31a83SMichael Große     */
8867a31a83SMichael Große    public function getStart() {
8967a31a83SMichael Große        return $this->_start;
9067a31a83SMichael Große    }
9167a31a83SMichael Große
9267a31a83SMichael Große    /**
9367a31a83SMichael Große     * @return int number of users per page
9467a31a83SMichael Große     */
9567a31a83SMichael Große    public function getPagesize() {
9667a31a83SMichael Große        return $this->_pagesize;
9767a31a83SMichael Große    }
9867a31a83SMichael Große
9967a31a83SMichael Große    /**
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\">");
225*f23f9594SAndreas Gohr                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".hsc($user)."]\" ".$delete_disable." /></td>");
2262365d73dSAnika Henke                if ($editable) {
227*f23f9594SAndreas 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\">");
244a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2450440ff15Schris        ptln("        </span>");
246a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
247a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
248a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
249a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
250a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2510440ff15Schris        ptln("        </span>");
2525c967d3dSChristopher Smith        if (!empty($this->_filter)) {
253a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2545c967d3dSChristopher Smith        }
2555c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2565164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2575164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
258daf4ca4eSAnika Henke
259daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
260daf4ca4eSAnika Henke
2610440ff15Schris        ptln("      </td></tr>");
2620440ff15Schris        ptln("    </tbody>");
2630440ff15Schris        ptln("  </table>");
264c7b28ffdSAnika Henke        ptln("  </div>");
2650440ff15Schris
2660440ff15Schris        ptln("</form>");
2670440ff15Schris        ptln("</div>");
2680440ff15Schris
269a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2700440ff15Schris
27182fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2720440ff15Schris            ptln("<div".$style.">");
2730440ff15Schris            print $this->locale_xhtml('add');
2740440ff15Schris            ptln("  <div class=\"level2\">");
2750440ff15Schris
27678c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2770440ff15Schris
2780440ff15Schris            ptln("  </div>");
2790440ff15Schris            ptln("</div>");
2800440ff15Schris        }
2810440ff15Schris
28282fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
283c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2840440ff15Schris            print $this->locale_xhtml('edit');
2850440ff15Schris            ptln("  <div class=\"level2\">");
2860440ff15Schris
28778c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2880440ff15Schris
2890440ff15Schris            ptln("  </div>");
2900440ff15Schris            ptln("</div>");
2910440ff15Schris        }
292ae1afd2fSChristopher Smith
293ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
294ae1afd2fSChristopher Smith            $this->_htmlImportForm();
295ae1afd2fSChristopher Smith        }
29658dde80dSAnika Henke        ptln("</div>");
297c5a7c0c6SGerrit Uitslag        return true;
2980440ff15Schris    }
2990440ff15Schris
30082fd59b6SAndreas Gohr    /**
301c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
302c5a7c0c6SGerrit Uitslag     *
303c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
304c5a7c0c6SGerrit Uitslag     * @param string $user id of user
305c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
306c5a7c0c6SGerrit Uitslag     * @param int    $indent
30782fd59b6SAndreas Gohr     */
3083712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
309a6858c6aSchris        global $conf;
310bb4866bdSchris        global $ID;
311be9008d3SChristopher Smith        global $lang;
31278c7c8c9Schris
31378c7c8c9Schris        $name = $mail = $groups = '';
314a6858c6aSchris        $notes = array();
3150440ff15Schris
3160440ff15Schris        if ($user) {
31778c7c8c9Schris            extract($userdata);
31878c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
319a6858c6aSchris        } else {
320a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
3210440ff15Schris        }
3220440ff15Schris
3230440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
324634d7150SAndreas Gohr        formSecurityToken();
325c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
3260440ff15Schris        ptln("  <table class=\"inline\">",$indent);
3270440ff15Schris        ptln("    <thead>",$indent);
3280440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
3290440ff15Schris        ptln("    </thead>",$indent);
3300440ff15Schris        ptln("    <tbody>",$indent);
33126fb387bSchris
33226fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
33326fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
334be9008d3SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),  $indent+6);
33526fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
33626fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
33726fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
33826fb387bSchris
339a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
340ee9498f5SChristopher Smith            if ($cmd == 'add') {
341c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
342ee9498f5SChristopher Smith            }
343a6858c6aSchris            if ($user) {
344a6858c6aSchris                $notes[] = $this->lang['note_notify'];
345a6858c6aSchris            }
346a6858c6aSchris
347a6858c6aSchris            ptln("<tr><td><label for=\"".$cmd."_usernotify\" >".$this->lang["user_notify"].": </label></td><td><input type=\"checkbox\" id=\"".$cmd."_usernotify\" name=\"usernotify\" value=\"1\" /></td></tr>", $indent);
348a6858c6aSchris        }
349a6858c6aSchris
3500440ff15Schris        ptln("    </tbody>",$indent);
3510440ff15Schris        ptln("    <tbody>",$indent);
3520440ff15Schris        ptln("      <tr>",$indent);
3530440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3540440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3550440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3560440ff15Schris
3570440ff15Schris        // save current $user, we need this to access details if the name is changed
3580440ff15Schris        if ($user)
359*f23f9594SAndreas Gohr          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".hsc($user)."\" />",$indent);
3600440ff15Schris
3610440ff15Schris        $this->_htmlFilterSettings($indent+10);
3620440ff15Schris
363a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3640440ff15Schris        ptln("        </td>",$indent);
3650440ff15Schris        ptln("      </tr>",$indent);
3660440ff15Schris        ptln("    </tbody>",$indent);
3670440ff15Schris        ptln("  </table>",$indent);
36845c19902SChristopher Smith
36945c19902SChristopher Smith        if ($notes) {
37045c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
37145c19902SChristopher Smith            foreach ($notes as $note) {
37245c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
37345c19902SChristopher Smith            }
37445c19902SChristopher Smith            ptln("    </ul>");
37545c19902SChristopher Smith        }
376c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3770440ff15Schris        ptln("</form>",$indent);
3780440ff15Schris    }
3790440ff15Schris
380c5a7c0c6SGerrit Uitslag    /**
381c5a7c0c6SGerrit Uitslag     * Prints a inputfield
382c5a7c0c6SGerrit Uitslag     *
383c5a7c0c6SGerrit Uitslag     * @param string $id
384c5a7c0c6SGerrit Uitslag     * @param string $name
385c5a7c0c6SGerrit Uitslag     * @param string $label
386c5a7c0c6SGerrit Uitslag     * @param string $value
387c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
388c5a7c0c6SGerrit Uitslag     * @param int $indent
389c5a7c0c6SGerrit Uitslag     */
3903712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3917de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3927de12fceSAndreas Gohr        echo str_pad('',$indent);
3937de12fceSAndreas Gohr
394359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
395d796a891SAndreas Gohr            $fieldtype = 'password';
396d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3977b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3987b3674bdSChristopher Smith            $fieldtype = 'email';
3997b3674bdSChristopher Smith            $autocomp  = '';
400d796a891SAndreas Gohr        }else{
401d796a891SAndreas Gohr            $fieldtype = 'text';
402d796a891SAndreas Gohr            $autocomp  = '';
403d796a891SAndreas Gohr        }
404*f23f9594SAndreas 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);
459ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$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);
52160b9901bSAndreas Gohr                    return false;
52260b9901bSAndreas Gohr                }
523359e9417SChristopher Smith            } else {
524359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
525359e9417SChristopher Smith                    return false;
526359e9417SChristopher Smith                }
5276733c4d7SChris Smith            }
5286733c4d7SChris Smith        } else {
5296733c4d7SChris Smith            if (!empty($pass)){
5306733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5316733c4d7SChris Smith                return false;
5326733c4d7SChris Smith            }
5336733c4d7SChris Smith        }
5346733c4d7SChris Smith
5356733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5366733c4d7SChris Smith            if (empty($name)){
5376733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5386733c4d7SChris Smith                return false;
5396733c4d7SChris Smith            }
5406733c4d7SChris Smith        } else {
5416733c4d7SChris Smith            if (!empty($name)){
5426733c4d7SChris Smith                return false;
5436733c4d7SChris Smith            }
5446733c4d7SChris Smith        }
5456733c4d7SChris Smith
5466733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5476733c4d7SChris Smith            if (empty($mail)){
5486733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5496733c4d7SChris Smith                return false;
5506733c4d7SChris Smith            }
5516733c4d7SChris Smith        } else {
5526733c4d7SChris Smith            if (!empty($mail)){
5536733c4d7SChris Smith                return false;
5546733c4d7SChris Smith            }
5556733c4d7SChris Smith        }
5560440ff15Schris
5577d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
558a6858c6aSchris
559a6858c6aSchris            msg($this->lang['add_ok'], 1);
560a6858c6aSchris
56100d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
562a6858c6aSchris                $this->_notifyUser($user,$pass);
563a6858c6aSchris            }
564a6858c6aSchris        } else {
56560b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
566a6858c6aSchris        }
567a6858c6aSchris
568a6858c6aSchris        return $ok;
5690440ff15Schris    }
5700440ff15Schris
5710440ff15Schris    /**
572c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
573c5a7c0c6SGerrit Uitslag     *
574c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5750440ff15Schris     */
5763712ca9aSGerrit Uitslag    protected function _deleteUser(){
57700d58927SMichael Hamann        global $conf, $INPUT;
5789ec82636SAndreas Gohr
579634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
58082fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5810440ff15Schris
58200d58927SMichael Hamann        $selected = $INPUT->arr('delete');
58300d58927SMichael Hamann        if (empty($selected)) return false;
5840440ff15Schris        $selected = array_keys($selected);
5850440ff15Schris
586c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
587c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
588c9a8f912SMichael Klier            return false;
589c9a8f912SMichael Klier        }
590c9a8f912SMichael Klier
5917d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5920440ff15Schris        if ($count == count($selected)) {
5930440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5940440ff15Schris            msg("$text.", 1);
5950440ff15Schris        } else {
5960440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5970440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5980440ff15Schris            msg("$part1, $part2",-1);
5990440ff15Schris        }
60078c7c8c9Schris
6019ec82636SAndreas Gohr        // invalidate all sessions
6029ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
6039ec82636SAndreas Gohr
60478c7c8c9Schris        return true;
60578c7c8c9Schris    }
60678c7c8c9Schris
60778c7c8c9Schris    /**
60878c7c8c9Schris     * Edit user (a user has been selected for editing)
609c5a7c0c6SGerrit Uitslag     *
610c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
611c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
61278c7c8c9Schris     */
6133712ca9aSGerrit Uitslag    protected function _editUser($param) {
614634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
61578c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
616786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
61778c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
61878c7c8c9Schris
61978c7c8c9Schris        // no user found?
62078c7c8c9Schris        if (!$userdata) {
62178c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
62278c7c8c9Schris            return false;
62378c7c8c9Schris        }
62478c7c8c9Schris
62578c7c8c9Schris        $this->_edit_user = $user;
62678c7c8c9Schris        $this->_edit_userdata = $userdata;
62778c7c8c9Schris
62878c7c8c9Schris        return true;
6290440ff15Schris    }
6300440ff15Schris
6310440ff15Schris    /**
632c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
633c5a7c0c6SGerrit Uitslag     *
634c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6350440ff15Schris     */
6363712ca9aSGerrit Uitslag    protected function _modifyUser(){
63700d58927SMichael Hamann        global $conf, $INPUT;
6389ec82636SAndreas Gohr
639634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
64082fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6410440ff15Schris
64226fb387bSchris        // get currently valid  user data
643786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
644073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
645073766c6Smatthiasgrimm
64626fb387bSchris        // get new user data subject to change
647359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
648073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6490440ff15Schris
6500440ff15Schris        $changes = array();
651073766c6Smatthiasgrimm        if ($newuser != $olduser) {
65226fb387bSchris
65326fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
65426fb387bSchris                msg($this->lang['update_fail'],-1);
65526fb387bSchris                return false;
65626fb387bSchris            }
65726fb387bSchris
65826fb387bSchris            // check if $newuser already exists
659073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
660073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
661a6858c6aSchris                $re_edit = true;
6620440ff15Schris            } else {
663073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6640440ff15Schris            }
66593eefc2fSAndreas Gohr        }
666359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6672400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
668359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
669359e9417SChristopher Smith                    $changes['pass'] = $newpass;
670359e9417SChristopher Smith                } else {
671359e9417SChristopher Smith                    return false;
672359e9417SChristopher Smith                }
673359e9417SChristopher Smith            } else {
674359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
675359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
676359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
677359e9417SChristopher Smith                }
678359e9417SChristopher Smith            }
6790440ff15Schris        }
6800440ff15Schris
68140d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
682073766c6Smatthiasgrimm            $changes['name'] = $newname;
68340d72af6SChristopher Smith        }
68440d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
685073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
68640d72af6SChristopher Smith        }
68740d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
688073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
68940d72af6SChristopher Smith        }
6900440ff15Schris
6917d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6920440ff15Schris            msg($this->lang['update_ok'],1);
693a6858c6aSchris
6946ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
695a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
6966ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
697a6858c6aSchris            }
698a6858c6aSchris
6999ec82636SAndreas Gohr            // invalidate all sessions
7009ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
7019ec82636SAndreas Gohr
7020440ff15Schris        } else {
7030440ff15Schris            msg($this->lang['update_fail'],-1);
7040440ff15Schris        }
70578c7c8c9Schris
706a6858c6aSchris        if (!empty($re_edit)) {
707a6858c6aSchris            $this->_editUser($olduser);
7080440ff15Schris        }
7090440ff15Schris
710a6858c6aSchris        return $ok;
711a6858c6aSchris    }
712a6858c6aSchris
713a6858c6aSchris    /**
714c5a7c0c6SGerrit Uitslag     * Send password change notification email
715c5a7c0c6SGerrit Uitslag     *
716c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
717c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
718c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
719c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
720a6858c6aSchris     */
7213712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
722a6858c6aSchris
723a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
724328143f8SChristopher Smith            if ($status_alert) {
725a6858c6aSchris                msg($this->lang['notify_ok'], 1);
726328143f8SChristopher Smith            }
727a6858c6aSchris        } else {
728328143f8SChristopher Smith            if ($status_alert) {
729a6858c6aSchris                msg($this->lang['notify_fail'], -1);
730a6858c6aSchris            }
731328143f8SChristopher Smith        }
732a6858c6aSchris
733a6858c6aSchris        return $sent;
734a6858c6aSchris    }
735a6858c6aSchris
736a6858c6aSchris    /**
737359e9417SChristopher Smith     * Verify password meets minimum requirements
738359e9417SChristopher Smith     * :TODO: extend to support password strength
739359e9417SChristopher Smith     *
740359e9417SChristopher Smith     * @param string  $password   candidate string for new password
741359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
742359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
743359e9417SChristopher Smith     */
744359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
745be9008d3SChristopher Smith        global $lang;
746359e9417SChristopher Smith
7472400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
748359e9417SChristopher Smith            return false;
749359e9417SChristopher Smith        }
750359e9417SChristopher Smith
751359e9417SChristopher Smith        if ($password !== $confirm) {
752be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
753359e9417SChristopher Smith            return false;
754359e9417SChristopher Smith        }
755359e9417SChristopher Smith
756359e9417SChristopher Smith        // :TODO: test password for required strength
757359e9417SChristopher Smith
758359e9417SChristopher Smith        // if we make it this far the password is good
759359e9417SChristopher Smith        return true;
760359e9417SChristopher Smith    }
761359e9417SChristopher Smith
762359e9417SChristopher Smith    /**
763c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
764a6858c6aSchris     *
765c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
766a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7670440ff15Schris     */
7683712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
769c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7707441e340SAndreas Gohr        global $auth;
771fbfbbe8aSHakan Sandell        global $INPUT;
7720440ff15Schris
77359bc3b48SGerrit Uitslag        $user = array();
774fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
775fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
776fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
777fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
778fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
779359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7800440ff15Schris
7817441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7827441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7837441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7847441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7857441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7860440ff15Schris
7870440ff15Schris        return $user;
7880440ff15Schris    }
7890440ff15Schris
790c5a7c0c6SGerrit Uitslag    /**
791c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
792c5a7c0c6SGerrit Uitslag     *
793c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
794c5a7c0c6SGerrit Uitslag     */
7953712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7960440ff15Schris
7970440ff15Schris        $this->_filter = array();
7980440ff15Schris
7990440ff15Schris        if ($op == 'new') {
80059bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
8010440ff15Schris
8020440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
8030440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
8040440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
8050440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
8060440ff15Schris        }
8070440ff15Schris    }
8080440ff15Schris
809c5a7c0c6SGerrit Uitslag    /**
810c5a7c0c6SGerrit Uitslag     * Get the current search terms
811c5a7c0c6SGerrit Uitslag     *
812c5a7c0c6SGerrit Uitslag     * @return array
813c5a7c0c6SGerrit Uitslag     */
8143712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
815fbfbbe8aSHakan Sandell        global $INPUT;
8160440ff15Schris
817fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
8180440ff15Schris
8190440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
8200440ff15Schris        $filter = array();
8210440ff15Schris
8220440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8230440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8240440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8250440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8260440ff15Schris
8270440ff15Schris        return $filter;
8280440ff15Schris    }
8290440ff15Schris
830c5a7c0c6SGerrit Uitslag    /**
831c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
832c5a7c0c6SGerrit Uitslag     */
8333712ca9aSGerrit Uitslag    protected function _validatePagination() {
8340440ff15Schris
8350440ff15Schris        if ($this->_start >= $this->_user_total) {
8360440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8370440ff15Schris        }
8380440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8390440ff15Schris
8400440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8410440ff15Schris    }
8420440ff15Schris
843c5a7c0c6SGerrit Uitslag    /**
844c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
845c5a7c0c6SGerrit Uitslag     *
846c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8470440ff15Schris     */
8483712ca9aSGerrit Uitslag    protected function _pagination() {
8490440ff15Schris
85051d94d49Schris        $disabled = 'disabled="disabled"';
85151d94d49Schris
85259bc3b48SGerrit Uitslag        $buttons = array();
85351d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
85451d94d49Schris
85551d94d49Schris        if ($this->_user_total == -1) {
85651d94d49Schris            $buttons['last'] = $disabled;
85751d94d49Schris            $buttons['next'] = '';
85851d94d49Schris        } else {
85951d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
86051d94d49Schris        }
8610440ff15Schris
862462e9e37SMichael Große        if ($this->_lastdisabled) {
863462e9e37SMichael Große            $buttons['last'] = $disabled;
864462e9e37SMichael Große        }
865462e9e37SMichael Große
8660440ff15Schris        return $buttons;
8670440ff15Schris    }
8685c967d3dSChristopher Smith
869c5a7c0c6SGerrit Uitslag    /**
870c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8715c967d3dSChristopher Smith     */
8723712ca9aSGerrit Uitslag    protected function _export() {
8735c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8745c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8755c967d3dSChristopher Smith        $column_headings = array(
8765c967d3dSChristopher Smith            $this->lang["user_id"],
8775c967d3dSChristopher Smith            $this->lang["user_name"],
8785c967d3dSChristopher Smith            $this->lang["user_mail"],
8795c967d3dSChristopher Smith            $this->lang["user_groups"]
8805c967d3dSChristopher Smith        );
8815c967d3dSChristopher Smith
8825c967d3dSChristopher Smith        // ==============================================================================================
8835c967d3dSChristopher Smith        // GENERATE OUTPUT
8845c967d3dSChristopher Smith        // normal headers for downloading...
8855c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8865c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8875c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8885c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8895c967d3dSChristopher Smith
8905c967d3dSChristopher Smith        // output the csv
8915c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8925c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8935c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8945c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8955c967d3dSChristopher Smith            fputcsv($fd, $line);
8965c967d3dSChristopher Smith        }
8975c967d3dSChristopher Smith        fclose($fd);
898b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
899b2c01466SChristopher Smith
9005c967d3dSChristopher Smith        die;
9015c967d3dSChristopher Smith    }
902ae1afd2fSChristopher Smith
903c5a7c0c6SGerrit Uitslag    /**
904c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
905ae1afd2fSChristopher Smith     *
906ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
907c5a7c0c6SGerrit Uitslag     *
9085ba64050SChristopher Smith     * @return bool whether successful
909ae1afd2fSChristopher Smith     */
9103712ca9aSGerrit Uitslag    protected function _import() {
911ae1afd2fSChristopher Smith        // check we are allowed to add users
912ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
913ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
914ae1afd2fSChristopher Smith
915ae1afd2fSChristopher Smith        // check file uploaded ok.
916b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
917ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
918ae1afd2fSChristopher Smith            return false;
919ae1afd2fSChristopher Smith        }
920ae1afd2fSChristopher Smith        // retrieve users from the file
921ae1afd2fSChristopher Smith        $this->_import_failures = array();
922ae1afd2fSChristopher Smith        $import_success_count = 0;
923ae1afd2fSChristopher Smith        $import_fail_count = 0;
924ae1afd2fSChristopher Smith        $line = 0;
925ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
926ae1afd2fSChristopher Smith        if ($fd) {
927ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
928efcec72bSChristopher Smith                if (!utf8_check($csv)) {
929efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
930efcec72bSChristopher Smith                }
931c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
932ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
933ae1afd2fSChristopher Smith                // data checks...
934ae1afd2fSChristopher Smith                if (1 == ++$line) {
935ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
936ae1afd2fSChristopher Smith                }
937ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
938ae1afd2fSChristopher Smith                    $import_fail_count++;
939ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
940ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
941ae1afd2fSChristopher Smith                    continue;
942ae1afd2fSChristopher Smith                }
943ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
944ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
945ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
946328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
947328143f8SChristopher Smith                    if (!$sent){
948328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
949328143f8SChristopher Smith                    }
950ae1afd2fSChristopher Smith                    $import_success_count++;
951ae1afd2fSChristopher Smith                } else {
952ae1afd2fSChristopher Smith                    $import_fail_count++;
953e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
954ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
955ae1afd2fSChristopher Smith                }
956ae1afd2fSChristopher Smith            }
957ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
958ae1afd2fSChristopher Smith            if ($import_fail_count) {
959ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
960ae1afd2fSChristopher Smith            }
961ae1afd2fSChristopher Smith        } else {
962ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
963ae1afd2fSChristopher Smith        }
964ae1afd2fSChristopher Smith
965ae1afd2fSChristopher Smith        // save import failures into the session
966ae1afd2fSChristopher Smith        if (!headers_sent()) {
967ae1afd2fSChristopher Smith            session_start();
968ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
969ae1afd2fSChristopher Smith            session_write_close();
970ae1afd2fSChristopher Smith        }
971c5a7c0c6SGerrit Uitslag        return true;
972ae1afd2fSChristopher Smith    }
973ae1afd2fSChristopher Smith
974c5a7c0c6SGerrit Uitslag    /**
975786dfb0eSGerrit Uitslag     * Returns cleaned user data
976c5a7c0c6SGerrit Uitslag     *
977c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
978253d4b48SGerrit Uitslag     * @param string $error
979253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
980c5a7c0c6SGerrit Uitslag     */
9813712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
982ae1afd2fSChristopher Smith        global $INPUT;
983ae1afd2fSChristopher Smith
984ae1afd2fSChristopher Smith        // kludgy ....
985ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
986ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
987ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
988ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
989ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
990ae1afd2fSChristopher Smith
991ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
99259bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
993ae1afd2fSChristopher Smith        if (empty($user)) {
994ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
995ae1afd2fSChristopher Smith            return false;
996ae1afd2fSChristopher Smith        }
997ae1afd2fSChristopher Smith
998ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
999ae1afd2fSChristopher Smith
1000ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
1001ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
1002ae1afd2fSChristopher Smith            return false;
1003ae1afd2fSChristopher Smith        }
1004ae1afd2fSChristopher Smith
1005328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
1006328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
1007ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
1008ae1afd2fSChristopher Smith                return false;
1009ae1afd2fSChristopher Smith            }
1010328143f8SChristopher Smith        } else {
1011328143f8SChristopher Smith            if (!empty($mail)) {
1012328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
1013328143f8SChristopher Smith                return false;
1014328143f8SChristopher Smith            }
1015328143f8SChristopher Smith        }
1016ae1afd2fSChristopher Smith
1017ae1afd2fSChristopher Smith        return $cleaned;
1018ae1afd2fSChristopher Smith    }
1019ae1afd2fSChristopher Smith
1020c5a7c0c6SGerrit Uitslag    /**
1021c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1022c5a7c0c6SGerrit Uitslag     *
1023c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1024c5a7c0c6SGerrit Uitslag     *
1025c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
1026c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10275ba64050SChristopher Smith     * @return bool whether successful
1028c5a7c0c6SGerrit Uitslag     */
10293712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1030ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1031ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1032ae1afd2fSChristopher Smith            return false;
1033ae1afd2fSChristopher Smith        }
1034ae1afd2fSChristopher Smith
1035ae1afd2fSChristopher Smith        return true;
1036ae1afd2fSChristopher Smith    }
1037ae1afd2fSChristopher Smith
1038c5a7c0c6SGerrit Uitslag    /**
1039c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1040c5a7c0c6SGerrit Uitslag     */
10413712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1042ae1afd2fSChristopher Smith
1043ae1afd2fSChristopher Smith        // ==============================================================================================
1044ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1045ae1afd2fSChristopher Smith        // normal headers for downloading...
1046ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1047ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1048ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1049ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1050ae1afd2fSChristopher Smith
1051ae1afd2fSChristopher Smith        // output the csv
1052ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1053c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1054ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1055ae1afd2fSChristopher Smith        }
1056ae1afd2fSChristopher Smith        fclose($fd);
1057ae1afd2fSChristopher Smith        die;
1058ae1afd2fSChristopher Smith    }
1059ae1afd2fSChristopher Smith
1060b2c01466SChristopher Smith    /**
1061b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1062253d4b48SGerrit Uitslag     *
1063253d4b48SGerrit Uitslag     * @param string $file filename
1064253d4b48SGerrit Uitslag     * @return bool
1065b2c01466SChristopher Smith     */
1066b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1067b2c01466SChristopher Smith        return is_uploaded_file($file);
1068b2c01466SChristopher Smith    }
1069b2c01466SChristopher Smith
1070c9454ee3SChristopher Smith    /**
1071c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1072c9454ee3SChristopher Smith     *
1073c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1074c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1075253d4b48SGerrit Uitslag     *
1076253d4b48SGerrit Uitslag     * @param string $csv string to parse
1077253d4b48SGerrit Uitslag     * @return array
1078c9454ee3SChristopher Smith     */
1079c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1080c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1081c9454ee3SChristopher Smith    }
1082c9454ee3SChristopher Smith
1083c9454ee3SChristopher Smith    /**
1084c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1085c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1086c9454ee3SChristopher Smith     *
1087c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1088253d4b48SGerrit Uitslag     *
1089253d4b48SGerrit Uitslag     * @param string $str string to parse
1090253d4b48SGerrit Uitslag     * @return array
1091c9454ee3SChristopher Smith     */
1092c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1093c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1094c9454ee3SChristopher Smith        fputs($fp, $str);
1095c9454ee3SChristopher Smith        rewind($fp);
1096c9454ee3SChristopher Smith
1097c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1098c9454ee3SChristopher Smith
1099c9454ee3SChristopher Smith        fclose($fp);
1100c9454ee3SChristopher Smith        return $data;
1101c9454ee3SChristopher Smith    }
11020440ff15Schris}
1103