xref: /dokuwiki/lib/plugins/authplain/auth.php (revision b346670ea2385f3ee7ea5e77ba74a02541a73dda)
1f4476bd9SJan Schumann<?php
293497020SAndreas Gohr
38553d24dSAndreas Gohruse dokuwiki\Extension\AuthPlugin;
493497020SAndreas Gohruse dokuwiki\Logger;
50489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
6f4476bd9SJan Schumann
7f4476bd9SJan Schumann/**
8f4476bd9SJan Schumann * Plaintext authentication backend
9f4476bd9SJan Schumann *
10f4476bd9SJan Schumann * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11f4476bd9SJan Schumann * @author     Andreas Gohr <andi@splitbrain.org>
12f4476bd9SJan Schumann * @author     Chris Smith <chris@jalakai.co.uk>
13f4476bd9SJan Schumann * @author     Jan Schumann <js@schumann-it.com>
14f4476bd9SJan Schumann */
158553d24dSAndreas Gohrclass auth_plugin_authplain extends AuthPlugin
165aa905e9SAndreas Gohr{
17311f4603SAndreas Gohr    /** @var array user cache */
18ab9790caSAndreas Gohr    protected $users;
19311f4603SAndreas Gohr
20311f4603SAndreas Gohr    /** @var array filter pattern */
21ab9790caSAndreas Gohr    protected $pattern = [];
22f4476bd9SJan Schumann
236c8c1f46SChristopher Smith    /** @var bool safe version of preg_split */
245aa905e9SAndreas Gohr    protected $pregsplit_safe = false;
256c8c1f46SChristopher Smith
26f4476bd9SJan Schumann    /**
27f4476bd9SJan Schumann     * Constructor
28f4476bd9SJan Schumann     *
29f4476bd9SJan Schumann     * Carry out sanity checks to ensure the object is
30f4476bd9SJan Schumann     * able to operate. Set capabilities.
31f4476bd9SJan Schumann     *
32f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
33f4476bd9SJan Schumann     */
345aa905e9SAndreas Gohr    public function __construct()
355aa905e9SAndreas Gohr    {
36454d868bSAndreas Gohr        parent::__construct();
37f4476bd9SJan Schumann        global $config_cascade;
38f4476bd9SJan Schumann
39f4476bd9SJan Schumann        if (!@is_readable($config_cascade['plainauth.users']['default'])) {
40f4476bd9SJan Schumann            $this->success = false;
41f4476bd9SJan Schumann        } else {
42f4476bd9SJan Schumann            if (@is_writable($config_cascade['plainauth.users']['default'])) {
43f4476bd9SJan Schumann                $this->cando['addUser']   = true;
44f4476bd9SJan Schumann                $this->cando['delUser']   = true;
45f4476bd9SJan Schumann                $this->cando['modLogin']  = true;
46f4476bd9SJan Schumann                $this->cando['modPass']   = true;
47f4476bd9SJan Schumann                $this->cando['modName']   = true;
48f4476bd9SJan Schumann                $this->cando['modMail']   = true;
49f4476bd9SJan Schumann                $this->cando['modGroups'] = true;
50f4476bd9SJan Schumann            }
51f4476bd9SJan Schumann            $this->cando['getUsers']     = true;
52f4476bd9SJan Schumann            $this->cando['getUserCount'] = true;
53b2fcc742SAnna Dabrowska            $this->cando['getGroups']    = true;
54f4476bd9SJan Schumann        }
55f4476bd9SJan Schumann    }
56f4476bd9SJan Schumann
57f4476bd9SJan Schumann    /**
58311f4603SAndreas Gohr     * Check user+password
59f4476bd9SJan Schumann     *
60f4476bd9SJan Schumann     * Checks if the given user exists and the given
61f4476bd9SJan Schumann     * plaintext password is correct
62f4476bd9SJan Schumann     *
63f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
64311f4603SAndreas Gohr     * @param string $user
65311f4603SAndreas Gohr     * @param string $pass
66f4476bd9SJan Schumann     * @return  bool
67f4476bd9SJan Schumann     */
685aa905e9SAndreas Gohr    public function checkPass($user, $pass)
695aa905e9SAndreas Gohr    {
70f4476bd9SJan Schumann        $userinfo = $this->getUserData($user);
71f4476bd9SJan Schumann        if ($userinfo === false) return false;
72f4476bd9SJan Schumann
73f4476bd9SJan Schumann        return auth_verifyPassword($pass, $this->users[$user]['pass']);
74f4476bd9SJan Schumann    }
75f4476bd9SJan Schumann
76f4476bd9SJan Schumann    /**
77f4476bd9SJan Schumann     * Return user info
78f4476bd9SJan Schumann     *
79f4476bd9SJan Schumann     * Returns info about the given user needs to contain
80f4476bd9SJan Schumann     * at least these fields:
81f4476bd9SJan Schumann     *
82f4476bd9SJan Schumann     * name string  full name of the user
83f4476bd9SJan Schumann     * mail string  email addres of the user
84f4476bd9SJan Schumann     * grps array   list of groups the user is in
85f4476bd9SJan Schumann     *
86f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
87311f4603SAndreas Gohr     * @param string $user
882046a654SChristopher Smith     * @param bool $requireGroups  (optional) ignored by this plugin, grps info always supplied
89253d4b48SGerrit Uitslag     * @return array|false
90f4476bd9SJan Schumann     */
915aa905e9SAndreas Gohr    public function getUserData($user, $requireGroups = true)
925aa905e9SAndreas Gohr    {
935aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
94ab9790caSAndreas Gohr        return $this->users[$user] ?? false;
95f4476bd9SJan Schumann    }
96f4476bd9SJan Schumann
97f4476bd9SJan Schumann    /**
98f95ecbbfSAngus Gratton     * Creates a string suitable for saving as a line
99f95ecbbfSAngus Gratton     * in the file database
100f95ecbbfSAngus Gratton     * (delimiters escaped, etc.)
101f95ecbbfSAngus Gratton     *
102f95ecbbfSAngus Gratton     * @param string $user
103f95ecbbfSAngus Gratton     * @param string $pass
104f95ecbbfSAngus Gratton     * @param string $name
105f95ecbbfSAngus Gratton     * @param string $mail
106f95ecbbfSAngus Gratton     * @param array  $grps list of groups the user is in
107f95ecbbfSAngus Gratton     * @return string
108f95ecbbfSAngus Gratton     */
1095aa905e9SAndreas Gohr    protected function createUserLine($user, $pass, $name, $mail, $grps)
1105aa905e9SAndreas Gohr    {
111ab9790caSAndreas Gohr        $groups   = implode(',', $grps);
112ab9790caSAndreas Gohr        $userline = [$user, $pass, $name, $mail, $groups];
113f95ecbbfSAngus Gratton        $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
114f95ecbbfSAngus Gratton        $userline = str_replace(':', '\\:', $userline); // escape : as \:
115*b346670eSAndreas Gohr        $userline = str_replace('#', '\\#', $userline); // escape # as \
116ab9790caSAndreas Gohr        $userline = implode(':', $userline) . "\n";
117f95ecbbfSAngus Gratton        return $userline;
118f95ecbbfSAngus Gratton    }
119f95ecbbfSAngus Gratton
120f95ecbbfSAngus Gratton    /**
121f4476bd9SJan Schumann     * Create a new User
122f4476bd9SJan Schumann     *
123f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
124f4476bd9SJan Schumann     * occurred and true if everything went well.
125f4476bd9SJan Schumann     *
126f4476bd9SJan Schumann     * The new user will be added to the default group by this
127f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
128f4476bd9SJan Schumann     *
129f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
130f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
131311f4603SAndreas Gohr     *
132311f4603SAndreas Gohr     * @param string $user
133311f4603SAndreas Gohr     * @param string $pwd
134311f4603SAndreas Gohr     * @param string $name
135311f4603SAndreas Gohr     * @param string $mail
136311f4603SAndreas Gohr     * @param array  $grps
137311f4603SAndreas Gohr     * @return bool|null|string
138f4476bd9SJan Schumann     */
1395aa905e9SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null)
1405aa905e9SAndreas Gohr    {
141f4476bd9SJan Schumann        global $conf;
142f4476bd9SJan Schumann        global $config_cascade;
143f4476bd9SJan Schumann
144f4476bd9SJan Schumann        // user mustn't already exist
145db9faf02SPatrick Brown        if ($this->getUserData($user) !== false) {
146db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
147db9faf02SPatrick Brown            return false;
148db9faf02SPatrick Brown        }
149f4476bd9SJan Schumann
150f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
151f4476bd9SJan Schumann
152f4476bd9SJan Schumann        // set default group if no groups specified
153ab9790caSAndreas Gohr        if (!is_array($grps)) $grps = [$conf['defaultgroup']];
154f4476bd9SJan Schumann
155f4476bd9SJan Schumann        // prepare user line
1565aa905e9SAndreas Gohr        $userline = $this->createUserLine($user, $pass, $name, $mail, $grps);
157f4476bd9SJan Schumann
158db9faf02SPatrick Brown        if (!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
159db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
160db9faf02SPatrick Brown            return null;
161f4476bd9SJan Schumann        }
162f4476bd9SJan Schumann
163ab9790caSAndreas Gohr        $this->users[$user] = [
164ab9790caSAndreas Gohr            'pass' => $pass,
165ab9790caSAndreas Gohr            'name' => $name,
166ab9790caSAndreas Gohr            'mail' => $mail,
167ab9790caSAndreas Gohr            'grps' => $grps
168ab9790caSAndreas Gohr        ];
169db9faf02SPatrick Brown        return $pwd;
170f4476bd9SJan Schumann    }
171f4476bd9SJan Schumann
172f4476bd9SJan Schumann    /**
173f4476bd9SJan Schumann     * Modify user data
174f4476bd9SJan Schumann     *
175f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
176311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
177311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
178f4476bd9SJan Schumann     * @return  bool
179f4476bd9SJan Schumann     */
1805aa905e9SAndreas Gohr    public function modifyUser($user, $changes)
1815aa905e9SAndreas Gohr    {
182f4476bd9SJan Schumann        global $ACT;
183f4476bd9SJan Schumann        global $config_cascade;
184f4476bd9SJan Schumann
185f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
186db9faf02SPatrick Brown        if (($userinfo = $this->getUserData($user)) === false) {
187db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
188db9faf02SPatrick Brown            return false;
189db9faf02SPatrick Brown        }
19042cbd322SAndreas Gohr
19142cbd322SAndreas Gohr        // don't modify protected users
19242cbd322SAndreas Gohr        if (!empty($userinfo['protected'])) {
19342cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
19442cbd322SAndreas Gohr            return false;
19542cbd322SAndreas Gohr        }
19642cbd322SAndreas Gohr
197ab9790caSAndreas Gohr        if (!is_array($changes) || $changes === []) return true;
198f4476bd9SJan Schumann
199f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
200f4476bd9SJan Schumann        $newuser = $user;
201f4476bd9SJan Schumann        foreach ($changes as $field => $value) {
202f4476bd9SJan Schumann            if ($field == 'user') {
203f4476bd9SJan Schumann                $newuser = $value;
204f4476bd9SJan Schumann                continue;
205f4476bd9SJan Schumann            }
206f4476bd9SJan Schumann            if ($field == 'pass') $value = auth_cryptPassword($value);
207f4476bd9SJan Schumann            $userinfo[$field] = $value;
208f4476bd9SJan Schumann        }
209f4476bd9SJan Schumann
2105aa905e9SAndreas Gohr        $userline = $this->createUserLine(
21164159a61SAndreas Gohr            $newuser,
21264159a61SAndreas Gohr            $userinfo['pass'],
21364159a61SAndreas Gohr            $userinfo['name'],
21464159a61SAndreas Gohr            $userinfo['mail'],
21564159a61SAndreas Gohr            $userinfo['grps']
21664159a61SAndreas Gohr        );
217f4476bd9SJan Schumann
218699e3c49SPatrick Brown        if (!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^' . $user . ':/', $userline, true)) {
219699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
220699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
221311f4603SAndreas Gohr            $ACT = 'register';
222f4476bd9SJan Schumann            return false;
223f4476bd9SJan Schumann        }
224f4476bd9SJan Schumann
22559440086SAndreas Gohr        if (isset($this->users[$user])) unset($this->users[$user]);
226f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
227f4476bd9SJan Schumann        return true;
228f4476bd9SJan Schumann    }
229f4476bd9SJan Schumann
230f4476bd9SJan Schumann    /**
231f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
232f4476bd9SJan Schumann     *
233f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
234f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
235f4476bd9SJan Schumann     * @return  int             the number of users deleted
236f4476bd9SJan Schumann     */
2375aa905e9SAndreas Gohr    public function deleteUsers($users)
2385aa905e9SAndreas Gohr    {
239f4476bd9SJan Schumann        global $config_cascade;
240f4476bd9SJan Schumann
241ab9790caSAndreas Gohr        if (!is_array($users) || $users === []) return 0;
242f4476bd9SJan Schumann
2435aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
244f4476bd9SJan Schumann
245ab9790caSAndreas Gohr        $deleted = [];
246f4476bd9SJan Schumann        foreach ($users as $user) {
24742cbd322SAndreas Gohr            // don't delete protected users
24842cbd322SAndreas Gohr            if (!empty($this->users[$user]['protected'])) {
24942cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
25042cbd322SAndreas Gohr                continue;
25142cbd322SAndreas Gohr            }
252f4476bd9SJan Schumann            if (isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
253f4476bd9SJan Schumann        }
254f4476bd9SJan Schumann
255ab9790caSAndreas Gohr        if ($deleted === []) return 0;
256f4476bd9SJan Schumann
257ab9790caSAndreas Gohr        $pattern = '/^(' . implode('|', $deleted) . '):/';
258db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
259db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
260db9faf02SPatrick Brown            return 0;
261db9faf02SPatrick Brown        }
262f4476bd9SJan Schumann
2639d24536dSAndreas Gohr        // reload the user list and count the difference
264f4476bd9SJan Schumann        $count = count($this->users);
2655aa905e9SAndreas Gohr        $this->loadUserData();
266f4476bd9SJan Schumann        $count -= count($this->users);
267f4476bd9SJan Schumann        return $count;
268f4476bd9SJan Schumann    }
269f4476bd9SJan Schumann
270f4476bd9SJan Schumann    /**
271f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
272f4476bd9SJan Schumann     *
273f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
274311f4603SAndreas Gohr     *
275311f4603SAndreas Gohr     * @param array $filter
276311f4603SAndreas Gohr     * @return int
277f4476bd9SJan Schumann     */
278ab9790caSAndreas Gohr    public function getUserCount($filter = [])
2795aa905e9SAndreas Gohr    {
280f4476bd9SJan Schumann
2815aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
282f4476bd9SJan Schumann
283ab9790caSAndreas Gohr        if ($filter === []) return count($this->users);
284f4476bd9SJan Schumann
285f4476bd9SJan Schumann        $count = 0;
2865aa905e9SAndreas Gohr        $this->constructPattern($filter);
287f4476bd9SJan Schumann
288f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
2895aa905e9SAndreas Gohr            $count += $this->filter($user, $info);
290f4476bd9SJan Schumann        }
291f4476bd9SJan Schumann
292f4476bd9SJan Schumann        return $count;
293f4476bd9SJan Schumann    }
294f4476bd9SJan Schumann
295f4476bd9SJan Schumann    /**
296f4476bd9SJan Schumann     * Bulk retrieval of user data
297f4476bd9SJan Schumann     *
298f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
299311f4603SAndreas Gohr     *
300311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
301311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
302311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
303311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
304f4476bd9SJan Schumann     */
305ab9790caSAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = [])
3065aa905e9SAndreas Gohr    {
307f4476bd9SJan Schumann
3085aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
309f4476bd9SJan Schumann
3100489c64bSMoisés Braga Ribeiro        Sort::ksort($this->users);
311f4476bd9SJan Schumann
312f4476bd9SJan Schumann        $i     = 0;
313f4476bd9SJan Schumann        $count = 0;
314ab9790caSAndreas Gohr        $out   = [];
3155aa905e9SAndreas Gohr        $this->constructPattern($filter);
316f4476bd9SJan Schumann
317f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
3185aa905e9SAndreas Gohr            if ($this->filter($user, $info)) {
319f4476bd9SJan Schumann                if ($i >= $start) {
320f4476bd9SJan Schumann                    $out[$user] = $info;
321f4476bd9SJan Schumann                    $count++;
322f4476bd9SJan Schumann                    if (($limit > 0) && ($count >= $limit)) break;
323f4476bd9SJan Schumann                }
324f4476bd9SJan Schumann                $i++;
325f4476bd9SJan Schumann            }
326f4476bd9SJan Schumann        }
327f4476bd9SJan Schumann
328f4476bd9SJan Schumann        return $out;
329f4476bd9SJan Schumann    }
330f4476bd9SJan Schumann
331f4476bd9SJan Schumann    /**
332b2fcc742SAnna Dabrowska     * Retrieves groups.
333b2fcc742SAnna Dabrowska     * Loads complete user data into memory before searching for groups.
334b2fcc742SAnna Dabrowska     *
335b2fcc742SAnna Dabrowska     * @param   int   $start index of first group to be returned
336b2fcc742SAnna Dabrowska     * @param   int   $limit max number of groups to be returned
337b2fcc742SAnna Dabrowska     * @return  array
338b2fcc742SAnna Dabrowska     */
339b2fcc742SAnna Dabrowska    public function retrieveGroups($start = 0, $limit = 0)
340b2fcc742SAnna Dabrowska    {
341b2fcc742SAnna Dabrowska        $groups = [];
342b2fcc742SAnna Dabrowska
34342c62e55SAndreas Gohr        if ($this->users === null) $this->loadUserData();
344ab9790caSAndreas Gohr        foreach ($this->users as $info) {
345b2fcc742SAnna Dabrowska            $groups = array_merge($groups, array_diff($info['grps'], $groups));
346b2fcc742SAnna Dabrowska        }
3470489c64bSMoisés Braga Ribeiro        Sort::ksort($groups);
348b2fcc742SAnna Dabrowska
349b2fcc742SAnna Dabrowska        if ($limit > 0) {
350b2fcc742SAnna Dabrowska            return array_splice($groups, $start, $limit);
351b2fcc742SAnna Dabrowska        }
352b2fcc742SAnna Dabrowska        return array_splice($groups, $start);
353b2fcc742SAnna Dabrowska    }
354b2fcc742SAnna Dabrowska
355b2fcc742SAnna Dabrowska    /**
356f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
357311f4603SAndreas Gohr     *
358311f4603SAndreas Gohr     * @param string $user
359311f4603SAndreas Gohr     * @return string
360f4476bd9SJan Schumann     */
3615aa905e9SAndreas Gohr    public function cleanUser($user)
3625aa905e9SAndreas Gohr    {
363f4476bd9SJan Schumann        global $conf;
3645f18fdf3SAndreas Gohr
3655f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $user));
366f4476bd9SJan Schumann    }
367f4476bd9SJan Schumann
368f4476bd9SJan Schumann    /**
369f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
370311f4603SAndreas Gohr     *
371311f4603SAndreas Gohr     * @param string $group
372311f4603SAndreas Gohr     * @return string
373f4476bd9SJan Schumann     */
3745aa905e9SAndreas Gohr    public function cleanGroup($group)
3755aa905e9SAndreas Gohr    {
376f4476bd9SJan Schumann        global $conf;
3775f18fdf3SAndreas Gohr
3785f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $group));
379f4476bd9SJan Schumann    }
380f4476bd9SJan Schumann
381f4476bd9SJan Schumann    /**
382f4476bd9SJan Schumann     * Load all user data
383f4476bd9SJan Schumann     *
384f4476bd9SJan Schumann     * loads the user file into a datastructure
385f4476bd9SJan Schumann     *
386f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
387f4476bd9SJan Schumann     */
3885aa905e9SAndreas Gohr    protected function loadUserData()
3895aa905e9SAndreas Gohr    {
390f4476bd9SJan Schumann        global $config_cascade;
391f4476bd9SJan Schumann
3925aa905e9SAndreas Gohr        $this->users = $this->readUserFile($config_cascade['plainauth.users']['default']);
393f4476bd9SJan Schumann
39442cbd322SAndreas Gohr        // support protected users
39542cbd322SAndreas Gohr        if (!empty($config_cascade['plainauth.users']['protected'])) {
3965aa905e9SAndreas Gohr            $protected = $this->readUserFile($config_cascade['plainauth.users']['protected']);
39742cbd322SAndreas Gohr            foreach (array_keys($protected) as $key) {
39842cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
39942cbd322SAndreas Gohr            }
40042cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
40142cbd322SAndreas Gohr        }
40242cbd322SAndreas Gohr    }
403f4476bd9SJan Schumann
40442cbd322SAndreas Gohr    /**
40542cbd322SAndreas Gohr     * Read user data from given file
40642cbd322SAndreas Gohr     *
40742cbd322SAndreas Gohr     * ignores non existing files
40842cbd322SAndreas Gohr     *
40942cbd322SAndreas Gohr     * @param string $file the file to load data from
41042cbd322SAndreas Gohr     * @return array
41142cbd322SAndreas Gohr     */
4125aa905e9SAndreas Gohr    protected function readUserFile($file)
4135aa905e9SAndreas Gohr    {
414ab9790caSAndreas Gohr        $users = [];
41542cbd322SAndreas Gohr        if (!file_exists($file)) return $users;
41642cbd322SAndreas Gohr
41742cbd322SAndreas Gohr        $lines = file($file);
418f4476bd9SJan Schumann        foreach ($lines as $line) {
419*b346670eSAndreas Gohr            $line = preg_replace('/(?<!\\\\)#.*$/', '', $line); //ignore comments (unless escaped)
420f4476bd9SJan Schumann            $line = trim($line);
421f4476bd9SJan Schumann            if (empty($line)) continue;
422f4476bd9SJan Schumann
4235aa905e9SAndreas Gohr            $row = $this->splitUserData($line);
424f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
425f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
426*b346670eSAndreas Gohr            $row = str_replace('\\#', '#', $row);
427f95ecbbfSAngus Gratton
428f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
429f4476bd9SJan Schumann
43042cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
43142cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
43242cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
43342cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
434f4476bd9SJan Schumann        }
43542cbd322SAndreas Gohr        return $users;
436f4476bd9SJan Schumann    }
437f4476bd9SJan Schumann
4385aa905e9SAndreas Gohr    /**
4395aa905e9SAndreas Gohr     * Get the user line split into it's parts
4405aa905e9SAndreas Gohr     *
4415aa905e9SAndreas Gohr     * @param string $line
4425aa905e9SAndreas Gohr     * @return string[]
4435aa905e9SAndreas Gohr     */
4445aa905e9SAndreas Gohr    protected function splitUserData($line)
4455aa905e9SAndreas Gohr    {
44693497020SAndreas Gohr        $data = preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
44793497020SAndreas Gohr        if (count($data) < 5) {
44893497020SAndreas Gohr            $data = array_pad($data, 5, '');
44993497020SAndreas Gohr            Logger::error('User line with less than 5 fields. Possibly corruption in your user file', $data);
4506c8c1f46SChristopher Smith        }
45193497020SAndreas Gohr        return $data;
4526c8c1f46SChristopher Smith    }
4536c8c1f46SChristopher Smith
454f4476bd9SJan Schumann    /**
455311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
456f4476bd9SJan Schumann     *
457f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
458311f4603SAndreas Gohr     *
459311f4603SAndreas Gohr     * @param string $user User login
460311f4603SAndreas Gohr     * @param array  $info User's userinfo array
461311f4603SAndreas Gohr     * @return bool
462f4476bd9SJan Schumann     */
4635aa905e9SAndreas Gohr    protected function filter($user, $info)
4645aa905e9SAndreas Gohr    {
4655aa905e9SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
466f4476bd9SJan Schumann            if ($item == 'user') {
467311f4603SAndreas Gohr                if (!preg_match($pattern, $user)) return false;
468f4476bd9SJan Schumann            } elseif ($item == 'grps') {
469311f4603SAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
470ab9790caSAndreas Gohr            } elseif (!preg_match($pattern, $info[$item])) {
471ab9790caSAndreas Gohr                return false;
472f4476bd9SJan Schumann            }
473f4476bd9SJan Schumann        }
474311f4603SAndreas Gohr        return true;
475f4476bd9SJan Schumann    }
476f4476bd9SJan Schumann
477311f4603SAndreas Gohr    /**
478311f4603SAndreas Gohr     * construct a filter pattern
479311f4603SAndreas Gohr     *
480311f4603SAndreas Gohr     * @param array $filter
481311f4603SAndreas Gohr     */
4825aa905e9SAndreas Gohr    protected function constructPattern($filter)
4835aa905e9SAndreas Gohr    {
484ab9790caSAndreas Gohr        $this->pattern = [];
485f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
4865aa905e9SAndreas Gohr            $this->pattern[$item] = '/' . str_replace('/', '\/', $pattern) . '/i'; // allow regex characters
487f4476bd9SJan Schumann        }
488f4476bd9SJan Schumann    }
489f4476bd9SJan Schumann}
490