lib_media.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 diconnectstributed 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. //we need to have the sha256 hash of passwords for ampache
  23. OCP\Util::connectHook('OC_User','post_login','OC_MEDIA','loginListener');
  24. //connect to the filesystem for auto updating
  25. OCP\Util::connectHook('OC_Filesystem','post_write','OC_MEDIA','updateFile');
  26. //listen for file deletions to clean the database if a song is deleted
  27. OCP\Util::connectHook('OC_Filesystem','post_delete','OC_MEDIA','deleteFile');
  28. //list for file moves to update the database
  29. OCP\Util::connectHook('OC_Filesystem','post_rename','OC_MEDIA','moveFile');
  30. class OC_MEDIA{
  31. /**
  32. * get the sha256 hash of the password needed for ampache
  33. * @param array $params, parameters passed from OC_Hook
  34. */
  35. public static function loginListener($params){
  36. if(isset($_POST['user']) and $_POST['password']){
  37. $name=$_POST['user'];
  38. $query=OCP\DB::prepare("SELECT user_id from *PREFIX*media_users WHERE user_id LIKE ?");
  39. $uid=$query->execute(array($name))->fetchAll();
  40. if(count($uid)==0){
  41. $password=hash('sha256',$_POST['password']);
  42. $query=OCP\DB::prepare("INSERT INTO *PREFIX*media_users (user_id, user_password_sha256) VALUES (?, ?);");
  43. $query->execute(array($name,$password));
  44. }
  45. }
  46. }
  47. /**
  48. *
  49. */
  50. public static function updateFile($params){
  51. $path=$params['path'];
  52. if(!$path) return;
  53. require_once 'lib_scanner.php';
  54. require_once 'lib_collection.php';
  55. //fix a bug where there were multiply '/' in front of the path, it should only be one
  56. while($path[0]=='/'){
  57. $path=substr($path,1);
  58. }
  59. $path='/'.$path;
  60. OC_MEDIA_SCANNER::scanFile($path);
  61. }
  62. /**
  63. *
  64. */
  65. public static function deleteFile($params){
  66. $path=$params['path'];
  67. require_once 'lib_collection.php';
  68. OC_MEDIA_COLLECTION::deleteSongByPath($path);
  69. }
  70. public static function moveFile($params){
  71. require_once 'lib_collection.php';
  72. OC_MEDIA_COLLECTION::moveSong($params['oldpath'],$params['newpath']);
  73. }
  74. }
  75. class OC_MediaSearchProvider extends OC_Search_Provider{
  76. function search($query){
  77. require_once('lib_collection.php');
  78. $artists=OC_MEDIA_COLLECTION::getArtists($query);
  79. $albums=OC_MEDIA_COLLECTION::getAlbums(0,$query);
  80. $songs=OC_MEDIA_COLLECTION::getSongs(0,0,$query);
  81. $results=array();
  82. foreach($artists as $artist){
  83. $results[]=new OC_Search_Result($artist['artist_name'],'',OCP\Util::linkTo( 'media', 'index.php').'#artist='.urlencode($artist['artist_name']),'Music');
  84. }
  85. foreach($albums as $album){
  86. $artist=OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
  87. $results[]=new OC_Search_Result($album['album_name'],'by '.$artist,OCP\Util::linkTo( 'media', 'index.php').'#artist='.urlencode($artist).'&album='.urlencode($album['album_name']),'Music');
  88. }
  89. foreach($songs as $song){
  90. $minutes=floor($song['song_length']/60);
  91. $secconds=$song['song_length']%60;
  92. $artist=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
  93. $album=OC_MEDIA_COLLECTION::getalbumName($song['song_album']);
  94. $results[]=new OC_Search_Result($song['song_name'],"by $artist, in $album $minutes:$secconds",OCP\Util::linkTo( 'media', 'index.php').'#artist='.urlencode($artist).'&album='.urlencode($album).'&song='.urlencode($song['song_name']),'Music');
  95. }
  96. return $results;
  97. }
  98. }