/
Shoutcast.php
279 lines (252 loc) · 7.66 KB
/
Shoutcast.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Shoutcast
* Class for reading the SHOUTcast XML file
* @author Wavestreaming http://Wavestreaming.com
*/
class Shoutcast
{
private $_host,
$_port,
$_stream,
$_pass,
$_xml,
$_status = false,
$_error = false;
public function __construct($host = '', $port = 8000, $pass = '', $stream = 1)
{
$this->setDetails($host, $port, $pass, $stream);
}
/**
* Set the details of the shoutcast server
* @param string $host shoutcast host (without http://)
* @param int $port shoutcast port (default 8000)
* @param string $pass shoutcast admin password (not always required)
* @param int $stream shoutcast stream id (for sc ver2, default is 1)
* @return Shoutcast just for method chaining
*/
public function setDetails($host, $port = 8000, $pass = '', $stream = 1)
{
$this->_host = $host;
$this->_port = $port;
$this->_pass = $pass;
$this->_stream = $stream;
return $this;
}
/**
* Get the full stats, including listeners etc
* http://wiki.winamp.com/wiki/SHOUTcast_DNAS_Server_2_XML_Reponses#Full_Server_Summary
* @return bool true if this is a real server, false if its not
*/
public function getAll()
{
$url = 'http://' . $this->_host . ':' . $this->_port . '/admin.cgi?mode=viewxml' . '&pass=' . $this->_pass . '&sid=' . $this->_stream;
return $this->_curl($url);
}
/**
* Get the basic stats from the /stats xml
* http://wiki.winamp.com/wiki/SHOUTcast_DNAS_Server_2_XML_Reponses#Equivalent_of_7.html
* @return bool
*/
public function getBasicStats()
{
return $this->_curl('http://' . $this->_host . ':' . $this->_port . '/stats?sid=' . $this->_stream);
}
/**
* Get data from the shout cast 7.html
* For shoutcast version <2
* @return stdClass
*/
public function getSevenHTML()
{
$data = $this->_curl('http://' . $this->_host . ':' . $this->_port . '/7.html', false);
$ret = new \stdClass();
if ($data) {
$parts = explode(',', $data);
if (count($parts) > 5) {
$ret->currentListeners = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->streamStatus = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->peakListeners = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->maxListeners = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->uniqueListeners = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->bitrate = (isset($parts[0])) ? array_shift($parts) : 0;
$ret->songTitle = implode(',', $parts);
return $ret;
} else {
$this->_error = 'Did not get all parts, data: <br/><br/>' . $data;
}
} else {
$this->_error = 'Failed to get data';
}
return false;
}
/**
* Get an array of connected listeners
* @return array array of listeners
*/
public function getListeners()
{
if ($this->_status === true) {
if (isset($this->_xml->LISTENERS, $this->_xml->LISTENERS->LISTENER)) {
$listeners = array();
foreach ($this->_xml->LISTENERS->LISTENER as $listener) {
$listeners[] = (array) $listener;
}
return $listeners;
}
}
return false;
}
/**
* Get a song history array
* @return array array of song arrays
*/
public function getSongHistory()
{
$songs = array();
if ($this->_status === true) {
if (isset($this->_xml->SONGHISTORY, $this->_xml->SONGHISTORY->SONG)) {
foreach ($this->_xml->SONGHISTORY->SONG as $song) {
$songs[] = array(
'playedat' => (string) $song->PLAYEDAT,
'title' => (string) $song->TITLE,
);
}
return $songs;
}
}
return false;
}
/**
* Method to check if the stream is online or not
* @return bool true if streaming
*/
public function streamStatus()
{
return (bool) $this->get('streamStatus');
}
/**
* Get an attribute from the feed
* @param string $attr the attribute
* @param bool $string if the output should be cast as string.
* If string is not true a simple xml element will be returned.
* @return mixed attribute or false
*/
public function get($attr, $string = true)
{
$attr = strtoupper($attr);
if ($this->_status === true) {
if (isset($this->_xml->$attr)) {
return ($string === true) ? (string) $this->_xml->$attr : $this->_xml->$attr;
}
}
return false;
}
/**
* A wrapper for the get() method, just to be lazy.
* @return Depends.
*/
public function __get($key)
{
return $this->get($key);
}
/**
* Get the full XML feed
* @return SimpleXMLElement
*/
public function getXML()
{
if ($this->_status === true) {
return $this->_xml;
}
return false;
}
/**
* Get information about na error
* @return string error string
*/
public function getError()
{
return $this->_error;
}
/**
* Kick a listener
* @param int $id the user id
* @return Shoutcast
*/
public function kick($id)
{
$url = 'http://' . $this->_host . ':' . $this->_port . '/admin.cgi?pass=' . $this->_pass . '&sid=' . $this->_stream . '&mode=kickdst&kickdst=' . $id;
$this->_curl($url,false);
return $this;
}
/**
* Ban a listener
* @param string $ip listener's ip addr
* @param type $banmsk
* @return Shoutcast
*/
public function ban($ip, $banmsk = 255)
{
$url = 'http://' . $this->_host . ':' . $this->_port . '/admin.cgi?pass=' . $this->_pass . '&sid=' . $this->_stream . '&mode=bandst&bandst=' . $ip . '&banmsk=' . $banmsk;
$this->_curl($url,false);
return $this;
}
/**
* Kick the currently connected source
* @return Shoutcast
*/
public function kickSource()
{
$url = 'http://' . $this->_host . ':' . $this->_port . '/admin.cgi?pass=' . $this->_pass . '&sid=' . $this->_stream . '&mode=kicksrc';
$r = $this->_curl($url,false);
return $this;
}
/**
* Tell the DNAS to reload the config file
* @return string DNAS response
*/
public function reloadConfig()
{
$url = 'http://' . $this->_host . ':' . $this->_port . '/admin.cgi?pass=' . $this->_pass . '&mode=reload&force=1';
return $this->_curl($url,false);
}
/**
* Get the xml feed etc
* @return bool
*/
private function _curl($url, $xml = true)
{
$ret = false;
$this->_status = false;
$this->_error = false;
$ch = curl_init();
if (is_resource($ch)) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13)');
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
if ($xml === true) {
$this->_xml = @simplexml_load_string($data);
if ($this->_xml instanceof \SimpleXMLElement && $this->_xml->getName() == 'SHOUTCASTSERVER') {
$this->_status = true;
$ret = true;
} else {
$this->_error = 'Not SHOUTcast';
}
} else {
$this->_status = true;
$ret = $data;
}
} else {
$this->_error = 'Failed to connect';
}
} else {
$this->_error = 'cURL is not a resource';
}
return $ret;
}
}