]> git.wh0rd.org Git - tt-rss.git/blob - classes/pref/users.php
php: remove trailing whitespaces
[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 edit() {
21                         global $access_level_names;
22
23                         print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
24
25                         print '<div dojoType="dijit.layout.TabContainer" style="height : 400px">
26                         <div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">';
27
28                         //print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
29
30                         $id = (int) clean($_REQUEST["id"]);
31
32                         print_hidden("id", "$id");
33                         print_hidden("op", "pref-users");
34                         print_hidden("method", "editSave");
35
36                         $sth = $this->pdo->prepare("SELECT * FROM ttrss_users WHERE id = ?");
37                         $sth->execute([$id]);
38
39                         if ($row = $sth->fetch()) {
40
41                                 $login = $row["login"];
42                                 $access_level = $row["access_level"];
43                                 $email = $row["email"];
44
45                                 $sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : "";
46
47                                 print "<div class=\"dlgSec\">".__("User")."</div>";
48                                 print "<div class=\"dlgSecCont\">";
49
50                                 if ($sel_disabled) {
51                                         print_hidden("login", "$login");
52                                 }
53
54                                 print "<input size=\"30\" style=\"font-size : 16px\"
55                                         dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
56                                         $sel_disabled
57                                         name=\"login\" value=\"$login\">";
58
59                                 print "</div>";
60
61                                 print "<div class=\"dlgSec\">".__("Authentication")."</div>";
62                                 print "<div class=\"dlgSecCont\">";
63
64                                 print __('Access level: ') . " ";
65
66                                 if (!$sel_disabled) {
67                                         print_select_hash("access_level", $access_level, $access_level_names,
68                                                 "dojoType=\"dijit.form.Select\" $sel_disabled");
69                                 } else {
70                                         print_select_hash("", $access_level, $access_level_names,
71                                                 "dojoType=\"dijit.form.Select\" $sel_disabled");
72                                         print_hidden("access_level", "$access_level");
73                                 }
74
75                                 print "<hr/>";
76
77                                 print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
78                                 name=\"password\">";
79
80                                 print "</div>";
81
82                                 print "<div class=\"dlgSec\">".__("Options")."</div>";
83                                 print "<div class=\"dlgSecCont\">";
84
85                                 print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
86                                 value=\"$email\">";
87
88                                 print "</div>";
89
90                                 print "</table>";
91
92                         }
93
94                         print '</div>'; #tab
95                         print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
96                                 dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
97
98                         print '</div>';
99                         print '</div>';
100
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>";
106
107                         print "</form>";
108
109                         return;
110                 }
111
112                 function userdetails() {
113                         $id = (int) clean($_REQUEST["id"]);
114
115                         $sth = $this->pdo->prepare("SELECT login,
116                                 ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
117                                 access_level,
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
121                                 FROM ttrss_users
122                                 WHERE id = ?");
123                         $sth->execute([$id]);
124
125                         if ($row = $sth->fetch()) {
126                                 print "<table width='100%'>";
127
128                                 $last_login = make_local_datetime(
129                                         $row["last_login"], true);
130
131                                 $created = make_local_datetime(
132                                         $row["created"], true);
133
134                                 $stored_articles = $row["stored_articles"];
135
136                                 print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
137                                 print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
138
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"];
144
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>";
147
148                                 print "</table>";
149
150                                 print "<h1>".__('Subscribed feeds')."</h1>";
151
152                                 $sth = $this->pdo->prepare("SELECT id,title,site_url FROM ttrss_feeds
153                                         WHERE owner_uid = ? ORDER BY title");
154                                 $sth->execute([$id]);
155
156                                 print "<ul class=\"userFeedList\">";
157
158                                 while ($line = $sth->fetch()) {
159
160                                         $icon_file = ICONS_URL."/".$line["id"].".ico";
161
162                                         if (file_exists($icon_file) && filesize($icon_file) > 0) {
163                                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
164                                         } else {
165                                                 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
166                                         }
167
168                                         print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
169
170                                 }
171
172                                 print "</ul>";
173
174
175                         } else {
176                                 print "<h1>".__('User not found')."</h1>";
177                         }
178
179                 }
180
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"]);
187
188                         if ($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).",";
193                         } else {
194                                 $pass_query_part = "";
195                         }
196
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]);
200
201                 }
202
203                 function remove() {
204                         $ids = explode(",", clean($_REQUEST["ids"]));
205
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]);
210
211                                         $sth = $this->pdo->prepare("DELETE FROM ttrss_feeds WHERE owner_uid = ?");
212                                         $sth->execute([$id]);
213
214                                         $sth = $this->pdo->prepare("DELETE FROM ttrss_users WHERE id = ?");
215                                         $sth->execute([$id]);
216                                 }
217                         }
218                 }
219
220                 function add() {
221
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);
226
227                         $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
228                                 login = ?");
229                         $sth->execute([$login]);
230
231                         if (!$sth->fetch()) {
232
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]);
237
238                                 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
239                                         login = ? AND pwd_hash = ?");
240                                 $sth->execute([$login, $pwd_hash]);
241
242                                 if ($row = $sth->fetch()) {
243
244                                         $new_uid = $row['id'];
245
246                                         print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
247                                                 $login, $tmp_user_pwd));
248
249                                         initialize_user($new_uid);
250
251                                 } else {
252
253                                         print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
254
255                                 }
256                         } else {
257                                 print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
258                         }
259                 }
260
261                 static function resetUserPassword($uid, $show_password) {
262
263                         $pdo = Db::pdo();
264
265                         $sth = $pdo->prepare("SELECT login, email
266                                 FROM ttrss_users WHERE id = ?");
267                         $sth->execute([$uid]);
268
269                         if ($row = $sth->fetch()) {
270
271                                 $login = $row["login"];
272                                 $email = $row["email"];
273
274                                 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
275                                 $tmp_user_pwd = make_password(8);
276
277                                 $pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
278
279                                 $sth = $pdo->prepare("UPDATE ttrss_users
280                                           SET pwd_hash = ?, salt = ?, otp_enabled = false
281                                         WHERE id = ?");
282                                 $sth->execute([$pwd_hash, $new_salt, $uid]);
283
284                                 if ($show_password) {
285                                         print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
286                                 } else {
287                                         print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
288                                 }
289
290                                 require_once 'classes/ttrssmailer.php';
291
292                                 if ($email) {
293                                         require_once "lib/MiniTemplator.class.php";
294
295                                         $tpl = new MiniTemplator;
296
297                                         $tpl->readTemplateFromFile("templates/resetpass_template.txt");
298
299                                         $tpl->setVariable('LOGIN', $login);
300                                         $tpl->setVariable('NEWPASS', $tmp_user_pwd);
301
302                                         $tpl->addBlock('message');
303
304                                         $message = "";
305
306                                         $tpl->generateOutputToString($message);
307
308                                         $mail = new ttrssMailer();
309
310                                         $rc = $mail->quickMail($email, $login,
311                                                 __("[tt-rss] Password change notification"),
312                                                 $message, false);
313
314                                         if (!$rc) print_error($mail->ErrorInfo);
315                                 }
316
317                         }
318                 }
319
320                 function resetPass() {
321                         $uid = clean($_REQUEST["id"]);
322                         Pref_Users::resetUserPassword($uid, true);
323                 }
324
325                 function index() {
326
327                         global $access_level_names;
328
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\">";
331
332                         print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">";
333
334                         $user_search = trim(clean($_REQUEST["search"]));
335
336                         if (array_key_exists("search", $_REQUEST)) {
337                                 $_SESSION["prefs_user_search"] = $user_search;
338                         } else {
339                                 $user_search = $_SESSION["prefs_user_search"];
340                         }
341
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>
347                                 </div>";
348
349                         $sort = clean($_REQUEST["sort"]);
350
351                         if (!$sort || $sort == "undefined") {
352                                 $sort = "login";
353                         }
354
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>";
363
364                         print "<button dojoType=\"dijit.form.Button\" onclick=\"addUser()\">".__('Create user')."</button>";
365
366                         print "
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\">";
373
374                         PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
375                                 "hook_prefs_tab_section", "prefUsersToolbar");
376
377                         print "</div>"; #toolbar
378                         print "</div>"; #pane
379                         print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
380
381                         $sort = validate_field($sort,
382                                 ["login", "access_level", "created", "num_feeds", "created", "last_login"], "login");
383
384                         if ($sort != "login") $sort = "$sort DESC";
385
386                         $sth = $this->pdo->prepare("SELECT
387                                         tu.id,
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
392                                 FROM
393                                         ttrss_users tu
394                                 WHERE
395                                         (:search = '' OR login LIKE :search) AND tu.id > 0
396                                 ORDER BY $sort");
397                         $sth->execute([":search" => $user_search ? "%$user_search%" : ""]);
398
399                         print "<p><table width=\"100%\" cellspacing=\"0\"
400                                 class=\"prefUserList\" id=\"prefUserList\">";
401
402                         print "<tr class=\"title\">
403                                                 <td align='center' width=\"5%\">&nbsp;</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>";
409
410                         $lnum = 0;
411
412                         while ($line = $sth->fetch()) {
413
414                                 $uid = $line["id"];
415
416                                 print "<tr id=\"UMRR-$uid\">";
417
418                                 $line["login"] = htmlspecialchars($line["login"]);
419
420                                 $line["created"] = make_local_datetime($line["created"], false);
421                                 $line["last_login"] = make_local_datetime($line["last_login"], false);
422
423                                 print "<td align='center'><input onclick='toggleSelectRow2(this);'
424                                         dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
425                                         id=\"UMCHK-$uid\"></td>";
426
427                                 $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
428
429                                 print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
430
431                                 if (!$line["email"]) $line["email"] = "&nbsp;";
432
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>";
437
438                                 print "</tr>";
439
440                                 ++$lnum;
441                         }
442
443                         print "</table>";
444
445                         if ($lnum == 0) {
446                                 if (!$user_search) {
447                                         print_warning(__('No users defined.'));
448                                 } else {
449                                         print_warning(__('No matching users found.'));
450                                 }
451                         }
452
453                         print "</div>"; #pane
454
455                         PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
456                                 "hook_prefs_tab", "prefUsers");
457
458                         print "</div>"; #container
459
460                 }
461         }