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