xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 67a31a83dd6c8a3ff9e87da0c2070a2783aec44e)
10440ff15Schris<?php
20440ff15Schris/*
30440ff15Schris *  User Manager
40440ff15Schris *
50440ff15Schris *  Dokuwiki Admin Plugin
60440ff15Schris *
70440ff15Schris *  This version of the user manager has been modified to only work with
80440ff15Schris *  objectified version of auth system
90440ff15Schris *
100440ff15Schris *  @author  neolao <neolao@neolao.com>
110440ff15Schris *  @author  Chris Smith <chris@jalakai.co.uk>
120440ff15Schris */
13e04f1f16Schris// must be run within Dokuwiki
14e04f1f16Schrisif(!defined('DOKU_INC')) die();
15e04f1f16Schris
160440ff15Schrisif(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/');
170440ff15Schris
180440ff15Schris/**
190440ff15Schris * All DokuWiki plugins to extend the admin function
200440ff15Schris * need to inherit from this class
210440ff15Schris */
220440ff15Schrisclass admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
230440ff15Schris
243712ca9aSGerrit Uitslag    protected $_auth = null;        // auth object
253712ca9aSGerrit Uitslag    protected $_user_total = 0;     // number of registered users
263712ca9aSGerrit Uitslag    protected $_filter = array();   // user selection filter(s)
273712ca9aSGerrit Uitslag    protected $_start = 0;          // index of first user to be displayed
283712ca9aSGerrit Uitslag    protected $_last = 0;           // index of the last user to be displayed
293712ca9aSGerrit Uitslag    protected $_pagesize = 20;      // number of users to list on one page
303712ca9aSGerrit Uitslag    protected $_edit_user = '';     // set to user selected for editing
313712ca9aSGerrit Uitslag    protected $_edit_userdata = array();
323712ca9aSGerrit Uitslag    protected $_disabled = '';      // if disabled set to explanatory string
333712ca9aSGerrit Uitslag    protected $_import_failures = array();
340440ff15Schris
350440ff15Schris    /**
360440ff15Schris     * Constructor
370440ff15Schris     */
38c5a7c0c6SGerrit Uitslag    public function admin_plugin_usermanager(){
39c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
400440ff15Schris        global $auth;
410440ff15Schris
420440ff15Schris        $this->setupLocale();
4351d94d49Schris
4451d94d49Schris        if (!isset($auth)) {
45c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['noauth'];
4682fd59b6SAndreas Gohr        } else if (!$auth->canDo('getUsers')) {
47c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['nosupport'];
4851d94d49Schris        } else {
4951d94d49Schris
5051d94d49Schris            // we're good to go
5151d94d49Schris            $this->_auth = & $auth;
5251d94d49Schris
5351d94d49Schris        }
54ae1afd2fSChristopher Smith
55ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
560e80bb5eSChristopher Smith        if (!empty($_SESSION['import_failures'])){
57ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
58ae1afd2fSChristopher Smith        }
590440ff15Schris    }
600440ff15Schris
610440ff15Schris    /**
62c5a7c0c6SGerrit Uitslag     * Return prompt for admin menu
63253d4b48SGerrit Uitslag     *
64253d4b48SGerrit Uitslag     * @param string $language
65253d4b48SGerrit Uitslag     * @return string
660440ff15Schris     */
67c5a7c0c6SGerrit Uitslag    public function getMenuText($language) {
680440ff15Schris
690440ff15Schris        if (!is_null($this->_auth))
700440ff15Schris          return parent::getMenuText($language);
710440ff15Schris
72c5a7c0c6SGerrit Uitslag        return $this->getLang('menu').' '.$this->_disabled;
730440ff15Schris    }
740440ff15Schris
750440ff15Schris    /**
760440ff15Schris     * return sort order for position in admin menu
77253d4b48SGerrit Uitslag     *
78253d4b48SGerrit Uitslag     * @return int
790440ff15Schris     */
80c5a7c0c6SGerrit Uitslag    public function getMenuSort() {
810440ff15Schris        return 2;
820440ff15Schris    }
830440ff15Schris
840440ff15Schris    /**
85*67a31a83SMichael Große     * @return int current start value for pageination
86*67a31a83SMichael Große     */
87*67a31a83SMichael Große    public function getStart() {
88*67a31a83SMichael Große        return $this->_start;
89*67a31a83SMichael Große    }
90*67a31a83SMichael Große
91*67a31a83SMichael Große    /**
92*67a31a83SMichael Große     * @return int number of users per page
93*67a31a83SMichael Große     */
94*67a31a83SMichael Große    public function getPagesize() {
95*67a31a83SMichael Große        return $this->_pagesize;
96*67a31a83SMichael Große    }
97*67a31a83SMichael Große
98*67a31a83SMichael Große    /**
99c5a7c0c6SGerrit Uitslag     * Handle user request
100253d4b48SGerrit Uitslag     *
101253d4b48SGerrit Uitslag     * @return bool
1020440ff15Schris     */
103c5a7c0c6SGerrit Uitslag    public function handle() {
10400d58927SMichael Hamann        global $INPUT;
1050440ff15Schris        if (is_null($this->_auth)) return false;
1060440ff15Schris
1070440ff15Schris        // extract the command and any specific parameters
1080440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
10900d58927SMichael Hamann        $fn   = $INPUT->param('fn');
1100440ff15Schris
1110440ff15Schris        if (is_array($fn)) {
1120440ff15Schris            $cmd = key($fn);
1130440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
1140440ff15Schris        } else {
1150440ff15Schris            $cmd = $fn;
1160440ff15Schris            $param = null;
1170440ff15Schris        }
1180440ff15Schris
1190440ff15Schris        if ($cmd != "search") {
12000d58927SMichael Hamann            $this->_start = $INPUT->int('start', 0);
1210440ff15Schris            $this->_filter = $this->_retrieveFilter();
1220440ff15Schris        }
1230440ff15Schris
1240440ff15Schris        switch($cmd){
1250440ff15Schris            case "add"    : $this->_addUser(); break;
1260440ff15Schris            case "delete" : $this->_deleteUser(); break;
1270440ff15Schris            case "modify" : $this->_modifyUser(); break;
12878c7c8c9Schris            case "edit"   : $this->_editUser($param); break;
1290440ff15Schris            case "search" : $this->_setFilter($param);
1300440ff15Schris                            $this->_start = 0;
1310440ff15Schris                            break;
1325c967d3dSChristopher Smith            case "export" : $this->_export(); break;
133ae1afd2fSChristopher Smith            case "import" : $this->_import(); break;
134ae1afd2fSChristopher Smith            case "importfails" : $this->_downloadImportFailures(); break;
1350440ff15Schris        }
1360440ff15Schris
13751d94d49Schris        $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
1380440ff15Schris
1390440ff15Schris        // page handling
1400440ff15Schris        switch($cmd){
1410440ff15Schris            case 'start' : $this->_start = 0; break;
1420440ff15Schris            case 'prev'  : $this->_start -= $this->_pagesize; break;
1430440ff15Schris            case 'next'  : $this->_start += $this->_pagesize; break;
1440440ff15Schris            case 'last'  : $this->_start = $this->_user_total; break;
1450440ff15Schris        }
1460440ff15Schris        $this->_validatePagination();
147c5a7c0c6SGerrit Uitslag        return true;
1480440ff15Schris    }
1490440ff15Schris
1500440ff15Schris    /**
151c5a7c0c6SGerrit Uitslag     * Output appropriate html
152253d4b48SGerrit Uitslag     *
153253d4b48SGerrit Uitslag     * @return bool
1540440ff15Schris     */
155c5a7c0c6SGerrit Uitslag    public function html() {
1560440ff15Schris        global $ID;
1570440ff15Schris
1580440ff15Schris        if(is_null($this->_auth)) {
1590440ff15Schris            print $this->lang['badauth'];
1600440ff15Schris            return false;
1610440ff15Schris        }
1620440ff15Schris
1630440ff15Schris        $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
1640440ff15Schris
1650440ff15Schris        $page_buttons = $this->_pagination();
16682fd59b6SAndreas Gohr        $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
1670440ff15Schris
16877d19185SAndreas Gohr        $editable = $this->_auth->canDo('UserMod');
169b59cff8bSGerrit Uitslag        $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang['export_filtered'];
1706154103cSmatthiasgrimm
1710440ff15Schris        print $this->locale_xhtml('intro');
1720440ff15Schris        print $this->locale_xhtml('list');
1730440ff15Schris
17458dde80dSAnika Henke        ptln("<div id=\"user__manager\">");
17558dde80dSAnika Henke        ptln("<div class=\"level2\">");
1760440ff15Schris
17767019d15Schris        if ($this->_user_total > 0) {
1780440ff15Schris            ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
1790440ff15Schris        } else {
180a102b175SGerrit Uitslag            if($this->_user_total < 0) {
181a102b175SGerrit Uitslag                $allUserTotal = 0;
182a102b175SGerrit Uitslag            } else {
183a102b175SGerrit Uitslag                $allUserTotal = $this->_auth->getUserCount();
184a102b175SGerrit Uitslag            }
185a102b175SGerrit Uitslag            ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>");
1860440ff15Schris        }
1870440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
188634d7150SAndreas Gohr        formSecurityToken();
189c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1900440ff15Schris        ptln("  <table class=\"inline\">");
1910440ff15Schris        ptln("    <thead>");
1920440ff15Schris        ptln("      <tr>");
193e260f93bSAnika 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>");
1940440ff15Schris        ptln("      </tr>");
1950440ff15Schris
1960440ff15Schris        ptln("      <tr>");
1972365d73dSAnika 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>");
198a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
199a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
200a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
201a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
2020440ff15Schris        ptln("      </tr>");
2030440ff15Schris        ptln("    </thead>");
2040440ff15Schris
2050440ff15Schris        if ($this->_user_total) {
2060440ff15Schris            ptln("    <tbody>");
2070440ff15Schris            foreach ($user_list as $user => $userinfo) {
2080440ff15Schris                extract($userinfo);
209c5a7c0c6SGerrit Uitslag                /**
210c5a7c0c6SGerrit Uitslag                 * @var string $name
211c5a7c0c6SGerrit Uitslag                 * @var string $pass
212c5a7c0c6SGerrit Uitslag                 * @var string $mail
213c5a7c0c6SGerrit Uitslag                 * @var array  $grps
214c5a7c0c6SGerrit Uitslag                 */
2150440ff15Schris                $groups = join(', ',$grps);
216a2c0246eSAnika Henke                ptln("    <tr class=\"user_info\">");
2170440ff15Schris                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
2182365d73dSAnika Henke                if ($editable) {
21977d19185SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
22077d19185SAndreas Gohr                                                           'do' => 'admin',
22177d19185SAndreas Gohr                                                           'page' => 'usermanager',
22277d19185SAndreas Gohr                                                           'sectok' => getSecurityToken())).
22377d19185SAndreas Gohr                         "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
2242365d73dSAnika Henke                } else {
2252365d73dSAnika Henke                    ptln("    <td>".hsc($user)."</td>");
2262365d73dSAnika Henke                }
2272365d73dSAnika Henke                ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
2280440ff15Schris                ptln("    </tr>");
2290440ff15Schris            }
2300440ff15Schris            ptln("    </tbody>");
2310440ff15Schris        }
2320440ff15Schris
2330440ff15Schris        ptln("    <tbody>");
2342365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
235a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
236a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2370440ff15Schris        ptln("        </span>");
238a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
239a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
240a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
241a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
242a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2430440ff15Schris        ptln("        </span>");
2445c967d3dSChristopher Smith        if (!empty($this->_filter)) {
245a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2465c967d3dSChristopher Smith        }
2475c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2485164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2495164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
250daf4ca4eSAnika Henke
251daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
252daf4ca4eSAnika Henke
2530440ff15Schris        ptln("      </td></tr>");
2540440ff15Schris        ptln("    </tbody>");
2550440ff15Schris        ptln("  </table>");
256c7b28ffdSAnika Henke        ptln("  </div>");
2570440ff15Schris
2580440ff15Schris        ptln("</form>");
2590440ff15Schris        ptln("</div>");
2600440ff15Schris
261a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2620440ff15Schris
26382fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2640440ff15Schris            ptln("<div".$style.">");
2650440ff15Schris            print $this->locale_xhtml('add');
2660440ff15Schris            ptln("  <div class=\"level2\">");
2670440ff15Schris
26878c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2690440ff15Schris
2700440ff15Schris            ptln("  </div>");
2710440ff15Schris            ptln("</div>");
2720440ff15Schris        }
2730440ff15Schris
27482fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
275c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2760440ff15Schris            print $this->locale_xhtml('edit');
2770440ff15Schris            ptln("  <div class=\"level2\">");
2780440ff15Schris
27978c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2800440ff15Schris
2810440ff15Schris            ptln("  </div>");
2820440ff15Schris            ptln("</div>");
2830440ff15Schris        }
284ae1afd2fSChristopher Smith
285ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
286ae1afd2fSChristopher Smith            $this->_htmlImportForm();
287ae1afd2fSChristopher Smith        }
28858dde80dSAnika Henke        ptln("</div>");
289c5a7c0c6SGerrit Uitslag        return true;
2900440ff15Schris    }
2910440ff15Schris
29282fd59b6SAndreas Gohr    /**
293c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
294c5a7c0c6SGerrit Uitslag     *
295c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
296c5a7c0c6SGerrit Uitslag     * @param string $user id of user
297c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
298c5a7c0c6SGerrit Uitslag     * @param int    $indent
29982fd59b6SAndreas Gohr     */
3003712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
301a6858c6aSchris        global $conf;
302bb4866bdSchris        global $ID;
303be9008d3SChristopher Smith        global $lang;
30478c7c8c9Schris
30578c7c8c9Schris        $name = $mail = $groups = '';
306a6858c6aSchris        $notes = array();
3070440ff15Schris
3080440ff15Schris        if ($user) {
30978c7c8c9Schris            extract($userdata);
31078c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
311a6858c6aSchris        } else {
312a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
3130440ff15Schris        }
3140440ff15Schris
3150440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
316634d7150SAndreas Gohr        formSecurityToken();
317c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
3180440ff15Schris        ptln("  <table class=\"inline\">",$indent);
3190440ff15Schris        ptln("    <thead>",$indent);
3200440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
3210440ff15Schris        ptln("    </thead>",$indent);
3220440ff15Schris        ptln("    <tbody>",$indent);
32326fb387bSchris
32426fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
32526fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
326be9008d3SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),  $indent+6);
32726fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
32826fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
32926fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
33026fb387bSchris
331a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
332ee9498f5SChristopher Smith            if ($cmd == 'add') {
333c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
334ee9498f5SChristopher Smith            }
335a6858c6aSchris            if ($user) {
336a6858c6aSchris                $notes[] = $this->lang['note_notify'];
337a6858c6aSchris            }
338a6858c6aSchris
339a6858c6aSchris            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);
340a6858c6aSchris        }
341a6858c6aSchris
3420440ff15Schris        ptln("    </tbody>",$indent);
3430440ff15Schris        ptln("    <tbody>",$indent);
3440440ff15Schris        ptln("      <tr>",$indent);
3450440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3460440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3470440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3480440ff15Schris
3490440ff15Schris        // save current $user, we need this to access details if the name is changed
3500440ff15Schris        if ($user)
3510440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3520440ff15Schris
3530440ff15Schris        $this->_htmlFilterSettings($indent+10);
3540440ff15Schris
355a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3560440ff15Schris        ptln("        </td>",$indent);
3570440ff15Schris        ptln("      </tr>",$indent);
3580440ff15Schris        ptln("    </tbody>",$indent);
3590440ff15Schris        ptln("  </table>",$indent);
36045c19902SChristopher Smith
36145c19902SChristopher Smith        if ($notes) {
36245c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
36345c19902SChristopher Smith            foreach ($notes as $note) {
36445c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
36545c19902SChristopher Smith            }
36645c19902SChristopher Smith            ptln("    </ul>");
36745c19902SChristopher Smith        }
368c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3690440ff15Schris        ptln("</form>",$indent);
3700440ff15Schris    }
3710440ff15Schris
372c5a7c0c6SGerrit Uitslag    /**
373c5a7c0c6SGerrit Uitslag     * Prints a inputfield
374c5a7c0c6SGerrit Uitslag     *
375c5a7c0c6SGerrit Uitslag     * @param string $id
376c5a7c0c6SGerrit Uitslag     * @param string $name
377c5a7c0c6SGerrit Uitslag     * @param string $label
378c5a7c0c6SGerrit Uitslag     * @param string $value
379c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
380c5a7c0c6SGerrit Uitslag     * @param int $indent
381c5a7c0c6SGerrit Uitslag     */
3823712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3837de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3847de12fceSAndreas Gohr        echo str_pad('',$indent);
3857de12fceSAndreas Gohr
386359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
387d796a891SAndreas Gohr            $fieldtype = 'password';
388d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3897b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3907b3674bdSChristopher Smith            $fieldtype = 'email';
3917b3674bdSChristopher Smith            $autocomp  = '';
392d796a891SAndreas Gohr        }else{
393d796a891SAndreas Gohr            $fieldtype = 'text';
394d796a891SAndreas Gohr            $autocomp  = '';
395d796a891SAndreas Gohr        }
396d796a891SAndreas Gohr
3977de12fceSAndreas Gohr        echo "<tr $class>";
3987de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3997de12fceSAndreas Gohr        echo "<td>";
4007de12fceSAndreas Gohr        if($cando){
401d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
4027de12fceSAndreas Gohr        }else{
4037de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
404ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
4057de12fceSAndreas Gohr        }
4067de12fceSAndreas Gohr        echo "</td>";
4077de12fceSAndreas Gohr        echo "</tr>";
40826fb387bSchris    }
40926fb387bSchris
410c5a7c0c6SGerrit Uitslag    /**
411c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
412c5a7c0c6SGerrit Uitslag     *
413c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
414c5a7c0c6SGerrit Uitslag     * @return string html escaped value
415c5a7c0c6SGerrit Uitslag     */
4163712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
4170440ff15Schris        if (empty($this->_filter)) return '';
4180440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
4190440ff15Schris    }
4200440ff15Schris
421c5a7c0c6SGerrit Uitslag    /**
422c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
423c5a7c0c6SGerrit Uitslag     *
424c5a7c0c6SGerrit Uitslag     * @param int $indent
425c5a7c0c6SGerrit Uitslag     */
4263712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4270440ff15Schris
4280440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4290440ff15Schris
4300440ff15Schris        foreach ($this->_filter as $key => $filter) {
4310440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4320440ff15Schris        }
4330440ff15Schris    }
4340440ff15Schris
435c5a7c0c6SGerrit Uitslag    /**
436c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
437c5a7c0c6SGerrit Uitslag     *
438c5a7c0c6SGerrit Uitslag     * @param int $indent
439c5a7c0c6SGerrit Uitslag     */
4403712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
441ae1afd2fSChristopher Smith        global $ID;
442ae1afd2fSChristopher Smith
443ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
444ae1afd2fSChristopher Smith
445ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
446ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
447ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
448ae1afd2fSChristopher Smith        formSecurityToken();
449b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
450ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
451ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
452ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
453ae1afd2fSChristopher Smith
454ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
455ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
456ae1afd2fSChristopher Smith        ptln('</div>');
457ae1afd2fSChristopher Smith
458ae1afd2fSChristopher Smith        // list failures from the previous import
459ae1afd2fSChristopher Smith        if ($this->_import_failures) {
460ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
461ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
462b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
463ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
464ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
465ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
466ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
467ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
468ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
469ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
470ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
471ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
472ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
473ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
474ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
475ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
476ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
477ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
478ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
479ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
480ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
481ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
482ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
483ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
484ae1afd2fSChristopher Smith            }
485ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
486ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
487b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
488ae1afd2fSChristopher Smith            ptln('</div>');
489ae1afd2fSChristopher Smith        }
490ae1afd2fSChristopher Smith
491ae1afd2fSChristopher Smith    }
492ae1afd2fSChristopher Smith
493c5a7c0c6SGerrit Uitslag    /**
494c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
495c5a7c0c6SGerrit Uitslag     *
496c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
497c5a7c0c6SGerrit Uitslag     */
4983712ca9aSGerrit Uitslag    protected function _addUser(){
49900d58927SMichael Hamann        global $INPUT;
500634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
50182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
5020440ff15Schris
503359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
5040440ff15Schris        if (empty($user)) return false;
5056733c4d7SChris Smith
5066733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
507c3f4fb63SGina Haeussge            if (empty($pass)){
50800d58927SMichael Hamann                if($INPUT->has('usernotify')){
5098a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
510c3f4fb63SGina Haeussge                } else {
51160b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
51260b9901bSAndreas Gohr                    return false;
51360b9901bSAndreas Gohr                }
514359e9417SChristopher Smith            } else {
515359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
516359e9417SChristopher Smith                    return false;
517359e9417SChristopher Smith                }
5186733c4d7SChris Smith            }
5196733c4d7SChris Smith        } else {
5206733c4d7SChris Smith            if (!empty($pass)){
5216733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5226733c4d7SChris Smith                return false;
5236733c4d7SChris Smith            }
5246733c4d7SChris Smith        }
5256733c4d7SChris Smith
5266733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5276733c4d7SChris Smith            if (empty($name)){
5286733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5296733c4d7SChris Smith                return false;
5306733c4d7SChris Smith            }
5316733c4d7SChris Smith        } else {
5326733c4d7SChris Smith            if (!empty($name)){
5336733c4d7SChris Smith                return false;
5346733c4d7SChris Smith            }
5356733c4d7SChris Smith        }
5366733c4d7SChris Smith
5376733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5386733c4d7SChris Smith            if (empty($mail)){
5396733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5406733c4d7SChris Smith                return false;
5416733c4d7SChris Smith            }
5426733c4d7SChris Smith        } else {
5436733c4d7SChris Smith            if (!empty($mail)){
5446733c4d7SChris Smith                return false;
5456733c4d7SChris Smith            }
5466733c4d7SChris Smith        }
5470440ff15Schris
5487d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
549a6858c6aSchris
550a6858c6aSchris            msg($this->lang['add_ok'], 1);
551a6858c6aSchris
55200d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
553a6858c6aSchris                $this->_notifyUser($user,$pass);
554a6858c6aSchris            }
555a6858c6aSchris        } else {
55660b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
557a6858c6aSchris        }
558a6858c6aSchris
559a6858c6aSchris        return $ok;
5600440ff15Schris    }
5610440ff15Schris
5620440ff15Schris    /**
563c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
564c5a7c0c6SGerrit Uitslag     *
565c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5660440ff15Schris     */
5673712ca9aSGerrit Uitslag    protected function _deleteUser(){
56800d58927SMichael Hamann        global $conf, $INPUT;
5699ec82636SAndreas Gohr
570634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
57182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5720440ff15Schris
57300d58927SMichael Hamann        $selected = $INPUT->arr('delete');
57400d58927SMichael Hamann        if (empty($selected)) return false;
5750440ff15Schris        $selected = array_keys($selected);
5760440ff15Schris
577c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
578c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
579c9a8f912SMichael Klier            return false;
580c9a8f912SMichael Klier        }
581c9a8f912SMichael Klier
5827d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5830440ff15Schris        if ($count == count($selected)) {
5840440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5850440ff15Schris            msg("$text.", 1);
5860440ff15Schris        } else {
5870440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5880440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5890440ff15Schris            msg("$part1, $part2",-1);
5900440ff15Schris        }
59178c7c8c9Schris
5929ec82636SAndreas Gohr        // invalidate all sessions
5939ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5949ec82636SAndreas Gohr
59578c7c8c9Schris        return true;
59678c7c8c9Schris    }
59778c7c8c9Schris
59878c7c8c9Schris    /**
59978c7c8c9Schris     * Edit user (a user has been selected for editing)
600c5a7c0c6SGerrit Uitslag     *
601c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
602c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
60378c7c8c9Schris     */
6043712ca9aSGerrit Uitslag    protected function _editUser($param) {
605634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
60678c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
607786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
60878c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
60978c7c8c9Schris
61078c7c8c9Schris        // no user found?
61178c7c8c9Schris        if (!$userdata) {
61278c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
61378c7c8c9Schris            return false;
61478c7c8c9Schris        }
61578c7c8c9Schris
61678c7c8c9Schris        $this->_edit_user = $user;
61778c7c8c9Schris        $this->_edit_userdata = $userdata;
61878c7c8c9Schris
61978c7c8c9Schris        return true;
6200440ff15Schris    }
6210440ff15Schris
6220440ff15Schris    /**
623c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
624c5a7c0c6SGerrit Uitslag     *
625c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6260440ff15Schris     */
6273712ca9aSGerrit Uitslag    protected function _modifyUser(){
62800d58927SMichael Hamann        global $conf, $INPUT;
6299ec82636SAndreas Gohr
630634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
63182fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6320440ff15Schris
63326fb387bSchris        // get currently valid  user data
634786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
635073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
636073766c6Smatthiasgrimm
63726fb387bSchris        // get new user data subject to change
638359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
639073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6400440ff15Schris
6410440ff15Schris        $changes = array();
642073766c6Smatthiasgrimm        if ($newuser != $olduser) {
64326fb387bSchris
64426fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
64526fb387bSchris                msg($this->lang['update_fail'],-1);
64626fb387bSchris                return false;
64726fb387bSchris            }
64826fb387bSchris
64926fb387bSchris            // check if $newuser already exists
650073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
651073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
652a6858c6aSchris                $re_edit = true;
6530440ff15Schris            } else {
654073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6550440ff15Schris            }
65693eefc2fSAndreas Gohr        }
657359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
6582400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
659359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
660359e9417SChristopher Smith                    $changes['pass'] = $newpass;
661359e9417SChristopher Smith                } else {
662359e9417SChristopher Smith                    return false;
663359e9417SChristopher Smith                }
664359e9417SChristopher Smith            } else {
665359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
666359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
667359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
668359e9417SChristopher Smith                }
669359e9417SChristopher Smith            }
6700440ff15Schris        }
6710440ff15Schris
67240d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
673073766c6Smatthiasgrimm            $changes['name'] = $newname;
67440d72af6SChristopher Smith        }
67540d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
676073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
67740d72af6SChristopher Smith        }
67840d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
679073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
68040d72af6SChristopher Smith        }
6810440ff15Schris
6827d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6830440ff15Schris            msg($this->lang['update_ok'],1);
684a6858c6aSchris
6856ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
686a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
6876ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
688a6858c6aSchris            }
689a6858c6aSchris
6909ec82636SAndreas Gohr            // invalidate all sessions
6919ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6929ec82636SAndreas Gohr
6930440ff15Schris        } else {
6940440ff15Schris            msg($this->lang['update_fail'],-1);
6950440ff15Schris        }
69678c7c8c9Schris
697a6858c6aSchris        if (!empty($re_edit)) {
698a6858c6aSchris            $this->_editUser($olduser);
6990440ff15Schris        }
7000440ff15Schris
701a6858c6aSchris        return $ok;
702a6858c6aSchris    }
703a6858c6aSchris
704a6858c6aSchris    /**
705c5a7c0c6SGerrit Uitslag     * Send password change notification email
706c5a7c0c6SGerrit Uitslag     *
707c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
708c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
709c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
710c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
711a6858c6aSchris     */
7123712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
713a6858c6aSchris
714a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
715328143f8SChristopher Smith            if ($status_alert) {
716a6858c6aSchris                msg($this->lang['notify_ok'], 1);
717328143f8SChristopher Smith            }
718a6858c6aSchris        } else {
719328143f8SChristopher Smith            if ($status_alert) {
720a6858c6aSchris                msg($this->lang['notify_fail'], -1);
721a6858c6aSchris            }
722328143f8SChristopher Smith        }
723a6858c6aSchris
724a6858c6aSchris        return $sent;
725a6858c6aSchris    }
726a6858c6aSchris
727a6858c6aSchris    /**
728359e9417SChristopher Smith     * Verify password meets minimum requirements
729359e9417SChristopher Smith     * :TODO: extend to support password strength
730359e9417SChristopher Smith     *
731359e9417SChristopher Smith     * @param string  $password   candidate string for new password
732359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
733359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
734359e9417SChristopher Smith     */
735359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
736be9008d3SChristopher Smith        global $lang;
737359e9417SChristopher Smith
7382400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
739359e9417SChristopher Smith            return false;
740359e9417SChristopher Smith        }
741359e9417SChristopher Smith
742359e9417SChristopher Smith        if ($password !== $confirm) {
743be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
744359e9417SChristopher Smith            return false;
745359e9417SChristopher Smith        }
746359e9417SChristopher Smith
747359e9417SChristopher Smith        // :TODO: test password for required strength
748359e9417SChristopher Smith
749359e9417SChristopher Smith        // if we make it this far the password is good
750359e9417SChristopher Smith        return true;
751359e9417SChristopher Smith    }
752359e9417SChristopher Smith
753359e9417SChristopher Smith    /**
754c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
755a6858c6aSchris     *
756c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
757a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7580440ff15Schris     */
7593712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
760c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7617441e340SAndreas Gohr        global $auth;
762fbfbbe8aSHakan Sandell        global $INPUT;
7630440ff15Schris
76459bc3b48SGerrit Uitslag        $user = array();
765fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
766fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
767fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
768fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
769fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
770359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7710440ff15Schris
7727441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7737441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7747441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7757441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7767441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7770440ff15Schris
7780440ff15Schris        return $user;
7790440ff15Schris    }
7800440ff15Schris
781c5a7c0c6SGerrit Uitslag    /**
782c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
783c5a7c0c6SGerrit Uitslag     *
784c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
785c5a7c0c6SGerrit Uitslag     */
7863712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7870440ff15Schris
7880440ff15Schris        $this->_filter = array();
7890440ff15Schris
7900440ff15Schris        if ($op == 'new') {
79159bc3b48SGerrit Uitslag            list($user,/* $pass */,$name,$mail,$grps) = $this->_retrieveUser(false);
7920440ff15Schris
7930440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7940440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7950440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7960440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7970440ff15Schris        }
7980440ff15Schris    }
7990440ff15Schris
800c5a7c0c6SGerrit Uitslag    /**
801c5a7c0c6SGerrit Uitslag     * Get the current search terms
802c5a7c0c6SGerrit Uitslag     *
803c5a7c0c6SGerrit Uitslag     * @return array
804c5a7c0c6SGerrit Uitslag     */
8053712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
806fbfbbe8aSHakan Sandell        global $INPUT;
8070440ff15Schris
808fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
8090440ff15Schris
8100440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
8110440ff15Schris        $filter = array();
8120440ff15Schris
8130440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
8140440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
8150440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
8160440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
8170440ff15Schris
8180440ff15Schris        return $filter;
8190440ff15Schris    }
8200440ff15Schris
821c5a7c0c6SGerrit Uitslag    /**
822c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
823c5a7c0c6SGerrit Uitslag     */
8243712ca9aSGerrit Uitslag    protected function _validatePagination() {
8250440ff15Schris
8260440ff15Schris        if ($this->_start >= $this->_user_total) {
8270440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8280440ff15Schris        }
8290440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8300440ff15Schris
8310440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8320440ff15Schris    }
8330440ff15Schris
834c5a7c0c6SGerrit Uitslag    /**
835c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
836c5a7c0c6SGerrit Uitslag     *
837c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8380440ff15Schris     */
8393712ca9aSGerrit Uitslag    protected function _pagination() {
8400440ff15Schris
84151d94d49Schris        $disabled = 'disabled="disabled"';
84251d94d49Schris
84359bc3b48SGerrit Uitslag        $buttons = array();
84451d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
84551d94d49Schris
84651d94d49Schris        if ($this->_user_total == -1) {
84751d94d49Schris            $buttons['last'] = $disabled;
84851d94d49Schris            $buttons['next'] = '';
84951d94d49Schris        } else {
85051d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
85151d94d49Schris        }
8520440ff15Schris
8530440ff15Schris        return $buttons;
8540440ff15Schris    }
8555c967d3dSChristopher Smith
856c5a7c0c6SGerrit Uitslag    /**
857c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8585c967d3dSChristopher Smith     */
8593712ca9aSGerrit Uitslag    protected function _export() {
8605c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8615c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8625c967d3dSChristopher Smith        $column_headings = array(
8635c967d3dSChristopher Smith            $this->lang["user_id"],
8645c967d3dSChristopher Smith            $this->lang["user_name"],
8655c967d3dSChristopher Smith            $this->lang["user_mail"],
8665c967d3dSChristopher Smith            $this->lang["user_groups"]
8675c967d3dSChristopher Smith        );
8685c967d3dSChristopher Smith
8695c967d3dSChristopher Smith        // ==============================================================================================
8705c967d3dSChristopher Smith        // GENERATE OUTPUT
8715c967d3dSChristopher Smith        // normal headers for downloading...
8725c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8735c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8745c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8755c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8765c967d3dSChristopher Smith
8775c967d3dSChristopher Smith        // output the csv
8785c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8795c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8805c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8815c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8825c967d3dSChristopher Smith            fputcsv($fd, $line);
8835c967d3dSChristopher Smith        }
8845c967d3dSChristopher Smith        fclose($fd);
885b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
886b2c01466SChristopher Smith
8875c967d3dSChristopher Smith        die;
8885c967d3dSChristopher Smith    }
889ae1afd2fSChristopher Smith
890c5a7c0c6SGerrit Uitslag    /**
891c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
892ae1afd2fSChristopher Smith     *
893ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
894c5a7c0c6SGerrit Uitslag     *
8955ba64050SChristopher Smith     * @return bool whether successful
896ae1afd2fSChristopher Smith     */
8973712ca9aSGerrit Uitslag    protected function _import() {
898ae1afd2fSChristopher Smith        // check we are allowed to add users
899ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
900ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
901ae1afd2fSChristopher Smith
902ae1afd2fSChristopher Smith        // check file uploaded ok.
903b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
904ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
905ae1afd2fSChristopher Smith            return false;
906ae1afd2fSChristopher Smith        }
907ae1afd2fSChristopher Smith        // retrieve users from the file
908ae1afd2fSChristopher Smith        $this->_import_failures = array();
909ae1afd2fSChristopher Smith        $import_success_count = 0;
910ae1afd2fSChristopher Smith        $import_fail_count = 0;
911ae1afd2fSChristopher Smith        $line = 0;
912ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
913ae1afd2fSChristopher Smith        if ($fd) {
914ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
915efcec72bSChristopher Smith                if (!utf8_check($csv)) {
916efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
917efcec72bSChristopher Smith                }
918c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
919ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
920ae1afd2fSChristopher Smith                // data checks...
921ae1afd2fSChristopher Smith                if (1 == ++$line) {
922ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
923ae1afd2fSChristopher Smith                }
924ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
925ae1afd2fSChristopher Smith                    $import_fail_count++;
926ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
927ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
928ae1afd2fSChristopher Smith                    continue;
929ae1afd2fSChristopher Smith                }
930ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
931ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
932ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
933328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
934328143f8SChristopher Smith                    if (!$sent){
935328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
936328143f8SChristopher Smith                    }
937ae1afd2fSChristopher Smith                    $import_success_count++;
938ae1afd2fSChristopher Smith                } else {
939ae1afd2fSChristopher Smith                    $import_fail_count++;
940e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
941ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
942ae1afd2fSChristopher Smith                }
943ae1afd2fSChristopher Smith            }
944ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
945ae1afd2fSChristopher Smith            if ($import_fail_count) {
946ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
947ae1afd2fSChristopher Smith            }
948ae1afd2fSChristopher Smith        } else {
949ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
950ae1afd2fSChristopher Smith        }
951ae1afd2fSChristopher Smith
952ae1afd2fSChristopher Smith        // save import failures into the session
953ae1afd2fSChristopher Smith        if (!headers_sent()) {
954ae1afd2fSChristopher Smith            session_start();
955ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
956ae1afd2fSChristopher Smith            session_write_close();
957ae1afd2fSChristopher Smith        }
958c5a7c0c6SGerrit Uitslag        return true;
959ae1afd2fSChristopher Smith    }
960ae1afd2fSChristopher Smith
961c5a7c0c6SGerrit Uitslag    /**
962786dfb0eSGerrit Uitslag     * Returns cleaned user data
963c5a7c0c6SGerrit Uitslag     *
964c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
965253d4b48SGerrit Uitslag     * @param string $error
966253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
967c5a7c0c6SGerrit Uitslag     */
9683712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
969ae1afd2fSChristopher Smith        global $INPUT;
970ae1afd2fSChristopher Smith
971ae1afd2fSChristopher Smith        // kludgy ....
972ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
973ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
974ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
975ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
976ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
977ae1afd2fSChristopher Smith
978ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
97959bc3b48SGerrit Uitslag        list($user,/* $pass */,$name,$mail,/* $grps */) = $cleaned;
980ae1afd2fSChristopher Smith        if (empty($user)) {
981ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
982ae1afd2fSChristopher Smith            return false;
983ae1afd2fSChristopher Smith        }
984ae1afd2fSChristopher Smith
985ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
986ae1afd2fSChristopher Smith
987ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
988ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
989ae1afd2fSChristopher Smith            return false;
990ae1afd2fSChristopher Smith        }
991ae1afd2fSChristopher Smith
992328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
993328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
994ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
995ae1afd2fSChristopher Smith                return false;
996ae1afd2fSChristopher Smith            }
997328143f8SChristopher Smith        } else {
998328143f8SChristopher Smith            if (!empty($mail)) {
999328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
1000328143f8SChristopher Smith                return false;
1001328143f8SChristopher Smith            }
1002328143f8SChristopher Smith        }
1003ae1afd2fSChristopher Smith
1004ae1afd2fSChristopher Smith        return $cleaned;
1005ae1afd2fSChristopher Smith    }
1006ae1afd2fSChristopher Smith
1007c5a7c0c6SGerrit Uitslag    /**
1008c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1009c5a7c0c6SGerrit Uitslag     *
1010c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1011c5a7c0c6SGerrit Uitslag     *
1012c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
1013c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
10145ba64050SChristopher Smith     * @return bool whether successful
1015c5a7c0c6SGerrit Uitslag     */
10163712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
1017ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
1018ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1019ae1afd2fSChristopher Smith            return false;
1020ae1afd2fSChristopher Smith        }
1021ae1afd2fSChristopher Smith
1022ae1afd2fSChristopher Smith        return true;
1023ae1afd2fSChristopher Smith    }
1024ae1afd2fSChristopher Smith
1025c5a7c0c6SGerrit Uitslag    /**
1026c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1027c5a7c0c6SGerrit Uitslag     */
10283712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1029ae1afd2fSChristopher Smith
1030ae1afd2fSChristopher Smith        // ==============================================================================================
1031ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1032ae1afd2fSChristopher Smith        // normal headers for downloading...
1033ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1034ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1035ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1036ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1037ae1afd2fSChristopher Smith
1038ae1afd2fSChristopher Smith        // output the csv
1039ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1040c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1041ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1042ae1afd2fSChristopher Smith        }
1043ae1afd2fSChristopher Smith        fclose($fd);
1044ae1afd2fSChristopher Smith        die;
1045ae1afd2fSChristopher Smith    }
1046ae1afd2fSChristopher Smith
1047b2c01466SChristopher Smith    /**
1048b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1049253d4b48SGerrit Uitslag     *
1050253d4b48SGerrit Uitslag     * @param string $file filename
1051253d4b48SGerrit Uitslag     * @return bool
1052b2c01466SChristopher Smith     */
1053b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1054b2c01466SChristopher Smith        return is_uploaded_file($file);
1055b2c01466SChristopher Smith    }
1056b2c01466SChristopher Smith
1057c9454ee3SChristopher Smith    /**
1058c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1059c9454ee3SChristopher Smith     *
1060c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1061c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1062253d4b48SGerrit Uitslag     *
1063253d4b48SGerrit Uitslag     * @param string $csv string to parse
1064253d4b48SGerrit Uitslag     * @return array
1065c9454ee3SChristopher Smith     */
1066c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1067c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1068c9454ee3SChristopher Smith    }
1069c9454ee3SChristopher Smith
1070c9454ee3SChristopher Smith    /**
1071c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1072c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1073c9454ee3SChristopher Smith     *
1074c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1075253d4b48SGerrit Uitslag     *
1076253d4b48SGerrit Uitslag     * @param string $str string to parse
1077253d4b48SGerrit Uitslag     * @return array
1078c9454ee3SChristopher Smith     */
1079c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1080c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1081c9454ee3SChristopher Smith        fputs($fp, $str);
1082c9454ee3SChristopher Smith        rewind($fp);
1083c9454ee3SChristopher Smith
1084c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1085c9454ee3SChristopher Smith
1086c9454ee3SChristopher Smith        fclose($fp);
1087c9454ee3SChristopher Smith        return $data;
1088c9454ee3SChristopher Smith    }
10890440ff15Schris}
1090