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