123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- new sc_audio();
- class sc_audio{
-
- public function __construct(){
-
- include "../data/config.php";
- include "../lib/curlproxy.php";
- $this->proxy = new proxy();
-
- if(isset($_GET["u"])){
-
-
- $viewkey = $_GET["u"];
-
- if(!isset($_GET["r"])){
-
- $this->do404("Ranges(r) are missing");
- }
-
- $ranges = explode(",", $_GET["r"]);
-
-
- foreach($ranges as &$range){
-
- if(!is_numeric($range)){
-
- $this->do404("Invalid range specified");
- }
-
- $range = (int)$range;
- }
-
-
- sort($ranges);
-
-
- $last = -1;
- foreach($ranges as &$r){
-
- $tmp = $r;
- $r = [$last + 1, $r];
-
- $last = $tmp;
- }
-
- $browser_headers = getallheaders();
-
-
- $client_range = 0;
- foreach($browser_headers as $key => $value){
-
- if(strtolower($key) == "range"){
-
- preg_match(
- '/bytes=([0-9]+)/',
- $value,
- $client_regex
- );
-
- if(isset($client_regex[1])){
-
- $client_range = (int)$client_regex[1];
- }else{
-
- $client_range = 0;
- }
- break;
- }
- }
-
- if(
- $client_range < 0 ||
- $client_range > $ranges[count($ranges) - 1][1]
- ){
-
-
- http_response_code(416);
- header("Content-Type: text/plain");
- die();
- }
-
- $rng = null;
- for($i=0; $i<count($ranges); $i++){
-
- if($ranges[$i][0] <= $client_range){
-
- $rng = $ranges[$i];
- }
- }
-
-
- http_response_code(206);
- header("Accept-Ranges: bytes");
- header("Content-Range: bytes {$rng[0]}-{$rng[1]}/" . ($ranges[count($ranges) - 1][1] + 1));
-
- $viewkey =
- preg_replace(
- '/\/media\/([0-9]+)\/[0-9]+\/[0-9]+/',
- '/media/$1/' . $rng[0] . '/' . $rng[1],
- $viewkey
- );
-
- try{
-
- $this->proxy->stream_linear_audio(
- $viewkey
- );
- }catch(Exception $error){
-
- $this->do404("Could not read stream");
- }
-
- die();
- }
-
-
- if(!isset($_GET["s"])){
-
- $this->do404("The URL(s) parameter is missing");
- }
-
- $viewkey = $_GET["s"];
-
- if(
- preg_match(
- '/soundcloud\.com$/',
- parse_url($viewkey, PHP_URL_HOST)
- ) === false
- ){
-
- $this->do404("This endpoint can only be used for soundcloud streams");
- }
-
- try{
-
- $json = $this->proxy->get($viewkey)["body"];
- }catch(Exception $error){
-
- $this->do404("Curl error: " . $error->getMessage());
- }
-
- $json = json_decode($json, true);
-
- if(!isset($json["url"])){
-
- $this->do404("Could not get URL from JSON");
- }
-
- $viewkey = $json["url"];
-
- $m3u8 = $this->proxy->get($viewkey)["body"];
-
- $m3u8 = explode("\n", $m3u8);
-
- $lineout = null;
- $streampos_arr = [];
- foreach($m3u8 as $line){
-
- $line = trim($line);
- if($line[0] == "#"){
-
- continue;
- }
-
- if($lineout === null){
- $lineout = $line;
- }
-
- preg_match(
- '/\/media\/[0-9]+\/([0-9]+)\/([0-9]+)/',
- $line,
- $matches
- );
-
- if(isset($matches[0])){
-
- $streampos_arr[] = [
- (int)$matches[1],
- (int)$matches[2]
- ];
- }
- }
-
- if($lineout === null){
-
- $this->do404("Could not get stream URL");
- }
-
- $lineout =
- preg_replace(
- '/\/media\/([0-9]+)\/[0-9]+\/[0-9]+/',
- '/media/$1/0/0',
- $lineout
- );
-
- $streampos = [];
-
- foreach($streampos_arr as $pos){
-
- $streampos[] = $pos[1];
- }
-
- $streampos = implode(",", $streampos);
-
- header("Location: /audio/sc?u=" . urlencode($lineout) . "&r=$streampos");
- header("Accept-Ranges: bytes");
- }
-
- private function do404($error){
-
- http_response_code(404);
- header("Content-Type: text/plain");
- header("X-Error: $error");
- die();
- }
- }
|