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', 77*7f89f089SAndreas Gohr 'leave-group', 78*7f89f089SAndreas 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 1000d586afdSAndreas Gohr // can real names and emails be changed? 1010d586afdSAndreas Gohr $this->cando['modName'] = $this->cando['modMail'] = $this->_chkcnf( 1020d586afdSAndreas Gohr array( 1030d586afdSAndreas Gohr 'select-user', 1040d586afdSAndreas Gohr 'select-user-groups', 1050d586afdSAndreas Gohr 'update-user-info' 1060d586afdSAndreas Gohr ) 1070d586afdSAndreas Gohr ); 1080d586afdSAndreas Gohr 1090d586afdSAndreas Gohr // can groups be changed? 1100d586afdSAndreas Gohr $this->cando['modGroups'] = $this->_chkcnf( 1110d586afdSAndreas Gohr array( 1120d586afdSAndreas Gohr 'select-user', 1130d586afdSAndreas Gohr 'select-user-groups', 1140d586afdSAndreas Gohr 'select-groups', 1150d586afdSAndreas Gohr 'leave-group', 1160d586afdSAndreas Gohr 'join-group', 1170d586afdSAndreas Gohr 'insert-group' 1180d586afdSAndreas Gohr ) 1190d586afdSAndreas Gohr ); 1200d586afdSAndreas Gohr 1210d586afdSAndreas Gohr // can a filtered list of users be retrieved? 1220d586afdSAndreas Gohr $this->cando['getUsers'] = $this->_chkcnf( 1230d586afdSAndreas Gohr array( 1240d586afdSAndreas Gohr 'list-users' 1250d586afdSAndreas Gohr ) 1260d586afdSAndreas Gohr ); 1270d586afdSAndreas Gohr 1280d586afdSAndreas Gohr // can the number of users be retrieved? 1290d586afdSAndreas Gohr $this->cando['getUserCount'] = $this->_chkcnf( 1300d586afdSAndreas Gohr array( 1310d586afdSAndreas Gohr 'count-users' 1320d586afdSAndreas Gohr ) 1330d586afdSAndreas Gohr ); 1340d586afdSAndreas Gohr 1350d586afdSAndreas Gohr // can a list of available groups be retrieved? 1360d586afdSAndreas Gohr $this->cando['getGroups'] = $this->_chkcnf( 1370d586afdSAndreas Gohr array( 1380d586afdSAndreas Gohr 'select-groups' 1390d586afdSAndreas Gohr ) 1400d586afdSAndreas Gohr ); 1410d586afdSAndreas Gohr 142f64dbc90SAndreas Gohr $this->success = true; 143f64dbc90SAndreas Gohr } 144f64dbc90SAndreas Gohr 145f64dbc90SAndreas Gohr /** 146f64dbc90SAndreas Gohr * Check user+password 147f64dbc90SAndreas Gohr * 148f64dbc90SAndreas Gohr * @param string $user the user name 149f64dbc90SAndreas Gohr * @param string $pass the clear text password 150f64dbc90SAndreas Gohr * @return bool 151f64dbc90SAndreas Gohr */ 152f64dbc90SAndreas Gohr public function checkPass($user, $pass) { 153f64dbc90SAndreas Gohr 154f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 155f64dbc90SAndreas Gohr if($data == false) return false; 156f64dbc90SAndreas Gohr 157f64dbc90SAndreas Gohr if(isset($data['hash'])) { 158f64dbc90SAndreas Gohr // hashed password 159f64dbc90SAndreas Gohr $passhash = new PassHash(); 160f64dbc90SAndreas Gohr return $passhash->verify_hash($pass, $data['hash']); 161f64dbc90SAndreas Gohr } else { 162f64dbc90SAndreas Gohr // clear text password in the database O_o 163f64dbc90SAndreas Gohr return ($pass == $data['clear']); 164f64dbc90SAndreas Gohr } 165f64dbc90SAndreas Gohr } 166f64dbc90SAndreas Gohr 167f64dbc90SAndreas Gohr /** 168f64dbc90SAndreas Gohr * Return user info 169f64dbc90SAndreas Gohr * 170f64dbc90SAndreas Gohr * Returns info about the given user needs to contain 171f64dbc90SAndreas Gohr * at least these fields: 172f64dbc90SAndreas Gohr * 173f64dbc90SAndreas Gohr * name string full name of the user 174f64dbc90SAndreas Gohr * mail string email addres of the user 175f64dbc90SAndreas Gohr * grps array list of groups the user is in 176f64dbc90SAndreas Gohr * 177f64dbc90SAndreas Gohr * @param string $user the user name 178f64dbc90SAndreas Gohr * @param bool $requireGroups whether or not the returned data must include groups 179358942b5SAndreas Gohr * @return array|bool containing user data or false 180f64dbc90SAndreas Gohr */ 181f64dbc90SAndreas Gohr public function getUserData($user, $requireGroups = true) { 182f64dbc90SAndreas Gohr $data = $this->_selectUser($user); 183f64dbc90SAndreas Gohr if($data == false) return false; 184f64dbc90SAndreas Gohr 18570a89417SAndreas Gohr if(isset($data['hash'])) unset($data['hash']); 18670a89417SAndreas Gohr if(isset($data['clean'])) unset($data['clean']); 187f64dbc90SAndreas Gohr 18870a89417SAndreas Gohr if($requireGroups) { 18970a89417SAndreas Gohr $data['grps'] = $this->_selectUserGroups($data); 1905de3a6a5SAndreas Gohr if($data['grps'] === false) return false; 191f64dbc90SAndreas Gohr } 192f64dbc90SAndreas Gohr 193f64dbc90SAndreas Gohr return $data; 194f64dbc90SAndreas Gohr } 195f64dbc90SAndreas Gohr 196f64dbc90SAndreas Gohr /** 197f64dbc90SAndreas Gohr * Create a new User [implement only where required/possible] 198f64dbc90SAndreas Gohr * 199f64dbc90SAndreas Gohr * Returns false if the user already exists, null when an error 200f64dbc90SAndreas Gohr * occurred and true if everything went well. 201f64dbc90SAndreas Gohr * 202f64dbc90SAndreas Gohr * The new user HAS TO be added to the default group by this 203f64dbc90SAndreas Gohr * function! 204f64dbc90SAndreas Gohr * 205f64dbc90SAndreas Gohr * Set addUser capability when implemented 206f64dbc90SAndreas Gohr * 207f64dbc90SAndreas Gohr * @param string $user 2085de3a6a5SAndreas Gohr * @param string $clear 209f64dbc90SAndreas Gohr * @param string $name 210f64dbc90SAndreas Gohr * @param string $mail 211f64dbc90SAndreas Gohr * @param null|array $grps 212f64dbc90SAndreas Gohr * @return bool|null 213f64dbc90SAndreas Gohr */ 2145de3a6a5SAndreas Gohr public function createUser($user, $clear, $name, $mail, $grps = null) { 2155de3a6a5SAndreas Gohr global $conf; 2165de3a6a5SAndreas Gohr 2175de3a6a5SAndreas Gohr if(($info = $this->getUserData($user, false)) !== false) { 2185de3a6a5SAndreas Gohr msg($this->getLang('userexists'), -1); 2195de3a6a5SAndreas Gohr return false; // user already exists 2205de3a6a5SAndreas Gohr } 2215de3a6a5SAndreas Gohr 2225de3a6a5SAndreas Gohr // prepare data 2235de3a6a5SAndreas Gohr if($grps == null) $grps = array(); 22407a11e2aSAndreas Gohr array_unshift($grps, $conf['defaultgroup']); 2255de3a6a5SAndreas Gohr $grps = array_unique($grps); 2265de3a6a5SAndreas Gohr $hash = auth_cryptPassword($clear); 2275de3a6a5SAndreas Gohr $userdata = compact('user', 'clear', 'hash', 'name', 'mail'); 2285de3a6a5SAndreas Gohr 2295de3a6a5SAndreas Gohr // action protected by transaction 2305de3a6a5SAndreas Gohr $this->pdo->beginTransaction(); 2315de3a6a5SAndreas Gohr { 2325de3a6a5SAndreas Gohr // insert the user 2335de3a6a5SAndreas Gohr $ok = $this->_query($this->getConf('insert-user'), $userdata); 2345de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2355de3a6a5SAndreas Gohr $userdata = $this->getUserData($user, false); 2365de3a6a5SAndreas Gohr if($userdata === false) goto FAIL; 2375de3a6a5SAndreas Gohr 2385de3a6a5SAndreas Gohr // create all groups that do not exist, the refetch the groups 2395de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2405de3a6a5SAndreas Gohr foreach($grps as $group) { 2415de3a6a5SAndreas Gohr if(!isset($allgroups[$group])) { 2426459f496SAndreas Gohr $ok = $this->addGroup($group); 2435de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2445de3a6a5SAndreas Gohr } 2455de3a6a5SAndreas Gohr } 2465de3a6a5SAndreas Gohr $allgroups = $this->_selectGroups(); 2475de3a6a5SAndreas Gohr 2485de3a6a5SAndreas Gohr // add user to the groups 2495de3a6a5SAndreas Gohr foreach($grps as $group) { 2505de3a6a5SAndreas Gohr $ok = $this->_joinGroup($userdata, $allgroups[$group]); 2515de3a6a5SAndreas Gohr if($ok === false) goto FAIL; 2525de3a6a5SAndreas Gohr } 2535de3a6a5SAndreas Gohr } 2545de3a6a5SAndreas Gohr $this->pdo->commit(); 2555de3a6a5SAndreas Gohr return true; 2565de3a6a5SAndreas Gohr 2575de3a6a5SAndreas Gohr // something went wrong, rollback 2585de3a6a5SAndreas Gohr FAIL: 2595de3a6a5SAndreas Gohr $this->pdo->rollBack(); 2605de3a6a5SAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 261c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 2625de3a6a5SAndreas Gohr return null; // return error 2635de3a6a5SAndreas Gohr } 264f64dbc90SAndreas Gohr 265f64dbc90SAndreas Gohr /** 2664fb8dfabSAndreas Gohr * Modify user data 267f64dbc90SAndreas Gohr * 268f64dbc90SAndreas Gohr * @param string $user nick of the user to be changed 269f64dbc90SAndreas Gohr * @param array $changes array of field/value pairs to be changed (password will be clear text) 270f64dbc90SAndreas Gohr * @return bool 271f64dbc90SAndreas Gohr */ 2724fb8dfabSAndreas Gohr public function modifyUser($user, $changes) { 2734fb8dfabSAndreas Gohr // secure everything in transaction 2744fb8dfabSAndreas Gohr $this->pdo->beginTransaction(); 2754fb8dfabSAndreas Gohr { 2764fb8dfabSAndreas Gohr $olddata = $this->getUserData($user); 2774fb8dfabSAndreas Gohr $oldgroups = $olddata['grps']; 2784fb8dfabSAndreas Gohr unset($olddata['grps']); 2794fb8dfabSAndreas Gohr 2804fb8dfabSAndreas Gohr // changing the user name? 2814fb8dfabSAndreas Gohr if(isset($changes['user'])) { 2824fb8dfabSAndreas Gohr if($this->getUserData($changes['user'], false)) goto FAIL; 2834fb8dfabSAndreas Gohr $params = $olddata; 2844fb8dfabSAndreas Gohr $params['newlogin'] = $changes['user']; 2854fb8dfabSAndreas Gohr 2864fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-login'), $params); 2874fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 2884fb8dfabSAndreas Gohr } 2894fb8dfabSAndreas Gohr 2904fb8dfabSAndreas Gohr // changing the password? 2914fb8dfabSAndreas Gohr if(isset($changes['pass'])) { 2924fb8dfabSAndreas Gohr $params = $olddata; 2934fb8dfabSAndreas Gohr $params['clear'] = $changes['pass']; 2944fb8dfabSAndreas Gohr $params['hash'] = auth_cryptPassword($changes['pass']); 2954fb8dfabSAndreas Gohr 2964fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-pass'), $params); 2974fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 2984fb8dfabSAndreas Gohr } 2994fb8dfabSAndreas Gohr 3004fb8dfabSAndreas Gohr // changing info? 3014fb8dfabSAndreas Gohr if(isset($changes['mail']) || isset($changes['name'])) { 3024fb8dfabSAndreas Gohr $params = $olddata; 3034fb8dfabSAndreas Gohr if(isset($changes['mail'])) $params['mail'] = $changes['mail']; 3044fb8dfabSAndreas Gohr if(isset($changes['name'])) $params['name'] = $changes['name']; 3054fb8dfabSAndreas Gohr 3064fb8dfabSAndreas Gohr $ok = $this->_query($this->getConf('update-user-info'), $params); 3074fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3084fb8dfabSAndreas Gohr } 3094fb8dfabSAndreas Gohr 3104fb8dfabSAndreas Gohr // changing groups? 3114fb8dfabSAndreas Gohr if(isset($changes['grps'])) { 3124fb8dfabSAndreas Gohr $allgroups = $this->_selectGroups(); 3134fb8dfabSAndreas Gohr 3144fb8dfabSAndreas Gohr // remove membership for previous groups 3154fb8dfabSAndreas Gohr foreach($oldgroups as $group) { 316358942b5SAndreas Gohr if(!in_array($group, $changes['grps']) && isset($allgroups[$group])) { 3174fb8dfabSAndreas Gohr $ok = $this->_leaveGroup($olddata, $allgroups[$group]); 3184fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3194fb8dfabSAndreas Gohr } 3204fb8dfabSAndreas Gohr } 3214fb8dfabSAndreas Gohr 3224fb8dfabSAndreas Gohr // create all new groups that are missing 3234fb8dfabSAndreas Gohr $added = 0; 3244fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3254fb8dfabSAndreas Gohr if(!isset($allgroups[$group])) { 3266459f496SAndreas Gohr $ok = $this->addGroup($group); 3274fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3284fb8dfabSAndreas Gohr $added++; 3294fb8dfabSAndreas Gohr } 3304fb8dfabSAndreas Gohr } 3314fb8dfabSAndreas Gohr // reload group info 3324fb8dfabSAndreas Gohr if($added > 0) $allgroups = $this->_selectGroups(); 3334fb8dfabSAndreas Gohr 3344fb8dfabSAndreas Gohr // add membership for new groups 3354fb8dfabSAndreas Gohr foreach($changes['grps'] as $group) { 3364fb8dfabSAndreas Gohr if(!in_array($group, $oldgroups)) { 3374fb8dfabSAndreas Gohr $ok = $this->_joinGroup($olddata, $allgroups[$group]); 3384fb8dfabSAndreas Gohr if($ok === false) goto FAIL; 3394fb8dfabSAndreas Gohr } 3404fb8dfabSAndreas Gohr } 3414fb8dfabSAndreas Gohr } 3424fb8dfabSAndreas Gohr 3434fb8dfabSAndreas Gohr } 3444fb8dfabSAndreas Gohr $this->pdo->commit(); 3454fb8dfabSAndreas Gohr return true; 3464fb8dfabSAndreas Gohr 3474fb8dfabSAndreas Gohr // something went wrong, rollback 3484fb8dfabSAndreas Gohr FAIL: 3494fb8dfabSAndreas Gohr $this->pdo->rollBack(); 3504fb8dfabSAndreas Gohr $this->_debug('Transaction rolled back', 0, __LINE__); 351c27579a6SAndreas Gohr msg($this->getLang('writefail'), -1); 3524fb8dfabSAndreas Gohr return false; // return error 3534fb8dfabSAndreas Gohr } 354f64dbc90SAndreas Gohr 355f64dbc90SAndreas Gohr /** 356e19be516SAndreas Gohr * Delete one or more users 357f64dbc90SAndreas Gohr * 358f64dbc90SAndreas Gohr * Set delUser capability when implemented 359f64dbc90SAndreas Gohr * 360f64dbc90SAndreas Gohr * @param array $users 361f64dbc90SAndreas Gohr * @return int number of users deleted 362f64dbc90SAndreas Gohr */ 363e19be516SAndreas Gohr public function deleteUsers($users) { 364e19be516SAndreas Gohr $count = 0; 365e19be516SAndreas Gohr foreach($users as $user) { 366e19be516SAndreas Gohr if($this->_deleteUser($user)) $count++; 367e19be516SAndreas Gohr } 368e19be516SAndreas Gohr return $count; 369e19be516SAndreas Gohr } 370f64dbc90SAndreas Gohr 371f64dbc90SAndreas Gohr /** 372f64dbc90SAndreas Gohr * Bulk retrieval of user data [implement only where required/possible] 373f64dbc90SAndreas Gohr * 374f64dbc90SAndreas Gohr * Set getUsers capability when implemented 375f64dbc90SAndreas Gohr * 376f64dbc90SAndreas Gohr * @param int $start index of first user to be returned 377f64dbc90SAndreas Gohr * @param int $limit max number of users to be returned 378f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 379f64dbc90SAndreas Gohr * @return array list of userinfo (refer getUserData for internal userinfo details) 380f64dbc90SAndreas Gohr */ 3816459f496SAndreas Gohr public function retrieveUsers($start = 0, $limit = -1, $filter = null) { 3826459f496SAndreas Gohr if($limit < 0) $limit = 10000; // we don't support no limit 3836459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 3846459f496SAndreas Gohr 3856459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 3866459f496SAndreas Gohr if(!isset($filter[$key])) { 3876459f496SAndreas Gohr $filter[$key] = '%'; 3886459f496SAndreas Gohr } else { 3896459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 3906459f496SAndreas Gohr } 3916459f496SAndreas Gohr } 39214119d44SAndreas Gohr $filter['start'] = (int) $start; 39314119d44SAndreas Gohr $filter['end'] = (int) $start + $limit; 39414119d44SAndreas Gohr $filter['limit'] = (int) $limit; 3956459f496SAndreas Gohr 3966459f496SAndreas Gohr $result = $this->_query($this->getConf('list-users'), $filter); 3976459f496SAndreas Gohr if(!$result) return array(); 3986459f496SAndreas Gohr $users = array(); 3996459f496SAndreas Gohr foreach($result as $row) { 4006459f496SAndreas Gohr if(!isset($row['user'])) { 4016459f496SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 4026459f496SAndreas Gohr return array(); 4036459f496SAndreas Gohr } 4046459f496SAndreas Gohr $users[] = $row['user']; 4056459f496SAndreas Gohr } 4066459f496SAndreas Gohr return $users; 4076459f496SAndreas Gohr } 408f64dbc90SAndreas Gohr 409f64dbc90SAndreas Gohr /** 410f64dbc90SAndreas Gohr * Return a count of the number of user which meet $filter criteria 411f64dbc90SAndreas Gohr * 412f64dbc90SAndreas Gohr * @param array $filter array of field/pattern pairs, empty array for no filter 413f64dbc90SAndreas Gohr * @return int 414f64dbc90SAndreas Gohr */ 4156459f496SAndreas Gohr public function getUserCount($filter = array()) { 4166459f496SAndreas Gohr if(is_null($filter)) $filter = array(); 4176459f496SAndreas Gohr 4186459f496SAndreas Gohr foreach(array('user', 'name', 'mail', 'group') as $key) { 4196459f496SAndreas Gohr if(!isset($filter[$key])) { 4206459f496SAndreas Gohr $filter[$key] = '%'; 4216459f496SAndreas Gohr } else { 4226459f496SAndreas Gohr $filter[$key] = '%' . $filter[$key] . '%'; 4236459f496SAndreas Gohr } 4246459f496SAndreas Gohr } 4256459f496SAndreas Gohr 4266459f496SAndreas Gohr $result = $this->_query($this->getConf('count-users'), $filter); 4276459f496SAndreas Gohr if(!$result || !isset($result[0]['count'])) { 4286459f496SAndreas Gohr $this->_debug("Statement did not return 'count' attribute", -1, __LINE__); 4296459f496SAndreas Gohr } 4306459f496SAndreas Gohr return isset($result[0]['count']); 4316459f496SAndreas Gohr } 432f64dbc90SAndreas Gohr 433f64dbc90SAndreas Gohr /** 4346459f496SAndreas Gohr * Create a new group with the given name 435f64dbc90SAndreas Gohr * 436f64dbc90SAndreas Gohr * @param string $group 437f64dbc90SAndreas Gohr * @return bool 438f64dbc90SAndreas Gohr */ 4396459f496SAndreas Gohr public function addGroup($group) { 4406459f496SAndreas Gohr $sql = $this->getConf('insert-group'); 4416459f496SAndreas Gohr 4426459f496SAndreas Gohr $result = $this->_query($sql, array(':group' => $group)); 4430cec3e2aSAndreas Gohr $this->_clearGroupCache(); 4446459f496SAndreas Gohr if($result === false) return false; 4456459f496SAndreas Gohr return true; 4466459f496SAndreas Gohr } 447f64dbc90SAndreas Gohr 448f64dbc90SAndreas Gohr /** 4495de3a6a5SAndreas Gohr * Retrieve groups 450f64dbc90SAndreas Gohr * 451f64dbc90SAndreas Gohr * Set getGroups capability when implemented 452f64dbc90SAndreas Gohr * 453f64dbc90SAndreas Gohr * @param int $start 454f64dbc90SAndreas Gohr * @param int $limit 455f64dbc90SAndreas Gohr * @return array 456f64dbc90SAndreas Gohr */ 4575de3a6a5SAndreas Gohr public function retrieveGroups($start = 0, $limit = 0) { 4585de3a6a5SAndreas Gohr $groups = array_keys($this->_selectGroups()); 4595de3a6a5SAndreas Gohr if($groups === false) return array(); 460f64dbc90SAndreas Gohr 4615de3a6a5SAndreas Gohr if(!$limit) { 4625de3a6a5SAndreas Gohr return array_splice($groups, $start); 4635de3a6a5SAndreas Gohr } else { 4645de3a6a5SAndreas Gohr return array_splice($groups, $start, $limit); 465f64dbc90SAndreas Gohr } 466f64dbc90SAndreas Gohr } 467f64dbc90SAndreas Gohr 468f64dbc90SAndreas Gohr /** 469f64dbc90SAndreas Gohr * Select data of a specified user 470f64dbc90SAndreas Gohr * 4715de3a6a5SAndreas Gohr * @param string $user the user name 4725de3a6a5SAndreas Gohr * @return bool|array user data, false on error 473f64dbc90SAndreas Gohr */ 474f64dbc90SAndreas Gohr protected function _selectUser($user) { 475f64dbc90SAndreas Gohr $sql = $this->getConf('select-user'); 476f64dbc90SAndreas Gohr 4775de3a6a5SAndreas Gohr $result = $this->_query($sql, array(':user' => $user)); 47870a89417SAndreas Gohr if(!$result) return false; 479f64dbc90SAndreas Gohr 48070a89417SAndreas Gohr if(count($result) > 1) { 481f64dbc90SAndreas Gohr $this->_debug('Found more than one matching user', -1, __LINE__); 482f64dbc90SAndreas Gohr return false; 483f64dbc90SAndreas Gohr } 484f64dbc90SAndreas Gohr 485f64dbc90SAndreas Gohr $data = array_shift($result); 486f64dbc90SAndreas Gohr $dataok = true; 487f64dbc90SAndreas Gohr 488f64dbc90SAndreas Gohr if(!isset($data['user'])) { 489f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); 490f64dbc90SAndreas Gohr $dataok = false; 491f64dbc90SAndreas Gohr } 492f64dbc90SAndreas Gohr if(!isset($data['hash']) && !isset($data['clear'])) { 493f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__); 494f64dbc90SAndreas Gohr $dataok = false; 495f64dbc90SAndreas Gohr } 496f64dbc90SAndreas Gohr if(!isset($data['name'])) { 497f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'name' attribute", -1, __LINE__); 498f64dbc90SAndreas Gohr $dataok = false; 499f64dbc90SAndreas Gohr } 500f64dbc90SAndreas Gohr if(!isset($data['mail'])) { 501f64dbc90SAndreas Gohr $this->_debug("Statement did not return 'mail' attribute", -1, __LINE__); 502f64dbc90SAndreas Gohr $dataok = false; 503f64dbc90SAndreas Gohr } 504f64dbc90SAndreas Gohr 505f64dbc90SAndreas Gohr if(!$dataok) return false; 506f64dbc90SAndreas Gohr return $data; 507f64dbc90SAndreas Gohr } 508f64dbc90SAndreas Gohr 509f64dbc90SAndreas Gohr /** 510e19be516SAndreas Gohr * Delete a user after removing all their group memberships 511e19be516SAndreas Gohr * 512e19be516SAndreas Gohr * @param string $user 513e19be516SAndreas Gohr * @return bool true when the user was deleted 514e19be516SAndreas Gohr */ 515e19be516SAndreas Gohr protected function _deleteUser($user) { 516e19be516SAndreas Gohr $this->pdo->beginTransaction(); 517e19be516SAndreas Gohr { 518e19be516SAndreas Gohr $userdata = $this->getUserData($user); 519e19be516SAndreas Gohr if($userdata === false) goto FAIL; 520e19be516SAndreas Gohr $allgroups = $this->_selectGroups(); 521e19be516SAndreas Gohr 522e19be516SAndreas Gohr // remove group memberships (ignore errors) 523e19be516SAndreas Gohr foreach($userdata['grps'] as $group) { 524358942b5SAndreas Gohr if(isset($allgroups[$group])) { 525e19be516SAndreas Gohr $this->_leaveGroup($userdata, $allgroups[$group]); 526e19be516SAndreas Gohr } 527358942b5SAndreas Gohr } 528e19be516SAndreas Gohr 529e19be516SAndreas Gohr $ok = $this->_query($this->getConf('delete-user'), $userdata); 530e19be516SAndreas Gohr if($ok === false) goto FAIL; 531e19be516SAndreas Gohr } 532e19be516SAndreas Gohr $this->pdo->commit(); 533e19be516SAndreas Gohr return true; 534e19be516SAndreas Gohr 535e19be516SAndreas Gohr FAIL: 536e19be516SAndreas Gohr $this->pdo->rollBack(); 537e19be516SAndreas Gohr return false; 538e19be516SAndreas Gohr } 539e19be516SAndreas Gohr 540e19be516SAndreas Gohr /** 54170a89417SAndreas Gohr * Select all groups of a user 54270a89417SAndreas Gohr * 54370a89417SAndreas Gohr * @param array $userdata The userdata as returned by _selectUser() 5445de3a6a5SAndreas Gohr * @return array|bool list of group names, false on error 54570a89417SAndreas Gohr */ 54670a89417SAndreas Gohr protected function _selectUserGroups($userdata) { 54770a89417SAndreas Gohr global $conf; 54870a89417SAndreas Gohr $sql = $this->getConf('select-user-groups'); 5495de3a6a5SAndreas Gohr $result = $this->_query($sql, $userdata); 5505de3a6a5SAndreas Gohr if($result === false) return false; 55170a89417SAndreas Gohr 55270a89417SAndreas Gohr $groups = array($conf['defaultgroup']); // always add default config 5535de3a6a5SAndreas Gohr foreach($result as $row) { 5545de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5555de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned in select-user-groups statement"); 5565de3a6a5SAndreas Gohr return false; 5575de3a6a5SAndreas Gohr } 55870a89417SAndreas Gohr $groups[] = $row['group']; 55970a89417SAndreas Gohr } 56070a89417SAndreas Gohr 56170a89417SAndreas Gohr $groups = array_unique($groups); 56270a89417SAndreas Gohr sort($groups); 56370a89417SAndreas Gohr return $groups; 56470a89417SAndreas Gohr } 56570a89417SAndreas Gohr 56670a89417SAndreas Gohr /** 5675de3a6a5SAndreas Gohr * Select all available groups 5685de3a6a5SAndreas Gohr * 5695de3a6a5SAndreas Gohr * @return array|bool list of all available groups and their properties 5705de3a6a5SAndreas Gohr */ 5715de3a6a5SAndreas Gohr protected function _selectGroups() { 5720cec3e2aSAndreas Gohr if($this->groupcache) return $this->groupcache; 5730cec3e2aSAndreas Gohr 5745de3a6a5SAndreas Gohr $sql = $this->getConf('select-groups'); 5755de3a6a5SAndreas Gohr $result = $this->_query($sql); 5765de3a6a5SAndreas Gohr if($result === false) return false; 5775de3a6a5SAndreas Gohr 5785de3a6a5SAndreas Gohr $groups = array(); 5795de3a6a5SAndreas Gohr foreach($result as $row) { 5805de3a6a5SAndreas Gohr if(!isset($row['group'])) { 5815de3a6a5SAndreas Gohr $this->_debug("No 'group' field returned from select-groups statement", -1, __LINE__); 5825de3a6a5SAndreas Gohr return false; 5835de3a6a5SAndreas Gohr } 5845de3a6a5SAndreas Gohr 5855de3a6a5SAndreas Gohr // relayout result with group name as key 5865de3a6a5SAndreas Gohr $group = $row['group']; 5875de3a6a5SAndreas Gohr $groups[$group] = $row; 5885de3a6a5SAndreas Gohr } 5895de3a6a5SAndreas Gohr 5905de3a6a5SAndreas Gohr ksort($groups); 5915de3a6a5SAndreas Gohr return $groups; 5925de3a6a5SAndreas Gohr } 5935de3a6a5SAndreas Gohr 5945de3a6a5SAndreas Gohr /** 5950cec3e2aSAndreas Gohr * Remove all entries from the group cache 5960cec3e2aSAndreas Gohr */ 5970cec3e2aSAndreas Gohr protected function _clearGroupCache() { 5980cec3e2aSAndreas Gohr $this->groupcache = null; 5990cec3e2aSAndreas Gohr } 6000cec3e2aSAndreas Gohr 6010cec3e2aSAndreas Gohr /** 6024fb8dfabSAndreas Gohr * Adds the user to the group 6035de3a6a5SAndreas Gohr * 6045de3a6a5SAndreas Gohr * @param array $userdata all the user data 6055de3a6a5SAndreas Gohr * @param array $groupdata all the group data 6065de3a6a5SAndreas Gohr * @return bool 6075de3a6a5SAndreas Gohr */ 6085de3a6a5SAndreas Gohr protected function _joinGroup($userdata, $groupdata) { 6095de3a6a5SAndreas Gohr $data = array_merge($userdata, $groupdata); 6105de3a6a5SAndreas Gohr $sql = $this->getConf('join-group'); 6115de3a6a5SAndreas Gohr $result = $this->_query($sql, $data); 6125de3a6a5SAndreas Gohr if($result === false) return false; 6135de3a6a5SAndreas Gohr return true; 6145de3a6a5SAndreas Gohr } 6155de3a6a5SAndreas Gohr 6165de3a6a5SAndreas Gohr /** 6174fb8dfabSAndreas Gohr * Removes the user from the group 6184fb8dfabSAndreas Gohr * 6194fb8dfabSAndreas Gohr * @param array $userdata all the user data 6204fb8dfabSAndreas Gohr * @param array $groupdata all the group data 6214fb8dfabSAndreas Gohr * @return bool 6224fb8dfabSAndreas Gohr */ 6234fb8dfabSAndreas Gohr protected function _leaveGroup($userdata, $groupdata) { 6244fb8dfabSAndreas Gohr $data = array_merge($userdata, $groupdata); 6254fb8dfabSAndreas Gohr $sql = $this->getConf('leave-group'); 6264fb8dfabSAndreas Gohr $result = $this->_query($sql, $data); 6274fb8dfabSAndreas Gohr if($result === false) return false; 6284fb8dfabSAndreas Gohr return true; 6294fb8dfabSAndreas Gohr } 6304fb8dfabSAndreas Gohr 6314fb8dfabSAndreas Gohr /** 63270a89417SAndreas Gohr * Executes a query 63370a89417SAndreas Gohr * 63470a89417SAndreas Gohr * @param string $sql The SQL statement to execute 63570a89417SAndreas Gohr * @param array $arguments Named parameters to be used in the statement 636f695c447SAndreas Gohr * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error 63770a89417SAndreas Gohr */ 6385de3a6a5SAndreas Gohr protected function _query($sql, $arguments = array()) { 639f695c447SAndreas Gohr $sql = trim($sql); 6405de3a6a5SAndreas Gohr if(empty($sql)) { 6415de3a6a5SAndreas Gohr $this->_debug('No SQL query given', -1, __LINE__); 6425de3a6a5SAndreas Gohr return false; 6435de3a6a5SAndreas Gohr } 6445de3a6a5SAndreas Gohr 64514119d44SAndreas Gohr // execute 64670a89417SAndreas Gohr $params = array(); 64714119d44SAndreas Gohr $sth = $this->pdo->prepare($sql); 64814119d44SAndreas Gohr try { 64914119d44SAndreas Gohr // prepare parameters - we only use those that exist in the SQL 65070a89417SAndreas Gohr foreach($arguments as $key => $value) { 65170a89417SAndreas Gohr if(is_array($value)) continue; 65270a89417SAndreas Gohr if(is_object($value)) continue; 65370a89417SAndreas Gohr if($key[0] != ':') $key = ":$key"; // prefix with colon if needed 65414119d44SAndreas Gohr if(strpos($sql, $key) === false) continue; // skip if parameter is missing 65514119d44SAndreas Gohr 65614119d44SAndreas Gohr if(is_int($value)) { 65714119d44SAndreas Gohr $sth->bindValue($key, $value, PDO::PARAM_INT); 65814119d44SAndreas Gohr } else { 65914119d44SAndreas Gohr $sth->bindValue($key, $value); 66014119d44SAndreas Gohr } 661f6cd8a7fSphjanderson $params[$key] = $value; //remember for debugging 66270a89417SAndreas Gohr } 66370a89417SAndreas Gohr 66414119d44SAndreas Gohr $sth->execute(); 665f695c447SAndreas Gohr if(strtolower(substr($sql, 0, 6)) == 'select') { 66670a89417SAndreas Gohr $result = $sth->fetchAll(); 667f695c447SAndreas Gohr } else { 668f695c447SAndreas Gohr $result = $sth->rowCount(); 669f695c447SAndreas Gohr } 6705de3a6a5SAndreas Gohr } catch(Exception $e) { 6714fb8dfabSAndreas Gohr // report the caller's line 6724fb8dfabSAndreas Gohr $trace = debug_backtrace(); 6734fb8dfabSAndreas Gohr $line = $trace[0]['line']; 6745de3a6a5SAndreas Gohr $dsql = $this->_debugSQL($sql, $params, !defined('DOKU_UNITTEST')); 6754fb8dfabSAndreas Gohr $this->_debug($e, -1, $line); 6764fb8dfabSAndreas Gohr $this->_debug("SQL: <pre>$dsql</pre>", -1, $line); 67770a89417SAndreas Gohr $result = false; 6781600c7ccSAndreas Gohr } 67970a89417SAndreas Gohr $sth->closeCursor(); 68070a89417SAndreas Gohr $sth = null; 68170a89417SAndreas Gohr 6825de3a6a5SAndreas Gohr return $result; 6835de3a6a5SAndreas Gohr } 68470a89417SAndreas Gohr 68570a89417SAndreas Gohr /** 686f64dbc90SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 687f64dbc90SAndreas Gohr * 688f64dbc90SAndreas Gohr * @param string|Exception $message 689f64dbc90SAndreas Gohr * @param int $err 690f64dbc90SAndreas Gohr * @param int $line 691f64dbc90SAndreas Gohr */ 692f64dbc90SAndreas Gohr protected function _debug($message, $err = 0, $line = 0) { 693f64dbc90SAndreas Gohr if(!$this->getConf('debug')) return; 694f64dbc90SAndreas Gohr if(is_a($message, 'Exception')) { 695f64dbc90SAndreas Gohr $err = -1; 696f64dbc90SAndreas Gohr $msg = $message->getMessage(); 6974fb8dfabSAndreas Gohr if(!$line) $line = $message->getLine(); 698f64dbc90SAndreas Gohr } else { 699f64dbc90SAndreas Gohr $msg = $message; 700f64dbc90SAndreas Gohr } 701f64dbc90SAndreas Gohr 702f64dbc90SAndreas Gohr if(defined('DOKU_UNITTEST')) { 703f64dbc90SAndreas Gohr printf("\n%s, %s:%d\n", $msg, __FILE__, $line); 704f64dbc90SAndreas Gohr } else { 705f64dbc90SAndreas Gohr msg('authpdo: ' . $msg, $err, $line, __FILE__); 706f64dbc90SAndreas Gohr } 707f64dbc90SAndreas Gohr } 7085de3a6a5SAndreas Gohr 7095de3a6a5SAndreas Gohr /** 7105de3a6a5SAndreas Gohr * Check if the given config strings are set 7115de3a6a5SAndreas Gohr * 7125de3a6a5SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 7135de3a6a5SAndreas Gohr * 7145de3a6a5SAndreas Gohr * @param string[] $keys 7155de3a6a5SAndreas Gohr * @return bool 7165de3a6a5SAndreas Gohr */ 7175de3a6a5SAndreas Gohr protected function _chkcnf($keys) { 7185de3a6a5SAndreas Gohr foreach($keys as $key) { 719745947b2SAndreas Gohr if(!trim($this->getConf($key))) return false; 7205de3a6a5SAndreas Gohr } 7215de3a6a5SAndreas Gohr 7225de3a6a5SAndreas Gohr return true; 7235de3a6a5SAndreas Gohr } 7245de3a6a5SAndreas Gohr 7255de3a6a5SAndreas Gohr /** 7265de3a6a5SAndreas Gohr * create an approximation of the SQL string with parameters replaced 7275de3a6a5SAndreas Gohr * 7285de3a6a5SAndreas Gohr * @param string $sql 7295de3a6a5SAndreas Gohr * @param array $params 7305de3a6a5SAndreas Gohr * @param bool $htmlescape Should the result be escaped for output in HTML? 7315de3a6a5SAndreas Gohr * @return string 7325de3a6a5SAndreas Gohr */ 7335de3a6a5SAndreas Gohr protected function _debugSQL($sql, $params, $htmlescape = true) { 7345de3a6a5SAndreas Gohr foreach($params as $key => $val) { 7355de3a6a5SAndreas Gohr if(is_int($val)) { 7365de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_INT); 7375de3a6a5SAndreas Gohr } elseif(is_bool($val)) { 7385de3a6a5SAndreas Gohr $val = $this->pdo->quote($val, PDO::PARAM_BOOL); 7395de3a6a5SAndreas Gohr } elseif(is_null($val)) { 7405de3a6a5SAndreas Gohr $val = 'NULL'; 7415de3a6a5SAndreas Gohr } else { 7425de3a6a5SAndreas Gohr $val = $this->pdo->quote($val); 7435de3a6a5SAndreas Gohr } 7445de3a6a5SAndreas Gohr $sql = str_replace($key, $val, $sql); 7455de3a6a5SAndreas Gohr } 7465de3a6a5SAndreas Gohr if($htmlescape) $sql = hsc($sql); 7475de3a6a5SAndreas Gohr return $sql; 7485de3a6a5SAndreas Gohr } 749f64dbc90SAndreas Gohr} 750f64dbc90SAndreas Gohr 751f64dbc90SAndreas Gohr// vim:ts=4:sw=4:et: 752