users.php 14 KB

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