xref: /dokuwiki/lib/plugins/authplain/auth.php (revision b2fcc742e10e700a52cea2d2c4ebf8bb440e35bd)
1f4476bd9SJan Schumann<?php
2f4476bd9SJan Schumann// must be run within Dokuwiki
3f4476bd9SJan Schumannif(!defined('DOKU_INC')) die();
4f4476bd9SJan Schumann
5f4476bd9SJan Schumann/**
6f4476bd9SJan Schumann * Plaintext authentication backend
7f4476bd9SJan Schumann *
8f4476bd9SJan Schumann * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9f4476bd9SJan Schumann * @author     Andreas Gohr <andi@splitbrain.org>
10f4476bd9SJan Schumann * @author     Chris Smith <chris@jalakai.co.uk>
11f4476bd9SJan Schumann * @author     Jan Schumann <js@schumann-it.com>
12f4476bd9SJan Schumann */
1393a7873eSAndreas Gohrclass auth_plugin_authplain extends DokuWiki_Auth_Plugin {
14311f4603SAndreas Gohr    /** @var array user cache */
15311f4603SAndreas Gohr    protected $users = null;
16311f4603SAndreas Gohr
17311f4603SAndreas Gohr    /** @var array filter pattern */
18311f4603SAndreas Gohr    protected $_pattern = array();
19f4476bd9SJan Schumann
206c8c1f46SChristopher Smith    /** @var bool safe version of preg_split */
216c8c1f46SChristopher Smith    protected $_pregsplit_safe = false;
226c8c1f46SChristopher Smith
23f4476bd9SJan Schumann    /**
24f4476bd9SJan Schumann     * Constructor
25f4476bd9SJan Schumann     *
26f4476bd9SJan Schumann     * Carry out sanity checks to ensure the object is
27f4476bd9SJan Schumann     * able to operate. Set capabilities.
28f4476bd9SJan Schumann     *
29f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
30f4476bd9SJan Schumann     */
31311f4603SAndreas Gohr    public function __construct() {
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;
49*b2fcc742SAnna Dabrowska            $this->cando['getGroups']    = true;
50f4476bd9SJan Schumann        }
516c8c1f46SChristopher Smith
526c8c1f46SChristopher Smith        $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     */
66311f4603SAndreas Gohr    public function checkPass($user, $pass) {
67f4476bd9SJan Schumann        $userinfo = $this->getUserData($user);
68f4476bd9SJan Schumann        if($userinfo === false) return false;
69f4476bd9SJan Schumann
70f4476bd9SJan Schumann        return auth_verifyPassword($pass, $this->users[$user]['pass']);
71f4476bd9SJan Schumann    }
72f4476bd9SJan Schumann
73f4476bd9SJan Schumann    /**
74f4476bd9SJan Schumann     * Return user info
75f4476bd9SJan Schumann     *
76f4476bd9SJan Schumann     * Returns info about the given user needs to contain
77f4476bd9SJan Schumann     * at least these fields:
78f4476bd9SJan Schumann     *
79f4476bd9SJan Schumann     * name string  full name of the user
80f4476bd9SJan Schumann     * mail string  email addres of the user
81f4476bd9SJan Schumann     * grps array   list of groups the user is in
82f4476bd9SJan Schumann     *
83f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
84311f4603SAndreas Gohr     * @param string $user
852046a654SChristopher Smith     * @param bool $requireGroups  (optional) ignored by this plugin, grps info always supplied
86253d4b48SGerrit Uitslag     * @return array|false
87f4476bd9SJan Schumann     */
882046a654SChristopher Smith    public function getUserData($user, $requireGroups=true) {
89f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
90f4476bd9SJan Schumann        return isset($this->users[$user]) ? $this->users[$user] : false;
91f4476bd9SJan Schumann    }
92f4476bd9SJan Schumann
93f4476bd9SJan Schumann    /**
94f95ecbbfSAngus Gratton     * Creates a string suitable for saving as a line
95f95ecbbfSAngus Gratton     * in the file database
96f95ecbbfSAngus Gratton     * (delimiters escaped, etc.)
97f95ecbbfSAngus Gratton     *
98f95ecbbfSAngus Gratton     * @param string $user
99f95ecbbfSAngus Gratton     * @param string $pass
100f95ecbbfSAngus Gratton     * @param string $name
101f95ecbbfSAngus Gratton     * @param string $mail
102f95ecbbfSAngus Gratton     * @param array  $grps list of groups the user is in
103f95ecbbfSAngus Gratton     * @return string
104f95ecbbfSAngus Gratton     */
105f95ecbbfSAngus Gratton    protected function _createUserLine($user, $pass, $name, $mail, $grps) {
106f95ecbbfSAngus Gratton        $groups   = join(',', $grps);
107f95ecbbfSAngus Gratton        $userline = array($user, $pass, $name, $mail, $groups);
108f95ecbbfSAngus Gratton        $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
109f95ecbbfSAngus Gratton        $userline = str_replace(':', '\\:', $userline); // escape : as \:
110f95ecbbfSAngus Gratton        $userline = join(':', $userline)."\n";
111f95ecbbfSAngus Gratton        return $userline;
112f95ecbbfSAngus Gratton    }
113f95ecbbfSAngus Gratton
114f95ecbbfSAngus Gratton    /**
115f4476bd9SJan Schumann     * Create a new User
116f4476bd9SJan Schumann     *
117f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
118f4476bd9SJan Schumann     * occurred and true if everything went well.
119f4476bd9SJan Schumann     *
120f4476bd9SJan Schumann     * The new user will be added to the default group by this
121f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
122f4476bd9SJan Schumann     *
123f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
124f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
125311f4603SAndreas Gohr     *
126311f4603SAndreas Gohr     * @param string $user
127311f4603SAndreas Gohr     * @param string $pwd
128311f4603SAndreas Gohr     * @param string $name
129311f4603SAndreas Gohr     * @param string $mail
130311f4603SAndreas Gohr     * @param array  $grps
131311f4603SAndreas Gohr     * @return bool|null|string
132f4476bd9SJan Schumann     */
133311f4603SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null) {
134f4476bd9SJan Schumann        global $conf;
135f4476bd9SJan Schumann        global $config_cascade;
136f4476bd9SJan Schumann
137f4476bd9SJan Schumann        // user mustn't already exist
138db9faf02SPatrick Brown        if($this->getUserData($user) !== false) {
139db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
140db9faf02SPatrick Brown            return false;
141db9faf02SPatrick Brown        }
142f4476bd9SJan Schumann
143f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
144f4476bd9SJan Schumann
145f4476bd9SJan Schumann        // set default group if no groups specified
146f4476bd9SJan Schumann        if(!is_array($grps)) $grps = array($conf['defaultgroup']);
147f4476bd9SJan Schumann
148f4476bd9SJan Schumann        // prepare user line
149f95ecbbfSAngus Gratton        $userline = $this->_createUserLine($user, $pass, $name, $mail, $grps);
150f4476bd9SJan Schumann
151db9faf02SPatrick Brown        if(!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
152db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
153db9faf02SPatrick Brown            return null;
154f4476bd9SJan Schumann        }
155f4476bd9SJan Schumann
156db9faf02SPatrick Brown        $this->users[$user] = compact('pass', 'name', 'mail', 'grps');
157db9faf02SPatrick Brown        return $pwd;
158f4476bd9SJan Schumann    }
159f4476bd9SJan Schumann
160f4476bd9SJan Schumann    /**
161f4476bd9SJan Schumann     * Modify user data
162f4476bd9SJan Schumann     *
163f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
164311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
165311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
166f4476bd9SJan Schumann     * @return  bool
167f4476bd9SJan Schumann     */
168311f4603SAndreas Gohr    public function modifyUser($user, $changes) {
169f4476bd9SJan Schumann        global $ACT;
170f4476bd9SJan Schumann        global $config_cascade;
171f4476bd9SJan Schumann
172f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
173db9faf02SPatrick Brown        if(($userinfo = $this->getUserData($user)) === false) {
174db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
175db9faf02SPatrick Brown            return false;
176db9faf02SPatrick Brown        }
17742cbd322SAndreas Gohr
17842cbd322SAndreas Gohr        // don't modify protected users
17942cbd322SAndreas Gohr        if(!empty($userinfo['protected'])) {
18042cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
18142cbd322SAndreas Gohr            return false;
18242cbd322SAndreas Gohr        }
18342cbd322SAndreas Gohr
184f4476bd9SJan Schumann        if(!is_array($changes) || !count($changes)) return true;
185f4476bd9SJan Schumann
186f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
187f4476bd9SJan Schumann        $newuser = $user;
188f4476bd9SJan Schumann        foreach($changes as $field => $value) {
189f4476bd9SJan Schumann            if($field == 'user') {
190f4476bd9SJan Schumann                $newuser = $value;
191f4476bd9SJan Schumann                continue;
192f4476bd9SJan Schumann            }
193f4476bd9SJan Schumann            if($field == 'pass') $value = auth_cryptPassword($value);
194f4476bd9SJan Schumann            $userinfo[$field] = $value;
195f4476bd9SJan Schumann        }
196f4476bd9SJan Schumann
197f95ecbbfSAngus Gratton        $userline = $this->_createUserLine($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $userinfo['grps']);
198f4476bd9SJan Schumann
199699e3c49SPatrick Brown        if(!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) {
200699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
201699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
202311f4603SAndreas Gohr            $ACT = 'register';
203f4476bd9SJan Schumann            return false;
204f4476bd9SJan Schumann        }
205f4476bd9SJan Schumann
206f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
207f4476bd9SJan Schumann        return true;
208f4476bd9SJan Schumann    }
209f4476bd9SJan Schumann
210f4476bd9SJan Schumann    /**
211f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
212f4476bd9SJan Schumann     *
213f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
214f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
215f4476bd9SJan Schumann     * @return  int             the number of users deleted
216f4476bd9SJan Schumann     */
217311f4603SAndreas Gohr    public function deleteUsers($users) {
218f4476bd9SJan Schumann        global $config_cascade;
219f4476bd9SJan Schumann
220f4476bd9SJan Schumann        if(!is_array($users) || empty($users)) return 0;
221f4476bd9SJan Schumann
222f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
223f4476bd9SJan Schumann
224f4476bd9SJan Schumann        $deleted = array();
225f4476bd9SJan Schumann        foreach($users as $user) {
22642cbd322SAndreas Gohr            // don't delete protected users
22742cbd322SAndreas Gohr            if(!empty($this->users[$user]['protected'])) {
22842cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
22942cbd322SAndreas Gohr                continue;
23042cbd322SAndreas Gohr            }
231f4476bd9SJan Schumann            if(isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
232f4476bd9SJan Schumann        }
233f4476bd9SJan Schumann
234f4476bd9SJan Schumann        if(empty($deleted)) return 0;
235f4476bd9SJan Schumann
236f4476bd9SJan Schumann        $pattern = '/^('.join('|', $deleted).'):/';
237db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
238db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
239db9faf02SPatrick Brown            return 0;
240db9faf02SPatrick Brown        }
241f4476bd9SJan Schumann
2429d24536dSAndreas Gohr        // reload the user list and count the difference
243f4476bd9SJan Schumann        $count = count($this->users);
244f4476bd9SJan Schumann        $this->_loadUserData();
245f4476bd9SJan Schumann        $count -= count($this->users);
246f4476bd9SJan Schumann        return $count;
247f4476bd9SJan Schumann    }
248f4476bd9SJan Schumann
249f4476bd9SJan Schumann    /**
250f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
251f4476bd9SJan Schumann     *
252f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
253311f4603SAndreas Gohr     *
254311f4603SAndreas Gohr     * @param array $filter
255311f4603SAndreas Gohr     * @return int
256f4476bd9SJan Schumann     */
257311f4603SAndreas Gohr    public function getUserCount($filter = array()) {
258f4476bd9SJan Schumann
259f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
260f4476bd9SJan Schumann
261f4476bd9SJan Schumann        if(!count($filter)) return count($this->users);
262f4476bd9SJan Schumann
263f4476bd9SJan Schumann        $count = 0;
264f4476bd9SJan Schumann        $this->_constructPattern($filter);
265f4476bd9SJan Schumann
266f4476bd9SJan Schumann        foreach($this->users as $user => $info) {
267f4476bd9SJan Schumann            $count += $this->_filter($user, $info);
268f4476bd9SJan Schumann        }
269f4476bd9SJan Schumann
270f4476bd9SJan Schumann        return $count;
271f4476bd9SJan Schumann    }
272f4476bd9SJan Schumann
273f4476bd9SJan Schumann    /**
274f4476bd9SJan Schumann     * Bulk retrieval of user data
275f4476bd9SJan Schumann     *
276f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
277311f4603SAndreas Gohr     *
278311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
279311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
280311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
281311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
282f4476bd9SJan Schumann     */
283311f4603SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array()) {
284f4476bd9SJan Schumann
285f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
286f4476bd9SJan Schumann
287f4476bd9SJan Schumann        ksort($this->users);
288f4476bd9SJan Schumann
289f4476bd9SJan Schumann        $i     = 0;
290f4476bd9SJan Schumann        $count = 0;
291f4476bd9SJan Schumann        $out   = array();
292f4476bd9SJan Schumann        $this->_constructPattern($filter);
293f4476bd9SJan Schumann
294f4476bd9SJan Schumann        foreach($this->users as $user => $info) {
295f4476bd9SJan Schumann            if($this->_filter($user, $info)) {
296f4476bd9SJan Schumann                if($i >= $start) {
297f4476bd9SJan Schumann                    $out[$user] = $info;
298f4476bd9SJan Schumann                    $count++;
299f4476bd9SJan Schumann                    if(($limit > 0) && ($count >= $limit)) break;
300f4476bd9SJan Schumann                }
301f4476bd9SJan Schumann                $i++;
302f4476bd9SJan Schumann            }
303f4476bd9SJan Schumann        }
304f4476bd9SJan Schumann
305f4476bd9SJan Schumann        return $out;
306f4476bd9SJan Schumann    }
307f4476bd9SJan Schumann
308f4476bd9SJan Schumann    /**
309*b2fcc742SAnna Dabrowska     * Retrieves groups.
310*b2fcc742SAnna Dabrowska     * Loads complete user data into memory before searching for groups.
311*b2fcc742SAnna Dabrowska     *
312*b2fcc742SAnna Dabrowska     * @param   int   $start index of first group to be returned
313*b2fcc742SAnna Dabrowska     * @param   int   $limit max number of groups to be returned
314*b2fcc742SAnna Dabrowska     * @return  array
315*b2fcc742SAnna Dabrowska     */
316*b2fcc742SAnna Dabrowska    public function retrieveGroups($start = 0, $limit = 0)
317*b2fcc742SAnna Dabrowska    {
318*b2fcc742SAnna Dabrowska        $groups = [];
319*b2fcc742SAnna Dabrowska
320*b2fcc742SAnna Dabrowska        if ($this->users === null) $this->_loadUserData();
321*b2fcc742SAnna Dabrowska        foreach($this->users as $user => $info) {
322*b2fcc742SAnna Dabrowska            $groups = array_merge($groups, array_diff($info['grps'], $groups));
323*b2fcc742SAnna Dabrowska        }
324*b2fcc742SAnna Dabrowska
325*b2fcc742SAnna Dabrowska        if($limit > 0) {
326*b2fcc742SAnna Dabrowska            return array_splice($groups, $start, $limit);
327*b2fcc742SAnna Dabrowska        }
328*b2fcc742SAnna Dabrowska        return array_splice($groups, $start);
329*b2fcc742SAnna Dabrowska    }
330*b2fcc742SAnna Dabrowska
331*b2fcc742SAnna Dabrowska    /**
332f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
333311f4603SAndreas Gohr     *
334311f4603SAndreas Gohr     * @param string $user
335311f4603SAndreas Gohr     * @return string
336f4476bd9SJan Schumann     */
337311f4603SAndreas Gohr    public function cleanUser($user) {
338f4476bd9SJan Schumann        global $conf;
339f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $user));
340f4476bd9SJan Schumann    }
341f4476bd9SJan Schumann
342f4476bd9SJan Schumann    /**
343f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
344311f4603SAndreas Gohr     *
345311f4603SAndreas Gohr     * @param string $group
346311f4603SAndreas Gohr     * @return string
347f4476bd9SJan Schumann     */
348311f4603SAndreas Gohr    public function cleanGroup($group) {
349f4476bd9SJan Schumann        global $conf;
350f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $group));
351f4476bd9SJan Schumann    }
352f4476bd9SJan Schumann
353f4476bd9SJan Schumann    /**
354f4476bd9SJan Schumann     * Load all user data
355f4476bd9SJan Schumann     *
356f4476bd9SJan Schumann     * loads the user file into a datastructure
357f4476bd9SJan Schumann     *
358f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
359f4476bd9SJan Schumann     */
360311f4603SAndreas Gohr    protected function _loadUserData() {
361f4476bd9SJan Schumann        global $config_cascade;
362f4476bd9SJan Schumann
36342cbd322SAndreas Gohr        $this->users = $this->_readUserFile($config_cascade['plainauth.users']['default']);
364f4476bd9SJan Schumann
36542cbd322SAndreas Gohr        // support protected users
36642cbd322SAndreas Gohr        if(!empty($config_cascade['plainauth.users']['protected'])) {
36742cbd322SAndreas Gohr            $protected = $this->_readUserFile($config_cascade['plainauth.users']['protected']);
36842cbd322SAndreas Gohr            foreach(array_keys($protected) as $key) {
36942cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
37042cbd322SAndreas Gohr            }
37142cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
37242cbd322SAndreas Gohr        }
37342cbd322SAndreas Gohr    }
374f4476bd9SJan Schumann
37542cbd322SAndreas Gohr    /**
37642cbd322SAndreas Gohr     * Read user data from given file
37742cbd322SAndreas Gohr     *
37842cbd322SAndreas Gohr     * ignores non existing files
37942cbd322SAndreas Gohr     *
38042cbd322SAndreas Gohr     * @param string $file the file to load data from
38142cbd322SAndreas Gohr     * @return array
38242cbd322SAndreas Gohr     */
38342cbd322SAndreas Gohr    protected function _readUserFile($file) {
38442cbd322SAndreas Gohr        $users = array();
38542cbd322SAndreas Gohr        if(!file_exists($file)) return $users;
38642cbd322SAndreas Gohr
38742cbd322SAndreas Gohr        $lines = file($file);
388f4476bd9SJan Schumann        foreach($lines as $line) {
389f4476bd9SJan Schumann            $line = preg_replace('/#.*$/', '', $line); //ignore comments
390f4476bd9SJan Schumann            $line = trim($line);
391f4476bd9SJan Schumann            if(empty($line)) continue;
392f4476bd9SJan Schumann
3936c8c1f46SChristopher Smith            $row = $this->_splitUserData($line);
394f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
395f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
396f95ecbbfSAngus Gratton
397f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
398f4476bd9SJan Schumann
39942cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
40042cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
40142cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
40242cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
403f4476bd9SJan Schumann        }
40442cbd322SAndreas Gohr        return $users;
405f4476bd9SJan Schumann    }
406f4476bd9SJan Schumann
4076c8c1f46SChristopher Smith    protected function _splitUserData($line){
4086c8c1f46SChristopher Smith        // due to a bug in PCRE 6.6, preg_split will fail with the regex we use here
4096c8c1f46SChristopher Smith        // refer github issues 877 & 885
4106c8c1f46SChristopher Smith        if ($this->_pregsplit_safe){
4116c8c1f46SChristopher Smith            return preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
4126c8c1f46SChristopher Smith        }
4136c8c1f46SChristopher Smith
4146c8c1f46SChristopher Smith        $row = array();
4156c8c1f46SChristopher Smith        $piece = '';
4169d846ff4SChristopher Smith        $len = strlen($line);
4179d846ff4SChristopher Smith        for($i=0; $i<$len; $i++){
4186c8c1f46SChristopher Smith            if ($line[$i]=='\\'){
4199d846ff4SChristopher Smith                $piece .= $line[$i];
4206c8c1f46SChristopher Smith                $i++;
4219d846ff4SChristopher Smith                if ($i>=$len) break;
4226c8c1f46SChristopher Smith            } else if ($line[$i]==':'){
4236c8c1f46SChristopher Smith                $row[] = $piece;
4246c8c1f46SChristopher Smith                $piece = '';
4256c8c1f46SChristopher Smith                continue;
4266c8c1f46SChristopher Smith            }
4276c8c1f46SChristopher Smith            $piece .= $line[$i];
4286c8c1f46SChristopher Smith        }
4296c8c1f46SChristopher Smith        $row[] = $piece;
4306c8c1f46SChristopher Smith
4316c8c1f46SChristopher Smith        return $row;
4326c8c1f46SChristopher Smith    }
4336c8c1f46SChristopher Smith
434f4476bd9SJan Schumann    /**
435311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
436f4476bd9SJan Schumann     *
437f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
438311f4603SAndreas Gohr     *
439311f4603SAndreas Gohr     * @param string $user User login
440311f4603SAndreas Gohr     * @param array  $info User's userinfo array
441311f4603SAndreas Gohr     * @return bool
442f4476bd9SJan Schumann     */
443311f4603SAndreas Gohr    protected function _filter($user, $info) {
444f4476bd9SJan Schumann        foreach($this->_pattern as $item => $pattern) {
445f4476bd9SJan Schumann            if($item == 'user') {
446311f4603SAndreas Gohr                if(!preg_match($pattern, $user)) return false;
447f4476bd9SJan Schumann            } else if($item == 'grps') {
448311f4603SAndreas Gohr                if(!count(preg_grep($pattern, $info['grps']))) return false;
449f4476bd9SJan Schumann            } else {
450311f4603SAndreas Gohr                if(!preg_match($pattern, $info[$item])) return false;
451f4476bd9SJan Schumann            }
452f4476bd9SJan Schumann        }
453311f4603SAndreas Gohr        return true;
454f4476bd9SJan Schumann    }
455f4476bd9SJan Schumann
456311f4603SAndreas Gohr    /**
457311f4603SAndreas Gohr     * construct a filter pattern
458311f4603SAndreas Gohr     *
459311f4603SAndreas Gohr     * @param array $filter
460311f4603SAndreas Gohr     */
461311f4603SAndreas Gohr    protected function _constructPattern($filter) {
462f4476bd9SJan Schumann        $this->_pattern = array();
463f4476bd9SJan Schumann        foreach($filter as $item => $pattern) {
464f4476bd9SJan Schumann            $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
465f4476bd9SJan Schumann        }
466f4476bd9SJan Schumann    }
467f4476bd9SJan Schumann}
468