<?php

require '../../lastfmapi/lastfmapi.php';

if ( !empty($_GET['token']) ) {
	$vars = array(
		'apiKey' => '3f14a0378de1ef0daccba218fd194f45',
		'secret' => '1f3ff09b495b36502822d5a5c1d359cb',
		'token' => $_GET['token']
	);
	
	$auth = new lastfmApiAuth('getsession', $vars);

	$file = fopen('../auth.txt', 'w');
	$contents = $auth->apiKey."\n".$auth->secret."\n".$auth->username."\n".$auth->sessionKey."\n".$auth->subscriber;
	fwrite($file, $contents, strlen($contents));
	fclose($file);
	
	echo 'New key has been generated and saved to auth.txt<br /><br />';
	echo '<a href="'.$_SERVER['PHP_SELF'].'">Reload</a>';
}
else {
	$file = fopen('../auth.txt', 'r');
	$vars = array(
		'apiKey' => trim(fgets($file)),
		'secret' => trim(fgets($file)),
		'username' => trim(fgets($file)),
		'sessionKey' => trim(fgets($file)),
		'subscriber' => trim(fgets($file))
	);
	$auth = new lastfmApiAuth('setsession', $vars);
	
	echo '<b>API Key:</b> '.$auth->apiKey.'<br />';
	echo '<b>Secret:</b> '.$auth->secret.'<br />';
	echo '<b>Username:</b> '.$auth->username.'<br />';
	echo '<b>Session Key:</b> '.$auth->sessionKey.'<br />';
	echo '<b>Subscriber:</b> '.$auth->subscriber.'<br /><br />';

	echo '<a href="http://www.last.fm/api/auth/?api_key='.$auth->apiKey.'">Get New Key</a>';
}

?>
