2023-08-21 18:28:02 -04:00
|
|
|
<?php
|
|
|
|
abstract class EngineRequest {
|
2023-08-22 13:41:02 -04:00
|
|
|
function __construct($opts, $mh) {
|
|
|
|
$this->query = $opts->query;
|
|
|
|
$this->page = $opts->page;
|
|
|
|
$this->opts = $opts;
|
2023-08-21 18:28:02 -04:00
|
|
|
|
|
|
|
$url = $this->get_request_url();
|
2023-08-24 10:45:45 -04:00
|
|
|
if (!$url)
|
|
|
|
return;
|
2023-08-22 19:38:59 -04:00
|
|
|
|
2023-08-24 10:45:45 -04:00
|
|
|
$this->ch = curl_init($url);
|
2023-08-22 19:38:59 -04:00
|
|
|
|
2023-08-24 10:45:45 -04:00
|
|
|
if ($opts->curl_settings)
|
|
|
|
curl_setopt_array($this->ch, $opts->curl_settings);
|
|
|
|
|
|
|
|
if ($mh)
|
|
|
|
curl_multi_add_handle($mh, $this->ch);
|
2023-08-21 18:28:02 -04:00
|
|
|
}
|
|
|
|
|
2023-08-24 10:45:45 -04:00
|
|
|
public function get_request_url() {
|
2023-08-21 18:28:02 -04:00
|
|
|
return "";
|
|
|
|
}
|
2023-08-23 05:55:18 -04:00
|
|
|
|
2023-08-22 08:25:44 -04:00
|
|
|
public function successful() {
|
|
|
|
return curl_getinfo($this->ch)['http_code'] == '200';
|
|
|
|
}
|
|
|
|
|
2023-08-21 18:28:02 -04:00
|
|
|
abstract function get_results();
|
|
|
|
static public function print_results($results){}
|
|
|
|
}
|
|
|
|
|
2023-08-22 13:41:02 -04:00
|
|
|
function load_opts() {
|
|
|
|
$opts = require "config.php";
|
|
|
|
|
2023-08-25 12:53:59 -04:00
|
|
|
$opts->request_cooldown ??= 25;
|
|
|
|
|
2023-08-22 17:49:24 -04:00
|
|
|
$opts->query = trim($_REQUEST["q"] ?? "");
|
2023-08-22 17:48:37 -04:00
|
|
|
$opts->type = (int) ($_REQUEST["t"] ?? 0);
|
|
|
|
$opts->page = (int) ($_REQUEST["p"] ?? 0);
|
2023-08-22 13:41:02 -04:00
|
|
|
|
2023-08-22 19:51:23 -04:00
|
|
|
$opts->theme = $_REQUEST["theme"] ?? trim(htmlspecialchars($_COOKIE["theme"] ?? "dark"));
|
2023-08-22 13:41:02 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
$opts->safe_search = (int) ($_REQUEST["safe"] ?? 0) == 1 || isset($_COOKIE["safe_search"]);
|
|
|
|
|
|
|
|
$opts->disable_special = (int) ($_REQUEST["ns"] ?? 0) == 1 || isset($_COOKIE["disable_special"]);
|
|
|
|
|
|
|
|
$opts->disable_frontends = (int) ($_REQUEST["nf"] ?? 0) == 1 || isset($_COOKIE["disable_frontends"]);
|
|
|
|
|
2023-08-25 19:41:25 -04:00
|
|
|
$opts->language = $_REQUEST["lang"] ?? trim(htmlspecialchars($_COOKIE["language"] ?? $opts->language));
|
2023-08-22 18:55:11 -04:00
|
|
|
|
|
|
|
$opts->do_fallback = (int) ($_REQUEST["nfb"] ?? 0) == 0;
|
|
|
|
if (!$opts->instance_fallback) {
|
|
|
|
$opts->do_fallback = false;
|
|
|
|
}
|
|
|
|
|
2023-08-22 13:41:02 -04:00
|
|
|
$opts->number_of_results ??= trim(htmlspecialchars($_COOKIE["number_of_results"]));
|
|
|
|
|
2023-08-22 17:48:37 -04:00
|
|
|
foreach (array_keys($opts->frontends ?? array()) as $frontend) {
|
2023-08-24 10:45:45 -04:00
|
|
|
$opts->frontends[$frontend]["instance_url"] = $_COOKIE[$frontend] ?? $opts->frontends[$frontend]["instance_url"];
|
2023-08-22 17:48:37 -04:00
|
|
|
}
|
2023-08-25 11:23:53 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
return $opts;
|
|
|
|
}
|
2023-08-22 17:48:37 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
function opts_to_params($opts) {
|
|
|
|
$query = urlencode($opts->query);
|
2023-08-22 13:41:02 -04:00
|
|
|
|
2023-08-22 18:55:11 -04:00
|
|
|
$params = "";
|
|
|
|
$params .= "p=$opts->page";
|
|
|
|
$params .= "&q=$query";
|
|
|
|
$params .= "&t=$opts->type";
|
2023-08-22 19:38:59 -04:00
|
|
|
$params .= "&nfb=" . ($opts->do_fallback ? 0 : 1);
|
|
|
|
$params .= "&safe=" . ($opts->safe_search ? 1 : 0);
|
|
|
|
$params .= "&nf=" . ($opts->disable_frontends ? 1 : 0);
|
|
|
|
$params .= "&ns=" . ($opts->disable_special ? 1 : 0);
|
2023-08-22 18:55:11 -04:00
|
|
|
|
|
|
|
return $params;
|
2023-08-22 13:41:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function init_search($opts, $mh) {
|
|
|
|
switch ($opts->type)
|
2023-08-21 18:28:02 -04:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
require "engines/qwant/image.php";
|
2023-08-22 13:41:02 -04:00
|
|
|
return new QwantImageSearch($opts, $mh);
|
2023-08-21 18:28:02 -04:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
require "engines/invidious/video.php";
|
2023-08-22 13:41:02 -04:00
|
|
|
return new VideoSearch($opts, $mh);
|
2023-08-21 18:28:02 -04:00
|
|
|
|
|
|
|
case 3:
|
2023-08-22 13:41:02 -04:00
|
|
|
if ($opts->disable_bittorent_search) {
|
2023-08-21 18:28:02 -04:00
|
|
|
echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
require "engines/bittorrent/merge.php";
|
2023-08-22 13:41:02 -04:00
|
|
|
return new TorrentSearch($opts, $mh);
|
2023-08-21 18:28:02 -04:00
|
|
|
|
|
|
|
case 4:
|
2023-08-22 13:41:02 -04:00
|
|
|
if ($opts->disable_hidden_service_search) {
|
2023-08-21 18:28:02 -04:00
|
|
|
echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
require "engines/ahmia/hidden_service.php";
|
2023-08-22 13:41:02 -04:00
|
|
|
return new TorSearch($opts, $mh);
|
2023-08-21 18:28:02 -04:00
|
|
|
|
|
|
|
default:
|
2023-08-21 19:16:22 -04:00
|
|
|
require "engines/text/text.php";
|
2023-08-22 13:41:02 -04:00
|
|
|
return new TextSearch($opts, $mh);
|
2023-08-21 18:28:02 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 18:58:09 -04:00
|
|
|
|
2023-08-22 13:41:02 -04:00
|
|
|
function fetch_search_results($opts, $do_print) {
|
2023-08-25 12:39:08 -04:00
|
|
|
require "misc/cooldowns.php";
|
|
|
|
$opts->cooldowns = load_cooldowns();
|
|
|
|
|
2023-08-21 18:58:09 -04:00
|
|
|
$start_time = microtime(true);
|
|
|
|
$mh = curl_multi_init();
|
2023-08-22 13:41:02 -04:00
|
|
|
$search_category = init_search($opts, $mh);
|
2023-08-21 18:58:09 -04:00
|
|
|
|
|
|
|
$running = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
curl_multi_exec($mh, $running);
|
|
|
|
} while ($running);
|
|
|
|
|
2023-08-22 13:41:02 -04:00
|
|
|
$results = $search_category->get_results();
|
|
|
|
|
2023-08-25 06:36:19 -04:00
|
|
|
if (count($results) <= 1) {
|
2023-08-22 19:38:59 -04:00
|
|
|
require "engines/librex/fallback.php";
|
|
|
|
$results = get_librex_results($opts);
|
2023-08-22 18:55:11 -04:00
|
|
|
}
|
2023-08-21 18:58:09 -04:00
|
|
|
|
|
|
|
if (!$do_print)
|
|
|
|
return $results;
|
|
|
|
|
|
|
|
print_elapsed_time($start_time);
|
|
|
|
$search_category->print_results($results);
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
2023-08-21 18:28:02 -04:00
|
|
|
?>
|