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 200cec3e2aSAndreas Gohr /** @var null|array The list of all groups */ 210cec3e2aSAndreas Gohr protected $groupcache = null; 220cec3e2aSAndreas Gohr 23f64dbc90SAndreas Gohr /** 24f64dbc90SAndreas Gohr * Constructor. 25f64dbc90SAndreas Gohr */ 26f64dbc90SAndreas Gohr public function __construct() { 27f64dbc90SAndreas Gohr parent::__construct(); // for compatibility 28f64dbc90SAndreas Gohr 29f64dbc90SAndreas Gohr if(!class_exists('PDO')) { 30f64dbc90SAndreas Gohr $this->_debug('PDO extension for PHP not found.', -1, __LINE__); 31f64dbc90SAndreas Gohr $this->success = false; 32f64dbc90SAndreas Gohr return; 33f64dbc90SAndreas Gohr } 34f64dbc90SAndreas Gohr 35f64dbc90SAndreas Gohr if(!$this->getConf('dsn')) { 36f64dbc90SAndreas Gohr $this->_debug('No DSN specified', -1, __LINE__); 37f64dbc90SAndreas Gohr $this->success = false; 38f64dbc90SAndreas Gohr return; 39f64dbc90SAndreas Gohr } 40f64dbc90SAndreas Gohr 41f64dbc90SAndreas Gohr try { 42f64dbc90SAndreas Gohr $this->pdo = new PDO( 43f64dbc90SAndreas Gohr $this->getConf('dsn'), 44f64dbc90SAndreas Gohr $this->getConf('user'), 45*0cc11d97SAndreas Gohr conf_decodeString($this->getConf('pass')), 46f64dbc90SAndreas Gohr array( 4770a89417SAndreas Gohr PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array 4870a89417SAndreas Gohr PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names 495de3a6a5SAndreas Gohr PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes 50f64dbc90SAndreas Gohr ) 51f64dbc90SAndreas Gohr ); 52f64dbc90SAndreas Gohr } catch(PDOException $e) { 53f64dbc90SAndreas Gohr $this->_debug($e); 54c27579a6SAndreas Gohr msg($this->getLang('connectfail'), -1); 55f64dbc90SAndreas Gohr $this->success = false; 56f64dbc90SAndreas Gohr return; 57f64dbc90SAndreas Gohr } 58f64dbc90SAndreas Gohr 590d586afdSAndreas Gohr // can Users be created? 600d586afdSAndreas Gohr $this->cando['addUser'] = $this->_chkcnf( 610d586afdSAndreas Gohr array( 620d586afdSAndreas Gohr 'select-user', 630d586afdSAndreas Gohr 'select-user-groups', 640d586afdSAndreas Gohr 'select-groups', 650d586afdSAndreas Gohr 'insert-user', 660d586afdSAndreas Gohr 'insert-group', 670d586afdSAndreas Gohr 'join-group' 680d586afdSAndreas Gohr ) 690d586afdSAndreas Gohr ); 70f64dbc90SAndreas Gohr 710d586afdSAndreas Gohr // can Users be deleted? 720d586afdSAndreas Gohr $this->cando['delUser'] = $this->_chkcnf( 730d586afdSAndreas Gohr array( 740d586afdSAndreas Gohr 'select-user', 750d586afdSAndreas Gohr 'select-user-groups', 760d586afdSAndreas Gohr 'select-groups', 770d586afdSAndreas Gohr 'leave-group' 780d586afdSAndreas Gohr ) 790d586afdSAndreas Gohr ); 800d586afdSAndreas Gohr 810d586afdSAndreas Gohr // can login names be changed? 820d586afdSAndreas Gohr $this->cando['modLogin'] = $this->_chkcnf( 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? 910d586afdSAndreas Gohr $this->cando['modPass'] = $this->_chkcnf( 920d586afdSAndreas Gohr array( 930d586afdSAndreas Gohr 'select-user', 940d586afdSAndreas Gohr 'select-user-groups', 950d586afdSAndreas Gohr 'update-user-pass' 960d586afdSAndreas Gohr ) 970d586afdSAndreas Gohr ); 980d586afdSAndreas Gohr 990d586afdSAndreas Gohr // can real names and emails be changed? 1000d586afdSAndreas Gohr $this->cando['modName'] = $this->cando['modMail'] = $this->_chkcnf( 1010d586afdSAndreas Gohr array( 1020d586afdSAndreas Gohr 'select-user', 1030d586afdSAndreas Gohr 'select-user-groups', 1040d586afdSAndreas Gohr 'update-user-info' 1050d586afdSAndreas Gohr ) 1060d586afdSAndreas Gohr ); 1070d586afdSAndreas Gohr 1080d586afdSAndreas Gohr // can groups be changed? 1090d586afdSAndreas Gohr $this->cando['modGroups'] = $this->_chkcnf( 1100d586afdSAndreas Gohr array( 1110d586afdSAndreas Gohr 'select-user', 1120d586afdSAndreas Gohr 'select-user-groups', 1130d586afdSAndreas Gohr 'select-groups', 1140d586afdSAndreas Gohr 'leave-group', 1150d586afdSAndreas Gohr 'join-group', 1160d586afdSAndreas Gohr 'insert-group' 1170d586afdSAndreas Gohr ) 1180d586afdSAndreas Gohr ); 1190d586afdSAndreas Gohr 1200d586afdSAndreas Gohr // can a filtered list of users be retrieved? 1210d586afdSAndreas Gohr $this->cando['getUsers'] = $this->_chkcnf( 1220d586afdSAndreas Gohr array( 1230d586afdSAndreas Gohr 'list-users' 1240d586afdSAndreas Gohr ) 1250d586afdSAndreas Gohr ); 1260d586afdSAndreas Gohr 1270d586afdSAndreas Gohr // can the number of users be retrieved? 1280d586afdSAndreas Gohr $this->cando['getUserCount'] = $this->_chkcnf( 1290d586afdSAndreas Gohr array( 1300d586afdSAndreas Gohr 'count-users' 1310d586afdSAndreas Gohr ) 1320d586afdSAndreas Gohr ); 1330d586afdSAndreas Gohr 1340d586afdSAndreas Gohr // can a list of available groups be retrieved? 1350d586afdSAndreas Gohr $this->cando['getGroups'] = $this->_chkcnf( 1360d586afdSAndreas Gohr array( 1370d586afdSAndreas Gohr 'select-groups' 1380d586afdSAndreas Gohr ) 1390d586afdSAndreas Gohr ); 1400d586afdSAndreas Gohr 141f64dbc90SAndreas Gohr $this->success = true; 142f64dbc90SAndreas Gohr } 143f64dbc90SAndreas Gohr 144f64dbc90SAndreas Gohr /** 145f64dbc90SAndreas Gohr * Check user+password 146f64dbc90SAndreas Gohr * 147f64dbc90SAndreas Gohr * @param string $user the user name 148f64dbc90SAndreas Gohr * @param string $pass the clear text password 149f64dbc90SAndreas Gohr * @return bool 150f64dbc90SAndreas Gohr */ 151f64dbc90SAndreas Gohr public function checkPass($user, $pass) { 152f64dbc90SAndreas Gohr 153f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 154f64dbc90SAndreas Gohr if($data == false) return false; 155f64dbc90SAndreas Gohr 156f64dbc90SAndreas Gohr if(isset($data['hash'])) { 157f64dbc90SAndreas Gohr // hashed password 158f64dbc90SAndreas Gohr $passhash = new PassHash(); 159f64dbc90SAndreas Gohr return $passhash->verify_hash($pass, $data['hash']); 160f64dbc90SAndreas Gohr } else { 161f64dbc90SAndreas Gohr // clear text password in the database O_o 162f64dbc90SAndreas Gohr return ($pass == $data['clear']); 163f64dbc90SAndreas Gohr } 164f64dbc90SAndreas Gohr } 165f64dbc90SAndreas Gohr 166f64dbc90SAndreas Gohr /** 167f64dbc90SAndreas Gohr * Return user info 168f64dbc90SAndreas Gohr * 169f64dbc90SAndreas Gohr * Returns info about the given user needs to contain 170f64dbc90SAndreas Gohr * at least these fields: 171f64dbc90SAndreas Gohr * 172f64dbc90SAndreas Gohr * name string full name of the user 173f64dbc90SAndreas Gohr * mail string email addres of the user 174f64dbc90SAndreas Gohr * grps array list of groups the user is in 175f64dbc90SAndreas Gohr * 176f64dbc90SAndreas Gohr * @param string $user the user name 177f64dbc90SAndreas Gohr * @param bool $requireGroups whether or not the returned data must include groups 178358942b5SAndreas Gohr * @return array|bool containing user data or false 179f64dbc90SAndreas Gohr */ 180f64dbc90SAndreas Gohr public function getUserData($user, $requireGroups = true) { 181f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 182f64dbc90SAndreas Gohr if($data == false) return false; 183f64dbc90SAndreas Gohr 18470a89417SAndreas Gohr if(isset($data['hash'])) unset($data['hash']); 18570a89417SAndreas Gohr if(isset($data['clean'])) unset($data['clean']); 186f64dbc90SAndreas Gohr 18770a89417SAndreas Gohr if($requireGroups) { 18870a89417SAndreas Gohr $data['grps'] = $this->_selectUserGroups($data); 1895de3a6a5SAndreas Gohr if($data['grps'] === false) return false; 190f64dbc90SAndreas Gohr } 191f64dbc90SAndreas Gohr 192f64dbc90SAndreas Gohr return $data; 193f64dbc90SAndreas Gohr } 194f64dbc90SAndreas Gohr 195f64dbc90SAndreas Gohr /** 196f64dbc90SAndreas Gohr * Create a new User [implement only where required/possible] 197f64dbc90SAndreas Gohr * 198f64dbc90SAndreas Gohr * Returns false if the user already exists, null when an error 199f64dbc90SAndreas Gohr * occurred and true if everything went well. 200f64dbc90SAndreas Gohr * 201f64dbc90SAndreas Gohr * The new user HAS TO be added to the default group by this 202f64dbc90SAndreas Gohr * function! 203f64dbc90SAndreas Gohr * 204f64dbc90SAndreas Gohr * Set addUser capability when implemented 205f64dbc90SAndreas Gohr * 206f64dbc90SAndreas Gohr * @param string $user 2075de3a6a5SAndreas Gohr * @param string $clear 208f64dbc90SAndreas Gohr * @param string $name 209f64dbc90SAndreas Gohr * @param string $mail 210f64dbc90SAndreas Gohr * @param null|array $grps 211f64dbc90SAndreas Gohr * @return bool|null 212f64dbc90SAndreas Gohr */ 2135de3a6a5SAndreas Gohr public function createUser($user, $clear, $name, $mail, $grps = null) { 2145de3a6a5SAndreas Gohr global $conf; 2155de3a6a5SAndreas Gohr 2165de3a6a5SAndreas Gohr if(($info = $this->getUserData($user, false)) !== false) { 2175de3a6a5SAndreas Gohr msg($this->getLang('userexists'), -1); 2185de3a6a5SAndreas Gohr return false; // user already exists 2195de3a6a5SAndreas Gohr } 2205de3a6a5SAndreas Gohr 2215de3a6a5SAndreas Gohr // prepare data 2225de3a6a5SAndreas Gohr if($grps == null) $grps = array(); 22307a11e2aSAndreas Gohr array_unshift($grps, $conf['defaultgroup']); 2245de3a6a5SAndreas Gohr $grps = array_unique($grps); 2255de3a6a5SAndreas Gohr $hash = auth_cryptPassword($clear); 2265de3a6a5SAndreas Gohr $userdata = compact('user', 'clear', 'hash', 'name', 'mail'); 2275de3a6a5SAndreas Gohr 2285de3a6a5SAndreas Gohr // action protected by transaction 2295de3a6a5SAndreas Gohr $this->pdo->beginTransaction(); 2305de3a6a5SAndreas Gohr { 2315de3a6a5SAndreas Gohr // insert the user 2325de3a6a5SAndreas Gohr $ok = $this->_query($this->getConf('insert-user'), $userdata); 2335de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2345de3a6a5SAndreas Gohr $userdata = $this->getUserData($user, false); 2355de3a6a5SAndreas Gohr if($userdata === false) goto FAIL; 2365de3a6a5SAndreas Gohr 2375de3a6a5SAndreas Gohr // create all groups that do not exist, the refetch the groups 2385de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2395de3a6a5SAndreas Gohr foreach($grps as $group) { 2405de3a6a5SAndreas Gohr if(!isset($allgroups[$group])) { 2416459f496SAndreas Gohr $ok = $this->addGroup($group); 2425de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2435de3a6a5SAndreas Gohr } 2445de3a6a5SAndreas Gohr } 2455de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2465de3a6a5SAndreas Gohr 2475de3a6a5SAndreas Gohr // add user to the groups 2485de3a6a5SAndreas Gohr foreach($grps as $group) { 2495de3a6a5SAndreas Gohr $ok = $this->_joinGroup($userdata, $allgroups[$group]); 2505de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2515de3a6a5SAndreas Gohr } 2525de3a6a5SAndreas Gohr } 2535de3a6a5SAndreas Gohr $this->pdo->commit(); 2545de3a6a5SAndreas Gohr return true; 2555de3a6a5SAndreas Gohr 2565de3a6a5SAndreas Gohr // something went wrong, rollback 2575de3a6a5SAndreas Gohr FAIL: 2585de3a6a5SAndreas Gohr $this->pdo->rollBack(); 2595de3a6a5SAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 260c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 2615de3a6a5SAndreas Gohr return null; // return error 2625de3a6a5SAndreas Gohr } 263f64dbc90SAndreas Gohr 264f64dbc90SAndreas Gohr /** 2654fb8dfabSAndreas Gohr * Modify user data 266f64dbc90SAndreas Gohr * 267f64dbc90SAndreas Gohr * @param string $user nick of the user to be changed 268f64dbc90SAndreas Gohr * @param array $changes array of field/value pairs to be changed (password will be clear text) 269f64dbc90SAndreas Gohr * @return bool 270f64dbc90SAndreas Gohr */ 2714fb8dfabSAndreas Gohr public function modifyUser($user, $changes) { 2724fb8dfabSAndreas Gohr // secure everything in transaction 2734fb8dfabSAndreas Gohr $this->pdo->beginTransaction(); 2744fb8dfabSAndreas Gohr { 2754fb8dfabSAndreas Gohr $olddata = $this->getUserData($user); 2764fb8dfabSAndreas Gohr $oldgroups = $olddata['grps']; 2774fb8dfabSAndreas Gohr unset($olddata['grps']); 2784fb8dfabSAndreas Gohr 2794fb8dfabSAndreas Gohr // changing the user name? 2804fb8dfabSAndreas Gohr if(isset($changes['user'])) { 2814fb8dfabSAndreas Gohr if($this->getUserData($changes['user'], false)) goto FAIL; 2824fb8dfabSAndreas Gohr $params = $olddata; 2834fb8dfabSAndreas Gohr $params['newlogin'] = $changes['user']; 2844fb8dfabSAndreas Gohr 2854fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-login'), $params); 2864fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 2874fb8dfabSAndreas Gohr } 2884fb8dfabSAndreas Gohr 2894fb8dfabSAndreas Gohr // changing the password? 2904fb8dfabSAndreas Gohr if(isset($changes['pass'])) { 2914fb8dfabSAndreas Gohr $params = $olddata; 2924fb8dfabSAndreas Gohr $params['clear'] = $changes['pass']; 2934fb8dfabSAndreas Gohr $params['hash'] = auth_cryptPassword($changes['pass']); 2944fb8dfabSAndreas Gohr 2954fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-pass'), $params); 2964fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 2974fb8dfabSAndreas Gohr } 2984fb8dfabSAndreas Gohr 2994fb8dfabSAndreas Gohr // changing info? 3004fb8dfabSAndreas Gohr if(isset($changes['mail']) || isset($changes['name'])) { 3014fb8dfabSAndreas Gohr $params = $olddata; 3024fb8dfabSAndreas Gohr if(isset($changes['mail'])) $params['mail'] = $changes['mail']; 3034fb8dfabSAndreas Gohr if(isset($changes['name'])) $params['name'] = $changes['name']; 3044fb8dfabSAndreas Gohr 3054fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-info'), $params); 3064fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3074fb8dfabSAndreas Gohr } 3084fb8dfabSAndreas Gohr 3094fb8dfabSAndreas Gohr // changing groups? 3104fb8dfabSAndreas Gohr if(isset($changes['grps'])) { 3114fb8dfabSAndreas Gohr $allgroups = $this->_selectGroups(); 3124fb8dfabSAndreas Gohr 3134fb8dfabSAndreas Gohr // remove membership for previous groups 3144fb8dfabSAndreas Gohr foreach($oldgroups as $group) { 315358942b5SAndreas Gohr if(!in_array($group, $changes['grps']) && isset($allgroups[$group])) { 3164fb8dfabSAndreas Gohr $ok = $this->_leaveGroup($olddata, $allgroups[$group]); 3174fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3184fb8dfabSAndreas Gohr } 3194fb8dfabSAndreas Gohr } 3204fb8dfabSAndreas Gohr 3214fb8dfabSAndreas Gohr // create all new groups that are missing 3224fb8dfabSAndreas Gohr $added = 0; 3234fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3244fb8dfabSAndreas Gohr if(!isset($allgroups[$group])) { 3256459f496SAndreas Gohr $ok = $this->addGroup($group); 3264fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3274fb8dfabSAndreas Gohr $added++; 3284fb8dfabSAndreas Gohr } 3294fb8dfabSAndreas Gohr } 3304fb8dfabSAndreas Gohr // reload group info 3314fb8dfabSAndreas Gohr if($added > 0) $allgroups = $this->_selectGroups(); 3324fb8dfabSAndreas Gohr 3334fb8dfabSAndreas Gohr // add membership for new groups 3344fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3354fb8dfabSAndreas Gohr if(!in_array($group, $oldgroups)) { 3364fb8dfabSAndreas Gohr $ok = $this->_joinGroup($olddata, $allgroups[$group]); 3374fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3384fb8dfabSAndreas Gohr } 3394fb8dfabSAndreas Gohr } 3404fb8dfabSAndreas Gohr } 3414fb8dfabSAndreas Gohr 3424fb8dfabSAndreas Gohr } 3434fb8dfabSAndreas Gohr $this->pdo->commit(); 3444fb8dfabSAndreas Gohr return true; 3454fb8dfabSAndreas Gohr 3464fb8dfabSAndreas Gohr // something went wrong, rollback 3474fb8dfabSAndreas Gohr FAIL: 3484fb8dfabSAndreas Gohr $this->pdo->rollBack(); 3494fb8dfabSAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 350c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 3514fb8dfabSAndreas Gohr return false; // return error 3524fb8dfabSAndreas Gohr } 353f64dbc90SAndreas Gohr 354f64dbc90SAndreas Gohr /** 355e19be516SAndreas Gohr * Delete one or more users 356f64dbc90SAndreas Gohr * 357f64dbc90SAndreas Gohr * Set delUser capability when implemented 358f64dbc90SAndreas Gohr * 359f64dbc90SAndreas Gohr * @param array $users 360f64dbc90SAndreas Gohr * @return int number of users deleted 361f64dbc90SAndreas Gohr */ 362e19be516SAndreas Gohr public function deleteUsers($users) { 363e19be516SAndreas Gohr $count = 0; 364e19be516SAndreas Gohr foreach($users as $user) { 365e19be516SAndreas Gohr if($this->_deleteUser($user)) $count++; 366e19be516SAndreas Gohr } 367e19be516SAndreas Gohr return $count; 368e19be516SAndreas Gohr } 369f64dbc90SAndreas Gohr 370f64dbc90SAndreas Gohr /** 371f64dbc90SAndreas Gohr * Bulk retrieval of user data [implement only where required/possible] 372f64dbc90SAndreas Gohr * 373f64dbc90SAndreas Gohr * Set getUsers capability when implemented 374f64dbc90SAndreas Gohr * 375f64dbc90SAndreas Gohr * @param int $start index of first user to be returned 376f64dbc90SAndreas Gohr * @param int $limit max number of users to be returned 377f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 378f64dbc90SAndreas Gohr * @return array list of userinfo (refer getUserData for internal userinfo details) 379f64dbc90SAndreas Gohr */ 3806459f496SAndreas Gohr public function retrieveUsers($start = 0, $limit = -1, $filter = null) { 3816459f496SAndreas Gohr if($limit < 0) $limit = 10000; // we don't support no limit 3826459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 3836459f496SAndreas Gohr 3846459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 3856459f496SAndreas Gohr if(!isset($filter[$key])) { 3866459f496SAndreas Gohr $filter[$key] = '%'; 3876459f496SAndreas Gohr } else { 3886459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 3896459f496SAndreas Gohr } 3906459f496SAndreas Gohr } 39114119d44SAndreas Gohr $filter['start'] = (int) $start; 39214119d44SAndreas Gohr $filter['end'] = (int) $start + $limit; 39314119d44SAndreas Gohr $filter['limit'] = (int) $limit; 3946459f496SAndreas Gohr 3956459f496SAndreas Gohr $result = $this->_query($this->getConf('list-users'), $filter); 3966459f496SAndreas Gohr if(!$result) return array(); 3976459f496SAndreas Gohr $users = array(); 3986459f496SAndreas Gohr foreach($result as $row) { 3996459f496SAndreas Gohr if(!isset($row['user'])) { 4006459f496SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 4016459f496SAndreas Gohr return array(); 4026459f496SAndreas Gohr } 4036459f496SAndreas Gohr $users[] = $row['user']; 4046459f496SAndreas Gohr } 4056459f496SAndreas Gohr return $users; 4066459f496SAndreas Gohr } 407f64dbc90SAndreas Gohr 408f64dbc90SAndreas Gohr /** 409f64dbc90SAndreas Gohr * Return a count of the number of user which meet $filter criteria 410f64dbc90SAndreas Gohr * 411f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, empty array for no filter 412f64dbc90SAndreas Gohr * @return int 413f64dbc90SAndreas Gohr */ 4146459f496SAndreas Gohr public function getUserCount($filter = array()) { 4156459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 4166459f496SAndreas Gohr 4176459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 4186459f496SAndreas Gohr if(!isset($filter[$key])) { 4196459f496SAndreas Gohr $filter[$key] = '%'; 4206459f496SAndreas Gohr } else { 4216459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 4226459f496SAndreas Gohr } 4236459f496SAndreas Gohr } 4246459f496SAndreas Gohr 4256459f496SAndreas Gohr $result = $this->_query($this->getConf('count-users'), $filter); 4266459f496SAndreas Gohr if(!$result || !isset($result[0]['count'])) { 4276459f496SAndreas Gohr $this->_debug("Statement did not return 'count' attribute", -1, __LINE__); 4286459f496SAndreas Gohr } 4296459f496SAndreas Gohr return isset($result[0]['count']); 4306459f496SAndreas Gohr } 431f64dbc90SAndreas Gohr 432f64dbc90SAndreas Gohr /** 4336459f496SAndreas Gohr * Create a new group with the given name 434f64dbc90SAndreas Gohr * 435f64dbc90SAndreas Gohr * @param string $group 436f64dbc90SAndreas Gohr * @return bool 437f64dbc90SAndreas Gohr */ 4386459f496SAndreas Gohr public function addGroup($group) { 4396459f496SAndreas Gohr $sql = $this->getConf('insert-group'); 4406459f496SAndreas Gohr 4416459f496SAndreas Gohr $result = $this->_query($sql, array(':group' => $group)); 4420cec3e2aSAndreas Gohr $this->_clearGroupCache(); 4436459f496SAndreas Gohr if($result === false) return false; 4446459f496SAndreas Gohr return true; 4456459f496SAndreas Gohr } 446f64dbc90SAndreas Gohr 447f64dbc90SAndreas Gohr /** 4485de3a6a5SAndreas Gohr * Retrieve groups 449f64dbc90SAndreas Gohr * 450f64dbc90SAndreas Gohr * Set getGroups capability when implemented 451f64dbc90SAndreas Gohr * 452f64dbc90SAndreas Gohr * @param int $start 453f64dbc90SAndreas Gohr * @param int $limit 454f64dbc90SAndreas Gohr * @return array 455f64dbc90SAndreas Gohr */ 4565de3a6a5SAndreas Gohr public function retrieveGroups($start = 0, $limit = 0) { 4575de3a6a5SAndreas Gohr $groups = array_keys($this->_selectGroups()); 4585de3a6a5SAndreas Gohr if($groups === false) return array(); 459f64dbc90SAndreas Gohr 4605de3a6a5SAndreas Gohr if(!$limit) { 4615de3a6a5SAndreas Gohr return array_splice($groups, $start); 4625de3a6a5SAndreas Gohr } else { 4635de3a6a5SAndreas Gohr return array_splice($groups, $start, $limit); 464f64dbc90SAndreas Gohr } 465f64dbc90SAndreas Gohr } 466f64dbc90SAndreas Gohr 467f64dbc90SAndreas Gohr /** 468f64dbc90SAndreas Gohr * Select data of a specified user 469f64dbc90SAndreas Gohr * 4705de3a6a5SAndreas Gohr * @param string $user the user name 4715de3a6a5SAndreas Gohr * @return bool|array user data, false on error 472f64dbc90SAndreas Gohr */ 473f64dbc90SAndreas Gohr protected function _selectUser($user) { 474f64dbc90SAndreas Gohr $sql = $this->getConf('select-user'); 475f64dbc90SAndreas Gohr 4765de3a6a5SAndreas Gohr $result = $this->_query($sql, array(':user' => $user)); 47770a89417SAndreas Gohr if(!$result) return false; 478f64dbc90SAndreas Gohr 47970a89417SAndreas Gohr if(count($result) > 1) { 480f64dbc90SAndreas Gohr $this->_debug('Found more than one matching user', -1, __LINE__); 481f64dbc90SAndreas Gohr return false; 482f64dbc90SAndreas Gohr } 483f64dbc90SAndreas Gohr 484f64dbc90SAndreas Gohr $data = array_shift($result); 485f64dbc90SAndreas Gohr $dataok = true; 486f64dbc90SAndreas Gohr 487f64dbc90SAndreas Gohr if(!isset($data['user'])) { 488f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 489f64dbc90SAndreas Gohr $dataok = false; 490f64dbc90SAndreas Gohr } 491f64dbc90SAndreas Gohr if(!isset($data['hash']) && !isset($data['clear'])) { 492f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__); 493f64dbc90SAndreas Gohr $dataok = false; 494f64dbc90SAndreas Gohr } 495f64dbc90SAndreas Gohr if(!isset($data['name'])) { 496f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'name' attribute", -1, __LINE__); 497f64dbc90SAndreas Gohr $dataok = false; 498f64dbc90SAndreas Gohr } 499f64dbc90SAndreas Gohr if(!isset($data['mail'])) { 500f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'mail' attribute", -1, __LINE__); 501f64dbc90SAndreas Gohr $dataok = false; 502f64dbc90SAndreas Gohr } 503f64dbc90SAndreas Gohr 504f64dbc90SAndreas Gohr if(!$dataok) return false; 505f64dbc90SAndreas Gohr return $data; 506f64dbc90SAndreas Gohr } 507f64dbc90SAndreas Gohr 508f64dbc90SAndreas Gohr /** 509e19be516SAndreas Gohr * Delete a user after removing all their group memberships 510e19be516SAndreas Gohr * 511e19be516SAndreas Gohr * @param string $user 512e19be516SAndreas Gohr * @return bool true when the user was deleted 513e19be516SAndreas Gohr */ 514e19be516SAndreas Gohr protected function _deleteUser($user) { 515e19be516SAndreas Gohr $this->pdo->beginTransaction(); 516e19be516SAndreas Gohr { 517e19be516SAndreas Gohr $userdata = $this->getUserData($user); 518e19be516SAndreas Gohr if($userdata === false) goto FAIL; 519e19be516SAndreas Gohr $allgroups = $this->_selectGroups(); 520e19be516SAndreas Gohr 521e19be516SAndreas Gohr // remove group memberships (ignore errors) 522e19be516SAndreas Gohr foreach($userdata['grps'] as $group) { 523358942b5SAndreas Gohr if(isset($allgroups[$group])) { 524e19be516SAndreas Gohr $this->_leaveGroup($userdata, $allgroups[$group]); 525e19be516SAndreas Gohr } 526358942b5SAndreas Gohr } 527e19be516SAndreas Gohr 528e19be516SAndreas Gohr $ok = $this->_query($this->getConf('delete-user'), $userdata); 529e19be516SAndreas Gohr if($ok === false) goto FAIL; 530e19be516SAndreas Gohr } 531e19be516SAndreas Gohr $this->pdo->commit(); 532e19be516SAndreas Gohr return true; 533e19be516SAndreas Gohr 534e19be516SAndreas Gohr FAIL: 535e19be516SAndreas Gohr $this->pdo->rollBack(); 536e19be516SAndreas Gohr return false; 537e19be516SAndreas Gohr } 538e19be516SAndreas Gohr 539e19be516SAndreas Gohr /** 54070a89417SAndreas Gohr * Select all groups of a user 54170a89417SAndreas Gohr * 54270a89417SAndreas Gohr * @param array $userdata The userdata as returned by _selectUser() 5435de3a6a5SAndreas Gohr * @return array|bool list of group names, false on error 54470a89417SAndreas Gohr */ 54570a89417SAndreas Gohr protected function _selectUserGroups($userdata) { 54670a89417SAndreas Gohr global $conf; 54770a89417SAndreas Gohr $sql = $this->getConf('select-user-groups'); 5485de3a6a5SAndreas Gohr $result = $this->_query($sql, $userdata); 5495de3a6a5SAndreas Gohr if($result === false) return false; 55070a89417SAndreas Gohr 55170a89417SAndreas Gohr $groups = array($conf['defaultgroup']); // always add default config 5525de3a6a5SAndreas Gohr foreach($result as $row) { 5535de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5545de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned in select-user-groups statement"); 5555de3a6a5SAndreas Gohr return false; 5565de3a6a5SAndreas Gohr } 55770a89417SAndreas Gohr $groups[] = $row['group']; 55870a89417SAndreas Gohr } 55970a89417SAndreas Gohr 56070a89417SAndreas Gohr $groups = array_unique($groups); 56170a89417SAndreas Gohr sort($groups); 56270a89417SAndreas Gohr return $groups; 56370a89417SAndreas Gohr } 56470a89417SAndreas Gohr 56570a89417SAndreas Gohr /** 5665de3a6a5SAndreas Gohr * Select all available groups 5675de3a6a5SAndreas Gohr * 5685de3a6a5SAndreas Gohr * @return array|bool list of all available groups and their properties 5695de3a6a5SAndreas Gohr */ 5705de3a6a5SAndreas Gohr protected function _selectGroups() { 5710cec3e2aSAndreas Gohr if($this->groupcache) return $this->groupcache; 5720cec3e2aSAndreas Gohr 5735de3a6a5SAndreas Gohr $sql = $this->getConf('select-groups'); 5745de3a6a5SAndreas Gohr $result = $this->_query($sql); 5755de3a6a5SAndreas Gohr if($result === false) return false; 5765de3a6a5SAndreas Gohr 5775de3a6a5SAndreas Gohr $groups = array(); 5785de3a6a5SAndreas Gohr foreach($result as $row) { 5795de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5805de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned from select-groups statement", -1, __LINE__); 5815de3a6a5SAndreas Gohr return false; 5825de3a6a5SAndreas Gohr } 5835de3a6a5SAndreas Gohr 5845de3a6a5SAndreas Gohr // relayout result with group name as key 5855de3a6a5SAndreas Gohr $group = $row['group']; 5865de3a6a5SAndreas Gohr $groups[$group] = $row; 5875de3a6a5SAndreas Gohr } 5885de3a6a5SAndreas Gohr 5895de3a6a5SAndreas Gohr ksort($groups); 5905de3a6a5SAndreas Gohr return $groups; 5915de3a6a5SAndreas Gohr } 5925de3a6a5SAndreas Gohr 5935de3a6a5SAndreas Gohr /** 5940cec3e2aSAndreas Gohr * Remove all entries from the group cache 5950cec3e2aSAndreas Gohr */ 5960cec3e2aSAndreas Gohr protected function _clearGroupCache() { 5970cec3e2aSAndreas Gohr $this->groupcache = null; 5980cec3e2aSAndreas Gohr } 5990cec3e2aSAndreas Gohr 6000cec3e2aSAndreas Gohr /** 6014fb8dfabSAndreas Gohr * Adds the user to the group 6025de3a6a5SAndreas Gohr * 6035de3a6a5SAndreas Gohr * @param array $userdata all the user data 6045de3a6a5SAndreas Gohr * @param array $groupdata all the group data 6055de3a6a5SAndreas Gohr * @return bool 6065de3a6a5SAndreas Gohr */ 6075de3a6a5SAndreas Gohr protected function _joinGroup($userdata, $groupdata) { 6085de3a6a5SAndreas Gohr $data = array_merge($userdata, $groupdata); 6095de3a6a5SAndreas Gohr $sql = $this->getConf('join-group'); 6105de3a6a5SAndreas Gohr $result = $this->_query($sql, $data); 6115de3a6a5SAndreas Gohr if($result === false) return false; 6125de3a6a5SAndreas Gohr return true; 6135de3a6a5SAndreas Gohr } 6145de3a6a5SAndreas Gohr 6155de3a6a5SAndreas Gohr /** 6164fb8dfabSAndreas Gohr * Removes the user from the group 6174fb8dfabSAndreas Gohr * 6184fb8dfabSAndreas Gohr * @param array $userdata all the user data 6194fb8dfabSAndreas Gohr * @param array $groupdata all the group data 6204fb8dfabSAndreas Gohr * @return bool 6214fb8dfabSAndreas Gohr */ 6224fb8dfabSAndreas Gohr protected function _leaveGroup($userdata, $groupdata) { 6234fb8dfabSAndreas Gohr $data = array_merge($userdata, $groupdata); 6244fb8dfabSAndreas Gohr $sql = $this->getConf('leave-group'); 6254fb8dfabSAndreas Gohr $result = $this->_query($sql, $data); 6264fb8dfabSAndreas Gohr if($result === false) return false; 6274fb8dfabSAndreas Gohr return true; 6284fb8dfabSAndreas Gohr } 6294fb8dfabSAndreas Gohr 6304fb8dfabSAndreas Gohr /** 63170a89417SAndreas Gohr * Executes a query 63270a89417SAndreas Gohr * 63370a89417SAndreas Gohr * @param string $sql The SQL statement to execute 63470a89417SAndreas Gohr * @param array $arguments Named parameters to be used in the statement 635f695c447SAndreas Gohr * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error 63670a89417SAndreas Gohr */ 6375de3a6a5SAndreas Gohr protected function _query($sql, $arguments = array()) { 638f695c447SAndreas Gohr $sql = trim($sql); 6395de3a6a5SAndreas Gohr if(empty($sql)) { 6405de3a6a5SAndreas Gohr $this->_debug('No SQL query given', -1, __LINE__); 6415de3a6a5SAndreas Gohr return false; 6425de3a6a5SAndreas Gohr } 6435de3a6a5SAndreas Gohr 64414119d44SAndreas Gohr // execute 64570a89417SAndreas Gohr $params = array(); 64614119d44SAndreas Gohr $sth = $this->pdo->prepare($sql); 64714119d44SAndreas Gohr try { 64814119d44SAndreas Gohr // prepare parameters - we only use those that exist in the SQL 64970a89417SAndreas Gohr foreach($arguments as $key => $value) { 65070a89417SAndreas Gohr if(is_array($value)) continue; 65170a89417SAndreas Gohr if(is_object($value)) continue; 65270a89417SAndreas Gohr if($key[0] != ':') $key = ":$key"; // prefix with colon if needed 65314119d44SAndreas Gohr if(strpos($sql, $key) === false) continue; // skip if parameter is missing 65414119d44SAndreas Gohr 65514119d44SAndreas Gohr if(is_int($value)) { 65614119d44SAndreas Gohr $sth->bindValue($key, $value, PDO::PARAM_INT); 65714119d44SAndreas Gohr } else { 65814119d44SAndreas Gohr $sth->bindValue($key, $value); 65914119d44SAndreas Gohr } 66014119d44SAndreas Gohr $param[$key] = $value; //remember for debugging 66170a89417SAndreas Gohr } 66270a89417SAndreas Gohr 66314119d44SAndreas Gohr $sth->execute(); 664f695c447SAndreas Gohr if(strtolower(substr($sql, 0, 6)) == 'select') { 66570a89417SAndreas Gohr $result = $sth->fetchAll(); 666f695c447SAndreas Gohr } else { 667f695c447SAndreas Gohr $result = $sth->rowCount(); 668f695c447SAndreas Gohr } 6695de3a6a5SAndreas Gohr } catch(Exception $e) { 6704fb8dfabSAndreas Gohr // report the caller's line 6714fb8dfabSAndreas Gohr $trace = debug_backtrace(); 6724fb8dfabSAndreas Gohr $line = $trace[0]['line']; 6735de3a6a5SAndreas Gohr $dsql = $this->_debugSQL($sql, $params, !defined('DOKU_UNITTEST')); 6744fb8dfabSAndreas Gohr $this->_debug($e, -1, $line); 6754fb8dfabSAndreas Gohr $this->_debug("SQL: <pre>$dsql</pre>", -1, $line); 67670a89417SAndreas Gohr $result = false; 6771600c7ccSAndreas Gohr } 67870a89417SAndreas Gohr $sth->closeCursor(); 67970a89417SAndreas Gohr $sth = null; 68070a89417SAndreas Gohr 6815de3a6a5SAndreas Gohr return $result; 6825de3a6a5SAndreas Gohr } 68370a89417SAndreas Gohr 68470a89417SAndreas Gohr /** 685f64dbc90SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 686f64dbc90SAndreas Gohr * 687f64dbc90SAndreas Gohr * @param string|Exception $message 688f64dbc90SAndreas Gohr * @param int $err 689f64dbc90SAndreas Gohr * @param int $line 690f64dbc90SAndreas Gohr */ 691f64dbc90SAndreas Gohr protected function _debug($message, $err = 0, $line = 0) { 692f64dbc90SAndreas Gohr if(!$this->getConf('debug')) return; 693f64dbc90SAndreas Gohr if(is_a($message, 'Exception')) { 694f64dbc90SAndreas Gohr $err = -1; 695f64dbc90SAndreas Gohr $msg = $message->getMessage(); 6964fb8dfabSAndreas Gohr if(!$line) $line = $message->getLine(); 697f64dbc90SAndreas Gohr } else { 698f64dbc90SAndreas Gohr $msg = $message; 699f64dbc90SAndreas Gohr } 700f64dbc90SAndreas Gohr 701f64dbc90SAndreas Gohr if(defined('DOKU_UNITTEST')) { 702f64dbc90SAndreas Gohr printf("\n%s, %s:%d\n", $msg, __FILE__, $line); 703f64dbc90SAndreas Gohr } else { 704f64dbc90SAndreas Gohr msg('authpdo: ' . $msg, $err, $line, __FILE__); 705f64dbc90SAndreas Gohr } 706f64dbc90SAndreas Gohr } 7075de3a6a5SAndreas Gohr 7085de3a6a5SAndreas Gohr /** 7095de3a6a5SAndreas Gohr * Check if the given config strings are set 7105de3a6a5SAndreas Gohr * 7115de3a6a5SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 7125de3a6a5SAndreas Gohr * 7135de3a6a5SAndreas Gohr * @param string[] $keys 7145de3a6a5SAndreas Gohr * @return bool 7155de3a6a5SAndreas Gohr */ 7165de3a6a5SAndreas Gohr protected function _chkcnf($keys) { 7175de3a6a5SAndreas Gohr foreach($keys as $key) { 718745947b2SAndreas Gohr if(!trim($this->getConf($key))) return false; 7195de3a6a5SAndreas Gohr } 7205de3a6a5SAndreas Gohr 7215de3a6a5SAndreas Gohr return true; 7225de3a6a5SAndreas Gohr } 7235de3a6a5SAndreas Gohr 7245de3a6a5SAndreas Gohr /** 7255de3a6a5SAndreas Gohr * create an approximation of the SQL string with parameters replaced 7265de3a6a5SAndreas Gohr * 7275de3a6a5SAndreas Gohr * @param string $sql 7285de3a6a5SAndreas Gohr * @param array $params 7295de3a6a5SAndreas Gohr * @param bool $htmlescape Should the result be escaped for output in HTML? 7305de3a6a5SAndreas Gohr * @return string 7315de3a6a5SAndreas Gohr */ 7325de3a6a5SAndreas Gohr protected function _debugSQL($sql, $params, $htmlescape = true) { 7335de3a6a5SAndreas Gohr foreach($params as $key => $val) { 7345de3a6a5SAndreas Gohr if(is_int($val)) { 7355de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_INT); 7365de3a6a5SAndreas Gohr } elseif(is_bool($val)) { 7375de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_BOOL); 7385de3a6a5SAndreas Gohr } elseif(is_null($val)) { 7395de3a6a5SAndreas Gohr $val = 'NULL'; 7405de3a6a5SAndreas Gohr } else { 7415de3a6a5SAndreas Gohr $val = $this->pdo->quote($val); 7425de3a6a5SAndreas Gohr } 7435de3a6a5SAndreas Gohr $sql = str_replace($key, $val, $sql); 7445de3a6a5SAndreas Gohr } 7455de3a6a5SAndreas Gohr if($htmlescape) $sql = hsc($sql); 7465de3a6a5SAndreas Gohr return $sql; 7475de3a6a5SAndreas Gohr } 748f64dbc90SAndreas Gohr} 749f64dbc90SAndreas Gohr 750f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et: 751