Refactor user class to store user information internally

This commit is contained in:
H.Rouatbi
2024-10-05 20:23:44 +01:00
parent 291604dbd9
commit 552423c700
2 changed files with 74 additions and 51 deletions

View File

@@ -29,7 +29,9 @@ if (! $username)
{
is_array($plugin_run_result = Plugins::getInstance()->run('user_not_admin_admin_page', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
redirect(PATH . 'ucp.php?go=login&return=' . urlencode(ADMIN_PATH . '?cp=' . $go_to));
}else if(!user_can('enter_acp')){
}
elseif(! user_can('enter_acp'))
{
$usrcp->logout_cp();
redirect($config['siteurl']);
}
@@ -42,7 +44,7 @@ get_lang('acp');
//
if (
(empty($_SESSION['ADMINLOGIN']) || $_SESSION['ADMINLOGIN'] != md5(sha1($config['h_key']) . $usrcp->name() . $config['siteurl'])) ||
(empty($_SESSION['USER_SESS']) || $_SESSION['USER_SESS'] != KJ_SESSION) ||
(empty($_SESSION['USER_SESS']) || $_SESSION['USER_SESS'] != KJ_SESSION) ||
(empty($_SESSION['ADMINLOGIN_T']) || $_SESSION['ADMINLOGIN_T'] < time())
) {
if (ig('go') && g('go') == 'login')
@@ -192,9 +194,9 @@ else
}
(! defined('LAST_VISIT')) ? define('LAST_VISIT', time() - 3600 * 12) : null;
//last visit
$last_visit = defined('LAST_VISIT') && preg_match('/[0-9]{10}/', LAST_VISIT) ? kleeja_date(LAST_VISIT) : false;
$last_visit = $usrcp->last_visit();
$last_visit = $last_visit && preg_match('/[0-9]{10}/', $last_visit) ? kleeja_date($last_visit) : false;
//
//exceptional
@@ -295,41 +297,41 @@ $adm_extensions_menu = $adm_topmenu = [];
//sort the items as alphabetic !
sort($adm_extensions);
$i = 0;
$cr_time = LAST_VISIT > 0 ? LAST_VISIT : time() - 3600*12;
$cr_time = $usrcp->last_visit() > 0 ? $usrcp->last_visit() : time() - 3600*12;
// check calls and reports numbers
if (ig('check_msgs') || ! ig('_ajax_')):
//small bubble system
//any item can show what is inside it as unread messages
$kbubbles = [];
//small bubble system
//any item can show what is inside it as unread messages
$kbubbles = [];
//for calls and reports
foreach (['call'=>'calls', 'reports'=>'reports'] as $table=>$n)
{
$query = [
'SELECT' => 'COUNT(' . $table[0] . '.id) AS total_rows',
'FROM' => "`{$dbprefix}" . $table . '` ' . $table[0]
];
//for calls and reports
foreach (['call'=>'calls', 'reports'=>'reports'] as $table=>$n)
{
$query = [
'SELECT' => 'COUNT(' . $table[0] . '.id) AS total_rows',
'FROM' => "`{$dbprefix}" . $table . '` ' . $table[0]
];
$fetched = $SQL->fetch_array($SQL->build($query));
$fetched = $SQL->fetch_array($SQL->build($query));
$kbubbles[$n] = $fetched['total_rows'];
$kbubbles[$n] = $fetched['total_rows'];
$SQL->freeresult();
}
$SQL->freeresult();
}
//if ajax, echo differntly
if (ig('check_msgs'))
{
$SQL->close();
//if ajax, echo differntly
if (ig('check_msgs'))
{
$SQL->close();
exit($kbubbles['calls'] . '::' . $kbubbles['reports']);
}
exit($kbubbles['calls'] . '::' . $kbubbles['reports']);
}
//add your own bubbles here
is_array($plugin_run_result = Plugins::getInstance()->run('kbubbles_admin_page', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
is_array($plugin_run_result = Plugins::getInstance()->run('kbubbles_admin_page', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
endif;
@@ -458,9 +460,10 @@ else
{
$is_ajax = 'yes';
echo_ajax(1,
empty($adminAjaxContent) ? $tpl->display($stylee, $styleePath) : $adminAjaxContent,
$go_menu_html
echo_ajax(
1,
empty($adminAjaxContent) ? $tpl->display($stylee, $styleePath) : $adminAjaxContent,
$go_menu_html
);
}

View File

@@ -16,6 +16,12 @@ if (! defined('IN_COMMON')) {
class usrcp
{
private $user_id = -1;
private $group_id = 2;
private $user_name = null;
private $user_mail = null;
private $last_visit = null;
public function data($name, $pass, $hashed = false, $expire = 86400, $loginadm = false)
{
//expire
@@ -57,8 +63,8 @@ class usrcp
global $SQL, $dbprefix, $config, $userinfo;
$userinfo = [
'id' => -1,
'group_id' => 2,
'id' => $this->user_id,
'group_id' => $this->group_id,
];
$query = [
@@ -117,6 +123,12 @@ class usrcp
//all user fileds info
$userinfo = $row;
$this->user_id = $row['id'];
$this->group_id = $row['group_id'];
$this->user_name = $row['name'];
$this->user_mail = $row['mail'];
$this->last_visit = $row['last_visit'];
$user_y = base64_encode(serialize(['id'=>$row['id'], 'name'=>$row['name'], 'mail'=>$row['mail'], 'last_visit'=>$row['last_visit']]));
if (! $hashed && ! $loginadm) {
@@ -126,9 +138,10 @@ class usrcp
//if last visit > 1 minute then update it
if (empty($row['last_visit']) || time() - $row['last_visit'] > 60) {
$this->last_visit = time();
$update_last_visit = [
'UPDATE' => "{$dbprefix}users",
'SET' => 'last_visit=' . time(),
'SET' => 'last_visit=' . $this->last_visit,
'WHERE' => 'id=' . intval($row['id'])
];
@@ -142,9 +155,6 @@ class usrcp
unset($pass);
return true;
} else {
//guest
define('USER_ID', $userinfo['id']);
define('GROUP_ID', $userinfo['group_id']);
return false;
}
}
@@ -182,7 +192,7 @@ class usrcp
{
is_array($plugin_run_result = Plugins::getInstance()->run('id_func_usr_class', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
return defined('USER_ID') ? USER_ID : false;
return $this->user_id;
}
// group ids
@@ -190,7 +200,7 @@ class usrcp
{
is_array($plugin_run_result = Plugins::getInstance()->run('group_id_func_usr_class', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
return defined('GROUP_ID') ? GROUP_ID : false;
return $this->group_id;
}
// user name
@@ -198,7 +208,7 @@ class usrcp
{
is_array($plugin_run_result = Plugins::getInstance()->run('name_func_usr_class', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
return defined('USER_NAME') ? USER_NAME : false;
return $this->user_name;
}
// user mail
@@ -206,7 +216,15 @@ class usrcp
{
is_array($plugin_run_result = Plugins::getInstance()->run('mail_func_usr_class', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
return defined('USER_MAIL') ? USER_MAIL : false;
return $this->user_mail;
}
// last visit
public function last_visit()
{
is_array($plugin_run_result = Plugins::getInstance()->run('last_visit_func_usr_class', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
return $this->last_visit;
}
// logout func
@@ -219,6 +237,12 @@ class usrcp
$this->logout_cp();
}
$this->user_id = -1;
$this->group_id = 2;
$this->user_name = null;
$this->user_mail = null;
$this->last_visit = null;
//is ther any cookies
$this->kleeja_set_cookie('ulogu', '', time() - 31536000);//31536000 = year
@@ -376,8 +400,8 @@ class usrcp
//to make sure
$userinfo = [
'id' => -1,
'group_id' => 2,
'id' => $this->user_id,
'group_id' => $this->group_id,
];
//if login up
@@ -399,19 +423,15 @@ class usrcp
if ($user_data == false) {
$this->logout();
} else {
define('USER_ID', $userinfo['id']);
define('GROUP_ID', $userinfo['group_id']);
define('USER_NAME', $userinfo['name']);
define('USER_MAIL', $userinfo['mail']);
define('LAST_VISIT', $userinfo['last_visit']);
$this->user_id = $userinfo['id'];
$this->group_id = $userinfo['group_id'];
$this->user_name = $userinfo['name'];
$this->user_mail = $userinfo['mail'];
$this->last_visit = $userinfo['last_visit'];
return $user_data;
}
} else {
//guest
define('USER_ID', $userinfo['id']);
define('GROUP_ID', $userinfo['group_id']);
}
return false; //nothing
}
}
}