| version 1.7, 2007/03/19 07:00:54 |
version 1.8, 2007/09/22 06:39:57 |
|
|
| <?php |
<?php |
| |
|
| /* |
/* |
| * phpPlatonLib - Platon Software Development Group PHP library |
* phpPlatonLib - Platon Group PHP library |
| * |
* |
| * Auth/AuthDB.php - powerful authentification interface |
* Auth/AuthDB.php - powerful authentification interface |
| * ____________________________________________________________ |
* ____________________________________________________________ |
| * |
* |
| * Developed by Ondrej Jombik <nepto@platon.sk> |
* Developed by Ondrej Jombik <nepto@platon.sk> |
| * Copyright (c) 2001-2003 Platon SDG, http://platon.sk/ |
* Copyright (c) 2001-2007 Platon Group, http://platon.sk/ |
| * All rights reserved. |
* All rights reserved. |
| * |
* |
| * See README file for more information about this software. |
* See README file for more information about this software. |
|
|
| * http://platon.sk/projects/phpPlatonLib/ |
* http://platon.sk/projects/phpPlatonLib/ |
| */ |
*/ |
| |
|
| /* $Platon: Metafox/lib/Auth/AuthDB.php,v 1.6 2007-03-19 06:49:12 nepto Exp $ */ |
/* $Platon: phpPlatonLib/Auth_AuthDB/AuthDB.php,v 1.16 2007-09-22 06:13:06 nepto Exp $ */ |
| |
|
| /* |
/* |
| * History: |
* History: |
| * |
* |
| * 31/3/2002 - initial release |
* 2002-03-31 - initial release |
| * 13/5/2002 - headers update |
* 2002-05-13 - headers update |
| * 29/5/2002 - code cleanup, little improvements (username and password |
* 2002-05-29 - code cleanup, little improvements |
| * moved to props array) |
* (username and password moved to props array) |
| * 05/9/2002 - expiration & timeout implementation |
* 2002-09-05 - expiration & timeout implementation |
| * - better HTTP_X_FORWARDED_FOR handling in authentification |
* - better HTTP_X_FORWARDED_FOR handling in authentification |
| * - extensive code cleanup |
* - extensive code cleanup |
| * 27/7/2003 - support for authentification via another cooperative |
* 2003-07-27 - support for authentification via another cooperative |
| * authentification interface |
* authentification interface |
| |
* 2007-09-22 - configurable cookie name, path, domain |
| |
* - configurable username/password CGI form variable name |
| */ |
*/ |
| |
|
| require_once 'DB.php'; |
require_once 'DB.php'; |
| |
|
| define('AUTH_DB_COOKIE_NAME', 'AuthDB_session_id_cookie'); |
define('AUTH_DB_COOKIE_NAME', 'AuthDB_session_id_cookie'); |
| |
define('AUTH_DB_VAR_USERNAME', 'username'); |
| |
define('AUTH_DB_VAR_PASSWORD', 'password'); |
| define('AUTH_DB_UNDEFINED', 0); |
define('AUTH_DB_UNDEFINED', 0); |
| define('AUTH_DB_NOT_LOGGED', -1); |
define('AUTH_DB_NOT_LOGGED', -1); |
| define('AUTH_DB_LOGGED_IN', 1); |
define('AUTH_DB_LOGGED_IN', 1); |
|
|
| 'remote_ip' => null |
'remote_ip' => null |
| ); |
); |
| |
|
| function AuthDB($options) |
function AuthDB($options) /* {{{ */ |
| { |
{ |
| $this->_options = $options; |
$this->_options = $options; |
| } |
$this->_checkOptions(); |
| |
} /* }}} */ |
| |
|
| |
function _checkOptions() /* {{{ */ |
| |
{ |
| |
if (! isset($this->_options['var']['username'])) { |
| |
$this->_options['var']['username'] = AUTH_DB_VAR_USERNAME; |
| |
} |
| |
if (! isset($this->_options['var']['password'])) { |
| |
$this->_options['var']['password'] = AUTH_DB_VAR_PASSWORD; |
| |
} |
| |
if (! isset($this->_options['cookie']['name'])) { |
| |
$this->_options['cookie']['name'] = AUTH_DB_COOKIE_NAME; |
| |
} |
| |
} /* }}} */ |
| |
|
| /** |
/** |
| * Tells AuthDB, that particular user is already authentificated |
* Tells AuthDB, that particular user is already authentificated |
|
|
| * username of already logged in user |
* username of already logged in user |
| * @return status |
* @return status |
| */ |
*/ |
| function setLoggedFunction($fncname) |
function setLoggedFunction($fncname) /* {{{ */ |
| { |
{ |
| $this->_logged_fnc = $fncname; |
$this->_logged_fnc = $fncname; |
| } |
} /* }}} */ |
| |
|
| |
|
| function assignData() |
function assignData() /* {{{ */ |
| { |
{ |
| |
$username_var = $this->_options['var']['username']; |
| |
$password_var = $this->_options['var']['password']; |
| |
|
| /* Empty passwords are allowed. Empty usernames not. */ |
/* Empty passwords are allowed. Empty usernames not. */ |
| |
|
| if (isset($_POST['username']) |
if (isset($_POST[$username_var]) && $_POST[$username_var] != '') { |
| && $_POST['username'] != '') { |
$this->_props['username'] = $_POST[$username_var]; |
| $this->_props['username'] = $_POST['username']; |
|
| |
|
| if (isset($_POST['password'])) { |
if (isset($_POST[$password_var])) { |
| $this->_props['password'] = $_POST['password']; |
$this->_props['password'] = $_POST[$password_var]; |
| } |
} |
| } |
} |
| if (isset($_POST['ezin_username']) |
} /* }}} */ |
| && $_POST['ezin_username'] != '') { |
|
| $this->_props['username'] = $_POST['ezin_username']; |
|
| |
|
| if (isset($_POST['ezin_password'])) { |
|
| $this->_props['password'] = $_POST['ezin_password']; |
|
| } |
|
| } |
|
| } |
|
| |
|
| /* PEAR Auth class compatibility */ |
/* PEAR Auth class compatibility */ |
| function getAuth() |
function getAuth() /* {{{ */ |
| { |
{ |
| return $this->getStatus() == AUTH_DB_LOGGED_IN; |
return $this->getStatus() == AUTH_DB_LOGGED_IN; |
| } |
} /* }}} */ |
| |
|
| function getStatus() |
function getStatus() /* {{{ */ |
| { |
{ |
| if ($this->_status != AUTH_DB_UNDEFINED) |
if ($this->_status != AUTH_DB_UNDEFINED) |
| return $this->_status; |
return $this->_status; |
|
|
| $this->_status = $this->_processStatus(); |
$this->_status = $this->_processStatus(); |
| |
|
| return $this->_status; |
return $this->_status; |
| } |
} /* }}} */ |
| |
|
| function _processStatus() |
function _processStatus() /* {{{ */ |
| { |
{ |
| if ($this->_logged_fnc != null && function_exists($this->_logged_fnc)) { |
if ($this->_logged_fnc != null && function_exists($this->_logged_fnc)) { |
| $this->_props['username'] = call_user_func($this->_logged_fnc); |
$this->_props['username'] = call_user_func($this->_logged_fnc); |
|
|
| authentificated via another cooperative authentification interface. |
authentificated via another cooperative authentification interface. |
| In this case only LOGGED_IN and FAILED are returned. |
In this case only LOGGED_IN and FAILED are returned. |
| Other statuses purpously force login function again. */ |
Other statuses purpously force login function again. */ |
| global $ezin_cfg; |
|
| if ($status != AUTH_DB_UNDEFINED && ($this->_logged_fnc == null |
if ($status != AUTH_DB_UNDEFINED && ($this->_logged_fnc == null |
| || $status == AUTH_DB_FAILED |
|| $status == AUTH_DB_FAILED |
| || $status == AUTH_DB_LOGGED_IN)) { |
|| $status == AUTH_DB_LOGGED_IN)) { |
| if ($status != AUTH_DB_LOGGED_IN) { |
if ($status != AUTH_DB_LOGGED_IN) { |
| echo $ezin_cfg['url']; |
setcookie($this->_options['cookie']['name'], |
| setcookie(AUTH_DB_COOKIE_NAME, '', time() - 100000, $ezin_cfg['url']); |
'', /* empty string */ |
| |
time() - 100000, /* time in the past */ |
| |
$this->_options['cookie']['path'], |
| |
$this->_options['cookie']['domain']); |
| } |
} |
| return $status; |
return $status; |
| } |
} |
|
|
| |
|
| if ($status == AUTH_DB_LOGGED_IN) { |
if ($status == AUTH_DB_LOGGED_IN) { |
| $this->_DBinsert(); |
$this->_DBinsert(); |
| setcookie(AUTH_DB_COOKIE_NAME, |
setcookie($this->_options['cookie']['name'], |
| $this->_getProperty('session_id'), 0, $ezin_cfg['url'], '', false); |
$this->_getProperty('session_id'), |
| |
0, /* expires at the end of session */ |
| |
$this->_options['cookie']['path'], |
| |
$this->_options['cookie']['domain']); |
| } elseif ($status == AUTH_DB_FAILED) { |
} elseif ($status == AUTH_DB_FAILED) { |
| $this->_DBlog(); |
$this->_DBlog(); |
| } |
} |
| |
|
| return $status; |
return $status; |
| } |
} /* }}} */ |
| |
|
| function login() |
function login() /* {{{ */ |
| { |
{ |
| $status = AUTH_DB_UNDEFINED; |
$status = AUTH_DB_UNDEFINED; |
| |
|
|
|
| } |
} |
| |
|
| return $status; |
return $status; |
| } |
} /* }}} */ |
| |
|
| function logout() |
function logout() /* {{{ */ |
| { |
{ |
| if ($this->getAuth()) { |
if ($this->getAuth()) { |
| $this->_DBlogout(); |
$this->_DBlogout(); |
| $this->_status = AUTH_DB_LOGGED_OUT; |
$this->_status = AUTH_DB_LOGGED_OUT; |
| } |
} |
| setcookie(AUTH_DB_COOKIE_NAME, '', time() - 100000); |
setcookie($this->_options['cookie']['name'], |
| } |
'', /* empty string */ |
| |
time() - 100000, /* time in the past */ |
| |
$this->_options['cookie']['path'], |
| |
$this->_options['cookie']['domain']); |
| |
} /* }}} */ |
| |
|
| function getUsername() |
function getUsername() /* {{{ */ |
| { |
{ |
| return $this->_DBgetUsername(); |
return $this->_DBgetUsername(); |
| } |
} /* }}} */ |
| |
|
| function getUserInfo() |
function getUserInfo() /* {{{ */ |
| { |
{ |
| if ($this->getAuth()) { |
if ($this->getAuth()) { |
| $this->_props['username'] = $this->getUsername(); |
$this->_props['username'] = $this->getUsername(); |
|
|
| } |
} |
| |
|
| return false; |
return false; |
| } |
} /* }}} */ |
| |
|
| |
|
| // Private methods |
// Private methods |
| |
|
| function _checkDB() |
function _checkDB() /* {{{ */ |
| { |
{ |
| if (! isset($this->_db)) { |
if (! isset($this->_db)) { |
| $this->_db = DB::connect($this->_options['dsn']); |
$this->_db = DB::connect($this->_options['dsn']); |
| DB::isError($this->_db) && $this->_error($this->_db); |
DB::isError($this->_db) && $this->_error($this->_db); |
| $this->_db->setFetchMode(DB_FETCHMODE_ASSOC); |
$this->_db->setFetchMode(DB_FETCHMODE_ASSOC); |
| } |
} |
| } |
} /* }}} */ |
| |
|
| function _error($error) |
function _error($error) /* {{{ */ |
| { |
{ |
| die('AuthDB error: '.$error->getMessage()); |
die('AuthDB error: '.$error->getMessage()); |
| // sensitive! |
// sensitive! |
| // $error->getDebugInfo(); |
// $error->getDebugInfo(); |
| } |
} /* }}} */ |
| |
|
| function _getProperty($property) |
function _getProperty($property) /* {{{ */ |
| { |
{ |
| if (isset($this->_props[$property])) |
if (isset($this->_props[$property])) |
| return $this->_props[$property]; |
return $this->_props[$property]; |
|
|
| |
|
| default: |
default: |
| case 'session_id': |
case 'session_id': |
| if (isset($_COOKIE[AUTH_DB_COOKIE_NAME])) { |
if (isset($_COOKIE[$this->_options['cookie']['name']])) { |
| $ret = $_COOKIE[AUTH_DB_COOKIE_NAME]; |
$ret = $_COOKIE[$this->_options['cookie']['name']]; |
| $ret = intval(stripslashes($ret)); |
$ret = intval(stripslashes($ret)); |
| } else { |
} else { |
| $ret = ''; |
$ret = ''; |
|
|
| |
|
| $this->_props[$property] = $ret; |
$this->_props[$property] = $ret; |
| return $ret; |
return $ret; |
| } |
} /* }}} */ |
| |
|
| function _DBgetPassword() |
function _DBgetPassword() /* {{{ */ |
| { |
{ |
| $query = sprintf('SELECT %s FROM %s WHERE %s = %s', |
$query = sprintf('SELECT %s FROM %s WHERE %s = %s', |
| $this->_options['users']['cols']['password'], |
$this->_options['users']['cols']['password'], |
|
|
| $this->_error($password); |
$this->_error($password); |
| |
|
| return $password; |
return $password; |
| } |
} /* }}} */ |
| |
|
| function _DBinsert() |
function _DBinsert() /* {{{ */ |
| { |
{ |
| mt_srand((double) microtime() * 1000000); |
mt_srand((double) microtime() * 1000000); |
| |
|
|
|
| |
|
| DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| $this->_props['session_id'] = $session_id; |
$this->_props['session_id'] = $session_id; |
| } |
} /* }}} */ |
| |
|
| function _DBlog() |
function _DBlog() /* {{{ */ |
| { |
{ |
| $query = sprintf('INSERT INTO %s' |
$query = sprintf('INSERT INTO %s' |
| .' (status, login, logout,' |
.' (status, login, logout,' |
|
|
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| } |
} /* }}} */ |
| |
|
| function _DBupdateTimeout() |
function _DBupdateTimeout() /* {{{ */ |
| { |
{ |
| $timeout = intval($this->_options['timeout']); |
$timeout = intval($this->_options['timeout']); |
| $expired = intval($this->_options['expired']); |
$expired = intval($this->_options['expired']); |
|
|
| $this->_db->quote($expired)); |
$this->_db->quote($expired)); |
| DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| } |
} |
| } |
} /* }}} */ |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
| function _DBlogout() |
function _DBlogout() /* {{{ */ |
| { |
{ |
| $query = sprintf('UPDATE %s' |
$query = sprintf('UPDATE %s' |
| .' SET status = "logout", logout = sysdate() ' |
.' SET status = "logout", logout = sysdate() ' |
|
|
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| } |
} /* }}} */ |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
| function _DBupdateLogin() |
function _DBupdateLogin() /* {{{ */ |
| { |
{ |
| $query = sprintf('UPDATE %s' |
$query = sprintf('UPDATE %s' |
| .' SET logout = sysdate()' |
.' SET logout = sysdate()' |
|
|
| |
|
| DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| return $this->_db->affectedRows() > 0 ? true : false; |
return $this->_db->affectedRows() > 0 ? true : false; |
| } |
} /* }}} */ |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
| function _DBcheckLogin() |
function _DBcheckLogin() /* {{{ */ |
| { |
{ |
| if ($this->_DBupdateLogin() == true) |
if ($this->_DBupdateLogin() == true) |
| return true; |
return true; |
|
|
| if (in_array('expired', $res)) return AUTH_DB_EXPIRED; |
if (in_array('expired', $res)) return AUTH_DB_EXPIRED; |
| if (in_array('online', $res)) return AUTH_DB_LOGGED_IN; |
if (in_array('online', $res)) return AUTH_DB_LOGGED_IN; |
| return AUTH_DB_UNDEFINED; |
return AUTH_DB_UNDEFINED; |
| } |
} /* }}} */ |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
| function _DBgetUsername() |
function _DBgetUsername() /* {{{ */ |
| { |
{ |
| $query = sprintf('SELECT username FROM %s' |
$query = sprintf('SELECT username FROM %s' |
| .' WHERE status = "online"' |
.' WHERE status = "online"' |
|
|
| |
|
| DB::isError($res = $this->_db->getCol($query)) && $this->_error($res); |
DB::isError($res = $this->_db->getCol($query)) && $this->_error($res); |
| return $res[0]; |
return $res[0]; |
| } |
} /* }}} */ |
| |
|
| function _DBgetUserInfo() |
function _DBgetUserInfo() /* {{{ */ |
| { |
{ |
| $select_cols = array(); |
$select_cols = array(); |
| $select_tables = array(); |
$select_tables = array(); |
|
|
| |
|
| DB::isError($res = $this->_db->getRow($query)) && $this->_error($res); |
DB::isError($res = $this->_db->getRow($query)) && $this->_error($res); |
| return $res; |
return $res; |
| } |
} /* }}} */ |
| |
|
| } |
} |
| |
|
| |
/* Modeline for ViM {{{ |
| |
* vim: set ts=4: |
| |
* vim600: fdm=marker fdl=0 fdc=0: |
| |
* }}} */ |
| |
|
| ?> |
?> |