xref: /dokuwiki/lib/plugins/authplain/auth.php (revision 42cbd322ad0cf816cca498eeb93bb1b8e4e7db5e)
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;
49f4476bd9SJan Schumann        }
506c8c1f46SChristopher Smith
516c8c1f46SChristopher Smith        $this->_pregsplit_safe = version_compare(PCRE_VERSION,'6.7','>=');
52f4476bd9SJan Schumann    }
53f4476bd9SJan Schumann
54f4476bd9SJan Schumann    /**
55311f4603SAndreas Gohr     * Check user+password
56f4476bd9SJan Schumann     *
57f4476bd9SJan Schumann     * Checks if the given user exists and the given
58f4476bd9SJan Schumann     * plaintext password is correct
59f4476bd9SJan Schumann     *
60f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
61311f4603SAndreas Gohr     * @param string $user
62311f4603SAndreas Gohr     * @param string $pass
63f4476bd9SJan Schumann     * @return  bool
64f4476bd9SJan Schumann     */
65311f4603SAndreas Gohr    public function checkPass($user, $pass) {
66f4476bd9SJan Schumann        $userinfo = $this->getUserData($user);
67f4476bd9SJan Schumann        if($userinfo === false) return false;
68f4476bd9SJan Schumann
69f4476bd9SJan Schumann        return auth_verifyPassword($pass, $this->users[$user]['pass']);
70f4476bd9SJan Schumann    }
71f4476bd9SJan Schumann
72f4476bd9SJan Schumann    /**
73f4476bd9SJan Schumann     * Return user info
74f4476bd9SJan Schumann     *
75f4476bd9SJan Schumann     * Returns info about the given user needs to contain
76f4476bd9SJan Schumann     * at least these fields:
77f4476bd9SJan Schumann     *
78f4476bd9SJan Schumann     * name string  full name of the user
79f4476bd9SJan Schumann     * mail string  email addres of the user
80f4476bd9SJan Schumann     * grps array   list of groups the user is in
81f4476bd9SJan Schumann     *
82f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
83311f4603SAndreas Gohr     * @param string $user
842046a654SChristopher Smith     * @param bool $requireGroups  (optional) ignored by this plugin, grps info always supplied
85253d4b48SGerrit Uitslag     * @return array|false
86f4476bd9SJan Schumann     */
872046a654SChristopher Smith    public function getUserData($user, $requireGroups=true) {
88f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
89f4476bd9SJan Schumann        return isset($this->users[$user]) ? $this->users[$user] : false;
90f4476bd9SJan Schumann    }
91f4476bd9SJan Schumann
92f4476bd9SJan Schumann    /**
93f95ecbbfSAngus Gratton     * Creates a string suitable for saving as a line
94f95ecbbfSAngus Gratton     * in the file database
95f95ecbbfSAngus Gratton     * (delimiters escaped, etc.)
96f95ecbbfSAngus Gratton     *
97f95ecbbfSAngus Gratton     * @param string $user
98f95ecbbfSAngus Gratton     * @param string $pass
99f95ecbbfSAngus Gratton     * @param string $name
100f95ecbbfSAngus Gratton     * @param string $mail
101f95ecbbfSAngus Gratton     * @param array  $grps list of groups the user is in
102f95ecbbfSAngus Gratton     * @return string
103f95ecbbfSAngus Gratton     */
104f95ecbbfSAngus Gratton    protected function _createUserLine($user, $pass, $name, $mail, $grps) {
105f95ecbbfSAngus Gratton        $groups   = join(',', $grps);
106f95ecbbfSAngus Gratton        $userline = array($user, $pass, $name, $mail, $groups);
107f95ecbbfSAngus Gratton        $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
108f95ecbbfSAngus Gratton        $userline = str_replace(':', '\\:', $userline); // escape : as \:
109f95ecbbfSAngus Gratton        $userline = join(':', $userline)."\n";
110f95ecbbfSAngus Gratton        return $userline;
111f95ecbbfSAngus Gratton    }
112f95ecbbfSAngus Gratton
113f95ecbbfSAngus Gratton    /**
114f4476bd9SJan Schumann     * Create a new User
115f4476bd9SJan Schumann     *
116f4476bd9SJan Schumann     * Returns false if the user already exists, null when an error
117f4476bd9SJan Schumann     * occurred and true if everything went well.
118f4476bd9SJan Schumann     *
119f4476bd9SJan Schumann     * The new user will be added to the default group by this
120f4476bd9SJan Schumann     * function if grps are not specified (default behaviour).
121f4476bd9SJan Schumann     *
122f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
123f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
124311f4603SAndreas Gohr     *
125311f4603SAndreas Gohr     * @param string $user
126311f4603SAndreas Gohr     * @param string $pwd
127311f4603SAndreas Gohr     * @param string $name
128311f4603SAndreas Gohr     * @param string $mail
129311f4603SAndreas Gohr     * @param array  $grps
130311f4603SAndreas Gohr     * @return bool|null|string
131f4476bd9SJan Schumann     */
132311f4603SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null) {
133f4476bd9SJan Schumann        global $conf;
134f4476bd9SJan Schumann        global $config_cascade;
135f4476bd9SJan Schumann
136f4476bd9SJan Schumann        // user mustn't already exist
137db9faf02SPatrick Brown        if($this->getUserData($user) !== false) {
138db9faf02SPatrick Brown            msg($this->getLang('userexists'), -1);
139db9faf02SPatrick Brown            return false;
140db9faf02SPatrick Brown        }
141f4476bd9SJan Schumann
142f4476bd9SJan Schumann        $pass = auth_cryptPassword($pwd);
143f4476bd9SJan Schumann
144f4476bd9SJan Schumann        // set default group if no groups specified
145f4476bd9SJan Schumann        if(!is_array($grps)) $grps = array($conf['defaultgroup']);
146f4476bd9SJan Schumann
147f4476bd9SJan Schumann        // prepare user line
148f95ecbbfSAngus Gratton        $userline = $this->_createUserLine($user, $pass, $name, $mail, $grps);
149f4476bd9SJan Schumann
150db9faf02SPatrick Brown        if(!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) {
151db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
152db9faf02SPatrick Brown            return null;
153f4476bd9SJan Schumann        }
154f4476bd9SJan Schumann
155db9faf02SPatrick Brown        $this->users[$user] = compact('pass', 'name', 'mail', 'grps');
156db9faf02SPatrick Brown        return $pwd;
157f4476bd9SJan Schumann    }
158f4476bd9SJan Schumann
159f4476bd9SJan Schumann    /**
160f4476bd9SJan Schumann     * Modify user data
161f4476bd9SJan Schumann     *
162f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
163311f4603SAndreas Gohr     * @param   string $user      nick of the user to be changed
164311f4603SAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed (password will be clear text)
165f4476bd9SJan Schumann     * @return  bool
166f4476bd9SJan Schumann     */
167311f4603SAndreas Gohr    public function modifyUser($user, $changes) {
168f4476bd9SJan Schumann        global $ACT;
169f4476bd9SJan Schumann        global $config_cascade;
170f4476bd9SJan Schumann
171f4476bd9SJan Schumann        // sanity checks, user must already exist and there must be something to change
172db9faf02SPatrick Brown        if(($userinfo = $this->getUserData($user)) === false) {
173db9faf02SPatrick Brown            msg($this->getLang('usernotexists'), -1);
174db9faf02SPatrick Brown            return false;
175db9faf02SPatrick Brown        }
176*42cbd322SAndreas Gohr
177*42cbd322SAndreas Gohr        // don't modify protected users
178*42cbd322SAndreas Gohr        if(!empty($userinfo['protected'])) {
179*42cbd322SAndreas Gohr            msg(sprintf($this->getLang('protected'), hsc($user)), -1);
180*42cbd322SAndreas Gohr            return false;
181*42cbd322SAndreas Gohr        }
182*42cbd322SAndreas Gohr
183f4476bd9SJan Schumann        if(!is_array($changes) || !count($changes)) return true;
184f4476bd9SJan Schumann
185f4476bd9SJan Schumann        // update userinfo with new data, remembering to encrypt any password
186f4476bd9SJan Schumann        $newuser = $user;
187f4476bd9SJan Schumann        foreach($changes as $field => $value) {
188f4476bd9SJan Schumann            if($field == 'user') {
189f4476bd9SJan Schumann                $newuser = $value;
190f4476bd9SJan Schumann                continue;
191f4476bd9SJan Schumann            }
192f4476bd9SJan Schumann            if($field == 'pass') $value = auth_cryptPassword($value);
193f4476bd9SJan Schumann            $userinfo[$field] = $value;
194f4476bd9SJan Schumann        }
195f4476bd9SJan Schumann
196f95ecbbfSAngus Gratton        $userline = $this->_createUserLine($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $userinfo['grps']);
197f4476bd9SJan Schumann
198699e3c49SPatrick Brown        if(!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) {
199699e3c49SPatrick Brown            msg('There was an error modifying your user data. You may need to register again.', -1);
200699e3c49SPatrick Brown            // FIXME, io functions should be fail-safe so existing data isn't lost
201311f4603SAndreas Gohr            $ACT = 'register';
202f4476bd9SJan Schumann            return false;
203f4476bd9SJan Schumann        }
204f4476bd9SJan Schumann
205f4476bd9SJan Schumann        $this->users[$newuser] = $userinfo;
206f4476bd9SJan Schumann        return true;
207f4476bd9SJan Schumann    }
208f4476bd9SJan Schumann
209f4476bd9SJan Schumann    /**
210f4476bd9SJan Schumann     * Remove one or more users from the list of registered users
211f4476bd9SJan Schumann     *
212f4476bd9SJan Schumann     * @author  Christopher Smith <chris@jalakai.co.uk>
213f4476bd9SJan Schumann     * @param   array  $users   array of users to be deleted
214f4476bd9SJan Schumann     * @return  int             the number of users deleted
215f4476bd9SJan Schumann     */
216311f4603SAndreas Gohr    public function deleteUsers($users) {
217f4476bd9SJan Schumann        global $config_cascade;
218f4476bd9SJan Schumann
219f4476bd9SJan Schumann        if(!is_array($users) || empty($users)) return 0;
220f4476bd9SJan Schumann
221f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
222f4476bd9SJan Schumann
223f4476bd9SJan Schumann        $deleted = array();
224f4476bd9SJan Schumann        foreach($users as $user) {
225*42cbd322SAndreas Gohr            // don't delete protected users
226*42cbd322SAndreas Gohr            if(!empty($this->users[$user]['protected'])) {
227*42cbd322SAndreas Gohr                msg(sprintf($this->getLang('protected'), hsc($user)), -1);
228*42cbd322SAndreas Gohr                continue;
229*42cbd322SAndreas Gohr            }
230f4476bd9SJan Schumann            if(isset($this->users[$user])) $deleted[] = preg_quote($user, '/');
231f4476bd9SJan Schumann        }
232f4476bd9SJan Schumann
233f4476bd9SJan Schumann        if(empty($deleted)) return 0;
234f4476bd9SJan Schumann
235f4476bd9SJan Schumann        $pattern = '/^('.join('|', $deleted).'):/';
236db9faf02SPatrick Brown        if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) {
237db9faf02SPatrick Brown            msg($this->getLang('writefail'), -1);
238db9faf02SPatrick Brown            return 0;
239db9faf02SPatrick Brown        }
240f4476bd9SJan Schumann
2419d24536dSAndreas Gohr        // reload the user list and count the difference
242f4476bd9SJan Schumann        $count = count($this->users);
243f4476bd9SJan Schumann        $this->_loadUserData();
244f4476bd9SJan Schumann        $count -= count($this->users);
245f4476bd9SJan Schumann        return $count;
246f4476bd9SJan Schumann    }
247f4476bd9SJan Schumann
248f4476bd9SJan Schumann    /**
249f4476bd9SJan Schumann     * Return a count of the number of user which meet $filter criteria
250f4476bd9SJan Schumann     *
251f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
252311f4603SAndreas Gohr     *
253311f4603SAndreas Gohr     * @param array $filter
254311f4603SAndreas Gohr     * @return int
255f4476bd9SJan Schumann     */
256311f4603SAndreas Gohr    public function getUserCount($filter = array()) {
257f4476bd9SJan Schumann
258f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
259f4476bd9SJan Schumann
260f4476bd9SJan Schumann        if(!count($filter)) return count($this->users);
261f4476bd9SJan Schumann
262f4476bd9SJan Schumann        $count = 0;
263f4476bd9SJan Schumann        $this->_constructPattern($filter);
264f4476bd9SJan Schumann
265f4476bd9SJan Schumann        foreach($this->users as $user => $info) {
266f4476bd9SJan Schumann            $count += $this->_filter($user, $info);
267f4476bd9SJan Schumann        }
268f4476bd9SJan Schumann
269f4476bd9SJan Schumann        return $count;
270f4476bd9SJan Schumann    }
271f4476bd9SJan Schumann
272f4476bd9SJan Schumann    /**
273f4476bd9SJan Schumann     * Bulk retrieval of user data
274f4476bd9SJan Schumann     *
275f4476bd9SJan Schumann     * @author  Chris Smith <chris@jalakai.co.uk>
276311f4603SAndreas Gohr     *
277311f4603SAndreas Gohr     * @param   int   $start index of first user to be returned
278311f4603SAndreas Gohr     * @param   int   $limit max number of users to be returned
279311f4603SAndreas Gohr     * @param   array $filter array of field/pattern pairs
280311f4603SAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
281f4476bd9SJan Schumann     */
282311f4603SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array()) {
283f4476bd9SJan Schumann
284f4476bd9SJan Schumann        if($this->users === null) $this->_loadUserData();
285f4476bd9SJan Schumann
286f4476bd9SJan Schumann        ksort($this->users);
287f4476bd9SJan Schumann
288f4476bd9SJan Schumann        $i     = 0;
289f4476bd9SJan Schumann        $count = 0;
290f4476bd9SJan Schumann        $out   = array();
291f4476bd9SJan Schumann        $this->_constructPattern($filter);
292f4476bd9SJan Schumann
293f4476bd9SJan Schumann        foreach($this->users as $user => $info) {
294f4476bd9SJan Schumann            if($this->_filter($user, $info)) {
295f4476bd9SJan Schumann                if($i >= $start) {
296f4476bd9SJan Schumann                    $out[$user] = $info;
297f4476bd9SJan Schumann                    $count++;
298f4476bd9SJan Schumann                    if(($limit > 0) && ($count >= $limit)) break;
299f4476bd9SJan Schumann                }
300f4476bd9SJan Schumann                $i++;
301f4476bd9SJan Schumann            }
302f4476bd9SJan Schumann        }
303f4476bd9SJan Schumann
304f4476bd9SJan Schumann        return $out;
305f4476bd9SJan Schumann    }
306f4476bd9SJan Schumann
307f4476bd9SJan Schumann    /**
308f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for usernames
309311f4603SAndreas Gohr     *
310311f4603SAndreas Gohr     * @param string $user
311311f4603SAndreas Gohr     * @return string
312f4476bd9SJan Schumann     */
313311f4603SAndreas Gohr    public function cleanUser($user) {
314f4476bd9SJan Schumann        global $conf;
315f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $user));
316f4476bd9SJan Schumann    }
317f4476bd9SJan Schumann
318f4476bd9SJan Schumann    /**
319f4476bd9SJan Schumann     * Only valid pageid's (no namespaces) for groupnames
320311f4603SAndreas Gohr     *
321311f4603SAndreas Gohr     * @param string $group
322311f4603SAndreas Gohr     * @return string
323f4476bd9SJan Schumann     */
324311f4603SAndreas Gohr    public function cleanGroup($group) {
325f4476bd9SJan Schumann        global $conf;
326f4476bd9SJan Schumann        return cleanID(str_replace(':', $conf['sepchar'], $group));
327f4476bd9SJan Schumann    }
328f4476bd9SJan Schumann
329f4476bd9SJan Schumann    /**
330f4476bd9SJan Schumann     * Load all user data
331f4476bd9SJan Schumann     *
332f4476bd9SJan Schumann     * loads the user file into a datastructure
333f4476bd9SJan Schumann     *
334f4476bd9SJan Schumann     * @author  Andreas Gohr <andi@splitbrain.org>
335f4476bd9SJan Schumann     */
336311f4603SAndreas Gohr    protected function _loadUserData() {
337f4476bd9SJan Schumann        global $config_cascade;
338f4476bd9SJan Schumann
339*42cbd322SAndreas Gohr        $this->users = $this->_readUserFile($config_cascade['plainauth.users']['default']);
340f4476bd9SJan Schumann
341*42cbd322SAndreas Gohr        // support protected users
342*42cbd322SAndreas Gohr        if(!empty($config_cascade['plainauth.users']['protected'])) {
343*42cbd322SAndreas Gohr            $protected = $this->_readUserFile($config_cascade['plainauth.users']['protected']);
344*42cbd322SAndreas Gohr            foreach(array_keys($protected) as $key) {
345*42cbd322SAndreas Gohr                $protected[$key]['protected'] = true;
346*42cbd322SAndreas Gohr            }
347*42cbd322SAndreas Gohr            $this->users = array_merge($this->users, $protected);
348*42cbd322SAndreas Gohr        }
349*42cbd322SAndreas Gohr    }
350f4476bd9SJan Schumann
351*42cbd322SAndreas Gohr    /**
352*42cbd322SAndreas Gohr     * Read user data from given file
353*42cbd322SAndreas Gohr     *
354*42cbd322SAndreas Gohr     * ignores non existing files
355*42cbd322SAndreas Gohr     *
356*42cbd322SAndreas Gohr     * @param string $file the file to load data from
357*42cbd322SAndreas Gohr     * @return array
358*42cbd322SAndreas Gohr     */
359*42cbd322SAndreas Gohr    protected function _readUserFile($file) {
360*42cbd322SAndreas Gohr        $users = array();
361*42cbd322SAndreas Gohr        if(!file_exists($file)) return $users;
362*42cbd322SAndreas Gohr
363*42cbd322SAndreas Gohr        $lines = file($file);
364f4476bd9SJan Schumann        foreach($lines as $line) {
365f4476bd9SJan Schumann            $line = preg_replace('/#.*$/', '', $line); //ignore comments
366f4476bd9SJan Schumann            $line = trim($line);
367f4476bd9SJan Schumann            if(empty($line)) continue;
368f4476bd9SJan Schumann
369f95ecbbfSAngus Gratton            /* NB: preg_split can be deprecated/replaced with str_getcsv once dokuwiki is min php 5.3 */
3706c8c1f46SChristopher Smith            $row = $this->_splitUserData($line);
371f95ecbbfSAngus Gratton            $row = str_replace('\\:', ':', $row);
372f95ecbbfSAngus Gratton            $row = str_replace('\\\\', '\\', $row);
373f95ecbbfSAngus Gratton
374f4476bd9SJan Schumann            $groups = array_values(array_filter(explode(",", $row[4])));
375f4476bd9SJan Schumann
376*42cbd322SAndreas Gohr            $users[$row[0]]['pass'] = $row[1];
377*42cbd322SAndreas Gohr            $users[$row[0]]['name'] = urldecode($row[2]);
378*42cbd322SAndreas Gohr            $users[$row[0]]['mail'] = $row[3];
379*42cbd322SAndreas Gohr            $users[$row[0]]['grps'] = $groups;
380f4476bd9SJan Schumann        }
381*42cbd322SAndreas Gohr        return $users;
382f4476bd9SJan Schumann    }
383f4476bd9SJan Schumann
3846c8c1f46SChristopher Smith    protected function _splitUserData($line){
3856c8c1f46SChristopher Smith        // due to a bug in PCRE 6.6, preg_split will fail with the regex we use here
3866c8c1f46SChristopher Smith        // refer github issues 877 & 885
3876c8c1f46SChristopher Smith        if ($this->_pregsplit_safe){
3886c8c1f46SChristopher Smith            return preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5);       // allow for : escaped as \:
3896c8c1f46SChristopher Smith        }
3906c8c1f46SChristopher Smith
3916c8c1f46SChristopher Smith        $row = array();
3926c8c1f46SChristopher Smith        $piece = '';
3939d846ff4SChristopher Smith        $len = strlen($line);
3949d846ff4SChristopher Smith        for($i=0; $i<$len; $i++){
3956c8c1f46SChristopher Smith            if ($line[$i]=='\\'){
3969d846ff4SChristopher Smith                $piece .= $line[$i];
3976c8c1f46SChristopher Smith                $i++;
3989d846ff4SChristopher Smith                if ($i>=$len) break;
3996c8c1f46SChristopher Smith            } else if ($line[$i]==':'){
4006c8c1f46SChristopher Smith                $row[] = $piece;
4016c8c1f46SChristopher Smith                $piece = '';
4026c8c1f46SChristopher Smith                continue;
4036c8c1f46SChristopher Smith            }
4046c8c1f46SChristopher Smith            $piece .= $line[$i];
4056c8c1f46SChristopher Smith        }
4066c8c1f46SChristopher Smith        $row[] = $piece;
4076c8c1f46SChristopher Smith
4086c8c1f46SChristopher Smith        return $row;
4096c8c1f46SChristopher Smith    }
4106c8c1f46SChristopher Smith
411f4476bd9SJan Schumann    /**
412311f4603SAndreas Gohr     * return true if $user + $info match $filter criteria, false otherwise
413f4476bd9SJan Schumann     *
414f4476bd9SJan Schumann     * @author   Chris Smith <chris@jalakai.co.uk>
415311f4603SAndreas Gohr     *
416311f4603SAndreas Gohr     * @param string $user User login
417311f4603SAndreas Gohr     * @param array  $info User's userinfo array
418311f4603SAndreas Gohr     * @return bool
419f4476bd9SJan Schumann     */
420311f4603SAndreas Gohr    protected function _filter($user, $info) {
421f4476bd9SJan Schumann        foreach($this->_pattern as $item => $pattern) {
422f4476bd9SJan Schumann            if($item == 'user') {
423311f4603SAndreas Gohr                if(!preg_match($pattern, $user)) return false;
424f4476bd9SJan Schumann            } else if($item == 'grps') {
425311f4603SAndreas Gohr                if(!count(preg_grep($pattern, $info['grps']))) return false;
426f4476bd9SJan Schumann            } else {
427311f4603SAndreas Gohr                if(!preg_match($pattern, $info[$item])) return false;
428f4476bd9SJan Schumann            }
429f4476bd9SJan Schumann        }
430311f4603SAndreas Gohr        return true;
431f4476bd9SJan Schumann    }
432f4476bd9SJan Schumann
433311f4603SAndreas Gohr    /**
434311f4603SAndreas Gohr     * construct a filter pattern
435311f4603SAndreas Gohr     *
436311f4603SAndreas Gohr     * @param array $filter
437311f4603SAndreas Gohr     */
438311f4603SAndreas Gohr    protected function _constructPattern($filter) {
439f4476bd9SJan Schumann        $this->_pattern = array();
440f4476bd9SJan Schumann        foreach($filter as $item => $pattern) {
441f4476bd9SJan Schumann            $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
442f4476bd9SJan Schumann        }
443f4476bd9SJan Schumann    }
444f4476bd9SJan Schumann}
445