handshake.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /* GNUkebox -- a free software server for recording your listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. // Implements the submissions handshake protocol 1.1 as detailed at: http://www.audioscrobbler.net/wiki/Protocol1.1.merged
  16. //
  17. // By sending the timestamp as the md5 challenge then creating the session key from md5(md5($password) . $timestamp) we can
  18. // 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
  19. // 1.1 then pass all submissions off to the 1.2 handler.
  20. require_once('auth-utils.php');
  21. require_once('config.php');
  22. require_once('temp-utils.php');
  23. $supported_protocols = array('1.1');
  24. if (!isset($_REQUEST['p']) || !isset($_REQUEST['u']) || !isset($_REQUEST['c'])) {
  25. die("FAILED\n");
  26. }
  27. $protocol = $_REQUEST['p']; $username = $_REQUEST['u']; $client = $_REQUEST['c'];
  28. if (!in_array($protocol, $supported_protocols)) {
  29. die("FAILED Unsupported protocol version\n");
  30. }
  31. $timestamp = time();
  32. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  33. try {
  34. $row = $adodb->GetRow('SELECT uniqueid,password FROM Users WHERE lower(username) = lower(' . $adodb->qstr($username) . ')');
  35. } catch (Exception $e) {
  36. die('FAILED ' . $e->getMessage() . "\n");
  37. }
  38. if (!$row) {
  39. die("BADUSER\n");
  40. }
  41. $password = $row['password'];
  42. $uniqueid = $row['uniqueid'];
  43. $session_id = md5($password . $timestamp);
  44. try {
  45. $res = $adodb->Execute('INSERT INTO Scrobble_Sessions(userid, sessionid, client, expires) VALUES ('
  46. . ($uniqueid) . ','
  47. . $adodb->qstr($session_id, 'text') . ','
  48. . $adodb->qstr($client, 'text') . ','
  49. . $adodb->qstr(time() + 86400) . ')');
  50. } catch (Exception $e) {
  51. die('FAILED ' . $e->getMessage() . "\n");
  52. }
  53. echo "UPTODATE\n";
  54. echo $timestamp . "\n";
  55. echo $submissions_server . "/submissions/1.2/\n";
  56. echo "INTERVAL 1\n";