tools.php 7.0 KB

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