tools.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 check_for_privacy_friendly_alternative($url)
  9. {
  10. $config = require "config.php";
  11. if ($config->replace_youtube_with_invidious != null && strpos($url, "youtube.com"))
  12. $url = $config->replace_youtube_with_invidious . explode("youtube.com", $url)[1];
  13. else if ($config->replace_instagram_with_bibliogram != null && strpos($url, "instagram.com"))
  14. {
  15. if (!strpos($url, "/p/"))
  16. $config->replace_instagram_with_bibliogram .= "/u";
  17. $url = $config->replace_instagram_with_bibliogram . explode("instagram.com", $url)[1];
  18. }
  19. else if ($config->replace_twitter_with_nitter != null && strpos($url, "twitter.com"))
  20. $url = $config->replace_twitter_with_nitter . explode("twitter.com", $url)[1];
  21. else if ($config->replace_reddit_with_libreddit != null && strpos($url, "reddit.com"))
  22. $url = $config->replace_reddit_with_libreddit . explode("reddit.com", $url)[1];
  23. return $url;
  24. }
  25. function get_xpath($response)
  26. {
  27. $htmlDom = new DOMDocument;
  28. @$htmlDom->loadHTML($response);
  29. $xpath = new DOMXPath($htmlDom);
  30. return $xpath;
  31. }
  32. function request($url)
  33. {
  34. $config = require "config.php";
  35. $ch = curl_init($url);
  36. curl_setopt_array($ch, $config->curl_settings);
  37. $response = curl_exec($ch);
  38. return $response;
  39. }
  40. function human_filesize($bytes, $dec = 2)
  41. {
  42. $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  43. $factor = floor((strlen($bytes) - 1) / 3);
  44. return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  45. }
  46. function remove_special($string)
  47. {
  48. $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
  49. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
  50. }
  51. function print_elapsed_time($start_time)
  52. {
  53. $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
  54. echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
  55. }
  56. function print_next_page_button($text, $page, $query, $type)
  57. {
  58. echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";
  59. echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
  60. echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
  61. echo "<input type=\"hidden\" name=\"type\" value=\"$type\" />";
  62. echo "<button type=\"submit\">$text</button>";
  63. echo "</form>";
  64. }
  65. ?>