1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-03 09:49:30 +02:00

api fixes, added librefm scrobbler (untested) and fixed minor error in header file with rtl vs ltr languages also removed dead RioPlayer plugin

This commit is contained in:
Karl 'vollmerk' Vollmer 2009-05-18 17:34:22 +00:00
parent 5bd82180f5
commit d84e62dba7
10 changed files with 286 additions and 106 deletions

View file

@ -30,12 +30,13 @@ class scrobbler {
public $submit_url; public $submit_url;
public $queued_tracks; public $queued_tracks;
public $reset_handshake = false; public $reset_handshake = false;
public $scrobble_host = 'post.audioscrobbler.com';
/** /**
* Constructor * Constructor
* This is the constructer it takes a username and password * This is the constructer it takes a username and password
*/ */
public function __construct($username, $password,$host='',$port='',$url='',$challenge='') { public function __construct($username, $password,$host='',$port='',$url='',$challenge='',$scrobble_host='') {
$this->error_msg = ''; $this->error_msg = '';
$this->username = trim($username); $this->username = trim($username);
@ -45,6 +46,7 @@ class scrobbler {
$this->submit_port = $port; $this->submit_port = $port;
$this->submit_url = $url; $this->submit_url = $url;
$this->queued_tracks = array(); $this->queued_tracks = array();
if ($scrobble_host) { $this->scrobble_host = $scrobble_host; }
} // scrobbler } // scrobbler
@ -73,7 +75,7 @@ class scrobbler {
*/ */
public function handshake() { public function handshake() {
$as_socket = fsockopen('post.audioscrobbler.com', 80, $errno, $errstr, 2); $as_socket = fsockopen($this->scrobble_host, 80, $errno, $errstr, 2);
if(!$as_socket) { if(!$as_socket) {
$this->error_msg = $errstr; $this->error_msg = $errstr;
return false; return false;
@ -86,7 +88,7 @@ class scrobbler {
$get_string = "GET /?hs=true&p=1.2&c=apa&v=0.1&u=$username&t=$timestamp&a=$auth_token HTTP/1.1\r\n"; $get_string = "GET /?hs=true&p=1.2&c=apa&v=0.1&u=$username&t=$timestamp&a=$auth_token HTTP/1.1\r\n";
fwrite($as_socket, $get_string); fwrite($as_socket, $get_string);
fwrite($as_socket, "Host: post.audioscrobbler.com\r\n"); fwrite($as_socket, "Host: $this->scrobble_host\r\n");
fwrite($as_socket, "Accept: */*\r\n\r\n"); fwrite($as_socket, "Accept: */*\r\n\r\n");
$buffer = ''; $buffer = '';
@ -202,7 +204,7 @@ class scrobbler {
fwrite($as_socket, $action); fwrite($as_socket, $action);
fwrite($as_socket, "Host: ".$this->submit_host."\r\n"); fwrite($as_socket, "Host: ".$this->submit_host."\r\n");
fwrite($as_socket, "Accept: */*\r\n"); fwrite($as_socket, "Accept: */*\r\n");
fwrite($as_socket, "User-Agent: Ampache/3.4\r\n"); fwrite($as_socket, "User-Agent: Ampache/3.6\r\n");
fwrite($as_socket, "Content-type: application/x-www-form-urlencoded\r\n"); fwrite($as_socket, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($as_socket, "Content-length: ".strlen($query_str)."\r\n\r\n"); fwrite($as_socket, "Content-length: ".strlen($query_str)."\r\n\r\n");

View file

@ -349,18 +349,20 @@ class Tag extends database_object {
/** /**
* get_object_tags * get_object_tags
* Display all tags that apply to maching target type of the specified id * Display all tags that apply to maching target type of the specified id
* UNUSED
*/ */
public static function get_object_tags($type, $id) { public static function get_object_tags($type, $id) {
if (!self::validate_type($type)) { return array(); } if (!self::validate_type($type)) { return array(); }
$id = Dba::escape($id);
$sql = "SELECT DISTINCT `tag_map`.`id`, `tag`.`name`, `tag_map`.`user` FROM `tag` " . $sql = "SELECT `tag_map`.`id`, `tag`.`name`, `tag_map`.`user` FROM `tag` " .
"LEFT JOIN `tag_map` ON `tag_map`.`id`=`tag`.`map_id` " . "LEFT JOIN `tag_map` ON `tag_map`.`tag_id`=`tag`.`id` " .
"LEFT JOIN `$type` ON `$type`.`id`=`tag_map`.`object_id` " . "WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id`='$id'";
"WHERE `tag_map`.`object_type`='$type'";
$results = array(); $results = array();
$db_results = Dba::query($sql); $db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) { while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row; $results[] = $row;
@ -370,6 +372,31 @@ class Tag extends database_object {
} // get_object_tags } // get_object_tags
/**
* get_tag_objects
* This gets the objects from a specified tag and returns an array of object ids, nothing more
*/
public static function get_tag_objects($type,$tag_id) {
if (!self::validate_type($type)) { return array(); }
$tag_id = Dba::escape($id);
$sql = "SELECT DISTINCT `tag_map`.`object_id` FROM `tag_map` " .
"WHERE `tag_map`.`tag_id`='$tag_id' AND `tag_map`.`object_type`='$type'";
$db_results = Dba::read($sql);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row['object_id'];
}
return $results;
} // get_tag_objects
/** /**
* get_tags * get_tags
* This is a non-object non type depedent function that just returns tags * This is a non-object non type depedent function that just returns tags

View file

@ -1762,7 +1762,23 @@ class Update {
*/ */
public static function update_360001() { public static function update_360001() {
// Remove any RIO related information from the database as the plugin has been removed
$sql = "DELETE FROM `update_info` WHERE `key` LIKE 'Plugin_Ri%'";
$db_results = Dba::write($sql);
$sql = "DELETE FROM `preference` WHERE `name` LIKE 'rio_%'";
$db_results = Dba::write($sql);
$sql = "SELECT `id` FROM `user`";
$db_results = Dba::query($sql);
User::fix_preferences('-1');
while ($r = Dba::fetch_assoc($db_results)) {
User::fix_preferences($r['id']);
} // while we're fixing the useres stuff
// self::set_version('db_version','360001');
} // update_360001 } // update_360001

View file

@ -561,6 +561,14 @@ class User extends database_object {
} }
} // end if is_installed } // end if is_installed
// Check and see if librefm is loaded and run scrobblizing
if (Plugin::is_installed('Libre.FM')) {
$librefm = new Plugin('Librefm');
if ($lastfm->_plugin->load($this->prefs,$this->id)) {
$lastfm->_plugin->submit($song_info,$this->id);
}
} // end if is_installed
// Do this last so the 'last played checks are correct' // Do this last so the 'last played checks are correct'
Stats::insert('song',$song_id,$user); Stats::insert('song',$song_id,$user);
Stats::insert('album',$song_info->album,$user); Stats::insert('album',$song_info->album,$user);

View file

@ -179,7 +179,6 @@ class xmlData {
$tags = array_splice($tags,self::$offset,self::$limit); $tags = array_splice($tags,self::$offset,self::$limit);
} }
$string = ''; $string = '';
foreach ($tags as $tag_id) { foreach ($tags as $tag_id) {
@ -190,12 +189,11 @@ class xmlData {
"\t<albums>" . intval($counts['album']) . "</albums>\n" . "\t<albums>" . intval($counts['album']) . "</albums>\n" .
"\t<artists>" . intval($counts['artist']) . "</artists>\n" . "\t<artists>" . intval($counts['artist']) . "</artists>\n" .
"\t<songs>" . intval($counts['songs']) . "</songs>\n" . "\t<songs>" . intval($counts['songs']) . "</songs>\n" .
"\t<videos>" . intval($counts['video']) . "</video>\n" . "\t<videos>" . intval($counts['video']) . "</videos>\n" .
"\t<playlists>" . intval($count['playlist']) . "</playlists>\n" . "\t<playlists>" . intval($count['playlist']) . "</playlists>\n" .
"\t<stream>" . intval($count['live_stream']) . "</stream>\n" . "\t<stream>" . intval($count['live_stream']) . "</stream>\n" .
"</tag>\n"; "</tag>\n";
} // end foreach } // end foreach
$final = self::_header() . $string . self::_footer(); $final = self::_header() . $string . self::_footer();

View file

@ -58,6 +58,7 @@ function update_preferences($pref_id=0) {
$value = Stream::validate_bitrate($value); $value = Stream::validate_bitrate($value);
break; break;
/* MD5 the LastFM & MyStrands so it's not plainTXT */ /* MD5 the LastFM & MyStrands so it's not plainTXT */
case 'librefm_pass':
case 'lastfm_pass': case 'lastfm_pass':
/* If it's our default blanking thing then don't use it */ /* If it's our default blanking thing then don't use it */
if ($value == '******') { unset($_REQUEST[$name]); break; } if ($value == '******') { unset($_REQUEST[$name]); break; }
@ -244,6 +245,7 @@ function create_preference_input($name,$value) {
echo "</select>\n"; echo "</select>\n";
break; break;
case 'lastfm_pass': case 'lastfm_pass':
case 'librefm_pass':
echo "<input type=\"password\" size=\"16\" name=\"$name\" value=\"******\" />"; echo "<input type=\"password\" size=\"16\" name=\"$name\" value=\"******\" />";
break; break;
case 'playlist_method': case 'playlist_method':

View file

@ -0,0 +1,217 @@
<?php
/*
Copyright (c) Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2
as published by the Free Software Foundation.
This program is distributed 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
class Ampachelibrefm {
public $name ='Libre.FM';
public $description ='Records your played songs to your Libre.FM Account';
public $url ='';
public $version ='000001';
public $min_ampache ='350001';
public $max_ampache ='360008';
// These are internal settings used by this class, run this->load to
// fill em out
private $username;
private $password;
private $hostname;
private $port;
private $path;
private $challenge;
/**
* Constructor
* This function does nothing...
*/
public function __construct() {
return true;
} // Pluginlibrefm
/**
* install
* This is a required plugin function it inserts the required preferences
* into Ampache
*/
public function install() {
// Check and see if it's already installed (they've just hit refresh, those dorks)
if (Preference::exists('librefm_user')) { return false; }
Preference::insert('librefm_user','Libre.FM Username','','25','string','plugins');
Preference::insert('librefm_pass','Libre.FM Password','','25','string','plugins');
Preference::insert('librefm_port','Libre.FM Submit Port','','25','string','internal');
Preference::insert('librefm_host','Libre.FM Submit Host','','25','string','internal');
Preference::insert('librefm_url','Libre.FM Submit URL','','25','string','internal');
Preference::insert('librefm_challenge','Libre.FM Submit Challenge','','25','string','internal');
return true;
} // install
/**
* uninstall
* This is a required plugin function it removes the required preferences from
* the database returning it to its origional form
*/
public function uninstall() {
Preference::delete('librefm_pass');
Preference::delete('librefm_user');
Preference::delete('librefm_url');
Preference::delete('librefm_host');
Preference::delete('librefm_port');
Preference::delete('librefm_challenge');
} // uninstall
/**
* submit
* This takes care of queueing and then submiting the tracks eventually this will make sure
* that you've haven't
*/
public function submit($song,$user_id) {
// Before we start let's pull the last song submited by this user
$previous = Stats::get_last_song($user_id);
$diff = time() - $previous['date'];
// Make sure it wasn't within the last min
if ($diff < 60) {
debug_event('librefm','Last song played within ' . $diff . ' seconds, not recording stats','3');
return false;
}
if ($song->time < 30) {
debug_event('librefm','Song less then 30 seconds not queueing','3');
return false;
}
// Make sure there's actually a username and password before we keep going
if (!$this->username || !$this->password) { return false; }
// Create our scrobbler with everything this time and then queue it
$scrobbler = new scrobbler($this->username,$this->password,$this->hostname,$this->port,$this->path,$this->challenge);
// Check to see if the scrobbling works
if (!$scrobbler->queue_track($song->f_artist_full,$song->f_album_full,$song->title,time(),$song->time,$song->track)) {
// Depending on the error we might need to do soemthing here
return false;
}
// Go ahead and submit it now
if (!$scrobbler->submit_tracks()) {
debug_event('librefm','Error Submit Failed: ' . $scrobbler->error_msg,'3');
if ($scrobbler->reset_handshake) {
debug_event('librefm','Re-running Handshake due to error','3');
$this->set_handshake($user_id);
// Try try again
if ($scrobbler->submit_tracks()) {
return true;
}
}
return false;
}
debug_event('librefm','Submission Successful','5');
return true;
} // submit
/**
* set_handshake
* This runs a handshake and properly updates the preferences as needed, it returns the data
* as an array so we don't have to requery the db. This requires a userid so it knows who's
* crap to update
*/
public function set_handshake($user_id) {
$scrobbler = new scrobbler($this->username,$this->password);
$data = $scrobbler->handshake();
if (!$data) {
debug_event('librefm','Handshake Failed: ' . $scrobbler->error_msg,'3');
return false;
}
$this->hostname = $data['submit_host'];
$this->port = $data['submit_port'];
$this->path = $data['submit_url'];
$this->challenge = $data['challenge'];
// Update the preferences
Preference::update('librefm_port',$user_id,$data['submit_port']);
Preference::update('librefm_host',$user_id,$data['submit_host']);
Preference::update('librefm_url',$user_id,$data['submit_url']);
Preference::update('librefm_challenge',$user_id,$data['challenge']);
return true;
} // set_handshake
/**
* load
* This loads up the data we need into this object, this stuff comes from the preferences
* it's passed as a key'd array
*/
public function load($data,$user_id) {
if (strlen(trim($data['librefm_user']))) {
$this->username = trim($data['librefm_user']);
}
else {
debug_event('librefm','No Username, not scrobbling','3');
return false;
}
if (strlen(trim($data['librefm_pass']))) {
$this->password = trim($data['librefm_pass']);
}
else {
debug_event('librefm','No Password, not scrobbling','3');
return false;
}
// If we don't have the other stuff try to get it before giving up
if (!$data['librefm_host'] || !$data['librefm_port'] || !$data['librefm_url'] || !$data['librefm_challenge']) {
debug_event('librefm','Running Handshake, missing information','3');
if (!$this->set_handshake($user_id)) {
debug_event('librefm','Handshake failed, you lose','3');
return false;
}
}
else {
$this->hostname = $data['librefm_host'];
$this->port = $data['librefm_port'];
$this->path = $data['librefm_url'];
$this->challenge = $data['librefm_challenge'];
}
return true;
} // load
} // end Ampachelibrefm
?>

View file

@ -1,87 +0,0 @@
<?php
/*
Copyright (c) 2001 - 2006 Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2
as published by the Free Software Foundation.
This program is distributed 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
class AmpacheRioPlayer {
var $name ='Rio Player';
var $description ='Sets up ampache so a Rio Player can access it';
var $url ='';
var $version ='000001';
var $min_ampache ='333001';
var $max_ampache ='333005';
/**
* Constructor
* This function does nothing...
*/
function PluginRioPlayer() {
return true;
} // PluginLastfm
/**
* install
* This is a required plugin function it inserts the required preferences
* into Ampache
*/
function install() {
/* We need to insert the new preferences */
$sql = "INSERT INTO preferences (`name`,`value`,`description`,`level`,`type`,`catagory`) " .
"VALUES ('rio_querylimit','3000','Rio Player Query Limit','100','integer','system')";
$db_results = mysql_query($sql,dbh());
$sql = "INSERT INTO preferences (`name`,`value`,`description`,`level`,`type`,`catagory`) " .
"VALUES ('rio_track_stats','0','Rio Player Track Stats','100','boolean','system')";
$db_results = mysql_query($sql,dbh());
$sql = "INSERT INTO preferences (`name`,`value`,`description`,`level`,`type`,`catagory`) " .
"VALUES ('rio_user','','Rio Player Global User','100','string','system')";
$db_results = mysql_query($sql,dbh());
$sql = "INSERT INTO preferences (`name`,`value`,`description`,`level`,`type`,`catagory`) " .
"VALUES ('rio_global_stats','0','Rio Player Group Stats','100','boolean','system')";
$db_results = mysql_query($sql,dbh());
fix_all_users_prefs();
} // install
/**
* uninstall
* This is a required plugin function it removes the required preferences from
* the database returning it to its origional form
*/
function uninstall() {
/* We need to remove the preivously added preferences */
$sql = "DELETE FROM preferences WHERE name='rio_querylimit' OR name='rio_track_stats' OR name='rio_user' OR name='rio_global_stats'";
$db_results = mysql_query($sql,dbh());
fix_all_users_prefs();
} // uninstall
} // end AmpacheRioPlayer
?>

View file

@ -199,8 +199,7 @@ switch ($_REQUEST['action']) {
echo xmlData::tags(array($uid)); echo xmlData::tags(array($uid));
break; break;
case 'tag_artists': case 'tag_artists':
$tag = new tag($_REQUEST['filter']); $artists = Tag::get_tag_objects('artist',$_REQUEST['filter']);
$tags = Tag::get_object_tags('artist',$tag->id);
xmlData::set_offset($_REQUEST['offset']); xmlData::set_offset($_REQUEST['offset']);
xmlData::set_limit($_REQUEST['limit']); xmlData::set_limit($_REQUEST['limit']);
@ -209,8 +208,7 @@ switch ($_REQUEST['action']) {
echo xmlData::artists($artists); echo xmlData::artists($artists);
break; break;
case 'tag_albums': case 'tag_albums':
$genre = new Genre($_REQUEST['filter']); $albums = Tag::get_tag_objects('album',$_REQUEST['filter']);
$albums = $genre->get_albums();
xmlData::set_offset($_REQUEST['offset']); xmlData::set_offset($_REQUEST['offset']);
xmlData::set_limit($_REQUEST['limit']); xmlData::set_limit($_REQUEST['limit']);
@ -219,8 +217,7 @@ switch ($_REQUEST['action']) {
echo xmlData::albums($albums); echo xmlData::albums($albums);
break; break;
case 'tag_songs': case 'tag_songs':
$genre = new Genre($_REQUEST['filter']); $songs = Tag::get_tag_objects('song',$_REQUEST['filter']);
$songs = $genre->get_songs();
xmlData::set_offset($_REQUEST['offset']); xmlData::set_offset($_REQUEST['offset']);
xmlData::set_limit($_REQUEST['limit']); xmlData::set_limit($_REQUEST['limit']);

View file

@ -24,7 +24,7 @@ if (INIT_LOADED != '1') { exit; }
$web_path = Config::get('web_path'); $web_path = Config::get('web_path');
$htmllang = str_replace("_","-",Config::get('lang')); $htmllang = str_replace("_","-",Config::get('lang'));
$location = get_location(); $location = get_location();
$dir = is_rtl() ? "rtl" : "ltr"; $dir = is_rtl(Config::get('lang')) ? "rtl" : "ltr";
?> ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $htmllang; ?>" lang="<?php echo $htmllang; ?>" dir="<?php echo $dir;?>"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $htmllang; ?>" lang="<?php echo $htmllang; ?>" dir="<?php echo $dir;?>">