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