users.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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) $this->dbh->escape_string($_REQUEST["id"]);
  23. print_hidden("id", "$id");
  24. print_hidden("op", "pref-users");
  25. print_hidden("method", "editSave");
  26. $result = $this->dbh->query("SELECT * FROM ttrss_users WHERE id = '$id'");
  27. $login = $this->dbh->fetch_result($result, 0, "login");
  28. $access_level = $this->dbh->fetch_result($result, 0, "access_level");
  29. $email = $this->dbh->fetch_result($result, 0, "email");
  30. $sel_disabled = ($id == $_SESSION["uid"] || $login == "admin") ? "disabled" : "";
  31. print "<div class=\"dlgSec\">".__("User")."</div>";
  32. print "<div class=\"dlgSecCont\">";
  33. if ($sel_disabled) {
  34. print_hidden("login", "$login");
  35. }
  36. print "<input size=\"30\" style=\"font-size : 16px\"
  37. dojoType=\"dijit.form.ValidationTextBox\" required=\"1\"
  38. $sel_disabled
  39. name=\"login\" value=\"$login\">";
  40. print "</div>";
  41. print "<div class=\"dlgSec\">".__("Authentication")."</div>";
  42. print "<div class=\"dlgSecCont\">";
  43. print __('Access level: ') . " ";
  44. if (!$sel_disabled) {
  45. print_select_hash("access_level", $access_level, $access_level_names,
  46. "dojoType=\"dijit.form.Select\" $sel_disabled");
  47. } else {
  48. print_select_hash("", $access_level, $access_level_names,
  49. "dojoType=\"dijit.form.Select\" $sel_disabled");
  50. print_hidden("access_level", "$access_level");
  51. }
  52. print "<hr/>";
  53. print "<input dojoType=\"dijit.form.TextBox\" type=\"password\" size=\"20\" placeholder=\"Change password\"
  54. name=\"password\">";
  55. print "</div>";
  56. print "<div class=\"dlgSec\">".__("Options")."</div>";
  57. print "<div class=\"dlgSecCont\">";
  58. print "<input dojoType=\"dijit.form.TextBox\" size=\"30\" name=\"email\" placeholder=\"E-mail\"
  59. value=\"$email\">";
  60. print "</div>";
  61. print "</table>";
  62. print "</form>";
  63. print '</div>'; #tab
  64. print "<div href=\"backend.php?op=pref-users&method=userdetails&id=$id\"
  65. dojoType=\"dijit.layout.ContentPane\" title=\"".__('User details')."\">";
  66. print '</div>';
  67. print '</div>';
  68. print "<div class=\"dlgButtons\">
  69. <button dojoType=\"dijit.form.Button\" type=\"submit\">".
  70. __('Save')."</button>
  71. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('userEditDlg').hide()\">".
  72. __('Cancel')."</button></div>";
  73. return;
  74. }
  75. function userdetails() {
  76. $id = (int) $this->dbh->escape_string($_REQUEST["id"]);
  77. $result = $this->dbh->query("SELECT login,
  78. ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login,
  79. access_level,
  80. (SELECT COUNT(int_id) FROM ttrss_user_entries
  81. WHERE owner_uid = id) AS stored_articles,
  82. ".SUBSTRING_FOR_DATE."(created,1,16) AS created
  83. FROM ttrss_users
  84. WHERE id = '$id'");
  85. if ($this->dbh->num_rows($result) == 0) {
  86. print "<h1>".__('User not found')."</h1>";
  87. return;
  88. }
  89. print "<table width='100%'>";
  90. $last_login = make_local_datetime(
  91. $this->dbh->fetch_result($result, 0, "last_login"), true);
  92. $created = make_local_datetime(
  93. $this->dbh->fetch_result($result, 0, "created"), true);
  94. $stored_articles = $this->dbh->fetch_result($result, 0, "stored_articles");
  95. print "<tr><td>".__('Registered')."</td><td>$created</td></tr>";
  96. print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>";
  97. $result = $this->dbh->query("SELECT COUNT(id) as num_feeds FROM ttrss_feeds
  98. WHERE owner_uid = '$id'");
  99. $num_feeds = $this->dbh->fetch_result($result, 0, "num_feeds");
  100. print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>";
  101. print "<tr><td>".__('Stored articles')."</td><td>$stored_articles</td></tr>";
  102. print "</table>";
  103. print "<h1>".__('Subscribed feeds')."</h1>";
  104. $result = $this->dbh->query("SELECT id,title,site_url FROM ttrss_feeds
  105. WHERE owner_uid = '$id' ORDER BY title");
  106. print "<ul class=\"userFeedList\">";
  107. while ($line = $this->dbh->fetch_assoc($result)) {
  108. $icon_file = ICONS_URL."/".$line["id"].".ico";
  109. if (file_exists($icon_file) && filesize($icon_file) > 0) {
  110. $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
  111. } else {
  112. $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
  113. }
  114. print "<li>$feed_icon&nbsp;<a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
  115. }
  116. if ($this->dbh->num_rows($result) < $num_feeds) {
  117. // FIXME - add link to show ALL subscribed feeds here somewhere
  118. print "<li><img
  119. class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">&nbsp;...</li>";
  120. }
  121. print "</ul>";
  122. }
  123. function editSave() {
  124. $login = $this->dbh->escape_string(trim($_REQUEST["login"]));
  125. $uid = $this->dbh->escape_string($_REQUEST["id"]);
  126. $access_level = (int) $_REQUEST["access_level"];
  127. $email = $this->dbh->escape_string(trim($_REQUEST["email"]));
  128. $password = $_REQUEST["password"];
  129. if ($password) {
  130. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  131. $pwd_hash = encrypt_password($password, $salt, true);
  132. $pass_query_part = "pwd_hash = '$pwd_hash', salt = '$salt',";
  133. } else {
  134. $pass_query_part = "";
  135. }
  136. $this->dbh->query("UPDATE ttrss_users SET $pass_query_part login = '$login',
  137. access_level = '$access_level', email = '$email', otp_enabled = false
  138. WHERE id = '$uid'");
  139. }
  140. function remove() {
  141. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  142. foreach ($ids as $id) {
  143. if ($id != $_SESSION["uid"] && $id != 1) {
  144. $this->dbh->query("DELETE FROM ttrss_tags WHERE owner_uid = '$id'");
  145. $this->dbh->query("DELETE FROM ttrss_feeds WHERE owner_uid = '$id'");
  146. $this->dbh->query("DELETE FROM ttrss_users WHERE id = '$id'");
  147. }
  148. }
  149. }
  150. function add() {
  151. $login = $this->dbh->escape_string(trim($_REQUEST["login"]));
  152. $tmp_user_pwd = make_password(8);
  153. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  154. $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true);
  155. $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
  156. login = '$login'");
  157. if ($this->dbh->num_rows($result) == 0) {
  158. $this->dbh->query("INSERT INTO ttrss_users
  159. (login,pwd_hash,access_level,last_login,created, salt)
  160. VALUES ('$login', '$pwd_hash', 0, null, NOW(), '$salt')");
  161. $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE
  162. login = '$login' AND pwd_hash = '$pwd_hash'");
  163. if ($this->dbh->num_rows($result) == 1) {
  164. $new_uid = $this->dbh->fetch_result($result, 0, "id");
  165. print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>",
  166. $login, $tmp_user_pwd));
  167. initialize_user($new_uid);
  168. } else {
  169. print format_warning(T_sprintf("Could not create user <b>%s</b>", $login));
  170. }
  171. } else {
  172. print format_warning(T_sprintf("User <b>%s</b> already exists.", $login));
  173. }
  174. }
  175. static function resetUserPassword($uid, $show_password) {
  176. $result = db_query("SELECT login,email
  177. FROM ttrss_users WHERE id = '$uid'");
  178. $login = db_fetch_result($result, 0, "login");
  179. $email = db_fetch_result($result, 0, "email");
  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', otp_enabled = false
  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=\"editSelectedUser()\">".
  243. __('Edit')."</button dojoType=\"dijit.form.Button\">
  244. <button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedUsers()\">".
  245. __('Remove')."</button dojoType=\"dijit.form.Button\">
  246. <button dojoType=\"dijit.form.Button\" onclick=\"resetSelectedUserPass()\">".
  247. __('Reset password')."</button dojoType=\"dijit.form.Button\">";
  248. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
  249. "hook_prefs_tab_section", "prefUsersToolbar");
  250. print "</div>"; #toolbar
  251. print "</div>"; #pane
  252. print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  253. print "<div id=\"sticky-status-msg\"></div>";
  254. if ($user_search) {
  255. $user_search = explode(" ", $user_search);
  256. $tokens = array();
  257. foreach ($user_search as $token) {
  258. $token = trim($token);
  259. array_push($tokens, "(UPPER(login) LIKE UPPER('%$token%'))");
  260. }
  261. $user_search_query = "(" . join($tokens, " AND ") . ") AND ";
  262. } else {
  263. $user_search_query = "";
  264. }
  265. $result = $this->dbh->query("SELECT
  266. tu.id,
  267. login,access_level,email,
  268. ".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login,
  269. ".SUBSTRING_FOR_DATE."(created,1,16) as created,
  270. (SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds
  271. FROM
  272. ttrss_users tu
  273. WHERE
  274. $user_search_query
  275. tu.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='20%'><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td>
  283. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td>
  284. <td width='10%'><a href=\"#\" onclick=\"updateUsersList('num_feeds')\">".__('Subscribed feeds')."</a></td>
  285. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td>
  286. <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>";
  287. $lnum = 0;
  288. while ($line = $this->dbh->fetch_assoc($result)) {
  289. $uid = $line["id"];
  290. print "<tr id=\"UMRR-$uid\">";
  291. $line["login"] = htmlspecialchars($line["login"]);
  292. $line["created"] = make_local_datetime($line["created"], false);
  293. $line["last_login"] = make_local_datetime($line["last_login"], false);
  294. print "<td align='center'><input onclick='toggleSelectRow2(this);'
  295. dojoType=\"dijit.form.CheckBox\" type=\"checkbox\"
  296. id=\"UMCHK-$uid\"></td>";
  297. $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'";
  298. print "<td $onclick><img src='images/user.png' class='markedPic' alt=''> " . $line["login"] . "</td>";
  299. if (!$line["email"]) $line["email"] = "&nbsp;";
  300. print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>";
  301. print "<td $onclick>" . $line["num_feeds"] . "</td>";
  302. print "<td $onclick>" . $line["created"] . "</td>";
  303. print "<td $onclick>" . $line["last_login"] . "</td>";
  304. print "</tr>";
  305. ++$lnum;
  306. }
  307. print "</table>";
  308. } else {
  309. print "<p>";
  310. if (!$user_search) {
  311. print_warning(__('No users defined.'));
  312. } else {
  313. print_warning(__('No matching users found.'));
  314. }
  315. print "</p>";
  316. }
  317. print "</div>"; #pane
  318. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  319. "hook_prefs_tab", "prefUsers");
  320. print "</div>"; #container
  321. }
  322. }