Hi.
The new version of the simple authentication class:
<?php
class Authentication extends getTables
{
var $__DB;
var $__listRights;
function Authentication(&$db)
{
(is_object($db))?$this->__DB = &$db:exit('Could not connect');
define('COOKIES_TIME',108000);
parent::getTables();
$this->__listRights = $this->_getListRights();
}
function startAuth($username,$password,$failedMsg,$setCookie=FALSE)
{
$this->logOut();
settype($setCookie,"bool");
$password = md5($password);
$query = "SELECT {$this->__tableName['user']['user_id']},{$this->__tableName['user']['last_login']}
FROM {$this->__tableName['user']['table']}
WHERE {$this->__tableName['user']['username']} = {$this->__DB->safeQuery($username)}
AND {$this->__tableName['user']['password']} = {$this->__DB->safeQuery($password)} AND {$this->__tableName['user']['confirm']} = '1'";
$result = $this->__DB->performQuery($query);
if($result->getNumRows() !== 0)
{
$row = $result->fetchObject();
$userId = $row->user_id;
$lastLogin = $row->user_last_login;
$loginTime = time();
$uid = $this->_generateUid($loginTime);
if($setCookie !== FALSE)
{
$this->_setAuthed($userId,$uid,$lastLogin,$loginTime,TRUE);
}
else
{
$this->_setAuthed($userId,$uid,$lastLogin,$loginTime,FALSE);
}
}
else
{
/* I use session for failed login because of I use redirect */
$_SESSION['failed'] = $failedMsg;
}
}
function _setAuthed($userId,$uid,$lastLogin,$loginTime,$setCookie=FALSE)
{
$query = "UPDATE {$this->__tableName['user']['table']}
SET {$this->__tableName['user']['last_login']}='".$loginTime."',
{$this->__tableName['user']['cookie_uid']}='".$uid."',
{$this->__tableName['user']['is_online']}='1'
WHERE {$this->__tableName['user']['user_id']}='".$userId."'";
$this->__DB->performQuery($query);
if($setCookie !== FALSE)
{
setcookie("authenticationUserId","",time()-COOKIES_TIME);
setcookie("authenticationUid","",time()-COOKIES_TIME);
setcookie("lastLogin","",time()-COOKIES_TIME);
unset($_COOKIE['authenticationUserId']);
unset($_COOKIE['authenticationUid']);
unset($_COOKIE['lastLogin']);
setcookie("authenticationUserId",$userId,time()+COOKIES_TIME);
setcookie("authenticationUid",$uid,time()+COOKIES_TIME);
setcookie("lastLogin",$lastLogin,time()+COOKIES_TIME);
}
unset($_SESSION['authenticationUserId']);
unset($_SESSION['authenticationUid']);
unset($_SESSION['lastLogin']);
$_SESSION['authenticationUserId']=$userId;
$_SESSION['authenticationUid']=$uid;
$_SESSION['lastLogin']= $lastLogin;
}
function checkAuth($right)
{
settype($right,"int");
$check = FALSE;
if(!isset($_SESSION['authenticationUserId']) || !isset($_SESSION['authenticationUid']))
{
(isset($_COOKIE['authenticationUserId']))?$_SESSION['authenticationUserId']=$_COOKIE['authenticationUserId']:$_SESSION['authenticationUserId']="";
(isset($_COOKIE['authenticationUid']))?$_SESSION['authenticationUid']=$_COOKIE['authenticationUid']:$_SESSION['authenticationUid']="";
(isset($_COOKIE['lastLogin']))?$_SESSION['lastLogin']=$_COOKIE['lastLogin']:$_SESSION['lastLogin']="";
}
$query = "SELECT {$this->__tableName['user']['right_id_sum']}
FROM {$this->__tableName['user']['table']}
WHERE
{$this->__tableName['user']['user_id']} = '".$_SESSION['authenticationUserId']."'
AND {$this->__tableName['user']['cookie_uid']} = '".$_SESSION['authenticationUid']."'";
$result = $this->__DB->performQuery($query);
if($result->getNumRows() !== 0)
{
$row = $result->fetchObject();
$userRightSum = $row->user_right_id_sum;
settype($userRightSum,"int");
$check = (bool)( $right & $userRightSum );
}
return $check;
}
function GetDetails()
{
$query = "SELECT {$this->__tableName['user']['table']}.{$this->__tableName['user']['username']},
{$this->__tableName['user']['table']}.{$this->__tableName['user']['last_post']},
{$this->__tableName['user']['table']}.{$this->__tableName['user']['num_posts']},
{$this->__tableName['privilege']['table']}.{$this->__tableName['privilege']['priv_type']}
FROM {$this->__tableName['user']['table']},{$this->__tableName['privilege']['table']}
WHERE {$this->__tableName['user']['table']}.{$this->__tableName['user']['user_id']} = '".$_SESSION['authenticationUserId']."'
AND {$this->__tableName['privilege']['table']}.{$this->__tableName['privilege']['priv_id']}='".$this->__userPrivId."'";
$result = $this->__DB->performQuery($query);
$row = $result->fetchObject();
return array($row->username,$row->priv_type,$_SESSION['lastLogin'],$row->last_post,$row->num_posts);
}
function logOut()
{
$query = "UPDATE {$this->__tableName['user']['table']}
SET {$this->__tableName['user']['is_online']}='0'
WHERE {$this->__tableName['user']['user_id']} = '".$_SESSION['authenticationUserId']."'";
$this->__DB->performQuery($query);
unset($_SESSION['authenticationUserId']);
unset($_SESSION['authenticationUid']);
unset($_SESSION['lastLogin']);
/*For good user who failed login ( ie typing a wrong password )*/
unset($_SESSION['failed']);
setcookie("authenticationUserId",'',time()-COOKIES_TIME);
setcookie("authenticationUid",'',time()-COOKIES_TIME);
setcookie("lastLogin","",time()-COOKIES_TIME);
unset($_COOKIE['authenticationUserId']);
unset($_COOKIE['authenticationUid']);
unset($_COOKIE['lastLogin']);
}
function _generateUid($time)
{
$id = md5($time.mt_rand(substr($time,-4),substr($time,-10)));
return $id;
}
function _getAllValuesRights()
{
$allValuesRights = array();
$query = "SELECT * FROM {$this->__tableName['rights']['table']}";
$result = $this->__DB->performQuery($query);
while($row = $result->fetchRowNum())
{
$allValuesRights[] = $row;
}
return $allValuesRights;
}
function _getListRights()
{
$listRights = array();
$allValuesRights = $this->_getAllValuesRights();
foreach($allValuesRights as $values)
{
$listRights[$values[1]] = $values[0];
}
return $listRights;
}
}//END
?>
and the snippet in abstractDb:
function safeQuery($value)
{
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
if (!is_numeric($value))
{
$value = "'".mysql_real_escape_string($value)."'";
}
return $value;
}
You can check ie with:
$authentication->checkAuth($authentication->__listRights['registered_guest'])
I'm waiting for comments 🙂
TKs again jdorsch.
Take care.
Bye.