link.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. function convert_bandcamp_link($link) {
  3. $base = get_base_url();
  4. $host = parse_url($link, PHP_URL_HOST);
  5. $path = ltrim(parse_url($link, PHP_URL_PATH), "/");
  6. parse_str(parse_url($link, PHP_URL_QUERY), $query);
  7. if ($host === "bandcamp.com" && $path === "search") {
  8. $file = "search";
  9. $data = [
  10. "query" => $query["q"]
  11. ];
  12. } elseif ($host === "bandcamp.com" && $path === "discover") {
  13. $file = "discover";
  14. } elseif ($host === "bandcamp.com" && str_starts_with($path, "discover/")) {
  15. $file = "discover";
  16. $data = [
  17. "tags" => explode("/", $path)[1]
  18. ];
  19. } elseif (str_ends_with($host, ".bandcamp.com") && !$path) {
  20. $file = "artist";
  21. $data = [
  22. "name" => explode(".", $host)[0]
  23. ];
  24. } elseif (str_ends_with($host, ".bandcamp.com")) {
  25. $file = "release";
  26. $data = [
  27. "artist" => explode(".", $host)[0],
  28. "type" => explode("/", $path)[0],
  29. "name" => explode("/", $path)[1]
  30. ];
  31. } elseif ($host === "f4.bcbits.com") {
  32. $file = "image";
  33. $data = [
  34. "file" => basename($link)
  35. ];
  36. } elseif ($host === "t4.bcbits.com") {
  37. $file = "audio";
  38. $data = [
  39. "directory" => explode("/", $path)[1],
  40. "format" => explode("/", $path)[2],
  41. "file" => explode("/", $path)[3],
  42. "token" => $query["token"]
  43. ];
  44. } elseif (is_bandcamp_host($host) && !$path) {
  45. $file = "artist";
  46. $data = [
  47. "name" => $host,
  48. "host" => true
  49. ];
  50. } elseif (is_bandcamp_host($host)) {
  51. $file = "release";
  52. $data = [
  53. "artist" => $host,
  54. "type" => explode("/", $path)[0],
  55. "name" => explode("/", $path)[1],
  56. "host" => true
  57. ];
  58. } elseif ($host === "bandcamp.23video.com" && str_starts_with(explode("/", $path)[3], "video_")) {
  59. $file = "video";
  60. $data = [
  61. "parent" => explode("/", $path)[0],
  62. "child" => explode("/", $path)[1],
  63. "hash" => explode("/", $path)[2],
  64. "file" => explode("/", $path)[4]
  65. ];
  66. } elseif ($host === "bandcamp.23video.com" && explode("/", $path)[4] === "thumbnail.png") {
  67. $file = "poster";
  68. $data = [
  69. "parent" => explode("/", $path)[0],
  70. "child" => explode("/", $path)[1],
  71. "hash" => explode("/", $path)[2]
  72. ];
  73. } else
  74. return htmlspecialchars($link);
  75. $link = $base . $file . ".php";
  76. if (isset($data))
  77. $link .= "?" . http_build_query($data);
  78. return $link;
  79. };
  80. function convert_tent_link($link) {
  81. $path = pathinfo(parse_url($link, PHP_URL_PATH), PATHINFO_FILENAME);
  82. parse_str(parse_url($link, PHP_URL_QUERY), $query);
  83. $link = "https://";
  84. switch ($path) {
  85. case "artist":
  86. if (!isset($query["name"]))
  87. return false;
  88. $link .= urlencode($query["name"]);
  89. if (!isset($query["host"]))
  90. $link .= ".bandcamp.com";
  91. $link .= "/music";
  92. break;
  93. case "discover":
  94. $link .= "bandcamp.com/discover";
  95. if (isset($query["tags"]))
  96. $link .= "/" . urlencode($query["tags"]);
  97. break;
  98. case "poster":
  99. if (!isset($query["parent"]) || !isset($query["child"]) || !isset($query["hash"]))
  100. return false;
  101. $link .= "bandcamp.23video.com/" . $query["parent"] . "/" . $query["child"] . "/" . $query["hash"] . "/standard/thumbnail.png";
  102. case "redirect":
  103. if (!isset($query["url"]))
  104. return false;
  105. $link = $query["url"];
  106. case "release":
  107. if (!isset($query["artist"]) || !isset($query["type"]) || !isset($query["name"]))
  108. return false;
  109. if (isset($query["host"]) && $query["host"])
  110. $link .= urlencode($query["artist"]) . "/";
  111. else
  112. $link .= urlencode($query["artist"]) . ".bandcamp.com/";
  113. $link .= urlencode($query["type"]) . "/" . urlencode($query["name"]);
  114. break;
  115. case "search":
  116. if (!isset($query["query"]))
  117. return false;
  118. $link .= "bandcamp.com/search?q=" . urlencode($query["query"]);
  119. break;
  120. case "video":
  121. if (!isset($query["parent"]) || !isset($query["child"]) || !isset($query["hash"]) || !isset($query["file"]))
  122. return false;
  123. $link .= "bandcamp.23video.com/" . $query["parent"] . "/" . $query["child"] . "/" . $query["hash"] . "/video_hd/" . $query["file"];
  124. default:
  125. return false;
  126. break;
  127. };
  128. return $link;
  129. };
  130. function get_base_url() {
  131. if (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]))
  132. $scheme = $_SERVER["HTTP_X_FORWARDED_PROTO"];
  133. elseif (isset($_SERVER["REQUEST_SCHEME"]))
  134. $scheme = $_SERVER["REQUEST_SCHEME"];
  135. elseif (isset($_SERVER["HTTPS"]))
  136. $scheme = "https";
  137. else
  138. $scheme = "http";
  139. $host = $_SERVER["HTTP_HOST"];
  140. $uri = $_SERVER["REQUEST_URI"];
  141. return $scheme . "://" . $host . preg_replace("/\/.*.php/", "/", strtok($uri, "?"));
  142. }
  143. function is_bandcamp_host($host) {
  144. $records = dns_get_record($host, DNS_ALL);
  145. if (!$records)
  146. return false;
  147. $records = array_filter($records, function($record) {
  148. $a = $record["type"] === "A" && $record["ip"] === "35.241.62.186";
  149. $cname = $record["type"] === "CNAME" && $record["target"] === "dom.bandcamp.com";
  150. return $a || $cname;
  151. });
  152. return boolval($records);
  153. };
  154. function prefix_link($link, $parameter) {
  155. if (!filter_var($link, FILTER_VALIDATE_URL)) {
  156. if (isset($_GET["host"]) && $_GET["host"])
  157. return $link = "https://" . urlencode($_GET[$parameter]) . $link;
  158. else
  159. return $link = "https://" . urlencode($_GET[$parameter]) . ".bandcamp.com" . $link;
  160. } else {
  161. return $link;
  162. };
  163. };
  164. function resize_link($link, $size) {
  165. $host = parse_url($link, PHP_URL_HOST);
  166. if ($host !== "f4.bcbits.com") return $link;
  167. $file = pathinfo($link)["filename"];
  168. $ext = pathinfo($link)["extension"];
  169. return "https://" . $host . "/img/" . explode("_", $file)[0] . "_" . $size . "." . $ext;
  170. };
  171. ?>