xref: /dokuwiki/lib/plugins/usermanager/admin.php (revision c8f554593ad2f272c18feca04beccedaa06dde05)
10440ff15Schris<?php
254cc7aa4SAndreas Gohr
38553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
451ee2399SGerrit Uitslaguse dokuwiki\Extension\AuthPlugin;
554cc7aa4SAndreas Gohruse dokuwiki\Utf8\Clean;
653c68e5cSAndreas Gohruse dokuwiki\Utf8\Conversion;
7a664aabaSAndreas Gohr
80440ff15Schris/*
90440ff15Schris *  User Manager
100440ff15Schris *
110440ff15Schris *  Dokuwiki Admin Plugin
120440ff15Schris *
130440ff15Schris *  This version of the user manager has been modified to only work with
140440ff15Schris *  objectified version of auth system
150440ff15Schris *
160440ff15Schris *  @author  neolao <neolao@neolao.com>
170440ff15Schris *  @author  Chris Smith <chris@jalakai.co.uk>
180440ff15Schris */
19a664aabaSAndreas Gohr
200440ff15Schris/**
210440ff15Schris * All DokuWiki plugins to extend the admin function
220440ff15Schris * need to inherit from this class
230440ff15Schris */
248553d24dSAndreas Gohrclass admin_plugin_usermanager extends AdminPlugin
253a97d936SAndreas Gohr{
26bf9be0e3SAndreas Gohr    protected const IMAGE_DIR = DOKU_BASE . 'lib/plugins/usermanager/images/';
270440ff15Schris
2854cc7aa4SAndreas Gohr    protected $auth;        // auth object
293a97d936SAndreas Gohr    protected $users_total = 0;     // number of registered users
3054cc7aa4SAndreas Gohr    protected $filter = [];   // user selection filter(s)
313a97d936SAndreas Gohr    protected $start = 0;          // index of first user to be displayed
323a97d936SAndreas Gohr    protected $last = 0;           // index of the last user to be displayed
333a97d936SAndreas Gohr    protected $pagesize = 20;      // number of users to list on one page
343a97d936SAndreas Gohr    protected $edit_user = '';     // set to user selected for editing
3554cc7aa4SAndreas Gohr    protected $edit_userdata = [];
363a97d936SAndreas Gohr    protected $disabled = '';      // if disabled set to explanatory string
3754cc7aa4SAndreas Gohr    protected $import_failures = [];
383a97d936SAndreas Gohr    protected $lastdisabled = false; // set to true if last user is unknown and last button is hence buggy
390440ff15Schris
400440ff15Schris    /**
410440ff15Schris     * Constructor
420440ff15Schris     */
433a97d936SAndreas Gohr    public function __construct()
443a97d936SAndreas Gohr    {
4551ee2399SGerrit Uitslag        /** @var AuthPlugin $auth */
460440ff15Schris        global $auth;
470440ff15Schris
480440ff15Schris        $this->setupLocale();
4951d94d49Schris
506547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin) {
513a97d936SAndreas Gohr            $this->disabled = $this->lang['noauth'];
5282fd59b6SAndreas Gohr        } elseif (!$auth->canDo('getUsers')) {
533a97d936SAndreas Gohr            $this->disabled = $this->lang['nosupport'];
5451d94d49Schris        } else {
5551d94d49Schris            // we're good to go
563a97d936SAndreas Gohr            $this->auth = &$auth;
5751d94d49Schris        }
58ae1afd2fSChristopher Smith
59ae1afd2fSChristopher Smith        // attempt to retrieve any import failures from the session
600e80bb5eSChristopher Smith        if (!empty($_SESSION['import_failures'])) {
613a97d936SAndreas Gohr            $this->import_failures = $_SESSION['import_failures'];
62ae1afd2fSChristopher Smith        }
630440ff15Schris    }
640440ff15Schris
650440ff15Schris    /**
66c5a7c0c6SGerrit Uitslag     * Return prompt for admin menu
67253d4b48SGerrit Uitslag     *
68253d4b48SGerrit Uitslag     * @param string $language
69253d4b48SGerrit Uitslag     * @return string
700440ff15Schris     */
713a97d936SAndreas Gohr    public function getMenuText($language)
723a97d936SAndreas Gohr    {
730440ff15Schris
743a97d936SAndreas Gohr        if (!is_null($this->auth))
750440ff15Schris            return parent::getMenuText($language);
760440ff15Schris
773a97d936SAndreas Gohr        return $this->getLang('menu') . ' ' . $this->disabled;
780440ff15Schris    }
790440ff15Schris
800440ff15Schris    /**
810440ff15Schris     * return sort order for position in admin menu
82253d4b48SGerrit Uitslag     *
83253d4b48SGerrit Uitslag     * @return int
840440ff15Schris     */
853a97d936SAndreas Gohr    public function getMenuSort()
863a97d936SAndreas Gohr    {
870440ff15Schris        return 2;
880440ff15Schris    }
890440ff15Schris
900440ff15Schris    /**
9167a31a83SMichael Große     * @return int current start value for pageination
9267a31a83SMichael Große     */
933a97d936SAndreas Gohr    public function getStart()
943a97d936SAndreas Gohr    {
953a97d936SAndreas Gohr        return $this->start;
9667a31a83SMichael Große    }
9767a31a83SMichael Große
9867a31a83SMichael Große    /**
9967a31a83SMichael Große     * @return int number of users per page
10067a31a83SMichael Große     */
1013a97d936SAndreas Gohr    public function getPagesize()
1023a97d936SAndreas Gohr    {
1033a97d936SAndreas Gohr        return $this->pagesize;
10467a31a83SMichael Große    }
10567a31a83SMichael Große
10667a31a83SMichael Große    /**
107462e9e37SMichael Große     * @param boolean $lastdisabled
108462e9e37SMichael Große     */
1093a97d936SAndreas Gohr    public function setLastdisabled($lastdisabled)
1103a97d936SAndreas Gohr    {
1113a97d936SAndreas Gohr        $this->lastdisabled = $lastdisabled;
112462e9e37SMichael Große    }
113462e9e37SMichael Große
114462e9e37SMichael Große    /**
115c5a7c0c6SGerrit Uitslag     * Handle user request
116253d4b48SGerrit Uitslag     *
117253d4b48SGerrit Uitslag     * @return bool
1180440ff15Schris     */
1193a97d936SAndreas Gohr    public function handle()
1203a97d936SAndreas Gohr    {
12100d58927SMichael Hamann        global $INPUT;
1223a97d936SAndreas Gohr        if (is_null($this->auth)) return false;
1230440ff15Schris
1240440ff15Schris        // extract the command and any specific parameters
1250440ff15Schris        // submit button name is of the form - fn[cmd][param(s)]
12600d58927SMichael Hamann        $fn = $INPUT->param('fn');
1270440ff15Schris
1280440ff15Schris        if (is_array($fn)) {
1290440ff15Schris            $cmd = key($fn);
1300440ff15Schris            $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
1310440ff15Schris        } else {
1320440ff15Schris            $cmd = $fn;
1330440ff15Schris            $param = null;
1340440ff15Schris        }
1350440ff15Schris
1360440ff15Schris        if ($cmd != "search") {
1373a97d936SAndreas Gohr            $this->start = $INPUT->int('start', 0);
1383a97d936SAndreas Gohr            $this->filter = $this->retrieveFilter();
1390440ff15Schris        }
1400440ff15Schris
1410440ff15Schris        switch ($cmd) {
1423a97d936SAndreas Gohr            case "add":
1433a97d936SAndreas Gohr                $this->addUser();
1440440ff15Schris                break;
1453a97d936SAndreas Gohr            case "delete":
1463a97d936SAndreas Gohr                $this->deleteUser();
1473a97d936SAndreas Gohr                break;
1483a97d936SAndreas Gohr            case "modify":
1493a97d936SAndreas Gohr                $this->modifyUser();
1503a97d936SAndreas Gohr                break;
1513a97d936SAndreas Gohr            case "edit":
1523a97d936SAndreas Gohr                $this->editUser($param);
1533a97d936SAndreas Gohr                break;
1543a97d936SAndreas Gohr            case "search":
1553a97d936SAndreas Gohr                $this->setFilter($param);
1563a97d936SAndreas Gohr                $this->start = 0;
1573a97d936SAndreas Gohr                break;
1583a97d936SAndreas Gohr            case "export":
1593a97d936SAndreas Gohr                $this->exportCSV();
1603a97d936SAndreas Gohr                break;
1613a97d936SAndreas Gohr            case "import":
1623a97d936SAndreas Gohr                $this->importCSV();
1633a97d936SAndreas Gohr                break;
1643a97d936SAndreas Gohr            case "importfails":
1653a97d936SAndreas Gohr                $this->downloadImportFailures();
1663a97d936SAndreas Gohr                break;
1670440ff15Schris        }
1680440ff15Schris
1693a97d936SAndreas Gohr        $this->users_total = $this->auth->canDo('getUserCount') ? $this->auth->getUserCount($this->filter) : -1;
1700440ff15Schris
1710440ff15Schris        // page handling
1720440ff15Schris        switch ($cmd) {
1733a97d936SAndreas Gohr            case 'start':
1743a97d936SAndreas Gohr                $this->start = 0;
1753a97d936SAndreas Gohr                break;
1763a97d936SAndreas Gohr            case 'prev':
1773a97d936SAndreas Gohr                $this->start -= $this->pagesize;
1783a97d936SAndreas Gohr                break;
1793a97d936SAndreas Gohr            case 'next':
1803a97d936SAndreas Gohr                $this->start += $this->pagesize;
1813a97d936SAndreas Gohr                break;
1823a97d936SAndreas Gohr            case 'last':
1833a97d936SAndreas Gohr                $this->start = $this->users_total;
1843a97d936SAndreas Gohr                break;
1850440ff15Schris        }
1863a97d936SAndreas Gohr        $this->validatePagination();
187c5a7c0c6SGerrit Uitslag        return true;
1880440ff15Schris    }
1890440ff15Schris
1900440ff15Schris    /**
191c5a7c0c6SGerrit Uitslag     * Output appropriate html
192253d4b48SGerrit Uitslag     *
193253d4b48SGerrit Uitslag     * @return bool
194a664aabaSAndreas Gohr     * @todo split into smaller functions, use Form class
1950440ff15Schris     */
1963a97d936SAndreas Gohr    public function html()
1973a97d936SAndreas Gohr    {
1980440ff15Schris        global $ID;
1990440ff15Schris
2003a97d936SAndreas Gohr        if (is_null($this->auth)) {
20126dfc232SAndreas Gohr            echo $this->lang['badauth'];
2020440ff15Schris            return false;
2030440ff15Schris        }
2040440ff15Schris
2053a97d936SAndreas Gohr        $user_list = $this->auth->retrieveUsers($this->start, $this->pagesize, $this->filter);
2060440ff15Schris
2073a97d936SAndreas Gohr        $page_buttons = $this->pagination();
2083a97d936SAndreas Gohr        $delete_disable = $this->auth->canDo('delUser') ? '' : 'disabled="disabled"';
2090440ff15Schris
2103a97d936SAndreas Gohr        $editable = $this->auth->canDo('UserMod');
2113a97d936SAndreas Gohr        $export_label = empty($this->filter) ? $this->lang['export_all'] : $this->lang['export_filtered'];
2126154103cSmatthiasgrimm
21326dfc232SAndreas Gohr        echo $this->locale_xhtml('intro');
21426dfc232SAndreas Gohr        echo $this->locale_xhtml('list');
2150440ff15Schris
216a664aabaSAndreas Gohr        echo '<div id="user__manager">';
217a664aabaSAndreas Gohr        echo '<div class="level2">';
2180440ff15Schris
2193a97d936SAndreas Gohr        if ($this->users_total > 0) {
220a664aabaSAndreas Gohr            printf(
221a664aabaSAndreas Gohr                '<p>' . $this->lang['summary'] . '</p>',
2223a97d936SAndreas Gohr                $this->start + 1,
2233a97d936SAndreas Gohr                $this->last,
2243a97d936SAndreas Gohr                $this->users_total,
2253a97d936SAndreas Gohr                $this->auth->getUserCount()
22664159a61SAndreas Gohr            );
2270440ff15Schris        } else {
2283a97d936SAndreas Gohr            if ($this->users_total < 0) {
229a102b175SGerrit Uitslag                $allUserTotal = 0;
230a102b175SGerrit Uitslag            } else {
2313a97d936SAndreas Gohr                $allUserTotal = $this->auth->getUserCount();
232a102b175SGerrit Uitslag            }
233a664aabaSAndreas Gohr            printf('<p>%s</p>', sprintf($this->lang['nonefound'], $allUserTotal));
2340440ff15Schris        }
235a664aabaSAndreas Gohr        printf('<form action="%s" method="post">', wl($ID));
236634d7150SAndreas Gohr        formSecurityToken();
237a664aabaSAndreas Gohr        echo '<div class="table">';
238a664aabaSAndreas Gohr        echo '<table class="inline">';
239a664aabaSAndreas Gohr        echo '<thead>';
240a664aabaSAndreas Gohr        echo '<tr>';
241a664aabaSAndreas Gohr        echo '<th>&#160;</th>';
242a664aabaSAndreas Gohr        echo '<th>' . $this->lang["user_id"] . '</th>';
243a664aabaSAndreas Gohr        echo '<th>' . $this->lang["user_name"] . '</th>';
244a664aabaSAndreas Gohr        echo '<th>' . $this->lang["user_mail"] . '</th>';
245a664aabaSAndreas Gohr        echo '<th>' . $this->lang["user_groups"] . '</th>';
246a664aabaSAndreas Gohr        echo '</tr>';
2470440ff15Schris
248a664aabaSAndreas Gohr        echo '<tr>';
249a664aabaSAndreas Gohr        echo '<td class="rightalign"><input type="image" src="' .
250a664aabaSAndreas Gohr            self::IMAGE_DIR . 'search.png" name="fn[search][new]" title="' .
251a664aabaSAndreas Gohr            $this->lang['search_prompt'] . '" alt="' . $this->lang['search'] . '" class="button" /></td>';
252a664aabaSAndreas Gohr        echo '<td><input type="text" name="userid" class="edit" value="' . $this->htmlFilter('user') . '" /></td>';
253a664aabaSAndreas Gohr        echo '<td><input type="text" name="username" class="edit" value="' . $this->htmlFilter('name') . '" /></td>';
254a664aabaSAndreas Gohr        echo '<td><input type="text" name="usermail" class="edit" value="' . $this->htmlFilter('mail') . '" /></td>';
255a664aabaSAndreas Gohr        echo '<td><input type="text" name="usergroups" class="edit" value="' . $this->htmlFilter('grps') . '" /></td>';
256a664aabaSAndreas Gohr        echo '</tr>';
257a664aabaSAndreas Gohr        echo '</thead>';
2580440ff15Schris
2593a97d936SAndreas Gohr        if ($this->users_total) {
260a664aabaSAndreas Gohr            echo '<tbody>';
2610440ff15Schris            foreach ($user_list as $user => $userinfo) {
2620440ff15Schris                extract($userinfo);
263c5a7c0c6SGerrit Uitslag                /**
264c5a7c0c6SGerrit Uitslag                 * @var string $name
265c5a7c0c6SGerrit Uitslag                 * @var string $pass
266c5a7c0c6SGerrit Uitslag                 * @var string $mail
267c5a7c0c6SGerrit Uitslag                 * @var array $grps
268c5a7c0c6SGerrit Uitslag                 */
26954cc7aa4SAndreas Gohr                $groups = implode(', ', $grps);
270a664aabaSAndreas Gohr                echo '<tr class="user_info">';
271a664aabaSAndreas Gohr                echo '<td class="centeralign"><input type="checkbox" name="delete[' . hsc($user) .
272a664aabaSAndreas Gohr                    ']" ' . $delete_disable . ' /></td>';
2732365d73dSAnika Henke                if ($editable) {
274a664aabaSAndreas Gohr                    echo '<td><a href="' . wl($ID, ['fn[edit][' . $user . ']' => 1,
27577d19185SAndreas Gohr                            'do' => 'admin',
27677d19185SAndreas Gohr                            'page' => 'usermanager',
27754cc7aa4SAndreas Gohr                            'sectok' => getSecurityToken()]) .
278a664aabaSAndreas Gohr                        '" title="' . $this->lang['edit_prompt'] . '">' . hsc($user) . '</a></td>';
2792365d73dSAnika Henke                } else {
280a664aabaSAndreas Gohr                    echo '<td>' . hsc($user) . '</td>';
2812365d73dSAnika Henke                }
282a664aabaSAndreas Gohr                echo '<td>' . hsc($name) . '</td><td>' . hsc($mail) . '</td><td>' . hsc($groups) . '</td>';
283a664aabaSAndreas Gohr                echo '</tr>';
2840440ff15Schris            }
285a664aabaSAndreas Gohr            echo '</tbody>';
2860440ff15Schris        }
2870440ff15Schris
288a664aabaSAndreas Gohr        echo '<tbody>';
289a664aabaSAndreas Gohr        echo '<tr><td colspan="5" class="centeralign">';
290a664aabaSAndreas Gohr        echo '<span class="medialeft">';
291a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[delete]" id="usrmgr__del" ' . $delete_disable . '>' .
292a664aabaSAndreas Gohr            $this->lang['delete_selected'] . '</button>';
293a664aabaSAndreas Gohr        echo '</span>';
294a664aabaSAndreas Gohr        echo '<span class="mediaright">';
295a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[start]" ' . $page_buttons['start'] . '>' .
296a664aabaSAndreas Gohr            $this->lang['start'] . '</button>';
297a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[prev]" ' . $page_buttons['prev'] . '>' .
298a664aabaSAndreas Gohr            $this->lang['prev'] . "</button>";
299a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[next]" ' . $page_buttons['next'] . '>' .
300a664aabaSAndreas Gohr            $this->lang['next'] . '</button>';
301a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[last]" ' . $page_buttons['last'] . '>' .
302a664aabaSAndreas Gohr            $this->lang['last'] . '</button>';
303a664aabaSAndreas Gohr        echo '</span>';
3043a97d936SAndreas Gohr        if (!empty($this->filter)) {
305a664aabaSAndreas Gohr            echo '<button type="submit" name="fn[search][clear]">' . $this->lang['clear'] . '</button>';
3065c967d3dSChristopher Smith        }
307a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[export]">' . $export_label . '</button>';
308a664aabaSAndreas Gohr        echo '<input type="hidden" name="do"    value="admin" />';
309a664aabaSAndreas Gohr        echo '<input type="hidden" name="page"  value="usermanager" />';
310daf4ca4eSAnika Henke
3113a97d936SAndreas Gohr        $this->htmlFilterSettings(2);
312daf4ca4eSAnika Henke
313a664aabaSAndreas Gohr        echo '</td></tr>';
314a664aabaSAndreas Gohr        echo '</tbody>';
315a664aabaSAndreas Gohr        echo '</table>';
316a664aabaSAndreas Gohr        echo '</div>';
3170440ff15Schris
318a664aabaSAndreas Gohr        echo '</form>';
319a664aabaSAndreas Gohr        echo '</div>';
3200440ff15Schris
321a664aabaSAndreas Gohr        $style = $this->edit_user ? ' class="edit_user"' : '';
3220440ff15Schris
3233a97d936SAndreas Gohr        if ($this->auth->canDo('addUser')) {
324a664aabaSAndreas Gohr            echo '<div' . $style . '>';
32526dfc232SAndreas Gohr            echo $this->locale_xhtml('add');
326a664aabaSAndreas Gohr            echo '<div class="level2">';
3270440ff15Schris
32854cc7aa4SAndreas Gohr            $this->htmlUserForm('add', null, [], 4);
3290440ff15Schris
330a664aabaSAndreas Gohr            echo '</div>';
331a664aabaSAndreas Gohr            echo '</div>';
3320440ff15Schris        }
3330440ff15Schris
3343a97d936SAndreas Gohr        if ($this->edit_user && $this->auth->canDo('UserMod')) {
335a664aabaSAndreas Gohr            echo '<div' . $style . ' id="scroll__here">';
33626dfc232SAndreas Gohr            echo $this->locale_xhtml('edit');
337a664aabaSAndreas Gohr            echo '<div class="level2">';
3380440ff15Schris
3393a97d936SAndreas Gohr            $this->htmlUserForm('modify', $this->edit_user, $this->edit_userdata, 4);
3400440ff15Schris
341a664aabaSAndreas Gohr            echo '</div>';
342a664aabaSAndreas Gohr            echo '</div>';
3430440ff15Schris        }
344ae1afd2fSChristopher Smith
3453a97d936SAndreas Gohr        if ($this->auth->canDo('addUser')) {
3463a97d936SAndreas Gohr            $this->htmlImportForm();
347ae1afd2fSChristopher Smith        }
348a664aabaSAndreas Gohr        echo '</div>';
349c5a7c0c6SGerrit Uitslag        return true;
3500440ff15Schris    }
3510440ff15Schris
35282fd59b6SAndreas Gohr    /**
35364cdf779SAndreas Gohr     * User Manager is only available if the auth backend supports it
35464cdf779SAndreas Gohr     *
35564cdf779SAndreas Gohr     * @inheritdoc
35664cdf779SAndreas Gohr     * @return bool
35764cdf779SAndreas Gohr     */
35864cdf779SAndreas Gohr    public function isAccessibleByCurrentUser()
35964cdf779SAndreas Gohr    {
36051ee2399SGerrit Uitslag        /** @var AuthPlugin $auth */
36164cdf779SAndreas Gohr        global $auth;
3626547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin || !$auth->canDo('getUsers')) {
36364cdf779SAndreas Gohr            return false;
36464cdf779SAndreas Gohr        }
36564cdf779SAndreas Gohr
36664cdf779SAndreas Gohr        return parent::isAccessibleByCurrentUser();
36764cdf779SAndreas Gohr    }
36864cdf779SAndreas Gohr
36964cdf779SAndreas Gohr
37064cdf779SAndreas Gohr    /**
371c5a7c0c6SGerrit Uitslag     * Display form to add or modify a user
372c5a7c0c6SGerrit Uitslag     *
373c5a7c0c6SGerrit Uitslag     * @param string $cmd 'add' or 'modify'
374c5a7c0c6SGerrit Uitslag     * @param string $user id of user
375c5a7c0c6SGerrit Uitslag     * @param array $userdata array with name, mail, pass and grps
376c5a7c0c6SGerrit Uitslag     * @param int $indent
377a664aabaSAndreas Gohr     * @todo use Form class
37882fd59b6SAndreas Gohr     */
37954cc7aa4SAndreas Gohr    protected function htmlUserForm($cmd, $user = '', $userdata = [], $indent = 0)
3803a97d936SAndreas Gohr    {
381a6858c6aSchris        global $conf;
382bb4866bdSchris        global $ID;
383be9008d3SChristopher Smith        global $lang;
38454cc7aa4SAndreas Gohr        $name = '';
38554cc7aa4SAndreas Gohr        $mail = '';
38654cc7aa4SAndreas Gohr        $groups = '';
38754cc7aa4SAndreas Gohr        $notes = [];
3880440ff15Schris
3890440ff15Schris        if ($user) {
39078c7c8c9Schris            extract($userdata);
39154cc7aa4SAndreas Gohr            if (!empty($grps)) $groups = implode(',', $grps);
392a6858c6aSchris        } else {
393a6858c6aSchris            $notes[] = sprintf($this->lang['note_group'], $conf['defaultgroup']);
3940440ff15Schris        }
3950440ff15Schris
396a664aabaSAndreas Gohr        printf('<form action="%s" method="post">', wl($ID));
397634d7150SAndreas Gohr        formSecurityToken();
398a664aabaSAndreas Gohr        echo '<div class="table">';
399a664aabaSAndreas Gohr        echo '<table class="inline">';
400a664aabaSAndreas Gohr        echo '<thead>';
401a664aabaSAndreas Gohr        echo '<tr><th>' . $this->lang["field"] . "</th><th>" . $this->lang["value"] . "</th></tr>";
402a664aabaSAndreas Gohr        echo '</thead>';
403a664aabaSAndreas Gohr        echo '<tbody>';
40426fb387bSchris
4053a97d936SAndreas Gohr        $this->htmlInputField(
40664159a61SAndreas Gohr            $cmd . "_userid",
40764159a61SAndreas Gohr            "userid",
40864159a61SAndreas Gohr            $this->lang["user_id"],
40964159a61SAndreas Gohr            $user,
4103a97d936SAndreas Gohr            $this->auth->canDo("modLogin"),
41164159a61SAndreas Gohr            true,
41264159a61SAndreas Gohr            $indent + 6
41364159a61SAndreas Gohr        );
4143a97d936SAndreas Gohr        $this->htmlInputField(
41564159a61SAndreas Gohr            $cmd . "_userpass",
41664159a61SAndreas Gohr            "userpass",
41764159a61SAndreas Gohr            $this->lang["user_pass"],
41864159a61SAndreas Gohr            "",
4193a97d936SAndreas Gohr            $this->auth->canDo("modPass"),
42064159a61SAndreas Gohr            false,
42164159a61SAndreas Gohr            $indent + 6
42264159a61SAndreas Gohr        );
4233a97d936SAndreas Gohr        $this->htmlInputField(
42464159a61SAndreas Gohr            $cmd . "_userpass2",
42564159a61SAndreas Gohr            "userpass2",
42664159a61SAndreas Gohr            $lang["passchk"],
42764159a61SAndreas Gohr            "",
4283a97d936SAndreas Gohr            $this->auth->canDo("modPass"),
42964159a61SAndreas Gohr            false,
43064159a61SAndreas Gohr            $indent + 6
43164159a61SAndreas Gohr        );
4323a97d936SAndreas Gohr        $this->htmlInputField(
43364159a61SAndreas Gohr            $cmd . "_username",
43464159a61SAndreas Gohr            "username",
43564159a61SAndreas Gohr            $this->lang["user_name"],
43664159a61SAndreas Gohr            $name,
4373a97d936SAndreas Gohr            $this->auth->canDo("modName"),
43864159a61SAndreas Gohr            true,
43964159a61SAndreas Gohr            $indent + 6
44064159a61SAndreas Gohr        );
4413a97d936SAndreas Gohr        $this->htmlInputField(
44264159a61SAndreas Gohr            $cmd . "_usermail",
44364159a61SAndreas Gohr            "usermail",
44464159a61SAndreas Gohr            $this->lang["user_mail"],
44564159a61SAndreas Gohr            $mail,
4463a97d936SAndreas Gohr            $this->auth->canDo("modMail"),
44764159a61SAndreas Gohr            true,
44864159a61SAndreas Gohr            $indent + 6
44964159a61SAndreas Gohr        );
4503a97d936SAndreas Gohr        $this->htmlInputField(
45164159a61SAndreas Gohr            $cmd . "_usergroups",
45264159a61SAndreas Gohr            "usergroups",
45364159a61SAndreas Gohr            $this->lang["user_groups"],
45464159a61SAndreas Gohr            $groups,
4553a97d936SAndreas Gohr            $this->auth->canDo("modGroups"),
45664159a61SAndreas Gohr            false,
45764159a61SAndreas Gohr            $indent + 6
45864159a61SAndreas Gohr        );
45926fb387bSchris
4603a97d936SAndreas Gohr        if ($this->auth->canDo("modPass")) {
461ee9498f5SChristopher Smith            if ($cmd == 'add') {
462c3f4fb63SGina Haeussge                $notes[] = $this->lang['note_pass'];
463ee9498f5SChristopher Smith            }
464a6858c6aSchris            if ($user) {
465a6858c6aSchris                $notes[] = $this->lang['note_notify'];
466a6858c6aSchris            }
467a6858c6aSchris
468a664aabaSAndreas Gohr            echo '<tr><td><label for="' . $cmd . "_usernotify\" >" .
469a664aabaSAndreas Gohr                $this->lang["user_notify"] . ': </label></td>
470a664aabaSAndreas Gohr                 <td><input type="checkbox" id="' . $cmd . '_usernotify" name="usernotify" value="1" />
471a664aabaSAndreas Gohr                 </td></tr>';
472a6858c6aSchris        }
473a6858c6aSchris
474a664aabaSAndreas Gohr        echo '</tbody>';
475a664aabaSAndreas Gohr        echo '<tbody>';
476a664aabaSAndreas Gohr        echo '<tr>';
477a664aabaSAndreas Gohr        echo '<td colspan="2">';
478a664aabaSAndreas Gohr        echo '<input type="hidden" name="do"    value="admin" />';
479a664aabaSAndreas Gohr        echo '<input type="hidden" name="page"  value="usermanager" />';
4800440ff15Schris
4810440ff15Schris        // save current $user, we need this to access details if the name is changed
482a664aabaSAndreas Gohr        if ($user) {
483a664aabaSAndreas Gohr            echo '<input type="hidden" name="userid_old"  value="' . hsc($user) . "\" />";
484a664aabaSAndreas Gohr        }
4850440ff15Schris
4863a97d936SAndreas Gohr        $this->htmlFilterSettings($indent + 10);
4870440ff15Schris
488a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[' . $cmd . ']">' . $this->lang[$cmd] . '</button>';
489a664aabaSAndreas Gohr        echo '</td>';
490a664aabaSAndreas Gohr        echo '</tr>';
491a664aabaSAndreas Gohr        echo '</tbody>';
492a664aabaSAndreas Gohr        echo '</table>';
49345c19902SChristopher Smith
49445c19902SChristopher Smith        if ($notes) {
495a664aabaSAndreas Gohr            echo '<ul class="notes">';
49645c19902SChristopher Smith            foreach ($notes as $note) {
497a664aabaSAndreas Gohr                echo '<li><span class="li">' . $note . '</li>';
49845c19902SChristopher Smith            }
499a664aabaSAndreas Gohr            echo '</ul>';
50045c19902SChristopher Smith        }
501a664aabaSAndreas Gohr        echo '</div>';
502a664aabaSAndreas Gohr        echo '</form>';
5030440ff15Schris    }
5040440ff15Schris
505c5a7c0c6SGerrit Uitslag    /**
506c5a7c0c6SGerrit Uitslag     * Prints a inputfield
507c5a7c0c6SGerrit Uitslag     *
508c5a7c0c6SGerrit Uitslag     * @param string $id
509c5a7c0c6SGerrit Uitslag     * @param string $name
510c5a7c0c6SGerrit Uitslag     * @param string $label
511c5a7c0c6SGerrit Uitslag     * @param string $value
512c5a7c0c6SGerrit Uitslag     * @param bool $cando whether auth backend is capable to do this action
5139b82d43fSAndreas Gohr     * @param bool $required is this field required?
514c5a7c0c6SGerrit Uitslag     * @param int $indent
515a664aabaSAndreas Gohr     * @todo obsolete when Form class is used
516c5a7c0c6SGerrit Uitslag     */
5173a97d936SAndreas Gohr    protected function htmlInputField($id, $name, $label, $value, $cando, $required, $indent = 0)
5183a97d936SAndreas Gohr    {
5197de12fceSAndreas Gohr        $class = $cando ? '' : ' class="disabled"';
5207de12fceSAndreas Gohr        echo str_pad('', $indent);
5217de12fceSAndreas Gohr
522359e9417SChristopher Smith        if ($name == 'userpass' || $name == 'userpass2') {
523d796a891SAndreas Gohr            $fieldtype = 'password';
524d796a891SAndreas Gohr            $autocomp = 'autocomplete="off"';
5257b3674bdSChristopher Smith        } elseif ($name == 'usermail') {
5267b3674bdSChristopher Smith            $fieldtype = 'email';
5277b3674bdSChristopher Smith            $autocomp = '';
528d796a891SAndreas Gohr        } else {
529d796a891SAndreas Gohr            $fieldtype = 'text';
530d796a891SAndreas Gohr            $autocomp = '';
531d796a891SAndreas Gohr        }
532f23f9594SAndreas Gohr        $value = hsc($value);
533d796a891SAndreas Gohr
5347de12fceSAndreas Gohr        echo "<tr $class>";
5357de12fceSAndreas Gohr        echo "<td><label for=\"$id\" >$label: </label></td>";
536a664aabaSAndreas Gohr        echo '<td>';
5377de12fceSAndreas Gohr        if ($cando) {
5389b82d43fSAndreas Gohr            $req = '';
5399b82d43fSAndreas Gohr            if ($required) $req = 'required="required"';
54064159a61SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\"
54164159a61SAndreas Gohr                  value=\"$value\" class=\"edit\" $autocomp $req />";
5427de12fceSAndreas Gohr        } else {
5437de12fceSAndreas Gohr            echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
54464159a61SAndreas Gohr            echo "<input type=\"$fieldtype\" id=\"$id\" name=\"$name\"
54564159a61SAndreas Gohr                  value=\"$value\" class=\"edit disabled\" disabled=\"disabled\" />";
5467de12fceSAndreas Gohr        }
547a664aabaSAndreas Gohr        echo '</td>';
548a664aabaSAndreas Gohr        echo '</tr>';
54926fb387bSchris    }
55026fb387bSchris
551c5a7c0c6SGerrit Uitslag    /**
552c5a7c0c6SGerrit Uitslag     * Returns htmlescaped filter value
553c5a7c0c6SGerrit Uitslag     *
554c5a7c0c6SGerrit Uitslag     * @param string $key name of search field
555c5a7c0c6SGerrit Uitslag     * @return string html escaped value
556c5a7c0c6SGerrit Uitslag     */
5573a97d936SAndreas Gohr    protected function htmlFilter($key)
5583a97d936SAndreas Gohr    {
5593a97d936SAndreas Gohr        if (empty($this->filter)) return '';
5603a97d936SAndreas Gohr        return (isset($this->filter[$key]) ? hsc($this->filter[$key]) : '');
5610440ff15Schris    }
5620440ff15Schris
563c5a7c0c6SGerrit Uitslag    /**
564c5a7c0c6SGerrit Uitslag     * Print hidden inputs with the current filter values
565c5a7c0c6SGerrit Uitslag     *
566c5a7c0c6SGerrit Uitslag     * @param int $indent
567c5a7c0c6SGerrit Uitslag     */
5683a97d936SAndreas Gohr    protected function htmlFilterSettings($indent = 0)
5693a97d936SAndreas Gohr    {
5700440ff15Schris
571a664aabaSAndreas Gohr        echo '<input type="hidden" name="start" value="' . $this->start . '" />';
5720440ff15Schris
5733a97d936SAndreas Gohr        foreach ($this->filter as $key => $filter) {
574a664aabaSAndreas Gohr            echo '<input type="hidden" name="filter[' . $key . ']" value="' . hsc($filter) . '" />';
5750440ff15Schris        }
5760440ff15Schris    }
5770440ff15Schris
578c5a7c0c6SGerrit Uitslag    /**
579c5a7c0c6SGerrit Uitslag     * Print import form and summary of previous import
580c5a7c0c6SGerrit Uitslag     *
581c5a7c0c6SGerrit Uitslag     * @param int $indent
582c5a7c0c6SGerrit Uitslag     */
5833a97d936SAndreas Gohr    protected function htmlImportForm($indent = 0)
5843a97d936SAndreas Gohr    {
585ae1afd2fSChristopher Smith        global $ID;
586ae1afd2fSChristopher Smith
58754cc7aa4SAndreas Gohr        $failure_download_link = wl($ID, ['do' => 'admin', 'page' => 'usermanager', 'fn[importfails]' => 1]);
588ae1afd2fSChristopher Smith
589a664aabaSAndreas Gohr        echo '<div class="level2 import_users">';
59026dfc232SAndreas Gohr        echo $this->locale_xhtml('import');
591a664aabaSAndreas Gohr        echo '<form action="' . wl($ID) . '" method="post" enctype="multipart/form-data">';
592ae1afd2fSChristopher Smith        formSecurityToken();
593a664aabaSAndreas Gohr        echo '<label>' . $this->lang['import_userlistcsv'] . '<input type="file" name="import" /></label>';
594a664aabaSAndreas Gohr        echo '<button type="submit" name="fn[import]">' . $this->lang['import'] . '</button>';
595a664aabaSAndreas Gohr        echo '<input type="hidden" name="do"    value="admin" />';
596a664aabaSAndreas Gohr        echo '<input type="hidden" name="page"  value="usermanager" />';
597ae1afd2fSChristopher Smith
5983a97d936SAndreas Gohr        $this->htmlFilterSettings($indent + 4);
599a664aabaSAndreas Gohr        echo '</form>';
600a664aabaSAndreas Gohr        echo '</div>';
601ae1afd2fSChristopher Smith
602ae1afd2fSChristopher Smith        // list failures from the previous import
6033a97d936SAndreas Gohr        if ($this->import_failures) {
6043a97d936SAndreas Gohr            $digits = strlen(count($this->import_failures));
605a664aabaSAndreas Gohr            echo '<div class="level3 import_failures">';
606a664aabaSAndreas Gohr            echo '<h3>' . $this->lang['import_header'] . '</h3>';
607a664aabaSAndreas Gohr            echo '<table class="import_failures">';
608a664aabaSAndreas Gohr            echo '<thead>';
609a664aabaSAndreas Gohr            echo '<tr>';
610a664aabaSAndreas Gohr            echo '<th class="line">' . $this->lang['line'] . '</th>';
611a664aabaSAndreas Gohr            echo '<th class="error">' . $this->lang['error'] . '</th>';
612a664aabaSAndreas Gohr            echo '<th class="userid">' . $this->lang['user_id'] . '</th>';
613a664aabaSAndreas Gohr            echo '<th class="username">' . $this->lang['user_name'] . '</th>';
614a664aabaSAndreas Gohr            echo '<th class="usermail">' . $this->lang['user_mail'] . '</th>';
615a664aabaSAndreas Gohr            echo '<th class="usergroups">' . $this->lang['user_groups'] . '</th>';
616a664aabaSAndreas Gohr            echo '</tr>';
617a664aabaSAndreas Gohr            echo '</thead>';
618a664aabaSAndreas Gohr            echo '<tbody>';
6193a97d936SAndreas Gohr            foreach ($this->import_failures as $line => $failure) {
620a664aabaSAndreas Gohr                echo '<tr>';
621a664aabaSAndreas Gohr                echo '<td class="lineno"> ' . sprintf('%0' . $digits . 'd', $line) . ' </td>';
622a664aabaSAndreas Gohr                echo '<td class="error">' . $failure['error'] . ' </td>';
623a664aabaSAndreas Gohr                echo '<td class="field userid"> ' . hsc($failure['user'][0]) . ' </td>';
624a664aabaSAndreas Gohr                echo '<td class="field username"> ' . hsc($failure['user'][2]) . ' </td>';
625a664aabaSAndreas Gohr                echo '<td class="field usermail"> ' . hsc($failure['user'][3]) . ' </td>';
626a664aabaSAndreas Gohr                echo '<td class="field usergroups"> ' . hsc($failure['user'][4]) . ' </td>';
627a664aabaSAndreas Gohr                echo '</tr>';
628ae1afd2fSChristopher Smith            }
629a664aabaSAndreas Gohr            echo '</tbody>';
630a664aabaSAndreas Gohr            echo '</table>';
631a664aabaSAndreas Gohr            echo '<p><a href="' . $failure_download_link . '">' . $this->lang['import_downloadfailures'] . '</a></p>';
632a664aabaSAndreas Gohr            echo '</div>';
633ae1afd2fSChristopher Smith        }
634ae1afd2fSChristopher Smith    }
635ae1afd2fSChristopher Smith
636c5a7c0c6SGerrit Uitslag    /**
637c5a7c0c6SGerrit Uitslag     * Add an user to auth backend
638c5a7c0c6SGerrit Uitslag     *
639c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
640c5a7c0c6SGerrit Uitslag     */
6413a97d936SAndreas Gohr    protected function addUser()
6423a97d936SAndreas Gohr    {
64300d58927SMichael Hamann        global $INPUT;
644634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
6453a97d936SAndreas Gohr        if (!$this->auth->canDo('addUser')) return false;
6460440ff15Schris
64754cc7aa4SAndreas Gohr        [$user, $pass, $name, $mail, $grps, $passconfirm] = $this->retrieveUser();
6480440ff15Schris        if (empty($user)) return false;
6496733c4d7SChris Smith
6503a97d936SAndreas Gohr        if ($this->auth->canDo('modPass')) {
651c3f4fb63SGina Haeussge            if (empty($pass)) {
65200d58927SMichael Hamann                if ($INPUT->has('usernotify')) {
6538a285f7fSAndreas Gohr                    $pass = auth_pwgen($user);
654c3f4fb63SGina Haeussge                } else {
65560b9901bSAndreas Gohr                    msg($this->lang['add_fail'], -1);
65609a5dcd6SMichael Große                    msg($this->lang['addUser_error_missing_pass'], -1);
65760b9901bSAndreas Gohr                    return false;
65860b9901bSAndreas Gohr                }
65954cc7aa4SAndreas Gohr            } elseif (!$this->verifyPassword($pass, $passconfirm)) {
66009a5dcd6SMichael Große                msg($this->lang['add_fail'], -1);
66109a5dcd6SMichael Große                msg($this->lang['addUser_error_pass_not_identical'], -1);
662359e9417SChristopher Smith                return false;
663359e9417SChristopher Smith            }
66454cc7aa4SAndreas Gohr        } elseif (!empty($pass)) {
6656733c4d7SChris Smith            msg($this->lang['add_fail'], -1);
66609a5dcd6SMichael Große            msg($this->lang['addUser_error_modPass_disabled'], -1);
6676733c4d7SChris Smith            return false;
6686733c4d7SChris Smith        }
6696733c4d7SChris Smith
6703a97d936SAndreas Gohr        if ($this->auth->canDo('modName')) {
6716733c4d7SChris Smith            if (empty($name)) {
6726733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
67309a5dcd6SMichael Große                msg($this->lang['addUser_error_name_missing'], -1);
6746733c4d7SChris Smith                return false;
6756733c4d7SChris Smith            }
67654cc7aa4SAndreas Gohr        } elseif (!empty($name)) {
67709a5dcd6SMichael Große            msg($this->lang['add_fail'], -1);
67809a5dcd6SMichael Große            msg($this->lang['addUser_error_modName_disabled'], -1);
6796733c4d7SChris Smith            return false;
6806733c4d7SChris Smith        }
6816733c4d7SChris Smith
6823a97d936SAndreas Gohr        if ($this->auth->canDo('modMail')) {
6836733c4d7SChris Smith            if (empty($mail)) {
6846733c4d7SChris Smith                msg($this->lang['add_fail'], -1);
68509a5dcd6SMichael Große                msg($this->lang['addUser_error_mail_missing'], -1);
6866733c4d7SChris Smith                return false;
6876733c4d7SChris Smith            }
68854cc7aa4SAndreas Gohr        } elseif (!empty($mail)) {
68909a5dcd6SMichael Große            msg($this->lang['add_fail'], -1);
69009a5dcd6SMichael Große            msg($this->lang['addUser_error_modMail_disabled'], -1);
6916733c4d7SChris Smith            return false;
6926733c4d7SChris Smith        }
6930440ff15Schris
69454cc7aa4SAndreas Gohr        if ($ok = $this->auth->triggerUserMod('create', [$user, $pass, $name, $mail, $grps])) {
695a6858c6aSchris            msg($this->lang['add_ok'], 1);
696a6858c6aSchris
69700d58927SMichael Hamann            if ($INPUT->has('usernotify') && $pass) {
6983a97d936SAndreas Gohr                $this->notifyUser($user, $pass);
699a6858c6aSchris            }
700a6858c6aSchris        } else {
70160b9901bSAndreas Gohr            msg($this->lang['add_fail'], -1);
70209a5dcd6SMichael Große            msg($this->lang['addUser_error_create_event_failed'], -1);
703a6858c6aSchris        }
704a6858c6aSchris
705a6858c6aSchris        return $ok;
7060440ff15Schris    }
7070440ff15Schris
7080440ff15Schris    /**
709c5a7c0c6SGerrit Uitslag     * Delete user from auth backend
710c5a7c0c6SGerrit Uitslag     *
711c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
7120440ff15Schris     */
7133a97d936SAndreas Gohr    protected function deleteUser()
7143a97d936SAndreas Gohr    {
71500d58927SMichael Hamann        global $conf, $INPUT;
7169ec82636SAndreas Gohr
717634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
7183a97d936SAndreas Gohr        if (!$this->auth->canDo('delUser')) return false;
7190440ff15Schris
72000d58927SMichael Hamann        $selected = $INPUT->arr('delete');
72100d58927SMichael Hamann        if (empty($selected)) return false;
7220440ff15Schris        $selected = array_keys($selected);
7230440ff15Schris
724c9a8f912SMichael Klier        if (in_array($_SERVER['REMOTE_USER'], $selected)) {
725*c8f55459SDamien Regad            msg($this->lang['delete_fail_self'], -1);
726c9a8f912SMichael Klier            return false;
727c9a8f912SMichael Klier        }
728c9a8f912SMichael Klier
72954cc7aa4SAndreas Gohr        $count = $this->auth->triggerUserMod('delete', [$selected]);
7300440ff15Schris        if ($count == count($selected)) {
7310440ff15Schris            $text = str_replace('%d', $count, $this->lang['delete_ok']);
7320440ff15Schris            msg("$text.", 1);
7330440ff15Schris        } else {
7340440ff15Schris            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
7350440ff15Schris            $part2 = str_replace('%d', (count($selected) - $count), $this->lang['delete_fail']);
7360440ff15Schris            msg("$part1, $part2", -1);
7370440ff15Schris        }
73878c7c8c9Schris
7399ec82636SAndreas Gohr        // invalidate all sessions
7409ec82636SAndreas Gohr        io_saveFile($conf['cachedir'] . '/sessionpurge', time());
7419ec82636SAndreas Gohr
74278c7c8c9Schris        return true;
74378c7c8c9Schris    }
74478c7c8c9Schris
74578c7c8c9Schris    /**
74678c7c8c9Schris     * Edit user (a user has been selected for editing)
747c5a7c0c6SGerrit Uitslag     *
748c5a7c0c6SGerrit Uitslag     * @param string $param id of the user
749c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
75078c7c8c9Schris     */
7513a97d936SAndreas Gohr    protected function editUser($param)
7523a97d936SAndreas Gohr    {
753634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
7543a97d936SAndreas Gohr        if (!$this->auth->canDo('UserMod')) return false;
7553a97d936SAndreas Gohr        $user = $this->auth->cleanUser(preg_replace('/.*[:\/]/', '', $param));
7563a97d936SAndreas Gohr        $userdata = $this->auth->getUserData($user);
75778c7c8c9Schris
75878c7c8c9Schris        // no user found?
75978c7c8c9Schris        if (!$userdata) {
76078c7c8c9Schris            msg($this->lang['edit_usermissing'], -1);
76178c7c8c9Schris            return false;
76278c7c8c9Schris        }
76378c7c8c9Schris
7643a97d936SAndreas Gohr        $this->edit_user = $user;
7653a97d936SAndreas Gohr        $this->edit_userdata = $userdata;
76678c7c8c9Schris
76778c7c8c9Schris        return true;
7680440ff15Schris    }
7690440ff15Schris
7700440ff15Schris    /**
771c5a7c0c6SGerrit Uitslag     * Modify user in the auth backend (modified user data has been recieved)
772c5a7c0c6SGerrit Uitslag     *
773c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
7740440ff15Schris     */
7753a97d936SAndreas Gohr    protected function modifyUser()
7763a97d936SAndreas Gohr    {
77700d58927SMichael Hamann        global $conf, $INPUT;
7789ec82636SAndreas Gohr
779634d7150SAndreas Gohr        if (!checkSecurityToken()) return false;
7803a97d936SAndreas Gohr        if (!$this->auth->canDo('UserMod')) return false;
7810440ff15Schris
78226fb387bSchris        // get currently valid  user data
7833a97d936SAndreas Gohr        $olduser = $this->auth->cleanUser(preg_replace('/.*[:\/]/', '', $INPUT->str('userid_old')));
7843a97d936SAndreas Gohr        $oldinfo = $this->auth->getUserData($olduser);
785073766c6Smatthiasgrimm
78626fb387bSchris        // get new user data subject to change
78754cc7aa4SAndreas Gohr        [$newuser, $newpass, $newname, $newmail, $newgrps, $passconfirm] = $this->retrieveUser();
788073766c6Smatthiasgrimm        if (empty($newuser)) return false;
7890440ff15Schris
79054cc7aa4SAndreas Gohr        $changes = [];
791073766c6Smatthiasgrimm        if ($newuser != $olduser) {
7923a97d936SAndreas Gohr            if (!$this->auth->canDo('modLogin')) {        // sanity check, shouldn't be possible
79326fb387bSchris                msg($this->lang['update_fail'], -1);
79426fb387bSchris                return false;
79526fb387bSchris            }
79626fb387bSchris
79726fb387bSchris            // check if $newuser already exists
7983a97d936SAndreas Gohr            if ($this->auth->getUserData($newuser)) {
799073766c6Smatthiasgrimm                msg(sprintf($this->lang['update_exists'], $newuser), -1);
800a6858c6aSchris                $re_edit = true;
8010440ff15Schris            } else {
802073766c6Smatthiasgrimm                $changes['user'] = $newuser;
8030440ff15Schris            }
80493eefc2fSAndreas Gohr        }
8053a97d936SAndreas Gohr        if ($this->auth->canDo('modPass')) {
8062400ddcbSChristopher Smith            if ($newpass || $passconfirm) {
8073a97d936SAndreas Gohr                if ($this->verifyPassword($newpass, $passconfirm)) {
808359e9417SChristopher Smith                    $changes['pass'] = $newpass;
809359e9417SChristopher Smith                } else {
810359e9417SChristopher Smith                    return false;
811359e9417SChristopher Smith                }
81254cc7aa4SAndreas Gohr            } elseif ($INPUT->has('usernotify')) {
813359e9417SChristopher Smith                // no new password supplied, check if we need to generate one (or it stays unchanged)
814359e9417SChristopher Smith                $changes['pass'] = auth_pwgen($olduser);
815359e9417SChristopher Smith            }
816359e9417SChristopher Smith        }
8170440ff15Schris
8183a97d936SAndreas Gohr        if (!empty($newname) && $this->auth->canDo('modName') && $newname != $oldinfo['name']) {
819073766c6Smatthiasgrimm            $changes['name'] = $newname;
82040d72af6SChristopher Smith        }
8213a97d936SAndreas Gohr        if (!empty($newmail) && $this->auth->canDo('modMail') && $newmail != $oldinfo['mail']) {
822073766c6Smatthiasgrimm            $changes['mail'] = $newmail;
82340d72af6SChristopher Smith        }
8243a97d936SAndreas Gohr        if (!empty($newgrps) && $this->auth->canDo('modGroups') && $newgrps != $oldinfo['grps']) {
825073766c6Smatthiasgrimm            $changes['grps'] = $newgrps;
82640d72af6SChristopher Smith        }
8270440ff15Schris
82854cc7aa4SAndreas Gohr        if ($ok = $this->auth->triggerUserMod('modify', [$olduser, $changes])) {
8290440ff15Schris            msg($this->lang['update_ok'], 1);
830a6858c6aSchris
8316ed3476bSChristopher Smith            if ($INPUT->has('usernotify') && !empty($changes['pass'])) {
832a6858c6aSchris                $notify = empty($changes['user']) ? $olduser : $newuser;
8333a97d936SAndreas Gohr                $this->notifyUser($notify, $changes['pass']);
834a6858c6aSchris            }
835a6858c6aSchris
8369ec82636SAndreas Gohr            // invalidate all sessions
8379ec82636SAndreas Gohr            io_saveFile($conf['cachedir'] . '/sessionpurge', time());
8380440ff15Schris        } else {
8390440ff15Schris            msg($this->lang['update_fail'], -1);
8400440ff15Schris        }
84178c7c8c9Schris
842a6858c6aSchris        if (!empty($re_edit)) {
8433a97d936SAndreas Gohr            $this->editUser($olduser);
8440440ff15Schris        }
8450440ff15Schris
846a6858c6aSchris        return $ok;
847a6858c6aSchris    }
848a6858c6aSchris
849a6858c6aSchris    /**
850c5a7c0c6SGerrit Uitslag     * Send password change notification email
851c5a7c0c6SGerrit Uitslag     *
852c5a7c0c6SGerrit Uitslag     * @param string $user id of user
853c5a7c0c6SGerrit Uitslag     * @param string $password plain text
854c5a7c0c6SGerrit Uitslag     * @param bool $status_alert whether status alert should be shown
855c5a7c0c6SGerrit Uitslag     * @return bool whether succesful
856a6858c6aSchris     */
8573a97d936SAndreas Gohr    protected function notifyUser($user, $password, $status_alert = true)
8583a97d936SAndreas Gohr    {
859a6858c6aSchris
860a6858c6aSchris        if ($sent = auth_sendPassword($user, $password)) {
861328143f8SChristopher Smith            if ($status_alert) {
862a6858c6aSchris                msg($this->lang['notify_ok'], 1);
863328143f8SChristopher Smith            }
86454cc7aa4SAndreas Gohr        } elseif ($status_alert) {
865a6858c6aSchris            msg($this->lang['notify_fail'], -1);
866a6858c6aSchris        }
867a6858c6aSchris
868a6858c6aSchris        return $sent;
869a6858c6aSchris    }
870a6858c6aSchris
871a6858c6aSchris    /**
872359e9417SChristopher Smith     * Verify password meets minimum requirements
873359e9417SChristopher Smith     * :TODO: extend to support password strength
874359e9417SChristopher Smith     *
875359e9417SChristopher Smith     * @param string $password candidate string for new password
876359e9417SChristopher Smith     * @param string $confirm repeated password for confirmation
877359e9417SChristopher Smith     * @return bool   true if meets requirements, false otherwise
878359e9417SChristopher Smith     */
8793a97d936SAndreas Gohr    protected function verifyPassword($password, $confirm)
8803a97d936SAndreas Gohr    {
881be9008d3SChristopher Smith        global $lang;
882359e9417SChristopher Smith
8832400ddcbSChristopher Smith        if (empty($password) && empty($confirm)) {
884359e9417SChristopher Smith            return false;
885359e9417SChristopher Smith        }
886359e9417SChristopher Smith
887359e9417SChristopher Smith        if ($password !== $confirm) {
888be9008d3SChristopher Smith            msg($lang['regbadpass'], -1);
889359e9417SChristopher Smith            return false;
890359e9417SChristopher Smith        }
891359e9417SChristopher Smith
892359e9417SChristopher Smith        // :TODO: test password for required strength
893359e9417SChristopher Smith
894359e9417SChristopher Smith        // if we make it this far the password is good
895359e9417SChristopher Smith        return true;
896359e9417SChristopher Smith    }
897359e9417SChristopher Smith
898359e9417SChristopher Smith    /**
899c5a7c0c6SGerrit Uitslag     * Retrieve & clean user data from the form
900a6858c6aSchris     *
901c5a7c0c6SGerrit Uitslag     * @param bool $clean whether the cleanUser method of the authentication backend is applied
902a6858c6aSchris     * @return array (user, password, full name, email, array(groups))
9030440ff15Schris     */
9043a97d936SAndreas Gohr    protected function retrieveUser($clean = true)
9053a97d936SAndreas Gohr    {
90651ee2399SGerrit Uitslag        /** @var AuthPlugin $auth */
9077441e340SAndreas Gohr        global $auth;
908fbfbbe8aSHakan Sandell        global $INPUT;
9090440ff15Schris
91054cc7aa4SAndreas Gohr        $user = [];
911fbfbbe8aSHakan Sandell        $user[0] = ($clean) ? $auth->cleanUser($INPUT->str('userid')) : $INPUT->str('userid');
912fbfbbe8aSHakan Sandell        $user[1] = $INPUT->str('userpass');
913fbfbbe8aSHakan Sandell        $user[2] = $INPUT->str('username');
914fbfbbe8aSHakan Sandell        $user[3] = $INPUT->str('usermail');
915fbfbbe8aSHakan Sandell        $user[4] = explode(',', $INPUT->str('usergroups'));
916359e9417SChristopher Smith        $user[5] = $INPUT->str('userpass2'); // repeated password for confirmation
9170440ff15Schris
9187441e340SAndreas Gohr        $user[4] = array_map('trim', $user[4]);
9196547cfc7SGerrit Uitslag        if ($clean) {
9206547cfc7SGerrit Uitslag            $user[4] = array_map([$auth, 'cleanGroup'], $user[4]);
9216547cfc7SGerrit Uitslag        }
9227441e340SAndreas Gohr        $user[4] = array_filter($user[4]);
9237441e340SAndreas Gohr        $user[4] = array_unique($user[4]);
9246547cfc7SGerrit Uitslag        if ($user[4] === []) {
9256547cfc7SGerrit Uitslag            $user[4] = null;
9266547cfc7SGerrit Uitslag        }
9270440ff15Schris
9280440ff15Schris        return $user;
9290440ff15Schris    }
9300440ff15Schris
931c5a7c0c6SGerrit Uitslag    /**
932c5a7c0c6SGerrit Uitslag     * Set the filter with the current search terms or clear the filter
933c5a7c0c6SGerrit Uitslag     *
934c5a7c0c6SGerrit Uitslag     * @param string $op 'new' or 'clear'
935c5a7c0c6SGerrit Uitslag     */
9363a97d936SAndreas Gohr    protected function setFilter($op)
9373a97d936SAndreas Gohr    {
9380440ff15Schris
93954cc7aa4SAndreas Gohr        $this->filter = [];
9400440ff15Schris
9410440ff15Schris        if ($op == 'new') {
942a19c9aa0SGerrit Uitslag            [$user, /* pass */, $name, $mail, $grps] = $this->retrieveUser(false);
9430440ff15Schris
9443a97d936SAndreas Gohr            if (!empty($user)) $this->filter['user'] = $user;
9453a97d936SAndreas Gohr            if (!empty($name)) $this->filter['name'] = $name;
9463a97d936SAndreas Gohr            if (!empty($mail)) $this->filter['mail'] = $mail;
94754cc7aa4SAndreas Gohr            if (!empty($grps)) $this->filter['grps'] = implode('|', $grps);
9480440ff15Schris        }
9490440ff15Schris    }
9500440ff15Schris
951c5a7c0c6SGerrit Uitslag    /**
952c5a7c0c6SGerrit Uitslag     * Get the current search terms
953c5a7c0c6SGerrit Uitslag     *
954c5a7c0c6SGerrit Uitslag     * @return array
955c5a7c0c6SGerrit Uitslag     */
9563a97d936SAndreas Gohr    protected function retrieveFilter()
9573a97d936SAndreas Gohr    {
958fbfbbe8aSHakan Sandell        global $INPUT;
9590440ff15Schris
960fbfbbe8aSHakan Sandell        $t_filter = $INPUT->arr('filter');
9610440ff15Schris
9620440ff15Schris        // messy, but this way we ensure we aren't getting any additional crap from malicious users
96354cc7aa4SAndreas Gohr        $filter = [];
9640440ff15Schris
9650440ff15Schris        if (isset($t_filter['user'])) $filter['user'] = $t_filter['user'];
9660440ff15Schris        if (isset($t_filter['name'])) $filter['name'] = $t_filter['name'];
9670440ff15Schris        if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail'];
9680440ff15Schris        if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps'];
9690440ff15Schris
9700440ff15Schris        return $filter;
9710440ff15Schris    }
9720440ff15Schris
973c5a7c0c6SGerrit Uitslag    /**
974c5a7c0c6SGerrit Uitslag     * Validate and improve the pagination values
975c5a7c0c6SGerrit Uitslag     */
9763a97d936SAndreas Gohr    protected function validatePagination()
9773a97d936SAndreas Gohr    {
9780440ff15Schris
9793a97d936SAndreas Gohr        if ($this->start >= $this->users_total) {
9803a97d936SAndreas Gohr            $this->start = $this->users_total - $this->pagesize;
9810440ff15Schris        }
9823a97d936SAndreas Gohr        if ($this->start < 0) $this->start = 0;
9830440ff15Schris
9843a97d936SAndreas Gohr        $this->last = min($this->users_total, $this->start + $this->pagesize);
9850440ff15Schris    }
9860440ff15Schris
987c5a7c0c6SGerrit Uitslag    /**
988c5a7c0c6SGerrit Uitslag     * Return an array of strings to enable/disable pagination buttons
989c5a7c0c6SGerrit Uitslag     *
990c5a7c0c6SGerrit Uitslag     * @return array with enable/disable attributes
9910440ff15Schris     */
9923a97d936SAndreas Gohr    protected function pagination()
9933a97d936SAndreas Gohr    {
9940440ff15Schris
99551d94d49Schris        $disabled = 'disabled="disabled"';
99651d94d49Schris
99754cc7aa4SAndreas Gohr        $buttons = [];
9983a97d936SAndreas Gohr        $buttons['start'] = $buttons['prev'] = ($this->start == 0) ? $disabled : '';
99951d94d49Schris
10003a97d936SAndreas Gohr        if ($this->users_total == -1) {
100151d94d49Schris            $buttons['last'] = $disabled;
100251d94d49Schris            $buttons['next'] = '';
100351d94d49Schris        } else {
100464159a61SAndreas Gohr            $buttons['last'] = $buttons['next'] =
10053a97d936SAndreas Gohr                (($this->start + $this->pagesize) >= $this->users_total) ? $disabled : '';
100651d94d49Schris        }
10070440ff15Schris
10083a97d936SAndreas Gohr        if ($this->lastdisabled) {
1009462e9e37SMichael Große            $buttons['last'] = $disabled;
1010462e9e37SMichael Große        }
1011462e9e37SMichael Große
10120440ff15Schris        return $buttons;
10130440ff15Schris    }
10145c967d3dSChristopher Smith
1015c5a7c0c6SGerrit Uitslag    /**
1016c5a7c0c6SGerrit Uitslag     * Export a list of users in csv format using the current filter criteria
10175c967d3dSChristopher Smith     */
10183a97d936SAndreas Gohr    protected function exportCSV()
10193a97d936SAndreas Gohr    {
10205c967d3dSChristopher Smith        // list of users for export - based on current filter criteria
10213a97d936SAndreas Gohr        $user_list = $this->auth->retrieveUsers(0, 0, $this->filter);
102254cc7aa4SAndreas Gohr        $column_headings = [
10235c967d3dSChristopher Smith            $this->lang["user_id"],
10245c967d3dSChristopher Smith            $this->lang["user_name"],
10255c967d3dSChristopher Smith            $this->lang["user_mail"],
10265c967d3dSChristopher Smith            $this->lang["user_groups"]
102754cc7aa4SAndreas Gohr        ];
10285c967d3dSChristopher Smith
10295c967d3dSChristopher Smith        // ==============================================================================================
10305c967d3dSChristopher Smith        // GENERATE OUTPUT
10315c967d3dSChristopher Smith        // normal headers for downloading...
10325c967d3dSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
10335c967d3dSChristopher Smith        header('Content-Disposition: attachment; filename="wikiusers.csv"');
10345c967d3dSChristopher Smith#       // for debugging assistance, send as text plain to the browser
10355c967d3dSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
10365c967d3dSChristopher Smith
10375c967d3dSChristopher Smith        // output the csv
10385c967d3dSChristopher Smith        $fd = fopen('php://output', 'w');
1039abc2dfe1SAndreas Gohr        fputcsv($fd, $column_headings, ',', '"', "\\");
10405c967d3dSChristopher Smith        foreach ($user_list as $user => $info) {
104154cc7aa4SAndreas Gohr            $line = [$user, $info['name'], $info['mail'], implode(',', $info['grps'])];
1042abc2dfe1SAndreas Gohr            fputcsv($fd, $line, ',', '"', "\\");
10435c967d3dSChristopher Smith        }
10445c967d3dSChristopher Smith        fclose($fd);
10453a97d936SAndreas Gohr        if (defined('DOKU_UNITTEST')) {
10463a97d936SAndreas Gohr            return;
10473a97d936SAndreas Gohr        }
1048b2c01466SChristopher Smith
10495c967d3dSChristopher Smith        die;
10505c967d3dSChristopher Smith    }
1051ae1afd2fSChristopher Smith
1052c5a7c0c6SGerrit Uitslag    /**
1053c5a7c0c6SGerrit Uitslag     * Import a file of users in csv format
1054ae1afd2fSChristopher Smith     *
1055ae1afd2fSChristopher Smith     * csv file should have 4 columns, user_id, full name, email, groups (comma separated)
1056c5a7c0c6SGerrit Uitslag     *
10575ba64050SChristopher Smith     * @return bool whether successful
1058ae1afd2fSChristopher Smith     */
10593a97d936SAndreas Gohr    protected function importCSV()
10603a97d936SAndreas Gohr    {
1061ae1afd2fSChristopher Smith        // check we are allowed to add users
1062ae1afd2fSChristopher Smith        if (!checkSecurityToken()) return false;
10633a97d936SAndreas Gohr        if (!$this->auth->canDo('addUser')) return false;
1064ae1afd2fSChristopher Smith
1065ae1afd2fSChristopher Smith        // check file uploaded ok.
10667d34963bSAndreas Gohr        if (
10677d34963bSAndreas Gohr            empty($_FILES['import']['size']) ||
10683a97d936SAndreas Gohr            !empty($_FILES['import']['error']) && $this->isUploadedFile($_FILES['import']['tmp_name'])
106964159a61SAndreas Gohr        ) {
1070ae1afd2fSChristopher Smith            msg($this->lang['import_error_upload'], -1);
1071ae1afd2fSChristopher Smith            return false;
1072ae1afd2fSChristopher Smith        }
1073ae1afd2fSChristopher Smith        // retrieve users from the file
107454cc7aa4SAndreas Gohr        $this->import_failures = [];
1075ae1afd2fSChristopher Smith        $import_success_count = 0;
1076ae1afd2fSChristopher Smith        $import_fail_count = 0;
1077ae1afd2fSChristopher Smith        $line = 0;
1078ae1afd2fSChristopher Smith        $fd = fopen($_FILES['import']['tmp_name'], 'r');
1079ae1afd2fSChristopher Smith        if ($fd) {
1080ae1afd2fSChristopher Smith            while ($csv = fgets($fd)) {
108154cc7aa4SAndreas Gohr                if (!Clean::isUtf8($csv)) {
108253c68e5cSAndreas Gohr                    $csv = Conversion::fromLatin1($csv);
1083efcec72bSChristopher Smith                }
1084abc2dfe1SAndreas Gohr                $raw = str_getcsv($csv, ',', '"', "\\");
1085ae1afd2fSChristopher Smith                $error = '';                        // clean out any errors from the previous line
1086ae1afd2fSChristopher Smith                // data checks...
1087ae1afd2fSChristopher Smith                if (1 == ++$line) {
1088ae1afd2fSChristopher Smith                    if ($raw[0] == 'user_id' || $raw[0] == $this->lang['user_id']) continue;    // skip headers
1089ae1afd2fSChristopher Smith                }
1090ae1afd2fSChristopher Smith                if (count($raw) < 4) {                                        // need at least four fields
1091ae1afd2fSChristopher Smith                    $import_fail_count++;
1092ae1afd2fSChristopher Smith                    $error = sprintf($this->lang['import_error_fields'], count($raw));
109354cc7aa4SAndreas Gohr                    $this->import_failures[$line] = ['error' => $error, 'user' => $raw, 'orig' => $csv];
1094ae1afd2fSChristopher Smith                    continue;
1095ae1afd2fSChristopher Smith                }
1096ae1afd2fSChristopher Smith                array_splice($raw, 1, 0, auth_pwgen());                          // splice in a generated password
10973a97d936SAndreas Gohr                $clean = $this->cleanImportUser($raw, $error);
10983a97d936SAndreas Gohr                if ($clean && $this->importUser($clean, $error)) {
10993a97d936SAndreas Gohr                    $sent = $this->notifyUser($clean[0], $clean[1], false);
1100328143f8SChristopher Smith                    if (!$sent) {
1101328143f8SChristopher Smith                        msg(sprintf($this->lang['import_notify_fail'], $clean[0], $clean[3]), -1);
1102328143f8SChristopher Smith                    }
1103ae1afd2fSChristopher Smith                    $import_success_count++;
1104ae1afd2fSChristopher Smith                } else {
1105ae1afd2fSChristopher Smith                    $import_fail_count++;
1106e73725baSChristopher Smith                    array_splice($raw, 1, 1);                                  // remove the spliced in password
110754cc7aa4SAndreas Gohr                    $this->import_failures[$line] = ['error' => $error, 'user' => $raw, 'orig' => $csv];
1108ae1afd2fSChristopher Smith                }
1109ae1afd2fSChristopher Smith            }
111064159a61SAndreas Gohr            msg(
111164159a61SAndreas Gohr                sprintf(
111264159a61SAndreas Gohr                    $this->lang['import_success_count'],
111364159a61SAndreas Gohr                    ($import_success_count + $import_fail_count),
111464159a61SAndreas Gohr                    $import_success_count
111564159a61SAndreas Gohr                ),
111664159a61SAndreas Gohr                ($import_success_count ? 1 : -1)
111764159a61SAndreas Gohr            );
1118ae1afd2fSChristopher Smith            if ($import_fail_count) {
1119ae1afd2fSChristopher Smith                msg(sprintf($this->lang['import_failure_count'], $import_fail_count), -1);
1120ae1afd2fSChristopher Smith            }
1121ae1afd2fSChristopher Smith        } else {
1122ae1afd2fSChristopher Smith            msg($this->lang['import_error_readfail'], -1);
1123ae1afd2fSChristopher Smith        }
1124ae1afd2fSChristopher Smith
1125ae1afd2fSChristopher Smith        // save import failures into the session
1126ae1afd2fSChristopher Smith        if (!headers_sent()) {
1127ae1afd2fSChristopher Smith            session_start();
11283a97d936SAndreas Gohr            $_SESSION['import_failures'] = $this->import_failures;
1129ae1afd2fSChristopher Smith            session_write_close();
1130ae1afd2fSChristopher Smith        }
1131c5a7c0c6SGerrit Uitslag        return true;
1132ae1afd2fSChristopher Smith    }
1133ae1afd2fSChristopher Smith
1134c5a7c0c6SGerrit Uitslag    /**
1135786dfb0eSGerrit Uitslag     * Returns cleaned user data
1136c5a7c0c6SGerrit Uitslag     *
1137c5a7c0c6SGerrit Uitslag     * @param array $candidate raw values of line from input file
1138253d4b48SGerrit Uitslag     * @param string $error
1139253d4b48SGerrit Uitslag     * @return array|false cleaned data or false
1140c5a7c0c6SGerrit Uitslag     */
11413a97d936SAndreas Gohr    protected function cleanImportUser($candidate, &$error)
11423a97d936SAndreas Gohr    {
1143ae1afd2fSChristopher Smith        global $INPUT;
1144ae1afd2fSChristopher Smith
11453a97d936SAndreas Gohr        // FIXME kludgy ....
1146ae1afd2fSChristopher Smith        $INPUT->set('userid', $candidate[0]);
1147ae1afd2fSChristopher Smith        $INPUT->set('userpass', $candidate[1]);
1148ae1afd2fSChristopher Smith        $INPUT->set('username', $candidate[2]);
1149ae1afd2fSChristopher Smith        $INPUT->set('usermail', $candidate[3]);
1150ae1afd2fSChristopher Smith        $INPUT->set('usergroups', $candidate[4]);
1151ae1afd2fSChristopher Smith
11523a97d936SAndreas Gohr        $cleaned = $this->retrieveUser();
115354cc7aa4SAndreas Gohr        [$user, /* pass */, $name, $mail, /* grps */] = $cleaned;
1154ae1afd2fSChristopher Smith        if (empty($user)) {
1155ae1afd2fSChristopher Smith            $error = $this->lang['import_error_baduserid'];
1156ae1afd2fSChristopher Smith            return false;
1157ae1afd2fSChristopher Smith        }
1158ae1afd2fSChristopher Smith
1159ae1afd2fSChristopher Smith        // no need to check password, handled elsewhere
1160ae1afd2fSChristopher Smith
11613a97d936SAndreas Gohr        if (!($this->auth->canDo('modName') xor empty($name))) {
1162ae1afd2fSChristopher Smith            $error = $this->lang['import_error_badname'];
1163ae1afd2fSChristopher Smith            return false;
1164ae1afd2fSChristopher Smith        }
1165ae1afd2fSChristopher Smith
11663a97d936SAndreas Gohr        if ($this->auth->canDo('modMail')) {
1167328143f8SChristopher Smith            if (empty($mail) || !mail_isvalid($mail)) {
1168ae1afd2fSChristopher Smith                $error = $this->lang['import_error_badmail'];
1169ae1afd2fSChristopher Smith                return false;
1170ae1afd2fSChristopher Smith            }
117154cc7aa4SAndreas Gohr        } elseif (!empty($mail)) {
1172328143f8SChristopher Smith            $error = $this->lang['import_error_badmail'];
1173328143f8SChristopher Smith            return false;
1174328143f8SChristopher Smith        }
1175ae1afd2fSChristopher Smith
1176ae1afd2fSChristopher Smith        return $cleaned;
1177ae1afd2fSChristopher Smith    }
1178ae1afd2fSChristopher Smith
1179c5a7c0c6SGerrit Uitslag    /**
1180c5a7c0c6SGerrit Uitslag     * Adds imported user to auth backend
1181c5a7c0c6SGerrit Uitslag     *
1182c5a7c0c6SGerrit Uitslag     * Required a check of canDo('addUser') before
1183c5a7c0c6SGerrit Uitslag     *
1184c5a7c0c6SGerrit Uitslag     * @param array $user data of user
1185c5a7c0c6SGerrit Uitslag     * @param string &$error reference catched error message
11865ba64050SChristopher Smith     * @return bool whether successful
1187c5a7c0c6SGerrit Uitslag     */
11883a97d936SAndreas Gohr    protected function importUser($user, &$error)
11893a97d936SAndreas Gohr    {
11903a97d936SAndreas Gohr        if (!$this->auth->triggerUserMod('create', $user)) {
1191ae1afd2fSChristopher Smith            $error = $this->lang['import_error_create'];
1192ae1afd2fSChristopher Smith            return false;
1193ae1afd2fSChristopher Smith        }
1194ae1afd2fSChristopher Smith
1195ae1afd2fSChristopher Smith        return true;
1196ae1afd2fSChristopher Smith    }
1197ae1afd2fSChristopher Smith
1198c5a7c0c6SGerrit Uitslag    /**
1199c5a7c0c6SGerrit Uitslag     * Downloads failures as csv file
1200c5a7c0c6SGerrit Uitslag     */
12013a97d936SAndreas Gohr    protected function downloadImportFailures()
12023a97d936SAndreas Gohr    {
1203ae1afd2fSChristopher Smith
1204ae1afd2fSChristopher Smith        // ==============================================================================================
1205ae1afd2fSChristopher Smith        // GENERATE OUTPUT
1206ae1afd2fSChristopher Smith        // normal headers for downloading...
1207ae1afd2fSChristopher Smith        header('Content-type: text/csv;charset=utf-8');
1208ae1afd2fSChristopher Smith        header('Content-Disposition: attachment; filename="importfails.csv"');
1209ae1afd2fSChristopher Smith#       // for debugging assistance, send as text plain to the browser
1210ae1afd2fSChristopher Smith#       header('Content-type: text/plain;charset=utf-8');
1211ae1afd2fSChristopher Smith
1212ae1afd2fSChristopher Smith        // output the csv
1213ae1afd2fSChristopher Smith        $fd = fopen('php://output', 'w');
12143a97d936SAndreas Gohr        foreach ($this->import_failures as $fail) {
121554cc7aa4SAndreas Gohr            fwrite($fd, $fail['orig']);
1216ae1afd2fSChristopher Smith        }
1217ae1afd2fSChristopher Smith        fclose($fd);
1218ae1afd2fSChristopher Smith        die;
1219ae1afd2fSChristopher Smith    }
1220ae1afd2fSChristopher Smith
1221b2c01466SChristopher Smith    /**
1222b2c01466SChristopher Smith     * wrapper for is_uploaded_file to facilitate overriding by test suite
1223253d4b48SGerrit Uitslag     *
1224253d4b48SGerrit Uitslag     * @param string $file filename
1225253d4b48SGerrit Uitslag     * @return bool
1226b2c01466SChristopher Smith     */
12273a97d936SAndreas Gohr    protected function isUploadedFile($file)
12283a97d936SAndreas Gohr    {
1229b2c01466SChristopher Smith        return is_uploaded_file($file);
1230b2c01466SChristopher Smith    }
12310440ff15Schris}
1232