1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
<?php
/**
* PgSQL authentication backend
*
* This class inherits much functionality from the MySQL class
* and just reimplements the Postgres specific parts.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
* @author Chris Smith <chris@jalakai.co.uk>
* @author Matthias Grimm <matthias.grimmm@sourceforge.net>
*/
define('DOKU_AUTH', dirname(__FILE__));
require_once(DOKU_AUTH.'/mysql.class.php');
class auth_pgsql extends auth_mysql {
/**
* Constructor
*
* checks if the pgsql interface is available, otherwise it will
* set the variable $success of the basis class to FALSE
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
* @author Andreas Gohr <andi@splitbrain.org>
*/
function auth_pgsql() {
global $conf;
$this->cnf = $conf['auth']['pgsql'];
if(!$this->cnf['port']) $this->cnf['port'] = 5432;
if (method_exists($this, 'auth_basic'))
parent::auth_basic();
if(!function_exists('pg_connect')) {
if ($this->cnf['debug'])
msg("PgSQL err: PHP Postgres extension not found.",-1);
$this->success = false;
return;
}
$this->defaultgroup = $conf['defaultgroup'];
// set capabilities based upon config strings set
if (empty($this->cnf['server']) || empty($this->cnf['user']) ||
empty($this->cnf['password']) || empty($this->cnf['database'])){
if ($this->cnf['debug'])
msg("PgSQL err: insufficient configuration.",-1,__LINE__,__FILE__);
$this->success = false;
return;
}
$this->cando['addUser'] = $this->_chkcnf(array('getUserInfo',
'getGroups',
'addUser',
'getUserID',
'getGroupID',
'addGroup',
'addUserGroup'));
$this->cando['delUser'] = $this->_chkcnf(array('getUserID',
'delUser',
'delUserRefs'));
$this->cando['modLogin'] = $this->_chkcnf(array('getUserID',
'updateUser',
'UpdateTarget'));
$this->cando['modPass'] = $this->cando['modLogin'];
$this->cando['modName'] = $this->cando['modLogin'];
$this->cando['modMail'] = $this->cando['modLogin'];
$this->cando['modGroups'] = $this->_chkcnf(array('getUserID',
'getGroups',
'getGroupID',
'addGroup',
'addUserGroup',
'delGroup',
'getGroupID',
'delUserGroup'));
/* getGroups is not yet supported
$this->cando['getGroups'] = $this->_chkcnf(array('getGroups',
'getGroupID')); */
$this->cando['getUsers'] = $this->_chkcnf(array('getUsers',
'getUserInfo',
'getGroups'));
$this->cando['getUserCount'] = $this->_chkcnf(array('getUsers'));
}
/**
* Check if the given config strings are set
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
* @return bool
*/
function _chkcnf($keys, $wop=false){
foreach ($keys as $key){
if (empty($this->cnf[$key])) return false;
}
return true;
}
// @inherit function checkPass($user,$pass)
// @inherit function getUserData($user)
// @inherit function createUser($user,$pwd,$name,$mail,$grps=null)
// @inherit function modifyUser($user, $changes)
// @inherit function deleteUsers($users)
/**
* [public function]
*
* Counts users which meet certain $filter criteria.
*
* @param array $filter filter criteria in item/pattern pairs
* @return count of found users.
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function getUserCount($filter=array()) {
$rc = 0;
if($this->_openDB()) {
$sql = $this->_createSQLFilter($this->cnf['getUsers'], $filter);
// no equivalent of SQL_CALC_FOUND_ROWS in pgsql?
if (($result = $this->_queryDB($sql))){
$rc = count($result);
}
$this->_closeDB();
}
return $rc;
}
/**
* Bulk retrieval of user data. [public function]
*
* @param first index of first user to be returned
* @param limit max number of users to be returned
* @param filter array of field/pattern pairs
* @return array of userinfo (refer getUserData for internal userinfo details)
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function retrieveUsers($first=0,$limit=10,$filter=array()) {
$out = array();
if($this->_openDB()) {
$this->_lockTables("READ");
$sql = $this->_createSQLFilter($this->cnf['getUsers'], $filter);
$sql .= " ".$this->cnf['SortOrder']." LIMIT $limit OFFSET $first";
$result = $this->_queryDB($sql);
foreach ($result as $user)
if (($info = $this->_getUserInfo($user['user'])))
$out[$user['user']] = $info;
$this->_unlockTables();
$this->_closeDB();
}
return $out;
}
// @inherit function joinGroup($user, $group)
// @inherit function leaveGroup($user, $group) {
/**
* Adds a user to a group.
*
* If $force is set to '1' non existing groups would be created.
*
* The database connection must already be established. Otherwise
* this function does nothing and returns 'false'.
*
* @param $uid user id to add to a group
* @param $group name of the group
* @param $force '1' create missing groups
* @return bool 'true' on success, 'false' on error
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
* @author Andreas Gohr <andi@splitbrain.org>
*/
function _addUserToGroup($uid, $group, $force=0) {
$newgroup = 0;
if (($this->dbcon) && ($uid)) {
$gid = $this->_getGroupID($group);
if (!$gid) {
if ($force) { // create missing groups
$sql = str_replace('%{group}',addslashes($group),$this->cnf['addGroup']);
$this->_modifyDB($sql);
//group should now exists try again to fetch it
$gid = $this->_getGroupID($group);
$newgroup = 1; // group newly created
}
}
if (!$gid) return false; // group didn't exist and can't be created
$sql = str_replace('%{uid}', addslashes($uid),$this->cnf['addUserGroup']);
$sql = str_replace('%{user}', addslashes($user),$sql);
$sql = str_replace('%{gid}', addslashes($gid),$sql);
$sql = str_replace('%{group}',addslashes($group),$sql);
if ($this->_modifyDB($sql) !== false) return true;
if ($newgroup) { // remove previously created group on error
$sql = str_replace('%{gid}', addslashes($gid),$this->cnf['delGroup']);
$sql = str_replace('%{group}',addslashes($group),$sql);
$this->_modifyDB($sql);
}
}
return false;
}
// @inherit function _delUserFromGroup($uid, $group)
// @inherit function _getGroups($user)
// @inherit function _getUserID($user)
/**
* Adds a new User to the database.
*
* The database connection must already be established
* for this function to work. Otherwise it will return
* 'false'.
*
* @param $user login of the user
* @param $pwd encrypted password
* @param $name full name of the user
* @param $mail email address
* @param $grps array of groups the user should become member of
* @return bool
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Chris Smith <chris@jalakai.co.uk>
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _addUser($user,$pwd,$name,$mail,$grps){
if($this->dbcon && is_array($grps)) {
$sql = str_replace('%{user}', addslashes($user),$this->cnf['addUser']);
$sql = str_replace('%{pass}', addslashes($pwd),$sql);
$sql = str_replace('%{name}', addslashes($name),$sql);
$sql = str_replace('%{email}',addslashes($mail),$sql);
if($this->_modifyDB($sql)){
$uid = $this->_getUserID($user);
}else{
return false;
}
if ($uid) {
foreach($grps as $group) {
$gid = $this->_addUserToGroup($uid, $group, 1);
if ($gid === false) break;
}
if ($gid) return true;
else {
/* remove the new user and all group relations if a group can't
* be assigned. Newly created groups will remain in the database
* and won't be removed. This might create orphaned groups but
* is not a big issue so we ignore this problem here.
*/
$this->_delUser($user);
if ($this->cnf['debug'])
msg("PgSQL err: Adding user '$user' to group '$group' failed.",-1,__LINE__,__FILE__);
}
}
}
return false;
}
// @inherit function _delUser($user)
// @inherit function _getUserInfo($user)
// @inherit function _updateUserInfo($changes, $uid)
// @inherit function _getGroupID($group)
/**
* Opens a connection to a database and saves the handle for further
* usage in the object. The successful call to this functions is
* essential for most functions in this object.
*
* @return bool
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _openDB() {
if (!$this->dbcon) {
$dsn = 'host='.$this->cnf['server'];
$dsn .= ' port='.$this->cnf['port'];
$dsn .= ' dbname='.$this->cnf['database'];
$dsn .= ' user='.$this->cnf['user'];
$dsn .= ' password='.$this->cnf['password'];
$con = @pg_connect($dsn);
if ($con) {
$this->dbcon = $con;
return true; // connection and database successfully opened
} else if ($this->cnf['debug']){
msg ("PgSQL err: Connection to {$this->cnf['user']}@{$this->cnf['server']} not possible.",
-1,__LINE__,__FILE__);
}
return false; // connection failed
}
return true; // connection already open
}
/**
* Closes a database connection.
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _closeDB() {
if ($this->dbcon) {
pg_close ($this->dbcon);
$this->dbcon = 0;
}
}
/**
* Sends a SQL query to the database and transforms the result into
* an associative array.
*
* This function is only able to handle queries that returns a
* table such as SELECT.
*
* @param $query SQL string that contains the query
* @return array with the result table
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _queryDB($query) {
if ($this->dbcon) {
$result = @pg_query($this->dbcon,$query);
if ($result) {
while (($t = pg_fetch_assoc($result)) !== false)
$resultarray[]=$t;
pg_free_result ($result);
return $resultarray;
}elseif ($this->cnf['debug'])
msg('PgSQL err: '.pg_last_error($this->dbcon),-1,__LINE__,__FILE__);
}
return false;
}
/**
* Executes an update or insert query. This differs from the
* MySQL one because it does NOT return the last insertID
*
* @author Andreas Gohr
*/
function _modifyDB($query) {
if ($this->dbcon) {
$result = @pg_query($this->dbcon,$query);
if ($result) {
pg_free_result ($result);
return true;
}
if ($this->cnf['debug']){
msg('PgSQL err: '.pg_last_error($this->dbcon),-1,__LINE__,__FILE__);
}
}
return false;
}
/**
* Start a transaction
*
* @param $mode could be 'READ' or 'WRITE'
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _lockTables($mode) {
if ($this->dbcon) {
$this->_modifyDB('BEGIN');
return true;
}
return false;
}
/**
* Commit a transaction
*
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function _unlockTables() {
if ($this->dbcon) {
$this->_modifyDB('COMMIT');
return true;
}
return false;
}
// @inherit function _createSQLFilter($sql, $filter)
/**
* Escape a string for insertion into the database
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string $string The string to escape
* @param boolean $like Escape wildcard chars as well?
*/
function _escape($string,$like=false){
$string = pg_escape_string($string);
if($like){
$string = addcslashes($string,'%_');
}
return $string;
}
}
//Setup VIM: ex: et ts=2 enc=utf-8 :
|