2011-07-22 04:30:52 +00:00
< ? php
/**
* ownCloud - media plugin
*
* @ author Robin Appelman
* @ copyright 2010 Robin Appelman icewind1991 @ gmail . com
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation ; either
* version 3 of the License , or any later version .
*
* This library is diconnectstributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see < http :// www . gnu . org / licenses />.
*
*/
//we need to have the sha256 hash of passwords for ampache
2012-05-05 08:18:45 +00:00
OCP\Util :: connectHook ( 'OC_User' , 'post_login' , 'OC_MEDIA' , 'loginListener' );
2011-07-22 04:30:52 +00:00
2011-08-26 13:17:39 +00:00
//connect to the filesystem for auto updating
2012-05-05 08:18:45 +00:00
OCP\Util :: connectHook ( 'OC_Filesystem' , 'post_write' , 'OC_MEDIA' , 'updateFile' );
2011-07-22 04:30:52 +00:00
//listen for file deletions to clean the database if a song is deleted
2012-05-05 08:18:45 +00:00
OCP\Util :: connectHook ( 'OC_Filesystem' , 'post_delete' , 'OC_MEDIA' , 'deleteFile' );
2011-07-22 04:30:52 +00:00
2011-09-24 23:34:19 +00:00
//list for file moves to update the database
2012-05-05 08:18:45 +00:00
OCP\Util :: connectHook ( 'OC_Filesystem' , 'post_rename' , 'OC_MEDIA' , 'moveFile' );
2011-09-24 23:34:19 +00:00
2011-07-22 04:30:52 +00:00
class OC_MEDIA {
/**
* get the sha256 hash of the password needed for ampache
2011-07-29 19:36:03 +00:00
* @ param array $params , parameters passed from OC_Hook
2011-07-22 04:30:52 +00:00
*/
public static function loginListener ( $params ){
if ( isset ( $_POST [ 'user' ]) and $_POST [ 'password' ]){
$name = $_POST [ 'user' ];
2012-05-03 11:06:08 +00:00
$query = OCP\DB :: prepare ( " SELECT user_id from *PREFIX*media_users WHERE user_id LIKE ? " );
2011-07-22 04:30:52 +00:00
$uid = $query -> execute ( array ( $name )) -> fetchAll ();
if ( count ( $uid ) == 0 ){
$password = hash ( 'sha256' , $_POST [ 'password' ]);
2012-05-03 11:06:08 +00:00
$query = OCP\DB :: prepare ( " INSERT INTO *PREFIX*media_users (user_id, user_password_sha256) VALUES (?, ?); " );
2011-07-22 04:30:52 +00:00
$query -> execute ( array ( $name , $password ));
}
}
}
/**
*
*/
public static function updateFile ( $params ){
$path = $params [ 'path' ];
2012-02-21 19:48:14 +00:00
if ( ! $path ) return ;
2011-08-26 13:17:39 +00:00
require_once 'lib_scanner.php' ;
require_once 'lib_collection.php' ;
//fix a bug where there were multiply '/' in front of the path, it should only be one
while ( $path [ 0 ] == '/' ){
$path = substr ( $path , 1 );
2011-07-22 04:30:52 +00:00
}
2011-08-26 13:17:39 +00:00
$path = '/' . $path ;
OC_MEDIA_SCANNER :: scanFile ( $path );
2011-07-22 04:30:52 +00:00
}
/**
*
*/
public static function deleteFile ( $params ){
$path = $params [ 'path' ];
require_once 'lib_collection.php' ;
OC_MEDIA_COLLECTION :: deleteSongByPath ( $path );
}
2011-09-24 23:34:19 +00:00
public static function moveFile ( $params ){
require_once 'lib_collection.php' ;
OC_MEDIA_COLLECTION :: moveSong ( $params [ 'oldpath' ], $params [ 'newpath' ]);
}
2011-07-22 04:30:52 +00:00
}
2012-04-14 09:29:44 +00:00
class OC_MediaSearchProvider extends OC_Search_Provider {
function search ( $query ){
2011-07-22 04:30:52 +00:00
require_once ( 'lib_collection.php' );
$artists = OC_MEDIA_COLLECTION :: getArtists ( $query );
$albums = OC_MEDIA_COLLECTION :: getAlbums ( 0 , $query );
$songs = OC_MEDIA_COLLECTION :: getSongs ( 0 , 0 , $query );
$results = array ();
foreach ( $artists as $artist ){
2012-05-01 21:19:39 +00:00
$results [] = new OC_Search_Result ( $artist [ 'artist_name' ], '' , OCP\Util :: linkTo ( 'media' , 'index.php' ) . '#artist=' . urlencode ( $artist [ 'artist_name' ]), 'Music' );
2011-07-22 04:30:52 +00:00
}
foreach ( $albums as $album ){
2011-07-30 22:50:04 +00:00
$artist = OC_MEDIA_COLLECTION :: getArtistName ( $album [ 'album_artist' ]);
2012-05-01 21:19:39 +00:00
$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' );
2011-07-22 04:30:52 +00:00
}
foreach ( $songs as $song ){
$minutes = floor ( $song [ 'song_length' ] / 60 );
$secconds = $song [ 'song_length' ] % 60 ;
2011-07-30 22:50:04 +00:00
$artist = OC_MEDIA_COLLECTION :: getArtistName ( $song [ 'song_artist' ]);
$album = OC_MEDIA_COLLECTION :: getalbumName ( $song [ 'song_album' ]);
2012-05-01 21:19:39 +00:00
$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' );
2011-07-22 04:30:52 +00:00
}
return $results ;
}
}