We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 5be3eb40 authored by elleo's avatar elleo

Refactor submissions handshake to be protocol specific

Implement 1.1 handshake (scrobbles from Rhythmbox now work)
Modify 1.2 submissions handler to be able to cope with 1.1 submissions as well
Store all scrobbles instead of just a summary
parent a5e574d8
......@@ -6,5 +6,11 @@ if (!isset($config_version) || $config_version != $version) {
die("Configuration file is out of date. Please delete it (and associated database) and <a href='install.php'>reinstall</a>."); //TODO: Upgrade script for release versions
}
require_once('submissions-handshake.php');
if (isset($_GET['hs']) && isset($_GET['p'])) {
if (substr($_GET['p'], 0, 3) == "1.2") {
require_once('submissions/1.2/handshake.php');
} elseif (substr($_GET['p'], 0, 3) == "1.1") {
require_once('submissions/1.1/handshake.php');
}
}
?>
......@@ -80,10 +80,8 @@ if (isset($_POST['install'])) {
username VARCHAR(255) REFERENCES User(username),
track VARCHAR(255) REFERENCES Track(name),
artist VARCHAR(255) REFERENCES Artist(name),
rating VARCHAR(1),
playcount int,
lastplayed TIMESTAMP,
PRIMARY KEY(username, track, artist))");
time TIMESTAMP,
PRIMARY KEY(username, track, artist, time))");
$mdb2->query("CREATE TABLE Scrobble_Sessions(
username VARCHAR(255) REFERENCES User(username),
......
<?
// Implements the submissions handshake protocol as detailed at: http://www.last.fm/api/submissions
require_once('auth-utils.php');
require_once('config.php');
$supported_protocols = array("1.2", "1.2.1");
if(isset($_GET['hs'])) {
//Handshake
if(!isset($_GET['p']) || !isset($_GET['u']) || !isset($_GET['t']) || !isset($_GET['a'])) {
die("BADAUTH");
}
$protocol = $_GET['p']; $username = $_GET['u']; $timestamp = $_GET['t']; $auth_token = $_GET['a'];
if(!in_array($protocol, $supported_protocols)) {
die("FAILED Unsupported protocol version");
}
if(isset($_GET['api_key']) && isset($_GET['sk'])) {
$authed = check_web_auth($username, $auth_token, $timestamp, $_GET['api_key'], $_GET['sk']);
} else {
$authed = check_standard_auth($username, $auth_token, $timestamp);
}
if(!$authed) {
die("BADAUTH");
}
$session_id = md5($auth_token . time());
$mdb2->query("INSERT INTO Scrobble_Sessions(username, sessionid, expires) VALUES ("
. $mdb2->quote($username, "text") . ","
. $mdb2->quote($session_id, "text") . ","
. $mdb2->quote(time() + 86400) . ")");
echo "OK\n";
echo $session_id . "\n";
echo $submissions_server . "/nowplaying/1.2/\n";
echo $submissions_server . "/submissions/1.2/\n";
}
?>
<?
// Implements the submissions handshake protocol 1.1 as detailed at: http://www.audioscrobbler.net/wiki/Protocol1.1.merged
//
// By sending the timestamp as the md5 challenge then creating the session key from md5(md5($password) . $timestamp) we can
// force a 1.1 client to give us a session key that can be used by the 1.2 protocol handler, so we only handle handshakes for
// 1.1 then pass all submissions off to the 1.2 handler.
require_once('auth-utils.php');
require_once('config.php');
$supported_protocols = array("1.1");
if(!isset($_GET['p']) || !isset($_GET['u'])) {
die("FAILED");
}
$protocol = $_GET['p']; $username = $_GET['u'];
if(!in_array($protocol, $supported_protocols)) {
die("FAILED Unsupported protocol version");
}
$timestamp = time();
$res = $mdb2->query("SELECT password FROM Users WHERE username = ". $mdb2->quote($username, "text"));
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
die("BADUSER");
}
$password = $res->fetchOne(0);
$session_id = md5($password . $timestamp);
$mdb2->query("INSERT INTO Scrobble_Sessions(username, sessionid, expires) VALUES ("
. $mdb2->quote($username, "text") . ","
. $mdb2->quote($session_id, "text") . ","
. $mdb2->quote(time() + 86400) . ")");
echo "UPTODATE\n";
echo $timestamp . "\n";
echo $submissions_server . "/submissions/1.2/\n";
echo "INTERVAL 0\n";
?>
<?
// Implements the submissions handshake protocol as detailed at: http://www.last.fm/api/submissions
require_once('auth-utils.php');
require_once('config.php');
$supported_protocols = array("1.2", "1.2.1");
if(!isset($_GET['p']) || !isset($_GET['u']) || !isset($_GET['t']) || !isset($_GET['a'])) {
die("BADAUTH");
}
$protocol = $_GET['p']; $username = $_GET['u']; $timestamp = $_GET['t']; $auth_token = $_GET['a'];
if(!in_array($protocol, $supported_protocols)) {
die("FAILED Unsupported protocol version");
}
if(isset($_GET['api_key']) && isset($_GET['sk'])) {
$authed = check_web_auth($username, $auth_token, $timestamp, $_GET['api_key'], $_GET['sk']);
} else {
$authed = check_standard_auth($username, $auth_token, $timestamp);
}
if(!$authed) {
die("BADAUTH");
}
$session_id = md5($auth_token . time());
$mdb2->query("INSERT INTO Scrobble_Sessions(username, sessionid, expires) VALUES ("
. $mdb2->quote($username, "text") . ","
. $mdb2->quote($session_id, "text") . ","
. $mdb2->quote(time() + 86400) . ")");
echo "OK\n";
echo $session_id . "\n";
echo $submissions_server . "/nowplaying/1.2/\n";
echo $submissions_server . "/submissions/1.2/\n";
?>
......@@ -2,11 +2,11 @@
require_once('../../database.php');
require_once('../../scrobble-utils.php');
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i']) || !isset($_POST['o'])) {
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t']) || !isset($_POST['i'])) {
die("FAILED Required POST parameters are not set");
}
if(!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i']) || !is_array($_POST['o'])) {
if(!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i'])) {
die("FAILED Track parameters must be arrays");
}
......@@ -34,8 +34,12 @@ for($i = 0; $i < count($_POST['a']); $i++) {
$album = 'NULL';
}
$track = $mdb2->quote($_POST['t'][$i], "text");
$time = (int) $_POST['i'][$i];
$origin = $mdb2->quote($_POST['o'][$i], "text");
if(is_numeric($_POST['i'][$i])) {
$time = (int) $_POST['i'][$i];
} else {
// 1.1 time format
$time = strtotime($_POST['i'][$i]);
}
if(isset($_POST['m'][$i])) {
$mbid = $mdb2->quote($_POST['m'][$i], "text");
} else {
......@@ -49,29 +53,11 @@ for($i = 0; $i < count($_POST['a']); $i++) {
createTrackIfNew($artist, $album, $track, $mbid);
// Scrobble!
$res = $mdb2->query("SELECT playcount FROM Scrobbles WHERE username = " . $username . " AND track = " . $track . " AND artist = " . $artist);
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
// New scrobble entry
$mdb2->query("INSERT INTO Scrobbles (username, artist, track, lastplayed, playcount) VALUES ("
. $username . ", "
. $artist . ", "
. $track . ", "
. $time . ", "
. "1)");
} else {
$playcount = $res->fetchOne(0);
$playcount++;
$mdb2->query("UPDATE Scrobbles SET "
. "playcount = " . $playcount . ", "
. "lastplayed = " . $time
. " WHERE username = " . $username
. " AND artist = " . $artist
. " AND track = " . $track);
}
$mdb2->query("INSERT INTO Scrobbles (username, artist, track, time) VALUES ("
. $username . ", "
. $artist . ", "
. $track . ", "
. $time . ")");
}
die("OK");
......
......@@ -39,6 +39,7 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
return $response;
}
private function scrobble($server, $session_id, $artist, $track) {
$r = new HTTP_Request($server);
$r->setMethod(HTTP_REQUEST_METHOD_POST);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment