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'), 450cc11d97SAndreas 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', 777f89f089SAndreas Gohr 'leave-group', 787f89f089SAndreas Gohr 'delete-user' 790d586afdSAndreas Gohr ) 800d586afdSAndreas Gohr ); 810d586afdSAndreas Gohr 820d586afdSAndreas Gohr // can login names be changed? 830d586afdSAndreas Gohr $this->cando['modLogin'] = $this->_chkcnf( 840d586afdSAndreas Gohr array( 850d586afdSAndreas Gohr 'select-user', 860d586afdSAndreas Gohr 'select-user-groups', 870d586afdSAndreas Gohr 'update-user-login' 880d586afdSAndreas Gohr ) 890d586afdSAndreas Gohr ); 900d586afdSAndreas Gohr 910d586afdSAndreas Gohr // can passwords be changed? 920d586afdSAndreas Gohr $this->cando['modPass'] = $this->_chkcnf( 930d586afdSAndreas Gohr array( 940d586afdSAndreas Gohr 'select-user', 950d586afdSAndreas Gohr 'select-user-groups', 960d586afdSAndreas Gohr 'update-user-pass' 970d586afdSAndreas Gohr ) 980d586afdSAndreas Gohr ); 990d586afdSAndreas Gohr 100*ad4d5631SAndreas Gohr // can real names be changed? 101*ad4d5631SAndreas Gohr $this->cando['modName'] = $this->_chkcnf( 1020d586afdSAndreas Gohr array( 1030d586afdSAndreas Gohr 'select-user', 1040d586afdSAndreas Gohr 'select-user-groups', 105*ad4d5631SAndreas Gohr 'update-user-info:name' 106*ad4d5631SAndreas Gohr ) 107*ad4d5631SAndreas Gohr ); 108*ad4d5631SAndreas Gohr 109*ad4d5631SAndreas Gohr // can real email be changed? 110*ad4d5631SAndreas Gohr $this->cando['modMail'] = $this->_chkcnf( 111*ad4d5631SAndreas Gohr array( 112*ad4d5631SAndreas Gohr 'select-user', 113*ad4d5631SAndreas Gohr 'select-user-groups', 114*ad4d5631SAndreas Gohr 'update-user-info:mail' 1150d586afdSAndreas Gohr ) 1160d586afdSAndreas Gohr ); 1170d586afdSAndreas Gohr 1180d586afdSAndreas Gohr // can groups be changed? 1190d586afdSAndreas Gohr $this->cando['modGroups'] = $this->_chkcnf( 1200d586afdSAndreas Gohr array( 1210d586afdSAndreas Gohr 'select-user', 1220d586afdSAndreas Gohr 'select-user-groups', 1230d586afdSAndreas Gohr 'select-groups', 1240d586afdSAndreas Gohr 'leave-group', 1250d586afdSAndreas Gohr 'join-group', 1260d586afdSAndreas Gohr 'insert-group' 1270d586afdSAndreas Gohr ) 1280d586afdSAndreas Gohr ); 1290d586afdSAndreas Gohr 1300d586afdSAndreas Gohr // can a filtered list of users be retrieved? 1310d586afdSAndreas Gohr $this->cando['getUsers'] = $this->_chkcnf( 1320d586afdSAndreas Gohr array( 1330d586afdSAndreas Gohr 'list-users' 1340d586afdSAndreas Gohr ) 1350d586afdSAndreas Gohr ); 1360d586afdSAndreas Gohr 1370d586afdSAndreas Gohr // can the number of users be retrieved? 1380d586afdSAndreas Gohr $this->cando['getUserCount'] = $this->_chkcnf( 1390d586afdSAndreas Gohr array( 1400d586afdSAndreas Gohr 'count-users' 1410d586afdSAndreas Gohr ) 1420d586afdSAndreas Gohr ); 1430d586afdSAndreas Gohr 1440d586afdSAndreas Gohr // can a list of available groups be retrieved? 1450d586afdSAndreas Gohr $this->cando['getGroups'] = $this->_chkcnf( 1460d586afdSAndreas Gohr array( 1470d586afdSAndreas Gohr 'select-groups' 1480d586afdSAndreas Gohr ) 1490d586afdSAndreas Gohr ); 1500d586afdSAndreas Gohr 151f64dbc90SAndreas Gohr $this->success = true; 152f64dbc90SAndreas Gohr } 153f64dbc90SAndreas Gohr 154f64dbc90SAndreas Gohr /** 155f64dbc90SAndreas Gohr * Check user+password 156f64dbc90SAndreas Gohr * 157f64dbc90SAndreas Gohr * @param string $user the user name 158f64dbc90SAndreas Gohr * @param string $pass the clear text password 159f64dbc90SAndreas Gohr * @return bool 160f64dbc90SAndreas Gohr */ 161f64dbc90SAndreas Gohr public function checkPass($user, $pass) { 162f64dbc90SAndreas Gohr 163f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 164f64dbc90SAndreas Gohr if($data == false) return false; 165f64dbc90SAndreas Gohr 166f64dbc90SAndreas Gohr if(isset($data['hash'])) { 167f64dbc90SAndreas Gohr // hashed password 168f64dbc90SAndreas Gohr $passhash = new PassHash(); 169f64dbc90SAndreas Gohr return $passhash->verify_hash($pass, $data['hash']); 170f64dbc90SAndreas Gohr } else { 171f64dbc90SAndreas Gohr // clear text password in the database O_o 172f64dbc90SAndreas Gohr return ($pass == $data['clear']); 173f64dbc90SAndreas Gohr } 174f64dbc90SAndreas Gohr } 175f64dbc90SAndreas Gohr 176f64dbc90SAndreas Gohr /** 177f64dbc90SAndreas Gohr * Return user info 178f64dbc90SAndreas Gohr * 179f64dbc90SAndreas Gohr * Returns info about the given user needs to contain 180f64dbc90SAndreas Gohr * at least these fields: 181f64dbc90SAndreas Gohr * 182f64dbc90SAndreas Gohr * name string full name of the user 183f64dbc90SAndreas Gohr * mail string email addres of the user 184f64dbc90SAndreas Gohr * grps array list of groups the user is in 185f64dbc90SAndreas Gohr * 186f64dbc90SAndreas Gohr * @param string $user the user name 187f64dbc90SAndreas Gohr * @param bool $requireGroups whether or not the returned data must include groups 188358942b5SAndreas Gohr * @return array|bool containing user data or false 189f64dbc90SAndreas Gohr */ 190f64dbc90SAndreas Gohr public function getUserData($user, $requireGroups = true) { 191f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 192f64dbc90SAndreas Gohr if($data == false) return false; 193f64dbc90SAndreas Gohr 19470a89417SAndreas Gohr if(isset($data['hash'])) unset($data['hash']); 19570a89417SAndreas Gohr if(isset($data['clean'])) unset($data['clean']); 196f64dbc90SAndreas Gohr 19770a89417SAndreas Gohr if($requireGroups) { 19870a89417SAndreas Gohr $data['grps'] = $this->_selectUserGroups($data); 1995de3a6a5SAndreas Gohr if($data['grps'] === false) return false; 200f64dbc90SAndreas Gohr } 201f64dbc90SAndreas Gohr 202f64dbc90SAndreas Gohr return $data; 203f64dbc90SAndreas Gohr } 204f64dbc90SAndreas Gohr 205f64dbc90SAndreas Gohr /** 206f64dbc90SAndreas Gohr * Create a new User [implement only where required/possible] 207f64dbc90SAndreas Gohr * 208f64dbc90SAndreas Gohr * Returns false if the user already exists, null when an error 209f64dbc90SAndreas Gohr * occurred and true if everything went well. 210f64dbc90SAndreas Gohr * 211f64dbc90SAndreas Gohr * The new user HAS TO be added to the default group by this 212f64dbc90SAndreas Gohr * function! 213f64dbc90SAndreas Gohr * 214f64dbc90SAndreas Gohr * Set addUser capability when implemented 215f64dbc90SAndreas Gohr * 216f64dbc90SAndreas Gohr * @param string $user 2175de3a6a5SAndreas Gohr * @param string $clear 218f64dbc90SAndreas Gohr * @param string $name 219f64dbc90SAndreas Gohr * @param string $mail 220f64dbc90SAndreas Gohr * @param null|array $grps 221f64dbc90SAndreas Gohr * @return bool|null 222f64dbc90SAndreas Gohr */ 2235de3a6a5SAndreas Gohr public function createUser($user, $clear, $name, $mail, $grps = null) { 2245de3a6a5SAndreas Gohr global $conf; 2255de3a6a5SAndreas Gohr 2265de3a6a5SAndreas Gohr if(($info = $this->getUserData($user, false)) !== false) { 2275de3a6a5SAndreas Gohr msg($this->getLang('userexists'), -1); 2285de3a6a5SAndreas Gohr return false; // user already exists 2295de3a6a5SAndreas Gohr } 2305de3a6a5SAndreas Gohr 2315de3a6a5SAndreas Gohr // prepare data 2325de3a6a5SAndreas Gohr if($grps == null) $grps = array(); 23307a11e2aSAndreas Gohr array_unshift($grps, $conf['defaultgroup']); 2345de3a6a5SAndreas Gohr $grps = array_unique($grps); 2355de3a6a5SAndreas Gohr $hash = auth_cryptPassword($clear); 2365de3a6a5SAndreas Gohr $userdata = compact('user', 'clear', 'hash', 'name', 'mail'); 2375de3a6a5SAndreas Gohr 2385de3a6a5SAndreas Gohr // action protected by transaction 2395de3a6a5SAndreas Gohr $this->pdo->beginTransaction(); 2405de3a6a5SAndreas Gohr { 2415de3a6a5SAndreas Gohr // insert the user 2425de3a6a5SAndreas Gohr $ok = $this->_query($this->getConf('insert-user'), $userdata); 2435de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2445de3a6a5SAndreas Gohr $userdata = $this->getUserData($user, false); 2455de3a6a5SAndreas Gohr if($userdata === false) goto FAIL; 2465de3a6a5SAndreas Gohr 2475de3a6a5SAndreas Gohr // create all groups that do not exist, the refetch the groups 2485de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2495de3a6a5SAndreas Gohr foreach($grps as $group) { 2505de3a6a5SAndreas Gohr if(!isset($allgroups[$group])) { 2516459f496SAndreas Gohr $ok = $this->addGroup($group); 2525de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2535de3a6a5SAndreas Gohr } 2545de3a6a5SAndreas Gohr } 2555de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2565de3a6a5SAndreas Gohr 2575de3a6a5SAndreas Gohr // add user to the groups 2585de3a6a5SAndreas Gohr foreach($grps as $group) { 2595de3a6a5SAndreas Gohr $ok = $this->_joinGroup($userdata, $allgroups[$group]); 2605de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2615de3a6a5SAndreas Gohr } 2625de3a6a5SAndreas Gohr } 2635de3a6a5SAndreas Gohr $this->pdo->commit(); 2645de3a6a5SAndreas Gohr return true; 2655de3a6a5SAndreas Gohr 2665de3a6a5SAndreas Gohr // something went wrong, rollback 2675de3a6a5SAndreas Gohr FAIL: 2685de3a6a5SAndreas Gohr $this->pdo->rollBack(); 2695de3a6a5SAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 270c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 2715de3a6a5SAndreas Gohr return null; // return error 2725de3a6a5SAndreas Gohr } 273f64dbc90SAndreas Gohr 274f64dbc90SAndreas Gohr /** 2754fb8dfabSAndreas Gohr * Modify user data 276f64dbc90SAndreas Gohr * 277f64dbc90SAndreas Gohr * @param string $user nick of the user to be changed 278f64dbc90SAndreas Gohr * @param array $changes array of field/value pairs to be changed (password will be clear text) 279f64dbc90SAndreas Gohr * @return bool 280f64dbc90SAndreas Gohr */ 2814fb8dfabSAndreas Gohr public function modifyUser($user, $changes) { 2824fb8dfabSAndreas Gohr // secure everything in transaction 2834fb8dfabSAndreas Gohr $this->pdo->beginTransaction(); 2844fb8dfabSAndreas Gohr { 2854fb8dfabSAndreas Gohr $olddata = $this->getUserData($user); 2864fb8dfabSAndreas Gohr $oldgroups = $olddata['grps']; 2874fb8dfabSAndreas Gohr unset($olddata['grps']); 2884fb8dfabSAndreas Gohr 2894fb8dfabSAndreas Gohr // changing the user name? 2904fb8dfabSAndreas Gohr if(isset($changes['user'])) { 2914fb8dfabSAndreas Gohr if($this->getUserData($changes['user'], false)) goto FAIL; 2924fb8dfabSAndreas Gohr $params = $olddata; 2934fb8dfabSAndreas Gohr $params['newlogin'] = $changes['user']; 2944fb8dfabSAndreas Gohr 2954fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-login'), $params); 2964fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 2974fb8dfabSAndreas Gohr } 2984fb8dfabSAndreas Gohr 2994fb8dfabSAndreas Gohr // changing the password? 3004fb8dfabSAndreas Gohr if(isset($changes['pass'])) { 3014fb8dfabSAndreas Gohr $params = $olddata; 3024fb8dfabSAndreas Gohr $params['clear'] = $changes['pass']; 3034fb8dfabSAndreas Gohr $params['hash'] = auth_cryptPassword($changes['pass']); 3044fb8dfabSAndreas Gohr 3054fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-pass'), $params); 3064fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3074fb8dfabSAndreas Gohr } 3084fb8dfabSAndreas Gohr 3094fb8dfabSAndreas Gohr // changing info? 3104fb8dfabSAndreas Gohr if(isset($changes['mail']) || isset($changes['name'])) { 3114fb8dfabSAndreas Gohr $params = $olddata; 3124fb8dfabSAndreas Gohr if(isset($changes['mail'])) $params['mail'] = $changes['mail']; 3134fb8dfabSAndreas Gohr if(isset($changes['name'])) $params['name'] = $changes['name']; 3144fb8dfabSAndreas Gohr 3154fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-info'), $params); 3164fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3174fb8dfabSAndreas Gohr } 3184fb8dfabSAndreas Gohr 3194fb8dfabSAndreas Gohr // changing groups? 3204fb8dfabSAndreas Gohr if(isset($changes['grps'])) { 3214fb8dfabSAndreas Gohr $allgroups = $this->_selectGroups(); 3224fb8dfabSAndreas Gohr 3234fb8dfabSAndreas Gohr // remove membership for previous groups 3244fb8dfabSAndreas Gohr foreach($oldgroups as $group) { 325358942b5SAndreas Gohr if(!in_array($group, $changes['grps']) && isset($allgroups[$group])) { 3264fb8dfabSAndreas Gohr $ok = $this->_leaveGroup($olddata, $allgroups[$group]); 3274fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3284fb8dfabSAndreas Gohr } 3294fb8dfabSAndreas Gohr } 3304fb8dfabSAndreas Gohr 3314fb8dfabSAndreas Gohr // create all new groups that are missing 3324fb8dfabSAndreas Gohr $added = 0; 3334fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3344fb8dfabSAndreas Gohr if(!isset($allgroups[$group])) { 3356459f496SAndreas Gohr $ok = $this->addGroup($group); 3364fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3374fb8dfabSAndreas Gohr $added++; 3384fb8dfabSAndreas Gohr } 3394fb8dfabSAndreas Gohr } 3404fb8dfabSAndreas Gohr // reload group info 3414fb8dfabSAndreas Gohr if($added > 0) $allgroups = $this->_selectGroups(); 3424fb8dfabSAndreas Gohr 3434fb8dfabSAndreas Gohr // add membership for new groups 3444fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3454fb8dfabSAndreas Gohr if(!in_array($group, $oldgroups)) { 3464fb8dfabSAndreas Gohr $ok = $this->_joinGroup($olddata, $allgroups[$group]); 3474fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3484fb8dfabSAndreas Gohr } 3494fb8dfabSAndreas Gohr } 3504fb8dfabSAndreas Gohr } 3514fb8dfabSAndreas Gohr 3524fb8dfabSAndreas Gohr } 3534fb8dfabSAndreas Gohr $this->pdo->commit(); 3544fb8dfabSAndreas Gohr return true; 3554fb8dfabSAndreas Gohr 3564fb8dfabSAndreas Gohr // something went wrong, rollback 3574fb8dfabSAndreas Gohr FAIL: 3584fb8dfabSAndreas Gohr $this->pdo->rollBack(); 3594fb8dfabSAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 360c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 3614fb8dfabSAndreas Gohr return false; // return error 3624fb8dfabSAndreas Gohr } 363f64dbc90SAndreas Gohr 364f64dbc90SAndreas Gohr /** 365e19be516SAndreas Gohr * Delete one or more users 366f64dbc90SAndreas Gohr * 367f64dbc90SAndreas Gohr * Set delUser capability when implemented 368f64dbc90SAndreas Gohr * 369f64dbc90SAndreas Gohr * @param array $users 370f64dbc90SAndreas Gohr * @return int number of users deleted 371f64dbc90SAndreas Gohr */ 372e19be516SAndreas Gohr public function deleteUsers($users) { 373e19be516SAndreas Gohr $count = 0; 374e19be516SAndreas Gohr foreach($users as $user) { 375e19be516SAndreas Gohr if($this->_deleteUser($user)) $count++; 376e19be516SAndreas Gohr } 377e19be516SAndreas Gohr return $count; 378e19be516SAndreas Gohr } 379f64dbc90SAndreas Gohr 380f64dbc90SAndreas Gohr /** 381f64dbc90SAndreas Gohr * Bulk retrieval of user data [implement only where required/possible] 382f64dbc90SAndreas Gohr * 383f64dbc90SAndreas Gohr * Set getUsers capability when implemented 384f64dbc90SAndreas Gohr * 385f64dbc90SAndreas Gohr * @param int $start index of first user to be returned 386f64dbc90SAndreas Gohr * @param int $limit max number of users to be returned 387f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 388f64dbc90SAndreas Gohr * @return array list of userinfo (refer getUserData for internal userinfo details) 389f64dbc90SAndreas Gohr */ 3906459f496SAndreas Gohr public function retrieveUsers($start = 0, $limit = -1, $filter = null) { 3916459f496SAndreas Gohr if($limit < 0) $limit = 10000; // we don't support no limit 3926459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 3936459f496SAndreas Gohr 3946459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 3956459f496SAndreas Gohr if(!isset($filter[$key])) { 3966459f496SAndreas Gohr $filter[$key] = '%'; 3976459f496SAndreas Gohr } else { 3986459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 3996459f496SAndreas Gohr } 4006459f496SAndreas Gohr } 40114119d44SAndreas Gohr $filter['start'] = (int) $start; 40214119d44SAndreas Gohr $filter['end'] = (int) $start + $limit; 40314119d44SAndreas Gohr $filter['limit'] = (int) $limit; 4046459f496SAndreas Gohr 4056459f496SAndreas Gohr $result = $this->_query($this->getConf('list-users'), $filter); 4066459f496SAndreas Gohr if(!$result) return array(); 4076459f496SAndreas Gohr $users = array(); 4086459f496SAndreas Gohr foreach($result as $row) { 4096459f496SAndreas Gohr if(!isset($row['user'])) { 4106459f496SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 4116459f496SAndreas Gohr return array(); 4126459f496SAndreas Gohr } 4136459f496SAndreas Gohr $users[] = $row['user']; 4146459f496SAndreas Gohr } 4156459f496SAndreas Gohr return $users; 4166459f496SAndreas Gohr } 417f64dbc90SAndreas Gohr 418f64dbc90SAndreas Gohr /** 419f64dbc90SAndreas Gohr * Return a count of the number of user which meet $filter criteria 420f64dbc90SAndreas Gohr * 421f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, empty array for no filter 422f64dbc90SAndreas Gohr * @return int 423f64dbc90SAndreas Gohr */ 4246459f496SAndreas Gohr public function getUserCount($filter = array()) { 4256459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 4266459f496SAndreas Gohr 4276459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 4286459f496SAndreas Gohr if(!isset($filter[$key])) { 4296459f496SAndreas Gohr $filter[$key] = '%'; 4306459f496SAndreas Gohr } else { 4316459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 4326459f496SAndreas Gohr } 4336459f496SAndreas Gohr } 4346459f496SAndreas Gohr 4356459f496SAndreas Gohr $result = $this->_query($this->getConf('count-users'), $filter); 4366459f496SAndreas Gohr if(!$result || !isset($result[0]['count'])) { 4376459f496SAndreas Gohr $this->_debug("Statement did not return 'count' attribute", -1, __LINE__); 4386459f496SAndreas Gohr } 4396459f496SAndreas Gohr return isset($result[0]['count']); 4406459f496SAndreas Gohr } 441f64dbc90SAndreas Gohr 442f64dbc90SAndreas Gohr /** 4436459f496SAndreas Gohr * Create a new group with the given name 444f64dbc90SAndreas Gohr * 445f64dbc90SAndreas Gohr * @param string $group 446f64dbc90SAndreas Gohr * @return bool 447f64dbc90SAndreas Gohr */ 4486459f496SAndreas Gohr public function addGroup($group) { 4496459f496SAndreas Gohr $sql = $this->getConf('insert-group'); 4506459f496SAndreas Gohr 4516459f496SAndreas Gohr $result = $this->_query($sql, array(':group' => $group)); 4520cec3e2aSAndreas Gohr $this->_clearGroupCache(); 4536459f496SAndreas Gohr if($result === false) return false; 4546459f496SAndreas Gohr return true; 4556459f496SAndreas Gohr } 456f64dbc90SAndreas Gohr 457f64dbc90SAndreas Gohr /** 4585de3a6a5SAndreas Gohr * Retrieve groups 459f64dbc90SAndreas Gohr * 460f64dbc90SAndreas Gohr * Set getGroups capability when implemented 461f64dbc90SAndreas Gohr * 462f64dbc90SAndreas Gohr * @param int $start 463f64dbc90SAndreas Gohr * @param int $limit 464f64dbc90SAndreas Gohr * @return array 465f64dbc90SAndreas Gohr */ 4665de3a6a5SAndreas Gohr public function retrieveGroups($start = 0, $limit = 0) { 4675de3a6a5SAndreas Gohr $groups = array_keys($this->_selectGroups()); 4685de3a6a5SAndreas Gohr if($groups === false) return array(); 469f64dbc90SAndreas Gohr 4705de3a6a5SAndreas Gohr if(!$limit) { 4715de3a6a5SAndreas Gohr return array_splice($groups, $start); 4725de3a6a5SAndreas Gohr } else { 4735de3a6a5SAndreas Gohr return array_splice($groups, $start, $limit); 474f64dbc90SAndreas Gohr } 475f64dbc90SAndreas Gohr } 476f64dbc90SAndreas Gohr 477f64dbc90SAndreas Gohr /** 478f64dbc90SAndreas Gohr * Select data of a specified user 479f64dbc90SAndreas Gohr * 4805de3a6a5SAndreas Gohr * @param string $user the user name 4815de3a6a5SAndreas Gohr * @return bool|array user data, false on error 482f64dbc90SAndreas Gohr */ 483f64dbc90SAndreas Gohr protected function _selectUser($user) { 484f64dbc90SAndreas Gohr $sql = $this->getConf('select-user'); 485f64dbc90SAndreas Gohr 4865de3a6a5SAndreas Gohr $result = $this->_query($sql, array(':user' => $user)); 48770a89417SAndreas Gohr if(!$result) return false; 488f64dbc90SAndreas Gohr 48970a89417SAndreas Gohr if(count($result) > 1) { 490f64dbc90SAndreas Gohr $this->_debug('Found more than one matching user', -1, __LINE__); 491f64dbc90SAndreas Gohr return false; 492f64dbc90SAndreas Gohr } 493f64dbc90SAndreas Gohr 494f64dbc90SAndreas Gohr $data = array_shift($result); 495f64dbc90SAndreas Gohr $dataok = true; 496f64dbc90SAndreas Gohr 497f64dbc90SAndreas Gohr if(!isset($data['user'])) { 498f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 499f64dbc90SAndreas Gohr $dataok = false; 500f64dbc90SAndreas Gohr } 501f64dbc90SAndreas Gohr if(!isset($data['hash']) && !isset($data['clear'])) { 502f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__); 503f64dbc90SAndreas Gohr $dataok = false; 504f64dbc90SAndreas Gohr } 505f64dbc90SAndreas Gohr if(!isset($data['name'])) { 506f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'name' attribute", -1, __LINE__); 507f64dbc90SAndreas Gohr $dataok = false; 508f64dbc90SAndreas Gohr } 509f64dbc90SAndreas Gohr if(!isset($data['mail'])) { 510f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'mail' attribute", -1, __LINE__); 511f64dbc90SAndreas Gohr $dataok = false; 512f64dbc90SAndreas Gohr } 513f64dbc90SAndreas Gohr 514f64dbc90SAndreas Gohr if(!$dataok) return false; 515f64dbc90SAndreas Gohr return $data; 516f64dbc90SAndreas Gohr } 517f64dbc90SAndreas Gohr 518f64dbc90SAndreas Gohr /** 519e19be516SAndreas Gohr * Delete a user after removing all their group memberships 520e19be516SAndreas Gohr * 521e19be516SAndreas Gohr * @param string $user 522e19be516SAndreas Gohr * @return bool true when the user was deleted 523e19be516SAndreas Gohr */ 524e19be516SAndreas Gohr protected function _deleteUser($user) { 525e19be516SAndreas Gohr $this->pdo->beginTransaction(); 526e19be516SAndreas Gohr { 527e19be516SAndreas Gohr $userdata = $this->getUserData($user); 528e19be516SAndreas Gohr if($userdata === false) goto FAIL; 529e19be516SAndreas Gohr $allgroups = $this->_selectGroups(); 530e19be516SAndreas Gohr 531e19be516SAndreas Gohr // remove group memberships (ignore errors) 532e19be516SAndreas Gohr foreach($userdata['grps'] as $group) { 533358942b5SAndreas Gohr if(isset($allgroups[$group])) { 534e19be516SAndreas Gohr $this->_leaveGroup($userdata, $allgroups[$group]); 535e19be516SAndreas Gohr } 536358942b5SAndreas Gohr } 537e19be516SAndreas Gohr 538e19be516SAndreas Gohr $ok = $this->_query($this->getConf('delete-user'), $userdata); 539e19be516SAndreas Gohr if($ok === false) goto FAIL; 540e19be516SAndreas Gohr } 541e19be516SAndreas Gohr $this->pdo->commit(); 542e19be516SAndreas Gohr return true; 543e19be516SAndreas Gohr 544e19be516SAndreas Gohr FAIL: 545e19be516SAndreas Gohr $this->pdo->rollBack(); 546e19be516SAndreas Gohr return false; 547e19be516SAndreas Gohr } 548e19be516SAndreas Gohr 549e19be516SAndreas Gohr /** 55070a89417SAndreas Gohr * Select all groups of a user 55170a89417SAndreas Gohr * 55270a89417SAndreas Gohr * @param array $userdata The userdata as returned by _selectUser() 5535de3a6a5SAndreas Gohr * @return array|bool list of group names, false on error 55470a89417SAndreas Gohr */ 55570a89417SAndreas Gohr protected function _selectUserGroups($userdata) { 55670a89417SAndreas Gohr global $conf; 55770a89417SAndreas Gohr $sql = $this->getConf('select-user-groups'); 5585de3a6a5SAndreas Gohr $result = $this->_query($sql, $userdata); 5595de3a6a5SAndreas Gohr if($result === false) return false; 56070a89417SAndreas Gohr 56170a89417SAndreas Gohr $groups = array($conf['defaultgroup']); // always add default config 5625de3a6a5SAndreas Gohr foreach($result as $row) { 5635de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5645de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned in select-user-groups statement"); 5655de3a6a5SAndreas Gohr return false; 5665de3a6a5SAndreas Gohr } 56770a89417SAndreas Gohr $groups[] = $row['group']; 56870a89417SAndreas Gohr } 56970a89417SAndreas Gohr 57070a89417SAndreas Gohr $groups = array_unique($groups); 57170a89417SAndreas Gohr sort($groups); 57270a89417SAndreas Gohr return $groups; 57370a89417SAndreas Gohr } 57470a89417SAndreas Gohr 57570a89417SAndreas Gohr /** 5765de3a6a5SAndreas Gohr * Select all available groups 5775de3a6a5SAndreas Gohr * 5785de3a6a5SAndreas Gohr * @return array|bool list of all available groups and their properties 5795de3a6a5SAndreas Gohr */ 5805de3a6a5SAndreas Gohr protected function _selectGroups() { 5810cec3e2aSAndreas Gohr if($this->groupcache) return $this->groupcache; 5820cec3e2aSAndreas Gohr 5835de3a6a5SAndreas Gohr $sql = $this->getConf('select-groups'); 5845de3a6a5SAndreas Gohr $result = $this->_query($sql); 5855de3a6a5SAndreas Gohr if($result === false) return false; 5865de3a6a5SAndreas Gohr 5875de3a6a5SAndreas Gohr $groups = array(); 5885de3a6a5SAndreas Gohr foreach($result as $row) { 5895de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5905de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned from select-groups statement", -1, __LINE__); 5915de3a6a5SAndreas Gohr return false; 5925de3a6a5SAndreas Gohr } 5935de3a6a5SAndreas Gohr 5945de3a6a5SAndreas Gohr // relayout result with group name as key 5955de3a6a5SAndreas Gohr $group = $row['group']; 5965de3a6a5SAndreas Gohr $groups[$group] = $row; 5975de3a6a5SAndreas Gohr } 5985de3a6a5SAndreas Gohr 5995de3a6a5SAndreas Gohr ksort($groups); 6005de3a6a5SAndreas Gohr return $groups; 6015de3a6a5SAndreas Gohr } 6025de3a6a5SAndreas Gohr 6035de3a6a5SAndreas Gohr /** 6040cec3e2aSAndreas Gohr * Remove all entries from the group cache 6050cec3e2aSAndreas Gohr */ 6060cec3e2aSAndreas Gohr protected function _clearGroupCache() { 6070cec3e2aSAndreas Gohr $this->groupcache = null; 6080cec3e2aSAndreas Gohr } 6090cec3e2aSAndreas Gohr 6100cec3e2aSAndreas Gohr /** 6114fb8dfabSAndreas Gohr * Adds the user to the group 6125de3a6a5SAndreas Gohr * 6135de3a6a5SAndreas Gohr * @param array $userdata all the user data 6145de3a6a5SAndreas Gohr * @param array $groupdata all the group data 6155de3a6a5SAndreas Gohr * @return bool 6165de3a6a5SAndreas Gohr */ 6175de3a6a5SAndreas Gohr protected function _joinGroup($userdata, $groupdata) { 6185de3a6a5SAndreas Gohr $data = array_merge($userdata, $groupdata); 6195de3a6a5SAndreas Gohr $sql = $this->getConf('join-group'); 6205de3a6a5SAndreas Gohr $result = $this->_query($sql, $data); 6215de3a6a5SAndreas Gohr if($result === false) return false; 6225de3a6a5SAndreas Gohr return true; 6235de3a6a5SAndreas Gohr } 6245de3a6a5SAndreas Gohr 6255de3a6a5SAndreas Gohr /** 6264fb8dfabSAndreas Gohr * Removes the user from the group 6274fb8dfabSAndreas Gohr * 6284fb8dfabSAndreas Gohr * @param array $userdata all the user data 6294fb8dfabSAndreas Gohr * @param array $groupdata all the group data 6304fb8dfabSAndreas Gohr * @return bool 6314fb8dfabSAndreas Gohr */ 6324fb8dfabSAndreas Gohr protected function _leaveGroup($userdata, $groupdata) { 6334fb8dfabSAndreas Gohr $data = array_merge($userdata, $groupdata); 6344fb8dfabSAndreas Gohr $sql = $this->getConf('leave-group'); 6354fb8dfabSAndreas Gohr $result = $this->_query($sql, $data); 6364fb8dfabSAndreas Gohr if($result === false) return false; 6374fb8dfabSAndreas Gohr return true; 6384fb8dfabSAndreas Gohr } 6394fb8dfabSAndreas Gohr 6404fb8dfabSAndreas Gohr /** 64170a89417SAndreas Gohr * Executes a query 64270a89417SAndreas Gohr * 64370a89417SAndreas Gohr * @param string $sql The SQL statement to execute 64470a89417SAndreas Gohr * @param array $arguments Named parameters to be used in the statement 645f695c447SAndreas Gohr * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error 64670a89417SAndreas Gohr */ 6475de3a6a5SAndreas Gohr protected function _query($sql, $arguments = array()) { 648f695c447SAndreas Gohr $sql = trim($sql); 6495de3a6a5SAndreas Gohr if(empty($sql)) { 6505de3a6a5SAndreas Gohr $this->_debug('No SQL query given', -1, __LINE__); 6515de3a6a5SAndreas Gohr return false; 6525de3a6a5SAndreas Gohr } 6535de3a6a5SAndreas Gohr 65414119d44SAndreas Gohr // execute 65570a89417SAndreas Gohr $params = array(); 65614119d44SAndreas Gohr $sth = $this->pdo->prepare($sql); 65714119d44SAndreas Gohr try { 65814119d44SAndreas Gohr // prepare parameters - we only use those that exist in the SQL 65970a89417SAndreas Gohr foreach($arguments as $key => $value) { 66070a89417SAndreas Gohr if(is_array($value)) continue; 66170a89417SAndreas Gohr if(is_object($value)) continue; 66270a89417SAndreas Gohr if($key[0] != ':') $key = ":$key"; // prefix with colon if needed 66314119d44SAndreas Gohr if(strpos($sql, $key) === false) continue; // skip if parameter is missing 66414119d44SAndreas Gohr 66514119d44SAndreas Gohr if(is_int($value)) { 66614119d44SAndreas Gohr $sth->bindValue($key, $value, PDO::PARAM_INT); 66714119d44SAndreas Gohr } else { 66814119d44SAndreas Gohr $sth->bindValue($key, $value); 66914119d44SAndreas Gohr } 670f6cd8a7fSphjanderson $params[$key] = $value; //remember for debugging 67170a89417SAndreas Gohr } 67270a89417SAndreas Gohr 67314119d44SAndreas Gohr $sth->execute(); 674f695c447SAndreas Gohr if(strtolower(substr($sql, 0, 6)) == 'select') { 67570a89417SAndreas Gohr $result = $sth->fetchAll(); 676f695c447SAndreas Gohr } else { 677f695c447SAndreas Gohr $result = $sth->rowCount(); 678f695c447SAndreas Gohr } 6795de3a6a5SAndreas Gohr } catch(Exception $e) { 6804fb8dfabSAndreas Gohr // report the caller's line 6814fb8dfabSAndreas Gohr $trace = debug_backtrace(); 6824fb8dfabSAndreas Gohr $line = $trace[0]['line']; 6835de3a6a5SAndreas Gohr $dsql = $this->_debugSQL($sql, $params, !defined('DOKU_UNITTEST')); 6844fb8dfabSAndreas Gohr $this->_debug($e, -1, $line); 6854fb8dfabSAndreas Gohr $this->_debug("SQL: <pre>$dsql</pre>", -1, $line); 68670a89417SAndreas Gohr $result = false; 6871600c7ccSAndreas Gohr } 68870a89417SAndreas Gohr $sth->closeCursor(); 68970a89417SAndreas Gohr $sth = null; 69070a89417SAndreas Gohr 6915de3a6a5SAndreas Gohr return $result; 6925de3a6a5SAndreas Gohr } 69370a89417SAndreas Gohr 69470a89417SAndreas Gohr /** 695f64dbc90SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 696f64dbc90SAndreas Gohr * 697f64dbc90SAndreas Gohr * @param string|Exception $message 698f64dbc90SAndreas Gohr * @param int $err 699f64dbc90SAndreas Gohr * @param int $line 700f64dbc90SAndreas Gohr */ 701f64dbc90SAndreas Gohr protected function _debug($message, $err = 0, $line = 0) { 702f64dbc90SAndreas Gohr if(!$this->getConf('debug')) return; 703f64dbc90SAndreas Gohr if(is_a($message, 'Exception')) { 704f64dbc90SAndreas Gohr $err = -1; 705f64dbc90SAndreas Gohr $msg = $message->getMessage(); 7064fb8dfabSAndreas Gohr if(!$line) $line = $message->getLine(); 707f64dbc90SAndreas Gohr } else { 708f64dbc90SAndreas Gohr $msg = $message; 709f64dbc90SAndreas Gohr } 710f64dbc90SAndreas Gohr 711f64dbc90SAndreas Gohr if(defined('DOKU_UNITTEST')) { 712f64dbc90SAndreas Gohr printf("\n%s, %s:%d\n", $msg, __FILE__, $line); 713f64dbc90SAndreas Gohr } else { 714f64dbc90SAndreas Gohr msg('authpdo: ' . $msg, $err, $line, __FILE__); 715f64dbc90SAndreas Gohr } 716f64dbc90SAndreas Gohr } 7175de3a6a5SAndreas Gohr 7185de3a6a5SAndreas Gohr /** 7195de3a6a5SAndreas Gohr * Check if the given config strings are set 7205de3a6a5SAndreas Gohr * 7215de3a6a5SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 7225de3a6a5SAndreas Gohr * 7235de3a6a5SAndreas Gohr * @param string[] $keys 7245de3a6a5SAndreas Gohr * @return bool 7255de3a6a5SAndreas Gohr */ 7265de3a6a5SAndreas Gohr protected function _chkcnf($keys) { 7275de3a6a5SAndreas Gohr foreach($keys as $key) { 728*ad4d5631SAndreas Gohr $params = explode(':', $key); 729*ad4d5631SAndreas Gohr $key = array_shift($params); 730*ad4d5631SAndreas Gohr $sql = trim($this->getConf($key)); 731*ad4d5631SAndreas Gohr 732*ad4d5631SAndreas Gohr // check if sql is set 733*ad4d5631SAndreas Gohr if(!$sql) return false; 734*ad4d5631SAndreas Gohr // check if needed params are there 735*ad4d5631SAndreas Gohr foreach($params as $param) { 736*ad4d5631SAndreas Gohr if(strpos($sql, ":$param") === false) return false; 737*ad4d5631SAndreas Gohr } 7385de3a6a5SAndreas Gohr } 7395de3a6a5SAndreas Gohr 7405de3a6a5SAndreas Gohr return true; 7415de3a6a5SAndreas Gohr } 7425de3a6a5SAndreas Gohr 7435de3a6a5SAndreas Gohr /** 7445de3a6a5SAndreas Gohr * create an approximation of the SQL string with parameters replaced 7455de3a6a5SAndreas Gohr * 7465de3a6a5SAndreas Gohr * @param string $sql 7475de3a6a5SAndreas Gohr * @param array $params 7485de3a6a5SAndreas Gohr * @param bool $htmlescape Should the result be escaped for output in HTML? 7495de3a6a5SAndreas Gohr * @return string 7505de3a6a5SAndreas Gohr */ 7515de3a6a5SAndreas Gohr protected function _debugSQL($sql, $params, $htmlescape = true) { 7525de3a6a5SAndreas Gohr foreach($params as $key => $val) { 7535de3a6a5SAndreas Gohr if(is_int($val)) { 7545de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_INT); 7555de3a6a5SAndreas Gohr } elseif(is_bool($val)) { 7565de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_BOOL); 7575de3a6a5SAndreas Gohr } elseif(is_null($val)) { 7585de3a6a5SAndreas Gohr $val = 'NULL'; 7595de3a6a5SAndreas Gohr } else { 7605de3a6a5SAndreas Gohr $val = $this->pdo->quote($val); 7615de3a6a5SAndreas Gohr } 7625de3a6a5SAndreas Gohr $sql = str_replace($key, $val, $sql); 7635de3a6a5SAndreas Gohr } 7645de3a6a5SAndreas Gohr if($htmlescape) $sql = hsc($sql); 7655de3a6a5SAndreas Gohr return $sql; 7665de3a6a5SAndreas Gohr } 767f64dbc90SAndreas Gohr} 768f64dbc90SAndreas Gohr 769f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et: 770