tt-rss/classes/pref/users.php

461 lines
14 KiB
PHP
Raw Normal View History

2011-12-13 11:00:36 +01:00
<?php
class Pref_Users extends Handler_Protected {
function before($method) {
if (parent::before($method)) {
2011-12-13 11:00:36 +01:00
if ($_SESSION["access_level"] < 10) {
print __("Your access level is insufficient to open this tab.");
return false;
}
return true;
}
return false;
}
2011-12-26 09:02:52 +01:00
function csrf_ignore($method) {
2013-06-11 10:11:02 +02:00
$csrf_ignored = array("index", "edit", "userdetails");
2011-12-26 09:02:52 +01:00
return array_search($method, $csrf_ignored) !== false;
}
2011-12-13 11:00:36 +01:00
function edit() {
global $access_level_names;
2017-12-11 16:41:32 +01:00
print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
2015-08-12 19:19:38 +02:00
print '<div dojoType="dijit.layout.TabContainer" style="height : 400px">
<div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">';
2017-12-11 16:41:32 +01:00
//print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
2011-12-13 11:00:36 +01:00
$id = (int) clean($_REQUEST["id"]);
2015-08-12 19:19:38 +02:00
print_hidden("id", "$id");
print_hidden("op", "pref-users");
print_hidden("method", "editSave");
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT * FROM ttrss_users WHERE id = ?");
$sth->execute([$id]);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if ($row = $sth->fetch()) {
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$login = $row["login"];
$access_level = $row["access_level"];
$email = $row["email"];
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : "";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<div class=\"dlgSec\">".__("User")."</div>";
print "<div class=\"dlgSecCont\">";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if ($sel_disabled) {
print_hidden("login", "$login");
}
2013-06-11 10:04:29 +02:00
2017-12-02 10:23:41 +01:00
print "<input size=\"30\" style=\"font-size : 16px\"
dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
$sel_disabled
name=\"login\" value=\"$login\">";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "</div>";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<div class=\"dlgSec\">".__("Authentication")."</div>";
print "<div class=\"dlgSecCont\">";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print __('Access level: ') . " ";
if (!$sel_disabled) {
print_select_hash("access_level", $access_level, $access_level_names,
"dojoType=\"dijit.form.Select\" $sel_disabled");
} else {
print_select_hash("", $access_level, $access_level_names,
"dojoType=\"dijit.form.Select\" $sel_disabled");
print_hidden("access_level", "$access_level");
}
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<hr/>";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
2011-12-13 11:00:36 +01:00
name=\"password\">";
2017-12-02 10:23:41 +01:00
print "</div>";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<div class=\"dlgSec\">".__("Options")."</div>";
print "<div class=\"dlgSecCont\">";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
2011-12-13 11:00:36 +01:00
value=\"$email\">";
2017-12-02 10:23:41 +01:00
print "</div>";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
print "</table>";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
}
2011-12-13 11:00:36 +01:00
2015-08-12 19:19:38 +02:00
print '</div>'; #tab
print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
print '</div>';
print '</div>';
2011-12-13 11:00:36 +01:00
print "<div class=\"dlgButtons\">
2017-12-11 16:41:32 +01:00
<button dojoType=\"dijit.form.Button\" class=\"btn-primary\" type=\"submit\" onclick=\"dijit.byId('userEditDlg').execute()\">".
2015-08-12 19:19:38 +02:00
__('Save')."</button>
2013-06-11 10:04:29 +02:00
<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
2015-08-12 19:19:38 +02:00
__('Cancel')."</button></div>";
2011-12-13 11:00:36 +01:00
2017-12-11 16:41:32 +01:00
print "</form>";
2011-12-13 11:00:36 +01:00
return;
}
2015-08-12 19:19:38 +02:00
function userdetails() {
$id = (int) clean($_REQUEST["id"]);
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT login,
2015-08-12 19:19:38 +02: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 10:23:41 +01:00
WHERE id = ?");
$sth->execute([$id]);
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
if ($row = $sth->fetch()) {
print "<table width='100%'>";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$last_login = make_local_datetime(
$row["last_login"], true);
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$created = make_local_datetime(
$row["created"], true);
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$stored_articles = $row["stored_articles"];
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
WHERE owner_uid = ?");
$sth->execute([$id]);
$row = $sth->fetch();
$num_feeds = $row["num_feeds"];
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "</table>";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "<h1>".__('Subscribed feeds')."</h1>";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT id,title,site_url FROM ttrss_feeds
WHERE owner_uid = ? ORDER BY title");
$sth->execute([$id]);
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "<ul class=\"userFeedList\">";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
while ($line = $sth->fetch()) {
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
$icon_file = ICONS_URL."/".$line["id"].".ico";
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
if (file_exists($icon_file) && filesize($icon_file) > 0) {
$feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
} else {
$feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
}
2015-08-12 19:19:38 +02:00
2017-12-02 10:23:41 +01:00
print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
2015-08-12 19:19:38 +02:00
}
2017-12-02 10:23:41 +01:00
print "</ul>";
} else {
print "<h1>".__('User not found')."</h1>";
2015-08-12 19:19:38 +02:00
}
2017-12-02 10:23:41 +01:00
2015-08-12 19:19:38 +02:00
}
2011-12-13 11:00:36 +01:00
function editSave() {
$login = trim(clean($_REQUEST["login"]));
$uid = clean($_REQUEST["id"]);
$access_level = (int) clean($_REQUEST["access_level"]);
$email = trim(clean($_REQUEST["email"]));
$password = clean($_REQUEST["password"]);
2011-12-13 11:00:36 +01:00
if ($password) {
$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
$pwd_hash = encrypt_password($password, $salt, true);
2017-12-02 10:23:41 +01:00
$pass_query_part = "pwd_hash = ".$this->pdo->quote($pwd_hash).",
salt = ".$this->pdo->quote($salt).",";
2011-12-13 11:00:36 +01:00
} else {
$pass_query_part = "";
}
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("UPDATE ttrss_users SET $pass_query_part login = ?,
access_level = ?, email = ?, otp_enabled = false WHERE id = ?");
$sth->execute([$login, $access_level, $email, $uid]);
2011-12-13 11:00:36 +01:00
}
function remove() {
$ids = explode(",", clean($_REQUEST["ids"]));
2011-12-13 11:00:36 +01:00
foreach ($ids as $id) {
if ($id != $_SESSION["uid"] && $id != 1) {
2017-12-02 10:23:41 +01: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 11:00:36 +01:00
}
}
}
function add() {
$login = trim(clean($_REQUEST["login"]));
2011-12-13 11:00:36 +01:00
$tmp_user_pwd = make_password(8);
$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
$pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
login = ?");
$sth->execute([$login]);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if (!$sth->fetch()) {
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("INSERT INTO ttrss_users
(login,pwd_hash,access_level,last_login,created, salt)
2017-12-02 10:23:41 +01:00
VALUES (?, ?, 0, null, NOW(), ?)");
$sth->execute([$login, $pwd_hash, $salt]);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
login = ? AND pwd_hash = ?");
$sth->execute([$login, $pwd_hash]);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if ($row = $sth->fetch()) {
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$new_uid = $row['id'];
2011-12-13 11:00:36 +01:00
print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
$login, $tmp_user_pwd));
initialize_user($new_uid);
2011-12-13 11:00:36 +01:00
} else {
print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
}
} else {
print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
}
}
static function resetUserPassword($uid, $show_password) {
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$pdo = Db::pdo();
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $pdo->prepare("SELECT login, email
FROM ttrss_users WHERE id = ?");
$sth->execute([$uid]);
if ($row = $sth->fetch()) {
2017-12-02 10:23:41 +01:00
$login = $row["login"];
$email = $row["email"];
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
$tmp_user_pwd = make_password(8);
2017-12-02 10:23:41 +01:00
$pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $pdo->prepare("UPDATE ttrss_users
SET pwd_hash = ?, salt = ?, otp_enabled = false
WHERE id = ?");
$sth->execute([$pwd_hash, $new_salt, $uid]);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if ($show_password) {
print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
} else {
print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
}
require_once 'classes/ttrssmailer.php';
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if ($email) {
require_once "lib/MiniTemplator.class.php";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$tpl = new MiniTemplator;
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$tpl->readTemplateFromFile("templates/resetpass_template.txt");
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$tpl->setVariable('LOGIN', $login);
$tpl->setVariable('NEWPASS', $tmp_user_pwd);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$tpl->addBlock('message');
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$message = "";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$tpl->generateOutputToString($message);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$mail = new ttrssMailer();
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$rc = $mail->quickMail($email, $login,
__("[tt-rss] Password change notification"),
$message, false);
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
if (!$rc) print_error($mail->ErrorInfo);
}
2011-12-13 11:00:36 +01:00
}
2013-03-22 11:50:02 +01:00
}
2011-12-13 11:00:36 +01:00
2013-03-22 11:50:02 +01:00
function resetPass() {
$uid = clean($_REQUEST["id"]);
Pref_Users::resetUserPassword($uid, true);
2011-12-13 11:00:36 +01:00
}
function index() {
global $access_level_names;
print "<div id=\"pref-user-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
print "<div id=\"pref-user-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">";
$user_search = trim(clean($_REQUEST["search"]));
2011-12-13 11:00:36 +01:00
if (array_key_exists("search", $_REQUEST)) {
$_SESSION["prefs_user_search"] = $user_search;
} else {
$user_search = $_SESSION["prefs_user_search"];
}
print "<div style='float : right; padding-right : 4px;'>
<input dojoType=\"dijit.form.TextBox\" id=\"user_search\" size=\"20\" type=\"search\"
value=\"$user_search\">
<button dojoType=\"dijit.form.Button\" onclick=\"updateUsersList()\">".
2011-12-13 11:00:36 +01:00
__('Search')."</button>
</div>";
$sort = clean($_REQUEST["sort"]);
2011-12-13 11:00:36 +01:00
if (!$sort || $sort == "undefined") {
$sort = "login";
}
print "<div dojoType=\"dijit.form.DropDownButton\">".
"<span>" . __('Select')."</span>";
print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
print "<div onclick=\"selectTableRows('prefUserList', 'all')\"
dojoType=\"dijit.MenuItem\">".__('All')."</div>";
print "<div onclick=\"selectTableRows('prefUserList', 'none')\"
dojoType=\"dijit.MenuItem\">".__('None')."</div>";
print "</div></div>";
print "<button dojoType=\"dijit.form.Button\" onclick=\"addUser()\">".__('Create user')."</button>";
2011-12-13 11:00:36 +01:00
print "
<button dojoType=\"dijit.form.Button\" onclick=\"editSelectedUser()\">".
2011-12-13 11:00:36 +01:00
__('Edit')."</button dojoType=\"dijit.form.Button\">
<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedUsers()\">".
2011-12-13 11:00:36 +01:00
__('Remove')."</button dojoType=\"dijit.form.Button\">
<button dojoType=\"dijit.form.Button\" onclick=\"resetSelectedUserPass()\">".
2011-12-13 11:00:36 +01:00
__('Reset password')."</button dojoType=\"dijit.form.Button\">";
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
"hook_prefs_tab_section", "prefUsersToolbar");
2011-12-13 11:00:36 +01:00
print "</div>"; #toolbar
print "</div>"; #pane
print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
2017-12-03 11:54:03 +01:00
$sort = validate_field($sort,
["login", "access_level", "created", "num_feeds", "created", "last_login"], "login");
if ($sort != "login") $sort = "$sort DESC";
2011-12-13 11:00:36 +01:00
2017-12-02 10:23:41 +01:00
$sth = $this->pdo->prepare("SELECT
tu.id,
login,access_level,email,
2011-12-13 11:00:36 +01:00
".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
2011-12-13 11:00:36 +01:00
FROM
ttrss_users tu
2011-12-13 11:00:36 +01:00
WHERE
2017-12-02 10:23:41 +01:00
(:search = '' OR login LIKE :search) AND tu.id > 0
2017-12-03 11:54:03 +01:00
ORDER BY $sort");
$sth->execute([":search" => $user_search ? "%$user_search%" : ""]);
2011-12-13 11:00:36 +01:00
print "<p><table width=\"100%\" cellspacing=\"0\"
class=\"prefUserList\" id=\"prefUserList\">";
print "<tr class=\"title\">
<td align='center' width=\"5%\">&nbsp;</td>
<td width='20%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
<td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
<td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Subscribed feeds')."</a></td>
2011-12-13 11:00:36 +01:00
<td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td>
<td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>";
$lnum = 0;
2017-12-02 10:23:41 +01:00
while ($line = $sth->fetch()) {
2011-12-13 11:00:36 +01:00
$uid = $line["id"];
2012-12-25 15:20:33 +01:00
print "<tr id=\"UMRR-$uid\">";
2011-12-13 11:00:36 +01:00
$line["login"] = htmlspecialchars($line["login"]);
$line["created"] = make_local_datetime($line["created"], false);
$line["last_login"] = make_local_datetime($line["last_login"], false);
2011-12-13 11:00:36 +01:00
2012-12-25 15:20:33 +01:00
print "<td align='center'><input onclick='toggleSelectRow2(this);'
dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
id=\"UMCHK-$uid\"></td>";
2011-12-13 11:00:36 +01:00
$onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
2013-07-10 11:09:12 +02:00
print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
2011-12-13 11:00:36 +01:00
if (!$line["email"]) $line["email"] = "&nbsp;";
print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
print "<td $onclick>" . $line["num_feeds"] . "</td>";
2011-12-13 11:00:36 +01:00
print "<td $onclick>" . $line["created"] . "</td>";
print "<td $onclick>" . $line["last_login"] . "</td>";
print "</tr>";
++$lnum;
}
print "</table>";
2017-12-02 10:23:41 +01:00
if ($lnum == 0) {
2011-12-13 11:00:36 +01:00
if (!$user_search) {
print_warning(__('No users defined.'));
} else {
print_warning(__('No matching users found.'));
}
}
print "</div>"; #pane
2012-12-23 13:15:34 +01:00
2013-04-18 10:27:34 +02:00
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
2012-12-23 13:15:34 +01:00
"hook_prefs_tab", "prefUsers");
2011-12-13 11:00:36 +01:00
print "</div>"; #container
}
2017-04-26 19:24:18 +02:00
}