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