]> git.wh0rd.org - tt-rss.git/blame - classes/pref_prefs.php
save module user authenticated with, only allow password change if module is 'internal'
[tt-rss.git] / classes / pref_prefs.php
CommitLineData
1395083e 1<?php
46da73c2 2class Pref_Prefs extends Protected_Handler {
1395083e 3
8484ce22
AD
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
1395083e
AD
10 function changepassword() {
11
12 $old_pw = $_POST["old_password"];
13 $new_pw = $_POST["new_password"];
14 $con_pw = $_POST["confirm_password"];
15
16 if ($old_pw == "") {
17 print "ERROR: ".__("Old password cannot be blank.");
18 return;
19 }
20
21 if ($new_pw == "") {
22 print "ERROR: ".__("New password cannot be blank.");
23 return;
24 }
25
26 if ($new_pw != $con_pw) {
27 print "ERROR: ".__("Entered passwords do not match.");
28 return;
29 }
30
098df83b
AD
31 $result = db_query($this->link, "SELECT salt FROM ttrss_users WHERE
32 id = " . $_SESSION['uid']);
1395083e 33
098df83b 34 $salt = db_fetch_result($result, 0, "salt");
1395083e 35
098df83b
AD
36 if (!$salt) {
37 $old_pw_hash1 = encrypt_password($old_pw);
38 $old_pw_hash2 = encrypt_password($old_pw, $_SESSION["name"]);
1395083e 39
098df83b
AD
40 $query = "SELECT id FROM ttrss_users WHERE
41 id = ".$_SESSION['uid']." AND (pwd_hash = '$old_pw_hash1' OR
42 pwd_hash = '$old_pw_hash2')";
1395083e 43
098df83b
AD
44 } else {
45 $old_pw_hash = encrypt_password($old_pw, $salt, true);
1395083e 46
098df83b
AD
47 $query = "SELECT id FROM ttrss_users WHERE
48 id = ".$_SESSION['uid']." AND pwd_hash = '$old_pw_hash'";
49 }
1395083e 50
098df83b 51 $result = db_query($this->link, $query);
1395083e 52
098df83b
AD
53 if (db_num_rows($result) == 1) {
54
8db5d8ea 55 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
098df83b 56 $new_pw_hash = encrypt_password($new_pw, $new_salt, true);
1395083e 57
098df83b
AD
58 db_query($this->link, "UPDATE ttrss_users SET
59 pwd_hash = '$new_pw_hash', salt = '$new_salt'
60 WHERE id = ".$_SESSION['uid']);
1395083e 61
098df83b
AD
62 $_SESSION["pwd_hash"] = $new_pw_hash;
63
64 print __("Password has been changed.");
65 } else {
66 print "ERROR: ".__('Old password is incorrect.');
67 }
1395083e
AD
68 }
69
70 function saveconfig() {
71
72 $_SESSION["prefs_cache"] = false;
73
74 $orig_theme = get_pref($this->link, "_THEME_ID");
75
76 foreach (array_keys($_POST) as $pref_name) {
77
78 $pref_name = db_escape_string($pref_name);
79 $value = db_escape_string($_POST[$pref_name]);
80
1b9b19af
AD
81 if ($pref_name == 'DIGEST_PREFERRED_TIME') {
82 if (get_pref($this->link, 'DIGEST_PREFERRED_TIME') != $value) {
83
84 db_query($this->link, "UPDATE ttrss_users SET
85 last_digest_sent = NULL WHERE id = " . $_SESSION['uid']);
86
87 }
88 }
89
1395083e
AD
90 set_pref($this->link, $pref_name, $value);
91
92 }
93
94 if ($orig_theme != get_pref($this->link, "_THEME_ID")) {
95 print "PREFS_THEME_CHANGED";
96 } else {
97 print __("The configuration was saved.");
98 }
99 }
100
101 function getHelp() {
102
103 $pref_name = db_escape_string($_REQUEST["pn"]);
104
105 $result = db_query($this->link, "SELECT help_text FROM ttrss_prefs
106 WHERE pref_name = '$pref_name'");
107
108 if (db_num_rows($result) > 0) {
109 $help_text = db_fetch_result($result, 0, "help_text");
110 print $help_text;
111 } else {
112 printf(__("Unknown option: %s"), $pref_name);
113 }
114 }
115
116 function changeemail() {
117
118 $email = db_escape_string($_POST["email"]);
119 $full_name = db_escape_string($_POST["full_name"]);
120
121 $active_uid = $_SESSION["uid"];
122
123 db_query($this->link, "UPDATE ttrss_users SET email = '$email',
124 full_name = '$full_name' WHERE id = '$active_uid'");
125
126 print __("Your personal data has been saved.");
127
128 return;
129 }
130
131 function resetconfig() {
132
133 $_SESSION["prefs_op_result"] = "reset-to-defaults";
134
135 if ($_SESSION["profile"]) {
136 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
137 } else {
138 $profile_qpart = "profile IS NULL";
139 }
140
141 db_query($this->link, "DELETE FROM ttrss_user_prefs
142 WHERE $profile_qpart AND owner_uid = ".$_SESSION["uid"]);
143
144 initialize_user_prefs($this->link, $_SESSION["uid"], $_SESSION["profile"]);
145
146 print "PREFS_THEME_CHANGED";
147 }
148
149 function index() {
150
151 global $access_level_names;
152
153 $prefs_blacklist = array("HIDE_READ_FEEDS", "FEEDS_SORT_BY_UNREAD",
154 "STRIP_UNSAFE_TAGS");
155
156 $profile_blacklist = array("ALLOW_DUPLICATE_POSTS", "PURGE_OLD_DAYS",
157 "PURGE_UNREAD_ARTICLES", "DIGEST_ENABLE", "DIGEST_CATCHUP",
158 "BLACKLISTED_TAGS", "ENABLE_API_ACCESS", "UPDATE_POST_ON_CHECKSUM_CHANGE",
159 "DEFAULT_UPDATE_INTERVAL", "USER_TIMEZONE", "SORT_HEADLINES_BY_FEED_DATE",
6d9455e9 160 "SSL_CERT_SERIAL", "DIGEST_PREFERRED_TIME");
1395083e
AD
161
162
d4c64ecc 163 $_SESSION["prefs_op_result"] = "";
1395083e 164
d4c64ecc
AD
165 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
166 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Personal data / Authentication')."\">";
1395083e 167
d4c64ecc 168 print "<form dojoType=\"dijit.form.Form\" id=\"changeUserdataForm\">";
1395083e 169
d4c64ecc
AD
170 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
171 evt.preventDefault();
172 if (this.validate()) {
173 notify_progress('Saving data...', true);
1395083e 174
d4c64ecc
AD
175 new Ajax.Request('backend.php', {
176 parameters: dojo.objectToQuery(this.getValues()),
177 onComplete: function(transport) {
178 notify_callback2(transport);
179 } });
1395083e 180
d4c64ecc
AD
181 }
182 </script>";
1395083e 183
d4c64ecc 184 print "<table width=\"100%\" class=\"prefPrefsList\">";
1395083e 185
d4c64ecc
AD
186 $result = db_query($this->link, "SELECT email,full_name,
187 access_level FROM ttrss_users
188 WHERE id = ".$_SESSION["uid"]);
1395083e 189
d4c64ecc
AD
190 $email = htmlspecialchars(db_fetch_result($result, 0, "email"));
191 $full_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
1395083e 192
d4c64ecc
AD
193 print "<tr><td width=\"40%\">".__('Full name')."</td>";
194 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
195 value=\"$full_name\"></td></tr>";
1395083e 196
d4c64ecc
AD
197 print "<tr><td width=\"40%\">".__('E-mail')."</td>";
198 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"email\" required=\"1\" value=\"$email\"></td></tr>";
1395083e 199
0d421af8 200 if (!SINGLE_USER_MODE && !$_SESSION["hide_hello"]) {
949b1a94 201
d4c64ecc
AD
202 $access_level = db_fetch_result($result, 0, "access_level");
203 print "<tr><td width=\"40%\">".__('Access level')."</td>";
204 print "<td>" . $access_level_names[$access_level] . "</td></tr>";
205 }
1395083e 206
d4c64ecc 207 print "</table>";
1395083e 208
d4c64ecc
AD
209 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
210 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"changeemail\">";
1395083e 211
d4c64ecc
AD
212 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
213 __("Save data")."</button>";
1395083e 214
d4c64ecc 215 print "</form>";
1395083e 216
200e0d4e 217 if (!SINGLE_USER_MODE && $_SESSION["auth_module"] == 'internal') {
1395083e 218
1395083e
AD
219 $result = db_query($this->link, "SELECT id FROM ttrss_users
220 WHERE id = ".$_SESSION["uid"]." AND pwd_hash
221 = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
222
223 if (db_num_rows($result) != 0) {
224 print format_warning(__("Your password is at default value, please change it."), "default_pass_warning");
225 }
226
227 print "<form dojoType=\"dijit.form.Form\">";
228
229 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
230 evt.preventDefault();
231 if (this.validate()) {
232 notify_progress('Changing password...', true);
233
234 new Ajax.Request('backend.php', {
235 parameters: dojo.objectToQuery(this.getValues()),
236 onComplete: function(transport) {
237 notify('');
238 if (transport.responseText.indexOf('ERROR: ') == 0) {
239 notify_error(transport.responseText.replace('ERROR: ', ''));
240 } else {
241 notify_info(transport.responseText);
242 var warn = $('default_pass_warning');
243 if (warn) Element.hide(warn);
244 }
245 }});
246 this.reset();
247 }
248 </script>";
249
250 print "<table width=\"100%\" class=\"prefPrefsList\">";
251
252 print "<tr><td width=\"40%\">".__("Old password")."</td>";
253 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"old_password\"></td></tr>";
254
255 print "<tr><td width=\"40%\">".__("New password")."</td>";
256
257 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
258 name=\"new_password\"></td></tr>";
259
260 print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
261
262 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"confirm_password\"></td></tr>";
263
264 print "</table>";
265
266 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
267 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"changepassword\">";
268
269 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
270 __("Change password")."</button>";
271
272 print "</form>";
273
1395083e
AD
274 }
275
d4c64ecc
AD
276 print "</div>"; #pane
277
1395083e
AD
278 print "<div dojoType=\"dijit.layout.AccordionPane\" selected=\"true\" title=\"".__('Preferences')."\">";
279
280 print "<form dojoType=\"dijit.form.Form\" id=\"changeSettingsForm\">";
281
282 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
283 evt.preventDefault();
284 if (this.validate()) {
285 console.log(dojo.objectToQuery(this.getValues()));
286
287 new Ajax.Request('backend.php', {
288 parameters: dojo.objectToQuery(this.getValues()),
289 onComplete: function(transport) {
290 var msg = transport.responseText;
291 if (msg.match('PREFS_THEME_CHANGED')) {
292 window.location.reload();
293 } else {
294 notify_info(msg);
295 }
296 } });
297 }
298 </script>";
299
300 print '<div dojoType="dijit.layout.BorderContainer" gutters="false">';
301
302 print '<div dojoType="dijit.layout.ContentPane" region="center" style="overflow-y : auto">';
303
304 if ($_SESSION["profile"]) {
305 print_notice("Some preferences are only available in default profile.");
306 }
307
308 if ($_SESSION["profile"]) {
309 initialize_user_prefs($this->link, $_SESSION["uid"], $_SESSION["profile"]);
310 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
311 } else {
312 initialize_user_prefs($this->link, $_SESSION["uid"]);
313 $profile_qpart = "profile IS NULL";
314 }
315
ddb575c7 316 $result = db_query($this->link, "SELECT DISTINCT
1395083e
AD
317 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
318 section_name,def_value,section_id
319 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
320 WHERE type_id = ttrss_prefs_types.id AND
321 $profile_qpart AND
322 section_id = ttrss_prefs_sections.id AND
323 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
324 short_desc != '' AND
325 owner_uid = ".$_SESSION["uid"]."
326 ORDER BY section_id,short_desc");
327
328 $lnum = 0;
329
330 $active_section = "";
331
332 while ($line = db_fetch_assoc($result)) {
333
334 if (in_array($line["pref_name"], $prefs_blacklist)) {
335 continue;
336 }
337
338 if ($_SESSION["profile"] && in_array($line["pref_name"],
339 $profile_blacklist)) {
340 continue;
341 }
342
343 if ($active_section != $line["section_name"]) {
344
345 if ($active_section != "") {
346 print "</table>";
347 }
348
349 print "<table width=\"100%\" class=\"prefPrefsList\">";
350
351 $active_section = $line["section_name"];
352
353 print "<tr><td colspan=\"3\"><h3>".__($active_section)."</h3></td></tr>";
354
355 if ($line["section_id"] == 2) {
356 print "<tr><td width=\"40%\">".__("Select theme")."</td>";
357
358 $user_theme = get_pref($this->link, "_THEME_ID");
359 $themes = get_all_themes();
360
361 print "<td><select name=\"_THEME_ID\" dojoType=\"dijit.form.Select\">";
362 print "<option value='Default'>".__('Default')."</option>";
363 print "<option value='----------------' disabled=\"1\">--------</option>";
364
365 foreach ($themes as $t) {
366 $base = $t['base'];
367 $name = $t['name'];
368
369 if ($base == $user_theme) {
370 $selected = "selected=\"1\"";
371 } else {
372 $selected = "";
373 }
374
375 print "<option $selected value='$base'>$name</option>";
376
377 }
378
379 print "</select></td></tr>";
380 }
381 $lnum = 0;
382 }
383
384 print "<tr>";
385
386 $type_name = $line["type_name"];
387 $pref_name = $line["pref_name"];
388 $value = $line["value"];
389 $def_value = $line["def_value"];
390 $help_text = $line["help_text"];
391
392 print "<td width=\"40%\" class=\"prefName\" id=\"$pref_name\">" . __($line["short_desc"]);
393
394 if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
395
396 print "</td>";
397
398 print "<td class=\"prefValue\">";
399
400 if ($pref_name == "USER_TIMEZONE") {
401
402 $timezones = explode("\n", file_get_contents("lib/timezones.txt"));
403
404 print_select($pref_name, $value, $timezones, 'dojoType="dijit.form.FilteringSelect"');
405 } else if ($pref_name == "USER_STYLESHEET") {
406
407 print "<button dojoType=\"dijit.form.Button\"
408 onclick=\"customizeCSS()\">" . __('Customize') . "</button>";
409
410 } else if ($pref_name == "DEFAULT_ARTICLE_LIMIT") {
411
412 $limits = array(15, 30, 45, 60);
413
414 print_select($pref_name, $value, $limits,
415 'dojoType="dijit.form.Select"');
416
417 } else if ($pref_name == "DEFAULT_UPDATE_INTERVAL") {
418
419 global $update_intervals_nodefault;
420
421 print_select_hash($pref_name, $value, $update_intervals_nodefault,
422 'dojoType="dijit.form.Select"');
423
424 } else if ($type_name == "bool") {
425
426 if ($value == "true") {
427 $value = __("Yes");
428 } else {
429 $value = __("No");
430 }
431
432 if ($pref_name == "PURGE_UNREAD_ARTICLES" && FORCE_ARTICLE_PURGE != 0) {
433 $disabled = "disabled=\"1\"";
434 $value = __("Yes");
435 } else {
436 $disabled = "";
437 }
438
439 print_radio($pref_name, $value, __("Yes"), array(__("Yes"), __("No")),
440 $disabled);
441
442 } else if (array_search($pref_name, array('FRESH_ARTICLE_MAX_AGE', 'DEFAULT_ARTICLE_LIMIT',
443 'PURGE_OLD_DAYS', 'LONG_DATE_FORMAT', 'SHORT_DATE_FORMAT')) !== false) {
444
445 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
446
447 if ($pref_name == "PURGE_OLD_DAYS" && FORCE_ARTICLE_PURGE != 0) {
448 $disabled = "disabled=\"1\"";
449 $value = FORCE_ARTICLE_PURGE;
450 } else {
451 $disabled = "";
452 }
453
454 print "<input dojoType=\"dijit.form.ValidationTextBox\"
455 required=\"1\" $regexp $disabled
456 name=\"$pref_name\" value=\"$value\">";
457
458 } else if ($pref_name == "SSL_CERT_SERIAL") {
459
460 print "<input dojoType=\"dijit.form.ValidationTextBox\"
461 id=\"SSL_CERT_SERIAL\" readonly=\"1\"
462 name=\"$pref_name\" value=\"$value\">";
463
464 $cert_serial = htmlspecialchars(get_ssl_certificate_id());
465 $has_serial = ($cert_serial) ? "false" : "true";
466
467 print " <button dojoType=\"dijit.form.Button\" disabled=\"$has_serial\"
468 onclick=\"insertSSLserial('$cert_serial')\">" .
469 __('Register') . "</button>";
470
471 print " <button dojoType=\"dijit.form.Button\"
472 onclick=\"insertSSLserial('')\">" .
473 __('Clear') . "</button>";
474
76c843a9 475 } else if ($pref_name == 'DIGEST_PREFERRED_TIME') {
61c1812f 476 print "<input dojoType=\"dijit.form.ValidationTextBox\"
1b92f543 477 id=\"$pref_name\" regexp=\"[012]?\d:\d\d\" placeHolder=\"12:00\"
61c1812f 478 name=\"$pref_name\" value=\"$value\"><div class=\"insensitive\">".
8eba830f 479 T_sprintf("Current server time: %s (UTC)", date("H:i")) . "</div>";
1395083e
AD
480 } else {
481 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
482
483 print "<input dojoType=\"dijit.form.ValidationTextBox\"
484 $regexp
485 name=\"$pref_name\" value=\"$value\">";
486 }
487
488 print "</td>";
489
490 print "</tr>";
491
492 $lnum++;
493 }
494
495 print "</table>";
496
497 print '</div>'; # inside pane
498 print '<div dojoType="dijit.layout.ContentPane" region="bottom">';
499
500 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
501 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"saveconfig\">";
502
503 print "<button dojoType=\"dijit.form.Button\" type=\"submit\">".
504 __('Save configuration')."</button> ";
505
506 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editProfiles()\">".
507 __('Manage profiles')."</button> ";
508
509 print "<button dojoType=\"dijit.form.Button\" onclick=\"return validatePrefsReset()\">".
510 __('Reset to defaults')."</button>";
511
512 print '</div>'; # inner pane
513 print '</div>'; # border container
514
515 print "</form>";
516
517 print "</div>"; #pane
518 print "</div>"; #container
519 }
520}
521?>