users.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. function csrf_ignore($method) {
  14. $csrf_ignored = array("index", "edit", "userdetails");
  15. return array_search($method, $csrf_ignored) !== false;
  16. }
  17. function edit() {
  18. global $access_level_names;
  19. print '<div dojoType="dijit.layout.TabContainer" style="height : 400px">
  20. <div dojoType="dijit.layout.ContentPane" title="'.__('Edit user').'">';
  21. print "<form id=\"user_edit_form\" onsubmit='return false' dojoType=\"dijit.form.Form\">";
  22. $id = (int) clean($_REQUEST["id"]);
  23. print_hidden("id", "$id");
  24. print_hidden("op", "pref-users");
  25. print_hidden("method", "editSave");
  26. $sth = $this->pdo->prepare("SELECT * FROM ttrss_users WHERE id = ?");
  27. $sth->execute([$id]);
  28. if ($row = $sth->fetch()) {
  29. $login = $row["login"];
  30. $access_level = $row["access_level"];
  31. $email = $row["email"];
  32. $sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : "";
  33. print "<div class=\"dlgSec\">".__("User")."</div>";
  34. print "<div class=\"dlgSecCont\">";
  35. if ($sel_disabled) {
  36. print_hidden("login", "$login");
  37. }
  38. print "<input size=\"30\" style=\"font-size : 16px\"
  39. dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
  40. $sel_disabled
  41. name=\"login\" value=\"$login\">";
  42. print "</div>";
  43. print "<div class=\"dlgSec\">".__("Authentication")."</div>";
  44. print "<div class=\"dlgSecCont\">";
  45. print __('Access level: ') . " ";
  46. if (!$sel_disabled) {
  47. print_select_hash("access_level", $access_level, $access_level_names,
  48. "dojoType=\"dijit.form.Select\" $sel_disabled");
  49. } else {
  50. print_select_hash("", $access_level, $access_level_names,
  51. "dojoType=\"dijit.form.Select\" $sel_disabled");
  52. print_hidden("access_level", "$access_level");
  53. }
  54. print "<hr/>";
  55. print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
  56. name=\"password\">";
  57. print "</div>";
  58. print "<div class=\"dlgSec\">".__("Options")."</div>";
  59. print "<div class=\"dlgSecCont\">";
  60. print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
  61. value=\"$email\">";
  62. print "</div>";
  63. print "</table>";
  64. print "</form>";
  65. }
  66. print '</div>'; #tab
  67. print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
  68. dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
  69. print '</div>';
  70. print '</div>';
  71. print "<div class=\"dlgButtons\">
  72. <button dojoType=\"dijit.form.Button\" type=\"submit\">".
  73. __('Save')."</button>
  74. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
  75. __('Cancel')."</button></div>";
  76. return;
  77. }
  78. function userdetails() {
  79. $id = (int) clean($_REQUEST["id"]);
  80. $sth = $this->pdo->prepare("SELECT login,
  81. ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
  82. access_level,
  83. (SELECT COUNT(int_id) FROM ttrss_user_entries
  84. WHERE owner_uid = id) AS stored_articles,
  85. ".SUBSTRING_FOR_DATE."(created,1,16) AS created
  86. FROM ttrss_users
  87. WHERE id = ?");
  88. $sth->execute([$id]);
  89. if ($row = $sth->fetch()) {
  90. print "<table width='100%'>";
  91. $last_login = make_local_datetime(
  92. $row["last_login"], true);
  93. $created = make_local_datetime(
  94. $row["created"], true);
  95. $stored_articles = $row["stored_articles"];
  96. print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
  97. print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
  98. $sth = $this->pdo->prepare("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
  99. WHERE owner_uid = ?");
  100. $sth->execute([$id]);
  101. $row = $sth->fetch();
  102. $num_feeds = $row["num_feeds"];
  103. print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
  104. print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
  105. print "</table>";
  106. print "<h1>".__('Subscribed feeds')."</h1>";
  107. $sth = $this->pdo->prepare("SELECT id,title,site_url FROM ttrss_feeds
  108. WHERE owner_uid = ? ORDER BY title");
  109. $sth->execute([$id]);
  110. print "<ul class=\"userFeedList\">";
  111. while ($line = $sth->fetch()) {
  112. $icon_file = ICONS_URL."/".$line["id"].".ico";
  113. if (file_exists($icon_file) && filesize($icon_file) > 0) {
  114. $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
  115. } else {
  116. $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
  117. }
  118. print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
  119. }
  120. print "</ul>";
  121. } else {
  122. print "<h1>".__('User not found')."</h1>";
  123. }
  124. }
  125. function editSave() {
  126. $login = trim(clean($_REQUEST["login"]));
  127. $uid = clean($_REQUEST["id"]);
  128. $access_level = (int) clean($_REQUEST["access_level"]);
  129. $email = trim(clean($_REQUEST["email"]));
  130. $password = clean($_REQUEST["password"]);
  131. if ($password) {
  132. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  133. $pwd_hash = encrypt_password($password, $salt, true);
  134. $pass_query_part = "pwd_hash = ".$this->pdo->quote($pwd_hash).",
  135. salt = ".$this->pdo->quote($salt).",";
  136. } else {
  137. $pass_query_part = "";
  138. }
  139. $sth = $this->pdo->prepare("UPDATE ttrss_users SET $pass_query_part login = ?,
  140. access_level = ?, email = ?, otp_enabled = false WHERE id = ?");
  141. $sth->execute([$login, $access_level, $email, $uid]);
  142. }
  143. function remove() {
  144. $ids = explode(",", clean($_REQUEST["ids"]));
  145. foreach ($ids as $id) {
  146. if ($id != $_SESSION["uid"] && $id != 1) {
  147. $sth = $this->pdo->prepare("DELETE FROM ttrss_tags WHERE owner_uid = ?");
  148. $sth->execute([$id]);
  149. $sth = $this->pdo->prepare("DELETE FROM ttrss_feeds WHERE owner_uid = ?");
  150. $sth->execute([$id]);
  151. $sth = $this->pdo->prepare("DELETE FROM ttrss_users WHERE id = ?");
  152. $sth->execute([$id]);
  153. }
  154. }
  155. }
  156. function add() {
  157. $login = trim(clean($_REQUEST["login"]));
  158. $tmp_user_pwd = make_password(8);
  159. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  160. $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
  161. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  162. login = ?");
  163. $sth->execute([$login]);
  164. if (!$sth->fetch()) {
  165. $sth = $this->pdo->prepare("INSERT INTO ttrss_users
  166. (login,pwd_hash,access_level,last_login,created, salt)
  167. VALUES (?, ?, 0, null, NOW(), ?)");
  168. $sth->execute([$login, $pwd_hash, $salt]);
  169. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  170. login = ? AND pwd_hash = ?");
  171. $sth->execute([$login, $pwd_hash]);
  172. if ($row = $sth->fetch()) {
  173. $new_uid = $row['id'];
  174. print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
  175. $login, $tmp_user_pwd));
  176. initialize_user($new_uid);
  177. } else {
  178. print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
  179. }
  180. } else {
  181. print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
  182. }
  183. }
  184. static function resetUserPassword($uid, $show_password) {
  185. $pdo = Db::pdo();
  186. $sth = $pdo->prepare("SELECT login, email
  187. FROM ttrss_users WHERE id = ?");
  188. $sth->execute([$uid]);
  189. if ($row = $sth->fetch()) {
  190. $login = $row["login"];
  191. $email = $row["email"];
  192. $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  193. $tmp_user_pwd = make_password(8);
  194. $pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true);
  195. $sth = $pdo->prepare("UPDATE ttrss_users
  196. SET pwd_hash = ?, salt = ?, otp_enabled = false
  197. WHERE id = ?");
  198. $sth->execute([$pwd_hash, $new_salt, $uid]);
  199. if ($show_password) {
  200. print T_sprintf("Changed password of user <b>%s</b> to <b>%s</b>", $login, $tmp_user_pwd);
  201. } else {
  202. print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $login, $email));
  203. }
  204. require_once 'classes/ttrssmailer.php';
  205. if ($email) {
  206. require_once "lib/MiniTemplator.class.php";
  207. $tpl = new MiniTemplator;
  208. $tpl->readTemplateFromFile("templates/resetpass_template.txt");
  209. $tpl->setVariable('LOGIN', $login);
  210. $tpl->setVariable('NEWPASS', $tmp_user_pwd);
  211. $tpl->addBlock('message');
  212. $message = "";
  213. $tpl->generateOutputToString($message);
  214. $mail = new ttrssMailer();
  215. $rc = $mail->quickMail($email, $login,
  216. __("[tt-rss] Password change notification"),
  217. $message, false);
  218. if (!$rc) print_error($mail->ErrorInfo);
  219. }
  220. }
  221. }
  222. function resetPass() {
  223. $uid = clean($_REQUEST["id"]);
  224. Pref_Users::resetUserPassword($uid, true);
  225. }
  226. function index() {
  227. global $access_level_names;
  228. print "<div id=\"pref-user-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  229. print "<div id=\"pref-user-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  230. print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">";
  231. $user_search = trim(clean($_REQUEST["search"]));
  232. if (array_key_exists("search", $_REQUEST)) {
  233. $_SESSION["prefs_user_search"] = $user_search;
  234. } else {
  235. $user_search = $_SESSION["prefs_user_search"];
  236. }
  237. print "<div style='float : right; padding-right : 4px;'>
  238. <input dojoType=\"dijit.form.TextBox\" id=\"user_search\" size=\"20\" type=\"search\"
  239. value=\"$user_search\">
  240. <button dojoType=\"dijit.form.Button\" onclick=\"updateUsersList()\">".
  241. __('Search')."</button>
  242. </div>";
  243. $sort = clean($_REQUEST["sort"]);
  244. if (!$sort || $sort == "undefined") {
  245. $sort = "login";
  246. }
  247. print "<div dojoType=\"dijit.form.DropDownButton\">".
  248. "<span>" . __('Select')."</span>";
  249. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  250. print "<div onclick=\"selectTableRows('prefUserList', 'all')\"
  251. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  252. print "<div onclick=\"selectTableRows('prefUserList', 'none')\"
  253. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  254. print "</div></div>";
  255. print "<button dojoType=\"dijit.form.Button\" onclick=\"addUser()\">".__('Create user')."</button>";
  256. print "
  257. <button dojoType=\"dijit.form.Button\" onclick=\"editSelectedUser()\">".
  258. __('Edit')."</button dojoType=\"dijit.form.Button\">
  259. <button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedUsers()\">".
  260. __('Remove')."</button dojoType=\"dijit.form.Button\">
  261. <button dojoType=\"dijit.form.Button\" onclick=\"resetSelectedUserPass()\">".
  262. __('Reset password')."</button dojoType=\"dijit.form.Button\">";
  263. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
  264. "hook_prefs_tab_section", "prefUsersToolbar");
  265. print "</div>"; #toolbar
  266. print "</div>"; #pane
  267. print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  268. $sort = validate_field($sort,
  269. ["login", "access_level", "created", "num_feeds", "created", "last_login"], "login");
  270. if ($sort != "login") $sort = "$sort DESC";
  271. $sth = $this->pdo->prepare("SELECT
  272. tu.id,
  273. login,access_level,email,
  274. ".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login,
  275. ".SUBSTRING_FOR_DATE."(created,1,16) as created,
  276. (SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds
  277. FROM
  278. ttrss_users tu
  279. WHERE
  280. (:search = '' OR login LIKE :search) AND tu.id > 0
  281. ORDER BY $sort");
  282. $sth->execute([":search" => $user_search ? "%$user_search%" : ""]);
  283. print "<p><table width=\"100%\" cellspacing=\"0\"
  284. class=\"prefUserList\" id=\"prefUserList\">";
  285. print "<tr class=\"title\">
  286. <td align='center' width=\"5%\">&nbsp;</td>
  287. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
  288. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
  289. <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Subscribed feeds')."</a></td>
  290. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td>
  291. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>";
  292. $lnum = 0;
  293. while ($line = $sth->fetch()) {
  294. $uid = $line["id"];
  295. print "<tr id=\"UMRR-$uid\">";
  296. $line["login"] = htmlspecialchars($line["login"]);
  297. $line["created"] = make_local_datetime($line["created"], false);
  298. $line["last_login"] = make_local_datetime($line["last_login"], false);
  299. print "<td align='center'><input onclick='toggleSelectRow2(this);'
  300. dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
  301. id=\"UMCHK-$uid\"></td>";
  302. $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
  303. print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
  304. if (!$line["email"]) $line["email"] = "&nbsp;";
  305. print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
  306. print "<td $onclick>" . $line["num_feeds"] . "</td>";
  307. print "<td $onclick>" . $line["created"] . "</td>";
  308. print "<td $onclick>" . $line["last_login"] . "</td>";
  309. print "</tr>";
  310. ++$lnum;
  311. }
  312. print "</table>";
  313. if ($lnum == 0) {
  314. if (!$user_search) {
  315. print_warning(__('No users defined.'));
  316. } else {
  317. print_warning(__('No matching users found.'));
  318. }
  319. }
  320. print "</div>"; #pane
  321. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  322. "hook_prefs_tab", "prefUsers");
  323. print "</div>"; #container
  324. }
  325. }