]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/users.php
2 class Pref_Users
extends Handler_Protected
{
3 function before($method) {
4 if (parent
::before($method)) {
5 if ($_SESSION["access_level"] < 10) {
6 print __("Your access level is insufficient to open this tab.");
14 function csrf_ignore($method) {
15 $csrf_ignored = array("index", "edit", "userdetails");
17 return array_search($method, $csrf_ignored) !== false;
21 global $access_level_names;
23 print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
25 print '<div dojoType="dijit.layout.TabContainer" style="height : 400px">
26 <div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">';
28 //print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
30 $id = (int) clean($_REQUEST["id"]);
32 print_hidden("id", "$id");
33 print_hidden("op", "pref-users");
34 print_hidden("method", "editSave");
36 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_users WHERE id = ?");
39 if ($row = $sth->fetch()) {
41 $login = $row["login"];
42 $access_level = $row["access_level"];
43 $email = $row["email"];
45 $sel_disabled = ($id == $_SESSION["uid"] ||
$login == "admin") ?
"disabled" : "";
47 print "<div class=\"dlgSec\">".__("User")."</div>";
48 print "<div class=\"dlgSecCont\">";
51 print_hidden("login", "$login");
54 print "<input size=\"30\" style=\"font-size : 16px\"
55 dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
57 name=\"login\" value=\"$login\">";
61 print "<div class=\"dlgSec\">".__("Authentication")."</div>";
62 print "<div class=\"dlgSecCont\">";
64 print __('Access level: ') . " ";
67 print_select_hash("access_level", $access_level, $access_level_names,
68 "dojoType=\"dijit.form.Select\" $sel_disabled");
70 print_select_hash("", $access_level, $access_level_names,
71 "dojoType=\"dijit.form.Select\" $sel_disabled");
72 print_hidden("access_level", "$access_level");
77 print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
82 print "<div class=\"dlgSec\">".__("Options")."</div>";
83 print "<div class=\"dlgSecCont\">";
85 print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
95 print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
96 dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
101 print "<div class=\"dlgButtons\">
102 <button dojoType=\"dijit.form.Button\" class=\"btn-primary\" type=\"submit\" onclick=\"dijit.byId('userEditDlg').execute()\">".
103 __('Save')."</button>
104 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
105 __('Cancel')."</button></div>";
112 function userdetails() {
113 $id = (int) clean($_REQUEST["id"]);
115 $sth = $this->pdo
->prepare("SELECT login,
116 ".SUBSTRING_FOR_DATE
."(last_login,1,16) AS last_login,
118 (SELECT COUNT(int_id) FROM ttrss_user_entries
119 WHERE owner_uid = id) AS stored_articles,
120 ".SUBSTRING_FOR_DATE
."(created,1,16) AS created
123 $sth->execute([$id]);
125 if ($row = $sth->fetch()) {
126 print "<table width='100%'>";
128 $last_login = make_local_datetime(
129 $row["last_login"], true);
131 $created = make_local_datetime(
132 $row["created"], true);
134 $stored_articles = $row["stored_articles"];
136 print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
137 print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
139 $sth = $this->pdo
->prepare("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
140 WHERE owner_uid = ?");
141 $sth->execute([$id]);
142 $row = $sth->fetch();
143 $num_feeds = $row["num_feeds"];
145 print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
146 print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
150 print "<h1>".__('Subscribed feeds')."</h1>";
152 $sth = $this->pdo
->prepare("SELECT id,title,site_url FROM ttrss_feeds
153 WHERE owner_uid = ? ORDER BY title");
154 $sth->execute([$id]);
156 print "<ul class=\"userFeedList\">";
158 while ($line = $sth->fetch()) {
160 $icon_file = ICONS_URL
."/".$line["id"].".ico";
162 if (file_exists($icon_file) && filesize($icon_file) > 0) {
163 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
165 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
168 print "<li>$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
176 print "<h1>".__('User not found')."</h1>";
181 function editSave() {
182 $login = trim(clean($_REQUEST["login"]));
183 $uid = clean($_REQUEST["id"]);
184 $access_level = (int) clean($_REQUEST["access_level"]);
185 $email = trim(clean($_REQUEST["email"]));
186 $password = clean($_REQUEST["password"]);
189 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
190 $pwd_hash = encrypt_password($password, $salt, true);
191 $pass_query_part = "pwd_hash = ".$this->pdo
->quote($pwd_hash).",
192 salt = ".$this->pdo
->quote($salt).",";
194 $pass_query_part = "";
197 $sth = $this->pdo
->prepare("UPDATE ttrss_users SET $pass_query_part login = ?,
198 access_level = ?, email = ?, otp_enabled = false WHERE id = ?");
199 $sth->execute([$login, $access_level, $email, $uid]);
204 $ids = explode(",", clean($_REQUEST["ids"]));
206 foreach ($ids as $id) {
207 if ($id != $_SESSION["uid"] && $id != 1) {
208 $sth = $this->pdo
->prepare("DELETE FROM ttrss_tags WHERE owner_uid = ?");
209 $sth->execute([$id]);
211 $sth = $this->pdo
->prepare("DELETE FROM ttrss_feeds WHERE owner_uid = ?");
212 $sth->execute([$id]);
214 $sth = $this->pdo
->prepare("DELETE FROM ttrss_users WHERE id = ?");
215 $sth->execute([$id]);
222 $login = trim(clean($_REQUEST["login"]));
223 $tmp_user_pwd = make_password(8);
224 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
225 $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
227 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_users WHERE
229 $sth->execute([$login]);
231 if (!$sth->fetch()) {
233 $sth = $this->pdo
->prepare("INSERT INTO ttrss_users
234 (login,pwd_hash,access_level,last_login,created, salt)
235 VALUES (?, ?, 0, null, NOW(), ?)");
236 $sth->execute([$login, $pwd_hash, $salt]);
238 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_users WHERE
239 login = ? AND pwd_hash = ?");
240 $sth->execute([$login, $pwd_hash]);
242 if ($row = $sth->fetch()) {
244 $new_uid = $row['id'];
246 print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
247 $login, $tmp_user_pwd));
249 initialize_user($new_uid);
253 print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
257 print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
261 static function resetUserPassword($uid, $show_password) {
265 $sth = $pdo->prepare("SELECT login, email
266 FROM ttrss_users WHERE id = ?");
267 $sth->execute([$uid]);
269 if ($row = $sth->fetch()) {
271 $login = $row["login"];
272 $email = $row["email"];
274 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
275 $tmp_user_pwd = make_password(8);
277 $pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
279 $sth = $pdo->prepare("UPDATE ttrss_users
280 SET pwd_hash = ?, salt = ?, otp_enabled = false
282 $sth->execute([$pwd_hash, $new_salt, $uid]);
284 if ($show_password) {
285 print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
287 print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
290 require_once 'classes/ttrssmailer.php';
293 require_once "lib/MiniTemplator.class.php";
295 $tpl = new MiniTemplator
;
297 $tpl->readTemplateFromFile("templates/resetpass_template.txt");
299 $tpl->setVariable('LOGIN', $login);
300 $tpl->setVariable('NEWPASS', $tmp_user_pwd);
302 $tpl->addBlock('message');
306 $tpl->generateOutputToString($message);
308 $mail = new ttrssMailer();
310 $rc = $mail->quickMail($email, $login,
311 __("[tt-rss] Password change notification"),
314 if (!$rc) print_error($mail->ErrorInfo
);
320 function resetPass() {
321 $uid = clean($_REQUEST["id"]);
322 Pref_Users
::resetUserPassword($uid, true);
327 global $access_level_names;
329 print "<div id=\"pref-user-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
330 print "<div id=\"pref-user-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
332 print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">";
334 $user_search = trim(clean($_REQUEST["search"]));
336 if (array_key_exists("search", $_REQUEST)) {
337 $_SESSION["prefs_user_search"] = $user_search;
339 $user_search = $_SESSION["prefs_user_search"];
342 print "<div style='float : right; padding-right : 4px;'>
343 <input dojoType=\"dijit.form.TextBox\" id=\"user_search\" size=\"20\" type=\"search\"
344 value=\"$user_search\">
345 <button dojoType=\"dijit.form.Button\" onclick=\"updateUsersList()\">".
346 __('Search')."</button>
349 $sort = clean($_REQUEST["sort"]);
351 if (!$sort ||
$sort == "undefined") {
355 print "<div dojoType=\"dijit.form.DropDownButton\">".
356 "<span>" . __('Select')."</span>";
357 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
358 print "<div onclick=\"selectTableRows('prefUserList', 'all')\"
359 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
360 print "<div onclick=\"selectTableRows('prefUserList', 'none')\"
361 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
362 print "</div></div>";
364 print "<button dojoType=\"dijit.form.Button\" onclick=\"addUser()\">".__('Create user')."</button>";
367 <button dojoType=\"dijit.form.Button\" onclick=\"editSelectedUser()\">".
368 __('Edit')."</button dojoType=\"dijit.form.Button\">
369 <button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedUsers()\">".
370 __('Remove')."</button dojoType=\"dijit.form.Button\">
371 <button dojoType=\"dijit.form.Button\" onclick=\"resetSelectedUserPass()\">".
372 __('Reset password')."</button dojoType=\"dijit.form.Button\">";
374 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB_SECTION
,
375 "hook_prefs_tab_section", "prefUsersToolbar");
377 print "</div>"; #toolbar
378 print "</div>"; #pane
379 print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
381 $sort = validate_field($sort,
382 ["login", "access_level", "created", "num_feeds", "created", "last_login"], "login");
384 if ($sort != "login") $sort = "$sort DESC";
386 $sth = $this->pdo
->prepare("SELECT
388 login,access_level,email,
389 ".SUBSTRING_FOR_DATE
."(last_login,1,16) as last_login,
390 ".SUBSTRING_FOR_DATE
."(created,1,16) as created,
391 (SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds
395 (:search = '' OR login LIKE :search) AND tu.id > 0
397 $sth->execute([":search" => $user_search ?
"%$user_search%" : ""]);
399 print "<p><table width=\"100%\" cellspacing=\"0\"
400 class=\"prefUserList\" id=\"prefUserList\">";
402 print "<tr class=\"title\">
403 <td align='center' width=\"5%\"> </td>
404 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
405 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
406 <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Subscribed feeds')."</a></td>
407 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td>
408 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>";
412 while ($line = $sth->fetch()) {
416 print "<tr id=\"UMRR-$uid\">";
418 $line["login"] = htmlspecialchars($line["login"]);
420 $line["created"] = make_local_datetime($line["created"], false);
421 $line["last_login"] = make_local_datetime($line["last_login"], false);
423 print "<td align='center'><input onclick='toggleSelectRow2(this);'
424 dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
425 id=\"UMCHK-$uid\"></td>";
427 $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
429 print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
431 if (!$line["email"]) $line["email"] = " ";
433 print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
434 print "<td $onclick>" . $line["num_feeds"] . "</td>";
435 print "<td $onclick>" . $line["created"] . "</td>";
436 print "<td $onclick>" . $line["last_login"] . "</td>";
447 print_warning(__('No users defined.'));
449 print_warning(__('No matching users found.'));
453 print "</div>"; #pane
455 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
456 "hook_prefs_tab", "prefUsers");
458 print "</div>"; #container