text.php 6.2 KB

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