xref: /dokuwiki/lib/plugins/authad/auth.php (revision 31667ec67823633de149106f2f0de3f1dbc5fd1e)
1f4476bd9SJan Schumann<?php
2f4476bd9SJan Schumann
3*31667ec6SAndreas Gohruse dokuwiki\Logger;
4*31667ec6SAndreas Gohr
5f4476bd9SJan Schumann/**
6f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki
7f4476bd9SJan Schumann *
8f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier
9f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library
10f4476bd9SJan Schumann *
11f4476bd9SJan Schumann * Usage:
12f4476bd9SJan Schumann *   Set DokuWiki's local.protected.php auth setting to read
13f4476bd9SJan Schumann *
14f4476bd9SJan Schumann *   $conf['authtype']       = 'authad';
15f4476bd9SJan Schumann *
1632fd494aSAndreas Gohr *   $conf['plugin']['authad']['account_suffix']     = '@my.domain.org';
1732fd494aSAndreas Gohr *   $conf['plugin']['authad']['base_dn']            = 'DC=my,DC=domain,DC=org';
1832fd494aSAndreas Gohr *   $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
19f4476bd9SJan Schumann *
20f4476bd9SJan Schumann *   //optional:
2132fd494aSAndreas Gohr *   $conf['plugin']['authad']['sso']                = 1;
223002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_username']     = 'root';
233002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_password']     = 'pass';
2432fd494aSAndreas Gohr *   $conf['plugin']['authad']['real_primarygroup']  = 1;
2532fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_ssl']            = 1;
2632fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_tls']            = 1;
2732fd494aSAndreas Gohr *   $conf['plugin']['authad']['debug']              = 1;
2893a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
2932fd494aSAndreas Gohr *   $conf['plugin']['authad']['expirywarn']         = 5;
30f4476bd9SJan Schumann *
31f4476bd9SJan Schumann *   // get additional information to the userinfo array
32f4476bd9SJan Schumann *   // add a list of comma separated ldap contact fields.
33f4476bd9SJan Schumann *   $conf['plugin']['authad']['additional'] = 'field1,field2';
34f4476bd9SJan Schumann *
35f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
36f4476bd9SJan Schumann * @author  James Van Lommel <jamesvl@gmail.com>
37f4476bd9SJan Schumann * @link    http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
38f4476bd9SJan Schumann * @author  Andreas Gohr <andi@splitbrain.org>
39f4476bd9SJan Schumann * @author  Jan Schumann <js@schumann-it.com>
40f4476bd9SJan Schumann */
41a4337320SAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin
42a4337320SAndreas Gohr{
4332fd494aSAndreas Gohr
4493a7873eSAndreas Gohr    /**
4593a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
4693a7873eSAndreas Gohr     */
4793a7873eSAndreas Gohr    protected $opts = array();
4832fd494aSAndreas Gohr
4993a7873eSAndreas Gohr    /**
5093a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
5193a7873eSAndreas Gohr     */
5293a7873eSAndreas Gohr    protected $adldap = array();
5393a7873eSAndreas Gohr
5493a7873eSAndreas Gohr    /**
5593a7873eSAndreas Gohr     * @var bool message state
5693a7873eSAndreas Gohr     */
5793a7873eSAndreas Gohr    protected $msgshown = false;
5893a7873eSAndreas Gohr
5993a7873eSAndreas Gohr    /**
6093a7873eSAndreas Gohr     * @var array user listing cache
6193a7873eSAndreas Gohr     */
6293a7873eSAndreas Gohr    protected $users = array();
6393a7873eSAndreas Gohr
6493a7873eSAndreas Gohr    /**
6593a7873eSAndreas Gohr     * @var array filter patterns for listing users
6693a7873eSAndreas Gohr     */
67a4337320SAndreas Gohr    protected $pattern = array();
68f4476bd9SJan Schumann
69a4337320SAndreas Gohr    protected $grpsusers = array();
70c52f6cd2SMichael Große
71f4476bd9SJan Schumann    /**
72f4476bd9SJan Schumann     * Constructor
73f4476bd9SJan Schumann     */
74a4337320SAndreas Gohr    public function __construct()
75a4337320SAndreas Gohr    {
7600d58927SMichael Hamann        global $INPUT;
77454d868bSAndreas Gohr        parent::__construct();
78454d868bSAndreas Gohr
79a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php');
80a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php');
81a4337320SAndreas Gohr
8232fd494aSAndreas Gohr        // we load the config early to modify it a bit here
8332fd494aSAndreas Gohr        $this->loadConfig();
84f4476bd9SJan Schumann
85f4476bd9SJan Schumann        // additional information fields
8632fd494aSAndreas Gohr        if (isset($this->conf['additional'])) {
8732fd494aSAndreas Gohr            $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']);
8832fd494aSAndreas Gohr            $this->conf['additional'] = explode(',', $this->conf['additional']);
8932fd494aSAndreas Gohr        } else $this->conf['additional'] = array();
90f4476bd9SJan Schumann
91f4476bd9SJan Schumann        // ldap extension is needed
92f4476bd9SJan Schumann        if (!function_exists('ldap_connect')) {
9332fd494aSAndreas Gohr            if ($this->conf['debug'])
94f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
95f4476bd9SJan Schumann            $this->success = false;
96f4476bd9SJan Schumann            return;
97f4476bd9SJan Schumann        }
98f4476bd9SJan Schumann
99f4476bd9SJan Schumann        // Prepare SSO
100d34a2a38SAndreas Gohr        if (!empty($_SERVER['REMOTE_USER'])) {
101d34a2a38SAndreas Gohr            // make sure the right encoding is used
102d34a2a38SAndreas Gohr            if ($this->getConf('sso_charset')) {
103d34a2a38SAndreas Gohr                $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']);
1048cbc5ee8SAndreas Gohr            } elseif (!\dokuwiki\Utf8\Clean::isUtf8($_SERVER['REMOTE_USER'])) {
10593a7873eSAndreas Gohr                $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
10693a7873eSAndreas Gohr            }
107d34a2a38SAndreas Gohr
108d34a2a38SAndreas Gohr            // trust the incoming user
109d34a2a38SAndreas Gohr            if ($this->conf['sso']) {
11093a7873eSAndreas Gohr                $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
111f4476bd9SJan Schumann
112f4476bd9SJan Schumann                // we need to simulate a login
113f4476bd9SJan Schumann                if (empty($_COOKIE[DOKU_COOKIE])) {
11400d58927SMichael Hamann                    $INPUT->set('u', $_SERVER['REMOTE_USER']);
11500d58927SMichael Hamann                    $INPUT->set('p', 'sso_only');
116f4476bd9SJan Schumann                }
117f4476bd9SJan Schumann            }
118d34a2a38SAndreas Gohr        }
119f4476bd9SJan Schumann
12093a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
121bb30445dSMichael Wilmes        $this->cando['modName'] = (bool)$this->conf['update_name'];
122bb30445dSMichael Wilmes        $this->cando['modMail'] = (bool)$this->conf['update_mail'];
12325f80763SMichael Große        $this->cando['getUserCount'] = true;
124f4476bd9SJan Schumann    }
125f4476bd9SJan Schumann
126f4476bd9SJan Schumann    /**
127a154806fSAndreas Gohr     * Load domain config on capability check
128a154806fSAndreas Gohr     *
129a154806fSAndreas Gohr     * @param string $cap
130a154806fSAndreas Gohr     * @return bool
131a154806fSAndreas Gohr     */
132a4337320SAndreas Gohr    public function canDo($cap)
133a4337320SAndreas Gohr    {
134a154806fSAndreas Gohr        //capabilities depend on config, which may change depending on domain
135a4337320SAndreas Gohr        $domain = $this->getUserDomain($_SERVER['REMOTE_USER']);
136a4337320SAndreas Gohr        $this->loadServerConfig($domain);
137a154806fSAndreas Gohr        return parent::canDo($cap);
138a154806fSAndreas Gohr    }
139a154806fSAndreas Gohr
140a154806fSAndreas Gohr    /**
141f4476bd9SJan Schumann     * Check user+password [required auth function]
142f4476bd9SJan Schumann     *
143f4476bd9SJan Schumann     * Checks if the given user exists and the given
144f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
145f4476bd9SJan Schumann     * to the LDAP server
146f4476bd9SJan Schumann     *
147f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
14893a7873eSAndreas Gohr     * @param string $user
14993a7873eSAndreas Gohr     * @param string $pass
150f4476bd9SJan Schumann     * @return  bool
151f4476bd9SJan Schumann     */
152a4337320SAndreas Gohr    public function checkPass($user, $pass)
153a4337320SAndreas Gohr    {
154f4476bd9SJan Schumann        if ($_SERVER['REMOTE_USER'] &&
155f4476bd9SJan Schumann            $_SERVER['REMOTE_USER'] == $user &&
15632fd494aSAndreas Gohr            $this->conf['sso']
15793a7873eSAndreas Gohr        ) return true;
158f4476bd9SJan Schumann
159a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
16093a7873eSAndreas Gohr        if (!$adldap) return false;
16193a7873eSAndreas Gohr
162a4337320SAndreas Gohr        try {
163a4337320SAndreas Gohr            return $adldap->authenticate($this->getUserName($user), $pass);
164a4337320SAndreas Gohr        } catch (adLDAPException $e) {
165a4337320SAndreas Gohr            // shouldn't really happen
166a4337320SAndreas Gohr            return false;
167a4337320SAndreas Gohr        }
168f4476bd9SJan Schumann    }
169f4476bd9SJan Schumann
170f4476bd9SJan Schumann    /**
171f4476bd9SJan Schumann     * Return user info [required auth function]
172f4476bd9SJan Schumann     *
173f4476bd9SJan Schumann     * Returns info about the given user needs to contain
174f4476bd9SJan Schumann     * at least these fields:
175f4476bd9SJan Schumann     *
176f4476bd9SJan Schumann     * name    string  full name of the user
177f4476bd9SJan Schumann     * mail    string  email address of the user
178f4476bd9SJan Schumann     * grps    array   list of groups the user is in
179f4476bd9SJan Schumann     *
18093a7873eSAndreas Gohr     * This AD specific function returns the following
181f4476bd9SJan Schumann     * addional fields:
182f4476bd9SJan Schumann     *
183f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
18493a7873eSAndreas Gohr     * uid        string    samaccountname
18593a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
18693a7873eSAndreas Gohr     * expires    true      if the password expires
18793a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
18893a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
189f4476bd9SJan Schumann     *
190f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
19193a7873eSAndreas Gohr     * @param string $user
1922046a654SChristopher Smith     * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin
19393a7873eSAndreas Gohr     * @return array
194f4476bd9SJan Schumann     */
195a4337320SAndreas Gohr    public function getUserData($user, $requireGroups = true)
196a4337320SAndreas Gohr    {
197f4476bd9SJan Schumann        global $conf;
19893a7873eSAndreas Gohr        global $lang;
19993a7873eSAndreas Gohr        global $ID;
200a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
201a4337320SAndreas Gohr        if (!$adldap) return array();
202f4476bd9SJan Schumann
20393a7873eSAndreas Gohr        if ($user == '') return array();
20493a7873eSAndreas Gohr
20593a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
206f4476bd9SJan Schumann
207f4476bd9SJan Schumann        // add additional fields to read
20832fd494aSAndreas Gohr        $fields = array_merge($fields, $this->conf['additional']);
209f4476bd9SJan Schumann        $fields = array_unique($fields);
21014642325SAndreas Gohr        $fields = array_filter($fields);
211f4476bd9SJan Schumann
212f4476bd9SJan Schumann        //get info for given user
213a4337320SAndreas Gohr        $result = $adldap->user()->info($this->getUserName($user), $fields);
21493a7873eSAndreas Gohr        if ($result == false) {
21593a7873eSAndreas Gohr            return array();
21693a7873eSAndreas Gohr        }
21793a7873eSAndreas Gohr
218f4476bd9SJan Schumann        //general user info
21959bc3b48SGerrit Uitslag        $info = array();
220f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
221f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
222f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
223f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
22493a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
22593a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
22693a7873eSAndreas Gohr        //will it expire?
22793a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
228f4476bd9SJan Schumann
229f4476bd9SJan Schumann        // additional information
23032fd494aSAndreas Gohr        foreach ($this->conf['additional'] as $field) {
231f4476bd9SJan Schumann            if (isset($result[0][strtolower($field)])) {
232f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
233f4476bd9SJan Schumann            }
234f4476bd9SJan Schumann        }
235f4476bd9SJan Schumann
236f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
237a4337320SAndreas Gohr        $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']);
238f4476bd9SJan Schumann
239f4476bd9SJan Schumann        if (is_array($info['grps'])) {
240f4476bd9SJan Schumann            foreach ($info['grps'] as $ndx => $group) {
241f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
242f4476bd9SJan Schumann            }
243f4476bd9SJan Schumann        }
244f4476bd9SJan Schumann
245f4476bd9SJan Schumann        // always add the default group to the list of groups
246f4476bd9SJan Schumann        if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
247f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
248f4476bd9SJan Schumann        }
249f4476bd9SJan Schumann
25093a7873eSAndreas Gohr        // add the user's domain to the groups
251a4337320SAndreas Gohr        $domain = $this->getUserDomain($user);
25293a7873eSAndreas Gohr        if ($domain && !in_array("domain-$domain", (array) $info['grps'])) {
25393a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
25493a7873eSAndreas Gohr        }
25593a7873eSAndreas Gohr
25693a7873eSAndreas Gohr        // check expiry time
25732fd494aSAndreas Gohr        if ($info['expires'] && $this->conf['expirywarn']) {
258a4337320SAndreas Gohr            try {
2591e52e72aSAndreas Gohr                $expiry = $adldap->user()->passwordExpiry($user);
2601e52e72aSAndreas Gohr                if (is_array($expiry)) {
2611e52e72aSAndreas Gohr                    $info['expiresat'] = $expiry['expiryts'];
2621e52e72aSAndreas Gohr                    $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60));
26393a7873eSAndreas Gohr
26493a7873eSAndreas Gohr                    // if this is the current user, warn him (once per request only)
26593a7873eSAndreas Gohr                    if (($_SERVER['REMOTE_USER'] == $user) &&
2661e52e72aSAndreas Gohr                        ($info['expiresin'] <= $this->conf['expirywarn']) &&
26793a7873eSAndreas Gohr                        !$this->msgshown
26893a7873eSAndreas Gohr                    ) {
2695b795a65SPatrick Brown                        $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']);
27093a7873eSAndreas Gohr                        if ($this->canDo('modPass')) {
27193a7873eSAndreas Gohr                            $url = wl($ID, array('do'=> 'profile'));
27293a7873eSAndreas Gohr                            $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
27393a7873eSAndreas Gohr                        }
27493a7873eSAndreas Gohr                        msg($msg);
27593a7873eSAndreas Gohr                        $this->msgshown = true;
27693a7873eSAndreas Gohr                    }
27793a7873eSAndreas Gohr                }
278a4337320SAndreas Gohr            } catch (adLDAPException $e) {
279a4337320SAndreas Gohr                // ignore. should usually not happen
280a4337320SAndreas Gohr            }
2811e52e72aSAndreas Gohr        }
28293a7873eSAndreas Gohr
283f4476bd9SJan Schumann        return $info;
284f4476bd9SJan Schumann    }
285f4476bd9SJan Schumann
286f4476bd9SJan Schumann    /**
287f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
288f4476bd9SJan Schumann     *
289f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
290f4476bd9SJan Schumann     *
291f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
29293a7873eSAndreas Gohr     * @param string $group
29393a7873eSAndreas Gohr     * @return string
294f4476bd9SJan Schumann     */
295a4337320SAndreas Gohr    public function cleanGroup($group)
296a4337320SAndreas Gohr    {
29793a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
29893a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
29993a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
3008cbc5ee8SAndreas Gohr        $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group));
30193a7873eSAndreas Gohr        return $group;
302f4476bd9SJan Schumann    }
303f4476bd9SJan Schumann
304f4476bd9SJan Schumann    /**
305f4476bd9SJan Schumann     * Sanitize user names
30693a7873eSAndreas Gohr     *
30793a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
30893a7873eSAndreas Gohr     *
30993a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
31093a7873eSAndreas Gohr     * @param string $user
31193a7873eSAndreas Gohr     * @return string
312f4476bd9SJan Schumann     */
313a4337320SAndreas Gohr    public function cleanUser($user)
314a4337320SAndreas Gohr    {
31593a7873eSAndreas Gohr        $domain = '';
31693a7873eSAndreas Gohr
31793a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
31893a7873eSAndreas Gohr        list($dom, $user) = explode('\\', $user, 2);
31993a7873eSAndreas Gohr        if (!$user) $user = $dom;
32093a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32193a7873eSAndreas Gohr        list($user, $dom) = explode('@', $user, 2);
32293a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32393a7873eSAndreas Gohr
32493a7873eSAndreas Gohr        // clean up both
3258cbc5ee8SAndreas Gohr        $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain));
3268cbc5ee8SAndreas Gohr        $user   = \dokuwiki\Utf8\PhpString::strtolower(trim($user));
32793a7873eSAndreas Gohr
328916ef7cfSAndreas Gohr        // is this a known, valid domain or do we work without account suffix? if not discard
329916ef7cfSAndreas Gohr        if (!is_array($this->conf[$domain]) && $this->conf['account_suffix'] !== '') {
33093a7873eSAndreas Gohr            $domain = '';
33193a7873eSAndreas Gohr        }
33293a7873eSAndreas Gohr
33393a7873eSAndreas Gohr        // reattach domain
33493a7873eSAndreas Gohr        if ($domain) $user = "$user@$domain";
33593a7873eSAndreas Gohr        return $user;
336f4476bd9SJan Schumann    }
337f4476bd9SJan Schumann
338f4476bd9SJan Schumann    /**
339f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
34093a7873eSAndreas Gohr     *
34193a7873eSAndreas Gohr     * @return bool
342f4476bd9SJan Schumann     */
343a4337320SAndreas Gohr    public function isCaseSensitive()
344a4337320SAndreas Gohr    {
345f4476bd9SJan Schumann        return false;
346f4476bd9SJan Schumann    }
347f4476bd9SJan Schumann
3486fcf992cSMichael Große    /**
3497910cbbbSMichael Große     * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true)
3507910cbbbSMichael Große     *
3516fcf992cSMichael Große     * @param array $filter
3526fcf992cSMichael Große     * @return string
3536fcf992cSMichael Große     */
354a4337320SAndreas Gohr    protected function constructSearchString($filter)
355a4337320SAndreas Gohr    {
35667a31a83SMichael Große        if (!$filter) {
35767a31a83SMichael Große            return '*';
35867a31a83SMichael Große        }
359a4337320SAndreas Gohr        $adldapUtils = new adLDAPUtils($this->initAdLdap(null));
36067a31a83SMichael Große        $result = '*';
36167a31a83SMichael Große        if (isset($filter['name'])) {
36207aec029SMichael Große            $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*';
36367a31a83SMichael Große            unset($filter['name']);
36467a31a83SMichael Große        }
3657910cbbbSMichael Große
36667a31a83SMichael Große        if (isset($filter['user'])) {
36707aec029SMichael Große            $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*';
36867a31a83SMichael Große            unset($filter['user']);
36967a31a83SMichael Große        }
37067a31a83SMichael Große
37167a31a83SMichael Große        if (isset($filter['mail'])) {
37207aec029SMichael Große            $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*';
37367a31a83SMichael Große            unset($filter['mail']);
37467a31a83SMichael Große        }
37567a31a83SMichael Große        return $result;
37667a31a83SMichael Große    }
37767a31a83SMichael Große
378f4476bd9SJan Schumann    /**
3797910cbbbSMichael Große     * Return a count of the number of user which meet $filter criteria
3807910cbbbSMichael Große     *
3817910cbbbSMichael Große     * @param array $filter  $filter array of field/pattern pairs, empty array for no filter
3827910cbbbSMichael Große     * @return int number of users
38325f80763SMichael Große     */
384a4337320SAndreas Gohr    public function getUserCount($filter = array())
385a4337320SAndreas Gohr    {
386a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
3876fcf992cSMichael Große        if (!$adldap) {
388*31667ec6SAndreas Gohr            Logger::debug("authad/auth.php getUserCount(): _adldap not set.");
3896fcf992cSMichael Große            return -1;
3906fcf992cSMichael Große        }
39167a31a83SMichael Große        if ($filter == array()) {
39225f80763SMichael Große            $result = $adldap->user()->all();
39367a31a83SMichael Große        } else {
394a4337320SAndreas Gohr            $searchString = $this->constructSearchString($filter);
39567a31a83SMichael Große            $result = $adldap->user()->all(false, $searchString);
396c52f6cd2SMichael Große            if (isset($filter['grps'])) {
397c52f6cd2SMichael Große                $this->users = array_fill_keys($result, false);
398a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
399c52f6cd2SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
400462e9e37SMichael Große                $usermanager->setLastdisabled(true);
401a4337320SAndreas Gohr                if (!isset($this->grpsusers[$this->filterToString($filter)])) {
402a4337320SAndreas Gohr                    $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize());
403a4337320SAndreas Gohr                } elseif (count($this->grpsusers[$this->filterToString($filter)]) <
40464159a61SAndreas Gohr                    $usermanager->getStart() + 3*$usermanager->getPagesize()
40564159a61SAndreas Gohr                ) {
406a4337320SAndreas Gohr                    $this->fillGroupUserArray(
40764159a61SAndreas Gohr                        $filter,
40864159a61SAndreas Gohr                        $usermanager->getStart() +
40964159a61SAndreas Gohr                        3*$usermanager->getPagesize() -
410a4337320SAndreas Gohr                        count($this->grpsusers[$this->filterToString($filter)])
41164159a61SAndreas Gohr                    );
412c52f6cd2SMichael Große                }
413a4337320SAndreas Gohr                $result = $this->grpsusers[$this->filterToString($filter)];
414462e9e37SMichael Große            } else {
415a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
416462e9e37SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
417462e9e37SMichael Große                $usermanager->setLastdisabled(false);
418c52f6cd2SMichael Große            }
41967a31a83SMichael Große        }
42067a31a83SMichael Große
42125f80763SMichael Große        if (!$result) {
4226fcf992cSMichael Große            return 0;
42325f80763SMichael Große        }
42425f80763SMichael Große        return count($result);
42525f80763SMichael Große    }
42625f80763SMichael Große
4276fcf992cSMichael Große    /**
4286fcf992cSMichael Große     *
4296fcf992cSMichael Große     * create a unique string for each filter used with a group
4306fcf992cSMichael Große     *
4316fcf992cSMichael Große     * @param array $filter
4326fcf992cSMichael Große     * @return string
4336fcf992cSMichael Große     */
434a4337320SAndreas Gohr    protected function filterToString($filter)
435a4337320SAndreas Gohr    {
436c52f6cd2SMichael Große        $result = '';
437c52f6cd2SMichael Große        if (isset($filter['user'])) {
438c52f6cd2SMichael Große            $result .= 'user-' . $filter['user'];
439c52f6cd2SMichael Große        }
440c52f6cd2SMichael Große        if (isset($filter['name'])) {
441c52f6cd2SMichael Große            $result .= 'name-' . $filter['name'];
442c52f6cd2SMichael Große        }
443c52f6cd2SMichael Große        if (isset($filter['mail'])) {
444c52f6cd2SMichael Große            $result .= 'mail-' . $filter['mail'];
445c52f6cd2SMichael Große        }
446c52f6cd2SMichael Große        if (isset($filter['grps'])) {
447c52f6cd2SMichael Große            $result .= 'grps-' . $filter['grps'];
448c52f6cd2SMichael Große        }
449c52f6cd2SMichael Große        return $result;
450c52f6cd2SMichael Große    }
451c52f6cd2SMichael Große
4526fcf992cSMichael Große    /**
4537910cbbbSMichael Große     * Create an array of $numberOfAdds users passing a certain $filter, including belonging
4547910cbbbSMichael Große     * to a certain group and save them to a object-wide array. If the array
4557910cbbbSMichael Große     * already exists try to add $numberOfAdds further users to it.
4567910cbbbSMichael Große     *
4576fcf992cSMichael Große     * @param array $filter
4586fcf992cSMichael Große     * @param int $numberOfAdds additional number of users requested
4596fcf992cSMichael Große     * @return int number of Users actually add to Array
4606fcf992cSMichael Große     */
461a4337320SAndreas Gohr    protected function fillGroupUserArray($filter, $numberOfAdds)
462a4337320SAndreas Gohr    {
463fdd649a2SAndreas Gohr        if (isset($this->grpsusers[$this->filterToString($filter)])) {
464fdd649a2SAndreas Gohr            $actualstart = count($this->grpsusers[$this->filterToString($filter)]);
465fdd649a2SAndreas Gohr        } else {
466fdd649a2SAndreas Gohr            $this->grpsusers[$this->filterToString($filter)] = [];
467fdd649a2SAndreas Gohr            $actualstart = 0;
468fdd649a2SAndreas Gohr        }
469fdd649a2SAndreas Gohr
470c52f6cd2SMichael Große        $i=0;
471c52f6cd2SMichael Große        $count = 0;
472a4337320SAndreas Gohr        $this->constructPattern($filter);
473c52f6cd2SMichael Große        foreach ($this->users as $user => &$info) {
474fdd649a2SAndreas Gohr            if ($i++ < $actualstart) {
475c52f6cd2SMichael Große                continue;
476c52f6cd2SMichael Große            }
477c52f6cd2SMichael Große            if ($info === false) {
478c52f6cd2SMichael Große                $info = $this->getUserData($user);
479c52f6cd2SMichael Große            }
480a4337320SAndreas Gohr            if ($this->filter($user, $info)) {
481a4337320SAndreas Gohr                $this->grpsusers[$this->filterToString($filter)][$user] = $info;
482c52f6cd2SMichael Große                if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break;
483c52f6cd2SMichael Große            }
484c52f6cd2SMichael Große        }
485c52f6cd2SMichael Große        return $count;
486c52f6cd2SMichael Große    }
487c52f6cd2SMichael Große
48825f80763SMichael Große    /**
489f4476bd9SJan Schumann     * Bulk retrieval of user data
490f4476bd9SJan Schumann     *
491f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
492253d4b48SGerrit Uitslag     *
49393a7873eSAndreas Gohr     * @param   int $start index of first user to be returned
49493a7873eSAndreas Gohr     * @param   int $limit max number of users to be returned
49593a7873eSAndreas Gohr     * @param   array $filter array of field/pattern pairs, null for no filter
49693a7873eSAndreas Gohr     * @return array userinfo (refer getUserData for internal userinfo details)
497f4476bd9SJan Schumann     */
498a4337320SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array())
499a4337320SAndreas Gohr    {
500a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
501a4337320SAndreas Gohr        if (!$adldap) return array();
502f4476bd9SJan Schumann
503fdd649a2SAndreas Gohr        //if (!$this->users) {
504f4476bd9SJan Schumann            //get info for given user
505a4337320SAndreas Gohr            $result = $adldap->user()->all(false, $this->constructSearchString($filter));
506f4476bd9SJan Schumann            if (!$result) return array();
507f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
508fdd649a2SAndreas Gohr        //}
509f4476bd9SJan Schumann
510f4476bd9SJan Schumann        $i     = 0;
511f4476bd9SJan Schumann        $count = 0;
512f4476bd9SJan Schumann        $result = array();
513f4476bd9SJan Schumann
514c52f6cd2SMichael Große        if (!isset($filter['grps'])) {
515a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
516462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
517462e9e37SMichael Große            $usermanager->setLastdisabled(false);
518a4337320SAndreas Gohr            $this->constructPattern($filter);
519f4476bd9SJan Schumann            foreach ($this->users as $user => &$info) {
520f4476bd9SJan Schumann                if ($i++ < $start) {
521f4476bd9SJan Schumann                    continue;
522f4476bd9SJan Schumann                }
523f4476bd9SJan Schumann                if ($info === false) {
524f4476bd9SJan Schumann                    $info = $this->getUserData($user);
525f4476bd9SJan Schumann                }
526f4476bd9SJan Schumann                $result[$user] = $info;
5279a2c73e8SAndreas Gohr                if (($limit > 0) && (++$count >= $limit)) break;
528f4476bd9SJan Schumann            }
529c52f6cd2SMichael Große        } else {
530a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
531462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
532462e9e37SMichael Große            $usermanager->setLastdisabled(true);
533a4337320SAndreas Gohr            if (!isset($this->grpsusers[$this->filterToString($filter)]) ||
534a4337320SAndreas Gohr                count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit)
53564159a61SAndreas Gohr            ) {
536fdd649a2SAndreas Gohr                if(!isset($this->grpsusers[$this->filterToString($filter)])) {
537fdd649a2SAndreas Gohr                    $this->grpsusers[$this->filterToString($filter)] = [];
538fdd649a2SAndreas Gohr                }
539fdd649a2SAndreas Gohr
540a4337320SAndreas Gohr                $this->fillGroupUserArray(
54164159a61SAndreas Gohr                    $filter,
542a4337320SAndreas Gohr                    $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1
54364159a61SAndreas Gohr                );
544c52f6cd2SMichael Große            }
545a4337320SAndreas Gohr            if (!$this->grpsusers[$this->filterToString($filter)]) return array();
546a4337320SAndreas Gohr            foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) {
547c52f6cd2SMichael Große                if ($i++ < $start) {
548c52f6cd2SMichael Große                    continue;
549c52f6cd2SMichael Große                }
550c52f6cd2SMichael Große                $result[$user] = $info;
551c52f6cd2SMichael Große                if (($limit > 0) && (++$count >= $limit)) break;
552c52f6cd2SMichael Große            }
553c52f6cd2SMichael Große        }
554f4476bd9SJan Schumann        return $result;
555f4476bd9SJan Schumann    }
556f4476bd9SJan Schumann
557f4476bd9SJan Schumann    /**
558f4476bd9SJan Schumann     * Modify user data
559f4476bd9SJan Schumann     *
56093a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
56193a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
562f4476bd9SJan Schumann     * @return  bool
563f4476bd9SJan Schumann     */
564a4337320SAndreas Gohr    public function modifyUser($user, $changes)
565a4337320SAndreas Gohr    {
566f4476bd9SJan Schumann        $return = true;
567a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
5688f03c311SPatrick Brown        if (!$adldap) {
5698f03c311SPatrick Brown            msg($this->getLang('connectfail'), -1);
5708f03c311SPatrick Brown            return false;
5718f03c311SPatrick Brown        }
572f4476bd9SJan Schumann
573f4476bd9SJan Schumann        // password changing
574f4476bd9SJan Schumann        if (isset($changes['pass'])) {
575f4476bd9SJan Schumann            try {
576a4337320SAndreas Gohr                $return = $adldap->user()->password($this->getUserName($user), $changes['pass']);
577f4476bd9SJan Schumann            } catch (adLDAPException $e) {
57832fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
579f4476bd9SJan Schumann                $return = false;
580f4476bd9SJan Schumann            }
5818f03c311SPatrick Brown            if (!$return) msg($this->getLang('passchangefail'), -1);
582f4476bd9SJan Schumann        }
583f4476bd9SJan Schumann
584f4476bd9SJan Schumann        // changing user data
585f4476bd9SJan Schumann        $adchanges = array();
586f4476bd9SJan Schumann        if (isset($changes['name'])) {
587f4476bd9SJan Schumann            // get first and last name
588f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
589f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
590f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
591f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
592f4476bd9SJan Schumann        }
593f4476bd9SJan Schumann        if (isset($changes['mail'])) {
594f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
595f4476bd9SJan Schumann        }
596f4476bd9SJan Schumann        if (count($adchanges)) {
597f4476bd9SJan Schumann            try {
598a4337320SAndreas Gohr                $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges);
599f4476bd9SJan Schumann            } catch (adLDAPException $e) {
60032fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
601f4476bd9SJan Schumann                $return = false;
602f4476bd9SJan Schumann            }
6038f03c311SPatrick Brown            if (!$return) msg($this->getLang('userchangefail'), -1);
604f4476bd9SJan Schumann        }
605f4476bd9SJan Schumann
606f4476bd9SJan Schumann        return $return;
607f4476bd9SJan Schumann    }
608f4476bd9SJan Schumann
609f4476bd9SJan Schumann    /**
610f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
61193a7873eSAndreas Gohr     *
61293a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
61393a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
61493a7873eSAndreas Gohr     * domain if no current one is available.
61593a7873eSAndreas Gohr     *
61693a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
61793a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
618f4476bd9SJan Schumann     */
619a4337320SAndreas Gohr    protected function initAdLdap($domain)
620a4337320SAndreas Gohr    {
62193a7873eSAndreas Gohr        if (is_null($domain) && is_array($this->opts)) {
62293a7873eSAndreas Gohr            $domain = $this->opts['domain'];
62393a7873eSAndreas Gohr        }
62493a7873eSAndreas Gohr
625a4337320SAndreas Gohr        $this->opts = $this->loadServerConfig((string) $domain);
62693a7873eSAndreas Gohr        if (isset($this->adldap[$domain])) return $this->adldap[$domain];
627f4476bd9SJan Schumann
628f4476bd9SJan Schumann        // connect
629f4476bd9SJan Schumann        try {
63093a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
63193a7873eSAndreas Gohr            return $this->adldap[$domain];
632a4337320SAndreas Gohr        } catch (Exception $e) {
63332fd494aSAndreas Gohr            if ($this->conf['debug']) {
634f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
635f4476bd9SJan Schumann            }
636f4476bd9SJan Schumann            $this->success         = false;
63793a7873eSAndreas Gohr            $this->adldap[$domain] = null;
638f4476bd9SJan Schumann        }
639f4476bd9SJan Schumann        return false;
640f4476bd9SJan Schumann    }
641f4476bd9SJan Schumann
642f4476bd9SJan Schumann    /**
64393a7873eSAndreas Gohr     * Get the domain part from a user
644f4476bd9SJan Schumann     *
645253d4b48SGerrit Uitslag     * @param string $user
64693a7873eSAndreas Gohr     * @return string
647f4476bd9SJan Schumann     */
648a4337320SAndreas Gohr    public function getUserDomain($user)
649a4337320SAndreas Gohr    {
65093a7873eSAndreas Gohr        list(, $domain) = explode('@', $user, 2);
65193a7873eSAndreas Gohr        return $domain;
652f4476bd9SJan Schumann    }
653f4476bd9SJan Schumann
65493a7873eSAndreas Gohr    /**
65593a7873eSAndreas Gohr     * Get the user part from a user
65693a7873eSAndreas Gohr     *
657916ef7cfSAndreas Gohr     * When an account suffix is set, we strip the domain part from the user
658916ef7cfSAndreas Gohr     *
659253d4b48SGerrit Uitslag     * @param string $user
66093a7873eSAndreas Gohr     * @return string
66193a7873eSAndreas Gohr     */
662a4337320SAndreas Gohr    public function getUserName($user)
663a4337320SAndreas Gohr    {
664916ef7cfSAndreas Gohr        if ($this->conf['account_suffix'] !== '') {
665916ef7cfSAndreas Gohr            list($user) = explode('@', $user, 2);
666916ef7cfSAndreas Gohr        }
667916ef7cfSAndreas Gohr        return $user;
66893a7873eSAndreas Gohr    }
66993a7873eSAndreas Gohr
67093a7873eSAndreas Gohr    /**
67193a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
67293a7873eSAndreas Gohr     *
67393a7873eSAndreas Gohr     * @param string $domain current AD domain
67493a7873eSAndreas Gohr     * @return array
67593a7873eSAndreas Gohr     */
676a4337320SAndreas Gohr    protected function loadServerConfig($domain)
677a4337320SAndreas Gohr    {
67893a7873eSAndreas Gohr        // prepare adLDAP standard configuration
67932fd494aSAndreas Gohr        $opts = $this->conf;
68093a7873eSAndreas Gohr
68193a7873eSAndreas Gohr        $opts['domain'] = $domain;
68293a7873eSAndreas Gohr
68393a7873eSAndreas Gohr        // add possible domain specific configuration
68432fd494aSAndreas Gohr        if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) {
68593a7873eSAndreas Gohr            $opts[$key] = $val;
68693a7873eSAndreas Gohr        }
68793a7873eSAndreas Gohr
68893a7873eSAndreas Gohr        // handle multiple AD servers
68993a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
69093a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
69193a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
69293a7873eSAndreas Gohr
6938257d713SAndreas Gohr        // compatibility with old option name
69464159a61SAndreas Gohr        if (empty($opts['admin_username']) && !empty($opts['ad_username'])) {
69564159a61SAndreas Gohr            $opts['admin_username'] = $opts['ad_username'];
69664159a61SAndreas Gohr        }
69764159a61SAndreas Gohr        if (empty($opts['admin_password']) && !empty($opts['ad_password'])) {
69864159a61SAndreas Gohr            $opts['admin_password'] = $opts['ad_password'];
69964159a61SAndreas Gohr        }
700342753d2SAndreas Gohr        $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate
7018257d713SAndreas Gohr
70293a7873eSAndreas Gohr        // we can change the password if SSL is set
70393a7873eSAndreas Gohr        if ($opts['use_ssl'] || $opts['use_tls']) {
70493a7873eSAndreas Gohr            $this->cando['modPass'] = true;
70593a7873eSAndreas Gohr        } else {
70693a7873eSAndreas Gohr            $this->cando['modPass'] = false;
70793a7873eSAndreas Gohr        }
70893a7873eSAndreas Gohr
70912d195abSAndreas Gohr        // adLDAP expects empty user/pass as NULL, we're less strict FS#2781
71012d195abSAndreas Gohr        if (empty($opts['admin_username'])) $opts['admin_username'] = null;
71112d195abSAndreas Gohr        if (empty($opts['admin_password'])) $opts['admin_password'] = null;
71212d195abSAndreas Gohr
71312d195abSAndreas Gohr        // user listing needs admin priviledges
7148257d713SAndreas Gohr        if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) {
71593a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
71693a7873eSAndreas Gohr        } else {
7171b228d28SKlap-in            $this->cando['getUsers'] = false;
71893a7873eSAndreas Gohr        }
71993a7873eSAndreas Gohr
72093a7873eSAndreas Gohr        return $opts;
72193a7873eSAndreas Gohr    }
72293a7873eSAndreas Gohr
72393a7873eSAndreas Gohr    /**
724741b8a48SAndreas Gohr     * Returns a list of configured domains
725741b8a48SAndreas Gohr     *
726741b8a48SAndreas Gohr     * The default domain has an empty string as key
727741b8a48SAndreas Gohr     *
728741b8a48SAndreas Gohr     * @return array associative array(key => domain)
729741b8a48SAndreas Gohr     */
730a4337320SAndreas Gohr    public function getConfiguredDomains()
731a4337320SAndreas Gohr    {
732741b8a48SAndreas Gohr        $domains = array();
733741b8a48SAndreas Gohr        if (empty($this->conf['account_suffix'])) return $domains; // not configured yet
734741b8a48SAndreas Gohr
735741b8a48SAndreas Gohr        // add default domain, using the name from account suffix
736741b8a48SAndreas Gohr        $domains[''] = ltrim($this->conf['account_suffix'], '@');
737741b8a48SAndreas Gohr
738741b8a48SAndreas Gohr        // find additional domains
739741b8a48SAndreas Gohr        foreach ($this->conf as $key => $val) {
740741b8a48SAndreas Gohr            if (is_array($val) && isset($val['account_suffix'])) {
741741b8a48SAndreas Gohr                $domains[$key] = ltrim($val['account_suffix'], '@');
742741b8a48SAndreas Gohr            }
743741b8a48SAndreas Gohr        }
744741b8a48SAndreas Gohr        ksort($domains);
745741b8a48SAndreas Gohr
746741b8a48SAndreas Gohr        return $domains;
747741b8a48SAndreas Gohr    }
748741b8a48SAndreas Gohr
749741b8a48SAndreas Gohr    /**
75093a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
75193a7873eSAndreas Gohr     *
75293a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
75393a7873eSAndreas Gohr     *
75493a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
755253d4b48SGerrit Uitslag     *
75693a7873eSAndreas Gohr     * @param string $user
75793a7873eSAndreas Gohr     * @param array  $info
75893a7873eSAndreas Gohr     * @return bool
75993a7873eSAndreas Gohr     */
760a4337320SAndreas Gohr    protected function filter($user, $info)
761a4337320SAndreas Gohr    {
762a4337320SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
76393a7873eSAndreas Gohr            if ($item == 'user') {
76493a7873eSAndreas Gohr                if (!preg_match($pattern, $user)) return false;
76593a7873eSAndreas Gohr            } elseif ($item == 'grps') {
76693a7873eSAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
76793a7873eSAndreas Gohr            } else {
76893a7873eSAndreas Gohr                if (!preg_match($pattern, $info[$item])) return false;
76993a7873eSAndreas Gohr            }
77093a7873eSAndreas Gohr        }
77193a7873eSAndreas Gohr        return true;
77293a7873eSAndreas Gohr    }
77393a7873eSAndreas Gohr
77493a7873eSAndreas Gohr    /**
77593a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
77693a7873eSAndreas Gohr     *
77793a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
778253d4b48SGerrit Uitslag     *
77993a7873eSAndreas Gohr     * @param array $filter
78093a7873eSAndreas Gohr     */
781a4337320SAndreas Gohr    protected function constructPattern($filter)
782a4337320SAndreas Gohr    {
783a4337320SAndreas Gohr        $this->pattern = array();
784f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
785a4337320SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
786f4476bd9SJan Schumann        }
787f4476bd9SJan Schumann    }
788f4476bd9SJan Schumann}
789