handshake.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 as detailed at: http://www.last.fm/api/submissions
  16. require_once('auth-utils.php');
  17. require_once('config.php');
  18. require_once('temp-utils.php');
  19. $supported_protocols = array('1.2', '1.2.1');
  20. if (!isset($_REQUEST['p']) || !isset($_REQUEST['u']) || !isset($_REQUEST['t']) || !isset($_REQUEST['a']) || !isset($_REQUEST['c'])) {
  21. die("BADAUTH\n");
  22. }
  23. $protocol = $_REQUEST['p'];
  24. $username = $_REQUEST['u'];
  25. $timestamp = $_REQUEST['t'];
  26. $auth_token = $_REQUEST['a'];
  27. $client = $_REQUEST['c'];
  28. if ($client == 'import') {
  29. die("FAILED Import scripts are broken\n"); // this should be removed or changed to check the version once import.php is fixed
  30. }
  31. if (!in_array($protocol, $supported_protocols)) {
  32. die("FAILED Unsupported protocol version\n");
  33. }
  34. if (abs($timestamp - time()) > 300) {
  35. die("BADTIME\n"); // let's try a 5-minute tolerance
  36. }
  37. if (isset($_REQUEST['api_key']) && isset($_REQUEST['sk'])) {
  38. $authed = check_web_auth($username, $auth_token, $timestamp, $_REQUEST['api_key'], $_REQUEST['sk']);
  39. } else {
  40. $authed = check_standard_auth($username, $auth_token, $timestamp);
  41. }
  42. if (!$authed) {
  43. die("BADAUTH\n");
  44. }
  45. $uniqueid = username_to_uniqueid($username);
  46. $session_id = md5($auth_token . time());
  47. $sql = 'INSERT INTO Scrobble_Sessions(userid, sessionid, client, expires) VALUES ('
  48. . $uniqueid . ','
  49. . $adodb->qstr($session_id) . ','
  50. . $adodb->qstr($client) . ','
  51. . (time() + 86400) . ')';
  52. try {
  53. $res = $adodb->Execute($sql);
  54. } catch (Exception $e) {
  55. $msg = $e->getMessage();
  56. reportError($msg, $sql);
  57. die('FAILED ' . $msg . "\n");
  58. }
  59. echo "OK\n";
  60. echo $session_id . "\n";
  61. echo $submissions_server . "/nowplaying/1.2/\n";
  62. echo $submissions_server . "/submissions/1.2/\n";