We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 5317334a authored by elleo's avatar elleo

Fix more space indentation and quoting

parent 5e7f6f89
......@@ -24,46 +24,46 @@ require_once('../api/ArtistXML.php');
require_once('../api/UserXML.php');
# Error constants
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_SUBSCRIPTION_REQD", 18);
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_SUBSCRIPTION_REQD', 18);
# Error descriptions as per API documentation
$error_text = array(
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_SUBSCRIPTION_REQD => "This user has no free radio plays left. Subscription required."
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_SUBSCRIPTION_REQD => 'This user has no free radio plays left. Subscription required.'
);
# Resolves method= parameters to handler functions
$method_map = array(
"auth.gettoken" => method_auth_gettoken,
"auth.getsession" => method_auth_getsession,
"auth.getmobilesession" => method_auth_getmobilesession,
"artist.getinfo" => method_artist_getinfo,
"artist.gettoptracks" => method_artist_gettoptracks,
"user.getinfo" => method_user_getinfo,
"user.gettoptracks" => method_user_gettoptracks
'auth.gettoken' => method_auth_gettoken,
'auth.getsession' => method_auth_getsession,
'auth.getmobilesession' => method_auth_getmobilesession,
'artist.getinfo' => method_artist_getinfo,
'artist.gettoptracks' => method_artist_gettoptracks,
'user.getinfo' => method_user_getinfo,
'user.gettoptracks' => method_user_gettoptracks
);
function method_user_gettoptracks() {
......@@ -71,7 +71,7 @@ function method_user_gettoptracks() {
report_failure(LFM_INVALID_SIGNATURE);
}
header("Content-Type: text/xml");
header('Content-Type: text/xml');
print(XML::prettyXML(UserXML::getTopTracks($_GET['user'], $_GET['period'])));
}
......@@ -79,7 +79,7 @@ function method_user_getinfo() {
if (!isset($_GET['user'])) {
report_failure(LFM_INVALID_SIGNATURE);
}
header("Content-Type: text/xml");
header('Content-Type: text/xml');
print(XML::prettyXML(UserXML::getInfo($_GET['user'])));
}
......@@ -87,7 +87,7 @@ function method_artist_getinfo() {
if (!isset($_GET['artist'])) {
report_failure(LFM_INVALID_SIGNATURE);
}
header("Content-Type: text/xml");
header('Content-Type: text/xml');
print(XML::prettyXML(ArtistXML::getInfo($_GET['artist'])));
}
......@@ -95,7 +95,7 @@ function method_artist_gettoptracks() {
if (!isset($_GET['artist'])) {
report_failure(LFM_INVALID_SIGNATURE);
}
header("Content-Type: text/xml");
header('Content-Type: text/xml');
print(XML::prettyXML(ArtistXML::getTopTracks($_GET['artist'])));
}
......@@ -116,7 +116,7 @@ function method_auth_gettoken() {
report_failure(LFM_SERVICE_OFFLINE);
print("<lfm status=\"ok\">\n");
print(" <token>$key</token></lfm>");
print(" <token>{$key}</token></lfm>");
}
function method_auth_getmobilesession() {
......@@ -159,8 +159,8 @@ function method_auth_getmobilesession() {
print("<lfm status=\"ok\">\n");
print(" <session>\n");
print(" <name>$username</name>\n");
print(" <key>$session</key>\n");
print(" <name>{$username}</name>\n");
print(" <key>{$session}</key>\n");
print(" <subscriber>0</subscriber>\n");
print(" </session>\n");
print("</lfm>");
......@@ -196,8 +196,8 @@ function method_auth_getsession() {
print("<lfm status=\"ok\">\n");
print(" <session>\n");
print(" <name>$username</name>\n");
print(" <key>$session</key>\n");
print(" <name>{$username}</name>\n");
print(" <key>{$session}</key>\n");
print(" <subscriber>0</subscriber>\n");
print(" </session>\n");
print("</lfm>");
......@@ -215,7 +215,7 @@ function report_failure($code) {
global $error_text;
print("<lfm status=\"failed\">\n");
print(" <error code=\"$code\">".$error_text[$code]."</error></lfm>");
print(" <error code=\"{$code}\">".$error_text[$code]."</error></lfm>");
die();
}
......
......@@ -19,7 +19,7 @@
*/
require("../../config.php");
require('../../config.php');
require_once($install_path . '/database2.php');
require_once($install_path . '/templating.php');
require_once($install_path . '/data/sanitize.php');
......@@ -36,7 +36,7 @@ while (!$recordSet->EOF) {
$recordSet->MoveNext();
}
$smarty->assign("entries", $aEntries);
$smarty->assign('entries', $aEntries);
$smarty->display("mbid-mismatch-report.tpl");
$smarty->display('mbid-mismatch-report.tpl');
?>
......@@ -32,32 +32,32 @@ class ArtistXML {
* @param string $lang A 2 character ISO 639 alpha-2 code indicating the language to return the information in
* @return A SimpleXMLElement containing the artist's information
*/
public static function getInfo($artistName, $api_key=false, $mbid=false, $lang="en") {
public static function getInfo($artistName, $api_key=false, $mbid=false, $lang='en') {
// We assume $api_key is valid and set at this point
if (!isset($artistName) && !isset($mbid)) {
echo XML::error("failed", "7", "Invalid resource specified");
echo XML::error('failed', '7', 'Invalid resource specified');
return;
}
$artist = new Artist($artistName, $mbid);
if (PEAR::isError($artist)) {
return(XML::error("failed", "7", "Invalid resource specified"));
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement("<lfm status=\"ok\"></lfm>");
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$artistXml = $xml->addChild("artist", null);
$artistXml->addChild("name", $artist->name);
$artistXml->addChild("mbid", $artist->mbid);
$artistXml->addChild("url", $artist->getURL());
$artistXml->addChild("streamable", $artist->streamable);
$artistXml = $xml->addChild('artist', null);
$artistXml->addChild('name', $artist->name);
$artistXml->addChild('mbid', $artist->mbid);
$artistXml->addChild('url', $artist->getURL());
$artistXml->addChild('streamable', $artist->streamable);
$bio = $artistXml->addChild("bio", null);
$bio->addChild("published", $artist->bio_published);
$bio->addChild("summary", $artist->bio_summary);
$bio->addChild("content", $artist->bio_content);
$bio = $artistXml->addChild('bio', null);
$bio->addChild('published', $artist->bio_published);
$bio->addChild('summary', $artist->bio_summary);
$bio->addChild('content', $artist->bio_content);
return($xml);
}
......@@ -67,23 +67,23 @@ class ArtistXML {
$artist = new Artist($artistName);
if (PEAR::isError($artist)) {
return(XML::error("failed", "7", "Invalid resource specified"));
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement("<lfm status=\"ok\"></lfm>");
$root = $xml->addChild("toptracks", null);
$root->addAttribute("artist", $artist->name);
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('toptracks', null);
$root->addAttribute('artist', $artist->name);
$tracks = $artist->getTopTracks(50);
// Loop over every result and add as children to "toptracks".
for($i = 1; $i < count($tracks); $i++) {
$track = $root->addChild("track", null);
$track->addAttribute("rank", $i);
$track->addChild("name", $tracks[$i]->name);
$track->addChild("mbid", $tracks[$i]->mbid);
$track->addChild("playcount", $tracks[$i]->getPlayCount());
$track->addChild("listeners", $tracks[$i]->getListenerCount());
$track = $root->addChild('track', null);
$track->addAttribute('rank', $i);
$track->addChild('name', $tracks[$i]->name);
$track->addChild('mbid', $tracks[$i]->mbid);
$track->addChild('playcount', $tracks[$i]->getPlayCount());
$track->addChild('listeners', $tracks[$i]->getListenerCount());
}
return($xml);
......
......@@ -24,109 +24,109 @@ require_once('xml.php');
class UserXML {
public static function getInfo($username) {
$user = new User($username);
if (PEAR::isError($user)) {
return(XML::error("failed", "7", "Invalid resource specified"));
}
$xml = new SimpleXMLElement("<lfm status=\"ok\"></lfm>");
$user_node = $xml->addChild("user", null);
$user_node->addChild("name", $user->name);
$user_node->addChild("email", $user->email);
$user_node->addChild("homepage", $user->homepage);
$user_node->addChild("location", $user->location);
$user_node->addChild("bio", $user->bio);
$user_node->addChild("profile_created", strftime("%c", $user->created));
if (isset($user->modified))
$user_node->addChild("profile_updated", strftime("%c", $user->modified));
return($xml);
}
public static function getTopTracks($username, $time) {
global $mdb2;
$timestamp;
if (!isset($time))
$time = "overall";
//TODO: Do better, this is too ugly :\
if (strcmp($time, "overall") == 0) {
$timestamp = 0;
} else if (strcmp($time, "3month") == 0) {
$timestamp = strtotime('-3 months');
} else if (strcmp($time, "6month") == 0) {
$timestamp = strtotime('-6 months');
} else if (strcmp($time, "9month") == 0) {
$timestamp = strtotime('-9 months');
} else if (strcmp($time, "12month") == 0) {
$timestamp = strtotime('-12 months');
} else {
return(XML::error("error", "13", "Invalid method signature supplied"));
}
$res = $mdb2->query("SELECT Track.*, Artist.mbid AS artmbid, COUNT(*) AS freq
FROM Track, Scrobbles,Artist
WHERE Scrobbles.username =" . $mdb2->quote($username, 'text') . "
AND Scrobbles.track = Track.name AND Scrobbles.time > " . $timestamp . " AND Track.artist = Artist.name
GROUP BY Track.name ORDER BY freq DESC LIMIT 20");
if (PEAR::isError($res) || !$res->numRows()) {
return(XML::error("failed", "7", "Invalid resource specified"));
public static function getInfo($username) {
$user = new User($username);
if (PEAR::isError($user)) {
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$user_node = $xml->addChild('user', null);
$user_node->addChild('name', $user->name);
$user_node->addChild('email', $user->email);
$user_node->addChild('homepage', $user->homepage);
$user_node->addChild('location', $user->location);
$user_node->addChild('bio', $user->bio);
$user_node->addChild('profile_created', strftime('%c', $user->created));
if (isset($user->modified))
$user_node->addChild('profile_updated', strftime('%c', $user->modified));
return($xml);
}
$xml = new SimpleXMLElement("<lfm status=\"ok\"></lfm>");
$root = $xml->addChild("toptracks", null);
$root->addAttribute("user", $username);
$root->addAttribute("type", $time);
$i = 1;
while(($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$track = $root->addChild("track", null);
$track->addAttribute("rank", $i);
$track->addChild("name", repamp($row['name']));
$track->addChild("playcount", $row['freq']);
$artist = $track->addChild("artist", repamp($row['artist']));
$artist->addChild("mbid", $row['artmbid']);
$i++;
}
return($xml);
}
public static function getRecentTracks($user, $limit) {
global $mdb2;
if (!isset($limit)) {
$limit = 10;
}
$res = $mdb2->query("SELECT Track . * , COUNT( * ) AS freq
FROM Track, Scrobbles
WHERE Scrobbles.username = " . $mdb2->quote($user, 'text') . "
AND Scrobbles.track = Track.name
GROUP BY Track.name
LIMIT 10");
if (PEAR::isError($res) || !$res->numRows()) {
return(XML::error("error", "7", "Invalid resource specified"));
}
$xml = new SimpleXMLElement("<lfm status=\"ok\"></lfm>");
$root = $xml->addChild("recenttracks", null);
$root->addAttribute("user", $user);
while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$track = $root->addChild("track", null);
$artist = $track->addChild("artist", repamp($row['artist']));
$artist->addAttribute("mbid", $row['artmbid']);
$track->addChild("name", repamp($row['name']));
public static function getTopTracks($username, $time) {
global $mdb2;
$timestamp;
if (!isset($time))
$time = 'overall';
//TODO: Do better, this is too ugly :\
if (strcmp($time, 'overall') == 0) {
$timestamp = 0;
} else if (strcmp($time, '3month') == 0) {
$timestamp = strtotime('-3 months');
} else if (strcmp($time, '6month') == 0) {
$timestamp = strtotime('-6 months');
} else if (strcmp($time, '9month') == 0) {
$timestamp = strtotime('-9 months');
} else if (strcmp($time, '12month') == 0) {
$timestamp = strtotime('-12 months');
} else {
return(XML::error('error', '13', 'Invalid method signature supplied'));
}
$res = $mdb2->query('SELECT Track.*, Artist.mbid AS artmbid, COUNT(*) AS freq
FROM Track, Scrobbles,Artist
WHERE Scrobbles.username = ' . $mdb2->quote($username, 'text') . '
AND Scrobbles.track = Track.name AND Scrobbles.time > ' . $timestamp . ' AND Track.artist = Artist.name
GROUP BY Track.name ORDER BY freq DESC LIMIT 20');
if (PEAR::isError($res) || !$res->numRows()) {
return(XML::error('failed', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('toptracks', null);
$root->addAttribute('user', $username);
$root->addAttribute('type', $time);
$i = 1;
while(($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$track = $root->addChild('track', null);
$track->addAttribute('rank', $i);
$track->addChild('name', repamp($row['name']));
$track->addChild('playcount', $row['freq']);
$artist = $track->addChild('artist', repamp($row['artist']));
$artist->addChild('mbid', $row['artmbid']);
$i++;
}
return($xml);
}
return($xml);
}
public static function getRecentTracks($user, $limit) {
global $mdb2;
if (!isset($limit)) {
$limit = 10;
}
$res = $mdb2->query('SELECT Track . * , COUNT( * ) AS freq
FROM Track, Scrobbles
WHERE Scrobbles.username = ' . $mdb2->quote($user, 'text') . '
AND Scrobbles.track = Track.name
GROUP BY Track.name
LIMIT 10');
if (PEAR::isError($res) || !$res->numRows()) {
return(XML::error('error', '7', 'Invalid resource specified'));
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('recenttracks', null);
$root->addAttribute('user', $user);
while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$track = $root->addChild('track', null);
$artist = $track->addChild('artist', repamp($row['artist']));
$artist->addAttribute('mbid', $row['artmbid']);
$track->addChild('name', repamp($row['name']));
}
return($xml);
}
}
?>
......@@ -34,9 +34,9 @@ $result = $mdb2->query('SELECT username FROM Users WHERE '
. 'username = ' . $mdb2->quote($_POST['username'], 'text') . ' AND '
. 'password = ' . $mdb2->quote(md5($_POST['password']), 'text'));
if (PEAR::isError($result))
die("Database error");
die('Database error');
if (!$result->numRows())
die("Authentication failed");
die('Authentication failed');
// Bind the user to the token and cancel the expiration rule
$result = $mdb2->query('UPDATE Auth SET '
......@@ -45,7 +45,7 @@ $result = $mdb2->query('UPDATE Auth SET '
. 'WHERE '
. 'token = ' . $mdb2->quote($_POST['token']));
if (PEAR::isError($result))
die("Database error");
die('Database error');
?>
<p>Thank you very much, <?php print($_POST['username']); ?>. Your authorization has been recorded.</p>
......@@ -64,9 +64,9 @@ $result = $mdb2->query('SELECT * FROM Auth WHERE '
. 'token = ' . $mdb2->quote($_GET['token'], 'text') . ' AND '
. 'username IS NULL');
if (PEAR::isError($result))
die("Database error");
die('Database error');
if (!$result->numRows())
die("Invalid token");
die('Invalid token');
?>
<form method="post" action="">
......
......@@ -19,25 +19,26 @@
*/
class XML {
public static function prettyXML($xml) {
$dom = new DOMDocument('1.0');
$dom->preserveWhitespace = false;
$dom->loadXML($xml->asXML());
$dom->formatOutput = true;
return($dom->saveXML());
}
public static function error($status, $errcode, $errtext) {
$xml = new SimpleXMLElement("<lfm></lfm>");
$xml->addAttribute("status", $status);
$error = $xml->addChild("error", $errtext);
$error->addAttribute("code", $errcode);
return($xml);
}
public static function prettyXML($xml) {
$dom = new DOMDocument('1.0');
$dom->preserveWhitespace = false;
$dom->loadXML($xml->asXML());
$dom->formatOutput = true;
return($dom->saveXML());
}
public static function error($status, $errcode, $errtext) {
$xml = new SimpleXMLElement('<lfm></lfm>');
$xml->addAttribute('status', $status);
$error = $xml->addChild('error', $errtext);
$error->addAttribute('code', $errcode);
return($xml);
}
}
function repamp($input) {
$input = str_replace('&', '&amp;', $input);
return($input);
$input = str_replace('&', '&amp;', $input);
return($input);
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment