sc.php 3.9 KB

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