xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision 2400ddcb0585bd4bba5d4abeb1be8e2f4ebc56d6)
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;
280be9008d3SChristopher Smith        global $lang;
28178c7c8c9Schris
28278c7c8c9Schris        $name = $mail = $groups = '';
283a6858c6aSchris        $notes = array();
2840440ff15Schris
2850440ff15Schris        if ($user) {
28678c7c8c9Schris            extract($userdata);
28778c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
288a6858c6aSchris        } else {
289a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2900440ff15Schris        }
2910440ff15Schris
2920440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
293634d7150SAndreas Gohr        formSecurityToken();
294c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
2950440ff15Schris        ptln("  <table class=\"inline\">",$indent);
2960440ff15Schris        ptln("    <thead>",$indent);
2970440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
2980440ff15Schris        ptln("    </thead>",$indent);
2990440ff15Schris        ptln("    <tbody>",$indent);
30026fb387bSchris
30126fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
30226fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
303be9008d3SChristopher Smith        $this->_htmlInputField($cmd."_userpass2", "userpass2", $lang["passchk"],          "",     $this->_auth->canDo("modPass"),  $indent+6);
30426fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
30526fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
30626fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
30726fb387bSchris
308a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
309ee9498f5SChristopher Smith            if ($cmd == 'add') {
310c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
311ee9498f5SChristopher Smith            }
312a6858c6aSchris            if ($user) {
313a6858c6aSchris                $notes[] = $this->lang['note_notify'];
314a6858c6aSchris            }
315a6858c6aSchris
316a6858c6aSchris            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);
317a6858c6aSchris        }
318a6858c6aSchris
3190440ff15Schris        ptln("    </tbody>",$indent);
3200440ff15Schris        ptln("    <tbody>",$indent);
3210440ff15Schris        ptln("      <tr>",$indent);
3220440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3230440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3240440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3250440ff15Schris
3260440ff15Schris        // save current $user, we need this to access details if the name is changed
3270440ff15Schris        if ($user)
3280440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3290440ff15Schris
3300440ff15Schris        $this->_htmlFilterSettings($indent+10);
3310440ff15Schris
332a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3330440ff15Schris        ptln("        </td>",$indent);
3340440ff15Schris        ptln("      </tr>",$indent);
3350440ff15Schris        ptln("    </tbody>",$indent);
3360440ff15Schris        ptln("  </table>",$indent);
33745c19902SChristopher Smith
33845c19902SChristopher Smith        if ($notes) {
33945c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
34045c19902SChristopher Smith            foreach ($notes as $note) {
34145c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
34245c19902SChristopher Smith            }
34345c19902SChristopher Smith            ptln("    </ul>");
34445c19902SChristopher Smith        }
345c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3460440ff15Schris        ptln("</form>",$indent);
3470440ff15Schris    }
3480440ff15Schris
349c5a7c0c6SGerrit Uitslag    /**
350c5a7c0c6SGerrit Uitslag     * Prints a inputfield
351c5a7c0c6SGerrit Uitslag     *
352c5a7c0c6SGerrit Uitslag     * @param string $id
353c5a7c0c6SGerrit Uitslag     * @param string $name
354c5a7c0c6SGerrit Uitslag     * @param string $label
355c5a7c0c6SGerrit Uitslag     * @param string $value
356c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
357c5a7c0c6SGerrit Uitslag     * @param int $indent
358c5a7c0c6SGerrit Uitslag     */
3593712ca9aSGerrit Uitslag    protected function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3607de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3617de12fceSAndreas Gohr        echo str_pad('',$indent);
3627de12fceSAndreas Gohr
363359e9417SChristopher Smith        if($name == 'userpass' || $name == 'userpass2'){
364d796a891SAndreas Gohr            $fieldtype = 'password';
365d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3667b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3677b3674bdSChristopher Smith            $fieldtype = 'email';
3687b3674bdSChristopher Smith            $autocomp  = '';
369d796a891SAndreas Gohr        }else{
370d796a891SAndreas Gohr            $fieldtype = 'text';
371d796a891SAndreas Gohr            $autocomp  = '';
372d796a891SAndreas Gohr        }
373d796a891SAndreas Gohr
3747de12fceSAndreas Gohr        echo "<tr $class>";
3757de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3767de12fceSAndreas Gohr        echo "<td>";
3777de12fceSAndreas Gohr        if($cando){
378d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3797de12fceSAndreas Gohr        }else{
3807de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
381ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3827de12fceSAndreas Gohr        }
3837de12fceSAndreas Gohr        echo "</td>";
3847de12fceSAndreas Gohr        echo "</tr>";
38526fb387bSchris    }
38626fb387bSchris
387c5a7c0c6SGerrit Uitslag    /**
388c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
389c5a7c0c6SGerrit Uitslag     *
390c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
391c5a7c0c6SGerrit Uitslag     * @return string html escaped value
392c5a7c0c6SGerrit Uitslag     */
3933712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
3940440ff15Schris        if (empty($this->_filter)) return '';
3950440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
3960440ff15Schris    }
3970440ff15Schris
398c5a7c0c6SGerrit Uitslag    /**
399c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
400c5a7c0c6SGerrit Uitslag     *
401c5a7c0c6SGerrit Uitslag     * @param int $indent
402c5a7c0c6SGerrit Uitslag     */
4033712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4040440ff15Schris
4050440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4060440ff15Schris
4070440ff15Schris        foreach ($this->_filter as $key => $filter) {
4080440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4090440ff15Schris        }
4100440ff15Schris    }
4110440ff15Schris
412c5a7c0c6SGerrit Uitslag    /**
413c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
414c5a7c0c6SGerrit Uitslag     *
415c5a7c0c6SGerrit Uitslag     * @param int $indent
416c5a7c0c6SGerrit Uitslag     */
4173712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
418ae1afd2fSChristopher Smith        global $ID;
419ae1afd2fSChristopher Smith
420ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
421ae1afd2fSChristopher Smith
422ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
423ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
424ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
425ae1afd2fSChristopher Smith        formSecurityToken();
426b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
427ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
428ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
429ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
430ae1afd2fSChristopher Smith
431ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
432ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
433ae1afd2fSChristopher Smith        ptln('</div>');
434ae1afd2fSChristopher Smith
435ae1afd2fSChristopher Smith        // list failures from the previous import
436ae1afd2fSChristopher Smith        if ($this->_import_failures) {
437ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
438ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
439b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
440ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
441ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
442ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
443ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
444ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
445ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
446ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
447ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
448ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
449ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
450ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
451ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
452ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
453ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
454ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
455ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
456ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
457ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
458ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
459ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
460ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
461ae1afd2fSChristopher Smith            }
462ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
463ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
464b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
465ae1afd2fSChristopher Smith            ptln('</div>');
466ae1afd2fSChristopher Smith        }
467ae1afd2fSChristopher Smith
468ae1afd2fSChristopher Smith    }
469ae1afd2fSChristopher Smith
470c5a7c0c6SGerrit Uitslag    /**
471c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
472c5a7c0c6SGerrit Uitslag     *
473c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
474c5a7c0c6SGerrit Uitslag     */
4753712ca9aSGerrit Uitslag    protected function _addUser(){
47600d58927SMichael Hamann        global $INPUT;
477634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
47882fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4790440ff15Schris
480359e9417SChristopher Smith        list($user,$pass,$name,$mail,$grps,$passconfirm) = $this->_retrieveUser();
4810440ff15Schris        if (empty($user)) return false;
4826733c4d7SChris Smith
4836733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
484c3f4fb63SGina Haeussge            if (empty($pass)){
48500d58927SMichael Hamann                if($INPUT->has('usernotify')){
4868a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
487c3f4fb63SGina Haeussge                } else {
48860b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
48960b9901bSAndreas Gohr                    return false;
49060b9901bSAndreas Gohr                }
491359e9417SChristopher Smith            } else {
492359e9417SChristopher Smith                if (!$this->_verifyPassword($pass,$passconfirm)) {
493359e9417SChristopher Smith                    return false;
494359e9417SChristopher Smith                }
4956733c4d7SChris Smith            }
4966733c4d7SChris Smith        } else {
4976733c4d7SChris Smith            if (!empty($pass)){
4986733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4996733c4d7SChris Smith                return false;
5006733c4d7SChris Smith            }
5016733c4d7SChris Smith        }
5026733c4d7SChris Smith
5036733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
5046733c4d7SChris Smith            if (empty($name)){
5056733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5066733c4d7SChris Smith                return false;
5076733c4d7SChris Smith            }
5086733c4d7SChris Smith        } else {
5096733c4d7SChris Smith            if (!empty($name)){
5106733c4d7SChris Smith                return false;
5116733c4d7SChris Smith            }
5126733c4d7SChris Smith        }
5136733c4d7SChris Smith
5146733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5156733c4d7SChris Smith            if (empty($mail)){
5166733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5176733c4d7SChris Smith                return false;
5186733c4d7SChris Smith            }
5196733c4d7SChris Smith        } else {
5206733c4d7SChris Smith            if (!empty($mail)){
5216733c4d7SChris Smith                return false;
5226733c4d7SChris Smith            }
5236733c4d7SChris Smith        }
5240440ff15Schris
5257d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
526a6858c6aSchris
527a6858c6aSchris            msg($this->lang['add_ok'], 1);
528a6858c6aSchris
52900d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
530a6858c6aSchris                $this->_notifyUser($user,$pass);
531a6858c6aSchris            }
532a6858c6aSchris        } else {
53360b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
534a6858c6aSchris        }
535a6858c6aSchris
536a6858c6aSchris        return $ok;
5370440ff15Schris    }
5380440ff15Schris
5390440ff15Schris    /**
540c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
541c5a7c0c6SGerrit Uitslag     *
542c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5430440ff15Schris     */
5443712ca9aSGerrit Uitslag    protected function _deleteUser(){
54500d58927SMichael Hamann        global $conf, $INPUT;
5469ec82636SAndreas Gohr
547634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
54882fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5490440ff15Schris
55000d58927SMichael Hamann        $selected = $INPUT->arr('delete');
55100d58927SMichael Hamann        if (empty($selected)) return false;
5520440ff15Schris        $selected = array_keys($selected);
5530440ff15Schris
554c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
555c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
556c9a8f912SMichael Klier            return false;
557c9a8f912SMichael Klier        }
558c9a8f912SMichael Klier
5597d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5600440ff15Schris        if ($count == count($selected)) {
5610440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5620440ff15Schris            msg("$text.", 1);
5630440ff15Schris        } else {
5640440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5650440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5660440ff15Schris            msg("$part1, $part2",-1);
5670440ff15Schris        }
56878c7c8c9Schris
5699ec82636SAndreas Gohr        // invalidate all sessions
5709ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5719ec82636SAndreas Gohr
57278c7c8c9Schris        return true;
57378c7c8c9Schris    }
57478c7c8c9Schris
57578c7c8c9Schris    /**
57678c7c8c9Schris     * Edit user (a user has been selected for editing)
577c5a7c0c6SGerrit Uitslag     *
578c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
579c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
58078c7c8c9Schris     */
5813712ca9aSGerrit Uitslag    protected function _editUser($param) {
582634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
58378c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
584786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
58578c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
58678c7c8c9Schris
58778c7c8c9Schris        // no user found?
58878c7c8c9Schris        if (!$userdata) {
58978c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
59078c7c8c9Schris            return false;
59178c7c8c9Schris        }
59278c7c8c9Schris
59378c7c8c9Schris        $this->_edit_user = $user;
59478c7c8c9Schris        $this->_edit_userdata = $userdata;
59578c7c8c9Schris
59678c7c8c9Schris        return true;
5970440ff15Schris    }
5980440ff15Schris
5990440ff15Schris    /**
600c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
601c5a7c0c6SGerrit Uitslag     *
602c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
6030440ff15Schris     */
6043712ca9aSGerrit Uitslag    protected function _modifyUser(){
60500d58927SMichael Hamann        global $conf, $INPUT;
6069ec82636SAndreas Gohr
607634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
60882fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6090440ff15Schris
61026fb387bSchris        // get currently valid  user data
611786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
612073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
613073766c6Smatthiasgrimm
61426fb387bSchris        // get new user data subject to change
615359e9417SChristopher Smith        list($newuser,$newpass,$newname,$newmail,$newgrps,$passconfirm) = $this->_retrieveUser();
616073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6170440ff15Schris
6180440ff15Schris        $changes = array();
619073766c6Smatthiasgrimm        if ($newuser != $olduser) {
62026fb387bSchris
62126fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
62226fb387bSchris                msg($this->lang['update_fail'],-1);
62326fb387bSchris                return false;
62426fb387bSchris            }
62526fb387bSchris
62626fb387bSchris            // check if $newuser already exists
627073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
628073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
629a6858c6aSchris                $re_edit = true;
6300440ff15Schris            } else {
631073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6320440ff15Schris            }
63393eefc2fSAndreas Gohr        }
634359e9417SChristopher Smith        if ($this->_auth->canDo('modPass')) {
635*2400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
636359e9417SChristopher Smith                if ($this->_verifyPassword($newpass,$passconfirm)) {
637359e9417SChristopher Smith                    $changes['pass'] = $newpass;
638359e9417SChristopher Smith                } else {
639359e9417SChristopher Smith                    return false;
640359e9417SChristopher Smith                }
641359e9417SChristopher Smith            } else {
642359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
643359e9417SChristopher Smith                if ($INPUT->has('usernotify')) {
644359e9417SChristopher Smith                    $changes['pass'] = auth_pwgen($olduser);
645359e9417SChristopher Smith                }
646359e9417SChristopher Smith            }
6470440ff15Schris        }
6480440ff15Schris
64940d72af6SChristopher Smith        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name']) {
650073766c6Smatthiasgrimm            $changes['name'] = $newname;
65140d72af6SChristopher Smith        }
65240d72af6SChristopher Smith        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
653073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
65440d72af6SChristopher Smith        }
65540d72af6SChristopher Smith        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
656073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
65740d72af6SChristopher Smith        }
6580440ff15Schris
6597d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6600440ff15Schris            msg($this->lang['update_ok'],1);
661a6858c6aSchris
6626ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
663a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
6646ed3476bSChristopher Smith                $this->_notifyUser($notify,$changes['pass']);
665a6858c6aSchris            }
666a6858c6aSchris
6679ec82636SAndreas Gohr            // invalidate all sessions
6689ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6699ec82636SAndreas Gohr
6700440ff15Schris        } else {
6710440ff15Schris            msg($this->lang['update_fail'],-1);
6720440ff15Schris        }
67378c7c8c9Schris
674a6858c6aSchris        if (!empty($re_edit)) {
675a6858c6aSchris            $this->_editUser($olduser);
6760440ff15Schris        }
6770440ff15Schris
678a6858c6aSchris        return $ok;
679a6858c6aSchris    }
680a6858c6aSchris
681a6858c6aSchris    /**
682c5a7c0c6SGerrit Uitslag     * Send password change notification email
683c5a7c0c6SGerrit Uitslag     *
684c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
685c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
686c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
687c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
688a6858c6aSchris     */
6893712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
690a6858c6aSchris
691a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
692328143f8SChristopher Smith            if ($status_alert) {
693a6858c6aSchris                msg($this->lang['notify_ok'], 1);
694328143f8SChristopher Smith            }
695a6858c6aSchris        } else {
696328143f8SChristopher Smith            if ($status_alert) {
697a6858c6aSchris                msg($this->lang['notify_fail'], -1);
698a6858c6aSchris            }
699328143f8SChristopher Smith        }
700a6858c6aSchris
701a6858c6aSchris        return $sent;
702a6858c6aSchris    }
703a6858c6aSchris
704a6858c6aSchris    /**
705359e9417SChristopher Smith     * Verify password meets minimum requirements
706359e9417SChristopher Smith     * :TODO: extend to support password strength
707359e9417SChristopher Smith     *
708359e9417SChristopher Smith     * @param string  $password   candidate string for new password
709359e9417SChristopher Smith     * @param string  $confirm    repeated password for confirmation
710359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
711359e9417SChristopher Smith     */
712359e9417SChristopher Smith    protected function _verifyPassword($password, $confirm) {
713be9008d3SChristopher Smith        global $lang;
714359e9417SChristopher Smith
715*2400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
716359e9417SChristopher Smith            return false;
717359e9417SChristopher Smith        }
718359e9417SChristopher Smith
719359e9417SChristopher Smith        if ($password !== $confirm) {
720be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
721359e9417SChristopher Smith            return false;
722359e9417SChristopher Smith        }
723359e9417SChristopher Smith
724359e9417SChristopher Smith        // :TODO: test password for required strength
725359e9417SChristopher Smith
726359e9417SChristopher Smith        // if we make it this far the password is good
727359e9417SChristopher Smith        return true;
728359e9417SChristopher Smith    }
729359e9417SChristopher Smith
730359e9417SChristopher Smith    /**
731c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
732a6858c6aSchris     *
733c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
734a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
7350440ff15Schris     */
7363712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
737c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
7387441e340SAndreas Gohr        global $auth;
739fbfbbe8aSHakan Sandell        global $INPUT;
7400440ff15Schris
741fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
742fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
743fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
744fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
745fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
746359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2');                // repeated password for confirmation
7470440ff15Schris
7487441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7497441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7507441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7517441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7527441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7530440ff15Schris
7540440ff15Schris        return $user;
7550440ff15Schris    }
7560440ff15Schris
757c5a7c0c6SGerrit Uitslag    /**
758c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
759c5a7c0c6SGerrit Uitslag     *
760c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
761c5a7c0c6SGerrit Uitslag     */
7623712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7630440ff15Schris
7640440ff15Schris        $this->_filter = array();
7650440ff15Schris
7660440ff15Schris        if ($op == 'new') {
7670440ff15Schris            list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
7680440ff15Schris
7690440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7700440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7710440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7720440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7730440ff15Schris        }
7740440ff15Schris    }
7750440ff15Schris
776c5a7c0c6SGerrit Uitslag    /**
777c5a7c0c6SGerrit Uitslag     * Get the current search terms
778c5a7c0c6SGerrit Uitslag     *
779c5a7c0c6SGerrit Uitslag     * @return array
780c5a7c0c6SGerrit Uitslag     */
7813712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
782fbfbbe8aSHakan Sandell        global $INPUT;
7830440ff15Schris
784fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
7850440ff15Schris
7860440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
7870440ff15Schris        $filter = array();
7880440ff15Schris
7890440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
7900440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
7910440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
7920440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
7930440ff15Schris
7940440ff15Schris        return $filter;
7950440ff15Schris    }
7960440ff15Schris
797c5a7c0c6SGerrit Uitslag    /**
798c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
799c5a7c0c6SGerrit Uitslag     */
8003712ca9aSGerrit Uitslag    protected function _validatePagination() {
8010440ff15Schris
8020440ff15Schris        if ($this->_start >= $this->_user_total) {
8030440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
8040440ff15Schris        }
8050440ff15Schris        if ($this->_start < 0) $this->_start = 0;
8060440ff15Schris
8070440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
8080440ff15Schris    }
8090440ff15Schris
810c5a7c0c6SGerrit Uitslag    /**
811c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
812c5a7c0c6SGerrit Uitslag     *
813c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
8140440ff15Schris     */
8153712ca9aSGerrit Uitslag    protected function _pagination() {
8160440ff15Schris
81751d94d49Schris        $disabled = 'disabled="disabled"';
81851d94d49Schris
81951d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
82051d94d49Schris
82151d94d49Schris        if ($this->_user_total == -1) {
82251d94d49Schris            $buttons['last'] = $disabled;
82351d94d49Schris            $buttons['next'] = '';
82451d94d49Schris        } else {
82551d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
82651d94d49Schris        }
8270440ff15Schris
8280440ff15Schris        return $buttons;
8290440ff15Schris    }
8305c967d3dSChristopher Smith
831c5a7c0c6SGerrit Uitslag    /**
832c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
8335c967d3dSChristopher Smith     */
8343712ca9aSGerrit Uitslag    protected function _export() {
8355c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
8365c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
8375c967d3dSChristopher Smith        $column_headings = array(
8385c967d3dSChristopher Smith            $this->lang["user_id"],
8395c967d3dSChristopher Smith            $this->lang["user_name"],
8405c967d3dSChristopher Smith            $this->lang["user_mail"],
8415c967d3dSChristopher Smith            $this->lang["user_groups"]
8425c967d3dSChristopher Smith        );
8435c967d3dSChristopher Smith
8445c967d3dSChristopher Smith        // ==============================================================================================
8455c967d3dSChristopher Smith        // GENERATE OUTPUT
8465c967d3dSChristopher Smith        // normal headers for downloading...
8475c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8485c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8495c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8505c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8515c967d3dSChristopher Smith
8525c967d3dSChristopher Smith        // output the csv
8535c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8545c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8555c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8565c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8575c967d3dSChristopher Smith            fputcsv($fd, $line);
8585c967d3dSChristopher Smith        }
8595c967d3dSChristopher Smith        fclose($fd);
860b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
861b2c01466SChristopher Smith
8625c967d3dSChristopher Smith        die;
8635c967d3dSChristopher Smith    }
864ae1afd2fSChristopher Smith
865c5a7c0c6SGerrit Uitslag    /**
866c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
867ae1afd2fSChristopher Smith     *
868ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
869c5a7c0c6SGerrit Uitslag     *
8705ba64050SChristopher Smith     * @return bool whether successful
871ae1afd2fSChristopher Smith     */
8723712ca9aSGerrit Uitslag    protected function _import() {
873ae1afd2fSChristopher Smith        // check we are allowed to add users
874ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
875ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
876ae1afd2fSChristopher Smith
877ae1afd2fSChristopher Smith        // check file uploaded ok.
878b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
879ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
880ae1afd2fSChristopher Smith            return false;
881ae1afd2fSChristopher Smith        }
882ae1afd2fSChristopher Smith        // retrieve users from the file
883ae1afd2fSChristopher Smith        $this->_import_failures = array();
884ae1afd2fSChristopher Smith        $import_success_count = 0;
885ae1afd2fSChristopher Smith        $import_fail_count = 0;
886ae1afd2fSChristopher Smith        $line = 0;
887ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
888ae1afd2fSChristopher Smith        if ($fd) {
889ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
890efcec72bSChristopher Smith                if (!utf8_check($csv)) {
891efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
892efcec72bSChristopher Smith                }
893c9454ee3SChristopher Smith                $raw = $this->_getcsv($csv);
894ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
895ae1afd2fSChristopher Smith                // data checks...
896ae1afd2fSChristopher Smith                if (1 == ++$line) {
897ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
898ae1afd2fSChristopher Smith                }
899ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
900ae1afd2fSChristopher Smith                    $import_fail_count++;
901ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
902ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
903ae1afd2fSChristopher Smith                    continue;
904ae1afd2fSChristopher Smith                }
905ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
906ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
907ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
908328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
909328143f8SChristopher Smith                    if (!$sent){
910328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
911328143f8SChristopher Smith                    }
912ae1afd2fSChristopher Smith                    $import_success_count++;
913ae1afd2fSChristopher Smith                } else {
914ae1afd2fSChristopher Smith                    $import_fail_count++;
915e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
916ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
917ae1afd2fSChristopher Smith                }
918ae1afd2fSChristopher Smith            }
919ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
920ae1afd2fSChristopher Smith            if ($import_fail_count) {
921ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
922ae1afd2fSChristopher Smith            }
923ae1afd2fSChristopher Smith        } else {
924ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
925ae1afd2fSChristopher Smith        }
926ae1afd2fSChristopher Smith
927ae1afd2fSChristopher Smith        // save import failures into the session
928ae1afd2fSChristopher Smith        if (!headers_sent()) {
929ae1afd2fSChristopher Smith            session_start();
930ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
931ae1afd2fSChristopher Smith            session_write_close();
932ae1afd2fSChristopher Smith        }
933c5a7c0c6SGerrit Uitslag        return true;
934ae1afd2fSChristopher Smith    }
935ae1afd2fSChristopher Smith
936c5a7c0c6SGerrit Uitslag    /**
937786dfb0eSGerrit Uitslag     * Returns cleaned user data
938c5a7c0c6SGerrit Uitslag     *
939c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
940c5a7c0c6SGerrit Uitslag     * @param $error
941c5a7c0c6SGerrit Uitslag     * @return array|bool cleaned data or false
942c5a7c0c6SGerrit Uitslag     */
9433712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
944ae1afd2fSChristopher Smith        global $INPUT;
945ae1afd2fSChristopher Smith
946ae1afd2fSChristopher Smith        // kludgy ....
947ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
948ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
949ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
950ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
951ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
952ae1afd2fSChristopher Smith
953ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
954ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
955ae1afd2fSChristopher Smith        if (empty($user)) {
956ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
957ae1afd2fSChristopher Smith            return false;
958ae1afd2fSChristopher Smith        }
959ae1afd2fSChristopher Smith
960ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
961ae1afd2fSChristopher Smith
962ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
963ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
964ae1afd2fSChristopher Smith            return false;
965ae1afd2fSChristopher Smith        }
966ae1afd2fSChristopher Smith
967328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
968328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
969ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
970ae1afd2fSChristopher Smith                return false;
971ae1afd2fSChristopher Smith            }
972328143f8SChristopher Smith        } else {
973328143f8SChristopher Smith            if (!empty($mail)) {
974328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
975328143f8SChristopher Smith                return false;
976328143f8SChristopher Smith            }
977328143f8SChristopher Smith        }
978ae1afd2fSChristopher Smith
979ae1afd2fSChristopher Smith        return $cleaned;
980ae1afd2fSChristopher Smith    }
981ae1afd2fSChristopher Smith
982c5a7c0c6SGerrit Uitslag    /**
983c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
984c5a7c0c6SGerrit Uitslag     *
985c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
986c5a7c0c6SGerrit Uitslag     *
987c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
988c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
9895ba64050SChristopher Smith     * @return bool whether successful
990c5a7c0c6SGerrit Uitslag     */
9913712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
992ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
993ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
994ae1afd2fSChristopher Smith            return false;
995ae1afd2fSChristopher Smith        }
996ae1afd2fSChristopher Smith
997ae1afd2fSChristopher Smith        return true;
998ae1afd2fSChristopher Smith    }
999ae1afd2fSChristopher Smith
1000c5a7c0c6SGerrit Uitslag    /**
1001c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1002c5a7c0c6SGerrit Uitslag     */
10033712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
1004ae1afd2fSChristopher Smith
1005ae1afd2fSChristopher Smith        // ==============================================================================================
1006ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1007ae1afd2fSChristopher Smith        // normal headers for downloading...
1008ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1009ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1010ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1011ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1012ae1afd2fSChristopher Smith
1013ae1afd2fSChristopher Smith        // output the csv
1014ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
1015c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
1016ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
1017ae1afd2fSChristopher Smith        }
1018ae1afd2fSChristopher Smith        fclose($fd);
1019ae1afd2fSChristopher Smith        die;
1020ae1afd2fSChristopher Smith    }
1021ae1afd2fSChristopher Smith
1022b2c01466SChristopher Smith    /**
1023b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1024b2c01466SChristopher Smith     */
1025b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
1026b2c01466SChristopher Smith        return is_uploaded_file($file);
1027b2c01466SChristopher Smith    }
1028b2c01466SChristopher Smith
1029c9454ee3SChristopher Smith    /**
1030c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
1031c9454ee3SChristopher Smith     *
1032c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1033c9454ee3SChristopher Smith     *                also associated unit test & mock access method
1034c9454ee3SChristopher Smith     */
1035c9454ee3SChristopher Smith    protected function _getcsv($csv) {
1036c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
1037c9454ee3SChristopher Smith    }
1038c9454ee3SChristopher Smith
1039c9454ee3SChristopher Smith    /**
1040c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
1041c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
1042c9454ee3SChristopher Smith     *
1043c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1044c9454ee3SChristopher Smith     */
1045c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1046c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1047c9454ee3SChristopher Smith        fputs($fp, $str);
1048c9454ee3SChristopher Smith        rewind($fp);
1049c9454ee3SChristopher Smith
1050c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1051c9454ee3SChristopher Smith
1052c9454ee3SChristopher Smith        fclose($fp);
1053c9454ee3SChristopher Smith        return $data;
1054c9454ee3SChristopher Smith    }
10550440ff15Schris}
1056