index.php 47 KB
Newer Older
1 2
<?php

3
/* GNU FM -- a free network service for sharing your music listening habits
mattl's avatar
mattl committed
4

5
   Copyright (C) 2009 Free Software Foundation, Inc
mattl's avatar
mattl committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

22 23 24 25 26 27
/**
 * GNU FM web service methods
 *
 * @link http://bugs.foocorp.net/projects/librefm/wiki/Librefm_API
 * @package Webservice
 */
28
require_once('../database.php');
29 30
require_once('../api/ArtistXML.php');
require_once('../api/UserXML.php');
31
require_once('../api/JSONEncoder.php');
32
require_once('../api/TrackXML.php');
33
require_once('../api/AlbumXML.php');
34
require_once('../api/TagXML.php');
Jonas Haraldsson's avatar
Jonas Haraldsson committed
35
require_once('../api/LibraryXML.php');
Mike Sheldon's avatar
Mike Sheldon committed
36 37
require_once('../data/Server.php');
require_once('../radio/radio-utils.php');
38

39
# Error constants
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
define('LFM_INVALID_SERVICE',       2);
define('LFM_INVALID_METHOD',        3);
define('LFM_INVALID_TOKEN',         4);
define('LFM_INVALID_FORMAT',        5);
define('LFM_INVALID_PARAMS',        6);
define('LFM_INVALID_RESOURCE',      7);
define('LFM_TOKEN_ERROR',           8);
define('LFM_INVALID_SESSION',       9);
define('LFM_INVALID_APIKEY',       10);
define('LFM_SERVICE_OFFLINE',      11);
define('LFM_SUBSCRIPTION_ERROR',   12);
define('LFM_INVALID_SIGNATURE',    13);
define('LFM_TOKEN_UNAUTHORISED',   14);
define('LFM_SUBSCRIPTION_REQD',    18);
define('LFM_NOT_ENOUGH_CONTENT',   20);
define('LFM_NOT_ENOUGH_MEMBERS',   21);
define('LFM_NOT_ENOUGH_FANS',      22);
define('LFM_NOT_ENOUGH_NEIGHBORS', 23);
58 59 60

# Error descriptions as per API documentation
$error_text = array(
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	LFM_INVALID_SERVICE      => 'Invalid service - This service does not exist',
	LFM_INVALID_METHOD       => 'Invalid Method - No method with that name in this package',
	LFM_INVALID_TOKEN        => 'Invalid authentication token supplied',
	LFM_INVALID_FORMAT       => 'Invalid format - This service doesn\'t exist in that format',
	LFM_INVALID_PARAMS       => 'Invalid parameters - Your request is missing a required parameter',
	LFM_INVALID_RESOURCE     => 'Invalid resource specified',
	LFM_TOKEN_ERROR          => 'There was an error granting the request token. Please try again later',
	LFM_INVALID_SESSION      => 'Invalid session key - Please re-authenticate',
	LFM_INVALID_APIKEY       => 'Invalid API key - You must be granted a valid key by last.fm',
	LFM_SERVICE_OFFLINE      => 'Service Offline - This service is temporarily offline. Try again later.',
	LFM_SUBSCRIPTION_ERROR   => 'Subscription Error - The user needs to be subscribed in order to do that',
	LFM_INVALID_SIGNATURE    => 'Invalid method signature supplied',
	LFM_TOKEN_UNAUTHORISED   => 'This token has not yet been authorised',
	LFM_SUBSCRIPTION_REQD    => 'This user has no free radio plays left. Subscription required.',
	LFM_NOT_ENOUGH_CONTENT   => 'There is not enough content to play this station',
	LFM_NOT_ENOUGH_MEMBERS   => 'This group does not have enough members for radio',
	LFM_NOT_ENOUGH_FANS      => 'This artist does not have enough fans for radio',
	LFM_NOT_ENOUGH_NEIGHBORS => 'Thare are not enough neighbors for radio'
79 80 81 82
);

# Resolves method= parameters to handler functions
$method_map = array(
83 84 85 86 87 88 89
	'auth.gettoken'         => method_auth_getToken,
	'auth.getsession'       => method_auth_getSession,
	'auth.getmobilesession' => method_auth_getMobileSession,
	'artist.addtags'        => method_artist_addTags,
	'artist.getinfo'        => method_artist_getInfo,
	'artist.gettoptracks'   => method_artist_getTopTracks,
	'artist.gettoptags'     => method_artist_getTopTags,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
90
	'artist.gettopfans'     => method_artist_getTopFans,
91
	'artist.gettags'        => method_artist_getTags,
92 93 94
	'artist.getflattr'      => method_artist_getFlattr,
	'album.addtags'         => method_album_addTags,
	'album.gettoptags'      => method_album_getTopTags,
95
	'album.gettags'         => method_album_getTags,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
96
	'library.removescrobble'=> method_library_removeScrobble,
97
	'user.getinfo'          => method_user_getInfo,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
98
	'user.gettopartists'    => method_user_getTopArtists,
99 100 101
	'user.gettoptracks'     => method_user_getTopTracks,
	'user.getrecenttracks'  => method_user_getRecentTracks,
	'user.gettoptags'       => method_user_getTopTags,
102 103
	'user.getpersonaltags'  => method_user_getPersonalTags,
	'user.gettaginfo'       => method_user_getTagInfo,
104 105
	'user.getlovedtracks'   => method_user_getLovedTracks,
	'user.getbannedtracks'  => method_user_getBannedTracks,
106
	'user.getneighbours'    => method_user_getNeighbours,
107 108
	'radio.tune'            => method_radio_tune,
	'radio.getplaylist'     => method_radio_getPlaylist,
109 110 111 112 113
	'tag.gettoptags'        => method_tag_getTopTags,
	'tag.gettopartists'     => method_tag_getTopArtists,
	'tag.gettopalbums'      => method_tag_getTopAlbums,
	'tag.gettoptracks'      => method_tag_getTopTracks,
	'tag.getinfo'           => method_tag_getInfo,
114
	'track.addtags'         => method_track_addTags,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
115
	'track.removetag'       => method_track_removeTag,
116
	'track.gettoptags'      => method_track_getTopTags,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
117
	'track.gettopfans'      => method_track_getTopFans,
118 119 120 121 122
	'track.gettags'         => method_track_getTags,
	'track.ban'             => method_track_ban,
	'track.love'            => method_track_love,
	'track.unlove'          => method_track_unlove,
	'track.unban'           => method_track_unban,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
123
	'track.updatenowplaying' => method_track_updateNowPlaying,
Jonas Haraldsson's avatar
Jonas Haraldsson committed
124
	'track.scrobble'        => method_track_scrobble,
125
);
126

