123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <?php
- require_once('database.php');
- function getArtistID($artist) {
- global $adodb;
- $query = 'SELECT id FROM Artist WHERE name=?';
- $params = array($artist);
- $artist_id = $adodb->GetOne($query, $params);
- if (!$artist_id) {
-
- $query = 'INSERT INTO Artist (name) VALUES (?)';
- $params = array($artist);
- $res = $adodb->Execute($query, $params);
- return getArtistID($artist);
- } else {
- return $artist_id;
- }
- }
- function getAlbumID($artist, $album) {
- global $adodb;
- $query = 'SELECT id FROM Album WHERE name=? AND artist_name=?';
- $params = array($album, $artist);
- $album_id = $adodb->GetOne($query, $params);
- if (!$album_id) {
-
-
- $artist_id = getArtistID($artist);
- $query = 'INSERT INTO Album (name, artist_name) VALUES (?,?)';
- $params = array($album, $artist);
- $adodb->Execute($query, $params);
- return getAlbumID($artist, $album);
- } else {
- return $album_id;
- }
- }
- function getTrackID($artist, $album, $track, $mbid, $duration) {
- global $adodb;
- if ($album === null) {
- $query = 'SELECT id FROM Track WHERE name=? AND artist_name=? AND album_name IS NULL';
- $params = array($track, $artist);
- } else {
- $query = 'SELECT id FROM Track WHERE name=? AND artist_name=? AND album_name=?';
- $params = array($track, $artist, $album);
- }
- $track_id = $adodb->GetOne($query, $params);
- if (!$track_id) {
-
- if ($album === null) {
- $artist_id = getArtistID($artist);
- } else {
- $album_id = getAlbumID($artist, $album);
- }
-
-
- $query = 'INSERT INTO Track (name, artist_name, album_name, mbid, duration) VALUES (?,?,?,?,?)';
- $params = array($track, $artist, $album, $mbid, $duration);
- $adodb->Execute($query, $params);
- return getTrackID($artist, $album, $track, $mbid, $duration);
- } else {
- return $track_id;
- }
- }
- function getScrobbleTrackID($artist, $album, $track, $mbid, $duration, $track_id) {
- global $adodb;
- $query = 'SELECT id FROM Scrobble_Track WHERE name=lower(?) AND artist=lower(?)';
- $params = array($track, $artist);
- if ($album === null) {
- $query .= ' AND album IS NULL';
- } else {
- $query .= ' AND album=lower(?)';
- $params[] = $album;
- }
- if ($mbid === null) {
- $query .= ' AND mbid IS NULL';
- } else {
- $query .= ' AND mbid=lower(?)';
- $params[] = $mbid;
- }
- $scrobbletrack_id = $adodb->GetOne($query, $params);
- if (!$scrobbletrack_id) {
- $query = 'INSERT INTO Scrobble_Track (name, artist, album, mbid, track) VALUES (lower(?), lower(?), lower(?), lower(?), ?)';
- $params = array($track, $artist, $album, $mbid, $track_id);
- $res = $adodb->Execute($query, $params);
- return getScrobbleTrackID($artist, $album, $track, $mbid, $duration, $track_id);
- } else {
- return $scrobbletrack_id;
- }
- }
- function correctInput($input, $type) {
- $old = $input;
- $new = $old;
- if ($type == 'artist' || $type == 'album' || $type == 'track' || $type == 'albumartist') {
-
- switch (mb_detect_encoding($new)) {
- case 'ASCII':
- case 'UTF-8':
- $new = mb_strcut($new, 0, 255, 'UTF-8');
- break;
- default:
- $new = null;
- }
-
- $new = str_replace(' (PREVIEW: buy it at www.magnatune.com)', '', $new);
- $new = trim($new);
- if (empty($new)) {
- $new = null;
- }
- } else if ($type == 'mbid') {
- if (isset($new)) {
- $new = strtolower(rtrim($new));
- if (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $new)) {
-
- } else {
- $new = null;
- }
- } else {
- $new = null;
- }
- } else if ($type == 'timestamp') {
- $new = (int) $new;
- } else if ($type == 'duration') {
- if($new) {
- $new = (int) $new;
- } else {
- $new = null;
- }
- } else if ($type == 'tracknumber') {
- if($new) {
- $new = (int) $new;
- }else {
- $new = null;
- }
- }
- $result = array($old, $new, (int)($old != $new));
- return $result;
- }
- function ignoreInput($input, $type) {
- $ignored_code = 0;
- $ignored_message = '';
- if ($type == 'artist' && empty($input)) {
- $ignored_code = 1;
- $ignored_message = 'Artist was ignored';
- }
- if ($type == 'track' && empty($input)) {
- $ignored_code = 2;
- $ignored_message = 'Track was ignored';
- }
- if ($type == 'timestamp') {
- $timestamp_upperlimit = time() + 300;
- $timestamp_lowerlimit = 1009000000;
-
- if ($input > $timestamp_upperlimit) {
- $ignored_message = 'Timestamp is too new';
- $ignored_code = 3;
- }
- if ($input < $timestamp_lowerlimit) {
- $ignored_message = 'Timestamp is too old';
- $ignored_code = 4;
- }
- }
- return array($ignored_code, $ignored_message);
- }
- function prepareTrack($userid, $t, $type) {
- list($t['track_old'], $t['track'], $t['track_corrected']) = correctInput($t['track'], 'track');
- list($t['artist_old'], $t['artist'], $t['artist_corrected']) = correctInput($t['artist'], 'artist');
- list($t['album_old'], $t['album'], $t['album_corrected']) = correctInput($t['album'], 'album');
- list($t['mbid_old'], $t['mbid'], $t['mbid_corrected']) = correctInput($t['mbid'], 'mbid');
- list($t['duration_old'], $t['duration'], $t['duration_corrected']) = correctInput($t['duration'], 'duration');
- list($t['albumartist_old'], $t['albumartist'], $t['albumartist_corrected']) = correctInput($t['albumartist'], 'albumartist');
- list($t['tracknumber_old'], $t['tracknumber'], $t['tracknumber_corrected']) = correctInput($t['tracknumber'], 'tracknumber');
-
- list($t['ignored_code'], $t['ignored_message']) = ignoreInput($t['artist'], 'artist');
- if($t['ignored_code'] === 0) {
- list($t['ignored_code'], $t['ignored_message']) = ignoreInput($t['track'], 'track');
- }
- if ($type == 'scrobble') {
- list($t['timestamp_old'], $t['timestamp'], $t['timestamp_corrected']) = correctInput($t['timestamp'], 'timestamp');
- if($t['ignored_code'] === 0) {
- list($t['ignored_code'], $t['ignored_message']) = ignoreInput($t['timestamp'], 'timestamp');
- }
- if ($t['ignored_code'] === 0) {
- $exists = scrobbleExists($userid, $t['artist'], $t['track'], $t['timestamp']);
- if ($exists) {
- $t['ignored_code'] = 91;
- $t['ignored_message'] = 'Already scrobbled';
- }
- }
- }
- return $t;
- }
- function scrobbleExists($userid, $artist, $track, $time) {
- global $adodb;
- $query = 'SELECT time FROM Scrobbles WHERE userid=? AND artist=? AND track=? AND time=?';
- $params = array($userid, $artist, $track, $time);
- $res = $adodb->GetOne($query, $params);
- if (!$res) {
- return false;
- } else {
- return true;
- }
- }
- function forwardScrobble($userid, $artist, $album, $track, $time, $mbid, $source, $rating, $length) {
- global $adodb, $lastfm_key, $lastfm_secret;
- $artist = rawurlencode($artist);
- $track = rawurlencode($track);
- $album = rawurlencode($album);
- $mbid = rawurlencode($mbid);
- $source = rawurlencode($source);
- $rating = rawurlencode($rating);
- $length = rawurlencode($length);
- $res = $adodb->CacheGetAll(600, 'SELECT * FROM Service_Connections WHERE userid = ' . $userid . ' AND forward = 1');
- foreach ($res as &$row) {
- $remote_key = $row['remote_key'];
- $ws_url = $row['webservice_url'];
- $curl_session = curl_init($ws_url);
- $post_vars = '';
- if ($album) {
- $post_vars .= 'album[0]=' . $album . '&';
- }
- $post_vars .= 'api_key=' . $lastfm_key . '&artist[0]=' . $artist;
- if ($length) {
- $post_vars .= '&length[0]=' . $length;
- }
- if ($mbid) {
- $post_vars .= '&mbid[0]=' . $mbid;
- }
- $post_vars .= '&method=track.scrobble';
- if ($rating) {
- $post_vars .= '&rating[0]=' . $rating;
- }
- $post_vars .= '&sk=' . $remote_key;
- if ($source) {
- $post_vars .= '&source[0]='. $source;
- }
- $post_vars .= '×tamp[0]=' . $time . '&track[0]=' . $track;
- $sig = urldecode(str_replace('&', '', $post_vars));
- $sig = str_replace('=', '', $sig);
- $sig = md5($sig . $lastfm_secret);
- $post_vars .= '&api_sig=' . $sig;
- curl_setopt($curl_session, CURLOPT_POST, true);
- curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_vars);
- curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, 1);
- curl_setopt($curl_session, CURLOPT_TIMEOUT, 1);
- $response = curl_exec($curl_session);
- curl_close($curl_session);
- }
- }
|