123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- function get_text_results($query, $page)
- {
- global $config;
- // Randomly choose a instance
- $results = array();
- $num = array_rand($config->instances);
- $instance = $config->instances[$num];
- $url = "https://$instance/api.php";
- // Create parameters in cookies
- // Long time ago parameters like google_language or alternative_frontends are in POST field
- // IMO those changes are not good for privacy, I will consider to create a fork to bring back POST field method
- // BTW domain (google) can't be changed but mostly .com is used
- $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site;
- $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results;
- $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results;
- $wikipedia_language = isset($_COOKIE["wikipedia_language"]) ? trim(htmlspecialchars($_COOKIE["wikipedia_language"])) : $config->wikipedia_language;
- $cookies = array(
- "google_language_site" => ((3 > strlen($site_language) && 0 < strlen($site_language))) ? $site_language : null,
- "google_language_results" => ((3 > strlen($results_language) && 0 < strlen($results_language))) ? $results_language : null,
- "google_number_of_results" => ((3 > strlen($number_of_results) && 0 < strlen($number_of_results))) ? $number_of_results : null,
- "wikipedia_language" => $wikipedia_language, // need some fix
- "safe_search" => isset($_COOKIE["safe_search"]) ? $_COOKIE["safe_search"] : null,
- "disable_special" => isset($_COOKIE["disable_special"]) ? $_COOKIE["disable_special"] : null
- );
- // API POST field
- // Query should be encoded in api/search.php first but dunno why original repo still encode query in this function
- $query_encoded = urlencode($query);
- $post_string = "q=$query_encoded&p=$page&t=0";
- // Alternative frontends
- // Fuck the cookie abuse in API
- if (!isset($_COOKIE["disable_frontends"]))
- {
- $frontends = array(
- "invidious"=>null,
- "rimgo"=>null,
- "scribe"=>null,
- "gothub"=>null,
- "librarian"=>null,
- "nitter"=>null,
- "libreddit"=>null,
- "proxitok"=>null,
- "wikiless"=>null,
- "quetre"=>null,
- "libremdb"=>null,
- "breezewiki"=>null,
- "anonymousoverflow"=>null,
- "suds"=>null,
- "biblioreads"=>null
- );
- foreach ($frontends as $i => $value) {
- if (isset($_COOKIE[$i]))
- $frontends[$i] = $_COOKIE[$i];
- else if (!empty($config->frontends[$i]["instance_url"]))
- $frontends[$i] = $config->frontends[$i]["instance_url"];
- }
- $cookies=array_merge($cookies, $frontends);
- }
- else
- $cookies=array_merge($cookies, array("disable_frontends"=>$_COOKIE["disable_frontends"]));
- $api_ch = curl_init($url);
- curl_setopt_array($api_ch, $config->curl_settings);
- curl_setopt($api_ch, CURLOPT_CUSTOMREQUEST, "POST");
- //curl_setopt($api_ch, CURLOPT_POST, true);
- curl_setopt($api_ch, CURLOPT_COOKIE, urldecode(http_build_query($cookies, '', ';')));
- curl_setopt($api_ch, CURLOPT_POSTFIELDS, $post_string);
- $results = json_decode(curl_exec($api_ch),true);
- // In case some instances were broken...
- if($results == null) $results = "$instance is broken";
- return $results;
- }
- function print_text_results($results)
- {
- $special = $results[0];
- 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=\"" . htmlspecialchars($source) . "\" target=\"_blank\">" . htmlspecialchars($source) . "</a>";
- echo "</p>";
- array_shift($results);
- }
- echo "<div class=\"text-result-container\">";
- foreach($results as $result)
- {
- $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>";
- }
- echo "</div>";
- }
- ?>
|