sc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. new sc_audio();
  3. class sc_audio{
  4. public function __construct(){
  5. include "../lib/curlproxy.php";
  6. $this->proxy = new proxy();
  7. if(isset($_GET["u"])){
  8. /*
  9. we're now proxying audio
  10. */
  11. $viewkey = $_GET["u"];
  12. if(!isset($_GET["r"])){
  13. $this->do404("Ranges(r) are missing");
  14. }
  15. $ranges = explode(",", $_GET["r"]);
  16. // sanitize ranges
  17. foreach($ranges as &$range){
  18. if(!is_numeric($range)){
  19. $this->do404("Invalid range specified");
  20. }
  21. $range = (int)$range;
  22. }
  23. // sort ranges (just to make sure)
  24. sort($ranges);
  25. // convert ranges to pairs
  26. $last = -1;
  27. foreach($ranges as &$r){
  28. $tmp = $r;
  29. $r = [$last + 1, $r];
  30. $last = $tmp;
  31. }
  32. $browser_headers = getallheaders();
  33. // get the requested range from client
  34. $client_range = 0;
  35. foreach($browser_headers as $key => $value){
  36. if(strtolower($key) == "range"){
  37. preg_match(
  38. '/bytes=([0-9]+)/',
  39. $value,
  40. $client_regex
  41. );
  42. if(isset($client_regex[1])){
  43. $client_range = (int)$client_regex[1];
  44. }else{
  45. $client_range = 0;
  46. }
  47. break;
  48. }
  49. }
  50. if(
  51. $client_range < 0 ||
  52. $client_range > $ranges[count($ranges) - 1][1]
  53. ){
  54. // range is not satisfiable
  55. http_response_code(416);
  56. header("Content-Type: text/plain");
  57. die();
  58. }
  59. $rng = null;
  60. for($i=0; $i<count($ranges); $i++){
  61. if($ranges[$i][0] <= $client_range){
  62. $rng = $ranges[$i];
  63. }
  64. }
  65. // proxy data!
  66. http_response_code(206); // partial content
  67. header("Accept-Ranges: bytes");
  68. header("Content-Range: bytes {$rng[0]}-{$rng[1]}/" . ($ranges[count($ranges) - 1][1] + 1));
  69. $viewkey =
  70. preg_replace(
  71. '/\/media\/([0-9]+)\/[0-9]+\/[0-9]+/',
  72. '/media/$1/' . $rng[0] . '/' . $rng[1],
  73. $viewkey
  74. );
  75. try{
  76. $this->proxy->stream_linear_audio(
  77. $viewkey
  78. );
  79. }catch(Exception $error){
  80. $this->do404("Could not read stream");
  81. }
  82. die();
  83. }
  84. /*
  85. redirect user to correct resource
  86. we need to scrape and store the byte positions in the result URL
  87. */
  88. if(!isset($_GET["s"])){
  89. $this->do404("The URL(s) parameter is missing");
  90. }
  91. $viewkey = $_GET["s"];
  92. if(
  93. preg_match(
  94. '/soundcloud\.com$/',
  95. parse_url($viewkey, PHP_URL_HOST)
  96. ) === false
  97. ){
  98. $this->do404("This endpoint can only be used for soundcloud streams");
  99. }
  100. try{
  101. $json = $this->proxy->get($viewkey)["body"];
  102. }catch(Exception $error){
  103. $this->do404("Curl error: " . $error->getMessage());
  104. }
  105. $json = json_decode($json, true);
  106. if(!isset($json["url"])){
  107. $this->do404("Could not get URL from JSON");
  108. }
  109. $viewkey = $json["url"];
  110. $m3u8 = $this->proxy->get($viewkey)["body"];
  111. $m3u8 = explode("\n", $m3u8);
  112. $lineout = null;
  113. $streampos_arr = [];
  114. foreach($m3u8 as $line){
  115. $line = trim($line);
  116. if($line[0] == "#"){
  117. continue;
  118. }
  119. if($lineout === null){
  120. $lineout = $line;
  121. }
  122. preg_match(
  123. '/\/media\/[0-9]+\/([0-9]+)\/([0-9]+)/',
  124. $line,
  125. $matches
  126. );
  127. if(isset($matches[0])){
  128. $streampos_arr[] = [
  129. (int)$matches[1],
  130. (int)$matches[2]
  131. ];
  132. }
  133. }
  134. if($lineout === null){
  135. $this->do404("Could not get stream URL");
  136. }
  137. $lineout =
  138. preg_replace(
  139. '/\/media\/([0-9]+)\/[0-9]+\/[0-9]+/',
  140. '/media/$1/0/0',
  141. $lineout
  142. );
  143. $streampos = [];
  144. foreach($streampos_arr as $pos){
  145. $streampos[] = $pos[1];
  146. }
  147. $streampos = implode(",", $streampos);
  148. header("Location: /audio/sc?u=" . urlencode($lineout) . "&r=$streampos");
  149. header("Accept-Ranges: bytes");
  150. }
  151. private function do404($error){
  152. http_response_code(404);
  153. header("Content-Type: text/plain");
  154. header("X-Error: $error");
  155. die();
  156. }
  157. }