* @since Version 2.0 */ class sysUsersController extends cCore { public $sDir; public $sName; public $oModule; public $oCfg; public $suser; // User session variable class public $iUserId = false; // User session saved id const USER_SESSION_KEY = 'cms-user'; CONST USER_2FA_SESSION_KEY = 'cms-user-2fa'; CONST USER_PWD_SESSION_KEY = 'cms-user-pwd'; /** * Class constructor * * @author Mareks Keišs * @category sysUsers Controller * @param bool|array $vUriData - uri data array * @param bool|string $sDir - Module file path * @param bool|string $sName - Module name * @param bool $bLoadData - load user permission data */ public function __construct($vUriData = false, $sDir = false, $sName = false, $bLoadData = false) { if($vUriData){ $this->sDir = $sDir; $this->sName = $sName; parent :: __construct($vUriData); $this->oView->setTemplateDir($this->sDir . "/tmpl/"); if(!$this->oView->cacheUsed()){ $this->oView->setJs(getCurrentFolder($this->sDir) . '/js/' . $this->sName . '.js'); $this->oView->setCss(getCurrentFolder($this->sDir) . '/css/' . $this->sName . '.css'); } // Set JS module init title $this->oView->setAsJSON(cCore::JS_MOD, ucfirst($sName)); } $this->oModule = new sysUsersModule(); $this->suser = new cSession(self::USER_SESSION_KEY); $this->iUserId = $this->suser->id; // Load user config $this->oCfg = new sysUsersCfg($this->suser->id); // Load additional user data if($bLoadData && $this->suser->id){ // Online user counter check new cUsersOnline(true, true); // Set permissions $this->suser->permissions = $this->oModule->loadUserPermissionArray($this->suser->id); // Load permissions } } /** * SYSTEM LOGIN MODULE * * @author Mareks Keišs * @category sysUsers Controller * @returns bool true / false */ public function isLoggedIn(): bool { return (bool)$this->getUser(); } /** * Function to check user for logout * * @author Mareks Keišs * @category sysUsers Controller * @return bool - true / false */ public function chkLogOut(){ if(getG('logout')) return $this->logOut(); return false; } public function chkLogoutStatus(){ if(getG('logout')) return $this->logOut(); return false; } /** * Function to Log user out * * @author Mareks Keišs * @category sysUsers Controller * @param bool $kill - kill user logout session * @return bool - true / false */ public function logOut($kill = true){ // Update user last location URL $sLocationUrl = str_replace("?{$_SERVER['QUERY_STRING']}", '', $_SERVER['REQUEST_URI']); if(strlen($sLocationUrl)) $this->oModule->updateUserDataById($this->iUserId, array('user_last_location_url' => $sLocationUrl)); // Destroy user data leaving session intact $this->suser->destroy(); unset($_SESSION[cCoreSystem::sessionCmsUlrId], $_SESSION[self::USER_SESSION_KEY]); return true; } /** * Function to get active in user id * * @author Mareks Keišs * @category sysUsers Controller * @return int - user id or 0 */ public function getUser(): int { if ($this->suser->id) { return $this->suser->id; } return 0; } /** * Function to get user language * * @author Mareks Keišs * @category sysUsers Controller * @return bool - true / false */ public function getUserLanguage(){ if($this->suser->lang_id){ return $this->suser->lang_id; } return false; } /** * Function to set user language * * @author Mareks Keišs * @category sysUsers Controller * @param $lang_id - language id * @return bool - true / false */ public function setUserLanguage($lang_id){ if($this->isLoggedIn()){ if($this->suser->lang_id != $lang_id){ $this->suser->lang_id = $lang_id; } return true; } return false; } /** * Function to check user permission rights * * @author Mareks Keišs * @category sysUsers Controller * @param string $p_name - permission name - sid * @param bool $p_id - permission id - pid * @param bool $dieErr - indicates whether to die on error * @return bool - true / false */ public function chkPermission($p_name, $p_id = false, $dieErr = true){ $dError = 'No access rights!'; if(($p_name || $p_id) && $this->suser->id && is_array($this->suser->permissions)){ if($p_name){ if(in_array($p_name, $this->suser->permissions)){ return true; } }else{ if(array_key_exists($p_id, $this->suser->permissions)){ return true; } } } if($dieErr){ die($dError); }else{ return false; } } }