initial import from pitchfork-0.5.5
[patchfork.git] / player / command.php
1 <?php
2 /* 
3     Pitchfork Music Player Daemon Client
4     Copyright (C) 2007  Roger Bystrøm
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; version 2 of the License.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20         
21         require_once("../inc/base.php");
22         require_once("../inc/JSON.php");
23
24         define('PF_FAILURE', 'failed');
25
26         $META_SEARCH = array("any", "Artist", "Title", "Album", "Genre", "filename", "Composer", "Performer", "Date" ); // "lyrics"...
27         $PL_SEARCH = array("any", "Artist", "Title", "Album", "Genre", "filename", "Composer", "Performer", "Date" ); // "lyrics"...
28
29         header("Content-Type: text/plain; charset=UTF-8");
30
31         function ids_to_list($sel) {
32                 $arr = explode(";", trim($sel));
33                 $res = array();
34                 $size = count($arr);
35                 foreach($arr as $val) {
36                         $pos = strpos($val, "-");
37                         if($pos === FALSE) {
38                                 $res[] = $val;
39                         }
40                         else {
41                                 $tmp = explode("-", $val);
42                                 $res = array_merge($res, range($tmp[0], $tmp[1])); 
43                         }
44                 }
45                 return $res;
46         }
47
48         function selection_to_list($sel) {
49                 $res = ids_to_list($sel);
50                 sort($sel, SORT_NUMERIC);
51                 return $res;
52         }
53                 
54         function selection_to_reverse_list($sel) {
55                 $res = ids_to_list($sel);
56                 rsort($res, SORT_NUMERIC);
57                 return $res;
58         }
59
60         function search_add($db, $pl, $search) {
61                 $tmp = $db->find($search, true);
62                 foreach($tmp as &$a) {
63                         $pl->addSong($a['file']);
64                 }
65                 if(count($tmp)>0) 
66                         return true;
67                 return false;
68         }
69
70         function parsePlaylist($txt, $type = false) {
71                 $txt = explode("\n", $txt); // trim will remove the \r
72                 $res = array();
73                 if($type=="pls"||$type===false) {
74                         foreach($txt as $t) {
75                                 $t = trim($t);
76                                 if(stripos($t, "file")!==false) { 
77                                         $pos = spliti("^file[0-9]*=", $t);
78                                         if(count($pos)==2&&strlen($pos[1])>0)
79                                                 $res[] = $pos[1];
80                                 }
81                         }
82                 }                       
83                 else if($type=="m3u" || ($type===false&&count($res)==0) ) {
84                         foreach($txt as $t) {
85                                 $t = trim($t);
86                                 if(strpos($t, "#")!==false||strlen($t)==0) {
87                                         echo "skipping: $t\n";
88                                         continue;
89                                 }
90                                 $res[] = $t;
91                         }
92                 }
93                 return $res;
94         }
95         function handle_playlist_url($pl, $url, $get = false) {
96                 if($get) {
97                         $fp = @fopen($url, "r");
98                         if($fp) {
99                                 $type = substr($url, strlen($url)-3); // just get three last chars..
100                                 $md = stream_get_meta_data($fp);
101                                 $md = $md['wrapper_data'];
102                                 foreach($md as $m) {
103                                         if(stripos($m, "content-type:")==0) {
104                                                 if(stripos($m, "audio/x-scpls")) {
105                                                         $typdde = "pls";
106                                                 }
107                                                 else if(stripos($m, "audio/x-mpegurl")
108                                                      || stripos($m, "audio/mpegurl")) {
109                                                      $type = "m3u";
110                                                 }
111                                         }
112                                 }
113                                 $type = strtolower($type);
114                                 $data = stream_get_contents($fp);
115                                 $stuff = parsePlaylist($data, $type);
116                                 foreach($stuff as $s) {
117                                         $pl->addSong($s);
118                                 }
119                                 return true;
120                         }
121                         return false;
122                 }
123                 else {
124                         $opts = array(
125                           'http'=>array(
126                              'method'=>"HEAD",
127                         ));
128                         $context = stream_context_create($opts);
129                         $fp = @fopen($url, "r", false, $context);
130                         $md = null;
131
132                         if(!$fp) { 
133                                 $md = array(); // head did not work....
134                         }
135                         else {
136                                 $md = stream_get_meta_data($fp);
137                                 $md = $md['wrapper_data'];
138                         }
139
140                         $type = substr($url, strlen($url)-3); // just get three last chars..
141
142                         /* these lists are probably incomplete, make a ticket if 
143                            you want something added */
144                         foreach($md as $m) {
145                                 if(stripos($m, "content-type:")==0) {
146                                         if(stripos($m, "audio/x-scpls")||
147                                            stripos($m, "audio/x-mpegurl")||
148                                            stripos($m, "audio/mpegurl")) {
149                                                 return handle_playlist_url($pl, $url, true);
150                                         }
151                                         else if(stripos($m, "audio/mpeg")||
152                                                 stripos($m, "audio/mpeg3")||
153                                                 stripos($m, "audio/x-mpeg3")||
154                                                 stripos($m, "audio/mpeg2")||
155                                                 stripos($m, "audio/x-mpeg2")||
156                                                 stripos($m, "application/ogg")||
157                                                 stripos($m, "audio/x-ogg")||
158                                                 stripos($m, "audio/mp4")||
159                                                 stripos($m, "audio/x-mod")||
160                                                 stripos($m, "audio/mod")||
161                                                 stripos($m, "audio/basic")||
162                                                 stripos($m, "audio/x-basic")||
163                                                 stripos($m, "audio/wav")||
164                                                 stripos($m, "audio/x-wav")||
165                                                 stripos($m, "audio/flac")||
166                                                 stripos($m, "audio/x-flac")
167                                                 ) {
168                                                 $pl->addSong($url);
169                                                 return true;
170                                         }
171                                 }
172                         }
173                         $type = strtolower($type);
174                         $type4 = strtolower($url, strlen($url)-4);
175                         if($type=="m3u"||$type=="pls") {
176                                 return handle_playlist_url($pl, $url, true);
177                         }
178                         else if($type=="ogg"||$type=="mp3"||$type=="mp2"||$type=="wav"
179                                 ||$type==".au"||$type=="m4a"||$type4=="flac"||$type4=="aiff") {
180                                 // ugh, just try to add it... 
181                                 $pl->addSong($url);
182                                 return true;
183                         }
184                 }
185                 return false;
186         }
187
188         function array_to_json(&$arr) {
189                 echo "(";
190                 if(function_exists("json_encode")) {
191                         echo json_encode($arr);
192                 } else {
193                         $json = new Services_JSON();
194                         $json->encode($arr);
195                 }
196                 echo ")";
197         }
198         
199         /* for auto-play-start on empty */
200         $playlist_empty = false;
201         $something_added = false;
202
203         $json = null;
204         $pl = get_playlist();
205         if(!$pl) {
206                 $v = array("connection" => PF_FAILURE);
207                 echo array_to_json($v);
208                 exit();
209         }
210         else if(isset($_GET['add'])||isset($_GET['ma'])||isset($_GET['searchadd'])) {
211                 /* for automatic playback start */
212                 try {
213                         $s = $pl->getStatus();
214                         if(isset($s['playlistlength'])&&intval($s['playlistlength'])==0) {
215                                 $playlist_empty = true;
216                         }
217                 }
218                 catch (PEAR_Exception $e) {
219                         $v = array("connection" => PF_FAILURE);
220                         echo array_to_json($v);
221                         exit();
222                 }
223         }
224
225         if(isset($_GET['playlist'])) {
226                 $act = $_GET['playlist'];
227                 try {
228                         if($act=="move"&&isset($_GET['from'])&&isset($_GET['to'])) {
229                                 // todo: sanity check
230                                 $response = null; 
231                                 if($pl->moveSongId($_GET['from'], $_GET['to'])) 
232                                         $response = array('result' => "ok");
233                                 else $response = array ('result' => PF_FAILURE);
234                                 $json = $response;
235                         }
236                         else if($act=="info"&&isset($_POST['ids'])) {
237                                 $list = ids_to_list($_POST['ids']);
238                                 $ret = array();
239                                 foreach($list as $id) {
240                                         $tmp = $pl->getPlaylistInfoId($id);
241                                         if(isset($tmp['file']))
242                                                 $ret[] = $tmp['file'][0];
243                                 }
244                                 $json = array();
245                                 unset($list);
246                                 $json['result'] = &$ret;
247                         }
248                         else {
249                                 $json = array("result" => PF_FAILURE);
250                         }
251                 }
252                 catch(PEAR_Exception $e) {
253                         $json = array ('result' => PF_FAILURE);
254                 }
255         }
256         else if(isset($_GET['rangemove'])&&is_numeric(trim($_GET['rangemove']))&&isset($_GET['elems'])) {
257                 $res = PF_FAILURE;
258                 $dest = intval($_GET['rangemove']);
259                 $pos_offset = 0;
260                 try {
261                         $list = selection_to_reverse_list($_GET['elems']);
262                         foreach($list as &$pos) {
263                                 //echo $pos-$pos_offset . "=>" .$dest."\n";
264
265                                 /* this means we've moved above the place where changing the position will
266                                  * have any effect on the list */
267                                 if($dest>$pos&&$pos_offset!=0) {
268                                         $pos_offset=0;
269                                         $dest--;
270                                 }
271
272                                 $pl->moveSong($pos-$pos_offset, $dest);
273                                 if($dest>$pos-$pos_offset) {
274                                         /* means we yanked something from over destination */
275                                         $dest--;
276                                 }
277                                 else if($dest<$pos-$pos_offset) {
278                                         /* means we yanked something from below destination */
279                                         $pos_offset--;
280                                 }
281                                 else {
282                                         /* moved it to our selves O_o */
283                                 //      echo "onself\n";
284                                 }
285                         }
286                         $res = "ok";
287                 }
288                 catch(PEAR_Exception $e) {
289                 }
290                 $json = array ('result' => $res);
291         }
292         else if(isset($_GET['ping'])) {
293                 $result = "pong";
294                 $json = array("result" => $result);
295         }
296         else if(isset($_GET['volume'])&&is_numeric(trim($_GET['volume']))) {
297                 $res = PF_FAILURE;
298                 try {
299                         $volume = trim($_GET['volume']);
300                         $play = get_playback();
301                         if($play->setVolume($volume)) 
302                                 $res = "ok";
303                         $play->disconnect();
304                 }
305                 catch(PEAR_Exception $e) {
306                 }
307                 $json = array("result" => $res);
308         }
309         else if(isset($_GET['position'])&&is_numeric(trim($_GET['position']))
310                 && isset($_GET['id']) && is_numeric(trim($_GET['id']))) {
311                 $result = PF_FAILURE;
312                 try {
313                         $pos = trim($_GET['position']);
314                         $id = trim($_GET['id']);
315                         $play = get_playback();
316                         if($play->seekId($id, $pos)) 
317                                 $result = "ok";
318                         $play->disconnect();
319                 }
320                 catch(PEAR_Exception $e) {
321                 }
322                 $json = array("result" => $result);
323
324         }
325         else if(isset($_GET['currentsong'])) {
326                 $res = "failure";
327                 try {
328                         $res = $pl->getCurrentSong();
329                         if(!$res) 
330                                 $res = "failure";
331                 }
332                 catch(PEAR_Exception $e) {
333                 }
334                 $json = array("result" => $res);
335         }
336         else if(isset($_GET['dirlist'])) {
337                 $dir = trim($_GET['dirlist']);
338                 $FILE = 0;
339                 $ARTIST = 1;
340                 $ALBUM = 2;
341                 $type = $FILE;
342
343                 if(isset($_GET['type'])&&is_numeric($_GET['type'])) {
344                         $type = $_GET['type'];
345                 }
346                 if(is_null($dir)||$dir=="")
347                         $dir = "/";
348                 $res = "failure";
349                 try {
350                         $db = get_database();
351
352                         if($type==$ALBUM||$type==$ARTIST) {
353                                 $t = false;
354                                 if(($t = strrpos($dir, "/")) !== false && $t == strlen($dir)-1) {
355                                         $dir = substr($dir, $t+1);
356                                 }
357                                 if(strlen($dir)==0) {
358                                         $type = $type==$ALBUM?"Album":"Artist";
359                                         $res = array(strtolower($type) => $db->getMetadata($type));
360                                 }
361                                 else {  
362                                         $res = array();
363                                         if($type==$ALBUM) {
364                                                 $res["artist"] = $db->getMetadata("Artist", "Album", $dir);
365                                                 $res['filelist'] = $db->find(array("Album" => $dir), true);
366                                         }
367                                         else if($type==$ARTIST) {
368                                                 $res["album"] = $db->getMetadata("Album","Artist", $dir);
369                                                 $res['filelist'] = $db->find(array("Artist" => $dir), true);
370                                         }
371                                 }
372                         }
373                         else {
374                                 $tmp = $db->getInfo($dir);
375                                 $res = array();
376
377                                 if(isset($tmp['directory'])) {
378                                         $res['directory'] =$tmp['directory'];
379                                 }
380                                 if(isset($tmp['file'])) {
381                                         $res['file'] = array();
382                                         foreach($tmp['file'] as &$row) {
383                                                 $res['file'][] = $row['file'];
384                                         }
385                                 }
386                                 if(isset($tmp['playlist'])) {
387                                         $res['playlist'] = $tmp['playlist'];
388                                 }
389                         }
390                         if(!$res) 
391                                 $res = "failure";
392                         $db->disconnect();
393                 }
394                 catch(PEAR_Exception $e) {
395                 }
396                 $json = array("result" => $res);
397         }
398         else if(isset($_GET['act'])) {
399                 $act = trim($_GET['act']);
400                 $result = "failure";
401                 try {
402                         $play = get_playback(); 
403
404                         if($act=="play") {
405                                 if(isset($_GET['id'])&&is_numeric(trim($_GET['id']))) {
406                                         if($play->playId(trim($_GET['id'])))
407                                                 $result = "ok";
408                                 }
409                                 else if(isset($_GET['pos'])&&is_numeric(trim($_GET['pos']))) {
410                                         if($play->play(trim($_GET['pos'])))
411                                                 $result = "ok";
412                                 }
413                                 else if($play->play()) {
414                                         $result = "ok";
415                                 }
416                         }
417                         else if($act == "toggle") {
418                                 if($play->pause())
419                                         $result = "ok";
420                         }
421                         else if($act == "next") {
422                                 if($play->nextSong())
423                                         $result = "ok";
424                         }
425                         else if( $act == "previous") {
426                                 if($play->previousSong())
427                                         $result = "ok";
428                         }
429                         else if($act=="stop") {
430                                 if($play->stop())
431                                         $result = "ok";
432                         }
433                         else $result = "invalid command";
434                         $play->disconnect();
435                 }
436                 catch(PEAR_Exception $e) {
437                         $result = "failure";
438                 }
439                 $json = array("result" => $result);
440         }
441         else if(isset($_GET['add'])) {
442                 $add = $_GET['add'];
443                 try {
444                         $res = PF_FAILURE;
445                         if($pl->addSong($add)) {
446                                 $res = "ok";
447                                 $something_added = true;
448                         }
449                 }
450                 catch(PEAR_Exception $e) {
451                 }
452                 $json = array("result" => $res);
453         }
454         else if(isset($_GET['remove'])) {
455                 $arr = selection_to_reverse_list($_GET['remove']);
456                 $res = "ok";
457                 try {
458                         foreach($arr as &$val) {
459                                 if(!$pl->deleteSong($val))
460                                         $res = "failure";
461                         }
462                 }
463                 catch(PEAR_Exception $e) {
464                         $result = "failure";
465                 }
466                 $json = array("result" => $res);
467         }
468         else if(isset($_GET['updatedb'])) {
469                 $res = PF_FAILURE;
470                 try {
471                         $adm = get_admin();
472                         if($adm->updateDatabase())
473                                 $res = "ok";
474                         $adm->disconnect();
475                 }
476                 catch(PEAR_Exception $e) {
477                         $res = PF_FAILURE;
478                 }
479                 $json = array("result" => $res);
480         }
481         else if(isset($_GET['outputs'])||isset($_GET['output_e'])||isset($_GET['output_d'])) {
482                 $res = PF_FAILURE;
483                 try {
484                         $admin = get_admin();
485                         if(isset($_GET['outputs']))
486                                 $res = $admin->getOutputs();
487                         else if(isset($_GET['output_e'])&&is_numeric($_GET['output_e']))
488                                 $res = $admin->enableOutput(trim($_GET['output_e']))?"1":PF_FAILURE;
489                         else if(isset($_GET['output_d'])&&is_numeric($_GET['output_d']))
490                                 $res = $admin->disableOutput(trim($_GET['output_d']))?"0":PF_FAILURE;
491                         $admin->disconnect();
492                 }
493                 catch(PEAR_Exception $e) {
494                         $res = PF_FAILURE;
495                 }
496                 $json = array("result" => $res);
497         }
498         else if(isset($_GET['random'])) {
499                 $res = "failure";
500                 try {
501                         $play = get_playback(); 
502                         $val = $_GET['random']=="1";
503                         if($play->random($val)) {
504                                 $res = $val?"1":"0";
505                         }
506                         $play->disconnect();
507                 }
508                 catch(PEAR_Exception $e) {
509                 }
510                 $json = array("result" => $res);
511
512         }
513         else if(isset($_GET['repeat'])) {
514                 $res = "failure";
515                 try {
516                         $play = get_playback(); 
517                         $val = $_GET['repeat']=="1";
518                         if($play->repeat($val)) {
519                                 $res = $val?"1":"0";
520                         }
521                         $play->disconnect();
522                 }
523                 catch(PEAR_Exception $e) {
524                 }
525                 $json = array("result" => $res);
526         }
527         else if(isset($_GET['xfade'])&&is_numeric($_GET['xfade'])) {
528                 $res = PF_FAILURE;
529                 try {
530                         $play = get_playback(); 
531                         if($play->setCrossfade(trim($_GET['xfade'])))
532                                 $res = "ok";
533                         $play->disconnect();
534                         
535                 }
536                 catch(PEAR_Exception $e) {
537                 }
538                 $json = array("result" => $res);
539         }
540         else if(isset($_GET['quick_search'])) {
541                 $dir = trim($_GET['quick_search']);
542                 $res = PF_FAILURE;
543                 try {
544                         $search_dir = strrpos($dir, "/");
545                         if($search_dir) {
546                                 $search_dir = substr($dir, 0, $search_dir);
547                         }
548                         else {
549                                 $search_dir = "";
550                         }
551                         $db = get_database(); 
552                         $tmp = $db->getInfo($search_dir);
553                         if(isset($tmp['directory'])) {
554                                 $res = array();
555                                 $i=0;
556                                 foreach($tmp['directory'] as $key => &$value) {
557                                         if(stripos($value, $dir)===0) {
558                                                 $i++;
559                                                 $res[$key] = &$value;
560                                         }
561                                         if($i>=20) /* return up to x entries */
562                                                 break;
563                                 }
564                         }
565                         $db->disconnect();
566                         
567                 }
568                 catch(PEAR_Exception $e) {
569                 }
570                 $json = array("result" => $res);
571         }
572         else if(isset($_GET['searchadd'])||isset($_GET['searchfile'])) {
573                 $artist = null;
574                 $album = null;
575                 $res = PF_FAILURE;
576                 if(isset($_GET['artist'])&&strlen($_GET['artist'])>0)
577                         $artist = $_GET['artist'];
578                 if(isset($_GET['album'])&&strlen($_GET['album'])>0)
579                         $album = $_GET['album'];
580                 if(!(is_null($artist)&&is_null($album))) {
581                 try {
582                         $db = get_database();
583                         $params = array();
584                         if(!is_null($artist)) 
585                                 $params["Artist"] = $artist;
586                         if(!is_null($album)) 
587                                 $params["Album"] = $album;
588
589
590                         if(isset($_GET['searchadd'])) {
591                                 if(search_add($db, $pl, $params)) {
592                                         $res = "ok";
593                                         $something_added = true;
594                                 }
595                                 else $res = "notfound";
596                         }
597                         else {
598                                 $res = array();
599                                 $res['filelist'] = $db->find($params, true);
600                         }
601                         $db->disconnect();
602                 }
603                 catch(PEAR_Exception $e) {
604                         $res = PF_FAILURE;
605                 }
606                 }
607                 $json = array("result" => $res);
608         }
609         else if(((isset($_GET['metasearch'])&&is_numeric($_GET['metasearch']))||
610                  (isset($_GET['plsearch'])&&is_numeric($_GET['plsearch'])))
611                 &&isset($_GET['s'])) {
612                 $plsearch = isset($_GET['plsearch']);
613
614                 $type = intval($plsearch?$_GET['plsearch']:$_GET['metasearch']);
615                 $search = $_GET['s'];
616                 $res = PF_FAILURE;
617                 if($type>=0&&$type<count($plsearch?$PL_SEARCH:$META_SEARCH)) {
618                         try {
619                                 $tmp = null; 
620                                 if($plsearch&&$pl->hasFind()) {
621                                         $tmp = $pl->find(array($PL_SEARCH[$type] => $search));
622                                 }
623                                 else if($plsearch) {
624                                         $data = $pl->getPlaylistInfoId();
625                                         if(isset($data['file']))
626                                                 $data = $data['file'];
627                                         $tmp = array();
628                                         $t = $PL_SEARCH[$type];
629                                         foreach($data as &$song) {
630                                                 if($type===0) { // any
631                                                         foreach($song as &$e)
632                                                                 if(stristr($e, $search)!==FALSE) {
633                                                                         $tmp[] = $song;
634                                                                         break;
635                                                                 }
636                                                 }
637                                                 else if(isset($song[$t]) && stristr($song[$t],$search)!==FALSE)
638                                                         $tmp[] = $song;
639                                         }
640                                 }
641                                 else {
642                                         $db = get_database();
643                                         $tmp = $db->find(array($META_SEARCH[$type] => $search));
644                                         $db->disconnect();
645                                 }
646                                 $res = array();
647                                 /* strip */
648                                 $keys = array("Artist", "Title", "file", "Pos");
649                                 foreach($tmp as &$row) {
650                                         $e = array();
651                                         foreach($row as $key => &$val) {
652                                                 if(in_array($key, $keys)!==FALSE)
653                                                         $e[$key] = $val;
654                                         }
655                                         $res[] = $e; 
656                                 }
657                         }
658                         catch(PEAR_Exception $e) {
659                                 //$res = $e->getMessage();
660                         }
661                 }
662                 else if($type==count($META_SEARCH)) { // search lyrics...
663                         /* this should probably have been in metadata.php, but don't need 
664                          * to change anything if we have it here, kiss */
665                         $tmp = array();
666                         if(is_dir($metadata_dir)&&is_readable($metadata_dir)) {
667                                 $files = scandir($metadata_dir); 
668                                 foreach($files as $file) {
669                                         $pos = strrpos($file, ".lyric");
670                                         if($pos!==false&&$pos==strlen($file)-6) {
671                                                 $xml = @simplexml_load_file($metadata_dir . $file);
672                                                 if($xml&&isset($xml->result[0])&&isset($xml->result[0]->lyric[0])) {
673                                                         $l = (string)$xml->result[0]->lyric[0];
674                                                         if(stripos($l, $search)!==false) {
675                                                                 if(isset($xml->file)) {
676                                                                         /*
677                                                                         foreach($xml->file as $f) {
678                                                                                 $e = array(); 
679                                                                                 $e['file'] = (string)$f;
680                                                                                 $e['Artist'] = (string)$xml->result[0]->artist[0];
681                                                                                 $e['Title'] = (string)$xml->result[0]->title[0];
682                                                                                 $res[] = $e;
683                                                                         }
684                                                                         */
685                                                                         $e = array(); 
686                                                                         $e['Artist'] = (string)$xml->result[0]->artist[0];
687                                                                         $e['Title'] = (string)$xml->result[0]->title[0];
688                                                                         $tmp[] = $e;
689                                                                 }
690                                                         }
691                                                 }
692                                         }
693                                 }
694                         }
695                         $db = get_database();
696
697                         $res = array();
698                         foreach($tmp as &$row) {
699                                 $sr = $db->find(array("Artist" => $row['Artist'], "Title" => $row["Title"]));
700                                 /*var_dump($tmp);
701                                 break;*/
702                                 if(isset($sr[0])&&isset($sr[0]['file'])) {
703                                         $row['file'] = $sr[0]['file'];
704                                         $res[] = $row;
705                                 }
706                         }
707                         $db->disconnect();
708                 }
709                 $json = array("result" => $res);
710         }
711         else if(isset($_GET['ma'])) {
712                 /* note to self: should merge single add with this */
713                 $res = PF_FAILURE;
714                 if (!isset($HTTP_RAW_POST_DATA))
715                    $HTTP_RAW_POST_DATA = file_get_contents("php://input");
716                 $ma = explode("\n", $HTTP_RAW_POST_DATA);
717                 $db = false;
718                 $sparam = array();
719                 if(count($ma)) {
720                         $tmp = explode(":", $ma[0], 2);
721                         if($tmp[0]=="baseartist") {
722                                 $sparam['Artist'] = $tmp[1];
723                         }
724                         else if($tmp[0]=="basealbum") {
725                                 $sparam['Album'] = $tmp[1];
726                         }       
727                 }
728                 try {
729                         foreach($ma as &$guom) {
730                                 $v = explode(":", $guom, 2);
731                                 if(!count($v)==2)
732                                         continue;
733                                 $res.=$v[0];
734                                 if($v[0]=="file"||$v[0]=="directory") {
735                                         $pl->addSong($v[1]);
736                                 } /* we should never get same as baseartist/album here, if we do I don't care */
737                                 else if($v[0]=="album"||$v[0]=="artist") {
738                                         if($v[0]=="album")
739                                                 $sparam["Album"] = $v[1];
740                                         else $sparam["Artist"] = $v[1];
741                                         if(!$db) 
742                                                 $db = get_database();
743                                         search_add($db, $pl, $sparam);
744                                 }
745                                 else if($v[0]=="playlist") {
746                                         $pl->loadPlaylist($v[1]);
747                                 }
748                         }
749                         $res = "ok";
750                         $something_added = true;
751                         if($db)
752                                 $db->disconnect();
753                 }
754                 catch(PEAR_Exception $e) { }
755                 $json = array("result" => $res);
756         }
757         else if(isset($_GET['playlist_rm'])||isset($_GET['playlist_load'])
758                 ||isset($_GET['playlist_save'])||isset($_GET['playlist_add_url'])) {
759
760                 $res = false;
761                 try {
762                         if(isset($_GET['playlist_rm'])) {
763                                 $res = $pl->deletePlaylist(trim($_GET['playlist_rm']));
764                         }
765                         else if(isset($_GET['playlist_load'])) {
766                                 $res = $pl->loadPlaylist(trim($_GET['playlist_load']));
767                         }
768                         else if(isset($_GET['playlist_save'])) {
769                                 $res = $pl->savePlaylist(trim($_GET['playlist_save']));
770                         }
771                         else if(isset($_GET['playlist_add_url'])) {
772                                 $url = trim($_GET['playlist_add_url']);
773                                 if(stripos($url, "http://")==0) {
774                                         $res = handle_playlist_url($pl, $url);
775                                 }
776                         }
777                 }
778                 catch(PEAR_Exception $e) {}
779                 $res = $res?"ok":PF_FAILURE;
780                 $json = array("result" => $res);
781         }
782         else if(isset($_GET['clearerror'])) {
783                 $pl->clearError();
784                 $json = array("result" => "ok");
785         }
786
787         if(!is_null($json)) {
788                 try {
789                         if($playlist_empty&&$something_added) {
790                                 $play = get_playback();
791                                 $play->play();
792                                 $play->disconnect();
793                         }
794                         $json["status"] = $pl->getStatus();
795                         if(isset($_GET['plchanges'])&&is_numeric(trim($_GET['plchanges']))&&$_GET['plchanges']!=$json['status']['playlist']) {
796                                 $res = $pl->getChanges(trim($_GET['plchanges']), true);
797                                 if($res&&isset($res['file'])&&is_array($res['file'])) {
798                                         
799                                         if(isset($_GET['pmax'])&&isset($_GET['page'])) {
800                                                 $arr = $res['file'];
801                                                 $max = intval($_GET['pmax']);
802                                                 $page = intval($_GET['page']);
803                                                 $start = $max * $page;
804                                                 $end = $start + $max;
805                                                 $ret = array();
806                                                 foreach($res['file'] as $f) {
807                                                         if($f['cpos']>=$start&&$f['cpos']<$end)
808                                                                 $ret[] = $f;
809                                                         if($f['cpos']>=$end)
810                                                                 break;
811                                                 }
812                                                 if(count($ret)>0)
813                                                         $json['plchanges'] = &$ret;
814
815                                         }
816                                         else {
817                                                 $json["plchanges"] = &$res['file'];
818                                         }
819                                 }
820                         }
821                         $pl->disconnect();
822                 }
823                 catch(PEAR_Exception $e) {
824                 }
825                 array_to_json($json);
826         }
827         else {
828                 try {
829                 $pl->disconnect();
830                 } catch(PEAR_Exception $e) {}
831         }
832 ?>
This page took 0.194894 seconds and 4 git commands to generate.