Commit 70922ba5 authored by Jonas Haraldsson's avatar Jonas Haraldsson

Moved getScrobbleSessionID()

Moved scrobbleutils.php::getScrobbleSessionID() to
Server::getScrobbleSession()
Added expire_limit parameter to Server::getScrobbleSession()
Changed User::getScrobbleSession to use Server::getScrobbleSession()
Changed login.php to use Server::getScrobbleSession()
parent 56fda368
......@@ -224,7 +224,7 @@ class TrackXML {
public static function updateNowPlaying($userid, $artist, $track, $album, $tracknumber, $context, $mbid, $duration, $albumartist, $api_key) {
global $adodb;
$sessionid = getScrobbleSessionID($userid, $api_key);
$sessionid = Server::getScrobbleSession($userid, $api_key);
$t = array(
'artist' => $artist,
......@@ -297,7 +297,7 @@ class TrackXML {
public static function scrobble($userid, $artist, $track, $timestamp, $album, $context, $streamid, $chosenbyuser, $tracknumber, $mbid, $albumartist, $duration, $api_key) {
global $adodb;
$sessionid = getScrobbleSessionID($userid, $api_key);
$sessionid = Server::getScrobbleSession($userid, $api_key);
$accepted_count = 0;
$ignored_count = 0;
......
......@@ -910,6 +910,49 @@ class Server {
return $sk;
}
/**
* Get scrobble session ID for a user.
*
* Gets the most recent scrobble session ID for userid,
* or creates a new session ID if it can't find one.
*
* @param int userid (required) User ID.
* @param string api_key (optional) Client API key (32 characters)
* @param int expire_limit (optional) Amount of time in seconds before session will expire (defaults to 86400 = 24 hours)
* @return string Scrobble session ID
*/
static function getScrobbleSession($userid, $api_key = null, $expire_limit = 86400) {
global $adodb;
$query = 'SELECT sessionid FROM Scrobble_Sessions WHERE userid = ? AND expires > ?';
$params = array( (int) $userid, time());
if (strlen($api_key) == 32) {
$query .= ' AND api_key=?';
$params[] = $api_key;
} elseif (strlen($api_key) == 3) {
// api_key is really a 3 char client code (2.0-scrobble-proxy.php sends client code in api_key)
$query .= ' AND client=?';
$client_id = $api_key;
$params[] = $client_id;
// we dont want to insert a 3 char code as api_key in db
$api_key = null;
}
$sessionid = $adodb->GetOne($query, $params);
if (!$sessionid) {
$sessionid = md5(mt_rand() . time());
$expires = time() + (int) $expire_limit;
$query = 'INSERT INTO Scrobble_Sessions(userid, sessionid, client, expires, api_key) VALUES (?,?,?,?,?)';
$params = array($userid, $sessionid, $client_id, $expires, $api_key);
try {
$adodb->Execute($query, $params);
} catch (Exception $e) {
return null;
}
}
return $sessionid;
}
/**
* Get all artists
*
......
......@@ -233,18 +233,7 @@ class User {
* @return array A string containing the session key to be used for scrobbling
*/
function getScrobbleSession() {
global $adodb;
$session_id = md5(mt_rand() . time());
$sql = 'INSERT INTO Scrobble_Sessions(userid, sessionid, client, expires) VALUES ('
. ($this->uniqueid) . ','
. $adodb->qstr($session_id) . ','
. '\'lfm\','
. (time() + 86400) . ')';
if ($adodb->Execute($sql)) {
return $session_id;
} else {
return false;
}
return Server::getScrobbleSession($this->uniqueid);
}
/**
......
......@@ -59,16 +59,13 @@ if (isset($_POST['login'])) {
$smarty->assign('invalid', true);
} else {
// Give the user a session id, like any other client
$session_id = md5(md5($password) . time());
if (isset($remember)) {
$session_time = time() + 31536000; // 1 year
$expire_limit = 31536000; // 1 year
} else {
$session_time = time() + 86400; // 1 day
$expire_limit = 86400; // 1 day
}
$adodb->Execute('INSERT INTO Scrobble_Sessions (userid, sessionid, expires) VALUES ('
. ($userid) . ', '
. $adodb->qstr($session_id) . ', '
. (int)($session_time) . ')');
$session_id = Server::getScrobbleSession($userid, $gnufm_key, $expire_limit);
$session_time = time() + $expire_limit;
setcookie('session_id', $session_id, $session_time);
$logged_in = true;
......
......@@ -166,48 +166,6 @@ function getScrobbleTrackID($artist, $album, $track, $mbid, $duration, $track_id
}
}
/**
* Get scrobble session ID for a user.
*
* Gets the most recent scrobble session ID for userid,
* or creates a new session ID if it can't find one.
*
* @param int userid (required) User ID.
* @param string api_key (optional) Client API key (32 characters)
* @return string Scrobble session ID
*/
function getScrobbleSessionID($userid, $api_key = null) {
global $adodb;
$query = 'SELECT sessionid FROM Scrobble_Sessions WHERE userid = ? AND expires > ?';
$params = array($userid, time());
if (strlen($api_key) == 32) {
$query .= ' AND api_key=?';
$params[] = $api_key;
} elseif (strlen($api_key) == 3) {
// api_key is really a 3 char client code (2.0-scrobble-proxy.php sends client code in api_key)
$query .= ' AND client=?';
$client_id = $api_key;
$params[] = $client_id;
// we dont want to insert a 3 char code as api_key in db
$api_key = null;
}
$sessionid = $adodb->GetOne($query, $params);
if (!$sessionid) {
$sessionid = md5(mt_rand() . time());
$expires = time() + 86400;
$query = 'INSERT INTO Scrobble_Sessions(userid, sessionid, client, expires, api_key) VALUES (?,?,?,?,?)';
$params = array($userid, $sessionid, $client_id, $expires, $api_key);
try {
$adodb->Execute($query, $params);
} catch (Exception $e) {
return null;
}
}
return $sessionid;
}
/**
* Correct artist/album/track/mbid/timestamp input
*
......
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