summaryrefslogtreecommitdiff
path: root/audio_sc.php
blob: 9a227e3f8d8e2e2ac99d5c79528c04ddf01392aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php

new sc_audio();

class sc_audio{
	
	public function __construct(){
		
		include "lib/curlproxy.php";
		$this->proxy = new proxy();
		
		if(isset($_GET["u"])){
			
			/*
				we're now proxying audio
			*/
			$viewkey = $_GET["u"];
			
			if(!isset($_GET["r"])){
				
				$this->do404("Ranges(r) are missing");
			}
			
			$ranges = explode(",", $_GET["r"]);
			
			// sanitize ranges
			foreach($ranges as &$range){
				
				if(!is_numeric($range)){
					
					$this->do404("Invalid range specified");
				}
				
				$range = (int)$range;
			}
			
			// sort ranges (just to make sure)
			sort($ranges);
			
			// convert ranges to pairs
			$last = -1;
			foreach($ranges as &$r){
				
				$tmp = $r;
				$r = [$last + 1, $r];
				
				$last = $tmp;
			}
			
			$browser_headers = getallheaders();
			
			// get the requested range from client
			$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]
			){
				
				// range is not satisfiable
				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];
				}
			}
			
			// proxy data!
			http_response_code(206); // partial content
			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();
		}
		
		/*
			redirect user to correct resource
			we need to scrape and store the byte positions in the result URL
		*/
		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();
	}
}