]> git.wh0rd.org - tt-rss.git/blob - modules/pref-prefs.php
ajaxify prefs save/reset; update translations
[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 $subop = $_REQUEST["subop"];
12
13 if ($subop == "change-password") {
14
15 $old_pw = $_POST["OLD_PASSWORD"];
16 $new_pw = $_POST["NEW_PASSWORD"];
17 $con_pw = $_POST["CONFIRM_PASSWORD"];
18
19 if ($old_pw == "") {
20 print "ERROR: ".__("Old password cannot be blank.");
21 return;
22 }
23
24 if ($new_pw == "") {
25 print "ERROR: ".__("New password cannot be blank.");
26 return;
27 }
28
29 if ($new_pw != $con_pw) {
30 print "ERROR: ".__("Entered passwords do not match.");
31 return;
32 }
33
34 $old_pw_hash = 'SHA1:' . sha1($_POST["OLD_PASSWORD"]);
35 $new_pw_hash = 'SHA1:' . sha1($_POST["NEW_PASSWORD"]);
36
37 $active_uid = $_SESSION["uid"];
38
39 if ($old_pw && $new_pw) {
40
41 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
42
43 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
44 id = '$active_uid' AND (pwd_hash = '$old_pw' OR
45 pwd_hash = '$old_pw_hash')");
46
47 if (db_num_rows($result) == 1) {
48 db_query($link, "UPDATE ttrss_users SET pwd_hash = '$new_pw_hash'
49 WHERE id = '$active_uid'");
50
51 print __("Password has been changed.");
52 } else {
53 print "ERROR: ".__('Old password is incorrect.');
54 }
55 }
56
57 return;
58
59 } else if ($subop == "save-config") {
60
61 # $_SESSION["prefs_op_result"] = "save-config";
62
63 $_SESSION["prefs_cache"] = false;
64
65 // print_r($_POST);
66
67 foreach (array_keys($_POST) as $pref_name) {
68
69 $pref_name = db_escape_string($pref_name);
70 $value = db_escape_string($_POST[$pref_name]);
71
72 $result = db_query($link, "SELECT type_name
73 FROM ttrss_prefs,ttrss_prefs_types
74 WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
75
76 if (db_num_rows($result) > 0) {
77
78 $type_name = db_fetch_result($result, 0, "type_name");
79
80 // print "$pref_name : $type_name : $value<br>";
81
82 if ($type_name == "bool") {
83 if ($value == "1") {
84 $value = "true";
85 } else {
86 $value = "false";
87 }
88 } else if ($type_name == "integer") {
89 $value = sprintf("%d", $value);
90 }
91
92 // print "$pref_name : $type_name : $value<br>";
93
94 db_query($link, "UPDATE ttrss_user_prefs SET value = '$value'
95 WHERE pref_name = '$pref_name' AND owner_uid = ".$_SESSION["uid"]);
96
97 }
98
99 }
100
101 #return prefs_js_redirect();
102
103 print __("The configuration was saved.");
104
105 return;
106
107 } else if ($subop == "getHelp") {
108
109 $pref_name = db_escape_string($_GET["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 print "Unknown option: $pref_name";
119 }
120
121 } else if ($subop == "change-email") {
122
123 $email = db_escape_string($_POST["email"]);
124 $active_uid = $_SESSION["uid"];
125
126 db_query($link, "UPDATE ttrss_users SET email = '$email'
127 WHERE id = '$active_uid'");
128
129 print __("E-mail has been changed.");
130
131 return;
132
133 } else if ($subop == "reset-config") {
134
135 $_SESSION["prefs_op_result"] = "reset-to-defaults";
136
137 db_query($link, "DELETE FROM ttrss_user_prefs
138 WHERE owner_uid = ".$_SESSION["uid"]);
139 initialize_user_prefs($link, $_SESSION["uid"]);
140
141 print __("The configuration was reset to defaults.");
142
143 return;
144
145 } else if ($subop == __("Change theme")) {
146
147 $theme = db_escape_string($_POST["theme"]);
148
149 if ($theme == "Default") {
150 $theme_qpart = 'NULL';
151 } else {
152 $theme_qpart = "'$theme'";
153 }
154
155 $result = db_query($link, "SELECT id,theme_path FROM ttrss_themes
156 WHERE theme_name = '$theme'");
157
158 if (db_num_rows($result) == 1) {
159 $theme_id = db_fetch_result($result, 0, "id");
160 $theme_path = db_fetch_result($result, 0, "theme_path");
161 } else {
162 $theme_id = "NULL";
163 $theme_path = "";
164 }
165
166 db_query($link, "UPDATE ttrss_users SET
167 theme_id = $theme_id WHERE id = " . $_SESSION["uid"]);
168
169 $_SESSION["theme"] = $theme_path;
170
171 return prefs_js_redirect();
172
173 } else {
174
175 // print check_for_update($link);
176
177 set_pref($link, "_PREFS_ACTIVE_TAB", "genConfig");
178
179 if (!SINGLE_USER_MODE) {
180
181 $result = db_query($link, "SELECT id FROM ttrss_users
182 WHERE id = ".$_SESSION["uid"]." AND pwd_hash
183 = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
184
185 if (db_num_rows($result) != 0) {
186 print format_warning(__("Your password is at default value,
187 please change it."), "default_pass_warning");
188 }
189
190 /* if ($_SESSION["pwd_change_result"] == "failed") {
191 print format_warning("Could not change the password.");
192 }
193
194 if ($_SESSION["pwd_change_result"] == "ok") {
195 print format_notice("Password was changed.");
196 }
197
198 $_SESSION["pwd_change_result"] = ""; */
199
200 if ($_SESSION["prefs_op_result"] == "reset-to-defaults") {
201 print format_notice(__("The configuration was reset to defaults."));
202 }
203
204 # if ($_SESSION["prefs_op_result"] == "save-config") {
205 # print format_notice(__("The configuration was saved."));
206 # }
207
208 $_SESSION["prefs_op_result"] = "";
209
210 print "<form onsubmit='return false' id='change_email_form'>";
211
212 print "<table width=\"100%\" class=\"prefPrefsList\">";
213 print "<tr><td colspan='3'><h3>".__("Personal data")."</h3></tr></td>";
214
215 $result = db_query($link, "SELECT email FROM ttrss_users
216 WHERE id = ".$_SESSION["uid"]);
217
218 $email = db_fetch_result($result, 0, "email");
219
220 print "<tr><td width=\"40%\">E-mail</td>";
221 print "<td><input class=\"editbox\" name=\"email\"
222 onkeypress=\"return filterCR(event, changeUserEmail)\"
223 value=\"$email\"></td></tr>";
224
225 print "</table>";
226
227 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
228 print "<input type=\"hidden\" name=\"subop\" value=\"change-email\">";
229
230 print "</form>";
231
232 print "<p><input class=\"button\" type=\"submit\"
233 onclick=\"return changeUserEmail()\" value=\"".__("Change e-mail")."\">";
234
235 print "<form onsubmit=\"return false\"
236 name=\"change_pass_form\" id=\"change_pass_form\">";
237
238 print "<table width=\"100%\" class=\"prefPrefsList\">";
239 print "<tr><td colspan='3'><h3>".__("Authentication")."</h3></tr></td>";
240
241 print "<tr><td width=\"40%\">".__("Old password")."</td>";
242 print "<td><input class=\"editbox\" type=\"password\"
243 onkeypress=\"return filterCR(event, changeUserPassword)\"
244 name=\"OLD_PASSWORD\"></td></tr>";
245
246 print "<tr><td width=\"40%\">".__("New password")."</td>";
247
248 print "<td><input class=\"editbox\" type=\"password\"
249 onkeypress=\"return filterCR(event, changeUserPassword)\"
250 name=\"NEW_PASSWORD\"></td></tr>";
251
252 print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
253
254 print "<td><input class=\"editbox\" type=\"password\"
255 onkeypress=\"return filterCR(event, changeUserPassword)\"
256 name=\"CONFIRM_PASSWORD\"></td></tr>";
257
258 print "</table>";
259
260 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
261 print "<input type=\"hidden\" name=\"subop\" value=\"change-password\">";
262
263 print "</form>";
264
265 print "<p><input class=\"button\" type=\"submit\"
266 onclick=\"return changeUserPassword()\"
267 value=\"".__("Change password")."\">";
268
269 }
270
271 $result = db_query($link, "SELECT
272 theme_id FROM ttrss_users WHERE id = " . $_SESSION["uid"]);
273
274 $user_theme_id = db_fetch_result($result, 0, "theme_id");
275
276 $result = db_query($link, "SELECT
277 id,theme_name FROM ttrss_themes ORDER BY theme_name");
278
279 if (db_num_rows($result) > 0) {
280
281 print "<form action=\"backend.php\" method=\"POST\">";
282 print "<table width=\"100%\" class=\"prefPrefsList\">";
283 print "<tr><td colspan='3'><h3>".__("Themes")."</h3></tr></td>";
284 print "<tr><td width=\"40%\">".__("Select theme")."</td>";
285 print "<td><select name=\"theme\">";
286 print "<option value='Default'>".__('Default')."</option>";
287 print "<option disabled>--------</option>";
288
289 while ($line = db_fetch_assoc($result)) {
290 if ($line["id"] == $user_theme_id) {
291 $selected = "selected";
292 } else {
293 $selected = "";
294 }
295 print "<option $selected>" . $line["theme_name"] . "</option>";
296 }
297 print "</select></td></tr>";
298 print "</table>";
299 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
300 print "<input type=\"hidden\" name=\"subop\" value=\"Change theme\">";
301 print "<p><input class=\"button\" type=\"submit\"
302 value=\"".__('Change theme')."\">";
303 print "</form>";
304 }
305
306 initialize_user_prefs($link, $_SESSION["uid"]);
307
308 $result = db_query($link, "SELECT
309 ttrss_user_prefs.pref_name,short_desc,help_text,value,type_name,
310 section_name,def_value
311 FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
312 WHERE type_id = ttrss_prefs_types.id AND
313 section_id = ttrss_prefs_sections.id AND
314 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
315 short_desc != '' AND
316 owner_uid = ".$_SESSION["uid"]."
317 ORDER BY section_id,short_desc");
318
319 print "<form action=\"backend.php\" method=\"POST\" id=\"pref_prefs_form\">";
320
321 $lnum = 0;
322
323 $active_section = "";
324
325 while ($line = db_fetch_assoc($result)) {
326
327 if ($active_section != $line["section_name"]) {
328
329 if ($active_section != "") {
330 print "</table>";
331 }
332
333 print "<p><table width=\"100%\" class=\"prefPrefsList\">";
334
335 $active_section = $line["section_name"];
336
337 print "<tr><td colspan=\"3\"><h3>".__($active_section)."</h3></td></tr>";
338 // print "<tr class=\"title\">
339 // <td width=\"25%\">Option</td><td>Value</td></tr>";
340
341 $lnum = 0;
342 }
343
344 // $class = ($lnum % 2) ? "even" : "odd";
345
346 print "<tr>";
347
348 $type_name = $line["type_name"];
349 $pref_name = $line["pref_name"];
350 $value = $line["value"];
351 $def_value = $line["def_value"];
352 $help_text = $line["help_text"];
353
354 print "<td width=\"40%\" id=\"$pref_name\">" . __($line["short_desc"]);
355
356 if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
357
358 print "</td>";
359
360 print "<td>";
361
362 if ($type_name == "bool") {
363 // print_select($pref_name, $value, array("true", "false"));
364
365 if ($value == "true") {
366 $value = __("Yes");
367 } else {
368 $value = __("No");
369 }
370
371 print_radio($pref_name, $value, __("Yes"), array(__("Yes"), __("No")));
372
373 } else {
374 print "<input class=\"editbox\" name=\"$pref_name\" value=\"$value\">";
375 }
376
377 print "</td>";
378
379 print "</tr>";
380
381 $lnum++;
382 }
383
384 print "</table>";
385
386 print "<input type=\"hidden\" name=\"op\" value=\"pref-prefs\">";
387
388 print "<p><input class=\"button\" type=\"submit\"
389 onclick=\"return validatePrefsSave()\"
390 value=\"".__('Save configuration')."\">";
391
392 print "&nbsp;<input class=\"button\" type=\"submit\"
393 onclick=\"return validatePrefsReset()\"
394 value=\"".__('Reset to defaults')."\"></p>";
395
396 print "</form>";
397
398 }
399 }
400 ?>