| version 1.7, 2002/07/31 15:34:14 |
version 1.8, 2002/09/05 19:22:00 |
|
|
| /* |
/* |
| * phpPlatonLib - Platon software development group PHP library |
* phpPlatonLib - Platon software development group PHP library |
| * |
* |
| * Auth/AuthDB.php - powerful authentification module |
* Auth/AuthDB.php - powerful authentification interface |
| * (under development, but it seems to work fine) |
|
| * ____________________________________________________________ |
* ____________________________________________________________ |
| * |
* |
| * Developed by Ondrej Jombik <nepto@pobox.sk> |
* Developed by Ondrej Jombik <nepto@pobox.sk> |
|
|
| * 13/5/2002 - headers update |
* 13/5/2002 - headers update |
| * 29/5/2002 - code cleanup, little improvements (username and password |
* 29/5/2002 - code cleanup, little improvements (username and password |
| * moved to props array) |
* moved to props array) |
| |
* 05/9/2002 - expiration & timeout implementation |
| |
* - better HTTP_X_FORWARDED_FOR handling in authentification |
| |
* - extensive code cleanup |
| */ |
*/ |
| |
|
| require_once 'DB.php'; |
require_once 'DB.php'; |
|
|
| var $_status = AUTH_DB_UNDEFINED; |
var $_status = AUTH_DB_UNDEFINED; |
| var $_options = null; |
var $_options = null; |
| var $_db = null; |
var $_db = null; |
| |
var $_props = array( |
| var $_props = array( |
|
| 'username' => null, |
'username' => null, |
| 'password' => null, |
'password' => null, |
| 'session_id' => null, |
'session_id' => null, |
|
|
| $this->_checkDB(); |
$this->_checkDB(); |
| $this->_DBupdateTimeout(); |
$this->_DBupdateTimeout(); |
| |
|
| if ($this->_DBcheckLogin()) |
$status = $this->_DBcheckLogin(); |
| return AUTH_DB_LOGGED_IN; |
if ($status != AUTH_DB_UNDEFINED) { |
| |
if ($status != AUTH_DB_LOGGED_IN) { |
| |
setcookie(AUTH_DB_COOKIE_NAME, '', time() - 100000); |
| |
} |
| |
return $status; |
| |
} |
| |
|
| |
|
| $this->assignData(); |
$this->assignData(); |
|
|
| |
|
| if ($status == AUTH_DB_LOGGED_IN) { |
if ($status == AUTH_DB_LOGGED_IN) { |
| $this->_DBinsert(); |
$this->_DBinsert(); |
| |
setcookie(AUTH_DB_COOKIE_NAME, |
| |
$this->_getProperty('session_id'), |
| |
0, '', '', false); |
| } elseif ($status == AUTH_DB_FAILED) { |
} elseif ($status == AUTH_DB_FAILED) { |
| $this->_DBlog(); |
$this->_DBlog(); |
| } |
} |
|
|
| $this->_DBlogout(); |
$this->_DBlogout(); |
| $this->_status = AUTH_DB_LOGGED_OUT; |
$this->_status = AUTH_DB_LOGGED_OUT; |
| } |
} |
| |
setcookie(AUTH_DB_COOKIE_NAME, '', time() - 100000); |
| } |
} |
| |
|
| function getUsername() |
function getUsername() |
|
|
| { |
{ |
| 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); |
| if (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) |
| { |
{ |
| echo 'AuthDB error: ' |
die('AuthDB error: '.$error->getMessage()); |
| . $error->getMessage() . '<br>' |
// sensitive! |
| . $error->getDebugInfo() . '<br>'; |
// $error->getDebugInfo(); |
| die(); |
|
| } |
} |
| |
|
| function _getProperty($property) |
function _getProperty($property) |
|
|
| |
|
| case 'remote_ip': |
case 'remote_ip': |
| global $HTTP_SERVER_VARS; |
global $HTTP_SERVER_VARS; |
| $ret = (empty($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']) |
/* Sending HTTP_X_FORWARDED_FOR? OK, send anything you want, |
| ? $HTTP_SERVER_VARS['REMOTE_ADDR'] |
but it must persis for whole session. */ |
| : $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']); |
$ret = array(); |
| $ret = substr(stripslashes($ret), 0, 15); |
foreach (array('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR') as $key) { |
| |
if (isset($HTTP_SERVER_VARS[$key])) |
| |
$ret[] = $HTTP_SERVER_VARS[$key]; |
| |
} |
| |
$ret = join(' / ', $ret); |
| break; |
break; |
| |
|
| default: |
default: |
|
|
| |
|
| do { |
do { |
| $session_id = mt_rand(); |
$session_id = mt_rand(); |
| |
|
| $query = sprintf('SELECT session_id FROM %s' |
$query = sprintf('SELECT session_id FROM %s' |
| . ' WHERE session_id = %s', |
. ' WHERE session_id = %s', |
| $this->_options['sessions']['table'], |
$this->_options['sessions']['table'], |
| $this->_db->quote($session_id)); |
$this->_db->quote($session_id)); |
| |
|
| if (DB::isError($res = $this->_db->getOne($query))) |
DB::isError($res = $this->_db->getOne($query)) |
| $this->_error($res); |
&& $this->_error($res); |
| |
|
| } while (isset($res)); |
} while (isset($res)); |
| |
|
| setcookie(AUTH_DB_COOKIE_NAME, $session_id, 0, '', '', false); |
|
| |
|
| $query = sprintf('INSERT INTO %s' |
$query = sprintf('INSERT INTO %s' |
| .' (status, login, logout,' |
.' (status, login, logout,' |
| .' username, session_id, ip, user_agent)' |
.' username, session_id, ip, user_agent)' |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->query($query))) |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| $this->_props['session_id'] = $session_id; |
$this->_props['session_id'] = $session_id; |
| |
|
| return true; |
|
| } |
} |
| |
|
| function _DBlog() |
function _DBlog() |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->query($query))) |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| return true; |
|
| } |
} |
| |
|
| function _DBupdateTimeout() |
function _DBupdateTimeout() |
| { |
{ |
| if (intval($this->_options['timeout']) <= 0) { |
$timeout = intval($this->_options['timeout']); |
| return true; |
$expired = intval($this->_options['expired']); |
| } |
|
| |
|
| $query = sprintf('UPDATE %s' |
|
| .' SET status = "timeout"' |
|
| .' WHERE status = "online"' |
|
| .' AND unix_timestamp(sysdate()) - unix_timestamp(logout) >= %s', |
|
| $this->_options['sessions']['table'], |
|
| $this->_db->quote(intval($this->_options['timeout']))); |
|
| |
|
| if (DB::isError($res = $this->_db->query($query))) |
|
| $this->_error($res); |
|
| |
|
| return true; |
if ($timeout > 0) { |
| |
$query = sprintf('UPDATE %s' |
| |
.' SET status = "timeout"' |
| |
.' WHERE status = "online"' |
| |
.' AND UNIX_TIMESTAMP(SYSDATE())' |
| |
.' - UNIX_TIMESTAMP(logout) >= %s', |
| |
$this->_options['sessions']['table'], |
| |
$this->_db->quote($timeout)); |
| |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| |
} |
| |
if ($expired > 0) { |
| |
$query = sprintf('UPDATE %s' |
| |
.' SET status = "expired"' |
| |
.' WHERE status = "online"' |
| |
.' AND UNIX_TIMESTAMP(SYSDATE())' |
| |
.' - UNIX_TIMESTAMP(login) >= %s', |
| |
$this->_options['sessions']['table'], |
| |
$this->_db->quote($expired)); |
| |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| |
} |
| } |
} |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->query($query))) |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| setcookie(AUTH_DB_COOKIE_NAME); |
|
| |
|
| return true; |
|
| } |
} |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->query($query))) |
DB::isError($res = $this->_db->query($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| return $this->_db->affectedRows() > 0 ? true : false; |
return $this->_db->affectedRows() > 0 ? true : false; |
| } |
} |
| |
|
|
|
| { |
{ |
| if ($this->_DBupdateLogin() == true) |
if ($this->_DBupdateLogin() == true) |
| return true; |
return true; |
| |
$query = sprintf('SELECT status FROM %s' |
| $query = sprintf('SELECT id FROM %s' |
.' WHERE session_id = %s' |
| .' WHERE status = "online"' |
|
| .' AND session_id = %s' |
|
| .' AND ip = %s' |
.' AND ip = %s' |
| .' AND user_agent = %s', |
.' AND user_agent = %s', |
| $this->_options['sessions']['table'], |
$this->_options['sessions']['table'], |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->getCol($query))) |
DB::isError($res = $this->_db->getCol($query)) && $this->_error($res); |
| $this->_error($res); |
$res = array_unique($res); |
| |
|
| return count($res) > 0 ? true : false; |
/* Keep this in priority order. */ |
| |
if (in_array('logout', $res)) return AUTH_DB_LOGGED_OUT; |
| |
if (in_array('failed', $res)) return AUTH_DB_FAILED; |
| |
if (in_array('timeout', $res)) return AUTH_DB_TIMEOUT; |
| |
if (in_array('expired', $res)) return AUTH_DB_EXPIRED; |
| |
if (in_array('online', $res)) return AUTH_DB_LOGGED_IN; |
| |
return AUTH_DB_UNDEFINED; |
| } |
} |
| |
|
| // This method needs ISDN fastfix |
// This method needs ISDN fastfix |
|
|
| $this->_db->quote($this->_getProperty('remote_ip')), |
$this->_db->quote($this->_getProperty('remote_ip')), |
| $this->_db->quote($this->_getProperty('user_agent'))); |
$this->_db->quote($this->_getProperty('user_agent'))); |
| |
|
| if (DB::isError($res = $this->_db->getCol($query))) |
DB::isError($res = $this->_db->getCol($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| return $res[0]; |
return $res[0]; |
| } |
} |
| |
|
|
|
| if (count($select_where)) |
if (count($select_where)) |
| $query .= ' WHERE ' . join(' AND ', $select_where); |
$query .= ' WHERE ' . join(' AND ', $select_where); |
| |
|
| if (DB::isError($res = $this->_db->getRow($query))) |
DB::isError($res = $this->_db->getRow($query)) && $this->_error($res); |
| $this->_error($res); |
|
| |
|
| return $res; |
return $res; |
| } |
} |
| |
|