-
Notifications
You must be signed in to change notification settings - Fork 1
/
music.php
192 lines (178 loc) · 5.84 KB
/
music.php
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
<?php
require_once('global.php');
set_time_limit ( 10 );
/* Source https://gist.github.com/kosinix/4cf0d432638817888149 */
class ResumeDownload {
private $file;
private $name;
private $boundary;
private $content_type;
private $delay = 0;
private $size = 0;
function __construct($file, $content_type = "application/octet-stream", $delay = 0) {
if (! is_file($file)) {
header("HTTP/1.1 400 Invalid Request");
die("<h3>File Not Found</h3>");
}
$this->size = filesize($file);
$this->file = fopen($file, "r");
$this->boundary = md5($file);
$this->delay = $delay;
$this->name = basename($file);
$this->content_type = $content_type;
}
public function process() {
$ranges = NULL;
$t = 0;
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SERVER['HTTP_RANGE']) && $range = stristr(trim($_SERVER['HTTP_RANGE']), 'bytes=')) {
$range = substr($range, 6);
$ranges = explode(',', $range);
$t = count($ranges);
}
header("Accept-Ranges: bytes");
header("Content-Type: " . $this->content_type);
header("Content-Transfer-Encoding: binary");
header(sprintf('Content-Disposition: attachment; filename="%s"', $this->name));
if ($t > 0) {
header("HTTP/1.1 206 Partial content");
$t === 1 ? $this->pushSingle($range) : $this->pushMulti($ranges);
} else {
header("Content-Length: " . $this->size);
$this->readFile();
}
flush();
}
private function pushSingle($range) {
$start = $end = 0;
$this->getRange($range, $start, $end);
header("Content-Length: " . ($end - $start + 1));
header(sprintf("Content-Range: bytes %d-%d/%d", $start, $end, $this->size));
fseek($this->file, $start);
$this->readBuffer($end - $start + 1);
$this->readFile();
}
private function pushMulti($ranges) {
$length = $start = $end = 0;
$output = "";
$tl = "Content-type: " . $this->content_type . "\r\n";
$formatRange = "Content-range: bytes %d-%d/%d\r\n\r\n";
foreach ( $ranges as $range ) {
$this->getRange($range, $start, $end);
$length += strlen("\r\n--$this->boundary\r\n");
$length += strlen($tl);
$length += strlen(sprintf($formatRange, $start, $end, $this->size));
$length += $end - $start + 1;
}
$length += strlen("\r\n--$this->boundary--\r\n");
header("Content-Length: $length");
header("Content-Type: multipart/x-byteranges; boundary=$this->boundary");
foreach ( $ranges as $range ) {
$this->getRange($range, $start, $end);
echo "\r\n--$this->boundary\r\n";
echo $tl;
echo sprintf($formatRange, $start, $end, $this->size);
fseek($this->file, $start);
$this->readBuffer($end - $start + 1);
}
echo "\r\n--$this->boundary--\r\n";
}
private function getRange($range, &$start, &$end) {
list($start, $end) = explode('-', $range);
$fileSize = $this->size;
if ($start == '') {
$tmp = $end;
$end = $fileSize - 1;
$start = $fileSize - $tmp;
if ($start < 0)
$start = 0;
} else {
if ($end == '' || $end > $fileSize - 1)
$end = $fileSize - 1;
}
if ($start > $end) {
header("Status: 416 Requested range not satisfiable");
header("Content-Range: */" . $fileSize);
exit();
}
return array(
$start,
$end
);
}
private function readFile() {
while ( ! feof($this->file) ) {
echo fgets($this->file);
flush();
usleep($this->delay);
}
}
private function readBuffer($bytes, $size = 1024) {
$bytesLeft = $bytes;
while ( $bytesLeft > 0 && ! feof($this->file) ) {
$bytesLeft > $size ? $bytesRead = $size : $bytesRead = $bytesLeft;
$bytesLeft -= $bytesRead;
echo fread($this->file, $bytesRead);
flush();
usleep($this->delay);
}
}
}
require_once('session.php');
session_write_close(); // don't block other scripts by locking the session file
if(isset($_GET['track'])) {
// is a valid file path given?
if(file_exists($_GET['track'])) {
try {
$download = new ResumeDownload($_GET['track'], "audio/mpeg", 0);
$download->process();
} catch (Exception $e) {
header('HTTP/1.1 404 File Not Found');
die('Sorry, an error occured.');
}
exit();
}
// otherwise search db for file path by track id
require_once('database.php');
$sql = "SELECT tr.id AS 'id', "
. "tr.title AS 'title', "
. "tr.path AS 'path', "
. "tr.track_number AS 'track_number', "
. "al.title AS 'album', "
. "al.id AS 'album_id', "
. "ar.title AS 'artist', "
. "tr.cover AS 'cover' "
. "FROM track tr "
. "INNER JOIN album al ON tr.album_id = al.id "
. "INNER JOIN artist ar ON tr.artist_id = ar.id "
. "WHERE tr.id = ?;";
$statement = $mysqli->prepare($sql);
$statement->bind_param('i', $_GET['track']);
$statement->execute();
$result = $statement->get_result();
while($row = $result->fetch_object()) {
$filename = $row->artist . " - " . $row->title . "." . pathinfo($row->path, PATHINFO_EXTENSION);
if(isset($_GET['download']) && $_GET['download'] != "") {
if(!ALLOW_DOWNLOADS) {
header('HTTP/1.1 401 Forbidden');
die('Download not allowed');
}
header("Content-Disposition: attachment; filename='$filename'");
if(LOG_DOWNLOADS) {
$sql = "INSERT INTO download (track_id, client) VALUES (?, ?)";
$statement2 = $mysqli->prepare($sql);
$statement2->bind_param('is', $row->id, $_SERVER['REMOTE_ADDR']);
$statement2->execute();
}
}
#readfile($row->path);
try {
$download = new ResumeDownload($row->path, "audio/mpeg", 0);
$download->process();
} catch (Exception $e) {
header('HTTP/1.1 404 File Not Found');
die('Sorry, an error occured.');
}
break;
}
}
?>