lib_ampache.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. OC_User::setUserId($users[0]['user_id']);
  140. return $users[0]['user_id'];
  141. }else{
  142. return false;
  143. }
  144. }
  145. public static function updateAuth($auth){
  146. $query=OCP\DB::prepare("UPDATE `*PREFIX*media_sessions` SET `start`=CURRENT_TIMESTAMP WHERE `token`=?");
  147. $query->execute(array($auth));
  148. }
  149. private static function printArtist($artist){
  150. $albums=count(OC_MEDIA_COLLECTION::getAlbums($artist['artist_id']));
  151. $songs=count(OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
  152. $id=$artist['artist_id'];
  153. $name=self::fixXmlString($artist['artist_name']);
  154. echo("\t<artist id='$id'>\n");
  155. echo("\t\t<name>$name</name>\n");
  156. echo("\t\t<albums>$albums</albums>\n");
  157. echo("\t\t<songs>$songs</songs>\n");
  158. echo("\t\t<rating>0</rating>\n");
  159. echo("\t\t<preciserating>0</preciserating>\n");
  160. echo("\t</artist>\n");
  161. }
  162. private static function printAlbum($album,$artistName=false){
  163. if(!$artistName){
  164. $artistName=OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
  165. }
  166. $artistName=self::fixXmlString($artistName);
  167. $songs=count(OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
  168. $id=$album['album_id'];
  169. $name=self::fixXmlString($album['album_name']);
  170. $artist=$album['album_artist'];
  171. echo("\t<album id='$id'>\n");
  172. echo("\t\t<name>$name</name>\n");
  173. echo("\t\t<artist id='$artist'>$artistName</artist>\n");
  174. echo("\t\t<tracks>$songs</tracks>\n");
  175. echo("\t\t<rating>0</rating>\n");
  176. echo("\t\t<year>0</year>\n"); /* make Viridian happy */
  177. echo("\t\t<disk>1</disk>\n"); /* make Viridian happy */
  178. echo("\t\t<art> </art>\n"); /* single space to make quickplay happy enough */
  179. echo("\t\t<preciserating>0</preciserating>\n");
  180. echo("\t</album>\n");
  181. }
  182. private static function printSong($song,$artistName=false,$albumName=false){
  183. if(!$artistName){
  184. $artistName=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
  185. }
  186. if(!$albumName){
  187. $albumName=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
  188. }
  189. $artistName=self::fixXmlString($artistName);
  190. $albumName=self::fixXmlString($albumName);
  191. $id=$song['song_id'];
  192. $name=self::fixXmlString($song['song_name']);
  193. $artist=$song['song_artist'];
  194. $album=$song['song_album'];
  195. echo("\t<song id='$id'>\n");
  196. echo("\t\t<title>$name</title>\n");
  197. echo("\t\t<artist id='$artist'>$artistName</artist>\n");
  198. echo("\t\t<album id='$album'>$albumName</album>\n");
  199. $url=OCP\Util::linkToRemote('ampache')."server/xml.server.php/?action=play&song=$id&auth={$_GET['auth']}";
  200. $url=self::fixXmlString($url);
  201. echo("\t\t<url>$url</url>\n");
  202. echo("\t\t<time>{$song['song_length']}</time>\n");
  203. echo("\t\t<track>{$song['song_track']}</track>\n");
  204. echo("\t\t<size>{$song['song_size']}</size>\n");
  205. echo("\t\t<art> </art>\n"); /* single space to make Viridian happy enough */
  206. echo("\t\t<rating>0</rating>\n");
  207. echo("\t\t<preciserating>0</preciserating>\n");
  208. echo("\t</song>\n");
  209. }
  210. public static function artists($params){
  211. if(!self::checkAuth($params)){
  212. echo('<?xml version="1.0" encoding="UTF-8"?>');
  213. echo("<root>
  214. <error code='400'>Invalid login</error>
  215. </root>");
  216. return;
  217. }
  218. $filter=isset($params['filter'])?$params['filter']:'';
  219. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  220. $artists=OC_MEDIA_COLLECTION::getArtists($filter,$exact);
  221. echo('<?xml version="1.0" encoding="UTF-8"?>');
  222. echo('<root>');
  223. foreach($artists as $artist){
  224. self::printArtist($artist);
  225. }
  226. echo('</root>');
  227. }
  228. public static function artist_songs($params){
  229. if(!self::checkAuth($params)){
  230. echo('<?xml version="1.0" encoding="UTF-8"?>');
  231. echo("<root>
  232. <error code='400'>Invalid login</error>
  233. </root>");
  234. return;
  235. }
  236. $filter=isset($params['filter'])?$params['filter']:'';
  237. $songs=OC_MEDIA_COLLECTION::getSongs($filter);
  238. $artist=OC_MEDIA_COLLECTION::getArtistName($filter);
  239. echo('<?xml version="1.0" encoding="UTF-8"?>');
  240. echo('<root>');
  241. foreach($songs as $song){
  242. self::printSong($song,$artist);
  243. }
  244. echo('</root>');
  245. }
  246. public static function artist_albums($params){
  247. if(!self::checkAuth($params)){
  248. echo('<?xml version="1.0" encoding="UTF-8"?>');
  249. echo("<root>
  250. <error code='400'>Invalid login</error>
  251. </root>");
  252. return;
  253. }
  254. global $SITEROOT;
  255. $filter = isset($params['filter']) ? $params['filter'] : '';
  256. $albums=OC_MEDIA_COLLECTION::getAlbums($filter);
  257. $artist=OC_MEDIA_COLLECTION::getArtistName($filter);
  258. echo('<?xml version="1.0" encoding="UTF-8"?>');
  259. echo('<root>');
  260. foreach($albums as $album){
  261. self::printAlbum($album,$artist);
  262. }
  263. echo('</root>');
  264. }
  265. public static function albums($params){
  266. if(!self::checkAuth($params)){
  267. echo('<?xml version="1.0" encoding="UTF-8"?>');
  268. echo("<root>
  269. <error code='400'>Invalid login</error>
  270. </root>");
  271. return;
  272. }
  273. $filter=isset($params['filter'])?$params['filter']:'';
  274. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  275. $albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter,$exact);
  276. echo('<?xml version="1.0" encoding="UTF-8"?>');
  277. echo('<root>');
  278. foreach($albums as $album){
  279. self::printAlbum($album,false);
  280. }
  281. echo('</root>');
  282. }
  283. public static function album_songs($params){
  284. if(!self::checkAuth($params)){
  285. echo('<?xml version="1.0" encoding="UTF-8"?>');
  286. echo("<root>
  287. <error code='400'>Invalid login</error>
  288. </root>");
  289. return;
  290. }
  291. $songs=OC_MEDIA_COLLECTION::getSongs(0,$params['filter']);
  292. if(count($songs)>0){
  293. $artist=OC_MEDIA_COLLECTION::getArtistName($songs[0]['song_artist']);
  294. }
  295. echo('<?xml version="1.0" encoding="UTF-8"?>');
  296. echo('<root>');
  297. foreach($songs as $song){
  298. self::printSong($song,$artist);
  299. }
  300. echo('</root>');
  301. }
  302. public static function songs($params){
  303. if(!self::checkAuth($params)){
  304. echo('<?xml version="1.0" encoding="UTF-8"?>');
  305. echo("<root>
  306. <error code='400'>Invalid login</error>
  307. </root>");
  308. return;
  309. }
  310. $filter=isset($params['filter'])?$params['filter']:'';
  311. $exact=isset($params['exact'])?($params['exact']=='true'):false;
  312. $songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter,$exact);
  313. echo('<?xml version="1.0" encoding="UTF-8"?>');
  314. echo('<root>');
  315. foreach($songs as $song){
  316. self::printSong($song);
  317. }
  318. echo('</root>');
  319. }
  320. public static function song($params){
  321. if(!self::checkAuth($params)){
  322. echo('<?xml version="1.0" encoding="UTF-8"?>');
  323. echo("<root>
  324. <error code='400'>Invalid login</error>
  325. </root>");
  326. return;
  327. }
  328. if($song=OC_MEDIA_COLLECTION::getSong($params['filter'])){
  329. echo('<?xml version="1.0" encoding="UTF-8"?>');
  330. echo('<root>');
  331. self::printSong($song);
  332. echo('</root>');
  333. }
  334. }
  335. public static function play($params){
  336. $username=!self::checkAuth($params);
  337. if($username){
  338. echo('<?xml version="1.0" encoding="UTF-8"?>');
  339. echo("<root>
  340. <error code='400'>Invalid login</error>
  341. </root>");
  342. return;
  343. }
  344. if($song=OC_MEDIA_COLLECTION::getSong($params['song'])){
  345. OC_Util::setupFS($song["song_user"]);
  346. header('Content-type: '.OC_Filesystem::getMimeType($song['song_path']));
  347. header('Content-Length: '.$song['song_size']);
  348. OC_Filesystem::readfile($song['song_path']);
  349. }
  350. }
  351. public static function url_to_song($params){
  352. if(!self::checkAuth($params)){
  353. echo('<?xml version="1.0" encoding="UTF-8"?>');
  354. echo("<root>
  355. <error code='400'>Invalid login</error>
  356. </root>");
  357. return;
  358. }
  359. $url=$params['url'];
  360. $songId=substr($url,strrpos($url,'song=')+5);
  361. if($song=OC_MEDIA_COLLECTION::getSong($songId)){
  362. echo('<?xml version="1.0" encoding="UTF-8"?>');
  363. echo('<root>');
  364. self::printSong($song);
  365. echo('</root>');
  366. }
  367. }
  368. public static function search_songs($params){
  369. if(!self::checkAuth($params)){
  370. echo('<?xml version="1.0" encoding="UTF-8"?>');
  371. echo("<root>
  372. <error code='400'>Invalid login</error>
  373. </root>");
  374. return;
  375. }
  376. $filter = isset($params['filter']) ? $params['filter'] : '';
  377. $artists=OC_MEDIA_COLLECTION::getArtists($filter);
  378. $albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter);
  379. $songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter);
  380. foreach($artists as $artist){
  381. $songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
  382. }
  383. foreach($albums as $album){
  384. $songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
  385. }
  386. echo('<?xml version="1.0" encoding="UTF-8"?>');
  387. echo('<root>');
  388. foreach($songs as $song){
  389. self::printSong($song);
  390. }
  391. echo('</root>');
  392. }
  393. }
  394. ?>