login([ 'user' => $login, 'password' => $password, 'userData' => true ]); if (!self::$data) { throw new Exception(); } if (self::$data['gui_access'] == GROUP_GUI_ACCESS_DISABLED) { error(_('GUI access disabled.')); throw new Exception(); } $result = (bool) self::$data; if (isset(self::$data['attempt_failed']) && self::$data['attempt_failed']) { CProfile::init(); CProfile::update('web.login.attempt.failed', self::$data['attempt_failed'], PROFILE_TYPE_INT); CProfile::update('web.login.attempt.ip', self::$data['attempt_ip'], PROFILE_TYPE_STR); CProfile::update('web.login.attempt.clock', self::$data['attempt_clock'], PROFILE_TYPE_INT); $result &= CProfile::flush(); } // remove guest session after successful login $result &= DBexecute('DELETE FROM sessions WHERE sessionid='.zbx_dbstr(get_cookie('zbx_sessionid'))); if ($result) { self::setSessionCookie(self::$data['sessionid']); add_audit_ext(AUDIT_ACTION_LOGIN, AUDIT_RESOURCE_USER, self::$data['userid'], '', null, null, null); } return $result; } catch (Exception $e) { self::setDefault(); return false; } } /** * Log-out the current user. */ public static function logout() { self::$data['sessionid'] = self::getSessionCookie(); self::$data = API::User()->logout([]); CSession::destroy(); zbx_unsetcookie('zbx_sessionid'); } public static function checkAuthentication($sessionId) { try { if ($sessionId !== null) { self::$data = API::User()->checkAuthentication([$sessionId]); } if ($sessionId === null || empty(self::$data)) { self::setDefault(); self::$data = API::User()->login([ 'user' => ZBX_GUEST_USER, 'password' => '', 'userData' => true ]); if (empty(self::$data)) { clear_messages(1); throw new Exception(); } $sessionId = self::$data['sessionid']; } if (self::$data['gui_access'] == GROUP_GUI_ACCESS_DISABLED) { throw new Exception(); } if (self::$set_cookie) { self::setSessionCookie($sessionId); } else { self::$set_cookie = true; } return $sessionId; } catch (Exception $e) { self::setDefault(); return false; } } /** * Shorthand method for setting current session ID in cookies. * * @param string $sessionId Session ID string */ public static function setSessionCookie($sessionId) { $autoLogin = self::isGuest() ? false : (bool) self::$data['autologin']; zbx_setcookie('zbx_sessionid', $sessionId, $autoLogin ? strtotime('+1 month') : 0); } /** * Retrieves current session ID from zbx_sessionid cookie. * * @return string */ public static function getSessionCookie() { return get_cookie('zbx_sessionid'); } public static function setDefault() { self::$data = [ 'alias' => ZBX_GUEST_USER, 'userid' => 0, 'lang' => 'en_gb', 'type' => '0', 'debug_mode' => false ]; } /** * Returns the type of the current user. * * @static * * @return int */ public static function getType() { return self::$data['type']; } /** * Returns true if debug mode is enabled. * * @return bool */ public static function getDebugMode() { return (self::$data['debug_mode']); } /** * Returns true if the current user is logged in. * * @return bool */ public static function isLoggedIn() { return (self::$data['userid']); } /** * Returns true if the user is not logged in or logged in as Guest. * * @return bool */ public static function isGuest() { return (self::$data['alias'] == ZBX_GUEST_USER); } }