aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/authpdo/auth.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/plugins/authpdo/auth.php')
-rw-r--r--lib/plugins/authpdo/auth.php461
1 files changed, 240 insertions, 221 deletions
diff --git a/lib/plugins/authpdo/auth.php b/lib/plugins/authpdo/auth.php
index 12263b7b9..9c0968e30 100644
--- a/lib/plugins/authpdo/auth.php
+++ b/lib/plugins/authpdo/auth.php
@@ -6,13 +6,11 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
-// must be run within Dokuwiki
-if(!defined('DOKU_INC')) die();
-
/**
* Class auth_plugin_authpdo
*/
-class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
+class auth_plugin_authpdo extends DokuWiki_Auth_Plugin
+{
/** @var PDO */
protected $pdo;
@@ -23,17 +21,18 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Constructor.
*/
- public function __construct() {
+ public function __construct()
+ {
parent::__construct(); // for compatibility
- if(!class_exists('PDO')) {
- $this->_debug('PDO extension for PHP not found.', -1, __LINE__);
+ if (!class_exists('PDO')) {
+ $this->debugMsg('PDO extension for PHP not found.', -1, __LINE__);
$this->success = false;
return;
}
- if(!$this->getConf('dsn')) {
- $this->_debug('No DSN specified', -1, __LINE__);
+ if (!$this->getConf('dsn')) {
+ $this->debugMsg('No DSN specified', -1, __LINE__);
$this->success = false;
return;
}
@@ -49,15 +48,15 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes
)
);
- } catch(PDOException $e) {
- $this->_debug($e);
+ } catch (PDOException $e) {
+ $this->debugMsg($e);
msg($this->getLang('connectfail'), -1);
$this->success = false;
return;
}
// can Users be created?
- $this->cando['addUser'] = $this->_chkcnf(
+ $this->cando['addUser'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -69,7 +68,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can Users be deleted?
- $this->cando['delUser'] = $this->_chkcnf(
+ $this->cando['delUser'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -80,7 +79,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can login names be changed?
- $this->cando['modLogin'] = $this->_chkcnf(
+ $this->cando['modLogin'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -89,7 +88,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can passwords be changed?
- $this->cando['modPass'] = $this->_chkcnf(
+ $this->cando['modPass'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -98,7 +97,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can real names be changed?
- $this->cando['modName'] = $this->_chkcnf(
+ $this->cando['modName'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -107,7 +106,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can real email be changed?
- $this->cando['modMail'] = $this->_chkcnf(
+ $this->cando['modMail'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -116,7 +115,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can groups be changed?
- $this->cando['modGroups'] = $this->_chkcnf(
+ $this->cando['modGroups'] = $this->checkConfig(
array(
'select-user',
'select-user-groups',
@@ -128,21 +127,21 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
);
// can a filtered list of users be retrieved?
- $this->cando['getUsers'] = $this->_chkcnf(
+ $this->cando['getUsers'] = $this->checkConfig(
array(
'list-users'
)
);
// can the number of users be retrieved?
- $this->cando['getUserCount'] = $this->_chkcnf(
+ $this->cando['getUserCount'] = $this->checkConfig(
array(
'count-users'
)
);
// can a list of available groups be retrieved?
- $this->cando['getGroups'] = $this->_chkcnf(
+ $this->cando['getGroups'] = $this->checkConfig(
array(
'select-groups'
)
@@ -154,28 +153,29 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Check user+password
*
- * @param string $user the user name
- * @param string $pass the clear text password
+ * @param string $user the user name
+ * @param string $pass the clear text password
* @return bool
*/
- public function checkPass($user, $pass) {
+ public function checkPass($user, $pass)
+ {
- $userdata = $this->_selectUser($user);
- if($userdata == false) return false;
+ $userdata = $this->selectUser($user);
+ if ($userdata == false) return false;
// password checking done in SQL?
- if($this->_chkcnf(array('check-pass'))) {
+ if ($this->checkConfig(array('check-pass'))) {
$userdata['clear'] = $pass;
$userdata['hash'] = auth_cryptPassword($pass);
- $result = $this->_query($this->getConf('check-pass'), $userdata);
- if($result === false) return false;
+ $result = $this->query($this->getConf('check-pass'), $userdata);
+ if ($result === false) return false;
return (count($result) == 1);
}
// we do password checking on our own
- if(isset($userdata['hash'])) {
+ if (isset($userdata['hash'])) {
// hashed password
- $passhash = new PassHash();
+ $passhash = new \dokuwiki\PassHash();
return $passhash->verify_hash($pass, $userdata['hash']);
} else {
// clear text password in the database O_o
@@ -193,20 +193,21 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* mail string email addres of the user
* grps array list of groups the user is in
*
- * @param string $user the user name
- * @param bool $requireGroups whether or not the returned data must include groups
+ * @param string $user the user name
+ * @param bool $requireGroups whether or not the returned data must include groups
* @return array|bool containing user data or false
*/
- public function getUserData($user, $requireGroups = true) {
- $data = $this->_selectUser($user);
- if($data == false) return false;
+ public function getUserData($user, $requireGroups = true)
+ {
+ $data = $this->selectUser($user);
+ if ($data == false) return false;
- if(isset($data['hash'])) unset($data['hash']);
- if(isset($data['clean'])) unset($data['clean']);
+ if (isset($data['hash'])) unset($data['hash']);
+ if (isset($data['clean'])) unset($data['clean']);
- if($requireGroups) {
- $data['grps'] = $this->_selectUserGroups($data);
- if($data['grps'] === false) return false;
+ if ($requireGroups) {
+ $data['grps'] = $this->selectUserGroups($data);
+ if ($data['grps'] === false) return false;
}
return $data;
@@ -223,23 +224,24 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
*
* Set addUser capability when implemented
*
- * @param string $user
- * @param string $clear
- * @param string $name
- * @param string $mail
- * @param null|array $grps
+ * @param string $user
+ * @param string $clear
+ * @param string $name
+ * @param string $mail
+ * @param null|array $grps
* @return bool|null
*/
- public function createUser($user, $clear, $name, $mail, $grps = null) {
+ public function createUser($user, $clear, $name, $mail, $grps = null)
+ {
global $conf;
- if(($info = $this->getUserData($user, false)) !== false) {
+ if (($info = $this->getUserData($user, false)) !== false) {
msg($this->getLang('userexists'), -1);
return false; // user already exists
}
// prepare data
- if($grps == null) $grps = array();
+ if ($grps == null) $grps = array();
array_unshift($grps, $conf['defaultgroup']);
$grps = array_unique($grps);
$hash = auth_cryptPassword($clear);
@@ -249,25 +251,25 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
$this->pdo->beginTransaction();
{
// insert the user
- $ok = $this->_query($this->getConf('insert-user'), $userdata);
- if($ok === false) goto FAIL;
+ $ok = $this->query($this->getConf('insert-user'), $userdata);
+ if ($ok === false) goto FAIL;
$userdata = $this->getUserData($user, false);
- if($userdata === false) goto FAIL;
+ if ($userdata === false) goto FAIL;
// create all groups that do not exist, the refetch the groups
- $allgroups = $this->_selectGroups();
- foreach($grps as $group) {
- if(!isset($allgroups[$group])) {
+ $allgroups = $this->selectGroups();
+ foreach ($grps as $group) {
+ if (!isset($allgroups[$group])) {
$ok = $this->addGroup($group);
- if($ok === false) goto FAIL;
+ if ($ok === false) goto FAIL;
}
}
- $allgroups = $this->_selectGroups();
+ $allgroups = $this->selectGroups();
// add user to the groups
- foreach($grps as $group) {
- $ok = $this->_joinGroup($userdata, $allgroups[$group]);
- if($ok === false) goto FAIL;
+ foreach ($grps as $group) {
+ $ok = $this->joinGroup($userdata, $allgroups[$group]);
+ if ($ok === false) goto FAIL;
}
}
$this->pdo->commit();
@@ -276,7 +278,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
// something went wrong, rollback
FAIL:
$this->pdo->rollBack();
- $this->_debug('Transaction rolled back', 0, __LINE__);
+ $this->debugMsg('Transaction rolled back', 0, __LINE__);
msg($this->getLang('writefail'), -1);
return null; // return error
}
@@ -284,11 +286,12 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Modify user data
*
- * @param string $user nick of the user to be changed
- * @param array $changes array of field/value pairs to be changed (password will be clear text)
+ * @param string $user nick of the user to be changed
+ * @param array $changes array of field/value pairs to be changed (password will be clear text)
* @return bool
*/
- public function modifyUser($user, $changes) {
+ public function modifyUser($user, $changes)
+ {
// secure everything in transaction
$this->pdo->beginTransaction();
{
@@ -297,64 +300,64 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
unset($olddata['grps']);
// changing the user name?
- if(isset($changes['user'])) {
- if($this->getUserData($changes['user'], false)) goto FAIL;
+ if (isset($changes['user'])) {
+ if ($this->getUserData($changes['user'], false)) goto FAIL;
$params = $olddata;
$params['newlogin'] = $changes['user'];
- $ok = $this->_query($this->getConf('update-user-login'), $params);
- if($ok === false) goto FAIL;
+ $ok = $this->query($this->getConf('update-user-login'), $params);
+ if ($ok === false) goto FAIL;
}
// changing the password?
- if(isset($changes['pass'])) {
+ if (isset($changes['pass'])) {
$params = $olddata;
$params['clear'] = $changes['pass'];
$params['hash'] = auth_cryptPassword($changes['pass']);
- $ok = $this->_query($this->getConf('update-user-pass'), $params);
- if($ok === false) goto FAIL;
+ $ok = $this->query($this->getConf('update-user-pass'), $params);
+ if ($ok === false) goto FAIL;
}
// changing info?
- if(isset($changes['mail']) || isset($changes['name'])) {
+ if (isset($changes['mail']) || isset($changes['name'])) {
$params = $olddata;
- if(isset($changes['mail'])) $params['mail'] = $changes['mail'];
- if(isset($changes['name'])) $params['name'] = $changes['name'];
+ if (isset($changes['mail'])) $params['mail'] = $changes['mail'];
+ if (isset($changes['name'])) $params['name'] = $changes['name'];
- $ok = $this->_query($this->getConf('update-user-info'), $params);
- if($ok === false) goto FAIL;
+ $ok = $this->query($this->getConf('update-user-info'), $params);
+ if ($ok === false) goto FAIL;
}
// changing groups?
- if(isset($changes['grps'])) {
- $allgroups = $this->_selectGroups();
+ if (isset($changes['grps'])) {
+ $allgroups = $this->selectGroups();
// remove membership for previous groups
- foreach($oldgroups as $group) {
- if(!in_array($group, $changes['grps']) && isset($allgroups[$group])) {
- $ok = $this->_leaveGroup($olddata, $allgroups[$group]);
- if($ok === false) goto FAIL;
+ foreach ($oldgroups as $group) {
+ if (!in_array($group, $changes['grps']) && isset($allgroups[$group])) {
+ $ok = $this->leaveGroup($olddata, $allgroups[$group]);
+ if ($ok === false) goto FAIL;
}
}
// create all new groups that are missing
$added = 0;
- foreach($changes['grps'] as $group) {
- if(!isset($allgroups[$group])) {
+ foreach ($changes['grps'] as $group) {
+ if (!isset($allgroups[$group])) {
$ok = $this->addGroup($group);
- if($ok === false) goto FAIL;
+ if ($ok === false) goto FAIL;
$added++;
}
}
// reload group info
- if($added > 0) $allgroups = $this->_selectGroups();
+ if ($added > 0) $allgroups = $this->selectGroups();
// add membership for new groups
- foreach($changes['grps'] as $group) {
- if(!in_array($group, $oldgroups)) {
- $ok = $this->_joinGroup($olddata, $allgroups[$group]);
- if($ok === false) goto FAIL;
+ foreach ($changes['grps'] as $group) {
+ if (!in_array($group, $oldgroups)) {
+ $ok = $this->joinGroup($olddata, $allgroups[$group]);
+ if ($ok === false) goto FAIL;
}
}
}
@@ -366,7 +369,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
// something went wrong, rollback
FAIL:
$this->pdo->rollBack();
- $this->_debug('Transaction rolled back', 0, __LINE__);
+ $this->debugMsg('Transaction rolled back', 0, __LINE__);
msg($this->getLang('writefail'), -1);
return false; // return error
}
@@ -376,13 +379,14 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
*
* Set delUser capability when implemented
*
- * @param array $users
+ * @param array $users
* @return int number of users deleted
*/
- public function deleteUsers($users) {
+ public function deleteUsers($users)
+ {
$count = 0;
- foreach($users as $user) {
- if($this->_deleteUser($user)) $count++;
+ foreach ($users as $user) {
+ if ($this->deleteUser($user)) $count++;
}
return $count;
}
@@ -392,40 +396,41 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
*
* Set getUsers capability when implemented
*
- * @param int $start index of first user to be returned
- * @param int $limit max number of users to be returned
- * @param array $filter array of field/pattern pairs, null for no filter
+ * @param int $start index of first user to be returned
+ * @param int $limit max number of users to be returned
+ * @param array $filter array of field/pattern pairs, null for no filter
* @return array list of userinfo (refer getUserData for internal userinfo details)
*/
- public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
- if($limit < 0) $limit = 10000; // we don't support no limit
- if(is_null($filter)) $filter = array();
-
- if(isset($filter['grps'])) $filter['group'] = $filter['grps'];
- foreach(array('user', 'name', 'mail', 'group') as $key) {
- if(!isset($filter[$key])) {
+ public function retrieveUsers($start = 0, $limit = -1, $filter = null)
+ {
+ if ($limit < 0) $limit = 10000; // we don't support no limit
+ if (is_null($filter)) $filter = array();
+
+ if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
+ foreach (array('user', 'name', 'mail', 'group') as $key) {
+ if (!isset($filter[$key])) {
$filter[$key] = '%';
} else {
$filter[$key] = '%' . $filter[$key] . '%';
}
}
- $filter['start'] = (int) $start;
- $filter['end'] = (int) $start + $limit;
- $filter['limit'] = (int) $limit;
+ $filter['start'] = (int)$start;
+ $filter['end'] = (int)$start + $limit;
+ $filter['limit'] = (int)$limit;
- $result = $this->_query($this->getConf('list-users'), $filter);
- if(!$result) return array();
+ $result = $this->query($this->getConf('list-users'), $filter);
+ if (!$result) return array();
$users = array();
- if(is_array($result)){
- foreach($result as $row) {
- if(!isset($row['user'])) {
- $this->_debug("list-users statement did not return 'user' attribute", -1, __LINE__);
+ if (is_array($result)) {
+ foreach ($result as $row) {
+ if (!isset($row['user'])) {
+ $this->debugMsg("list-users statement did not return 'user' attribute", -1, __LINE__);
return array();
}
$users[] = $this->getUserData($row['user']);
}
- }else{
- $this->_debug("list-users statement did not return a list of result", -1, __LINE__);
+ } else {
+ $this->debugMsg("list-users statement did not return a list of result", -1, __LINE__);
}
return $users;
}
@@ -433,26 +438,27 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Return a count of the number of user which meet $filter criteria
*
- * @param array $filter array of field/pattern pairs, empty array for no filter
+ * @param array $filter array of field/pattern pairs, empty array for no filter
* @return int
*/
- public function getUserCount($filter = array()) {
- if(is_null($filter)) $filter = array();
+ public function getUserCount($filter = array())
+ {
+ if (is_null($filter)) $filter = array();
- if(isset($filter['grps'])) $filter['group'] = $filter['grps'];
- foreach(array('user', 'name', 'mail', 'group') as $key) {
- if(!isset($filter[$key])) {
+ if (isset($filter['grps'])) $filter['group'] = $filter['grps'];
+ foreach (array('user', 'name', 'mail', 'group') as $key) {
+ if (!isset($filter[$key])) {
$filter[$key] = '%';
} else {
$filter[$key] = '%' . $filter[$key] . '%';
}
}
- $result = $this->_query($this->getConf('count-users'), $filter);
- if(!$result || !isset($result[0]['count'])) {
- $this->_debug("Statement did not return 'count' attribute", -1, __LINE__);
+ $result = $this->query($this->getConf('count-users'), $filter);
+ if (!$result || !isset($result[0]['count'])) {
+ $this->debugMsg("Statement did not return 'count' attribute", -1, __LINE__);
}
- return (int) $result[0]['count'];
+ return (int)$result[0]['count'];
}
/**
@@ -461,12 +467,13 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param string $group
* @return bool
*/
- public function addGroup($group) {
+ public function addGroup($group)
+ {
$sql = $this->getConf('insert-group');
- $result = $this->_query($sql, array(':group' => $group));
- $this->_clearGroupCache();
- if($result === false) return false;
+ $result = $this->query($sql, array(':group' => $group));
+ $this->clearGroupCache();
+ if ($result === false) return false;
return true;
}
@@ -475,15 +482,16 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
*
* Set getGroups capability when implemented
*
- * @param int $start
- * @param int $limit
+ * @param int $start
+ * @param int $limit
* @return array
*/
- public function retrieveGroups($start = 0, $limit = 0) {
- $groups = array_keys($this->_selectGroups());
- if($groups === false) return array();
+ public function retrieveGroups($start = 0, $limit = 0)
+ {
+ $groups = array_keys($this->selectGroups());
+ if ($groups === false) return array();
- if(!$limit) {
+ if (!$limit) {
return array_splice($groups, $start);
} else {
return array_splice($groups, $start, $limit);
@@ -496,38 +504,39 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param string $user the user name
* @return bool|array user data, false on error
*/
- protected function _selectUser($user) {
+ protected function selectUser($user)
+ {
$sql = $this->getConf('select-user');
- $result = $this->_query($sql, array(':user' => $user));
- if(!$result) return false;
+ $result = $this->query($sql, array(':user' => $user));
+ if (!$result) return false;
- if(count($result) > 1) {
- $this->_debug('Found more than one matching user', -1, __LINE__);
+ if (count($result) > 1) {
+ $this->debugMsg('Found more than one matching user', -1, __LINE__);
return false;
}
$data = array_shift($result);
$dataok = true;
- if(!isset($data['user'])) {
- $this->_debug("Statement did not return 'user' attribute", -1, __LINE__);
+ if (!isset($data['user'])) {
+ $this->debugMsg("Statement did not return 'user' attribute", -1, __LINE__);
$dataok = false;
}
- if(!isset($data['hash']) && !isset($data['clear']) && !$this->_chkcnf(array('check-pass'))) {
- $this->_debug("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
+ if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(array('check-pass'))) {
+ $this->debugMsg("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__);
$dataok = false;
}
- if(!isset($data['name'])) {
- $this->_debug("Statement did not return 'name' attribute", -1, __LINE__);
+ if (!isset($data['name'])) {
+ $this->debugMsg("Statement did not return 'name' attribute", -1, __LINE__);
$dataok = false;
}
- if(!isset($data['mail'])) {
- $this->_debug("Statement did not return 'mail' attribute", -1, __LINE__);
+ if (!isset($data['mail'])) {
+ $this->debugMsg("Statement did not return 'mail' attribute", -1, __LINE__);
$dataok = false;
}
- if(!$dataok) return false;
+ if (!$dataok) return false;
return $data;
}
@@ -537,22 +546,23 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param string $user
* @return bool true when the user was deleted
*/
- protected function _deleteUser($user) {
+ protected function deleteUser($user)
+ {
$this->pdo->beginTransaction();
{
$userdata = $this->getUserData($user);
- if($userdata === false) goto FAIL;
- $allgroups = $this->_selectGroups();
+ if ($userdata === false) goto FAIL;
+ $allgroups = $this->selectGroups();
// remove group memberships (ignore errors)
- foreach($userdata['grps'] as $group) {
- if(isset($allgroups[$group])) {
- $this->_leaveGroup($userdata, $allgroups[$group]);
+ foreach ($userdata['grps'] as $group) {
+ if (isset($allgroups[$group])) {
+ $this->leaveGroup($userdata, $allgroups[$group]);
}
}
- $ok = $this->_query($this->getConf('delete-user'), $userdata);
- if($ok === false) goto FAIL;
+ $ok = $this->query($this->getConf('delete-user'), $userdata);
+ if ($ok === false) goto FAIL;
}
$this->pdo->commit();
return true;
@@ -568,23 +578,24 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param array $userdata The userdata as returned by _selectUser()
* @return array|bool list of group names, false on error
*/
- protected function _selectUserGroups($userdata) {
+ protected function selectUserGroups($userdata)
+ {
global $conf;
$sql = $this->getConf('select-user-groups');
- $result = $this->_query($sql, $userdata);
- if($result === false) return false;
+ $result = $this->query($sql, $userdata);
+ if ($result === false) return false;
$groups = array($conf['defaultgroup']); // always add default config
- if(is_array($result)){
- foreach($result as $row) {
- if(!isset($row['group'])) {
- $this->_debug("No 'group' field returned in select-user-groups statement");
+ if (is_array($result)) {
+ foreach ($result as $row) {
+ if (!isset($row['group'])) {
+ $this->debugMsg("No 'group' field returned in select-user-groups statement", -1, __LINE__);
return false;
}
$groups[] = $row['group'];
}
- }else{
- $this->_debug("select-user-groups statement did not return a list of result", -1, __LINE__);
+ } else {
+ $this->debugMsg("select-user-groups statement did not return a list of result", -1, __LINE__);
}
$groups = array_unique($groups);
@@ -597,18 +608,19 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
*
* @return array|bool list of all available groups and their properties
*/
- protected function _selectGroups() {
- if($this->groupcache) return $this->groupcache;
+ protected function selectGroups()
+ {
+ if ($this->groupcache) return $this->groupcache;
$sql = $this->getConf('select-groups');
- $result = $this->_query($sql);
- if($result === false) return false;
+ $result = $this->query($sql);
+ if ($result === false) return false;
$groups = array();
- if(is_array($result)){
- foreach($result as $row) {
- if(!isset($row['group'])) {
- $this->_debug("No 'group' field returned from select-groups statement", -1, __LINE__);
+ if (is_array($result)) {
+ foreach ($result as $row) {
+ if (!isset($row['group'])) {
+ $this->debugMsg("No 'group' field returned from select-groups statement", -1, __LINE__);
return false;
}
@@ -616,8 +628,8 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
$group = $row['group'];
$groups[$group] = $row;
}
- }else{
- $this->_debug("select-groups statement did not return a list of result", -1, __LINE__);
+ } else {
+ $this->debugMsg("select-groups statement did not return a list of result", -1, __LINE__);
}
ksort($groups);
@@ -627,7 +639,8 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Remove all entries from the group cache
*/
- protected function _clearGroupCache() {
+ protected function clearGroupCache()
+ {
$this->groupcache = null;
}
@@ -638,11 +651,12 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param array $groupdata all the group data
* @return bool
*/
- protected function _joinGroup($userdata, $groupdata) {
+ protected function joinGroup($userdata, $groupdata)
+ {
$data = array_merge($userdata, $groupdata);
$sql = $this->getConf('join-group');
- $result = $this->_query($sql, $data);
- if($result === false) return false;
+ $result = $this->query($sql, $data);
+ if ($result === false) return false;
return true;
}
@@ -653,11 +667,12 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param array $groupdata all the group data
* @return bool
*/
- protected function _leaveGroup($userdata, $groupdata) {
+ protected function leaveGroup($userdata, $groupdata)
+ {
$data = array_merge($userdata, $groupdata);
$sql = $this->getConf('leave-group');
- $result = $this->_query($sql, $data);
- if($result === false) return false;
+ $result = $this->query($sql, $data);
+ if ($result === false) return false;
return true;
}
@@ -668,10 +683,11 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param array $arguments Named parameters to be used in the statement
* @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error
*/
- protected function _query($sql, $arguments = array()) {
+ protected function query($sql, $arguments = array())
+ {
$sql = trim($sql);
- if(empty($sql)) {
- $this->_debug('No SQL query given', -1, __LINE__);
+ if (empty($sql)) {
+ $this->debugMsg('No SQL query given', -1, __LINE__);
return false;
}
@@ -681,13 +697,13 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
$result = false;
try {
// prepare parameters - we only use those that exist in the SQL
- foreach($arguments as $key => $value) {
- if(is_array($value)) continue;
- if(is_object($value)) continue;
- if($key[0] != ':') $key = ":$key"; // prefix with colon if needed
- if(strpos($sql, $key) === false) continue; // skip if parameter is missing
+ foreach ($arguments as $key => $value) {
+ if (is_array($value)) continue;
+ if (is_object($value)) continue;
+ if ($key[0] != ':') $key = ":$key"; // prefix with colon if needed
+ if (strpos($sql, $key) === false) continue; // skip if parameter is missing
- if(is_int($value)) {
+ if (is_int($value)) {
$sth->bindValue($key, $value, PDO::PARAM_INT);
} else {
$sth->bindValue($key, $value);
@@ -699,29 +715,29 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
// only report last line's result
$hasnextrowset = true;
$currentsql = $sql;
- while($hasnextrowset){
- if(strtolower(substr($currentsql, 0, 6)) == 'select') {
+ while ($hasnextrowset) {
+ if (strtolower(substr($currentsql, 0, 6)) == 'select') {
$result = $sth->fetchAll();
} else {
$result = $sth->rowCount();
}
$semi_pos = strpos($currentsql, ';');
- if($semi_pos){
- $currentsql = trim(substr($currentsql, $semi_pos+1));
+ if ($semi_pos) {
+ $currentsql = trim(substr($currentsql, $semi_pos + 1));
}
- try{
+ try {
$hasnextrowset = $sth->nextRowset(); // run next rowset
- }catch(PDOException $rowset_e){
+ } catch (PDOException $rowset_e) {
$hasnextrowset = false; // driver does not support multi-rowset, should be executed in one time
}
}
- } catch(Exception $e) {
+ } catch (Exception $e) {
// report the caller's line
$trace = debug_backtrace();
$line = $trace[0]['line'];
- $dsql = $this->_debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
- $this->_debug($e, -1, $line);
- $this->_debug("SQL: <pre>$dsql</pre>", -1, $line);
+ $dsql = $this->debugSQL($sql, $params, !defined('DOKU_UNITTEST'));
+ $this->debugMsg($e, -1, $line);
+ $this->debugMsg("SQL: <pre>$dsql</pre>", -1, $line);
}
$sth->closeCursor();
$sth = null;
@@ -736,17 +752,18 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param int $err
* @param int $line
*/
- protected function _debug($message, $err = 0, $line = 0) {
- if(!$this->getConf('debug')) return;
- if(is_a($message, 'Exception')) {
+ protected function debugMsg($message, $err = 0, $line = 0)
+ {
+ if (!$this->getConf('debug')) return;
+ if (is_a($message, 'Exception')) {
$err = -1;
$msg = $message->getMessage();
- if(!$line) $line = $message->getLine();
+ if (!$line) $line = $message->getLine();
} else {
$msg = $message;
}
- if(defined('DOKU_UNITTEST')) {
+ if (defined('DOKU_UNITTEST')) {
printf("\n%s, %s:%d\n", $msg, __FILE__, $line);
} else {
msg('authpdo: ' . $msg, $err, $line, __FILE__);
@@ -756,22 +773,23 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
/**
* Check if the given config strings are set
*
+ * @param string[] $keys
+ * @return bool
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*
- * @param string[] $keys
- * @return bool
*/
- protected function _chkcnf($keys) {
- foreach($keys as $key) {
+ protected function checkConfig($keys)
+ {
+ foreach ($keys as $key) {
$params = explode(':', $key);
$key = array_shift($params);
$sql = trim($this->getConf($key));
// check if sql is set
- if(!$sql) return false;
+ if (!$sql) return false;
// check if needed params are there
- foreach($params as $param) {
- if(strpos($sql, ":$param") === false) return false;
+ foreach ($params as $param) {
+ if (strpos($sql, ":$param") === false) return false;
}
}
@@ -786,20 +804,21 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin {
* @param bool $htmlescape Should the result be escaped for output in HTML?
* @return string
*/
- protected function _debugSQL($sql, $params, $htmlescape = true) {
- foreach($params as $key => $val) {
- if(is_int($val)) {
+ protected function debugSQL($sql, $params, $htmlescape = true)
+ {
+ foreach ($params as $key => $val) {
+ if (is_int($val)) {
$val = $this->pdo->quote($val, PDO::PARAM_INT);
- } elseif(is_bool($val)) {
+ } elseif (is_bool($val)) {
$val = $this->pdo->quote($val, PDO::PARAM_BOOL);
- } elseif(is_null($val)) {
+ } elseif (is_null($val)) {
$val = 'NULL';
} else {
$val = $this->pdo->quote($val);
}
$sql = str_replace($key, $val, $sql);
}
- if($htmlescape) $sql = hsc($sql);
+ if ($htmlescape) $sql = hsc($sql);
return $sql;
}
}