xref: /dokuwiki/lib/plugins/authpdo/auth.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1f64dbc90SAndreas Gohr<?php
2*d4f83172SAndreas Gohr
38553d24dSAndreas Gohruse dokuwiki\Extension\AuthPlugin;
4ab9790caSAndreas Gohruse dokuwiki\PassHash;
50489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort;
60489c64bSMoisés Braga Ribeiro
7f64dbc90SAndreas Gohr/**
8f64dbc90SAndreas Gohr * DokuWiki Plugin authpdo (Auth Component)
9f64dbc90SAndreas Gohr *
10f64dbc90SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11f64dbc90SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
12f64dbc90SAndreas Gohr */
13f64dbc90SAndreas Gohr
140d586afdSAndreas Gohr/**
150d586afdSAndreas Gohr * Class auth_plugin_authpdo
160d586afdSAndreas Gohr */
178553d24dSAndreas Gohrclass auth_plugin_authpdo extends AuthPlugin
183213bf4eSAndreas Gohr{
19f64dbc90SAndreas Gohr    /** @var PDO */
20f64dbc90SAndreas Gohr    protected $pdo;
21f64dbc90SAndreas Gohr
220cec3e2aSAndreas Gohr    /** @var null|array The list of all groups */
23ab9790caSAndreas Gohr    protected $groupcache;
240cec3e2aSAndreas Gohr
25f64dbc90SAndreas Gohr    /**
26f64dbc90SAndreas Gohr     * Constructor.
27f64dbc90SAndreas Gohr     */
283213bf4eSAndreas Gohr    public function __construct()
293213bf4eSAndreas Gohr    {
30f64dbc90SAndreas Gohr        parent::__construct(); // for compatibility
31f64dbc90SAndreas Gohr
32f64dbc90SAndreas Gohr        if (!class_exists('PDO')) {
333213bf4eSAndreas Gohr            $this->debugMsg('PDO extension for PHP not found.', -1, __LINE__);
34f64dbc90SAndreas Gohr            $this->success = false;
35f64dbc90SAndreas Gohr            return;
36f64dbc90SAndreas Gohr        }
37f64dbc90SAndreas Gohr
38f64dbc90SAndreas Gohr        if (!$this->getConf('dsn')) {
393213bf4eSAndreas Gohr            $this->debugMsg('No DSN specified', -1, __LINE__);
40f64dbc90SAndreas Gohr            $this->success = false;
41f64dbc90SAndreas Gohr            return;
42f64dbc90SAndreas Gohr        }
43f64dbc90SAndreas Gohr
44f64dbc90SAndreas Gohr        try {
45f64dbc90SAndreas Gohr            $this->pdo = new PDO(
46f64dbc90SAndreas Gohr                $this->getConf('dsn'),
47f64dbc90SAndreas Gohr                $this->getConf('user'),
480cc11d97SAndreas Gohr                conf_decodeString($this->getConf('pass')),
49ab9790caSAndreas Gohr                [
5070a89417SAndreas Gohr                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array
5170a89417SAndreas Gohr                    PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names
525de3a6a5SAndreas Gohr                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
53ab9790caSAndreas Gohr                ]
54f64dbc90SAndreas Gohr            );
55f64dbc90SAndreas Gohr        } catch (PDOException $e) {
563213bf4eSAndreas Gohr            $this->debugMsg($e);
57c27579a6SAndreas Gohr            msg($this->getLang('connectfail'), -1);
58f64dbc90SAndreas Gohr            $this->success = false;
59f64dbc90SAndreas Gohr            return;
60f64dbc90SAndreas Gohr        }
61f64dbc90SAndreas Gohr
620d586afdSAndreas Gohr        // can Users be created?
633213bf4eSAndreas Gohr        $this->cando['addUser'] = $this->checkConfig(
64ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'select-groups', 'insert-user', 'insert-group', 'join-group']
650d586afdSAndreas Gohr        );
66f64dbc90SAndreas Gohr
670d586afdSAndreas Gohr        // can Users be deleted?
683213bf4eSAndreas Gohr        $this->cando['delUser'] = $this->checkConfig(
69ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'select-groups', 'leave-group', 'delete-user']
700d586afdSAndreas Gohr        );
710d586afdSAndreas Gohr
720d586afdSAndreas Gohr        // can login names be changed?
733213bf4eSAndreas Gohr        $this->cando['modLogin'] = $this->checkConfig(
74ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'update-user-login']
750d586afdSAndreas Gohr        );
760d586afdSAndreas Gohr
770d586afdSAndreas Gohr        // can passwords be changed?
783213bf4eSAndreas Gohr        $this->cando['modPass'] = $this->checkConfig(
79ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'update-user-pass']
800d586afdSAndreas Gohr        );
810d586afdSAndreas Gohr
82ad4d5631SAndreas Gohr        // can real names be changed?
833213bf4eSAndreas Gohr        $this->cando['modName'] = $this->checkConfig(
84ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'update-user-info:name']
85ad4d5631SAndreas Gohr        );
86ad4d5631SAndreas Gohr
87ad4d5631SAndreas Gohr        // can real email be changed?
883213bf4eSAndreas Gohr        $this->cando['modMail'] = $this->checkConfig(
89ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'update-user-info:mail']
900d586afdSAndreas Gohr        );
910d586afdSAndreas Gohr
920d586afdSAndreas Gohr        // can groups be changed?
933213bf4eSAndreas Gohr        $this->cando['modGroups'] = $this->checkConfig(
94ab9790caSAndreas Gohr            ['select-user', 'select-user-groups', 'select-groups', 'leave-group', 'join-group', 'insert-group']
950d586afdSAndreas Gohr        );
960d586afdSAndreas Gohr
970d586afdSAndreas Gohr        // can a filtered list of users be retrieved?
983213bf4eSAndreas Gohr        $this->cando['getUsers'] = $this->checkConfig(
99ab9790caSAndreas Gohr            ['list-users']
1000d586afdSAndreas Gohr        );
1010d586afdSAndreas Gohr
1020d586afdSAndreas Gohr        // can the number of users be retrieved?
1033213bf4eSAndreas Gohr        $this->cando['getUserCount'] = $this->checkConfig(
104ab9790caSAndreas Gohr            ['count-users']
1050d586afdSAndreas Gohr        );
1060d586afdSAndreas Gohr
1070d586afdSAndreas Gohr        // can a list of available groups be retrieved?
1083213bf4eSAndreas Gohr        $this->cando['getGroups'] = $this->checkConfig(
109ab9790caSAndreas Gohr            ['select-groups']
1100d586afdSAndreas Gohr        );
1110d586afdSAndreas Gohr
112f64dbc90SAndreas Gohr        $this->success = true;
113f64dbc90SAndreas Gohr    }
114f64dbc90SAndreas Gohr
115f64dbc90SAndreas Gohr    /**
116f64dbc90SAndreas Gohr     * Check user+password
117f64dbc90SAndreas Gohr     *
118f64dbc90SAndreas Gohr     * @param string $user the user name
119f64dbc90SAndreas Gohr     * @param string $pass the clear text password
120f64dbc90SAndreas Gohr     * @return  bool
121f64dbc90SAndreas Gohr     */
1223213bf4eSAndreas Gohr    public function checkPass($user, $pass)
1233213bf4eSAndreas Gohr    {
124f64dbc90SAndreas Gohr
1253213bf4eSAndreas Gohr        $userdata = $this->selectUser($user);
126397d62a2SAndreas Gohr        if ($userdata == false) return false;
127f64dbc90SAndreas Gohr
128397d62a2SAndreas Gohr        // password checking done in SQL?
129ab9790caSAndreas Gohr        if ($this->checkConfig(['check-pass'])) {
130397d62a2SAndreas Gohr            $userdata['clear'] = $pass;
131397d62a2SAndreas Gohr            $userdata['hash'] = auth_cryptPassword($pass);
1323213bf4eSAndreas Gohr            $result = $this->query($this->getConf('check-pass'), $userdata);
133397d62a2SAndreas Gohr            if ($result === false) return false;
134397d62a2SAndreas Gohr            return (count($result) == 1);
135397d62a2SAndreas Gohr        }
136397d62a2SAndreas Gohr
137397d62a2SAndreas Gohr        // we do password checking on our own
138397d62a2SAndreas Gohr        if (isset($userdata['hash'])) {
139f64dbc90SAndreas Gohr            // hashed password
140ab9790caSAndreas Gohr            $passhash = new PassHash();
141397d62a2SAndreas Gohr            return $passhash->verify_hash($pass, $userdata['hash']);
142f64dbc90SAndreas Gohr        } else {
143f64dbc90SAndreas Gohr            // clear text password in the database O_o
144d5c0422fSAndreas Gohr            return ($pass === $userdata['clear']);
145f64dbc90SAndreas Gohr        }
146f64dbc90SAndreas Gohr    }
147f64dbc90SAndreas Gohr
148f64dbc90SAndreas Gohr    /**
149f64dbc90SAndreas Gohr     * Return user info
150f64dbc90SAndreas Gohr     *
151f64dbc90SAndreas Gohr     * Returns info about the given user needs to contain
152f64dbc90SAndreas Gohr     * at least these fields:
153f64dbc90SAndreas Gohr     *
154f64dbc90SAndreas Gohr     * name string  full name of the user
155f64dbc90SAndreas Gohr     * mail string  email addres of the user
156f64dbc90SAndreas Gohr     * grps array   list of groups the user is in
157f64dbc90SAndreas Gohr     *
158f64dbc90SAndreas Gohr     * @param string $user the user name
159f64dbc90SAndreas Gohr     * @param bool $requireGroups whether or not the returned data must include groups
160358942b5SAndreas Gohr     * @return array|bool containing user data or false
161f64dbc90SAndreas Gohr     */
1623213bf4eSAndreas Gohr    public function getUserData($user, $requireGroups = true)
1633213bf4eSAndreas Gohr    {
1643213bf4eSAndreas Gohr        $data = $this->selectUser($user);
165f64dbc90SAndreas Gohr        if ($data == false) return false;
166f64dbc90SAndreas Gohr
16770a89417SAndreas Gohr        if (isset($data['hash'])) unset($data['hash']);
16870a89417SAndreas Gohr        if (isset($data['clean'])) unset($data['clean']);
169f64dbc90SAndreas Gohr
17070a89417SAndreas Gohr        if ($requireGroups) {
1713213bf4eSAndreas Gohr            $data['grps'] = $this->selectUserGroups($data);
1725de3a6a5SAndreas Gohr            if ($data['grps'] === false) return false;
173f64dbc90SAndreas Gohr        }
174f64dbc90SAndreas Gohr
175f64dbc90SAndreas Gohr        return $data;
176f64dbc90SAndreas Gohr    }
177f64dbc90SAndreas Gohr
178f64dbc90SAndreas Gohr    /**
179f64dbc90SAndreas Gohr     * Create a new User [implement only where required/possible]
180f64dbc90SAndreas Gohr     *
181f64dbc90SAndreas Gohr     * Returns false if the user already exists, null when an error
182f64dbc90SAndreas Gohr     * occurred and true if everything went well.
183f64dbc90SAndreas Gohr     *
184f64dbc90SAndreas Gohr     * The new user HAS TO be added to the default group by this
185f64dbc90SAndreas Gohr     * function!
186f64dbc90SAndreas Gohr     *
187f64dbc90SAndreas Gohr     * Set addUser capability when implemented
188f64dbc90SAndreas Gohr     *
189f64dbc90SAndreas Gohr     * @param string $user
1905de3a6a5SAndreas Gohr     * @param string $clear
191f64dbc90SAndreas Gohr     * @param string $name
192f64dbc90SAndreas Gohr     * @param string $mail
193f64dbc90SAndreas Gohr     * @param null|array $grps
194f64dbc90SAndreas Gohr     * @return bool|null
195f64dbc90SAndreas Gohr     */
1963213bf4eSAndreas Gohr    public function createUser($user, $clear, $name, $mail, $grps = null)
1973213bf4eSAndreas Gohr    {
1985de3a6a5SAndreas Gohr        global $conf;
1995de3a6a5SAndreas Gohr
2005de3a6a5SAndreas Gohr        if (($info = $this->getUserData($user, false)) !== false) {
2015de3a6a5SAndreas Gohr            msg($this->getLang('userexists'), -1);
2025de3a6a5SAndreas Gohr            return false; // user already exists
2035de3a6a5SAndreas Gohr        }
2045de3a6a5SAndreas Gohr
2055de3a6a5SAndreas Gohr        // prepare data
206ab9790caSAndreas Gohr        if ($grps == null) $grps = [];
20707a11e2aSAndreas Gohr        array_unshift($grps, $conf['defaultgroup']);
2085de3a6a5SAndreas Gohr        $grps = array_unique($grps);
2095de3a6a5SAndreas Gohr        $hash = auth_cryptPassword($clear);
210ab9790caSAndreas Gohr        $userdata = ['user' => $user, 'clear' => $clear, 'hash' => $hash, 'name' => $name, 'mail' => $mail];
2115de3a6a5SAndreas Gohr
2125de3a6a5SAndreas Gohr        // action protected by transaction
2135de3a6a5SAndreas Gohr        $this->pdo->beginTransaction();
2145de3a6a5SAndreas Gohr        {
2155de3a6a5SAndreas Gohr            // insert the user
2163213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('insert-user'), $userdata);
2175de3a6a5SAndreas Gohr            if ($ok === false) goto FAIL;
2185de3a6a5SAndreas Gohr            $userdata = $this->getUserData($user, false);
2195de3a6a5SAndreas Gohr            if ($userdata === false) goto FAIL;
2205de3a6a5SAndreas Gohr
2215de3a6a5SAndreas Gohr            // create all groups that do not exist, the refetch the groups
2223213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
2235de3a6a5SAndreas Gohr        foreach ($grps as $group) {
2245de3a6a5SAndreas Gohr            if (!isset($allgroups[$group])) {
2256459f496SAndreas Gohr                $ok = $this->addGroup($group);
2265de3a6a5SAndreas Gohr                if ($ok === false) goto FAIL;
2275de3a6a5SAndreas Gohr            }
2285de3a6a5SAndreas Gohr        }
2293213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
2305de3a6a5SAndreas Gohr
2315de3a6a5SAndreas Gohr            // add user to the groups
2325de3a6a5SAndreas Gohr        foreach ($grps as $group) {
2333213bf4eSAndreas Gohr            $ok = $this->joinGroup($userdata, $allgroups[$group]);
2345de3a6a5SAndreas Gohr            if ($ok === false) goto FAIL;
2355de3a6a5SAndreas Gohr        }
2365de3a6a5SAndreas Gohr        }
2375de3a6a5SAndreas Gohr        $this->pdo->commit();
2385de3a6a5SAndreas Gohr        return true;
2395de3a6a5SAndreas Gohr
2405de3a6a5SAndreas Gohr        // something went wrong, rollback
2415de3a6a5SAndreas Gohr        FAIL:
2425de3a6a5SAndreas Gohr        $this->pdo->rollBack();
2433213bf4eSAndreas Gohr        $this->debugMsg('Transaction rolled back', 0, __LINE__);
244c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
2455de3a6a5SAndreas Gohr        return null; // return error
2465de3a6a5SAndreas Gohr    }
247f64dbc90SAndreas Gohr
248f64dbc90SAndreas Gohr    /**
2494fb8dfabSAndreas Gohr     * Modify user data
250f64dbc90SAndreas Gohr     *
251f64dbc90SAndreas Gohr     * @param string $user nick of the user to be changed
252f64dbc90SAndreas Gohr     * @param array $changes array of field/value pairs to be changed (password will be clear text)
253f64dbc90SAndreas Gohr     * @return  bool
254f64dbc90SAndreas Gohr     */
2553213bf4eSAndreas Gohr    public function modifyUser($user, $changes)
2563213bf4eSAndreas Gohr    {
2574fb8dfabSAndreas Gohr        // secure everything in transaction
2584fb8dfabSAndreas Gohr        $this->pdo->beginTransaction();
2594fb8dfabSAndreas Gohr        {
2604fb8dfabSAndreas Gohr            $olddata = $this->getUserData($user);
2614fb8dfabSAndreas Gohr            $oldgroups = $olddata['grps'];
2624fb8dfabSAndreas Gohr            unset($olddata['grps']);
2634fb8dfabSAndreas Gohr
2644fb8dfabSAndreas Gohr            // changing the user name?
2654fb8dfabSAndreas Gohr        if (isset($changes['user'])) {
2664fb8dfabSAndreas Gohr            if ($this->getUserData($changes['user'], false)) goto FAIL;
2674fb8dfabSAndreas Gohr            $params = $olddata;
2684fb8dfabSAndreas Gohr            $params['newlogin'] = $changes['user'];
2694fb8dfabSAndreas Gohr
2703213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('update-user-login'), $params);
2714fb8dfabSAndreas Gohr            if ($ok === false) goto FAIL;
2724fb8dfabSAndreas Gohr        }
2734fb8dfabSAndreas Gohr
2744fb8dfabSAndreas Gohr            // changing the password?
2754fb8dfabSAndreas Gohr        if (isset($changes['pass'])) {
2764fb8dfabSAndreas Gohr            $params = $olddata;
2774fb8dfabSAndreas Gohr            $params['clear'] = $changes['pass'];
2784fb8dfabSAndreas Gohr            $params['hash'] = auth_cryptPassword($changes['pass']);
2794fb8dfabSAndreas Gohr
2803213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('update-user-pass'), $params);
2814fb8dfabSAndreas Gohr            if ($ok === false) goto FAIL;
2824fb8dfabSAndreas Gohr        }
2834fb8dfabSAndreas Gohr
2844fb8dfabSAndreas Gohr            // changing info?
2854fb8dfabSAndreas Gohr        if (isset($changes['mail']) || isset($changes['name'])) {
2864fb8dfabSAndreas Gohr            $params = $olddata;
2874fb8dfabSAndreas Gohr            if (isset($changes['mail'])) $params['mail'] = $changes['mail'];
2884fb8dfabSAndreas Gohr            if (isset($changes['name'])) $params['name'] = $changes['name'];
2894fb8dfabSAndreas Gohr
2903213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('update-user-info'), $params);
2914fb8dfabSAndreas Gohr            if ($ok === false) goto FAIL;
2924fb8dfabSAndreas Gohr        }
2934fb8dfabSAndreas Gohr
2944fb8dfabSAndreas Gohr            // changing groups?
2954fb8dfabSAndreas Gohr        if (isset($changes['grps'])) {
2963213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
2974fb8dfabSAndreas Gohr
2984fb8dfabSAndreas Gohr            // remove membership for previous groups
2994fb8dfabSAndreas Gohr            foreach ($oldgroups as $group) {
300358942b5SAndreas Gohr                if (!in_array($group, $changes['grps']) && isset($allgroups[$group])) {
3013213bf4eSAndreas Gohr                    $ok = $this->leaveGroup($olddata, $allgroups[$group]);
3024fb8dfabSAndreas Gohr                    if ($ok === false) goto FAIL;
3034fb8dfabSAndreas Gohr                }
3044fb8dfabSAndreas Gohr            }
3054fb8dfabSAndreas Gohr
3064fb8dfabSAndreas Gohr            // create all new groups that are missing
3074fb8dfabSAndreas Gohr            $added = 0;
3084fb8dfabSAndreas Gohr            foreach ($changes['grps'] as $group) {
3094fb8dfabSAndreas Gohr                if (!isset($allgroups[$group])) {
3106459f496SAndreas Gohr                    $ok = $this->addGroup($group);
3114fb8dfabSAndreas Gohr                    if ($ok === false) goto FAIL;
3124fb8dfabSAndreas Gohr                    $added++;
3134fb8dfabSAndreas Gohr                }
3144fb8dfabSAndreas Gohr            }
3154fb8dfabSAndreas Gohr            // reload group info
3163213bf4eSAndreas Gohr            if ($added > 0) $allgroups = $this->selectGroups();
3174fb8dfabSAndreas Gohr
3184fb8dfabSAndreas Gohr            // add membership for new groups
3194fb8dfabSAndreas Gohr            foreach ($changes['grps'] as $group) {
3204fb8dfabSAndreas Gohr                if (!in_array($group, $oldgroups)) {
3213213bf4eSAndreas Gohr                    $ok = $this->joinGroup($olddata, $allgroups[$group]);
3224fb8dfabSAndreas Gohr                    if ($ok === false) goto FAIL;
3234fb8dfabSAndreas Gohr                }
3244fb8dfabSAndreas Gohr            }
3254fb8dfabSAndreas Gohr        }
3264fb8dfabSAndreas Gohr
3274fb8dfabSAndreas Gohr        }
3284fb8dfabSAndreas Gohr        $this->pdo->commit();
3294fb8dfabSAndreas Gohr        return true;
3304fb8dfabSAndreas Gohr
3314fb8dfabSAndreas Gohr        // something went wrong, rollback
3324fb8dfabSAndreas Gohr        FAIL:
3334fb8dfabSAndreas Gohr        $this->pdo->rollBack();
3343213bf4eSAndreas Gohr        $this->debugMsg('Transaction rolled back', 0, __LINE__);
335c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
3364fb8dfabSAndreas Gohr        return false; // return error
3374fb8dfabSAndreas Gohr    }
338f64dbc90SAndreas Gohr
339f64dbc90SAndreas Gohr    /**
340e19be516SAndreas Gohr     * Delete one or more users
341f64dbc90SAndreas Gohr     *
342f64dbc90SAndreas Gohr     * Set delUser capability when implemented
343f64dbc90SAndreas Gohr     *
344f64dbc90SAndreas Gohr     * @param array $users
345f64dbc90SAndreas Gohr     * @return  int    number of users deleted
346f64dbc90SAndreas Gohr     */
3473213bf4eSAndreas Gohr    public function deleteUsers($users)
3483213bf4eSAndreas Gohr    {
349e19be516SAndreas Gohr        $count = 0;
350e19be516SAndreas Gohr        foreach ($users as $user) {
3513213bf4eSAndreas Gohr            if ($this->deleteUser($user)) $count++;
352e19be516SAndreas Gohr        }
353e19be516SAndreas Gohr        return $count;
354e19be516SAndreas Gohr    }
355f64dbc90SAndreas Gohr
356f64dbc90SAndreas Gohr    /**
357f64dbc90SAndreas Gohr     * Bulk retrieval of user data [implement only where required/possible]
358f64dbc90SAndreas Gohr     *
359f64dbc90SAndreas Gohr     * Set getUsers capability when implemented
360f64dbc90SAndreas Gohr     *
361f64dbc90SAndreas Gohr     * @param int $start index of first user to be returned
362f64dbc90SAndreas Gohr     * @param int $limit max number of users to be returned
363f64dbc90SAndreas Gohr     * @param array $filter array of field/pattern pairs, null for no filter
364f64dbc90SAndreas Gohr     * @return  array list of userinfo (refer getUserData for internal userinfo details)
365f64dbc90SAndreas Gohr     */
3663213bf4eSAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = null)
3673213bf4eSAndreas Gohr    {
3686459f496SAndreas Gohr        if ($limit < 0) $limit = 10000; // we don't support no limit
369ab9790caSAndreas Gohr        if (is_null($filter)) $filter = [];
3706459f496SAndreas Gohr
37112c7f5c3SAndreas Gohr        if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
372ab9790caSAndreas Gohr        foreach (['user', 'name', 'mail', 'group'] as $key) {
3736459f496SAndreas Gohr            if (!isset($filter[$key])) {
3746459f496SAndreas Gohr                $filter[$key] = '%';
3756459f496SAndreas Gohr            } else {
3766459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
3776459f496SAndreas Gohr            }
3786459f496SAndreas Gohr        }
37914119d44SAndreas Gohr        $filter['start'] = (int)$start;
38014119d44SAndreas Gohr        $filter['end'] = (int)$start + $limit;
38114119d44SAndreas Gohr        $filter['limit'] = (int)$limit;
3826459f496SAndreas Gohr
3833213bf4eSAndreas Gohr        $result = $this->query($this->getConf('list-users'), $filter);
384ab9790caSAndreas Gohr        if (!$result) return [];
385ab9790caSAndreas Gohr        $users = [];
38688ca2487SPhy        if (is_array($result)) {
3876459f496SAndreas Gohr            foreach ($result as $row) {
3886459f496SAndreas Gohr                if (!isset($row['user'])) {
38931a58abaSAndreas Gohr                    $this->debugMsg("list-users statement did not return 'user' attribute", -1, __LINE__);
390ab9790caSAndreas Gohr                    return [];
3916459f496SAndreas Gohr                }
3923e2a8145SAndreas Gohr                $users[] = $this->getUserData($row['user']);
3936459f496SAndreas Gohr            }
39488ca2487SPhy        } else {
39531a58abaSAndreas Gohr            $this->debugMsg("list-users statement did not return a list of result", -1, __LINE__);
39688ca2487SPhy        }
3976459f496SAndreas Gohr        return $users;
3986459f496SAndreas Gohr    }
399f64dbc90SAndreas Gohr
400f64dbc90SAndreas Gohr    /**
401f64dbc90SAndreas Gohr     * Return a count of the number of user which meet $filter criteria
402f64dbc90SAndreas Gohr     *
403f64dbc90SAndreas Gohr     * @param array $filter array of field/pattern pairs, empty array for no filter
404f64dbc90SAndreas Gohr     * @return int
405f64dbc90SAndreas Gohr     */
406ab9790caSAndreas Gohr    public function getUserCount($filter = [])
4073213bf4eSAndreas Gohr    {
408ab9790caSAndreas Gohr        if (is_null($filter)) $filter = [];
4096459f496SAndreas Gohr
41012c7f5c3SAndreas Gohr        if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
411ab9790caSAndreas Gohr        foreach (['user', 'name', 'mail', 'group'] as $key) {
4126459f496SAndreas Gohr            if (!isset($filter[$key])) {
4136459f496SAndreas Gohr                $filter[$key] = '%';
4146459f496SAndreas Gohr            } else {
4156459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
4166459f496SAndreas Gohr            }
4176459f496SAndreas Gohr        }
4186459f496SAndreas Gohr
4193213bf4eSAndreas Gohr        $result = $this->query($this->getConf('count-users'), $filter);
4206459f496SAndreas Gohr        if (!$result || !isset($result[0]['count'])) {
4213213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'count' attribute", -1, __LINE__);
4226459f496SAndreas Gohr        }
423f3c1c207SAndreas Gohr        return (int)$result[0]['count'];
4246459f496SAndreas Gohr    }
425f64dbc90SAndreas Gohr
426f64dbc90SAndreas Gohr    /**
4276459f496SAndreas Gohr     * Create a new group with the given name
428f64dbc90SAndreas Gohr     *
429f64dbc90SAndreas Gohr     * @param string $group
430f64dbc90SAndreas Gohr     * @return bool
431f64dbc90SAndreas Gohr     */
4323213bf4eSAndreas Gohr    public function addGroup($group)
4333213bf4eSAndreas Gohr    {
4346459f496SAndreas Gohr        $sql = $this->getConf('insert-group');
4356459f496SAndreas Gohr
436ab9790caSAndreas Gohr        $result = $this->query($sql, [':group' => $group]);
4373213bf4eSAndreas Gohr        $this->clearGroupCache();
4386459f496SAndreas Gohr        if ($result === false) return false;
4396459f496SAndreas Gohr        return true;
4406459f496SAndreas Gohr    }
441f64dbc90SAndreas Gohr
442f64dbc90SAndreas Gohr    /**
4435de3a6a5SAndreas Gohr     * Retrieve groups
444f64dbc90SAndreas Gohr     *
445f64dbc90SAndreas Gohr     * Set getGroups capability when implemented
446f64dbc90SAndreas Gohr     *
447f64dbc90SAndreas Gohr     * @param int $start
448f64dbc90SAndreas Gohr     * @param int $limit
449f64dbc90SAndreas Gohr     * @return  array
450f64dbc90SAndreas Gohr     */
4513213bf4eSAndreas Gohr    public function retrieveGroups($start = 0, $limit = 0)
4523213bf4eSAndreas Gohr    {
4533213bf4eSAndreas Gohr        $groups = array_keys($this->selectGroups());
454ab9790caSAndreas Gohr        if ($groups === false) return [];
455f64dbc90SAndreas Gohr
4565de3a6a5SAndreas Gohr        if (!$limit) {
4575de3a6a5SAndreas Gohr            return array_splice($groups, $start);
4585de3a6a5SAndreas Gohr        } else {
4595de3a6a5SAndreas Gohr            return array_splice($groups, $start, $limit);
460f64dbc90SAndreas Gohr        }
461f64dbc90SAndreas Gohr    }
462f64dbc90SAndreas Gohr
463f64dbc90SAndreas Gohr    /**
464f64dbc90SAndreas Gohr     * Select data of a specified user
465f64dbc90SAndreas Gohr     *
4665de3a6a5SAndreas Gohr     * @param string $user the user name
4675de3a6a5SAndreas Gohr     * @return bool|array user data, false on error
468f64dbc90SAndreas Gohr     */
4693213bf4eSAndreas Gohr    protected function selectUser($user)
4703213bf4eSAndreas Gohr    {
471f64dbc90SAndreas Gohr        $sql = $this->getConf('select-user');
472f64dbc90SAndreas Gohr
473ab9790caSAndreas Gohr        $result = $this->query($sql, [':user' => $user]);
47470a89417SAndreas Gohr        if (!$result) return false;
475f64dbc90SAndreas Gohr
47670a89417SAndreas Gohr        if (count($result) > 1) {
4773213bf4eSAndreas Gohr            $this->debugMsg('Found more than one matching user', -1, __LINE__);
478f64dbc90SAndreas Gohr            return false;
479f64dbc90SAndreas Gohr        }
480f64dbc90SAndreas Gohr
481f64dbc90SAndreas Gohr        $data = array_shift($result);
482f64dbc90SAndreas Gohr        $dataok = true;
483f64dbc90SAndreas Gohr
484f64dbc90SAndreas Gohr        if (!isset($data['user'])) {
4853213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'user' attribute", -1, __LINE__);
486f64dbc90SAndreas Gohr            $dataok = false;
487f64dbc90SAndreas Gohr        }
488ab9790caSAndreas Gohr        if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(['check-pass'])) {
4893213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
490f64dbc90SAndreas Gohr            $dataok = false;
491f64dbc90SAndreas Gohr        }
492f64dbc90SAndreas Gohr        if (!isset($data['name'])) {
4933213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'name' attribute", -1, __LINE__);
494f64dbc90SAndreas Gohr            $dataok = false;
495f64dbc90SAndreas Gohr        }
496f64dbc90SAndreas Gohr        if (!isset($data['mail'])) {
4973213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'mail' attribute", -1, __LINE__);
498f64dbc90SAndreas Gohr            $dataok = false;
499f64dbc90SAndreas Gohr        }
500f64dbc90SAndreas Gohr
501f64dbc90SAndreas Gohr        if (!$dataok) return false;
502f64dbc90SAndreas Gohr        return $data;
503f64dbc90SAndreas Gohr    }
504f64dbc90SAndreas Gohr
505f64dbc90SAndreas Gohr    /**
506e19be516SAndreas Gohr     * Delete a user after removing all their group memberships
507e19be516SAndreas Gohr     *
508e19be516SAndreas Gohr     * @param string $user
509e19be516SAndreas Gohr     * @return bool true when the user was deleted
510e19be516SAndreas Gohr     */
5113213bf4eSAndreas Gohr    protected function deleteUser($user)
5123213bf4eSAndreas Gohr    {
513e19be516SAndreas Gohr        $this->pdo->beginTransaction();
514e19be516SAndreas Gohr        {
515e19be516SAndreas Gohr            $userdata = $this->getUserData($user);
516e19be516SAndreas Gohr            if ($userdata === false) goto FAIL;
5173213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
518e19be516SAndreas Gohr
519e19be516SAndreas Gohr            // remove group memberships (ignore errors)
520e19be516SAndreas Gohr        foreach ($userdata['grps'] as $group) {
521358942b5SAndreas Gohr            if (isset($allgroups[$group])) {
5223213bf4eSAndreas Gohr                $this->leaveGroup($userdata, $allgroups[$group]);
523e19be516SAndreas Gohr            }
524358942b5SAndreas Gohr        }
525e19be516SAndreas Gohr
5263213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('delete-user'), $userdata);
527e19be516SAndreas Gohr            if ($ok === false) goto FAIL;
528e19be516SAndreas Gohr        }
529e19be516SAndreas Gohr        $this->pdo->commit();
530e19be516SAndreas Gohr        return true;
531e19be516SAndreas Gohr
532e19be516SAndreas Gohr        FAIL:
533e19be516SAndreas Gohr        $this->pdo->rollBack();
534e19be516SAndreas Gohr        return false;
535e19be516SAndreas Gohr    }
536e19be516SAndreas Gohr
537e19be516SAndreas Gohr    /**
53870a89417SAndreas Gohr     * Select all groups of a user
53970a89417SAndreas Gohr     *
54070a89417SAndreas Gohr     * @param array $userdata The userdata as returned by _selectUser()
5415de3a6a5SAndreas Gohr     * @return array|bool list of group names, false on error
54270a89417SAndreas Gohr     */
5433213bf4eSAndreas Gohr    protected function selectUserGroups($userdata)
5443213bf4eSAndreas Gohr    {
54570a89417SAndreas Gohr        global $conf;
54670a89417SAndreas Gohr        $sql = $this->getConf('select-user-groups');
5473213bf4eSAndreas Gohr        $result = $this->query($sql, $userdata);
5485de3a6a5SAndreas Gohr        if ($result === false) return false;
54970a89417SAndreas Gohr
550ab9790caSAndreas Gohr        $groups = [$conf['defaultgroup']]; // always add default config
55188ca2487SPhy        if (is_array($result)) {
5525de3a6a5SAndreas Gohr            foreach ($result as $row) {
5535de3a6a5SAndreas Gohr                if (!isset($row['group'])) {
55431a58abaSAndreas Gohr                    $this->debugMsg("No 'group' field returned in select-user-groups statement", -1, __LINE__);
5555de3a6a5SAndreas Gohr                    return false;
5565de3a6a5SAndreas Gohr                }
55770a89417SAndreas Gohr                $groups[] = $row['group'];
55870a89417SAndreas Gohr            }
55988ca2487SPhy        } else {
56031a58abaSAndreas Gohr            $this->debugMsg("select-user-groups statement did not return a list of result", -1, __LINE__);
56188ca2487SPhy        }
56270a89417SAndreas Gohr
56370a89417SAndreas Gohr        $groups = array_unique($groups);
5640489c64bSMoisés Braga Ribeiro        Sort::sort($groups);
56570a89417SAndreas Gohr        return $groups;
56670a89417SAndreas Gohr    }
56770a89417SAndreas Gohr
56870a89417SAndreas Gohr    /**
5695de3a6a5SAndreas Gohr     * Select all available groups
5705de3a6a5SAndreas Gohr     *
5715de3a6a5SAndreas Gohr     * @return array|bool list of all available groups and their properties
5725de3a6a5SAndreas Gohr     */
5733213bf4eSAndreas Gohr    protected function selectGroups()
5743213bf4eSAndreas Gohr    {
5750cec3e2aSAndreas Gohr        if ($this->groupcache) return $this->groupcache;
5760cec3e2aSAndreas Gohr
5775de3a6a5SAndreas Gohr        $sql = $this->getConf('select-groups');
5783213bf4eSAndreas Gohr        $result = $this->query($sql);
5795de3a6a5SAndreas Gohr        if ($result === false) return false;
5805de3a6a5SAndreas Gohr
581ab9790caSAndreas Gohr        $groups = [];
58288ca2487SPhy        if (is_array($result)) {
5835de3a6a5SAndreas Gohr            foreach ($result as $row) {
5845de3a6a5SAndreas Gohr                if (!isset($row['group'])) {
5853213bf4eSAndreas Gohr                    $this->debugMsg("No 'group' field returned from select-groups statement", -1, __LINE__);
5865de3a6a5SAndreas Gohr                    return false;
5875de3a6a5SAndreas Gohr                }
5885de3a6a5SAndreas Gohr
5895de3a6a5SAndreas Gohr                // relayout result with group name as key
5905de3a6a5SAndreas Gohr                $group = $row['group'];
5915de3a6a5SAndreas Gohr                $groups[$group] = $row;
5925de3a6a5SAndreas Gohr            }
59388ca2487SPhy        } else {
59431a58abaSAndreas Gohr            $this->debugMsg("select-groups statement did not return a list of result", -1, __LINE__);
59588ca2487SPhy        }
5965de3a6a5SAndreas Gohr
5970489c64bSMoisés Braga Ribeiro        Sort::ksort($groups);
5985de3a6a5SAndreas Gohr        return $groups;
5995de3a6a5SAndreas Gohr    }
6005de3a6a5SAndreas Gohr
6015de3a6a5SAndreas Gohr    /**
6020cec3e2aSAndreas Gohr     * Remove all entries from the group cache
6030cec3e2aSAndreas Gohr     */
6043213bf4eSAndreas Gohr    protected function clearGroupCache()
6053213bf4eSAndreas Gohr    {
6060cec3e2aSAndreas Gohr        $this->groupcache = null;
6070cec3e2aSAndreas Gohr    }
6080cec3e2aSAndreas Gohr
6090cec3e2aSAndreas Gohr    /**
6104fb8dfabSAndreas Gohr     * Adds the user to the group
6115de3a6a5SAndreas Gohr     *
6125de3a6a5SAndreas Gohr     * @param array $userdata all the user data
6135de3a6a5SAndreas Gohr     * @param array $groupdata all the group data
6145de3a6a5SAndreas Gohr     * @return bool
6155de3a6a5SAndreas Gohr     */
6163213bf4eSAndreas Gohr    protected function joinGroup($userdata, $groupdata)
6173213bf4eSAndreas Gohr    {
6185de3a6a5SAndreas Gohr        $data = array_merge($userdata, $groupdata);
6195de3a6a5SAndreas Gohr        $sql = $this->getConf('join-group');
6203213bf4eSAndreas Gohr        $result = $this->query($sql, $data);
6215de3a6a5SAndreas Gohr        if ($result === false) return false;
6225de3a6a5SAndreas Gohr        return true;
6235de3a6a5SAndreas Gohr    }
6245de3a6a5SAndreas Gohr
6255de3a6a5SAndreas Gohr    /**
6264fb8dfabSAndreas Gohr     * Removes the user from the group
6274fb8dfabSAndreas Gohr     *
6284fb8dfabSAndreas Gohr     * @param array $userdata all the user data
6294fb8dfabSAndreas Gohr     * @param array $groupdata all the group data
6304fb8dfabSAndreas Gohr     * @return bool
6314fb8dfabSAndreas Gohr     */
6323213bf4eSAndreas Gohr    protected function leaveGroup($userdata, $groupdata)
6333213bf4eSAndreas Gohr    {
6344fb8dfabSAndreas Gohr        $data = array_merge($userdata, $groupdata);
6354fb8dfabSAndreas Gohr        $sql = $this->getConf('leave-group');
6363213bf4eSAndreas Gohr        $result = $this->query($sql, $data);
6374fb8dfabSAndreas Gohr        if ($result === false) return false;
6384fb8dfabSAndreas Gohr        return true;
6394fb8dfabSAndreas Gohr    }
6404fb8dfabSAndreas Gohr
6414fb8dfabSAndreas Gohr    /**
64270a89417SAndreas Gohr     * Executes a query
64370a89417SAndreas Gohr     *
64470a89417SAndreas Gohr     * @param string $sql The SQL statement to execute
64570a89417SAndreas Gohr     * @param array $arguments Named parameters to be used in the statement
646f695c447SAndreas Gohr     * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error
64770a89417SAndreas Gohr     */
648ab9790caSAndreas Gohr    protected function query($sql, $arguments = [])
6493213bf4eSAndreas Gohr    {
650f695c447SAndreas Gohr        $sql = trim($sql);
6515de3a6a5SAndreas Gohr        if (empty($sql)) {
6523213bf4eSAndreas Gohr            $this->debugMsg('No SQL query given', -1, __LINE__);
6535de3a6a5SAndreas Gohr            return false;
6545de3a6a5SAndreas Gohr        }
6555de3a6a5SAndreas Gohr
65614119d44SAndreas Gohr        // execute
657ab9790caSAndreas Gohr        $params = [];
65814119d44SAndreas Gohr        $sth = $this->pdo->prepare($sql);
65988ca2487SPhy        $result = false;
66014119d44SAndreas Gohr        try {
66114119d44SAndreas Gohr            // prepare parameters - we only use those that exist in the SQL
66270a89417SAndreas Gohr            foreach ($arguments as $key => $value) {
66370a89417SAndreas Gohr                if (is_array($value)) continue;
66470a89417SAndreas Gohr                if (is_object($value)) continue;
66570a89417SAndreas Gohr                if ($key[0] != ':') $key = ":$key"; // prefix with colon if needed
666ab9790caSAndreas Gohr                if (strpos($sql, (string) $key) === false) continue; // skip if parameter is missing
66714119d44SAndreas Gohr
66814119d44SAndreas Gohr                if (is_int($value)) {
66914119d44SAndreas Gohr                    $sth->bindValue($key, $value, PDO::PARAM_INT);
67014119d44SAndreas Gohr                } else {
67114119d44SAndreas Gohr                    $sth->bindValue($key, $value);
67214119d44SAndreas Gohr                }
673f6cd8a7fSphjanderson                $params[$key] = $value; //remember for debugging
67470a89417SAndreas Gohr            }
67570a89417SAndreas Gohr
67614119d44SAndreas Gohr            $sth->execute();
6776a1b9bfeSPhy            // only report last line's result
6786a1b9bfeSPhy            $hasnextrowset = true;
6796a1b9bfeSPhy            $currentsql = $sql;
6806a1b9bfeSPhy            while ($hasnextrowset) {
6816a1b9bfeSPhy                if (strtolower(substr($currentsql, 0, 6)) == 'select') {
68270a89417SAndreas Gohr                    $result = $sth->fetchAll();
683f695c447SAndreas Gohr                } else {
684f695c447SAndreas Gohr                    $result = $sth->rowCount();
685f695c447SAndreas Gohr                }
6866a1b9bfeSPhy                $semi_pos = strpos($currentsql, ';');
6876a1b9bfeSPhy                if ($semi_pos) {
6886a1b9bfeSPhy                    $currentsql = trim(substr($currentsql, $semi_pos + 1));
6896a1b9bfeSPhy                }
6906a1b9bfeSPhy                try {
6916a1b9bfeSPhy                    $hasnextrowset = $sth->nextRowset(); // run next rowset
6926a1b9bfeSPhy                } catch (PDOException $rowset_e) {
6936a1b9bfeSPhy                    $hasnextrowset = false; // driver does not support multi-rowset, should be executed in one time
6946a1b9bfeSPhy                }
6956a1b9bfeSPhy            }
6965de3a6a5SAndreas Gohr        } catch (Exception $e) {
6974fb8dfabSAndreas Gohr            // report the caller's line
6984fb8dfabSAndreas Gohr            $trace = debug_backtrace();
6994fb8dfabSAndreas Gohr            $line = $trace[0]['line'];
7003213bf4eSAndreas Gohr            $dsql = $this->debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
7013213bf4eSAndreas Gohr            $this->debugMsg($e, -1, $line);
7023213bf4eSAndreas Gohr            $this->debugMsg("SQL: <pre>$dsql</pre>", -1, $line);
7031600c7ccSAndreas Gohr        }
70470a89417SAndreas Gohr        $sth->closeCursor();
70570a89417SAndreas Gohr
7065de3a6a5SAndreas Gohr        return $result;
7075de3a6a5SAndreas Gohr    }
70870a89417SAndreas Gohr
70970a89417SAndreas Gohr    /**
710f64dbc90SAndreas Gohr     * Wrapper around msg() but outputs only when debug is enabled
711f64dbc90SAndreas Gohr     *
712f64dbc90SAndreas Gohr     * @param string|Exception $message
713f64dbc90SAndreas Gohr     * @param int $err
714f64dbc90SAndreas Gohr     * @param int $line
715f64dbc90SAndreas Gohr     */
7163213bf4eSAndreas Gohr    protected function debugMsg($message, $err = 0, $line = 0)
7173213bf4eSAndreas Gohr    {
718f64dbc90SAndreas Gohr        if (!$this->getConf('debug')) return;
719f64dbc90SAndreas Gohr        if (is_a($message, 'Exception')) {
720f64dbc90SAndreas Gohr            $err = -1;
721f64dbc90SAndreas Gohr            $msg = $message->getMessage();
7224fb8dfabSAndreas Gohr            if (!$line) $line = $message->getLine();
723f64dbc90SAndreas Gohr        } else {
724f64dbc90SAndreas Gohr            $msg = $message;
725f64dbc90SAndreas Gohr        }
726f64dbc90SAndreas Gohr
727f64dbc90SAndreas Gohr        if (defined('DOKU_UNITTEST')) {
728f64dbc90SAndreas Gohr            printf("\n%s, %s:%d\n", $msg, __FILE__, $line);
729f64dbc90SAndreas Gohr        } else {
730f64dbc90SAndreas Gohr            msg('authpdo: ' . $msg, $err, $line, __FILE__);
731f64dbc90SAndreas Gohr        }
732f64dbc90SAndreas Gohr    }
7335de3a6a5SAndreas Gohr
7345de3a6a5SAndreas Gohr    /**
7355de3a6a5SAndreas Gohr     * Check if the given config strings are set
7365de3a6a5SAndreas Gohr     *
7375de3a6a5SAndreas Gohr     * @param string[] $keys
7385de3a6a5SAndreas Gohr     * @return  bool
73931a58abaSAndreas Gohr     * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
74031a58abaSAndreas Gohr     *
7415de3a6a5SAndreas Gohr     */
7423213bf4eSAndreas Gohr    protected function checkConfig($keys)
7433213bf4eSAndreas Gohr    {
7445de3a6a5SAndreas Gohr        foreach ($keys as $key) {
745ad4d5631SAndreas Gohr            $params = explode(':', $key);
746ad4d5631SAndreas Gohr            $key = array_shift($params);
747ad4d5631SAndreas Gohr            $sql = trim($this->getConf($key));
748ad4d5631SAndreas Gohr
749ad4d5631SAndreas Gohr            // check if sql is set
750ad4d5631SAndreas Gohr            if (!$sql) return false;
751ad4d5631SAndreas Gohr            // check if needed params are there
752ad4d5631SAndreas Gohr            foreach ($params as $param) {
753ad4d5631SAndreas Gohr                if (strpos($sql, ":$param") === false) return false;
754ad4d5631SAndreas Gohr            }
7555de3a6a5SAndreas Gohr        }
7565de3a6a5SAndreas Gohr
7575de3a6a5SAndreas Gohr        return true;
7585de3a6a5SAndreas Gohr    }
7595de3a6a5SAndreas Gohr
7605de3a6a5SAndreas Gohr    /**
7615de3a6a5SAndreas Gohr     * create an approximation of the SQL string with parameters replaced
7625de3a6a5SAndreas Gohr     *
7635de3a6a5SAndreas Gohr     * @param string $sql
7645de3a6a5SAndreas Gohr     * @param array $params
7655de3a6a5SAndreas Gohr     * @param bool $htmlescape Should the result be escaped for output in HTML?
7665de3a6a5SAndreas Gohr     * @return string
7675de3a6a5SAndreas Gohr     */
7683213bf4eSAndreas Gohr    protected function debugSQL($sql, $params, $htmlescape = true)
7693213bf4eSAndreas Gohr    {
7705de3a6a5SAndreas Gohr        foreach ($params as $key => $val) {
7715de3a6a5SAndreas Gohr            if (is_int($val)) {
7725de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_INT);
7735de3a6a5SAndreas Gohr            } elseif (is_bool($val)) {
7745de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_BOOL);
7755de3a6a5SAndreas Gohr            } elseif (is_null($val)) {
7765de3a6a5SAndreas Gohr                $val = 'NULL';
7775de3a6a5SAndreas Gohr            } else {
7785de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val);
7795de3a6a5SAndreas Gohr            }
7805de3a6a5SAndreas Gohr            $sql = str_replace($key, $val, $sql);
7815de3a6a5SAndreas Gohr        }
7825de3a6a5SAndreas Gohr        if ($htmlescape) $sql = hsc($sql);
7835de3a6a5SAndreas Gohr        return $sql;
7845de3a6a5SAndreas Gohr    }
785f64dbc90SAndreas Gohr}
786f64dbc90SAndreas Gohr
787f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et:
788