xref: /dokuwiki/lib/plugins/authplain/auth.php (revision 42c62e557b443e27796171b5bedd777d77472586)
1f4476bd9SJan Schumann<?php
2f4476bd9SJan Schumann
3f4476bd9SJan Schumann/**
4f4476bd9SJan Schumann * Plaintext authentication backend
5f4476bd9SJan Schumann *
6f4476bd9SJan Schumann * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7f4476bd9SJan Schumann * @author     Andreas Gohr <andi@splitbrain.org>
8f4476bd9SJan Schumann * @author     Chris Smith <chris@jalakai.co.uk>
9f4476bd9SJan Schumann * @author     Jan Schumann <js@schumann-it.com>
10f4476bd9SJan Schumann */
115aa905e9SAndreas Gohrclass auth_plugin_authplain extends DokuWiki_Auth_Plugin
125aa905e9SAndreas Gohr{
13311f4603SAndreas Gohr    /** @var array user cache */
14311f4603SAndreas Gohr    protected $users = null;
15311f4603SAndreas Gohr
16311f4603SAndreas Gohr    /** @var array filter pattern */
175aa905e9SAndreas Gohr    protected $pattern = array();
18f4476bd9SJan Schumann
196c8c1f46SChristopher Smith    /** @var bool safe version of preg_split */
205aa905e9SAndreas Gohr    protected $pregsplit_safe = false;
216c8c1f46SChristopher Smith
22f4476bd9SJan Schumann    /**
23f4476bd9SJan Schumann     * Constructor
24f4476bd9SJan Schumann     *
25f4476bd9SJan Schumann     * Carry out sanity checks to ensure the object is
26f4476bd9SJan Schumann     * able to operate. Set capabilities.
27f4476bd9SJan Schumann     *
28f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
29f4476bd9SJan Schumann     */
305aa905e9SAndreas Gohr    public function __construct()
315aa905e9SAndreas Gohr    {
32454d868bSAndreas Gohr        parent::__construct();
33f4476bd9SJan Schumann        global $config_cascade;
34f4476bd9SJan Schumann
35f4476bd9SJan Schumann        if (!@is_readable($config_cascade['plainauth.users']['default'])) {
36f4476bd9SJan Schumann            $this->success = false;
37f4476bd9SJan Schumann        } else {
38f4476bd9SJan Schumann            if (@is_writable($config_cascade['plainauth.users']['default'])) {
39f4476bd9SJan Schumann                $this->cando['addUser']   = true;
40f4476bd9SJan Schumann                $this->cando['delUser']   = true;
41f4476bd9SJan Schumann                $this->cando['modLogin']  = true;
42f4476bd9SJan Schumann                $this->cando['modPass']   = true;
43f4476bd9SJan Schumann                $this->cando['modName']   = true;
44f4476bd9SJan Schumann                $this->cando['modMail']   = true;
45f4476bd9SJan Schumann                $this->cando['modGroups'] = true;
46f4476bd9SJan Schumann            }
47f4476bd9SJan Schumann            $this->cando['getUsers']     = true;
48f4476bd9SJan Schumann            $this->cando['getUserCount'] = true;
49b2fcc742SAnna Dabrowska            $this->cando['getGroups']    = true;
50f4476bd9SJan Schumann        }
516c8c1f46SChristopher Smith
525aa905e9SAndreas Gohr        $this->pregsplit_safe = version_compare(PCRE_VERSION, '6.7', '>=');
53f4476bd9SJan Schumann    }
54f4476bd9SJan Schumann
55f4476bd9SJan Schumann    /**
56311f4603SAndreas Gohr     * Check user+password
57f4476bd9SJan Schumann     *
58f4476bd9SJan Schumann     * Checks if the given user exists and the given
59f4476bd9SJan Schumann     * plaintext password is correct
60f4476bd9SJan Schumann     *
61f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
62311f4603SAndreas Gohr     * @param string $user
63311f4603SAndreas Gohr     * @param string $pass
64f4476bd9SJan Schumann     * @return  bool
65f4476bd9SJan Schumann     */
665aa905e9SAndreas Gohr    public function checkPass($user, $pass)
675aa905e9SAndreas Gohr    {
68f4476bd9SJan Schumann        $userinfo = $this->getUserData($user);
69f4476bd9SJan Schumann        if ($userinfo === false) return false;
70f4476bd9SJan Schumann
71f4476bd9SJan Schumann        return auth_verifyPassword($pass, $this->users[$user]['pass']);
72f4476bd9SJan Schumann    }
73f4476bd9SJan Schumann
74f4476bd9SJan Schumann    /**
75f4476bd9SJan Schumann     * Return user info
76f4476bd9SJan Schumann     *
77f4476bd9SJan Schumann     * Returns info about the given user needs to contain
78f4476bd9SJan Schumann     * at least these fields:
79f4476bd9SJan Schumann     *
80f4476bd9SJan Schumann     * name string  full name of the user
81f4476bd9SJan Schumann     * mail string  email addres of the user
82f4476bd9SJan Schumann     * grps array   list of groups the user is in
83f4476bd9SJan Schumann     *
84f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
85311f4603SAndreas Gohr     * @param string $user
862046a654SChristopher Smith     * @param bool $requireGroups  (optional) ignored by this plugin, grps info always supplied
87253d4b48SGerrit Uitslag     * @return array|false
88f4476bd9SJan Schumann     */
895aa905e9SAndreas Gohr    public function getUserData($user, $requireGroups = true)
905aa905e9SAndreas Gohr    {
915aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
92f4476bd9SJan Schumann        return isset($this->users[$user]) ? $this->users[$user] : false;
93f4476bd9SJan Schumann    }
94f4476bd9SJan Schumann
95f4476bd9SJan Schumann    /**
96f95ecbbfSAngus Gratton     * Creates a string suitable for saving as a line
97f95ecbbfSAngus Gratton     * in the file database
98f95ecbbfSAngus Gratton     * (delimiters escaped, etc.)
99f95ecbbfSAngus Gratton     *
100f95ecbbfSAngus Gratton     * @param string $user
101f95ecbbfSAngus Gratton     * @param string $pass
102f95ecbbfSAngus Gratton     * @param string $name
103f95ecbbfSAngus Gratton     * @param string $mail
104f95ecbbfSAngus Gratton     * @param array  $grps list of groups the user is in
105f95ecbbfSAngus Gratton     * @return string
106f95ecbbfSAngus Gratton     */
1075aa905e9SAndreas Gohr    protected function createUserLine($user, $pass, $name, $mail, $grps)
1085aa905e9SAndreas Gohr    {
109f95ecbbfSAngus Gratton        $groups   = join(',', $grps);
110f95ecbbfSAngus Gratton        $userline = array($user, $pass, $name, $mail, $groups);
111f95ecbbfSAngus Gratton        $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
112f95ecbbfSAngus Gratton        $userline = str_replace(':', '\\:', $userline); // escape : as \:
113f95ecbbfSAngus Gratton        $userline = join(':', $userline)."\n";
114f95ecbbfSAngus Gratton        return $userline;
115f95ecbbfSAngus Gratton    }
116f95ecbbfSAngus Gratton
117f95ecbbfSAngus Gratton    /**
118f4476bd9SJan Schumann     * Create a new User
119f4476bd9SJan Schumann     *
120f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
121f4476bd9SJan Schumann     * occurred and true if everything went well.
122f4476bd9SJan Schumann     *
123f4476bd9SJan Schumann     * The new user will be added to the default group by this
124f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
125f4476bd9SJan Schumann     *
126f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
127f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
128311f4603SAndreas Gohr     *
129311f4603SAndreas Gohr     * @param string $user
130311f4603SAndreas Gohr     * @param string $pwd
131311f4603SAndreas Gohr     * @param string $name
132311f4603SAndreas Gohr     * @param string $mail
133311f4603SAndreas Gohr     * @param array  $grps
134311f4603SAndreas Gohr     * @return bool|null|string
135f4476bd9SJan Schumann     */
1365aa905e9SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null)
1375aa905e9SAndreas Gohr    {
138f4476bd9SJan Schumann        global $conf;
139f4476bd9SJan Schumann        global $config_cascade;
140f4476bd9SJan Schumann
141f4476bd9SJan Schumann        // user mustn't already exist
142db9faf02SPatrick Brown        if ($this->getUserData($user) !== false) {
143db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
144db9faf02SPatrick Brown            return false;
145db9faf02SPatrick Brown        }
146f4476bd9SJan Schumann
147f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
148f4476bd9SJan Schumann
149f4476bd9SJan Schumann        // set default group if no groups specified
150f4476bd9SJan Schumann        if (!is_array($grps)) $grps = array($conf['defaultgroup']);
151f4476bd9SJan Schumann
152f4476bd9SJan Schumann        // prepare user line
1535aa905e9SAndreas Gohr        $userline = $this->createUserLine($user, $pass, $name, $mail, $grps);
154f4476bd9SJan Schumann
155db9faf02SPatrick Brown        if (!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
156db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
157db9faf02SPatrick Brown            return null;
158f4476bd9SJan Schumann        }
159f4476bd9SJan Schumann
160db9faf02SPatrick Brown        $this->users[$user] = compact('pass', 'name', 'mail', 'grps');
161db9faf02SPatrick Brown        return $pwd;
162f4476bd9SJan Schumann    }
163f4476bd9SJan Schumann
164f4476bd9SJan Schumann    /**
165f4476bd9SJan Schumann     * Modify user data
166f4476bd9SJan Schumann     *
167f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
168311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
169311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
170f4476bd9SJan Schumann     * @return  bool
171f4476bd9SJan Schumann     */
1725aa905e9SAndreas Gohr    public function modifyUser($user, $changes)
1735aa905e9SAndreas Gohr    {
174f4476bd9SJan Schumann        global $ACT;
175f4476bd9SJan Schumann        global $config_cascade;
176f4476bd9SJan Schumann
177f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
178db9faf02SPatrick Brown        if (($userinfo = $this->getUserData($user)) === false) {
179db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
180db9faf02SPatrick Brown            return false;
181db9faf02SPatrick Brown        }
18242cbd322SAndreas Gohr
18342cbd322SAndreas Gohr        // don't modify protected users
18442cbd322SAndreas Gohr        if (!empty($userinfo['protected'])) {
18542cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
18642cbd322SAndreas Gohr            return false;
18742cbd322SAndreas Gohr        }
18842cbd322SAndreas Gohr
189f4476bd9SJan Schumann        if (!is_array($changes) || !count($changes)) return true;
190f4476bd9SJan Schumann
191f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
192f4476bd9SJan Schumann        $newuser = $user;
193f4476bd9SJan Schumann        foreach ($changes as $field => $value) {
194f4476bd9SJan Schumann            if ($field == 'user') {
195f4476bd9SJan Schumann                $newuser = $value;
196f4476bd9SJan Schumann                continue;
197f4476bd9SJan Schumann            }
198f4476bd9SJan Schumann            if ($field == 'pass') $value = auth_cryptPassword($value);
199f4476bd9SJan Schumann            $userinfo[$field] = $value;
200f4476bd9SJan Schumann        }
201f4476bd9SJan Schumann
2025aa905e9SAndreas Gohr        $userline = $this->createUserLine(
20364159a61SAndreas Gohr            $newuser,
20464159a61SAndreas Gohr            $userinfo['pass'],
20564159a61SAndreas Gohr            $userinfo['name'],
20664159a61SAndreas Gohr            $userinfo['mail'],
20764159a61SAndreas Gohr            $userinfo['grps']
20864159a61SAndreas Gohr        );
209f4476bd9SJan Schumann
210699e3c49SPatrick Brown        if (!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) {
211699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
212699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
213311f4603SAndreas Gohr            $ACT = 'register';
214f4476bd9SJan Schumann            return false;
215f4476bd9SJan Schumann        }
216f4476bd9SJan Schumann
217f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
218f4476bd9SJan Schumann        return true;
219f4476bd9SJan Schumann    }
220f4476bd9SJan Schumann
221f4476bd9SJan Schumann    /**
222f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
223f4476bd9SJan Schumann     *
224f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
225f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
226f4476bd9SJan Schumann     * @return  int             the number of users deleted
227f4476bd9SJan Schumann     */
2285aa905e9SAndreas Gohr    public function deleteUsers($users)
2295aa905e9SAndreas Gohr    {
230f4476bd9SJan Schumann        global $config_cascade;
231f4476bd9SJan Schumann
232f4476bd9SJan Schumann        if (!is_array($users) || empty($users)) return 0;
233f4476bd9SJan Schumann
2345aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
235f4476bd9SJan Schumann
236f4476bd9SJan Schumann        $deleted = array();
237f4476bd9SJan Schumann        foreach ($users as $user) {
23842cbd322SAndreas Gohr            // don't delete protected users
23942cbd322SAndreas Gohr            if (!empty($this->users[$user]['protected'])) {
24042cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
24142cbd322SAndreas Gohr                continue;
24242cbd322SAndreas Gohr            }
243f4476bd9SJan Schumann            if (isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
244f4476bd9SJan Schumann        }
245f4476bd9SJan Schumann
246f4476bd9SJan Schumann        if (empty($deleted)) return 0;
247f4476bd9SJan Schumann
248f4476bd9SJan Schumann        $pattern = '/^('.join('|', $deleted).'):/';
249db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
250db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
251db9faf02SPatrick Brown            return 0;
252db9faf02SPatrick Brown        }
253f4476bd9SJan Schumann
2549d24536dSAndreas Gohr        // reload the user list and count the difference
255f4476bd9SJan Schumann        $count = count($this->users);
2565aa905e9SAndreas Gohr        $this->loadUserData();
257f4476bd9SJan Schumann        $count -= count($this->users);
258f4476bd9SJan Schumann        return $count;
259f4476bd9SJan Schumann    }
260f4476bd9SJan Schumann
261f4476bd9SJan Schumann    /**
262f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
263f4476bd9SJan Schumann     *
264f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
265311f4603SAndreas Gohr     *
266311f4603SAndreas Gohr     * @param array $filter
267311f4603SAndreas Gohr     * @return int
268f4476bd9SJan Schumann     */
2695aa905e9SAndreas Gohr    public function getUserCount($filter = array())
2705aa905e9SAndreas Gohr    {
271f4476bd9SJan Schumann
2725aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
273f4476bd9SJan Schumann
274f4476bd9SJan Schumann        if (!count($filter)) return count($this->users);
275f4476bd9SJan Schumann
276f4476bd9SJan Schumann        $count = 0;
2775aa905e9SAndreas Gohr        $this->constructPattern($filter);
278f4476bd9SJan Schumann
279f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
2805aa905e9SAndreas Gohr            $count += $this->filter($user, $info);
281f4476bd9SJan Schumann        }
282f4476bd9SJan Schumann
283f4476bd9SJan Schumann        return $count;
284f4476bd9SJan Schumann    }
285f4476bd9SJan Schumann
286f4476bd9SJan Schumann    /**
287f4476bd9SJan Schumann     * Bulk retrieval of user data
288f4476bd9SJan Schumann     *
289f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
290311f4603SAndreas Gohr     *
291311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
292311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
293311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
294311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
295f4476bd9SJan Schumann     */
2965aa905e9SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array())
2975aa905e9SAndreas Gohr    {
298f4476bd9SJan Schumann
2995aa905e9SAndreas Gohr        if ($this->users === null) $this->loadUserData();
300f4476bd9SJan Schumann
301f4476bd9SJan Schumann        ksort($this->users);
302f4476bd9SJan Schumann
303f4476bd9SJan Schumann        $i     = 0;
304f4476bd9SJan Schumann        $count = 0;
305f4476bd9SJan Schumann        $out   = array();
3065aa905e9SAndreas Gohr        $this->constructPattern($filter);
307f4476bd9SJan Schumann
308f4476bd9SJan Schumann        foreach ($this->users as $user => $info) {
3095aa905e9SAndreas Gohr            if ($this->filter($user, $info)) {
310f4476bd9SJan Schumann                if ($i >= $start) {
311f4476bd9SJan Schumann                    $out[$user] = $info;
312f4476bd9SJan Schumann                    $count++;
313f4476bd9SJan Schumann                    if (($limit > 0) && ($count >= $limit)) break;
314f4476bd9SJan Schumann                }
315f4476bd9SJan Schumann                $i++;
316f4476bd9SJan Schumann            }
317f4476bd9SJan Schumann        }
318f4476bd9SJan Schumann
319f4476bd9SJan Schumann        return $out;
320f4476bd9SJan Schumann    }
321f4476bd9SJan Schumann
322f4476bd9SJan Schumann    /**
323b2fcc742SAnna Dabrowska     * Retrieves groups.
324b2fcc742SAnna Dabrowska     * Loads complete user data into memory before searching for groups.
325b2fcc742SAnna Dabrowska     *
326b2fcc742SAnna Dabrowska     * @param   int   $start index of first group to be returned
327b2fcc742SAnna Dabrowska     * @param   int   $limit max number of groups to be returned
328b2fcc742SAnna Dabrowska     * @return  array
329b2fcc742SAnna Dabrowska     */
330b2fcc742SAnna Dabrowska    public function retrieveGroups($start = 0, $limit = 0)
331b2fcc742SAnna Dabrowska    {
332b2fcc742SAnna Dabrowska        $groups = [];
333b2fcc742SAnna Dabrowska
334*42c62e55SAndreas Gohr        if ($this->users === null) $this->loadUserData();
335b2fcc742SAnna Dabrowska        foreach($this->users as $user => $info) {
336b2fcc742SAnna Dabrowska            $groups = array_merge($groups, array_diff($info['grps'], $groups));
337b2fcc742SAnna Dabrowska        }
338b2fcc742SAnna Dabrowska
339b2fcc742SAnna Dabrowska        if($limit > 0) {
340b2fcc742SAnna Dabrowska            return array_splice($groups, $start, $limit);
341b2fcc742SAnna Dabrowska        }
342b2fcc742SAnna Dabrowska        return array_splice($groups, $start);
343b2fcc742SAnna Dabrowska    }
344b2fcc742SAnna Dabrowska
345b2fcc742SAnna Dabrowska    /**
346f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
347311f4603SAndreas Gohr     *
348311f4603SAndreas Gohr     * @param string $user
349311f4603SAndreas Gohr     * @return string
350f4476bd9SJan Schumann     */
3515aa905e9SAndreas Gohr    public function cleanUser($user)
3525aa905e9SAndreas Gohr    {
353f4476bd9SJan Schumann        global $conf;
354f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $user));
355f4476bd9SJan Schumann    }
356f4476bd9SJan Schumann
357f4476bd9SJan Schumann    /**
358f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
359311f4603SAndreas Gohr     *
360311f4603SAndreas Gohr     * @param string $group
361311f4603SAndreas Gohr     * @return string
362f4476bd9SJan Schumann     */
3635aa905e9SAndreas Gohr    public function cleanGroup($group)
3645aa905e9SAndreas Gohr    {
365f4476bd9SJan Schumann        global $conf;
366f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $group));
367f4476bd9SJan Schumann    }
368f4476bd9SJan Schumann
369f4476bd9SJan Schumann    /**
370f4476bd9SJan Schumann     * Load all user data
371f4476bd9SJan Schumann     *
372f4476bd9SJan Schumann     * loads the user file into a datastructure
373f4476bd9SJan Schumann     *
374f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
375f4476bd9SJan Schumann     */
3765aa905e9SAndreas Gohr    protected function loadUserData()
3775aa905e9SAndreas Gohr    {
378f4476bd9SJan Schumann        global $config_cascade;
379f4476bd9SJan Schumann
3805aa905e9SAndreas Gohr        $this->users = $this->readUserFile($config_cascade['plainauth.users']['default']);
381f4476bd9SJan Schumann
38242cbd322SAndreas Gohr        // support protected users
38342cbd322SAndreas Gohr        if (!empty($config_cascade['plainauth.users']['protected'])) {
3845aa905e9SAndreas Gohr            $protected = $this->readUserFile($config_cascade['plainauth.users']['protected']);
38542cbd322SAndreas Gohr            foreach (array_keys($protected) as $key) {
38642cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
38742cbd322SAndreas Gohr            }
38842cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
38942cbd322SAndreas Gohr        }
39042cbd322SAndreas Gohr    }
391f4476bd9SJan Schumann
39242cbd322SAndreas Gohr    /**
39342cbd322SAndreas Gohr     * Read user data from given file
39442cbd322SAndreas Gohr     *
39542cbd322SAndreas Gohr     * ignores non existing files
39642cbd322SAndreas Gohr     *
39742cbd322SAndreas Gohr     * @param string $file the file to load data from
39842cbd322SAndreas Gohr     * @return array
39942cbd322SAndreas Gohr     */
4005aa905e9SAndreas Gohr    protected function readUserFile($file)
4015aa905e9SAndreas Gohr    {
40242cbd322SAndreas Gohr        $users = array();
40342cbd322SAndreas Gohr        if (!file_exists($file)) return $users;
40442cbd322SAndreas Gohr
40542cbd322SAndreas Gohr        $lines = file($file);
406f4476bd9SJan Schumann        foreach ($lines as $line) {
407f4476bd9SJan Schumann            $line = preg_replace('/#.*$/', '', $line); //ignore comments
408f4476bd9SJan Schumann            $line = trim($line);
409f4476bd9SJan Schumann            if (empty($line)) continue;
410f4476bd9SJan Schumann
4115aa905e9SAndreas Gohr            $row = $this->splitUserData($line);
412f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
413f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
414f95ecbbfSAngus Gratton
415f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
416f4476bd9SJan Schumann
41742cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
41842cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
41942cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
42042cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
421f4476bd9SJan Schumann        }
42242cbd322SAndreas Gohr        return $users;
423f4476bd9SJan Schumann    }
424f4476bd9SJan Schumann
4255aa905e9SAndreas Gohr    /**
4265aa905e9SAndreas Gohr     * Get the user line split into it's parts
4275aa905e9SAndreas Gohr     *
4285aa905e9SAndreas Gohr     * @param string $line
4295aa905e9SAndreas Gohr     * @return string[]
4305aa905e9SAndreas Gohr     */
4315aa905e9SAndreas Gohr    protected function splitUserData($line)
4325aa905e9SAndreas Gohr    {
4336c8c1f46SChristopher Smith        // due to a bug in PCRE 6.6, preg_split will fail with the regex we use here
4346c8c1f46SChristopher Smith        // refer github issues 877 & 885
4355aa905e9SAndreas Gohr        if ($this->pregsplit_safe) {
4366c8c1f46SChristopher Smith            return preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
4376c8c1f46SChristopher Smith        }
4386c8c1f46SChristopher Smith
4396c8c1f46SChristopher Smith        $row = array();
4406c8c1f46SChristopher Smith        $piece = '';
4419d846ff4SChristopher Smith        $len = strlen($line);
4429d846ff4SChristopher Smith        for ($i=0; $i<$len; $i++) {
4436c8c1f46SChristopher Smith            if ($line[$i]=='\\') {
4449d846ff4SChristopher Smith                $piece .= $line[$i];
4456c8c1f46SChristopher Smith                $i++;
4469d846ff4SChristopher Smith                if ($i>=$len) break;
4476c8c1f46SChristopher Smith            } elseif ($line[$i]==':') {
4486c8c1f46SChristopher Smith                $row[] = $piece;
4496c8c1f46SChristopher Smith                $piece = '';
4506c8c1f46SChristopher Smith                continue;
4516c8c1f46SChristopher Smith            }
4526c8c1f46SChristopher Smith            $piece .= $line[$i];
4536c8c1f46SChristopher Smith        }
4546c8c1f46SChristopher Smith        $row[] = $piece;
4556c8c1f46SChristopher Smith
4566c8c1f46SChristopher Smith        return $row;
4576c8c1f46SChristopher Smith    }
4586c8c1f46SChristopher Smith
459f4476bd9SJan Schumann    /**
460311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
461f4476bd9SJan Schumann     *
462f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
463311f4603SAndreas Gohr     *
464311f4603SAndreas Gohr     * @param string $user User login
465311f4603SAndreas Gohr     * @param array  $info User's userinfo array
466311f4603SAndreas Gohr     * @return bool
467f4476bd9SJan Schumann     */
4685aa905e9SAndreas Gohr    protected function filter($user, $info)
4695aa905e9SAndreas Gohr    {
4705aa905e9SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
471f4476bd9SJan Schumann            if ($item == 'user') {
472311f4603SAndreas Gohr                if (!preg_match($pattern, $user)) return false;
473f4476bd9SJan Schumann            } elseif ($item == 'grps') {
474311f4603SAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
475f4476bd9SJan Schumann            } else {
476311f4603SAndreas Gohr                if (!preg_match($pattern, $info[$item])) return false;
477f4476bd9SJan Schumann            }
478f4476bd9SJan Schumann        }
479311f4603SAndreas Gohr        return true;
480f4476bd9SJan Schumann    }
481f4476bd9SJan Schumann
482311f4603SAndreas Gohr    /**
483311f4603SAndreas Gohr     * construct a filter pattern
484311f4603SAndreas Gohr     *
485311f4603SAndreas Gohr     * @param array $filter
486311f4603SAndreas Gohr     */
4875aa905e9SAndreas Gohr    protected function constructPattern($filter)
4885aa905e9SAndreas Gohr    {
4895aa905e9SAndreas Gohr        $this->pattern = array();
490f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
4915aa905e9SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
492f4476bd9SJan Schumann        }
493f4476bd9SJan Schumann    }
494f4476bd9SJan Schumann}
495