xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision b59cff8b714aa11b5b1afd209e9d73f6803883cd)
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
24c5a7c0c6SGerrit Uitslag    private $_auth = null;        // auth object
25c5a7c0c6SGerrit Uitslag    private $_user_total = 0;     // number of registered users
26c5a7c0c6SGerrit Uitslag    private $_filter = array();   // user selection filter(s)
27c5a7c0c6SGerrit Uitslag    private $_start = 0;          // index of first user to be displayed
28c5a7c0c6SGerrit Uitslag    private $_last = 0;           // index of the last user to be displayed
29c5a7c0c6SGerrit Uitslag    private $_pagesize = 20;      // number of users to list on one page
30c5a7c0c6SGerrit Uitslag    private $_edit_user = '';     // set to user selected for editing
31c5a7c0c6SGerrit Uitslag    private $_edit_userdata = array();
32c5a7c0c6SGerrit Uitslag    private $_disabled = '';      // if disabled set to explanatory string
33c5a7c0c6SGerrit Uitslag    private $_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');
146*b59cff8bSGerrit 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     */
277c5a7c0c6SGerrit Uitslag    private 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);
30226fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
30326fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
30426fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
30526fb387bSchris
306a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
307ee9498f5SChristopher Smith            if ($cmd == 'add') {
308c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
309ee9498f5SChristopher Smith            }
310a6858c6aSchris            if ($user) {
311a6858c6aSchris                $notes[] = $this->lang['note_notify'];
312a6858c6aSchris            }
313a6858c6aSchris
314a6858c6aSchris            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);
315a6858c6aSchris        }
316a6858c6aSchris
3170440ff15Schris        ptln("    </tbody>",$indent);
3180440ff15Schris        ptln("    <tbody>",$indent);
3190440ff15Schris        ptln("      <tr>",$indent);
3200440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3210440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3220440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3230440ff15Schris
3240440ff15Schris        // save current $user, we need this to access details if the name is changed
3250440ff15Schris        if ($user)
3260440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3270440ff15Schris
3280440ff15Schris        $this->_htmlFilterSettings($indent+10);
3290440ff15Schris
330a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3310440ff15Schris        ptln("        </td>",$indent);
3320440ff15Schris        ptln("      </tr>",$indent);
3330440ff15Schris        ptln("    </tbody>",$indent);
3340440ff15Schris        ptln("  </table>",$indent);
33545c19902SChristopher Smith
33645c19902SChristopher Smith        if ($notes) {
33745c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
33845c19902SChristopher Smith            foreach ($notes as $note) {
33945c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
34045c19902SChristopher Smith            }
34145c19902SChristopher Smith            ptln("    </ul>");
34245c19902SChristopher Smith        }
343c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3440440ff15Schris        ptln("</form>",$indent);
3450440ff15Schris    }
3460440ff15Schris
347c5a7c0c6SGerrit Uitslag    /**
348c5a7c0c6SGerrit Uitslag     * Prints a inputfield
349c5a7c0c6SGerrit Uitslag     *
350c5a7c0c6SGerrit Uitslag     * @param string $id
351c5a7c0c6SGerrit Uitslag     * @param string $name
352c5a7c0c6SGerrit Uitslag     * @param string $label
353c5a7c0c6SGerrit Uitslag     * @param string $value
354c5a7c0c6SGerrit Uitslag     * @param bool   $cando whether auth backend is capable to do this action
355c5a7c0c6SGerrit Uitslag     * @param int $indent
356c5a7c0c6SGerrit Uitslag     */
357c5a7c0c6SGerrit Uitslag    private function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3587de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3597de12fceSAndreas Gohr        echo str_pad('',$indent);
3607de12fceSAndreas Gohr
361d796a891SAndreas Gohr        if($name == 'userpass'){
362d796a891SAndreas Gohr            $fieldtype = 'password';
363d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3647b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3657b3674bdSChristopher Smith            $fieldtype = 'email';
3667b3674bdSChristopher Smith            $autocomp  = '';
367d796a891SAndreas Gohr        }else{
368d796a891SAndreas Gohr            $fieldtype = 'text';
369d796a891SAndreas Gohr            $autocomp  = '';
370d796a891SAndreas Gohr        }
371d796a891SAndreas Gohr
372ee54059bSTimo Voipio
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     */
392c5a7c0c6SGerrit Uitslag    private 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     */
402c5a7c0c6SGerrit Uitslag    private 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     */
416c5a7c0c6SGerrit Uitslag    private 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();
425*b59cff8bSGerrit 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);
438*b59cff8bSGerrit 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);
463*b59cff8bSGerrit 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     */
474c5a7c0c6SGerrit Uitslag    private function _addUser(){
47500d58927SMichael Hamann        global $INPUT;
476634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
47782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4780440ff15Schris
4790440ff15Schris        list($user,$pass,$name,$mail,$grps) = $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                }
4906733c4d7SChris Smith            }
4916733c4d7SChris Smith        } else {
4926733c4d7SChris Smith            if (!empty($pass)){
4936733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4946733c4d7SChris Smith                return false;
4956733c4d7SChris Smith            }
4966733c4d7SChris Smith        }
4976733c4d7SChris Smith
4986733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
4996733c4d7SChris Smith            if (empty($name)){
5006733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5016733c4d7SChris Smith                return false;
5026733c4d7SChris Smith            }
5036733c4d7SChris Smith        } else {
5046733c4d7SChris Smith            if (!empty($name)){
5056733c4d7SChris Smith                return false;
5066733c4d7SChris Smith            }
5076733c4d7SChris Smith        }
5086733c4d7SChris Smith
5096733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5106733c4d7SChris Smith            if (empty($mail)){
5116733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5126733c4d7SChris Smith                return false;
5136733c4d7SChris Smith            }
5146733c4d7SChris Smith        } else {
5156733c4d7SChris Smith            if (!empty($mail)){
5166733c4d7SChris Smith                return false;
5176733c4d7SChris Smith            }
5186733c4d7SChris Smith        }
5190440ff15Schris
5207d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
521a6858c6aSchris
522a6858c6aSchris            msg($this->lang['add_ok'], 1);
523a6858c6aSchris
52400d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
525a6858c6aSchris                $this->_notifyUser($user,$pass);
526a6858c6aSchris            }
527a6858c6aSchris        } else {
52860b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
529a6858c6aSchris        }
530a6858c6aSchris
531a6858c6aSchris        return $ok;
5320440ff15Schris    }
5330440ff15Schris
5340440ff15Schris    /**
535c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
536c5a7c0c6SGerrit Uitslag     *
537c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5380440ff15Schris     */
539c5a7c0c6SGerrit Uitslag    private function _deleteUser(){
54000d58927SMichael Hamann        global $conf, $INPUT;
5419ec82636SAndreas Gohr
542634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
54382fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5440440ff15Schris
54500d58927SMichael Hamann        $selected = $INPUT->arr('delete');
54600d58927SMichael Hamann        if (empty($selected)) return false;
5470440ff15Schris        $selected = array_keys($selected);
5480440ff15Schris
549c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
550c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
551c9a8f912SMichael Klier            return false;
552c9a8f912SMichael Klier        }
553c9a8f912SMichael Klier
5547d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5550440ff15Schris        if ($count == count($selected)) {
5560440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5570440ff15Schris            msg("$text.", 1);
5580440ff15Schris        } else {
5590440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5600440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5610440ff15Schris            msg("$part1, $part2",-1);
5620440ff15Schris        }
56378c7c8c9Schris
5649ec82636SAndreas Gohr        // invalidate all sessions
5659ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5669ec82636SAndreas Gohr
56778c7c8c9Schris        return true;
56878c7c8c9Schris    }
56978c7c8c9Schris
57078c7c8c9Schris    /**
57178c7c8c9Schris     * Edit user (a user has been selected for editing)
572c5a7c0c6SGerrit Uitslag     *
573c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
574c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
57578c7c8c9Schris     */
576c5a7c0c6SGerrit Uitslag    private function _editUser($param) {
577634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
57878c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
57978c7c8c9Schris
58078c7c8c9Schris        $user = cleanID(preg_replace('/.*:/','',$param));
58178c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
58278c7c8c9Schris
58378c7c8c9Schris        // no user found?
58478c7c8c9Schris        if (!$userdata) {
58578c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
58678c7c8c9Schris            return false;
58778c7c8c9Schris        }
58878c7c8c9Schris
58978c7c8c9Schris        $this->_edit_user = $user;
59078c7c8c9Schris        $this->_edit_userdata = $userdata;
59178c7c8c9Schris
59278c7c8c9Schris        return true;
5930440ff15Schris    }
5940440ff15Schris
5950440ff15Schris    /**
596c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
597c5a7c0c6SGerrit Uitslag     *
598c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5990440ff15Schris     */
600c5a7c0c6SGerrit Uitslag    private function _modifyUser(){
60100d58927SMichael Hamann        global $conf, $INPUT;
6029ec82636SAndreas Gohr
603634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
60482fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6050440ff15Schris
60626fb387bSchris        // get currently valid  user data
60700d58927SMichael Hamann        $olduser = cleanID(preg_replace('/.*:/','',$INPUT->str('userid_old')));
608073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
609073766c6Smatthiasgrimm
61026fb387bSchris        // get new user data subject to change
611073766c6Smatthiasgrimm        list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser();
612073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6130440ff15Schris
6140440ff15Schris        $changes = array();
615073766c6Smatthiasgrimm        if ($newuser != $olduser) {
61626fb387bSchris
61726fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
61826fb387bSchris                msg($this->lang['update_fail'],-1);
61926fb387bSchris                return false;
62026fb387bSchris            }
62126fb387bSchris
62226fb387bSchris            // check if $newuser already exists
623073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
624073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
625a6858c6aSchris                $re_edit = true;
6260440ff15Schris            } else {
627073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6280440ff15Schris            }
62993eefc2fSAndreas Gohr        }
63093eefc2fSAndreas Gohr
63193eefc2fSAndreas Gohr        // generate password if left empty and notification is on
63200d58927SMichael Hamann        if($INPUT->has('usernotify') && empty($newpass)){
6338a285f7fSAndreas Gohr            $newpass = auth_pwgen($olduser);
6340440ff15Schris        }
6350440ff15Schris
63626fb387bSchris        if (!empty($newpass) && $this->_auth->canDo('modPass'))
637073766c6Smatthiasgrimm          $changes['pass'] = $newpass;
63826fb387bSchris        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name'])
639073766c6Smatthiasgrimm          $changes['name'] = $newname;
64026fb387bSchris        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail'])
641073766c6Smatthiasgrimm          $changes['mail'] = $newmail;
64226fb387bSchris        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps'])
643073766c6Smatthiasgrimm          $changes['grps'] = $newgrps;
6440440ff15Schris
6457d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6460440ff15Schris            msg($this->lang['update_ok'],1);
647a6858c6aSchris
64800d58927SMichael Hamann            if ($INPUT->has('usernotify') && $newpass) {
649a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
650a6858c6aSchris                $this->_notifyUser($notify,$newpass);
651a6858c6aSchris            }
652a6858c6aSchris
6539ec82636SAndreas Gohr            // invalidate all sessions
6549ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6559ec82636SAndreas Gohr
6560440ff15Schris        } else {
6570440ff15Schris            msg($this->lang['update_fail'],-1);
6580440ff15Schris        }
65978c7c8c9Schris
660a6858c6aSchris        if (!empty($re_edit)) {
661a6858c6aSchris            $this->_editUser($olduser);
6620440ff15Schris        }
6630440ff15Schris
664a6858c6aSchris        return $ok;
665a6858c6aSchris    }
666a6858c6aSchris
667a6858c6aSchris    /**
668c5a7c0c6SGerrit Uitslag     * Send password change notification email
669c5a7c0c6SGerrit Uitslag     *
670c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
671c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
672c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
673c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
674a6858c6aSchris     */
675c5a7c0c6SGerrit Uitslag    private function _notifyUser($user, $password, $status_alert=true) {
676a6858c6aSchris
677a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
678328143f8SChristopher Smith            if ($status_alert) {
679a6858c6aSchris                msg($this->lang['notify_ok'], 1);
680328143f8SChristopher Smith            }
681a6858c6aSchris        } else {
682328143f8SChristopher Smith            if ($status_alert) {
683a6858c6aSchris                msg($this->lang['notify_fail'], -1);
684a6858c6aSchris            }
685328143f8SChristopher Smith        }
686a6858c6aSchris
687a6858c6aSchris        return $sent;
688a6858c6aSchris    }
689a6858c6aSchris
690a6858c6aSchris    /**
691c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
692a6858c6aSchris     *
693c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
694a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
6950440ff15Schris     */
696c5a7c0c6SGerrit Uitslag    private function _retrieveUser($clean=true) {
697c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
6987441e340SAndreas Gohr        global $auth;
699fbfbbe8aSHakan Sandell        global $INPUT;
7000440ff15Schris
701fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
702fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
703fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
704fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
705fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
7060440ff15Schris
7077441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7087441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7097441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7107441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7117441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7120440ff15Schris
7130440ff15Schris        return $user;
7140440ff15Schris    }
7150440ff15Schris
716c5a7c0c6SGerrit Uitslag    /**
717c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
718c5a7c0c6SGerrit Uitslag     *
719c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
720c5a7c0c6SGerrit Uitslag     */
721c5a7c0c6SGerrit Uitslag    private function _setFilter($op) {
7220440ff15Schris
7230440ff15Schris        $this->_filter = array();
7240440ff15Schris
7250440ff15Schris        if ($op == 'new') {
7260440ff15Schris            list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
7270440ff15Schris
7280440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7290440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7300440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7310440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7320440ff15Schris        }
7330440ff15Schris    }
7340440ff15Schris
735c5a7c0c6SGerrit Uitslag    /**
736c5a7c0c6SGerrit Uitslag     * Get the current search terms
737c5a7c0c6SGerrit Uitslag     *
738c5a7c0c6SGerrit Uitslag     * @return array
739c5a7c0c6SGerrit Uitslag     */
740c5a7c0c6SGerrit Uitslag    private function _retrieveFilter() {
741fbfbbe8aSHakan Sandell        global $INPUT;
7420440ff15Schris
743fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
7440440ff15Schris
7450440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
7460440ff15Schris        $filter = array();
7470440ff15Schris
7480440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
7490440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
7500440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
7510440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
7520440ff15Schris
7530440ff15Schris        return $filter;
7540440ff15Schris    }
7550440ff15Schris
756c5a7c0c6SGerrit Uitslag    /**
757c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
758c5a7c0c6SGerrit Uitslag     */
759c5a7c0c6SGerrit Uitslag    private function _validatePagination() {
7600440ff15Schris
7610440ff15Schris        if ($this->_start >= $this->_user_total) {
7620440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
7630440ff15Schris        }
7640440ff15Schris        if ($this->_start < 0) $this->_start = 0;
7650440ff15Schris
7660440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
7670440ff15Schris    }
7680440ff15Schris
769c5a7c0c6SGerrit Uitslag    /**
770c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
771c5a7c0c6SGerrit Uitslag     *
772c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
7730440ff15Schris     */
774c5a7c0c6SGerrit Uitslag    private function _pagination() {
7750440ff15Schris
77651d94d49Schris        $disabled = 'disabled="disabled"';
77751d94d49Schris
77851d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
77951d94d49Schris
78051d94d49Schris        if ($this->_user_total == -1) {
78151d94d49Schris            $buttons['last'] = $disabled;
78251d94d49Schris            $buttons['next'] = '';
78351d94d49Schris        } else {
78451d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
78551d94d49Schris        }
7860440ff15Schris
7870440ff15Schris        return $buttons;
7880440ff15Schris    }
7895c967d3dSChristopher Smith
790c5a7c0c6SGerrit Uitslag    /**
791c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
7925c967d3dSChristopher Smith     */
793c5a7c0c6SGerrit Uitslag    private function _export() {
7945c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
7955c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
7965c967d3dSChristopher Smith        $column_headings = array(
7975c967d3dSChristopher Smith            $this->lang["user_id"],
7985c967d3dSChristopher Smith            $this->lang["user_name"],
7995c967d3dSChristopher Smith            $this->lang["user_mail"],
8005c967d3dSChristopher Smith            $this->lang["user_groups"]
8015c967d3dSChristopher Smith        );
8025c967d3dSChristopher Smith
8035c967d3dSChristopher Smith        // ==============================================================================================
8045c967d3dSChristopher Smith        // GENERATE OUTPUT
8055c967d3dSChristopher Smith        // normal headers for downloading...
8065c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8075c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8085c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8095c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8105c967d3dSChristopher Smith
8115c967d3dSChristopher Smith        // output the csv
8125c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8135c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8145c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8155c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8165c967d3dSChristopher Smith            fputcsv($fd, $line);
8175c967d3dSChristopher Smith        }
8185c967d3dSChristopher Smith        fclose($fd);
8195c967d3dSChristopher Smith        die;
8205c967d3dSChristopher Smith    }
821ae1afd2fSChristopher Smith
822c5a7c0c6SGerrit Uitslag    /**
823c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
824ae1afd2fSChristopher Smith     *
825ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
826c5a7c0c6SGerrit Uitslag     *
827c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
828ae1afd2fSChristopher Smith     */
829c5a7c0c6SGerrit Uitslag    private function _import() {
830ae1afd2fSChristopher Smith        // check we are allowed to add users
831ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
832ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
833ae1afd2fSChristopher Smith
834ae1afd2fSChristopher Smith        // check file uploaded ok.
835ae1afd2fSChristopher Smith        if (empty($_FILES['import']['size']) || !empty($FILES['import']['error']) && is_uploaded_file($FILES['import']['tmp_name'])) {
836ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
837ae1afd2fSChristopher Smith            return false;
838ae1afd2fSChristopher Smith        }
839ae1afd2fSChristopher Smith        // retrieve users from the file
840ae1afd2fSChristopher Smith        $this->_import_failures = array();
841ae1afd2fSChristopher Smith        $import_success_count = 0;
842ae1afd2fSChristopher Smith        $import_fail_count = 0;
843ae1afd2fSChristopher Smith        $line = 0;
844ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
845ae1afd2fSChristopher Smith        if ($fd) {
846ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
847efcec72bSChristopher Smith                if (!utf8_check($csv)) {
848efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
849efcec72bSChristopher Smith                }
850ae1afd2fSChristopher Smith                $raw = str_getcsv($csv);
851ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
852ae1afd2fSChristopher Smith                // data checks...
853ae1afd2fSChristopher Smith                if (1 == ++$line) {
854ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
855ae1afd2fSChristopher Smith                }
856ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
857ae1afd2fSChristopher Smith                    $import_fail_count++;
858ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
859ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
860ae1afd2fSChristopher Smith                    continue;
861ae1afd2fSChristopher Smith                }
862ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
863ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
864ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
865328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
866328143f8SChristopher Smith                    if (!$sent){
867328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
868328143f8SChristopher Smith                    }
869ae1afd2fSChristopher Smith                    $import_success_count++;
870ae1afd2fSChristopher Smith                } else {
871ae1afd2fSChristopher Smith                    $import_fail_count++;
872ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
873ae1afd2fSChristopher Smith                }
874ae1afd2fSChristopher Smith            }
875ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
876ae1afd2fSChristopher Smith            if ($import_fail_count) {
877ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
878ae1afd2fSChristopher Smith            }
879ae1afd2fSChristopher Smith        } else {
880ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
881ae1afd2fSChristopher Smith        }
882ae1afd2fSChristopher Smith
883ae1afd2fSChristopher Smith        // save import failures into the session
884ae1afd2fSChristopher Smith        if (!headers_sent()) {
885ae1afd2fSChristopher Smith            session_start();
886ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
887ae1afd2fSChristopher Smith            session_write_close();
888ae1afd2fSChristopher Smith        }
889c5a7c0c6SGerrit Uitslag        return true;
890ae1afd2fSChristopher Smith    }
891ae1afd2fSChristopher Smith
892c5a7c0c6SGerrit Uitslag    /**
893c5a7c0c6SGerrit Uitslag     * Returns cleaned row data
894c5a7c0c6SGerrit Uitslag     *
895c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
896c5a7c0c6SGerrit Uitslag     * @param $error
897c5a7c0c6SGerrit Uitslag     * @return array|bool cleaned data or false
898c5a7c0c6SGerrit Uitslag     */
899c5a7c0c6SGerrit Uitslag    private function _cleanImportUser($candidate, & $error){
900ae1afd2fSChristopher Smith        global $INPUT;
901ae1afd2fSChristopher Smith
902ae1afd2fSChristopher Smith        // kludgy ....
903ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
904ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
905ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
906ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
907ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
908ae1afd2fSChristopher Smith
909ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
910ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
911ae1afd2fSChristopher Smith        if (empty($user)) {
912ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
913ae1afd2fSChristopher Smith            return false;
914ae1afd2fSChristopher Smith        }
915ae1afd2fSChristopher Smith
916ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
917ae1afd2fSChristopher Smith
918ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
919ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
920ae1afd2fSChristopher Smith            return false;
921ae1afd2fSChristopher Smith        }
922ae1afd2fSChristopher Smith
923328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
924328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
925ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
926ae1afd2fSChristopher Smith                return false;
927ae1afd2fSChristopher Smith            }
928328143f8SChristopher Smith        } else {
929328143f8SChristopher Smith            if (!empty($mail)) {
930328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
931328143f8SChristopher Smith                return false;
932328143f8SChristopher Smith            }
933328143f8SChristopher Smith        }
934ae1afd2fSChristopher Smith
935ae1afd2fSChristopher Smith        return $cleaned;
936ae1afd2fSChristopher Smith    }
937ae1afd2fSChristopher Smith
938c5a7c0c6SGerrit Uitslag    /**
939c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
940c5a7c0c6SGerrit Uitslag     *
941c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
942c5a7c0c6SGerrit Uitslag     *
943c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
944c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
945c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
946c5a7c0c6SGerrit Uitslag     */
947c5a7c0c6SGerrit Uitslag    private function _addImportUser($user, & $error){
948ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
949ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
950ae1afd2fSChristopher Smith            return false;
951ae1afd2fSChristopher Smith        }
952ae1afd2fSChristopher Smith
953ae1afd2fSChristopher Smith        return true;
954ae1afd2fSChristopher Smith    }
955ae1afd2fSChristopher Smith
956c5a7c0c6SGerrit Uitslag    /**
957c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
958c5a7c0c6SGerrit Uitslag     */
959c5a7c0c6SGerrit Uitslag    private function _downloadImportFailures(){
960ae1afd2fSChristopher Smith
961ae1afd2fSChristopher Smith        // ==============================================================================================
962ae1afd2fSChristopher Smith        // GENERATE OUTPUT
963ae1afd2fSChristopher Smith        // normal headers for downloading...
964ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
965ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
966ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
967ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
968ae1afd2fSChristopher Smith
969ae1afd2fSChristopher Smith        // output the csv
970ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
971c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
972ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
973ae1afd2fSChristopher Smith        }
974ae1afd2fSChristopher Smith        fclose($fd);
975ae1afd2fSChristopher Smith        die;
976ae1afd2fSChristopher Smith    }
977ae1afd2fSChristopher Smith
9780440ff15Schris}
979