2023-08-21 08:29:50 -07:00
|
|
|
<?php
|
2023-08-21 13:55:24 -07:00
|
|
|
class TextSearch extends EngineRequest {
|
2023-08-22 10:41:02 -07:00
|
|
|
public function __construct($opts, $mh) {
|
|
|
|
$this->query = $opts->query;
|
|
|
|
$this->page = $opts->page;
|
|
|
|
$this->opts = $opts;
|
|
|
|
|
2023-08-25 09:39:08 -07:00
|
|
|
$this->engine = $opts->preferred_engines["text"] ?? "google";
|
2023-08-22 10:41:02 -07:00
|
|
|
|
|
|
|
$query_parts = explode(" ", $this->query);
|
2023-08-21 13:55:24 -07:00
|
|
|
$last_word_query = end($query_parts);
|
2023-08-22 10:41:02 -07:00
|
|
|
if (substr($this->query, 0, 1) == "!" || substr($last_word_query, 0, 1) == "!")
|
2023-08-24 16:10:03 -07:00
|
|
|
check_ddg_bang($this->query, $opts);
|
2023-08-21 13:55:24 -07:00
|
|
|
|
2023-08-25 09:39:08 -07:00
|
|
|
if (has_cooldown($this->engine, $this->opts->cooldowns))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ($this->engine == "google") {
|
|
|
|
|
2023-08-21 16:16:22 -07:00
|
|
|
require "engines/text/google.php";
|
2023-08-22 10:41:02 -07:00
|
|
|
$this->engine_request = new GoogleRequest($opts, $mh);
|
2023-08-21 13:55:24 -07:00
|
|
|
}
|
|
|
|
|
2023-08-25 09:39:08 -07:00
|
|
|
if ($this->engine == "duckduckgo") {
|
2023-08-21 16:16:22 -07:00
|
|
|
require "engines/text/duckduckgo.php";
|
2023-08-22 10:41:02 -07:00
|
|
|
$this->engine_request = new DuckDuckGoRequest($opts, $mh);
|
2023-08-21 13:55:24 -07:00
|
|
|
}
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 16:16:22 -07:00
|
|
|
require "engines/special/special.php";
|
2023-08-22 10:41:02 -07:00
|
|
|
$this->special_request = get_special_search_request($opts, $mh);
|
2023-08-21 08:29:50 -07:00
|
|
|
}
|
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
public function get_results() {
|
2023-08-25 09:39:08 -07:00
|
|
|
if (!$this->engine_request)
|
|
|
|
return array();
|
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
$results = $this->engine_request->get_results();
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 16:16:22 -07:00
|
|
|
if ($this->special_request) {
|
|
|
|
$special_result = $this->special_request->get_results();
|
|
|
|
|
|
|
|
if ($special_result)
|
|
|
|
$results = array_merge(array($special_result), $results);
|
|
|
|
}
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-25 09:39:08 -07:00
|
|
|
if (count($results) <= 1)
|
|
|
|
set_cooldown($this->engine, ($opts->request_cooldown ?? "1") * 60, $this->opts->cooldowns);
|
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
return $results;
|
2023-08-21 08:29:50 -07:00
|
|
|
}
|
|
|
|
|
2023-08-21 15:28:02 -07:00
|
|
|
public static function print_results($results) {
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
if (empty($results))
|
|
|
|
return;
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
$special = $results[0];
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
if (array_key_exists("did_you_mean", $special))
|
|
|
|
{
|
|
|
|
$didyoumean = $special["did_you_mean"];
|
|
|
|
$new_url = "/search.php?q=" . urlencode($didyoumean);
|
|
|
|
echo "<p class=\"did-you-mean\">Did you mean ";
|
|
|
|
echo "<a href=\"$new_url\">$didyoumean</a>";
|
|
|
|
echo "?</p>";
|
|
|
|
}
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
if (array_key_exists("special_response", $special))
|
|
|
|
{
|
|
|
|
$response = $special["special_response"]["response"];
|
|
|
|
$source = $special["special_response"]["source"];
|
|
|
|
|
|
|
|
echo "<p class=\"special-result-container\">";
|
|
|
|
if (array_key_exists("image", $special["special_response"]))
|
|
|
|
{
|
|
|
|
$image_url = $special["special_response"]["image"];
|
|
|
|
echo "<img src=\"image_proxy.php?url=$image_url\">";
|
|
|
|
}
|
|
|
|
echo $response;
|
|
|
|
if ($source)
|
|
|
|
echo "<a href=\"$source\" target=\"_blank\">$source</a>";
|
|
|
|
echo "</p>";
|
|
|
|
}
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
echo "<div class=\"text-result-container\">";
|
2023-08-21 08:29:50 -07:00
|
|
|
|
2023-08-21 13:55:24 -07:00
|
|
|
foreach($results as $result)
|
2023-08-21 08:29:50 -07:00
|
|
|
{
|
2023-08-21 13:55:24 -07:00
|
|
|
if (!array_key_exists("title", $result))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$title = $result["title"];
|
|
|
|
$url = $result["url"];
|
|
|
|
$base_url = $result["base_url"];
|
|
|
|
$description = $result["description"];
|
|
|
|
|
|
|
|
echo "<div class=\"text-result-wrapper\">";
|
|
|
|
echo "<a href=\"$url\">";
|
|
|
|
echo "$base_url";
|
|
|
|
echo "<h2>$title</h2>";
|
|
|
|
echo "</a>";
|
|
|
|
echo "<span>$description</span>";
|
|
|
|
echo "</div>";
|
2023-08-21 08:29:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "</div>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 16:10:03 -07:00
|
|
|
function check_ddg_bang($query, $opts)
|
2023-08-21 08:29:50 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$bangs_json = file_get_contents("static/misc/ddg_bang.json");
|
|
|
|
$bangs = json_decode($bangs_json, true);
|
|
|
|
|
|
|
|
if (substr($query, 0, 1) == "!")
|
|
|
|
$search_word = substr(explode(" ", $query)[0], 1);
|
|
|
|
else
|
|
|
|
$search_word = substr(end(explode(" ", $query)), 1);
|
|
|
|
|
|
|
|
$bang_url = null;
|
|
|
|
|
|
|
|
foreach($bangs as $bang)
|
|
|
|
{
|
|
|
|
if ($bang["t"] == $search_word)
|
|
|
|
{
|
|
|
|
$bang_url = $bang["u"];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($bang_url)
|
|
|
|
{
|
|
|
|
$bang_query_array = explode("!" . $search_word, $query);
|
|
|
|
$bang_query = trim(implode("", $bang_query_array));
|
|
|
|
|
|
|
|
$request_url = str_replace("{{{s}}}", str_replace('%26quot%3B','%22', urlencode($bang_query)), $bang_url);
|
2023-08-24 16:10:03 -07:00
|
|
|
$request_url = check_for_privacy_frontend($request_url, $opts);
|
2023-08-21 08:29:50 -07:00
|
|
|
|
|
|
|
header("Location: " . $request_url);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|