xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 359e941731104cd989739d789f476590011eb518)
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
56ae1afd2fSChristopher Smith        if ($_SESSION['import_failures']){
57ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
58ae1afd2fSChristopher Smith        }
590440ff15Schris    }
600440ff15Schris
610440ff15Schris     /**
62c5a7c0c6SGerrit Uitslag      * Return prompt for admin menu
630440ff15Schris      */
64c5a7c0c6SGerrit Uitslag    public function getMenuText($language) {
650440ff15Schris
660440ff15Schris        if (!is_null($this->_auth))
670440ff15Schris          return parent::getMenuText($language);
680440ff15Schris
69c5a7c0c6SGerrit Uitslag        return $this->getLang('menu').' '.$this->_disabled;
700440ff15Schris    }
710440ff15Schris
720440ff15Schris    /**
730440ff15Schris     * return sort order for position in admin menu
740440ff15Schris     */
75c5a7c0c6SGerrit Uitslag    public function getMenuSort() {
760440ff15Schris        return 2;
770440ff15Schris    }
780440ff15Schris
790440ff15Schris    /**
80c5a7c0c6SGerrit Uitslag     * Handle user request
810440ff15Schris     */
82c5a7c0c6SGerrit Uitslag    public function handle() {
8300d58927SMichael Hamann        global $INPUT;
840440ff15Schris        if (is_null($this->_auth)) return false;
850440ff15Schris
860440ff15Schris        // extract the command and any specific parameters
870440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
8800d58927SMichael Hamann        $fn   = $INPUT->param('fn');
890440ff15Schris
900440ff15Schris        if (is_array($fn)) {
910440ff15Schris            $cmd = key($fn);
920440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
930440ff15Schris        } else {
940440ff15Schris            $cmd = $fn;
950440ff15Schris            $param = null;
960440ff15Schris        }
970440ff15Schris
980440ff15Schris        if ($cmd != "search") {
9900d58927SMichael Hamann            $this->_start = $INPUT->int('start', 0);
1000440ff15Schris            $this->_filter = $this->_retrieveFilter();
1010440ff15Schris        }
1020440ff15Schris
1030440ff15Schris        switch($cmd){
1040440ff15Schris            case "add"    : $this->_addUser(); break;
1050440ff15Schris            case "delete" : $this->_deleteUser(); break;
1060440ff15Schris            case "modify" : $this->_modifyUser(); break;
10778c7c8c9Schris            case "edit"   : $this->_editUser($param); break;
1080440ff15Schris            case "search" : $this->_setFilter($param);
1090440ff15Schris                            $this->_start = 0;
1100440ff15Schris                            break;
1115c967d3dSChristopher Smith            case "export" : $this->_export(); break;
112ae1afd2fSChristopher Smith            case "import" : $this->_import(); break;
113ae1afd2fSChristopher Smith            case "importfails" : $this->_downloadImportFailures(); break;
1140440ff15Schris        }
1150440ff15Schris
11651d94d49Schris        $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
1170440ff15Schris
1180440ff15Schris        // page handling
1190440ff15Schris        switch($cmd){
1200440ff15Schris            case 'start' : $this->_start = 0; break;
1210440ff15Schris            case 'prev'  : $this->_start -= $this->_pagesize; break;
1220440ff15Schris            case 'next'  : $this->_start += $this->_pagesize; break;
1230440ff15Schris            case 'last'  : $this->_start = $this->_user_total; break;
1240440ff15Schris        }
1250440ff15Schris        $this->_validatePagination();
126c5a7c0c6SGerrit Uitslag        return true;
1270440ff15Schris    }
1280440ff15Schris
1290440ff15Schris    /**
130c5a7c0c6SGerrit Uitslag     * Output appropriate html
1310440ff15Schris     */
132c5a7c0c6SGerrit Uitslag    public function html() {
1330440ff15Schris        global $ID;
1340440ff15Schris
1350440ff15Schris        if(is_null($this->_auth)) {
1360440ff15Schris            print $this->lang['badauth'];
1370440ff15Schris            return false;
1380440ff15Schris        }
1390440ff15Schris
1400440ff15Schris        $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
1410440ff15Schris
1420440ff15Schris        $page_buttons = $this->_pagination();
14382fd59b6SAndreas Gohr        $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
1440440ff15Schris
14577d19185SAndreas Gohr        $editable = $this->_auth->canDo('UserMod');
146b59cff8bSGerrit Uitslag        $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang['export_filtered'];
1476154103cSmatthiasgrimm
1480440ff15Schris        print $this->locale_xhtml('intro');
1490440ff15Schris        print $this->locale_xhtml('list');
1500440ff15Schris
15158dde80dSAnika Henke        ptln("<div id=\"user__manager\">");
15258dde80dSAnika Henke        ptln("<div class=\"level2\">");
1530440ff15Schris
15467019d15Schris        if ($this->_user_total > 0) {
1550440ff15Schris            ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
1560440ff15Schris        } else {
157a102b175SGerrit Uitslag            if($this->_user_total < 0) {
158a102b175SGerrit Uitslag                $allUserTotal = 0;
159a102b175SGerrit Uitslag            } else {
160a102b175SGerrit Uitslag                $allUserTotal = $this->_auth->getUserCount();
161a102b175SGerrit Uitslag            }
162a102b175SGerrit Uitslag            ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>");
1630440ff15Schris        }
1640440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
165634d7150SAndreas Gohr        formSecurityToken();
166c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1670440ff15Schris        ptln("  <table class=\"inline\">");
1680440ff15Schris        ptln("    <thead>");
1690440ff15Schris        ptln("      <tr>");
170e260f93bSAnika 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>");
1710440ff15Schris        ptln("      </tr>");
1720440ff15Schris
1730440ff15Schris        ptln("      <tr>");
1742365d73dSAnika 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>");
175a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
176a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
177a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
178a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
1790440ff15Schris        ptln("      </tr>");
1800440ff15Schris        ptln("    </thead>");
1810440ff15Schris
1820440ff15Schris        if ($this->_user_total) {
1830440ff15Schris            ptln("    <tbody>");
1840440ff15Schris            foreach ($user_list as $user => $userinfo) {
1850440ff15Schris                extract($userinfo);
186c5a7c0c6SGerrit Uitslag                /**
187c5a7c0c6SGerrit Uitslag                 * @var string $name
188c5a7c0c6SGerrit Uitslag                 * @var string $pass
189c5a7c0c6SGerrit Uitslag                 * @var string $mail
190c5a7c0c6SGerrit Uitslag                 * @var array  $grps
191c5a7c0c6SGerrit Uitslag                 */
1920440ff15Schris                $groups = join(', ',$grps);
193a2c0246eSAnika Henke                ptln("    <tr class=\"user_info\">");
1940440ff15Schris                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
1952365d73dSAnika Henke                if ($editable) {
19677d19185SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
19777d19185SAndreas Gohr                                                           'do' => 'admin',
19877d19185SAndreas Gohr                                                           'page' => 'usermanager',
19977d19185SAndreas Gohr                                                           'sectok' => getSecurityToken())).
20077d19185SAndreas Gohr                         "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
2012365d73dSAnika Henke                } else {
2022365d73dSAnika Henke                    ptln("    <td>".hsc($user)."</td>");
2032365d73dSAnika Henke                }
2042365d73dSAnika Henke                ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
2050440ff15Schris                ptln("    </tr>");
2060440ff15Schris            }
2070440ff15Schris            ptln("    </tbody>");
2080440ff15Schris        }
2090440ff15Schris
2100440ff15Schris        ptln("    <tbody>");
2112365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
212a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
213a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2140440ff15Schris        ptln("        </span>");
215a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
216a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
217a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
218a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
219a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2200440ff15Schris        ptln("        </span>");
2215c967d3dSChristopher Smith        if (!empty($this->_filter)) {
222a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2235c967d3dSChristopher Smith        }
2245c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2255164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2265164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
227daf4ca4eSAnika Henke
228daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
229daf4ca4eSAnika Henke
2300440ff15Schris        ptln("      </td></tr>");
2310440ff15Schris        ptln("    </tbody>");
2320440ff15Schris        ptln("  </table>");
233c7b28ffdSAnika Henke        ptln("  </div>");
2340440ff15Schris
2350440ff15Schris        ptln("</form>");
2360440ff15Schris        ptln("</div>");
2370440ff15Schris
238a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2390440ff15Schris
24082fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2410440ff15Schris            ptln("<div".$style.">");
2420440ff15Schris            print $this->locale_xhtml('add');
2430440ff15Schris            ptln("  <div class=\"level2\">");
2440440ff15Schris
24578c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2460440ff15Schris
2470440ff15Schris            ptln("  </div>");
2480440ff15Schris            ptln("</div>");
2490440ff15Schris        }
2500440ff15Schris
25182fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
252c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2530440ff15Schris            print $this->locale_xhtml('edit');
2540440ff15Schris            ptln("  <div class=\"level2\">");
2550440ff15Schris
25678c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2570440ff15Schris
2580440ff15Schris            ptln("  </div>");
2590440ff15Schris            ptln("</div>");
2600440ff15Schris        }
261ae1afd2fSChristopher Smith
262ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
263ae1afd2fSChristopher Smith            $this->_htmlImportForm();
264ae1afd2fSChristopher Smith        }
26558dde80dSAnika Henke        ptln("</div>");
266c5a7c0c6SGerrit Uitslag        return true;
2670440ff15Schris    }
2680440ff15Schris
26982fd59b6SAndreas Gohr    /**
270c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
271c5a7c0c6SGerrit Uitslag     *
272c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
273c5a7c0c6SGerrit Uitslag     * @param string $user id of user
274c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
275c5a7c0c6SGerrit Uitslag     * @param int    $indent
27682fd59b6SAndreas Gohr     */
2773712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
278a6858c6aSchris        global $conf;
279bb4866bdSchris        global $ID;
28078c7c8c9Schris
28178c7c8c9Schris        $name = $mail = $groups = '';
282a6858c6aSchris        $notes = array();
2830440ff15Schris
2840440ff15Schris        if ($user) {
28578c7c8c9Schris            extract($userdata);
28678c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
287a6858c6aSchris        } else {
288a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2890440ff15Schris        }
2900440ff15Schris
2910440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
292634d7150SAndreas Gohr        formSecurityToken();
293c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
2940440ff15Schris        ptln("  <table class=\"inline\">",$indent);
2950440ff15Schris        ptln("    <thead>",$indent);
2960440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
2970440ff15Schris        ptln("    </thead>",$indent);
2980440ff15Schris        ptln("    <tbody>",$indent);
29926fb387bSchris
30026fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
30126fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
302*359e9417SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $this->lang["user_passconfirm"], "", $this->_auth->canDo("modPass"), $indent+6);
30326fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
30426fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
30526fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
30626fb387bSchris
307a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
308ee9498f5SChristopher Smith            if ($cmd == 'add') {
309c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
310ee9498f5SChristopher Smith            }
311a6858c6aSchris            if ($user) {
312a6858c6aSchris                $notes[] = $this->lang['note_notify'];
313a6858c6aSchris            }
314a6858c6aSchris
315a6858c6aSchris            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);
316a6858c6aSchris        }
317a6858c6aSchris
3180440ff15Schris        ptln("    </tbody>",$indent);
3190440ff15Schris        ptln("    <tbody>",$indent);
3200440ff15Schris        ptln("      <tr>",$indent);
3210440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3220440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3230440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3240440ff15Schris
3250440ff15Schris        // save current $user, we need this to access details if the name is changed
3260440ff15Schris        if ($user)
3270440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3280440ff15Schris
3290440ff15Schris        $this->_htmlFilterSettings($indent+10);
3300440ff15Schris
331a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3320440ff15Schris        ptln("        </td>",$indent);
3330440ff15Schris        ptln("      </tr>",$indent);
3340440ff15Schris        ptln("    </tbody>",$indent);
3350440ff15Schris        ptln("  </table>",$indent);
33645c19902SChristopher Smith
33745c19902SChristopher Smith        if ($notes) {
33845c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
33945c19902SChristopher Smith            foreach ($notes as $note) {
34045c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
34145c19902SChristopher Smith            }
34245c19902SChristopher Smith            ptln("    </ul>");
34345c19902SChristopher Smith        }
344c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3450440ff15Schris        ptln("</form>",$indent);
3460440ff15Schris    }
3470440ff15Schris
348c5a7c0c6SGerrit Uitslag    /**
349c5a7c0c6SGerrit Uitslag     * Prints a inputfield
350c5a7c0c6SGerrit Uitslag     *
351c5a7c0c6SGerrit Uitslag     * @param string $id
352c5a7c0c6SGerrit Uitslag     * @param string $name
353c5a7c0c6SGerrit Uitslag     * @param string $label
354c5a7c0c6SGerrit Uitslag     * @param string $value
355c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
356c5a7c0c6SGerrit Uitslag     * @param int $indent
357c5a7c0c6SGerrit Uitslag     */
3583712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3597de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3607de12fceSAndreas Gohr        echo str_pad('',$indent);
3617de12fceSAndreas Gohr
362*359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
363d796a891SAndreas Gohr            $fieldtype = 'password';
364d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3657b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3667b3674bdSChristopher Smith            $fieldtype = 'email';
3677b3674bdSChristopher Smith            $autocomp  = '';
368d796a891SAndreas Gohr        }else{
369d796a891SAndreas Gohr            $fieldtype = 'text';
370d796a891SAndreas Gohr            $autocomp  = '';
371d796a891SAndreas Gohr        }
372d796a891SAndreas Gohr
3737de12fceSAndreas Gohr        echo "<tr $class>";
3747de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3757de12fceSAndreas Gohr        echo "<td>";
3767de12fceSAndreas Gohr        if($cando){
377d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3787de12fceSAndreas Gohr        }else{
3797de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
380ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3817de12fceSAndreas Gohr        }
3827de12fceSAndreas Gohr        echo "</td>";
3837de12fceSAndreas Gohr        echo "</tr>";
38426fb387bSchris    }
38526fb387bSchris
386c5a7c0c6SGerrit Uitslag    /**
387c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
388c5a7c0c6SGerrit Uitslag     *
389c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
390c5a7c0c6SGerrit Uitslag     * @return string html escaped value
391c5a7c0c6SGerrit Uitslag     */
3923712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
3930440ff15Schris        if (empty($this->_filter)) return '';
3940440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
3950440ff15Schris    }
3960440ff15Schris
397c5a7c0c6SGerrit Uitslag    /**
398c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
399c5a7c0c6SGerrit Uitslag     *
400c5a7c0c6SGerrit Uitslag     * @param int $indent
401c5a7c0c6SGerrit Uitslag     */
4023712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4030440ff15Schris
4040440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4050440ff15Schris
4060440ff15Schris        foreach ($this->_filter as $key => $filter) {
4070440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4080440ff15Schris        }
4090440ff15Schris    }
4100440ff15Schris
411c5a7c0c6SGerrit Uitslag    /**
412c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
413c5a7c0c6SGerrit Uitslag     *
414c5a7c0c6SGerrit Uitslag     * @param int $indent
415c5a7c0c6SGerrit Uitslag     */
4163712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
417ae1afd2fSChristopher Smith        global $ID;
418ae1afd2fSChristopher Smith
419ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
420ae1afd2fSChristopher Smith
421ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
422ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
423ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
424ae1afd2fSChristopher Smith        formSecurityToken();
425b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
426ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
427ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
428ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
429ae1afd2fSChristopher Smith
430ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
431ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
432ae1afd2fSChristopher Smith        ptln('</div>');
433ae1afd2fSChristopher Smith
434ae1afd2fSChristopher Smith        // list failures from the previous import
435ae1afd2fSChristopher Smith        if ($this->_import_failures) {
436ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
437ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
438b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
439ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
440ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
441ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
442ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
443ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
444ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
445ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
446ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
447ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
448ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
449ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
450ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
451ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
452ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
453ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
454ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
455ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
456ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
457ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
458ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
459ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
460ae1afd2fSChristopher Smith            }
461ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
462ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
463b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
464ae1afd2fSChristopher Smith            ptln('</div>');
465ae1afd2fSChristopher Smith        }
466ae1afd2fSChristopher Smith
467ae1afd2fSChristopher Smith    }
468ae1afd2fSChristopher Smith
469c5a7c0c6SGerrit Uitslag    /**
470c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
471c5a7c0c6SGerrit Uitslag     *
472c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
473c5a7c0c6SGerrit Uitslag     */
4743712ca9aSGerrit Uitslag    protected function _addUser(){
47500d58927SMichael Hamann        global $INPUT;
476634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
47782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4780440ff15Schris
479*359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
4800440ff15Schris        if (empty($user)) return false;
4816733c4d7SChris Smith
4826733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
483c3f4fb63SGina Haeussge            if (empty($pass)){
48400d58927SMichael Hamann                if($INPUT->has('usernotify')){
4858a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
486c3f4fb63SGina Haeussge                } else {
48760b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
48860b9901bSAndreas Gohr                    return false;
48960b9901bSAndreas Gohr                }
490*359e9417SChristopher Smith            } else {
491*359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
492*359e9417SChristopher Smith                    return false;
493*359e9417SChristopher Smith                }
4946733c4d7SChris Smith            }
4956733c4d7SChris Smith        } else {
4966733c4d7SChris Smith            if (!empty($pass)){
4976733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4986733c4d7SChris Smith                return false;
4996733c4d7SChris Smith            }
5006733c4d7SChris Smith        }
5016733c4d7SChris Smith
5026733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5036733c4d7SChris Smith            if (empty($name)){
5046733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5056733c4d7SChris Smith                return false;
5066733c4d7SChris Smith            }
5076733c4d7SChris Smith        } else {
5086733c4d7SChris Smith            if (!empty($name)){
5096733c4d7SChris Smith                return false;
5106733c4d7SChris Smith            }
5116733c4d7SChris Smith        }
5126733c4d7SChris Smith
5136733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5146733c4d7SChris Smith            if (empty($mail)){
5156733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5166733c4d7SChris Smith                return false;
5176733c4d7SChris Smith            }
5186733c4d7SChris Smith        } else {
5196733c4d7SChris Smith            if (!empty($mail)){
5206733c4d7SChris Smith                return false;
5216733c4d7SChris Smith            }
5226733c4d7SChris Smith        }
5230440ff15Schris
5247d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
525a6858c6aSchris
526a6858c6aSchris            msg($this->lang['add_ok'], 1);
527a6858c6aSchris
52800d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
529a6858c6aSchris                $this->_notifyUser($user,$pass);
530a6858c6aSchris            }
531a6858c6aSchris        } else {
53260b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
533a6858c6aSchris        }
534a6858c6aSchris
535a6858c6aSchris        return $ok;
5360440ff15Schris    }
5370440ff15Schris
5380440ff15Schris    /**
539c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
540c5a7c0c6SGerrit Uitslag     *
541c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5420440ff15Schris     */
5433712ca9aSGerrit Uitslag    protected function _deleteUser(){
54400d58927SMichael Hamann        global $conf, $INPUT;
5459ec82636SAndreas Gohr
546634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
54782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5480440ff15Schris
54900d58927SMichael Hamann        $selected = $INPUT->arr('delete');
55000d58927SMichael Hamann        if (empty($selected)) return false;
5510440ff15Schris        $selected = array_keys($selected);
5520440ff15Schris
553c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
554c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
555c9a8f912SMichael Klier            return false;
556c9a8f912SMichael Klier        }
557c9a8f912SMichael Klier
5587d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5590440ff15Schris        if ($count == count($selected)) {
5600440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5610440ff15Schris            msg("$text.", 1);
5620440ff15Schris        } else {
5630440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5640440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5650440ff15Schris            msg("$part1, $part2",-1);
5660440ff15Schris        }
56778c7c8c9Schris
5689ec82636SAndreas Gohr        // invalidate all sessions
5699ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5709ec82636SAndreas Gohr
57178c7c8c9Schris        return true;
57278c7c8c9Schris    }
57378c7c8c9Schris
57478c7c8c9Schris    /**
57578c7c8c9Schris     * Edit user (a user has been selected for editing)
576c5a7c0c6SGerrit Uitslag     *
577c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
578c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
57978c7c8c9Schris     */
5803712ca9aSGerrit Uitslag    protected function _editUser($param) {
581634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
58278c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
583786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
58478c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
58578c7c8c9Schris
58678c7c8c9Schris        // no user found?
58778c7c8c9Schris        if (!$userdata) {
58878c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
58978c7c8c9Schris            return false;
59078c7c8c9Schris        }
59178c7c8c9Schris
59278c7c8c9Schris        $this->_edit_user = $user;
59378c7c8c9Schris        $this->_edit_userdata = $userdata;
59478c7c8c9Schris
59578c7c8c9Schris        return true;
5960440ff15Schris    }
5970440ff15Schris
5980440ff15Schris    /**
599c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
600c5a7c0c6SGerrit Uitslag     *
601c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6020440ff15Schris     */
6033712ca9aSGerrit Uitslag    protected function _modifyUser(){
60400d58927SMichael Hamann        global $conf, $INPUT;
6059ec82636SAndreas Gohr
606634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
60782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6080440ff15Schris
60926fb387bSchris        // get currently valid  user data
610786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
611073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
612073766c6Smatthiasgrimm
61326fb387bSchris        // get new user data subject to change
614*359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
615073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6160440ff15Schris
6170440ff15Schris        $changes = array();
618073766c6Smatthiasgrimm        if ($newuser != $olduser) {
61926fb387bSchris
62026fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
62126fb387bSchris                msg($this->lang['update_fail'],-1);
62226fb387bSchris                return false;
62326fb387bSchris            }
62426fb387bSchris
62526fb387bSchris            // check if $newuser already exists
626073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
627073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
628a6858c6aSchris                $re_edit = true;
6290440ff15Schris            } else {
630073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6310440ff15Schris            }
63293eefc2fSAndreas Gohr        }
633*359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
634*359e9417SChristopher Smith            if ($newpass || $confirm) {
635*359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
636*359e9417SChristopher Smith                    $changes['pass'] = $newpass;
637*359e9417SChristopher Smith                } else {
638*359e9417SChristopher Smith                    return false;
639*359e9417SChristopher Smith                }
640*359e9417SChristopher Smith            } else {
641*359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
642*359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
643*359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
644*359e9417SChristopher Smith                }
645*359e9417SChristopher Smith            }
6460440ff15Schris        }
6470440ff15Schris
64840d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
649073766c6Smatthiasgrimm            $changes['name'] = $newname;
65040d72af6SChristopher Smith        }
65140d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
652073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
65340d72af6SChristopher Smith        }
65440d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
655073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
65640d72af6SChristopher Smith        }
6570440ff15Schris
6587d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6590440ff15Schris            msg($this->lang['update_ok'],1);
660a6858c6aSchris
6616ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
662a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
6636ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
664a6858c6aSchris            }
665a6858c6aSchris
6669ec82636SAndreas Gohr            // invalidate all sessions
6679ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6689ec82636SAndreas Gohr
6690440ff15Schris        } else {
6700440ff15Schris            msg($this->lang['update_fail'],-1);
6710440ff15Schris        }
67278c7c8c9Schris
673a6858c6aSchris        if (!empty($re_edit)) {
674a6858c6aSchris            $this->_editUser($olduser);
6750440ff15Schris        }
6760440ff15Schris
677a6858c6aSchris        return $ok;
678a6858c6aSchris    }
679a6858c6aSchris
680a6858c6aSchris    /**
681c5a7c0c6SGerrit Uitslag     * Send password change notification email
682c5a7c0c6SGerrit Uitslag     *
683c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
684c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
685c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
686c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
687a6858c6aSchris     */
6883712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
689a6858c6aSchris
690a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
691328143f8SChristopher Smith            if ($status_alert) {
692a6858c6aSchris                msg($this->lang['notify_ok'], 1);
693328143f8SChristopher Smith            }
694a6858c6aSchris        } else {
695328143f8SChristopher Smith            if ($status_alert) {
696a6858c6aSchris                msg($this->lang['notify_fail'], -1);
697a6858c6aSchris            }
698328143f8SChristopher Smith        }
699a6858c6aSchris
700a6858c6aSchris        return $sent;
701a6858c6aSchris    }
702a6858c6aSchris
703a6858c6aSchris    /**
704*359e9417SChristopher Smith     * Verify password meets minimum requirements
705*359e9417SChristopher Smith     * :TODO: extend to support password strength
706*359e9417SChristopher Smith     *
707*359e9417SChristopher Smith     * @param string  $password   candidate string for new password
708*359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
709*359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
710*359e9417SChristopher Smith     */
711*359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
712*359e9417SChristopher Smith
713*359e9417SChristopher Smith        if (empty($password)) {
714*359e9417SChristopher Smith            return false;
715*359e9417SChristopher Smith        }
716*359e9417SChristopher Smith
717*359e9417SChristopher Smith        if ($password !== $confirm) {
718*359e9417SChristopher Smith            msg($this->lang['pass_confirm_fail'], -1);
719*359e9417SChristopher Smith            return false;
720*359e9417SChristopher Smith        }
721*359e9417SChristopher Smith
722*359e9417SChristopher Smith        // :TODO: test password for required strength
723*359e9417SChristopher Smith
724*359e9417SChristopher Smith        // if we make it this far the password is good
725*359e9417SChristopher Smith        return true;
726*359e9417SChristopher Smith    }
727*359e9417SChristopher Smith
728*359e9417SChristopher Smith    /**
729c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
730a6858c6aSchris     *
731c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
732a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7330440ff15Schris     */
7343712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
735c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7367441e340SAndreas Gohr        global $auth;
737fbfbbe8aSHakan Sandell        global $INPUT;
7380440ff15Schris
739fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
740fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
741fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
742fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
743fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
744*359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7450440ff15Schris
7467441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7477441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7487441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7497441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7507441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7510440ff15Schris
7520440ff15Schris        return $user;
7530440ff15Schris    }
7540440ff15Schris
755c5a7c0c6SGerrit Uitslag    /**
756c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
757c5a7c0c6SGerrit Uitslag     *
758c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
759c5a7c0c6SGerrit Uitslag     */
7603712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7610440ff15Schris
7620440ff15Schris        $this->_filter = array();
7630440ff15Schris
7640440ff15Schris        if ($op == 'new') {
7650440ff15Schris            list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
7660440ff15Schris
7670440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7680440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7690440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7700440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7710440ff15Schris        }
7720440ff15Schris    }
7730440ff15Schris
774c5a7c0c6SGerrit Uitslag    /**
775c5a7c0c6SGerrit Uitslag     * Get the current search terms
776c5a7c0c6SGerrit Uitslag     *
777c5a7c0c6SGerrit Uitslag     * @return array
778c5a7c0c6SGerrit Uitslag     */
7793712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
780fbfbbe8aSHakan Sandell        global $INPUT;
7810440ff15Schris
782fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
7830440ff15Schris
7840440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
7850440ff15Schris        $filter = array();
7860440ff15Schris
7870440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
7880440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
7890440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
7900440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
7910440ff15Schris
7920440ff15Schris        return $filter;
7930440ff15Schris    }
7940440ff15Schris
795c5a7c0c6SGerrit Uitslag    /**
796c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
797c5a7c0c6SGerrit Uitslag     */
7983712ca9aSGerrit Uitslag    protected function _validatePagination() {
7990440ff15Schris
8000440ff15Schris        if ($this->_start >= $this->_user_total) {
8010440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8020440ff15Schris        }
8030440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8040440ff15Schris
8050440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8060440ff15Schris    }
8070440ff15Schris
808c5a7c0c6SGerrit Uitslag    /**
809c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
810c5a7c0c6SGerrit Uitslag     *
811c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8120440ff15Schris     */
8133712ca9aSGerrit Uitslag    protected function _pagination() {
8140440ff15Schris
81551d94d49Schris        $disabled = 'disabled="disabled"';
81651d94d49Schris
81751d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
81851d94d49Schris
81951d94d49Schris        if ($this->_user_total == -1) {
82051d94d49Schris            $buttons['last'] = $disabled;
82151d94d49Schris            $buttons['next'] = '';
82251d94d49Schris        } else {
82351d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
82451d94d49Schris        }
8250440ff15Schris
8260440ff15Schris        return $buttons;
8270440ff15Schris    }
8285c967d3dSChristopher Smith
829c5a7c0c6SGerrit Uitslag    /**
830c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8315c967d3dSChristopher Smith     */
8323712ca9aSGerrit Uitslag    protected function _export() {
8335c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8345c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8355c967d3dSChristopher Smith        $column_headings = array(
8365c967d3dSChristopher Smith            $this->lang["user_id"],
8375c967d3dSChristopher Smith            $this->lang["user_name"],
8385c967d3dSChristopher Smith            $this->lang["user_mail"],
8395c967d3dSChristopher Smith            $this->lang["user_groups"]
8405c967d3dSChristopher Smith        );
8415c967d3dSChristopher Smith
8425c967d3dSChristopher Smith        // ==============================================================================================
8435c967d3dSChristopher Smith        // GENERATE OUTPUT
8445c967d3dSChristopher Smith        // normal headers for downloading...
8455c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8465c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8475c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8485c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8495c967d3dSChristopher Smith
8505c967d3dSChristopher Smith        // output the csv
8515c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8525c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8535c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8545c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8555c967d3dSChristopher Smith            fputcsv($fd, $line);
8565c967d3dSChristopher Smith        }
8575c967d3dSChristopher Smith        fclose($fd);
858b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
859b2c01466SChristopher Smith
8605c967d3dSChristopher Smith        die;
8615c967d3dSChristopher Smith    }
862ae1afd2fSChristopher Smith
863c5a7c0c6SGerrit Uitslag    /**
864c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
865ae1afd2fSChristopher Smith     *
866ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
867c5a7c0c6SGerrit Uitslag     *
8685ba64050SChristopher Smith     * @return bool whether successful
869ae1afd2fSChristopher Smith     */
8703712ca9aSGerrit Uitslag    protected function _import() {
871ae1afd2fSChristopher Smith        // check we are allowed to add users
872ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
873ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
874ae1afd2fSChristopher Smith
875ae1afd2fSChristopher Smith        // check file uploaded ok.
876b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
877ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
878ae1afd2fSChristopher Smith            return false;
879ae1afd2fSChristopher Smith        }
880ae1afd2fSChristopher Smith        // retrieve users from the file
881ae1afd2fSChristopher Smith        $this->_import_failures = array();
882ae1afd2fSChristopher Smith        $import_success_count = 0;
883ae1afd2fSChristopher Smith        $import_fail_count = 0;
884ae1afd2fSChristopher Smith        $line = 0;
885ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
886ae1afd2fSChristopher Smith        if ($fd) {
887ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
888efcec72bSChristopher Smith                if (!utf8_check($csv)) {
889efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
890efcec72bSChristopher Smith                }
891c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
892ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
893ae1afd2fSChristopher Smith                // data checks...
894ae1afd2fSChristopher Smith                if (1 == ++$line) {
895ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
896ae1afd2fSChristopher Smith                }
897ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
898ae1afd2fSChristopher Smith                    $import_fail_count++;
899ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
900ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
901ae1afd2fSChristopher Smith                    continue;
902ae1afd2fSChristopher Smith                }
903ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
904ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
905ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
906328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
907328143f8SChristopher Smith                    if (!$sent){
908328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
909328143f8SChristopher Smith                    }
910ae1afd2fSChristopher Smith                    $import_success_count++;
911ae1afd2fSChristopher Smith                } else {
912ae1afd2fSChristopher Smith                    $import_fail_count++;
913e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
914ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
915ae1afd2fSChristopher Smith                }
916ae1afd2fSChristopher Smith            }
917ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
918ae1afd2fSChristopher Smith            if ($import_fail_count) {
919ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
920ae1afd2fSChristopher Smith            }
921ae1afd2fSChristopher Smith        } else {
922ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
923ae1afd2fSChristopher Smith        }
924ae1afd2fSChristopher Smith
925ae1afd2fSChristopher Smith        // save import failures into the session
926ae1afd2fSChristopher Smith        if (!headers_sent()) {
927ae1afd2fSChristopher Smith            session_start();
928ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
929ae1afd2fSChristopher Smith            session_write_close();
930ae1afd2fSChristopher Smith        }
931c5a7c0c6SGerrit Uitslag        return true;
932ae1afd2fSChristopher Smith    }
933ae1afd2fSChristopher Smith
934c5a7c0c6SGerrit Uitslag    /**
935786dfb0eSGerrit Uitslag     * Returns cleaned user data
936c5a7c0c6SGerrit Uitslag     *
937c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
938c5a7c0c6SGerrit Uitslag     * @param $error
939c5a7c0c6SGerrit Uitslag     * @return array|bool cleaned data or false
940c5a7c0c6SGerrit Uitslag     */
9413712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
942ae1afd2fSChristopher Smith        global $INPUT;
943ae1afd2fSChristopher Smith
944ae1afd2fSChristopher Smith        // kludgy ....
945ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
946ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
947ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
948ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
949ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
950ae1afd2fSChristopher Smith
951ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
952ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
953ae1afd2fSChristopher Smith        if (empty($user)) {
954ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
955ae1afd2fSChristopher Smith            return false;
956ae1afd2fSChristopher Smith        }
957ae1afd2fSChristopher Smith
958ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
959ae1afd2fSChristopher Smith
960ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
961ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
962ae1afd2fSChristopher Smith            return false;
963ae1afd2fSChristopher Smith        }
964ae1afd2fSChristopher Smith
965328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
966328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
967ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
968ae1afd2fSChristopher Smith                return false;
969ae1afd2fSChristopher Smith            }
970328143f8SChristopher Smith        } else {
971328143f8SChristopher Smith            if (!empty($mail)) {
972328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
973328143f8SChristopher Smith                return false;
974328143f8SChristopher Smith            }
975328143f8SChristopher Smith        }
976ae1afd2fSChristopher Smith
977ae1afd2fSChristopher Smith        return $cleaned;
978ae1afd2fSChristopher Smith    }
979ae1afd2fSChristopher Smith
980c5a7c0c6SGerrit Uitslag    /**
981c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
982c5a7c0c6SGerrit Uitslag     *
983c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
984c5a7c0c6SGerrit Uitslag     *
985c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
986c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
9875ba64050SChristopher Smith     * @return bool whether successful
988c5a7c0c6SGerrit Uitslag     */
9893712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
990ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
991ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
992ae1afd2fSChristopher Smith            return false;
993ae1afd2fSChristopher Smith        }
994ae1afd2fSChristopher Smith
995ae1afd2fSChristopher Smith        return true;
996ae1afd2fSChristopher Smith    }
997ae1afd2fSChristopher Smith
998c5a7c0c6SGerrit Uitslag    /**
999c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1000c5a7c0c6SGerrit Uitslag     */
10013712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1002ae1afd2fSChristopher Smith
1003ae1afd2fSChristopher Smith        // ==============================================================================================
1004ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1005ae1afd2fSChristopher Smith        // normal headers for downloading...
1006ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1007ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1008ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1009ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1010ae1afd2fSChristopher Smith
1011ae1afd2fSChristopher Smith        // output the csv
1012ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1013c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1014ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1015ae1afd2fSChristopher Smith        }
1016ae1afd2fSChristopher Smith        fclose($fd);
1017ae1afd2fSChristopher Smith        die;
1018ae1afd2fSChristopher Smith    }
1019ae1afd2fSChristopher Smith
1020b2c01466SChristopher Smith    /**
1021b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1022b2c01466SChristopher Smith     */
1023b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1024b2c01466SChristopher Smith        return is_uploaded_file($file);
1025b2c01466SChristopher Smith    }
1026b2c01466SChristopher Smith
1027c9454ee3SChristopher Smith    /**
1028c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1029c9454ee3SChristopher Smith     *
1030c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1031c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1032c9454ee3SChristopher Smith     */
1033c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1034c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1035c9454ee3SChristopher Smith    }
1036c9454ee3SChristopher Smith
1037c9454ee3SChristopher Smith    /**
1038c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1039c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1040c9454ee3SChristopher Smith     *
1041c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1042c9454ee3SChristopher Smith     */
1043c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1044c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1045c9454ee3SChristopher Smith        fputs($fp, $str);
1046c9454ee3SChristopher Smith        rewind($fp);
1047c9454ee3SChristopher Smith
1048c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1049c9454ee3SChristopher Smith
1050c9454ee3SChristopher Smith        fclose($fp);
1051c9454ee3SChristopher Smith        return $data;
1052c9454ee3SChristopher Smith    }
10530440ff15Schris}
1054