127
/**
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
 * user.gettopartists : Get the top artists for a user.
 *
 * ###Description
 * Get the top artists for a user, ordered by play count.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user
 * * **limit** (optional)		: How many items to return. Defaults to 50.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **period** (optional)		: The time period to get top artists for - overall|7day|1month|3month|6month|12month. Default is overall.
 * * **streamable** (optional)	: Only show streamable artists. Default is false.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
145
 */
146
function method_user_getTopArtists() {
147
	if (!isset($_REQUEST['user'])) {
148 149 150 151 152 153 154 155 156
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 50);
	$page = get_with_default('page', 1);
	$period = get_with_default('period', 'overall');
	$streamable = get_with_default('streamable', 0);
	$cache = 600;

157
	$xml = UserXML::getTopArtists($_REQUEST['user'], $limit, $streamable, $page, $period, $cache);
158 159 160 161

	respond($xml);
}

162 163 164 165 166 167 168 169 170 171
/**
 * user.getrecenttracks : Get recently played tracks by a user.
 *
 * ###Description
 * Get recently played tracks by a user, ordered by time.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **page** (optional)		: The page to show. Defaults to 1.
172 173
 * * **from** (optional)		: Show items newer than this timestamp (UNIX format)
 * * **to** (optional)			: Show items older than this timestamp (UNIX format)
174 175 176 177 178 179 180
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
181
function method_user_getRecentTracks() {
182
	if (!isset($_REQUEST['user'])) {
183
		report_failure(LFM_INVALID_PARAMS);
184 185
	}

186 187
	$limit = get_with_default('limit', 50);
	$page = get_with_default('page', 1);
188

189
	$xml = UserXML::getRecentTracks($_REQUEST['user'], $limit, $page, $_REQUEST['from'], $_REQUEST['to']);
190
	respond($xml);
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/**
 * user.gettoptags : Get the top tags for a user.
 *
 * ###Description
 * Get the top tags for a user, ordered by tag count.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
209
function method_user_getTopTags() {
210
	if (!isset($_REQUEST['user'])) {
211
		report_failure(LFM_INVALID_PARAMS);
212 213
	}

214 215 216 217
	$limit = get_with_default('limit', 50);

	$cache = 600;

218
	$xml = UserXML::getTopTags($_REQUEST['user'], $limit, $cache);
219 220 221
	respond($xml);
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/**
 * user.getpersonaltags : Get a list of items tagged by a user with a specific tag.
 *
 * ###Description
 * Get a list of items (artists, albums or tracks)
 * that has been tagged by a user with a specific tag
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **tag** (required)			: Name of the tag.
 * * **taggingtype** (required)	: Type of tag - artist|album|track.
 * * **limit** (optional)		: How many items to show. Defaults to 10.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
242
function method_user_getPersonalTags() {
Jonas Haraldsson's avatar
Jonas Haraldsson committed
243
	if(!isset($_REQUEST['user']) || !isset($_REQUEST['tag']) || !isset($_REQUEST['taggingtype'])) {
244 245 246 247 248 249 250 251 252
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 10);
	$page = get_with_default('page', 1);

	$streamable = False;
	$cache = 600;

253
	$xml = UserXML::getPersonalTags($_REQUEST['user'], $_REQUEST['tag'], $_REQUEST['taggingtype'], $limit, $page, $cache, $streamable);
254
	respond($xml);
255 256
}

257 258 259 260 261 262 263 264 265
/**
 * user.gettaginfo : Get info about a user's tag.
 *
 * ###Description
 * Get info about a user's tag.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **tag** (required)			: Name of the tag.
266
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
267 268 269 270 271 272
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
273
function method_user_getTagInfo() {
Jonas Haraldsson's avatar
Jonas Haraldsson committed
274
	if(!isset($_REQUEST['user']) || !isset($_REQUEST['tag'])) {
275 276 277 278 279
		report_failure(LFM_INVALID_PARAMS);
	}

	$cache = 600;

280
	$xml = UserXML::getTagInfo($_REQUEST['user'], $_REQUEST['tag'], $cache);
281 282
	respond($xml);
}
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/**
 * user.gettoptracks : Get the top tracks for a user.
 *
 * ###Description
 * Get the top tracks for a user, ordered by play count.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 10.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **period** (optional)		: The time period to get top artists for - overall|7day|1month|3month|6month|12month. Default is overall.
 * * **streamable** (optional)	: Only show streamable tracks. Default is false.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
303
function method_user_getTopTracks() {
304
	if (!isset($_REQUEST['user'])) {
305
		report_failure(LFM_INVALID_PARAMS);
306
	}
Jonas Haraldsson's avatar
Jonas Haraldsson committed
307 308 309 310 311
	$limit = get_with_default('limit', 10);
	$streamable = get_with_default('streamable', False);
	$page = get_with_default('page', 1);
	$period = get_with_default('period', 'overall');
	$cache = 600;
312

313
	$xml = UserXML::getTopTracks($_REQUEST['user'], $limit, $streamable, $page, $period, $cache);
314
	respond($xml);
315 316
}

317 318 319 320 321 322 323
/** user.getinfo : Get information about a user
 *
 * ###Description
 * Get information (such as biography and playcount) about a user.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
324
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
325 326 327 328 329 330
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
331
function method_user_getInfo() {
332
	if (!isset($_REQUEST['user'])) {
333
		report_failure(LFM_INVALID_PARAMS);
334
	}
335

336
	$xml = UserXML::getInfo($_REQUEST['user']);
337
	respond($xml);
338 339
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/**
 * user.getlovedtracks : Get the loved tracks for a user.
 *
 * ###Description
 * Get the loved tracks for a user, ordered by time.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **streamable** (optional)	: Only show streamable tracks. Default is false.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
358
function method_user_getLovedTracks() {
359
	if (!isset($_REQUEST['user'])) {
360 361 362
		report_failure(LFM_INVALID_PARAMS);
	}

363
	$user = $_REQUEST['user'];
364

365 366
	$limit = get_with_default('limit', 50);
	$page = get_with_default('page', 1);
367 368
	$streamable = get_with_default('streamable', False);
	$cache = 600;
369

370
	$xml = UserXML::getLovedTracks($user, $limit, $page, $streamable, $cache);
371
	respond($xml);
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/**
 * user.getbannedtracks : Get the banned tracks for a user.
 *
 * ###Description
 * Get the banned tracks for a user, ordered by time.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
391
function method_user_getBannedTracks() {
392
	if (!isset($_REQUEST['user'])) {
393 394 395
		report_failure(LFM_INVALID_PARAMS);
	}

396
	$user = $_REQUEST['user'];
397
	$limit = get_with_default('limit', 50);
398
	$page = get_with_default('page', 1);
399

400
	$xml = UserXML::getBannedTracks($user, $limit, $page);
401 402 403
	respond($xml);
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/**
 * user.getneighbours : Get the neighbours for a user.
 *
 * ###Description
 * Get the neighbours for a user, ordered by relevance.
 *
 * ###Parameters
 * * **user** (required)		: Name of the user.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage User
 * @api
 */
