. */ require_once('database.php'); // include the database connection string function usernameFromSID($session_id) { //derive the username from a session ID global $mdb2; // include the Database connector // 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")); // get the username from the table if(PEAR::isError($res)) { die("FAILED ufs " . $res->getMessage() . "\n"); // die is there is an error, printing the error } if(!$res->numRows()) { die("BADSESSION\n"); // the user has no session } return $res->fetchOne(0); // return the first user } function createArtistIfNew($artist) { global $mdb2; $artist = NoSpamTracks($artist); $res = $mdb2->query("SELECT name FROM Artist WHERE name = " . ($artist)); if(PEAR::isError($res)) { die("FAILED art " . $res->getMessage() . "\n"); } if(!$res->numRows()) { // Artist doesn't exist, so we create them $res = $mdb2->query("INSERT INTO Artist (name) VALUES (" . ($artist) . ")"); if(PEAR::isError($res)) { die("FAILED artc " . $res->getMessage() . "\n"); } } } function createAlbumIfNew($artist, $album) { global $mdb2; $res = $mdb2->query("SELECT name FROM Album WHERE name = " . ($album) . " AND artist_name = " . ($artist)); if(PEAR::isError($res)) { die("FAILED alb " . $res->getMessage() . "\n"); } if(!$res->numRows()) { // Album doesn't exist, so create it $art = $mdb2->quote(getAlbumArt($artist, $album)); if ($art !="") { $license = $mdb2->quote("amazon"); $res = $mdb2->query("INSERT INTO Album (name, artist_name, image, artwork_license) VALUES (" . ($album) . ", " . ($artist) . ", " . ($art) . ", " . ($license) .")"); } else { $res = $mdb2->query("INSERT INTO Album (name, artist_name) VALUES (" . ($album) . ", " . ($artist) . ")"); } if(PEAR::isError($res)) { die("FAILED albc " . $res->getMessage() . "\n"); } } } function getTrackCreateIfNew($artist, $album, $track, $mbid) { global $mdb2; $track = NoSpamTracks($track); $artist = NoSpamTracks($artist); if($album != 'NULL') { $res = $mdb2->query("SELECT id FROM Track WHERE lower(name) = lower(" . ($track) . ") AND lower(artist) = lower(" . ($artist) . ") AND lower(album) = lower(" . ($album) . ")"); } else { $res = $mdb2->query("SELECT id FROM Track WHERE lower(name) = lower(" . ($track) . ") AND lower(artist) = lower(" . ($artist) . ") AND album IS NULL"); } if(PEAR::isError($res)) { die("FAILED trk " . $res->getMessage() . "\n"); } if(!$res->numRows()) { // Create new track $res = $mdb2->exec("INSERT INTO Track (name, artist, album, mbid) VALUES (" . ($track) . ", " . ($artist) . ", " . ($album) . ", " . ($mbid) . ")"); if(PEAR::isError($res)) { die("FAILED trkc " . $res->getMessage() . "\n"); } return getTrackCreateIfNew($artist, $album, $track, $mbid); } else { return $res->fetchOne(0); } } function getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid) { global $mdb2; $res = $mdb2->query("SELECT id FROM Scrobble_Track WHERE name = lower(" . ($track) . ") AND artist = lower(" . ($artist) . ") AND album " . (($album == 'NULL') ? "IS NULL" : ("= lower(" . ($album) . ")")) . " AND mbid " . (($mbid == 'NULL') ? "IS NULL" : ("= lower(" . ($mbid) . ")"))); if(PEAR::isError($res)) { die("FAILED st " . $res->getMessage() . "\n"); } if(!$res->numRows()) { $sql = "INSERT INTO Scrobble_Track (name, artist, album, mbid, track) VALUES (" . "lower(" . ($track) . "), " . "lower(" . ($artist) . "), " . (($album == 'NULL') ? "NULL" : "lower(" . ($album) . ")") . ", " . (($mbid == 'NULL') ? "NULL" : "lower(" . ($mbid) . ")") . ", " . ($tid) . ")"; $res = $mdb2->exec($sql); if(PEAR::isError($res)) { $msg = $res->getMessage() . " - " . $res->getUserInfo(); reportError($msg, $sql); die("FAILED stc " . $res->getMessage() . "\n"); } return getScrobbleTrackCreateIfNew($artist, $album, $track, $mbid, $tid); } else { return $res->fetchOne(0); } } function scrobbleExists($username, $artist, $track, $time) { global $mdb2; $res = $mdb2->query("SELECT time FROM Scrobbles WHERE username = " . ($username) . " AND artist = " . ($artist) . " AND track = " . ($track) . " AND time = " . ($time)); if(PEAR::isError($res)) { die("FAILED se " . $res->getMessage() . "\n"); } if(!$res->numRows()) { return false; } else { return true; } } function NoSpamTracks ($track) { // This function exists to remove things like '(PREVIEW: buy it at www.magnatune.com)' from track names. $track = str_replace(" (PREVIEW: buy it at www.magnatune.com)", "", $track); return $track; } function getAlbumArt($artist, $album) { $Access_Key_ID = "1EST86JB355JBS3DFE82"; // this is mattl's personal key :) $SearchIndex='Music'; $Keywords=urlencode($artist.' '.$album); $Operation = "ItemSearch"; $Version = "2007-07-16"; $ResponseGroup = "ItemAttributes,Images"; $request= "http://ecs.amazonaws.com/onca/xml" . "?Service=AWSECommerceService" . "&AssociateTag=" . $Associate_tag . "&AWSAccessKeyId=" . $Access_Key_ID . "&Operation=" . $Operation . "&Version=" . $Version . "&SearchIndex=" . $SearchIndex . "&Keywords=" . $Keywords . "&ResponseGroup=" . $ResponseGroup; $aws_xml = simplexml_load_file($request) or die("xml response not loading\n"); $image = $aws_xml->Items->Item->MediumImage->URL; $URI = $aws_xml->Items->Item->DetailPageURL; return $image; } function validateMBID ($input) { if(isset($input)) { $input = strtolower(rtrim($input)); if(preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $input)) { return $mdb2->quote($input, "text"); } else { return 'NULL' } } else { return 'NULL'; } ?>