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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
<?php
/*
Copyright (c) 2001 - 2007 Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
class Localplay {
/* Base Variables */
public $type;
/* Built Variables */
private $_function_map = array();
private $_template;
private $_preferences = array();
private $_player;
/**
* Constructor
* This must be called with a localplay type, it then loads the config
* file for the specified type and attempts to load in the function
* map, the preferences and the template
*/
public function __construct($type) {
$this->type = $type;
$this->_get_info();
} // Localplay
/**
* _get_info
* This functions takes the type and attempts to get all the
* information needed to load it. Will log errors if there are
* any failures, fatal errors will actually return something to the
* gui
*/
private function _get_info() {
$this->_load_player();
} // _get_info
/**
* format
* This makes the localplay/plugin information
* human readable
*/
public function format() {
if (!is_object($this->_player)) { return false; }
$this->f_name = ucfirst($this->type);
$this->f_description = $this->_player->get_description();
$this->f_version = $this->_player->get_version();
} // format
/**
* _load_player
* This function attempts to load the player class that localplay
* Will interface with in order to make all this magical stuf work
* all LocalPlay modules should be located in /modules/<name>/<name>.class.php
*/
private function _load_player() {
if (!$this->type) { return false; }
$filename = Config::get('prefix') . '/modules/localplay/' . $this->type . '.controller.php';
$include = require_once ($filename);
if (!$include) {
/* Throw Error Here */
debug_event('localplay','Unable to load ' . $this->type . ' controller','2');
return false;
} // include
else {
$class_name = "Ampache" . $this->type;
$this->_player = new $class_name();
if (!($this->_player instanceof localplay_controller)) {
debug_event('Localplay',$this->type . ' not an instance of controller abstract, unable to load','1');
unset($this->_player);
return false;
}
$function_map = $this->_player->function_map();
$this->_map_functions($function_map);
}
} // _load_player
/**
* has_function
* This is used to check the function map and see if the current
* player type supports the indicated function.
*/
public function has_function($function_name) {
/* Check the function map, if it's got a value it must
* be possible
*/
if (strlen($this->_function_map[$function_name]) > 0) { return true; }
return false;
} // has_function
/**
* format_name
* This function takes the track name and checks to see if 'skip'
* is supported in the current player, if so it returns a 'skip to'
* link, otherwise it returns just the text
*/
public function format_name($name,$id) {
$name = scrub_out($name);
if ($this->has_function('skip')) {
$url = conf('ajax_url') . "?action=localplay&cmd=skip&value=$id" . conf('ajax_info');
$name = "<span style=\"cursor:pointer;text-decoration:underline;\" onclick=\"ajaxPut('$url');return true;\">$name</span>";
}
return $name;
} // format_name
/**
* _map_functions
* This takes the results from the loaded from the target player
* and maps them to the defined functions that Ampache currently
* supports, this is broken into require and optional componets
* Failure of required componets will cause log entry and gui
* warning. The value of the elements in the $data array should
* be function names that are called on the action in question
*/
private function _map_functions($data) {
/* Required Functions */
$this->_function_map['add'] = $data['add'];
$this->_function_map['delete'] = $data['delete'];
$this->_function_map['play'] = $data['play'];
$this->_function_map['stop'] = $data['stop'];
$this->_function_map['get'] = $data['get'];
$this->_function_map['connect'] = $data['connect'];
$this->_function_map['status'] = $data['status'];
/* Recommended Functions */
$this->_function_map['pause'] = $data['pause'];
$this->_function_map['next'] = $data['next'];
$this->_function_map['prev'] = $data['prev'];
$this->_function_map['skip'] = $data['skip'];
$this->_function_map['get_playlist'] = $data['get_playlist'];
$this->_function_map['get_playing'] = $data['get_playing'];
$this->_function_map['repeat'] = $data['repeat'];
$this->_function_map['random'] = $data['random'];
$this->_function_map['loop'] = $data['loop'];
/* Optional Functions */
$this->_function_map['volume_set'] = $data['volume_set'];
$this->_function_map['volume_up'] = $data['volume_up'];
$this->_function_map['volume_down'] = $data['volume_down'];
$this->_function_map['delete_all'] = $data['delete_all'];
$this->_function_map['randomize'] = $data['randomize'];
$this->_function_map['move'] = $data['move'];
$this->_function_map['add_url'] = $data['add_url'];
} // _map_functions
/**
* get_controllers
* This returns the controllers that are currently loaded into this instance
*/
public static function get_controllers() {
/* First open the dir */
$handle = opendir(Config::get('prefix') . '/modules/localplay');
if (!is_resource($handle)) {
debug_event('Localplay','Error: Unable to read localplay controller directory','1');
return array();
}
$results = array();
while ($file = readdir($handle)) {
if (substr($file,-14,14) != 'controller.php') { continue; }
/* Make sure it isn't a dir */
if (!is_dir($file)) {
/* Get the basename and then everything before controller */
$filename = basename($file,'.controller.php');
$results[] = $filename;
}
} // end while
return $results;
} // get_controllers
/**
* is_enabled
* This returns true or false depending on if the specified controller
* is currently enabled
*/
public static function is_enabled($controller) {
// Load the controller and then check for its preferences
$localplay = new Localplay($controller);
// If we can't even load it no sense in going on
if (!isset($localplay->_player)) { return false; }
return $localplay->_player->is_installed();
} // is_enabled
/**
* connect
* This function attempts to connect to the localplay
* player that we are using
*/
public function connect() {
$function = $this->_function_map['connect'];
/* This is very bad, that means they don't even
* have a connection function defined
*/
if (!$function) { return false; }
if (!$this->_player->$function()) {
debug_event('localplay','Error Unable to connect, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // connect
/**
* play
* This function passes NULL and calls the play function of the player
* object
*/
public function play() {
$function = $this->_function_map['play'];
if (!$this->_player->$function()) {
debug_event('localplay','Error Unable to start playback, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // play
/**
* stop
* This functions passes NULl and calls the stop function of the player
* object, it should recieve a true/false boolean value
*/
public function stop() {
$function = $this->_function_map['stop'];
if (!$this->_player->$function()) {
debug_event('localplay','Error Unable to stop playback, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // stop
/**
* add
* This function takes an array of song_ids and then passes the full URL
* to the player, this is a required function.
*/
public function add($songs) {
/* Call the Function Specified in the Function Map */
$function = $this->_function_map['add'];
if (!$this->_player->$function($songs)) {
debug_event('localplay','Error Unable to add songs, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // add
/**
* add_url
* This directly adds an array of URLs to the localplay module. This is really how I should
* have done add, will migrate to this eventually
*/
public function add_url($urls) {
$function = $this->_function_map['add_url'];
if (!$this->_player->$function($urls)) {
debug_event('localplay','Error Unable to add urls, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // add_url
/**
* repeat
* This turns the repeat feature of a localplay method on or
* off, takes a 0/1 value
*/
public function repeat($state) {
$function = $this->_function_map['repeat'];
$data = $this->_player->$function($state);
if (!$data) {
debug_event('localplay',"Error Unable to set Repeat to $state",'1');
}
return $data;
} // repeat
/**
* random
* This turns on the random feature of a localplay method
* It takes a 0/1 value
*/
public function random($state) {
$function = $this->_function_map['random'];
$data = $this->_player->$function($state);
if (!$data) {
debug_event('localplay',"Error Unable to set Random to $state",'1');
}
return $data;
} // random
/**
* status
* This returns current information about the state of the player
* There is an expected array format
*/
public function status() {
$function = $this->_function_map['status'];
$data = $this->_player->$function();
if (!count($data)) {
debug_event('localplay','Error Unable to get status, check ' . $this->type . ' controller','1');
return false;
}
return $data;
} // status
/**
* get
* This calls the get function of the player and then returns
* the array of current songs for display or whatever
* an empty array is passed on failure
*/
public function get() {
$function = $this->_function_map['get'];
$data = $this->_player->$function();
if (!count($data) OR !is_array($data)) {
debug_event('localplay','Error Unable to get song info, check ' . $this->type . ' controller','1');
return array();
}
return $data;
} // get
/**
* volume_set
* This isn't a required function, it sets the volume to a specified value
* as passed in the variable it is a 0 - 100 scale the controller is
* responsible for adjusting the scale if nessecary
*/
public function volume_set($value) {
/* Make sure it's int and 0 - 100 */
$value = int($value);
/* Make sure that it's between 0 and 100 */
if ($value > 100 OR $value < 0) { return false; }
$function = $this->_function_map['volume_set'];
if (!$this->_player->$function($value)) {
debug_event('localplay','Error: Unable to set volume, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // volume_set
/**
* volume_up
* This function isn't required. It tells the daemon to increase the volume
* by a pre-defined amount controlled by the controller
*/
public function volume_up() {
$function = $this->_function_map['volume_up'];
if (!$this->_player->$function()) {
debug_event('localplay','Error: Unable to increase volume, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // volume_up
/**
* volume_down
* This function isn't required. It tells the daemon to decrese the volume
* by a pre-defined amount controlled by the controller.
*/
public function volume_down() {
$function = $this->_function_map['volume_down'];
if (!$this->_player->$function()) {
debug_event('localplay','Error: Unable to decrese volume, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // volume_down
/**
* volume_mute
* This function isn't required, It tells the daemon to mute all output
* It's up to the controller to decide what that actually entails
*/
public function volume_mute() {
$function = $this->_function_map['volume_mute'];
if (!$this->_player->$function()){
debug_event('localplay','Error: Unable to mute volume, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // volume_mute
/**
* skip
* This isn't a required function, it tells the daemon to skip to the specified song
*/
public function skip($song_id) {
$function = $this->_function_map['skip'];
if (!$this->_player->$function($song_id)) {
debug_event('localplay','Error: Unable to skip to next song, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // skip
/**
* next
* This isn't a required function, it tells the daemon to go to the next
* song
*/
public function next() {
$function = $this->_function_map['next'];
if (!$this->_player->$function()) {
debug_event('localplay','Error: Unable to skip to next song, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // next
/**
* prev
* This isn't a required function, it tells the daemon to go the the previous
* song
*/
public function prev() {
$function = $this->_function_map['prev'];
if (!$this->_player->$function()) {
debug_event('localplay','Error: Unable to skip to previous song, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // prev
/**
* pause
* This isn't a required function, it tells the daemon to pause the
* song
*/
public function pause() {
$function = $this->_function_map['pause'];
if (!$this->_player->$function()) {
debug_event('localplay','Error: Unable to pause song, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // pause
/**
* get_preferences
* This functions returns an array of the preferences that the localplay
* controller needs in order to actually work
*/
public function get_preferences() {
$preferences = $this->_player->get_preferences();
return $preferences;
} // get_preferences
/**
* delete
* This removes songs from the players playlist as defined get function
*/
public function delete($songs) {
$function = $this->_function_map['delete'];
if (!$this->_player->$function($songs)) {
debug_event('localplay','Error: Unable to remove songs, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // delete
/**
* delete_all
* This removes every song from the players playlist as defined by the delete_all function
* map
*/
public function delete_all() {
$function = $this->_function_map['delete_all'];
if (!$this->_player->$function($songs)) {
debug_event('localplay','Error: Unable to delete entire playlist, check ' . $this->type . ' controller','1');
return false;
}
return true;
} // delete_all
/**
* get_user_state
* This function returns a user friendly version
* of the current player state
*/
public function get_user_state($state) {
switch ($state) {
case 'play':
return _('Now Playing');
break;
case 'stop':
return _('Stopped');
break;
case 'pause':
return _('Paused');
break;
default:
return _('Unknown');
break;
} // switch on state
} // get_user_state
/**
* get_user_playing
* This attempts to return a nice user friendly
* currently playing string
*/
public function get_user_playing() {
$status = $this->status();
/* Format the track name */
$track_name = $status['track_artist'] . ' - ' . $status['track_album'] . ' - ' . $status['track_title'];
/* This is a cheezball fix for when we were unable to find a
* artist/album (or one wasn't provided)
*/
$track_name = ltrim(ltrim($track_name,' - '),' - ');
$track_name = "[" . $status['track'] . "] - " . $track_name;
return $track_name;
} // get_user_playing
} //end localplay class
?>
|