users.php 14 KB

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