2020-09-22 06:04:33 +00:00
|
|
|
<?php
|
|
|
|
class UserHelper {
|
|
|
|
|
2021-02-11 07:22:27 +00:00
|
|
|
static function authenticate(string $login = null, string $password = null, bool $check_only = false, string $service = null) {
|
2021-02-22 18:47:48 +00:00
|
|
|
if (!Config::get(Config::SINGLE_USER_MODE)) {
|
2020-09-22 06:04:33 +00:00
|
|
|
$user_id = false;
|
|
|
|
$auth_module = false;
|
|
|
|
|
2021-02-08 16:11:31 +00:00
|
|
|
PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_AUTH_USER,
|
|
|
|
function ($result, $plugin) use (&$user_id, &$auth_module) {
|
|
|
|
if ($result) {
|
|
|
|
$user_id = (int)$result;
|
|
|
|
$auth_module = strtolower(get_class($plugin));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$login, $password, $service);
|
2020-09-22 06:04:33 +00:00
|
|
|
|
|
|
|
if ($user_id && !$check_only) {
|
|
|
|
|
2021-02-25 06:33:36 +00:00
|
|
|
if (session_status() != PHP_SESSION_ACTIVE)
|
|
|
|
session_start();
|
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
session_regenerate_id(true);
|
|
|
|
|
|
|
|
$_SESSION["uid"] = $user_id;
|
|
|
|
$_SESSION["auth_module"] = $auth_module;
|
|
|
|
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
$sth = $pdo->prepare("SELECT login,access_level,pwd_hash FROM ttrss_users
|
|
|
|
WHERE id = ?");
|
|
|
|
$sth->execute([$user_id]);
|
|
|
|
$row = $sth->fetch();
|
|
|
|
|
|
|
|
$_SESSION["name"] = $row["login"];
|
|
|
|
$_SESSION["access_level"] = $row["access_level"];
|
|
|
|
$_SESSION["csrf_token"] = bin2hex(get_random_bytes(16));
|
|
|
|
|
|
|
|
$usth = $pdo->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
|
|
|
|
$usth->execute([$user_id]);
|
|
|
|
|
2021-01-05 07:17:24 +00:00
|
|
|
$_SESSION["ip_address"] = UserHelper::get_user_ip();
|
2020-09-22 06:04:33 +00:00
|
|
|
$_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']);
|
|
|
|
$_SESSION["pwd_hash"] = $row["pwd_hash"];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-25 12:39:46 +00:00
|
|
|
if (!$user_id)
|
2021-02-25 12:49:30 +00:00
|
|
|
Logger::log(E_USER_WARNING, "Failed login attempt for $login (service: $service) from " . UserHelper::get_user_ip());
|
2021-02-25 12:39:46 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$_SESSION["uid"] = 1;
|
|
|
|
$_SESSION["name"] = "admin";
|
|
|
|
$_SESSION["access_level"] = 10;
|
|
|
|
|
|
|
|
$_SESSION["hide_hello"] = true;
|
|
|
|
$_SESSION["hide_logout"] = true;
|
|
|
|
|
|
|
|
$_SESSION["auth_module"] = false;
|
|
|
|
|
|
|
|
if (!$_SESSION["csrf_token"])
|
|
|
|
$_SESSION["csrf_token"] = bin2hex(get_random_bytes(16));
|
|
|
|
|
2021-01-05 07:17:24 +00:00
|
|
|
$_SESSION["ip_address"] = UserHelper::get_user_ip();
|
2020-09-22 06:04:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 07:22:27 +00:00
|
|
|
static function load_user_plugins(int $owner_uid, PluginHost $pluginhost = null) {
|
2020-09-22 06:04:33 +00:00
|
|
|
|
|
|
|
if (!$pluginhost) $pluginhost = PluginHost::getInstance();
|
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if ($owner_uid && SCHEMA_VERSION >= 100 && empty($_SESSION["safe_mode"])) {
|
2021-02-25 11:49:58 +00:00
|
|
|
$plugins = get_pref(Prefs::_ENABLED_PLUGINS, $owner_uid);
|
2020-09-22 06:04:33 +00:00
|
|
|
|
2021-02-12 14:20:37 +00:00
|
|
|
$pluginhost->load((string)$plugins, PluginHost::KIND_USER, $owner_uid);
|
2020-09-22 06:04:33 +00:00
|
|
|
|
2021-01-15 05:32:06 +00:00
|
|
|
/*if (get_schema_version() > 100) {
|
2020-09-22 06:04:33 +00:00
|
|
|
$pluginhost->load_data();
|
2021-01-15 05:32:06 +00:00
|
|
|
}*/
|
2020-09-22 06:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function login_sequence() {
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
2021-02-22 18:47:48 +00:00
|
|
|
if (Config::get(Config::SINGLE_USER_MODE)) {
|
2021-02-25 12:28:27 +00:00
|
|
|
if (session_status() != PHP_SESSION_ACTIVE)
|
|
|
|
session_start();
|
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
self::authenticate("admin", null);
|
2020-09-22 06:04:33 +00:00
|
|
|
startup_gettext();
|
2020-09-22 11:54:15 +00:00
|
|
|
self::load_user_plugins($_SESSION["uid"]);
|
2020-09-22 06:04:33 +00:00
|
|
|
} else {
|
2021-02-25 15:21:48 +00:00
|
|
|
if (!\Sessions\validate_session())
|
|
|
|
$_SESSION["uid"] = null;
|
2020-09-22 06:04:33 +00:00
|
|
|
|
2021-02-05 21:12:15 +00:00
|
|
|
if (empty($_SESSION["uid"])) {
|
2020-09-22 06:04:33 +00:00
|
|
|
|
2021-02-22 18:47:48 +00:00
|
|
|
if (Config::get(Config::AUTH_AUTO_LOGIN) && self::authenticate(null, null)) {
|
2021-02-25 12:49:30 +00:00
|
|
|
$_SESSION["ref_schema_version"] = get_schema_version();
|
2020-09-22 06:04:33 +00:00
|
|
|
} else {
|
2020-09-22 11:54:15 +00:00
|
|
|
self::authenticate(null, null, true);
|
2020-09-22 06:04:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 21:12:15 +00:00
|
|
|
if (empty($_SESSION["uid"])) {
|
2021-02-14 12:31:03 +00:00
|
|
|
UserHelper::logout();
|
2020-09-22 06:04:33 +00:00
|
|
|
|
2021-02-19 17:21:36 +00:00
|
|
|
Handler_Public::_render_login_form();
|
2020-09-22 06:04:33 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* bump login timestamp */
|
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
|
|
|
|
$sth->execute([$_SESSION['uid']]);
|
|
|
|
|
|
|
|
$_SESSION["last_login_update"] = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SESSION["uid"]) {
|
|
|
|
startup_gettext();
|
2020-09-22 11:54:15 +00:00
|
|
|
self::load_user_plugins($_SESSION["uid"]);
|
2020-09-22 06:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function print_user_stylesheet() {
|
2021-02-25 11:49:58 +00:00
|
|
|
$value = get_pref(Prefs::USER_STYLESHEET);
|
2020-09-22 06:04:33 +00:00
|
|
|
|
|
|
|
if ($value) {
|
|
|
|
print "<style type='text/css' id='user_css_style'>";
|
|
|
|
print str_replace("<br/>", "\n", $value);
|
|
|
|
print "</style>";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:17:24 +00:00
|
|
|
static function get_user_ip() {
|
2021-01-05 07:25:43 +00:00
|
|
|
foreach (["HTTP_X_REAL_IP", "REMOTE_ADDR"] as $hdr) {
|
2021-01-05 07:17:24 +00:00
|
|
|
if (isset($_SERVER[$hdr]))
|
|
|
|
return $_SERVER[$hdr];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 07:22:27 +00:00
|
|
|
static function find_user_by_login(string $login) {
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT id FROM ttrss_users WHERE
|
|
|
|
LOWER(login) = LOWER(?)");
|
|
|
|
$sth->execute([$login]);
|
|
|
|
|
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
return $row["id"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-14 12:31:03 +00:00
|
|
|
|
|
|
|
static function logout() {
|
|
|
|
if (session_status() === PHP_SESSION_ACTIVE)
|
|
|
|
session_destroy();
|
|
|
|
|
|
|
|
if (isset($_COOKIE[session_name()])) {
|
|
|
|
setcookie(session_name(), '', time()-42000, '/');
|
|
|
|
|
|
|
|
}
|
|
|
|
session_commit();
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:59:54 +00:00
|
|
|
static function reset_password($uid, $format_output = false) {
|
|
|
|
|
|
|
|
$pdo = Db::pdo();
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("SELECT login FROM ttrss_users WHERE id = ?");
|
|
|
|
$sth->execute([$uid]);
|
|
|
|
|
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
|
|
|
|
$login = $row["login"];
|
|
|
|
|
|
|
|
$new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
|
|
|
|
$tmp_user_pwd = make_password();
|
|
|
|
|
|
|
|
$pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
|
|
|
|
|
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_users
|
|
|
|
SET pwd_hash = ?, salt = ?, otp_enabled = false
|
|
|
|
WHERE id = ?");
|
|
|
|
$sth->execute([$pwd_hash, $new_salt, $uid]);
|
|
|
|
|
|
|
|
$message = T_sprintf("Changed password of user %s to %s", "<strong>$login</strong>", "<strong>$tmp_user_pwd</strong>");
|
|
|
|
|
|
|
|
if ($format_output)
|
|
|
|
print_notice($message);
|
|
|
|
else
|
|
|
|
print $message;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 06:04:33 +00:00
|
|
|
}
|