420
function method_user_getNeighbours() {
421
	if (!isset($_REQUEST['user'])) {
422 423 424
		report_failure(LFM_INVALID_PARAMS);
	}

425
	$user = $_REQUEST['user'];
426
	$limit = get_with_default('limit', 50);
427

428 429 430
	$xml = UserXML::getNeighbours($user, $limit);
	respond($xml);
}
431 432

/**
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 * artist.addtags : Add tags to an artist.
 *
 * ###Description
 * Add tags to an artist using a comma-separated list of tags.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the album's artist.
 * * **tags** (required)		: Comma-separated list of tags.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Artist
 * @api
453
 */
454 455 456 457
function method_artist_addTags() {
	if (!isset($_POST['artist']) || !isset($_POST['tags'])) {
		report_failure(LFM_INVALID_PARAMS);
	}
458

459
	$userid = get_userid();
460
	$xml = TrackXML::addTags($userid, $_POST['artist'], '', '', $_POST['tags']);
461
	respond($xml);
462 463
}

464 465 466
/** artist.getinfo : Get information about an artist.
 *
 * ###Description
467
 * Get information about an artist.
468 469 470
 *
 * ###Parameters
 * * **artist** (required)		: Name of the artist.
471
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
472 473 474 475 476 477
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 */
478
function method_artist_getInfo() {
479
	if (!isset($_REQUEST['artist'])) {
480
		report_failure(LFM_INVALID_PARAMS);
481
	}
482

483
	$xml = ArtistXML::getInfo($_REQUEST['artist']);
484
	respond($xml);
485 486
}

487
/**
Jonas Haraldsson's avatar
Jonas Haraldsson committed
488
 * artist.gettoptracks : Get the top tracks for an artist.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
 *
 * ###Description
 * Get the top tracks for an artist, ordered by play count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the artist.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **page** (optional)		: The page to show. Defaults to 1.
 * * **streamable** (optional)	: Only show streamable tracks. Default is false.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 */
