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