text.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. function get_text_results($query, $page)
  3. {
  4. global $config;
  5. $mh = curl_multi_init();
  6. $query_encoded = urlencode($query);
  7. $results = array();
  8. $domain = $config->google_domain;
  9. $site_language = isset($_COOKIE["google_language_site"]) ? trim(htmlspecialchars($_COOKIE["google_language_site"])) : $config->google_language_site;
  10. $results_language = isset($_COOKIE["google_language_results"]) ? trim(htmlspecialchars($_COOKIE["google_language_results"])) : $config->google_language_results;
  11. $number_of_results = isset($_COOKIE["google_number_of_results"]) ? trim(htmlspecialchars($_COOKIE["google_number_of_results"])) : $config->google_number_of_results;
  12. $url = "https://www.google.$domain/search?q=$query_encoded&start=$page";
  13. if (3 > strlen($site_language) && 0 < strlen($site_language))
  14. $url .= "&hl=$site_language";
  15. if (3 > strlen($results_language) && 0 < strlen($results_language))
  16. $url .= "&lr=lang_$results_language";
  17. if (3 > strlen($number_of_results) && 0 < strlen($number_of_results))
  18. $url .= "&num=$number_of_results";
  19. if (isset($_COOKIE["safe_search"]))
  20. $url .= "&safe=medium";
  21. $google_ch = curl_init($url);
  22. curl_setopt_array($google_ch, $config->curl_settings);
  23. curl_multi_add_handle($mh, $google_ch);
  24. $special_search = $page ? 0 : check_for_special_search($query);
  25. $special_ch = null;
  26. $url = null;
  27. if ($special_search != 0)
  28. {
  29. switch ($special_search)
  30. {
  31. case 1:
  32. $url = "https://cdn.moneyconvert.net/api/latest.json";
  33. break;
  34. case 2:
  35. $split_query = explode(" ", $query);
  36. $reversed_split_q = array_reverse($split_query);
  37. $word_to_define = $reversed_split_q[1];
  38. $url = "https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define";
  39. break;
  40. case 5:
  41. $url = "https://wttr.in/@" . $_SERVER["REMOTE_ADDR"] . "?format=j1";
  42. break;
  43. case 6:
  44. $url = "https://check.torproject.org/torbulkexitlist";
  45. break;
  46. case 7:
  47. $wikipedia_language = isset($_COOKIE["wikipedia_language"]) ? trim(htmlspecialchars($_COOKIE["wikipedia_language"])) : $config->wikipedia_language;
  48. $url = "https://$wikipedia_language.wikipedia.org/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro&explaintext&redirects=1&pithumbsize=500&titles=$query_encoded";
  49. break;
  50. }
  51. if ($url != NULL)
  52. {
  53. $special_ch = curl_init($url);
  54. curl_setopt_array($special_ch, $config->curl_settings);
  55. curl_multi_add_handle($mh, $special_ch);
  56. }
  57. }
  58. $running = null;
  59. do {
  60. curl_multi_exec($mh, $running);
  61. } while ($running);
  62. if (curl_getinfo($google_ch)['http_code'] == '302') {
  63. $instances_json = json_decode(file_get_contents("instances.json"), true);
  64. $instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
  65. header("Location: " . $instances[array_rand($instances)] . "search.php?q=$query");
  66. die();
  67. }
  68. if ($special_search != 0)
  69. {
  70. $special_result = null;
  71. switch ($special_search)
  72. {
  73. case 1:
  74. require "engines/special/currency.php";
  75. $special_result = currency_results($query, curl_multi_getcontent($special_ch));
  76. break;
  77. case 2:
  78. require "engines/special/definition.php";
  79. $special_result = definition_results($query, curl_multi_getcontent($special_ch));
  80. break;
  81. case 3:
  82. require "engines/special/ip.php";
  83. $special_result = ip_result();
  84. break;
  85. case 4:
  86. require "engines/special/user_agent.php";
  87. $special_result = user_agent_result();
  88. break;
  89. case 5:
  90. require "engines/special/weather.php";
  91. $special_result = weather_results(curl_multi_getcontent($special_ch));
  92. break;
  93. case 6:
  94. require "engines/special/tor.php";
  95. $special_result = tor_result(curl_multi_getcontent($special_ch));
  96. break;
  97. case 7:
  98. require "engines/special/wikipedia.php";
  99. $special_result = wikipedia_results($query, curl_multi_getcontent($special_ch));
  100. break;
  101. }
  102. if ($special_result != null)
  103. array_push($results, $special_result);
  104. }
  105. $xpath = get_xpath(curl_multi_getcontent($google_ch));
  106. foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result)
  107. {
  108. $url = $xpath->evaluate(".//div[@class='yuRUbf']//a/@href", $result)[0];
  109. if ($url == null)
  110. continue;
  111. if (!empty($results)) // filter duplicate results, ignore special result
  112. {
  113. if (!array_key_exists("special_response", end($results)))
  114. if (end($results)["url"] == $url->textContent)
  115. continue;
  116. }
  117. $url = $url->textContent;
  118. $url = check_for_privacy_frontend($url);
  119. $title = $xpath->evaluate(".//h3", $result)[0];
  120. $description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];
  121. array_push($results,
  122. array (
  123. "title" => htmlspecialchars($title->textContent),
  124. "url" => htmlspecialchars($url),
  125. "base_url" => htmlspecialchars(get_base_url($url)),
  126. "description" => $description == null ?
  127. "No description was provided for this site." :
  128. htmlspecialchars($description->textContent)
  129. )
  130. );
  131. }
  132. return $results;
  133. }
  134. function print_text_results($results)
  135. {
  136. $special = $results[0];
  137. if (array_key_exists("special_response", $special))
  138. {
  139. $response = $special["special_response"]["response"];
  140. $source = $special["special_response"]["source"];
  141. echo "<p class=\"special-result-container\">";
  142. if (array_key_exists("image", $special["special_response"]))
  143. {
  144. $image_url = $special["special_response"]["image"];
  145. echo "<img src=\"image_proxy.php?url=$image_url\">";
  146. }
  147. echo $response;
  148. if ($source)
  149. echo "<a href=\"$source\" target=\"_blank\">$source</a>";
  150. echo "</p>";
  151. array_shift($results);
  152. }
  153. echo "<div class=\"text-result-container\">";
  154. foreach($results as $result)
  155. {
  156. $title = $result["title"];
  157. $url = $result["url"];
  158. $base_url = $result["base_url"];
  159. $description = $result["description"];
  160. echo "<div class=\"text-result-wrapper\">";
  161. echo "<a href=\"$url\">";
  162. echo "$base_url";
  163. echo "<h2>$title</h2>";
  164. echo "</a>";
  165. echo "<span>$description</span>";
  166. echo "</div>";
  167. }
  168. echo "</div>";
  169. }
  170. ?>