505
function method_artist_getTopTracks() {
506
	if (!isset($_REQUEST['artist'])) {
507
		report_failure(LFM_INVALID_PARAMS);
508
	}
509 510 511 512
	$limit = get_with_default('limit', 50);
	$page = get_with_default('page', 1);
	$streamable = get_with_default('streamable', False);
	$cache = 600;
513

514
	$xml = ArtistXML::getTopTracks($_REQUEST['artist'], $limit, $streamable, $page, $cache);
515
	respond($xml);
516
}
517

518 519 520 521
/**
 * artist.gettoptags : Get the top tags for an artist.
 *
 * ###Description
Jonas Haraldsson's avatar
typo  
Jonas Haraldsson committed
522
 * Get the top tags used for an artist, ordered by tag count.
523 524
 *
 * ###Parameters
Jonas Haraldsson's avatar
Jonas Haraldsson committed
525
 * * **artist** (required)		: Name of the artist.
526 527 528 529 530 531 532 533
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 */
534
function method_artist_getTopTags() {
535
	if (!isset($_REQUEST['artist'])) {
536 537
		report_failure(LFM_INVALID_PARAMS);
	}
538

539 540 541 542
	$limit = get_with_default('limit', 50);

	$cache = 600;	

543
	$xml = ArtistXML::getTopTags($_REQUEST['artist'], $limit, $cache);
544 545 546
	respond($xml);
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/**
 * artist.gettopfans : Get the top fans for an artist.
 *
 * ###Description
 * Get the top fans used for an artist, ordered by play count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the artist.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 */
function method_artist_getTopFans() {
	if (!isset($_REQUEST['artist'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 50);

	$cache = 600;	

	$xml = ArtistXML::getTopFans($_REQUEST['artist'], $limit, $cache);
	respond($xml);
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/**
 * artist.gettags : Get a user's top tags for an artist.
 *
 * ###Description
 * Get the top tags used for an artist, filtered by user name and ordered by tag count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the artist.
 * * **sk** (required)			: Session key.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 * @todo Only require sk if no user specified, see http://www.last.fm/api/show/artist.getTags.
 */
597 598 599 600 601 602 603 604 605 606 607
function method_artist_getTags() {
	if (!isset($_REQUEST['artist'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 50);

	$userid = get_userid();
	$cache = 600;

	$xml = ArtistXML::getTags($_REQUEST['artist'], $userid, $limit, $cache);
608
	respond($xml);
609
}
610

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
/**
 * artist.getflattr : Get the Flattr id of an artist.
 *
 * ###Description
 * Get the Flattr id of an artist.
 *
 * ###Parameters
 * * **artist**					: Name of the artist.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Artist
 * @api
 */
626
function method_artist_getFlattr() {
627
	if (!isset($_REQUEST['artist'])) {
628 629 630
		report_failure(LFM_INVALID_PARAMS);
	}

631
	$xml = ArtistXML::getFlattr($_REQUEST['artist']);
632 633
	respond($xml);
}
634 635

/**
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
 * album.addtags : Add tags to an album.
 *
 * ###Description
 * Add tags to an album using a comma-separated list of tags.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the album's artist.
 * * **album** (required)		: Name of the album.
 * * **tags** (required)		: Comma-separated list of tags.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Album
 * @api
657
 */
658 659 660 661 662 663
function method_album_addTags() {
	if (!isset($_POST['artist']) || !isset($_POST['album']) || !isset($_POST['tags'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
664
	$xml = TrackXML::addTags($userid, $_POST['artist'], $_POST['album'], '', $_POST['tags']);
665
	respond($xml);
666 667
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
/**
 * album.gettoptags : Get the top tags for an album.
 *
 * ###Description
 * Get the top tags used for an album, ordered by tag count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the album's artist.
 * * **album** (required)		: Name of the album.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Album
 * @api
 */
685
function method_album_getTopTags() {
686
	if (!isset($_REQUEST['artist']) || !isset($_REQUEST['album'])) {
687 688
		report_failure(LFM_INVALID_PARAMS);
	}
689

690 691 692 693
	$limit = get_with_default('limit', 50);

	$cache = 600;

694
	$xml = AlbumXML::getTopTags($_REQUEST['artist'], $_REQUEST['album'], $limit, $cache);
695
	respond($xml);
696 697
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/**
 * album.gettags : Get a user's top tags for an album.
 *
 * ###Description
 * Get the top tags used for an album, filtered by user name and ordered by tag count.
 *
 * ###Parameters
 *
 * * **artist** (required)		: Name of the album's artist.
 * * **album** (required)		: Name of the album.
 * * **sk** (required)			: Session key.
 * * **limit** (optional)		: How many items to show. Defaults to 10.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**
 * - - -
 *
 * @package Webservice
 * @subpackage Album
 * @api
 * @todo Only require sk if no user specified, see http://www.last.fm/api/show/album.getTags.
 */
721
function method_album_getTags() {
722
	if (!isset($_REQUEST['artist']) || !isset($_REQUEST['album'])) {
723 724 725 726 727 728 729 730
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 10);

	$userid = get_userid();
	$cache = 600;

731
	$xml = AlbumXML::getTags($_REQUEST['artist'], $_REQUEST['album'], $userid, $limit, $cache);
732 733
	respond($xml);
}
734

735
/**
736 737 738 739 740 741 742 743 744
 * auth.gettoken : Get a request token
 *
 * ###Description
 *
 * - - -
 * @todo Documentation
 * @package Webservice
 * @subpackage Auth
 * @api
745
 */
746
function method_auth_getToken() {
747

748
	$token = Server::getAuthToken();
749

750
	if(!$token) {
751
		report_failure(LFM_SERVICE_OFFLINE);
clint's avatar
clint committed
752
	}
753

754
	$xml = simplexml_load_string('<lfm status="ok"><token>' . $token . '</token></lfm>');
755
	respond($xml);
756
}
757 758 759 760 761 762 763 764 765 766
/**
 * auth.getmobilesession : Create a webservice session
 *
 * ###Description
 * Create a webservice session and a session key required for authenticating with other webservice methods,
 * the formula for the authentication token required is:
 *
 *     md5(username+md5(password))
 *
 * ###Parameters
767
 * * **username** (required)	: Name of the user
768 769 770 771
 * * **authtoken** (required)	: Authentication token
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
772
 * @todo parameter 'authtoken' is deprecated on last.fm's version, uses parameter 'password' with HTTPS and POST instead
773
 * @todo make XML response better (use xml_response)
774 775 776 777
 * @package Webservice
 * @subpackage Auth
 * @api
 */
778
function method_auth_getMobileSession() {
clint's avatar
clint committed
779
	global $adodb;
780

781 782 783
	$_REQUEST_lower = array_change_key_case($_REQUEST, CASE_LOWER);

	if (!isset($_REQUEST_lower['authtoken']) || !isset($_REQUEST['username'])) {
784
		report_failure(LFM_INVALID_PARAMS);
785
	}
786

787
	// Check for a token that is bound to a user
clint's avatar
clint committed
788
	try {
789
		$result = $adodb->GetRow('SELECT username, lower(username) AS lc_username, password FROM Users WHERE '
790
			. 'lower(username) = lower(' . $adodb->qstr($_REQUEST['username']) . ')');
791
	} catch (Exception $e) {
792
		report_failure(LFM_SERVICE_OFFLINE);
793
	}
794 795 796 797
	if (is_null($result)) {
		report_failure(LFM_INVALID_TOKEN);
	}

Mike Sheldon's avatar
Mike Sheldon committed
798 799 800
	$username = $result['username'];
	$lc_username = $result['lc_username'];
	$password = $result['password'];
801
	if (md5($lc_username . $password) != $_REQUEST_lower['authtoken']) {
802
		report_failure(LFM_INVALID_TOKEN);
803
	}
804 805 806 807 808

	$key = md5(time() . rand());
	$session = md5(time() . rand());

	// Update the Auth record with the new session key
clint's avatar
clint committed
809
	try {
810 811 812 813 814 815 816
		$result = $adodb->Execute('INSERT INTO Auth (token, sk, expires, username) '
			. 'VALUES ('
			. $adodb->qstr($key) . ', '
			. $adodb->qstr($session) . ', '
			. (int)(time() + 3600) . ', '
			. $adodb->qstr($username)
			. ')');
817
	} catch (Exception $e) {
818
		report_failure(LFM_SERVICE_OFFLINE);
clint's avatar
clint committed
819
	}
820

821
	if ($_REQUEST['format'] == 'json') {
822 823 824
		$json_data = array('session' => array('name' => $username, 'key' => $session, 'subscriber' => 0));
		json_response(json_encode($json_data));
	} else {
825 826
		header('Content-Type: text/xml');
		print("<?xml version=\"1.0\"?>\n");
827 828 829 830 831 832 833 834
		print("<lfm status=\"ok\">\n");
		print("	<session>\n");
		print("		<name>{$username}</name>\n");
		print("		<key>{$session}</key>\n");
		print("		<subscriber>0</subscriber>\n");
		print("	</session>\n");
		print("</lfm>");
	}
835 836
}

837 838 839 840 841 842 843 844 845 846 847
/**
 * auth.getsession : Create a webservice session.
 *
 * ###Description
 *
 *
 * ###Parameters
 * * **token** (required)		: Token
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 * @todo Documentation
848
 * @todo make XML response better (use xml_response)
849 850 851 852
 * @package Webservice
 * @subpackage Auth
 * @api
 */
853
function method_auth_getSession() {
clint's avatar
clint committed
854
	global $adodb;
855

856
	if (!isset($_REQUEST['token'])) {
857
		report_failure(LFM_INVALID_TOKEN);
858
	}
859

860
	// Check for a token that (1) is bound to a user, and (2) is not bound to a session
clint's avatar
clint committed
861
	try {
Mike Sheldon's avatar
Mike Sheldon committed
862
		$username = $adodb->GetOne('SELECT username FROM Auth WHERE '
863
			. 'token = ' . $adodb->qstr($_REQUEST['token']) . ' AND '
Mike Sheldon's avatar
Mike Sheldon committed
864
			. 'username IS NOT NULL AND sk IS NULL');
865
	} catch (Exception $e) {
866
		report_failure(LFM_SERVICE_OFFLINE);
clint's avatar
clint committed
867 868
	}
	if (!$username) {
869
		report_failure(LFM_TOKEN_UNAUTHORISED);
clint's avatar
clint committed
870
	}
871

872
	$session = md5(time() . rand());
873

874
	// Update the Auth record with the new session key
clint's avatar
clint committed
875
	try {
Mike Sheldon's avatar
Mike Sheldon committed
876 877
		$result = $adodb->Execute('UPDATE Auth SET '
			. 'sk = ' . $adodb->qstr($session) . ' WHERE '
878
			. 'token = ' . $adodb->qstr($_REQUEST['token']));
879
	} catch (Exception $e) {
880
		report_failure(LFM_SERVICE_OFFLINE);
clint's avatar
clint committed
881
	}
882

883
	if ($_REQUEST['format'] == 'json') {
884
		$json_data = array('session' => array('name' => $username, 'key' => $session, 'subscriber' => 0));
885
		json_response(json_encode($json_data));
886 887 888 889 890 891 892 893 894
	} else {
		print("<lfm status=\"ok\">\n");
		print("	<session>\n");
		print("		<name>{$username}</name>\n");
		print("		<key>{$session}</key>\n");
		print("		<subscriber>0</subscriber>\n");
		print("	</session>\n");
		print("</lfm>");
	}
895 896
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
897 898 899 900 901 902 903
/**
 * library.removescrobble : Remove a scrobble
 *
 * ###Description
 * Remove a scrobble from user's library
 *
 * #Parameters
Jonas Haraldsson's avatar
Jonas Haraldsson committed
904 905 906 907
 * * **timestamp** (required)		: Timestamp in Unix time.
 * * **artist** (required)		: Artist name.
 * * **track** (required)			: Track name.
 * * **sk** (required)			: Session key.
Jonas Haraldsson's avatar
docs  
Jonas Haraldsson committed
908
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
 *
 * #Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST
 * - - -
 *
 * @package Webservice
 * @subpackage Library
 * @api
 */
function method_library_removeScrobble() {
	if (!isset($_POST['artist']) || !isset($_POST['track']) || !isset($_POST['timestamp'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
	$xml = LibraryXML::removeScrobble($userid, $_POST['timestamp'], $_POST['artist'], $_POST['track']);
	respond($xml);
}

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
/**
 * radio.tune : Tune in to a radio station
 *
 * ###Description
 * Tune in to a radio station
 *
 * ###Parameters
 * * **station** (required)		: Station URL
 * * **sk** (required)			: Session key
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
947
 * @todo make XML response better (use xml_response)
948 949 950 951
 * @package Webservice
 * @subpackage Radio
 * @api
 */
clint's avatar
clint committed
952 953 954
function method_radio_tune() {
	global $adodb;

955
	if (!isset($_POST['station'])) {
clint's avatar
clint committed
956
		report_failure(LFM_INVALID_PARAMS);
957
	}
clint's avatar
clint committed
958

959
	if (!isset($_POST['sk'])) {
clint's avatar
clint committed
960
		report_failure(LFM_INVALID_PARAMS);
961
	}
clint's avatar
clint committed
962 963 964

	try {
	$username = $adodb->GetOne('SELECT username FROM Auth WHERE '
Mike Sheldon's avatar
Mike Sheldon committed
965 966
		. 'sk = ' . $adodb->qstr($_POST['sk']) . ' AND '
		. 'username IS NOT NULL');
967
	} catch (Exception $e) {
clint's avatar
clint committed
968 969 970 971 972 973
		report_failure(LFM_SERVICE_OFFLINE);
	}
	if (!$username) {
		report_failure(LFM_INVALID_TOKEN);
	}

974
	Server::getRadioSession($_POST['station'], $username, $_POST['sk']);
975 976 977
	$stationtype = 'globaltag';
	$stationname = radio_title_from_url($_POST['station']);
	$stationurl = 'http://libre.fm';
clint's avatar
clint committed
978

979
	if ($_REQUEST['format'] == 'json') {
980
		header('Content-Type: application/json; charset=utf-8');
981 982 983
		$json_data = array('station' => array('type' => $stationtype, 'name' => $stationname, 'url' => $stationurl, 'supportsdiscovery' => 1));
		print(json_encode($json_data));
	} else {
984
		header('Content-Type: text/xml');
985 986 987 988 989 990 991 992
		print("<lfm status=\"ok\">\n");
		print("	<station>\n");
		print("		<type>" . $stationtype . "</type>\n");
		print("		<name>" .$stationname . "</name>\n");
		print("		<url>" . $stationurl . "</url>\n");
		print("		<supportsdiscovery>1</supportsdiscovery>\n");
		print("	</station>\n");
		print("</lfm>");
993
	}
clint's avatar
clint committed
994 995
}

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
/**
 * radio.getplaylist : Get playlist from a tuned station
 *
 * ###Description
 * Get playlist from a tuned station
 *
 * ###Parameters
 * * **sk** (required)			: Session key
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 * - - -
 *
 * @todo Any errors should be in whatever format specified, currently plaintext
 * @package Webservice
 * @subpackage Radio
 * @api
 */
clint's avatar
clint committed
1015 1016 1017
function method_radio_getPlaylist() {
	global $adodb;

1018
	if (!isset($_REQUEST['sk'])) {
clint's avatar
clint committed
1019
		report_failure(LFM_INVALID_PARAMS);
1020
	}
clint's avatar
clint committed
1021

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	//we return a JSPF (http://wiki.xiph.org/JSPF_Draft) playlist if format=json
	//any errors will still be plaintext (make_playlist can return BADSESSION or FAILED)
	if ($_REQUEST['format'] == 'json') {
		list($title, $res) = make_playlist($_REQUEST['sk'], false, 'json');

		$tracks = array();
		foreach($res as &$row) {
			$track = array(
				'location' => $row['location'],
				'title' => $row['title'],
				'identifier' => $row['id'],
				'album' => $row['album'],
				'creator' => $row['creator'],
				'duration' => $row['duration'],
				'image' => $row['image'],
				'extension' => array(
					'http://alpha.libre.fm/' => array(
						'trackauth' => null,
						'albumid' => null,
						'artistid' => null,
						'recording' => null,
						'artistpage' => $row['artisturl'],
						'albumpage' => $row['albumurl'],
						'trackpage' => $row['trackurl'],
						'buyTrackURL' => null,
						'buyAlbumURL' => null,
						'freeTrackURL' => $row['downloadurl']
					)
				)
			);
			$tracks[] = $track;
		}

		$playlist = array(
			'playlist' => array(
				'title' => $title,
				'creator' => 'libre.fm',
				'date' => date('c'),
				'link' => array('http://www.last.fm/expiry' => 3600),
				'track' => $tracks)
			);

1064
		header('Content-Type: application/json; charset=utf-8');
1065 1066 1067 1068 1069
		print(json_encode($playlist));
	}else{
		//we return XSPF playlists by default
		make_playlist($_REQUEST['sk']);
	}
clint's avatar
clint committed
1070 1071
}

1072
/**
1073 1074 1075 1076 1077 1078
 * track.addtags : Add tags to a track.
 *
 * ###Description
 * Add tags to a track using a comma-separated list of tags.
 * 
 * ###Parameters
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1079 1080
 * * **artist** (required)		: Name of the track's artist.
 * * **track** (required)		: Name of the track.
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
 * * **tags** (required)		: Comma-separated list of tags.
 * * **sk** (required)			: Session key.
 * * **album** (optional)		: Name of the tracks's album.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
1095
 */
1096 1097
function method_track_addTags() {
	if (!isset($_POST['artist']) || !isset($_POST['track']) || !isset($_POST['tags'])) {
Mike Sheldon's avatar
Mike Sheldon committed
1098
		report_failure(LFM_INVALID_PARAMS);
1099 1100 1101
	}

	$userid = get_userid();
1102
	$xml = TrackXML::addTags($userid, $_POST['artist'], $_POST['album'], $_POST['track'], $_POST['tags']);
1103
	respond($xml);
1104 1105
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
/**
 * track.removetag : Remove tag from a track.
 *
 * ###Description
 * Remove a tag from a track.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the track's artist.
 * * **track** (required)		: Name of the track.
 * * **tag** (required)			: Name of tag.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
function method_track_removeTag() {
	if (!isset($_POST['artist']) || !isset($_POST['track']) || !isset($_POST['tag'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
	$xml = TrackXML::removeTag($userid, $_POST['artist'], $_POST['track'], $_POST['tag']);
	respond($xml);
}


1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
/**
 * track.gettoptags : Get the top tags for a track.
 *
 * ###Description
 * Get the top tags used for a track, ordered by tag count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the track's artist.
 * * **track** (required)		: Name of the track.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
1157
function method_track_getTopTags() {
1158
	if (!isset($_REQUEST['artist']) || !isset($_REQUEST['track'])) {
1159
		report_failure(LFM_INVALID_PARAMS);
1160 1161
	}

1162 1163 1164 1165
	$limit = get_with_default('limit', 50);

	$cache = 600;

1166
	$xml = TrackXML::getTopTags($_REQUEST['artist'], $_REQUEST['track'], $limit, $cache);
1167
	respond($xml);
1168 1169
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
/**
 * track.gettopfans : Get the top fans for a track.
 *
 * ###Description
 * Get the top fans for a track, ordered by play count.
 *
 * ###Parameters
 * * **artist** (required)		: Name of the artist.
 * * **track** (required)		: Name of the track.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 * - - -
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
function method_track_getTopFans() {
	if (!isset($_REQUEST['artist']) OR !isset($_REQUEST['track'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$limit = get_with_default('limit', 50);

	$cache = 600;	

	$xml = TrackXML::getTopFans($_REQUEST['track'], $_REQUEST['artist'], $limit, $cache);
	respond($xml);
}

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
/**
 * track.gettags : Get a user's top tags for a track.
 *
 * ###Description
 * Get the top tags used for a track, filtered by user name and ordered by tag count.
 *
 * ###Parameters
 *
 * * **artist** (required)		: Name of the track's artist.
 * * **track** (required)		: Name of the track.
 * * **sk** (required)			: Session key.
 * * **limit** (optional)		: How many items to show. Defaults to 50.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**
 * - - -
 *
 * @package Webservice
 * @subpackage Track
 * @api
 * @todo Only require sk if no user specified, see http://www.last.fm/api/show/track.getTags.
 */
1223
function method_track_getTags() {
1224
	if (!isset($_REQUEST['artist']) || !isset($_REQUEST['track'])) {
1225 1226 1227
		report_failure(LFM_INVALID_PARAMS);
	}

1228 1229 1230 1231
	$limit = get_with_default('limit', 50);

	$cache = 600;
	
1232
	$userid = get_userid();
1233
	$xml = TrackXML::getTags($_REQUEST['artist'], $_REQUEST['track'], $userid, $limit, $cache);
1234
	respond($xml);
1235 1236
}

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
/**
 * track.ban : Add a track to the user's banned tracks list.
 *
 * ###Description
 * Add a track to the user's banned tracks list.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the tracks's artist.
 * * **track** (required)		: Name of the track.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
1259
function method_track_ban() {
1260
	if (!isset($_POST['artist']) || !isset($_POST['track'])) {
1261 1262 1263 1264
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
1265
	$xml = TrackXML::ban($_POST['artist'], $_POST['track'], $userid);
1266
	respond($xml);
1267 1268
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
/**
 * track.love : Add a track to the user's loved tracks list.
 *
 * ###Description
 * Add a track to the user's loved tracks list.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the tracks's artist.
 * * **track** (required)		: Name of the track.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
1291 1292 1293 1294 1295 1296
function method_track_love() {
	if (!isset($_POST['artist']) || !isset($_POST['track'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
1297
	$xml = TrackXML::love($_POST['artist'], $_POST['track'], $userid);
1298
	respond($xml);
1299
}
1300

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
/**
 * track.unlove : Remove a track from the user's loved tracks list.
 *
 * ###Description
 * Remove a track from the user's loved tracks list.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the tracks's artist.
 * * **track** (required)		: Name of the track.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
function method_track_unlove() {
	if (!isset($_POST['artist']) || !isset($_POST['track'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
	$xml = TrackXML::unlove($_POST['artist'], $_POST['track'], $userid);
	respond($xml);
}

1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
/**
 * track.unban : Remove a track from the user's banned tracks list.
 *
 * ###Description
 * Remove a track from the user's banned tracks list.
 * 
 * ###Parameters
 * * **artist** (required)		: Name of the tracks's artist.
 * * **track** (required)		: Name of the track.
 * * **sk** (required)			: Session key.
 * * **format** (optional)		: Format of response, **xml** or **json**. Default is xml.
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
 * @package Webservice
 * @subpackage Track
 * @api
 */
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
function method_track_unban() {
	if (!isset($_POST['artist']) || !isset($_POST['track'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

	$userid = get_userid();
	$xml = TrackXML::unban($_POST['artist'], $_POST['track'], $userid);
	respond($xml);
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1365 1366 1367 1368
/**
 * track.updatenowplaying : Submits the user's currently playing track.
 *
 * ###Description
1369
 * Submits the user's currently playing track.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1370 1371
 * 
 * ###Parameters
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1372 1373 1374
 * * **artist** (required)      : Artist name.
 * * **track** (required)       : Track name.
 * * **sk** (required)          : Session key.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1375
 * * **album** (optional)		: Album name.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1376
 * * **tracknumber** (optional) : Track's number on the album.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1377 1378 1379
 * * **context** (optional)     : TODO
 * * **mbid** (optional)        : Track's musicbrainz ID.
 * * **duration** (optional)    : Length of the track in seconds.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1380 1381
 * * **albumartist** (optional) : Album's artist.
 * * **api_key** (optional)     : Client API key.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1382
 * * **format** (optional)      : Format of response, **xml** or **json**. Default is xml.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1383 1384 1385 1386 1387 1388 1389
 *
 * ###Additional info
 * **This method requires authentication**.
 *
 * **HTTP request method** : POST.
 * - - - 
 *
1390
 * @todo context parameter not used
1391 1392
 * @todo tracknumber parameter not stored in db
 * @todo albumartist parameter not stored in db
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1393 1394 1395 1396 1397 1398 1399 1400 1401
 * @package Webservice
 * @subpackage Track
 * @api
 */
function method_track_updateNowPlaying() {
	if (!isset($_POST['artist']) || !isset($_POST['track'])) {
		report_failure(LFM_INVALID_PARAMS);
	}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1402 1403
	$_POST_lower = array_change_key_case($_POST, CASE_LOWER);

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1404 1405 1406 1407 1408
	$userid = get_userid();
	$xml = TrackXML::updateNowPlaying($userid,
		$_POST['artist'],
		$_POST['track'],
		$_POST['album'],
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1409
		$_POST_lower['tracknumber'],
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1410 1411 1412
		$_POST['context'],
		$_POST['mbid'],
		$_POST['duration'],
1413 1414 1415
		$_POST_lower['albumartist'],
		$_POST['api_key']
	);
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1416

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1417 1418 1419
	respond($xml);
}

Jonas Haraldsson's avatar
Jonas Haraldsson committed
1420 1421 1422 1423 1424
/**
 * track.scrobble : Submits a track for scrobbling.
 *
 * ###Description
 *
1425
 * Submits a track or a batch of tracks for scrobbling.
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1426 1427
 * 
 * ###Parameters
Jonas Haraldsson's avatar
Jonas Haraldsson committed
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
 * * **artist[i]** (required)       : Artist name.
 * * **track[i]** (required)        : Track name.
 * * **sk** (required)              : Session key.
 * * **timestamp[i]** (required)    : The time the track started playing (in UNIX time).
 * * **album[i]** (optional)        : Album name.
 * * **context[i]** (optional)      : TODO
 * * **streamid[i]** (optional)     : TODO
 * * **chosenbyuser[i]** (optional) : TODO
 * * **tracknumber[i]** (optional)  : Track's number on album.
 * * **mbid[i]** (optional)         : Track's Musicbrainz ID.
 * * **albumartist[i]** (optional)  : Album's artist.
 * * **duration[i]** (optional)     : Length of the track in seconds.