xref: /dokuwiki/lib/plugins/authplain/auth.php (revision ab9790ca81397622fcfd58faf56a69b68bd36257)
1f4476bd9SJan Schumann<?php
293497020SAndreas Gohr
393497020SAndreas Gohruse dokuwiki\Logger;
40489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
5f4476bd9SJan Schumann
6f4476bd9SJan Schumann/**
7f4476bd9SJan Schumann * Plaintext authentication backend
8f4476bd9SJan Schumann *
9f4476bd9SJan Schumann * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10f4476bd9SJan Schumann * @author     Andreas Gohr <andi@splitbrain.org>
11f4476bd9SJan Schumann * @author     Chris Smith <chris@jalakai.co.uk>
12f4476bd9SJan Schumann * @author     Jan Schumann <js@schumann-it.com>
13f4476bd9SJan Schumann */
145aa905e9SAndreas Gohrclass auth_plugin_authplain extends DokuWiki_Auth_Plugin
155aa905e9SAndreas Gohr{
16311f4603SAndreas Gohr    /** @var array user cache */
17*ab9790caSAndreas Gohr    protected $users;
18311f4603SAndreas Gohr
19311f4603SAndreas Gohr    /** @var array filter pattern */
20*ab9790caSAndreas Gohr    protected $pattern = [];
21f4476bd9SJan Schumann
226c8c1f46SChristopher Smith    /** @var bool safe version of preg_split */
235aa905e9SAndreas Gohr    protected $pregsplit_safe = false;
246c8c1f46SChristopher Smith
25f4476bd9SJan Schumann    /**
26f4476bd9SJan Schumann     * Constructor
27f4476bd9SJan Schumann     *
28f4476bd9SJan Schumann     * Carry out sanity checks to ensure the object is
29f4476bd9SJan Schumann     * able to operate. Set capabilities.
30f4476bd9SJan Schumann     *
31f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
32f4476bd9SJan Schumann     */
335aa905e9SAndreas Gohr    public function __construct()
345aa905e9SAndreas Gohr    {
35454d868bSAndreas Gohr        parent::__construct();
36f4476bd9SJan Schumann        global $config_cascade;
37f4476bd9SJan Schumann
38f4476bd9SJan Schumann        if (!@is_readable($config_cascade['plainauth.users']['default'])) {
39f4476bd9SJan Schumann            $this->success = false;
40f4476bd9SJan Schumann        } else {
41f4476bd9SJan Schumann            if (@is_writable($config_cascade['plainauth.users']['default'])) {
42f4476bd9SJan Schumann                $this->cando['addUser']   = true;
43f4476bd9SJan Schumann                $this->cando['delUser']   = true;
44f4476bd9SJan Schumann                $this->cando['modLogin']  = true;
45f4476bd9SJan Schumann                $this->cando['modPass']   = true;
46f4476bd9SJan Schumann                $this->cando['modName']   = true;
47f4476bd9SJan Schumann                $this->cando['modMail']   = true;
48f4476bd9SJan Schumann                $this->cando['modGroups'] = true;
49f4476bd9SJan Schumann            }
50f4476bd9SJan Schumann            $this->cando['getUsers']     = true;
51f4476bd9SJan Schumann            $this->cando['getUserCount'] = true;
52b2fcc742SAnna Dabrowska            $this->cando['getGroups']    = true;
53f4476bd9SJan Schumann        }
54f4476bd9SJan Schumann    }
55f4476bd9SJan Schumann
56f4476bd9SJan Schumann    /**
57311f4603SAndreas Gohr     * Check user+password
58f4476bd9SJan Schumann     *
59f4476bd9SJan Schumann     * Checks if the given user exists and the given
60f4476bd9SJan Schumann     * plaintext password is correct
61f4476bd9SJan Schumann     *
62f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
63311f4603SAndreas Gohr     * @param string $user
64311f4603SAndreas Gohr     * @param string $pass
65f4476bd9SJan Schumann     * @return  bool
66f4476bd9SJan Schumann     */
675aa905e9SAndreas Gohr    public function checkPass($user, $pass)
685aa905e9SAndreas Gohr    {
69f4476bd9SJan Schumann        $userinfo = $this->getUserData($user);
70f4476bd9SJan Schumann        if ($userinfo === false) return false;
71f4476bd9SJan Schumann
72f4476bd9SJan Schumann        return auth_verifyPassword($pass, $this->users[$user]['pass']);
73f4476bd9SJan Schumann    }
74f4476bd9SJan Schumann
75f4476bd9SJan Schumann    /**
76f4476bd9SJan Schumann     * Return user info
77f4476bd9SJan Schumann     *
78f4476bd9SJan Schumann     * Returns info about the given user needs to contain
79f4476bd9SJan Schumann     * at least these fields:
80f4476bd9SJan Schumann     *
81f4476bd9SJan Schumann     * name string  full name of the user
82f4476bd9SJan Schumann     * mail string  email addres of the user
83f4476bd9SJan Schumann     * grps array   list of groups the user is in
84f4476bd9SJan Schumann     *
85f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
86311f4603SAndreas Gohr     * @param string $user
872046a654SChristopher Smith     * @param bool $requireGroups  (optional) ignored by this plugin, grps info always supplied
88253d4b48SGerrit Uitslag     * @return array|false
89f4476bd9SJan Schumann     */
905aa905e9SAndreas Gohr    public function getUserData($user, $requireGroups = true)
915aa905e9SAndreas Gohr    {
925aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
93*ab9790caSAndreas Gohr        return $this->users[$user] ?? false;
94f4476bd9SJan Schumann    }
95f4476bd9SJan Schumann
96f4476bd9SJan Schumann    /**
97f95ecbbfSAngus Gratton     * Creates a string suitable for saving as a line
98f95ecbbfSAngus Gratton     * in the file database
99f95ecbbfSAngus Gratton     * (delimiters escaped, etc.)
100f95ecbbfSAngus Gratton     *
101f95ecbbfSAngus Gratton     * @param string $user
102f95ecbbfSAngus Gratton     * @param string $pass
103f95ecbbfSAngus Gratton     * @param string $name
104f95ecbbfSAngus Gratton     * @param string $mail
105f95ecbbfSAngus Gratton     * @param array  $grps list of groups the user is in
106f95ecbbfSAngus Gratton     * @return string
107f95ecbbfSAngus Gratton     */
1085aa905e9SAndreas Gohr    protected function createUserLine($user, $pass, $name, $mail, $grps)
1095aa905e9SAndreas Gohr    {
110*ab9790caSAndreas Gohr        $groups   = implode(',', $grps);
111*ab9790caSAndreas Gohr        $userline = [$user, $pass, $name, $mail, $groups];
112f95ecbbfSAngus Gratton        $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
113f95ecbbfSAngus Gratton        $userline = str_replace(':', '\\:', $userline); // escape : as \:
114*ab9790caSAndreas Gohr        $userline = implode(':', $userline)."\n";
115f95ecbbfSAngus Gratton        return $userline;
116f95ecbbfSAngus Gratton    }
117f95ecbbfSAngus Gratton
118f95ecbbfSAngus Gratton    /**
119f4476bd9SJan Schumann     * Create a new User
120f4476bd9SJan Schumann     *
121f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
122f4476bd9SJan Schumann     * occurred and true if everything went well.
123f4476bd9SJan Schumann     *
124f4476bd9SJan Schumann     * The new user will be added to the default group by this
125f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
126f4476bd9SJan Schumann     *
127f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
128f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
129311f4603SAndreas Gohr     *
130311f4603SAndreas Gohr     * @param string $user
131311f4603SAndreas Gohr     * @param string $pwd
132311f4603SAndreas Gohr     * @param string $name
133311f4603SAndreas Gohr     * @param string $mail
134311f4603SAndreas Gohr     * @param array  $grps
135311f4603SAndreas Gohr     * @return bool|null|string
136f4476bd9SJan Schumann     */
1375aa905e9SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null)
1385aa905e9SAndreas Gohr    {
139f4476bd9SJan Schumann        global $conf;
140f4476bd9SJan Schumann        global $config_cascade;
141f4476bd9SJan Schumann
142f4476bd9SJan Schumann        // user mustn't already exist
143db9faf02SPatrick Brown        if ($this->getUserData($user) !== false) {
144db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
145db9faf02SPatrick Brown            return false;
146db9faf02SPatrick Brown        }
147f4476bd9SJan Schumann
148f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
149f4476bd9SJan Schumann
150f4476bd9SJan Schumann        // set default group if no groups specified
151*ab9790caSAndreas Gohr        if (!is_array($grps)) $grps = [$conf['defaultgroup']];
152f4476bd9SJan Schumann
153f4476bd9SJan Schumann        // prepare user line
1545aa905e9SAndreas Gohr        $userline = $this->createUserLine($user, $pass, $name, $mail, $grps);
155f4476bd9SJan Schumann
156db9faf02SPatrick Brown        if (!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
157db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
158db9faf02SPatrick Brown            return null;
159f4476bd9SJan Schumann        }
160f4476bd9SJan Schumann
161*ab9790caSAndreas Gohr        $this->users[$user] = [
162*ab9790caSAndreas Gohr            'pass' => $pass,
163*ab9790caSAndreas Gohr            'name' => $name,
164*ab9790caSAndreas Gohr            'mail' => $mail,
165*ab9790caSAndreas Gohr            'grps' => $grps
166*ab9790caSAndreas Gohr        ];
167db9faf02SPatrick Brown        return $pwd;
168f4476bd9SJan Schumann    }
169f4476bd9SJan Schumann
170f4476bd9SJan Schumann    /**
171f4476bd9SJan Schumann     * Modify user data
172f4476bd9SJan Schumann     *
173f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
174311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
175311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
176f4476bd9SJan Schumann     * @return  bool
177f4476bd9SJan Schumann     */
1785aa905e9SAndreas Gohr    public function modifyUser($user, $changes)
1795aa905e9SAndreas Gohr    {
180f4476bd9SJan Schumann        global $ACT;
181f4476bd9SJan Schumann        global $config_cascade;
182f4476bd9SJan Schumann
183f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
184db9faf02SPatrick Brown        if (($userinfo = $this->getUserData($user)) === false) {
185db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
186db9faf02SPatrick Brown            return false;
187db9faf02SPatrick Brown        }
18842cbd322SAndreas Gohr
18942cbd322SAndreas Gohr        // don't modify protected users
19042cbd322SAndreas Gohr        if (!empty($userinfo['protected'])) {
19142cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
19242cbd322SAndreas Gohr            return false;
19342cbd322SAndreas Gohr        }
19442cbd322SAndreas Gohr
195*ab9790caSAndreas Gohr        if (!is_array($changes) || $changes === []) return true;
196f4476bd9SJan Schumann
197f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
198f4476bd9SJan Schumann        $newuser = $user;
199f4476bd9SJan Schumann        foreach ($changes as $field => $value) {
200f4476bd9SJan Schumann            if ($field == 'user') {
201f4476bd9SJan Schumann                $newuser = $value;
202f4476bd9SJan Schumann                continue;
203f4476bd9SJan Schumann            }
204f4476bd9SJan Schumann            if ($field == 'pass') $value = auth_cryptPassword($value);
205f4476bd9SJan Schumann            $userinfo[$field] = $value;
206f4476bd9SJan Schumann        }
207f4476bd9SJan Schumann
2085aa905e9SAndreas Gohr        $userline = $this->createUserLine(
20964159a61SAndreas Gohr            $newuser,
21064159a61SAndreas Gohr            $userinfo['pass'],
21164159a61SAndreas Gohr            $userinfo['name'],
21264159a61SAndreas Gohr            $userinfo['mail'],
21364159a61SAndreas Gohr            $userinfo['grps']
21464159a61SAndreas Gohr        );
215f4476bd9SJan Schumann
216699e3c49SPatrick Brown        if (!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) {
217699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
218699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
219311f4603SAndreas Gohr            $ACT = 'register';
220f4476bd9SJan Schumann            return false;
221f4476bd9SJan Schumann        }
222f4476bd9SJan Schumann
22359440086SAndreas Gohr        if(isset($this->users[$user])) unset($this->users[$user]);
224f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
225f4476bd9SJan Schumann        return true;
226f4476bd9SJan Schumann    }
227f4476bd9SJan Schumann
228f4476bd9SJan Schumann    /**
229f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
230f4476bd9SJan Schumann     *
231f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
232f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
233f4476bd9SJan Schumann     * @return  int             the number of users deleted
234f4476bd9SJan Schumann     */
2355aa905e9SAndreas Gohr    public function deleteUsers($users)
2365aa905e9SAndreas Gohr    {
237f4476bd9SJan Schumann        global $config_cascade;
238f4476bd9SJan Schumann
239*ab9790caSAndreas Gohr        if (!is_array($users) || $users === []) return 0;
240f4476bd9SJan Schumann
2415aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
242f4476bd9SJan Schumann
243*ab9790caSAndreas Gohr        $deleted = [];
244f4476bd9SJan Schumann        foreach ($users as $user) {
24542cbd322SAndreas Gohr            // don't delete protected users
24642cbd322SAndreas Gohr            if (!empty($this->users[$user]['protected'])) {
24742cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
24842cbd322SAndreas Gohr                continue;
24942cbd322SAndreas Gohr            }
250f4476bd9SJan Schumann            if (isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
251f4476bd9SJan Schumann        }
252f4476bd9SJan Schumann
253*ab9790caSAndreas Gohr        if ($deleted === []) return 0;
254f4476bd9SJan Schumann
255*ab9790caSAndreas Gohr        $pattern = '/^('.implode('|', $deleted).'):/';
256db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
257db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
258db9faf02SPatrick Brown            return 0;
259db9faf02SPatrick Brown        }
260f4476bd9SJan Schumann
2619d24536dSAndreas Gohr        // reload the user list and count the difference
262f4476bd9SJan Schumann        $count = count($this->users);
2635aa905e9SAndreas Gohr        $this->loadUserData();
264f4476bd9SJan Schumann        $count -= count($this->users);
265f4476bd9SJan Schumann        return $count;
266f4476bd9SJan Schumann    }
267f4476bd9SJan Schumann
268f4476bd9SJan Schumann    /**
269f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
270f4476bd9SJan Schumann     *
271f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
272311f4603SAndreas Gohr     *
273311f4603SAndreas Gohr     * @param array $filter
274311f4603SAndreas Gohr     * @return int
275f4476bd9SJan Schumann     */
276*ab9790caSAndreas Gohr    public function getUserCount($filter = [])
2775aa905e9SAndreas Gohr    {
278f4476bd9SJan Schumann
2795aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
280f4476bd9SJan Schumann
281*ab9790caSAndreas Gohr        if ($filter === []) return count($this->users);
282f4476bd9SJan Schumann
283f4476bd9SJan Schumann        $count = 0;
2845aa905e9SAndreas Gohr        $this->constructPattern($filter);
285f4476bd9SJan Schumann
286f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
2875aa905e9SAndreas Gohr            $count += $this->filter($user, $info);
288f4476bd9SJan Schumann        }
289f4476bd9SJan Schumann
290f4476bd9SJan Schumann        return $count;
291f4476bd9SJan Schumann    }
292f4476bd9SJan Schumann
293f4476bd9SJan Schumann    /**
294f4476bd9SJan Schumann     * Bulk retrieval of user data
295f4476bd9SJan Schumann     *
296f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
297311f4603SAndreas Gohr     *
298311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
299311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
300311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
301311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
302f4476bd9SJan Schumann     */
303*ab9790caSAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = [])
3045aa905e9SAndreas Gohr    {
305f4476bd9SJan Schumann
3065aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
307f4476bd9SJan Schumann
3080489c64bSMoisés Braga Ribeiro        Sort::ksort($this->users);
309f4476bd9SJan Schumann
310f4476bd9SJan Schumann        $i     = 0;
311f4476bd9SJan Schumann        $count = 0;
312*ab9790caSAndreas Gohr        $out   = [];
3135aa905e9SAndreas Gohr        $this->constructPattern($filter);
314f4476bd9SJan Schumann
315f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
3165aa905e9SAndreas Gohr            if ($this->filter($user, $info)) {
317f4476bd9SJan Schumann                if ($i >= $start) {
318f4476bd9SJan Schumann                    $out[$user] = $info;
319f4476bd9SJan Schumann                    $count++;
320f4476bd9SJan Schumann                    if (($limit > 0) && ($count >= $limit)) break;
321f4476bd9SJan Schumann                }
322f4476bd9SJan Schumann                $i++;
323f4476bd9SJan Schumann            }
324f4476bd9SJan Schumann        }
325f4476bd9SJan Schumann
326f4476bd9SJan Schumann        return $out;
327f4476bd9SJan Schumann    }
328f4476bd9SJan Schumann
329f4476bd9SJan Schumann    /**
330b2fcc742SAnna Dabrowska     * Retrieves groups.
331b2fcc742SAnna Dabrowska     * Loads complete user data into memory before searching for groups.
332b2fcc742SAnna Dabrowska     *
333b2fcc742SAnna Dabrowska     * @param   int   $start index of first group to be returned
334b2fcc742SAnna Dabrowska     * @param   int   $limit max number of groups to be returned
335b2fcc742SAnna Dabrowska     * @return  array
336b2fcc742SAnna Dabrowska     */
337b2fcc742SAnna Dabrowska    public function retrieveGroups($start = 0, $limit = 0)
338b2fcc742SAnna Dabrowska    {
339b2fcc742SAnna Dabrowska        $groups = [];
340b2fcc742SAnna Dabrowska
34142c62e55SAndreas Gohr        if ($this->users === null) $this->loadUserData();
342*ab9790caSAndreas Gohr        foreach($this->users as $info) {
343b2fcc742SAnna Dabrowska            $groups = array_merge($groups, array_diff($info['grps'], $groups));
344b2fcc742SAnna Dabrowska        }
3450489c64bSMoisés Braga Ribeiro        Sort::ksort($groups);
346b2fcc742SAnna Dabrowska
347b2fcc742SAnna Dabrowska        if($limit > 0) {
348b2fcc742SAnna Dabrowska            return array_splice($groups, $start, $limit);
349b2fcc742SAnna Dabrowska        }
350b2fcc742SAnna Dabrowska        return array_splice($groups, $start);
351b2fcc742SAnna Dabrowska    }
352b2fcc742SAnna Dabrowska
353b2fcc742SAnna Dabrowska    /**
354f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
355311f4603SAndreas Gohr     *
356311f4603SAndreas Gohr     * @param string $user
357311f4603SAndreas Gohr     * @return string
358f4476bd9SJan Schumann     */
3595aa905e9SAndreas Gohr    public function cleanUser($user)
3605aa905e9SAndreas Gohr    {
361f4476bd9SJan Schumann        global $conf;
3625f18fdf3SAndreas Gohr
3635f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $user));
364f4476bd9SJan Schumann    }
365f4476bd9SJan Schumann
366f4476bd9SJan Schumann    /**
367f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
368311f4603SAndreas Gohr     *
369311f4603SAndreas Gohr     * @param string $group
370311f4603SAndreas Gohr     * @return string
371f4476bd9SJan Schumann     */
3725aa905e9SAndreas Gohr    public function cleanGroup($group)
3735aa905e9SAndreas Gohr    {
374f4476bd9SJan Schumann        global $conf;
3755f18fdf3SAndreas Gohr
3765f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $group));
377f4476bd9SJan Schumann    }
378f4476bd9SJan Schumann
379f4476bd9SJan Schumann    /**
380f4476bd9SJan Schumann     * Load all user data
381f4476bd9SJan Schumann     *
382f4476bd9SJan Schumann     * loads the user file into a datastructure
383f4476bd9SJan Schumann     *
384f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
385f4476bd9SJan Schumann     */
3865aa905e9SAndreas Gohr    protected function loadUserData()
3875aa905e9SAndreas Gohr    {
388f4476bd9SJan Schumann        global $config_cascade;
389f4476bd9SJan Schumann
3905aa905e9SAndreas Gohr        $this->users = $this->readUserFile($config_cascade['plainauth.users']['default']);
391f4476bd9SJan Schumann
39242cbd322SAndreas Gohr        // support protected users
39342cbd322SAndreas Gohr        if (!empty($config_cascade['plainauth.users']['protected'])) {
3945aa905e9SAndreas Gohr            $protected = $this->readUserFile($config_cascade['plainauth.users']['protected']);
39542cbd322SAndreas Gohr            foreach (array_keys($protected) as $key) {
39642cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
39742cbd322SAndreas Gohr            }
39842cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
39942cbd322SAndreas Gohr        }
40042cbd322SAndreas Gohr    }
401f4476bd9SJan Schumann
40242cbd322SAndreas Gohr    /**
40342cbd322SAndreas Gohr     * Read user data from given file
40442cbd322SAndreas Gohr     *
40542cbd322SAndreas Gohr     * ignores non existing files
40642cbd322SAndreas Gohr     *
40742cbd322SAndreas Gohr     * @param string $file the file to load data from
40842cbd322SAndreas Gohr     * @return array
40942cbd322SAndreas Gohr     */
4105aa905e9SAndreas Gohr    protected function readUserFile($file)
4115aa905e9SAndreas Gohr    {
412*ab9790caSAndreas Gohr        $users = [];
41342cbd322SAndreas Gohr        if (!file_exists($file)) return $users;
41442cbd322SAndreas Gohr
41542cbd322SAndreas Gohr        $lines = file($file);
416f4476bd9SJan Schumann        foreach ($lines as $line) {
417f4476bd9SJan Schumann            $line = preg_replace('/#.*$/', '', $line); //ignore comments
418f4476bd9SJan Schumann            $line = trim($line);
419f4476bd9SJan Schumann            if (empty($line)) continue;
420f4476bd9SJan Schumann
4215aa905e9SAndreas Gohr            $row = $this->splitUserData($line);
422f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
423f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
424f95ecbbfSAngus Gratton
425f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
426f4476bd9SJan Schumann
42742cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
42842cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
42942cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
43042cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
431f4476bd9SJan Schumann        }
43242cbd322SAndreas Gohr        return $users;
433f4476bd9SJan Schumann    }
434f4476bd9SJan Schumann
4355aa905e9SAndreas Gohr    /**
4365aa905e9SAndreas Gohr     * Get the user line split into it's parts
4375aa905e9SAndreas Gohr     *
4385aa905e9SAndreas Gohr     * @param string $line
4395aa905e9SAndreas Gohr     * @return string[]
4405aa905e9SAndreas Gohr     */
4415aa905e9SAndreas Gohr    protected function splitUserData($line)
4425aa905e9SAndreas Gohr    {
44393497020SAndreas Gohr        $data = preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
44493497020SAndreas Gohr        if(count($data) < 5) {
44593497020SAndreas Gohr            $data = array_pad($data, 5, '');
44693497020SAndreas Gohr            Logger::error('User line with less than 5 fields. Possibly corruption in your user file', $data);
4476c8c1f46SChristopher Smith        }
44893497020SAndreas Gohr        return $data;
4496c8c1f46SChristopher Smith    }
4506c8c1f46SChristopher Smith
451f4476bd9SJan Schumann    /**
452311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
453f4476bd9SJan Schumann     *
454f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
455311f4603SAndreas Gohr     *
456311f4603SAndreas Gohr     * @param string $user User login
457311f4603SAndreas Gohr     * @param array  $info User's userinfo array
458311f4603SAndreas Gohr     * @return bool
459f4476bd9SJan Schumann     */
4605aa905e9SAndreas Gohr    protected function filter($user, $info)
4615aa905e9SAndreas Gohr    {
4625aa905e9SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
463f4476bd9SJan Schumann            if ($item == 'user') {
464311f4603SAndreas Gohr                if (!preg_match($pattern, $user)) return false;
465f4476bd9SJan Schumann            } elseif ($item == 'grps') {
466311f4603SAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
467*ab9790caSAndreas Gohr            } elseif (!preg_match($pattern, $info[$item])) {
468*ab9790caSAndreas Gohr                return false;
469f4476bd9SJan Schumann            }
470f4476bd9SJan Schumann        }
471311f4603SAndreas Gohr        return true;
472f4476bd9SJan Schumann    }
473f4476bd9SJan Schumann
474311f4603SAndreas Gohr    /**
475311f4603SAndreas Gohr     * construct a filter pattern
476311f4603SAndreas Gohr     *
477311f4603SAndreas Gohr     * @param array $filter
478311f4603SAndreas Gohr     */
4795aa905e9SAndreas Gohr    protected function constructPattern($filter)
4805aa905e9SAndreas Gohr    {
481*ab9790caSAndreas Gohr        $this->pattern = [];
482f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
4835aa905e9SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
484f4476bd9SJan Schumann        }
485f4476bd9SJan Schumann    }
486f4476bd9SJan Schumann}
487