search_engine.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. require_once "misc/cooldowns.php";
  3. abstract class EngineRequest {
  4. protected $url, $query, $page, $opts, $mh, $ch;
  5. protected $DO_CACHING = true;
  6. public function __construct($opts, $mh) {
  7. $this->query = $opts->query;
  8. $this->page = $opts->page;
  9. $this->mh = $mh;
  10. $this->opts = $opts;
  11. $this->url = $this->get_request_url();
  12. if (!$this->url)
  13. return;
  14. if (has_cached_results($this->url))
  15. return;
  16. $this->ch = curl_init($this->url);
  17. if ($opts->curl_settings)
  18. curl_setopt_array($this->ch, $opts->curl_settings);
  19. if ($mh)
  20. curl_multi_add_handle($mh, $this->ch);
  21. }
  22. public function get_request_url() {
  23. return "";
  24. }
  25. public function successful() {
  26. return (isset($this->ch) && curl_getinfo($this->ch)['http_code'] == '200')
  27. || has_cached_results($this->url);
  28. }
  29. abstract function parse_results($response);
  30. public function get_results() {
  31. if (!isset($this->url))
  32. return $this->parse_results(null);
  33. if ($this->DO_CACHING && has_cached_results($this->url))
  34. return fetch_cached_results($this->url);
  35. if (!isset($this->ch))
  36. return $this->parse_results(null);
  37. $response = $this->mh ? curl_multi_getcontent($this->ch) : curl_exec($this->ch);
  38. $results = $this->parse_results($response) ?? array();
  39. if ($this->DO_CACHING && !empty($results))
  40. store_cached_results($this->url, $results, $this->opts->cache_time * 60);
  41. return $results;
  42. }
  43. public static function print_results($results, $opts) {}
  44. }
  45. function load_opts() {
  46. if (isset($GLOBALS["opts"]))
  47. $opts = $GLOBALS["opts"];
  48. else
  49. $opts = require_once "config.php";
  50. # account for the old, misspelled options
  51. if (isset($opts->disable_bittorent_search))
  52. $opts->disable_bittorrent_search = $opts->disable_bittorent_search;
  53. if (isset($opts->bittorent_trackers))
  54. $opts->bittorrent_trackers = $opts->bittorent_trackers;
  55. $opts->request_cooldown ??= 25;
  56. $opts->cache_time ??= 25;
  57. $opts->query = trim($_REQUEST["q"] ?? "");
  58. $opts->type = (int) ($_REQUEST["t"] ?? 0);
  59. $opts->page = (int) ($_REQUEST["p"] ?? 0);
  60. $opts->theme = $_REQUEST["theme"] ?? trim(htmlspecialchars($_COOKIE["theme"] ?? $opts->default_theme ?? "dark"));
  61. $opts->safe_search = (int) ($_REQUEST["safe"] ?? 0) == 1 || isset($_COOKIE["safe_search"]);
  62. $opts->disable_special = (int) ($_REQUEST["ns"] ?? 0) == 1 || isset($_COOKIE["disable_special"]);
  63. $opts->disable_frontends = (int) ($_REQUEST["nf"] ?? 0) == 1 || isset($_COOKIE["disable_frontends"]);
  64. $opts->language = $_REQUEST["lang"] ?? trim(htmlspecialchars($_COOKIE["language"] ?? $opts->language ?? "en"));
  65. $opts->do_fallback = (int) ($_REQUEST["nfb"] ?? 0) == 0;
  66. if (!$opts->instance_fallback) {
  67. $opts->do_fallback = false;
  68. }
  69. $opts->number_of_results ??= trim(htmlspecialchars($_COOKIE["number_of_results"]));
  70. foreach (array_keys($opts->frontends ?? array()) as $frontend) {
  71. $opts->frontends[$frontend]["instance_url"] = $_COOKIE[$frontend] ?? $opts->frontends[$frontend]["instance_url"];
  72. }
  73. $opts->curl_settings[CURLOPT_FOLLOWLOCATION] ??= true;
  74. $opts->engine = $_REQUEST["engine"] ?? $_COOKIE["engine"] ?? $opts->preferred_engines["text"] ?? "auto";
  75. return $opts;
  76. }
  77. function opts_to_params($opts) {
  78. $query = urlencode($opts->query);
  79. $params = "";
  80. $params .= "p=$opts->page";
  81. $params .= "&q=$query";
  82. $params .= "&t=$opts->type";
  83. $params .= "&safe=" . ($opts->safe_search ? 1 : 0);
  84. $params .= "&nf=" . ($opts->disable_frontends ? 1 : 0);
  85. $params .= "&ns=" . ($opts->disable_special ? 1 : 0);
  86. $params .= "&engine=" . ($opts->engine ?? "auto");
  87. return $params;
  88. }
  89. function init_search($opts, $mh) {
  90. switch ($opts->type)
  91. {
  92. case 1:
  93. require_once "engines/qwant/image.php";
  94. return new QwantImageSearch($opts, $mh);
  95. case 2:
  96. require_once "engines/invidious/video.php";
  97. return new VideoSearch($opts, $mh);
  98. case 3:
  99. if ($opts->disable_bittorrent_search) {
  100. echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>";
  101. break;
  102. }
  103. require_once "engines/bittorrent/merge.php";
  104. return new TorrentSearch($opts, $mh);
  105. case 4:
  106. if ($opts->disable_hidden_service_search) {
  107. echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>";
  108. break;
  109. }
  110. require_once "engines/ahmia/hidden_service.php";
  111. return new TorSearch($opts, $mh);
  112. case 5:
  113. require_once "engines/maps/openstreetmap.php";
  114. return new OSMRequest($opts, $mh);
  115. default:
  116. require_once "engines/text/text.php";
  117. return new TextSearch($opts, $mh);
  118. }
  119. }
  120. function fetch_search_results($opts, $do_print) {
  121. $opts->cooldowns = load_cooldowns();
  122. $start_time = microtime(true);
  123. $mh = curl_multi_init();
  124. $search_category = init_search($opts, $mh);
  125. $running = null;
  126. do {
  127. curl_multi_exec($mh, $running);
  128. } while ($running);
  129. $results = $search_category->get_results();
  130. if (empty($results)) {
  131. require_once "engines/librex/fallback.php";
  132. $results = get_librex_results($opts);
  133. }
  134. if (!$do_print || empty($results))
  135. return $results;
  136. print_elapsed_time($start_time, $results, $opts);
  137. $search_category->print_results($results, $opts);
  138. return $results;
  139. }
  140. ?>