xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision a102b1759a790dbefcb92ac83a988bf6b4ae7f08)
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
240440ff15Schris    var $_auth = null;        // auth object
250440ff15Schris    var $_user_total = 0;     // number of registered users
260440ff15Schris    var $_filter = array();   // user selection filter(s)
270440ff15Schris    var $_start = 0;          // index of first user to be displayed
280440ff15Schris    var $_last = 0;           // index of the last user to be displayed
290440ff15Schris    var $_pagesize = 20;      // number of users to list on one page
3078c7c8c9Schris    var $_edit_user = '';     // set to user selected for editing
3178c7c8c9Schris    var $_edit_userdata = array();
3251d94d49Schris    var $_disabled = '';      // if disabled set to explanatory string
33ae1afd2fSChristopher Smith    var $_import_failures = array();
340440ff15Schris
350440ff15Schris    /**
360440ff15Schris     * Constructor
370440ff15Schris     */
380440ff15Schris    function admin_plugin_usermanager(){
390440ff15Schris        global $auth;
400440ff15Schris
410440ff15Schris        $this->setupLocale();
4251d94d49Schris
4351d94d49Schris        if (!isset($auth)) {
4451d94d49Schris            $this->disabled = $this->lang['noauth'];
4582fd59b6SAndreas Gohr        } else if (!$auth->canDo('getUsers')) {
46e165ecfdSchris            $this->disabled = $this->lang['nosupport'];
4751d94d49Schris        } else {
4851d94d49Schris
4951d94d49Schris            // we're good to go
5051d94d49Schris            $this->_auth = & $auth;
5151d94d49Schris
5251d94d49Schris        }
53ae1afd2fSChristopher Smith
54ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
55ae1afd2fSChristopher Smith        if ($_SESSION['import_failures']){
56ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
57ae1afd2fSChristopher Smith        }
580440ff15Schris    }
590440ff15Schris
600440ff15Schris     /**
610440ff15Schris      * return prompt for admin menu
620440ff15Schris      */
630440ff15Schris    function getMenuText($language) {
640440ff15Schris
650440ff15Schris        if (!is_null($this->_auth))
660440ff15Schris          return parent::getMenuText($language);
670440ff15Schris
68e165ecfdSchris        return $this->getLang('menu').' '.$this->disabled;
690440ff15Schris    }
700440ff15Schris
710440ff15Schris    /**
720440ff15Schris     * return sort order for position in admin menu
730440ff15Schris     */
740440ff15Schris    function getMenuSort() {
750440ff15Schris        return 2;
760440ff15Schris    }
770440ff15Schris
780440ff15Schris    /**
790440ff15Schris     * handle user request
800440ff15Schris     */
810440ff15Schris    function handle() {
8200d58927SMichael Hamann        global $INPUT;
830440ff15Schris        if (is_null($this->_auth)) return false;
840440ff15Schris
850440ff15Schris        // extract the command and any specific parameters
860440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
8700d58927SMichael Hamann        $fn   = $INPUT->param('fn');
880440ff15Schris
890440ff15Schris        if (is_array($fn)) {
900440ff15Schris            $cmd = key($fn);
910440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
920440ff15Schris        } else {
930440ff15Schris            $cmd = $fn;
940440ff15Schris            $param = null;
950440ff15Schris        }
960440ff15Schris
970440ff15Schris        if ($cmd != "search") {
9800d58927SMichael Hamann            $this->_start = $INPUT->int('start', 0);
990440ff15Schris            $this->_filter = $this->_retrieveFilter();
1000440ff15Schris        }
1010440ff15Schris
1020440ff15Schris        switch($cmd){
1030440ff15Schris            case "add"    : $this->_addUser(); break;
1040440ff15Schris            case "delete" : $this->_deleteUser(); break;
1050440ff15Schris            case "modify" : $this->_modifyUser(); break;
10678c7c8c9Schris            case "edit"   : $this->_editUser($param); break;
1070440ff15Schris            case "search" : $this->_setFilter($param);
1080440ff15Schris                            $this->_start = 0;
1090440ff15Schris                            break;
1105c967d3dSChristopher Smith            case "export" : $this->_export(); break;
111ae1afd2fSChristopher Smith            case "import" : $this->_import(); break;
112ae1afd2fSChristopher Smith            case "importfails" : $this->_downloadImportFailures(); break;
1130440ff15Schris        }
1140440ff15Schris
11551d94d49Schris        $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1;
1160440ff15Schris
1170440ff15Schris        // page handling
1180440ff15Schris        switch($cmd){
1190440ff15Schris            case 'start' : $this->_start = 0; break;
1200440ff15Schris            case 'prev'  : $this->_start -= $this->_pagesize; break;
1210440ff15Schris            case 'next'  : $this->_start += $this->_pagesize; break;
1220440ff15Schris            case 'last'  : $this->_start = $this->_user_total; break;
1230440ff15Schris        }
1240440ff15Schris        $this->_validatePagination();
1250440ff15Schris    }
1260440ff15Schris
1270440ff15Schris    /**
1280440ff15Schris     * output appropriate html
1290440ff15Schris     */
1300440ff15Schris    function html() {
1310440ff15Schris        global $ID;
1320440ff15Schris
1330440ff15Schris        if(is_null($this->_auth)) {
1340440ff15Schris            print $this->lang['badauth'];
1350440ff15Schris            return false;
1360440ff15Schris        }
1370440ff15Schris
1380440ff15Schris        $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter);
1390440ff15Schris        $users = array_keys($user_list);
1400440ff15Schris
1410440ff15Schris        $page_buttons = $this->_pagination();
14282fd59b6SAndreas Gohr        $delete_disable = $this->_auth->canDo('delUser') ? '' : 'disabled="disabled"';
1430440ff15Schris
14477d19185SAndreas Gohr        $editable = $this->_auth->canDo('UserMod');
1455c967d3dSChristopher Smith        $export_label = empty($this->_filter) ? $this->lang['export_all'] : $this->lang[export_filtered];
1466154103cSmatthiasgrimm
1470440ff15Schris        print $this->locale_xhtml('intro');
1480440ff15Schris        print $this->locale_xhtml('list');
1490440ff15Schris
15058dde80dSAnika Henke        ptln("<div id=\"user__manager\">");
15158dde80dSAnika Henke        ptln("<div class=\"level2\">");
1520440ff15Schris
15367019d15Schris        if ($this->_user_total > 0) {
1540440ff15Schris            ptln("<p>".sprintf($this->lang['summary'],$this->_start+1,$this->_last,$this->_user_total,$this->_auth->getUserCount())."</p>");
1550440ff15Schris        } else {
156*a102b175SGerrit Uitslag            if($this->_user_total < 0) {
157*a102b175SGerrit Uitslag                $allUserTotal = 0;
158*a102b175SGerrit Uitslag            } else {
159*a102b175SGerrit Uitslag                $allUserTotal = $this->_auth->getUserCount();
160*a102b175SGerrit Uitslag            }
161*a102b175SGerrit Uitslag            ptln("<p>".sprintf($this->lang['nonefound'], $allUserTotal)."</p>");
1620440ff15Schris        }
1630440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
164634d7150SAndreas Gohr        formSecurityToken();
165c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1660440ff15Schris        ptln("  <table class=\"inline\">");
1670440ff15Schris        ptln("    <thead>");
1680440ff15Schris        ptln("      <tr>");
169e260f93bSAnika 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>");
1700440ff15Schris        ptln("      </tr>");
1710440ff15Schris
1720440ff15Schris        ptln("      <tr>");
1732365d73dSAnika 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>");
174a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
175a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
176a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
177a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
1780440ff15Schris        ptln("      </tr>");
1790440ff15Schris        ptln("    </thead>");
1800440ff15Schris
1810440ff15Schris        if ($this->_user_total) {
1820440ff15Schris            ptln("    <tbody>");
1830440ff15Schris            foreach ($user_list as $user => $userinfo) {
1840440ff15Schris                extract($userinfo);
1850440ff15Schris                $groups = join(', ',$grps);
186a2c0246eSAnika Henke                ptln("    <tr class=\"user_info\">");
1870440ff15Schris                ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
1882365d73dSAnika Henke                if ($editable) {
18977d19185SAndreas Gohr                    ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
19077d19185SAndreas Gohr                                                           'do' => 'admin',
19177d19185SAndreas Gohr                                                           'page' => 'usermanager',
19277d19185SAndreas Gohr                                                           'sectok' => getSecurityToken())).
19377d19185SAndreas Gohr                         "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
1942365d73dSAnika Henke                } else {
1952365d73dSAnika Henke                    ptln("    <td>".hsc($user)."</td>");
1962365d73dSAnika Henke                }
1972365d73dSAnika Henke                ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
1980440ff15Schris                ptln("    </tr>");
1990440ff15Schris            }
2000440ff15Schris            ptln("    </tbody>");
2010440ff15Schris        }
2020440ff15Schris
2030440ff15Schris        ptln("    <tbody>");
2042365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
205a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
206a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2070440ff15Schris        ptln("        </span>");
208a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
209a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
210a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
211a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
212a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2130440ff15Schris        ptln("        </span>");
2145c967d3dSChristopher Smith        if (!empty($this->_filter)) {
215a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2165c967d3dSChristopher Smith        }
2175c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2185164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2195164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
220daf4ca4eSAnika Henke
221daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
222daf4ca4eSAnika Henke
2230440ff15Schris        ptln("      </td></tr>");
2240440ff15Schris        ptln("    </tbody>");
2250440ff15Schris        ptln("  </table>");
226c7b28ffdSAnika Henke        ptln("  </div>");
2270440ff15Schris
2280440ff15Schris        ptln("</form>");
2290440ff15Schris        ptln("</div>");
2300440ff15Schris
231a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2320440ff15Schris
23382fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2340440ff15Schris            ptln("<div".$style.">");
2350440ff15Schris            print $this->locale_xhtml('add');
2360440ff15Schris            ptln("  <div class=\"level2\">");
2370440ff15Schris
23878c7c8c9Schris            $this->_htmlUserForm('add',null,array(),4);
2390440ff15Schris
2400440ff15Schris            ptln("  </div>");
2410440ff15Schris            ptln("</div>");
2420440ff15Schris        }
2430440ff15Schris
24482fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
245c632fc69SAndreas Gohr            ptln("<div".$style." id=\"scroll__here\">");
2460440ff15Schris            print $this->locale_xhtml('edit');
2470440ff15Schris            ptln("  <div class=\"level2\">");
2480440ff15Schris
24978c7c8c9Schris            $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2500440ff15Schris
2510440ff15Schris            ptln("  </div>");
2520440ff15Schris            ptln("</div>");
2530440ff15Schris        }
254ae1afd2fSChristopher Smith
255ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
256ae1afd2fSChristopher Smith            $this->_htmlImportForm();
257ae1afd2fSChristopher Smith        }
25858dde80dSAnika Henke        ptln("</div>");
2590440ff15Schris    }
2600440ff15Schris
26182fd59b6SAndreas Gohr
26282fd59b6SAndreas Gohr    /**
26382fd59b6SAndreas Gohr     * @todo disable fields which the backend can't change
26482fd59b6SAndreas Gohr     */
26578c7c8c9Schris    function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
266a6858c6aSchris        global $conf;
267bb4866bdSchris        global $ID;
26878c7c8c9Schris
26978c7c8c9Schris        $name = $mail = $groups = '';
270a6858c6aSchris        $notes = array();
2710440ff15Schris
2720440ff15Schris        if ($user) {
27378c7c8c9Schris            extract($userdata);
27478c7c8c9Schris            if (!empty($grps)) $groups = join(',',$grps);
275a6858c6aSchris        } else {
276a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2770440ff15Schris        }
2780440ff15Schris
2790440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
280634d7150SAndreas Gohr        formSecurityToken();
281c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
2820440ff15Schris        ptln("  <table class=\"inline\">",$indent);
2830440ff15Schris        ptln("    <thead>",$indent);
2840440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
2850440ff15Schris        ptln("    </thead>",$indent);
2860440ff15Schris        ptln("    <tbody>",$indent);
28726fb387bSchris
28826fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
28926fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
29026fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
29126fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
29226fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
29326fb387bSchris
294a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
295ee9498f5SChristopher Smith            if ($cmd == 'add') {
296c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
297ee9498f5SChristopher Smith            }
298a6858c6aSchris            if ($user) {
299a6858c6aSchris                $notes[] = $this->lang['note_notify'];
300a6858c6aSchris            }
301a6858c6aSchris
302a6858c6aSchris            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);
303a6858c6aSchris        }
304a6858c6aSchris
3050440ff15Schris        ptln("    </tbody>",$indent);
3060440ff15Schris        ptln("    <tbody>",$indent);
3070440ff15Schris        ptln("      <tr>",$indent);
3080440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3090440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3100440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3110440ff15Schris
3120440ff15Schris        // save current $user, we need this to access details if the name is changed
3130440ff15Schris        if ($user)
3140440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3150440ff15Schris
3160440ff15Schris        $this->_htmlFilterSettings($indent+10);
3170440ff15Schris
318a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3190440ff15Schris        ptln("        </td>",$indent);
3200440ff15Schris        ptln("      </tr>",$indent);
3210440ff15Schris        ptln("    </tbody>",$indent);
3220440ff15Schris        ptln("  </table>",$indent);
32345c19902SChristopher Smith
32445c19902SChristopher Smith        if ($notes) {
32545c19902SChristopher Smith            ptln("    <ul class=\"notes\">");
32645c19902SChristopher Smith            foreach ($notes as $note) {
32745c19902SChristopher Smith                ptln("      <li><span class=\"li\">".$note."</span></li>",$indent);
32845c19902SChristopher Smith            }
32945c19902SChristopher Smith            ptln("    </ul>");
33045c19902SChristopher Smith        }
331c7b28ffdSAnika Henke        ptln("  </div>",$indent);
3320440ff15Schris        ptln("</form>",$indent);
3330440ff15Schris    }
3340440ff15Schris
33526fb387bSchris    function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3367de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3377de12fceSAndreas Gohr        echo str_pad('',$indent);
3387de12fceSAndreas Gohr
339d796a891SAndreas Gohr        if($name == 'userpass'){
340d796a891SAndreas Gohr            $fieldtype = 'password';
341d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3427b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3437b3674bdSChristopher Smith            $fieldtype = 'email';
3447b3674bdSChristopher Smith            $autocomp  = '';
345d796a891SAndreas Gohr        }else{
346d796a891SAndreas Gohr            $fieldtype = 'text';
347d796a891SAndreas Gohr            $autocomp  = '';
348d796a891SAndreas Gohr        }
349d796a891SAndreas Gohr
350ee54059bSTimo Voipio
3517de12fceSAndreas Gohr        echo "<tr $class>";
3527de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3537de12fceSAndreas Gohr        echo "<td>";
3547de12fceSAndreas Gohr        if($cando){
355d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3567de12fceSAndreas Gohr        }else{
3577de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
358ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3597de12fceSAndreas Gohr        }
3607de12fceSAndreas Gohr        echo "</td>";
3617de12fceSAndreas Gohr        echo "</tr>";
36226fb387bSchris    }
36326fb387bSchris
3640440ff15Schris    function _htmlFilter($key) {
3650440ff15Schris        if (empty($this->_filter)) return '';
3660440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
3670440ff15Schris    }
3680440ff15Schris
3690440ff15Schris    function _htmlFilterSettings($indent=0) {
3700440ff15Schris
3710440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
3720440ff15Schris
3730440ff15Schris        foreach ($this->_filter as $key => $filter) {
3740440ff15Schris            ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
3750440ff15Schris        }
3760440ff15Schris    }
3770440ff15Schris
378ae1afd2fSChristopher Smith    function _htmlImportForm($indent=0) {
379ae1afd2fSChristopher Smith        global $ID;
380ae1afd2fSChristopher Smith
381ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
382ae1afd2fSChristopher Smith
383ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
384ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
385ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
386ae1afd2fSChristopher Smith        formSecurityToken();
387ae1afd2fSChristopher Smith        ptln('    <label>User list file (csv):  <input type="file" name="import" /></label>',$indent);
388ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
389ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
390ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
391ae1afd2fSChristopher Smith
392ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
393ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
394ae1afd2fSChristopher Smith        ptln('</div>');
395ae1afd2fSChristopher Smith
396ae1afd2fSChristopher Smith        // list failures from the previous import
397ae1afd2fSChristopher Smith        if ($this->_import_failures) {
398ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
399ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
400ae1afd2fSChristopher Smith            ptln('  <h3>Most Recent Import - Failures</h3>');
401ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
402ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
403ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
404ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
405ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
406ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
407ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
408ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
409ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
410ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
411ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
412ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
413ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
414ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
415ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
416ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
417ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
418ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
419ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
420ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
421ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
422ae1afd2fSChristopher Smith            }
423ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
424ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
425ae1afd2fSChristopher Smith            ptln('  <p><a href="'.$failure_download_link.'">Download Failures as CSV for correction</a></p>');
426ae1afd2fSChristopher Smith            ptln('</div>');
427ae1afd2fSChristopher Smith        }
428ae1afd2fSChristopher Smith
429ae1afd2fSChristopher Smith    }
430ae1afd2fSChristopher Smith
4310440ff15Schris    function _addUser(){
43200d58927SMichael Hamann        global $INPUT;
433634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
43482fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4350440ff15Schris
4360440ff15Schris        list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser();
4370440ff15Schris        if (empty($user)) return false;
4386733c4d7SChris Smith
4396733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
440c3f4fb63SGina Haeussge            if (empty($pass)){
44100d58927SMichael Hamann                if($INPUT->has('usernotify')){
4428a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
443c3f4fb63SGina Haeussge                } else {
44460b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
44560b9901bSAndreas Gohr                    return false;
44660b9901bSAndreas Gohr                }
4476733c4d7SChris Smith            }
4486733c4d7SChris Smith        } else {
4496733c4d7SChris Smith            if (!empty($pass)){
4506733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4516733c4d7SChris Smith                return false;
4526733c4d7SChris Smith            }
4536733c4d7SChris Smith        }
4546733c4d7SChris Smith
4556733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
4566733c4d7SChris Smith            if (empty($name)){
4576733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4586733c4d7SChris Smith                return false;
4596733c4d7SChris Smith            }
4606733c4d7SChris Smith        } else {
4616733c4d7SChris Smith            if (!empty($name)){
4626733c4d7SChris Smith                return false;
4636733c4d7SChris Smith            }
4646733c4d7SChris Smith        }
4656733c4d7SChris Smith
4666733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
4676733c4d7SChris Smith            if (empty($mail)){
4686733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
4696733c4d7SChris Smith                return false;
4706733c4d7SChris Smith            }
4716733c4d7SChris Smith        } else {
4726733c4d7SChris Smith            if (!empty($mail)){
4736733c4d7SChris Smith                return false;
4746733c4d7SChris Smith            }
4756733c4d7SChris Smith        }
4760440ff15Schris
4777d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
478a6858c6aSchris
479a6858c6aSchris            msg($this->lang['add_ok'], 1);
480a6858c6aSchris
48100d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
482a6858c6aSchris                $this->_notifyUser($user,$pass);
483a6858c6aSchris            }
484a6858c6aSchris        } else {
48560b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
486a6858c6aSchris        }
487a6858c6aSchris
488a6858c6aSchris        return $ok;
4890440ff15Schris    }
4900440ff15Schris
4910440ff15Schris    /**
4920440ff15Schris     * Delete user
4930440ff15Schris     */
4940440ff15Schris    function _deleteUser(){
49500d58927SMichael Hamann        global $conf, $INPUT;
4969ec82636SAndreas Gohr
497634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
49882fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
4990440ff15Schris
50000d58927SMichael Hamann        $selected = $INPUT->arr('delete');
50100d58927SMichael Hamann        if (empty($selected)) return false;
5020440ff15Schris        $selected = array_keys($selected);
5030440ff15Schris
504c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
505c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
506c9a8f912SMichael Klier            return false;
507c9a8f912SMichael Klier        }
508c9a8f912SMichael Klier
5097d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
5100440ff15Schris        if ($count == count($selected)) {
5110440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
5120440ff15Schris            msg("$text.", 1);
5130440ff15Schris        } else {
5140440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5150440ff15Schris            $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5160440ff15Schris            msg("$part1, $part2",-1);
5170440ff15Schris        }
51878c7c8c9Schris
5199ec82636SAndreas Gohr        // invalidate all sessions
5209ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5219ec82636SAndreas Gohr
52278c7c8c9Schris        return true;
52378c7c8c9Schris    }
52478c7c8c9Schris
52578c7c8c9Schris    /**
52678c7c8c9Schris     * Edit user (a user has been selected for editing)
52778c7c8c9Schris     */
52878c7c8c9Schris    function _editUser($param) {
529634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
53078c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
53178c7c8c9Schris
53278c7c8c9Schris        $user = cleanID(preg_replace('/.*:/','',$param));
53378c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
53478c7c8c9Schris
53578c7c8c9Schris        // no user found?
53678c7c8c9Schris        if (!$userdata) {
53778c7c8c9Schris            msg($this->lang['edit_usermissing'],-1);
53878c7c8c9Schris            return false;
53978c7c8c9Schris        }
54078c7c8c9Schris
54178c7c8c9Schris        $this->_edit_user = $user;
54278c7c8c9Schris        $this->_edit_userdata = $userdata;
54378c7c8c9Schris
54478c7c8c9Schris        return true;
5450440ff15Schris    }
5460440ff15Schris
5470440ff15Schris    /**
548ef64b3a2Schris     * Modify user (modified user data has been recieved)
5490440ff15Schris     */
5500440ff15Schris    function _modifyUser(){
55100d58927SMichael Hamann        global $conf, $INPUT;
5529ec82636SAndreas Gohr
553634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
55482fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
5550440ff15Schris
55626fb387bSchris        // get currently valid  user data
55700d58927SMichael Hamann        $olduser = cleanID(preg_replace('/.*:/','',$INPUT->str('userid_old')));
558073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
559073766c6Smatthiasgrimm
56026fb387bSchris        // get new user data subject to change
561073766c6Smatthiasgrimm        list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser();
562073766c6Smatthiasgrimm        if (empty($newuser)) return false;
5630440ff15Schris
5640440ff15Schris        $changes = array();
565073766c6Smatthiasgrimm        if ($newuser != $olduser) {
56626fb387bSchris
56726fb387bSchris            if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
56826fb387bSchris                msg($this->lang['update_fail'],-1);
56926fb387bSchris                return false;
57026fb387bSchris            }
57126fb387bSchris
57226fb387bSchris            // check if $newuser already exists
573073766c6Smatthiasgrimm            if ($this->_auth->getUserData($newuser)) {
574073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'],$newuser),-1);
575a6858c6aSchris                $re_edit = true;
5760440ff15Schris            } else {
577073766c6Smatthiasgrimm                $changes['user'] = $newuser;
5780440ff15Schris            }
57993eefc2fSAndreas Gohr        }
58093eefc2fSAndreas Gohr
58193eefc2fSAndreas Gohr        // generate password if left empty and notification is on
58200d58927SMichael Hamann        if($INPUT->has('usernotify') && empty($newpass)){
5838a285f7fSAndreas Gohr            $newpass = auth_pwgen($olduser);
5840440ff15Schris        }
5850440ff15Schris
58626fb387bSchris        if (!empty($newpass) && $this->_auth->canDo('modPass'))
587073766c6Smatthiasgrimm          $changes['pass'] = $newpass;
58826fb387bSchris        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name'])
589073766c6Smatthiasgrimm          $changes['name'] = $newname;
59026fb387bSchris        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail'])
591073766c6Smatthiasgrimm          $changes['mail'] = $newmail;
59226fb387bSchris        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps'])
593073766c6Smatthiasgrimm          $changes['grps'] = $newgrps;
5940440ff15Schris
5957d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
5960440ff15Schris            msg($this->lang['update_ok'],1);
597a6858c6aSchris
59800d58927SMichael Hamann            if ($INPUT->has('usernotify') && $newpass) {
599a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
600a6858c6aSchris                $this->_notifyUser($notify,$newpass);
601a6858c6aSchris            }
602a6858c6aSchris
6039ec82636SAndreas Gohr            // invalidate all sessions
6049ec82636SAndreas Gohr            io_saveFile($conf['cachedir'].'/sessionpurge',time());
6059ec82636SAndreas Gohr
6060440ff15Schris        } else {
6070440ff15Schris            msg($this->lang['update_fail'],-1);
6080440ff15Schris        }
60978c7c8c9Schris
610a6858c6aSchris        if (!empty($re_edit)) {
611a6858c6aSchris            $this->_editUser($olduser);
6120440ff15Schris        }
6130440ff15Schris
614a6858c6aSchris        return $ok;
615a6858c6aSchris    }
616a6858c6aSchris
617a6858c6aSchris    /**
618a6858c6aSchris     * send password change notification email
619a6858c6aSchris     */
620328143f8SChristopher Smith    function _notifyUser($user, $password, $status_alert=true) {
621a6858c6aSchris
622a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
623328143f8SChristopher Smith            if ($status_alert) {
624a6858c6aSchris                msg($this->lang['notify_ok'], 1);
625328143f8SChristopher Smith            }
626a6858c6aSchris        } else {
627328143f8SChristopher Smith            if ($status_alert) {
628a6858c6aSchris                msg($this->lang['notify_fail'], -1);
629a6858c6aSchris            }
630328143f8SChristopher Smith        }
631a6858c6aSchris
632a6858c6aSchris        return $sent;
633a6858c6aSchris    }
634a6858c6aSchris
635a6858c6aSchris    /**
6360440ff15Schris     * retrieve & clean user data from the form
637a6858c6aSchris     *
638a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
6390440ff15Schris     */
6400440ff15Schris    function _retrieveUser($clean=true) {
6417441e340SAndreas Gohr        global $auth;
642fbfbbe8aSHakan Sandell        global $INPUT;
6430440ff15Schris
644fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
645fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
646fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
647fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
648fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
6490440ff15Schris
6507441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
6517441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
6527441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
6537441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
6547441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
6550440ff15Schris
6560440ff15Schris        return $user;
6570440ff15Schris    }
6580440ff15Schris
6590440ff15Schris    function _setFilter($op) {
6600440ff15Schris
6610440ff15Schris        $this->_filter = array();
6620440ff15Schris
6630440ff15Schris        if ($op == 'new') {
6640440ff15Schris            list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
6650440ff15Schris
6660440ff15Schris            if (!empty($user)) $this->_filter['user'] = $user;
6670440ff15Schris            if (!empty($name)) $this->_filter['name'] = $name;
6680440ff15Schris            if (!empty($mail)) $this->_filter['mail'] = $mail;
6690440ff15Schris            if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
6700440ff15Schris        }
6710440ff15Schris    }
6720440ff15Schris
6730440ff15Schris    function _retrieveFilter() {
674fbfbbe8aSHakan Sandell        global $INPUT;
6750440ff15Schris
676fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
6770440ff15Schris
6780440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
6790440ff15Schris        $filter = array();
6800440ff15Schris
6810440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
6820440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
6830440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
6840440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
6850440ff15Schris
6860440ff15Schris        return $filter;
6870440ff15Schris    }
6880440ff15Schris
6890440ff15Schris    function _validatePagination() {
6900440ff15Schris
6910440ff15Schris        if ($this->_start >= $this->_user_total) {
6920440ff15Schris            $this->_start = $this->_user_total - $this->_pagesize;
6930440ff15Schris        }
6940440ff15Schris        if ($this->_start < 0) $this->_start = 0;
6950440ff15Schris
6960440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
6970440ff15Schris    }
6980440ff15Schris
6990440ff15Schris    /*
7000440ff15Schris     *  return an array of strings to enable/disable pagination buttons
7010440ff15Schris     */
7020440ff15Schris    function _pagination() {
7030440ff15Schris
70451d94d49Schris        $disabled = 'disabled="disabled"';
70551d94d49Schris
70651d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
70751d94d49Schris
70851d94d49Schris        if ($this->_user_total == -1) {
70951d94d49Schris            $buttons['last'] = $disabled;
71051d94d49Schris            $buttons['next'] = '';
71151d94d49Schris        } else {
71251d94d49Schris            $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
71351d94d49Schris        }
7140440ff15Schris
7150440ff15Schris        return $buttons;
7160440ff15Schris    }
7175c967d3dSChristopher Smith
7185c967d3dSChristopher Smith    /*
7195c967d3dSChristopher Smith     *  export a list of users in csv format using the current filter criteria
7205c967d3dSChristopher Smith     */
7215c967d3dSChristopher Smith    function _export() {
7225c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
7235c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
7245c967d3dSChristopher Smith        $column_headings = array(
7255c967d3dSChristopher Smith            $this->lang["user_id"],
7265c967d3dSChristopher Smith            $this->lang["user_name"],
7275c967d3dSChristopher Smith            $this->lang["user_mail"],
7285c967d3dSChristopher Smith            $this->lang["user_groups"]
7295c967d3dSChristopher Smith        );
7305c967d3dSChristopher Smith
7315c967d3dSChristopher Smith        // ==============================================================================================
7325c967d3dSChristopher Smith        // GENERATE OUTPUT
7335c967d3dSChristopher Smith        // normal headers for downloading...
7345c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
7355c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
7365c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
7375c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
7385c967d3dSChristopher Smith
7395c967d3dSChristopher Smith        // output the csv
7405c967d3dSChristopher Smith        $fd = fopen('php://output','w');
7415c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
7425c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
7435c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
7445c967d3dSChristopher Smith            fputcsv($fd, $line);
7455c967d3dSChristopher Smith        }
7465c967d3dSChristopher Smith        fclose($fd);
7475c967d3dSChristopher Smith        die;
7485c967d3dSChristopher Smith    }
749ae1afd2fSChristopher Smith
750ae1afd2fSChristopher Smith    /*
751ae1afd2fSChristopher Smith     * import a file of users in csv format
752ae1afd2fSChristopher Smith     *
753ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
754ae1afd2fSChristopher Smith     */
755ae1afd2fSChristopher Smith    function _import() {
756ae1afd2fSChristopher Smith        // check we are allowed to add users
757ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
758ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
759ae1afd2fSChristopher Smith
760ae1afd2fSChristopher Smith        // check file uploaded ok.
761ae1afd2fSChristopher Smith        if (empty($_FILES['import']['size']) || !empty($FILES['import']['error']) && is_uploaded_file($FILES['import']['tmp_name'])) {
762ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
763ae1afd2fSChristopher Smith            return false;
764ae1afd2fSChristopher Smith        }
765ae1afd2fSChristopher Smith        // retrieve users from the file
766ae1afd2fSChristopher Smith        $this->_import_failures = array();
767ae1afd2fSChristopher Smith        $import_success_count = 0;
768ae1afd2fSChristopher Smith        $import_fail_count = 0;
769ae1afd2fSChristopher Smith        $line = 0;
770ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
771ae1afd2fSChristopher Smith        if ($fd) {
772ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
773efcec72bSChristopher Smith                if (!utf8_check($csv)) {
774efcec72bSChristopher Smith                    $csv = utf8_encode($csv);
775efcec72bSChristopher Smith                }
776ae1afd2fSChristopher Smith                $raw = str_getcsv($csv);
777ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
778ae1afd2fSChristopher Smith                // data checks...
779ae1afd2fSChristopher Smith                if (1 == ++$line) {
780ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
781ae1afd2fSChristopher Smith                }
782ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
783ae1afd2fSChristopher Smith                    $import_fail_count++;
784ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
785ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
786ae1afd2fSChristopher Smith                    continue;
787ae1afd2fSChristopher Smith                }
788ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
789ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
790ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
791328143f8SChristopher Smith                    $sent = $this->_notifyUser($clean[0],$clean[1],false);
792328143f8SChristopher Smith                    if (!$sent){
793328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'],$clean[0],$clean[3]),-1);
794328143f8SChristopher Smith                    }
795ae1afd2fSChristopher Smith                    $import_success_count++;
796ae1afd2fSChristopher Smith                } else {
797ae1afd2fSChristopher Smith                    $import_fail_count++;
798ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
799ae1afd2fSChristopher Smith                }
800ae1afd2fSChristopher Smith            }
801ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
802ae1afd2fSChristopher Smith            if ($import_fail_count) {
803ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
804ae1afd2fSChristopher Smith            }
805ae1afd2fSChristopher Smith        } else {
806ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
807ae1afd2fSChristopher Smith        }
808ae1afd2fSChristopher Smith
809ae1afd2fSChristopher Smith        // save import failures into the session
810ae1afd2fSChristopher Smith        if (!headers_sent()) {
811ae1afd2fSChristopher Smith            session_start();
812ae1afd2fSChristopher Smith            $_SESSION['import_failures'] = $this->_import_failures;
813ae1afd2fSChristopher Smith            session_write_close();
814ae1afd2fSChristopher Smith        }
815ae1afd2fSChristopher Smith    }
816ae1afd2fSChristopher Smith
817ae1afd2fSChristopher Smith    function _cleanImportUser($candidate, & $error){
818ae1afd2fSChristopher Smith        global $INPUT;
819ae1afd2fSChristopher Smith
820ae1afd2fSChristopher Smith        // kludgy ....
821ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
822ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
823ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
824ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
825ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
826ae1afd2fSChristopher Smith
827ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
828ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
829ae1afd2fSChristopher Smith        if (empty($user)) {
830ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
831ae1afd2fSChristopher Smith            return false;
832ae1afd2fSChristopher Smith        }
833ae1afd2fSChristopher Smith
834ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
835ae1afd2fSChristopher Smith
836ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
837ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
838ae1afd2fSChristopher Smith            return false;
839ae1afd2fSChristopher Smith        }
840ae1afd2fSChristopher Smith
841328143f8SChristopher Smith        if ($this->_auth->canDo('modMail')) {
842328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
843ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
844ae1afd2fSChristopher Smith                return false;
845ae1afd2fSChristopher Smith            }
846328143f8SChristopher Smith        } else {
847328143f8SChristopher Smith            if (!empty($mail)) {
848328143f8SChristopher Smith                $error = $this->lang['import_error_badmail'];
849328143f8SChristopher Smith                return false;
850328143f8SChristopher Smith            }
851328143f8SChristopher Smith        }
852ae1afd2fSChristopher Smith
853ae1afd2fSChristopher Smith        return $cleaned;
854ae1afd2fSChristopher Smith    }
855ae1afd2fSChristopher Smith
856ae1afd2fSChristopher Smith    function _addImportUser($user, & $error){
857ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
858ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
859ae1afd2fSChristopher Smith            return false;
860ae1afd2fSChristopher Smith        }
861ae1afd2fSChristopher Smith
862ae1afd2fSChristopher Smith        return true;
863ae1afd2fSChristopher Smith    }
864ae1afd2fSChristopher Smith
865ae1afd2fSChristopher Smith    function _downloadImportFailures(){
866ae1afd2fSChristopher Smith
867ae1afd2fSChristopher Smith        // ==============================================================================================
868ae1afd2fSChristopher Smith        // GENERATE OUTPUT
869ae1afd2fSChristopher Smith        // normal headers for downloading...
870ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
871ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
872ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
873ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
874ae1afd2fSChristopher Smith
875ae1afd2fSChristopher Smith        // output the csv
876ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
877ae1afd2fSChristopher Smith        foreach ($this->_import_failures as $line => $fail) {
878ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
879ae1afd2fSChristopher Smith        }
880ae1afd2fSChristopher Smith        fclose($fd);
881ae1afd2fSChristopher Smith        die;
882ae1afd2fSChristopher Smith    }
883ae1afd2fSChristopher Smith
8840440ff15Schris}
885