tools.php 3.3 KB

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