-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
371 lines (316 loc) · 8.55 KB
/
common.php
File metadata and controls
371 lines (316 loc) · 8.55 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
// Include settings file.
if (file_exists('settings.php')) {
require_once 'settings.php';
}
else {
throw new Exception('Missing settings file');
}
// Define commands.
define('WEB_MPD_MUTE', 'mute');
define('WEB_MPD_VOLUME', 'volume');
define('WEB_MPD_PLAY', 'play');
define('WEB_MPD_STOP', 'stop');
define('WEB_MPD_PAUSE', 'pause');
define('WEB_MPD_CURRENT', 'current');
define('WEB_MPD_STATUS', 'status');
define('WEB_MPD_PLAYLIST', 'playlist');
define('WEB_MPD_CLEAR', 'clear');
define('WEB_MPD_UPDATE', 'update');
define('WEB_MPD_SEEK_CURRENT', 'current');
define('WEB_MPD_SEEK_TOTAL', 'total');
/**
* Render all css files.
*
* @return string
*/
function web_mpd_render_css() {
$css[] = '<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">';
$css[] = '<link rel="stylesheet" href="/includes/css/JQuery.JSAjaxFileUploader.css" >';
$css[] = '<link rel="stylesheet" href="/includes/css/web-mpd.css">';
return implode(PHP_EOL, $css) . PHP_EOL;
}
/**
* Render all js files.
*
* @return string
*/
function web_mpd_render_js() {
$js[] = '<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>';
$js[] = '<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>';
$js[] = '<script src="/includes/js/JQuery.JSAjaxFileUploader.min.js"></script>';
$js[] = '<script src="/includes/js/web-mpd.js"></script>';
return implode(PHP_EOL, $js) . PHP_EOL;
}
/**
* Render additional head tags files.
*
* @return string
*/
function web_mpd_render_metatag() {
$meta[] = '<link rel="shortcut icon" href="/includes/images/favicon.ico" type="image/x-icon">';
$meta[] = '<link rel="icon" href="/includes/images/favicon.ico" type="image/x-icon">';
$meta[] = '<link rel="apple-touch-icon" href="/includes/images/touch-icon-iphone.jpg">';
$meta[] = '<link rel="apple-touch-icon" sizes="76x76" href="/includes/images/touch-icon-ipad.jpg">';
$meta[] = '<link rel="apple-touch-icon" sizes="120x120" href="/includes/images/touch-icon-iphone-retina.jpg">';
$meta[] = '<link rel="apple-touch-icon" sizes="152x152" href="/includes/images/touch-icon-ipad-retina.jpg">';
return implode(PHP_EOL, $meta) . PHP_EOL;
}
/**
* Callback for handle POST requests.
*/
function web_mpd_post_handle() {
if (!empty($_POST['command'])) {
$command = $_POST['command'];
if ($command == WEB_MPD_MUTE) {
web_mpd_mute_toggle();
exit;
}
isset($_POST['value'])
? web_mpd_command($command, $_POST['value'])
: web_mpd_command($command);
}
elseif (!empty($_POST['upload_url'])) {
web_mpd_upload_url($_POST['upload_url']);
}
}
/**
* Callback for handle GET requests.
*/
function web_mpd_get_handle() {
header('Content-Type: application/json');
$data = array();
if (isset($_GET['current'])) {
$data['current'] = web_mpd_current() . ' (#' . web_mpd_current_track_state() . ')';
$data['current_id'] = web_mpd_current_id();
}
if (isset($_GET['current_seek'])) {
$data['seek']['current'] = web_mpd_seek_get(WEB_MPD_SEEK_CURRENT);
$data['seek']['total'] = web_mpd_seek_get(WEB_MPD_SEEK_TOTAL);
}
print json_encode($data);
exit;
}
/**
* Callback for handle FILE requests.
*/
function web_mpd_files_handle() {
global $conf;
// Handle errors.
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
// Save file into music directory and update playlist
move_uploaded_file($_FILES['file']['tmp_name'], $conf['music_path'] . '/' . $_FILES['file']['name']);
sleep(1);
web_mpd_update_playlist();
}
/**
* Upload file by url.
*/
function web_mpd_upload_url($url) {
global $conf;
if ($data = file_get_contents($url)) {
file_put_contents($conf['music_path'] . '/' . time() . '.mp3', $data);
sleep(1);
web_mpd_update_playlist();
}
}
/**
* Render playlist.
*
* @return string
*/
function web_mpd_render_playlist() {
$playlist = web_mpd_playlist();
if ($playlist[1]) {
$already = FALSE;
foreach ($playlist as $id => $track) {
if (!$already && $track == web_mpd_current()) {
$already = TRUE;
$class = 'active';
$button = '<div class="btn-group btn-group-xs"><button data-id="' . $id . '" type="button" class="btn btn-default btn-playlist"><span class="glyphicon glyphicon-pause"></span></button></div>';
}
else {
$class = '';
$button = '<div class="btn-group btn-group-xs"><button data-id="' . $id . '" type="button" class="btn btn-default btn-playlist"><span class="glyphicon glyphicon-play"></span></button></div>';
}
$list[$id] = '<li class="list-group-item ' . $class . '">';
$list[$id] .= $button;
$list[$id] .= $track;
$list[$id] .= '<button data-id="' . $id . '" type="button" class="btn btn-default btn-remove"><span class="glyphicon glyphicon-remove"></span></button>';
$list[$id] .= '</li>';
}
return implode(PHP_EOL, $list) . PHP_EOL;
}
return '';
}
/**
* Render control buttons.
*
* @return string
*/
function web_mpd_render_buttons() {
$buttons = array(
'backward' => 'Previous',
WEB_MPD_PLAY => 'Play',
WEB_MPD_PAUSE => 'Pause',
WEB_MPD_STOP => 'Stop',
'forward' => 'Next',
);
foreach ($buttons as $class => $text) {
$list[] = "<button type='button' class='btn btn-default btn-action' title='$text'><span class='glyphicon glyphicon-$class'></span> $text</button>";
}
return implode(PHP_EOL, $list) . PHP_EOL;
}
/**
* Execute specific mpc command.
*
* @param string $command
* @param string $arg
*
* @return string
*/
function web_mpd_command($command, $arg = '') {
$result = array();
exec("mpc $command $arg", $result);
return implode(PHP_EOL, $result);
}
/**
* Print title of current track.
*
* @return string
*/
function web_mpd_current() {
return web_mpd_command(WEB_MPD_CURRENT);
}
/**
* Get current track state.
*
* @return string
*/
function web_mpd_current_track_state() {
$status = web_mpd_status();
preg_match('/#[0-9]*\/[0-9]*/', $status, $matches);
$track_info = trim(reset($matches), '#');
return $track_info;
}
/**
* Get current volume.
*
* @return int
*/
function web_mpd_volume_get() {
$volume = web_mpd_command(WEB_MPD_VOLUME);
return trim(substr($volume, 7), '% ');
}
/**
* Get seek of current song.
*
* @param string $type
*
* @return int
*/
function web_mpd_seek_get($type = WEB_MPD_SEEK_TOTAL) {
$status = web_mpd_status();
if (preg_match('/\d+:\d+\/\d+:\d+/', $status, $seeks)) {
list($current, $total) = explode('/', $seeks[0]);
$current = explode(':', $current);
$total = explode(':', $total);
$current = ($current[0] * 60) + $current[1];
$total = ($total[0] * 60) + $total[1];
return $type == WEB_MPD_SEEK_TOTAL ? $total : $current;
}
return 0;
}
/**
* Get mpd status.
*
* @return string
*/
function web_mpd_status() {
return web_mpd_command(WEB_MPD_STATUS);
}
/**
* Check if repeat is enabled.
*
* @return string
*/
function web_mpd_is_repeat() {
return strpos(web_mpd_status(), 'repeat: on') === FALSE ? '' : 'active';
}
/**
* Check if single is enabled.
*
* @return string
*/
function web_mpd_is_single() {
return strpos(web_mpd_status(), 'single: on') === FALSE ? '' : 'active';
}
/**
* Check if random is enabled.
*
* @return string
*/
function web_mpd_is_random() {
return strpos(web_mpd_status(), 'random: on') === FALSE ? '' : 'active';
}
/**
* Check if volume is disable.
*
* @return string
*/
function web_mpd_is_mute() {
return web_mpd_volume_get() == 0 ? 'active' : '';
}
/**
* Get playlist.
*
* @return array.
*/
function web_mpd_playlist() {
$list = web_mpd_command(WEB_MPD_PLAYLIST);
$list = explode(PHP_EOL, $list);
$count = count($list);
for($i = $count; $i > 0; $i--){
$list[$i] = $list[$i - 1];
}
unset($list[0]);
return $list;
}
/**
* Get current track id.
*
* @return int
*/
function web_mpd_current_id() {
$track_info = reset(explode('/', web_mpd_current_track_state()));
return $track_info;
}
/**
* Add all tracks to playlist.
*/
function web_mpd_update_playlist() {
web_mpd_command(WEB_MPD_CLEAR);
web_mpd_command(WEB_MPD_UPDATE);
web_mpd_command('ls', '| mpc add');
}
/**
* Toggle the volume muting.
*/
function web_mpd_mute_toggle() {
if (web_mpd_is_mute()) {
web_mpd_command(WEB_MPD_VOLUME, 50);
}
else {
web_mpd_command(WEB_MPD_VOLUME, 0);
}
}