7 * Written by Dan Fuhry - 2008 |
7 * Written by Dan Fuhry - 2008 |
8 * |
8 * |
9 * This script is in the public domain. Use it for good, not evil. |
9 * This script is in the public domain. Use it for good, not evil. |
10 */ |
10 */ |
11 |
11 |
|
12 status('initializing Services_JSON'); |
|
13 $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
12 |
14 |
|
15 // keep track of playlist refresh |
|
16 $playlist_last_refresh = time(); |
13 |
17 |
|
18 /** |
|
19 * Terminate a request with an error string formatted as JSON. |
|
20 * @param string Error message |
|
21 */ |
|
22 |
|
23 function json_die($msg) |
|
24 { |
|
25 global $json; |
|
26 echo $json->encode(array( |
|
27 'mode' => 'error', |
|
28 'error' => $msg |
|
29 )); |
|
30 return true; |
|
31 } |
|
32 |
|
33 function ajax_request_handler($httpd) |
|
34 { |
|
35 global $playlist, $mime_types, $json; |
|
36 |
|
37 // Set content type |
|
38 $httpd->header("Content-type: {$mime_types['js']}"); |
|
39 |
|
40 // get PATH_INFO |
|
41 $pathinfo = @substr(@substr($_SERVER['REQUEST_URI'], 1), @strpos(@substr($_SERVER['REQUEST_URI'], 1), '/')+1); |
|
42 if ( empty($pathinfo) ) |
|
43 { |
|
44 return json_die('No action specified on URI'); |
|
45 } |
|
46 |
|
47 $params = explode('/', $pathinfo); |
|
48 $action =& $params[0]; |
|
49 switch ( $action ) |
|
50 { |
|
51 case 'stop': |
|
52 case 'next': |
|
53 case 'prev': |
|
54 echo dcop_action('player', 'stop'); |
|
55 break; |
|
56 case 'play': |
|
57 echo dcop_action('player', 'playPause'); |
|
58 break; |
|
59 case 'jump': |
|
60 $tid =& $params[1]; |
|
61 if ( !preg_match('/^[0-9]+$/', $tid) ) |
|
62 { |
|
63 return die_json('Invalid track ID'); |
|
64 } |
|
65 $tid = intval($tid); |
|
66 dcop_action('playlist', "playByIndex $tid"); |
|
67 $return = array( |
|
68 'current_track_length' => $playlist[$tid]['length_int'], |
|
69 'current_track_pos' => 0 |
|
70 ); |
|
71 echo $json->encode($return); |
|
72 break; |
|
73 case 'volume': |
|
74 $volume =& $params[1]; |
|
75 if ( !preg_match('/^[0-9]+$/', $volume) ) |
|
76 { |
|
77 return die_json('Invalid track ID'); |
|
78 } |
|
79 $volume = intval($volume); |
|
80 dcop_action('player', "setVolume $volume"); |
|
81 $return = array( |
|
82 'volume' => $volume |
|
83 ); |
|
84 echo $json->encode($return); |
|
85 break; |
|
86 case 'refresh': |
|
87 global $playlist_last_refresh, $playlist, $playlist_last_md5; |
|
88 if ( $playlist_last_refresh + 60 < time() ) |
|
89 { |
|
90 rebuild_playlist(); |
|
91 } |
|
92 $current_track = dcop_action('playlist', 'getActiveIndex'); |
|
93 $return = array( |
|
94 'is_playing' => dcop_action('player', 'isPlaying'), |
|
95 'current_track' => $current_track, |
|
96 'volume' => dcop_action('player', 'getVolume'), |
|
97 // include the MD5 of the playlist so that if it changes, the |
|
98 // client can refresh (otherwise things get madly corrupted) |
|
99 'playlist_hash' => $playlist_last_md5 |
|
100 ); |
|
101 if ( isset($playlist[$current_track]) ) |
|
102 { |
|
103 $return['current_track_length'] = $playlist[$current_track]['length_int']; |
|
104 $return['current_track_pos'] = dcop_action('player', 'trackCurrentTime'); |
|
105 } |
|
106 echo $json->encode($return); |
|
107 break; |
|
108 default: |
|
109 return json_die("Undefined action: $action"); |
|
110 } |
|
111 } |
|
112 |