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