mirror of
https://git.bakhai.co.in/FbIN/LibreY.git
synced 2025-11-06 12:31:32 +05:30
commit
c9898cf261
95 changed files with 5217 additions and 0 deletions
45
misc/cooldowns.php
Normal file
45
misc/cooldowns.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
if (!function_exists("apcu_fetch"))
|
||||
error_log("apcu is not installed! Please consider installing php-pecl-apcu for significant performance improvements");
|
||||
|
||||
|
||||
function load_cooldowns() {
|
||||
if (function_exists("apcu_fetch"))
|
||||
return apcu_exists("cooldowns") ? apcu_fetch("cooldowns") : array();
|
||||
return array();
|
||||
}
|
||||
|
||||
function save_cooldowns($cooldowns) {
|
||||
if (function_exists("apcu_store"))
|
||||
apcu_store("cooldowns", $cooldowns);
|
||||
}
|
||||
|
||||
function set_cooldown($instance, $timeout, $cooldowns) {
|
||||
$cooldowns[$instance] = time() + $timeout;
|
||||
save_cooldowns($cooldowns);
|
||||
return $cooldowns;
|
||||
}
|
||||
|
||||
function has_cooldown($instance, $cooldowns) {
|
||||
return ($cooldowns[$instance] ?? 0) > time();
|
||||
}
|
||||
|
||||
function has_cached_results($url) {
|
||||
if (function_exists("apcu_exists"))
|
||||
return apcu_exists("cached:$url");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function store_cached_results($url, $results, $ttl = 0) {
|
||||
if (function_exists("apcu_store") && !empty($results) && $ttl >= 0)
|
||||
return apcu_store("cached:$url", $results, $ttl);
|
||||
}
|
||||
|
||||
function fetch_cached_results($url) {
|
||||
if (function_exists("apcu_fetch"))
|
||||
return apcu_fetch("cached:$url");
|
||||
|
||||
return array();
|
||||
}
|
||||
?>
|
||||
19
misc/footer.php
Normal file
19
misc/footer.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<div class="footer-container">
|
||||
<a href="./">FbIN</a>
|
||||
<a href="https://git.flossboxin.org.in/FbIN/LibreY/" target="_blank"><?php printtext("source_code_link");?></a>
|
||||
<a href="./settings.php"><?php printtext("settings_link");?></a>
|
||||
<?php if(!$opts->disable_api) {
|
||||
echo '<a href="./api.php" target="_blank">', printtext("api_link"), '</a>';
|
||||
} ?>
|
||||
</div>
|
||||
<div class="git-container">
|
||||
<?php
|
||||
if (file_exists(".git/refs/heads/main")) {
|
||||
$hash = file_get_contents(".git/refs/heads/main");
|
||||
}
|
||||
|
||||
echo "<a href='https://git.flossboxin.org.in/FbIN/LibreY/commit/$hash' target='_blank'>" . printftext("latest_commit", $hash) . "</a>";
|
||||
?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
16
misc/header.php
Normal file
16
misc/header.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php require_once "locale/localization.php";
|
||||
$GLOBALS["opts"] = require_once "config.php";
|
||||
?>
|
||||
<!DOCTYPE html >
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="description" content="A privacy respecting meta search engine."/>
|
||||
<meta name="referrer" content="no-referrer"/>
|
||||
<link rel="stylesheet" type="text/css" href="static/css/styles.css"/>
|
||||
<link title="<?php printtext("page_title"); ?>" type="application/opensearchdescription+xml" href="opensearch.xml?method=POST" rel="search"/>
|
||||
<link rel="stylesheet" type="text/css" href="<?php
|
||||
$theme = $_REQUEST["theme"] ?? trim(htmlspecialchars($_COOKIE["theme"] ?? $GLOBALS["opts"]->default_theme ?? "catppuccin_macchiato"));
|
||||
echo "static/css/" . $theme . ".css";
|
||||
?>"/>
|
||||
191
misc/search_engine.php
Normal file
191
misc/search_engine.php
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
require_once "misc/cooldowns.php";
|
||||
abstract class EngineRequest {
|
||||
protected $url, $query, $page, $opts, $mh, $ch;
|
||||
|
||||
protected $DO_CACHING = true;
|
||||
public function __construct($opts, $mh) {
|
||||
$this->query = $opts->query;
|
||||
$this->page = $opts->page;
|
||||
$this->mh = $mh;
|
||||
$this->opts = $opts;
|
||||
|
||||
$this->url = $this->get_request_url();
|
||||
if (!$this->url)
|
||||
return;
|
||||
|
||||
if (has_cached_results($this->url))
|
||||
return;
|
||||
|
||||
$this->ch = curl_init($this->url);
|
||||
|
||||
if ($opts->curl_settings)
|
||||
curl_setopt_array($this->ch, $opts->curl_settings);
|
||||
|
||||
if ($mh)
|
||||
curl_multi_add_handle($mh, $this->ch);
|
||||
}
|
||||
|
||||
public function get_request_url() {
|
||||
return "";
|
||||
}
|
||||
|
||||
public function successful() {
|
||||
return (isset($this->ch) && curl_getinfo($this->ch)['http_code'] == '200')
|
||||
|| has_cached_results($this->url);
|
||||
}
|
||||
|
||||
abstract function parse_results($response);
|
||||
|
||||
public function get_results() {
|
||||
if (!isset($this->url))
|
||||
return $this->parse_results(null);
|
||||
|
||||
if ($this->DO_CACHING && has_cached_results($this->url))
|
||||
return fetch_cached_results($this->url);
|
||||
|
||||
if (!isset($this->ch))
|
||||
return $this->parse_results(null);
|
||||
|
||||
$response = $this->mh ? curl_multi_getcontent($this->ch) : curl_exec($this->ch);
|
||||
$results = $this->parse_results($response) ?? array();
|
||||
|
||||
if ($this->DO_CACHING && !empty($results))
|
||||
store_cached_results($this->url, $results, $this->opts->cache_time * 60);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function print_results($results, $opts) {}
|
||||
}
|
||||
|
||||
function load_opts() {
|
||||
if (isset($GLOBALS["opts"]))
|
||||
$opts = $GLOBALS["opts"];
|
||||
else
|
||||
$opts = require_once "config.php";
|
||||
|
||||
# account for the old, misspelled options
|
||||
if (isset($opts->disable_bittorent_search))
|
||||
$opts->disable_bittorrent_search = $opts->disable_bittorent_search;
|
||||
|
||||
if (isset($opts->bittorent_trackers))
|
||||
$opts->bittorrent_trackers = $opts->bittorent_trackers;
|
||||
|
||||
$opts->request_cooldown ??= 25;
|
||||
$opts->cache_time ??= 25;
|
||||
|
||||
$opts->query = trim($_REQUEST["q"] ?? "");
|
||||
$opts->type = (int) ($_REQUEST["t"] ?? 0);
|
||||
$opts->page = (int) ($_REQUEST["p"] ?? 0);
|
||||
|
||||
$opts->theme = $_REQUEST["theme"] ?? trim(htmlspecialchars($_COOKIE["theme"] ?? $opts->default_theme ?? "dark"));
|
||||
|
||||
$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"]);
|
||||
|
||||
$opts->language = $_REQUEST["lang"] ?? trim(htmlspecialchars($_COOKIE["language"] ?? $opts->language ?? "en"));
|
||||
|
||||
$opts->do_fallback = (int) ($_REQUEST["nfb"] ?? 0) == 0;
|
||||
if (!$opts->instance_fallback) {
|
||||
$opts->do_fallback = false;
|
||||
}
|
||||
|
||||
$opts->number_of_results ??= trim(htmlspecialchars($_COOKIE["number_of_results"]));
|
||||
|
||||
foreach (array_keys($opts->frontends ?? array()) as $frontend) {
|
||||
$opts->frontends[$frontend]["instance_url"] = $_COOKIE[$frontend] ?? $opts->frontends[$frontend]["instance_url"];
|
||||
}
|
||||
|
||||
$opts->curl_settings[CURLOPT_FOLLOWLOCATION] ??= true;
|
||||
|
||||
$opts->engine = $_REQUEST["engine"] ?? $_COOKIE["engine"] ?? $opts->preferred_engines["text"] ?? "auto";
|
||||
|
||||
return $opts;
|
||||
}
|
||||
|
||||
function opts_to_params($opts) {
|
||||
$query = urlencode($opts->query);
|
||||
|
||||
$params = "";
|
||||
$params .= "p=$opts->page";
|
||||
$params .= "&q=$query";
|
||||
$params .= "&t=$opts->type";
|
||||
$params .= "&safe=" . ($opts->safe_search ? 1 : 0);
|
||||
$params .= "&nf=" . ($opts->disable_frontends ? 1 : 0);
|
||||
$params .= "&ns=" . ($opts->disable_special ? 1 : 0);
|
||||
$params .= "&engine=" . ($opts->engine ?? "auto");
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
function init_search($opts, $mh) {
|
||||
switch ($opts->type)
|
||||
{
|
||||
case 1:
|
||||
require_once "engines/qwant/image.php";
|
||||
return new QwantImageSearch($opts, $mh);
|
||||
|
||||
case 2:
|
||||
require_once "engines/invidious/video.php";
|
||||
return new VideoSearch($opts, $mh);
|
||||
|
||||
case 3:
|
||||
if ($opts->disable_bittorrent_search) {
|
||||
echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>";
|
||||
break;
|
||||
}
|
||||
|
||||
require_once "engines/bittorrent/merge.php";
|
||||
return new TorrentSearch($opts, $mh);
|
||||
|
||||
case 4:
|
||||
if ($opts->disable_hidden_service_search) {
|
||||
echo "<p class=\"text-result-container\">" . TEXTS["feature_disabled"] . "</p>";
|
||||
break;
|
||||
}
|
||||
require_once "engines/ahmia/hidden_service.php";
|
||||
return new TorSearch($opts, $mh);
|
||||
|
||||
case 5:
|
||||
require_once "engines/maps/openstreetmap.php";
|
||||
return new OSMRequest($opts, $mh);
|
||||
|
||||
default:
|
||||
require_once "engines/text/text.php";
|
||||
return new TextSearch($opts, $mh);
|
||||
}
|
||||
}
|
||||
|
||||
function fetch_search_results($opts, $do_print) {
|
||||
$opts->cooldowns = load_cooldowns();
|
||||
|
||||
$start_time = microtime(true);
|
||||
$mh = curl_multi_init();
|
||||
$search_category = init_search($opts, $mh);
|
||||
|
||||
$running = null;
|
||||
|
||||
do {
|
||||
curl_multi_exec($mh, $running);
|
||||
} while ($running);
|
||||
|
||||
$results = $search_category->get_results();
|
||||
|
||||
if (empty($results)) {
|
||||
require_once "engines/librex/fallback.php";
|
||||
$results = get_librex_results($opts);
|
||||
}
|
||||
|
||||
if (!$do_print || empty($results))
|
||||
return $results;
|
||||
|
||||
print_elapsed_time($start_time, $results, $opts);
|
||||
$search_category->print_results($results, $opts);
|
||||
|
||||
return $results;
|
||||
}
|
||||
?>
|
||||
205
misc/tools.php
Normal file
205
misc/tools.php
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
<?php
|
||||
function get_base_url($url) {
|
||||
$parsed = parse_url($url);
|
||||
|
||||
if (isset($parsed["scheme"]) && isset($parsed["host"]) && !empty($parsed["scheme"]) && !empty($parsed["host"])) {
|
||||
return $parsed["scheme"] . "://" . $parsed["host"] . "/";
|
||||
} else {
|
||||
logStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function seconds_to_human_readable($inputSeconds) {
|
||||
$secondsInAMinute = 60;
|
||||
$secondsInAnHour = 60 * $secondsInAMinute;
|
||||
$secondsInADay = 24 * $secondsInAnHour;
|
||||
|
||||
$days = floor($inputSeconds / $secondsInADay);
|
||||
$hourSeconds = $inputSeconds % $secondsInADay;
|
||||
$hours = floor($hourSeconds / $secondsInAnHour);
|
||||
$minuteSeconds = $hourSeconds % $secondsInAnHour;
|
||||
$minutes = floor($minuteSeconds / $secondsInAMinute);
|
||||
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
|
||||
$seconds = ceil($remainingSeconds);
|
||||
$timeParts = [];
|
||||
$sections = [
|
||||
'day' => (int)$days,
|
||||
'hour' => (int)$hours,
|
||||
'minute' => (int)$minutes,
|
||||
'second' => (int)$seconds,
|
||||
];
|
||||
|
||||
foreach ($sections as $name => $value){
|
||||
if ($value > 0){
|
||||
$timeParts[] = $value. ' '.$name.($value == 1 ? '' : 's');
|
||||
}
|
||||
}
|
||||
|
||||
return implode(', ', $timeParts);
|
||||
}
|
||||
|
||||
function get_root_domain($url) {
|
||||
return parse_url($url, PHP_URL_HOST);
|
||||
}
|
||||
|
||||
function try_replace_with_frontend($url, $frontend, $original, $opts) {
|
||||
$frontends = $opts->frontends;
|
||||
|
||||
if (array_key_exists($frontend, $opts->frontends)) {
|
||||
$frontend = $frontends[$frontend]["instance_url"];
|
||||
|
||||
if (empty(trim($frontend)))
|
||||
return $url;
|
||||
|
||||
if (strpos($url, "wikipedia.org") !== false) {
|
||||
$wiki_split = explode(".", $url);
|
||||
if (count($wiki_split) > 1) {
|
||||
$lang = explode("://", $wiki_split[0])[1];
|
||||
$url = $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?") . "lang=" . $lang;
|
||||
}
|
||||
} else if (strpos($url, "fandom.com") !== false) {
|
||||
$fandom_split = explode(".", $url);
|
||||
if (count($fandom_split) > 1) {
|
||||
$wiki_name = explode("://", $fandom_split[0])[1];
|
||||
$url = $frontend . "/" . $wiki_name . explode($original, $url)[1];
|
||||
}
|
||||
} else if (strpos($url, "gist.github.com") !== false) {
|
||||
$gist_path = explode("gist.github.com", $url)[1];
|
||||
$url = $frontend . "/gist" . $gist_path;
|
||||
} else if (strpos($url, "stackexchange.com") !== false) {
|
||||
$se_domain = explode(".", explode("://", $url)[1])[0];
|
||||
$se_path = explode("stackexchange.com", $url)[1];
|
||||
$url = $frontend . "/exchange" . "/" . $se_domain . $se_path;
|
||||
} else {
|
||||
$url = $frontend . explode($original, $url)[1];
|
||||
}
|
||||
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function check_for_privacy_frontend($url, $opts) {
|
||||
if ($opts->disable_frontends)
|
||||
return $url;
|
||||
|
||||
foreach($opts->frontends as $frontend => $data) {
|
||||
$original = $data["original_url"];
|
||||
|
||||
if (strpos($url, $original)) {
|
||||
$url = try_replace_with_frontend($url, $frontend, $original, $opts);
|
||||
break;
|
||||
} else if (strpos($url, "stackexchange.com")) {
|
||||
$url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com", $opts);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function get_xpath($response) {
|
||||
if (!$response)
|
||||
return null;
|
||||
|
||||
$htmlDom = new DOMDocument;
|
||||
@$htmlDom->loadHTML($response);
|
||||
$xpath = new DOMXPath($htmlDom);
|
||||
|
||||
return $xpath;
|
||||
}
|
||||
|
||||
function request($url, $conf) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, $conf);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
function human_filesize($bytes, $dec = 2) {
|
||||
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
|
||||
return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
|
||||
}
|
||||
|
||||
function remove_special($string) {
|
||||
$string = preg_replace("/[\r\n]+/", "\n", $string);
|
||||
return trim(preg_replace("/\s+/", ' ', $string));
|
||||
}
|
||||
|
||||
function print_elapsed_time($start_time, $results, $opts) {
|
||||
$source = "";
|
||||
if (array_key_exists("results_source", $results)) {
|
||||
$source = " from " . $results["results_source"];
|
||||
unset($results["results_source"]);
|
||||
}
|
||||
|
||||
$end_time = number_format(microtime(true) - $start_time, 2, '.', '');
|
||||
echo "<p id=\"time\">Fetched the results in $end_time seconds$source</p>";
|
||||
}
|
||||
|
||||
function print_next_page_button($text, $page, $query, $type) {
|
||||
echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
|
||||
echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
|
||||
echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
|
||||
echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
|
||||
echo "<button type=\"submit\">$text</button>";
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
function copy_cookies($curl) {
|
||||
if (array_key_exists("HTTP_COOKIE", $_SERVER))
|
||||
curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
|
||||
}
|
||||
|
||||
|
||||
function get_country_emote($code) {
|
||||
$emoji = [];
|
||||
foreach(str_split($code) as $c) {
|
||||
if(($o = ord($c)) > 64 && $o % 32 < 27) {
|
||||
$emoji[] = hex2bin("f09f87" . dechex($o % 32 + 165));
|
||||
continue;
|
||||
}
|
||||
|
||||
$emoji[] = $c;
|
||||
}
|
||||
|
||||
return join($emoji);
|
||||
}
|
||||
|
||||
function logStackTrace() {
|
||||
// Get the stack trace
|
||||
$stackTrace = debug_backtrace();
|
||||
|
||||
// Format the stack trace for logging
|
||||
$logMessage = "Stack Trace: ";
|
||||
foreach ($stackTrace as $index => $trace) {
|
||||
// Skip the first entry as it's the current function call
|
||||
if ($index === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build the log message for each stack frame
|
||||
$logMessage .= "#{$index} ";
|
||||
if (isset($trace['file'])) {
|
||||
$logMessage .= "File: {$trace['file']} ";
|
||||
}
|
||||
if (isset($trace['line'])) {
|
||||
$logMessage .= "Line: {$trace['line']} ";
|
||||
}
|
||||
if (isset($trace['function'])) {
|
||||
$logMessage .= "Function: {$trace['function']} ";
|
||||
}
|
||||
$logMessage .= "\n";
|
||||
}
|
||||
|
||||
// Log the stack trace to the error log
|
||||
error_log($logMessage);
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue