]> git.wh0rd.org Git - tt-rss.git/blob - classes/pref/prefs.php
simplify error handling
[tt-rss.git] / classes / pref / prefs.php
1 <?php
2
3 class Pref_Prefs extends Handler_Protected {
4
5         private $pref_help = array();
6         private $pref_sections = array();
7
8         function csrf_ignore($method) {
9                 $csrf_ignored = array("index", "updateself", "customizecss", "editprefprofiles");
10
11                 return array_search($method, $csrf_ignored) !== false;
12         }
13
14         function __construct($args) {
15                 parent::__construct($args);
16
17                 $this->pref_sections = array(
18                         1 => __('General'),
19                         2 => __('Interface'),
20                         3 => __('Advanced'),
21                         4 => __('Digest')
22                 );
23
24                 $this->pref_help = array(
25                         "ALLOW_DUPLICATE_POSTS" => array(__("Allow duplicate articles"), ""),
26                         "BLACKLISTED_TAGS" => array(__("Blacklisted tags"), __("When auto-detecting tags in articles these tags will not be applied (comma-separated list).")),
27                         "CDM_AUTO_CATCHUP" => array(__("Automatically mark articles as read"), __("This option enables marking articles as read automatically while you scroll article list.")),
28                         "CDM_EXPANDED" => array(__("Automatically expand articles in combined mode"), ""),
29                         "COMBINED_DISPLAY_MODE" => array(__("Combined feed display"), __("Display expanded list of feed articles, instead of separate displays for headlines and article content")),
30                         "CONFIRM_FEED_CATCHUP" => array(__("Confirm marking feed as read"), ""),
31                         "DEFAULT_ARTICLE_LIMIT" => array(__("Amount of articles to display at once"), ""),
32                         "DEFAULT_UPDATE_INTERVAL" => array(__("Default feed update interval"), __("Shortest interval at which a feed will be checked for updates regardless of update method")),
33                         "DIGEST_CATCHUP" => array(__("Mark articles in e-mail digest as read"), ""),
34                         "DIGEST_ENABLE" => array(__("Enable e-mail digest"), __("This option enables sending daily digest of new (and unread) headlines on your configured e-mail address")),
35                         "DIGEST_PREFERRED_TIME" => array(__("Try to send digests around specified time"), __("Uses UTC timezone")),
36                         "ENABLE_API_ACCESS" => array(__("Enable API access"), __("Allows external clients to access this account through the API")),
37                         "ENABLE_FEED_CATS" => array(__("Enable feed categories"), ""),
38                         "FEEDS_SORT_BY_UNREAD" => array(__("Sort feeds by unread articles count"), ""),
39                         "FRESH_ARTICLE_MAX_AGE" => array(__("Maximum age of fresh articles (in hours)"), ""),
40                         "HIDE_READ_FEEDS" => array(__("Hide feeds with no unread articles"), ""),
41                         "HIDE_READ_SHOWS_SPECIAL" => array(__("Show special feeds when hiding read feeds"), ""),
42                         "LONG_DATE_FORMAT" => array(__("Long date format"), __("The syntax used is identical to the PHP <a href='http://php.net/manual/function.date.php'>date()</a> function.")),
43                         "ON_CATCHUP_SHOW_NEXT_FEED" => array(__("On catchup show next feed"), __("Automatically open next feed with unread articles after marking one as read")),
44                         "PURGE_OLD_DAYS" => array(__("Purge articles after this number of days (0 - disables)"), ""),
45                         "PURGE_UNREAD_ARTICLES" => array(__("Purge unread articles"), ""),
46                         "REVERSE_HEADLINES" => array(__("Reverse headline order (oldest first)"), ""),
47                         "SHORT_DATE_FORMAT" => array(__("Short date format"), ""),
48                         "SHOW_CONTENT_PREVIEW" => array(__("Show content preview in headlines list"), ""),
49                         "SORT_HEADLINES_BY_FEED_DATE" => array(__("Sort headlines by feed date"), __("Use feed-specified date to sort headlines instead of local import date.")),
50                         "SSL_CERT_SERIAL" => array(__("Login with an SSL certificate"), __("Click to register your SSL client certificate with tt-rss")),
51                         "STRIP_IMAGES" => array(__("Do not embed images in articles"), ""),
52                         "STRIP_UNSAFE_TAGS" => array(__("Strip unsafe tags from articles"), __("Strip all but most common HTML tags when reading articles.")),
53                         "USER_STYLESHEET" => array(__("Customize stylesheet"), __("Customize CSS stylesheet to your liking")),
54                         "USER_TIMEZONE" => array(__("Time zone"), ""),
55                         "VFEED_GROUP_BY_FEED" => array(__("Group headlines in virtual feeds"), __("Special feeds, labels, and categories are grouped by originating feeds")),
56                         "USER_LANGUAGE" => array(__("Language")),
57                         "USER_CSS_THEME" => array(__("Theme"), __("Select one of the available CSS themes"))
58                 );
59         }
60
61         function changepassword() {
62
63                 $old_pw = $_POST["old_password"];
64                 $new_pw = $_POST["new_password"];
65                 $con_pw = $_POST["confirm_password"];
66
67                 if ($old_pw == "") {
68                         print "ERROR: ".format_error("Old password cannot be blank.");
69                         return;
70                 }
71
72                 if ($new_pw == "") {
73                         print "ERROR: ".format_error("New password cannot be blank.");
74                         return;
75                 }
76
77                 if ($new_pw != $con_pw) {
78                         print "ERROR: ".format_error("Entered passwords do not match.");
79                         return;
80                 }
81
82                 $authenticator = PluginHost::getInstance()->get_plugin($_SESSION["auth_module"]);
83
84                 if (method_exists($authenticator, "change_password")) {
85                         print format_notice($authenticator->change_password($_SESSION["uid"], $old_pw, $new_pw));
86                 } else {
87                         print "ERROR: ".format_error("Function not supported by authentication module.");
88                 }
89         }
90
91         function saveconfig() {
92                 $boolean_prefs = explode(",", $_POST["boolean_prefs"]);
93
94                 foreach ($boolean_prefs as $pref) {
95                         if (!isset($_POST[$pref])) $_POST[$pref] = 'false';
96                 }
97
98                 $need_reload = false;
99
100                 foreach (array_keys($_POST) as $pref_name) {
101
102                         $pref_name = $this->dbh->escape_string($pref_name);
103                         $value = $this->dbh->escape_string($_POST[$pref_name]);
104
105                         if ($pref_name == 'DIGEST_PREFERRED_TIME') {
106                                 if (get_pref('DIGEST_PREFERRED_TIME') != $value) {
107
108                                         $this->dbh->query("UPDATE ttrss_users SET
109                                                 last_digest_sent = NULL WHERE id = " . $_SESSION['uid']);
110
111                                 }
112                         }
113
114                         if ($pref_name == "USER_LANGUAGE") {
115                                 if ($_SESSION["language"] != $value) {
116                                         $need_reload = true;
117                                 }
118                         }
119
120                         set_pref($pref_name, $value);
121                 }
122
123                 if ($need_reload) {
124                         print "PREFS_NEED_RELOAD";
125                 } else {
126                         print __("The configuration was saved.");
127                 }
128         }
129
130         function changeemail() {
131
132                 $email = $this->dbh->escape_string($_POST["email"]);
133                 $full_name = $this->dbh->escape_string($_POST["full_name"]);
134
135                 $active_uid = $_SESSION["uid"];
136
137                 $this->dbh->query("UPDATE ttrss_users SET email = '$email',
138                         full_name = '$full_name' WHERE id = '$active_uid'");
139
140                 print __("Your personal data has been saved.");
141
142                 return;
143         }
144
145         function resetconfig() {
146
147                 $_SESSION["prefs_op_result"] = "reset-to-defaults";
148
149                 if ($_SESSION["profile"]) {
150                         $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
151                 } else {
152                         $profile_qpart = "profile IS NULL";
153                 }
154
155                 $this->dbh->query("DELETE FROM ttrss_user_prefs
156                         WHERE $profile_qpart AND owner_uid = ".$_SESSION["uid"]);
157
158                 initialize_user_prefs($_SESSION["uid"], $_SESSION["profile"]);
159
160                 echo __("Your preferences are now set to default values.");
161         }
162
163         function index() {
164
165                 global $access_level_names;
166
167                 $prefs_blacklist = array("ALLOW_DUPLICATE_POSTS", "STRIP_UNSAFE_TAGS", "REVERSE_HEADLINES",
168                         "SORT_HEADLINES_BY_FEED_DATE", "DEFAULT_ARTICLE_LIMIT",
169                         "FEEDS_SORT_BY_UNREAD");
170
171                 /* "FEEDS_SORT_BY_UNREAD", "HIDE_READ_FEEDS", "REVERSE_HEADLINES" */
172
173                 $profile_blacklist = array("ALLOW_DUPLICATE_POSTS", "PURGE_OLD_DAYS",
174                                         "PURGE_UNREAD_ARTICLES", "DIGEST_ENABLE", "DIGEST_CATCHUP",
175                                         "BLACKLISTED_TAGS", "ENABLE_API_ACCESS", "UPDATE_POST_ON_CHECKSUM_CHANGE",
176                                         "DEFAULT_UPDATE_INTERVAL", "USER_TIMEZONE", "SORT_HEADLINES_BY_FEED_DATE",
177                                         "SSL_CERT_SERIAL", "DIGEST_PREFERRED_TIME");
178
179
180                 $_SESSION["prefs_op_result"] = "";
181
182                 print "<div dojoType=\"dijit.layout.AccordionContainer\" region=\"center\">";
183                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Personal data / Authentication')."\">";
184
185                 print "<form dojoType=\"dijit.form.Form\" id=\"changeUserdataForm\">";
186
187                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
188                 evt.preventDefault();
189                 if (this.validate()) {
190                         notify_progress('Saving data...', true);
191
192                         new Ajax.Request('backend.php', {
193                                 parameters: dojo.objectToQuery(this.getValues()),
194                                 onComplete: function(transport) {
195                                         notify_callback2(transport);
196                         } });
197
198                 }
199                 </script>";
200
201                 print "<table width=\"100%\" class=\"prefPrefsList\">";
202
203                 print "<h2>" . __("Personal data") . "</h2>";
204
205                 $result = $this->dbh->query("SELECT email,full_name,otp_enabled,
206                         access_level FROM ttrss_users
207                         WHERE id = ".$_SESSION["uid"]);
208
209                 $email = htmlspecialchars($this->dbh->fetch_result($result, 0, "email"));
210                 $full_name = htmlspecialchars($this->dbh->fetch_result($result, 0, "full_name"));
211                 $otp_enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "otp_enabled"));
212
213                 print "<tr><td width=\"40%\">".__('Full name')."</td>";
214                 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
215                         value=\"$full_name\"></td></tr>";
216
217                 print "<tr><td width=\"40%\">".__('E-mail')."</td>";
218                 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"email\" required=\"1\" value=\"$email\"></td></tr>";
219
220                 if (!SINGLE_USER_MODE && !$_SESSION["hide_hello"]) {
221
222                         $access_level = $this->dbh->fetch_result($result, 0, "access_level");
223                         print "<tr><td width=\"40%\">".__('Access level')."</td>";
224                         print "<td>" . $access_level_names[$access_level] . "</td></tr>";
225                 }
226
227                 print "</table>";
228
229                 print_hidden("op", "pref-prefs");
230                 print_hidden("method", "changeemail");
231
232                 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
233                         __("Save data")."</button>";
234
235                 print "</form>";
236
237                 if ($_SESSION["auth_module"]) {
238                         $authenticator = PluginHost::getInstance()->get_plugin($_SESSION["auth_module"]);
239                 } else {
240                         $authenticator = false;
241                 }
242
243                 if ($authenticator && method_exists($authenticator, "change_password")) {
244
245                         print "<h2>" . __("Password") . "</h2>";
246
247                         print "<div style='display : none' id='pwd_change_infobox'></div>";
248
249                         $result = $this->dbh->query("SELECT id FROM ttrss_users
250                                 WHERE id = ".$_SESSION["uid"]." AND pwd_hash
251                                 = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
252
253                         if ($this->dbh->num_rows($result) != 0) {
254                                 print format_warning(__("Your password is at default value, please change it."), "default_pass_warning");
255                         }
256
257                         print "<form dojoType=\"dijit.form.Form\">";
258
259                         print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
260                         evt.preventDefault();
261                         if (this.validate()) {
262                                 notify_progress('Changing password...', true);
263
264                                 new Ajax.Request('backend.php', {
265                                         parameters: dojo.objectToQuery(this.getValues()),
266                                         onComplete: function(transport) {
267                                                 notify('');
268                                                 if (transport.responseText.indexOf('ERROR: ') == 0) {
269
270                                                         $('pwd_change_infobox').innerHTML =
271                                                                 transport.responseText.replace('ERROR: ', '');
272
273                                                 } else {
274                                                         $('pwd_change_infobox').innerHTML =
275                                                                 transport.responseText.replace('ERROR: ', '');
276
277                                                         var warn = $('default_pass_warning');
278                                                         if (warn) Element.hide(warn);
279                                                 }
280
281                                                 new Effect.Appear('pwd_change_infobox');
282
283                                 }});
284                                 this.reset();
285                         }
286                         </script>";
287
288                         if ($otp_enabled) {
289                                 print_notice(__("Changing your current password will disable OTP."));
290                         }
291
292                         print "<table width=\"100%\" class=\"prefPrefsList\">";
293
294                         print "<tr><td width=\"40%\">".__("Old password")."</td>";
295                         print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"old_password\"></td></tr>";
296
297                         print "<tr><td width=\"40%\">".__("New password")."</td>";
298
299                         print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
300                                 name=\"new_password\"></td></tr>";
301
302                         print "<tr><td width=\"40%\">".__("Confirm password")."</td>";
303
304                         print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\" name=\"confirm_password\"></td></tr>";
305
306                         print "</table>";
307
308                         print_hidden("op", "pref-prefs");
309                         print_hidden("method", "changepassword");
310
311                         print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
312                                 __("Change password")."</button>";
313
314                         print "</form>";
315
316                         if ($_SESSION["auth_module"] == "auth_internal") {
317
318                                 print "<h2>" . __("One time passwords / Authenticator") . "</h2>";
319
320                                 if ($otp_enabled) {
321
322                                         print_notice(__("One time passwords are currently enabled. Enter your current password below to disable."));
323
324                                         print "<form dojoType=\"dijit.form.Form\">";
325
326                                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
327                                 evt.preventDefault();
328                                 if (this.validate()) {
329                                         notify_progress('Disabling OTP', true);
330
331                                         new Ajax.Request('backend.php', {
332                                                 parameters: dojo.objectToQuery(this.getValues()),
333                                                 onComplete: function(transport) {
334                                                         notify('');
335                                                         if (transport.responseText.indexOf('ERROR: ') == 0) {
336                                                                 notify_error(transport.responseText.replace('ERROR: ', ''));
337                                                         } else {
338                                                                 window.location.reload();
339                                                         }
340                                         }});
341                                         this.reset();
342                                 }
343                                 </script>";
344
345                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
346
347                                 print "<tr><td width=\"40%\">".__("Enter your password")."</td>";
348
349                                 print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
350                                         name=\"password\"></td></tr>";
351
352                                 print "</table>";
353
354                                 print_hidden("op", "pref-prefs");
355                                 print_hidden("method", "otpdisable");
356
357                                 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
358                                         __("Disable OTP")."</button>";
359
360                                 print "</form>";
361
362                                 } else if (function_exists("imagecreatefromstring")) {
363
364                                         print_warning(__("You will need a compatible Authenticator to use this. Changing your password would automatically disable OTP."));
365
366                                         print "<p>".__("Scan the following code by the Authenticator application:")."</p>";
367
368                                         $csrf_token = $_SESSION["csrf_token"];
369
370                                         print "<img src=\"backend.php?op=pref-prefs&method=otpqrcode&csrf_token=$csrf_token\">";
371
372                                         print "<form dojoType=\"dijit.form.Form\" id=\"changeOtpForm\">";
373
374                                         print_hidden("op", "pref-prefs");
375                                         print_hidden("method", "otpenable");
376
377                                         print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
378                                         evt.preventDefault();
379                                         if (this.validate()) {
380                                                 notify_progress('Saving data...', true);
381
382                                                 new Ajax.Request('backend.php', {
383                                                         parameters: dojo.objectToQuery(this.getValues()),
384                                                         onComplete: function(transport) {
385                                                                 notify('');
386                                                                 if (transport.responseText.indexOf('ERROR:') == 0) {
387                                                                         notify_error(transport.responseText.replace('ERROR:', ''));
388                                                                 } else {
389                                                                         window.location.reload();
390                                                                 }
391                                                 } });
392
393                                         }
394                                         </script>";
395
396                                         print "<table width=\"100%\" class=\"prefPrefsList\">";
397
398                                         print "<tr><td width=\"40%\">".__("Enter your password")."</td>";
399
400                                         print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
401                                                 name=\"password\"></td></tr>";
402
403                                         print "<tr><td width=\"40%\">".__("Enter the generated one time password")."</td>";
404
405                                         print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" autocomplete=\"off\"
406                                                 required=\"1\"
407                                                 name=\"otp\"></td></tr>";
408
409                                         print "<tr><td colspan=\"2\">";
410
411                                         print "</td></tr><tr><td colspan=\"2\">";
412
413                                         print "</td></tr>";
414                                         print "</table>";
415
416                                         print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
417                                                 __("Enable OTP")."</button>";
418
419                                         print "</form>";
420
421                                 } else {
422
423                                         print_notice(__("PHP GD functions are required for OTP support."));
424
425                                 }
426
427                         }
428                 }
429
430                 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
431                         "hook_prefs_tab_section", "prefPrefsAuth");
432
433                 print "</div>"; #pane
434
435                 print "<div dojoType=\"dijit.layout.AccordionPane\" selected=\"true\" title=\"".__('Preferences')."\">";
436
437                 print "<form dojoType=\"dijit.form.Form\" id=\"changeSettingsForm\">";
438
439                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt, quit\">
440                 if (evt) evt.preventDefault();
441                 if (this.validate()) {
442                         console.log(dojo.objectToQuery(this.getValues()));
443
444                         new Ajax.Request('backend.php', {
445                                 parameters: dojo.objectToQuery(this.getValues()),
446                                 onComplete: function(transport) {
447                                         var msg = transport.responseText;
448                                         if (quit) {
449                                                 gotoMain();
450                                         } else {
451                                                 if (msg == 'PREFS_NEED_RELOAD') {
452                                                         window.location.reload();
453                                                 } else {
454                                                         notify_info(msg);
455                                                 }
456                                         }
457                         } });
458                 }
459                 </script>";
460
461                 print '<div dojoType="dijit.layout.BorderContainer" gutters="false">';
462
463                 print '<div dojoType="dijit.layout.ContentPane" region="center" style="overflow-y : auto">';
464
465                 if ($_SESSION["profile"]) {
466                         print_notice(__("Some preferences are only available in default profile."));
467                 }
468
469                 if ($_SESSION["profile"]) {
470                         initialize_user_prefs($_SESSION["uid"], $_SESSION["profile"]);
471                         $profile_qpart = "profile = '" . $_SESSION["profile"] . "'";
472                 } else {
473                         initialize_user_prefs($_SESSION["uid"]);
474                         $profile_qpart = "profile IS NULL";
475                 }
476
477                 /* if ($_SESSION["prefs_show_advanced"])
478                         $access_query = "true";
479                 else
480                         $access_query = "(access_level = 0 AND section_id != 3)"; */
481
482                 $access_query = 'true';
483
484                 $result = $this->dbh->query("SELECT DISTINCT
485                         ttrss_user_prefs.pref_name,value,type_name,
486                         ttrss_prefs_sections.order_id,
487                         def_value,section_id
488                         FROM ttrss_prefs,ttrss_prefs_types,ttrss_prefs_sections,ttrss_user_prefs
489                         WHERE type_id = ttrss_prefs_types.id AND
490                                 $profile_qpart AND
491                                 section_id = ttrss_prefs_sections.id AND
492                                 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name AND
493                                 $access_query AND
494                                 owner_uid = ".$_SESSION["uid"]."
495                         ORDER BY ttrss_prefs_sections.order_id,pref_name");
496
497                 $lnum = 0;
498
499                 $active_section = "";
500
501                 $listed_boolean_prefs = array();
502
503                 while ($line = $this->dbh->fetch_assoc($result)) {
504
505                         if (in_array($line["pref_name"], $prefs_blacklist)) {
506                                 continue;
507                         }
508
509                         $type_name = $line["type_name"];
510                         $pref_name = $line["pref_name"];
511                         $section_name = $this->getSectionName($line["section_id"]);
512                         $value = $line["value"];
513
514                         $short_desc = $this->getShortDesc($pref_name);
515                         $help_text = $this->getHelpText($pref_name);
516
517                         if (!$short_desc) continue;
518
519                         if ($_SESSION["profile"] && in_array($line["pref_name"],
520                                         $profile_blacklist)) {
521                                 continue;
522                         }
523
524                         if ($active_section != $line["section_id"]) {
525
526                                 if ($active_section != "") {
527                                         print "</table>";
528                                 }
529
530                                 print "<table width=\"100%\" class=\"prefPrefsList\">";
531
532                                 $active_section = $line["section_id"];
533
534                                 print "<tr><td colspan=\"3\"><h3>".$section_name."</h3></td></tr>";
535
536                                 $lnum = 0;
537                         }
538
539                         print "<tr>";
540
541                         print "<td width=\"40%\" class=\"prefName\" id=\"$pref_name\">";
542                         print "<label for='CB_$pref_name'>";
543                         print $short_desc;
544                         print "</label>";
545
546                         if ($help_text) print "<div class=\"prefHelp\">".__($help_text)."</div>";
547
548                         print "</td>";
549
550                         print "<td class=\"prefValue\">";
551
552                         if ($pref_name == "USER_LANGUAGE") {
553                                 print_select_hash($pref_name, $value, get_translations(),
554                                         "style='width : 220px; margin : 0px' dojoType='dijit.form.Select'");
555
556                         } else if ($pref_name == "USER_TIMEZONE") {
557
558                                 $timezones = explode("\n", file_get_contents("lib/timezones.txt"));
559
560                                 print_select($pref_name, $value, $timezones, 'dojoType="dijit.form.FilteringSelect"');
561                         } else if ($pref_name == "USER_STYLESHEET") {
562
563                                 print "<button dojoType=\"dijit.form.Button\"
564                                         onclick=\"customizeCSS()\">" . __('Customize') . "</button>";
565
566                         } else if ($pref_name == "USER_CSS_THEME") {
567
568                                 $themes = array_merge(glob("themes/*.php"), glob("themes/*.css"), glob("themes.local/*.css"));
569                                 $themes = array_map("basename", $themes);
570                                 $themes = array_filter($themes, "theme_valid");
571                                 asort($themes);
572
573                                 if (!theme_valid($value)) $value = "default.php";
574
575                                 print_select($pref_name, $value, $themes,
576                                         'dojoType="dijit.form.Select"');
577
578
579                         } else if ($pref_name == "DEFAULT_UPDATE_INTERVAL") {
580
581                                 global $update_intervals_nodefault;
582
583                                 print_select_hash($pref_name, $value, $update_intervals_nodefault,
584                                         'dojoType="dijit.form.Select"');
585
586                         } else if ($type_name == "bool") {
587
588                                 array_push($listed_boolean_prefs, $pref_name);
589
590                                 $checked = ($value == "true") ? "checked=\"checked\"" : "";
591
592                                 if ($pref_name == "PURGE_UNREAD_ARTICLES" && FORCE_ARTICLE_PURGE != 0) {
593                                         $disabled = "disabled=\"1\"";
594                                         $checked = "checked=\"checked\"";
595                                 } else {
596                                         $disabled = "";
597                                 }
598
599                                 print "<input type='checkbox' name='$pref_name' $checked $disabled
600                                         dojoType='dijit.form.CheckBox' id='CB_$pref_name' value='1'>";
601
602                         } else if (array_search($pref_name, array('FRESH_ARTICLE_MAX_AGE',
603                                         'PURGE_OLD_DAYS', 'LONG_DATE_FORMAT', 'SHORT_DATE_FORMAT')) !== false) {
604
605                                 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
606
607                                 if ($pref_name == "PURGE_OLD_DAYS" && FORCE_ARTICLE_PURGE != 0) {
608                                         $disabled = "disabled=\"1\"";
609                                         $value = FORCE_ARTICLE_PURGE;
610                                 } else {
611                                         $disabled = "";
612                                 }
613
614                                 print "<input dojoType=\"dijit.form.ValidationTextBox\"
615                                         required=\"1\" $regexp $disabled
616                                         name=\"$pref_name\" value=\"$value\">";
617
618                         } else if ($pref_name == "SSL_CERT_SERIAL") {
619
620                                 print "<input dojoType=\"dijit.form.ValidationTextBox\"
621                                         id=\"SSL_CERT_SERIAL\" readonly=\"1\"
622                                         name=\"$pref_name\" value=\"$value\">";
623
624                                 $cert_serial = htmlspecialchars(get_ssl_certificate_id());
625                                 $has_serial = ($cert_serial) ? "false" : "true";
626
627                                 print "<br/>";
628
629                                 print " <button dojoType=\"dijit.form.Button\" disabled=\"$has_serial\"
630                                         onclick=\"insertSSLserial('$cert_serial')\">" .
631                                         __('Register') . "</button>";
632
633                                 print " <button dojoType=\"dijit.form.Button\"
634                                         onclick=\"insertSSLserial('')\">" .
635                                         __('Clear') . "</button>";
636
637                         } else if ($pref_name == 'DIGEST_PREFERRED_TIME') {
638                                 print "<input dojoType=\"dijit.form.ValidationTextBox\"
639                                         id=\"$pref_name\" regexp=\"[012]?\d:\d\d\" placeHolder=\"12:00\"
640                                         name=\"$pref_name\" value=\"$value\"><div class=\"insensitive\">".
641                                         T_sprintf("Current server time: %s (UTC)", date("H:i")) . "</div>";
642                         } else {
643                                 $regexp = ($type_name == 'integer') ? 'regexp="^\d*$"' : '';
644
645                                 print "<input dojoType=\"dijit.form.ValidationTextBox\"
646                                         $regexp
647                                         name=\"$pref_name\" value=\"$value\">";
648                         }
649
650                         print "</td>";
651
652                         print "</tr>";
653
654                         $lnum++;
655                 }
656
657                 print "</table>";
658
659                 $listed_boolean_prefs = htmlspecialchars(join(",", $listed_boolean_prefs));
660
661                 print_hidden("boolean_prefs", "$listed_boolean_prefs");
662
663                 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
664                         "hook_prefs_tab_section", "prefPrefsPrefsInside");
665
666                 print '</div>'; # inside pane
667                 print '<div dojoType="dijit.layout.ContentPane" region="bottom">';
668
669                 print_hidden("op", "pref-prefs");
670                 print_hidden("method", "saveconfig");
671
672                 print "<div dojoType=\"dijit.form.ComboButton\" type=\"submit\">
673                         <span>".__('Save configuration')."</span>
674                         <div dojoType=\"dijit.DropDownMenu\">
675                                 <div dojoType=\"dijit.MenuItem\"
676                                         onclick=\"dijit.byId('changeSettingsForm').onSubmit(null, true)\">".
677                                 __("Save and exit preferences")."</div>
678                         </div>
679                         </div>";
680
681                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editProfiles()\">".
682                         __('Manage profiles')."</button> ";
683
684                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return validatePrefsReset()\">".
685                         __('Reset to defaults')."</button>";
686
687                 print "&nbsp;";
688
689                 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB_SECTION,
690                         "hook_prefs_tab_section", "prefPrefsPrefsOutside");
691
692                 print "</form>";
693                 print '</div>'; # inner pane
694                 print '</div>'; # border container
695
696                 print "</div>"; #pane
697
698                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Plugins')."\">";
699
700                 print "<p>" . __("You will need to reload Tiny Tiny RSS for plugin changes to take effect.") . "</p>";
701
702                 print_notice(__("Download more plugins at tt-rss.org <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/forum/viewforum.php?f=22\">forums</a> or <a target=\"_blank\" class=\"visibleLink\" href=\"http://tt-rss.org/wiki/Plugins\">wiki</a>."));
703
704                 if (ini_get("open_basedir") && function_exists("curl_init") && !defined("NO_CURL")) {
705                         print_warning("Your PHP configuration has open_basedir restrictions enabled. Some plugins relying on CURL for functionality may not work correctly.");
706                 }
707
708                 print "<form dojoType=\"dijit.form.Form\" id=\"changePluginsForm\">";
709
710                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
711                 evt.preventDefault();
712                 if (this.validate()) {
713                         notify_progress('Saving data...', true);
714
715                         new Ajax.Request('backend.php', {
716                                 parameters: dojo.objectToQuery(this.getValues()),
717                                 onComplete: function(transport) {
718                                         notify('');
719                                         if (confirm(__('Selected plugins have been enabled. Reload?'))) {
720                                                 window.location.reload();
721                                         }
722                         } });
723
724                 }
725                 </script>";
726
727                 print_hidden("op", "pref-prefs");
728                 print_hidden("method", "setplugins");
729
730                 print "<table width='100%' class='prefPluginsList'>";
731
732                 print "<tr><td colspan='4'><h3>".__("System plugins")."</h3></td></tr>";
733
734                 print "<tr class=\"title\">
735                                 <td width=\"5%\">&nbsp;</td>
736                                 <td width='10%'>".__('Plugin')."</td>
737                                 <td width=''>".__('Description')."</td>
738                                 <td width='5%'>".__('Version')."</td>
739                                 <td width='10%'>".__('Author')."</td></tr>";
740
741                 $system_enabled = array_map("trim", explode(",", PLUGINS));
742                 $user_enabled = array_map("trim", explode(",", get_pref("_ENABLED_PLUGINS")));
743
744                 $tmppluginhost = new PluginHost();
745                 $tmppluginhost->load_all($tmppluginhost::KIND_ALL, $_SESSION["uid"], true);
746                 $tmppluginhost->load_data(true);
747
748                 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
749                         $about = $plugin->about();
750
751                         if ($about[3]) {
752                                 if (in_array($name, $system_enabled)) {
753                                         $checked = "checked='1'";
754                                 } else {
755                                         $checked = "";
756                                 }
757
758                                 print "<tr>";
759
760                                 print "<td align='center'><input disabled='1'
761                                                 dojoType=\"dijit.form.CheckBox\" $checked
762                                                 type=\"checkbox\"></td>";
763
764                                 $plugin_icon = $checked ? "plugin.png" : "plugin_disabled.png";
765
766                                 print "<td><label><img src='images/$plugin_icon' alt=''> $name</label></td>";
767                                 print "<td>" . htmlspecialchars($about[1]);
768                                 if (@$about[4]) {
769                                         print " &mdash; <a target=\"_blank\" rel=\"noopener noreferrer\" class=\"visibleLink\"
770                                                 href=\"".htmlspecialchars($about[4])."\">".__("more info")."</a>";
771                                 }
772                                 print "</td>";
773                                 print "<td>" . htmlspecialchars(sprintf("%.2f", $about[0])) . "</td>";
774                                 print "<td>" . htmlspecialchars($about[2]) . "</td>";
775
776                                 if (count($tmppluginhost->get_all($plugin)) > 0) {
777                                         if (in_array($name, $system_enabled)) {
778                                                 print "<td><a href='#' onclick=\"clearPluginData('$name')\"
779                                                         class='visibleLink'>".__("Clear data")."</a></td>";
780                                         }
781                                 }
782
783                                 print "</tr>";
784
785                         }
786                 }
787
788                 print "<tr><td colspan='4'><h3>".__("User plugins")."</h3></td></tr>";
789
790                 print "<tr class=\"title\">
791                                 <td width=\"5%\">&nbsp;</td>
792                                 <td width='10%'>".__('Plugin')."</td>
793                                 <td width=''>".__('Description')."</td>
794                                 <td width='5%'>".__('Version')."</td>
795                                 <td width='10%'>".__('Author')."</td></tr>";
796
797
798                 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
799                         $about = $plugin->about();
800
801                         if (!$about[3]) {
802
803                                 if (in_array($name, $system_enabled)) {
804                                         $checked = "checked='1'";
805                                         $disabled = "disabled='1'";
806                                         $rowclass = '';
807                                 } else if (in_array($name, $user_enabled)) {
808                                         $checked = "checked='1'";
809                                         $disabled = "";
810                                         $rowclass = "Selected";
811                                 } else {
812                                         $checked = "";
813                                         $disabled = "";
814                                         $rowclass = '';
815                                 }
816
817                                 print "<tr class='$rowclass'>";
818
819                                 $plugin_icon = $checked ? "plugin.png" : "plugin_disabled.png";
820
821                                 print "<td align='center'><input id='FPCHK-$name' name='plugins[]' value='$name' onclick='toggleSelectRow2(this);'
822                                         dojoType=\"dijit.form.CheckBox\" $checked $disabled
823                                         type=\"checkbox\"></td>";
824
825                                 print "<td><label for='FPCHK-$name'><img src='images/$plugin_icon' alt=''> $name</label></td>";
826                                 print "<td><label for='FPCHK-$name'>" . htmlspecialchars($about[1]) . "</label>";
827                                 if (@$about[4]) {
828                                         print " &mdash; <a target=\"_blank\" rel=\"noopener noreferrer\" class=\"visibleLink\"
829                                                 href=\"".htmlspecialchars($about[4])."\">".__("more info")."</a>";
830                                 }
831                                 print "</td>";
832
833                                 print "<td>" . htmlspecialchars(sprintf("%.2f", $about[0])) . "</td>";
834                                 print "<td>" . htmlspecialchars($about[2]) . "</td>";
835
836                                 if (count($tmppluginhost->get_all($plugin)) > 0) {
837                                         if (in_array($name, $system_enabled) || in_array($name, $user_enabled)) {
838                                                 print "<td><a href='#' onclick=\"clearPluginData('$name')\" class='visibleLink'>".__("Clear data")."</a></td>";
839                                         }
840                                 }
841
842                                 print "</tr>";
843
844
845
846                         }
847
848                 }
849
850                 print "</table>";
851
852                 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
853                         __("Enable selected plugins")."</button></p>";
854
855                 print "</form>";
856
857                 print "</div>"; #pane
858
859                 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
860                         "hook_prefs_tab", "prefPrefs");
861
862                 print "</div>"; #container
863         }
864
865         function toggleAdvanced() {
866                 $_SESSION["prefs_show_advanced"] = !$_SESSION["prefs_show_advanced"];
867         }
868
869         function otpqrcode() {
870                 require_once "lib/otphp/vendor/base32.php";
871                 require_once "lib/otphp/lib/otp.php";
872                 require_once "lib/otphp/lib/totp.php";
873                 require_once "lib/phpqrcode/phpqrcode.php";
874
875                 $result = $this->dbh->query("SELECT login,salt,otp_enabled
876                         FROM ttrss_users
877                         WHERE id = ".$_SESSION["uid"]);
878
879                 $base32 = new Base32();
880
881                 $login = $this->dbh->fetch_result($result, 0, "login");
882                 $otp_enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "otp_enabled"));
883
884                 if (!$otp_enabled) {
885                         $secret = $base32->encode(sha1($this->dbh->fetch_result($result, 0, "salt")));
886               print QRcode::png("otpauth://totp/".urlencode($login).
887                                 "?secret=$secret&issuer=".urlencode("Tiny Tiny RSS"));
888
889                 }
890         }
891
892         function otpenable() {
893                 require_once "lib/otphp/vendor/base32.php";
894                 require_once "lib/otphp/lib/otp.php";
895                 require_once "lib/otphp/lib/totp.php";
896
897                 $password = $_REQUEST["password"];
898                 $otp = $_REQUEST["otp"];
899
900                 $authenticator = PluginHost::getInstance()->get_plugin($_SESSION["auth_module"]);
901
902                 if ($authenticator->check_password($_SESSION["uid"], $password)) {
903
904                         $result = $this->dbh->query("SELECT salt
905                                 FROM ttrss_users
906                                 WHERE id = ".$_SESSION["uid"]);
907
908                         $base32 = new Base32();
909
910                         $secret = $base32->encode(sha1($this->dbh->fetch_result($result, 0, "salt")));
911                         $topt = new \OTPHP\TOTP($secret);
912
913                         $otp_check = $topt->now();
914
915                         if ($otp == $otp_check) {
916                                 $this->dbh->query("UPDATE ttrss_users SET otp_enabled = true WHERE
917                                         id = " . $_SESSION["uid"]);
918
919                                 print "OK";
920                         } else {
921                                 print "ERROR:".__("Incorrect one time password");
922                         }
923                 } else {
924                         print "ERROR:".__("Incorrect password");
925                 }
926
927         }
928
929         function otpdisable() {
930                 $password = $this->dbh->escape_string($_REQUEST["password"]);
931
932                 $authenticator = PluginHost::getInstance()->get_plugin($_SESSION["auth_module"]);
933
934                 if ($authenticator->check_password($_SESSION["uid"], $password)) {
935
936                         $this->dbh->query("UPDATE ttrss_users SET otp_enabled = false WHERE
937                                 id = " . $_SESSION["uid"]);
938
939                         print "OK";
940                 } else {
941                         print "ERROR: ".__("Incorrect password");
942                 }
943
944         }
945
946         function setplugins() {
947                 if (is_array($_REQUEST["plugins"]))
948                         $plugins = join(",", $_REQUEST["plugins"]);
949                 else
950                         $plugins = "";
951
952                 set_pref("_ENABLED_PLUGINS", $plugins);
953         }
954
955         function clearplugindata() {
956                 $name = $this->dbh->escape_string($_REQUEST["name"]);
957
958                 PluginHost::getInstance()->clear_data(PluginHost::getInstance()->get_plugin($name));
959         }
960
961         function customizeCSS() {
962                 $value = get_pref("USER_STYLESHEET");
963
964                 $value = str_replace("<br/>", "\n", $value);
965
966                 print_notice(T_sprintf("You can override colors, fonts and layout of your currently selected theme with custom CSS declarations here. <a target=\"_blank\" class=\"visibleLink\" href=\"%s\">This file</a> can be used as a baseline.", "css/tt-rss.css"));
967
968                 print_hidden("op", "rpc");
969                 print_hidden("method", "setpref");
970                 print_hidden("key", "USER_STYLESHEET");
971
972                 print "<table width='100%'><tr><td>";
973                 print "<textarea dojoType=\"dijit.form.SimpleTextarea\"
974                         style='font-size : 12px; width : 98%; height: 200px;'
975                         placeHolder='body#ttrssMain { font-size : 14px; };'
976                         name='value'>$value</textarea>";
977                 print "</td></tr></table>";
978
979                 print "<div class='dlgButtons'>";
980                 print "<button dojoType=\"dijit.form.Button\"
981                         onclick=\"dijit.byId('cssEditDlg').execute()\">".__('Save')."</button> ";
982                 print "<button dojoType=\"dijit.form.Button\"
983                         onclick=\"dijit.byId('cssEditDlg').hide()\">".__('Cancel')."</button>";
984                 print "</div>";
985
986         }
987
988         function editPrefProfiles() {
989                 print "<div dojoType=\"dijit.Toolbar\">";
990
991                 print "<div dojoType=\"dijit.form.DropDownButton\">".
992                                 "<span>" . __('Select')."</span>";
993                 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
994                 print "<div onclick=\"selectTableRows('prefFeedProfileList', 'all')\"
995                         dojoType=\"dijit.MenuItem\">".__('All')."</div>";
996                 print "<div onclick=\"selectTableRows('prefFeedProfileList', 'none')\"
997                         dojoType=\"dijit.MenuItem\">".__('None')."</div>";
998                 print "</div></div>";
999
1000                 print "<div style=\"float : right\">";
1001
1002                 print "<input name=\"newprofile\" dojoType=\"dijit.form.ValidationTextBox\"
1003                                 required=\"1\">
1004                         <button dojoType=\"dijit.form.Button\"
1005                         onclick=\"dijit.byId('profileEditDlg').addProfile()\">".
1006                                 __('Create profile')."</button></div>";
1007
1008                 print "</div>";
1009
1010                 $result = $this->dbh->query("SELECT title,id FROM ttrss_settings_profiles
1011                         WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1012
1013                 print "<div class=\"prefProfileHolder\">";
1014
1015                 print "<form id=\"profile_edit_form\" onsubmit=\"return false\">";
1016
1017                 print "<table width=\"100%\" class=\"prefFeedProfileList\"
1018                         cellspacing=\"0\" id=\"prefFeedProfileList\">";
1019
1020                 print "<tr class=\"placeholder\" id=\"FCATR-0\">"; #odd
1021
1022                 print "<td width='5%' align='center'><input
1023                         id='FCATC-0'
1024                         onclick='toggleSelectRow2(this);'
1025                         dojoType=\"dijit.form.CheckBox\"
1026                         type=\"checkbox\"></td>";
1027
1028                 if (!$_SESSION["profile"]) {
1029                         $is_active = __("(active)");
1030                 } else {
1031                         $is_active = "";
1032                 }
1033
1034                 print "<td><span>" .
1035                         __("Default profile") . " $is_active</span></td>";
1036
1037                 print "</tr>";
1038
1039                 $lnum = 1;
1040
1041                 while ($line = $this->dbh->fetch_assoc($result)) {
1042
1043                         $profile_id = $line["id"];
1044                         $this_row_id = "id=\"FCATR-$profile_id\"";
1045
1046                         print "<tr class=\"placeholder\" $this_row_id>";
1047
1048                         $edit_title = htmlspecialchars($line["title"]);
1049
1050                         print "<td width='5%' align='center'><input
1051                                 onclick='toggleSelectRow2(this);'
1052                                 id='FCATC-$profile_id'
1053                                 dojoType=\"dijit.form.CheckBox\"
1054                                 type=\"checkbox\"></td>";
1055
1056                         if ($_SESSION["profile"] == $line["id"]) {
1057                                 $is_active = __("(active)");
1058                         } else {
1059                                 $is_active = "";
1060                         }
1061
1062                         print "<td><span dojoType=\"dijit.InlineEditBox\"
1063                                 width=\"300px\" autoSave=\"false\"
1064                                 profile-id=\"$profile_id\">" . $edit_title .
1065                                 "<script type=\"dojo/method\" event=\"onChange\" args=\"item\">
1066                                         var elem = this;
1067                                         dojo.xhrPost({
1068                                                 url: 'backend.php',
1069                                                 content: {op: 'rpc', method: 'saveprofile',
1070                                                         value: this.value,
1071                                                         id: this.srcNodeRef.getAttribute('profile-id')},
1072                                                         load: function(response) {
1073                                                                 elem.attr('value', response);
1074                                                 }
1075                                         });
1076                                 </script>
1077                         </span> $is_active</td>";
1078
1079                         print "</tr>";
1080
1081                         ++$lnum;
1082                 }
1083
1084                 print "</table>";
1085                 print "</form>";
1086                 print "</div>";
1087
1088                 print "<div class='dlgButtons'>
1089                         <div style='float : left'>
1090                         <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('profileEditDlg').removeSelected()\">".
1091                         __('Remove selected profiles')."</button>
1092                         <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('profileEditDlg').activateProfile()\">".
1093                         __('Activate profile')."</button>
1094                         </div>";
1095
1096                 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('profileEditDlg').hide()\">".
1097                         __('Close this window')."</button>";
1098                 print "</div>";
1099
1100         }
1101
1102         private function getShortDesc($pref_name) {
1103                 if (isset($this->pref_help[$pref_name])) {
1104                         return $this->pref_help[$pref_name][0];
1105                 }
1106                 return "";
1107         }
1108
1109         private function getHelpText($pref_name) {
1110                 if (isset($this->pref_help[$pref_name])) {
1111                         return $this->pref_help[$pref_name][1];
1112                 }
1113                 return "";
1114         }
1115
1116         private function getSectionName($id) {
1117                 if (isset($this->pref_sections[$id])) {
1118                         return $this->pref_sections[$id];
1119                 }
1120
1121                 return "";
1122         }
1123 }
1124 ?>