1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CWebUser {
public static $data = null;
/**
* Flag used to ignore setting authentication cookie performed checkAuthentication.
*/
static $set_cookie = true;
/**
* Flag used to not to extend session lifetime in checkAuthentication.
*/
static $extend_session = true;
/**
* Disable automatic cookie setting.
* First checkAuthentication call (performed in initialization phase) will not be sending cookies.
*/
public static function disableSessionCookie() {
self::$set_cookie = false;
}
/**
* Disable automatic session extension.
*/
public static function disableSessionExtension() {
self::$extend_session = false;
}
/**
* Tries to login a user and populates self::$data on success.
*
* @param string $login user login
* @param string $password user password
*
* @throws Exception if user cannot be logged in
*
* @return bool
*/
public static function login($login, $password) {
try {
self::setDefault();
self::$data = API::User()->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_SESSION_NAME)));
if ($result) {
self::setSessionCookie(self::$data['sessionid']);
}
return $result;
}
catch (Exception $e) {
self::setDefault();
return false;
}
}
/**
* Log-out the current user.
*/
public static function logout() {
self::$data['sessionid'] = self::getSessionCookie();
if (API::User()->logout([])) {
self::$data = null;
CSession::destroy();
zbx_unsetcookie(ZBX_SESSION_NAME);
}
}
public static function checkAuthentication($sessionId) {
try {
if ($sessionId !== null) {
self::$data = API::User()->checkAuthentication([
'sessionid' => $sessionId,
'extend' => self::$extend_session
]);
}
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_SESSION_NAME, $sessionId, $autoLogin ? strtotime('+1 month') : 0);
}
/**
* Retrieves current session ID from cookie named as defined in ZBX_SESSION_NAME.
*
* @return string
*/
public static function getSessionCookie() {
return get_cookie(ZBX_SESSION_NAME);
}
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);
}
/**
* Return true if guest user has access to frontend.
*
* @return bool
*/
public static function isGuestAllowed() {
$guest = DB::select('users', [
'output' => ['userid'],
'filter' => ['alias' => ZBX_GUEST_USER]
]);
return getUserGuiAccess($guest[0]['userid']) != GROUP_GUI_ACCESS_DISABLED;
}
/**
* Returns refresh rate in seconds.
*
* @return int
*/
public static function getRefresh() {
return timeUnitToSeconds(self::$data['refresh']);
}
/**
* Returns interface language attribute value for HTML lang tag.
*
* @return string
*/
public static function getLang() {
return (self::$data) ? substr(self::$data['lang'], 0, strpos(self::$data['lang'], '_')) : 'en';
}
}
|