]> git.wh0rd.org - tt-rss.git/blame - modules/pref-prefs.php
prefs: code cleanup
[tt-rss.git] / modules / pref-prefs.php
CommitLineData
ef8be8ea 1<?php
f27d955a
AD
2 function prefs_js_redirect() {
3 print "<html><body>
4 <script type=\"text/javascript\">
5 window.location = 'prefs.php';
6 </script>
7 </body></html>";
8 }
9
ef8be8ea 10 function module_pref_prefs($link) {
a88d37e5
AD
11
12 global $access_level_names;
13
ef8be8ea
AD
14 $subop = $_REQUEST["subop"];
15
3b0a65ad 16 $prefs_blacklist = array("HIDE_FEEDLIST", "SYNC_COUNTERS", "ENABLE_LABELS",
75c51032 17 "ENABLE_SEARCH_TOOLBAR", "HIDE_READ_FEEDS", "ENABLE_FEED_ICONS",
296c8134 18 "ENABLE_OFFLINE_READING", "EXTENDED_FEEDLIST");
4bb2b579 19
d9084cf2
AD
20 $profile_blacklist = array("ALLOW_DUPLICATE_POSTS", "PURGE_OLD_DAYS",
21 "PURGE_UNREAD_ARTICLES", "DIGEST_ENABLE", "DIGEST_CATCHUP",
22 "BLACKLISTED_TAGS", "ENABLE_FEED_ICONS", "ENABLE_API_ACCESS",
23 "UPDATE_POST_ON_CHECKSUM_CHANGE", "DEFAULT_UPDATE_INTERVAL",
b3990c92 24 "MARK_UNREAD_ON_UPDATE", "USER_TIMEZONE", "SORT_HEADLINES_BY_FEED_DATE");
d9084cf2 25
3907ef71
AD
26 if (FORCE_ARTICLE_PURGE != 0) {
27 array_push($prefs_blacklist, "PURGE_OLD_DAYS");
28 array_push($prefs_blacklist, "PURGE_UNREAD_ARTICLES");
29 }
30
d95bd220
AD
31 if ($subop == "change-password") {
32
60807300
AD
33 $old_pw = $_POST["old_password"];
34 $new_pw = $_POST["new_password"];
35 $con_pw = $_POST["confirm_password"];
d95bd220
AD
36
37 if ($old_pw == "") {
7990ac73 38 print "ERROR: ".__("Old password cannot be blank.");
d95bd220
AD
39 return;
40 }
41
42 if ($new_pw == "") {
7990ac73 43 print "ERROR: ".__("New password cannot be blank.");
d95bd220
AD
44 return;
45 }
46
47 if ($new_pw != $con_pw) {
7990ac73 48 print "ERROR: ".__("Entered passwords do not match.");
d95bd220
AD
49 return;
50 }
51
60807300
AD
52 $old_pw_hash1 = encrypt_password($old_pw);
53 $old_pw_hash2 = encrypt_password($old_pw, $_SESSION["name"]);
54 $new_pw_hash = encrypt_password($new_pw, $_SESSION["name"]);
d95bd220
AD
55
56 $active_uid = $_SESSION["uid"];
57
58 if ($old_pw && $new_pw) {
59
60 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
61
62 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1a9f4d3c
AD
63 id = '$active_uid' AND (pwd_hash = '$old_pw_hash1' OR
64 pwd_hash = '$old_pw_hash2')");
d95bd220
AD
65
66 if (db_num_rows($result) == 1) {
67 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash'
68 WHERE id = '$active_uid'");
69
d43f7837
AD
70 $_SESSION["pwd_hash"] = $new_pw_hash;
71
7990ac73 72 print __("Password has been changed.");
d95bd220 73 } else {
7990ac73 74 print "ERROR: ".__('Old password is incorrect.');
d95bd220
AD
75 }
76 }
77
78 return;
d95bd220 79
b652c1b7 80 } else if ($subop == "save-config") {
ef8be8ea 81
b652c1b7 82# $_SESSION["prefs_op_result"] = "save-config";
ef8be8ea
AD
83
84 $_SESSION["prefs_cache"] = false;
85
f541eb78
AD
86// print_r($_POST);
87
dce46cad 88 $orig_theme = get_pref($link, "_THEME_ID");
d9084cf2 89
ef8be8ea
AD
90 foreach (array_keys($_POST) as $pref_name) {
91
92 $pref_name = db_escape_string($pref_name);
93 $value = db_escape_string($_POST[$pref_name]);
94
e9105eb5 95 set_pref($link, $pref_name, $value);
ef8be8ea 96
ef8be8ea
AD
97 }
98
dce46cad 99 if ($orig_theme != get_pref($link, "_THEME_ID")) {
d9084cf2
AD
100 print "PREFS_THEME_CHANGED";
101 } else {
102 print __("The configuration was saved.");
103 }
b652c1b7
AD
104
105 return;
f27d955a 106
ef8be8ea
AD
107 } else if ($subop == "getHelp") {
108
b4e75b2a 109 $pref_name = db_escape_string($_REQUEST["pn"]);
ef8be8ea
AD
110
111 $result = db_query($link, "SELECT help_text FROM ttrss_prefs
112 WHERE pref_name = '$pref_name'");
113
114 if (db_num_rows($result) > 0) {
115 $help_text = db_fetch_result($result, 0, "help_text");
116 print $help_text;
117 } else {
2a52d96f 118 printf(__("Unknown option: %s"), $pref_name);
ef8be8ea
AD
119 }
120
42395d28 121 } else if ($subop == "change-email") {
ef8be8ea 122
42395d28 123 $email = db_escape_string($_POST["email"]);
73fe13af
AD
124 $full_name = db_escape_string($_POST["full_name"]);
125
ef8be8ea
AD
126 $active_uid = $_SESSION["uid"];
127
73fe13af
AD
128 db_query($link, "UPDATE ttrss_users SET email = '$email',
129 full_name = '$full_name' WHERE id = '$active_uid'");
42395d28 130
73fe13af 131 print __("Your personal data has been saved.");
42395d28
AD
132
133 return;
ef8be8ea 134
b652c1b7 135 } else if ($subop == "reset-config") {
ef8be8ea 136
ef8be8ea
AD
137 $_SESSION["prefs_op_result"] = "reset-to-defaults";
138
1a697fa3
AD
139 if ($_SESSION["profile"]) {
140 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
141 } else {
142 $profile_qpart = "profile IS NULL";
143 }
144
eb7ab952 145 db_query($link, "DELETE FROM ttrss_user_prefs
1a697fa3
AD
146 WHERE $profile_qpart AND owner_uid = ".$_SESSION["uid"]);
147
148 initialize_user_prefs($link, $_SESSION["uid"], $_SESSION["profile"]);
ef8be8ea 149
1a697fa3
AD
150 print "PREFS_THEME_CHANGED";
151
152// print __("The configuration was reset to defaults.");
b652c1b7
AD
153
154 return;
ef8be8ea 155
ef8be8ea
AD
156 } else {
157
ef8be8ea
AD
158 if (!SINGLE_USER_MODE) {
159
d95bd220 160/* if ($_SESSION["pwd_change_result"] == "failed") {
0d32b41e 161 print format_warning("Could not change the password.");
ef8be8ea
AD
162 }
163
164 if ($_SESSION["pwd_change_result"] == "ok") {
0d32b41e 165 print format_notice("Password was changed.");
ef8be8ea
AD
166 }
167
d95bd220 168 $_SESSION["pwd_change_result"] = ""; */
ef8be8ea 169
1a697fa3 170/* if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
7990ac73 171 print format_notice(__("The configuration was reset to defaults."));
1a697fa3 172} */
ef8be8ea 173
b652c1b7
AD
174# if ($_SESSION["prefs_op_result"] == "save-config") {
175# print format_notice(__("The configuration was saved."));
176# }
ef8be8ea
AD
177
178 $_SESSION["prefs_op_result"] = "";
179
8df7184c
AD
180 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
181 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Personal data')."\">";
182
60807300
AD
183 print "<form dojoType=\"dijit.form.Form\" id=\"changeUserdataForm\">";
184
185 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
186 evt.preventDefault();
187 if (this.validate()) {
188 notify_progress('Saving data...', true);
189
190 new Ajax.Request('backend.php', {
191 parameters: dojo.objectToQuery(this.getValues()),
192 onComplete: function(transport) {
193 notify_callback2(transport);
194 } });
195
196 }
197 </script>";
198
ef8be8ea 199 print "<table width=\"100%\" class=\"prefPrefsList\">";
ef8be8ea 200
73fe13af
AD
201 $result = db_query($link, "SELECT email,full_name,
202 access_level FROM ttrss_users
ef8be8ea
AD
203 WHERE id = ".$_SESSION["uid"]);
204
73fe13af
AD
205 $email = htmlspecialchars(db_fetch_result($result, 0, "email"));
206 $full_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
207
208 print "<tr><td width=\"40%\">".__('Full name')."</td>";
d1bcacae 209 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
73fe13af
AD
210 value=\"$full_name\"></td></tr>";
211
e400230e 212 print "<tr><td width=\"40%\">".__('E-mail')."</td>";
d1bcacae 213 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"email\" required=\"1\" value=\"$email\"></td></tr>";
a88d37e5
AD
214
215 if (!SINGLE_USER_MODE) {
a88d37e5 216 $access_level = db_fetch_result($result, 0, "access_level");
a88d37e5
AD
217 print "<tr><td width=\"40%\">".__('Access level')."</td>";
218 print "<td>" . $access_level_names[$access_level] . "</td></tr>";
a88d37e5 219 }
ef8be8ea
AD
220
221 print "</table>";
222
60807300
AD
223 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
224 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"subop\" value=\"change-email\">";
ef8be8ea 225
60807300 226 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
73fe13af 227 __("Save data")."</button>";
42395d28 228
60807300
AD
229 print "</form>";
230
8df7184c
AD
231 print "</div>"; # pane
232 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Authentication')."\">";
233
60807300
AD
234 $result = db_query($link, "SELECT id FROM ttrss_users
235 WHERE id = ".$_SESSION["uid"]." AND pwd_hash
236 = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
237
238 if (db_num_rows($result) != 0) {
239 print format_warning(__("Your password is at default value, please change it."), "default_pass_warning");
240 }
241
242 print "<form dojoType=\"dijit.form.Form\">";
243
244 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
245 evt.preventDefault();
246 if (this.validate()) {
247 notify_progress('Changing password...', true);
248
249 new Ajax.Request('backend.php', {
250 parameters: dojo.objectToQuery(this.getValues()),
251 onComplete: function(transport) {
252 notify('');
253 if (transport.responseText.indexOf('ERROR: ') == 0) {
254 notify_error(transport.responseText.replace('ERROR: ', ''));
255 } else {
256 notify_info(transport.responseText);
257 var warn = $('default_pass_warning');
258 if (warn) Element.hide(warn);
259 }
260 }});
261 this.reset();
262 }
263 </script>";
264
ef8be8ea 265 print "<table width=\"100%\" class=\"prefPrefsList\">";
ef8be8ea 266
7990ac73 267 print "<tr><td width=\"40%\">".__("Old password")."</td>";
d1bcacae 268 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"old_password\"></td></tr>";
ef8be8ea 269
7990ac73 270 print "<tr><td width=\"40%\">".__("New password")."</td>";
ef8be8ea 271
d1bcacae 272 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
60807300 273 name=\"new_password\"></td></tr>";
d95bd220 274
7990ac73 275 print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
d95bd220 276
d1bcacae 277 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"confirm_password\"></td></tr>";
d95bd220 278
ef8be8ea
AD
279 print "</table>";
280
60807300
AD
281 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
282 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"subop\" value=\"change-password\">";
d95bd220 283
60807300 284 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
577399e8 285 __("Change password")."</button>";
d95bd220 286
60807300 287 print "</form>";
8df7184c
AD
288
289 print "</div>"; #pane
ef8be8ea
AD
290 }
291
8df7184c
AD
292 print "<div dojoType=\"dijit.layout.AccordionPane\" selected=\"true\" title=\"".__('Preferences')."\">";
293
35717425
AD
294 if ($_SESSION["profile"]) {
295 print_notice("Some preferences are only available in default profile.");
296 }
297
d9084cf2
AD
298 if ($_SESSION["profile"]) {
299 initialize_user_prefs($link, $_SESSION["uid"], $_SESSION["profile"]);
300 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
301 } else {
302 initialize_user_prefs($link, $_SESSION["uid"]);
303 $profile_qpart = "profile IS NULL";
ef8be8ea
AD
304 }
305
ef8be8ea
AD
306 $result = db_query($link, "SELECT
307 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
d9084cf2 308 section_name,def_value,section_id
ef8be8ea
AD
309 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
310 WHERE type_id = ttrss_prefs_types.id AND
d9084cf2 311 $profile_qpart AND
ef8be8ea
AD
312 section_id = ttrss_prefs_sections.id AND
313 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
40496720 314 short_desc != '' AND
ef8be8ea
AD
315 owner_uid = ".$_SESSION["uid"]."
316 ORDER BY section_id,short_desc");
317
60807300
AD
318 print "<form dojoType=\"dijit.form.Form\" id=\"changeSettingsForm\">";
319
320 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
321 evt.preventDefault();
322 if (this.validate()) {
323 console.log(dojo.objectToQuery(this.getValues()));
324
325 new Ajax.Request('backend.php', {
326 parameters: dojo.objectToQuery(this.getValues()),
327 onComplete: function(transport) {
328 var msg = transport.responseText;
329 if (msg.match('PREFS_THEME_CHANGED')) {
330 window.location.reload();
331 } else {
332 notify_info(msg);
333 }
334 } });
335 }
336 </script>";
ef8be8ea
AD
337
338 $lnum = 0;
339
340 $active_section = "";
341
342 while ($line = db_fetch_assoc($result)) {
343
4bb2b579
AD
344 if (in_array($line["pref_name"], $prefs_blacklist)) {
345 continue;
346 }
347
d9084cf2
AD
348 if ($_SESSION["profile"] && in_array($line["pref_name"],
349 $profile_blacklist)) {
350 continue;
351 }
352
ef8be8ea
AD
353 if ($active_section != $line["section_name"]) {
354
355 if ($active_section != "") {
356 print "</table>";
357 }
358
8df7184c 359 print "<table width=\"100%\" class=\"prefPrefsList\">";
d9084cf2 360
ef8be8ea
AD
361 $active_section = $line["section_name"];
362
89cb787e 363 print "<tr><td colspan=\"3\"><h3>".__($active_section)."</h3></td></tr>";
d9084cf2
AD
364
365 if ($line["section_id"] == 2) {
366 print "<tr><td width=\"40%\">".__("Select theme")."</td>";
dce46cad
AD
367
368 $user_theme = get_pref($link, "_THEME_ID");
369 $themes = get_all_themes();
370
60807300 371 print "<td><select name=\"_THEME_ID\" dojoType=\"dijit.form.Select\">";
d69fa6d6 372 print "<option value='Default'>".__('Default')."</option>";
60807300 373 print "<option value='----------------' disabled=\"1\">--------</option>";
dce46cad
AD
374
375 foreach ($themes as $t) {
376 $base = $t['base'];
377 $name = $t['name'];
378
379 if ($base == $user_theme) {
380 $selected = "selected=\"1\"";
d9084cf2
AD
381 } else {
382 $selected = "";
383 }
dce46cad
AD
384
385 print "<option $selected value='$base'>$name</option>";
386
d9084cf2 387 }
dce46cad 388
d9084cf2
AD
389 print "</select></td></tr>";
390 }
391
ef8be8ea
AD
392// print "<tr class=\"title\">
393// <td width=\"25%\">Option</td><td>Value</td></tr>";
394
395 $lnum = 0;
396 }
397
398// $class = ($lnum % 2) ? "even" : "odd";
399
400 print "<tr>";
401
402 $type_name = $line["type_name"];
403 $pref_name = $line["pref_name"];
404 $value = $line["value"];
405 $def_value = $line["def_value"];
406 $help_text = $line["help_text"];
407
e351c494 408 print "<td width=\"40%\" class=\"prefName\" id=\"$pref_name\">" . __($line["short_desc"]);
ef8be8ea 409
89cb787e 410 if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
ef8be8ea
AD
411
412 print "</td>";
413
e351c494 414 print "<td class=\"prefValue\">";
ef8be8ea 415
324944f3
AD
416 if ($pref_name == "USER_TIMEZONE") {
417
418 $timezones = explode("\n", file_get_contents("lib/timezones.txt"));
419
60807300 420 print_select($pref_name, $value, $timezones, 'dojoType="dijit.form.FilteringSelect"');
324944f3
AD
421
422 } else if ($pref_name == "DEFAULT_UPDATE_INTERVAL") {
505f2f0e
AD
423
424 global $update_intervals_nodefault;
425
60807300
AD
426 print_select_hash($pref_name, $value, $update_intervals_nodefault,
427 'dojoType="dijit.form.Select"');
505f2f0e
AD
428
429 } else if ($type_name == "bool") {
ef8be8ea
AD
430// print_select($pref_name, $value, array("true", "false"));
431
432 if ($value == "true") {
836537f7 433 $value = __("Yes");
ef8be8ea 434 } else {
836537f7 435 $value = __("No");
ef8be8ea
AD
436 }
437
f541eb78 438 print_radio($pref_name, $value, __("Yes"), array(__("Yes"), __("No")));
60807300
AD
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 print "<input dojoType=\"dijit.form.ValidationTextBox\"
446 required=\"1\" $regexp
6068d33b 447 name=\"$pref_name\" value=\"$value\">";
60807300
AD
448
449 } else {
450 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
451
452 print "<input dojoType=\"dijit.form.ValidationTextBox\"
d1bcacae 453 $regexp
60807300 454 name=\"$pref_name\" value=\"$value\">";
ef8be8ea
AD
455 }
456
457 print "</td>";
458
459 print "</tr>";
460
461 $lnum++;
462 }
463
464 print "</table>";
465
60807300
AD
466 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
467 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"subop\" value=\"save-config\">";
ef8be8ea 468
60807300 469 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
577399e8 470 __('Save configuration')."</button> ";
d9084cf2 471
60807300 472 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editProfiles()\">".
d9084cf2
AD
473 __('Manage profiles')."</button> ";
474
60807300 475 print "<button dojoType=\"dijit.form.Button\" onclick=\"return validatePrefsReset()\">".
577399e8 476 __('Reset to defaults')."</button></p>";
ef8be8ea
AD
477
478 print "</form>";
479
8df7184c
AD
480 print "</div>"; #pane
481 print "</div>"; #container
482
ef8be8ea
AD
483 }
484 }
485?>