]> git.wh0rd.org - tt-rss.git/blame - classes/pref_prefs.php
try to send digests within 2 hours of preferred time only
[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
163 if (!SINGLE_USER_MODE) {
164
165 $_SESSION["prefs_op_result"] = "";
166
167 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
e95e7819 168 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Personal data / Authentication')."\">";
1395083e
AD
169
170 print "<form dojoType=\"dijit.form.Form\" id=\"changeUserdataForm\">";
171
172 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
173 evt.preventDefault();
174 if (this.validate()) {
175 notify_progress('Saving data...', true);
176
177 new Ajax.Request('backend.php', {
178 parameters: dojo.objectToQuery(this.getValues()),
179 onComplete: function(transport) {
180 notify_callback2(transport);
181 } });
182
183 }
184 </script>";
185
186 print "<table width=\"100%\" class=\"prefPrefsList\">";
187
188 $result = db_query($this->link, "SELECT email,full_name,
189 access_level FROM ttrss_users
190 WHERE id = ".$_SESSION["uid"]);
191
192 $email = htmlspecialchars(db_fetch_result($result, 0, "email"));
193 $full_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
194
195 print "<tr><td width=\"40%\">".__('Full name')."</td>";
196 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
197 value=\"$full_name\"></td></tr>";
198
199 print "<tr><td width=\"40%\">".__('E-mail')."</td>";
200 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"email\" required=\"1\" value=\"$email\"></td></tr>";
201
202 if (!SINGLE_USER_MODE) {
203 $access_level = db_fetch_result($result, 0, "access_level");
204 print "<tr><td width=\"40%\">".__('Access level')."</td>";
205 print "<td>" . $access_level_names[$access_level] . "</td></tr>";
206 }
207
208 print "</table>";
209
210 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
211 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"changeemail\">";
212
213 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
214 __("Save data")."</button>";
215
216 print "</form>";
217
1395083e
AD
218 $result = db_query($this->link, "SELECT id FROM ttrss_users
219 WHERE id = ".$_SESSION["uid"]." AND pwd_hash
220 = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
221
222 if (db_num_rows($result) != 0) {
223 print format_warning(__("Your password is at default value, please change it."), "default_pass_warning");
224 }
225
226 print "<form dojoType=\"dijit.form.Form\">";
227
228 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
229 evt.preventDefault();
230 if (this.validate()) {
231 notify_progress('Changing password...', true);
232
233 new Ajax.Request('backend.php', {
234 parameters: dojo.objectToQuery(this.getValues()),
235 onComplete: function(transport) {
236 notify('');
237 if (transport.responseText.indexOf('ERROR: ') == 0) {
238 notify_error(transport.responseText.replace('ERROR: ', ''));
239 } else {
240 notify_info(transport.responseText);
241 var warn = $('default_pass_warning');
242 if (warn) Element.hide(warn);
243 }
244 }});
245 this.reset();
246 }
247 </script>";
248
249 print "<table width=\"100%\" class=\"prefPrefsList\">";
250
251 print "<tr><td width=\"40%\">".__("Old password")."</td>";
252 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"old_password\"></td></tr>";
253
254 print "<tr><td width=\"40%\">".__("New password")."</td>";
255
256 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
257 name=\"new_password\"></td></tr>";
258
259 print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
260
261 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"confirm_password\"></td></tr>";
262
263 print "</table>";
264
265 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
266 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"changepassword\">";
267
268 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
269 __("Change password")."</button>";
270
271 print "</form>";
272
273 print "</div>"; #pane
274 }
275
276 print "<div dojoType=\"dijit.layout.AccordionPane\" selected=\"true\" title=\"".__('Preferences')."\">";
277
278 print "<form dojoType=\"dijit.form.Form\" id=\"changeSettingsForm\">";
279
280 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
281 evt.preventDefault();
282 if (this.validate()) {
283 console.log(dojo.objectToQuery(this.getValues()));
284
285 new Ajax.Request('backend.php', {
286 parameters: dojo.objectToQuery(this.getValues()),
287 onComplete: function(transport) {
288 var msg = transport.responseText;
289 if (msg.match('PREFS_THEME_CHANGED')) {
290 window.location.reload();
291 } else {
292 notify_info(msg);
293 }
294 } });
295 }
296 </script>";
297
298 print '<div dojoType="dijit.layout.BorderContainer" gutters="false">';
299
300 print '<div dojoType="dijit.layout.ContentPane" region="center" style="overflow-y : auto">';
301
302 if ($_SESSION["profile"]) {
303 print_notice("Some preferences are only available in default profile.");
304 }
305
306 if ($_SESSION["profile"]) {
307 initialize_user_prefs($this->link, $_SESSION["uid"], $_SESSION["profile"]);
308 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
309 } else {
310 initialize_user_prefs($this->link, $_SESSION["uid"]);
311 $profile_qpart = "profile IS NULL";
312 }
313
314 $result = db_query($this->link, "SELECT
315 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
316 section_name,def_value,section_id
317 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
318 WHERE type_id = ttrss_prefs_types.id AND
319 $profile_qpart AND
320 section_id = ttrss_prefs_sections.id AND
321 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
322 short_desc != '' AND
323 owner_uid = ".$_SESSION["uid"]."
324 ORDER BY section_id,short_desc");
325
326 $lnum = 0;
327
328 $active_section = "";
329
330 while ($line = db_fetch_assoc($result)) {
331
332 if (in_array($line["pref_name"], $prefs_blacklist)) {
333 continue;
334 }
335
336 if ($_SESSION["profile"] && in_array($line["pref_name"],
337 $profile_blacklist)) {
338 continue;
339 }
340
341 if ($active_section != $line["section_name"]) {
342
343 if ($active_section != "") {
344 print "</table>";
345 }
346
347 print "<table width=\"100%\" class=\"prefPrefsList\">";
348
349 $active_section = $line["section_name"];
350
351 print "<tr><td colspan=\"3\"><h3>".__($active_section)."</h3></td></tr>";
352
353 if ($line["section_id"] == 2) {
354 print "<tr><td width=\"40%\">".__("Select theme")."</td>";
355
356 $user_theme = get_pref($this->link, "_THEME_ID");
357 $themes = get_all_themes();
358
359 print "<td><select name=\"_THEME_ID\" dojoType=\"dijit.form.Select\">";
360 print "<option value='Default'>".__('Default')."</option>";
361 print "<option value='----------------' disabled=\"1\">--------</option>";
362
363 foreach ($themes as $t) {
364 $base = $t['base'];
365 $name = $t['name'];
366
367 if ($base == $user_theme) {
368 $selected = "selected=\"1\"";
369 } else {
370 $selected = "";
371 }
372
373 print "<option $selected value='$base'>$name</option>";
374
375 }
376
377 print "</select></td></tr>";
378 }
379 $lnum = 0;
380 }
381
382 print "<tr>";
383
384 $type_name = $line["type_name"];
385 $pref_name = $line["pref_name"];
386 $value = $line["value"];
387 $def_value = $line["def_value"];
388 $help_text = $line["help_text"];
389
390 print "<td width=\"40%\" class=\"prefName\" id=\"$pref_name\">" . __($line["short_desc"]);
391
392 if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
393
394 print "</td>";
395
396 print "<td class=\"prefValue\">";
397
398 if ($pref_name == "USER_TIMEZONE") {
399
400 $timezones = explode("\n", file_get_contents("lib/timezones.txt"));
401
402 print_select($pref_name, $value, $timezones, 'dojoType="dijit.form.FilteringSelect"');
403 } else if ($pref_name == "USER_STYLESHEET") {
404
405 print "<button dojoType=\"dijit.form.Button\"
406 onclick=\"customizeCSS()\">" . __('Customize') . "</button>";
407
408 } else if ($pref_name == "DEFAULT_ARTICLE_LIMIT") {
409
410 $limits = array(15, 30, 45, 60);
411
412 print_select($pref_name, $value, $limits,
413 'dojoType="dijit.form.Select"');
414
415 } else if ($pref_name == "DEFAULT_UPDATE_INTERVAL") {
416
417 global $update_intervals_nodefault;
418
419 print_select_hash($pref_name, $value, $update_intervals_nodefault,
420 'dojoType="dijit.form.Select"');
421
422 } else if ($type_name == "bool") {
423
424 if ($value == "true") {
425 $value = __("Yes");
426 } else {
427 $value = __("No");
428 }
429
430 if ($pref_name == "PURGE_UNREAD_ARTICLES" && FORCE_ARTICLE_PURGE != 0) {
431 $disabled = "disabled=\"1\"";
432 $value = __("Yes");
433 } else {
434 $disabled = "";
435 }
436
437 print_radio($pref_name, $value, __("Yes"), array(__("Yes"), __("No")),
438 $disabled);
439
440 } else if (array_search($pref_name, array('FRESH_ARTICLE_MAX_AGE', 'DEFAULT_ARTICLE_LIMIT',
441 'PURGE_OLD_DAYS', 'LONG_DATE_FORMAT', 'SHORT_DATE_FORMAT')) !== false) {
442
443 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
444
445 if ($pref_name == "PURGE_OLD_DAYS" && FORCE_ARTICLE_PURGE != 0) {
446 $disabled = "disabled=\"1\"";
447 $value = FORCE_ARTICLE_PURGE;
448 } else {
449 $disabled = "";
450 }
451
452 print "<input dojoType=\"dijit.form.ValidationTextBox\"
453 required=\"1\" $regexp $disabled
454 name=\"$pref_name\" value=\"$value\">";
455
456 } else if ($pref_name == "SSL_CERT_SERIAL") {
457
458 print "<input dojoType=\"dijit.form.ValidationTextBox\"
459 id=\"SSL_CERT_SERIAL\" readonly=\"1\"
460 name=\"$pref_name\" value=\"$value\">";
461
462 $cert_serial = htmlspecialchars(get_ssl_certificate_id());
463 $has_serial = ($cert_serial) ? "false" : "true";
464
465 print " <button dojoType=\"dijit.form.Button\" disabled=\"$has_serial\"
466 onclick=\"insertSSLserial('$cert_serial')\">" .
467 __('Register') . "</button>";
468
469 print " <button dojoType=\"dijit.form.Button\"
470 onclick=\"insertSSLserial('')\">" .
471 __('Clear') . "</button>";
472
76c843a9 473 } else if ($pref_name == 'DIGEST_PREFERRED_TIME') {
61c1812f
AD
474 print "<input dojoType=\"dijit.form.ValidationTextBox\"
475 id=\"$pref_name\" regexp=\"[012]\d:\d\d\" placeHolder=\"12:00\"
476 name=\"$pref_name\" value=\"$value\"><div class=\"insensitive\">".
477 T_sprintf("Current server time: %s", date("H:i")) . "</div>";
1395083e
AD
478 } else {
479 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
480
481 print "<input dojoType=\"dijit.form.ValidationTextBox\"
482 $regexp
483 name=\"$pref_name\" value=\"$value\">";
484 }
485
486 print "</td>";
487
488 print "</tr>";
489
490 $lnum++;
491 }
492
493 print "</table>";
494
495 print '</div>'; # inside pane
496 print '<div dojoType="dijit.layout.ContentPane" region="bottom">';
497
498 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
499 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"saveconfig\">";
500
501 print "<button dojoType=\"dijit.form.Button\" type=\"submit\">".
502 __('Save configuration')."</button> ";
503
504 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editProfiles()\">".
505 __('Manage profiles')."</button> ";
506
507 print "<button dojoType=\"dijit.form.Button\" onclick=\"return validatePrefsReset()\">".
508 __('Reset to defaults')."</button>";
509
510 print '</div>'; # inner pane
511 print '</div>'; # border container
512
513 print "</form>";
514
515 print "</div>"; #pane
516 print "</div>"; #container
517 }
518}
519?>