xref: /dokuwiki/lib/plugins/authpdo/auth.php (revision 14119d44248a8e38fe695b9761f0d359df30fe1e)
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
9f64dbc90SAndreas Gohr// must be run within Dokuwiki
10f64dbc90SAndreas Gohrif(!defined('DOKU_INC')) die();
11f64dbc90SAndreas Gohr
120d586afdSAndreas Gohr/**
130d586afdSAndreas Gohr * Class auth_plugin_authpdo
140d586afdSAndreas Gohr */
15f64dbc90SAndreas Gohrclass auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
16f64dbc90SAndreas Gohr
17f64dbc90SAndreas Gohr    /** @var PDO */
18f64dbc90SAndreas Gohr    protected $pdo;
19f64dbc90SAndreas Gohr
20f64dbc90SAndreas Gohr    /**
21f64dbc90SAndreas Gohr     * Constructor.
22f64dbc90SAndreas Gohr     */
23f64dbc90SAndreas Gohr    public function __construct() {
24f64dbc90SAndreas Gohr        parent::__construct(); // for compatibility
25f64dbc90SAndreas Gohr
26f64dbc90SAndreas Gohr        if(!class_exists('PDO')) {
27f64dbc90SAndreas Gohr            $this->_debug('PDO extension for PHP not found.', -1, __LINE__);
28f64dbc90SAndreas Gohr            $this->success = false;
29f64dbc90SAndreas Gohr            return;
30f64dbc90SAndreas Gohr        }
31f64dbc90SAndreas Gohr
32f64dbc90SAndreas Gohr        if(!$this->getConf('dsn')) {
33f64dbc90SAndreas Gohr            $this->_debug('No DSN specified', -1, __LINE__);
34f64dbc90SAndreas Gohr            $this->success = false;
35f64dbc90SAndreas Gohr            return;
36f64dbc90SAndreas Gohr        }
37f64dbc90SAndreas Gohr
38f64dbc90SAndreas Gohr        try {
39f64dbc90SAndreas Gohr            $this->pdo = new PDO(
40f64dbc90SAndreas Gohr                $this->getConf('dsn'),
41f64dbc90SAndreas Gohr                $this->getConf('user'),
42f64dbc90SAndreas Gohr                $this->getConf('pass'),
43f64dbc90SAndreas Gohr                array(
4470a89417SAndreas Gohr                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array
4570a89417SAndreas Gohr                    PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names
465de3a6a5SAndreas Gohr                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
47f64dbc90SAndreas Gohr                )
48f64dbc90SAndreas Gohr            );
49f64dbc90SAndreas Gohr        } catch(PDOException $e) {
50f64dbc90SAndreas Gohr            $this->_debug($e);
51c27579a6SAndreas Gohr            msg($this->getLang('connectfail'), -1);
52f64dbc90SAndreas Gohr            $this->success = false;
53f64dbc90SAndreas Gohr            return;
54f64dbc90SAndreas Gohr        }
55f64dbc90SAndreas Gohr
560d586afdSAndreas Gohr        // can Users be created?
570d586afdSAndreas Gohr        $this->cando['addUser'] = $this->_chkcnf(
580d586afdSAndreas Gohr            array(
590d586afdSAndreas Gohr                'select-user',
600d586afdSAndreas Gohr                'select-user-groups',
610d586afdSAndreas Gohr                'select-groups',
620d586afdSAndreas Gohr                'insert-user',
630d586afdSAndreas Gohr                'insert-group',
640d586afdSAndreas Gohr                'join-group'
650d586afdSAndreas Gohr            )
660d586afdSAndreas Gohr        );
67f64dbc90SAndreas Gohr
680d586afdSAndreas Gohr        // can Users be deleted?
690d586afdSAndreas Gohr        $this->cando['delUser'] = $this->_chkcnf(
700d586afdSAndreas Gohr            array(
710d586afdSAndreas Gohr                'select-user',
720d586afdSAndreas Gohr                'select-user-groups',
730d586afdSAndreas Gohr                'select-groups',
740d586afdSAndreas Gohr                'leave-group'
750d586afdSAndreas Gohr            )
760d586afdSAndreas Gohr        );
770d586afdSAndreas Gohr
780d586afdSAndreas Gohr        // can login names be changed?
790d586afdSAndreas Gohr        $this->cando['modLogin'] = $this->_chkcnf(
800d586afdSAndreas Gohr            array(
810d586afdSAndreas Gohr                'select-user',
820d586afdSAndreas Gohr                'select-user-groups',
830d586afdSAndreas Gohr                'update-user-login'
840d586afdSAndreas Gohr            )
850d586afdSAndreas Gohr        );
860d586afdSAndreas Gohr
870d586afdSAndreas Gohr        // can passwords be changed?
880d586afdSAndreas Gohr        $this->cando['modPass'] = $this->_chkcnf(
890d586afdSAndreas Gohr            array(
900d586afdSAndreas Gohr                'select-user',
910d586afdSAndreas Gohr                'select-user-groups',
920d586afdSAndreas Gohr                'update-user-pass'
930d586afdSAndreas Gohr            )
940d586afdSAndreas Gohr        );
950d586afdSAndreas Gohr
960d586afdSAndreas Gohr        // can real names and emails be changed?
970d586afdSAndreas Gohr        $this->cando['modName'] = $this->cando['modMail'] = $this->_chkcnf(
980d586afdSAndreas Gohr            array(
990d586afdSAndreas Gohr                'select-user',
1000d586afdSAndreas Gohr                'select-user-groups',
1010d586afdSAndreas Gohr                'update-user-info'
1020d586afdSAndreas Gohr            )
1030d586afdSAndreas Gohr        );
1040d586afdSAndreas Gohr
1050d586afdSAndreas Gohr        // can groups be changed?
1060d586afdSAndreas Gohr        $this->cando['modGroups'] = $this->_chkcnf(
1070d586afdSAndreas Gohr            array(
1080d586afdSAndreas Gohr                'select-user',
1090d586afdSAndreas Gohr                'select-user-groups',
1100d586afdSAndreas Gohr                'select-groups',
1110d586afdSAndreas Gohr                'leave-group',
1120d586afdSAndreas Gohr                'join-group',
1130d586afdSAndreas Gohr                'insert-group'
1140d586afdSAndreas Gohr            )
1150d586afdSAndreas Gohr        );
1160d586afdSAndreas Gohr
1170d586afdSAndreas Gohr        // can a filtered list of users be retrieved?
1180d586afdSAndreas Gohr        $this->cando['getUsers'] = $this->_chkcnf(
1190d586afdSAndreas Gohr            array(
1200d586afdSAndreas Gohr                'list-users'
1210d586afdSAndreas Gohr            )
1220d586afdSAndreas Gohr        );
1230d586afdSAndreas Gohr
1240d586afdSAndreas Gohr        // can the number of users be retrieved?
1250d586afdSAndreas Gohr        $this->cando['getUserCount'] = $this->_chkcnf(
1260d586afdSAndreas Gohr            array(
1270d586afdSAndreas Gohr                'count-users'
1280d586afdSAndreas Gohr            )
1290d586afdSAndreas Gohr        );
1300d586afdSAndreas Gohr
1310d586afdSAndreas Gohr        // can a list of available groups be retrieved?
1320d586afdSAndreas Gohr        $this->cando['getGroups'] = $this->_chkcnf(
1330d586afdSAndreas Gohr            array(
1340d586afdSAndreas Gohr                'select-groups'
1350d586afdSAndreas Gohr            )
1360d586afdSAndreas Gohr        );
1370d586afdSAndreas Gohr
138f64dbc90SAndreas Gohr        $this->success = true;
139f64dbc90SAndreas Gohr    }
140f64dbc90SAndreas Gohr
141f64dbc90SAndreas Gohr    /**
142f64dbc90SAndreas Gohr     * Check user+password
143f64dbc90SAndreas Gohr     *
144f64dbc90SAndreas Gohr     * @param   string $user the user name
145f64dbc90SAndreas Gohr     * @param   string $pass the clear text password
146f64dbc90SAndreas Gohr     * @return  bool
147f64dbc90SAndreas Gohr     */
148f64dbc90SAndreas Gohr    public function checkPass($user, $pass) {
149f64dbc90SAndreas Gohr
150f64dbc90SAndreas Gohr        $data = $this->_selectUser($user);
151f64dbc90SAndreas Gohr        if($data == false) return false;
152f64dbc90SAndreas Gohr
153f64dbc90SAndreas Gohr        if(isset($data['hash'])) {
154f64dbc90SAndreas Gohr            // hashed password
155f64dbc90SAndreas Gohr            $passhash = new PassHash();
156f64dbc90SAndreas Gohr            return $passhash->verify_hash($pass, $data['hash']);
157f64dbc90SAndreas Gohr        } else {
158f64dbc90SAndreas Gohr            // clear text password in the database O_o
159f64dbc90SAndreas Gohr            return ($pass == $data['clear']);
160f64dbc90SAndreas Gohr        }
161f64dbc90SAndreas Gohr    }
162f64dbc90SAndreas Gohr
163f64dbc90SAndreas Gohr    /**
164f64dbc90SAndreas Gohr     * Return user info
165f64dbc90SAndreas Gohr     *
166f64dbc90SAndreas Gohr     * Returns info about the given user needs to contain
167f64dbc90SAndreas Gohr     * at least these fields:
168f64dbc90SAndreas Gohr     *
169f64dbc90SAndreas Gohr     * name string  full name of the user
170f64dbc90SAndreas Gohr     * mail string  email addres of the user
171f64dbc90SAndreas Gohr     * grps array   list of groups the user is in
172f64dbc90SAndreas Gohr     *
173f64dbc90SAndreas Gohr     * @param   string $user the user name
174f64dbc90SAndreas Gohr     * @param   bool $requireGroups whether or not the returned data must include groups
175f64dbc90SAndreas Gohr     * @return array containing user data or false
176f64dbc90SAndreas Gohr     */
177f64dbc90SAndreas Gohr    public function getUserData($user, $requireGroups = true) {
178f64dbc90SAndreas Gohr        $data = $this->_selectUser($user);
179f64dbc90SAndreas Gohr        if($data == false) return false;
180f64dbc90SAndreas Gohr
18170a89417SAndreas Gohr        if(isset($data['hash'])) unset($data['hash']);
18270a89417SAndreas Gohr        if(isset($data['clean'])) unset($data['clean']);
183f64dbc90SAndreas Gohr
18470a89417SAndreas Gohr        if($requireGroups) {
18570a89417SAndreas Gohr            $data['grps'] = $this->_selectUserGroups($data);
1865de3a6a5SAndreas Gohr            if($data['grps'] === false) return false;
187f64dbc90SAndreas Gohr        }
188f64dbc90SAndreas Gohr
189f64dbc90SAndreas Gohr        return $data;
190f64dbc90SAndreas Gohr    }
191f64dbc90SAndreas Gohr
192f64dbc90SAndreas Gohr    /**
193f64dbc90SAndreas Gohr     * Create a new User [implement only where required/possible]
194f64dbc90SAndreas Gohr     *
195f64dbc90SAndreas Gohr     * Returns false if the user already exists, null when an error
196f64dbc90SAndreas Gohr     * occurred and true if everything went well.
197f64dbc90SAndreas Gohr     *
198f64dbc90SAndreas Gohr     * The new user HAS TO be added to the default group by this
199f64dbc90SAndreas Gohr     * function!
200f64dbc90SAndreas Gohr     *
201f64dbc90SAndreas Gohr     * Set addUser capability when implemented
202f64dbc90SAndreas Gohr     *
203f64dbc90SAndreas Gohr     * @param  string $user
2045de3a6a5SAndreas Gohr     * @param  string $clear
205f64dbc90SAndreas Gohr     * @param  string $name
206f64dbc90SAndreas Gohr     * @param  string $mail
207f64dbc90SAndreas Gohr     * @param  null|array $grps
208f64dbc90SAndreas Gohr     * @return bool|null
209f64dbc90SAndreas Gohr     */
2105de3a6a5SAndreas Gohr    public function createUser($user, $clear, $name, $mail, $grps = null) {
2115de3a6a5SAndreas Gohr        global $conf;
2125de3a6a5SAndreas Gohr
2135de3a6a5SAndreas Gohr        if(($info = $this->getUserData($user, false)) !== false) {
2145de3a6a5SAndreas Gohr            msg($this->getLang('userexists'), -1);
2155de3a6a5SAndreas Gohr            return false; // user already exists
2165de3a6a5SAndreas Gohr        }
2175de3a6a5SAndreas Gohr
2185de3a6a5SAndreas Gohr        // prepare data
2195de3a6a5SAndreas Gohr        if($grps == null) $grps = array();
2205de3a6a5SAndreas Gohr        $grps[] = $conf['defaultgroup'];
2215de3a6a5SAndreas Gohr        $grps = array_unique($grps);
2225de3a6a5SAndreas Gohr        $hash = auth_cryptPassword($clear);
2235de3a6a5SAndreas Gohr        $userdata = compact('user', 'clear', 'hash', 'name', 'mail');
2245de3a6a5SAndreas Gohr
2255de3a6a5SAndreas Gohr        // action protected by transaction
2265de3a6a5SAndreas Gohr        $this->pdo->beginTransaction();
2275de3a6a5SAndreas Gohr        {
2285de3a6a5SAndreas Gohr            // insert the user
2295de3a6a5SAndreas Gohr            $ok = $this->_query($this->getConf('insert-user'), $userdata);
2305de3a6a5SAndreas Gohr            if($ok === false) goto FAIL;
2315de3a6a5SAndreas Gohr            $userdata = $this->getUserData($user, false);
2325de3a6a5SAndreas Gohr            if($userdata === false) goto FAIL;
2335de3a6a5SAndreas Gohr
2345de3a6a5SAndreas Gohr            // create all groups that do not exist, the refetch the groups
2355de3a6a5SAndreas Gohr            $allgroups = $this->_selectGroups();
2365de3a6a5SAndreas Gohr            foreach($grps as $group) {
2375de3a6a5SAndreas Gohr                if(!isset($allgroups[$group])) {
2386459f496SAndreas Gohr                    $ok = $this->addGroup($group);
2395de3a6a5SAndreas Gohr                    if($ok === false) goto FAIL;
2405de3a6a5SAndreas Gohr                }
2415de3a6a5SAndreas Gohr            }
2425de3a6a5SAndreas Gohr            $allgroups = $this->_selectGroups();
2435de3a6a5SAndreas Gohr
2445de3a6a5SAndreas Gohr            // add user to the groups
2455de3a6a5SAndreas Gohr            foreach($grps as $group) {
2465de3a6a5SAndreas Gohr                $ok = $this->_joinGroup($userdata, $allgroups[$group]);
2475de3a6a5SAndreas Gohr                if($ok === false) goto FAIL;
2485de3a6a5SAndreas Gohr            }
2495de3a6a5SAndreas Gohr        }
2505de3a6a5SAndreas Gohr        $this->pdo->commit();
2515de3a6a5SAndreas Gohr        return true;
2525de3a6a5SAndreas Gohr
2535de3a6a5SAndreas Gohr        // something went wrong, rollback
2545de3a6a5SAndreas Gohr        FAIL:
2555de3a6a5SAndreas Gohr        $this->pdo->rollBack();
2565de3a6a5SAndreas Gohr        $this->_debug('Transaction rolled back', 0, __LINE__);
257c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
2585de3a6a5SAndreas Gohr        return null; // return error
2595de3a6a5SAndreas Gohr    }
260f64dbc90SAndreas Gohr
261f64dbc90SAndreas Gohr    /**
2624fb8dfabSAndreas Gohr     * Modify user data
263f64dbc90SAndreas Gohr     *
264f64dbc90SAndreas Gohr     * @param   string $user nick of the user to be changed
265f64dbc90SAndreas Gohr     * @param   array $changes array of field/value pairs to be changed (password will be clear text)
266f64dbc90SAndreas Gohr     * @return  bool
267f64dbc90SAndreas Gohr     */
2684fb8dfabSAndreas Gohr    public function modifyUser($user, $changes) {
2694fb8dfabSAndreas Gohr        // secure everything in transaction
2704fb8dfabSAndreas Gohr        $this->pdo->beginTransaction();
2714fb8dfabSAndreas Gohr        {
2724fb8dfabSAndreas Gohr            $olddata = $this->getUserData($user);
2734fb8dfabSAndreas Gohr            $oldgroups = $olddata['grps'];
2744fb8dfabSAndreas Gohr            unset($olddata['grps']);
2754fb8dfabSAndreas Gohr
2764fb8dfabSAndreas Gohr            // changing the user name?
2774fb8dfabSAndreas Gohr            if(isset($changes['user'])) {
2784fb8dfabSAndreas Gohr                if($this->getUserData($changes['user'], false)) goto FAIL;
2794fb8dfabSAndreas Gohr                $params = $olddata;
2804fb8dfabSAndreas Gohr                $params['newlogin'] = $changes['user'];
2814fb8dfabSAndreas Gohr
2824fb8dfabSAndreas Gohr                $ok = $this->_query($this->getConf('update-user-login'), $params);
2834fb8dfabSAndreas Gohr                if($ok === false) goto FAIL;
2844fb8dfabSAndreas Gohr            }
2854fb8dfabSAndreas Gohr
2864fb8dfabSAndreas Gohr            // changing the password?
2874fb8dfabSAndreas Gohr            if(isset($changes['pass'])) {
2884fb8dfabSAndreas Gohr                $params = $olddata;
2894fb8dfabSAndreas Gohr                $params['clear'] = $changes['pass'];
2904fb8dfabSAndreas Gohr                $params['hash'] = auth_cryptPassword($changes['pass']);
2914fb8dfabSAndreas Gohr
2924fb8dfabSAndreas Gohr                $ok = $this->_query($this->getConf('update-user-pass'), $params);
2934fb8dfabSAndreas Gohr                if($ok === false) goto FAIL;
2944fb8dfabSAndreas Gohr            }
2954fb8dfabSAndreas Gohr
2964fb8dfabSAndreas Gohr            // changing info?
2974fb8dfabSAndreas Gohr            if(isset($changes['mail']) || isset($changes['name'])) {
2984fb8dfabSAndreas Gohr                $params = $olddata;
2994fb8dfabSAndreas Gohr                if(isset($changes['mail'])) $params['mail'] = $changes['mail'];
3004fb8dfabSAndreas Gohr                if(isset($changes['name'])) $params['name'] = $changes['name'];
3014fb8dfabSAndreas Gohr
3024fb8dfabSAndreas Gohr                $ok = $this->_query($this->getConf('update-user-info'), $params);
3034fb8dfabSAndreas Gohr                if($ok === false) goto FAIL;
3044fb8dfabSAndreas Gohr            }
3054fb8dfabSAndreas Gohr
3064fb8dfabSAndreas Gohr            // changing groups?
3074fb8dfabSAndreas Gohr            if(isset($changes['grps'])) {
3084fb8dfabSAndreas Gohr                $allgroups = $this->_selectGroups();
3094fb8dfabSAndreas Gohr
3104fb8dfabSAndreas Gohr                // remove membership for previous groups
3114fb8dfabSAndreas Gohr                foreach($oldgroups as $group) {
3124fb8dfabSAndreas Gohr                    if(!in_array($group, $changes['grps'])) {
3134fb8dfabSAndreas Gohr                        $ok = $this->_leaveGroup($olddata, $allgroups[$group]);
3144fb8dfabSAndreas Gohr                        if($ok === false) goto FAIL;
3154fb8dfabSAndreas Gohr                    }
3164fb8dfabSAndreas Gohr                }
3174fb8dfabSAndreas Gohr
3184fb8dfabSAndreas Gohr                // create all new groups that are missing
3194fb8dfabSAndreas Gohr                $added = 0;
3204fb8dfabSAndreas Gohr                foreach($changes['grps'] as $group) {
3214fb8dfabSAndreas Gohr                    if(!isset($allgroups[$group])) {
3226459f496SAndreas Gohr                        $ok = $this->addGroup($group);
3234fb8dfabSAndreas Gohr                        if($ok === false) goto FAIL;
3244fb8dfabSAndreas Gohr                        $added++;
3254fb8dfabSAndreas Gohr                    }
3264fb8dfabSAndreas Gohr                }
3274fb8dfabSAndreas Gohr                // reload group info
3284fb8dfabSAndreas Gohr                if($added > 0) $allgroups = $this->_selectGroups();
3294fb8dfabSAndreas Gohr
3304fb8dfabSAndreas Gohr                // add membership for new groups
3314fb8dfabSAndreas Gohr                foreach($changes['grps'] as $group) {
3324fb8dfabSAndreas Gohr                    if(!in_array($group, $oldgroups)) {
3334fb8dfabSAndreas Gohr                        $ok = $this->_joinGroup($olddata, $allgroups[$group]);
3344fb8dfabSAndreas Gohr                        if($ok === false) goto FAIL;
3354fb8dfabSAndreas Gohr                    }
3364fb8dfabSAndreas Gohr                }
3374fb8dfabSAndreas Gohr            }
3384fb8dfabSAndreas Gohr
3394fb8dfabSAndreas Gohr        }
3404fb8dfabSAndreas Gohr        $this->pdo->commit();
3414fb8dfabSAndreas Gohr        return true;
3424fb8dfabSAndreas Gohr
3434fb8dfabSAndreas Gohr        // something went wrong, rollback
3444fb8dfabSAndreas Gohr        FAIL:
3454fb8dfabSAndreas Gohr        $this->pdo->rollBack();
3464fb8dfabSAndreas Gohr        $this->_debug('Transaction rolled back', 0, __LINE__);
347c27579a6SAndreas Gohr        msg($this->getLang('writefail'), -1);
3484fb8dfabSAndreas Gohr        return false; // return error
3494fb8dfabSAndreas Gohr    }
350f64dbc90SAndreas Gohr
351f64dbc90SAndreas Gohr    /**
352e19be516SAndreas Gohr     * Delete one or more users
353f64dbc90SAndreas Gohr     *
354f64dbc90SAndreas Gohr     * Set delUser capability when implemented
355f64dbc90SAndreas Gohr     *
356f64dbc90SAndreas Gohr     * @param   array $users
357f64dbc90SAndreas Gohr     * @return  int    number of users deleted
358f64dbc90SAndreas Gohr     */
359e19be516SAndreas Gohr    public function deleteUsers($users) {
360e19be516SAndreas Gohr        $count = 0;
361e19be516SAndreas Gohr        foreach($users as $user) {
362e19be516SAndreas Gohr            if($this->_deleteUser($user)) $count++;
363e19be516SAndreas Gohr        }
364e19be516SAndreas Gohr        return $count;
365e19be516SAndreas Gohr    }
366f64dbc90SAndreas Gohr
367f64dbc90SAndreas Gohr    /**
368f64dbc90SAndreas Gohr     * Bulk retrieval of user data [implement only where required/possible]
369f64dbc90SAndreas Gohr     *
370f64dbc90SAndreas Gohr     * Set getUsers capability when implemented
371f64dbc90SAndreas Gohr     *
372f64dbc90SAndreas Gohr     * @param   int $start index of first user to be returned
373f64dbc90SAndreas Gohr     * @param   int $limit max number of users to be returned
374f64dbc90SAndreas Gohr     * @param   array $filter array of field/pattern pairs, null for no filter
375f64dbc90SAndreas Gohr     * @return  array list of userinfo (refer getUserData for internal userinfo details)
376f64dbc90SAndreas Gohr     */
3776459f496SAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
3786459f496SAndreas Gohr        if($limit < 0) $limit = 10000; // we don't support no limit
3796459f496SAndreas Gohr        if(is_null($filter)) $filter = array();
3806459f496SAndreas Gohr
3816459f496SAndreas Gohr        foreach(array('user', 'name', 'mail', 'group') as $key) {
3826459f496SAndreas Gohr            if(!isset($filter[$key])) {
3836459f496SAndreas Gohr                $filter[$key] = '%';
3846459f496SAndreas Gohr            } else {
3856459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
3866459f496SAndreas Gohr            }
3876459f496SAndreas Gohr        }
388*14119d44SAndreas Gohr        $filter['start'] = (int) $start;
389*14119d44SAndreas Gohr        $filter['end'] = (int) $start + $limit;
390*14119d44SAndreas Gohr        $filter['limit'] = (int) $limit;
3916459f496SAndreas Gohr
3926459f496SAndreas Gohr        $result = $this->_query($this->getConf('list-users'), $filter);
3936459f496SAndreas Gohr        if(!$result) return array();
3946459f496SAndreas Gohr        $users = array();
3956459f496SAndreas Gohr        foreach($result as $row) {
3966459f496SAndreas Gohr            if(!isset($row['user'])) {
3976459f496SAndreas Gohr                $this->_debug("Statement did not return 'user' attribute", -1, __LINE__);
3986459f496SAndreas Gohr                return array();
3996459f496SAndreas Gohr            }
4006459f496SAndreas Gohr            $users[] = $row['user'];
4016459f496SAndreas Gohr        }
4026459f496SAndreas Gohr        return $users;
4036459f496SAndreas Gohr    }
404f64dbc90SAndreas Gohr
405f64dbc90SAndreas Gohr    /**
406f64dbc90SAndreas Gohr     * Return a count of the number of user which meet $filter criteria
407f64dbc90SAndreas Gohr     *
408f64dbc90SAndreas Gohr     * @param  array $filter array of field/pattern pairs, empty array for no filter
409f64dbc90SAndreas Gohr     * @return int
410f64dbc90SAndreas Gohr     */
4116459f496SAndreas Gohr    public function getUserCount($filter = array()) {
4126459f496SAndreas Gohr        if(is_null($filter)) $filter = array();
4136459f496SAndreas Gohr
4146459f496SAndreas Gohr        foreach(array('user', 'name', 'mail', 'group') as $key) {
4156459f496SAndreas Gohr            if(!isset($filter[$key])) {
4166459f496SAndreas Gohr                $filter[$key] = '%';
4176459f496SAndreas Gohr            } else {
4186459f496SAndreas Gohr                $filter[$key] = '%' . $filter[$key] . '%';
4196459f496SAndreas Gohr            }
4206459f496SAndreas Gohr        }
4216459f496SAndreas Gohr
4226459f496SAndreas Gohr        $result = $this->_query($this->getConf('count-users'), $filter);
4236459f496SAndreas Gohr        if(!$result || !isset($result[0]['count'])) {
4246459f496SAndreas Gohr            $this->_debug("Statement did not return 'count' attribute", -1, __LINE__);
4256459f496SAndreas Gohr        }
4266459f496SAndreas Gohr        return isset($result[0]['count']);
4276459f496SAndreas Gohr    }
428f64dbc90SAndreas Gohr
429f64dbc90SAndreas Gohr    /**
4306459f496SAndreas Gohr     * Create a new group with the given name
431f64dbc90SAndreas Gohr     *
432f64dbc90SAndreas Gohr     * @param string $group
433f64dbc90SAndreas Gohr     * @return bool
434f64dbc90SAndreas Gohr     */
4356459f496SAndreas Gohr    public function addGroup($group) {
4366459f496SAndreas Gohr        $sql = $this->getConf('insert-group');
4376459f496SAndreas Gohr
4386459f496SAndreas Gohr        $result = $this->_query($sql, array(':group' => $group));
4396459f496SAndreas Gohr        if($result === false) return false;
4406459f496SAndreas Gohr        return true;
4416459f496SAndreas Gohr    }
442f64dbc90SAndreas Gohr
443f64dbc90SAndreas Gohr    /**
4445de3a6a5SAndreas Gohr     * Retrieve groups
445f64dbc90SAndreas Gohr     *
446f64dbc90SAndreas Gohr     * Set getGroups capability when implemented
447f64dbc90SAndreas Gohr     *
448f64dbc90SAndreas Gohr     * @param   int $start
449f64dbc90SAndreas Gohr     * @param   int $limit
450f64dbc90SAndreas Gohr     * @return  array
451f64dbc90SAndreas Gohr     */
4525de3a6a5SAndreas Gohr    public function retrieveGroups($start = 0, $limit = 0) {
4535de3a6a5SAndreas Gohr        $groups = array_keys($this->_selectGroups());
4545de3a6a5SAndreas Gohr        if($groups === false) return array();
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     */
469f64dbc90SAndreas Gohr    protected function _selectUser($user) {
470f64dbc90SAndreas Gohr        $sql = $this->getConf('select-user');
471f64dbc90SAndreas Gohr
4725de3a6a5SAndreas Gohr        $result = $this->_query($sql, array(':user' => $user));
47370a89417SAndreas Gohr        if(!$result) return false;
474f64dbc90SAndreas Gohr
47570a89417SAndreas Gohr        if(count($result) > 1) {
476f64dbc90SAndreas Gohr            $this->_debug('Found more than one matching user', -1, __LINE__);
477f64dbc90SAndreas Gohr            return false;
478f64dbc90SAndreas Gohr        }
479f64dbc90SAndreas Gohr
480f64dbc90SAndreas Gohr        $data = array_shift($result);
481f64dbc90SAndreas Gohr        $dataok = true;
482f64dbc90SAndreas Gohr
483f64dbc90SAndreas Gohr        if(!isset($data['user'])) {
484f64dbc90SAndreas Gohr            $this->_debug("Statement did not return 'user' attribute", -1, __LINE__);
485f64dbc90SAndreas Gohr            $dataok = false;
486f64dbc90SAndreas Gohr        }
487f64dbc90SAndreas Gohr        if(!isset($data['hash']) && !isset($data['clear'])) {
488f64dbc90SAndreas Gohr            $this->_debug("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
489f64dbc90SAndreas Gohr            $dataok = false;
490f64dbc90SAndreas Gohr        }
491f64dbc90SAndreas Gohr        if(!isset($data['name'])) {
492f64dbc90SAndreas Gohr            $this->_debug("Statement did not return 'name' attribute", -1, __LINE__);
493f64dbc90SAndreas Gohr            $dataok = false;
494f64dbc90SAndreas Gohr        }
495f64dbc90SAndreas Gohr        if(!isset($data['mail'])) {
496f64dbc90SAndreas Gohr            $this->_debug("Statement did not return 'mail' attribute", -1, __LINE__);
497f64dbc90SAndreas Gohr            $dataok = false;
498f64dbc90SAndreas Gohr        }
499f64dbc90SAndreas Gohr
500f64dbc90SAndreas Gohr        if(!$dataok) return false;
501f64dbc90SAndreas Gohr        return $data;
502f64dbc90SAndreas Gohr    }
503f64dbc90SAndreas Gohr
504f64dbc90SAndreas Gohr    /**
505e19be516SAndreas Gohr     * Delete a user after removing all their group memberships
506e19be516SAndreas Gohr     *
507e19be516SAndreas Gohr     * @param string $user
508e19be516SAndreas Gohr     * @return bool true when the user was deleted
509e19be516SAndreas Gohr     */
510e19be516SAndreas Gohr    protected function _deleteUser($user) {
511e19be516SAndreas Gohr        $this->pdo->beginTransaction();
512e19be516SAndreas Gohr        {
513e19be516SAndreas Gohr            $userdata = $this->getUserData($user);
514e19be516SAndreas Gohr            if($userdata === false) goto FAIL;
515e19be516SAndreas Gohr            $allgroups = $this->_selectGroups();
516e19be516SAndreas Gohr
517e19be516SAndreas Gohr            // remove group memberships (ignore errors)
518e19be516SAndreas Gohr            foreach($userdata['grps'] as $group) {
519e19be516SAndreas Gohr                $this->_leaveGroup($userdata, $allgroups[$group]);
520e19be516SAndreas Gohr            }
521e19be516SAndreas Gohr
522e19be516SAndreas Gohr            $ok = $this->_query($this->getConf('delete-user'), $userdata);
523e19be516SAndreas Gohr            if($ok === false) goto FAIL;
524e19be516SAndreas Gohr        }
525e19be516SAndreas Gohr        $this->pdo->commit();
526e19be516SAndreas Gohr        return true;
527e19be516SAndreas Gohr
528e19be516SAndreas Gohr        FAIL:
529e19be516SAndreas Gohr        $this->pdo->rollBack();
530e19be516SAndreas Gohr        return false;
531e19be516SAndreas Gohr    }
532e19be516SAndreas Gohr
533e19be516SAndreas Gohr    /**
53470a89417SAndreas Gohr     * Select all groups of a user
53570a89417SAndreas Gohr     *
53670a89417SAndreas Gohr     * @param array $userdata The userdata as returned by _selectUser()
5375de3a6a5SAndreas Gohr     * @return array|bool list of group names, false on error
53870a89417SAndreas Gohr     */
53970a89417SAndreas Gohr    protected function _selectUserGroups($userdata) {
54070a89417SAndreas Gohr        global $conf;
54170a89417SAndreas Gohr        $sql = $this->getConf('select-user-groups');
5425de3a6a5SAndreas Gohr        $result = $this->_query($sql, $userdata);
5435de3a6a5SAndreas Gohr        if($result === false) return false;
54470a89417SAndreas Gohr
54570a89417SAndreas Gohr        $groups = array($conf['defaultgroup']); // always add default config
5465de3a6a5SAndreas Gohr        foreach($result as $row) {
5475de3a6a5SAndreas Gohr            if(!isset($row['group'])) {
5485de3a6a5SAndreas Gohr                $this->_debug("No 'group' field returned in select-user-groups statement");
5495de3a6a5SAndreas Gohr                return false;
5505de3a6a5SAndreas Gohr            }
55170a89417SAndreas Gohr            $groups[] = $row['group'];
55270a89417SAndreas Gohr        }
55370a89417SAndreas Gohr
55470a89417SAndreas Gohr        $groups = array_unique($groups);
55570a89417SAndreas Gohr        sort($groups);
55670a89417SAndreas Gohr        return $groups;
55770a89417SAndreas Gohr    }
55870a89417SAndreas Gohr
55970a89417SAndreas Gohr    /**
5605de3a6a5SAndreas Gohr     * Select all available groups
5615de3a6a5SAndreas Gohr     *
5625de3a6a5SAndreas Gohr     * @todo this should be cached
5635de3a6a5SAndreas Gohr     * @return array|bool list of all available groups and their properties
5645de3a6a5SAndreas Gohr     */
5655de3a6a5SAndreas Gohr    protected function _selectGroups() {
5665de3a6a5SAndreas Gohr        $sql = $this->getConf('select-groups');
5675de3a6a5SAndreas Gohr        $result = $this->_query($sql);
5685de3a6a5SAndreas Gohr        if($result === false) return false;
5695de3a6a5SAndreas Gohr
5705de3a6a5SAndreas Gohr        $groups = array();
5715de3a6a5SAndreas Gohr        foreach($result as $row) {
5725de3a6a5SAndreas Gohr            if(!isset($row['group'])) {
5735de3a6a5SAndreas Gohr                $this->_debug("No 'group' field returned from select-groups statement", -1, __LINE__);
5745de3a6a5SAndreas Gohr                return false;
5755de3a6a5SAndreas Gohr            }
5765de3a6a5SAndreas Gohr
5775de3a6a5SAndreas Gohr            // relayout result with group name as key
5785de3a6a5SAndreas Gohr            $group = $row['group'];
5795de3a6a5SAndreas Gohr            $groups[$group] = $row;
5805de3a6a5SAndreas Gohr        }
5815de3a6a5SAndreas Gohr
5825de3a6a5SAndreas Gohr        ksort($groups);
5835de3a6a5SAndreas Gohr        return $groups;
5845de3a6a5SAndreas Gohr    }
5855de3a6a5SAndreas Gohr
5865de3a6a5SAndreas Gohr    /**
5874fb8dfabSAndreas Gohr     * Adds the user to the group
5885de3a6a5SAndreas Gohr     *
5895de3a6a5SAndreas Gohr     * @param array $userdata all the user data
5905de3a6a5SAndreas Gohr     * @param array $groupdata all the group data
5915de3a6a5SAndreas Gohr     * @return bool
5925de3a6a5SAndreas Gohr     */
5935de3a6a5SAndreas Gohr    protected function _joinGroup($userdata, $groupdata) {
5945de3a6a5SAndreas Gohr        $data = array_merge($userdata, $groupdata);
5955de3a6a5SAndreas Gohr        $sql = $this->getConf('join-group');
5965de3a6a5SAndreas Gohr        $result = $this->_query($sql, $data);
5975de3a6a5SAndreas Gohr        if($result === false) return false;
5985de3a6a5SAndreas Gohr        return true;
5995de3a6a5SAndreas Gohr    }
6005de3a6a5SAndreas Gohr
6015de3a6a5SAndreas Gohr    /**
6024fb8dfabSAndreas Gohr     * Removes the user from the group
6034fb8dfabSAndreas Gohr     *
6044fb8dfabSAndreas Gohr     * @param array $userdata all the user data
6054fb8dfabSAndreas Gohr     * @param array $groupdata all the group data
6064fb8dfabSAndreas Gohr     * @return bool
6074fb8dfabSAndreas Gohr     */
6084fb8dfabSAndreas Gohr    protected function _leaveGroup($userdata, $groupdata) {
6094fb8dfabSAndreas Gohr        $data = array_merge($userdata, $groupdata);
6104fb8dfabSAndreas Gohr        $sql = $this->getConf('leave-group');
6114fb8dfabSAndreas Gohr        $result = $this->_query($sql, $data);
6124fb8dfabSAndreas Gohr        if($result === false) return false;
6134fb8dfabSAndreas Gohr        return true;
6144fb8dfabSAndreas Gohr    }
6154fb8dfabSAndreas Gohr
6164fb8dfabSAndreas Gohr    /**
61770a89417SAndreas Gohr     * Executes a query
61870a89417SAndreas Gohr     *
61970a89417SAndreas Gohr     * @param string $sql The SQL statement to execute
62070a89417SAndreas Gohr     * @param array $arguments Named parameters to be used in the statement
621f695c447SAndreas Gohr     * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error
62270a89417SAndreas Gohr     */
6235de3a6a5SAndreas Gohr    protected function _query($sql, $arguments = array()) {
624f695c447SAndreas Gohr        $sql = trim($sql);
6255de3a6a5SAndreas Gohr        if(empty($sql)) {
6265de3a6a5SAndreas Gohr            $this->_debug('No SQL query given', -1, __LINE__);
6275de3a6a5SAndreas Gohr            return false;
6285de3a6a5SAndreas Gohr        }
6295de3a6a5SAndreas Gohr
630*14119d44SAndreas Gohr        // execute
63170a89417SAndreas Gohr        $params = array();
632*14119d44SAndreas Gohr        $sth = $this->pdo->prepare($sql);
633*14119d44SAndreas Gohr        try {
634*14119d44SAndreas Gohr            // prepare parameters - we only use those that exist in the SQL
63570a89417SAndreas Gohr            foreach($arguments as $key => $value) {
63670a89417SAndreas Gohr                if(is_array($value)) continue;
63770a89417SAndreas Gohr                if(is_object($value)) continue;
63870a89417SAndreas Gohr                if($key[0] != ':') $key = ":$key"; // prefix with colon if needed
639*14119d44SAndreas Gohr                if(strpos($sql, $key) === false) continue; // skip if parameter is missing
640*14119d44SAndreas Gohr
641*14119d44SAndreas Gohr                if(is_int($value)) {
642*14119d44SAndreas Gohr                    $sth->bindValue($key, $value, PDO::PARAM_INT);
643*14119d44SAndreas Gohr                } else {
644*14119d44SAndreas Gohr                    $sth->bindValue($key, $value);
645*14119d44SAndreas Gohr                }
646*14119d44SAndreas Gohr                $param[$key] = $value; //remember for debugging
64770a89417SAndreas Gohr            }
64870a89417SAndreas Gohr
649*14119d44SAndreas Gohr            $sth->execute();
650f695c447SAndreas Gohr            if(strtolower(substr($sql, 0, 6)) == 'select') {
65170a89417SAndreas Gohr                $result = $sth->fetchAll();
652f695c447SAndreas Gohr            } else {
653f695c447SAndreas Gohr                $result = $sth->rowCount();
654f695c447SAndreas Gohr            }
6555de3a6a5SAndreas Gohr        } catch(Exception $e) {
6564fb8dfabSAndreas Gohr            // report the caller's line
6574fb8dfabSAndreas Gohr            $trace = debug_backtrace();
6584fb8dfabSAndreas Gohr            $line = $trace[0]['line'];
6595de3a6a5SAndreas Gohr            $dsql = $this->_debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
6604fb8dfabSAndreas Gohr            $this->_debug($e, -1, $line);
6614fb8dfabSAndreas Gohr            $this->_debug("SQL: <pre>$dsql</pre>", -1, $line);
66270a89417SAndreas Gohr            $result = false;
6631600c7ccSAndreas Gohr        }
66470a89417SAndreas Gohr        $sth->closeCursor();
66570a89417SAndreas Gohr        $sth = null;
66670a89417SAndreas Gohr
6675de3a6a5SAndreas Gohr        return $result;
6685de3a6a5SAndreas Gohr    }
66970a89417SAndreas Gohr
67070a89417SAndreas Gohr    /**
671f64dbc90SAndreas Gohr     * Wrapper around msg() but outputs only when debug is enabled
672f64dbc90SAndreas Gohr     *
673f64dbc90SAndreas Gohr     * @param string|Exception $message
674f64dbc90SAndreas Gohr     * @param int $err
675f64dbc90SAndreas Gohr     * @param int $line
676f64dbc90SAndreas Gohr     */
677f64dbc90SAndreas Gohr    protected function _debug($message, $err = 0, $line = 0) {
678f64dbc90SAndreas Gohr        if(!$this->getConf('debug')) return;
679f64dbc90SAndreas Gohr        if(is_a($message, 'Exception')) {
680f64dbc90SAndreas Gohr            $err = -1;
681f64dbc90SAndreas Gohr            $msg = $message->getMessage();
6824fb8dfabSAndreas Gohr            if(!$line) $line = $message->getLine();
683f64dbc90SAndreas Gohr        } else {
684f64dbc90SAndreas Gohr            $msg = $message;
685f64dbc90SAndreas Gohr        }
686f64dbc90SAndreas Gohr
687f64dbc90SAndreas Gohr        if(defined('DOKU_UNITTEST')) {
688f64dbc90SAndreas Gohr            printf("\n%s, %s:%d\n", $msg, __FILE__, $line);
689f64dbc90SAndreas Gohr        } else {
690f64dbc90SAndreas Gohr            msg('authpdo: ' . $msg, $err, $line, __FILE__);
691f64dbc90SAndreas Gohr        }
692f64dbc90SAndreas Gohr    }
6935de3a6a5SAndreas Gohr
6945de3a6a5SAndreas Gohr    /**
6955de3a6a5SAndreas Gohr     * Check if the given config strings are set
6965de3a6a5SAndreas Gohr     *
6975de3a6a5SAndreas Gohr     * @author  Matthias Grimm <matthiasgrimm@users.sourceforge.net>
6985de3a6a5SAndreas Gohr     *
6995de3a6a5SAndreas Gohr     * @param   string[] $keys
7005de3a6a5SAndreas Gohr     * @return  bool
7015de3a6a5SAndreas Gohr     */
7025de3a6a5SAndreas Gohr    protected function _chkcnf($keys) {
7035de3a6a5SAndreas Gohr        foreach($keys as $key) {
7045de3a6a5SAndreas Gohr            if(!$this->getConf($key)) return false;
7055de3a6a5SAndreas Gohr        }
7065de3a6a5SAndreas Gohr
7075de3a6a5SAndreas Gohr        return true;
7085de3a6a5SAndreas Gohr    }
7095de3a6a5SAndreas Gohr
7105de3a6a5SAndreas Gohr    /**
7115de3a6a5SAndreas Gohr     * create an approximation of the SQL string with parameters replaced
7125de3a6a5SAndreas Gohr     *
7135de3a6a5SAndreas Gohr     * @param string $sql
7145de3a6a5SAndreas Gohr     * @param array $params
7155de3a6a5SAndreas Gohr     * @param bool $htmlescape Should the result be escaped for output in HTML?
7165de3a6a5SAndreas Gohr     * @return string
7175de3a6a5SAndreas Gohr     */
7185de3a6a5SAndreas Gohr    protected function _debugSQL($sql, $params, $htmlescape = true) {
7195de3a6a5SAndreas Gohr        foreach($params as $key => $val) {
7205de3a6a5SAndreas Gohr            if(is_int($val)) {
7215de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_INT);
7225de3a6a5SAndreas Gohr            } elseif(is_bool($val)) {
7235de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val, PDO::PARAM_BOOL);
7245de3a6a5SAndreas Gohr            } elseif(is_null($val)) {
7255de3a6a5SAndreas Gohr                $val = 'NULL';
7265de3a6a5SAndreas Gohr            } else {
7275de3a6a5SAndreas Gohr                $val = $this->pdo->quote($val);
7285de3a6a5SAndreas Gohr            }
7295de3a6a5SAndreas Gohr            $sql = str_replace($key, $val, $sql);
7305de3a6a5SAndreas Gohr        }
7315de3a6a5SAndreas Gohr        if($htmlescape) $sql = hsc($sql);
7325de3a6a5SAndreas Gohr        return $sql;
7335de3a6a5SAndreas Gohr    }
734f64dbc90SAndreas Gohr}
735f64dbc90SAndreas Gohr
736f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et:
737