tools.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. function get_base_url($url) {
  3. $parsed = parse_url($url);
  4. if (isset($parsed["scheme"]) && isset($parsed["host"]) && !empty($parsed["scheme"]) && !empty($parsed["host"])) {
  5. return $parsed["scheme"] . "://" . $parsed["host"] . "/";
  6. } else {
  7. logStackTrace();
  8. return "";
  9. }
  10. }
  11. function get_root_domain($url) {
  12. return parse_url($url, PHP_URL_HOST);
  13. }
  14. function try_replace_with_frontend($url, $frontend, $original, $opts) {
  15. $frontends = $opts->frontends;
  16. if (array_key_exists($frontend, $opts->frontends)) {
  17. $frontend = $frontends[$frontend]["instance_url"];
  18. if (empty(trim($frontend)))
  19. return $url;
  20. if (strpos($url, "wikipedia.org") !== false) {
  21. $wiki_split = explode(".", $url);
  22. if (count($wiki_split) > 1) {
  23. $lang = explode("://", $wiki_split[0])[1];
  24. $url = $frontend . explode($original, $url)[1] . (strpos($url, "?") !== false ? "&" : "?") . "lang=" . $lang;
  25. }
  26. } else if (strpos($url, "fandom.com") !== false) {
  27. $fandom_split = explode(".", $url);
  28. if (count($fandom_split) > 1) {
  29. $wiki_name = explode("://", $fandom_split[0])[1];
  30. $url = $frontend . "/" . $wiki_name . explode($original, $url)[1];
  31. }
  32. } else if (strpos($url, "gist.github.com") !== false) {
  33. $gist_path = explode("gist.github.com", $url)[1];
  34. $url = $frontend . "/gist" . $gist_path;
  35. } else if (strpos($url, "stackexchange.com") !== false) {
  36. $se_domain = explode(".", explode("://", $url)[1])[0];
  37. $se_path = explode("stackexchange.com", $url)[1];
  38. $url = $frontend . "/exchange" . "/" . $se_domain . $se_path;
  39. } else {
  40. $url = $frontend . explode($original, $url)[1];
  41. }
  42. return $url;
  43. }
  44. return $url;
  45. }
  46. function check_for_privacy_frontend($url, $opts) {
  47. if ($opts->disable_frontends)
  48. return $url;
  49. foreach($opts->frontends as $frontend => $data) {
  50. $original = $data["original_url"];
  51. if (strpos($url, $original)) {
  52. $url = try_replace_with_frontend($url, $frontend, $original, $opts);
  53. break;
  54. } else if (strpos($url, "stackexchange.com")) {
  55. $url = try_replace_with_frontend($url, "anonymousoverflow", "stackexchange.com", $opts);
  56. break;
  57. }
  58. }
  59. return $url;
  60. }
  61. function get_xpath($response) {
  62. if (!$response)
  63. return null;
  64. $htmlDom = new DOMDocument;
  65. @$htmlDom->loadHTML($response);
  66. $xpath = new DOMXPath($htmlDom);
  67. return $xpath;
  68. }
  69. function request($url, $conf) {
  70. $ch = curl_init($url);
  71. curl_setopt_array($ch, $conf);
  72. $response = curl_exec($ch);
  73. return $response;
  74. }
  75. function human_filesize($bytes, $dec = 2) {
  76. $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  77. $factor = floor((strlen($bytes) - 1) / 3);
  78. return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  79. }
  80. function remove_special($string) {
  81. $string = preg_replace("/[\r\n]+/", "\n", $string);
  82. return trim(preg_replace("/\s+/", ' ', $string));
  83. }
  84. function print_elapsed_time($start_time, $results, $opts) {
  85. $source = "";
  86. if (array_key_exists("results_source", $results)) {
  87. $source = " from " . $results["results_source"];
  88. unset($results["results_source"]);
  89. }
  90. $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
  91. echo "<p id=\"time\">Fetched the results in $end_time seconds$source</p>";
  92. }
  93. function print_next_page_button($text, $page, $query, $type) {
  94. echo "<form class=\"page\" action=\"search.php\" target=\"_top\" method=\"get\" autocomplete=\"off\">";
  95. echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
  96. echo "<input type=\"hidden\" name=\"q\" value=\"$query\" />";
  97. echo "<input type=\"hidden\" name=\"t\" value=\"$type\" />";
  98. echo "<button type=\"submit\">$text</button>";
  99. echo "</form>";
  100. }
  101. function copy_cookies($curl) {
  102. if (array_key_exists("HTTP_COOKIE", $_SERVER))
  103. curl_setopt( $curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE'] );
  104. }
  105. function get_country_emote($code) {
  106. $emoji = [];
  107. foreach(str_split($code) as $c) {
  108. if(($o = ord($c)) > 64 && $o % 32 < 27) {
  109. $emoji[] = hex2bin("f09f87" . dechex($o % 32 + 165));
  110. continue;
  111. }
  112. $emoji[] = $c;
  113. }
  114. return join($emoji);
  115. }
  116. function logStackTrace() {
  117. // Get the stack trace
  118. $stackTrace = debug_backtrace();
  119. // Format the stack trace for logging
  120. $logMessage = "Stack Trace: ";
  121. foreach ($stackTrace as $index => $trace) {
  122. // Skip the first entry as it's the current function call
  123. if ($index === 0) {
  124. continue;
  125. }
  126. // Build the log message for each stack frame
  127. $logMessage .= "#{$index} ";
  128. if (isset($trace['file'])) {
  129. $logMessage .= "File: {$trace['file']} ";
  130. }
  131. if (isset($trace['line'])) {
  132. $logMessage .= "Line: {$trace['line']} ";
  133. }
  134. if (isset($trace['function'])) {
  135. $logMessage .= "Function: {$trace['function']} ";
  136. }
  137. $logMessage .= "\n";
  138. }
  139. // Log the stack trace to the error log
  140. error_log($logMessage);
  141. }
  142. ?>