ttrss/classes/pref/users.php

458 lines
14 KiB
PHP
Raw Normal View History

2011-12-13 10:00:36 +00:00
<?php
class Pref_Users extends Handler_Protected {
function before($method) {
if (parent::before($method)) {
2011-12-13 10:00:36 +00: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 08:02:52 +00:00
function csrf_ignore($method) {
2013-06-11 08:04:29 +00:00
$csrf_ignored = array("index", "edit");
2011-12-26 08:02:52 +00:00
return array_search($method, $csrf_ignored) !== false;
}
2011-12-13 10:00:36 +00:00
function userdetails() {
$uid = sprintf("%d", $_REQUEST["id"]);
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT login,
2011-12-13 10:00:36 +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
WHERE id = '$uid'");
2013-04-17 16:12:14 +00:00
if ($this->dbh->num_rows($result) == 0) {
2011-12-13 10:00:36 +00:00
print "<h1>".__('User not found')."</h1>";
return;
}
// print "<h1>User Details</h1>";
2013-04-17 16:12:14 +00:00
$login = $this->dbh->fetch_result($result, 0, "login");
2011-12-13 10:00:36 +00:00
print "<table width='100%'>";
2013-04-17 12:23:15 +00:00
$last_login = make_local_datetime(
2013-04-17 16:12:14 +00:00
$this->dbh->fetch_result($result, 0, "last_login"), true);
2011-12-13 10:00:36 +00:00
2013-04-17 12:23:15 +00:00
$created = make_local_datetime(
2013-04-17 16:12:14 +00:00
$this->dbh->fetch_result($result, 0, "created"), true);
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$access_level = $this->dbh->fetch_result($result, 0, "access_level");
$stored_articles = $this->dbh->fetch_result($result, 0, "stored_articles");
2011-12-13 10:00:36 +00:00
print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
2011-12-13 10:00:36 +00:00
WHERE owner_uid = '$uid'");
2013-04-17 16:12:14 +00:00
$num_feeds = $this->dbh->fetch_result($result, 0, "num_feeds");
2011-12-13 10:00:36 +00:00
print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
print "</table>";
print "<h1>".__('Subscribed feeds')."</h1>";
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT id,title,site_url FROM ttrss_feeds
2011-12-13 10:00:36 +00:00
WHERE owner_uid = '$uid' ORDER BY title");
print "<ul class=\"userFeedList\">";
2013-04-17 16:12:14 +00:00
while ($line = $this->dbh->fetch_assoc($result)) {
2011-12-13 10:00:36 +00:00
$icon_file = ICONS_URL."/".$line["id"].".ico";
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\">";
}
print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
2011-12-13 10:00:36 +00:00
}
2013-04-17 16:12:14 +00:00
if ($this->dbh->num_rows($result) < $num_feeds) {
2011-12-13 10:00:36 +00:00
// FIXME - add link to show ALL subscribed feeds here somewhere
print "<li><img
class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
}
print "</ul>";
print "<div align='center'>
<button onclick=\"closeInfoBox()\">".__("Close this window").
"</button></div>";
return;
}
function edit() {
global $access_level_names;
2013-04-17 16:12:14 +00:00
$id = $this->dbh->escape_string($_REQUEST["id"]);
2013-06-11 08:04:29 +00:00
print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
2011-12-13 10:00:36 +00:00
2013-06-11 08:04:29 +00:00
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$id\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-users\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT * FROM ttrss_users WHERE id = '$id'");
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$login = $this->dbh->fetch_result($result, 0, "login");
$access_level = $this->dbh->fetch_result($result, 0, "access_level");
$email = $this->dbh->fetch_result($result, 0, "email");
2011-12-13 10:00:36 +00:00
$sel_disabled = ($id == $_SESSION["uid"]) ? "disabled" : "";
print "<div class=\"dlgSec\">".__("User")."</div>";
print "<div class=\"dlgSecCont\">";
if ($sel_disabled) {
2013-06-11 08:04:29 +00:00
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"login\" value=\"$login\">";
2011-12-13 10:00:36 +00:00
}
2013-06-11 08:04:29 +00:00
print "<input size=\"30\" style=\"font-size : 16px\"
dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
onkeypress=\"return filterCR(event, userEditSave)\" $sel_disabled
name=\"login\" value=\"$login\">";
2011-12-13 10:00:36 +00:00
print "</div>";
print "<div class=\"dlgSec\">".__("Authentication")."</div>";
print "<div class=\"dlgSecCont\">";
print __('Access level: ') . " ";
if (!$sel_disabled) {
print_select_hash("access_level", $access_level, $access_level_names,
2013-06-11 08:04:29 +00:00
"dojoType=\"dijit.form.Select\" $sel_disabled");
2011-12-13 10:00:36 +00:00
} else {
print_select_hash("", $access_level, $access_level_names,
2013-06-11 08:04:29 +00:00
"dojoType=\"dijit.form.Select\" $sel_disabled");
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"access_level\" value=\"$access_level\">";
2011-12-13 10:00:36 +00:00
}
2013-06-11 08:04:29 +00:00
print "<hr/>";
2011-12-13 10:00:36 +00:00
2013-06-11 08:04:29 +00:00
print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" onkeypress=\"return filterCR(event, userEditSave)\" placeholder=\"Change password to\"
2011-12-13 10:00:36 +00:00
name=\"password\">";
print "</div>";
print "<div class=\"dlgSec\">".__("Options")."</div>";
print "<div class=\"dlgSecCont\">";
2013-06-11 08:04:29 +00:00
print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" onkeypress=\"return filterCR(event, userEditSave)\" placeholder=\"Email\"
2011-12-13 10:00:36 +00:00
value=\"$email\">";
print "</div>";
print "</table>";
print "</form>";
print "<div class=\"dlgButtons\">
2013-06-11 08:04:29 +00:00
<button dojoType=\"dijit.form.Button\" type=\"submit\">".
2011-12-13 10:00:36 +00:00
__('Save')."</button>
2013-06-11 08:04:29 +00:00
<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
2011-12-13 10:00:36 +00:00
__('Cancel')."</button></div>";
return;
}
function editSave() {
2013-04-17 16:12:14 +00:00
$login = $this->dbh->escape_string(trim($_REQUEST["login"]));
$uid = $this->dbh->escape_string($_REQUEST["id"]);
2011-12-13 10:00:36 +00:00
$access_level = (int) $_REQUEST["access_level"];
2013-04-17 16:12:14 +00:00
$email = $this->dbh->escape_string(trim($_REQUEST["email"]));
$password = $_REQUEST["password"];
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);
$pass_query_part = "pwd_hash = '$pwd_hash', salt = '$salt',";
2011-12-13 10:00:36 +00:00
} else {
$pass_query_part = "";
}
2013-04-17 16:12:14 +00:00
$this->dbh->query("UPDATE ttrss_users SET $pass_query_part login = '$login',
access_level = '$access_level', email = '$email', otp_enabled = false
2012-09-09 08:39:04 +00:00
WHERE id = '$uid'");
2011-12-13 10:00:36 +00:00
}
function remove() {
2013-04-17 16:12:14 +00:00
$ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
2011-12-13 10:00:36 +00:00
foreach ($ids as $id) {
if ($id != $_SESSION["uid"] && $id != 1) {
2013-04-17 16:12:14 +00:00
$this->dbh->query("DELETE FROM ttrss_tags WHERE owner_uid = '$id'");
$this->dbh->query("DELETE FROM ttrss_feeds WHERE owner_uid = '$id'");
$this->dbh->query("DELETE FROM ttrss_users WHERE id = '$id'");
2011-12-13 10:00:36 +00:00
}
}
}
function add() {
2013-04-17 16:12:14 +00:00
$login = $this->dbh->escape_string(trim($_REQUEST["login"]));
2011-12-13 10:00:36 +00: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 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
2011-12-13 10:00:36 +00:00
login = '$login'");
2013-04-17 16:12:14 +00:00
if ($this->dbh->num_rows($result) == 0) {
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$this->dbh->query("INSERT INTO ttrss_users
(login,pwd_hash,access_level,last_login,created, salt)
VALUES ('$login', '$pwd_hash', 0, null, NOW(), '$salt')");
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
2011-12-13 10:00:36 +00:00
login = '$login' AND pwd_hash = '$pwd_hash'");
2013-04-17 16:12:14 +00:00
if ($this->dbh->num_rows($result) == 1) {
2011-12-13 10:00:36 +00:00
2013-04-17 16:12:14 +00:00
$new_uid = $this->dbh->fetch_result($result, 0, "id");
2011-12-13 10:00:36 +00: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 10:00:36 +00: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 10:00:36 +00:00
$result = db_query("SELECT login,email
2011-12-13 10:00:36 +00:00
FROM ttrss_users WHERE id = '$uid'");
$login = db_fetch_result($result, 0, "login");
$email = db_fetch_result($result, 0, "email");
$salt = db_fetch_result($result, 0, "salt");
$new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
2011-12-13 10:00:36 +00:00
$tmp_user_pwd = make_password(8);
$pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
db_query("UPDATE ttrss_users SET pwd_hash = '$pwd_hash', salt = '$new_salt'
2011-12-13 10:00:36 +00:00
WHERE id = '$uid'");
2013-03-22 10:50:02 +00:00
if ($show_password) {
2013-03-24 12:37:40 +00:00
print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
2013-03-22 10:50:02 +00:00
} else {
print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
2013-03-22 10:50:02 +00:00
}
2011-12-13 10:00:36 +00:00
require_once 'classes/ttrssmailer.php';
2011-12-13 10:00:36 +00:00
if ($email) {
require_once "lib/MiniTemplator.class.php";
$tpl = new MiniTemplator;
$tpl->readTemplateFromFile("templates/resetpass_template.txt");
$tpl->setVariable('LOGIN', $login);
$tpl->setVariable('NEWPASS', $tmp_user_pwd);
$tpl->addBlock('message');
$message = "";
$tpl->generateOutputToString($message);
$mail = new ttrssMailer();
2011-12-13 10:00:36 +00:00
$rc = $mail->quickMail($email, $login,
__("[tt-rss] Password change notification"),
$message, false);
2011-12-13 10:00:36 +00:00
if (!$rc) print_error($mail->ErrorInfo);
}
2013-03-22 10:50:02 +00:00
}
2011-12-13 10:00:36 +00:00
2013-03-22 10:50:02 +00:00
function resetPass() {
2013-04-17 16:12:14 +00:00
$uid = $this->dbh->escape_string($_REQUEST["id"]);
Pref_Users::resetUserPassword($uid, true);
2011-12-13 10:00:36 +00: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\">";
2013-04-17 16:12:14 +00:00
$user_search = $this->dbh->escape_string($_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"];
}
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=\"javascript:updateUsersList()\">".
__('Search')."</button>
</div>";
2013-04-17 16:12:14 +00:00
$sort = $this->dbh->escape_string($_REQUEST["sort"]);
2011-12-13 10:00:36 +00: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=\"javascript:addUser()\">".__('Create user')."</button>";
print "
<button dojoType=\"dijit.form.Button\" onclick=\"javascript:selectedUserDetails()\">".
__('Details')."</button dojoType=\"dijit.form.Button\">
<button dojoType=\"dijit.form.Button\" onclick=\"javascript:editSelectedUser()\">".
__('Edit')."</button dojoType=\"dijit.form.Button\">
<button dojoType=\"dijit.form.Button\" onclick=\"javascript:removeSelectedUsers()\">".
__('Remove')."</button dojoType=\"dijit.form.Button\">
<button dojoType=\"dijit.form.Button\" onclick=\"javascript:resetSelectedUserPass()\">".
__('Reset password')."</button dojoType=\"dijit.form.Button\">";
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
"hook_prefs_tab_section", "prefUsersToolbar");
2011-12-13 10:00:36 +00:00
print "</div>"; #toolbar
print "</div>"; #pane
print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
print "<div id=\"sticky-status-msg\"></div>";
if ($user_search) {
2013-04-16 16:40:23 +00:00
$user_search = explode(" ", $user_search);
2011-12-13 10:00:36 +00:00
$tokens = array();
foreach ($user_search as $token) {
$token = trim($token);
array_push($tokens, "(UPPER(login) LIKE UPPER('%$token%'))");
}
$user_search_query = "(" . join($tokens, " AND ") . ") AND ";
} else {
$user_search_query = "";
}
2013-04-17 16:12:14 +00:00
$result = $this->dbh->query("SELECT
2011-12-13 10:00:36 +00:00
id,login,access_level,email,
".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login,
".SUBSTRING_FOR_DATE."(created,1,16) as created
FROM
ttrss_users
WHERE
$user_search_query
id > 0
ORDER BY $sort");
2013-04-17 16:12:14 +00:00
if ($this->dbh->num_rows($result) > 0) {
2011-12-13 10:00:36 +00:00
print "<p><table width=\"100%\" cellspacing=\"0\"
class=\"prefUserList\" id=\"prefUserList\">";
print "<tr class=\"title\">
<td align='center' width=\"5%\">&nbsp;</td>
2012-12-25 14:20:33 +00:00
<td width='30%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
<td width='30%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
2011-12-13 10:00:36 +00: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;
2013-04-17 16:12:14 +00:00
while ($line = $this->dbh->fetch_assoc($result)) {
2011-12-13 10:00:36 +00:00
$uid = $line["id"];
2012-12-25 14:20:33 +00:00
print "<tr id=\"UMRR-$uid\">";
2011-12-13 10:00:36 +00: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 10:00:36 +00:00
2012-12-25 14:20:33 +00:00
print "<td align='center'><input onclick='toggleSelectRow2(this);'
dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
id=\"UMCHK-$uid\"></td>";
2011-12-13 10:00:36 +00:00
$onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
print "<td $onclick>" . $line["login"] . "</td>";
if (!$line["email"]) $line["email"] = "&nbsp;";
print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
print "<td $onclick>" . $line["created"] . "</td>";
print "<td $onclick>" . $line["last_login"] . "</td>";
print "</tr>";
++$lnum;
}
print "</table>";
} else {
print "<p>";
if (!$user_search) {
print_warning(__('No users defined.'));
} else {
print_warning(__('No matching users found.'));
}
print "</p>";
}
print "</div>"; #pane
2012-12-23 12:15:34 +00:00
2013-04-18 08:27:34 +00:00
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
2012-12-23 12:15:34 +00:00
"hook_prefs_tab", "prefUsers");
2011-12-13 10:00:36 +00:00
print "</div>"; #container
}
}
?>