put all settings in single "opts" array

This commit is contained in:
davidovski 2023-08-22 18:41:02 +01:00
parent 4941a6aa58
commit 6c253bcb47
22 changed files with 151 additions and 164 deletions

View File

@ -4,13 +4,9 @@
// e.g.: fr -> https://google.fr/
"google_domain" => "com",
// Google results will be in this language
"google_language_site" => "",
"google_language_results" => "",
"google_number_of_results" => 10,
// You can set a language for results in wikipedia
"wikipedia_language" => "en",
// Results will be in this language
"language" => "",
"number_of_results" => 10,
// You can use any Invidious instance here
"invidious_instance_for_video_results" => "https://invidious.snopyta.org",

View File

@ -1,10 +1,9 @@
<?php
require "engines/text.php";
require "engines/text/text.php";
class TorSearch extends EngineRequest {
public function get_request_url() {
$query = urlencode($this->query);
return "https://ahmia.fi/search/?q=$query";
return "https://ahmia.fi/search/?q=" . urlencode($this->query);
}
public function get_results() {

View File

@ -11,9 +11,7 @@
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tbody/tr") as $result)
{
foreach($xpath->query("//table/tbody/tr") as $result) {
$name = $xpath->evaluate(".//td[@class='coll-1 name']/a", $result)[1]->textContent;
$magnet = "./engines/bittorrent/get_magnet_1337x.php?url=https://1337x.to" . $xpath->evaluate(".//td[@class='coll-1 name']/a/@href", $result)[1]->textContent;
$size_unformatted = explode(" ", $xpath->evaluate(".//td[contains(@class, 'coll-4 size')]", $result)[0]->textContent);

View File

@ -1,8 +1,7 @@
<?php
class TorrentSearch extends EngineRequest {
public function __construct($query, $page, $mh, $config) {
$this->query = $query;
$this->page = $page;
public function __construct($opts, $mh) {
parent::__construct($opts, $mh);
require "engines/bittorrent/thepiratebay.php";
require "engines/bittorrent/rutor.php";
@ -12,25 +11,21 @@
require "engines/bittorrent/sukebei.php";
$this->requests = array(
new PirateBayRequest($query, $page, $mh, $config),
new _1337xRequest($query, $page, $mh, $config),
new NyaaRequest($query, $page, $mh, $config),
new RutorRequest($query, $page, $mh, $config),
new SukebeiRequest($query, $page, $mh, $config),
new TorrentGalaxyRequest($query, $page, $mh, $config),
new YTSRequest($query, $page, $mh, $config),
new PirateBayRequest($opts, $mh),
new _1337xRequest($opts, $mh),
new NyaaRequest($opts, $mh),
new RutorRequest($opts, $mh),
new SukebeiRequest($opts, $mh),
new TorrentGalaxyRequest($opts, $mh),
new YTSRequest($opts, $mh),
);
}
public function get_results() {
$query = urlencode($this->query);
$results = array();
foreach ($this->requests as $request) {
error_log($request->get_request_url());
error_log( curl_getinfo($request->ch)['http_code'] );
if ($request->successful()) {
if ($request->successful())
$results = array_merge($results, $request->get_results());
}
}
$seeders = array_column($results, "seeders");

View File

@ -24,7 +24,7 @@
if ($magnet_node->length > 0) {
$magnet = $magnet_node[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
$magnet = $magnet_without_tracker . $this->opts->bittorent_trackers;
} else {
$magnet = "";
}

View File

@ -15,7 +15,7 @@
$name = $xpath->evaluate(".//td/a", $result)[2]->textContent;
$magnet = $xpath->evaluate(".//td/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
$magnet = $magnet_without_tracker . $this->opts->bittorrent_trackers;
$td = $xpath->evaluate(".//td", $result);
$size = $td[count($td) == 5 ? 3 : 2]->textContent;
$seeders = $xpath->evaluate(".//span", $result)[0]->textContent;

View File

@ -22,7 +22,7 @@
$seeders = (int) $response["seeders"];
$leechers = (int) $response["leechers"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name" . $config->bittorent_trackers;
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name" . $this->opts->bittorrent_trackers;
if ($name == "No results returned")
break;

View File

@ -15,7 +15,7 @@
$name = $xpath->evaluate(".//div[contains(@class, 'clickable-row')]", $result)[0]->textContent;
$magnet = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
$magnet = $magnet_without_tracker . $this->opts->bittorrent_trackers;
$size = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span", $result)[0]->textContent;
$seeders = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[1]->textContent;
$leechers = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[2]->textContent;

View File

@ -10,24 +10,25 @@
$results = array();
$json_response = json_decode($response, true);
if ($json_response["status"] == "ok" && $json_response["data"]["movie_count"] != 0)
if ($json_response["status"] != "ok" || $json_response["data"]["movie_count"] == 0)
return $results;
foreach ($json_response["data"]["movies"] as $movie)
{
foreach ($json_response["data"]["movies"] as $movie)
{
$name = $movie["title"];
$name_encoded = urlencode($name);
$name = $movie["title"];
$name_encoded = urlencode($name);
foreach ($movie["torrents"] as $torrent)
{
foreach ($movie["torrents"] as $torrent)
{
$hash = $torrent["hash"];
$seeders = $torrent["seeds"];
$leechers = $torrent["peers"];
$size = $torrent["size"];
$hash = $torrent["hash"];
$seeders = $torrent["seeds"];
$leechers = $torrent["peers"];
$size = $torrent["size"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config->bittorent_trackers";
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$this->opts->bittorrent_trackers";
array_push($results,
array_push($results,
array (
"size" => htmlspecialchars($size),
"name" => htmlspecialchars($name),
@ -37,8 +38,7 @@
"source" => "yts.mx"
)
);
}
}
}
}
return $results;

View File

@ -1,25 +1,24 @@
<?php
class CurrencyRequest extends EngineRequest {
public function get_request_url() {
return "https://cdn.moneyconvert.net/api/latest.json";
}
public function get_results()
{
$response = curl_multi_getcontent($this->ch);
$split_query = explode(" ", $this->query);
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$amount_to_convert = floatval($split_query[0]);
class CurrencyRequest extends EngineRequest {
public function get_request_url() {
return "https://cdn.moneyconvert.net/api/latest.json";
}
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
public function get_results() {
$response = curl_multi_getcontent($this->ch);
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$split_query = explode(" ", $this->query);
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$amount_to_convert = floatval($split_query[0]);
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
if (!array_key_exists($base_currency, $rates) || !array_key_exists($currency_to_convert, $rates))
return array();
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
@ -33,6 +32,6 @@ class CurrencyRequest extends EngineRequest {
"source" => $source
)
);
}
}
}
?>

View File

@ -1,5 +1,6 @@
<?php
class DefinitionRequest extends EngineRequest {
public function get_request_url() {
$split_query = explode(" ", $this->query);
$reversed_split_q = array_reverse($split_query);

View File

@ -1,17 +1,12 @@
<?php
class IPRequest extends EngineRequest {
function __construct($query, $page, $mh, $config) {
$this->query = $query;
}
function get_results()
{
return array(
"special_response" => array(
"response" => $_SERVER["REMOTE_ADDR"],
"source" => null
)
);
function get_results() {
return array(
"special_response" => array(
"response" => $_SERVER["REMOTE_ADDR"],
"source" => null
)
);
}
}
?>

View File

@ -44,8 +44,11 @@
return 0;
}
function get_special_search_request ($query, $page, $mh, $config) {
$special_search = $page ? 0 : check_for_special_search($query);
function get_special_search_request($opts, $mh) {
if ($opts->page != 0)
return null;
$special_search = check_for_special_search($opts->query);
$special_request = null;
$url = null;
@ -55,31 +58,31 @@
switch ($special_search) {
case 1:
require "engines/special/currency.php";
$special_request = new CurrencyRequest($query, $page, $mh, $config);
$special_request = new CurrencyRequest($opts, $mh);
break;
case 2:
require "engines/special/definition.php";
$special_request = new DefinitionRequest($query, $page, $mh, $config);
$special_request = new DefinitionRequest($opts, $mh);
break;
case 3:
require "engines/special/ip.php";
$special_request = new IPRequest($query, $page, $mh, $config);
$special_request = new IPRequest($opts, $mh);
break;
case 4:
require "engines/special/user_agent.php";
$special_request = new UserAgentRequest($query, $page, $mh, $config);
$special_request = new UserAgentRequest($opts, $mh);
break;
case 5:
require "engines/special/weather.php";
$special_request = new WeatherRequest($query, $page, $mh, $config);
$special_request = new WeatherRequest($opts, $mh);
break;
case 6:
require "engines/special/tor.php";
$special_request = new TorRequest($query, $page, $mh, $config);
$special_request = new TorRequest($opts, $mh);
break;
case 7:
require "engines/special/wikipedia.php";
$special_request = new WikipediaRequest($query, $page, $mh, $config);
$special_request = new WikipediaRequest($opts, $mh);
break;
}

View File

@ -1,12 +1,6 @@
<?php
class UserAgentRequest extends EngineRequest {
function __construct($query, $page, $mh, $config) {
$this->query = $query;
$this->page = $page;
}
function get_results()
{
function get_results() {
return array(
"special_response" => array(
"response" => $_SERVER["HTTP_USER_AGENT"],

View File

@ -1,13 +1,13 @@
<?php
class WikipediaRequest extends EngineRequest {
public function get_request_url() {
$this->wikipedia_language = isset($_COOKIE["wikipedia_language"]) ? trim(htmlspecialchars($_COOKIE["wikipedia_language"])) : $this->config->wikipedia_language;
$this->wikipedia_language = $this->opts->language;
$query_encoded = urlencode($this->query);
if (in_array($this->wikipedia_language, json_decode(file_get_contents("static/misc/wikipedia_langs.json"), true)))
return "https://$this->wikipedia_language.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
if (!in_array($this->wikipedia_language, json_decode(file_get_contents("static/misc/wikipedia_langs.json"), true)))
$this->wikipedia_language = "en";
return "";
return "https://$this->wikipedia_language.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
}
public function get_results() {
@ -19,6 +19,7 @@
if (array_key_exists("missing", $first_page))
return array();
$description = substr($first_page["extract"], 0, 250) . "...";
$source = check_for_privacy_frontend("https://$this->wikipedia_language.wikipedia.org/wiki/$this->query");
@ -29,8 +30,7 @@
)
);
if (array_key_exists("thumbnail", $first_page))
{
if (array_key_exists("thumbnail", $first_page)) {
$image_url = $first_page["thumbnail"]["source"];
$response["special_response"]["image"] = $image_url;
}

View File

@ -5,15 +5,11 @@
$query_encoded = str_replace("%22", "\"", urlencode($this->query));
$results = array();
// $domain = $this->config->google_domain;
$domain = 'com';
$site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $this->config->google_language_site;
$results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $this->config->google_language_results;
$number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $this->config->google_number_of_results;
$results_language = $this->opts->language;
$number_of_results = $this->opts->number_of_results;
$url = "https://html.duckduckgo.$domain/html/?q=$query_encoded&kd=-1&s=" . 3 * $this->page;
if (3 > strlen($site_language) && 0 < strlen($site_language))
$url .= "&hl=$site_language";
if (3 > strlen($results_language) && 0 < strlen($results_language))
$url .= "&lr=lang_$results_language";

View File

@ -6,16 +6,11 @@
$query_encoded = str_replace("%22", "\"", urlencode($this->query));
$results = array();
$domain = $this->config->google_domain;
$site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $this->config->google_language_site;
$results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $this->config->google_language_results;
$number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $this->config->google_number_of_results;
$domain = $this->opts->google_domain;
$results_language = $this->opts->language;
$number_of_results = $this->opts->number_of_results;
$url = "https://www.google.$domain/search?q=$query_encoded&nfpr=1&start=$this->page";
error_log($url);
if (3 > strlen($site_language) && 0 < strlen($site_language))
$url .= "&hl=$site_language";
if (3 > strlen($results_language) && 0 < strlen($results_language))
$url .= "&lr=lang_$results_language";

View File

@ -1,29 +1,29 @@
<?php
class TextSearch extends EngineRequest {
public function __construct($query, $page, $mh, $config) {
$this->query = $query;
$this->page = $page;
public function __construct($opts, $mh) {
$this->query = $opts->query;
$this->page = $opts->page;
$this->opts = $opts;
$engine=$config->preferred_engines['text'];
if (is_null($engine))
$engine = "google";
$query_parts = explode(" ", $query);
$engine = $opts->preferred_engines->text ?? "google";
$query_parts = explode(" ", $this->query);
$last_word_query = end($query_parts);
if (substr($query, 0, 1) == "!" || substr($last_word_query, 0, 1) == "!")
check_ddg_bang($query);
if (substr($this->query, 0, 1) == "!" || substr($last_word_query, 0, 1) == "!")
check_ddg_bang($this->query);
if ($engine == "google") {
require "engines/text/google.php";
$this->engine_request = new GoogleRequest($query, $page, $mh, $config);
$this->engine_request = new GoogleRequest($opts, $mh);
}
if ($engine == "duckduckgo") {
require "engines/text/duckduckgo.php";
$this->engine_request = new DuckDuckGoRequest($query, $page, $mh, $config);
$this->engine_request = new DuckDuckGoRequest($opts, $mh);
}
require "engines/special/special.php";
$this->special_request = get_special_search_request($query, $page, $mh, $config);
$this->special_request = get_special_search_request($opts, $mh);
}
public function get_results() {

View File

@ -8,9 +8,5 @@
<link rel="stylesheet" type="text/css" href="static/css/styles.css"/>
<link title="LibreY search" type="application/opensearchdescription+xml" href="opensearch.xml?method=POST" rel="search"/>
<link rel="stylesheet" type="text/css" href="<?php
echo "static/css/";
if (isset($_COOKIE["theme"]))
echo htmlspecialchars($_COOKIE["theme"] . ".css");
else
echo "dark.css";
echo "static/css/" . htmlspecialchars($_COOKIE["theme"] ?? "dark") . ".css";
?>"/>

View File

@ -1,14 +1,14 @@
<?php
abstract class EngineRequest {
function __construct($query, $page, $mh, $config) {
$this->query = $query;
$this->page = $page;
$this->config = $config;
function __construct($opts, $mh) {
$this->query = $opts->query;
$this->page = $opts->page;
$this->opts = $opts;
$url = $this->get_request_url();
if ($url) {
$this->ch = curl_init($url);
curl_setopt_array($this->ch, $config->curl_settings);
curl_setopt_array($this->ch, $opts->curl_settings);
curl_multi_add_handle($mh, $this->ch);
}
}
@ -25,44 +25,64 @@
static public function print_results($results){}
}
function init_search($type, $query, $page, $mh, $config) {
switch ($type)
function load_opts() {
$opts = require "config.php";
$opts->query = trim($_REQUEST["q"]);
$opts->type = (int) $_REQUEST["t"] ?? 0;
$opts->page = (int) $_REQUEST["p"] ?? 0;
$opts->theme = trim(htmlspecialchars($_COOKIE["theme"] ?? "dark"));
$opts->safe_search = isset($_COOKIE["safe_search"]);
$opts->disable_special = isset($_COOKIE["disable_special"]);
$opts->disable_frontends = isset($_COOKIE["disable_frontends"]);
$opts->language ??= trim(htmlspecialchars($_COOKIE["language"]));
$opts->number_of_results ??= trim(htmlspecialchars($_COOKIE["number_of_results"]));
// TODO frontends
return $opts;
}
function init_search($opts, $mh) {
switch ($opts->type)
{
case 1:
require "engines/qwant/image.php";
return new QwantImageSearch($query, $page, $mh, $config);
return new QwantImageSearch($opts, $mh);
case 2:
require "engines/invidious/video.php";
return new VideoSearch($query, $page, $mh, $config);
return new VideoSearch($opts, $mh);
case 3:
if ($config->disable_bittorent_search) {
if ($opts->disable_bittorent_search) {
echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
break;
}
require "engines/bittorrent/merge.php";
return new TorrentSearch($query, $page, $mh, $config);
return new TorrentSearch($opts, $mh);
case 4:
if ($config->disable_hidden_service_search) {
if ($opts->disable_hidden_service_search) {
echo "<p class=\"text-result-container\">The host disabled this feature! :C</p>";
break;
}
require "engines/ahmia/hidden_service.php";
return new TorSearch($query, $page, $mh, $config);
return new TorSearch($opts, $mh);
default:
require "engines/text/text.php";
return new TextSearch($query, $page, $mh, $config);
return new TextSearch($opts, $mh);
}
}
function fetch_search_results($type, $query, $page, $config, $do_print) {
function fetch_search_results($opts, $do_print) {
$start_time = microtime(true);
$mh = curl_multi_init();
$search_category = init_search($type, $query, $page, $mh, $config);
$search_category = init_search($opts, $mh);
$running = null;
@ -70,7 +90,9 @@
curl_multi_exec($mh, $running);
} while ($running);
$results = $search_category->get_results($query, $page);
$results = $search_category->get_results();
// TODO test if no results here and fallback
if (!$do_print)
return $results;

View File

@ -1,10 +1,11 @@
<?php
<?php
require "misc/header.php";
$config = require "config.php";
require "misc/tools.php";
require "misc/search_engine.php";
$opts = load_opts();
function print_page_buttons($type, $query, $page) {
if ($type > 1)
return;
@ -23,15 +24,11 @@
echo "</div>";
}
$query = trim($_REQUEST["q"]);
$type = isset($_REQUEST["t"]) ? (int) $_REQUEST["t"] : 0;
$page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
?>
<title>
<?php
echo $query;
echo $opts->query;
?> - LibreY</title>
</head>
<body>
@ -39,18 +36,18 @@
<h1 class="logomobile"><a class="no-decoration" href="./">Libre<span class="Y">Y</span></a></h1>
<input type="text" name="q"
<?php
if (1 > strlen($query) || strlen($query) > 256)
if (1 > strlen($opts->query) || strlen($opts->query) > 256)
{
header("Location: ./");
die();
}
echo "value=\"" . htmlspecialchars($query) . "\"";
echo "value=\"" . htmlspecialchars($opts->query) . "\"";
?>
>
<br>
<?php
echo "<button class=\"hide\" name=\"t\" value=\"$type\"/></button>";
echo "<button class=\"hide\" name=\"t\" value=\"$opts->type\"/></button>";
?>
<button type="submit" class="hide"></button>
<input type="hidden" name="p" value="0">
@ -62,13 +59,13 @@
{
$category_index = array_search($category, $categories);
if (($config->disable_bittorent_search && $category_index == 3) ||
($config->disable_hidden_service_search && $category_index ==4))
if (($opts->disable_bittorent_search && $category_index == 3) ||
($opts->disable_hidden_service_search && $category_index ==4))
{
continue;
}
echo "<a " . (($category_index == $type) ? "class=\"active\" " : "") . "href=\"./search.php?q=" . urlencode($query) . "&p=0&t=" . $category_index . "\"><img src=\"static/images/" . $category . "_result.png\" alt=\"" . $category . " result\" />" . ucfirst($category) . "</a>";
echo "<a " . (($category_index == $opts->type) ? "class=\"active\" " : "") . "href=\"./search.php?q=" . urlencode($opts->query) . "&p=0&t=" . $category_index . "\"><img src=\"static/images/" . $category . "_result.png\" alt=\"" . $category . " result\" />" . ucfirst($category) . "</a>";
}
?>
</div>
@ -76,8 +73,8 @@
</form>
<?php
fetch_search_results($type, $query, $page, $config, true);
print_page_buttons($type, $query, $page);
fetch_search_results($opts, true);
print_page_buttons($opts->type, $opts->query, $opts->page);
?>
<?php require "misc/footer.php"; ?>

View File

@ -1,4 +1,5 @@
<?php
// TODO fix this page based on opts
$config = require "config.php";
// Reset all cookies when resetting, or before saving new cookies