xref: /dokuwiki/lib/plugins/authplain/auth.php (revision 8553d24d33ab5f260c6e19959de764dd8472d438)
1f4476bd9SJan Schumann<?php
293497020SAndreas Gohr
3*8553d24dSAndreas 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 */
15*8553d24dSAndreas 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 \:
115ab9790caSAndreas Gohr        $userline = implode(':', $userline)."\n";
116f95ecbbfSAngus Gratton        return $userline;
117f95ecbbfSAngus Gratton    }
118f95ecbbfSAngus Gratton
119f95ecbbfSAngus Gratton    /**
120f4476bd9SJan Schumann     * Create a new User
121f4476bd9SJan Schumann     *
122f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
123f4476bd9SJan Schumann     * occurred and true if everything went well.
124f4476bd9SJan Schumann     *
125f4476bd9SJan Schumann     * The new user will be added to the default group by this
126f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
127f4476bd9SJan Schumann     *
128f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
129f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
130311f4603SAndreas Gohr     *
131311f4603SAndreas Gohr     * @param string $user
132311f4603SAndreas Gohr     * @param string $pwd
133311f4603SAndreas Gohr     * @param string $name
134311f4603SAndreas Gohr     * @param string $mail
135311f4603SAndreas Gohr     * @param array  $grps
136311f4603SAndreas Gohr     * @return bool|null|string
137f4476bd9SJan Schumann     */
1385aa905e9SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null)
1395aa905e9SAndreas Gohr    {
140f4476bd9SJan Schumann        global $conf;
141f4476bd9SJan Schumann        global $config_cascade;
142f4476bd9SJan Schumann
143f4476bd9SJan Schumann        // user mustn't already exist
144db9faf02SPatrick Brown        if ($this->getUserData($user) !== false) {
145db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
146db9faf02SPatrick Brown            return false;
147db9faf02SPatrick Brown        }
148f4476bd9SJan Schumann
149f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
150f4476bd9SJan Schumann
151f4476bd9SJan Schumann        // set default group if no groups specified
152ab9790caSAndreas Gohr        if (!is_array($grps)) $grps = [$conf['defaultgroup']];
153f4476bd9SJan Schumann
154f4476bd9SJan Schumann        // prepare user line
1555aa905e9SAndreas Gohr        $userline = $this->createUserLine($user, $pass, $name, $mail, $grps);
156f4476bd9SJan Schumann
157db9faf02SPatrick Brown        if (!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
158db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
159db9faf02SPatrick Brown            return null;
160f4476bd9SJan Schumann        }
161f4476bd9SJan Schumann
162ab9790caSAndreas Gohr        $this->users[$user] = [
163ab9790caSAndreas Gohr            'pass' => $pass,
164ab9790caSAndreas Gohr            'name' => $name,
165ab9790caSAndreas Gohr            'mail' => $mail,
166ab9790caSAndreas Gohr            'grps' => $grps
167ab9790caSAndreas Gohr        ];
168db9faf02SPatrick Brown        return $pwd;
169f4476bd9SJan Schumann    }
170f4476bd9SJan Schumann
171f4476bd9SJan Schumann    /**
172f4476bd9SJan Schumann     * Modify user data
173f4476bd9SJan Schumann     *
174f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
175311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
176311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
177f4476bd9SJan Schumann     * @return  bool
178f4476bd9SJan Schumann     */
1795aa905e9SAndreas Gohr    public function modifyUser($user, $changes)
1805aa905e9SAndreas Gohr    {
181f4476bd9SJan Schumann        global $ACT;
182f4476bd9SJan Schumann        global $config_cascade;
183f4476bd9SJan Schumann
184f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
185db9faf02SPatrick Brown        if (($userinfo = $this->getUserData($user)) === false) {
186db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
187db9faf02SPatrick Brown            return false;
188db9faf02SPatrick Brown        }
18942cbd322SAndreas Gohr
19042cbd322SAndreas Gohr        // don't modify protected users
19142cbd322SAndreas Gohr        if (!empty($userinfo['protected'])) {
19242cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
19342cbd322SAndreas Gohr            return false;
19442cbd322SAndreas Gohr        }
19542cbd322SAndreas Gohr
196ab9790caSAndreas Gohr        if (!is_array($changes) || $changes === []) return true;
197f4476bd9SJan Schumann
198f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
199f4476bd9SJan Schumann        $newuser = $user;
200f4476bd9SJan Schumann        foreach ($changes as $field => $value) {
201f4476bd9SJan Schumann            if ($field == 'user') {
202f4476bd9SJan Schumann                $newuser = $value;
203f4476bd9SJan Schumann                continue;
204f4476bd9SJan Schumann            }
205f4476bd9SJan Schumann            if ($field == 'pass') $value = auth_cryptPassword($value);
206f4476bd9SJan Schumann            $userinfo[$field] = $value;
207f4476bd9SJan Schumann        }
208f4476bd9SJan Schumann
2095aa905e9SAndreas Gohr        $userline = $this->createUserLine(
21064159a61SAndreas Gohr            $newuser,
21164159a61SAndreas Gohr            $userinfo['pass'],
21264159a61SAndreas Gohr            $userinfo['name'],
21364159a61SAndreas Gohr            $userinfo['mail'],
21464159a61SAndreas Gohr            $userinfo['grps']
21564159a61SAndreas Gohr        );
216f4476bd9SJan Schumann
217699e3c49SPatrick Brown        if (!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) {
218699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
219699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
220311f4603SAndreas Gohr            $ACT = 'register';
221f4476bd9SJan Schumann            return false;
222f4476bd9SJan Schumann        }
223f4476bd9SJan Schumann
22459440086SAndreas Gohr        if(isset($this->users[$user])) unset($this->users[$user]);
225f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
226f4476bd9SJan Schumann        return true;
227f4476bd9SJan Schumann    }
228f4476bd9SJan Schumann
229f4476bd9SJan Schumann    /**
230f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
231f4476bd9SJan Schumann     *
232f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
233f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
234f4476bd9SJan Schumann     * @return  int             the number of users deleted
235f4476bd9SJan Schumann     */
2365aa905e9SAndreas Gohr    public function deleteUsers($users)
2375aa905e9SAndreas Gohr    {
238f4476bd9SJan Schumann        global $config_cascade;
239f4476bd9SJan Schumann
240ab9790caSAndreas Gohr        if (!is_array($users) || $users === []) return 0;
241f4476bd9SJan Schumann
2425aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
243f4476bd9SJan Schumann
244ab9790caSAndreas Gohr        $deleted = [];
245f4476bd9SJan Schumann        foreach ($users as $user) {
24642cbd322SAndreas Gohr            // don't delete protected users
24742cbd322SAndreas Gohr            if (!empty($this->users[$user]['protected'])) {
24842cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
24942cbd322SAndreas Gohr                continue;
25042cbd322SAndreas Gohr            }
251f4476bd9SJan Schumann            if (isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
252f4476bd9SJan Schumann        }
253f4476bd9SJan Schumann
254ab9790caSAndreas Gohr        if ($deleted === []) return 0;
255f4476bd9SJan Schumann
256ab9790caSAndreas Gohr        $pattern = '/^('.implode('|', $deleted).'):/';
257db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
258db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
259db9faf02SPatrick Brown            return 0;
260db9faf02SPatrick Brown        }
261f4476bd9SJan Schumann
2629d24536dSAndreas Gohr        // reload the user list and count the difference
263f4476bd9SJan Schumann        $count = count($this->users);
2645aa905e9SAndreas Gohr        $this->loadUserData();
265f4476bd9SJan Schumann        $count -= count($this->users);
266f4476bd9SJan Schumann        return $count;
267f4476bd9SJan Schumann    }
268f4476bd9SJan Schumann
269f4476bd9SJan Schumann    /**
270f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
271f4476bd9SJan Schumann     *
272f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
273311f4603SAndreas Gohr     *
274311f4603SAndreas Gohr     * @param array $filter
275311f4603SAndreas Gohr     * @return int
276f4476bd9SJan Schumann     */
277ab9790caSAndreas Gohr    public function getUserCount($filter = [])
2785aa905e9SAndreas Gohr    {
279f4476bd9SJan Schumann
2805aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
281f4476bd9SJan Schumann
282ab9790caSAndreas Gohr        if ($filter === []) return count($this->users);
283f4476bd9SJan Schumann
284f4476bd9SJan Schumann        $count = 0;
2855aa905e9SAndreas Gohr        $this->constructPattern($filter);
286f4476bd9SJan Schumann
287f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
2885aa905e9SAndreas Gohr            $count += $this->filter($user, $info);
289f4476bd9SJan Schumann        }
290f4476bd9SJan Schumann
291f4476bd9SJan Schumann        return $count;
292f4476bd9SJan Schumann    }
293f4476bd9SJan Schumann
294f4476bd9SJan Schumann    /**
295f4476bd9SJan Schumann     * Bulk retrieval of user data
296f4476bd9SJan Schumann     *
297f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
298311f4603SAndreas Gohr     *
299311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
300311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
301311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
302311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
303f4476bd9SJan Schumann     */
304ab9790caSAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = [])
3055aa905e9SAndreas Gohr    {
306f4476bd9SJan Schumann
3075aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
308f4476bd9SJan Schumann
3090489c64bSMoisés Braga Ribeiro        Sort::ksort($this->users);
310f4476bd9SJan Schumann
311f4476bd9SJan Schumann        $i     = 0;
312f4476bd9SJan Schumann        $count = 0;
313ab9790caSAndreas Gohr        $out   = [];
3145aa905e9SAndreas Gohr        $this->constructPattern($filter);
315f4476bd9SJan Schumann
316f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
3175aa905e9SAndreas Gohr            if ($this->filter($user, $info)) {
318f4476bd9SJan Schumann                if ($i >= $start) {
319f4476bd9SJan Schumann                    $out[$user] = $info;
320f4476bd9SJan Schumann                    $count++;
321f4476bd9SJan Schumann                    if (($limit > 0) && ($count >= $limit)) break;
322f4476bd9SJan Schumann                }
323f4476bd9SJan Schumann                $i++;
324f4476bd9SJan Schumann            }
325f4476bd9SJan Schumann        }
326f4476bd9SJan Schumann
327f4476bd9SJan Schumann        return $out;
328f4476bd9SJan Schumann    }
329f4476bd9SJan Schumann
330f4476bd9SJan Schumann    /**
331b2fcc742SAnna Dabrowska     * Retrieves groups.
332b2fcc742SAnna Dabrowska     * Loads complete user data into memory before searching for groups.
333b2fcc742SAnna Dabrowska     *
334b2fcc742SAnna Dabrowska     * @param   int   $start index of first group to be returned
335b2fcc742SAnna Dabrowska     * @param   int   $limit max number of groups to be returned
336b2fcc742SAnna Dabrowska     * @return  array
337b2fcc742SAnna Dabrowska     */
338b2fcc742SAnna Dabrowska    public function retrieveGroups($start = 0, $limit = 0)
339b2fcc742SAnna Dabrowska    {
340b2fcc742SAnna Dabrowska        $groups = [];
341b2fcc742SAnna Dabrowska
34242c62e55SAndreas Gohr        if ($this->users === null) $this->loadUserData();
343ab9790caSAndreas Gohr        foreach($this->users as $info) {
344b2fcc742SAnna Dabrowska            $groups = array_merge($groups, array_diff($info['grps'], $groups));
345b2fcc742SAnna Dabrowska        }
3460489c64bSMoisés Braga Ribeiro        Sort::ksort($groups);
347b2fcc742SAnna Dabrowska
348b2fcc742SAnna Dabrowska        if($limit > 0) {
349b2fcc742SAnna Dabrowska            return array_splice($groups, $start, $limit);
350b2fcc742SAnna Dabrowska        }
351b2fcc742SAnna Dabrowska        return array_splice($groups, $start);
352b2fcc742SAnna Dabrowska    }
353b2fcc742SAnna Dabrowska
354b2fcc742SAnna Dabrowska    /**
355f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
356311f4603SAndreas Gohr     *
357311f4603SAndreas Gohr     * @param string $user
358311f4603SAndreas Gohr     * @return string
359f4476bd9SJan Schumann     */
3605aa905e9SAndreas Gohr    public function cleanUser($user)
3615aa905e9SAndreas Gohr    {
362f4476bd9SJan Schumann        global $conf;
3635f18fdf3SAndreas Gohr
3645f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $user));
365f4476bd9SJan Schumann    }
366f4476bd9SJan Schumann
367f4476bd9SJan Schumann    /**
368f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
369311f4603SAndreas Gohr     *
370311f4603SAndreas Gohr     * @param string $group
371311f4603SAndreas Gohr     * @return string
372f4476bd9SJan Schumann     */
3735aa905e9SAndreas Gohr    public function cleanGroup($group)
3745aa905e9SAndreas Gohr    {
375f4476bd9SJan Schumann        global $conf;
3765f18fdf3SAndreas Gohr
3775f18fdf3SAndreas Gohr        return cleanID(str_replace([':', '/', ';'], $conf['sepchar'], $group));
378f4476bd9SJan Schumann    }
379f4476bd9SJan Schumann
380f4476bd9SJan Schumann    /**
381f4476bd9SJan Schumann     * Load all user data
382f4476bd9SJan Schumann     *
383f4476bd9SJan Schumann     * loads the user file into a datastructure
384f4476bd9SJan Schumann     *
385f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
386f4476bd9SJan Schumann     */
3875aa905e9SAndreas Gohr    protected function loadUserData()
3885aa905e9SAndreas Gohr    {
389f4476bd9SJan Schumann        global $config_cascade;
390f4476bd9SJan Schumann
3915aa905e9SAndreas Gohr        $this->users = $this->readUserFile($config_cascade['plainauth.users']['default']);
392f4476bd9SJan Schumann
39342cbd322SAndreas Gohr        // support protected users
39442cbd322SAndreas Gohr        if (!empty($config_cascade['plainauth.users']['protected'])) {
3955aa905e9SAndreas Gohr            $protected = $this->readUserFile($config_cascade['plainauth.users']['protected']);
39642cbd322SAndreas Gohr            foreach (array_keys($protected) as $key) {
39742cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
39842cbd322SAndreas Gohr            }
39942cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
40042cbd322SAndreas Gohr        }
40142cbd322SAndreas Gohr    }
402f4476bd9SJan Schumann
40342cbd322SAndreas Gohr    /**
40442cbd322SAndreas Gohr     * Read user data from given file
40542cbd322SAndreas Gohr     *
40642cbd322SAndreas Gohr     * ignores non existing files
40742cbd322SAndreas Gohr     *
40842cbd322SAndreas Gohr     * @param string $file the file to load data from
40942cbd322SAndreas Gohr     * @return array
41042cbd322SAndreas Gohr     */
4115aa905e9SAndreas Gohr    protected function readUserFile($file)
4125aa905e9SAndreas Gohr    {
413ab9790caSAndreas Gohr        $users = [];
41442cbd322SAndreas Gohr        if (!file_exists($file)) return $users;
41542cbd322SAndreas Gohr
41642cbd322SAndreas Gohr        $lines = file($file);
417f4476bd9SJan Schumann        foreach ($lines as $line) {
418f4476bd9SJan Schumann            $line = preg_replace('/#.*$/', '', $line); //ignore comments
419f4476bd9SJan Schumann            $line = trim($line);
420f4476bd9SJan Schumann            if (empty($line)) continue;
421f4476bd9SJan Schumann
4225aa905e9SAndreas Gohr            $row = $this->splitUserData($line);
423f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
424f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
425f95ecbbfSAngus Gratton
426f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
427f4476bd9SJan Schumann
42842cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
42942cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
43042cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
43142cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
432f4476bd9SJan Schumann        }
43342cbd322SAndreas Gohr        return $users;
434f4476bd9SJan Schumann    }
435f4476bd9SJan Schumann
4365aa905e9SAndreas Gohr    /**
4375aa905e9SAndreas Gohr     * Get the user line split into it's parts
4385aa905e9SAndreas Gohr     *
4395aa905e9SAndreas Gohr     * @param string $line
4405aa905e9SAndreas Gohr     * @return string[]
4415aa905e9SAndreas Gohr     */
4425aa905e9SAndreas Gohr    protected function splitUserData($line)
4435aa905e9SAndreas Gohr    {
44493497020SAndreas Gohr        $data = preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
44593497020SAndreas Gohr        if(count($data) < 5) {
44693497020SAndreas Gohr            $data = array_pad($data, 5, '');
44793497020SAndreas Gohr            Logger::error('User line with less than 5 fields. Possibly corruption in your user file', $data);
4486c8c1f46SChristopher Smith        }
44993497020SAndreas Gohr        return $data;
4506c8c1f46SChristopher Smith    }
4516c8c1f46SChristopher Smith
452f4476bd9SJan Schumann    /**
453311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
454f4476bd9SJan Schumann     *
455f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
456311f4603SAndreas Gohr     *
457311f4603SAndreas Gohr     * @param string $user User login
458311f4603SAndreas Gohr     * @param array  $info User's userinfo array
459311f4603SAndreas Gohr     * @return bool
460f4476bd9SJan Schumann     */
4615aa905e9SAndreas Gohr    protected function filter($user, $info)
4625aa905e9SAndreas Gohr    {
4635aa905e9SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
464f4476bd9SJan Schumann            if ($item == 'user') {
465311f4603SAndreas Gohr                if (!preg_match($pattern, $user)) return false;
466f4476bd9SJan Schumann            } elseif ($item == 'grps') {
467311f4603SAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
468ab9790caSAndreas Gohr            } elseif (!preg_match($pattern, $info[$item])) {
469ab9790caSAndreas Gohr                return false;
470f4476bd9SJan Schumann            }
471f4476bd9SJan Schumann        }
472311f4603SAndreas Gohr        return true;
473f4476bd9SJan Schumann    }
474f4476bd9SJan Schumann
475311f4603SAndreas Gohr    /**
476311f4603SAndreas Gohr     * construct a filter pattern
477311f4603SAndreas Gohr     *
478311f4603SAndreas Gohr     * @param array $filter
479311f4603SAndreas Gohr     */
4805aa905e9SAndreas Gohr    protected function constructPattern($filter)
4815aa905e9SAndreas Gohr    {
482ab9790caSAndreas Gohr        $this->pattern = [];
483f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
4845aa905e9SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
485f4476bd9SJan Schumann        }
486f4476bd9SJan Schumann    }
487f4476bd9SJan Schumann}
488