xref: /dokuwiki/lib/plugins/authad/auth.php (revision 0489c64b7de1b71fdd124114dd18525156f26327)
1f4476bd9SJan Schumann<?php
2*0489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
3f4476bd9SJan Schumann
4f4476bd9SJan Schumann/**
5f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki
6f4476bd9SJan Schumann *
7f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier
8f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library
9f4476bd9SJan Schumann *
10f4476bd9SJan Schumann * Usage:
11f4476bd9SJan Schumann *   Set DokuWiki's local.protected.php auth setting to read
12f4476bd9SJan Schumann *
13f4476bd9SJan Schumann *   $conf['authtype']       = 'authad';
14f4476bd9SJan Schumann *
1532fd494aSAndreas Gohr *   $conf['plugin']['authad']['account_suffix']     = '@my.domain.org';
1632fd494aSAndreas Gohr *   $conf['plugin']['authad']['base_dn']            = 'DC=my,DC=domain,DC=org';
1732fd494aSAndreas Gohr *   $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
18f4476bd9SJan Schumann *
19f4476bd9SJan Schumann *   //optional:
2032fd494aSAndreas Gohr *   $conf['plugin']['authad']['sso']                = 1;
213002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_username']     = 'root';
223002d731SAndreas Gohr *   $conf['plugin']['authad']['admin_password']     = 'pass';
2332fd494aSAndreas Gohr *   $conf['plugin']['authad']['real_primarygroup']  = 1;
2432fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_ssl']            = 1;
2532fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_tls']            = 1;
2632fd494aSAndreas Gohr *   $conf['plugin']['authad']['debug']              = 1;
2793a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
2832fd494aSAndreas Gohr *   $conf['plugin']['authad']['expirywarn']         = 5;
29f4476bd9SJan Schumann *
30f4476bd9SJan Schumann *   // get additional information to the userinfo array
31f4476bd9SJan Schumann *   // add a list of comma separated ldap contact fields.
32f4476bd9SJan Schumann *   $conf['plugin']['authad']['additional'] = 'field1,field2';
33f4476bd9SJan Schumann *
34f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
35f4476bd9SJan Schumann * @author  James Van Lommel <jamesvl@gmail.com>
36f4476bd9SJan Schumann * @link    http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
37f4476bd9SJan Schumann * @author  Andreas Gohr <andi@splitbrain.org>
38f4476bd9SJan Schumann * @author  Jan Schumann <js@schumann-it.com>
39f4476bd9SJan Schumann */
40a4337320SAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin
41a4337320SAndreas Gohr{
4232fd494aSAndreas Gohr
4393a7873eSAndreas Gohr    /**
4493a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
4593a7873eSAndreas Gohr     */
4693a7873eSAndreas Gohr    protected $opts = array();
4732fd494aSAndreas Gohr
4893a7873eSAndreas Gohr    /**
4993a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
5093a7873eSAndreas Gohr     */
5193a7873eSAndreas Gohr    protected $adldap = array();
5293a7873eSAndreas Gohr
5393a7873eSAndreas Gohr    /**
5493a7873eSAndreas Gohr     * @var bool message state
5593a7873eSAndreas Gohr     */
5693a7873eSAndreas Gohr    protected $msgshown = false;
5793a7873eSAndreas Gohr
5893a7873eSAndreas Gohr    /**
5993a7873eSAndreas Gohr     * @var array user listing cache
6093a7873eSAndreas Gohr     */
6193a7873eSAndreas Gohr    protected $users = array();
6293a7873eSAndreas Gohr
6393a7873eSAndreas Gohr    /**
6493a7873eSAndreas Gohr     * @var array filter patterns for listing users
6593a7873eSAndreas Gohr     */
66a4337320SAndreas Gohr    protected $pattern = array();
67f4476bd9SJan Schumann
68a4337320SAndreas Gohr    protected $actualstart = 0;
69c52f6cd2SMichael Große
70a4337320SAndreas Gohr    protected $grpsusers = array();
71c52f6cd2SMichael Große
72f4476bd9SJan Schumann    /**
73f4476bd9SJan Schumann     * Constructor
74f4476bd9SJan Schumann     */
75a4337320SAndreas Gohr    public function __construct()
76a4337320SAndreas Gohr    {
7700d58927SMichael Hamann        global $INPUT;
78454d868bSAndreas Gohr        parent::__construct();
79454d868bSAndreas Gohr
80a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php');
81a4337320SAndreas Gohr        require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php');
82a4337320SAndreas Gohr
8332fd494aSAndreas Gohr        // we load the config early to modify it a bit here
8432fd494aSAndreas Gohr        $this->loadConfig();
85f4476bd9SJan Schumann
86f4476bd9SJan Schumann        // additional information fields
8732fd494aSAndreas Gohr        if (isset($this->conf['additional'])) {
8832fd494aSAndreas Gohr            $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']);
8932fd494aSAndreas Gohr            $this->conf['additional'] = explode(',', $this->conf['additional']);
9032fd494aSAndreas Gohr        } else $this->conf['additional'] = array();
91f4476bd9SJan Schumann
92f4476bd9SJan Schumann        // ldap extension is needed
93f4476bd9SJan Schumann        if (!function_exists('ldap_connect')) {
9432fd494aSAndreas Gohr            if ($this->conf['debug'])
95f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
96f4476bd9SJan Schumann            $this->success = false;
97f4476bd9SJan Schumann            return;
98f4476bd9SJan Schumann        }
99f4476bd9SJan Schumann
100f4476bd9SJan Schumann        // Prepare SSO
101d34a2a38SAndreas Gohr        if (!empty($_SERVER['REMOTE_USER'])) {
102d34a2a38SAndreas Gohr            // make sure the right encoding is used
103d34a2a38SAndreas Gohr            if ($this->getConf('sso_charset')) {
104d34a2a38SAndreas Gohr                $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']);
1058cbc5ee8SAndreas Gohr            } elseif (!\dokuwiki\Utf8\Clean::isUtf8($_SERVER['REMOTE_USER'])) {
10693a7873eSAndreas Gohr                $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
10793a7873eSAndreas Gohr            }
108d34a2a38SAndreas Gohr
109d34a2a38SAndreas Gohr            // trust the incoming user
110d34a2a38SAndreas Gohr            if ($this->conf['sso']) {
11193a7873eSAndreas Gohr                $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
112f4476bd9SJan Schumann
113f4476bd9SJan Schumann                // we need to simulate a login
114f4476bd9SJan Schumann                if (empty($_COOKIE[DOKU_COOKIE])) {
11500d58927SMichael Hamann                    $INPUT->set('u', $_SERVER['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    {
135a154806fSAndreas Gohr        //capabilities depend on config, which may change depending on domain
136a4337320SAndreas Gohr        $domain = $this->getUserDomain($_SERVER['REMOTE_USER']);
137a4337320SAndreas Gohr        $this->loadServerConfig($domain);
138a154806fSAndreas Gohr        return parent::canDo($cap);
139a154806fSAndreas Gohr    }
140a154806fSAndreas Gohr
141a154806fSAndreas Gohr    /**
142f4476bd9SJan Schumann     * Check user+password [required auth function]
143f4476bd9SJan Schumann     *
144f4476bd9SJan Schumann     * Checks if the given user exists and the given
145f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
146f4476bd9SJan Schumann     * to the LDAP server
147f4476bd9SJan Schumann     *
148f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
14993a7873eSAndreas Gohr     * @param string $user
15093a7873eSAndreas Gohr     * @param string $pass
151f4476bd9SJan Schumann     * @return  bool
152f4476bd9SJan Schumann     */
153a4337320SAndreas Gohr    public function checkPass($user, $pass)
154a4337320SAndreas Gohr    {
155f4476bd9SJan Schumann        if ($_SERVER['REMOTE_USER'] &&
156f4476bd9SJan Schumann            $_SERVER['REMOTE_USER'] == $user &&
15732fd494aSAndreas Gohr            $this->conf['sso']
15893a7873eSAndreas Gohr        ) return true;
159f4476bd9SJan Schumann
160a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
16193a7873eSAndreas Gohr        if (!$adldap) return false;
16293a7873eSAndreas Gohr
163a4337320SAndreas Gohr        try {
164a4337320SAndreas Gohr            return $adldap->authenticate($this->getUserName($user), $pass);
165a4337320SAndreas Gohr        } catch (adLDAPException $e) {
166a4337320SAndreas Gohr            // shouldn't really happen
167a4337320SAndreas Gohr            return false;
168a4337320SAndreas Gohr        }
169f4476bd9SJan Schumann    }
170f4476bd9SJan Schumann
171f4476bd9SJan Schumann    /**
172f4476bd9SJan Schumann     * Return user info [required auth function]
173f4476bd9SJan Schumann     *
174f4476bd9SJan Schumann     * Returns info about the given user needs to contain
175f4476bd9SJan Schumann     * at least these fields:
176f4476bd9SJan Schumann     *
177f4476bd9SJan Schumann     * name    string  full name of the user
178f4476bd9SJan Schumann     * mail    string  email address of the user
179f4476bd9SJan Schumann     * grps    array   list of groups the user is in
180f4476bd9SJan Schumann     *
18193a7873eSAndreas Gohr     * This AD specific function returns the following
182f4476bd9SJan Schumann     * addional fields:
183f4476bd9SJan Schumann     *
184f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
18593a7873eSAndreas Gohr     * uid        string    samaccountname
18693a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
18793a7873eSAndreas Gohr     * expires    true      if the password expires
18893a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
18993a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
190f4476bd9SJan Schumann     *
191f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
19293a7873eSAndreas Gohr     * @param string $user
1932046a654SChristopher Smith     * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin
19493a7873eSAndreas Gohr     * @return array
195f4476bd9SJan Schumann     */
196a4337320SAndreas Gohr    public function getUserData($user, $requireGroups = true)
197a4337320SAndreas Gohr    {
198f4476bd9SJan Schumann        global $conf;
19993a7873eSAndreas Gohr        global $lang;
20093a7873eSAndreas Gohr        global $ID;
201a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
202a4337320SAndreas Gohr        if (!$adldap) return array();
203f4476bd9SJan Schumann
20493a7873eSAndreas Gohr        if ($user == '') return array();
20593a7873eSAndreas Gohr
20693a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
207f4476bd9SJan Schumann
208f4476bd9SJan Schumann        // add additional fields to read
20932fd494aSAndreas Gohr        $fields = array_merge($fields, $this->conf['additional']);
210f4476bd9SJan Schumann        $fields = array_unique($fields);
21114642325SAndreas Gohr        $fields = array_filter($fields);
212f4476bd9SJan Schumann
213f4476bd9SJan Schumann        //get info for given user
214a4337320SAndreas Gohr        $result = $adldap->user()->info($this->getUserName($user), $fields);
21593a7873eSAndreas Gohr        if ($result == false) {
21693a7873eSAndreas Gohr            return array();
21793a7873eSAndreas Gohr        }
21893a7873eSAndreas Gohr
219f4476bd9SJan Schumann        //general user info
22059bc3b48SGerrit Uitslag        $info = array();
221f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
222f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
223f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
224f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
22593a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
22693a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
22793a7873eSAndreas Gohr        //will it expire?
22893a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
229f4476bd9SJan Schumann
230f4476bd9SJan Schumann        // additional information
23132fd494aSAndreas Gohr        foreach ($this->conf['additional'] as $field) {
232f4476bd9SJan Schumann            if (isset($result[0][strtolower($field)])) {
233f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
234f4476bd9SJan Schumann            }
235f4476bd9SJan Schumann        }
236f4476bd9SJan Schumann
237f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
238a4337320SAndreas Gohr        $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']);
239f4476bd9SJan Schumann
240f4476bd9SJan Schumann        if (is_array($info['grps'])) {
241f4476bd9SJan Schumann            foreach ($info['grps'] as $ndx => $group) {
242f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
243f4476bd9SJan Schumann            }
244f4476bd9SJan Schumann        }
245f4476bd9SJan Schumann
246f4476bd9SJan Schumann        // always add the default group to the list of groups
247f4476bd9SJan Schumann        if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
248f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
249f4476bd9SJan Schumann        }
250f4476bd9SJan Schumann
25193a7873eSAndreas Gohr        // add the user's domain to the groups
252a4337320SAndreas Gohr        $domain = $this->getUserDomain($user);
25393a7873eSAndreas Gohr        if ($domain && !in_array("domain-$domain", (array) $info['grps'])) {
25493a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
25593a7873eSAndreas Gohr        }
25693a7873eSAndreas Gohr
25793a7873eSAndreas Gohr        // check expiry time
25832fd494aSAndreas Gohr        if ($info['expires'] && $this->conf['expirywarn']) {
259a4337320SAndreas Gohr            try {
2601e52e72aSAndreas Gohr                $expiry = $adldap->user()->passwordExpiry($user);
2611e52e72aSAndreas Gohr                if (is_array($expiry)) {
2621e52e72aSAndreas Gohr                    $info['expiresat'] = $expiry['expiryts'];
2631e52e72aSAndreas Gohr                    $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60));
26493a7873eSAndreas Gohr
26593a7873eSAndreas Gohr                    // if this is the current user, warn him (once per request only)
26693a7873eSAndreas Gohr                    if (($_SERVER['REMOTE_USER'] == $user) &&
2671e52e72aSAndreas Gohr                        ($info['expiresin'] <= $this->conf['expirywarn']) &&
26893a7873eSAndreas Gohr                        !$this->msgshown
26993a7873eSAndreas Gohr                    ) {
2705b795a65SPatrick Brown                        $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']);
27193a7873eSAndreas Gohr                        if ($this->canDo('modPass')) {
27293a7873eSAndreas Gohr                            $url = wl($ID, array('do'=> 'profile'));
27393a7873eSAndreas Gohr                            $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
27493a7873eSAndreas Gohr                        }
27593a7873eSAndreas Gohr                        msg($msg);
27693a7873eSAndreas Gohr                        $this->msgshown = true;
27793a7873eSAndreas Gohr                    }
27893a7873eSAndreas Gohr                }
279a4337320SAndreas Gohr            } catch (adLDAPException $e) {
280a4337320SAndreas Gohr                // ignore. should usually not happen
281a4337320SAndreas Gohr            }
2821e52e72aSAndreas Gohr        }
28393a7873eSAndreas Gohr
284f4476bd9SJan Schumann        return $info;
285f4476bd9SJan Schumann    }
286f4476bd9SJan Schumann
287f4476bd9SJan Schumann    /**
288f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
289f4476bd9SJan Schumann     *
290f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
291f4476bd9SJan Schumann     *
292f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
29393a7873eSAndreas Gohr     * @param string $group
29493a7873eSAndreas Gohr     * @return string
295f4476bd9SJan Schumann     */
296a4337320SAndreas Gohr    public function cleanGroup($group)
297a4337320SAndreas Gohr    {
29893a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
29993a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
30093a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
3018cbc5ee8SAndreas Gohr        $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group));
30293a7873eSAndreas Gohr        return $group;
303f4476bd9SJan Schumann    }
304f4476bd9SJan Schumann
305f4476bd9SJan Schumann    /**
306f4476bd9SJan Schumann     * Sanitize user names
30793a7873eSAndreas Gohr     *
30893a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
30993a7873eSAndreas Gohr     *
31093a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
31193a7873eSAndreas Gohr     * @param string $user
31293a7873eSAndreas Gohr     * @return string
313f4476bd9SJan Schumann     */
314a4337320SAndreas Gohr    public function cleanUser($user)
315a4337320SAndreas Gohr    {
31693a7873eSAndreas Gohr        $domain = '';
31793a7873eSAndreas Gohr
31893a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
31993a7873eSAndreas Gohr        list($dom, $user) = explode('\\', $user, 2);
32093a7873eSAndreas Gohr        if (!$user) $user = $dom;
32193a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32293a7873eSAndreas Gohr        list($user, $dom) = explode('@', $user, 2);
32393a7873eSAndreas Gohr        if ($dom) $domain = $dom;
32493a7873eSAndreas Gohr
32593a7873eSAndreas Gohr        // clean up both
3268cbc5ee8SAndreas Gohr        $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain));
3278cbc5ee8SAndreas Gohr        $user   = \dokuwiki\Utf8\PhpString::strtolower(trim($user));
32893a7873eSAndreas Gohr
329916ef7cfSAndreas Gohr        // is this a known, valid domain or do we work without account suffix? if not discard
330916ef7cfSAndreas Gohr        if (!is_array($this->conf[$domain]) && $this->conf['account_suffix'] !== '') {
33193a7873eSAndreas Gohr            $domain = '';
33293a7873eSAndreas Gohr        }
33393a7873eSAndreas Gohr
33493a7873eSAndreas Gohr        // reattach domain
33593a7873eSAndreas Gohr        if ($domain) $user = "$user@$domain";
33693a7873eSAndreas Gohr        return $user;
337f4476bd9SJan Schumann    }
338f4476bd9SJan Schumann
339f4476bd9SJan Schumann    /**
340f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
34193a7873eSAndreas Gohr     *
34293a7873eSAndreas Gohr     * @return bool
343f4476bd9SJan Schumann     */
344a4337320SAndreas Gohr    public function isCaseSensitive()
345a4337320SAndreas Gohr    {
346f4476bd9SJan Schumann        return false;
347f4476bd9SJan Schumann    }
348f4476bd9SJan Schumann
3496fcf992cSMichael Große    /**
3507910cbbbSMichael Große     * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true)
3517910cbbbSMichael Große     *
3526fcf992cSMichael Große     * @param array $filter
3536fcf992cSMichael Große     * @return string
3546fcf992cSMichael Große     */
355a4337320SAndreas Gohr    protected function constructSearchString($filter)
356a4337320SAndreas Gohr    {
35767a31a83SMichael Große        if (!$filter) {
35867a31a83SMichael Große            return '*';
35967a31a83SMichael Große        }
360a4337320SAndreas Gohr        $adldapUtils = new adLDAPUtils($this->initAdLdap(null));
36167a31a83SMichael Große        $result = '*';
36267a31a83SMichael Große        if (isset($filter['name'])) {
36307aec029SMichael Große            $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*';
36467a31a83SMichael Große            unset($filter['name']);
36567a31a83SMichael Große        }
3667910cbbbSMichael Große
36767a31a83SMichael Große        if (isset($filter['user'])) {
36807aec029SMichael Große            $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*';
36967a31a83SMichael Große            unset($filter['user']);
37067a31a83SMichael Große        }
37167a31a83SMichael Große
37267a31a83SMichael Große        if (isset($filter['mail'])) {
37307aec029SMichael Große            $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*';
37467a31a83SMichael Große            unset($filter['mail']);
37567a31a83SMichael Große        }
37667a31a83SMichael Große        return $result;
37767a31a83SMichael Große    }
37867a31a83SMichael Große
379f4476bd9SJan Schumann    /**
3807910cbbbSMichael Große     * Return a count of the number of user which meet $filter criteria
3817910cbbbSMichael Große     *
3827910cbbbSMichael Große     * @param array $filter  $filter array of field/pattern pairs, empty array for no filter
3837910cbbbSMichael Große     * @return int number of users
38425f80763SMichael Große     */
385a4337320SAndreas Gohr    public function getUserCount($filter = array())
386a4337320SAndreas Gohr    {
387a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
3886fcf992cSMichael Große        if (!$adldap) {
3896fcf992cSMichael Große            dbglog("authad/auth.php getUserCount(): _adldap not set.");
3906fcf992cSMichael Große            return -1;
3916fcf992cSMichael Große        }
39267a31a83SMichael Große        if ($filter == array()) {
39325f80763SMichael Große            $result = $adldap->user()->all();
39467a31a83SMichael Große        } else {
395a4337320SAndreas Gohr            $searchString = $this->constructSearchString($filter);
39667a31a83SMichael Große            $result = $adldap->user()->all(false, $searchString);
397c52f6cd2SMichael Große            if (isset($filter['grps'])) {
398c52f6cd2SMichael Große                $this->users = array_fill_keys($result, false);
399a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
400c52f6cd2SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
401462e9e37SMichael Große                $usermanager->setLastdisabled(true);
402a4337320SAndreas Gohr                if (!isset($this->grpsusers[$this->filterToString($filter)])) {
403a4337320SAndreas Gohr                    $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize());
404a4337320SAndreas Gohr                } elseif (count($this->grpsusers[$this->filterToString($filter)]) <
40564159a61SAndreas Gohr                    $usermanager->getStart() + 3*$usermanager->getPagesize()
40664159a61SAndreas Gohr                ) {
407a4337320SAndreas Gohr                    $this->fillGroupUserArray(
40864159a61SAndreas Gohr                        $filter,
40964159a61SAndreas Gohr                        $usermanager->getStart() +
41064159a61SAndreas Gohr                        3*$usermanager->getPagesize() -
411a4337320SAndreas Gohr                        count($this->grpsusers[$this->filterToString($filter)])
41264159a61SAndreas Gohr                    );
413c52f6cd2SMichael Große                }
414a4337320SAndreas Gohr                $result = $this->grpsusers[$this->filterToString($filter)];
415462e9e37SMichael Große            } else {
416a4337320SAndreas Gohr                /** @var admin_plugin_usermanager $usermanager */
417462e9e37SMichael Große                $usermanager = plugin_load("admin", "usermanager", false);
418462e9e37SMichael Große                $usermanager->setLastdisabled(false);
419c52f6cd2SMichael Große            }
42067a31a83SMichael Große        }
42167a31a83SMichael Große
42225f80763SMichael Große        if (!$result) {
4236fcf992cSMichael Große            return 0;
42425f80763SMichael Große        }
42525f80763SMichael Große        return count($result);
42625f80763SMichael Große    }
42725f80763SMichael Große
4286fcf992cSMichael Große    /**
4296fcf992cSMichael Große     *
4306fcf992cSMichael Große     * create a unique string for each filter used with a group
4316fcf992cSMichael Große     *
4326fcf992cSMichael Große     * @param array $filter
4336fcf992cSMichael Große     * @return string
4346fcf992cSMichael Große     */
435a4337320SAndreas Gohr    protected function filterToString($filter)
436a4337320SAndreas Gohr    {
437c52f6cd2SMichael Große        $result = '';
438c52f6cd2SMichael Große        if (isset($filter['user'])) {
439c52f6cd2SMichael Große            $result .= 'user-' . $filter['user'];
440c52f6cd2SMichael Große        }
441c52f6cd2SMichael Große        if (isset($filter['name'])) {
442c52f6cd2SMichael Große            $result .= 'name-' . $filter['name'];
443c52f6cd2SMichael Große        }
444c52f6cd2SMichael Große        if (isset($filter['mail'])) {
445c52f6cd2SMichael Große            $result .= 'mail-' . $filter['mail'];
446c52f6cd2SMichael Große        }
447c52f6cd2SMichael Große        if (isset($filter['grps'])) {
448c52f6cd2SMichael Große            $result .= 'grps-' . $filter['grps'];
449c52f6cd2SMichael Große        }
450c52f6cd2SMichael Große        return $result;
451c52f6cd2SMichael Große    }
452c52f6cd2SMichael Große
4536fcf992cSMichael Große    /**
4547910cbbbSMichael Große     * Create an array of $numberOfAdds users passing a certain $filter, including belonging
4557910cbbbSMichael Große     * to a certain group and save them to a object-wide array. If the array
4567910cbbbSMichael Große     * already exists try to add $numberOfAdds further users to it.
4577910cbbbSMichael Große     *
4586fcf992cSMichael Große     * @param array $filter
4596fcf992cSMichael Große     * @param int $numberOfAdds additional number of users requested
4606fcf992cSMichael Große     * @return int number of Users actually add to Array
4616fcf992cSMichael Große     */
462a4337320SAndreas Gohr    protected function fillGroupUserArray($filter, $numberOfAdds)
463a4337320SAndreas Gohr    {
464a4337320SAndreas Gohr        $this->grpsusers[$this->filterToString($filter)];
465c52f6cd2SMichael Große        $i = 0;
466c52f6cd2SMichael Große        $count = 0;
467a4337320SAndreas Gohr        $this->constructPattern($filter);
468c52f6cd2SMichael Große        foreach ($this->users as $user => &$info) {
469a4337320SAndreas Gohr            if ($i++ < $this->actualstart) {
470c52f6cd2SMichael Große                continue;
471c52f6cd2SMichael Große            }
472c52f6cd2SMichael Große            if ($info === false) {
473c52f6cd2SMichael Große                $info = $this->getUserData($user);
474c52f6cd2SMichael Große            }
475a4337320SAndreas Gohr            if ($this->filter($user, $info)) {
476a4337320SAndreas Gohr                $this->grpsusers[$this->filterToString($filter)][$user] = $info;
477c52f6cd2SMichael Große                if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break;
478c52f6cd2SMichael Große            }
479c52f6cd2SMichael Große        }
480a4337320SAndreas Gohr        $this->actualstart = $i;
481c52f6cd2SMichael Große        return $count;
482c52f6cd2SMichael Große    }
483c52f6cd2SMichael Große
48425f80763SMichael Große    /**
485f4476bd9SJan Schumann     * Bulk retrieval of user data
486f4476bd9SJan Schumann     *
487f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
488253d4b48SGerrit Uitslag     *
48993a7873eSAndreas Gohr     * @param   int $start index of first user to be returned
49093a7873eSAndreas Gohr     * @param   int $limit max number of users to be returned
49193a7873eSAndreas Gohr     * @param   array $filter array of field/pattern pairs, null for no filter
49293a7873eSAndreas Gohr     * @return array userinfo (refer getUserData for internal userinfo details)
493f4476bd9SJan Schumann     */
494a4337320SAndreas Gohr    public function retrieveUsers($start = 0, $limit = 0, $filter = array())
495a4337320SAndreas Gohr    {
496a4337320SAndreas Gohr        $adldap = $this->initAdLdap(null);
497a4337320SAndreas Gohr        if (!$adldap) return array();
498f4476bd9SJan Schumann
4990ba750c0SAndreas Gohr        if (!$this->users) {
500f4476bd9SJan Schumann            //get info for given user
501a4337320SAndreas Gohr            $result = $adldap->user()->all(false, $this->constructSearchString($filter));
502f4476bd9SJan Schumann            if (!$result) return array();
503f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
504f4476bd9SJan Schumann        }
505f4476bd9SJan Schumann
506f4476bd9SJan Schumann        $i     = 0;
507f4476bd9SJan Schumann        $count = 0;
508f4476bd9SJan Schumann        $result = array();
509f4476bd9SJan Schumann
510c52f6cd2SMichael Große        if (!isset($filter['grps'])) {
511a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
512462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
513462e9e37SMichael Große            $usermanager->setLastdisabled(false);
514a4337320SAndreas Gohr            $this->constructPattern($filter);
515f4476bd9SJan Schumann            foreach ($this->users as $user => &$info) {
516f4476bd9SJan Schumann                if ($i++ < $start) {
517f4476bd9SJan Schumann                    continue;
518f4476bd9SJan Schumann                }
519f4476bd9SJan Schumann                if ($info === false) {
520f4476bd9SJan Schumann                    $info = $this->getUserData($user);
521f4476bd9SJan Schumann                }
522f4476bd9SJan Schumann                $result[$user] = $info;
5239a2c73e8SAndreas Gohr                if (($limit > 0) && (++$count >= $limit)) break;
524f4476bd9SJan Schumann            }
525c52f6cd2SMichael Große        } else {
526a4337320SAndreas Gohr            /** @var admin_plugin_usermanager $usermanager */
527462e9e37SMichael Große            $usermanager = plugin_load("admin", "usermanager", false);
528462e9e37SMichael Große            $usermanager->setLastdisabled(true);
529a4337320SAndreas Gohr            if (!isset($this->grpsusers[$this->filterToString($filter)]) ||
530a4337320SAndreas Gohr                count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit)
53164159a61SAndreas Gohr            ) {
532a4337320SAndreas Gohr                $this->fillGroupUserArray(
53364159a61SAndreas Gohr                    $filter,
534a4337320SAndreas Gohr                    $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1
53564159a61SAndreas Gohr                );
536c52f6cd2SMichael Große            }
537a4337320SAndreas Gohr            if (!$this->grpsusers[$this->filterToString($filter)]) return array();
538a4337320SAndreas Gohr            foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) {
539c52f6cd2SMichael Große                if ($i++ < $start) {
540c52f6cd2SMichael Große                    continue;
541c52f6cd2SMichael Große                }
542c52f6cd2SMichael Große                $result[$user] = $info;
543c52f6cd2SMichael Große                if (($limit > 0) && (++$count >= $limit)) break;
544c52f6cd2SMichael Große            }
545c52f6cd2SMichael Große        }
546f4476bd9SJan Schumann        return $result;
547f4476bd9SJan Schumann    }
548f4476bd9SJan Schumann
549f4476bd9SJan Schumann    /**
550f4476bd9SJan Schumann     * Modify user data
551f4476bd9SJan Schumann     *
55293a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
55393a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
554f4476bd9SJan Schumann     * @return  bool
555f4476bd9SJan Schumann     */
556a4337320SAndreas Gohr    public function modifyUser($user, $changes)
557a4337320SAndreas Gohr    {
558f4476bd9SJan Schumann        $return = true;
559a4337320SAndreas Gohr        $adldap = $this->initAdLdap($this->getUserDomain($user));
5608f03c311SPatrick Brown        if (!$adldap) {
5618f03c311SPatrick Brown            msg($this->getLang('connectfail'), -1);
5628f03c311SPatrick Brown            return false;
5638f03c311SPatrick Brown        }
564f4476bd9SJan Schumann
565f4476bd9SJan Schumann        // password changing
566f4476bd9SJan Schumann        if (isset($changes['pass'])) {
567f4476bd9SJan Schumann            try {
568a4337320SAndreas Gohr                $return = $adldap->user()->password($this->getUserName($user), $changes['pass']);
569f4476bd9SJan Schumann            } catch (adLDAPException $e) {
57032fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
571f4476bd9SJan Schumann                $return = false;
572f4476bd9SJan Schumann            }
5738f03c311SPatrick Brown            if (!$return) msg($this->getLang('passchangefail'), -1);
574f4476bd9SJan Schumann        }
575f4476bd9SJan Schumann
576f4476bd9SJan Schumann        // changing user data
577f4476bd9SJan Schumann        $adchanges = array();
578f4476bd9SJan Schumann        if (isset($changes['name'])) {
579f4476bd9SJan Schumann            // get first and last name
580f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
581f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
582f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
583f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
584f4476bd9SJan Schumann        }
585f4476bd9SJan Schumann        if (isset($changes['mail'])) {
586f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
587f4476bd9SJan Schumann        }
588f4476bd9SJan Schumann        if (count($adchanges)) {
589f4476bd9SJan Schumann            try {
590a4337320SAndreas Gohr                $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges);
591f4476bd9SJan Schumann            } catch (adLDAPException $e) {
59232fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
593f4476bd9SJan Schumann                $return = false;
594f4476bd9SJan Schumann            }
5958f03c311SPatrick Brown            if (!$return) msg($this->getLang('userchangefail'), -1);
596f4476bd9SJan Schumann        }
597f4476bd9SJan Schumann
598f4476bd9SJan Schumann        return $return;
599f4476bd9SJan Schumann    }
600f4476bd9SJan Schumann
601f4476bd9SJan Schumann    /**
602f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
60393a7873eSAndreas Gohr     *
60493a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
60593a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
60693a7873eSAndreas Gohr     * domain if no current one is available.
60793a7873eSAndreas Gohr     *
60893a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
60993a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
610f4476bd9SJan Schumann     */
611a4337320SAndreas Gohr    protected function initAdLdap($domain)
612a4337320SAndreas Gohr    {
61393a7873eSAndreas Gohr        if (is_null($domain) && is_array($this->opts)) {
61493a7873eSAndreas Gohr            $domain = $this->opts['domain'];
61593a7873eSAndreas Gohr        }
61693a7873eSAndreas Gohr
617a4337320SAndreas Gohr        $this->opts = $this->loadServerConfig((string) $domain);
61893a7873eSAndreas Gohr        if (isset($this->adldap[$domain])) return $this->adldap[$domain];
619f4476bd9SJan Schumann
620f4476bd9SJan Schumann        // connect
621f4476bd9SJan Schumann        try {
62293a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
62393a7873eSAndreas Gohr            return $this->adldap[$domain];
624a4337320SAndreas Gohr        } catch (Exception $e) {
62532fd494aSAndreas Gohr            if ($this->conf['debug']) {
626f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
627f4476bd9SJan Schumann            }
628f4476bd9SJan Schumann            $this->success         = false;
62993a7873eSAndreas Gohr            $this->adldap[$domain] = null;
630f4476bd9SJan Schumann        }
631f4476bd9SJan Schumann        return false;
632f4476bd9SJan Schumann    }
633f4476bd9SJan Schumann
634f4476bd9SJan Schumann    /**
63593a7873eSAndreas Gohr     * Get the domain part from a user
636f4476bd9SJan Schumann     *
637253d4b48SGerrit Uitslag     * @param string $user
63893a7873eSAndreas Gohr     * @return string
639f4476bd9SJan Schumann     */
640a4337320SAndreas Gohr    public function getUserDomain($user)
641a4337320SAndreas Gohr    {
64293a7873eSAndreas Gohr        list(, $domain) = explode('@', $user, 2);
64393a7873eSAndreas Gohr        return $domain;
644f4476bd9SJan Schumann    }
645f4476bd9SJan Schumann
64693a7873eSAndreas Gohr    /**
64793a7873eSAndreas Gohr     * Get the user part from a user
64893a7873eSAndreas Gohr     *
649916ef7cfSAndreas Gohr     * When an account suffix is set, we strip the domain part from the user
650916ef7cfSAndreas Gohr     *
651253d4b48SGerrit Uitslag     * @param string $user
65293a7873eSAndreas Gohr     * @return string
65393a7873eSAndreas Gohr     */
654a4337320SAndreas Gohr    public function getUserName($user)
655a4337320SAndreas Gohr    {
656916ef7cfSAndreas Gohr        if ($this->conf['account_suffix'] !== '') {
657916ef7cfSAndreas Gohr            list($user) = explode('@', $user, 2);
658916ef7cfSAndreas Gohr        }
659916ef7cfSAndreas Gohr        return $user;
66093a7873eSAndreas Gohr    }
66193a7873eSAndreas Gohr
66293a7873eSAndreas Gohr    /**
66393a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
66493a7873eSAndreas Gohr     *
66593a7873eSAndreas Gohr     * @param string $domain current AD domain
66693a7873eSAndreas Gohr     * @return array
66793a7873eSAndreas Gohr     */
668a4337320SAndreas Gohr    protected function loadServerConfig($domain)
669a4337320SAndreas Gohr    {
67093a7873eSAndreas Gohr        // prepare adLDAP standard configuration
67132fd494aSAndreas Gohr        $opts = $this->conf;
67293a7873eSAndreas Gohr
67393a7873eSAndreas Gohr        $opts['domain'] = $domain;
67493a7873eSAndreas Gohr
67593a7873eSAndreas Gohr        // add possible domain specific configuration
67632fd494aSAndreas Gohr        if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) {
67793a7873eSAndreas Gohr            $opts[$key] = $val;
67893a7873eSAndreas Gohr        }
67993a7873eSAndreas Gohr
68093a7873eSAndreas Gohr        // handle multiple AD servers
68193a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
68293a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
68393a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
68493a7873eSAndreas Gohr
6858257d713SAndreas Gohr        // compatibility with old option name
68664159a61SAndreas Gohr        if (empty($opts['admin_username']) && !empty($opts['ad_username'])) {
68764159a61SAndreas Gohr            $opts['admin_username'] = $opts['ad_username'];
68864159a61SAndreas Gohr        }
68964159a61SAndreas Gohr        if (empty($opts['admin_password']) && !empty($opts['ad_password'])) {
69064159a61SAndreas Gohr            $opts['admin_password'] = $opts['ad_password'];
69164159a61SAndreas Gohr        }
692342753d2SAndreas Gohr        $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate
6938257d713SAndreas Gohr
69493a7873eSAndreas Gohr        // we can change the password if SSL is set
69593a7873eSAndreas Gohr        if ($opts['use_ssl'] || $opts['use_tls']) {
69693a7873eSAndreas Gohr            $this->cando['modPass'] = true;
69793a7873eSAndreas Gohr        } else {
69893a7873eSAndreas Gohr            $this->cando['modPass'] = false;
69993a7873eSAndreas Gohr        }
70093a7873eSAndreas Gohr
70112d195abSAndreas Gohr        // adLDAP expects empty user/pass as NULL, we're less strict FS#2781
70212d195abSAndreas Gohr        if (empty($opts['admin_username'])) $opts['admin_username'] = null;
70312d195abSAndreas Gohr        if (empty($opts['admin_password'])) $opts['admin_password'] = null;
70412d195abSAndreas Gohr
70512d195abSAndreas Gohr        // user listing needs admin priviledges
7068257d713SAndreas Gohr        if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) {
70793a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
70893a7873eSAndreas Gohr        } else {
7091b228d28SKlap-in            $this->cando['getUsers'] = false;
71093a7873eSAndreas Gohr        }
71193a7873eSAndreas Gohr
71293a7873eSAndreas Gohr        return $opts;
71393a7873eSAndreas Gohr    }
71493a7873eSAndreas Gohr
71593a7873eSAndreas Gohr    /**
716741b8a48SAndreas Gohr     * Returns a list of configured domains
717741b8a48SAndreas Gohr     *
718741b8a48SAndreas Gohr     * The default domain has an empty string as key
719741b8a48SAndreas Gohr     *
720741b8a48SAndreas Gohr     * @return array associative array(key => domain)
721741b8a48SAndreas Gohr     */
722a4337320SAndreas Gohr    public function getConfiguredDomains()
723a4337320SAndreas Gohr    {
724741b8a48SAndreas Gohr        $domains = array();
725741b8a48SAndreas Gohr        if (empty($this->conf['account_suffix'])) return $domains; // not configured yet
726741b8a48SAndreas Gohr
727741b8a48SAndreas Gohr        // add default domain, using the name from account suffix
728741b8a48SAndreas Gohr        $domains[''] = ltrim($this->conf['account_suffix'], '@');
729741b8a48SAndreas Gohr
730741b8a48SAndreas Gohr        // find additional domains
731741b8a48SAndreas Gohr        foreach ($this->conf as $key => $val) {
732741b8a48SAndreas Gohr            if (is_array($val) && isset($val['account_suffix'])) {
733741b8a48SAndreas Gohr                $domains[$key] = ltrim($val['account_suffix'], '@');
734741b8a48SAndreas Gohr            }
735741b8a48SAndreas Gohr        }
736*0489c64bSMoisés Braga Ribeiro        Sort::ksort($domains);
737741b8a48SAndreas Gohr
738741b8a48SAndreas Gohr        return $domains;
739741b8a48SAndreas Gohr    }
740741b8a48SAndreas Gohr
741741b8a48SAndreas Gohr    /**
74293a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
74393a7873eSAndreas Gohr     *
74493a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
74593a7873eSAndreas Gohr     *
74693a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
747253d4b48SGerrit Uitslag     *
74893a7873eSAndreas Gohr     * @param string $user
74993a7873eSAndreas Gohr     * @param array  $info
75093a7873eSAndreas Gohr     * @return bool
75193a7873eSAndreas Gohr     */
752a4337320SAndreas Gohr    protected function filter($user, $info)
753a4337320SAndreas Gohr    {
754a4337320SAndreas Gohr        foreach ($this->pattern as $item => $pattern) {
75593a7873eSAndreas Gohr            if ($item == 'user') {
75693a7873eSAndreas Gohr                if (!preg_match($pattern, $user)) return false;
75793a7873eSAndreas Gohr            } elseif ($item == 'grps') {
75893a7873eSAndreas Gohr                if (!count(preg_grep($pattern, $info['grps']))) return false;
75993a7873eSAndreas Gohr            } else {
76093a7873eSAndreas Gohr                if (!preg_match($pattern, $info[$item])) return false;
76193a7873eSAndreas Gohr            }
76293a7873eSAndreas Gohr        }
76393a7873eSAndreas Gohr        return true;
76493a7873eSAndreas Gohr    }
76593a7873eSAndreas Gohr
76693a7873eSAndreas Gohr    /**
76793a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
76893a7873eSAndreas Gohr     *
76993a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
770253d4b48SGerrit Uitslag     *
77193a7873eSAndreas Gohr     * @param array $filter
77293a7873eSAndreas Gohr     */
773a4337320SAndreas Gohr    protected function constructPattern($filter)
774a4337320SAndreas Gohr    {
775a4337320SAndreas Gohr        $this->pattern = array();
776f4476bd9SJan Schumann        foreach ($filter as $item => $pattern) {
777a4337320SAndreas Gohr            $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
778f4476bd9SJan Schumann        }
779f4476bd9SJan Schumann    }
780f4476bd9SJan Schumann}
781