tools.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $url = $frontend . explode($original, $url)[1];
  25. return $url;
  26. }
  27. return $url;
  28. }
  29. function check_for_privacy_frontend($url)
  30. {
  31. $frontends = array(
  32. "youtube.com" => "invidious",
  33. "instagram.com" => "bibliogram",
  34. "twitter.com" => "nitter",
  35. "reddit.com" => "libreddit",
  36. "tiktok.com" => "proxitok",
  37. "wikipedia.org" => "wikiless"
  38. );
  39. foreach($frontends as $original => $frontend)
  40. {
  41. if (strpos($url, $original))
  42. {
  43. $url = try_replace_with_frontend($url, $frontend, $original);
  44. break;
  45. }
  46. }
  47. return $url;
  48. }
  49. function check_ddg_bang($query)
  50. {
  51. $bangs_json = file_get_contents("static/misc/ddg_bang.json");
  52. $bangs = json_decode($bangs_json, true);
  53. $search_word = substr(explode(" ", $query)[0], 1);
  54. $bang_url = null;
  55. foreach($bangs as $bang)
  56. {
  57. if ($bang["t"] == $search_word)
  58. {
  59. $bang_url = $bang["u"];
  60. break;
  61. }
  62. }
  63. if ($bang_url)
  64. {
  65. $bang_query_array = explode("!" . $search_word, $query);
  66. $bang_query = trim(implode("", $bang_query_array));
  67. $request_url = str_replace("{{{s}}}", $bang_query, $bang_url);
  68. $request_url = check_for_privacy_frontend($request_url);
  69. header("Location: " . $request_url);
  70. die();
  71. }
  72. }
  73. function get_xpath($response)
  74. {
  75. $htmlDom = new DOMDocument;
  76. @$htmlDom->loadHTML($response);
  77. $xpath = new DOMXPath($htmlDom);
  78. return $xpath;
  79. }
  80. function request($url)
  81. {
  82. global $config;
  83. $ch = curl_init($url);
  84. curl_setopt_array($ch, $config->curl_settings);
  85. $response = curl_exec($ch);
  86. return $response;
  87. }
  88. function human_filesize($bytes, $dec = 2)
  89. {
  90. $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  91. $factor = floor((strlen($bytes) - 1) / 3);
  92. return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  93. }
  94. function remove_special($string)
  95. {
  96. $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
  97. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
  98. }
  99. function print_elapsed_time($start_time)
  100. {
  101. $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
  102. echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
  103. }
  104. function print_next_page_button($text, $page, $query, $type)
  105. {
  106. echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
  107. echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
  108. echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
  109. echo "<input type=\"hidden\" name=\"type\" value=\"$type\" />";
  110. echo "<button type=\"submit\">$text</button>";
  111. echo "</form>";
  112. }
  113. ?>