query = $opts->query; $this->page = $opts->page; $this->opts = $opts; $this->engine = $opts->preferred_engines["text"] ?? "google"; $query_parts = explode(" ", $this->query); $last_word_query = end($query_parts); if (substr($this->query, 0, 1) == "!" || substr($last_word_query, 0, 1) == "!") check_ddg_bang($this->query, $opts); if ($this->engine == "google") { require "engines/text/google.php"; $this->engine_request = new GoogleRequest($opts, $mh); } if ($this->engine == "duckduckgo") { require "engines/text/duckduckgo.php"; $this->engine_request = new DuckDuckGoRequest($opts, $mh); } if (has_cooldown($this->engine, $this->opts->cooldowns) && !has_cached_results($this->engine_request->url)) { // TODO dont add it in the first place curl_multi_remove_handle($mh, $this->engine_request->ch); $this->engine_request = null; return; } require "engines/special/special.php"; $this->special_request = get_special_search_request($opts, $mh); } public function parse_results($response) { if (!isset($this->engine_request)) return array(); $results = $this->engine_request->get_results(); if (empty($results)) { set_cooldown($this->engine, ($opts->request_cooldown ?? "1") * 60, $this->opts->cooldowns); } else { if ($this->special_request) { $special_result = $this->special_request->get_results(); if ($special_result) $results = array_merge(array($special_result), $results); } } return $results; } public static function print_results($results, $opts) { if (empty($results)) { echo "

An error occured fetching results

"; return; } if (array_key_exists("error", $results)) { echo "

" . $results["error"]["message"] . "

"; return; } $special = $results[0]; if (array_key_exists("did_you_mean", $special)) { $didyoumean = $special["did_you_mean"]; $new_url = "/search.php?q=" . urlencode($didyoumean); echo "

Did you mean "; echo "$didyoumean"; echo "?

"; } if (array_key_exists("special_response", $special)) { $response = $special["special_response"]["response"]; $source = $special["special_response"]["source"]; echo "

"; if (array_key_exists("image", $special["special_response"])) { $image_url = $special["special_response"]["image"]; echo ""; } echo $response; if ($source) { $source = check_for_privacy_frontend($source, $opts); echo "$source"; } echo "

"; } echo "
"; foreach($results as $result) { if (!array_key_exists("title", $result)) continue; $title = $result["title"]; $url = $result["url"]; $url = check_for_privacy_frontend($url, $opts); $base_url = get_base_url($url); $description = $result["description"]; echo "
"; echo ""; echo "$base_url"; echo "

$title

"; echo "
"; echo "$description"; echo "
"; } echo "
"; } } function check_ddg_bang($query, $opts) { $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); header("Location: " . $request_url); die(); } } ?>