]> git.wh0rd.org - tt-rss.git/blob - modules/pref-prefs.php
prefs: code cleanup
[tt-rss.git] / modules / pref-prefs.php
1 <?php
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
10 function module_pref_prefs($link) {
11
12 global $access_level_names;
13
14 $subop = $_REQUEST["subop"];
15
16 $prefs_blacklist = array("HIDE_FEEDLIST", "SYNC_COUNTERS", "ENABLE_LABELS",
17 "ENABLE_SEARCH_TOOLBAR", "HIDE_READ_FEEDS", "ENABLE_FEED_ICONS",
18 "ENABLE_OFFLINE_READING", "EXTENDED_FEEDLIST");
19
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",
24 "MARK_UNREAD_ON_UPDATE", "USER_TIMEZONE", "SORT_HEADLINES_BY_FEED_DATE");
25
26 if (FORCE_ARTICLE_PURGE != 0) {
27 array_push($prefs_blacklist, "PURGE_OLD_DAYS");
28 array_push($prefs_blacklist, "PURGE_UNREAD_ARTICLES");
29 }
30
31 if ($subop == "change-password") {
32
33 $old_pw = $_POST["old_password"];
34 $new_pw = $_POST["new_password"];
35 $con_pw = $_POST["confirm_password"];
36
37 if ($old_pw == "") {
38 print "ERROR: ".__("Old password cannot be blank.");
39 return;
40 }
41
42 if ($new_pw == "") {
43 print "ERROR: ".__("New password cannot be blank.");
44 return;
45 }
46
47 if ($new_pw != $con_pw) {
48 print "ERROR: ".__("Entered passwords do not match.");
49 return;
50 }
51
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"]);
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
63 id = '$active_uid' AND (pwd_hash = '$old_pw_hash1' OR
64 pwd_hash = '$old_pw_hash2')");
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
70 $_SESSION["pwd_hash"] = $new_pw_hash;
71
72 print __("Password has been changed.");
73 } else {
74 print "ERROR: ".__('Old password is incorrect.');
75 }
76 }
77
78 return;
79
80 } else if ($subop == "save-config") {
81
82 # $_SESSION["prefs_op_result"] = "save-config";
83
84 $_SESSION["prefs_cache"] = false;
85
86 // print_r($_POST);
87
88 $orig_theme = get_pref($link, "_THEME_ID");
89
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
95 set_pref($link, $pref_name, $value);
96
97 }
98
99 if ($orig_theme != get_pref($link, "_THEME_ID")) {
100 print "PREFS_THEME_CHANGED";
101 } else {
102 print __("The configuration was saved.");
103 }
104
105 return;
106
107 } else if ($subop == "getHelp") {
108
109 $pref_name = db_escape_string($_REQUEST["pn"]);
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 {
118 printf(__("Unknown option: %s"), $pref_name);
119 }
120
121 } else if ($subop == "change-email") {
122
123 $email = db_escape_string($_POST["email"]);
124 $full_name = db_escape_string($_POST["full_name"]);
125
126 $active_uid = $_SESSION["uid"];
127
128 db_query($link, "UPDATE ttrss_users SET email = '$email',
129 full_name = '$full_name' WHERE id = '$active_uid'");
130
131 print __("Your personal data has been saved.");
132
133 return;
134
135 } else if ($subop == "reset-config") {
136
137 $_SESSION["prefs_op_result"] = "reset-to-defaults";
138
139 if ($_SESSION["profile"]) {
140 $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
141 } else {
142 $profile_qpart = "profile IS NULL";
143 }
144
145 db_query($link, "DELETE FROM ttrss_user_prefs
146 WHERE $profile_qpart AND owner_uid = ".$_SESSION["uid"]);
147
148 initialize_user_prefs($link, $_SESSION["uid"], $_SESSION["profile"]);
149
150 print "PREFS_THEME_CHANGED";
151
152 // print __("The configuration was reset to defaults.");
153
154 return;
155
156 } else {
157
158 if (!SINGLE_USER_MODE) {
159
160 /* if ($_SESSION["pwd_change_result"] == "failed") {
161 print format_warning("Could not change the password.");
162 }
163
164 if ($_SESSION["pwd_change_result"] == "ok") {
165 print format_notice("Password was changed.");
166 }
167
168 $_SESSION["pwd_change_result"] = ""; */
169
170 /* if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
171 print format_notice(__("The configuration was reset to defaults."));
172 } */
173
174 # if ($_SESSION["prefs_op_result"] == "save-config") {
175 # print format_notice(__("The configuration was saved."));
176 # }
177
178 $_SESSION["prefs_op_result"] = "";
179
180 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
181 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Personal data')."\">";
182
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
199 print "<table width=\"100%\" class=\"prefPrefsList\">";
200
201 $result = db_query($link, "SELECT email,full_name,
202 access_level FROM ttrss_users
203 WHERE id = ".$_SESSION["uid"]);
204
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>";
209 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
210 value=\"$full_name\"></td></tr>";
211
212 print "<tr><td width=\"40%\">".__('E-mail')."</td>";
213 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"email\" required=\"1\" value=\"$email\"></td></tr>";
214
215 if (!SINGLE_USER_MODE) {
216 $access_level = db_fetch_result($result, 0, "access_level");
217 print "<tr><td width=\"40%\">".__('Access level')."</td>";
218 print "<td>" . $access_level_names[$access_level] . "</td></tr>";
219 }
220
221 print "</table>";
222
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\">";
225
226 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
227 __("Save data")."</button>";
228
229 print "</form>";
230
231 print "</div>"; # pane
232 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Authentication')."\">";
233
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
265 print "<table width=\"100%\" class=\"prefPrefsList\">";
266
267 print "<tr><td width=\"40%\">".__("Old password")."</td>";
268 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"old_password\"></td></tr>";
269
270 print "<tr><td width=\"40%\">".__("New password")."</td>";
271
272 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
273 name=\"new_password\"></td></tr>";
274
275 print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
276
277 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"confirm_password\"></td></tr>";
278
279 print "</table>";
280
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\">";
283
284 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
285 __("Change password")."</button>";
286
287 print "</form>";
288
289 print "</div>"; #pane
290 }
291
292 print "<div dojoType=\"dijit.layout.AccordionPane\" selected=\"true\" title=\"".__('Preferences')."\">";
293
294 if ($_SESSION["profile"]) {
295 print_notice("Some preferences are only available in default profile.");
296 }
297
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";
304 }
305
306 $result = db_query($link, "SELECT
307 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
308 section_name,def_value,section_id
309 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
310 WHERE type_id = ttrss_prefs_types.id AND
311 $profile_qpart AND
312 section_id = ttrss_prefs_sections.id AND
313 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
314 short_desc != '' AND
315 owner_uid = ".$_SESSION["uid"]."
316 ORDER BY section_id,short_desc");
317
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>";
337
338 $lnum = 0;
339
340 $active_section = "";
341
342 while ($line = db_fetch_assoc($result)) {
343
344 if (in_array($line["pref_name"], $prefs_blacklist)) {
345 continue;
346 }
347
348 if ($_SESSION["profile"] && in_array($line["pref_name"],
349 $profile_blacklist)) {
350 continue;
351 }
352
353 if ($active_section != $line["section_name"]) {
354
355 if ($active_section != "") {
356 print "</table>";
357 }
358
359 print "<table width=\"100%\" class=\"prefPrefsList\">";
360
361 $active_section = $line["section_name"];
362
363 print "<tr><td colspan=\"3\"><h3>".__($active_section)."</h3></td></tr>";
364
365 if ($line["section_id"] == 2) {
366 print "<tr><td width=\"40%\">".__("Select theme")."</td>";
367
368 $user_theme = get_pref($link, "_THEME_ID");
369 $themes = get_all_themes();
370
371 print "<td><select name=\"_THEME_ID\" dojoType=\"dijit.form.Select\">";
372 print "<option value='Default'>".__('Default')."</option>";
373 print "<option value='----------------' disabled=\"1\">--------</option>";
374
375 foreach ($themes as $t) {
376 $base = $t['base'];
377 $name = $t['name'];
378
379 if ($base == $user_theme) {
380 $selected = "selected=\"1\"";
381 } else {
382 $selected = "";
383 }
384
385 print "<option $selected value='$base'>$name</option>";
386
387 }
388
389 print "</select></td></tr>";
390 }
391
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
408 print "<td width=\"40%\" class=\"prefName\" id=\"$pref_name\">" . __($line["short_desc"]);
409
410 if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
411
412 print "</td>";
413
414 print "<td class=\"prefValue\">";
415
416 if ($pref_name == "USER_TIMEZONE") {
417
418 $timezones = explode("\n", file_get_contents("lib/timezones.txt"));
419
420 print_select($pref_name, $value, $timezones, 'dojoType="dijit.form.FilteringSelect"');
421
422 } else if ($pref_name == "DEFAULT_UPDATE_INTERVAL") {
423
424 global $update_intervals_nodefault;
425
426 print_select_hash($pref_name, $value, $update_intervals_nodefault,
427 'dojoType="dijit.form.Select"');
428
429 } else if ($type_name == "bool") {
430 // print_select($pref_name, $value, array("true", "false"));
431
432 if ($value == "true") {
433 $value = __("Yes");
434 } else {
435 $value = __("No");
436 }
437
438 print_radio($pref_name, $value, __("Yes"), array(__("Yes"), __("No")));
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
447 name=\"$pref_name\" value=\"$value\">";
448
449 } else {
450 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
451
452 print "<input dojoType=\"dijit.form.ValidationTextBox\"
453 $regexp
454 name=\"$pref_name\" value=\"$value\">";
455 }
456
457 print "</td>";
458
459 print "</tr>";
460
461 $lnum++;
462 }
463
464 print "</table>";
465
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\">";
468
469 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
470 __('Save configuration')."</button> ";
471
472 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editProfiles()\">".
473 __('Manage profiles')."</button> ";
474
475 print "<button dojoType=\"dijit.form.Button\" onclick=\"return validatePrefsReset()\">".
476 __('Reset to defaults')."</button></p>";
477
478 print "</form>";
479
480 print "</div>"; #pane
481 print "</div>"; #container
482
483 }
484 }
485 ?>