xref: /dokuwiki/lib/plugins/authpdo/auth.php (revision 31a58aba4c24b34c34ad5764d1a35b7c398c3a2c)
1f64dbc90SAndreas Gohr<?php
2f64dbc90SAndreas Gohr/**
3f64dbc90SAndreas Gohr * DokuWiki Plugin authpdo (Auth Component)
4f64dbc90SAndreas Gohr *
5f64dbc90SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6f64dbc90SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
7f64dbc90SAndreas Gohr */
8f64dbc90SAndreas Gohr
90d586afdSAndreas Gohr/**
100d586afdSAndreas Gohr * Class auth_plugin_authpdo
110d586afdSAndreas Gohr */
123213bf4eSAndreas Gohrclass auth_plugin_authpdo extends DokuWiki_Auth_Plugin
133213bf4eSAndreas Gohr{
14f64dbc90SAndreas Gohr
15f64dbc90SAndreas Gohr    /** @var PDO */
16f64dbc90SAndreas Gohr    protected $pdo;
17f64dbc90SAndreas Gohr
180cec3e2aSAndreas Gohr    /** @var null|array The list of all groups */
190cec3e2aSAndreas Gohr    protected $groupcache = null;
200cec3e2aSAndreas Gohr
21f64dbc90SAndreas Gohr    /**
22f64dbc90SAndreas Gohr     * Constructor.
23f64dbc90SAndreas Gohr     */
243213bf4eSAndreas Gohr    public function __construct()
253213bf4eSAndreas Gohr    {
26f64dbc90SAndreas Gohr        parent::__construct(); // for compatibility
27f64dbc90SAndreas Gohr
28f64dbc90SAndreas Gohr        if (!class_exists('PDO')) {
293213bf4eSAndreas Gohr            $this->debugMsg('PDO extension for PHP not found.', -1, __LINE__);
30f64dbc90SAndreas Gohr            $this->success = false;
31f64dbc90SAndreas Gohr            return;
32f64dbc90SAndreas Gohr        }
33f64dbc90SAndreas Gohr
34f64dbc90SAndreas Gohr        if (!$this->getConf('dsn')) {
353213bf4eSAndreas Gohr            $this->debugMsg('No DSN specified', -1, __LINE__);
36f64dbc90SAndreas Gohr            $this->success = false;
37f64dbc90SAndreas Gohr            return;
38f64dbc90SAndreas Gohr        }
39f64dbc90SAndreas Gohr
40f64dbc90SAndreas Gohr        try {
41f64dbc90SAndreas Gohr            $this->pdo = new PDO(
42f64dbc90SAndreas Gohr                $this->getConf('dsn'),
43f64dbc90SAndreas Gohr                $this->getConf('user'),
440cc11d97SAndreas Gohr                conf_decodeString($this->getConf('pass')),
45f64dbc90SAndreas Gohr                array(
4670a89417SAndreas Gohr                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array
4770a89417SAndreas Gohr                    PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names
485de3a6a5SAndreas Gohr                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
49f64dbc90SAndreas Gohr                )
50f64dbc90SAndreas Gohr            );
51f64dbc90SAndreas Gohr        } catch (PDOException $e) {
523213bf4eSAndreas Gohr            $this->debugMsg($e);
53c27579a6SAndreas Gohr            msg($this->getLang('connectfail'), -1);
54f64dbc90SAndreas Gohr            $this->success = false;
55f64dbc90SAndreas Gohr            return;
56f64dbc90SAndreas Gohr        }
57f64dbc90SAndreas Gohr
580d586afdSAndreas Gohr        // can Users be created?
593213bf4eSAndreas Gohr        $this->cando['addUser'] = $this->checkConfig(
600d586afdSAndreas Gohr            array(
610d586afdSAndreas Gohr                'select-user',
620d586afdSAndreas Gohr                'select-user-groups',
630d586afdSAndreas Gohr                'select-groups',
640d586afdSAndreas Gohr                'insert-user',
650d586afdSAndreas Gohr                'insert-group',
660d586afdSAndreas Gohr                'join-group'
670d586afdSAndreas Gohr            )
680d586afdSAndreas Gohr        );
69f64dbc90SAndreas Gohr
700d586afdSAndreas Gohr        // can Users be deleted?
713213bf4eSAndreas Gohr        $this->cando['delUser'] = $this->checkConfig(
720d586afdSAndreas Gohr            array(
730d586afdSAndreas Gohr                'select-user',
740d586afdSAndreas Gohr                'select-user-groups',
750d586afdSAndreas Gohr                'select-groups',
767f89f089SAndreas Gohr                'leave-group',
777f89f089SAndreas Gohr                'delete-user'
780d586afdSAndreas Gohr            )
790d586afdSAndreas Gohr        );
800d586afdSAndreas Gohr
810d586afdSAndreas Gohr        // can login names be changed?
823213bf4eSAndreas Gohr        $this->cando['modLogin'] = $this->checkConfig(
830d586afdSAndreas Gohr            array(
840d586afdSAndreas Gohr                'select-user',
850d586afdSAndreas Gohr                'select-user-groups',
860d586afdSAndreas Gohr                'update-user-login'
870d586afdSAndreas Gohr            )
880d586afdSAndreas Gohr        );
890d586afdSAndreas Gohr
900d586afdSAndreas Gohr        // can passwords be changed?
913213bf4eSAndreas Gohr        $this->cando['modPass'] = $this->checkConfig(
920d586afdSAndreas Gohr            array(
930d586afdSAndreas Gohr                'select-user',
940d586afdSAndreas Gohr                'select-user-groups',
950d586afdSAndreas Gohr                'update-user-pass'
960d586afdSAndreas Gohr            )
970d586afdSAndreas Gohr        );
980d586afdSAndreas Gohr
99ad4d5631SAndreas Gohr        // can real names be changed?
1003213bf4eSAndreas Gohr        $this->cando['modName'] = $this->checkConfig(
1010d586afdSAndreas Gohr            array(
1020d586afdSAndreas Gohr                'select-user',
1030d586afdSAndreas Gohr                'select-user-groups',
104ad4d5631SAndreas Gohr                'update-user-info:name'
105ad4d5631SAndreas Gohr            )
106ad4d5631SAndreas Gohr        );
107ad4d5631SAndreas Gohr
108ad4d5631SAndreas Gohr        // can real email be changed?
1093213bf4eSAndreas Gohr        $this->cando['modMail'] = $this->checkConfig(
110ad4d5631SAndreas Gohr            array(
111ad4d5631SAndreas Gohr                'select-user',
112ad4d5631SAndreas Gohr                'select-user-groups',
113ad4d5631SAndreas Gohr                'update-user-info:mail'
1140d586afdSAndreas Gohr            )
1150d586afdSAndreas Gohr        );
1160d586afdSAndreas Gohr
1170d586afdSAndreas Gohr        // can groups be changed?
1183213bf4eSAndreas Gohr        $this->cando['modGroups'] = $this->checkConfig(
1190d586afdSAndreas Gohr            array(
1200d586afdSAndreas Gohr                'select-user',
1210d586afdSAndreas Gohr                'select-user-groups',
1220d586afdSAndreas Gohr                'select-groups',
1230d586afdSAndreas Gohr                'leave-group',
1240d586afdSAndreas Gohr                'join-group',
1250d586afdSAndreas Gohr                'insert-group'
1260d586afdSAndreas Gohr            )
1270d586afdSAndreas Gohr        );
1280d586afdSAndreas Gohr
1290d586afdSAndreas Gohr        // can a filtered list of users be retrieved?
1303213bf4eSAndreas Gohr        $this->cando['getUsers'] = $this->checkConfig(
1310d586afdSAndreas Gohr            array(
1320d586afdSAndreas Gohr                'list-users'
1330d586afdSAndreas Gohr            )
1340d586afdSAndreas Gohr        );
1350d586afdSAndreas Gohr
1360d586afdSAndreas Gohr        // can the number of users be retrieved?
1373213bf4eSAndreas Gohr        $this->cando['getUserCount'] = $this->checkConfig(
1380d586afdSAndreas Gohr            array(
1390d586afdSAndreas Gohr                'count-users'
1400d586afdSAndreas Gohr            )
1410d586afdSAndreas Gohr        );
1420d586afdSAndreas Gohr
1430d586afdSAndreas Gohr        // can a list of available groups be retrieved?
1443213bf4eSAndreas Gohr        $this->cando['getGroups'] = $this->checkConfig(
1450d586afdSAndreas Gohr            array(
1460d586afdSAndreas Gohr                'select-groups'
1470d586afdSAndreas Gohr            )
1480d586afdSAndreas Gohr        );
1490d586afdSAndreas Gohr
150f64dbc90SAndreas Gohr        $this->success = true;
151f64dbc90SAndreas Gohr    }
152f64dbc90SAndreas Gohr
153f64dbc90SAndreas Gohr    /**
154f64dbc90SAndreas Gohr     * Check user+password
155f64dbc90SAndreas Gohr     *
156f64dbc90SAndreas Gohr     * @param string $user the user name
157f64dbc90SAndreas Gohr     * @param string $pass the clear text password
158f64dbc90SAndreas Gohr     * @return  bool
159f64dbc90SAndreas Gohr     */
1603213bf4eSAndreas Gohr    public function checkPass($user, $pass)
1613213bf4eSAndreas Gohr    {
162f64dbc90SAndreas Gohr
1633213bf4eSAndreas Gohr        $userdata = $this->selectUser($user);
164397d62a2SAndreas Gohr        if ($userdata == false) return false;
165f64dbc90SAndreas Gohr
166397d62a2SAndreas Gohr        // password checking done in SQL?
1673213bf4eSAndreas Gohr        if ($this->checkConfig(array('check-pass'))) {
168397d62a2SAndreas Gohr            $userdata['clear'] = $pass;
169397d62a2SAndreas Gohr            $userdata['hash'] = auth_cryptPassword($pass);
1703213bf4eSAndreas Gohr            $result = $this->query($this->getConf('check-pass'), $userdata);
171397d62a2SAndreas Gohr            if ($result === false) return false;
172397d62a2SAndreas Gohr            return (count($result) == 1);
173397d62a2SAndreas Gohr        }
174397d62a2SAndreas Gohr
175397d62a2SAndreas Gohr        // we do password checking on our own
176397d62a2SAndreas Gohr        if (isset($userdata['hash'])) {
177f64dbc90SAndreas Gohr            // hashed password
178c3cc6e05SAndreas Gohr            $passhash = new \dokuwiki\PassHash();
179397d62a2SAndreas Gohr            return $passhash->verify_hash($pass, $userdata['hash']);
180f64dbc90SAndreas Gohr        } else {
181f64dbc90SAndreas Gohr            // clear text password in the database O_o
182d5c0422fSAndreas Gohr            return ($pass === $userdata['clear']);
183f64dbc90SAndreas Gohr        }
184f64dbc90SAndreas Gohr    }
185f64dbc90SAndreas Gohr
186f64dbc90SAndreas Gohr    /**
187f64dbc90SAndreas Gohr     * Return user info
188f64dbc90SAndreas Gohr     *
189f64dbc90SAndreas Gohr     * Returns info about the given user needs to contain
190f64dbc90SAndreas Gohr     * at least these fields:
191f64dbc90SAndreas Gohr     *
192f64dbc90SAndreas Gohr     * name string  full name of the user
193f64dbc90SAndreas Gohr     * mail string  email addres of the user
194f64dbc90SAndreas Gohr     * grps array   list of groups the user is in
195f64dbc90SAndreas Gohr     *
196f64dbc90SAndreas Gohr     * @param string $user the user name
197f64dbc90SAndreas Gohr     * @param bool $requireGroups whether or not the returned data must include groups
198358942b5SAndreas Gohr     * @return array|bool containing user data or false
199f64dbc90SAndreas Gohr     */
2003213bf4eSAndreas Gohr    public function getUserData($user, $requireGroups = true)
2013213bf4eSAndreas Gohr    {
2023213bf4eSAndreas Gohr        $data = $this->selectUser($user);
203f64dbc90SAndreas Gohr        if ($data == false) return false;
204f64dbc90SAndreas Gohr
20570a89417SAndreas Gohr        if (isset($data['hash'])) unset($data['hash']);
20670a89417SAndreas Gohr        if (isset($data['clean'])) unset($data['clean']);
207f64dbc90SAndreas Gohr
20870a89417SAndreas Gohr        if ($requireGroups) {
2093213bf4eSAndreas Gohr            $data['grps'] = $this->selectUserGroups($data);
2105de3a6a5SAndreas Gohr            if ($data['grps'] === false) return false;
211f64dbc90SAndreas Gohr        }
212f64dbc90SAndreas Gohr
213f64dbc90SAndreas Gohr        return $data;
214f64dbc90SAndreas Gohr    }
215f64dbc90SAndreas Gohr
216f64dbc90SAndreas Gohr    /**
217f64dbc90SAndreas Gohr     * Create a new User [implement only where required/possible]
218f64dbc90SAndreas Gohr     *
219f64dbc90SAndreas Gohr     * Returns false if the user already exists, null when an error
220f64dbc90SAndreas Gohr     * occurred and true if everything went well.
221f64dbc90SAndreas Gohr     *
222f64dbc90SAndreas Gohr     * The new user HAS TO be added to the default group by this
223f64dbc90SAndreas Gohr     * function!
224f64dbc90SAndreas Gohr     *
225f64dbc90SAndreas Gohr     * Set addUser capability when implemented
226f64dbc90SAndreas Gohr     *
227f64dbc90SAndreas Gohr     * @param string $user
2285de3a6a5SAndreas Gohr     * @param string $clear
229f64dbc90SAndreas Gohr     * @param string $name
230f64dbc90SAndreas Gohr     * @param string $mail
231f64dbc90SAndreas Gohr     * @param null|array $grps
232f64dbc90SAndreas Gohr     * @return bool|null
233f64dbc90SAndreas Gohr     */
2343213bf4eSAndreas Gohr    public function createUser($user, $clear, $name, $mail, $grps = null)
2353213bf4eSAndreas Gohr    {
2365de3a6a5SAndreas Gohr        global $conf;
2375de3a6a5SAndreas Gohr
2385de3a6a5SAndreas Gohr        if (($info = $this->getUserData($user, false)) !== false) {
2395de3a6a5SAndreas Gohr            msg($this->getLang('userexists'), -1);
2405de3a6a5SAndreas Gohr            return false; // user already exists
2415de3a6a5SAndreas Gohr        }
2425de3a6a5SAndreas Gohr
2435de3a6a5SAndreas Gohr        // prepare data
2445de3a6a5SAndreas Gohr        if ($grps == null) $grps = array();
24507a11e2aSAndreas Gohr        array_unshift($grps, $conf['defaultgroup']);
2465de3a6a5SAndreas Gohr        $grps = array_unique($grps);
2475de3a6a5SAndreas Gohr        $hash = auth_cryptPassword($clear);
2485de3a6a5SAndreas Gohr        $userdata = compact('user', 'clear', 'hash', 'name', 'mail');
2495de3a6a5SAndreas Gohr
2505de3a6a5SAndreas Gohr        // action protected by transaction
2515de3a6a5SAndreas Gohr        $this->pdo->beginTransaction();
2525de3a6a5SAndreas Gohr        {
2535de3a6a5SAndreas Gohr            // insert the user
2543213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('insert-user'), $userdata);
2555de3a6a5SAndreas Gohr            if ($ok === false) goto FAIL;
2565de3a6a5SAndreas Gohr            $userdata = $this->getUserData($user, false);
2575de3a6a5SAndreas Gohr            if ($userdata === false) goto FAIL;
2585de3a6a5SAndreas Gohr
2595de3a6a5SAndreas Gohr            // create all groups that do not exist, the refetch the groups
2603213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
2615de3a6a5SAndreas Gohr            foreach ($grps as $group) {
2625de3a6a5SAndreas Gohr                if (!isset($allgroups[$group])) {
2636459f496SAndreas Gohr                    $ok = $this->addGroup($group);
2645de3a6a5SAndreas Gohr                    if ($ok === false) goto FAIL;
2655de3a6a5SAndreas Gohr                }
2665de3a6a5SAndreas Gohr            }
2673213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
2685de3a6a5SAndreas Gohr
2695de3a6a5SAndreas Gohr            // add user to the groups
2705de3a6a5SAndreas Gohr            foreach ($grps as $group) {
2713213bf4eSAndreas Gohr                $ok = $this->joinGroup($userdata, $allgroups[$group]);
2725de3a6a5SAndreas Gohr                if ($ok === false) goto FAIL;
2735de3a6a5SAndreas Gohr            }
2745de3a6a5SAndreas Gohr        }
2755de3a6a5SAndreas Gohr        $this->pdo->commit();
2765de3a6a5SAndreas Gohr        return true;
2775de3a6a5SAndreas Gohr
2785de3a6a5SAndreas Gohr        // something went wrong, rollback
2795de3a6a5SAndreas Gohr        FAIL:
2805de3a6a5SAndreas Gohr        $this->pdo->rollBack();
2813213bf4eSAndreas Gohr        $this->debugMsg('Transaction rolled back', 0, __LINE__);
282c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
2835de3a6a5SAndreas Gohr        return null; // return error
2845de3a6a5SAndreas Gohr    }
285f64dbc90SAndreas Gohr
286f64dbc90SAndreas Gohr    /**
2874fb8dfabSAndreas Gohr     * Modify user data
288f64dbc90SAndreas Gohr     *
289f64dbc90SAndreas Gohr     * @param string $user nick of the user to be changed
290f64dbc90SAndreas Gohr     * @param array $changes array of field/value pairs to be changed (password will be clear text)
291f64dbc90SAndreas Gohr     * @return  bool
292f64dbc90SAndreas Gohr     */
2933213bf4eSAndreas Gohr    public function modifyUser($user, $changes)
2943213bf4eSAndreas Gohr    {
2954fb8dfabSAndreas Gohr        // secure everything in transaction
2964fb8dfabSAndreas Gohr        $this->pdo->beginTransaction();
2974fb8dfabSAndreas Gohr        {
2984fb8dfabSAndreas Gohr            $olddata = $this->getUserData($user);
2994fb8dfabSAndreas Gohr            $oldgroups = $olddata['grps'];
3004fb8dfabSAndreas Gohr            unset($olddata['grps']);
3014fb8dfabSAndreas Gohr
3024fb8dfabSAndreas Gohr            // changing the user name?
3034fb8dfabSAndreas Gohr            if (isset($changes['user'])) {
3044fb8dfabSAndreas Gohr                if ($this->getUserData($changes['user'], false)) goto FAIL;
3054fb8dfabSAndreas Gohr                $params = $olddata;
3064fb8dfabSAndreas Gohr                $params['newlogin'] = $changes['user'];
3074fb8dfabSAndreas Gohr
3083213bf4eSAndreas Gohr                $ok = $this->query($this->getConf('update-user-login'), $params);
3094fb8dfabSAndreas Gohr                if ($ok === false) goto FAIL;
3104fb8dfabSAndreas Gohr            }
3114fb8dfabSAndreas Gohr
3124fb8dfabSAndreas Gohr            // changing the password?
3134fb8dfabSAndreas Gohr            if (isset($changes['pass'])) {
3144fb8dfabSAndreas Gohr                $params = $olddata;
3154fb8dfabSAndreas Gohr                $params['clear'] = $changes['pass'];
3164fb8dfabSAndreas Gohr                $params['hash'] = auth_cryptPassword($changes['pass']);
3174fb8dfabSAndreas Gohr
3183213bf4eSAndreas Gohr                $ok = $this->query($this->getConf('update-user-pass'), $params);
3194fb8dfabSAndreas Gohr                if ($ok === false) goto FAIL;
3204fb8dfabSAndreas Gohr            }
3214fb8dfabSAndreas Gohr
3224fb8dfabSAndreas Gohr            // changing info?
3234fb8dfabSAndreas Gohr            if (isset($changes['mail']) || isset($changes['name'])) {
3244fb8dfabSAndreas Gohr                $params = $olddata;
3254fb8dfabSAndreas Gohr                if (isset($changes['mail'])) $params['mail'] = $changes['mail'];
3264fb8dfabSAndreas Gohr                if (isset($changes['name'])) $params['name'] = $changes['name'];
3274fb8dfabSAndreas Gohr
3283213bf4eSAndreas Gohr                $ok = $this->query($this->getConf('update-user-info'), $params);
3294fb8dfabSAndreas Gohr                if ($ok === false) goto FAIL;
3304fb8dfabSAndreas Gohr            }
3314fb8dfabSAndreas Gohr
3324fb8dfabSAndreas Gohr            // changing groups?
3334fb8dfabSAndreas Gohr            if (isset($changes['grps'])) {
3343213bf4eSAndreas Gohr                $allgroups = $this->selectGroups();
3354fb8dfabSAndreas Gohr
3364fb8dfabSAndreas Gohr                // remove membership for previous groups
3374fb8dfabSAndreas Gohr                foreach ($oldgroups as $group) {
338358942b5SAndreas Gohr                    if (!in_array($group, $changes['grps']) && isset($allgroups[$group])) {
3393213bf4eSAndreas Gohr                        $ok = $this->leaveGroup($olddata, $allgroups[$group]);
3404fb8dfabSAndreas Gohr                        if ($ok === false) goto FAIL;
3414fb8dfabSAndreas Gohr                    }
3424fb8dfabSAndreas Gohr                }
3434fb8dfabSAndreas Gohr
3444fb8dfabSAndreas Gohr                // create all new groups that are missing
3454fb8dfabSAndreas Gohr                $added = 0;
3464fb8dfabSAndreas Gohr                foreach ($changes['grps'] as $group) {
3474fb8dfabSAndreas Gohr                    if (!isset($allgroups[$group])) {
3486459f496SAndreas Gohr                        $ok = $this->addGroup($group);
3494fb8dfabSAndreas Gohr                        if ($ok === false) goto FAIL;
3504fb8dfabSAndreas Gohr                        $added++;
3514fb8dfabSAndreas Gohr                    }
3524fb8dfabSAndreas Gohr                }
3534fb8dfabSAndreas Gohr                // reload group info
3543213bf4eSAndreas Gohr                if ($added > 0) $allgroups = $this->selectGroups();
3554fb8dfabSAndreas Gohr
3564fb8dfabSAndreas Gohr                // add membership for new groups
3574fb8dfabSAndreas Gohr                foreach ($changes['grps'] as $group) {
3584fb8dfabSAndreas Gohr                    if (!in_array($group, $oldgroups)) {
3593213bf4eSAndreas Gohr                        $ok = $this->joinGroup($olddata, $allgroups[$group]);
3604fb8dfabSAndreas Gohr                        if ($ok === false) goto FAIL;
3614fb8dfabSAndreas Gohr                    }
3624fb8dfabSAndreas Gohr                }
3634fb8dfabSAndreas Gohr            }
3644fb8dfabSAndreas Gohr
3654fb8dfabSAndreas Gohr        }
3664fb8dfabSAndreas Gohr        $this->pdo->commit();
3674fb8dfabSAndreas Gohr        return true;
3684fb8dfabSAndreas Gohr
3694fb8dfabSAndreas Gohr        // something went wrong, rollback
3704fb8dfabSAndreas Gohr        FAIL:
3714fb8dfabSAndreas Gohr        $this->pdo->rollBack();
3723213bf4eSAndreas Gohr        $this->debugMsg('Transaction rolled back', 0, __LINE__);
373c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
3744fb8dfabSAndreas Gohr        return false; // return error
3754fb8dfabSAndreas Gohr    }
376f64dbc90SAndreas Gohr
377f64dbc90SAndreas Gohr    /**
378e19be516SAndreas Gohr     * Delete one or more users
379f64dbc90SAndreas Gohr     *
380f64dbc90SAndreas Gohr     * Set delUser capability when implemented
381f64dbc90SAndreas Gohr     *
382f64dbc90SAndreas Gohr     * @param array $users
383f64dbc90SAndreas Gohr     * @return  int    number of users deleted
384f64dbc90SAndreas Gohr     */
3853213bf4eSAndreas Gohr    public function deleteUsers($users)
3863213bf4eSAndreas Gohr    {
387e19be516SAndreas Gohr        $count = 0;
388e19be516SAndreas Gohr        foreach ($users as $user) {
3893213bf4eSAndreas Gohr            if ($this->deleteUser($user)) $count++;
390e19be516SAndreas Gohr        }
391e19be516SAndreas Gohr        return $count;
392e19be516SAndreas Gohr    }
393f64dbc90SAndreas Gohr
394f64dbc90SAndreas Gohr    /**
395f64dbc90SAndreas Gohr     * Bulk retrieval of user data [implement only where required/possible]
396f64dbc90SAndreas Gohr     *
397f64dbc90SAndreas Gohr     * Set getUsers capability when implemented
398f64dbc90SAndreas Gohr     *
399f64dbc90SAndreas Gohr     * @param int $start index of first user to be returned
400f64dbc90SAndreas Gohr     * @param int $limit max number of users to be returned
401f64dbc90SAndreas Gohr     * @param array $filter array of field/pattern pairs, null for no filter
402f64dbc90SAndreas Gohr     * @return  array list of userinfo (refer getUserData for internal userinfo details)
403f64dbc90SAndreas Gohr     */
4043213bf4eSAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = null)
4053213bf4eSAndreas Gohr    {
4066459f496SAndreas Gohr        if ($limit < 0) $limit = 10000; // we don't support no limit
4076459f496SAndreas Gohr        if (is_null($filter)) $filter = array();
4086459f496SAndreas Gohr
40912c7f5c3SAndreas Gohr        if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
4106459f496SAndreas Gohr        foreach (array('user', 'name', 'mail', 'group') as $key) {
4116459f496SAndreas Gohr            if (!isset($filter[$key])) {
4126459f496SAndreas Gohr                $filter[$key] = '%';
4136459f496SAndreas Gohr            } else {
4146459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
4156459f496SAndreas Gohr            }
4166459f496SAndreas Gohr        }
41714119d44SAndreas Gohr        $filter['start'] = (int)$start;
41814119d44SAndreas Gohr        $filter['end'] = (int)$start + $limit;
41914119d44SAndreas Gohr        $filter['limit'] = (int)$limit;
4206459f496SAndreas Gohr
4213213bf4eSAndreas Gohr        $result = $this->query($this->getConf('list-users'), $filter);
4226459f496SAndreas Gohr        if (!$result) return array();
4236459f496SAndreas Gohr        $users = array();
42488ca2487SPhy        if (is_array($result)) {
4256459f496SAndreas Gohr            foreach ($result as $row) {
4266459f496SAndreas Gohr                if (!isset($row['user'])) {
427*31a58abaSAndreas Gohr                    $this->debugMsg("list-users statement did not return 'user' attribute", -1, __LINE__);
4286459f496SAndreas Gohr                    return array();
4296459f496SAndreas Gohr                }
4303e2a8145SAndreas Gohr                $users[] = $this->getUserData($row['user']);
4316459f496SAndreas Gohr            }
43288ca2487SPhy        } else {
433*31a58abaSAndreas Gohr            $this->debugMsg("list-users statement did not return a list of result", -1, __LINE__);
43488ca2487SPhy        }
4356459f496SAndreas Gohr        return $users;
4366459f496SAndreas Gohr    }
437f64dbc90SAndreas Gohr
438f64dbc90SAndreas Gohr    /**
439f64dbc90SAndreas Gohr     * Return a count of the number of user which meet $filter criteria
440f64dbc90SAndreas Gohr     *
441f64dbc90SAndreas Gohr     * @param array $filter array of field/pattern pairs, empty array for no filter
442f64dbc90SAndreas Gohr     * @return int
443f64dbc90SAndreas Gohr     */
4443213bf4eSAndreas Gohr    public function getUserCount($filter = array())
4453213bf4eSAndreas Gohr    {
4466459f496SAndreas Gohr        if (is_null($filter)) $filter = array();
4476459f496SAndreas Gohr
44812c7f5c3SAndreas Gohr        if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
4496459f496SAndreas Gohr        foreach (array('user', 'name', 'mail', 'group') as $key) {
4506459f496SAndreas Gohr            if (!isset($filter[$key])) {
4516459f496SAndreas Gohr                $filter[$key] = '%';
4526459f496SAndreas Gohr            } else {
4536459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
4546459f496SAndreas Gohr            }
4556459f496SAndreas Gohr        }
4566459f496SAndreas Gohr
4573213bf4eSAndreas Gohr        $result = $this->query($this->getConf('count-users'), $filter);
4586459f496SAndreas Gohr        if (!$result || !isset($result[0]['count'])) {
4593213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'count' attribute", -1, __LINE__);
4606459f496SAndreas Gohr        }
461f3c1c207SAndreas Gohr        return (int)$result[0]['count'];
4626459f496SAndreas Gohr    }
463f64dbc90SAndreas Gohr
464f64dbc90SAndreas Gohr    /**
4656459f496SAndreas Gohr     * Create a new group with the given name
466f64dbc90SAndreas Gohr     *
467f64dbc90SAndreas Gohr     * @param string $group
468f64dbc90SAndreas Gohr     * @return bool
469f64dbc90SAndreas Gohr     */
4703213bf4eSAndreas Gohr    public function addGroup($group)
4713213bf4eSAndreas Gohr    {
4726459f496SAndreas Gohr        $sql = $this->getConf('insert-group');
4736459f496SAndreas Gohr
4743213bf4eSAndreas Gohr        $result = $this->query($sql, array(':group' => $group));
4753213bf4eSAndreas Gohr        $this->clearGroupCache();
4766459f496SAndreas Gohr        if ($result === false) return false;
4776459f496SAndreas Gohr        return true;
4786459f496SAndreas Gohr    }
479f64dbc90SAndreas Gohr
480f64dbc90SAndreas Gohr    /**
4815de3a6a5SAndreas Gohr     * Retrieve groups
482f64dbc90SAndreas Gohr     *
483f64dbc90SAndreas Gohr     * Set getGroups capability when implemented
484f64dbc90SAndreas Gohr     *
485f64dbc90SAndreas Gohr     * @param int $start
486f64dbc90SAndreas Gohr     * @param int $limit
487f64dbc90SAndreas Gohr     * @return  array
488f64dbc90SAndreas Gohr     */
4893213bf4eSAndreas Gohr    public function retrieveGroups($start = 0, $limit = 0)
4903213bf4eSAndreas Gohr    {
4913213bf4eSAndreas Gohr        $groups = array_keys($this->selectGroups());
4925de3a6a5SAndreas Gohr        if ($groups === false) return array();
493f64dbc90SAndreas Gohr
4945de3a6a5SAndreas Gohr        if (!$limit) {
4955de3a6a5SAndreas Gohr            return array_splice($groups, $start);
4965de3a6a5SAndreas Gohr        } else {
4975de3a6a5SAndreas Gohr            return array_splice($groups, $start, $limit);
498f64dbc90SAndreas Gohr        }
499f64dbc90SAndreas Gohr    }
500f64dbc90SAndreas Gohr
501f64dbc90SAndreas Gohr    /**
502f64dbc90SAndreas Gohr     * Select data of a specified user
503f64dbc90SAndreas Gohr     *
5045de3a6a5SAndreas Gohr     * @param string $user the user name
5055de3a6a5SAndreas Gohr     * @return bool|array user data, false on error
506f64dbc90SAndreas Gohr     */
5073213bf4eSAndreas Gohr    protected function selectUser($user)
5083213bf4eSAndreas Gohr    {
509f64dbc90SAndreas Gohr        $sql = $this->getConf('select-user');
510f64dbc90SAndreas Gohr
5113213bf4eSAndreas Gohr        $result = $this->query($sql, array(':user' => $user));
51270a89417SAndreas Gohr        if (!$result) return false;
513f64dbc90SAndreas Gohr
51470a89417SAndreas Gohr        if (count($result) > 1) {
5153213bf4eSAndreas Gohr            $this->debugMsg('Found more than one matching user', -1, __LINE__);
516f64dbc90SAndreas Gohr            return false;
517f64dbc90SAndreas Gohr        }
518f64dbc90SAndreas Gohr
519f64dbc90SAndreas Gohr        $data = array_shift($result);
520f64dbc90SAndreas Gohr        $dataok = true;
521f64dbc90SAndreas Gohr
522f64dbc90SAndreas Gohr        if (!isset($data['user'])) {
5233213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'user' attribute", -1, __LINE__);
524f64dbc90SAndreas Gohr            $dataok = false;
525f64dbc90SAndreas Gohr        }
5263213bf4eSAndreas Gohr        if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(array('check-pass'))) {
5273213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
528f64dbc90SAndreas Gohr            $dataok = false;
529f64dbc90SAndreas Gohr        }
530f64dbc90SAndreas Gohr        if (!isset($data['name'])) {
5313213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'name' attribute", -1, __LINE__);
532f64dbc90SAndreas Gohr            $dataok = false;
533f64dbc90SAndreas Gohr        }
534f64dbc90SAndreas Gohr        if (!isset($data['mail'])) {
5353213bf4eSAndreas Gohr            $this->debugMsg("Statement did not return 'mail' attribute", -1, __LINE__);
536f64dbc90SAndreas Gohr            $dataok = false;
537f64dbc90SAndreas Gohr        }
538f64dbc90SAndreas Gohr
539f64dbc90SAndreas Gohr        if (!$dataok) return false;
540f64dbc90SAndreas Gohr        return $data;
541f64dbc90SAndreas Gohr    }
542f64dbc90SAndreas Gohr
543f64dbc90SAndreas Gohr    /**
544e19be516SAndreas Gohr     * Delete a user after removing all their group memberships
545e19be516SAndreas Gohr     *
546e19be516SAndreas Gohr     * @param string $user
547e19be516SAndreas Gohr     * @return bool true when the user was deleted
548e19be516SAndreas Gohr     */
5493213bf4eSAndreas Gohr    protected function deleteUser($user)
5503213bf4eSAndreas Gohr    {
551e19be516SAndreas Gohr        $this->pdo->beginTransaction();
552e19be516SAndreas Gohr        {
553e19be516SAndreas Gohr            $userdata = $this->getUserData($user);
554e19be516SAndreas Gohr            if ($userdata === false) goto FAIL;
5553213bf4eSAndreas Gohr            $allgroups = $this->selectGroups();
556e19be516SAndreas Gohr
557e19be516SAndreas Gohr            // remove group memberships (ignore errors)
558e19be516SAndreas Gohr            foreach ($userdata['grps'] as $group) {
559358942b5SAndreas Gohr                if (isset($allgroups[$group])) {
5603213bf4eSAndreas Gohr                    $this->leaveGroup($userdata, $allgroups[$group]);
561e19be516SAndreas Gohr                }
562358942b5SAndreas Gohr            }
563e19be516SAndreas Gohr
5643213bf4eSAndreas Gohr            $ok = $this->query($this->getConf('delete-user'), $userdata);
565e19be516SAndreas Gohr            if ($ok === false) goto FAIL;
566e19be516SAndreas Gohr        }
567e19be516SAndreas Gohr        $this->pdo->commit();
568e19be516SAndreas Gohr        return true;
569e19be516SAndreas Gohr
570e19be516SAndreas Gohr        FAIL:
571e19be516SAndreas Gohr        $this->pdo->rollBack();
572e19be516SAndreas Gohr        return false;
573e19be516SAndreas Gohr    }
574e19be516SAndreas Gohr
575e19be516SAndreas Gohr    /**
57670a89417SAndreas Gohr     * Select all groups of a user
57770a89417SAndreas Gohr     *
57870a89417SAndreas Gohr     * @param array $userdata The userdata as returned by _selectUser()
5795de3a6a5SAndreas Gohr     * @return array|bool list of group names, false on error
58070a89417SAndreas Gohr     */
5813213bf4eSAndreas Gohr    protected function selectUserGroups($userdata)
5823213bf4eSAndreas Gohr    {
58370a89417SAndreas Gohr        global $conf;
58470a89417SAndreas Gohr        $sql = $this->getConf('select-user-groups');
5853213bf4eSAndreas Gohr        $result = $this->query($sql, $userdata);
5865de3a6a5SAndreas Gohr        if ($result === false) return false;
58770a89417SAndreas Gohr
58870a89417SAndreas Gohr        $groups = array($conf['defaultgroup']); // always add default config
58988ca2487SPhy        if (is_array($result)) {
5905de3a6a5SAndreas Gohr            foreach ($result as $row) {
5915de3a6a5SAndreas Gohr                if (!isset($row['group'])) {
592*31a58abaSAndreas Gohr                    $this->debugMsg("No 'group' field returned in select-user-groups statement", -1, __LINE__);
5935de3a6a5SAndreas Gohr                    return false;
5945de3a6a5SAndreas Gohr                }
59570a89417SAndreas Gohr                $groups[] = $row['group'];
59670a89417SAndreas Gohr            }
59788ca2487SPhy        } else {
598*31a58abaSAndreas Gohr            $this->debugMsg("select-user-groups statement did not return a list of result", -1, __LINE__);
59988ca2487SPhy        }
60070a89417SAndreas Gohr
60170a89417SAndreas Gohr        $groups = array_unique($groups);
60270a89417SAndreas Gohr        sort($groups);
60370a89417SAndreas Gohr        return $groups;
60470a89417SAndreas Gohr    }
60570a89417SAndreas Gohr
60670a89417SAndreas Gohr    /**
6075de3a6a5SAndreas Gohr     * Select all available groups
6085de3a6a5SAndreas Gohr     *
6095de3a6a5SAndreas Gohr     * @return array|bool list of all available groups and their properties
6105de3a6a5SAndreas Gohr     */
6113213bf4eSAndreas Gohr    protected function selectGroups()
6123213bf4eSAndreas Gohr    {
6130cec3e2aSAndreas Gohr        if ($this->groupcache) return $this->groupcache;
6140cec3e2aSAndreas Gohr
6155de3a6a5SAndreas Gohr        $sql = $this->getConf('select-groups');
6163213bf4eSAndreas Gohr        $result = $this->query($sql);
6175de3a6a5SAndreas Gohr        if ($result === false) return false;
6185de3a6a5SAndreas Gohr
6195de3a6a5SAndreas Gohr        $groups = array();
62088ca2487SPhy        if (is_array($result)) {
6215de3a6a5SAndreas Gohr            foreach ($result as $row) {
6225de3a6a5SAndreas Gohr                if (!isset($row['group'])) {
6233213bf4eSAndreas Gohr                    $this->debugMsg("No 'group' field returned from select-groups statement", -1, __LINE__);
6245de3a6a5SAndreas Gohr                    return false;
6255de3a6a5SAndreas Gohr                }
6265de3a6a5SAndreas Gohr
6275de3a6a5SAndreas Gohr                // relayout result with group name as key
6285de3a6a5SAndreas Gohr                $group = $row['group'];
6295de3a6a5SAndreas Gohr                $groups[$group] = $row;
6305de3a6a5SAndreas Gohr            }
63188ca2487SPhy        } else {
632*31a58abaSAndreas Gohr            $this->debugMsg("select-groups statement did not return a list of result", -1, __LINE__);
63388ca2487SPhy        }
6345de3a6a5SAndreas Gohr
6355de3a6a5SAndreas Gohr        ksort($groups);
6365de3a6a5SAndreas Gohr        return $groups;
6375de3a6a5SAndreas Gohr    }
6385de3a6a5SAndreas Gohr
6395de3a6a5SAndreas Gohr    /**
6400cec3e2aSAndreas Gohr     * Remove all entries from the group cache
6410cec3e2aSAndreas Gohr     */
6423213bf4eSAndreas Gohr    protected function clearGroupCache()
6433213bf4eSAndreas Gohr    {
6440cec3e2aSAndreas Gohr        $this->groupcache = null;
6450cec3e2aSAndreas Gohr    }
6460cec3e2aSAndreas Gohr
6470cec3e2aSAndreas Gohr    /**
6484fb8dfabSAndreas Gohr     * Adds the user to the group
6495de3a6a5SAndreas Gohr     *
6505de3a6a5SAndreas Gohr     * @param array $userdata all the user data
6515de3a6a5SAndreas Gohr     * @param array $groupdata all the group data
6525de3a6a5SAndreas Gohr     * @return bool
6535de3a6a5SAndreas Gohr     */
6543213bf4eSAndreas Gohr    protected function joinGroup($userdata, $groupdata)
6553213bf4eSAndreas Gohr    {
6565de3a6a5SAndreas Gohr        $data = array_merge($userdata, $groupdata);
6575de3a6a5SAndreas Gohr        $sql = $this->getConf('join-group');
6583213bf4eSAndreas Gohr        $result = $this->query($sql, $data);
6595de3a6a5SAndreas Gohr        if ($result === false) return false;
6605de3a6a5SAndreas Gohr        return true;
6615de3a6a5SAndreas Gohr    }
6625de3a6a5SAndreas Gohr
6635de3a6a5SAndreas Gohr    /**
6644fb8dfabSAndreas Gohr     * Removes the user from the group
6654fb8dfabSAndreas Gohr     *
6664fb8dfabSAndreas Gohr     * @param array $userdata all the user data
6674fb8dfabSAndreas Gohr     * @param array $groupdata all the group data
6684fb8dfabSAndreas Gohr     * @return bool
6694fb8dfabSAndreas Gohr     */
6703213bf4eSAndreas Gohr    protected function leaveGroup($userdata, $groupdata)
6713213bf4eSAndreas Gohr    {
6724fb8dfabSAndreas Gohr        $data = array_merge($userdata, $groupdata);
6734fb8dfabSAndreas Gohr        $sql = $this->getConf('leave-group');
6743213bf4eSAndreas Gohr        $result = $this->query($sql, $data);
6754fb8dfabSAndreas Gohr        if ($result === false) return false;
6764fb8dfabSAndreas Gohr        return true;
6774fb8dfabSAndreas Gohr    }
6784fb8dfabSAndreas Gohr
6794fb8dfabSAndreas Gohr    /**
68070a89417SAndreas Gohr     * Executes a query
68170a89417SAndreas Gohr     *
68270a89417SAndreas Gohr     * @param string $sql The SQL statement to execute
68370a89417SAndreas Gohr     * @param array $arguments Named parameters to be used in the statement
684f695c447SAndreas Gohr     * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error
68570a89417SAndreas Gohr     */
6863213bf4eSAndreas Gohr    protected function query($sql, $arguments = array())
6873213bf4eSAndreas Gohr    {
688f695c447SAndreas Gohr        $sql = trim($sql);
6895de3a6a5SAndreas Gohr        if (empty($sql)) {
6903213bf4eSAndreas Gohr            $this->debugMsg('No SQL query given', -1, __LINE__);
6915de3a6a5SAndreas Gohr            return false;
6925de3a6a5SAndreas Gohr        }
6935de3a6a5SAndreas Gohr
69414119d44SAndreas Gohr        // execute
69570a89417SAndreas Gohr        $params = array();
69614119d44SAndreas Gohr        $sth = $this->pdo->prepare($sql);
69788ca2487SPhy        $result = false;
69814119d44SAndreas Gohr        try {
69914119d44SAndreas Gohr            // prepare parameters - we only use those that exist in the SQL
70070a89417SAndreas Gohr            foreach ($arguments as $key => $value) {
70170a89417SAndreas Gohr                if (is_array($value)) continue;
70270a89417SAndreas Gohr                if (is_object($value)) continue;
70370a89417SAndreas Gohr                if ($key[0] != ':') $key = ":$key"; // prefix with colon if needed
70414119d44SAndreas Gohr                if (strpos($sql, $key) === false) continue; // skip if parameter is missing
70514119d44SAndreas Gohr
70614119d44SAndreas Gohr                if (is_int($value)) {
70714119d44SAndreas Gohr                    $sth->bindValue($key, $value, PDO::PARAM_INT);
70814119d44SAndreas Gohr                } else {
70914119d44SAndreas Gohr                    $sth->bindValue($key, $value);
71014119d44SAndreas Gohr                }
711f6cd8a7fSphjanderson                $params[$key] = $value; //remember for debugging
71270a89417SAndreas Gohr            }
71370a89417SAndreas Gohr
71414119d44SAndreas Gohr            $sth->execute();
7156a1b9bfeSPhy            // only report last line's result
7166a1b9bfeSPhy            $hasnextrowset = true;
7176a1b9bfeSPhy            $currentsql = $sql;
7186a1b9bfeSPhy            while ($hasnextrowset) {
7196a1b9bfeSPhy                if (strtolower(substr($currentsql, 0, 6)) == 'select') {
72070a89417SAndreas Gohr                    $result = $sth->fetchAll();
721f695c447SAndreas Gohr                } else {
722f695c447SAndreas Gohr                    $result = $sth->rowCount();
723f695c447SAndreas Gohr                }
7246a1b9bfeSPhy                $semi_pos = strpos($currentsql, ';');
7256a1b9bfeSPhy                if ($semi_pos) {
7266a1b9bfeSPhy                    $currentsql = trim(substr($currentsql, $semi_pos + 1));
7276a1b9bfeSPhy                }
7286a1b9bfeSPhy                try {
7296a1b9bfeSPhy                    $hasnextrowset = $sth->nextRowset(); // run next rowset
7306a1b9bfeSPhy                } catch (PDOException $rowset_e) {
7316a1b9bfeSPhy                    $hasnextrowset = false; // driver does not support multi-rowset, should be executed in one time
7326a1b9bfeSPhy                }
7336a1b9bfeSPhy            }
7345de3a6a5SAndreas Gohr        } catch (Exception $e) {
7354fb8dfabSAndreas Gohr            // report the caller's line
7364fb8dfabSAndreas Gohr            $trace = debug_backtrace();
7374fb8dfabSAndreas Gohr            $line = $trace[0]['line'];
7383213bf4eSAndreas Gohr            $dsql = $this->debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
7393213bf4eSAndreas Gohr            $this->debugMsg($e, -1, $line);
7403213bf4eSAndreas Gohr            $this->debugMsg("SQL: <pre>$dsql</pre>", -1, $line);
7411600c7ccSAndreas Gohr        }
74270a89417SAndreas Gohr        $sth->closeCursor();
74370a89417SAndreas Gohr        $sth = null;
74470a89417SAndreas Gohr
7455de3a6a5SAndreas Gohr        return $result;
7465de3a6a5SAndreas Gohr    }
74770a89417SAndreas Gohr
74870a89417SAndreas Gohr    /**
749f64dbc90SAndreas Gohr     * Wrapper around msg() but outputs only when debug is enabled
750f64dbc90SAndreas Gohr     *
751f64dbc90SAndreas Gohr     * @param string|Exception $message
752f64dbc90SAndreas Gohr     * @param int $err
753f64dbc90SAndreas Gohr     * @param int $line
754f64dbc90SAndreas Gohr     */
7553213bf4eSAndreas Gohr    protected function debugMsg($message, $err = 0, $line = 0)
7563213bf4eSAndreas Gohr    {
757f64dbc90SAndreas Gohr        if (!$this->getConf('debug')) return;
758f64dbc90SAndreas Gohr        if (is_a($message, 'Exception')) {
759f64dbc90SAndreas Gohr            $err = -1;
760f64dbc90SAndreas Gohr            $msg = $message->getMessage();
7614fb8dfabSAndreas Gohr            if (!$line) $line = $message->getLine();
762f64dbc90SAndreas Gohr        } else {
763f64dbc90SAndreas Gohr            $msg = $message;
764f64dbc90SAndreas Gohr        }
765f64dbc90SAndreas Gohr
766f64dbc90SAndreas Gohr        if (defined('DOKU_UNITTEST')) {
767f64dbc90SAndreas Gohr            printf("\n%s, %s:%d\n", $msg, __FILE__, $line);
768f64dbc90SAndreas Gohr        } else {
769f64dbc90SAndreas Gohr            msg('authpdo: ' . $msg, $err, $line, __FILE__);
770f64dbc90SAndreas Gohr        }
771f64dbc90SAndreas Gohr    }
7725de3a6a5SAndreas Gohr
7735de3a6a5SAndreas Gohr    /**
7745de3a6a5SAndreas Gohr     * Check if the given config strings are set
7755de3a6a5SAndreas Gohr     *
7765de3a6a5SAndreas Gohr     * @param string[] $keys
7775de3a6a5SAndreas Gohr     * @return  bool
778*31a58abaSAndreas Gohr     * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
779*31a58abaSAndreas Gohr     *
7805de3a6a5SAndreas Gohr     */
7813213bf4eSAndreas Gohr    protected function checkConfig($keys)
7823213bf4eSAndreas Gohr    {
7835de3a6a5SAndreas Gohr        foreach ($keys as $key) {
784ad4d5631SAndreas Gohr            $params = explode(':', $key);
785ad4d5631SAndreas Gohr            $key = array_shift($params);
786ad4d5631SAndreas Gohr            $sql = trim($this->getConf($key));
787ad4d5631SAndreas Gohr
788ad4d5631SAndreas Gohr            // check if sql is set
789ad4d5631SAndreas Gohr            if (!$sql) return false;
790ad4d5631SAndreas Gohr            // check if needed params are there
791ad4d5631SAndreas Gohr            foreach ($params as $param) {
792ad4d5631SAndreas Gohr                if (strpos($sql, ":$param") === false) return false;
793ad4d5631SAndreas Gohr            }
7945de3a6a5SAndreas Gohr        }
7955de3a6a5SAndreas Gohr
7965de3a6a5SAndreas Gohr        return true;
7975de3a6a5SAndreas Gohr    }
7985de3a6a5SAndreas Gohr
7995de3a6a5SAndreas Gohr    /**
8005de3a6a5SAndreas Gohr     * create an approximation of the SQL string with parameters replaced
8015de3a6a5SAndreas Gohr     *
8025de3a6a5SAndreas Gohr     * @param string $sql
8035de3a6a5SAndreas Gohr     * @param array $params
8045de3a6a5SAndreas Gohr     * @param bool $htmlescape Should the result be escaped for output in HTML?
8055de3a6a5SAndreas Gohr     * @return string
8065de3a6a5SAndreas Gohr     */
8073213bf4eSAndreas Gohr    protected function debugSQL($sql, $params, $htmlescape = true)
8083213bf4eSAndreas Gohr    {
8095de3a6a5SAndreas Gohr        foreach ($params as $key => $val) {
8105de3a6a5SAndreas Gohr            if (is_int($val)) {
8115de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_INT);
8125de3a6a5SAndreas Gohr            } elseif (is_bool($val)) {
8135de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_BOOL);
8145de3a6a5SAndreas Gohr            } elseif (is_null($val)) {
8155de3a6a5SAndreas Gohr                $val = 'NULL';
8165de3a6a5SAndreas Gohr            } else {
8175de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val);
8185de3a6a5SAndreas Gohr            }
8195de3a6a5SAndreas Gohr            $sql = str_replace($key, $val, $sql);
8205de3a6a5SAndreas Gohr        }
8215de3a6a5SAndreas Gohr        if ($htmlescape) $sql = hsc($sql);
8225de3a6a5SAndreas Gohr        return $sql;
8235de3a6a5SAndreas Gohr    }
824f64dbc90SAndreas Gohr}
825f64dbc90SAndreas Gohr
826f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et:
827