settings.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. require_once "misc/search_engine.php";
  3. // Reset all cookies when resetting, or before saving new cookies
  4. if (isset($_REQUEST["reset"])) {
  5. // if (isset($_REQUEST["reset"]) || isset($_REQUEST["save"])) {
  6. // Removing isset($_REQUEST["save"])) fixes the problem that settings don't "stick" if you go back into settings page to make additional changes.
  7. if (isset($_SERVER["HTTP_COOKIE"])) {
  8. $cookies = explode(";", $_SERVER["HTTP_COOKIE"]);
  9. foreach($cookies as $cookie) {
  10. $parts = explode("=", $cookie);
  11. $name = trim($parts[0]);
  12. $domain = parse_url($_SERVER['SERVER_NAME']);
  13. setcookie($name, "", time() - 1000);
  14. }
  15. }
  16. }
  17. if (isset($_REQUEST["save"])) {
  18. foreach($_POST as $key=>$value) {
  19. if (!empty($value)) {
  20. setcookie($key, $value, [
  21. "expires" => time() + (86400 * 90), // Sets cookie to expire in 90 days
  22. "path" => "/",
  23. "domain" => "$domain",
  24. "secure" => true, // Ensure cookies are only sent over HTTPS
  25. "httponly" => true, // Prevent client-side JavaScript access to cookies
  26. "samesite" => "Strict" // Strict SameSite policy for better protection against CSRF attacks
  27. ]);
  28. } else {
  29. setcookie($key, "", time() - 1000);
  30. }
  31. }
  32. }
  33. if (isset($_REQUEST["save"]) || isset($_REQUEST["reset"])) {
  34. header("Location: ./");
  35. die();
  36. }
  37. require_once "misc/header.php";
  38. $opts = load_opts();
  39. ?>
  40. <title>LibreY - <?php printtext("settings_title");?></title>
  41. </head>
  42. <body>
  43. <div class="misc-container">
  44. <h1><?php printtext("settings_title");?></h1>
  45. <div class="instances-container">
  46. <form method="post" enctype="multipart/form-data" autocomplete="off">
  47. <div>
  48. <span><?php printtext("settings_preferred_engine");?></span>
  49. <select name="engine">
  50. <?php
  51. require_once "engines/text/text.php";
  52. $engines = get_engines();
  53. $options = "";
  54. $options .= "<option value=\"\" " . (!isset($opts->engine) ? "selected" : "") . ">auto</option>";
  55. foreach ($engines as $engine) {
  56. $selected = $opts->engine == $engine ? "selected" : "";
  57. $options .= "<option value=\"$engine\" $selected>$engine</option>";
  58. }
  59. echo $options;
  60. ?>
  61. </select>
  62. </div>
  63. <div class="instances-container">
  64. <div>
  65. <span><?php printtext("settings_language");?></span>
  66. <select name="language">
  67. <?php
  68. $languages = json_decode(file_get_contents("static/misc/languages.json"), true);
  69. $options = "";
  70. $options .= "<option value=\"\" " . (!isset($opts->language) ? "selected" : "") . ">Any</option>";
  71. foreach ($languages as $lang_code => $language) {
  72. $name = $language["name"];
  73. $selected = $opts->language == $lang_code ? "selected" : "";
  74. $options .= "<option value=\"$lang_code\" $selected>$name</option>";
  75. }
  76. echo $options;
  77. ?>
  78. </select>
  79. </div>
  80. <div>
  81. <label><?php printtext("settings_number_of_results");?></label>
  82. <input type="number" name="number_of_results" value="<?php echo htmlspecialchars($opts->number_of_results ?? "10") ?>" >
  83. </div> </div>
  84. <div>
  85. <div><label><?php printtext("settings_special_disabled");?></label>
  86. <input type="checkbox" name="disable_special" <?php echo $opts->disable_special ? "checked" : ""; ?> ></div>
  87. <div><label><?php printtext("settings_frontends_disable");?></label>
  88. <input type="checkbox" name="disable_frontends" <?php echo $opts->disable_frontends ? "checked" : ""; ?> ></div>
  89. <div><label><?php printtext("settings_safe_search");?></label>
  90. <input type="checkbox" name="safe_search" <?php echo $opts->safe_search ? "checked" : ""; ?> ></div>
  91. </div>
  92. </div>
  93. <h2><?php printtext("settings_frontends");?></h2>
  94. <p><?php printtext("settings_frontends_description");?></p>
  95. <div class="instances-container">
  96. <?php
  97. foreach($opts->frontends as $frontend => $data)
  98. {
  99. echo "<div>";
  100. echo "<a for=\"$frontend\" href=\"" . $data["project_url"] . "\" target=\"_blank\">" . ucfirst($frontend) . "</a>";
  101. echo "<input type=\"text\" name=\"$frontend\" placeholder=\"Replace " . $data["original_name"] . "\" value=";
  102. echo htmlspecialchars($opts->frontends["$frontend"]["instance_url"] ?? "");
  103. echo ">";
  104. echo "</div>";
  105. }
  106. ?>
  107. </div>
  108. <div>
  109. <button type="submit" name="save" value="1"><?php printtext("settings_save");?></button>
  110. <button type="submit" name="reset" value="1"><?php printtext("settings_reset");?></button>
  111. </div>
  112. </form>
  113. </div>
  114. <?php require_once "misc/footer.php"; ?>