users.php 14 KB

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