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

Commit 9dd12d1d authored by elleo's avatar elleo

Add "now playing" support

Fix invalid foreign key constraints on some tables
parent f852c4ad
......@@ -27,7 +27,7 @@ require_once('database.php');
if($field == "time") {
$value = strftime("%c", $value);
}
echo "<td>$value</td>";
echo "<td>". stripslashes($value) . "</td>";
}
echo "</tr>";
}
......@@ -37,6 +37,15 @@ require_once('database.php');
<h2>Now Playing?</h2>
<p><em>Coming soon</em></p>
<?php
$res = $mdb2->query("SELECT username, artist, track FROM Now_Playing ORDER BY expires DESC LIMIT 10");
if(PEAR::isError($res)) {
die($res->getMessage());
}
while($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
echo "<p>" . stripslashes($row["username"]) . " is listening to " . stripslashes($row["track"]) . " by " . stripslashes($row["artist"]) . "</p>";
}
?>
</body>
</html>
......@@ -36,7 +36,7 @@ if (isset($_POST['install'])) {
$res = $mdb2->query("CREATE TABLE Auth (token VARCHAR(32) PRIMARY KEY,
sk VARCHAR(32),
expires TIMESTAMP,
username VARCHAR(255) REFERENCES Users(username))");
username VARCHAR(64) REFERENCES Users(username))");
$mdb2->query("CREATE TABLE Artist(
name VARCHAR(255) PRIMARY KEY,
......@@ -77,18 +77,24 @@ if (isset($_POST['install'])) {
PRIMARY KEY(name, artist))");
$mdb2->query("CREATE TABLE Scrobbles(
username VARCHAR(255) REFERENCES User(username),
username VARCHAR(64) REFERENCES Users(username),
track VARCHAR(255) REFERENCES Track(name),
artist VARCHAR(255) REFERENCES Artist(name),
time TIMESTAMP,
PRIMARY KEY(username, track, artist, time))");
$mdb2->query("CREATE TABLE Scrobble_Sessions(
username VARCHAR(255) REFERENCES User(username),
username VARCHAR(64) REFERENCES Users(username),
sessionid VARCHAR(32),
expires TIMESTAMP,
PRIMARY KEY(username, sessionid))");
$mdb2->query("CREATE TABLE Now_Playing(
username VARCHAR(64) REFERENCES Users(username) PRIMARY KEY,
track VARCHAR(255) REFERENCES Track(name),
artist VARCHAR(255) REFERENCES Artist(name),
expires TIMESTAMP)");
// Test user configuration
$res = $mdb2->query("INSERT INTO Users
(username, password, created)
......
<?
require_once('../../database.php');
require_once('../../scrobble-utils.php');
if(!isset($_POST['s']) || !isset($_POST['a']) || !isset($_POST['t'])) {
die("FAILED Required POST parameters are not set");
}
$session_id = $_POST['s'];
$username = $mdb2->quote(usernameFromSID($session_id), "text");
$artist = $mdb2->quote($_POST['a'], "text");
if(isset($_POST['b'])) {
$album = $mdb2->quote($_POST['b'], "text");
} else {
$album = 'NULL';
}
$track = $mdb2->quote($_POST['t'], "text");
if(isset($_POST['l']) && is_numeric($_POST['l'])) {
$expires = time() + (int) $_POST['l'];
} else {
$expires = time() + 250; //Expire in 5 minutes if we don't know the track length
}
if(isset($_POST['m'])) {
$mbid = $mdb2->quote($_POST['m'], "text");
} else {
$mbid = 'NULL';
}
createArtistIfNew($artist);
if($album != 'NULL') {
createAlbumIfNew($artist, $album);
}
createTrackIfNew($artist, $album, $track, $mbid);
//Expire old tracks
$mdb2->query("DELETE FROM Now_Playing WHERE expires < " . time());
//Delete this user's last playing song (if any)
$mdb2->query("DELETE FROM Now_Playing WHERE username = " . $username);
$mdb2->query("INSERT INTO Now_Playing (username, artist, track, expires) VALUES ("
. $username . ", "
. $artist . ", "
. $track . ", "
. $expires . ")");
// Close your eyes and pretend that everything's okay...
die("OK");
?>
<?
require_once('database.php');
function usernameFromSID($session_id) {
global $mdb2;
// Delete any expired session ids
$mdb2->query("DELETE FROM Scrobble_Sessions WHERE expires < " . time());
$res = $mdb2->query("SELECT username FROM Scrobble_Sessions WHERE sessionid = " . $mdb2->quote($session_id, "text"));
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
die("BADSESSION");
}
return $res->fetchOne(0);
}
function createArtistIfNew($artist) {
global $mdb2;
......
......@@ -12,19 +12,7 @@ if(!is_array($_POST['a']) || !is_array($_POST['t']) || !is_array($_POST['i'])) {
$session_id = $_POST['s'];
// Delete any expired session ids
$mdb2->query("DELETE FROM Scrobble_Sessions WHERE expires < " . time());
$res = $mdb2->query("SELECT username FROM Scrobble_Sessions WHERE sessionid = " . $mdb2->quote($session_id, "text"));
if(PEAR::isError($res)) {
die("FAILED " . $res->getMessage());
}
if(!$res->numRows()) {
die("BADSESSION");
}
$username = $mdb2->quote($res->fetchOne(0), "text");
$username = $mdb2->quote(usernameFromSID($session_id), "text");
for($i = 0; $i < count($_POST['a']); $i++) {
$artist = $mdb2->quote($_POST['a'][$i], "text");
......
......@@ -8,6 +8,7 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
public function testStandardAuth() {
$result = $this->standardLogin("testuser", "password");
$this->assertEquals("OK", trim($result[0]));
sleep(1);
}
......@@ -15,6 +16,7 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
public function testFailedStandardAuth() {
$result = $this->standardLogin("testuser", "icanhazsecurity?");
$this->assertEquals("BADAUTH", trim($result[0]));
sleep(1);
}
......@@ -25,8 +27,20 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
$session_id = trim($result[1]);
$scrobble_server = trim($result[3]);
$result = $this->scrobble($scrobble_server, $session_id, "Richard Stallman", "The Free Software Song");
echo $result;
$this->assertEquals("OK", $result);
sleep(1);
}
public function testNowPlaying() {
$result = $this->standardLogin("testuser", "password");
$this->assertEquals("OK", trim($result[0]));
$session_id = trim($result[1]);
$nowplaying_server = trim($result[2]);
$result = $this->nowPlaying($nowplaying_server, $session_id, "The Libre.fm Players", "Let Freedom Ring");
$this->assertEquals("OK", $result);
sleep(1);
}
......@@ -52,4 +66,14 @@ class SubmissionsTest extends PHPUnit_Framework_TestCase
return $r->getResponseBody();
}
private function nowPlaying($server, $session_id, $artist, $track) {
$r = new HTTP_Request($server);
$r->setMethod(HTTP_REQUEST_METHOD_POST);
$r->addPostData('s', $session_id);
$r->addPostData('a', $artist);
$r->addPostData('t', $track);
$r->sendRequest();
return $r->getResponseBody();
}
}
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