text.php 5.8 KB

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