]> git.wh0rd.org - tt-rss.git/blame - classes/pref/users.php
ci: fix typo
[tt-rss.git] / classes / pref / users.php
CommitLineData
66665fba 1<?php
369dbc19 2class Pref_Users extends Handler_Protected {
17f9d200
JK
3 function before($method) {
4 if (parent::before($method)) {
66665fba
AD
5 if ($_SESSION["access_level"] < 10) {
6 print __("Your access level is insufficient to open this tab.");
7 return false;
8 }
9 return true;
10 }
11 return false;
12 }
13
8484ce22 14 function csrf_ignore($method) {
d9afd9b2 15 $csrf_ignored = array("index", "edit", "userdetails");
8484ce22
AD
16
17 return array_search($method, $csrf_ignored) !== false;
18 }
19
66665fba
AD
20 function edit() {
21 global $access_level_names;
22
bf9cc9ad
AD
23 print '<div dojoType="dijit.layout.TabContainer" style="height : 400px">
24 <div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">';
25
222a61c2 26 print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
66665fba 27
bf9cc9ad
AD
28 $id = (int) $this->dbh->escape_string($_REQUEST["id"]);
29
328118d1
AD
30 print_hidden("id", "$id");
31 print_hidden("op", "pref-users");
32 print_hidden("method", "editSave");
66665fba 33
d9c85e0f 34 $result = $this->dbh->query("SELECT * FROM ttrss_users WHERE id = '$id'");
66665fba 35
d9c85e0f
AD
36 $login = $this->dbh->fetch_result($result, 0, "login");
37 $access_level = $this->dbh->fetch_result($result, 0, "access_level");
38 $email = $this->dbh->fetch_result($result, 0, "email");
66665fba 39
a84fb943 40 $sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : "";
66665fba
AD
41
42 print "<div class=\"dlgSec\">".__("User")."</div>";
43 print "<div class=\"dlgSecCont\">";
44
45 if ($sel_disabled) {
328118d1 46 print_hidden("login", "$login");
66665fba
AD
47 }
48
222a61c2
AD
49 print "<input size=\"30\" style=\"font-size : 16px\"
50 dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
bf9cc9ad 51 $sel_disabled
222a61c2
AD
52 name=\"login\" value=\"$login\">";
53
66665fba
AD
54 print "</div>";
55
56 print "<div class=\"dlgSec\">".__("Authentication")."</div>";
57 print "<div class=\"dlgSecCont\">";
58
59 print __('Access level: ') . " ";
60
61 if (!$sel_disabled) {
62 print_select_hash("access_level", $access_level, $access_level_names,
222a61c2 63 "dojoType=\"dijit.form.Select\" $sel_disabled");
66665fba
AD
64 } else {
65 print_select_hash("", $access_level, $access_level_names,
222a61c2 66 "dojoType=\"dijit.form.Select\" $sel_disabled");
328118d1 67 print_hidden("access_level", "$access_level");
66665fba
AD
68 }
69
222a61c2 70 print "<hr/>";
66665fba 71
bf9cc9ad 72 print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
66665fba
AD
73 name=\"password\">";
74
75 print "</div>";
76
77 print "<div class=\"dlgSec\">".__("Options")."</div>";
78 print "<div class=\"dlgSecCont\">";
79
bf9cc9ad 80 print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
66665fba
AD
81 value=\"$email\">";
82
83 print "</div>";
84
85 print "</table>";
86
87 print "</form>";
88
bf9cc9ad
AD
89 print '</div>'; #tab
90 print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
91 dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
92
93 print '</div>';
94 print '</div>';
95
66665fba 96 print "<div class=\"dlgButtons\">
222a61c2 97 <button dojoType=\"dijit.form.Button\" type=\"submit\">".
bf9cc9ad 98 __('Save')."</button>
222a61c2 99 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
bf9cc9ad 100 __('Cancel')."</button></div>";
66665fba 101
66665fba
AD
102 return;
103 }
104
bf9cc9ad
AD
105 function userdetails() {
106 $id = (int) $this->dbh->escape_string($_REQUEST["id"]);
107
108 $result = $this->dbh->query("SELECT login,
109 ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
110 access_level,
111 (SELECT COUNT(int_id) FROM ttrss_user_entries
112 WHERE owner_uid = id) AS stored_articles,
113 ".SUBSTRING_FOR_DATE."(created,1,16) AS created
114 FROM ttrss_users
115 WHERE id = '$id'");
116
117 if ($this->dbh->num_rows($result) == 0) {
118 print "<h1>".__('User not found')."</h1>";
119 return;
120 }
121
122 print "<table width='100%'>";
123
124 $last_login = make_local_datetime(
125 $this->dbh->fetch_result($result, 0, "last_login"), true);
126
127 $created = make_local_datetime(
128 $this->dbh->fetch_result($result, 0, "created"), true);
129
130 $stored_articles = $this->dbh->fetch_result($result, 0, "stored_articles");
131
132 print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
133 print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
134
135 $result = $this->dbh->query("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
136 WHERE owner_uid = '$id'");
137
138 $num_feeds = $this->dbh->fetch_result($result, 0, "num_feeds");
139
140 print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
141 print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
142
143 print "</table>";
144
145 print "<h1>".__('Subscribed feeds')."</h1>";
146
147 $result = $this->dbh->query("SELECT id,title,site_url FROM ttrss_feeds
148 WHERE owner_uid = '$id' ORDER BY title");
149
150 print "<ul class=\"userFeedList\">";
151
152 while ($line = $this->dbh->fetch_assoc($result)) {
153
154 $icon_file = ICONS_URL."/".$line["id"].".ico";
155
156 if (file_exists($icon_file) && filesize($icon_file) > 0) {
157 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
158 } else {
159 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
160 }
161
162 print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
163
164 }
165
166 if ($this->dbh->num_rows($result) < $num_feeds) {
167 // FIXME - add link to show ALL subscribed feeds here somewhere
168 print "<li><img
169 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
170 }
171
172 print "</ul>";
173 }
174
66665fba 175 function editSave() {
d9c85e0f
AD
176 $login = $this->dbh->escape_string(trim($_REQUEST["login"]));
177 $uid = $this->dbh->escape_string($_REQUEST["id"]);
66665fba 178 $access_level = (int) $_REQUEST["access_level"];
d9c85e0f 179 $email = $this->dbh->escape_string(trim($_REQUEST["email"]));
c72069b0 180 $password = $_REQUEST["password"];
66665fba
AD
181
182 if ($password) {
8db5d8ea 183 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
098df83b
AD
184 $pwd_hash = encrypt_password($password, $salt, true);
185 $pass_query_part = "pwd_hash = '$pwd_hash', salt = '$salt',";
66665fba
AD
186 } else {
187 $pass_query_part = "";
188 }
189
d9c85e0f 190 $this->dbh->query("UPDATE ttrss_users SET $pass_query_part login = '$login',
e9f42579 191 access_level = '$access_level', email = '$email', otp_enabled = false
b8386ad3 192 WHERE id = '$uid'");
66665fba
AD
193
194 }
195
196 function remove() {
d9c85e0f 197 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
66665fba
AD
198
199 foreach ($ids as $id) {
200 if ($id != $_SESSION["uid"] && $id != 1) {
d9c85e0f
AD
201 $this->dbh->query("DELETE FROM ttrss_tags WHERE owner_uid = '$id'");
202 $this->dbh->query("DELETE FROM ttrss_feeds WHERE owner_uid = '$id'");
203 $this->dbh->query("DELETE FROM ttrss_users WHERE id = '$id'");
66665fba
AD
204 }
205 }
206 }
207
208 function add() {
209
d9c85e0f 210 $login = $this->dbh->escape_string(trim($_REQUEST["login"]));
66665fba 211 $tmp_user_pwd = make_password(8);
8db5d8ea 212 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
098df83b 213 $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
66665fba 214
d9c85e0f 215 $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
66665fba
AD
216 login = '$login'");
217
d9c85e0f 218 if ($this->dbh->num_rows($result) == 0) {
66665fba 219
d9c85e0f 220 $this->dbh->query("INSERT INTO ttrss_users
098df83b
AD
221 (login,pwd_hash,access_level,last_login,created, salt)
222 VALUES ('$login', '$pwd_hash', 0, null, NOW(), '$salt')");
66665fba
AD
223
224
d9c85e0f 225 $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
66665fba
AD
226 login = '$login' AND pwd_hash = '$pwd_hash'");
227
d9c85e0f 228 if ($this->dbh->num_rows($result) == 1) {
66665fba 229
d9c85e0f 230 $new_uid = $this->dbh->fetch_result($result, 0, "id");
66665fba
AD
231
232 print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
233 $login, $tmp_user_pwd));
234
a42c55f0 235 initialize_user($new_uid);
66665fba
AD
236
237 } else {
238
239 print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
240
241 }
242 } else {
243 print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
244 }
245 }
246
233b74ad 247 static function resetUserPassword($uid, $show_password) {
66665fba 248
233b74ad 249 $result = db_query("SELECT login,email
66665fba
AD
250 FROM ttrss_users WHERE id = '$uid'");
251
252 $login = db_fetch_result($result, 0, "login");
253 $email = db_fetch_result($result, 0, "email");
098df83b 254
8db5d8ea 255 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
66665fba 256 $tmp_user_pwd = make_password(8);
66665fba 257
098df83b
AD
258 $pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
259
b41c5741 260 db_query("UPDATE ttrss_users SET pwd_hash = '$pwd_hash', salt = '$new_salt', otp_enabled = false
66665fba
AD
261 WHERE id = '$uid'");
262
f43e9e97 263 if ($show_password) {
18446943 264 print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
f43e9e97 265 } else {
483f15d5 266 print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
f43e9e97 267 }
66665fba 268
68fb3c95 269 require_once 'classes/ttrssmailer.php';
66665fba
AD
270
271 if ($email) {
66665fba
AD
272 require_once "lib/MiniTemplator.class.php";
273
274 $tpl = new MiniTemplator;
275
276 $tpl->readTemplateFromFile("templates/resetpass_template.txt");
277
278 $tpl->setVariable('LOGIN', $login);
279 $tpl->setVariable('NEWPASS', $tmp_user_pwd);
280
281 $tpl->addBlock('message');
282
283 $message = "";
284
285 $tpl->generateOutputToString($message);
286
68fb3c95 287 $mail = new ttrssMailer();
66665fba 288
68fb3c95
AD
289 $rc = $mail->quickMail($email, $login,
290 __("[tt-rss] Password change notification"),
291 $message, false);
66665fba
AD
292
293 if (!$rc) print_error($mail->ErrorInfo);
294 }
f43e9e97 295 }
66665fba 296
f43e9e97 297 function resetPass() {
d9c85e0f 298 $uid = $this->dbh->escape_string($_REQUEST["id"]);
a42c55f0 299 Pref_Users::resetUserPassword($uid, true);
66665fba
AD
300 }
301
302 function index() {
303
304 global $access_level_names;
305
306 print "<div id=\"pref-user-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
307 print "<div id=\"pref-user-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
308
309 print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">";
310
d9c85e0f 311 $user_search = $this->dbh->escape_string($_REQUEST["search"]);
66665fba
AD
312
313 if (array_key_exists("search", $_REQUEST)) {
314 $_SESSION["prefs_user_search"] = $user_search;
315 } else {
316 $user_search = $_SESSION["prefs_user_search"];
317 }
318
319 print "<div style='float : right; padding-right : 4px;'>
320 <input dojoType=\"dijit.form.TextBox\" id=\"user_search\" size=\"20\" type=\"search\"
321 value=\"$user_search\">
808ef3d4 322 <button dojoType=\"dijit.form.Button\" onclick=\"updateUsersList()\">".
66665fba
AD
323 __('Search')."</button>
324 </div>";
325
d9c85e0f 326 $sort = $this->dbh->escape_string($_REQUEST["sort"]);
66665fba
AD
327
328 if (!$sort || $sort == "undefined") {
329 $sort = "login";
330 }
331
332 print "<div dojoType=\"dijit.form.DropDownButton\">".
333 "<span>" . __('Select')."</span>";
334 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
335 print "<div onclick=\"selectTableRows('prefUserList', 'all')\"
336 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
337 print "<div onclick=\"selectTableRows('prefUserList', 'none')\"
338 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
339 print "</div></div>";
340
808ef3d4 341 print "<button dojoType=\"dijit.form.Button\" onclick=\"addUser()\">".__('Create user')."</button>";
66665fba
AD
342
343 print "
808ef3d4 344 <button dojoType=\"dijit.form.Button\" onclick=\"editSelectedUser()\">".
66665fba 345 __('Edit')."</button dojoType=\"dijit.form.Button\">
808ef3d4 346 <button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedUsers()\">".
66665fba 347 __('Remove')."</button dojoType=\"dijit.form.Button\">
808ef3d4 348 <button dojoType=\"dijit.form.Button\" onclick=\"resetSelectedUserPass()\">".
66665fba
AD
349 __('Reset password')."</button dojoType=\"dijit.form.Button\">";
350
610fe115
AD
351 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
352 "hook_prefs_tab_section", "prefUsersToolbar");
353
66665fba
AD
354 print "</div>"; #toolbar
355 print "</div>"; #pane
356 print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
357
358 print "<div id=\"sticky-status-msg\"></div>";
359
360 if ($user_search) {
361
a5680acb 362 $user_search = explode(" ", $user_search);
66665fba
AD
363 $tokens = array();
364
365 foreach ($user_search as $token) {
366 $token = trim($token);
367 array_push($tokens, "(UPPER(login) LIKE UPPER('%$token%'))");
368 }
369
370 $user_search_query = "(" . join($tokens, " AND ") . ") AND ";
371
372 } else {
373 $user_search_query = "";
374 }
375
d9c85e0f 376 $result = $this->dbh->query("SELECT
1edff0d4
AD
377 tu.id,
378 login,access_level,email,
66665fba 379 ".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login,
1edff0d4 380 ".SUBSTRING_FOR_DATE."(created,1,16) as created,
f232aa5a 381 (SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds
66665fba 382 FROM
1edff0d4 383 ttrss_users tu
66665fba
AD
384 WHERE
385 $user_search_query
1edff0d4 386 tu.id > 0
66665fba
AD
387 ORDER BY $sort");
388
d9c85e0f 389 if ($this->dbh->num_rows($result) > 0) {
66665fba
AD
390
391 print "<p><table width=\"100%\" cellspacing=\"0\"
392 class=\"prefUserList\" id=\"prefUserList\">";
393
394 print "<tr class=\"title\">
395 <td align='center' width=\"5%\">&nbsp;</td>
1edff0d4
AD
396 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
397 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
398 <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Subscribed feeds')."</a></td>
66665fba
AD
399 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td>
400 <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>";
401
402 $lnum = 0;
403
d9c85e0f 404 while ($line = $this->dbh->fetch_assoc($result)) {
66665fba 405
66665fba
AD
406 $uid = $line["id"];
407
84391d69 408 print "<tr id=\"UMRR-$uid\">";
66665fba
AD
409
410 $line["login"] = htmlspecialchars($line["login"]);
411
a42c55f0
AD
412 $line["created"] = make_local_datetime($line["created"], false);
413 $line["last_login"] = make_local_datetime($line["last_login"], false);
66665fba 414
84391d69
AD
415 print "<td align='center'><input onclick='toggleSelectRow2(this);'
416 dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
417 id=\"UMCHK-$uid\"></td>";
66665fba
AD
418
419 $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
420
2f20dd58 421 print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
66665fba
AD
422
423 if (!$line["email"]) $line["email"] = "&nbsp;";
424
425 print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
1edff0d4 426 print "<td $onclick>" . $line["num_feeds"] . "</td>";
66665fba
AD
427 print "<td $onclick>" . $line["created"] . "</td>";
428 print "<td $onclick>" . $line["last_login"] . "</td>";
429
430 print "</tr>";
431
432 ++$lnum;
433 }
434
435 print "</table>";
436
437 } else {
438 print "<p>";
439 if (!$user_search) {
440 print_warning(__('No users defined.'));
441 } else {
442 print_warning(__('No matching users found.'));
443 }
444 print "</p>";
445
446 }
447
448 print "</div>"; #pane
6065f3ad 449
1ffe3391 450 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
6065f3ad
AD
451 "hook_prefs_tab", "prefUsers");
452
66665fba
AD
453 print "</div>"; #container
454
455 }
456
457 }
458?>