xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision ae1afd2f6529e4d07b18317304e5e2c302d783ce)
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
33*ae1afd2fSChristopher 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        }
53*ae1afd2fSChristopher Smith
54*ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
55*ae1afd2fSChristopher Smith        if ($_SESSION['import_failures']){
56*ae1afd2fSChristopher Smith            $this->_import_failures = $_SESSION['import_failures'];
57*ae1afd2fSChristopher 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;
111*ae1afd2fSChristopher Smith          case "import" : $this->_import(); break;
112*ae1afd2fSChristopher 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 {
1560440ff15Schris          ptln("<p>".sprintf($this->lang['nonefound'],$this->_auth->getUserCount())."</p>");
1570440ff15Schris        }
1580440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">");
159634d7150SAndreas Gohr        formSecurityToken();
160c7b28ffdSAnika Henke        ptln("  <div class=\"table\">");
1610440ff15Schris        ptln("  <table class=\"inline\">");
1620440ff15Schris        ptln("    <thead>");
1630440ff15Schris        ptln("      <tr>");
164e260f93bSAnika 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>");
1650440ff15Schris        ptln("      </tr>");
1660440ff15Schris
1670440ff15Schris        ptln("      <tr>");
1682365d73dSAnika 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>");
169a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"userid\" class=\"edit\" value=\"".$this->_htmlFilter('user')."\" /></td>");
170a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"username\" class=\"edit\" value=\"".$this->_htmlFilter('name')."\" /></td>");
171a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usermail\" class=\"edit\" value=\"".$this->_htmlFilter('mail')."\" /></td>");
172a2c0246eSAnika Henke        ptln("        <td><input type=\"text\" name=\"usergroups\" class=\"edit\" value=\"".$this->_htmlFilter('grps')."\" /></td>");
1730440ff15Schris        ptln("      </tr>");
1740440ff15Schris        ptln("    </thead>");
1750440ff15Schris
1760440ff15Schris        if ($this->_user_total) {
1770440ff15Schris          ptln("    <tbody>");
1780440ff15Schris          foreach ($user_list as $user => $userinfo) {
1790440ff15Schris            extract($userinfo);
1800440ff15Schris            $groups = join(', ',$grps);
181a2c0246eSAnika Henke            ptln("    <tr class=\"user_info\">");
1820440ff15Schris            ptln("      <td class=\"centeralign\"><input type=\"checkbox\" name=\"delete[".$user."]\" ".$delete_disable." /></td>");
1832365d73dSAnika Henke            if ($editable) {
18477d19185SAndreas Gohr              ptln("    <td><a href=\"".wl($ID,array('fn[edit]['.hsc($user).']' => 1,
18577d19185SAndreas Gohr                                                     'do' => 'admin',
18677d19185SAndreas Gohr                                                     'page' => 'usermanager',
18777d19185SAndreas Gohr                                                     'sectok' => getSecurityToken())).
18877d19185SAndreas Gohr                   "\" title=\"".$this->lang['edit_prompt']."\">".hsc($user)."</a></td>");
1892365d73dSAnika Henke            } else {
1902365d73dSAnika Henke              ptln("    <td>".hsc($user)."</td>");
1912365d73dSAnika Henke            }
1922365d73dSAnika Henke            ptln("      <td>".hsc($name)."</td><td>".hsc($mail)."</td><td>".hsc($groups)."</td>");
1930440ff15Schris            ptln("    </tr>");
1940440ff15Schris          }
1950440ff15Schris          ptln("    </tbody>");
1960440ff15Schris        }
1970440ff15Schris
1980440ff15Schris        ptln("    <tbody>");
1992365d73dSAnika Henke        ptln("      <tr><td colspan=\"5\" class=\"centeralign\">");
200a2c0246eSAnika Henke        ptln("        <span class=\"medialeft\">");
201a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[delete]\" ".$delete_disable." class=\"button\" value=\"".$this->lang['delete_selected']."\" id=\"usrmgr__del\" />");
2020440ff15Schris        ptln("        </span>");
203a2c0246eSAnika Henke        ptln("        <span class=\"mediaright\">");
204a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[start]\" ".$page_buttons['start']." class=\"button\" value=\"".$this->lang['start']."\" />");
205a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[prev]\" ".$page_buttons['prev']." class=\"button\" value=\"".$this->lang['prev']."\" />");
206a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[next]\" ".$page_buttons['next']." class=\"button\" value=\"".$this->lang['next']."\" />");
207a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[last]\" ".$page_buttons['last']." class=\"button\" value=\"".$this->lang['last']."\" />");
2080440ff15Schris        ptln("        </span>");
2095c967d3dSChristopher Smith        if (!empty($this->_filter)) {
210a2c0246eSAnika Henke            ptln("    <input type=\"submit\" name=\"fn[search][clear]\" class=\"button\" value=\"".$this->lang['clear']."\" />");
2115c967d3dSChristopher Smith        }
2125c967d3dSChristopher Smith        ptln("        <input type=\"submit\" name=\"fn[export]\" class=\"button\" value=\"".$export_label."\" />");
2135164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"do\"    value=\"admin\" />");
2145164d9c9SAnika Henke        ptln("        <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />");
215daf4ca4eSAnika Henke
216daf4ca4eSAnika Henke        $this->_htmlFilterSettings(2);
217daf4ca4eSAnika Henke
2180440ff15Schris        ptln("      </td></tr>");
2190440ff15Schris        ptln("    </tbody>");
2200440ff15Schris        ptln("  </table>");
221c7b28ffdSAnika Henke        ptln("  </div>");
2220440ff15Schris
2230440ff15Schris        ptln("</form>");
2240440ff15Schris        ptln("</div>");
2250440ff15Schris
226a2c0246eSAnika Henke        $style = $this->_edit_user ? " class=\"edit_user\"" : "";
2270440ff15Schris
22882fd59b6SAndreas Gohr        if ($this->_auth->canDo('addUser')) {
2290440ff15Schris          ptln("<div".$style.">");
2300440ff15Schris          print $this->locale_xhtml('add');
2310440ff15Schris          ptln("  <div class=\"level2\">");
2320440ff15Schris
23378c7c8c9Schris          $this->_htmlUserForm('add',null,array(),4);
2340440ff15Schris
2350440ff15Schris          ptln("  </div>");
2360440ff15Schris          ptln("</div>");
2370440ff15Schris        }
2380440ff15Schris
23982fd59b6SAndreas Gohr        if($this->_edit_user  && $this->_auth->canDo('UserMod')){
240c632fc69SAndreas Gohr          ptln("<div".$style." id=\"scroll__here\">");
2410440ff15Schris          print $this->locale_xhtml('edit');
2420440ff15Schris          ptln("  <div class=\"level2\">");
2430440ff15Schris
24478c7c8c9Schris          $this->_htmlUserForm('modify',$this->_edit_user,$this->_edit_userdata,4);
2450440ff15Schris
2460440ff15Schris          ptln("  </div>");
2470440ff15Schris          ptln("</div>");
2480440ff15Schris        }
249*ae1afd2fSChristopher Smith
250*ae1afd2fSChristopher Smith        if ($this->_auth->canDo('addUser')) {
251*ae1afd2fSChristopher Smith          $this->_htmlImportForm();
252*ae1afd2fSChristopher Smith        }
25358dde80dSAnika Henke        ptln("</div>");
2540440ff15Schris    }
2550440ff15Schris
25682fd59b6SAndreas Gohr
25782fd59b6SAndreas Gohr    /**
25882fd59b6SAndreas Gohr     * @todo disable fields which the backend can't change
25982fd59b6SAndreas Gohr     */
26078c7c8c9Schris    function _htmlUserForm($cmd,$user='',$userdata=array(),$indent=0) {
261a6858c6aSchris        global $conf;
262bb4866bdSchris        global $ID;
26378c7c8c9Schris
26478c7c8c9Schris        $name = $mail = $groups = '';
265a6858c6aSchris        $notes = array();
2660440ff15Schris
2670440ff15Schris        if ($user) {
26878c7c8c9Schris          extract($userdata);
26978c7c8c9Schris          if (!empty($grps)) $groups = join(',',$grps);
270a6858c6aSchris        } else {
271a6858c6aSchris          $notes[] = sprintf($this->lang['note_group'],$conf['defaultgroup']);
2720440ff15Schris        }
2730440ff15Schris
2740440ff15Schris        ptln("<form action=\"".wl($ID)."\" method=\"post\">",$indent);
275634d7150SAndreas Gohr        formSecurityToken();
276c7b28ffdSAnika Henke        ptln("  <div class=\"table\">",$indent);
2770440ff15Schris        ptln("  <table class=\"inline\">",$indent);
2780440ff15Schris        ptln("    <thead>",$indent);
2790440ff15Schris        ptln("      <tr><th>".$this->lang["field"]."</th><th>".$this->lang["value"]."</th></tr>",$indent);
2800440ff15Schris        ptln("    </thead>",$indent);
2810440ff15Schris        ptln("    <tbody>",$indent);
28226fb387bSchris
28326fb387bSchris        $this->_htmlInputField($cmd."_userid",    "userid",    $this->lang["user_id"],    $user,  $this->_auth->canDo("modLogin"), $indent+6);
28426fb387bSchris        $this->_htmlInputField($cmd."_userpass",  "userpass",  $this->lang["user_pass"],  "",     $this->_auth->canDo("modPass"),  $indent+6);
28526fb387bSchris        $this->_htmlInputField($cmd."_username",  "username",  $this->lang["user_name"],  $name,  $this->_auth->canDo("modName"),  $indent+6);
28626fb387bSchris        $this->_htmlInputField($cmd."_usermail",  "usermail",  $this->lang["user_mail"],  $mail,  $this->_auth->canDo("modMail"),  $indent+6);
28726fb387bSchris        $this->_htmlInputField($cmd."_usergroups","usergroups",$this->lang["user_groups"],$groups,$this->_auth->canDo("modGroups"),$indent+6);
28826fb387bSchris
289a6858c6aSchris        if ($this->_auth->canDo("modPass")) {
290c3f4fb63SGina Haeussge          $notes[] = $this->lang['note_pass'];
291a6858c6aSchris          if ($user) {
292a6858c6aSchris            $notes[] = $this->lang['note_notify'];
293a6858c6aSchris          }
294a6858c6aSchris
295a6858c6aSchris          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);
296a6858c6aSchris        }
297a6858c6aSchris
2980440ff15Schris        ptln("    </tbody>",$indent);
2990440ff15Schris        ptln("    <tbody>",$indent);
3000440ff15Schris        ptln("      <tr>",$indent);
3010440ff15Schris        ptln("        <td colspan=\"2\">",$indent);
3020440ff15Schris        ptln("          <input type=\"hidden\" name=\"do\"    value=\"admin\" />",$indent);
3030440ff15Schris        ptln("          <input type=\"hidden\" name=\"page\"  value=\"usermanager\" />",$indent);
3040440ff15Schris
3050440ff15Schris        // save current $user, we need this to access details if the name is changed
3060440ff15Schris        if ($user)
3070440ff15Schris          ptln("          <input type=\"hidden\" name=\"userid_old\"  value=\"".$user."\" />",$indent);
3080440ff15Schris
3090440ff15Schris        $this->_htmlFilterSettings($indent+10);
3100440ff15Schris
311a2c0246eSAnika Henke        ptln("          <input type=\"submit\" name=\"fn[".$cmd."]\" class=\"button\" value=\"".$this->lang[$cmd]."\" />",$indent);
3120440ff15Schris        ptln("        </td>",$indent);
3130440ff15Schris        ptln("      </tr>",$indent);
3140440ff15Schris        ptln("    </tbody>",$indent);
3150440ff15Schris        ptln("  </table>",$indent);
316c7b28ffdSAnika Henke        ptln("  </div>",$indent);
317a6858c6aSchris
318a6858c6aSchris        foreach ($notes as $note)
319a6858c6aSchris          ptln("<div class=\"fn\">".$note."</div>",$indent);
320a6858c6aSchris
3210440ff15Schris        ptln("</form>",$indent);
3220440ff15Schris    }
3230440ff15Schris
32426fb387bSchris    function _htmlInputField($id, $name, $label, $value, $cando, $indent=0) {
3257de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
3267de12fceSAndreas Gohr        echo str_pad('',$indent);
3277de12fceSAndreas Gohr
328d796a891SAndreas Gohr        if($name == 'userpass'){
329d796a891SAndreas Gohr            $fieldtype = 'password';
330d796a891SAndreas Gohr            $autocomp  = 'autocomplete="off"';
3317b3674bdSChristopher Smith        }elseif($name == 'usermail'){
3327b3674bdSChristopher Smith            $fieldtype = 'email';
3337b3674bdSChristopher Smith            $autocomp  = '';
334d796a891SAndreas Gohr        }else{
335d796a891SAndreas Gohr            $fieldtype = 'text';
336d796a891SAndreas Gohr            $autocomp  = '';
337d796a891SAndreas Gohr        }
338d796a891SAndreas Gohr
339ee54059bSTimo Voipio
3407de12fceSAndreas Gohr        echo "<tr $class>";
3417de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
3427de12fceSAndreas Gohr        echo "<td>";
3437de12fceSAndreas Gohr        if($cando){
344d796a891SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit\" $autocomp />";
3457de12fceSAndreas Gohr        }else{
3467de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
347ee54059bSTimo Voipio            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\" value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
3487de12fceSAndreas Gohr        }
3497de12fceSAndreas Gohr        echo "</td>";
3507de12fceSAndreas Gohr        echo "</tr>";
35126fb387bSchris    }
35226fb387bSchris
3530440ff15Schris    function _htmlFilter($key) {
3540440ff15Schris        if (empty($this->_filter)) return '';
3550440ff15Schris        return (isset($this->_filter[$key]) ? hsc($this->_filter[$key]) : '');
3560440ff15Schris    }
3570440ff15Schris
3580440ff15Schris    function _htmlFilterSettings($indent=0) {
3590440ff15Schris
3600440ff15Schris        ptln("<input type=\"hidden\" name=\"start\" value=\"".$this->_start."\" />",$indent);
3610440ff15Schris
3620440ff15Schris        foreach ($this->_filter as $key => $filter) {
3630440ff15Schris          ptln("<input type=\"hidden\" name=\"filter[".$key."]\" value=\"".hsc($filter)."\" />",$indent);
3640440ff15Schris        }
3650440ff15Schris    }
3660440ff15Schris
367*ae1afd2fSChristopher Smith    function _htmlImportForm($indent=0) {
368*ae1afd2fSChristopher Smith        global $ID;
369*ae1afd2fSChristopher Smith
370*ae1afd2fSChristopher Smith        $failure_download_link = wl($ID,array('do'=>'admin','page'=>'usermanager','fn[importfails]'=>1));
371*ae1afd2fSChristopher Smith
372*ae1afd2fSChristopher Smith        ptln('<div class="level2 import_users">',$indent);
373*ae1afd2fSChristopher Smith        print $this->locale_xhtml('import');
374*ae1afd2fSChristopher Smith        ptln('  <form action="'.wl($ID).'" method="post" enctype="multipart/form-data">',$indent);
375*ae1afd2fSChristopher Smith        formSecurityToken();
376*ae1afd2fSChristopher Smith        ptln('    <label>User list file (csv):  <input type="file" name="import" /></label>',$indent);
377*ae1afd2fSChristopher Smith        ptln('    <input type="submit" name="fn[import]" value="'.$this->lang['import'].'" />',$indent);
378*ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="do"    value="admin" />',$indent);
379*ae1afd2fSChristopher Smith        ptln('    <input type="hidden" name="page"  value="usermanager" />',$indent);
380*ae1afd2fSChristopher Smith
381*ae1afd2fSChristopher Smith        $this->_htmlFilterSettings($indent+4);
382*ae1afd2fSChristopher Smith        ptln('  </form>',$indent);
383*ae1afd2fSChristopher Smith        ptln('</div>');
384*ae1afd2fSChristopher Smith
385*ae1afd2fSChristopher Smith        // list failures from the previous import
386*ae1afd2fSChristopher Smith        if ($this->_import_failures) {
387*ae1afd2fSChristopher Smith            $digits = strlen(count($this->_import_failures));
388*ae1afd2fSChristopher Smith            ptln('<div class="level3 import_failures">',$indent);
389*ae1afd2fSChristopher Smith            ptln('  <h3>Most Recent Import - Failures</h3>');
390*ae1afd2fSChristopher Smith            ptln('  <table class="import_failures">',$indent);
391*ae1afd2fSChristopher Smith            ptln('    <thead>',$indent);
392*ae1afd2fSChristopher Smith            ptln('      <tr>',$indent);
393*ae1afd2fSChristopher Smith            ptln('        <th class="line">'.$this->lang['line'].'</th>',$indent);
394*ae1afd2fSChristopher Smith            ptln('        <th class="error">'.$this->lang['error'].'</th>',$indent);
395*ae1afd2fSChristopher Smith            ptln('        <th class="userid">'.$this->lang['user_id'].'</th>',$indent);
396*ae1afd2fSChristopher Smith            ptln('        <th class="username">'.$this->lang['user_name'].'</th>',$indent);
397*ae1afd2fSChristopher Smith            ptln('        <th class="usermail">'.$this->lang['user_mail'].'</th>',$indent);
398*ae1afd2fSChristopher Smith            ptln('        <th class="usergroups">'.$this->lang['user_groups'].'</th>',$indent);
399*ae1afd2fSChristopher Smith            ptln('      </tr>',$indent);
400*ae1afd2fSChristopher Smith            ptln('    </thead>',$indent);
401*ae1afd2fSChristopher Smith            ptln('    <tbody>',$indent);
402*ae1afd2fSChristopher Smith            foreach ($this->_import_failures as $line => $failure) {
403*ae1afd2fSChristopher Smith                ptln('      <tr>',$indent);
404*ae1afd2fSChristopher Smith                ptln('        <td class="lineno"> '.sprintf('%0'.$digits.'d',$line).' </td>',$indent);
405*ae1afd2fSChristopher Smith                ptln('        <td class="error">' .$failure['error'].' </td>', $indent);
406*ae1afd2fSChristopher Smith                ptln('        <td class="field userid"> '.hsc($failure['user'][0]).' </td>',$indent);
407*ae1afd2fSChristopher Smith                ptln('        <td class="field username"> '.hsc($failure['user'][2]).' </td>',$indent);
408*ae1afd2fSChristopher Smith                ptln('        <td class="field usermail"> '.hsc($failure['user'][3]).' </td>',$indent);
409*ae1afd2fSChristopher Smith                ptln('        <td class="field usergroups"> '.hsc($failure['user'][4]).' </td>',$indent);
410*ae1afd2fSChristopher Smith                ptln('      </tr>',$indent);
411*ae1afd2fSChristopher Smith            }
412*ae1afd2fSChristopher Smith            ptln('    </tbody>',$indent);
413*ae1afd2fSChristopher Smith            ptln('  </table>',$indent);
414*ae1afd2fSChristopher Smith            ptln('  <p><a href="'.$failure_download_link.'">Download Failures as CSV for correction</a></p>');
415*ae1afd2fSChristopher Smith            ptln('</div>');
416*ae1afd2fSChristopher Smith        }
417*ae1afd2fSChristopher Smith
418*ae1afd2fSChristopher Smith    }
419*ae1afd2fSChristopher Smith
4200440ff15Schris    function _addUser(){
42100d58927SMichael Hamann        global $INPUT;
422634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
42382fd59b6SAndreas Gohr        if (!$this->_auth->canDo('addUser')) return false;
4240440ff15Schris
4250440ff15Schris        list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser();
4260440ff15Schris        if (empty($user)) return false;
4276733c4d7SChris Smith
4286733c4d7SChris Smith        if ($this->_auth->canDo('modPass')){
429c3f4fb63SGina Haeussge          if (empty($pass)){
43000d58927SMichael Hamann            if($INPUT->has('usernotify')){
4318a285f7fSAndreas Gohr              $pass = auth_pwgen($user);
432c3f4fb63SGina Haeussge            } else {
43360b9901bSAndreas Gohr              msg($this->lang['add_fail'], -1);
43460b9901bSAndreas Gohr              return false;
43560b9901bSAndreas Gohr            }
4366733c4d7SChris Smith          }
4376733c4d7SChris Smith        } else {
4386733c4d7SChris Smith          if (!empty($pass)){
4396733c4d7SChris Smith            msg($this->lang['add_fail'], -1);
4406733c4d7SChris Smith            return false;
4416733c4d7SChris Smith          }
4426733c4d7SChris Smith        }
4436733c4d7SChris Smith
4446733c4d7SChris Smith        if ($this->_auth->canDo('modName')){
4456733c4d7SChris Smith          if (empty($name)){
4466733c4d7SChris Smith            msg($this->lang['add_fail'], -1);
4476733c4d7SChris Smith            return false;
4486733c4d7SChris Smith          }
4496733c4d7SChris Smith        } else {
4506733c4d7SChris Smith          if (!empty($name)){
4516733c4d7SChris Smith            return false;
4526733c4d7SChris Smith          }
4536733c4d7SChris Smith        }
4546733c4d7SChris Smith
4556733c4d7SChris Smith        if ($this->_auth->canDo('modMail')){
4566733c4d7SChris Smith          if (empty($mail)){
4576733c4d7SChris Smith            msg($this->lang['add_fail'], -1);
4586733c4d7SChris Smith            return false;
4596733c4d7SChris Smith          }
4606733c4d7SChris Smith        } else {
4616733c4d7SChris Smith          if (!empty($mail)){
4626733c4d7SChris Smith            return false;
4636733c4d7SChris Smith          }
4646733c4d7SChris Smith        }
4650440ff15Schris
4667d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('create', array($user,$pass,$name,$mail,$grps))) {
467a6858c6aSchris
468a6858c6aSchris          msg($this->lang['add_ok'], 1);
469a6858c6aSchris
47000d58927SMichael Hamann          if ($INPUT->has('usernotify') && $pass) {
471a6858c6aSchris            $this->_notifyUser($user,$pass);
472a6858c6aSchris          }
473a6858c6aSchris        } else {
47460b9901bSAndreas Gohr          msg($this->lang['add_fail'], -1);
475a6858c6aSchris        }
476a6858c6aSchris
477a6858c6aSchris        return $ok;
4780440ff15Schris    }
4790440ff15Schris
4800440ff15Schris    /**
4810440ff15Schris     * Delete user
4820440ff15Schris     */
4830440ff15Schris    function _deleteUser(){
48400d58927SMichael Hamann        global $conf, $INPUT;
4859ec82636SAndreas Gohr
486634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
48782fd59b6SAndreas Gohr        if (!$this->_auth->canDo('delUser')) return false;
4880440ff15Schris
48900d58927SMichael Hamann        $selected = $INPUT->arr('delete');
49000d58927SMichael Hamann        if (empty($selected)) return false;
4910440ff15Schris        $selected = array_keys($selected);
4920440ff15Schris
493c9a8f912SMichael Klier        if(in_array($_SERVER['REMOTE_USER'], $selected)) {
494c9a8f912SMichael Klier            msg("You can't delete yourself!", -1);
495c9a8f912SMichael Klier            return false;
496c9a8f912SMichael Klier        }
497c9a8f912SMichael Klier
4987d3c8d42SGabriel Birke        $count = $this->_auth->triggerUserMod('delete', array($selected));
4990440ff15Schris        if ($count == count($selected)) {
5000440ff15Schris          $text = str_replace('%d', $count, $this->lang['delete_ok']);
5010440ff15Schris          msg("$text.", 1);
5020440ff15Schris        } else {
5030440ff15Schris          $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
5040440ff15Schris          $part2 = str_replace('%d', (count($selected)-$count), $this->lang['delete_fail']);
5050440ff15Schris          msg("$part1, $part2",-1);
5060440ff15Schris        }
50778c7c8c9Schris
5089ec82636SAndreas Gohr        // invalidate all sessions
5099ec82636SAndreas Gohr        io_saveFile($conf['cachedir'].'/sessionpurge',time());
5109ec82636SAndreas Gohr
51178c7c8c9Schris        return true;
51278c7c8c9Schris    }
51378c7c8c9Schris
51478c7c8c9Schris    /**
51578c7c8c9Schris     * Edit user (a user has been selected for editing)
51678c7c8c9Schris     */
51778c7c8c9Schris    function _editUser($param) {
518634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
51978c7c8c9Schris        if (!$this->_auth->canDo('UserMod')) return false;
52078c7c8c9Schris
52178c7c8c9Schris        $user = cleanID(preg_replace('/.*:/','',$param));
52278c7c8c9Schris        $userdata = $this->_auth->getUserData($user);
52378c7c8c9Schris
52478c7c8c9Schris        // no user found?
52578c7c8c9Schris        if (!$userdata) {
52678c7c8c9Schris          msg($this->lang['edit_usermissing'],-1);
52778c7c8c9Schris          return false;
52878c7c8c9Schris        }
52978c7c8c9Schris
53078c7c8c9Schris        $this->_edit_user = $user;
53178c7c8c9Schris        $this->_edit_userdata = $userdata;
53278c7c8c9Schris
53378c7c8c9Schris        return true;
5340440ff15Schris    }
5350440ff15Schris
5360440ff15Schris    /**
537ef64b3a2Schris     * Modify user (modified user data has been recieved)
5380440ff15Schris     */
5390440ff15Schris    function _modifyUser(){
54000d58927SMichael Hamann        global $conf, $INPUT;
5419ec82636SAndreas Gohr
542634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
54382fd59b6SAndreas Gohr        if (!$this->_auth->canDo('UserMod')) return false;
5440440ff15Schris
54526fb387bSchris        // get currently valid  user data
54600d58927SMichael Hamann        $olduser = cleanID(preg_replace('/.*:/','',$INPUT->str('userid_old')));
547073766c6Smatthiasgrimm        $oldinfo = $this->_auth->getUserData($olduser);
548073766c6Smatthiasgrimm
54926fb387bSchris        // get new user data subject to change
550073766c6Smatthiasgrimm        list($newuser,$newpass,$newname,$newmail,$newgrps) = $this->_retrieveUser();
551073766c6Smatthiasgrimm        if (empty($newuser)) return false;
5520440ff15Schris
5530440ff15Schris        $changes = array();
554073766c6Smatthiasgrimm        if ($newuser != $olduser) {
55526fb387bSchris
55626fb387bSchris          if (!$this->_auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
55726fb387bSchris            msg($this->lang['update_fail'],-1);
55826fb387bSchris            return false;
55926fb387bSchris          }
56026fb387bSchris
56126fb387bSchris          // check if $newuser already exists
562073766c6Smatthiasgrimm          if ($this->_auth->getUserData($newuser)) {
563073766c6Smatthiasgrimm            msg(sprintf($this->lang['update_exists'],$newuser),-1);
564a6858c6aSchris            $re_edit = true;
5650440ff15Schris          } else {
566073766c6Smatthiasgrimm            $changes['user'] = $newuser;
5670440ff15Schris          }
56893eefc2fSAndreas Gohr        }
56993eefc2fSAndreas Gohr
57093eefc2fSAndreas Gohr        // generate password if left empty and notification is on
57100d58927SMichael Hamann        if($INPUT->has('usernotify') && empty($newpass)){
5728a285f7fSAndreas Gohr            $newpass = auth_pwgen($olduser);
5730440ff15Schris        }
5740440ff15Schris
57526fb387bSchris        if (!empty($newpass) && $this->_auth->canDo('modPass'))
576073766c6Smatthiasgrimm          $changes['pass'] = $newpass;
57726fb387bSchris        if (!empty($newname) && $this->_auth->canDo('modName') && $newname != $oldinfo['name'])
578073766c6Smatthiasgrimm          $changes['name'] = $newname;
57926fb387bSchris        if (!empty($newmail) && $this->_auth->canDo('modMail') && $newmail != $oldinfo['mail'])
580073766c6Smatthiasgrimm          $changes['mail'] = $newmail;
58126fb387bSchris        if (!empty($newgrps) && $this->_auth->canDo('modGroups') && $newgrps != $oldinfo['grps'])
582073766c6Smatthiasgrimm          $changes['grps'] = $newgrps;
5830440ff15Schris
5847d3c8d42SGabriel Birke        if ($ok = $this->_auth->triggerUserMod('modify', array($olduser, $changes))) {
5850440ff15Schris          msg($this->lang['update_ok'],1);
586a6858c6aSchris
58700d58927SMichael Hamann          if ($INPUT->has('usernotify') && $newpass) {
588a6858c6aSchris            $notify = empty($changes['user']) ? $olduser : $newuser;
589a6858c6aSchris            $this->_notifyUser($notify,$newpass);
590a6858c6aSchris          }
591a6858c6aSchris
5929ec82636SAndreas Gohr          // invalidate all sessions
5939ec82636SAndreas Gohr          io_saveFile($conf['cachedir'].'/sessionpurge',time());
5949ec82636SAndreas Gohr
5950440ff15Schris        } else {
5960440ff15Schris          msg($this->lang['update_fail'],-1);
5970440ff15Schris        }
59878c7c8c9Schris
599a6858c6aSchris        if (!empty($re_edit)) {
600a6858c6aSchris            $this->_editUser($olduser);
6010440ff15Schris        }
6020440ff15Schris
603a6858c6aSchris        return $ok;
604a6858c6aSchris    }
605a6858c6aSchris
606a6858c6aSchris    /**
607a6858c6aSchris     * send password change notification email
608a6858c6aSchris     */
609a6858c6aSchris    function _notifyUser($user, $password) {
610a6858c6aSchris
611a6858c6aSchris        if ($sent = auth_sendPassword($user,$password)) {
612a6858c6aSchris          msg($this->lang['notify_ok'], 1);
613a6858c6aSchris        } else {
614a6858c6aSchris          msg($this->lang['notify_fail'], -1);
615a6858c6aSchris        }
616a6858c6aSchris
617a6858c6aSchris        return $sent;
618a6858c6aSchris    }
619a6858c6aSchris
620a6858c6aSchris    /**
6210440ff15Schris     * retrieve & clean user data from the form
622a6858c6aSchris     *
623a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
6240440ff15Schris     */
6250440ff15Schris    function _retrieveUser($clean=true) {
6267441e340SAndreas Gohr        global $auth;
627fbfbbe8aSHakan Sandell        global $INPUT;
6280440ff15Schris
629fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
630fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
631fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
632fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
633fbfbbe8aSHakan Sandell        $user[4] = explode(',',$INPUT->str('usergroups'));
6340440ff15Schris
6357441e340SAndreas Gohr        $user[4] = array_map('trim',$user[4]);
6367441e340SAndreas Gohr        if($clean) $user[4] = array_map(array($auth,'cleanGroup'),$user[4]);
6377441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
6387441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
6397441e340SAndreas Gohr        if(!count($user[4])) $user[4] = null;
6400440ff15Schris
6410440ff15Schris        return $user;
6420440ff15Schris    }
6430440ff15Schris
6440440ff15Schris    function _setFilter($op) {
6450440ff15Schris
6460440ff15Schris        $this->_filter = array();
6470440ff15Schris
6480440ff15Schris        if ($op == 'new') {
6490440ff15Schris          list($user,$pass,$name,$mail,$grps) = $this->_retrieveUser(false);
6500440ff15Schris
6510440ff15Schris          if (!empty($user)) $this->_filter['user'] = $user;
6520440ff15Schris          if (!empty($name)) $this->_filter['name'] = $name;
6530440ff15Schris          if (!empty($mail)) $this->_filter['mail'] = $mail;
6540440ff15Schris          if (!empty($grps)) $this->_filter['grps'] = join('|',$grps);
6550440ff15Schris        }
6560440ff15Schris    }
6570440ff15Schris
6580440ff15Schris    function _retrieveFilter() {
659fbfbbe8aSHakan Sandell        global $INPUT;
6600440ff15Schris
661fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
6620440ff15Schris
6630440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
6640440ff15Schris        $filter = array();
6650440ff15Schris
6660440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
6670440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
6680440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
6690440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
6700440ff15Schris
6710440ff15Schris        return $filter;
6720440ff15Schris    }
6730440ff15Schris
6740440ff15Schris    function _validatePagination() {
6750440ff15Schris
6760440ff15Schris        if ($this->_start >= $this->_user_total) {
6770440ff15Schris          $this->_start = $this->_user_total - $this->_pagesize;
6780440ff15Schris        }
6790440ff15Schris        if ($this->_start < 0) $this->_start = 0;
6800440ff15Schris
6810440ff15Schris        $this->_last = min($this->_user_total, $this->_start + $this->_pagesize);
6820440ff15Schris    }
6830440ff15Schris
6840440ff15Schris    /*
6850440ff15Schris     *  return an array of strings to enable/disable pagination buttons
6860440ff15Schris     */
6870440ff15Schris    function _pagination() {
6880440ff15Schris
68951d94d49Schris        $disabled = 'disabled="disabled"';
69051d94d49Schris
69151d94d49Schris        $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : '';
69251d94d49Schris
69351d94d49Schris        if ($this->_user_total == -1) {
69451d94d49Schris          $buttons['last'] = $disabled;
69551d94d49Schris          $buttons['next'] = '';
69651d94d49Schris        } else {
69751d94d49Schris          $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : '';
69851d94d49Schris        }
6990440ff15Schris
7000440ff15Schris        return $buttons;
7010440ff15Schris    }
7025c967d3dSChristopher Smith
7035c967d3dSChristopher Smith    /*
7045c967d3dSChristopher Smith     *  export a list of users in csv format using the current filter criteria
7055c967d3dSChristopher Smith     */
7065c967d3dSChristopher Smith    function _export() {
7075c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
7085c967d3dSChristopher Smith        $user_list = $this->_auth->retrieveUsers(0, 0, $this->_filter);
7095c967d3dSChristopher Smith        $column_headings = array(
7105c967d3dSChristopher Smith            $this->lang["user_id"],
7115c967d3dSChristopher Smith            $this->lang["user_name"],
7125c967d3dSChristopher Smith            $this->lang["user_mail"],
7135c967d3dSChristopher Smith            $this->lang["user_groups"]
7145c967d3dSChristopher Smith        );
7155c967d3dSChristopher Smith
7165c967d3dSChristopher Smith        // ==============================================================================================
7175c967d3dSChristopher Smith        // GENERATE OUTPUT
7185c967d3dSChristopher Smith        // normal headers for downloading...
7195c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
7205c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
7215c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
7225c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
7235c967d3dSChristopher Smith
7245c967d3dSChristopher Smith        // output the csv
7255c967d3dSChristopher Smith        $fd = fopen('php://output','w');
7265c967d3dSChristopher Smith        fputcsv($fd, $column_headings);
7275c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
7285c967d3dSChristopher Smith            $line = array($user, $info['name'], $info['mail'], join(',',$info['grps']));
7295c967d3dSChristopher Smith            fputcsv($fd, $line);
7305c967d3dSChristopher Smith        }
7315c967d3dSChristopher Smith        fclose($fd);
7325c967d3dSChristopher Smith        die;
7335c967d3dSChristopher Smith    }
734*ae1afd2fSChristopher Smith
735*ae1afd2fSChristopher Smith    /*
736*ae1afd2fSChristopher Smith     * import a file of users in csv format
737*ae1afd2fSChristopher Smith     *
738*ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
739*ae1afd2fSChristopher Smith     */
740*ae1afd2fSChristopher Smith    function _import() {
741*ae1afd2fSChristopher Smith        // check we are allowed to add users
742*ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
743*ae1afd2fSChristopher Smith        if (!$this->_auth->canDo('addUser')) return false;
744*ae1afd2fSChristopher Smith
745*ae1afd2fSChristopher Smith        // check file uploaded ok.
746*ae1afd2fSChristopher Smith        if (empty($_FILES['import']['size']) || !empty($FILES['import']['error']) && is_uploaded_file($FILES['import']['tmp_name'])) {
747*ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'],-1);
748*ae1afd2fSChristopher Smith            return false;
749*ae1afd2fSChristopher Smith        }
750*ae1afd2fSChristopher Smith        // retrieve users from the file
751*ae1afd2fSChristopher Smith        $this->_import_failures = array();
752*ae1afd2fSChristopher Smith        $import_success_count = 0;
753*ae1afd2fSChristopher Smith        $import_fail_count = 0;
754*ae1afd2fSChristopher Smith        $line = 0;
755*ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'],'r');
756*ae1afd2fSChristopher Smith        if ($fd) {
757*ae1afd2fSChristopher Smith            while($csv = fgets($fd)){
758*ae1afd2fSChristopher Smith                $raw = str_getcsv($csv);
759*ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
760*ae1afd2fSChristopher Smith                // data checks...
761*ae1afd2fSChristopher Smith                if (1 == ++$line) {
762*ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
763*ae1afd2fSChristopher Smith                }
764*ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
765*ae1afd2fSChristopher Smith                    $import_fail_count++;
766*ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
767*ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
768*ae1afd2fSChristopher Smith                    continue;
769*ae1afd2fSChristopher Smith                }
770*ae1afd2fSChristopher Smith                array_splice($raw,1,0,auth_pwgen());                          // splice in a generated password
771*ae1afd2fSChristopher Smith                $clean = $this->_cleanImportUser($raw, $error);
772*ae1afd2fSChristopher Smith                if ($clean && $this->_addImportUser($clean, $error)) {
773*ae1afd2fSChristopher Smith#                    $this->_notifyUser($clean[0],$clean[1]);
774*ae1afd2fSChristopher Smith                    $import_success_count++;
775*ae1afd2fSChristopher Smith                } else {
776*ae1afd2fSChristopher Smith                    $import_fail_count++;
777*ae1afd2fSChristopher Smith                    $this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
778*ae1afd2fSChristopher Smith                }
779*ae1afd2fSChristopher Smith            }
780*ae1afd2fSChristopher Smith            msg(sprintf($this->lang['import_success_count'], ($import_success_count+$import_fail_count), $import_success_count),($import_success_count ? 1 : -1));
781*ae1afd2fSChristopher Smith            if ($import_fail_count) {
782*ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count),-1);
783*ae1afd2fSChristopher Smith            }
784*ae1afd2fSChristopher Smith        } else {
785*ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'],-1);
786*ae1afd2fSChristopher Smith        }
787*ae1afd2fSChristopher Smith
788*ae1afd2fSChristopher Smith        // save import failures into the session
789*ae1afd2fSChristopher Smith        if (!headers_sent()) {
790*ae1afd2fSChristopher Smith          session_start();
791*ae1afd2fSChristopher Smith          $_SESSION['import_failures'] = $this->_import_failures;
792*ae1afd2fSChristopher Smith          session_write_close();
793*ae1afd2fSChristopher Smith        }
794*ae1afd2fSChristopher Smith    }
795*ae1afd2fSChristopher Smith
796*ae1afd2fSChristopher Smith    function _cleanImportUser($candidate, & $error){
797*ae1afd2fSChristopher Smith        global $INPUT;
798*ae1afd2fSChristopher Smith
799*ae1afd2fSChristopher Smith        // kludgy ....
800*ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
801*ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
802*ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
803*ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
804*ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
805*ae1afd2fSChristopher Smith
806*ae1afd2fSChristopher Smith        $cleaned = $this->_retrieveUser();
807*ae1afd2fSChristopher Smith        list($user,$pass,$name,$mail,$grps) = $cleaned;
808*ae1afd2fSChristopher Smith        if (empty($user)) {
809*ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
810*ae1afd2fSChristopher Smith            return false;
811*ae1afd2fSChristopher Smith        }
812*ae1afd2fSChristopher Smith
813*ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
814*ae1afd2fSChristopher Smith
815*ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modName') xor empty($name))){
816*ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
817*ae1afd2fSChristopher Smith            return false;
818*ae1afd2fSChristopher Smith        }
819*ae1afd2fSChristopher Smith
820*ae1afd2fSChristopher Smith        if (!($this->_auth->canDo('modMail') xor empty($mail))){
821*ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badmail'];
822*ae1afd2fSChristopher Smith            return false;
823*ae1afd2fSChristopher Smith        }
824*ae1afd2fSChristopher Smith
825*ae1afd2fSChristopher Smith        return $cleaned;
826*ae1afd2fSChristopher Smith    }
827*ae1afd2fSChristopher Smith
828*ae1afd2fSChristopher Smith    function _addImportUser($user, & $error){
829*ae1afd2fSChristopher Smith        if (!$this->_auth->triggerUserMod('create', $user)) {
830*ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
831*ae1afd2fSChristopher Smith            return false;
832*ae1afd2fSChristopher Smith        }
833*ae1afd2fSChristopher Smith
834*ae1afd2fSChristopher Smith        return true;
835*ae1afd2fSChristopher Smith    }
836*ae1afd2fSChristopher Smith
837*ae1afd2fSChristopher Smith    function _downloadImportFailures(){
838*ae1afd2fSChristopher Smith
839*ae1afd2fSChristopher Smith        // ==============================================================================================
840*ae1afd2fSChristopher Smith        // GENERATE OUTPUT
841*ae1afd2fSChristopher Smith        // normal headers for downloading...
842*ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
843*ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
844*ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
845*ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
846*ae1afd2fSChristopher Smith
847*ae1afd2fSChristopher Smith        // output the csv
848*ae1afd2fSChristopher Smith        $fd = fopen('php://output','w');
849*ae1afd2fSChristopher Smith        foreach ($this->_import_failures as $line => $fail) {
850*ae1afd2fSChristopher Smith            fputs($fd, $fail['orig']);
851*ae1afd2fSChristopher Smith        }
852*ae1afd2fSChristopher Smith        fclose($fd);
853*ae1afd2fSChristopher Smith        die;
854*ae1afd2fSChristopher Smith    }
855*ae1afd2fSChristopher Smith
8560440ff15Schris}
857