-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
308 lines (231 loc) · 6.52 KB
/
Copy pathaction.php
File metadata and controls
308 lines (231 loc) · 6.52 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
<?PHP
/*
* AJAX wraper script for the Net_MPD class
* this script is for makeing changes to the MPD server, changing the song, setting the mode to play or stop, playlist manipulation, etc
* note this is expected to be run from a secure environment and it does not interface with an actual database
* so standard security measures are not as importaint, things like SQL injection are impossible because there is no SQL to inject into
* this is NOT intended to be open to an unsecure network, it is intended to be operated from behind a secure firewall or a VPN tunnel
* the action performed is in $_GET all perameters should be in $_POST
*/
/*
* this returns JSON data, lets give it the right header
*/
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Fri, 28 May 1982 00:00:00 GMT');
header('Content-type: application/json');
/*
* get the MDP object
*/
require_once('Net/MPD.php');
$MPD = new Net_MPD();
/*
* first sanity check, an action needs to be defined, if it is not then this script will not know what to do
*/
if(!isset($_GET['action'])){
echo '{"success": false, "reason":"no action defined"}';
}else{
/*
* NET_MPD will throw errors so we need to catch them and report the failure
*/
try{
/*
* this is fairly straight forward, map the action to the code that implements it
*/
switch($_GET['action']){
/*
* play the current song
*/
case 'play':
//if the state is 'stop' then we need to use play, otherwise we use pause
$state = $MPD->Common->getStatus();
$song = $state['song'];
$state = $state['state'];
//if passed a song to play, play that song
if(isset($_POST['id'])){
$MPD->Playback->playId($_POST['id']);
}else if($state == 'stop'){
//we must specify a song or it will default to the first in the list
$MPD->Playback->play($song);
}else{
$MPD->Playback->pause();
}
echo '{"success": true}';
break;
/*
* stop the current song
*/
case 'stop':
$MPD->Playback->stop();
echo '{"success": true}';
break;
/*
* play the next song
*/
case 'skip':
$MPD->Playback->nextSong();
echo '{"success": true}';
break;
/*
* play the song before the current one
*/
case 'back':
$MPD->Playback->previousSong();
echo '{"success": true}';
break;
/*
* save the current playlist to the passed name
*/
case 'save_playlist':
//MPD does not support overwriteing of playlists, so if one exsists already we need to delete it
$playlists = $MPD->Playlist->getPlaylists();
foreach($playlists as &$playlist){
if($playlist == $_POST['playlist'])
$MPD->Playlist->deletePlaylist($playlist);
}
//now we should be able to save the playlist
$MPD->Playlist->savePlaylist($_POST['playlist']);
echo '{"success": true}';
break;
/*
* set the volume
*/
case 'set_volume':
$vol = (int)$_POST['volume'];
if($vol<0)$vol=0;
if($vol>100)$vol=100;
$MPD->Playback->setVolume($vol);
echo '{"success": true}';
break;
/*
* seek to the specified playback point
*/
case 'set_seek':
$song = $MPD->Common->getStatus();
$song = $song['song'];
$MPD->Playback->seek((int)$song, (int)$_POST['time']);
echo '{"success": true}';
break;
/*
* change the currently playing song to the song passed
*/
case 'change_song':
$MPD->Playback->play((int)$_POST['song_idx']);
echo '{"success": true}';
break;
/*
* remove the passed songs from the playlist
*/
case 'remove_songs':
$songs = json_decode($_POST['songs']);
foreach($songs as $song){
$MPD->Playlist->deleteSongId($song);
}
echo '{"success": true}';
break;
/*
* clear theplaylist
*/
case 'clear_playlist':
$MPD->Playlist->clear();
break;
/*
* delete playlists given by name
*/
case 'delete_playlist':
$playlists = json_decode($_POST['playlists']);
foreach($playlists as $playlist){
$MPD->Playlist->deletePlaylist($playlist);
}
echo '{"success": true}';
break;
/*
* add the passed songs to the playlist
*/
case 'add_songs':
$songs = json_decode($_POST['songs']);
foreach($songs as $song){
$MPD->Playlist->addSong($song);
}
echo '{"success": true}';
break;
/*
* set the random play state
*/
case 'set_random':
$val =$_POST['value'];
if($val<0)$val=0;
if($val>1)$val=1;
$MPD->Playback->random($val);
echo '{"success": true}';
break;
/*
* set the repeat play state
*/
case 'set_repeat':
$val =$_POST['value'];
if($val<0)$val=0;
if($val>1)$val=1;
$MPD->Playback->repeat($val);
echo '{"success": true}';
break;
/*
* set the crossfade play state
*/
case 'set_crossfade':
$val = $_POST['value'];
if($val<0)$val=0;
$MPD->Playback->setCrossfade($val);
echo '{"success": true}';
break;
/*
* load the passed playlists as the current one
* optional parameter 'append' should be set to true if you do not want to erase what is in the current playlist
*/
case 'load_playlist':
//when the playlist changes MPD stops playback, but the user never tells it to do this,
//so lets work arround this by telling it to play after changeing if it was playing before
$was_playing = $MPD->Common->getStatus();
$was_playing = $was_playing['state'] == 'play';
//MPD default behaviour is to append
if(!isset($_POST['append']) || !$_POST['append']){
$MPD->Playlist->clear();
}
$playlists = json_decode($_POST['playlist']);
foreach($playlists as $playlist){
//load the actual playlist
$MPD->Playlist->loadPlaylist($playlist);
}
//play the music if it was playing before
if($was_playing)
$MPD->Playback->play();
//return success
echo '{"success": true}';
break;
/*
* handle unhandeled actions
*/
default:
echo '{"success": false, "reason":"unrecognised action", "parameters":"';
foreach($_POST as $key => $value){
echo '['.$key.' => '.$value.']';
}
echo '"}';
}
} catch (Exception $e) {
/*
* send a nicely formatted error responce
* add all info we might have gotten from NET_MPD
*/
$error = $MPD->Common->getErrorData();
echo '{"success": false, "reason":"'.$e->getMessage().' | ';
foreach($error as $key => $value){
echo '['.$key.' => '.$value.']';
}
echo '", "parameters":"';
foreach($_POST as $key => $value){
echo '['.$key.' => '.$value.']';
}
echo '"}';
}
}
?>