xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision c9454ee3a82b29418a7ee59f0bb35006ea6a7fb1)
10440ff15Schris<?php
20440ff15Schris/*
30440ff15Schris *  User Manager
40440ff15Schris *
50440ff15Schris *  Dokuwiki Admin Plugin
60440ff15Schris *
70440ff15Schris *  This version of the user manager has been modified to only work with
80440ff15Schris *  objectified version of auth system
90440ff15Schris *
100440ff15Schris *  @author  neolao <neolao@neolao.com>
110440ff15Schris *  @author  Chris Smith <chris@jalakai.co.uk>
120440ff15Schris */
13e04f1f16Schris// must be run within Dokuwiki
14e04f1f16Schrisif(!defined('DOKU_INC')) die();
15e04f1f16Schris
160440ff15Schrisif(!defined('DOKU_PLUGIN_IMAGES')) define('DOKU_PLUGIN_IMAGES',DOKU_BASE.'lib/plugins/usermanager/images/');
170440ff15Schris
180440ff15Schris/**
190440ff15Schris * All DokuWiki plugins to extend the admin function
200440ff15Schris * need to inherit from this class
210440ff15Schris */
220440ff15Schrisclass admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
230440ff15Schris
243712ca9aSGerrit Uitslag    protected $_auth = null;        // auth object
253712ca9aSGerrit Uitslag    protected $_user_total = 0;     // number of registered users
263712ca9aSGerrit Uitslag    protected $_filter = array();   // user selection filter(s)
273712ca9aSGerrit Uitslag    protected $_start = 0;          // index of first user to be displayed
283712ca9aSGerrit Uitslag    protected $_last = 0;           // index of the last user to be displayed
293712ca9aSGerrit Uitslag    protected $_pagesize = 20;      // number of users to list on one page
303712ca9aSGerrit Uitslag    protected $_edit_user = '';     // set to user selected for editing
313712ca9aSGerrit Uitslag    protected $_edit_userdata = array();
323712ca9aSGerrit Uitslag    protected $_disabled = '';      // if disabled set to explanatory string
333712ca9aSGerrit Uitslag    protected $_import_failures = array();
340440ff15Schris
350440ff15Schris    /**
360440ff15Schris     * Constructor
370440ff15Schris     */
38c5a7c0c6SGerrit Uitslag    public function admin_plugin_usermanager(){
39c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
400440ff15Schris        global $auth;
410440ff15Schris
420440ff15Schris        $this->setupLocale();
4351d94d49Schris
4451d94d49Schris        if (!isset($auth)) {
45c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['noauth'];
4682fd59b6SAndreas Gohr        } else if (!$auth->canDo('getUsers')) {
47c5a7c0c6SGerrit Uitslag            $this->_disabled = $this->lang['nosupport'];
4851d94d49Schris        } else {
4951d94d49Schris
5051d94d49Schris            // we're good to go
5151d94d49Schris            $this->_auth = & $auth;
5251d94d49Schris
5351d94d49Schris        }
54ae1afd2fSChristopher Smith
55ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
56ae1afd2fSChristopher Smith        if ($_SESSION['import_failures']){
57ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
58ae1afd2fSChristopher Smith        }
590440ff15Schris    }
600440ff15Schris
610440ff15Schris     /**
62c5a7c0c6SGerrit Uitslag      * Return prompt for admin menu
630440ff15Schris      */
64c5a7c0c6SGerrit Uitslag    public function getMenuText($language) {
650440ff15Schris
660440ff15Schris        if (!is_null($this->_auth))
670440ff15Schris          return parent::getMenuText($language);
680440ff15Schris
69c5a7c0c6SGerrit Uitslag        return $this->getLang('menu').' '.$this->_disabled;
700440ff15Schris    }
710440ff15Schris
720440ff15Schris    /**
730440ff15Schris     * return sort order for position in admin menu
740440ff15Schris     */
75c5a7c0c6SGerrit Uitslag    public function getMenuSort() {
760440ff15Schris        return 2;
770440ff15Schris    }
780440ff15Schris
790440ff15Schris    /**
80c5a7c0c6SGerrit Uitslag     * Handle user request
810440ff15Schris     */
82c5a7c0c6SGerrit Uitslag    public function handle() {
8300d58927SMichael Hamann        global $INPUT;
840440ff15Schris        if (is_null($this->_auth)) return false;
850440ff15Schris
860440ff15Schris        // extract the command and any specific parameters
870440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
8800d58927SMichael Hamann        $fn   = $INPUT->param('fn');
890440ff15Schris
900440ff15Schris        if (is_array($fn)) {
910440ff15Schris            $cmd = key($fn);
920440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
930440ff15Schris        } else {
940440ff15Schris            $cmd = $fn;
950440ff15Schris            $param = null;
960440ff15Schris        }
970440ff15Schris
980440ff15Schris        if ($cmd != "search") {
9900d58927SMichael Hamann            $this->_start = $INPUT->int('start', 0);
1000440ff15Schris            $this->_filter = $this->_retrieveFilter();
1010440ff15Schris        }
1020440ff15Schris
1030440ff15Schris        switch($cmd){
1040440ff15Schris            case "add"    : $this->_addUser(); break;
1050440ff15Schris            case "delete" : $this->_deleteUser(); break;
1060440ff15Schris            case "modify" : $this->_modifyUser(); break;
10778c7c8c9Schris            case "edit"   : $this->_editUser($param); break;
1080440ff15Schris            case "search" : $this->_setFilter($param);
1090440ff15Schris                            $this->_start = 0;
1100440ff15Schris                            break;
1115c967d3dSChristopher Smith            case "export" : $this->_export(); break;
112ae1afd2fSChristopher Smith            case "import" : $this->_import(); break;
113ae1afd2fSChristopher Smith            case "importfails" : $this->_downloadImportFailures(); break;
1140440ff15Schris        }
1150440ff15Schris
11651d94d49Schris        $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
1170440ff15Schris
1180440ff15Schris        // page handling
1190440ff15Schris        switch($cmd){
1200440ff15Schris            case 'start' : $this->_start = 0; break;
1210440ff15Schris            case 'prev'  : $this->_start -= $this->_pagesize; break;
1220440ff15Schris            case 'next'  : $this->_start += $this->_pagesize; break;
1230440ff15Schris            case 'last'  : $this->_start = $this->_user_total; break;
1240440ff15Schris        }
1250440ff15Schris        $this->_validatePagination();
126c5a7c0c6SGerrit Uitslag        return true;
1270440ff15Schris    }
1280440ff15Schris
1290440ff15Schris    /**
130c5a7c0c6SGerrit Uitslag     * Output appropriate html
1310440ff15Schris     */
132c5a7c0c6SGerrit Uitslag    public function html() {
1330440ff15Schris        global $ID;
1340440ff15Schris
1350440ff15Schris        if(is_null($this->_auth)) {
1360440ff15Schris            print $this->lang['badauth'];
1370440ff15Schris            return false;
1380440ff15Schris        }
1390440ff15Schris
1400440ff15Schris        $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
1410440ff15Schris
1420440ff15Schris        $page_buttons = $this->_pagination();
14382fd59b6SAndreas Gohr        $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
1440440ff15Schris
14577d19185SAndreas Gohr        $editable = $this->_auth->canDo('UserMod');
146b59cff8bSGerrit Uitslag        $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang['export_filtered'];
1476154103cSmatthiasgrimm
1480440ff15Schris        print $this->locale_xhtml('intro');
1490440ff15Schris        print $this->locale_xhtml('list');
1500440ff15Schris
15158dde80dSAnika Henke        ptln("<div id=\"user__manager\">");
15258dde80dSAnika Henke        ptln("<div class=\"level2\">");
1530440ff15Schris
15467019d15Schris        if ($this->_user_total > 0) {
1550440ff15Schris            ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
1560440ff15Schris        } else {
157a102b175SGerrit Uitslag            if($this->_user_total < 0) {
158a102b175SGerrit Uitslag                $allUserTotal = 0;
159a102b175SGerrit Uitslag            } else {
160a102b175SGerrit Uitslag                $allUserTotal = $this->_auth->getUserCount();
161a102b175SGerrit Uitslag            }
162a102b175SGerrit Uitslag            ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>");
1630440ff15Schris        }
1640440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
165634d7150SAndreas Gohr        formSecurityToken();
166c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1670440ff15Schris        ptln("  <table class=\"inline\">");
1680440ff15Schris        ptln("    <thead>");
1690440ff15Schris        ptln("      <tr>");
170e260f93bSAnika Henke        ptln("        <th>&#160;</th><th>".$this->lang["user_id"]."</th><th>".$this->lang["user_name"]."</th><th>".$this->lang["user_mail"]."</th><th>".$this->lang["user_groups"]."</th>");
1710440ff15Schris        ptln("      </tr>");
1720440ff15Schris
1730440ff15Schris        ptln("      <tr>");
1742365d73dSAnika Henke        ptln("        <td class=\"rightalign\"><input type=\"image\" src=\"".DOKU_PLUGIN_IMAGES."search.png\" name=\"fn[search][new]\" title=\"".$this->lang['search_prompt']."\" alt=\"".$this->lang['search']."\" class=\"button\" /></td>");
175a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
176a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
177a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
178a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
1790440ff15Schris        ptln("      </tr>");
1800440ff15Schris        ptln("    </thead>");
1810440ff15Schris
1820440ff15Schris        if ($this->_user_total) {
1830440ff15Schris            ptln("    <tbody>");
1840440ff15Schris            foreach ($user_list as $user => $userinfo) {
1850440ff15Schris                extract($userinfo);
186c5a7c0c6SGerrit Uitslag                /**
187c5a7c0c6SGerrit Uitslag                 * @var string $name
188c5a7c0c6SGerrit Uitslag                 * @var string $pass
189c5a7c0c6SGerrit Uitslag                 * @var string $mail
190c5a7c0c6SGerrit Uitslag                 * @var array  $grps
191c5a7c0c6SGerrit Uitslag                 */
1920440ff15Schris                $groups = join(', ',$grps);
193a2c0246eSAnika Henke                ptln("    <tr class=\"user_info\">");
1940440ff15Schris                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
1952365d73dSAnika Henke                if ($editable) {
19677d19185SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
19777d19185SAndreas Gohr                                                           'do' => 'admin',
19877d19185SAndreas Gohr                                                           'page' => 'usermanager',
19977d19185SAndreas Gohr                                                           'sectok' => getSecurityToken())).
20077d19185SAndreas Gohr                         "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
2012365d73dSAnika Henke                } else {
2022365d73dSAnika Henke                    ptln("    <td>".hsc($user)."</td>");
2032365d73dSAnika Henke                }
2042365d73dSAnika Henke                ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
2050440ff15Schris                ptln("    </tr>");
2060440ff15Schris            }
2070440ff15Schris            ptln("    </tbody>");
2080440ff15Schris        }
2090440ff15Schris
2100440ff15Schris        ptln("    <tbody>");
2112365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
212a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
213a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2140440ff15Schris        ptln("        </span>");
215a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
216a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
217a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
218a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
219a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2200440ff15Schris        ptln("        </span>");
2215c967d3dSChristopher Smith        if (!empty($this->_filter)) {
222a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2235c967d3dSChristopher Smith        }
2245c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2255164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2265164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
227daf4ca4eSAnika Henke
228daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
229daf4ca4eSAnika Henke
2300440ff15Schris        ptln("      </td></tr>");
2310440ff15Schris        ptln("    </tbody>");
2320440ff15Schris        ptln("  </table>");
233c7b28ffdSAnika Henke        ptln("  </div>");
2340440ff15Schris
2350440ff15Schris        ptln("</form>");
2360440ff15Schris        ptln("</div>");
2370440ff15Schris
238a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2390440ff15Schris
24082fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2410440ff15Schris            ptln("<div".$style.">");
2420440ff15Schris            print $this->locale_xhtml('add');
2430440ff15Schris            ptln("  <div class=\"level2\">");
2440440ff15Schris
24578c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2460440ff15Schris
2470440ff15Schris            ptln("  </div>");
2480440ff15Schris            ptln("</div>");
2490440ff15Schris        }
2500440ff15Schris
25182fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
252c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2530440ff15Schris            print $this->locale_xhtml('edit');
2540440ff15Schris            ptln("  <div class=\"level2\">");
2550440ff15Schris
25678c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2570440ff15Schris
2580440ff15Schris            ptln("  </div>");
2590440ff15Schris            ptln("</div>");
2600440ff15Schris        }
261ae1afd2fSChristopher Smith
262ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
263ae1afd2fSChristopher Smith            $this->_htmlImportForm();
264ae1afd2fSChristopher Smith        }
26558dde80dSAnika Henke        ptln("</div>");
266c5a7c0c6SGerrit Uitslag        return true;
2670440ff15Schris    }
2680440ff15Schris
26982fd59b6SAndreas Gohr    /**
270c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
271c5a7c0c6SGerrit Uitslag     *
272c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
273c5a7c0c6SGerrit Uitslag     * @param string $user id of user
274c5a7c0c6SGerrit Uitslag     * @param array  $userdata array with name, mail, pass and grps
275c5a7c0c6SGerrit Uitslag     * @param int    $indent
27682fd59b6SAndreas Gohr     */
2773712ca9aSGerrit Uitslag    protected function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
278a6858c6aSchris        global $conf;
279bb4866bdSchris        global $ID;
28078c7c8c9Schris
28178c7c8c9Schris        $name = $mail = $groups = '';
282a6858c6aSchris        $notes = array();
2830440ff15Schris
2840440ff15Schris        if ($user) {
28578c7c8c9Schris            extract($userdata);
28678c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
287a6858c6aSchris        } else {
288a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2890440ff15Schris        }
2900440ff15Schris
2910440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
292634d7150SAndreas Gohr        formSecurityToken();
293c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
2940440ff15Schris        ptln("  <table class=\"inline\">",$indent);
2950440ff15Schris        ptln("    <thead>",$indent);
2960440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
2970440ff15Schris        ptln("    </thead>",$indent);
2980440ff15Schris        ptln("    <tbody>",$indent);
29926fb387bSchris
30026fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
30126fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
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     */
3573712ca9aSGerrit Uitslag    protected 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
3727de12fceSAndreas Gohr        echo "<tr $class>";
3737de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3747de12fceSAndreas Gohr        echo "<td>";
3757de12fceSAndreas Gohr        if($cando){
376d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3777de12fceSAndreas Gohr        }else{
3787de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
379ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3807de12fceSAndreas Gohr        }
3817de12fceSAndreas Gohr        echo "</td>";
3827de12fceSAndreas Gohr        echo "</tr>";
38326fb387bSchris    }
38426fb387bSchris
385c5a7c0c6SGerrit Uitslag    /**
386c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
387c5a7c0c6SGerrit Uitslag     *
388c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
389c5a7c0c6SGerrit Uitslag     * @return string html escaped value
390c5a7c0c6SGerrit Uitslag     */
3913712ca9aSGerrit Uitslag    protected function _htmlFilter($key) {
3920440ff15Schris        if (empty($this->_filter)) return '';
3930440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
3940440ff15Schris    }
3950440ff15Schris
396c5a7c0c6SGerrit Uitslag    /**
397c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
398c5a7c0c6SGerrit Uitslag     *
399c5a7c0c6SGerrit Uitslag     * @param int $indent
400c5a7c0c6SGerrit Uitslag     */
4013712ca9aSGerrit Uitslag    protected function _htmlFilterSettings($indent=0) {
4020440ff15Schris
4030440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
4040440ff15Schris
4050440ff15Schris        foreach ($this->_filter as $key => $filter) {
4060440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
4070440ff15Schris        }
4080440ff15Schris    }
4090440ff15Schris
410c5a7c0c6SGerrit Uitslag    /**
411c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
412c5a7c0c6SGerrit Uitslag     *
413c5a7c0c6SGerrit Uitslag     * @param int $indent
414c5a7c0c6SGerrit Uitslag     */
4153712ca9aSGerrit Uitslag    protected function _htmlImportForm($indent=0) {
416ae1afd2fSChristopher Smith        global $ID;
417ae1afd2fSChristopher Smith
418ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
419ae1afd2fSChristopher Smith
420ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
421ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
422ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
423ae1afd2fSChristopher Smith        formSecurityToken();
424b59cff8bSGerrit Uitslag        ptln('    <label>'.$this->lang['import_userlistcsv'].'<input type="file" name="import" /></label>',$indent);
425ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
426ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
427ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
428ae1afd2fSChristopher Smith
429ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
430ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
431ae1afd2fSChristopher Smith        ptln('</div>');
432ae1afd2fSChristopher Smith
433ae1afd2fSChristopher Smith        // list failures from the previous import
434ae1afd2fSChristopher Smith        if ($this->_import_failures) {
435ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
436ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
437b59cff8bSGerrit Uitslag            ptln('  <h3>'.$this->lang['import_header'].'</h3>');
438ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
439ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
440ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
441ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
442ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
443ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
444ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
445ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
446ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
447ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
448ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
449ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
450ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
451ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
452ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
453ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
454ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
455ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
456ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
457ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
458ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
459ae1afd2fSChristopher Smith            }
460ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
461ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
462b59cff8bSGerrit Uitslag            ptln('  <p><a href="'.$failure_download_link.'">'.$this->lang['import_downloadfailures'].'</a></p>');
463ae1afd2fSChristopher Smith            ptln('</div>');
464ae1afd2fSChristopher Smith        }
465ae1afd2fSChristopher Smith
466ae1afd2fSChristopher Smith    }
467ae1afd2fSChristopher Smith
468c5a7c0c6SGerrit Uitslag    /**
469c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
470c5a7c0c6SGerrit Uitslag     *
471c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
472c5a7c0c6SGerrit Uitslag     */
4733712ca9aSGerrit Uitslag    protected function _addUser(){
47400d58927SMichael Hamann        global $INPUT;
475634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
47682fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4770440ff15Schris
4780440ff15Schris        list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser();
4790440ff15Schris        if (empty($user)) return false;
4806733c4d7SChris Smith
4816733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
482c3f4fb63SGina Haeussge            if (empty($pass)){
48300d58927SMichael Hamann                if($INPUT->has('usernotify')){
4848a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
485c3f4fb63SGina Haeussge                } else {
48660b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
48760b9901bSAndreas Gohr                    return false;
48860b9901bSAndreas Gohr                }
4896733c4d7SChris Smith            }
4906733c4d7SChris Smith        } else {
4916733c4d7SChris Smith            if (!empty($pass)){
4926733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4936733c4d7SChris Smith                return false;
4946733c4d7SChris Smith            }
4956733c4d7SChris Smith        }
4966733c4d7SChris Smith
4976733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
4986733c4d7SChris Smith            if (empty($name)){
4996733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5006733c4d7SChris Smith                return false;
5016733c4d7SChris Smith            }
5026733c4d7SChris Smith        } else {
5036733c4d7SChris Smith            if (!empty($name)){
5046733c4d7SChris Smith                return false;
5056733c4d7SChris Smith            }
5066733c4d7SChris Smith        }
5076733c4d7SChris Smith
5086733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
5096733c4d7SChris Smith            if (empty($mail)){
5106733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
5116733c4d7SChris Smith                return false;
5126733c4d7SChris Smith            }
5136733c4d7SChris Smith        } else {
5146733c4d7SChris Smith            if (!empty($mail)){
5156733c4d7SChris Smith                return false;
5166733c4d7SChris Smith            }
5176733c4d7SChris Smith        }
5180440ff15Schris
5197d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
520a6858c6aSchris
521a6858c6aSchris            msg($this->lang['add_ok'], 1);
522a6858c6aSchris
52300d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
524a6858c6aSchris                $this->_notifyUser($user,$pass);
525a6858c6aSchris            }
526a6858c6aSchris        } else {
52760b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
528a6858c6aSchris        }
529a6858c6aSchris
530a6858c6aSchris        return $ok;
5310440ff15Schris    }
5320440ff15Schris
5330440ff15Schris    /**
534c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
535c5a7c0c6SGerrit Uitslag     *
536c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5370440ff15Schris     */
5383712ca9aSGerrit Uitslag    protected function _deleteUser(){
53900d58927SMichael Hamann        global $conf, $INPUT;
5409ec82636SAndreas Gohr
541634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
54282fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
5430440ff15Schris
54400d58927SMichael Hamann        $selected = $INPUT->arr('delete');
54500d58927SMichael Hamann        if (empty($selected)) return false;
5460440ff15Schris        $selected = array_keys($selected);
5470440ff15Schris
548c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
549c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
550c9a8f912SMichael Klier            return false;
551c9a8f912SMichael Klier        }
552c9a8f912SMichael Klier
5537d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5540440ff15Schris        if ($count == count($selected)) {
5550440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5560440ff15Schris            msg("$text.", 1);
5570440ff15Schris        } else {
5580440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5590440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5600440ff15Schris            msg("$part1, $part2",-1);
5610440ff15Schris        }
56278c7c8c9Schris
5639ec82636SAndreas Gohr        // invalidate all sessions
5649ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5659ec82636SAndreas Gohr
56678c7c8c9Schris        return true;
56778c7c8c9Schris    }
56878c7c8c9Schris
56978c7c8c9Schris    /**
57078c7c8c9Schris     * Edit user (a user has been selected for editing)
571c5a7c0c6SGerrit Uitslag     *
572c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
573c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
57478c7c8c9Schris     */
5753712ca9aSGerrit Uitslag    protected function _editUser($param) {
576634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
57778c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
578786dfb0eSGerrit Uitslag        $user = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$param));
57978c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
58078c7c8c9Schris
58178c7c8c9Schris        // no user found?
58278c7c8c9Schris        if (!$userdata) {
58378c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
58478c7c8c9Schris            return false;
58578c7c8c9Schris        }
58678c7c8c9Schris
58778c7c8c9Schris        $this->_edit_user = $user;
58878c7c8c9Schris        $this->_edit_userdata = $userdata;
58978c7c8c9Schris
59078c7c8c9Schris        return true;
5910440ff15Schris    }
5920440ff15Schris
5930440ff15Schris    /**
594c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
595c5a7c0c6SGerrit Uitslag     *
596c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
5970440ff15Schris     */
5983712ca9aSGerrit Uitslag    protected function _modifyUser(){
59900d58927SMichael Hamann        global $conf, $INPUT;
6009ec82636SAndreas Gohr
601634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
60282fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
6030440ff15Schris
60426fb387bSchris        // get currently valid  user data
605786dfb0eSGerrit Uitslag        $olduser = $this->_auth->cleanUser(preg_replace('/.*[:\/]/','',$INPUT->str('userid_old')));
606073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
607073766c6Smatthiasgrimm
60826fb387bSchris        // get new user data subject to change
609073766c6Smatthiasgrimm        list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser();
610073766c6Smatthiasgrimm        if (empty($newuser)) return false;
6110440ff15Schris
6120440ff15Schris        $changes = array();
613073766c6Smatthiasgrimm        if ($newuser != $olduser) {
61426fb387bSchris
61526fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
61626fb387bSchris                msg($this->lang['update_fail'],-1);
61726fb387bSchris                return false;
61826fb387bSchris            }
61926fb387bSchris
62026fb387bSchris            // check if $newuser already exists
621073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
622073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
623a6858c6aSchris                $re_edit = true;
6240440ff15Schris            } else {
625073766c6Smatthiasgrimm                $changes['user'] = $newuser;
6260440ff15Schris            }
62793eefc2fSAndreas Gohr        }
62893eefc2fSAndreas Gohr
62993eefc2fSAndreas Gohr        // generate password if left empty and notification is on
63000d58927SMichael Hamann        if($INPUT->has('usernotify') && empty($newpass)){
6318a285f7fSAndreas Gohr            $newpass = auth_pwgen($olduser);
6320440ff15Schris        }
6330440ff15Schris
63426fb387bSchris        if (!empty($newpass) && $this->_auth->canDo('modPass'))
635073766c6Smatthiasgrimm          $changes['pass'] = $newpass;
63626fb387bSchris        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name'])
637073766c6Smatthiasgrimm          $changes['name'] = $newname;
63826fb387bSchris        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail'])
639073766c6Smatthiasgrimm          $changes['mail'] = $newmail;
64026fb387bSchris        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps'])
641073766c6Smatthiasgrimm          $changes['grps'] = $newgrps;
6420440ff15Schris
6437d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
6440440ff15Schris            msg($this->lang['update_ok'],1);
645a6858c6aSchris
64600d58927SMichael Hamann            if ($INPUT->has('usernotify') && $newpass) {
647a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
648a6858c6aSchris                $this->_notifyUser($notify,$newpass);
649a6858c6aSchris            }
650a6858c6aSchris
6519ec82636SAndreas Gohr            // invalidate all sessions
6529ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6539ec82636SAndreas Gohr
6540440ff15Schris        } else {
6550440ff15Schris            msg($this->lang['update_fail'],-1);
6560440ff15Schris        }
65778c7c8c9Schris
658a6858c6aSchris        if (!empty($re_edit)) {
659a6858c6aSchris            $this->_editUser($olduser);
6600440ff15Schris        }
6610440ff15Schris
662a6858c6aSchris        return $ok;
663a6858c6aSchris    }
664a6858c6aSchris
665a6858c6aSchris    /**
666c5a7c0c6SGerrit Uitslag     * Send password change notification email
667c5a7c0c6SGerrit Uitslag     *
668c5a7c0c6SGerrit Uitslag     * @param string $user         id of user
669c5a7c0c6SGerrit Uitslag     * @param string $password     plain text
670c5a7c0c6SGerrit Uitslag     * @param bool   $status_alert whether status alert should be shown
671c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
672a6858c6aSchris     */
6733712ca9aSGerrit Uitslag    protected function _notifyUser($user, $password, $status_alert=true) {
674a6858c6aSchris
675a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
676328143f8SChristopher Smith            if ($status_alert) {
677a6858c6aSchris                msg($this->lang['notify_ok'], 1);
678328143f8SChristopher Smith            }
679a6858c6aSchris        } else {
680328143f8SChristopher Smith            if ($status_alert) {
681a6858c6aSchris                msg($this->lang['notify_fail'], -1);
682a6858c6aSchris            }
683328143f8SChristopher Smith        }
684a6858c6aSchris
685a6858c6aSchris        return $sent;
686a6858c6aSchris    }
687a6858c6aSchris
688a6858c6aSchris    /**
689c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
690a6858c6aSchris     *
691c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
692a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
6930440ff15Schris     */
6943712ca9aSGerrit Uitslag    protected function _retrieveUser($clean=true) {
695c5a7c0c6SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
6967441e340SAndreas Gohr        global $auth;
697fbfbbe8aSHakan Sandell        global $INPUT;
6980440ff15Schris
699fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
700fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
701fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
702fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
703fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
7040440ff15Schris
7057441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
7067441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
7077441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
7087441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
7097441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
7100440ff15Schris
7110440ff15Schris        return $user;
7120440ff15Schris    }
7130440ff15Schris
714c5a7c0c6SGerrit Uitslag    /**
715c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
716c5a7c0c6SGerrit Uitslag     *
717c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
718c5a7c0c6SGerrit Uitslag     */
7193712ca9aSGerrit Uitslag    protected function _setFilter($op) {
7200440ff15Schris
7210440ff15Schris        $this->_filter = array();
7220440ff15Schris
7230440ff15Schris        if ($op == 'new') {
7240440ff15Schris            list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
7250440ff15Schris
7260440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
7270440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
7280440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
7290440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
7300440ff15Schris        }
7310440ff15Schris    }
7320440ff15Schris
733c5a7c0c6SGerrit Uitslag    /**
734c5a7c0c6SGerrit Uitslag     * Get the current search terms
735c5a7c0c6SGerrit Uitslag     *
736c5a7c0c6SGerrit Uitslag     * @return array
737c5a7c0c6SGerrit Uitslag     */
7383712ca9aSGerrit Uitslag    protected function _retrieveFilter() {
739fbfbbe8aSHakan Sandell        global $INPUT;
7400440ff15Schris
741fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
7420440ff15Schris
7430440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
7440440ff15Schris        $filter = array();
7450440ff15Schris
7460440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
7470440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
7480440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
7490440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
7500440ff15Schris
7510440ff15Schris        return $filter;
7520440ff15Schris    }
7530440ff15Schris
754c5a7c0c6SGerrit Uitslag    /**
755c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
756c5a7c0c6SGerrit Uitslag     */
7573712ca9aSGerrit Uitslag    protected function _validatePagination() {
7580440ff15Schris
7590440ff15Schris        if ($this->_start >= $this->_user_total) {
7600440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
7610440ff15Schris        }
7620440ff15Schris        if ($this->_start < 0) $this->_start = 0;
7630440ff15Schris
7640440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
7650440ff15Schris    }
7660440ff15Schris
767c5a7c0c6SGerrit Uitslag    /**
768c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
769c5a7c0c6SGerrit Uitslag     *
770c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
7710440ff15Schris     */
7723712ca9aSGerrit Uitslag    protected function _pagination() {
7730440ff15Schris
77451d94d49Schris        $disabled = 'disabled="disabled"';
77551d94d49Schris
77651d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
77751d94d49Schris
77851d94d49Schris        if ($this->_user_total == -1) {
77951d94d49Schris            $buttons['last'] = $disabled;
78051d94d49Schris            $buttons['next'] = '';
78151d94d49Schris        } else {
78251d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
78351d94d49Schris        }
7840440ff15Schris
7850440ff15Schris        return $buttons;
7860440ff15Schris    }
7875c967d3dSChristopher Smith
788c5a7c0c6SGerrit Uitslag    /**
789c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
7905c967d3dSChristopher Smith     */
7913712ca9aSGerrit Uitslag    protected function _export() {
7925c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
7935c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
7945c967d3dSChristopher Smith        $column_headings = array(
7955c967d3dSChristopher Smith            $this->lang["user_id"],
7965c967d3dSChristopher Smith            $this->lang["user_name"],
7975c967d3dSChristopher Smith            $this->lang["user_mail"],
7985c967d3dSChristopher Smith            $this->lang["user_groups"]
7995c967d3dSChristopher Smith        );
8005c967d3dSChristopher Smith
8015c967d3dSChristopher Smith        // ==============================================================================================
8025c967d3dSChristopher Smith        // GENERATE OUTPUT
8035c967d3dSChristopher Smith        // normal headers for downloading...
8045c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
8055c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
8065c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
8075c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
8085c967d3dSChristopher Smith
8095c967d3dSChristopher Smith        // output the csv
8105c967d3dSChristopher Smith        $fd = fopen('php://output','w');
8115c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
8125c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
8135c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
8145c967d3dSChristopher Smith            fputcsv($fd, $line);
8155c967d3dSChristopher Smith        }
8165c967d3dSChristopher Smith        fclose($fd);
817b2c01466SChristopher Smith        if (defined('DOKU_UNITTEST')){ return; }
818b2c01466SChristopher Smith
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     *
8275ba64050SChristopher Smith     * @return bool whether successful
828ae1afd2fSChristopher Smith     */
8293712ca9aSGerrit Uitslag    protected 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.
835b2c01466SChristopher Smith        if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_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                }
850*c9454ee3SChristopher Smith                $raw = $this->_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++;
872e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
873ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
874ae1afd2fSChristopher Smith                }
875ae1afd2fSChristopher Smith            }
876ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
877ae1afd2fSChristopher Smith            if ($import_fail_count) {
878ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
879ae1afd2fSChristopher Smith            }
880ae1afd2fSChristopher Smith        } else {
881ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
882ae1afd2fSChristopher Smith        }
883ae1afd2fSChristopher Smith
884ae1afd2fSChristopher Smith        // save import failures into the session
885ae1afd2fSChristopher Smith        if (!headers_sent()) {
886ae1afd2fSChristopher Smith            session_start();
887ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
888ae1afd2fSChristopher Smith            session_write_close();
889ae1afd2fSChristopher Smith        }
890c5a7c0c6SGerrit Uitslag        return true;
891ae1afd2fSChristopher Smith    }
892ae1afd2fSChristopher Smith
893c5a7c0c6SGerrit Uitslag    /**
894786dfb0eSGerrit Uitslag     * Returns cleaned user data
895c5a7c0c6SGerrit Uitslag     *
896c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
897c5a7c0c6SGerrit Uitslag     * @param $error
898c5a7c0c6SGerrit Uitslag     * @return array|bool cleaned data or false
899c5a7c0c6SGerrit Uitslag     */
9003712ca9aSGerrit Uitslag    protected function _cleanImportUser($candidate, & $error){
901ae1afd2fSChristopher Smith        global $INPUT;
902ae1afd2fSChristopher Smith
903ae1afd2fSChristopher Smith        // kludgy ....
904ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
905ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
906ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
907ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
908ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
909ae1afd2fSChristopher Smith
910ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
911ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
912ae1afd2fSChristopher Smith        if (empty($user)) {
913ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
914ae1afd2fSChristopher Smith            return false;
915ae1afd2fSChristopher Smith        }
916ae1afd2fSChristopher Smith
917ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
918ae1afd2fSChristopher Smith
919ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
920ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
921ae1afd2fSChristopher Smith            return false;
922ae1afd2fSChristopher Smith        }
923ae1afd2fSChristopher Smith
924328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
925328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
926ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
927ae1afd2fSChristopher Smith                return false;
928ae1afd2fSChristopher Smith            }
929328143f8SChristopher Smith        } else {
930328143f8SChristopher Smith            if (!empty($mail)) {
931328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
932328143f8SChristopher Smith                return false;
933328143f8SChristopher Smith            }
934328143f8SChristopher Smith        }
935ae1afd2fSChristopher Smith
936ae1afd2fSChristopher Smith        return $cleaned;
937ae1afd2fSChristopher Smith    }
938ae1afd2fSChristopher Smith
939c5a7c0c6SGerrit Uitslag    /**
940c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
941c5a7c0c6SGerrit Uitslag     *
942c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
943c5a7c0c6SGerrit Uitslag     *
944c5a7c0c6SGerrit Uitslag     * @param array  $user   data of user
945c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
9465ba64050SChristopher Smith     * @return bool whether successful
947c5a7c0c6SGerrit Uitslag     */
9483712ca9aSGerrit Uitslag    protected function _addImportUser($user, & $error){
949ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
950ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
951ae1afd2fSChristopher Smith            return false;
952ae1afd2fSChristopher Smith        }
953ae1afd2fSChristopher Smith
954ae1afd2fSChristopher Smith        return true;
955ae1afd2fSChristopher Smith    }
956ae1afd2fSChristopher Smith
957c5a7c0c6SGerrit Uitslag    /**
958c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
959c5a7c0c6SGerrit Uitslag     */
9603712ca9aSGerrit Uitslag    protected function _downloadImportFailures(){
961ae1afd2fSChristopher Smith
962ae1afd2fSChristopher Smith        // ==============================================================================================
963ae1afd2fSChristopher Smith        // GENERATE OUTPUT
964ae1afd2fSChristopher Smith        // normal headers for downloading...
965ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
966ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
967ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
968ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
969ae1afd2fSChristopher Smith
970ae1afd2fSChristopher Smith        // output the csv
971ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
972c5a7c0c6SGerrit Uitslag        foreach ($this->_import_failures as $fail) {
973ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
974ae1afd2fSChristopher Smith        }
975ae1afd2fSChristopher Smith        fclose($fd);
976ae1afd2fSChristopher Smith        die;
977ae1afd2fSChristopher Smith    }
978ae1afd2fSChristopher Smith
979b2c01466SChristopher Smith    /**
980b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
981b2c01466SChristopher Smith     */
982b2c01466SChristopher Smith    protected function _isUploadedFile($file) {
983b2c01466SChristopher Smith        return is_uploaded_file($file);
984b2c01466SChristopher Smith    }
985b2c01466SChristopher Smith
986*c9454ee3SChristopher Smith    /**
987*c9454ee3SChristopher Smith     * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
988*c9454ee3SChristopher Smith     *
989*c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
990*c9454ee3SChristopher Smith     *                also associated unit test & mock access method
991*c9454ee3SChristopher Smith     */
992*c9454ee3SChristopher Smith    protected function _getcsv($csv) {
993*c9454ee3SChristopher Smith        return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
994*c9454ee3SChristopher Smith    }
995*c9454ee3SChristopher Smith
996*c9454ee3SChristopher Smith    /**
997*c9454ee3SChristopher Smith     * replacement str_getcsv() function for php < 5.3
998*c9454ee3SChristopher Smith     * loosely based on www.php.net/str_getcsv#88311
999*c9454ee3SChristopher Smith     *
1000*c9454ee3SChristopher Smith     * @deprecated    remove when dokuwiki php requirement increases to 5.3+
1001*c9454ee3SChristopher Smith     */
1002*c9454ee3SChristopher Smith    protected function str_getcsv($str) {
1003*c9454ee3SChristopher Smith        $fp = fopen("php://temp/maxmemory:1048576", 'r+');    // 1MiB
1004*c9454ee3SChristopher Smith        fputs($fp, $str);
1005*c9454ee3SChristopher Smith        rewind($fp);
1006*c9454ee3SChristopher Smith
1007*c9454ee3SChristopher Smith        $data = fgetcsv($fp);
1008*c9454ee3SChristopher Smith
1009*c9454ee3SChristopher Smith        fclose($fp);
1010*c9454ee3SChristopher Smith        return $data;
1011*c9454ee3SChristopher Smith    }
10120440ff15Schris}
1013