tools.php 6.1 KB

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