]>
Commit | Line | Data |
---|---|---|
66665fba | 1 | <?php |
46da73c2 | 2 | class Pref_Users extends Protected_Handler { |
66665fba AD |
3 | function before() { |
4 | if (parent::before()) { | |
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 | ||
8484ce22 AD |
14 | function csrf_ignore($method) { |
15 | $csrf_ignored = array("index"); | |
16 | ||
17 | return array_search($method, $csrf_ignored) !== false; | |
18 | } | |
19 | ||
66665fba AD |
20 | function userdetails() { |
21 | ||
22 | header("Content-Type: text/xml"); | |
23 | print "<dlg>"; | |
24 | ||
25 | $uid = sprintf("%d", $_REQUEST["id"]); | |
26 | ||
27 | print "<title>".__('User details')."</title>"; | |
28 | ||
29 | print "<content><![CDATA["; | |
30 | ||
31 | $result = db_query($this->link, "SELECT login, | |
32 | ".SUBSTRING_FOR_DATE."(last_login,1,16) AS last_login, | |
33 | access_level, | |
34 | (SELECT COUNT(int_id) FROM ttrss_user_entries | |
35 | WHERE owner_uid = id) AS stored_articles, | |
36 | ".SUBSTRING_FOR_DATE."(created,1,16) AS created | |
37 | FROM ttrss_users | |
38 | WHERE id = '$uid'"); | |
39 | ||
40 | if (db_num_rows($result) == 0) { | |
41 | print "<h1>".__('User not found')."</h1>"; | |
42 | return; | |
43 | } | |
44 | ||
45 | // print "<h1>User Details</h1>"; | |
46 | ||
47 | $login = db_fetch_result($result, 0, "login"); | |
48 | ||
49 | print "<table width='100%'>"; | |
50 | ||
51 | $last_login = make_local_datetime($this->link, | |
52 | db_fetch_result($result, 0, "last_login"), true); | |
53 | ||
54 | $created = make_local_datetime($this->link, | |
55 | db_fetch_result($result, 0, "created"), true); | |
56 | ||
57 | $access_level = db_fetch_result($result, 0, "access_level"); | |
58 | $stored_articles = db_fetch_result($result, 0, "stored_articles"); | |
59 | ||
60 | print "<tr><td>".__('Registered')."</td><td>$created</td></tr>"; | |
61 | print "<tr><td>".__('Last logged in')."</td><td>$last_login</td></tr>"; | |
62 | ||
63 | $result = db_query($this->link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds | |
64 | WHERE owner_uid = '$uid'"); | |
65 | ||
66 | $num_feeds = db_fetch_result($result, 0, "num_feeds"); | |
67 | ||
68 | print "<tr><td>".__('Subscribed feeds count')."</td><td>$num_feeds</td></tr>"; | |
69 | ||
70 | print "</table>"; | |
71 | ||
72 | print "<h1>".__('Subscribed feeds')."</h1>"; | |
73 | ||
74 | $result = db_query($this->link, "SELECT id,title,site_url FROM ttrss_feeds | |
75 | WHERE owner_uid = '$uid' ORDER BY title"); | |
76 | ||
77 | print "<ul class=\"userFeedList\">"; | |
78 | ||
79 | $row_class = "odd"; | |
80 | ||
81 | while ($line = db_fetch_assoc($result)) { | |
82 | ||
83 | $icon_file = ICONS_URL."/".$line["id"].".ico"; | |
84 | ||
85 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
86 | $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">"; | |
87 | } else { | |
88 | $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">"; | |
89 | } | |
90 | ||
91 | print "<li class=\"$row_class\">$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>"; | |
92 | ||
93 | $row_class = $row_class == "even" ? "odd" : "even"; | |
94 | ||
95 | } | |
96 | ||
97 | if (db_num_rows($result) < $num_feeds) { | |
98 | // FIXME - add link to show ALL subscribed feeds here somewhere | |
99 | print "<li><img | |
100 | class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\"> ...</li>"; | |
101 | } | |
102 | ||
103 | print "</ul>"; | |
104 | ||
105 | print "<div align='center'> | |
106 | <button onclick=\"closeInfoBox()\">".__("Close this window"). | |
107 | "</button></div>"; | |
108 | ||
109 | print "]]></content></dlg>"; | |
110 | ||
111 | return; | |
112 | } | |
113 | ||
114 | function edit() { | |
115 | global $access_level_names; | |
116 | ||
117 | header("Content-Type: text/xml"); | |
118 | ||
119 | $id = db_escape_string($_REQUEST["id"]); | |
120 | ||
121 | print "<dlg id=\"$method\">"; | |
122 | print "<title>".__('User Editor')."</title>"; | |
123 | print "<content><![CDATA["; | |
124 | ||
125 | print "<form id=\"user_edit_form\" onsubmit='return false'>"; | |
126 | ||
127 | print "<input type=\"hidden\" name=\"id\" value=\"$id\">"; | |
128 | print "<input type=\"hidden\" name=\"op\" value=\"pref-users\">"; | |
129 | print "<input type=\"hidden\" name=\"method\" value=\"editSave\">"; | |
130 | ||
131 | $result = db_query($this->link, "SELECT * FROM ttrss_users WHERE id = '$id'"); | |
132 | ||
133 | $login = db_fetch_result($result, 0, "login"); | |
134 | $access_level = db_fetch_result($result, 0, "access_level"); | |
135 | $email = db_fetch_result($result, 0, "email"); | |
136 | ||
137 | $sel_disabled = ($id == $_SESSION["uid"]) ? "disabled" : ""; | |
138 | ||
139 | print "<div class=\"dlgSec\">".__("User")."</div>"; | |
140 | print "<div class=\"dlgSecCont\">"; | |
141 | ||
142 | if ($sel_disabled) { | |
143 | print "<input type=\"hidden\" name=\"login\" value=\"$login\">"; | |
144 | print "<input size=\"30\" style=\"font-size : 16px\" | |
145 | onkeypress=\"return filterCR(event, userEditSave)\" $sel_disabled | |
146 | value=\"$login\">"; | |
147 | } else { | |
148 | print "<input size=\"30\" style=\"font-size : 16px\" | |
149 | onkeypress=\"return filterCR(event, userEditSave)\" $sel_disabled | |
150 | name=\"login\" value=\"$login\">"; | |
151 | } | |
152 | ||
153 | print "</div>"; | |
154 | ||
155 | print "<div class=\"dlgSec\">".__("Authentication")."</div>"; | |
156 | print "<div class=\"dlgSecCont\">"; | |
157 | ||
158 | print __('Access level: ') . " "; | |
159 | ||
160 | if (!$sel_disabled) { | |
161 | print_select_hash("access_level", $access_level, $access_level_names, | |
162 | $sel_disabled); | |
163 | } else { | |
164 | print_select_hash("", $access_level, $access_level_names, | |
165 | $sel_disabled); | |
166 | print "<input type=\"hidden\" name=\"access_level\" value=\"$access_level\">"; | |
167 | } | |
168 | ||
169 | print "<br/>"; | |
170 | ||
171 | print __('Change password to') . | |
172 | " <input size=\"20\" onkeypress=\"return filterCR(event, userEditSave)\" | |
173 | name=\"password\">"; | |
174 | ||
175 | print "</div>"; | |
176 | ||
177 | print "<div class=\"dlgSec\">".__("Options")."</div>"; | |
178 | print "<div class=\"dlgSecCont\">"; | |
179 | ||
180 | print __('E-mail: '). | |
181 | " <input size=\"30\" name=\"email\" onkeypress=\"return filterCR(event, userEditSave)\" | |
182 | value=\"$email\">"; | |
183 | ||
184 | print "</div>"; | |
185 | ||
186 | print "</table>"; | |
187 | ||
188 | print "</form>"; | |
189 | ||
190 | print "<div class=\"dlgButtons\"> | |
191 | <button onclick=\"return userEditSave()\">". | |
192 | __('Save')."</button> | |
193 | <button onclick=\"return userEditCancel()\">". | |
194 | __('Cancel')."</button></div>"; | |
195 | ||
196 | print "]]></content></dlg>"; | |
197 | ||
198 | return; | |
199 | } | |
200 | ||
201 | function editSave() { | |
202 | $login = db_escape_string(trim($_REQUEST["login"])); | |
203 | $uid = db_escape_string($_REQUEST["id"]); | |
204 | $access_level = (int) $_REQUEST["access_level"]; | |
205 | $email = db_escape_string(trim($_REQUEST["email"])); | |
206 | $password = db_escape_string(trim($_REQUEST["password"])); | |
207 | ||
208 | if ($password) { | |
8db5d8ea | 209 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); |
098df83b AD |
210 | $pwd_hash = encrypt_password($password, $salt, true); |
211 | $pass_query_part = "pwd_hash = '$pwd_hash', salt = '$salt',"; | |
66665fba AD |
212 | } else { |
213 | $pass_query_part = ""; | |
214 | } | |
215 | ||
216 | db_query($this->link, "UPDATE ttrss_users SET $pass_query_part login = '$login', | |
217 | access_level = '$access_level', email = '$email' WHERE id = '$uid'"); | |
218 | ||
219 | } | |
220 | ||
221 | function remove() { | |
222 | $ids = split(",", db_escape_string($_REQUEST["ids"])); | |
223 | ||
224 | foreach ($ids as $id) { | |
225 | if ($id != $_SESSION["uid"] && $id != 1) { | |
226 | db_query($this->link, "DELETE FROM ttrss_tags WHERE owner_uid = '$id'"); | |
227 | db_query($this->link, "DELETE FROM ttrss_feeds WHERE owner_uid = '$id'"); | |
228 | db_query($this->link, "DELETE FROM ttrss_users WHERE id = '$id'"); | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
233 | function add() { | |
234 | ||
235 | $login = db_escape_string(trim($_REQUEST["login"])); | |
236 | $tmp_user_pwd = make_password(8); | |
8db5d8ea | 237 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); |
098df83b | 238 | $pwd_hash = encrypt_password($tmp_user_pwd, $salt, true); |
66665fba AD |
239 | |
240 | $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE | |
241 | login = '$login'"); | |
242 | ||
243 | if (db_num_rows($result) == 0) { | |
244 | ||
245 | db_query($this->link, "INSERT INTO ttrss_users | |
098df83b AD |
246 | (login,pwd_hash,access_level,last_login,created, salt) |
247 | VALUES ('$login', '$pwd_hash', 0, null, NOW(), '$salt')"); | |
66665fba AD |
248 | |
249 | ||
250 | $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE | |
251 | login = '$login' AND pwd_hash = '$pwd_hash'"); | |
252 | ||
253 | if (db_num_rows($result) == 1) { | |
254 | ||
255 | $new_uid = db_fetch_result($result, 0, "id"); | |
256 | ||
257 | print format_notice(T_sprintf("Added user <b>%s</b> with password <b>%s</b>", | |
258 | $login, $tmp_user_pwd)); | |
259 | ||
260 | initialize_user($this->link, $new_uid); | |
261 | ||
262 | } else { | |
263 | ||
264 | print format_warning(T_sprintf("Could not create user <b>%s</b>", $login)); | |
265 | ||
266 | } | |
267 | } else { | |
268 | print format_warning(T_sprintf("User <b>%s</b> already exists.", $login)); | |
269 | } | |
270 | } | |
271 | ||
272 | function resetPass() { | |
273 | ||
274 | $uid = db_escape_string($_REQUEST["id"]); | |
275 | ||
276 | $result = db_query($this->link, "SELECT login,email | |
277 | FROM ttrss_users WHERE id = '$uid'"); | |
278 | ||
279 | $login = db_fetch_result($result, 0, "login"); | |
280 | $email = db_fetch_result($result, 0, "email"); | |
098df83b AD |
281 | $salt = db_fetch_result($result, 0, "salt"); |
282 | ||
8db5d8ea | 283 | $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250); |
66665fba | 284 | $tmp_user_pwd = make_password(8); |
66665fba | 285 | |
098df83b AD |
286 | $pwd_hash = encrypt_password($tmp_user_pwd, $new_salt, true); |
287 | ||
288 | db_query($this->link, "UPDATE ttrss_users SET pwd_hash = '$pwd_hash', salt = '$new_salt' | |
66665fba AD |
289 | WHERE id = '$uid'"); |
290 | ||
291 | print T_sprintf("Changed password of user <b>%s</b> | |
292 | to <b>%s</b>", $login, $tmp_user_pwd); | |
293 | ||
294 | require_once 'lib/phpmailer/class.phpmailer.php'; | |
295 | ||
296 | if ($email) { | |
297 | print " "; | |
298 | print T_sprintf("Notifying <b>%s</b>.", $email); | |
299 | ||
300 | require_once "lib/MiniTemplator.class.php"; | |
301 | ||
302 | $tpl = new MiniTemplator; | |
303 | ||
304 | $tpl->readTemplateFromFile("templates/resetpass_template.txt"); | |
305 | ||
306 | $tpl->setVariable('LOGIN', $login); | |
307 | $tpl->setVariable('NEWPASS', $tmp_user_pwd); | |
308 | ||
309 | $tpl->addBlock('message'); | |
310 | ||
311 | $message = ""; | |
312 | ||
313 | $tpl->generateOutputToString($message); | |
314 | ||
315 | $mail = new PHPMailer(); | |
316 | ||
317 | $mail->PluginDir = "lib/phpmailer/"; | |
318 | $mail->SetLanguage("en", "lib/phpmailer/language/"); | |
319 | ||
320 | $mail->CharSet = "UTF-8"; | |
321 | ||
09e8bdfd AD |
322 | $mail->From = SMTP_FROM_ADDRESS; |
323 | $mail->FromName = SMTP_FROM_NAME; | |
66665fba AD |
324 | $mail->AddAddress($email, $login); |
325 | ||
09e8bdfd AD |
326 | if (SMTP_HOST) { |
327 | $mail->Host = SMTP_HOST; | |
66665fba | 328 | $mail->Mailer = "smtp"; |
09e8bdfd AD |
329 | $mail->SMTPAuth = SMTP_LOGIN != ''; |
330 | $mail->Username = SMTP_LOGIN; | |
331 | $mail->Password = SMTP_PASSWORD; | |
66665fba AD |
332 | } |
333 | ||
334 | $mail->IsHTML(false); | |
335 | $mail->Subject = __("[tt-rss] Password change notification"); | |
336 | $mail->Body = $message; | |
337 | ||
338 | $rc = $mail->Send(); | |
339 | ||
340 | if (!$rc) print_error($mail->ErrorInfo); | |
341 | } | |
342 | ||
343 | print "</div>"; | |
344 | } | |
345 | ||
346 | function index() { | |
347 | ||
348 | global $access_level_names; | |
349 | ||
350 | print "<div id=\"pref-user-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">"; | |
351 | print "<div id=\"pref-user-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">"; | |
352 | ||
353 | print "<div id=\"pref-user-toolbar\" dojoType=\"dijit.Toolbar\">"; | |
354 | ||
355 | $user_search = db_escape_string($_REQUEST["search"]); | |
356 | ||
357 | if (array_key_exists("search", $_REQUEST)) { | |
358 | $_SESSION["prefs_user_search"] = $user_search; | |
359 | } else { | |
360 | $user_search = $_SESSION["prefs_user_search"]; | |
361 | } | |
362 | ||
363 | print "<div style='float : right; padding-right : 4px;'> | |
364 | <input dojoType=\"dijit.form.TextBox\" id=\"user_search\" size=\"20\" type=\"search\" | |
365 | value=\"$user_search\"> | |
366 | <button dojoType=\"dijit.form.Button\" onclick=\"javascript:updateUsersList()\">". | |
367 | __('Search')."</button> | |
368 | </div>"; | |
369 | ||
370 | $sort = db_escape_string($_REQUEST["sort"]); | |
371 | ||
372 | if (!$sort || $sort == "undefined") { | |
373 | $sort = "login"; | |
374 | } | |
375 | ||
376 | print "<div dojoType=\"dijit.form.DropDownButton\">". | |
377 | "<span>" . __('Select')."</span>"; | |
378 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; | |
379 | print "<div onclick=\"selectTableRows('prefUserList', 'all')\" | |
380 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; | |
381 | print "<div onclick=\"selectTableRows('prefUserList', 'none')\" | |
382 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; | |
383 | print "</div></div>"; | |
384 | ||
385 | print "<button dojoType=\"dijit.form.Button\" onclick=\"javascript:addUser()\">".__('Create user')."</button>"; | |
386 | ||
387 | print " | |
388 | <button dojoType=\"dijit.form.Button\" onclick=\"javascript:selectedUserDetails()\">". | |
389 | __('Details')."</button dojoType=\"dijit.form.Button\"> | |
390 | <button dojoType=\"dijit.form.Button\" onclick=\"javascript:editSelectedUser()\">". | |
391 | __('Edit')."</button dojoType=\"dijit.form.Button\"> | |
392 | <button dojoType=\"dijit.form.Button\" onclick=\"javascript:removeSelectedUsers()\">". | |
393 | __('Remove')."</button dojoType=\"dijit.form.Button\"> | |
394 | <button dojoType=\"dijit.form.Button\" onclick=\"javascript:resetSelectedUserPass()\">". | |
395 | __('Reset password')."</button dojoType=\"dijit.form.Button\">"; | |
396 | ||
397 | print "</div>"; #toolbar | |
398 | print "</div>"; #pane | |
399 | print "<div id=\"pref-user-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">"; | |
400 | ||
401 | print "<div id=\"sticky-status-msg\"></div>"; | |
402 | ||
403 | if ($user_search) { | |
404 | ||
405 | $user_search = split(" ", $user_search); | |
406 | $tokens = array(); | |
407 | ||
408 | foreach ($user_search as $token) { | |
409 | $token = trim($token); | |
410 | array_push($tokens, "(UPPER(login) LIKE UPPER('%$token%'))"); | |
411 | } | |
412 | ||
413 | $user_search_query = "(" . join($tokens, " AND ") . ") AND "; | |
414 | ||
415 | } else { | |
416 | $user_search_query = ""; | |
417 | } | |
418 | ||
419 | $result = db_query($this->link, "SELECT | |
420 | id,login,access_level,email, | |
421 | ".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login, | |
422 | ".SUBSTRING_FOR_DATE."(created,1,16) as created | |
423 | FROM | |
424 | ttrss_users | |
425 | WHERE | |
426 | $user_search_query | |
427 | id > 0 | |
428 | ORDER BY $sort"); | |
429 | ||
430 | if (db_num_rows($result) > 0) { | |
431 | ||
432 | print "<p><table width=\"100%\" cellspacing=\"0\" | |
433 | class=\"prefUserList\" id=\"prefUserList\">"; | |
434 | ||
435 | print "<tr class=\"title\"> | |
436 | <td align='center' width=\"5%\"> </td> | |
437 | <td width=''><a href=\"#\" onclick=\"updateUsersList('login')\">".__('Login')."</a></td> | |
438 | <td width='20%'><a href=\"#\" onclick=\"updateUsersList('access_level')\">".__('Access Level')."</a></td> | |
439 | <td width='20%'><a href=\"#\" onclick=\"updateUsersList('created')\">".__('Registered')."</a></td> | |
440 | <td width='20%'><a href=\"#\" onclick=\"updateUsersList('last_login')\">".__('Last login')."</a></td></tr>"; | |
441 | ||
442 | $lnum = 0; | |
443 | ||
444 | while ($line = db_fetch_assoc($result)) { | |
445 | ||
446 | $class = ($lnum % 2) ? "even" : "odd"; | |
447 | ||
448 | $uid = $line["id"]; | |
449 | ||
450 | print "<tr class=\"$class\" id=\"UMRR-$uid\">"; | |
451 | ||
452 | $line["login"] = htmlspecialchars($line["login"]); | |
453 | ||
454 | $line["created"] = make_local_datetime($this->link, $line["created"], false); | |
455 | $line["last_login"] = make_local_datetime($this->link, $line["last_login"], false); | |
456 | ||
457 | print "<td align='center'><input onclick='toggleSelectRow(this);' | |
458 | type=\"checkbox\" id=\"UMCHK-$uid\"></td>"; | |
459 | ||
460 | $onclick = "onclick='editUser($uid, event)' title='".__('Click to edit')."'"; | |
461 | ||
462 | print "<td $onclick>" . $line["login"] . "</td>"; | |
463 | ||
464 | if (!$line["email"]) $line["email"] = " "; | |
465 | ||
466 | print "<td $onclick>" . $access_level_names[$line["access_level"]] . "</td>"; | |
467 | print "<td $onclick>" . $line["created"] . "</td>"; | |
468 | print "<td $onclick>" . $line["last_login"] . "</td>"; | |
469 | ||
470 | print "</tr>"; | |
471 | ||
472 | ++$lnum; | |
473 | } | |
474 | ||
475 | print "</table>"; | |
476 | ||
477 | } else { | |
478 | print "<p>"; | |
479 | if (!$user_search) { | |
480 | print_warning(__('No users defined.')); | |
481 | } else { | |
482 | print_warning(__('No matching users found.')); | |
483 | } | |
484 | print "</p>"; | |
485 | ||
486 | } | |
487 | ||
488 | print "</div>"; #pane | |
489 | print "</div>"; #container | |
490 | ||
491 | } | |
492 | ||
493 | } | |
494 | ?> |