api.php 4.2 KB

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