api.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. header('Content-type: text/html; charset=UTF-8') ;
  23. OCP\JSON::checkAppEnabled('media');
  24. error_reporting(E_ALL); //no script error reporting because of getID3
  25. $arguments=$_POST;
  26. if(!isset($_POST['action']) and isset($_GET['action'])){
  27. $arguments=$_GET;
  28. }
  29. foreach($arguments as &$argument){
  30. $argument=stripslashes($argument);
  31. }
  32. @ob_clean();
  33. if(!isset($arguments['artist'])){
  34. $arguments['artist']=0;
  35. }
  36. if(!isset($arguments['album'])){
  37. $arguments['album']=0;
  38. }
  39. if(!isset($arguments['search'])){
  40. $arguments['search']='';
  41. }
  42. session_write_close();
  43. OC_MEDIA_COLLECTION::$uid=OCP\USER::getUser();
  44. if($arguments['action']){
  45. switch($arguments['action']){
  46. case 'delete':
  47. $path=$arguments['path'];
  48. OC_MEDIA_COLLECTION::deleteSongByPath($path);
  49. $paths=explode(PATH_SEPARATOR,OCP\Config::getUserValue(OCP\USER::getUser(),'media','paths',''));
  50. if(array_search($path,$paths)!==false){
  51. unset($paths[array_search($path,$paths)]);
  52. OCP\Config::setUserValue(OCP\USER::getUser(),'media','paths',implode(PATH_SEPARATOR,$paths));
  53. }
  54. case 'get_collection':
  55. $data=array();
  56. $data['artists']=OC_MEDIA_COLLECTION::getArtists();
  57. $data['albums']=OC_MEDIA_COLLECTION::getAlbums();
  58. $data['songs']=OC_MEDIA_COLLECTION::getSongs();
  59. OCP\JSON::encodedPrint($data);
  60. break;
  61. case 'scan':
  62. OCP\DB::beginTransaction();
  63. set_time_limit(0); //recursive scan can take a while
  64. $eventSource=new OC_EventSource();
  65. OC_MEDIA_SCANNER::scanCollection($eventSource);
  66. $eventSource->close();
  67. OCP\DB::commit();
  68. break;
  69. case 'scanFile':
  70. echo (OC_MEDIA_SCANNER::scanFile($arguments['path']))?'true':'false';
  71. break;
  72. case 'get_artists':
  73. OCP\JSON::encodedPrint(OC_MEDIA_COLLECTION::getArtists($arguments['search']));
  74. break;
  75. case 'get_albums':
  76. OCP\JSON::encodedPrint(OC_MEDIA_COLLECTION::getAlbums($arguments['artist'],$arguments['search']));
  77. break;
  78. case 'get_songs':
  79. OCP\JSON::encodedPrint(OC_MEDIA_COLLECTION::getSongs($arguments['artist'],$arguments['album'],$arguments['search']));
  80. break;
  81. case 'get_path_info':
  82. if(OC_Filesystem::file_exists($arguments['path'])){
  83. $songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
  84. if($songId==0){
  85. unset($_SESSION['collection']);
  86. $songId= OC_MEDIA_SCANNER::scanFile($arguments['path']);
  87. }
  88. if($songId>0){
  89. $song=OC_MEDIA_COLLECTION::getSong($songId);
  90. $song['artist']=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
  91. $song['album']=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
  92. OCP\JSON::encodedPrint($song);
  93. }
  94. }
  95. break;
  96. case 'play':
  97. @ob_end_clean();
  98. $ftype=OC_Filesystem::getMimeType( $arguments['path'] );
  99. if(substr($ftype,0,5)!='audio' and $ftype!='application/ogg'){
  100. echo 'Not an audio file';
  101. exit();
  102. }
  103. $songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
  104. OC_MEDIA_COLLECTION::registerPlay($songId);
  105. header('Content-Type:'.$ftype);
  106. OCP\Response::enableCaching(3600 * 24); // 24 hour
  107. header('Accept-Ranges: bytes');
  108. header('Content-Length: '.OC_Filesystem::filesize($arguments['path']));
  109. $mtime = OC_Filesystem::filemtime($arguments['path']);
  110. OCP\Response::setLastModifiedHeader($mtime);
  111. OC_Filesystem::readfile($arguments['path']);
  112. exit;
  113. case 'find_music':
  114. $music=OC_FileCache::searchByMime('audio');
  115. $ogg=OC_FileCache::searchByMime('application','ogg');
  116. $music=array_merge($music,$ogg);
  117. OCP\JSON::encodedPrint($music);
  118. exit;
  119. }
  120. }
  121. ?>