lib_ampache.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * ownCloud - media plugin
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2010 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. //implementation of ampache's xml api
  23. class OC_MEDIA_AMPACHE{
  24. /**
  25. * fix the string to be XML compatible
  26. * @param string name
  27. * @return string
  28. */
  29. /* this is an ugly hack(tm), this should be: */
  30. /* htmlentities($name, ENT_XML1, 'UTF-8'); */
  31. /* with PHP 5.4 and later */
  32. public static function fixXmlString($name){
  33. $result=str_replace("&", "&amp;", $name);
  34. $result=str_replace("'", "&apos;", $result);
  35. $result=str_replace("<", "&lt;", $result);
  36. $result=str_replace(">", "&gt;", $result);
  37. $result=str_replace("\"", "&quot;", $result);
  38. $result=str_replace("Ä", "&#196;", $result);
  39. $result=str_replace("Ö", "&#214;", $result);
  40. $result=str_replace("Ü", "&#220;", $result);
  41. $result=str_replace("ä", "&#228;", $result);
  42. $result=str_replace("ö", "&#246;", $result);
  43. $result=str_replace("ü", "&#252;", $result);
  44. $result=str_replace("ß", "&#223;", $result);
  45. return $result;
  46. }
  47. /**
  48. * do the initial handshake
  49. * @param array params
  50. */
  51. public static function handshake($params){
  52. $auth=(isset($params['auth']))?$params['auth']:false;
  53. $user=(isset($params['user']))?$params['user']:false;
  54. $time=(isset($params['timestamp']))?$params['timestamp']:false;
  55. $now=time();
  56. if($now-$time>(10*60)){
  57. echo('<?xml version="1.0" encoding="UTF-8"?>');
  58. echo("<root>
  59. <error code='400'>timestamp is more then 10 minutes old</error>
  60. </root>");
  61. }
  62. if($auth and $user and $time){
  63. $query=OCP\DB::prepare("SELECT user_id, user_password_sha256 from *PREFIX*media_users WHERE user_id=?");
  64. $users=$query->execute(array($user))->fetchAll();
  65. if(count($users)>0){
  66. $pass=$users[0]['user_password_sha256'];
  67. $key=hash('sha256',$time.$pass);
  68. if($key==$auth){
  69. $token=hash('sha256','oc_media_'.$key);
  70. OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
  71. $date=date('c');//todo proper update/add/clean dates
  72. $songs=OC_MEDIA_COLLECTION::getSongCount();
  73. $artists=OC_MEDIA_COLLECTION::getArtistCount();
  74. $albums=OC_MEDIA_COLLECTION::getAlbumCount();
  75. $query=OCP\DB::prepare("INSERT INTO *PREFIX*media_sessions (`session_id`, `token`, `user_id`, `start`) VALUES (NULL, ?, ?, now());");
  76. $query->execute(array($token,$user));
  77. $expire=date('c',time()+600);
  78. echo('<?xml version="1.0" encoding="UTF-8"?>');
  79. echo("<root>
  80. <auth>$token</auth>
  81. <version>350001</version>
  82. <update>$date</update>
  83. <add>$date</add>
  84. <clean>$date</clean>
  85. <songs>$songs</songs>
  86. <artists>$artists</artists>
  87. <albums>$albums</albums>\
  88. <session_length>600</session_length>
  89. <session_expire>$expire</session_expire>
  90. <tags>0</tags>
  91. <videos>0</videos>
  92. </root>");
  93. return;
  94. }
  95. }
  96. echo('<?xml version="1.0" encoding="UTF-8"?>');
  97. echo("<root>
  98. <error code='400'>Invalid login</error>
  99. </root>");
  100. }else{
  101. echo('<?xml version="1.0" encoding="UTF-8"?>');
  102. echo("<root>
  103. <error code='400'>Missing arguments</error>
  104. </root>");
  105. }
  106. }
  107. public static function ping($params){
  108. if(isset($params['auth'])){
  109. if(self::checkAuth($params['auth'])){
  110. self::updateAuth($params['auth']);
  111. }else{
  112. echo('<?xml version="1.0" encoding="UTF-8"?>');
  113. echo("<root>
  114. <error code='400'>Invalid login</error>
  115. </root>");
  116. return;
  117. }
  118. }
  119. echo('<?xml version="1.0" encoding="UTF-8"?>');
  120. echo('<root>');
  121. echo('<version>350001</version>');
  122. echo('</root>');
  123. }
  124. public static function checkAuth($auth){
  125. if(is_array($auth)){
  126. if(isset($auth['auth'])){
  127. $auth=$auth['auth'];
  128. }else{
  129. return false;
  130. }
  131. }
  132. //remove old sessions
  133. $query=OCP\DB::prepare("DELETE from *PREFIX*media_sessions WHERE start<(NOW()-600)");
  134. $query->execute();
  135. $query=OCP\DB::prepare("SELECT user_id from *PREFIX*media_sessions WHERE token=?");
  136. $users=$query->execute(array($auth))->fetchAll();
  137. if(count($users)>0){
  138. OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
  139. return $users[0]['user_id'];
  140. }else{
  141. return false;
  142. }
  143. }
  144. public static function updateAuth($auth){
  145. $query=OCP\DB::prepare("UPDATE *PREFIX*media_sessions SET start=CURRENT_TIMESTAMP WHERE token=?");
  146. $query->execute(array($auth));
  147. }
  148. private static function printArtist($artist){
  149. $albums=count(OC_MEDIA_COLLECTION::getAlbums($artist['artist_id']));
  150. $songs=count(OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
  151. $id=$artist['artist_id'];
  152. $name=self::fixXmlString($artist['artist_name']);
  153. echo("\t<artist id='$id'>\n");
  154. echo("\t\t<name>$name</name>\n");
  155. echo("\t\t<albums>$albums</albums>\n");
  156. echo("\t\t<songs>$songs</songs>\n");
  157. echo("\t\t<rating>0</rating>\n");
  158. echo("\t\t<preciserating>0</preciserating>\n");
  159. echo("\t</artist>\n");
  160. }
  161. private static function printAlbum($album,$artistName=false){
  162. if(!$artistName){
  163. $artistName=OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
  164. }
  165. $artistName=self::fixXmlString($artistName);
  166. $songs=count(OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
  167. $id=$album['album_id'];
  168. $name=self::fixXmlString($album['album_name']);
  169. $artist=$album['album_artist'];
  170. echo("\t<album id='$id'>\n");
  171. echo("\t\t<name>$name</name>\n");
  172. echo("\t\t<artist id='$artist'>$artistName</artist>\n");
  173. echo("\t\t<tracks>$songs</tracks>\n");
  174. echo("\t\t<rating>0</rating>\n");
  175. echo("\t\t<year>0</year>\n"); /* make Viridian happy */
  176. echo("\t\t<disk>1</disk>\n"); /* make Viridian happy */
  177. echo("\t\t<art> </art>\n"); /* single space to make quickplay happy enough */
  178. echo("\t\t<preciserating>0</preciserating>\n");
  179. echo("\t</album>\n");
  180. }
  181. private static function printSong($song,$artistName=false,$albumName=false){
  182. if(!$artistName){
  183. $artistName=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
  184. }
  185. if(!$albumName){
  186. $albumName=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
  187. }
  188. $artistName=self::fixXmlString($artistName);
  189. $albumName=self::fixXmlString($albumName);
  190. $id=$song['song_id'];
  191. $name=self::fixXmlString($song['song_name']);
  192. $artist=$song['song_artist'];
  193. $album=$song['song_album'];
  194. echo("\t<song id='$id'>\n");
  195. echo("\t\t<title>$name</title>\n");
  196. echo("\t\t<artist id='$artist'>$artistName</artist>\n");
  197. echo("\t\t<album id='$album'>$albumName</album>\n");
  198. $url=OCP\Util::linkToRemote('ampache')."server/xml.server.php/?action=play&song=$id&auth={$_GET['auth']}";
  199. $url=self::fixXmlString($url);
  200. echo("\t\t<url>$url</url>\n");
  201. echo("\t\t<time>{$song['song_length']}</time>\n");
  202. echo("\t\t<track>{$song['song_track']}</track>\n");
  203. echo("\t\t<size>{$song['song_size']}</size>\n");
  204. echo("\t\t<art> </art>\n"); /* single space to make Viridian happy enough */
  205. echo("\t\t<rating>0</rating>\n");
  206. echo("\t\t<preciserating>0</preciserating>\n");
  207. echo("\t</song>\n");
  208. }
  209. public static function artists($params){
  210. if(!self::checkAuth($params)){
  211. echo('<?xml version="1.0" encoding="UTF-8"?>');
  212. echo("<root>
  213. <error code='400'>Invalid login</error>
  214. </root>");
  215. return;
  216. }
  217. $filter=isset($params['filter'])?$params['filter']:'';
  218. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  219. $artists=OC_MEDIA_COLLECTION::getArtists($filter,$exact);
  220. echo('<?xml version="1.0" encoding="UTF-8"?>');
  221. echo('<root>');
  222. foreach($artists as $artist){
  223. self::printArtist($artist);
  224. }
  225. echo('</root>');
  226. }
  227. public static function artist_songs($params){
  228. if(!self::checkAuth($params)){
  229. echo('<?xml version="1.0" encoding="UTF-8"?>');
  230. echo("<root>
  231. <error code='400'>Invalid login</error>
  232. </root>");
  233. return;
  234. }
  235. $filter=isset($params['filter'])?$params['filter']:'';
  236. $songs=OC_MEDIA_COLLECTION::getSongs($filter);
  237. $artist=OC_MEDIA_COLLECTION::getArtistName($filter);
  238. echo('<?xml version="1.0" encoding="UTF-8"?>');
  239. echo('<root>');
  240. foreach($songs as $song){
  241. self::printSong($song,$artist);
  242. }
  243. echo('</root>');
  244. }
  245. public static function artist_albums($params){
  246. if(!self::checkAuth($params)){
  247. echo('<?xml version="1.0" encoding="UTF-8"?>');
  248. echo("<root>
  249. <error code='400'>Invalid login</error>
  250. </root>");
  251. return;
  252. }
  253. global $SITEROOT;
  254. $filter=$params['filter'];
  255. $albums=OC_MEDIA_COLLECTION::getAlbums($filter);
  256. $artist=OC_MEDIA_COLLECTION::getArtistName($filter);
  257. echo('<?xml version="1.0" encoding="UTF-8"?>');
  258. echo('<root>');
  259. foreach($albums as $album){
  260. self::printAlbum($album,$artist);
  261. }
  262. echo('</root>');
  263. }
  264. public static function albums($params){
  265. if(!self::checkAuth($params)){
  266. echo('<?xml version="1.0" encoding="UTF-8"?>');
  267. echo("<root>
  268. <error code='400'>Invalid login</error>
  269. </root>");
  270. return;
  271. }
  272. $filter=isset($params['filter'])?$params['filter']:'';
  273. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  274. $albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter,$exact);
  275. echo('<?xml version="1.0" encoding="UTF-8"?>');
  276. echo('<root>');
  277. foreach($albums as $album){
  278. self::printAlbum($album,false);
  279. }
  280. echo('</root>');
  281. }
  282. public static function album_songs($params){
  283. if(!self::checkAuth($params)){
  284. echo('<?xml version="1.0" encoding="UTF-8"?>');
  285. echo("<root>
  286. <error code='400'>Invalid login</error>
  287. </root>");
  288. return;
  289. }
  290. $songs=OC_MEDIA_COLLECTION::getSongs(0,$params['filter']);
  291. if(count($songs)>0){
  292. $artist=OC_MEDIA_COLLECTION::getArtistName($songs[0]['song_artist']);
  293. }
  294. echo('<?xml version="1.0" encoding="UTF-8"?>');
  295. echo('<root>');
  296. foreach($songs as $song){
  297. self::printSong($song,$artist);
  298. }
  299. echo('</root>');
  300. }
  301. public static function songs($params){
  302. if(!self::checkAuth($params)){
  303. echo('<?xml version="1.0" encoding="UTF-8"?>');
  304. echo("<root>
  305. <error code='400'>Invalid login</error>
  306. </root>");
  307. return;
  308. }
  309. $filter=isset($params['filter'])?$params['filter']:'';
  310. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  311. $songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter,$exact);
  312. echo('<?xml version="1.0" encoding="UTF-8"?>');
  313. echo('<root>');
  314. foreach($songs as $song){
  315. self::printSong($song);
  316. }
  317. echo('</root>');
  318. }
  319. public static function song($params){
  320. if(!self::checkAuth($params)){
  321. echo('<?xml version="1.0" encoding="UTF-8"?>');
  322. echo("<root>
  323. <error code='400'>Invalid login</error>
  324. </root>");
  325. return;
  326. }
  327. if($song=OC_MEDIA_COLLECTION::getSong($params['filter'])){
  328. echo('<?xml version="1.0" encoding="UTF-8"?>');
  329. echo('<root>');
  330. self::printSong($song);
  331. echo('</root>');
  332. }
  333. }
  334. public static function play($params){
  335. $username=!self::checkAuth($params);
  336. if($username){
  337. echo('<?xml version="1.0" encoding="UTF-8"?>');
  338. echo("<root>
  339. <error code='400'>Invalid login</error>
  340. </root>");
  341. return;
  342. }
  343. if($song=OC_MEDIA_COLLECTION::getSong($params['song'])){
  344. OC_Util::setupFS($song["song_user"]);
  345. header('Content-type: '.OC_Filesystem::getMimeType($song['song_path']));
  346. header('Content-Length: '.$song['song_size']);
  347. OC_Filesystem::readfile($song['song_path']);
  348. }
  349. }
  350. public static function url_to_song($params){
  351. if(!self::checkAuth($params)){
  352. echo('<?xml version="1.0" encoding="UTF-8"?>');
  353. echo("<root>
  354. <error code='400'>Invalid login</error>
  355. </root>");
  356. return;
  357. }
  358. $url=$params['url'];
  359. $songId=substr($url,strrpos($url,'song=')+5);
  360. if($song=OC_MEDIA_COLLECTION::getSong($songId)){
  361. echo('<?xml version="1.0" encoding="UTF-8"?>');
  362. echo('<root>');
  363. self::printSong($song);
  364. echo('</root>');
  365. }
  366. }
  367. public static function search_songs($params){
  368. if(!self::checkAuth($params)){
  369. echo('<?xml version="1.0" encoding="UTF-8"?>');
  370. echo("<root>
  371. <error code='400'>Invalid login</error>
  372. </root>");
  373. return;
  374. }
  375. $filter=$params['filter'];
  376. $artists=OC_MEDIA_COLLECTION::getArtists($filter);
  377. $albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter);
  378. $songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter);
  379. foreach($artists as $artist){
  380. $songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
  381. }
  382. foreach($albums as $album){
  383. $songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
  384. }
  385. echo('<?xml version="1.0" encoding="UTF-8"?>');
  386. echo('<root>');
  387. foreach($songs as $song){
  388. self::printSong($song);
  389. }
  390. echo('</root>');
  391. }
  392. }
  393. ?>