ttrss/classes/pref/users.php

305 lines
8.9 KiB
PHP
Raw Normal View History

2011-12-13 10:00:36 +00:00
<?php
2021-02-14 12:50:46 +00:00
class Pref_Users extends Handler_Administrative {
2011-12-26 08:02:52 +00:00
function csrf_ignore($method) {
2021-02-14 13:44:41 +00:00
$csrf_ignored = array("index");
2011-12-26 08:02:52 +00:00
return array_search($method, $csrf_ignored) !== false;
}
2011-12-13 10:00:36 +00:00
function edit() {
global $access_level_names;
2021-02-14 13:44:41 +00:00
$id = (int)clean($_REQUEST["id"]);
2017-12-11 15:41:32 +00:00
2021-02-14 13:44:41 +00:00
$sth = $this->pdo->prepare("SELECT id, login, access_level, email FROM ttrss_users WHERE id = ?");
2017-12-02 09:23:41 +00:00
$sth->execute([$id]);
2011-12-13 10:00:36 +00:00
2021-02-14 13:44:41 +00:00
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
print json_encode([
"user" => $row,
"access_level_names" => $access_level_names
]);
2017-12-02 09:23:41 +00:00
}
2011-12-13 10:00:36 +00:00
}
2015-08-12 17:19:38 +00:00
function userdetails() {
$id = (int) clean($_REQUEST["id"]);
2015-08-12 17:19:38 +00:00
2017-12-02 09:23:41 +00:00
$sth = $this->pdo->prepare("SELECT login,
2015-08-12 17:19:38 +00:00
".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
access_level,
(SELECT COUNT(int_id) FROM ttrss_user_entries
WHERE owner_uid = id) AS stored_articles,
".SUBSTRING_FOR_DATE."(created,1,16) AS created
FROM ttrss_users
2017-12-02 09:23:41 +00:00
WHERE id = ?");
$sth->execute([$id]);
2015-08-12 17:19:38 +00:00
2017-12-02 09:23:41 +00:00
if ($row = $sth->fetch()) {
2015-08-12 17:19:38 +00:00
$last_login = TimeHelper::make_local_datetime(
2017-12-02 09:23:41 +00:00
$row["last_login"], true);
2015-08-12 17:19:38 +00:00
$created = TimeHelper::make_local_datetime(
2017-12-02 09:23:41 +00:00
$row["created"], true);
2015-08-12 17:19:38 +00:00
2017-12-02 09:23:41 +00:00
$stored_articles = $row["stored_articles"];
2015-08-12 17:19:38 +00:00
2017-12-02 09:23:41 +00:00
$sth = $this->pdo->prepare("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
WHERE owner_uid = ?");
$sth->execute([$id]);
$row = $sth->fetch();
2015-08-12 17:19:38 +00:00
2021-02-18 10:00:20 +00:00
$num_feeds = $row["num_feeds"];
2018-11-03 12:08:43 +00:00
2021-02-18 10:00:20 +00:00
?>
<fieldset>
<label><?= __('Registered') ?>:</label>
<?= $created ?>
</fieldset>
<fieldset>
<label><?= __('Last logged in') ?>:</label>
<?= $last_login ?>
</fieldset>
<fieldset>
<label><?= __('Subscribed feeds') ?>:</label>
<?= $num_feeds ?>
</fieldset>
<fieldset>
<label><?= __('Stored articles') ?>:</label>
<?= $stored_articles ?>
</fieldset>
<?php
$sth = $this->pdo->prepare("SELECT id,title,site_url FROM ttrss_feeds
WHERE owner_uid = ? ORDER BY title");
$sth->execute([$id]);
?>
<ul class="panel panel-scrollable list list-unstyled">
<?php while ($row = $sth->fetch()) { ?>
<li>
<?php
2021-02-22 19:35:27 +00:00
$icon_file = Config::get(Config::ICONS_URL) . "/" . $row["id"] . ".ico";
2021-02-18 10:00:20 +00:00
$icon = file_exists($icon_file) ? $icon_file : "images/blank_icon.gif";
?>
<img class="icon" src="<?= $icon_file ?>">
<a target="_blank" href="<?= htmlspecialchars($row["site_url"]) ?>">
<?= htmlspecialchars($row["title"]) ?>
</a>
</li>
<?php } ?>
</ul>
<?php
2018-11-03 12:08:43 +00:00
2017-12-02 09:23:41 +00:00
} else {
2021-02-18 10:00:20 +00:00
print_error(__('User not found'));
2015-08-12 17:19:38 +00:00
}
2018-11-03 12:08:43 +00:00
2015-08-12 17:19:38 +00:00
}
2011-12-13 10:00:36 +00:00
function editSave() {
$login = clean($_REQUEST["login"]);
$uid = clean($_REQUEST["id"]);
$access_level = (int) clean($_REQUEST["access_level"]);
$email = clean($_REQUEST["email"]);
$password = clean($_REQUEST["password"]);
2011-12-13 10:00:36 +00:00
2021-02-14 13:44:41 +00:00
// no blank usernames
if (!$login) return;
// forbid renaming admin
if ($uid == 1) $login = "admin";
2011-12-13 10:00:36 +00:00
if ($password) {
$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
$pwd_hash = encrypt_password($password, $salt, true);
2018-11-03 12:08:43 +00:00
$pass_query_part = "pwd_hash = ".$this->pdo->quote($pwd_hash).",
2017-12-02 09:23:41 +00:00
salt = ".$this->pdo->quote($salt).",";
2011-12-13 10:00:36 +00:00
} else {
$pass_query_part = "";
}
$sth = $this->pdo->prepare("UPDATE ttrss_users SET $pass_query_part login = LOWER(?),
2017-12-02 09:23:41 +00:00
access_level = ?, email = ?, otp_enabled = false WHERE id = ?");
$sth->execute([$login, $access_level, $email, $uid]);
2011-12-13 10:00:36 +00:00
}
function remove() {
$ids = explode(",", clean($_REQUEST["ids"]));
2011-12-13 10:00:36 +00:00
foreach ($ids as $id) {
if ($id != $_SESSION["uid"] && $id != 1) {
2017-12-02 09:23:41 +00:00
$sth = $this->pdo->prepare("DELETE FROM ttrss_tags WHERE owner_uid = ?");
$sth->execute([$id]);
$sth = $this->pdo->prepare("DELETE FROM ttrss_feeds WHERE owner_uid = ?");
$sth->execute([$id]);
$sth = $this->pdo->prepare("DELETE FROM ttrss_users WHERE id = ?");
$sth->execute([$id]);
2011-12-13 10:00:36 +00:00
}
}
}
function add() {
$login = clean($_REQUEST["login"]);
$tmp_user_pwd = make_password();
$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
$pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
2011-12-13 10:00:36 +00:00
if (!$login) return; // no blank usernames
if (!UserHelper::find_user_by_login($login)) {
2011-12-13 10:00:36 +00:00
2017-12-02 09:23:41 +00:00
$sth = $this->pdo->prepare("INSERT INTO ttrss_users
(login,pwd_hash,access_level,last_login,created, salt)
VALUES (LOWER(?), ?, 0, null, NOW(), ?)");
2017-12-02 09:23:41 +00:00
$sth->execute([$login, $pwd_hash, $salt]);
2011-12-13 10:00:36 +00:00
if ($new_uid = UserHelper::find_user_by_login($login)) {
2011-12-13 10:00:36 +00:00
print T_sprintf("Added user %s with password %s",
$login, $tmp_user_pwd);
2011-12-13 10:00:36 +00:00
} else {
print T_sprintf("Could not create user %s", $login);
2011-12-13 10:00:36 +00:00
}
} else {
print T_sprintf("User %s already exists.", $login);
2011-12-13 10:00:36 +00:00
}
}
2013-03-22 10:50:02 +00:00
function resetPass() {
2021-02-15 13:59:54 +00:00
UserHelper::reset_password(clean($_REQUEST["id"]));
2011-12-13 10:00:36 +00:00
}
function index() {
global $access_level_names;
$user_search = clean($_REQUEST["search"] ?? "");
2011-12-13 10:00:36 +00:00
if (array_key_exists("search", $_REQUEST)) {
$_SESSION["prefs_user_search"] = $user_search;
} else {
$user_search = ($_SESSION["prefs_user_search"] ?? "");
2011-12-13 10:00:36 +00:00
}
$sort = clean($_REQUEST["sort"] ?? "");
2011-12-13 10:00:36 +00:00
if (!$sort || $sort == "undefined") {
$sort = "login";
}
2021-02-15 13:07:22 +00:00
$sort = $this->_validate_field($sort,
2017-12-03 10:54:03 +00:00
["login", "access_level", "created", "num_feeds", "created", "last_login"], "login");
if ($sort != "login") $sort = "$sort DESC";
2011-12-13 10:00:36 +00:00
2021-02-14 11:59:22 +00:00
?>
<div dojoType='dijit.layout.BorderContainer' gutters='false'>
<div style='padding : 0px' dojoType='dijit.layout.ContentPane' region='top'>
<div dojoType='fox.Toolbar'>
<div style='float : right'>
<input dojoType='dijit.form.TextBox' id='user_search' size='20' type='search'
value="<?= htmlspecialchars($user_search) ?>">
<button dojoType='dijit.form.Button' onclick='Users.reload()'>
<?= __('Search') ?>
</button>
</div>
<div dojoType='fox.form.DropDownButton'>
<span><?= __('Select') ?></span>
<div dojoType='dijit.Menu' style='display: none'>
<div onclick="Tables.select('users-list', true)"
dojoType='dijit.MenuItem'><?= __('All') ?></div>
<div onclick="Tables.select('users-list', false)"
dojoType='dijit.MenuItem'><?= __('None') ?></div>
</div>
</div>
<button dojoType='dijit.form.Button' onclick='Users.add()'>
<?= __('Create user') ?>
</button>
<button dojoType='dijit.form.Button' onclick='Users.removeSelected()'>
<?= __('Remove') ?>
</button>
<button dojoType='dijit.form.Button' onclick='Users.resetSelected()'>
<?= __('Reset password') ?>
</button>
<?php PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION, "prefUsersToolbar") ?>
</div>
</div>
<div style='padding : 0px' dojoType='dijit.layout.ContentPane' region='center'>
<table width='100%' class='users-list' id='users-list'>
<tr class='title'>
<td align='center' width='5%'> </td>
<td width='20%'><a href='#' onclick="Users.reload('login')"><?= ('Login') ?></a></td>
<td width='20%'><a href='#' onclick="Users.reload('access_level')"><?= ('Access Level') ?></a></td>
<td width='10%'><a href='#' onclick="Users.reload('num_feeds')"><?= ('Subscribed feeds') ?></a></td>
<td width='20%'><a href='#' onclick="Users.reload('created')"><?= ('Registered') ?></a></td>
<td width='20%'><a href='#' onclick="Users.reload('last_login')"><?= ('Last login') ?></a></td>
</tr>
<?php
$sth = $this->pdo->prepare("SELECT
tu.id,
login,access_level,email,
".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login,
".SUBSTRING_FOR_DATE."(created,1,16) as created,
(SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds
FROM
ttrss_users tu
WHERE
(:search = '' OR login LIKE :search) AND tu.id > 0
ORDER BY $sort");
$sth->execute([":search" => $user_search ? "%$user_search%" : ""]);
while ($row = $sth->fetch()) { ?>
<tr data-row-id='<?= $row["id"] ?>' onclick='Users.edit(<?= $row["id"] ?>)' title="<?= __('Click to edit') ?>">
<td align='center'>
<input onclick='Tables.onRowChecked(this); event.stopPropagation();'
dojoType='dijit.form.CheckBox' type='checkbox'>
</td>
<td><i class='material-icons'>person</i> <?= htmlspecialchars($row["login"]) ?></td>
<td><?= $access_level_names[$row["access_level"]] ?></td>
<td><?= $row["num_feeds"] ?></td>
<td><?= TimeHelper::make_local_datetime($row["created"], false) ?></td>
<td><?= TimeHelper::make_local_datetime($row["last_login"], false) ?></td>
</tr>
<?php } ?>
</table>
</div>
<?php PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB, "prefUsers") ?>
</div>
<?php
}
2021-02-15 13:07:22 +00:00
private function _validate_field($string, $allowed, $default = "") {
if (in_array($string, $allowed))
return $string;
else
return $default;
}
}