tools.php 3.0 KB

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