3 Pitchfork Music Player Daemon Client
4 Copyright (C) 2007 Roger Bystrøm
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.
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.
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.
21 /* how much time must pass before we try searching for cover art again */
22 $COVER_SEARCH_AGAIN = 86400;
24 include("aws_signed_request.php");
25 require_once("../inc/base.php");
26 require_once("metadata_cover.php");
28 function amazonlink($params)
30 $params["SubscriptionId"] = "15BH771NY941TX2NKC02";
31 $res = aws_signed_request("com", $params, "Access Key ID", "Secret Access Key");
36 /* metadata should not require locking of session */
37 session_write_close();
39 $missing_metadata_dir = false;
41 if(!file_exists($metadata_dir)) {
42 if(!mkdir($metadata_dir, 0755)) {
43 $missing_metadata_dir = true;
46 if(!is_writeable($metadata_dir)) {
47 $missing_metadata_dir = true;
50 if($missing_metadata_dir) {
51 $xml = array_to_xml(array("result" => "nocachedir"));
56 function escape_name($name) {
57 return str_replace(DIRECTORY_SEPARATOR, "_", $name);
60 function get_cover_base_name($artist, $album) {
62 return $cover_dir . escape_name($artist) . " - " . escape_name($album);
65 function get_album_info_name($artist, $album) {
66 return get_cover_base_name($artist, $album) . ".txt";
68 function get_lyric_filename($artist, $title) {
70 return $cover_dir . escape_name($artist) . " - ". escape_name($title) . ".lyric";
73 function find_lyrics($arr) {
74 foreach($arr as $val) {
77 if(isset($val['name'])&&$val['name']=="RETURN") {
78 return $val['children'];
80 else if(is_array($val)) {
81 $ret = find_lyrics($val);
89 function fp_get_contents($fp) {
92 while($tmp = fgets($fp))
100 /* Queries amazon with the specified url, strict serach first and then a more careless one,
101 * will urlencode artist and albumname
102 * returns xml document or false upon failure */
103 function amazon_album_query($params) {
104 $stype = array("Title", "Keywords");
105 $artist = urlencode($artist);
106 $album = urlencode($album);
107 foreach($stype as $st) {
110 $xml = amazonlink($params);
111 if($xml&&isset($xml->Items[0])&&isset($xml->Items[0]->Item[0]))
117 /* returns file pointer or false */
118 function get_album_lock($artist, $album) {
119 $file_name = get_album_info_name($artist, $album);
120 $exists = file_exists($file_name);
124 $fp = @fopen($file_name, "r+");
125 else $fp = @fopen($file_name, "w+");
126 if($fp && flock($fp, LOCK_EX))
129 trigger_error("Can't lock album-file: $file_name", E_USER_WARNING);
133 /* waits for appropriate amazon time, have to be called before making any amazon requests
134 returns true if ok to continue otherwise false */
135 function amazon_wait() {
136 global $metadata_dir;
138 /* rationale behind this:
139 * amazon requires that we don't make more than one request pr. second pr. ip */
141 $file_name = $metadata_dir . "amazon_time";
142 if(file_exists($file_name))
143 $fp = @fopen($file_name, "r+");
144 else $fp = @fopen($file_name, "w+");
147 trigger_error("Can't open amazon_time", E_USER_WARNING);
150 if(!flock($fp, LOCK_EX)) {
152 trigger_error("Can't lock amazon_time", E_USER_WARNING);
156 $last = fp_get_contents($fp);
159 if(is_numeric($last)) {
160 $stime = current_time_millis() - $last;
162 $stime = abs($stime);
164 usleep($stime*1000); // micro seconds
167 if(@fwrite($fp, current_time_millis())===false) {
169 trigger_error("Can't write to amazon_time", E_USER_WARNING);
178 /* returns artist and album info and get's album lock or dies */
179 /* return value: array($fp, $artist, $album) */
180 function init_album_artist_or_die() {
182 header("Content-Type: text/xml; charset=UTF-8");
186 if(isset($_GET['artist'])&&isset($_GET['album']) &&
187 strlen(trim($_GET['artist']))>0&&strlen(trim($_GET['album']))>0) {
188 $album = trim($_GET['album']);
189 $artist = trim($_GET['artist']);
192 $xml = array_to_xml(array("result" => "missingparam"));
197 $fp = get_album_lock($artist, $album);
200 $xml = array_to_xml(array("result" => "failed"));
204 return array($fp, $artist, $album);
207 /* returns array(artist, album, filename) or false */
208 function get_current_info() {
210 $pl = get_playback();
212 $info = $pl->getCurrentSong();
213 if(isset($info['Artist'])&&isset($info['Title'])) {
214 $artist = trim($info['Artist']);
215 $title = trim($info['Title']);
216 $file_name = $info['file'];
217 return array($artist, $title, $file_name);
222 catch(PEARException $e) {
228 function get_cover() {
229 global $COVER_SEARCH_AGAIN, $cover_providers;
231 list($fp, $artist, $album) = init_album_artist_or_die();
233 $xml = fp_get_contents($fp);
235 $xml = @simplexml_load_string($xml);
238 if(isset($xml->notfound)&&is_numeric((string)$xml->notfound[0])) {
239 $time = @intval((string)$xml->notfound[0]);
240 if($time+$COVER_SEARCH_AGAIN<time())
243 else if(!isset($xml->image[0])&&!isset($xml->thumbnail[0])) {
248 $xml->addChild("cached", "true");
258 foreach($cover_providers as $cp) {
259 $res = $cp($artist, $album);
260 if($res&&is_array($res))
265 if($res&&is_array($res)) {
266 foreach($res as $key => $val) {
267 if(!isset($xml->$key))
268 $xml->$key = (string)$val;
272 $xml->notfound = time();
276 if($res&&is_array($res)) {
277 $res['time'] = time();
278 $xml = array_to_xml($res);
281 $xml = array("notfound" => time());
282 $xml = array_to_xml($xml);
286 @fwrite($fp, $xml->asXML());
293 function get_review() {
294 global $COVER_SEARCH_AGAIN;
296 list($fp, $artist, $album) = init_album_artist_or_die();
298 $xml = fp_get_contents($fp);
309 $xml = @simplexml_load_string($xml);
311 if(isset($xml->rnotfound)&&is_numeric((string)$xml->rnotfound[0])) {
312 $time = @intval((string)$xml->rnotfound[0]);
313 if($time+$COVER_SEARCH_AGAIN>time())
319 if(!$xml||(!(isset($xml->review[0])||isset($xml->desc[0]))&&!$no_search)) {
322 echo array_to_xml(array("result" => "failed"))->asXML();
326 if($xml&&isset($xml->asin[0])) {
327 $res = amazonlink(array("Operation"=>"ItemLookup", "IdType"=>"ASIN", "ItemId"=>"urlencode($xml->asin[0])"));
331 $res = @amazon_album_query(array("Operation"=>"ItemSearch", "SearchIndex"=>"Music", "Artist"=>"$artist", "Album"=>"$album"));
334 if($res&&isset($res->Items[0])&&isset($res->Items[0]->Item[0])) {
335 $p = $res->Items[0]->Item[0];
336 $asin = (string) $p->ASIN;
337 if(isset($p->EditorialReviews[0])) {
338 $p = $p->EditorialReviews[0];
339 foreach($p->EditorialReview as $er) {
340 if(!$desc&&"Album Description" == (string)$er->Source) {
341 $desc = (string) $er->Content;
344 $review_src = (string) $er->Source;
345 $review = (string) $er->Content;
349 /* set info in xml-file... */
352 $xml->review_src = htmlspecialchars($review_src);
353 $xml->review = htmlspecialchars($review);
356 $xml->desc = htmlspecialchars($desc);
358 if(!isset($xml->asin[0])) {
359 $xml->addChild("asin", $asin);
362 if(!$review&&!$desc) {
371 $xml['asin'] = $asin;
373 $xml['desc'] = $desc;
375 $xml['review_src'] = $review_src;
376 $xml['review'] = $review;
380 $xml = array_to_xml($xml);
393 $xml->addChild("cached", "true");
398 if(isset($xml->rnotfound)) {
399 $xml->rnotfound = time();
402 $xml->addChild("rnotfound", time());
404 @fwrite($fp, $xml->asXML());
407 @fwrite($fp, $xml->asXML());
411 $xml = array_to_xml(array("rnotfound" => time()));
412 @fwrite($fp, $xml->asXML());
419 /* artist, title and song file name in system */
420 function _get_lyric_lyricwiki($artist, $title, $file_name) {
421 $file = get_lyric_filename($artist, $title);
422 $fp = fsockopen("lyricwiki.org", 80);
424 $xml = array_to_xml(array("result"=>"connectionfailed"));
425 return $xml->asXML();
428 $out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
429 $out .= "<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" ";
430 $out .= "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ";
431 $out .= "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
432 $out .= "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
433 $out .= "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ";
434 $out .= "xmlns:tns=\"urn:LyricWiki\">";
435 $out .= "<SOAP-ENV:Body><tns:getSong xmlns:tns=\"urn:LyricWiki\">";
436 $out .= "<artist xsi:type=\"xsd:string\">";
437 $out .= htmlspecialchars($artist);
439 $out .= "<song xsi:type=\"xsd:string\">";
440 $out .= htmlspecialchars($title);
442 $out .= "</tns:getSong></SOAP-ENV:Body></SOAP-ENV:Envelope>\r\n";
444 $head = "POST /server.php HTTP/1.1\r\n";
445 $head .= "Host: lyricwiki.org\r\n";
446 $head .= "SOAPAction: urn:LyricWiki#getSong\r\n";
447 $head .= "Content-Type: text/xml; charset=UTF-8\r\n";
448 $head .= "User-Agent: RemissPitchfork/0.1\r\n";
449 $head .= "Content-Length: " . str_byte_count($out) . "\r\n";
450 $head .= "Connection: Close\r\n\r\n";
452 fwrite($fp, $head . $out);
454 $responseHeader = "";
455 /* assume everything is dandy */
457 $responseHeader.= fread($fp, 1);
459 while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));
463 $ret .= fgets($fp, 128);
466 /* stupid hack to get around wrong xml declearation */
468 if(strpos($ret, "<". $qmark . "xml version=\"1.0\" encoding=\"ISO-8859-1\"".$qmark.">")===0)
469 $ret = str_replace("<". $qmark . "xml version=\"1.0\" encoding=\"ISO-8859-1\"".$qmark.">",
470 "<". $qmark . "xml version=\"1.0\" encoding=\"UTF-8\"".$qmark.">",
475 $parser = new xml2array();
476 $parser->parse($ret);
477 $data = find_lyrics($parser->arrOutput);
478 // check that data is ok and lyrics exist
479 if($data&&isset($data[2]['tagData'])) {
481 foreach($data as $d) {
482 if($d['name']=="ARTIST")
483 $res['artist'] = $d['tagData'];
484 else if($d['name']=="SONG")
485 $res['title'] = $d['tagData'];
486 else if($d['name']=="LYRICS")
487 $res['lyric'] = $d['tagData'];
488 else if($d['name']=="URL")
489 $res['url'] = $d['tagData'];
491 $res['from'] = "lyricwiki.org";
492 $res['time'] = time();
493 /* this caching thing will have to be extracted if we
494 * put in another lyrics source */
495 if(trim($res['lyric'])&&trim($res['lyric'])!="Not found") {
496 $xml = array_to_xml(array("result" => $res));
497 $xml->addChild("file", htmlspecialchars($file_name));
498 $res = $xml->asXML();
499 @file_put_contents($file, $res);
502 $out = array("result" => "notfound");
503 if(isset($res['url']))
504 $out['url'] = $res['url'];
505 $res = array_to_xml($out);
506 $res = $res->asXML();
513 /* $file: filename of cached version
514 * $file_name: file name of song */
515 function _get_lyric_cache($file, $file_name) {
516 $xml = @simplexml_load_file($file);
519 if(isset($xml->file)) {
520 foreach($xml->file as $f) {
521 if(((string)$f)==$file_name)
526 $xml->addChild("file", htmlspecialchars($file_name));
527 @file_put_contents($file, $xml->asXML());
529 $xml->addChild("cached", "true");
530 return $xml->asXML();
535 function get_lyric($info = null) {
536 header("Content-Type: text/xml; charset=UTF-8");
539 $info = get_current_info();
541 $xml = array_to_xml(array("result"=>"failed"));
547 $file_name = $info[2];
549 $file = get_lyric_filename($artist, $title);
550 if(file_exists($file)&&!isset($_GET['force'])) {
551 $xml = _get_lyric_cache($file, $file_name);
558 $xml = _get_lyric_lyricwiki($artist, $title, $file_name);
563 echo array_to_xml(array("result" => "failed"))->asXML();
571 $b_name = basename(trim($_GET['pic']));
572 $name = $cover_dir . $b_name;
573 if(file_exists($name)&&is_readable($name)) {
574 if(function_exists("finfo_open")&&function_exists("finfo_file")) {
575 $f = finfo_open(FILEINFO_MIME);
576 header("Content-Type: " . finfo_file($f, $name));
578 else if(function_exists("mime_content_type")) {
579 header("Content-Type: " . mime_content_type($name));
582 header("Content-Type: image/jpeg");
584 $c = "Content-Disposition: inline; filename=\"";
585 $c .= rawurlencode($b_name) . "\"";
587 echo @file_get_contents($name);
592 echo "File does not exist\n";
593 trigger_error("Did not find albumart althought it was requested", E_USER_WARNING);
598 function get_recommendations_from_playlist() {
599 require_once("../player/openstrands.php");
600 $pl = get_playlist();
601 $list = $pl->getPlaylistInfo();
603 foreach($list as $song) {
604 if(isset($song['Artist'])&&$song['Artist'])
605 $artist[$song['Artist']] = true;
607 $artist = array_keys(array_change_key_case($artist));
610 header("Content-Type: text/xml; charset=UTF-8");
612 $ret = strands_get_recommendations($artist);
614 if(!$ret || ! count($ret)) {
615 $res['result'] = is_array($ret)?"notfound":"failed";
616 echo array_to_xml($res)->asXML();
619 $db = get_database();
620 foreach($ret as $a) {
623 $tmp['album'] = $db->getMetadata("Album", "Artist", $a);
626 $out = array("result" => $res);
628 echo array_to_xml($out)->asXML();
631 function do_houseclean() {
632 /* this is a *very* inefficient method, but it's needed... */
633 //header("Content-Type: text/xml; charset=UTF-8");
634 header("Content-type: multipart/x-mixed-replace;boundary=--ThisRandomString");
636 global $metadata_dir;
638 echo "--ThisRandomString\n";
639 $out = "Content-type: text/html\n\n".
640 "<html><head><title>Housecleaning</title></head><body>\n".
641 "<p>Performing housecleaning, please wait...</p>\n";
643 echo "$out--ThisRandomString\n";
646 set_time_limit(300); // this might take a while, but
647 // shouldn't be more than 5 mins even on slow machines
648 $db = get_database();
651 $time = current_time_millis();
652 $list = $db->getAll();
653 if(!isset($list['file']))
655 $files = $list['file'];
657 $list = scandir($metadata_dir);
658 $total = count($list);
664 foreach($list as $f) {
665 $r = strrpos($f, ".lyric");
667 if($r!==false&&$r+6==strlen($f)) {
668 $xml = @simplexml_load_file($metadata_dir . $f);
670 if($fcount%100 == 0) {
672 echo "<p>Processed $fcount (".(int)($tcount*100/$total)."%)..</p>\n";
673 echo "--ThisRandomString\n";
679 foreach($xml->file as $v) {
680 $x_files[] = (string)$v;
682 $dis = array_intersect($x_files, $files);
683 if(count($dis)!=count($x_files)) {
684 $dom = @dom_import_simplexml($xml);
690 while($elem = $dom->getElementsByTagName("file")->item(0)) {
691 $dom->removeChild($elem);
694 $xml = simplexml_import_dom($dom);
695 array_to_xml($dis, $xml, "file");
696 @$xml->asXML($metadata_dir . $f);
705 $result = array("time" => intval(current_time_millis() - $time), "fixed" => $fixed, "errors" => $errors);
707 catch(PEAR_Exception $e) {
709 echo "Content-type: text/html\n\n";
711 if(is_array($result)) {
712 echo "Result of cleaning:<br/>\n";
713 echo "$fcount files checked in " . $result['time'] . "ms of which $fcount_inv was invalid<br/>";
714 echo "Fixed: " . $result['fixed'] . "<br/>";
715 echo "Errors: " . $result['errors'] . "<br/>\n";
718 else if($result=="failed") {
719 echo "It appears housecleaning failed, check your MPD settings";
722 echo "hmm.. somethings wrong, try again";
724 echo "</p><p><a href='config.php'>Back to configuration</a></p></body></html>\n";
725 echo "\n--ThisRandomString\n";
729 if(!isset($iamincluded)) {
730 if(isset($_GET['cover'])) get_cover();
731 else if(isset($_GET['review'])) get_review();
732 else if(isset($_GET['lyric'])) get_lyric();
733 else if(isset($_GET['pic'])) get_pic();
734 else if(isset($_GET['housecleaning'])) do_houseclean();
735 else if(isset($_GET['plrecommend'])) get_recommendations_from_playlist();
737 header("Content-Type: text/xml; charset=UTF-8");
738 $xml = array_to_xml(array("result"=>"what do you want?"));
747 var $arrOutput = array();
752 function parse($strInputXML) {
754 $this->resParser = xml_parser_create("UTF-8");
756 xml_set_object($this->resParser,$this);
757 xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
758 xml_parser_set_option($this->resParser, XML_OPTION_TARGET_ENCODING, "UTF-8");
760 xml_set_character_data_handler($this->resParser, "tagData");
762 $this->strXmlData = xml_parse($this->resParser,$strInputXML );
763 if(!$this->strXmlData) {
764 die(sprintf("XML error: %s at line %d",
765 xml_error_string(xml_get_error_code($this->resParser)),
766 xml_get_current_line_number($this->resParser)));
769 xml_parser_free($this->resParser);
771 return $this->arrOutput;
773 function tagOpen($parser, $name, $attrs) {
774 $tag=array("name"=>$name,"attrs"=>$attrs);
775 array_push($this->arrOutput,$tag);
778 function tagData($parser, $tagData) {
779 if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData']))
780 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
782 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
785 function tagClosed($parser, $name) {
786 $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
787 array_pop($this->arrOutput);