link.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. function convert_link($link) {
  3. if (! function_exists('str_ends_with')) {
  4. function str_ends_with(string $haystack, string $needle): bool
  5. {
  6. $needle_len = strlen($needle);
  7. return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
  8. }
  9. }
  10. $host = parse_url($link, PHP_URL_HOST);
  11. $path = ltrim(parse_url($link, PHP_URL_PATH), "/");
  12. parse_str(parse_url($link, PHP_URL_QUERY), $query);
  13. if ($host === "bandcamp.com" && $path === "search") {
  14. $file = "search";
  15. $data = [
  16. "query" => $query["q"]
  17. ];
  18. } elseif (str_ends_with($host, ".bandcamp.com") && !$path) {
  19. $file = "artist";
  20. $data = [
  21. "name" => explode(".", $host)[0]
  22. ];
  23. } elseif (str_ends_with($host, ".bandcamp.com")) {
  24. $file = "release";
  25. $data = [
  26. "artist" => explode(".", $host)[0],
  27. "type" => explode("/", $path)[0],
  28. "name" => explode("/", $path)[1]
  29. ];
  30. } elseif ($host === "f4.bcbits.com") {
  31. $file = "image";
  32. $data = [
  33. "file" => basename($link)
  34. ];
  35. } elseif ($host === "t4.bcbits.com") {
  36. $file = "audio";
  37. $data = [
  38. "directory" => explode("/", $path)[1],
  39. "format" => explode("/", $path)[2],
  40. "file" => explode("/", $path)[3],
  41. "token" => $query["token"]
  42. ];
  43. } else
  44. return $link;
  45. return $file . ".php?" . http_build_query($data);
  46. };
  47. function prefix_link($link, $parameter) {
  48. if (!filter_var($link, FILTER_VALIDATE_URL))
  49. return $link = "https://" . urlencode($_GET[$parameter]) . ".bandcamp.com" . $link;
  50. else
  51. return $link;
  52. };
  53. function resize_link($link, $size) {
  54. $host = parse_url($link, PHP_URL_HOST);
  55. if ($host !== "f4.bcbits.com") return $link;
  56. $file = pathinfo($link)["filename"];
  57. $ext = pathinfo($link)["extension"];
  58. return "https://" . $host . "/img/" . explode("_", $file)[0] . "_" . $size . "." . $ext;
  59. };
  60. ?>