xref: /dokuwiki/lib/plugins/authad/auth.php (revision ab9790ca81397622fcfd58faf56a69b68bd36257)
1f4476bd9SJan Schumann<?php
2*ab9790caSAndreas Gohruse dokuwiki\Utf8\Clean;
3*ab9790caSAndreas Gohruse dokuwiki\Utf8\PhpString;
40489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
531667ec6SAndreas Gohruse dokuwiki\Logger;
631667ec6SAndreas Gohr
7f4476bd9SJan Schumann/**
8f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki
9f4476bd9SJan Schumann *
10f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier
11f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library
12f4476bd9SJan Schumann *
13f4476bd9SJan Schumann * Usage:
14f4476bd9SJan Schumann *   Set DokuWiki's local.protected.php auth setting to read
15f4476bd9SJan Schumann *
16f4476bd9SJan Schumann *   $conf['authtype']       = 'authad';
17f4476bd9SJan Schumann *
1832fd494aSAndreas Gohr *   $conf['plugin']['authad']['account_suffix']     = '@my.domain.org';
1932fd494aSAndreas Gohr *   $conf['plugin']['authad']['base_dn']            = 'DC=my,DC=domain,DC=org';
2032fd494aSAndreas Gohr *   $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
21f4476bd9SJan Schumann *
22f4476bd9SJan Schumann *   //optional:
2332fd494aSAndreas Gohr *   $conf['plugin']['authad']['sso']                = 1;
243002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_username']     = 'root';
253002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_password']     = 'pass';
2632fd494aSAndreas Gohr *   $conf['plugin']['authad']['real_primarygroup']  = 1;
2732fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_ssl']            = 1;
2832fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_tls']            = 1;
2932fd494aSAndreas Gohr *   $conf['plugin']['authad']['debug']              = 1;
3093a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
3132fd494aSAndreas Gohr *   $conf['plugin']['authad']['expirywarn']         = 5;
32f4476bd9SJan Schumann *
33f4476bd9SJan Schumann *   // get additional information to the userinfo array
34f4476bd9SJan Schumann *   // add a list of comma separated ldap contact fields.
35f4476bd9SJan Schumann *   $conf['plugin']['authad']['additional'] = 'field1,field2';
36f4476bd9SJan Schumann *
37f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
38f4476bd9SJan Schumann * @author  James Van Lommel <jamesvl@gmail.com>
39f4476bd9SJan Schumann * @link    http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
40f4476bd9SJan Schumann * @author  Andreas Gohr <andi@splitbrain.org>
41f4476bd9SJan Schumann * @author  Jan Schumann <js@schumann-it.com>
42f4476bd9SJan Schumann */
43a4337320SAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin
44a4337320SAndreas Gohr{
4532fd494aSAndreas Gohr
4693a7873eSAndreas Gohr    /**
4793a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
4893a7873eSAndreas Gohr     */
49*ab9790caSAndreas Gohr    protected $opts = [];
5032fd494aSAndreas Gohr
5193a7873eSAndreas Gohr    /**
5293a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
5393a7873eSAndreas Gohr     */
54*ab9790caSAndreas Gohr    protected $adldap = [];
5593a7873eSAndreas Gohr
5693a7873eSAndreas Gohr    /**
5793a7873eSAndreas Gohr     * @var bool message state
5893a7873eSAndreas Gohr     */
5993a7873eSAndreas Gohr    protected $msgshown = false;
6093a7873eSAndreas Gohr
6193a7873eSAndreas Gohr    /**
6293a7873eSAndreas Gohr     * @var array user listing cache
6393a7873eSAndreas Gohr     */
64*ab9790caSAndreas Gohr    protected $users = [];
6593a7873eSAndreas Gohr
6693a7873eSAndreas Gohr    /**
6793a7873eSAndreas Gohr     * @var array filter patterns for listing users
6893a7873eSAndreas Gohr     */
69*ab9790caSAndreas Gohr    protected $pattern = [];
70f4476bd9SJan Schumann
71*ab9790caSAndreas Gohr    protected $grpsusers = [];
72c52f6cd2SMichael Große
73f4476bd9SJan Schumann    /**
74f4476bd9SJan Schumann     * Constructor
75f4476bd9SJan Schumann     */
76a4337320SAndreas Gohr    public function __construct()
77a4337320SAndreas Gohr    {
7800d58927SMichael Hamann        global $INPUT;
79454d868bSAndreas Gohr        parent::__construct();
80454d868bSAndreas Gohr
81a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php');
82a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php');
83a4337320SAndreas Gohr
8432fd494aSAndreas Gohr        // we load the config early to modify it a bit here
8532fd494aSAndreas Gohr        $this->loadConfig();
86f4476bd9SJan Schumann
87f4476bd9SJan Schumann        // additional information fields
8832fd494aSAndreas Gohr        if (isset($this->conf['additional'])) {
8932fd494aSAndreas Gohr            $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']);
9032fd494aSAndreas Gohr            $this->conf['additional'] = explode(',', $this->conf['additional']);
91*ab9790caSAndreas Gohr        } else $this->conf['additional'] = [];
92f4476bd9SJan Schumann
93f4476bd9SJan Schumann        // ldap extension is needed
94f4476bd9SJan Schumann        if (!function_exists('ldap_connect')) {
9532fd494aSAndreas Gohr            if ($this->conf['debug'])
96f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
97f4476bd9SJan Schumann            $this->success = false;
98f4476bd9SJan Schumann            return;
99f4476bd9SJan Schumann        }
100f4476bd9SJan Schumann
101f4476bd9SJan Schumann        // Prepare SSO
1021d5848a6Sfiwswe        if (!empty($INPUT->server->str('REMOTE_USER'))) {
103d34a2a38SAndreas Gohr            // make sure the right encoding is used
104d34a2a38SAndreas Gohr            if ($this->getConf('sso_charset')) {
1051d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER',
1061d5848a6Sfiwswe                    iconv($this->getConf('sso_charset'), 'UTF-8', $INPUT->server->str('REMOTE_USER')));
107*ab9790caSAndreas Gohr            } elseif (!Clean::isUtf8($INPUT->server->str('REMOTE_USER'))) {
1081d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER', utf8_encode($INPUT->server->str('REMOTE_USER')));
10993a7873eSAndreas Gohr            }
110d34a2a38SAndreas Gohr
111d34a2a38SAndreas Gohr            // trust the incoming user
112d34a2a38SAndreas Gohr            if ($this->conf['sso']) {
1131d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER', $this->cleanUser($INPUT->server->str('REMOTE_USER')));
114f4476bd9SJan Schumann
115f4476bd9SJan Schumann                // we need to simulate a login
116f4476bd9SJan Schumann                if (empty($_COOKIE[DOKU_COOKIE])) {
1171d5848a6Sfiwswe                    $INPUT->set('u', $INPUT->server->str('REMOTE_USER'));
11800d58927SMichael Hamann                    $INPUT->set('p', 'sso_only');
119f4476bd9SJan Schumann                }
120f4476bd9SJan Schumann            }
121d34a2a38SAndreas Gohr        }
122f4476bd9SJan Schumann
12393a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
124bb30445dSMichael Wilmes        $this->cando['modName'] = (bool)$this->conf['update_name'];
125bb30445dSMichael Wilmes        $this->cando['modMail'] = (bool)$this->conf['update_mail'];
12625f80763SMichael Große        $this->cando['getUserCount'] = true;
127f4476bd9SJan Schumann    }
128f4476bd9SJan Schumann
129f4476bd9SJan Schumann    /**
130a154806fSAndreas Gohr     * Load domain config on capability check
131a154806fSAndreas Gohr     *
132a154806fSAndreas Gohr     * @param string $cap
133a154806fSAndreas Gohr     * @return bool
134a154806fSAndreas Gohr     */
135a4337320SAndreas Gohr    public function canDo($cap)
136a4337320SAndreas Gohr    {
1371d5848a6Sfiwswe        global $INPUT;
138a154806fSAndreas Gohr        //capabilities depend on config, which may change depending on domain
1391d5848a6Sfiwswe        $domain = $this->getUserDomain($INPUT->server->str('REMOTE_USER'));
140a4337320SAndreas Gohr        $this->loadServerConfig($domain);
141a154806fSAndreas Gohr        return parent::canDo($cap);
142a154806fSAndreas Gohr    }
143a154806fSAndreas Gohr
144a154806fSAndreas Gohr    /**
145f4476bd9SJan Schumann     * Check user+password [required auth function]
146f4476bd9SJan Schumann     *
147f4476bd9SJan Schumann     * Checks if the given user exists and the given
148f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
149f4476bd9SJan Schumann     * to the LDAP server
150f4476bd9SJan Schumann     *
151f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
15293a7873eSAndreas Gohr     * @param string $user
15393a7873eSAndreas Gohr     * @param string $pass
154f4476bd9SJan Schumann     * @return  bool
155f4476bd9SJan Schumann     */
156a4337320SAndreas Gohr    public function checkPass($user, $pass)
157a4337320SAndreas Gohr    {
1581d5848a6Sfiwswe        global $INPUT;
1591d5848a6Sfiwswe        if ($INPUT->server->str('REMOTE_USER') == $user &&
16032fd494aSAndreas Gohr            $this->conf['sso']
16193a7873eSAndreas Gohr        ) return true;
162f4476bd9SJan Schumann
163a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
16493a7873eSAndreas Gohr        if (!$adldap) return false;
16593a7873eSAndreas Gohr
166a4337320SAndreas Gohr        try {
167a4337320SAndreas Gohr            return $adldap->authenticate($this->getUserName($user), $pass);
168a4337320SAndreas Gohr        } catch (adLDAPException $e) {
169a4337320SAndreas Gohr            // shouldn't really happen
170a4337320SAndreas Gohr            return false;
171a4337320SAndreas Gohr        }
172f4476bd9SJan Schumann    }
173f4476bd9SJan Schumann
174f4476bd9SJan Schumann    /**
175f4476bd9SJan Schumann     * Return user info [required auth function]
176f4476bd9SJan Schumann     *
177f4476bd9SJan Schumann     * Returns info about the given user needs to contain
178f4476bd9SJan Schumann     * at least these fields:
179f4476bd9SJan Schumann     *
180f4476bd9SJan Schumann     * name    string  full name of the user
181f4476bd9SJan Schumann     * mail    string  email address of the user
182f4476bd9SJan Schumann     * grps    array   list of groups the user is in
183f4476bd9SJan Schumann     *
18493a7873eSAndreas Gohr     * This AD specific function returns the following
185f4476bd9SJan Schumann     * addional fields:
186f4476bd9SJan Schumann     *
187f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
18893a7873eSAndreas Gohr     * uid        string    samaccountname
18993a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
19093a7873eSAndreas Gohr     * expires    true      if the password expires
19193a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
19293a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
193f4476bd9SJan Schumann     *
194f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
19593a7873eSAndreas Gohr     * @param string $user
1962046a654SChristopher Smith     * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin
19793a7873eSAndreas Gohr     * @return array
198f4476bd9SJan Schumann     */
199a4337320SAndreas Gohr    public function getUserData($user, $requireGroups = true)
200a4337320SAndreas Gohr    {
201f4476bd9SJan Schumann        global $conf;
20293a7873eSAndreas Gohr        global $lang;
20393a7873eSAndreas Gohr        global $ID;
2041d5848a6Sfiwswe        global $INPUT;
205a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
206*ab9790caSAndreas Gohr        if (!$adldap) return [];
207f4476bd9SJan Schumann
208*ab9790caSAndreas Gohr        if ($user == '') return [];
20993a7873eSAndreas Gohr
210*ab9790caSAndreas Gohr        $fields = ['mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'];
211f4476bd9SJan Schumann
212f4476bd9SJan Schumann        // add additional fields to read
21332fd494aSAndreas Gohr        $fields = array_merge($fields, $this->conf['additional']);
214f4476bd9SJan Schumann        $fields = array_unique($fields);
21514642325SAndreas Gohr        $fields = array_filter($fields);
216f4476bd9SJan Schumann
217f4476bd9SJan Schumann        //get info for given user
218a4337320SAndreas Gohr        $result = $adldap->user()->info($this->getUserName($user), $fields);
21993a7873eSAndreas Gohr        if ($result == false) {
220*ab9790caSAndreas Gohr            return [];
22193a7873eSAndreas Gohr        }
22293a7873eSAndreas Gohr
223f4476bd9SJan Schumann        //general user info
224*ab9790caSAndreas Gohr        $info = [];
225f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
226f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
227f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
228f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
22993a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
230*ab9790caSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10_000_000 - 11_644_473_600;
23193a7873eSAndreas Gohr        //will it expire?
23293a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
233f4476bd9SJan Schumann
234f4476bd9SJan Schumann        // additional information
23532fd494aSAndreas Gohr        foreach ($this->conf['additional'] as $field) {
236f4476bd9SJan Schumann            if (isset($result[0][strtolower($field)])) {
237f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
238f4476bd9SJan Schumann            }
239f4476bd9SJan Schumann        }
240f4476bd9SJan Schumann
241f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
242a4337320SAndreas Gohr        $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']);
243f4476bd9SJan Schumann
244f4476bd9SJan Schumann        if (is_array($info['grps'])) {
245f4476bd9SJan Schumann            foreach ($info['grps'] as $ndx => $group) {
246f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
247f4476bd9SJan Schumann            }
248f4476bd9SJan Schumann        }
249f4476bd9SJan Schumann
250f4476bd9SJan Schumann        // always add the default group to the list of groups
251f4476bd9SJan Schumann        if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
252f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
253f4476bd9SJan Schumann        }
254f4476bd9SJan Schumann
25593a7873eSAndreas Gohr        // add the user's domain to the groups
256a4337320SAndreas Gohr        $domain = $this->getUserDomain($user);
25793a7873eSAndreas Gohr        if ($domain && !in_array("domain-$domain", (array) $info['grps'])) {
25893a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
25993a7873eSAndreas Gohr        }
26093a7873eSAndreas Gohr
26193a7873eSAndreas Gohr        // check expiry time
26232fd494aSAndreas Gohr        if ($info['expires'] && $this->conf['expirywarn']) {
263a4337320SAndreas Gohr            try {
2641e52e72aSAndreas Gohr                $expiry = $adldap->user()->passwordExpiry($user);
2651e52e72aSAndreas Gohr                if (is_array($expiry)) {
2661e52e72aSAndreas Gohr                    $info['expiresat'] = $expiry['expiryts'];
2671e52e72aSAndreas Gohr                    $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60));
26893a7873eSAndreas Gohr
26993a7873eSAndreas Gohr                    // if this is the current user, warn him (once per request only)
2701d5848a6Sfiwswe                    if (($INPUT->server->str('REMOTE_USER') == $user) &&
2711e52e72aSAndreas Gohr                        ($info['expiresin'] <= $this->conf['expirywarn']) &&
27293a7873eSAndreas Gohr                        !$this->msgshown
27393a7873eSAndreas Gohr                    ) {
2745b795a65SPatrick Brown                        $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']);
27593a7873eSAndreas Gohr                        if ($this->canDo('modPass')) {
276*ab9790caSAndreas Gohr                            $url = wl($ID, ['do'=> 'profile']);
27793a7873eSAndreas Gohr                            $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
27893a7873eSAndreas Gohr                        }
27993a7873eSAndreas Gohr                        msg($msg);
28093a7873eSAndreas Gohr                        $this->msgshown = true;
28193a7873eSAndreas Gohr                    }
28293a7873eSAndreas Gohr                }
283a4337320SAndreas Gohr            } catch (adLDAPException $e) {
284a4337320SAndreas Gohr                // ignore. should usually not happen
285a4337320SAndreas Gohr            }
2861e52e72aSAndreas Gohr        }
28793a7873eSAndreas Gohr
288f4476bd9SJan Schumann        return $info;
289f4476bd9SJan Schumann    }
290f4476bd9SJan Schumann
291f4476bd9SJan Schumann    /**
292f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
293f4476bd9SJan Schumann     *
294f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
295f4476bd9SJan Schumann     *
296f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
29793a7873eSAndreas Gohr     * @param string $group
29893a7873eSAndreas Gohr     * @return string
299f4476bd9SJan Schumann     */
300a4337320SAndreas Gohr    public function cleanGroup($group)
301a4337320SAndreas Gohr    {
30293a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
30393a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
30493a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
305*ab9790caSAndreas Gohr        $group = PhpString::strtolower(trim($group));
30693a7873eSAndreas Gohr        return $group;
307f4476bd9SJan Schumann    }
308f4476bd9SJan Schumann
309f4476bd9SJan Schumann    /**
310f4476bd9SJan Schumann     * Sanitize user names
31193a7873eSAndreas Gohr     *
31293a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
31393a7873eSAndreas Gohr     *
31493a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
31593a7873eSAndreas Gohr     * @param string $user
31693a7873eSAndreas Gohr     * @return string
317f4476bd9SJan Schumann     */
318a4337320SAndreas Gohr    public function cleanUser($user)
319a4337320SAndreas Gohr    {
32093a7873eSAndreas Gohr        $domain = '';
32193a7873eSAndreas Gohr
32293a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
323*ab9790caSAndreas Gohr        [$dom, $user] = sexplode('\\', $user, 2, '');
32493a7873eSAndreas Gohr        if (!$user) $user = $dom;
32593a7873eSAndreas Gohr        if ($dom) $domain = $dom;
326*ab9790caSAndreas Gohr        [$user, $dom] = sexplode('@', $user, 2, '');
32793a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32893a7873eSAndreas Gohr
32993a7873eSAndreas Gohr        // clean up both
330*ab9790caSAndreas Gohr        $domain = PhpString::strtolower(trim($domain));
331*ab9790caSAndreas Gohr        $user   = PhpString::strtolower(trim($user));
33293a7873eSAndreas Gohr
333916ef7cfSAndreas Gohr        // is this a known, valid domain or do we work without account suffix? if not discard
3343e2e2c4fSAndreas Gohr        if ((!isset($this->conf[$domain]) || !is_array($this->conf[$domain])) &&
3353e2e2c4fSAndreas Gohr            $this->conf['account_suffix'] !== '') {
33693a7873eSAndreas Gohr            $domain = '';
33793a7873eSAndreas Gohr        }
33893a7873eSAndreas Gohr
33993a7873eSAndreas Gohr        // reattach domain
34093a7873eSAndreas Gohr        if ($domain) $user = "$user@$domain";
34193a7873eSAndreas Gohr        return $user;
342f4476bd9SJan Schumann    }
343f4476bd9SJan Schumann
344f4476bd9SJan Schumann    /**
345f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
34693a7873eSAndreas Gohr     *
34793a7873eSAndreas Gohr     * @return bool
348f4476bd9SJan Schumann     */
349a4337320SAndreas Gohr    public function isCaseSensitive()
350a4337320SAndreas Gohr    {
351f4476bd9SJan Schumann        return false;
352f4476bd9SJan Schumann    }
353f4476bd9SJan Schumann
3546fcf992cSMichael Große    /**
3557910cbbbSMichael Große     * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true)
3567910cbbbSMichael Große     *
3576fcf992cSMichael Große     * @param array $filter
3586fcf992cSMichael Große     * @return string
3596fcf992cSMichael Große     */
360a4337320SAndreas Gohr    protected function constructSearchString($filter)
361a4337320SAndreas Gohr    {
36267a31a83SMichael Große        if (!$filter) {
36367a31a83SMichael Große            return '*';
36467a31a83SMichael Große        }
365a4337320SAndreas Gohr        $adldapUtils = new adLDAPUtils($this->initAdLdap(null));
36667a31a83SMichael Große        $result = '*';
36767a31a83SMichael Große        if (isset($filter['name'])) {
36807aec029SMichael Große            $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*';
36967a31a83SMichael Große            unset($filter['name']);
37067a31a83SMichael Große        }
3717910cbbbSMichael Große
37267a31a83SMichael Große        if (isset($filter['user'])) {
37307aec029SMichael Große            $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*';
37467a31a83SMichael Große            unset($filter['user']);
37567a31a83SMichael Große        }
37667a31a83SMichael Große
37767a31a83SMichael Große        if (isset($filter['mail'])) {
37807aec029SMichael Große            $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*';
37967a31a83SMichael Große            unset($filter['mail']);
38067a31a83SMichael Große        }
38167a31a83SMichael Große        return $result;
38267a31a83SMichael Große    }
38367a31a83SMichael Große
384f4476bd9SJan Schumann    /**
3857910cbbbSMichael Große     * Return a count of the number of user which meet $filter criteria
3867910cbbbSMichael Große     *
3877910cbbbSMichael Große     * @param array $filter  $filter array of field/pattern pairs, empty array for no filter
3887910cbbbSMichael Große     * @return int number of users
38925f80763SMichael Große     */
390*ab9790caSAndreas Gohr    public function getUserCount($filter = [])
391a4337320SAndreas Gohr    {
392a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
3936fcf992cSMichael Große        if (!$adldap) {
39431667ec6SAndreas Gohr            Logger::debug("authad/auth.php getUserCount(): _adldap not set.");
3956fcf992cSMichael Große            return -1;
3966fcf992cSMichael Große        }
397*ab9790caSAndreas Gohr        if ($filter == []) {
39825f80763SMichael Große            $result = $adldap->user()->all();
39967a31a83SMichael Große        } else {
400a4337320SAndreas Gohr            $searchString = $this->constructSearchString($filter);
40167a31a83SMichael Große            $result = $adldap->user()->all(false, $searchString);
402c52f6cd2SMichael Große            if (isset($filter['grps'])) {
403c52f6cd2SMichael Große                $this->users = array_fill_keys($result, false);
404a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
405c52f6cd2SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
406462e9e37SMichael Große                $usermanager->setLastdisabled(true);
407a4337320SAndreas Gohr                if (!isset($this->grpsusers[$this->filterToString($filter)])) {
408a4337320SAndreas Gohr                    $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize());
409a4337320SAndreas Gohr                } elseif (count($this->grpsusers[$this->filterToString($filter)]) <
41064159a61SAndreas Gohr                    $usermanager->getStart() + 3*$usermanager->getPagesize()
41164159a61SAndreas Gohr                ) {
412a4337320SAndreas Gohr                    $this->fillGroupUserArray(
41364159a61SAndreas Gohr                        $filter,
41464159a61SAndreas Gohr                        $usermanager->getStart() +
41564159a61SAndreas Gohr                        3*$usermanager->getPagesize() -
416a4337320SAndreas Gohr                        count($this->grpsusers[$this->filterToString($filter)])
41764159a61SAndreas Gohr                    );
418c52f6cd2SMichael Große                }
419a4337320SAndreas Gohr                $result = $this->grpsusers[$this->filterToString($filter)];
420462e9e37SMichael Große            } else {
421a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
422462e9e37SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
423462e9e37SMichael Große                $usermanager->setLastdisabled(false);
424c52f6cd2SMichael Große            }
42567a31a83SMichael Große        }
42667a31a83SMichael Große
42725f80763SMichael Große        if (!$result) {
4286fcf992cSMichael Große            return 0;
42925f80763SMichael Große        }
43025f80763SMichael Große        return count($result);
43125f80763SMichael Große    }
43225f80763SMichael Große
4336fcf992cSMichael Große    /**
4346fcf992cSMichael Große     *
4356fcf992cSMichael Große     * create a unique string for each filter used with a group
4366fcf992cSMichael Große     *
4376fcf992cSMichael Große     * @param array $filter
4386fcf992cSMichael Große     * @return string
4396fcf992cSMichael Große     */
440a4337320SAndreas Gohr    protected function filterToString($filter)
441a4337320SAndreas Gohr    {
442c52f6cd2SMichael Große        $result = '';
443c52f6cd2SMichael Große        if (isset($filter['user'])) {
444c52f6cd2SMichael Große            $result .= 'user-' . $filter['user'];
445c52f6cd2SMichael Große        }
446c52f6cd2SMichael Große        if (isset($filter['name'])) {
447c52f6cd2SMichael Große            $result .= 'name-' . $filter['name'];
448c52f6cd2SMichael Große        }
449c52f6cd2SMichael Große        if (isset($filter['mail'])) {
450c52f6cd2SMichael Große            $result .= 'mail-' . $filter['mail'];
451c52f6cd2SMichael Große        }
452c52f6cd2SMichael Große        if (isset($filter['grps'])) {
453c52f6cd2SMichael Große            $result .= 'grps-' . $filter['grps'];
454c52f6cd2SMichael Große        }
455c52f6cd2SMichael Große        return $result;
456c52f6cd2SMichael Große    }
457c52f6cd2SMichael Große
4586fcf992cSMichael Große    /**
4597910cbbbSMichael Große     * Create an array of $numberOfAdds users passing a certain $filter, including belonging
4607910cbbbSMichael Große     * to a certain group and save them to a object-wide array. If the array
4617910cbbbSMichael Große     * already exists try to add $numberOfAdds further users to it.
4627910cbbbSMichael Große     *
4636fcf992cSMichael Große     * @param array $filter
4646fcf992cSMichael Große     * @param int $numberOfAdds additional number of users requested
4656fcf992cSMichael Große     * @return int number of Users actually add to Array
4666fcf992cSMichael Große     */
467a4337320SAndreas Gohr    protected function fillGroupUserArray($filter, $numberOfAdds)
468a4337320SAndreas Gohr    {
469fdd649a2SAndreas Gohr        if (isset($this->grpsusers[$this->filterToString($filter)])) {
470fdd649a2SAndreas Gohr            $actualstart = count($this->grpsusers[$this->filterToString($filter)]);
471fdd649a2SAndreas Gohr        } else {
472fdd649a2SAndreas Gohr            $this->grpsusers[$this->filterToString($filter)] = [];
473fdd649a2SAndreas Gohr            $actualstart = 0;
474fdd649a2SAndreas Gohr        }
475fdd649a2SAndreas Gohr
476c52f6cd2SMichael Große        $i=0;
477c52f6cd2SMichael Große        $count = 0;
478a4337320SAndreas Gohr        $this->constructPattern($filter);
479c52f6cd2SMichael Große        foreach ($this->users as $user => &$info) {
480fdd649a2SAndreas Gohr            if ($i++ < $actualstart) {
481c52f6cd2SMichael Große                continue;
482c52f6cd2SMichael Große            }
483c52f6cd2SMichael Große            if ($info === false) {
484c52f6cd2SMichael Große                $info = $this->getUserData($user);
485c52f6cd2SMichael Große            }
486a4337320SAndreas Gohr            if ($this->filter($user, $info)) {
487a4337320SAndreas Gohr                $this->grpsusers[$this->filterToString($filter)][$user] = $info;
488c52f6cd2SMichael Große                if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break;
489c52f6cd2SMichael Große            }
490c52f6cd2SMichael Große        }
491c52f6cd2SMichael Große        return $count;
492c52f6cd2SMichael Große    }
493c52f6cd2SMichael Große
49425f80763SMichael Große    /**
495f4476bd9SJan Schumann     * Bulk retrieval of user data
496f4476bd9SJan Schumann     *
497f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
498253d4b48SGerrit Uitslag     *
49993a7873eSAndreas Gohr     * @param   int $start index of first user to be returned
50093a7873eSAndreas Gohr     * @param   int $limit max number of users to be returned
50193a7873eSAndreas Gohr     * @param   array $filter array of field/pattern pairs, null for no filter
50293a7873eSAndreas Gohr     * @return array userinfo (refer getUserData for internal userinfo details)
503f4476bd9SJan Schumann     */
504*ab9790caSAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = [])
505a4337320SAndreas Gohr    {
506a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
507*ab9790caSAndreas Gohr        if (!$adldap) return [];
508f4476bd9SJan Schumann
509fdd649a2SAndreas Gohr        //if (!$this->users) {
510f4476bd9SJan Schumann            //get info for given user
511a4337320SAndreas Gohr            $result = $adldap->user()->all(false, $this->constructSearchString($filter));
512*ab9790caSAndreas Gohr            if (!$result) return [];
513f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
514fdd649a2SAndreas Gohr        //}
515f4476bd9SJan Schumann
516f4476bd9SJan Schumann        $i     = 0;
517f4476bd9SJan Schumann        $count = 0;
518*ab9790caSAndreas Gohr        $result = [];
519f4476bd9SJan Schumann
520c52f6cd2SMichael Große        if (!isset($filter['grps'])) {
521a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
522462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
523462e9e37SMichael Große            $usermanager->setLastdisabled(false);
524a4337320SAndreas Gohr            $this->constructPattern($filter);
525f4476bd9SJan Schumann            foreach ($this->users as $user => &$info) {
526f4476bd9SJan Schumann                if ($i++ < $start) {
527f4476bd9SJan Schumann                    continue;
528f4476bd9SJan Schumann                }
529f4476bd9SJan Schumann                if ($info === false) {
530f4476bd9SJan Schumann                    $info = $this->getUserData($user);
531f4476bd9SJan Schumann                }
532f4476bd9SJan Schumann                $result[$user] = $info;
5339a2c73e8SAndreas Gohr                if (($limit > 0) && (++$count >= $limit)) break;
534f4476bd9SJan Schumann            }
535c52f6cd2SMichael Große        } else {
536a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
537462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
538462e9e37SMichael Große            $usermanager->setLastdisabled(true);
539a4337320SAndreas Gohr            if (!isset($this->grpsusers[$this->filterToString($filter)]) ||
540a4337320SAndreas Gohr                count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit)
54164159a61SAndreas Gohr            ) {
542fdd649a2SAndreas Gohr                if(!isset($this->grpsusers[$this->filterToString($filter)])) {
543fdd649a2SAndreas Gohr                    $this->grpsusers[$this->filterToString($filter)] = [];
544fdd649a2SAndreas Gohr                }
545fdd649a2SAndreas Gohr
546a4337320SAndreas Gohr                $this->fillGroupUserArray(
54764159a61SAndreas Gohr                    $filter,
548a4337320SAndreas Gohr                    $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1
54964159a61SAndreas Gohr                );
550c52f6cd2SMichael Große            }
551*ab9790caSAndreas Gohr            if (!$this->grpsusers[$this->filterToString($filter)]) return [];
552a4337320SAndreas Gohr            foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) {
553c52f6cd2SMichael Große                if ($i++ < $start) {
554c52f6cd2SMichael Große                    continue;
555c52f6cd2SMichael Große                }
556c52f6cd2SMichael Große                $result[$user] = $info;
557c52f6cd2SMichael Große                if (($limit > 0) && (++$count >= $limit)) break;
558c52f6cd2SMichael Große            }
559c52f6cd2SMichael Große        }
560f4476bd9SJan Schumann        return $result;
561f4476bd9SJan Schumann    }
562f4476bd9SJan Schumann
563f4476bd9SJan Schumann    /**
564f4476bd9SJan Schumann     * Modify user data
565f4476bd9SJan Schumann     *
56693a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
56793a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
568f4476bd9SJan Schumann     * @return  bool
569f4476bd9SJan Schumann     */
570a4337320SAndreas Gohr    public function modifyUser($user, $changes)
571a4337320SAndreas Gohr    {
572f4476bd9SJan Schumann        $return = true;
573a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
5748f03c311SPatrick Brown        if (!$adldap) {
5758f03c311SPatrick Brown            msg($this->getLang('connectfail'), -1);
5768f03c311SPatrick Brown            return false;
5778f03c311SPatrick Brown        }
578f4476bd9SJan Schumann
579f4476bd9SJan Schumann        // password changing
580f4476bd9SJan Schumann        if (isset($changes['pass'])) {
581f4476bd9SJan Schumann            try {
582a4337320SAndreas Gohr                $return = $adldap->user()->password($this->getUserName($user), $changes['pass']);
583f4476bd9SJan Schumann            } catch (adLDAPException $e) {
58432fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
585f4476bd9SJan Schumann                $return = false;
586f4476bd9SJan Schumann            }
5878f03c311SPatrick Brown            if (!$return) msg($this->getLang('passchangefail'), -1);
588f4476bd9SJan Schumann        }
589f4476bd9SJan Schumann
590f4476bd9SJan Schumann        // changing user data
591*ab9790caSAndreas Gohr        $adchanges = [];
592f4476bd9SJan Schumann        if (isset($changes['name'])) {
593f4476bd9SJan Schumann            // get first and last name
594f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
595f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
596*ab9790caSAndreas Gohr            $adchanges['firstname']    = implode(' ', $parts);
597f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
598f4476bd9SJan Schumann        }
599f4476bd9SJan Schumann        if (isset($changes['mail'])) {
600f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
601f4476bd9SJan Schumann        }
602*ab9790caSAndreas Gohr        if ($adchanges !== []) {
603f4476bd9SJan Schumann            try {
604*ab9790caSAndreas Gohr                $return &= $adldap->user()->modify($this->getUserName($user), $adchanges);
605f4476bd9SJan Schumann            } catch (adLDAPException $e) {
60632fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
607f4476bd9SJan Schumann                $return = false;
608f4476bd9SJan Schumann            }
6098f03c311SPatrick Brown            if (!$return) msg($this->getLang('userchangefail'), -1);
610f4476bd9SJan Schumann        }
611f4476bd9SJan Schumann
612f4476bd9SJan Schumann        return $return;
613f4476bd9SJan Schumann    }
614f4476bd9SJan Schumann
615f4476bd9SJan Schumann    /**
616f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
61793a7873eSAndreas Gohr     *
61893a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
61993a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
62093a7873eSAndreas Gohr     * domain if no current one is available.
62193a7873eSAndreas Gohr     *
62293a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
62393a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
624f4476bd9SJan Schumann     */
625a4337320SAndreas Gohr    protected function initAdLdap($domain)
626a4337320SAndreas Gohr    {
62793a7873eSAndreas Gohr        if (is_null($domain) && is_array($this->opts)) {
62893a7873eSAndreas Gohr            $domain = $this->opts['domain'];
62993a7873eSAndreas Gohr        }
63093a7873eSAndreas Gohr
631a4337320SAndreas Gohr        $this->opts = $this->loadServerConfig((string) $domain);
63293a7873eSAndreas Gohr        if (isset($this->adldap[$domain])) return $this->adldap[$domain];
633f4476bd9SJan Schumann
634f4476bd9SJan Schumann        // connect
635f4476bd9SJan Schumann        try {
63693a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
63793a7873eSAndreas Gohr            return $this->adldap[$domain];
638a4337320SAndreas Gohr        } catch (Exception $e) {
63932fd494aSAndreas Gohr            if ($this->conf['debug']) {
640f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
641f4476bd9SJan Schumann            }
642f4476bd9SJan Schumann            $this->success         = false;
64393a7873eSAndreas Gohr            $this->adldap[$domain] = null;
644f4476bd9SJan Schumann        }
645f4476bd9SJan Schumann        return false;
646f4476bd9SJan Schumann    }
647f4476bd9SJan Schumann
648f4476bd9SJan Schumann    /**
64993a7873eSAndreas Gohr     * Get the domain part from a user
650f4476bd9SJan Schumann     *
651253d4b48SGerrit Uitslag     * @param string $user
65293a7873eSAndreas Gohr     * @return string
653f4476bd9SJan Schumann     */
654a4337320SAndreas Gohr    public function getUserDomain($user)
655a4337320SAndreas Gohr    {
656*ab9790caSAndreas Gohr        [, $domain] = sexplode('@', $user, 2, '');
65793a7873eSAndreas Gohr        return $domain;
658f4476bd9SJan Schumann    }
659f4476bd9SJan Schumann
66093a7873eSAndreas Gohr    /**
66193a7873eSAndreas Gohr     * Get the user part from a user
66293a7873eSAndreas Gohr     *
663916ef7cfSAndreas Gohr     * When an account suffix is set, we strip the domain part from the user
664916ef7cfSAndreas Gohr     *
665253d4b48SGerrit Uitslag     * @param string $user
66693a7873eSAndreas Gohr     * @return string
66793a7873eSAndreas Gohr     */
668a4337320SAndreas Gohr    public function getUserName($user)
669a4337320SAndreas Gohr    {
670916ef7cfSAndreas Gohr        if ($this->conf['account_suffix'] !== '') {
671*ab9790caSAndreas Gohr            [$user] = explode('@', $user, 2);
672916ef7cfSAndreas Gohr        }
673916ef7cfSAndreas Gohr        return $user;
67493a7873eSAndreas Gohr    }
67593a7873eSAndreas Gohr
67693a7873eSAndreas Gohr    /**
67793a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
67893a7873eSAndreas Gohr     *
67993a7873eSAndreas Gohr     * @param string $domain current AD domain
68093a7873eSAndreas Gohr     * @return array
68193a7873eSAndreas Gohr     */
682a4337320SAndreas Gohr    protected function loadServerConfig($domain)
683a4337320SAndreas Gohr    {
68493a7873eSAndreas Gohr        // prepare adLDAP standard configuration
68532fd494aSAndreas Gohr        $opts = $this->conf;
68693a7873eSAndreas Gohr
68793a7873eSAndreas Gohr        $opts['domain'] = $domain;
68893a7873eSAndreas Gohr
68993a7873eSAndreas Gohr        // add possible domain specific configuration
69032fd494aSAndreas Gohr        if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) {
69193a7873eSAndreas Gohr            $opts[$key] = $val;
69293a7873eSAndreas Gohr        }
69393a7873eSAndreas Gohr
69493a7873eSAndreas Gohr        // handle multiple AD servers
69593a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
69693a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
69793a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
69893a7873eSAndreas Gohr
6998257d713SAndreas Gohr        // compatibility with old option name
70064159a61SAndreas Gohr        if (empty($opts['admin_username']) && !empty($opts['ad_username'])) {
70164159a61SAndreas Gohr            $opts['admin_username'] = $opts['ad_username'];
70264159a61SAndreas Gohr        }
70364159a61SAndreas Gohr        if (empty($opts['admin_password']) && !empty($opts['ad_password'])) {
70464159a61SAndreas Gohr            $opts['admin_password'] = $opts['ad_password'];
70564159a61SAndreas Gohr        }
706342753d2SAndreas Gohr        $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate
7078257d713SAndreas Gohr
70893a7873eSAndreas Gohr        // we can change the password if SSL is set
709a847f473Spluto00987        if ($opts['update_pass'] && ($opts['use_ssl'] || $opts['use_tls'])) {
71093a7873eSAndreas Gohr            $this->cando['modPass'] = true;
71193a7873eSAndreas Gohr        } else {
71293a7873eSAndreas Gohr            $this->cando['modPass'] = false;
71393a7873eSAndreas Gohr        }
71493a7873eSAndreas Gohr
71512d195abSAndreas Gohr        // adLDAP expects empty user/pass as NULL, we're less strict FS#2781
71612d195abSAndreas Gohr        if (empty($opts['admin_username'])) $opts['admin_username'] = null;
71712d195abSAndreas Gohr        if (empty($opts['admin_password'])) $opts['admin_password'] = null;
71812d195abSAndreas Gohr
71912d195abSAndreas Gohr        // user listing needs admin priviledges
7208257d713SAndreas Gohr        if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) {
72193a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
72293a7873eSAndreas Gohr        } else {
7231b228d28SKlap-in            $this->cando['getUsers'] = false;
72493a7873eSAndreas Gohr        }
72593a7873eSAndreas Gohr
72693a7873eSAndreas Gohr        return $opts;
72793a7873eSAndreas Gohr    }
72893a7873eSAndreas Gohr
72993a7873eSAndreas Gohr    /**
730741b8a48SAndreas Gohr     * Returns a list of configured domains
731741b8a48SAndreas Gohr     *
732741b8a48SAndreas Gohr     * The default domain has an empty string as key
733741b8a48SAndreas Gohr     *
734741b8a48SAndreas Gohr     * @return array associative array(key => domain)
735741b8a48SAndreas Gohr     */
736a4337320SAndreas Gohr    public function getConfiguredDomains()
737a4337320SAndreas Gohr    {
738*ab9790caSAndreas Gohr        $domains = [];
739741b8a48SAndreas Gohr        if (empty($this->conf['account_suffix'])) return $domains; // not configured yet
740741b8a48SAndreas Gohr
741741b8a48SAndreas Gohr        // add default domain, using the name from account suffix
742741b8a48SAndreas Gohr        $domains[''] = ltrim($this->conf['account_suffix'], '@');
743741b8a48SAndreas Gohr
744741b8a48SAndreas Gohr        // find additional domains
745741b8a48SAndreas Gohr        foreach ($this->conf as $key => $val) {
746741b8a48SAndreas Gohr            if (is_array($val) && isset($val['account_suffix'])) {
747741b8a48SAndreas Gohr                $domains[$key] = ltrim($val['account_suffix'], '@');
748741b8a48SAndreas Gohr            }
749741b8a48SAndreas Gohr        }
7500489c64bSMoisés Braga Ribeiro        Sort::ksort($domains);
751741b8a48SAndreas Gohr
752741b8a48SAndreas Gohr        return $domains;
753741b8a48SAndreas Gohr    }
754741b8a48SAndreas Gohr
755741b8a48SAndreas Gohr    /**
75693a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
75793a7873eSAndreas Gohr     *
75893a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
75993a7873eSAndreas Gohr     *
76093a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
761253d4b48SGerrit Uitslag     *
76293a7873eSAndreas Gohr     * @param string $user
76393a7873eSAndreas Gohr     * @param array  $info
76493a7873eSAndreas Gohr     * @return bool
76593a7873eSAndreas Gohr     */
766a4337320SAndreas Gohr    protected function filter($user, $info)
767a4337320SAndreas Gohr    {
768a4337320SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
76993a7873eSAndreas Gohr            if ($item == 'user') {
77093a7873eSAndreas Gohr                if (!preg_match($pattern, $user)) return false;
77193a7873eSAndreas Gohr            } elseif ($item == 'grps') {
77293a7873eSAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
773*ab9790caSAndreas Gohr            } elseif (!preg_match($pattern, $info[$item])) {
774*ab9790caSAndreas Gohr                return false;
77593a7873eSAndreas Gohr            }
77693a7873eSAndreas Gohr        }
77793a7873eSAndreas Gohr        return true;
77893a7873eSAndreas Gohr    }
77993a7873eSAndreas Gohr
78093a7873eSAndreas Gohr    /**
78193a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
78293a7873eSAndreas Gohr     *
78393a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
784253d4b48SGerrit Uitslag     *
78593a7873eSAndreas Gohr     * @param array $filter
78693a7873eSAndreas Gohr     */
787a4337320SAndreas Gohr    protected function constructPattern($filter)
788a4337320SAndreas Gohr    {
789*ab9790caSAndreas Gohr        $this->pattern = [];
790f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
791a4337320SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
792f4476bd9SJan Schumann        }
793f4476bd9SJan Schumann    }
794f4476bd9SJan Schumann}
795