text.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. function get_text_results($query, $page)
  3. {
  4. global $config;
  5. // Randomly choose a instance
  6. $results = array();
  7. $num = array_rand($config->instances);
  8. $instance = $config->instances[$num];
  9. $url = "https://$instance/api.php";
  10. // Create parameters in cookies
  11. // Long time ago parameters like google_language or alternative_frontends are in POST field
  12. // IMO those changes are not good for privacy, I will consider to create a fork to bring back POST field method
  13. // BTW domain (google) can't be changed but mostly .com is used
  14. $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site;
  15. $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results;
  16. $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results;
  17. $wikipedia_language = isset($_COOKIE["wikipedia_language"]) ? trim(htmlspecialchars($_COOKIE["wikipedia_language"])) : $config->wikipedia_language;
  18. $cookies = array(
  19. "google_language_site" => ((3 > strlen($site_language) && 0 < strlen($site_language))) ? $site_language : null,
  20. "google_language_results" => ((3 > strlen($results_language) && 0 < strlen($results_language))) ? $results_language : null,
  21. "google_number_of_results" => ((3 > strlen($number_of_results) && 0 < strlen($number_of_results))) ? $number_of_results : null,
  22. "wikipedia_language" => $wikipedia_language, // need some fix
  23. "safe_search" => isset($_COOKIE["safe_search"]) ? $_COOKIE["safe_search"] : null,
  24. "disable_special" => isset($_COOKIE["disable_special"]) ? $_COOKIE["disable_special"] : null
  25. );
  26. // API POST field
  27. // Query should be encoded in api/search.php first but dunno why original repo still encode query in this function
  28. $query_encoded = urlencode($query);
  29. $post_string = "q=$query_encoded&p=$page&t=0";
  30. // Alternative frontends
  31. // Fuck the cookie abuse in API
  32. if (!isset($_COOKIE["disable_frontends"]))
  33. {
  34. $frontends = array(
  35. "invidious"=>null,
  36. "rimgo"=>null,
  37. "scribe"=>null,
  38. "gothub"=>null,
  39. "librarian"=>null,
  40. "nitter"=>null,
  41. "libreddit"=>null,
  42. "proxitok"=>null,
  43. "wikiless"=>null,
  44. "quetre"=>null,
  45. "libremdb"=>null,
  46. "breezewiki"=>null,
  47. "anonymousoverflow"=>null,
  48. "suds"=>null,
  49. "biblioreads"=>null
  50. );
  51. foreach ($frontends as $i => $value) {
  52. if (isset($_COOKIE[$i]))
  53. $frontends[$i] = $_COOKIE[$i];
  54. else if (!empty($config->frontends[$i]["instance_url"]))
  55. $frontends[$i] = $config->frontends[$i]["instance_url"];
  56. }
  57. $cookies=array_merge($cookies, $frontends);
  58. }
  59. else
  60. $cookies=array_merge($cookies, array("disable_frontends"=>$_COOKIE["disable_frontends"]));
  61. $api_ch = curl_init($url);
  62. curl_setopt_array($api_ch, $config->curl_settings);
  63. curl_setopt($api_ch, CURLOPT_CUSTOMREQUEST, "POST");
  64. //curl_setopt($api_ch, CURLOPT_POST, true);
  65. curl_setopt($api_ch, CURLOPT_COOKIE, urldecode(http_build_query($cookies, '', ';')));
  66. curl_setopt($api_ch, CURLOPT_POSTFIELDS, $post_string);
  67. $results = json_decode(curl_exec($api_ch),true);
  68. // In case some instances were broken...
  69. if($results == null) $results = "$instance is broken";
  70. return $results;
  71. }
  72. function print_text_results($results)
  73. {
  74. $special = $results[0];
  75. if (array_key_exists("special_response", $special))
  76. {
  77. $response = $special["special_response"]["response"];
  78. $source = $special["special_response"]["source"];
  79. echo "<p class=\"special-result-container\">";
  80. if (array_key_exists("image", $special["special_response"]))
  81. {
  82. $image_url = $special["special_response"]["image"];
  83. echo "<img src=\"image_proxy.php?url=$image_url\">";
  84. }
  85. echo $response;
  86. if ($source)
  87. echo "<a href=\"" . htmlspecialchars($source) . "\" target=\"_blank\">" . htmlspecialchars($source) . "</a>";
  88. echo "</p>";
  89. array_shift($results);
  90. }
  91. echo "<div class=\"text-result-container\">";
  92. foreach($results as $result)
  93. {
  94. $title = $result["title"];
  95. $url = $result["url"];
  96. $base_url = $result["base_url"];
  97. $description = $result["description"];
  98. echo "<div class=\"text-result-wrapper\">";
  99. echo "<a href=\"$url\">";
  100. echo "$base_url";
  101. echo "<h2>$title</h2>";
  102. echo "</a>";
  103. echo "<span>$description</span>";
  104. echo "</div>";
  105. }
  106. echo "</div>";
  107. }
  108. ?>