tools.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. function get_base_url($url)
  3. {
  4. $split_url = explode("/", $url);
  5. $base_url = $split_url[0] . "//" . $split_url[2] . "/";
  6. return $base_url;
  7. }
  8. function get_root_domain($url)
  9. {
  10. $split_url = explode("/", $url);
  11. $base_url = $split_url[2];
  12. $base_url_main_split = explode(".", strrev($base_url));
  13. $root_domain = strrev($base_url_main_split[1]) . "." . strrev($base_url_main_split[0]);
  14. return $root_domain;
  15. }
  16. function try_replace_with_frontend($url, $frontend, $original)
  17. {
  18. global $config;
  19. $frontends = $config->frontends;
  20. if (isset($_COOKIE[$frontend]) || !empty($frontends[$frontend]["instance_url"]))
  21. {
  22. if (isset($_COOKIE[$frontend]))
  23. $frontend = $_COOKIE[$frontend];
  24. else if (!empty($frontends[$frontend]["instance_url"]))
  25. $frontend = $frontends[$frontend]["instance_url"];
  26. if (empty(trim($frontend)))
  27. return $url;
  28. if (strpos($url, "wikipedia.org") !== false)
  29. {
  30. $wiki_split = explode(".", $url);
  31. if (count($wiki_split) > 1)
  32. {
  33. $lang = explode("://", $wiki_split[0])[1];
  34. $url = $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?") . "lang=" . $lang;
  35. }
  36. }
  37. else if (strpos($url, "fandom.com") !== false)
  38. {
  39. $fandom_split = explode(".", $url);
  40. if (count($fandom_split) > 1)
  41. {
  42. $wiki_name = explode("://", $fandom_split[0])[1];
  43. $url = $frontend . "/" . $wiki_name . explode($original, $url)[1];
  44. }
  45. }
  46. else if (strpos($url, "gist.github.com") !== false)
  47. {
  48. $gist_path = explode("gist.github.com", $url)[1];
  49. $url = $frontend . "/gist" . $gist_path;
  50. }
  51. else if (strpos($url, "stackexchange.com") !== false)
  52. {
  53. $se_domain = explode(".", explode("://", $url)[1])[0];
  54. $se_path = explode("stackexchange.com", $url)[1];
  55. $url = $frontend . "/exchange" . "/" . $se_domain . $se_path;
  56. }
  57. else
  58. {
  59. $url = $frontend . explode($original, $url)[1];
  60. }
  61. return $url;
  62. }
  63. return $url;
  64. }
  65. function check_for_privacy_frontend($url)
  66. {
  67. global $config;
  68. if (isset($_COOKIE["disable_frontends"]))
  69. return $url;
  70. foreach($config->frontends as $frontend => $data)
  71. {
  72. $original = $data["original_url"];
  73. if (strpos($url, $original))
  74. {
  75. $url = try_replace_with_frontend($url, $frontend, $original);
  76. break;
  77. }
  78. else if (strpos($url, "stackexchange.com"))
  79. {
  80. $url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com");
  81. break;
  82. }
  83. }
  84. return $url;
  85. }
  86. function check_ddg_bang($query)
  87. {
  88. $bangs_json = file_get_contents("static/misc/ddg_bang.json");
  89. $bangs = json_decode($bangs_json, true);
  90. if (substr($query, 0, 1) == "!")
  91. $search_word = substr(explode(" ", $query)[0], 1);
  92. else
  93. $search_word = substr(end(explode(" ", $query)), 1);
  94. $bang_url = null;
  95. foreach($bangs as $bang)
  96. {
  97. if ($bang["t"] == $search_word)
  98. {
  99. $bang_url = $bang["u"];
  100. break;
  101. }
  102. }
  103. if ($bang_url)
  104. {
  105. $bang_query_array = explode("!" . $search_word, $query);
  106. $bang_query = trim(implode("", $bang_query_array));
  107. $request_url = str_replace("{{{s}}}", $bang_query, $bang_url);
  108. $request_url = check_for_privacy_frontend($request_url);
  109. header("Location: " . $request_url);
  110. die();
  111. }
  112. }
  113. function check_for_special_search($query)
  114. {
  115. if (isset($_COOKIE["disable_special"]))
  116. return 0;
  117. $query_lower = strtolower($query);
  118. $split_query = explode(" ", $query);
  119. if (strpos($query_lower, "to") && count($split_query) >= 4) // currency
  120. {
  121. $amount_to_convert = floatval($split_query[0]);
  122. if ($amount_to_convert != 0)
  123. return 1;
  124. }
  125. else if (strpos($query_lower, "mean") && count($split_query) >= 2) // definition
  126. {
  127. return 2;
  128. }
  129. else if (strpos($query_lower, "my") !== false)
  130. {
  131. if (strpos($query_lower, "ip"))
  132. {
  133. return 3;
  134. }
  135. else if (strpos($query_lower, "user agent") || strpos($query_lower, "ua"))
  136. {
  137. return 4;
  138. }
  139. }
  140. else if (strpos($query_lower, "weather") !== false)
  141. {
  142. return 5;
  143. }
  144. else if ($query_lower == "tor")
  145. {
  146. return 6;
  147. }
  148. else if (3 > count(explode(" ", $query))) // wikipedia
  149. {
  150. return 7;
  151. }
  152. return 0;
  153. }
  154. function get_xpath($response)
  155. {
  156. $htmlDom = new DOMDocument;
  157. @$htmlDom->loadHTML($response);
  158. $xpath = new DOMXPath($htmlDom);
  159. return $xpath;
  160. }
  161. function request($url)
  162. {
  163. global $config;
  164. $ch = curl_init($url);
  165. curl_setopt_array($ch, $config->curl_settings);
  166. $response = curl_exec($ch);
  167. return $response;
  168. }
  169. function human_filesize($bytes, $dec = 2)
  170. {
  171. $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  172. $factor = floor((strlen($bytes) - 1) / 3);
  173. return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  174. }
  175. function remove_special($string)
  176. {
  177. $string = preg_replace("/[\r\n]+/", "\n", $string);
  178. return trim(preg_replace("/\s+/", ' ', $string));
  179. }
  180. function print_elapsed_time($start_time)
  181. {
  182. $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
  183. echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
  184. }
  185. function print_next_page_button($text, $page, $query, $type)
  186. {
  187. echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
  188. echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
  189. echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
  190. echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
  191. echo "<button type=\"submit\">$text</button>";
  192. echo "</form>";
  193. }
  194. ?>