.
*
*/
//implementation of ampache's xml api
class OC_MEDIA_AMPACHE{
/**
* do the initial handshake
* @param array params
*/
public static function handshake($params){
$auth=(isset($params['auth']))?$params['auth']:false;
$user=(isset($params['user']))?$params['user']:false;
$time=(isset($params['timestamp']))?$params['timestamp']:false;
$now=time();
if($now-$time>(10*60)){
echo("
timestamp is more then 10 minutes old
");
}
if($auth and $user and $time){
$query=OC_DB::prepare("SELECT user_id, user_password_sha256 from *PREFIX*media_users WHERE user_id=?");
$users=$query->execute(array($user))->fetchAll();
if(count($users)>0){
$pass=$users[0]['user_password_sha256'];
$key=hash('sha256',$time.$pass);
if($key==$auth){
$token=hash('sha256','oc_media_'.$key);
OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
$date=date('c');//todo proper update/add/clean dates
$songs=OC_MEDIA_COLLECTION::getSongCount();
$artists=OC_MEDIA_COLLECTION::getArtistCount();
$albums=OC_MEDIA_COLLECTION::getAlbumCount();
$query=OC_DB::prepare("INSERT INTO *PREFIX*media_sessions (`session_id`, `token`, `user_id`, `start`) VALUES (NULL, ?, ?, now());");
$query->execute(array($token,$user));
$expire=date('c',time()+600);
echo("
$token
350001
$date
$date
$date
$songs
$artists
$albums\
600
$expire
0
0
");
return;
}
}
echo("
Invalid login
");
}else{
echo("
Missing arguments
");
}
}
public static function ping($params){
if(isset($params['auth'])){
if(self::checkAuth($params['auth'])){
self::updateAuth($params['auth']);
}else{
echo("
Invalid login
");
return;
}
}
echo('');
echo('350001');
echo('');
}
public static function checkAuth($auth){
if(is_array($auth)){
if(isset($auth['auth'])){
$auth=$auth['auth'];
}else{
return false;
}
}
//remove old sessions
$query=OC_DB::prepare("DELETE from *PREFIX*media_sessions WHERE start<(NOW()-600)");
$query->execute();
$query=OC_DB::prepare("SELECT user_id from *PREFIX*media_sessions WHERE token=?");
$users=$query->execute(array($auth))->fetchAll();
if(count($users)>0){
OC_MEDIA_COLLECTION::$uid=$users[0]['user_id'];
return $users[0]['user_id'];
}else{
return false;
}
}
public static function updateAuth($auth){
$query=OC_DB::prepare("UPDATE *PREFIX*media_sessions SET start=CURRENT_TIMESTAMP WHERE token=?");
$query->execute(array($auth));
}
private static function printArtist($artist){
$albums=count(OC_MEDIA_COLLECTION::getAlbums($artist['artist_id']));
$songs=count(OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
$id=$artist['artist_id'];
$name=utf8_decode(htmlentities($artist['artist_name']));
echo("\t\n");
echo("\t\t$name\n");
echo("\t\t$albums\n");
echo("\t\t$songs\n");
echo("\t\t0\n");
echo("\t\t0\n");
echo("\t\n");
}
private static function printAlbum($album,$artistName=false){
if(!$artistName){
$artistName=OC_MEDIA_COLLECTION::getArtistName($album['album_artist']);
}
$artistName=utf8_decode(htmlentities($artistName));
$songs=count(OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
$id=$album['album_id'];
$name=utf8_decode(htmlentities($album['album_name']));
$artist=$album['album_artist'];
echo("\t\n");
echo("\t\t$name\n");
echo("\t\t$artistName\n");
echo("\t\t$songs\n");
echo("\t\t0\n");
echo("\t\t0\n");
echo("\t\n");
}
private static function printSong($song,$artistName=false,$albumName=false){
if(!$artistName){
$artistName=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
}
if(!$albumName){
$albumName=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
}
$artistName=utf8_decode(htmlentities($artistName));
$albumName=utf8_decode(htmlentities($albumName));
$id=$song['song_id'];
$name=utf8_decode(htmlentities($song['song_name']));
$artist=$song['song_artist'];
$album=$song['song_album'];
echo("\t\n");
echo("\t\t$name\n");
echo("\t\t$artistName\n");
echo("\t\t$albumName\n");
$url=OC_Helper::linkTo('media', 'server/xml.server.php', null, true)."?action=play&song=$id&auth={$_GET['auth']}";
$url=htmlentities($url);
echo("\t\t$url\n");
echo("\t\t\n");
echo("\t\t\n");
echo("\t\t{$song['song_size']}\n");
echo("\t\t\n");
echo("\t\t0\n");
echo("\t\t0\n");
echo("\t\n");
}
public static function artists($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$filter=isset($params['filter'])?$params['filter']:'';
$exact=isset($params['exact'])?($params['exact']=='true'):false;
$artists=OC_MEDIA_COLLECTION::getArtists($filter,$exact);
echo('');
foreach($artists as $artist){
self::printArtist($artist);
}
echo('');
}
public static function artist_songs($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$filter=isset($params['filter'])?$params['filter']:'';
$songs=OC_MEDIA_COLLECTION::getSongs($filter);
$artist=OC_MEDIA_COLLECTION::getArtistName($filter);
echo('');
foreach($songs as $song){
self::printSong($song,$artist);
}
echo('');
}
public static function artist_albums($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
global $SITEROOT;
$filter=$params['filter'];
$albums=OC_MEDIA_COLLECTION::getAlbums($filter);
$artist=OC_MEDIA_COLLECTION::getArtistName($filter);
echo('');
foreach($albums as $album){
self::printAlbum($album,$artist);
}
echo('');
}
public static function albums($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$filter=isset($params['filter'])?$params['filter']:'';
$exact=isset($params['exact'])?($params['exact']=='true'):false;
$albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter,$exact);
echo('');
foreach($albums as $album){
self::printAlbum($album,$artist);
}
echo('');
}
public static function album_songs($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$songs=OC_MEDIA_COLLECTION::getSongs(0,$params['filter']);
if(count($songs)>0){
$artist=OC_MEDIA_COLLECTION::getArtistName($songs[0]['song_artist']);
}
echo('');
foreach($songs as $song){
self::printSong($song,$artist);
}
echo('');
}
public static function songs($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$filter=isset($params['filter'])?$params['filter']:'';
$exact=isset($params['exact'])?($params['exact']=='true'):false;
$songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter,$exact);
echo('');
foreach($songs as $song){
self::printSong($song);
}
echo('');
}
public static function song($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
if($song=OC_MEDIA_COLLECTION::getSong($params['filter'])){
echo('');
self::printSong($song);
echo('');
}
}
public static function play($params){
$username=!self::checkAuth($params);
if($username){
echo("
Invalid login
");
return;
}
if($song=OC_MEDIA_COLLECTION::getSong($params['song'])){
OC_Util::setupFS($song["song_user"]);
header('Content-type: '.OC_Filesystem::getMimeType($song['song_path']));
header('Content-Length: '.$song['song_size']);
OC_Filesystem::readfile($song['song_path']);
}
}
public static function url_to_song($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$url=$params['url'];
$songId=substr($url,strrpos($url,'song=')+5);
if($song=OC_MEDIA_COLLECTION::getSong($songId)){
echo('');
self::printSong($song);
echo('');
}
}
public static function search_songs($params){
if(!self::checkAuth($params)){
echo("
Invalid login
");
return;
}
$filter=$params['filter'];
$artists=OC_MEDIA_COLLECTION::getArtists($filter);
$albums=OC_MEDIA_COLLECTION::getAlbums(0,$filter);
$songs=OC_MEDIA_COLLECTION::getSongs(0,0,$filter);
foreach($artists as $artist){
$songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($artist['artist_id']));
}
foreach($albums as $album){
$songs=array_merge($songs,OC_MEDIA_COLLECTION::getSongs($album['album_artist'],$album['album_id']));
}
echo('');
foreach($songs as $song){
self::printSong($song);
}
echo('');
}
}
?>