xref: /dokuwiki/lib/plugins/authad/auth.php (revision 1d5848a6aaaaa63311d817f85deca9cf129531fd)
1f4476bd9SJan Schumann<?php
20489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
331667ec6SAndreas Gohruse dokuwiki\Logger;
431667ec6SAndreas 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
100*1d5848a6Sfiwswe        if (!empty($INPUT->server->str('REMOTE_USER'))) {
101d34a2a38SAndreas Gohr            // make sure the right encoding is used
102d34a2a38SAndreas Gohr            if ($this->getConf('sso_charset')) {
103*1d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER',
104*1d5848a6Sfiwswe                    iconv($this->getConf('sso_charset'), 'UTF-8', $INPUT->server->str('REMOTE_USER')));
105*1d5848a6Sfiwswe            } elseif (!\dokuwiki\Utf8\Clean::isUtf8($INPUT->server->str('REMOTE_USER'))) {
106*1d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER', utf8_encode($INPUT->server->str('REMOTE_USER')));
10793a7873eSAndreas Gohr            }
108d34a2a38SAndreas Gohr
109d34a2a38SAndreas Gohr            // trust the incoming user
110d34a2a38SAndreas Gohr            if ($this->conf['sso']) {
111*1d5848a6Sfiwswe                $INPUT->server->set('REMOTE_USER', $this->cleanUser($INPUT->server->str('REMOTE_USER')));
112f4476bd9SJan Schumann
113f4476bd9SJan Schumann                // we need to simulate a login
114f4476bd9SJan Schumann                if (empty($_COOKIE[DOKU_COOKIE])) {
115*1d5848a6Sfiwswe                    $INPUT->set('u', $INPUT->server->str('REMOTE_USER'));
11600d58927SMichael Hamann                    $INPUT->set('p', 'sso_only');
117f4476bd9SJan Schumann                }
118f4476bd9SJan Schumann            }
119d34a2a38SAndreas Gohr        }
120f4476bd9SJan Schumann
12193a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
122bb30445dSMichael Wilmes        $this->cando['modName'] = (bool)$this->conf['update_name'];
123bb30445dSMichael Wilmes        $this->cando['modMail'] = (bool)$this->conf['update_mail'];
12425f80763SMichael Große        $this->cando['getUserCount'] = true;
125f4476bd9SJan Schumann    }
126f4476bd9SJan Schumann
127f4476bd9SJan Schumann    /**
128a154806fSAndreas Gohr     * Load domain config on capability check
129a154806fSAndreas Gohr     *
130a154806fSAndreas Gohr     * @param string $cap
131a154806fSAndreas Gohr     * @return bool
132a154806fSAndreas Gohr     */
133a4337320SAndreas Gohr    public function canDo($cap)
134a4337320SAndreas Gohr    {
135*1d5848a6Sfiwswe        global $INPUT;
136a154806fSAndreas Gohr        //capabilities depend on config, which may change depending on domain
137*1d5848a6Sfiwswe        $domain = $this->getUserDomain($INPUT->server->str('REMOTE_USER'));
138a4337320SAndreas Gohr        $this->loadServerConfig($domain);
139a154806fSAndreas Gohr        return parent::canDo($cap);
140a154806fSAndreas Gohr    }
141a154806fSAndreas Gohr
142a154806fSAndreas Gohr    /**
143f4476bd9SJan Schumann     * Check user+password [required auth function]
144f4476bd9SJan Schumann     *
145f4476bd9SJan Schumann     * Checks if the given user exists and the given
146f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
147f4476bd9SJan Schumann     * to the LDAP server
148f4476bd9SJan Schumann     *
149f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
15093a7873eSAndreas Gohr     * @param string $user
15193a7873eSAndreas Gohr     * @param string $pass
152f4476bd9SJan Schumann     * @return  bool
153f4476bd9SJan Schumann     */
154a4337320SAndreas Gohr    public function checkPass($user, $pass)
155a4337320SAndreas Gohr    {
156*1d5848a6Sfiwswe        global $INPUT;
157*1d5848a6Sfiwswe        if ($INPUT->server->str('REMOTE_USER') == $user &&
15832fd494aSAndreas Gohr            $this->conf['sso']
15993a7873eSAndreas Gohr        ) return true;
160f4476bd9SJan Schumann
161a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
16293a7873eSAndreas Gohr        if (!$adldap) return false;
16393a7873eSAndreas Gohr
164a4337320SAndreas Gohr        try {
165a4337320SAndreas Gohr            return $adldap->authenticate($this->getUserName($user), $pass);
166a4337320SAndreas Gohr        } catch (adLDAPException $e) {
167a4337320SAndreas Gohr            // shouldn't really happen
168a4337320SAndreas Gohr            return false;
169a4337320SAndreas Gohr        }
170f4476bd9SJan Schumann    }
171f4476bd9SJan Schumann
172f4476bd9SJan Schumann    /**
173f4476bd9SJan Schumann     * Return user info [required auth function]
174f4476bd9SJan Schumann     *
175f4476bd9SJan Schumann     * Returns info about the given user needs to contain
176f4476bd9SJan Schumann     * at least these fields:
177f4476bd9SJan Schumann     *
178f4476bd9SJan Schumann     * name    string  full name of the user
179f4476bd9SJan Schumann     * mail    string  email address of the user
180f4476bd9SJan Schumann     * grps    array   list of groups the user is in
181f4476bd9SJan Schumann     *
18293a7873eSAndreas Gohr     * This AD specific function returns the following
183f4476bd9SJan Schumann     * addional fields:
184f4476bd9SJan Schumann     *
185f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
18693a7873eSAndreas Gohr     * uid        string    samaccountname
18793a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
18893a7873eSAndreas Gohr     * expires    true      if the password expires
18993a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
19093a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
191f4476bd9SJan Schumann     *
192f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
19393a7873eSAndreas Gohr     * @param string $user
1942046a654SChristopher Smith     * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin
19593a7873eSAndreas Gohr     * @return array
196f4476bd9SJan Schumann     */
197a4337320SAndreas Gohr    public function getUserData($user, $requireGroups = true)
198a4337320SAndreas Gohr    {
199f4476bd9SJan Schumann        global $conf;
20093a7873eSAndreas Gohr        global $lang;
20193a7873eSAndreas Gohr        global $ID;
202*1d5848a6Sfiwswe        global $INPUT;
203a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
204a4337320SAndreas Gohr        if (!$adldap) return array();
205f4476bd9SJan Schumann
20693a7873eSAndreas Gohr        if ($user == '') return array();
20793a7873eSAndreas Gohr
20893a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
209f4476bd9SJan Schumann
210f4476bd9SJan Schumann        // add additional fields to read
21132fd494aSAndreas Gohr        $fields = array_merge($fields, $this->conf['additional']);
212f4476bd9SJan Schumann        $fields = array_unique($fields);
21314642325SAndreas Gohr        $fields = array_filter($fields);
214f4476bd9SJan Schumann
215f4476bd9SJan Schumann        //get info for given user
216a4337320SAndreas Gohr        $result = $adldap->user()->info($this->getUserName($user), $fields);
21793a7873eSAndreas Gohr        if ($result == false) {
21893a7873eSAndreas Gohr            return array();
21993a7873eSAndreas Gohr        }
22093a7873eSAndreas Gohr
221f4476bd9SJan Schumann        //general user info
22259bc3b48SGerrit Uitslag        $info = array();
223f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
224f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
225f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
226f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
22793a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
22893a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
22993a7873eSAndreas Gohr        //will it expire?
23093a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
231f4476bd9SJan Schumann
232f4476bd9SJan Schumann        // additional information
23332fd494aSAndreas Gohr        foreach ($this->conf['additional'] as $field) {
234f4476bd9SJan Schumann            if (isset($result[0][strtolower($field)])) {
235f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
236f4476bd9SJan Schumann            }
237f4476bd9SJan Schumann        }
238f4476bd9SJan Schumann
239f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
240a4337320SAndreas Gohr        $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']);
241f4476bd9SJan Schumann
242f4476bd9SJan Schumann        if (is_array($info['grps'])) {
243f4476bd9SJan Schumann            foreach ($info['grps'] as $ndx => $group) {
244f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
245f4476bd9SJan Schumann            }
246f4476bd9SJan Schumann        }
247f4476bd9SJan Schumann
248f4476bd9SJan Schumann        // always add the default group to the list of groups
249f4476bd9SJan Schumann        if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
250f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
251f4476bd9SJan Schumann        }
252f4476bd9SJan Schumann
25393a7873eSAndreas Gohr        // add the user's domain to the groups
254a4337320SAndreas Gohr        $domain = $this->getUserDomain($user);
25593a7873eSAndreas Gohr        if ($domain && !in_array("domain-$domain", (array) $info['grps'])) {
25693a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
25793a7873eSAndreas Gohr        }
25893a7873eSAndreas Gohr
25993a7873eSAndreas Gohr        // check expiry time
26032fd494aSAndreas Gohr        if ($info['expires'] && $this->conf['expirywarn']) {
261a4337320SAndreas Gohr            try {
2621e52e72aSAndreas Gohr                $expiry = $adldap->user()->passwordExpiry($user);
2631e52e72aSAndreas Gohr                if (is_array($expiry)) {
2641e52e72aSAndreas Gohr                    $info['expiresat'] = $expiry['expiryts'];
2651e52e72aSAndreas Gohr                    $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60));
26693a7873eSAndreas Gohr
26793a7873eSAndreas Gohr                    // if this is the current user, warn him (once per request only)
268*1d5848a6Sfiwswe                    if (($INPUT->server->str('REMOTE_USER') == $user) &&
2691e52e72aSAndreas Gohr                        ($info['expiresin'] <= $this->conf['expirywarn']) &&
27093a7873eSAndreas Gohr                        !$this->msgshown
27193a7873eSAndreas Gohr                    ) {
2725b795a65SPatrick Brown                        $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']);
27393a7873eSAndreas Gohr                        if ($this->canDo('modPass')) {
27493a7873eSAndreas Gohr                            $url = wl($ID, array('do'=> 'profile'));
27593a7873eSAndreas Gohr                            $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
27693a7873eSAndreas Gohr                        }
27793a7873eSAndreas Gohr                        msg($msg);
27893a7873eSAndreas Gohr                        $this->msgshown = true;
27993a7873eSAndreas Gohr                    }
28093a7873eSAndreas Gohr                }
281a4337320SAndreas Gohr            } catch (adLDAPException $e) {
282a4337320SAndreas Gohr                // ignore. should usually not happen
283a4337320SAndreas Gohr            }
2841e52e72aSAndreas Gohr        }
28593a7873eSAndreas Gohr
286f4476bd9SJan Schumann        return $info;
287f4476bd9SJan Schumann    }
288f4476bd9SJan Schumann
289f4476bd9SJan Schumann    /**
290f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
291f4476bd9SJan Schumann     *
292f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
293f4476bd9SJan Schumann     *
294f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
29593a7873eSAndreas Gohr     * @param string $group
29693a7873eSAndreas Gohr     * @return string
297f4476bd9SJan Schumann     */
298a4337320SAndreas Gohr    public function cleanGroup($group)
299a4337320SAndreas Gohr    {
30093a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
30193a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
30293a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
3038cbc5ee8SAndreas Gohr        $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group));
30493a7873eSAndreas Gohr        return $group;
305f4476bd9SJan Schumann    }
306f4476bd9SJan Schumann
307f4476bd9SJan Schumann    /**
308f4476bd9SJan Schumann     * Sanitize user names
30993a7873eSAndreas Gohr     *
31093a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
31193a7873eSAndreas Gohr     *
31293a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
31393a7873eSAndreas Gohr     * @param string $user
31493a7873eSAndreas Gohr     * @return string
315f4476bd9SJan Schumann     */
316a4337320SAndreas Gohr    public function cleanUser($user)
317a4337320SAndreas Gohr    {
31893a7873eSAndreas Gohr        $domain = '';
31993a7873eSAndreas Gohr
32093a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
3213e2e2c4fSAndreas Gohr        list($dom, $user) = array_pad(explode('\\', $user, 2), 2, '');
32293a7873eSAndreas Gohr        if (!$user) $user = $dom;
32393a7873eSAndreas Gohr        if ($dom) $domain = $dom;
3243e2e2c4fSAndreas Gohr        list($user, $dom) = array_pad(explode('@', $user, 2), 2, '');
32593a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32693a7873eSAndreas Gohr
32793a7873eSAndreas Gohr        // clean up both
3288cbc5ee8SAndreas Gohr        $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain));
3298cbc5ee8SAndreas Gohr        $user   = \dokuwiki\Utf8\PhpString::strtolower(trim($user));
33093a7873eSAndreas Gohr
331916ef7cfSAndreas Gohr        // is this a known, valid domain or do we work without account suffix? if not discard
3323e2e2c4fSAndreas Gohr        if ((!isset($this->conf[$domain]) || !is_array($this->conf[$domain])) &&
3333e2e2c4fSAndreas Gohr            $this->conf['account_suffix'] !== '') {
33493a7873eSAndreas Gohr            $domain = '';
33593a7873eSAndreas Gohr        }
33693a7873eSAndreas Gohr
33793a7873eSAndreas Gohr        // reattach domain
33893a7873eSAndreas Gohr        if ($domain) $user = "$user@$domain";
33993a7873eSAndreas Gohr        return $user;
340f4476bd9SJan Schumann    }
341f4476bd9SJan Schumann
342f4476bd9SJan Schumann    /**
343f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
34493a7873eSAndreas Gohr     *
34593a7873eSAndreas Gohr     * @return bool
346f4476bd9SJan Schumann     */
347a4337320SAndreas Gohr    public function isCaseSensitive()
348a4337320SAndreas Gohr    {
349f4476bd9SJan Schumann        return false;
350f4476bd9SJan Schumann    }
351f4476bd9SJan Schumann
3526fcf992cSMichael Große    /**
3537910cbbbSMichael Große     * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true)
3547910cbbbSMichael Große     *
3556fcf992cSMichael Große     * @param array $filter
3566fcf992cSMichael Große     * @return string
3576fcf992cSMichael Große     */
358a4337320SAndreas Gohr    protected function constructSearchString($filter)
359a4337320SAndreas Gohr    {
36067a31a83SMichael Große        if (!$filter) {
36167a31a83SMichael Große            return '*';
36267a31a83SMichael Große        }
363a4337320SAndreas Gohr        $adldapUtils = new adLDAPUtils($this->initAdLdap(null));
36467a31a83SMichael Große        $result = '*';
36567a31a83SMichael Große        if (isset($filter['name'])) {
36607aec029SMichael Große            $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*';
36767a31a83SMichael Große            unset($filter['name']);
36867a31a83SMichael Große        }
3697910cbbbSMichael Große
37067a31a83SMichael Große        if (isset($filter['user'])) {
37107aec029SMichael Große            $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*';
37267a31a83SMichael Große            unset($filter['user']);
37367a31a83SMichael Große        }
37467a31a83SMichael Große
37567a31a83SMichael Große        if (isset($filter['mail'])) {
37607aec029SMichael Große            $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*';
37767a31a83SMichael Große            unset($filter['mail']);
37867a31a83SMichael Große        }
37967a31a83SMichael Große        return $result;
38067a31a83SMichael Große    }
38167a31a83SMichael Große
382f4476bd9SJan Schumann    /**
3837910cbbbSMichael Große     * Return a count of the number of user which meet $filter criteria
3847910cbbbSMichael Große     *
3857910cbbbSMichael Große     * @param array $filter  $filter array of field/pattern pairs, empty array for no filter
3867910cbbbSMichael Große     * @return int number of users
38725f80763SMichael Große     */
388a4337320SAndreas Gohr    public function getUserCount($filter = array())
389a4337320SAndreas Gohr    {
390a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
3916fcf992cSMichael Große        if (!$adldap) {
39231667ec6SAndreas Gohr            Logger::debug("authad/auth.php getUserCount(): _adldap not set.");
3936fcf992cSMichael Große            return -1;
3946fcf992cSMichael Große        }
39567a31a83SMichael Große        if ($filter == array()) {
39625f80763SMichael Große            $result = $adldap->user()->all();
39767a31a83SMichael Große        } else {
398a4337320SAndreas Gohr            $searchString = $this->constructSearchString($filter);
39967a31a83SMichael Große            $result = $adldap->user()->all(false, $searchString);
400c52f6cd2SMichael Große            if (isset($filter['grps'])) {
401c52f6cd2SMichael Große                $this->users = array_fill_keys($result, false);
402a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
403c52f6cd2SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
404462e9e37SMichael Große                $usermanager->setLastdisabled(true);
405a4337320SAndreas Gohr                if (!isset($this->grpsusers[$this->filterToString($filter)])) {
406a4337320SAndreas Gohr                    $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize());
407a4337320SAndreas Gohr                } elseif (count($this->grpsusers[$this->filterToString($filter)]) <
40864159a61SAndreas Gohr                    $usermanager->getStart() + 3*$usermanager->getPagesize()
40964159a61SAndreas Gohr                ) {
410a4337320SAndreas Gohr                    $this->fillGroupUserArray(
41164159a61SAndreas Gohr                        $filter,
41264159a61SAndreas Gohr                        $usermanager->getStart() +
41364159a61SAndreas Gohr                        3*$usermanager->getPagesize() -
414a4337320SAndreas Gohr                        count($this->grpsusers[$this->filterToString($filter)])
41564159a61SAndreas Gohr                    );
416c52f6cd2SMichael Große                }
417a4337320SAndreas Gohr                $result = $this->grpsusers[$this->filterToString($filter)];
418462e9e37SMichael Große            } else {
419a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
420462e9e37SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
421462e9e37SMichael Große                $usermanager->setLastdisabled(false);
422c52f6cd2SMichael Große            }
42367a31a83SMichael Große        }
42467a31a83SMichael Große
42525f80763SMichael Große        if (!$result) {
4266fcf992cSMichael Große            return 0;
42725f80763SMichael Große        }
42825f80763SMichael Große        return count($result);
42925f80763SMichael Große    }
43025f80763SMichael Große
4316fcf992cSMichael Große    /**
4326fcf992cSMichael Große     *
4336fcf992cSMichael Große     * create a unique string for each filter used with a group
4346fcf992cSMichael Große     *
4356fcf992cSMichael Große     * @param array $filter
4366fcf992cSMichael Große     * @return string
4376fcf992cSMichael Große     */
438a4337320SAndreas Gohr    protected function filterToString($filter)
439a4337320SAndreas Gohr    {
440c52f6cd2SMichael Große        $result = '';
441c52f6cd2SMichael Große        if (isset($filter['user'])) {
442c52f6cd2SMichael Große            $result .= 'user-' . $filter['user'];
443c52f6cd2SMichael Große        }
444c52f6cd2SMichael Große        if (isset($filter['name'])) {
445c52f6cd2SMichael Große            $result .= 'name-' . $filter['name'];
446c52f6cd2SMichael Große        }
447c52f6cd2SMichael Große        if (isset($filter['mail'])) {
448c52f6cd2SMichael Große            $result .= 'mail-' . $filter['mail'];
449c52f6cd2SMichael Große        }
450c52f6cd2SMichael Große        if (isset($filter['grps'])) {
451c52f6cd2SMichael Große            $result .= 'grps-' . $filter['grps'];
452c52f6cd2SMichael Große        }
453c52f6cd2SMichael Große        return $result;
454c52f6cd2SMichael Große    }
455c52f6cd2SMichael Große
4566fcf992cSMichael Große    /**
4577910cbbbSMichael Große     * Create an array of $numberOfAdds users passing a certain $filter, including belonging
4587910cbbbSMichael Große     * to a certain group and save them to a object-wide array. If the array
4597910cbbbSMichael Große     * already exists try to add $numberOfAdds further users to it.
4607910cbbbSMichael Große     *
4616fcf992cSMichael Große     * @param array $filter
4626fcf992cSMichael Große     * @param int $numberOfAdds additional number of users requested
4636fcf992cSMichael Große     * @return int number of Users actually add to Array
4646fcf992cSMichael Große     */
465a4337320SAndreas Gohr    protected function fillGroupUserArray($filter, $numberOfAdds)
466a4337320SAndreas Gohr    {
467fdd649a2SAndreas Gohr        if (isset($this->grpsusers[$this->filterToString($filter)])) {
468fdd649a2SAndreas Gohr            $actualstart = count($this->grpsusers[$this->filterToString($filter)]);
469fdd649a2SAndreas Gohr        } else {
470fdd649a2SAndreas Gohr            $this->grpsusers[$this->filterToString($filter)] = [];
471fdd649a2SAndreas Gohr            $actualstart = 0;
472fdd649a2SAndreas Gohr        }
473fdd649a2SAndreas Gohr
474c52f6cd2SMichael Große        $i=0;
475c52f6cd2SMichael Große        $count = 0;
476a4337320SAndreas Gohr        $this->constructPattern($filter);
477c52f6cd2SMichael Große        foreach ($this->users as $user => &$info) {
478fdd649a2SAndreas Gohr            if ($i++ < $actualstart) {
479c52f6cd2SMichael Große                continue;
480c52f6cd2SMichael Große            }
481c52f6cd2SMichael Große            if ($info === false) {
482c52f6cd2SMichael Große                $info = $this->getUserData($user);
483c52f6cd2SMichael Große            }
484a4337320SAndreas Gohr            if ($this->filter($user, $info)) {
485a4337320SAndreas Gohr                $this->grpsusers[$this->filterToString($filter)][$user] = $info;
486c52f6cd2SMichael Große                if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break;
487c52f6cd2SMichael Große            }
488c52f6cd2SMichael Große        }
489c52f6cd2SMichael Große        return $count;
490c52f6cd2SMichael Große    }
491c52f6cd2SMichael Große
49225f80763SMichael Große    /**
493f4476bd9SJan Schumann     * Bulk retrieval of user data
494f4476bd9SJan Schumann     *
495f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
496253d4b48SGerrit Uitslag     *
49793a7873eSAndreas Gohr     * @param   int $start index of first user to be returned
49893a7873eSAndreas Gohr     * @param   int $limit max number of users to be returned
49993a7873eSAndreas Gohr     * @param   array $filter array of field/pattern pairs, null for no filter
50093a7873eSAndreas Gohr     * @return array userinfo (refer getUserData for internal userinfo details)
501f4476bd9SJan Schumann     */
502a4337320SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array())
503a4337320SAndreas Gohr    {
504a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
505a4337320SAndreas Gohr        if (!$adldap) return array();
506f4476bd9SJan Schumann
507fdd649a2SAndreas Gohr        //if (!$this->users) {
508f4476bd9SJan Schumann            //get info for given user
509a4337320SAndreas Gohr            $result = $adldap->user()->all(false, $this->constructSearchString($filter));
510f4476bd9SJan Schumann            if (!$result) return array();
511f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
512fdd649a2SAndreas Gohr        //}
513f4476bd9SJan Schumann
514f4476bd9SJan Schumann        $i     = 0;
515f4476bd9SJan Schumann        $count = 0;
516f4476bd9SJan Schumann        $result = array();
517f4476bd9SJan Schumann
518c52f6cd2SMichael Große        if (!isset($filter['grps'])) {
519a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
520462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
521462e9e37SMichael Große            $usermanager->setLastdisabled(false);
522a4337320SAndreas Gohr            $this->constructPattern($filter);
523f4476bd9SJan Schumann            foreach ($this->users as $user => &$info) {
524f4476bd9SJan Schumann                if ($i++ < $start) {
525f4476bd9SJan Schumann                    continue;
526f4476bd9SJan Schumann                }
527f4476bd9SJan Schumann                if ($info === false) {
528f4476bd9SJan Schumann                    $info = $this->getUserData($user);
529f4476bd9SJan Schumann                }
530f4476bd9SJan Schumann                $result[$user] = $info;
5319a2c73e8SAndreas Gohr                if (($limit > 0) && (++$count >= $limit)) break;
532f4476bd9SJan Schumann            }
533c52f6cd2SMichael Große        } else {
534a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
535462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
536462e9e37SMichael Große            $usermanager->setLastdisabled(true);
537a4337320SAndreas Gohr            if (!isset($this->grpsusers[$this->filterToString($filter)]) ||
538a4337320SAndreas Gohr                count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit)
53964159a61SAndreas Gohr            ) {
540fdd649a2SAndreas Gohr                if(!isset($this->grpsusers[$this->filterToString($filter)])) {
541fdd649a2SAndreas Gohr                    $this->grpsusers[$this->filterToString($filter)] = [];
542fdd649a2SAndreas Gohr                }
543fdd649a2SAndreas Gohr
544a4337320SAndreas Gohr                $this->fillGroupUserArray(
54564159a61SAndreas Gohr                    $filter,
546a4337320SAndreas Gohr                    $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1
54764159a61SAndreas Gohr                );
548c52f6cd2SMichael Große            }
549a4337320SAndreas Gohr            if (!$this->grpsusers[$this->filterToString($filter)]) return array();
550a4337320SAndreas Gohr            foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) {
551c52f6cd2SMichael Große                if ($i++ < $start) {
552c52f6cd2SMichael Große                    continue;
553c52f6cd2SMichael Große                }
554c52f6cd2SMichael Große                $result[$user] = $info;
555c52f6cd2SMichael Große                if (($limit > 0) && (++$count >= $limit)) break;
556c52f6cd2SMichael Große            }
557c52f6cd2SMichael Große        }
558f4476bd9SJan Schumann        return $result;
559f4476bd9SJan Schumann    }
560f4476bd9SJan Schumann
561f4476bd9SJan Schumann    /**
562f4476bd9SJan Schumann     * Modify user data
563f4476bd9SJan Schumann     *
56493a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
56593a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
566f4476bd9SJan Schumann     * @return  bool
567f4476bd9SJan Schumann     */
568a4337320SAndreas Gohr    public function modifyUser($user, $changes)
569a4337320SAndreas Gohr    {
570f4476bd9SJan Schumann        $return = true;
571a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
5728f03c311SPatrick Brown        if (!$adldap) {
5738f03c311SPatrick Brown            msg($this->getLang('connectfail'), -1);
5748f03c311SPatrick Brown            return false;
5758f03c311SPatrick Brown        }
576f4476bd9SJan Schumann
577f4476bd9SJan Schumann        // password changing
578f4476bd9SJan Schumann        if (isset($changes['pass'])) {
579f4476bd9SJan Schumann            try {
580a4337320SAndreas Gohr                $return = $adldap->user()->password($this->getUserName($user), $changes['pass']);
581f4476bd9SJan Schumann            } catch (adLDAPException $e) {
58232fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
583f4476bd9SJan Schumann                $return = false;
584f4476bd9SJan Schumann            }
5858f03c311SPatrick Brown            if (!$return) msg($this->getLang('passchangefail'), -1);
586f4476bd9SJan Schumann        }
587f4476bd9SJan Schumann
588f4476bd9SJan Schumann        // changing user data
589f4476bd9SJan Schumann        $adchanges = array();
590f4476bd9SJan Schumann        if (isset($changes['name'])) {
591f4476bd9SJan Schumann            // get first and last name
592f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
593f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
594f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
595f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
596f4476bd9SJan Schumann        }
597f4476bd9SJan Schumann        if (isset($changes['mail'])) {
598f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
599f4476bd9SJan Schumann        }
600f4476bd9SJan Schumann        if (count($adchanges)) {
601f4476bd9SJan Schumann            try {
602a4337320SAndreas Gohr                $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges);
603f4476bd9SJan Schumann            } catch (adLDAPException $e) {
60432fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
605f4476bd9SJan Schumann                $return = false;
606f4476bd9SJan Schumann            }
6078f03c311SPatrick Brown            if (!$return) msg($this->getLang('userchangefail'), -1);
608f4476bd9SJan Schumann        }
609f4476bd9SJan Schumann
610f4476bd9SJan Schumann        return $return;
611f4476bd9SJan Schumann    }
612f4476bd9SJan Schumann
613f4476bd9SJan Schumann    /**
614f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
61593a7873eSAndreas Gohr     *
61693a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
61793a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
61893a7873eSAndreas Gohr     * domain if no current one is available.
61993a7873eSAndreas Gohr     *
62093a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
62193a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
622f4476bd9SJan Schumann     */
623a4337320SAndreas Gohr    protected function initAdLdap($domain)
624a4337320SAndreas Gohr    {
62593a7873eSAndreas Gohr        if (is_null($domain) && is_array($this->opts)) {
62693a7873eSAndreas Gohr            $domain = $this->opts['domain'];
62793a7873eSAndreas Gohr        }
62893a7873eSAndreas Gohr
629a4337320SAndreas Gohr        $this->opts = $this->loadServerConfig((string) $domain);
63093a7873eSAndreas Gohr        if (isset($this->adldap[$domain])) return $this->adldap[$domain];
631f4476bd9SJan Schumann
632f4476bd9SJan Schumann        // connect
633f4476bd9SJan Schumann        try {
63493a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
63593a7873eSAndreas Gohr            return $this->adldap[$domain];
636a4337320SAndreas Gohr        } catch (Exception $e) {
63732fd494aSAndreas Gohr            if ($this->conf['debug']) {
638f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
639f4476bd9SJan Schumann            }
640f4476bd9SJan Schumann            $this->success         = false;
64193a7873eSAndreas Gohr            $this->adldap[$domain] = null;
642f4476bd9SJan Schumann        }
643f4476bd9SJan Schumann        return false;
644f4476bd9SJan Schumann    }
645f4476bd9SJan Schumann
646f4476bd9SJan Schumann    /**
64793a7873eSAndreas Gohr     * Get the domain part from a user
648f4476bd9SJan Schumann     *
649253d4b48SGerrit Uitslag     * @param string $user
65093a7873eSAndreas Gohr     * @return string
651f4476bd9SJan Schumann     */
652a4337320SAndreas Gohr    public function getUserDomain($user)
653a4337320SAndreas Gohr    {
6543e2e2c4fSAndreas Gohr        list(, $domain) = array_pad(explode('@', $user, 2), 2, '');
65593a7873eSAndreas Gohr        return $domain;
656f4476bd9SJan Schumann    }
657f4476bd9SJan Schumann
65893a7873eSAndreas Gohr    /**
65993a7873eSAndreas Gohr     * Get the user part from a user
66093a7873eSAndreas Gohr     *
661916ef7cfSAndreas Gohr     * When an account suffix is set, we strip the domain part from the user
662916ef7cfSAndreas Gohr     *
663253d4b48SGerrit Uitslag     * @param string $user
66493a7873eSAndreas Gohr     * @return string
66593a7873eSAndreas Gohr     */
666a4337320SAndreas Gohr    public function getUserName($user)
667a4337320SAndreas Gohr    {
668916ef7cfSAndreas Gohr        if ($this->conf['account_suffix'] !== '') {
669916ef7cfSAndreas Gohr            list($user) = explode('@', $user, 2);
670916ef7cfSAndreas Gohr        }
671916ef7cfSAndreas Gohr        return $user;
67293a7873eSAndreas Gohr    }
67393a7873eSAndreas Gohr
67493a7873eSAndreas Gohr    /**
67593a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
67693a7873eSAndreas Gohr     *
67793a7873eSAndreas Gohr     * @param string $domain current AD domain
67893a7873eSAndreas Gohr     * @return array
67993a7873eSAndreas Gohr     */
680a4337320SAndreas Gohr    protected function loadServerConfig($domain)
681a4337320SAndreas Gohr    {
68293a7873eSAndreas Gohr        // prepare adLDAP standard configuration
68332fd494aSAndreas Gohr        $opts = $this->conf;
68493a7873eSAndreas Gohr
68593a7873eSAndreas Gohr        $opts['domain'] = $domain;
68693a7873eSAndreas Gohr
68793a7873eSAndreas Gohr        // add possible domain specific configuration
68832fd494aSAndreas Gohr        if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) {
68993a7873eSAndreas Gohr            $opts[$key] = $val;
69093a7873eSAndreas Gohr        }
69193a7873eSAndreas Gohr
69293a7873eSAndreas Gohr        // handle multiple AD servers
69393a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
69493a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
69593a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
69693a7873eSAndreas Gohr
6978257d713SAndreas Gohr        // compatibility with old option name
69864159a61SAndreas Gohr        if (empty($opts['admin_username']) && !empty($opts['ad_username'])) {
69964159a61SAndreas Gohr            $opts['admin_username'] = $opts['ad_username'];
70064159a61SAndreas Gohr        }
70164159a61SAndreas Gohr        if (empty($opts['admin_password']) && !empty($opts['ad_password'])) {
70264159a61SAndreas Gohr            $opts['admin_password'] = $opts['ad_password'];
70364159a61SAndreas Gohr        }
704342753d2SAndreas Gohr        $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate
7058257d713SAndreas Gohr
70693a7873eSAndreas Gohr        // we can change the password if SSL is set
707a847f473Spluto00987        if ($opts['update_pass'] && ($opts['use_ssl'] || $opts['use_tls'])) {
70893a7873eSAndreas Gohr            $this->cando['modPass'] = true;
70993a7873eSAndreas Gohr        } else {
71093a7873eSAndreas Gohr            $this->cando['modPass'] = false;
71193a7873eSAndreas Gohr        }
71293a7873eSAndreas Gohr
71312d195abSAndreas Gohr        // adLDAP expects empty user/pass as NULL, we're less strict FS#2781
71412d195abSAndreas Gohr        if (empty($opts['admin_username'])) $opts['admin_username'] = null;
71512d195abSAndreas Gohr        if (empty($opts['admin_password'])) $opts['admin_password'] = null;
71612d195abSAndreas Gohr
71712d195abSAndreas Gohr        // user listing needs admin priviledges
7188257d713SAndreas Gohr        if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) {
71993a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
72093a7873eSAndreas Gohr        } else {
7211b228d28SKlap-in            $this->cando['getUsers'] = false;
72293a7873eSAndreas Gohr        }
72393a7873eSAndreas Gohr
72493a7873eSAndreas Gohr        return $opts;
72593a7873eSAndreas Gohr    }
72693a7873eSAndreas Gohr
72793a7873eSAndreas Gohr    /**
728741b8a48SAndreas Gohr     * Returns a list of configured domains
729741b8a48SAndreas Gohr     *
730741b8a48SAndreas Gohr     * The default domain has an empty string as key
731741b8a48SAndreas Gohr     *
732741b8a48SAndreas Gohr     * @return array associative array(key => domain)
733741b8a48SAndreas Gohr     */
734a4337320SAndreas Gohr    public function getConfiguredDomains()
735a4337320SAndreas Gohr    {
736741b8a48SAndreas Gohr        $domains = array();
737741b8a48SAndreas Gohr        if (empty($this->conf['account_suffix'])) return $domains; // not configured yet
738741b8a48SAndreas Gohr
739741b8a48SAndreas Gohr        // add default domain, using the name from account suffix
740741b8a48SAndreas Gohr        $domains[''] = ltrim($this->conf['account_suffix'], '@');
741741b8a48SAndreas Gohr
742741b8a48SAndreas Gohr        // find additional domains
743741b8a48SAndreas Gohr        foreach ($this->conf as $key => $val) {
744741b8a48SAndreas Gohr            if (is_array($val) && isset($val['account_suffix'])) {
745741b8a48SAndreas Gohr                $domains[$key] = ltrim($val['account_suffix'], '@');
746741b8a48SAndreas Gohr            }
747741b8a48SAndreas Gohr        }
7480489c64bSMoisés Braga Ribeiro        Sort::ksort($domains);
749741b8a48SAndreas Gohr
750741b8a48SAndreas Gohr        return $domains;
751741b8a48SAndreas Gohr    }
752741b8a48SAndreas Gohr
753741b8a48SAndreas Gohr    /**
75493a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
75593a7873eSAndreas Gohr     *
75693a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
75793a7873eSAndreas Gohr     *
75893a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
759253d4b48SGerrit Uitslag     *
76093a7873eSAndreas Gohr     * @param string $user
76193a7873eSAndreas Gohr     * @param array  $info
76293a7873eSAndreas Gohr     * @return bool
76393a7873eSAndreas Gohr     */
764a4337320SAndreas Gohr    protected function filter($user, $info)
765a4337320SAndreas Gohr    {
766a4337320SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
76793a7873eSAndreas Gohr            if ($item == 'user') {
76893a7873eSAndreas Gohr                if (!preg_match($pattern, $user)) return false;
76993a7873eSAndreas Gohr            } elseif ($item == 'grps') {
77093a7873eSAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
77193a7873eSAndreas Gohr            } else {
77293a7873eSAndreas Gohr                if (!preg_match($pattern, $info[$item])) return false;
77393a7873eSAndreas Gohr            }
77493a7873eSAndreas Gohr        }
77593a7873eSAndreas Gohr        return true;
77693a7873eSAndreas Gohr    }
77793a7873eSAndreas Gohr
77893a7873eSAndreas Gohr    /**
77993a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
78093a7873eSAndreas Gohr     *
78193a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
782253d4b48SGerrit Uitslag     *
78393a7873eSAndreas Gohr     * @param array $filter
78493a7873eSAndreas Gohr     */
785a4337320SAndreas Gohr    protected function constructPattern($filter)
786a4337320SAndreas Gohr    {
787a4337320SAndreas Gohr        $this->pattern = array();
788f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
789a4337320SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
790f4476bd9SJan Schumann        }
791f4476bd9SJan Schumann    }
792f4476bd9SJan Schumann}
793