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

Commit 2c5bbc01 authored by P. J. McDermott's avatar P. J. McDermott

Clean up code under nixtape/api/.

parent 6c8bb7f0
......@@ -5,47 +5,47 @@ require_once('xml.php');
class API {
function __construct() {
$this->api_errors = array(
2 => 'Invalid Service - This service does not exist',
3 => 'Invalid Method - No method with that name in this package',
4 => 'Authentication Failed - You do not have permissions to access the service',
5 => 'Invalid format - This service doesn\'t exist in that format',
6 => 'Invalid parameters - Your request is missing a required parameter',
7 => 'Invalid resource especified',
8 => '',
9 => 'Invalid session key',
10 => 'Invalid API key - You must be granted a valid key by libre.fm',
11 => 'Service Offline - This Service is temporarily offline. Try again later.',
12 => 'Subscription Error - The user needs to be subscribed in order to do that',
13 => 'Invalid method signature supplied',
14 => '',
15 => '',
16 => '',
17 => '',
18 => 'This user has no free radio plays left. Subscription required.',
19 => '',
20 => '',
);
}
function __construct() {
$this->api_errors = array(
2 => 'Invalid Service - This service does not exist',
3 => 'Invalid Method - No method with that name in this package',
4 => 'Authentication Failed - You do not have permissions to access the service',
5 => 'Invalid format - This service doesn\'t exist in that format',
6 => 'Invalid parameters - Your request is missing a required parameter',
7 => 'Invalid resource especified',
8 => '',
9 => 'Invalid session key',
10 => 'Invalid API key - You must be granted a valid key by libre.fm',
11 => 'Service Offline - This Service is temporarily offline. Try again later.',
12 => 'Subscription Error - The user needs to be subscribed in order to do that',
13 => 'Invalid method signature supplied',
14 => '',
15 => '',
16 => '',
17 => '',
18 => 'This user has no free radio plays left. Subscription required.',
19 => '',
20 => '',
);
}
static public function checkParams($params, $required, $optional = null) {
/*
* @param array $params parameters
* @param array $required required params
*/
if (empty($required) || empty($params)) return false;
if (count(array_diff_key($params, $required) > 0)) {
$this->giveError(6);
}
}
static public function checkParams($params, $required, $optional = null) {
/*
* @param array $params parameters
* @param array $required required params
*/
if (empty($required) || empty($params)) return false;
if (count(array_diff_key($params, $required) > 0)) {
$this->giveError(6);
}
}
static public function giveError($error) {
/*
* @param integer $error error key to give back
*/
static public function giveError($error) {
/*
* @param integer $error error key to give back
*/
global $xml;
return (XML::error('failed', $error, $this->api_error[$error]));
}
global $xml;
return (XML::error('failed', $error, $this->api_error[$error]));
}
}
......@@ -37,7 +37,7 @@ class AlbumXML {
$root->addAttribute('album', $album->name);
$tags = $album->getTopTags();
foreach($tags as &$tag) {
foreach ($tags as &$tag) {
$tag_node = $root->addChild('tag', null);
$tag_node->addChild('name', repamp($tag['tag']));
$tag_node->addChild('count', $tag['freq']);
......
......@@ -77,7 +77,7 @@ class ArtistXML {
$tracks = $artist->getTopTracks(50);
// Loop over every result and add as children to "toptracks".
for($i = 1; $i < count($tracks); $i++) {
for ($i = 1; $i < count($tracks); $i++) {
$track = $root->addChild('track', null);
$track->addAttribute('rank', $i);
$track->addChild('name', $tracks[$i]->name);
......@@ -102,7 +102,7 @@ class ArtistXML {
$root->addAttribute('artist', $artist->name);
$tags = $artist->getTopTags();
foreach($tags as &$tag) {
foreach ($tags as &$tag) {
$tag_node = $root->addChild('tag', null);
$tag_node->addChild('name', repamp($tag['tag']));
$tag_node->addChild('count', $tag['freq']);
......
......@@ -29,21 +29,21 @@ class JSONEncoder {
* @return A JSON formatted string
*/
public static function encodeXML($xml) {
if($xml->getName() == 'lfm' && count($xml->children())) {
if ($xml->getName() == 'lfm' && count($xml->children())) {
$xml = $xml->children();
}
$json_data[$xml->getName()] = JSONEncoder::recursivelyEncodeXML($xml);
return json_encode($json_data);
}
public static function recursivelyEncodeXML($xml) {
$json_data = array();
if(count($xml->children())) {
foreach($xml->children() as $child) {
if(array_key_exists($child->getName(), $json_data)) {
if(!array_key_exists(0, $json_data[$child->getName()])) {
if (count($xml->children())) {
foreach ($xml->children() as $child) {
if (array_key_exists($child->getName(), $json_data)) {
if (!array_key_exists(0, $json_data[$child->getName()])) {
$a = array();
$a[] = $json_data[$child->getName()];
$json_data[$child->getName()] = $a;
......@@ -53,17 +53,17 @@ class JSONEncoder {
$json_data[$child->getName()] = JSONEncoder::recursivelyEncodeXML($child);
}
}
if(count($xml->attributes())) {
if (count($xml->attributes())) {
$json_data['@attr'] = array();
foreach($xml->attributes() as $k => $v) {
foreach ($xml->attributes() as $k => $v) {
$json_data['@attr'][$k] = (string) $v;
}
}
} else {
// Leaf node
if(count($xml->attributes())) {
if (count($xml->attributes())) {
$json_data['#text'] = (string) $xml;
foreach($xml->attributes() as $k => $v) {
foreach ($xml->attributes() as $k => $v) {
$json_data[$k] = (string) $v;
}
} else {
......
......@@ -27,8 +27,8 @@ class TrackXML {
public static function addTags($userid, $artist, $album, $track, $tags) {
global $adodb;
$tags = explode(",", strtolower($tags));
foreach($tags as $tag) {
$tags = explode(',', strtolower($tags));
foreach ($tags as $tag) {
$tag = trim($tag);
if (strlen($tag) == 0) {
continue;
......@@ -40,7 +40,7 @@ class TrackXML {
. $adodb->qstr($album) . ', '
. $adodb->qstr($track) . ', '
. $userid . ')');
} catch (exception $ex) {}
} catch (Exception $e) {}
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
......@@ -58,7 +58,7 @@ class TrackXML {
$root->addAttribute('artist', $artist);
$root->addAttribute('track', $name);
foreach($tags as &$tag) {
foreach ($tags as &$tag) {
$tag_node = $root->addChild('tag', null);
$tag_node->addChild('name', repamp($tag['tag']));
$tag_node->addChild('count', $tag['freq']);
......@@ -79,7 +79,7 @@ class TrackXML {
$root->addAttribute('artist', $artist);
$root->addAttribute('track', $name);
foreach($tags as $tag) {
foreach ($tags as $tag) {
$tag_node = $root->addChild('tag', null);
$tag_node->addChild('name', repamp($tag));
$tag_node->addChild('url', Server::getTagURL($row['tag']));
......@@ -96,8 +96,8 @@ class TrackXML {
. $userid . ', '
. $adodb->qstr($name) . ', '
. $adodb->qstr($artist) . ', '
. time() . ")");
} catch (exception $ex) {}
. time() . ')');
} catch (Exception $e) {}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
......@@ -112,8 +112,8 @@ class TrackXML {
. $userid . ', '
. $adodb->qstr($name) . ', '
. $adodb->qstr($artist) . ', '
. time() . ")");
} catch (exception $ex) {}
. time() . ')');
} catch (Exception $e) {}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
......@@ -124,8 +124,8 @@ class TrackXML {
global $adodb;
try {
$res = $adodb->Execute('DELETE FROM banned_tracks WHERE userid=' . $userid . ' AND track=' . $adodb->qstr($name) . ' AND artist=' . $adodb->qstr($artist) );
} catch (exception $ex) {}
$res = $adodb->Execute('DELETE FROM banned_tracks WHERE userid=' . $userid . ' AND track=' . $adodb->qstr($name) . ' AND artist=' . $adodb->qstr($artist));
} catch (Exception $e) {}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
......@@ -137,8 +137,8 @@ class TrackXML {
global $adodb;
try {
$res = $adodb->Execute('DELETE FROM loved_tracks WHERE userid=' . $userid . ' AND track=' . $adodb->qstr($name) . ' AND artist=' . $adodb->qstr($artist) );
} catch (exception $ex) {}
$res = $adodb->Execute('DELETE FROM loved_tracks WHERE userid=' . $userid . ' AND track=' . $adodb->qstr($name) . ' AND artist=' . $adodb->qstr($artist));
} catch (Exception $e) {}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
......
......@@ -41,18 +41,20 @@ class UserXML {
$user_node->addChild('url', $user->getURL());
$user_node->addChild('playcount', $user->getTotalTracks());
$user_node->addChild('profile_created', strftime('%c', $user->created));
if (isset($user->modified))
if (isset($user->modified)) {
$user_node->addChild('profile_updated', strftime('%c', $user->modified));
}
return($xml);
return $xml;
}
public static function getTopTracks($username, $time) {
global $adodb;
$timestamp;
if (!isset($time))
if (!isset($time)) {
$time = 'overall';
}
//TODO: Do better, this is too ugly :\
if (strcmp($time, 'overall') == 0) {
$timestamp = 0;
......@@ -73,7 +75,7 @@ class UserXML {
$user = new User($username);
$res = $user->getTopTracks(20, $timestamp);
}
catch (exception $e) {
catch (Exception $e) {
$err = 1;
}
......@@ -86,7 +88,7 @@ class UserXML {
$root->addAttribute('user', $username);
$root->addAttribute('type', $time);
$i = 1;
foreach($res as &$row) {
foreach ($res as &$row) {
$track = $root->addChild('track', null);
$track->addAttribute('rank', $i);
$track->addChild('name', repamp($row['track']));
......@@ -97,7 +99,7 @@ class UserXML {
$i++;
}
return($xml);
return $xml;
}
......@@ -112,11 +114,11 @@ class UserXML {
$err = 0;
try {
$user = new User($u);
if($page == 1) {
if ($page == 1) {
$npres = $user->getNowPlaying(1);
}
$res = $user->getScrobbles($limit, $offset);
} catch (exception $e) {
} catch (Exception $e) {
$err = 1;
}
......@@ -134,8 +136,8 @@ class UserXML {
$root->addAttribute('perPage', $limit);
$root->addAttribute('totalPages', $totalPages);
if($npres) {
foreach($npres as &$row) {
if ($npres) {
foreach ($npres as &$row) {
$track = $root->addChild('track');
$track->addAttribute('nowplaying', 'true');
$row['time'] = time();
......@@ -143,7 +145,7 @@ class UserXML {
}
}
foreach($res as &$row) {
foreach ($res as &$row) {
$track = $root->addChild('track', null);
UserXML::_addTrackDetails($track, $row);
}
......@@ -159,7 +161,7 @@ class UserXML {
$album = $track->addChild('album', repamp($row['album']));
$album->addAttribute('mbid', $row['album_mbid']);
$track->addChild('url', Server::getTrackURL($row['artist'], $row['album'], $row['track']));
$date = $track->addChild('date', gmdate("d M Y H:i",$row['time']) . " GMT");
$date = $track->addChild('date', gmdate("d M Y H:i", $row['time']) . " GMT");
$date->addAttribute('uts', $row['time']);
$track->addChild('streamable', null);
}
......@@ -170,7 +172,7 @@ class UserXML {
try {
$user = new User($u);
$res = $user->getTopTags($limit);
} catch (exception $ex) {
} catch (Exception $e) {
return XML::error('error', '7', 'Invalid resource specified');
}
......@@ -178,7 +180,7 @@ class UserXML {
$root = $xml->addChild('toptags');
$root->addAttribute('user', $user->name);
foreach($res as &$row) {
foreach ($res as &$row) {
$tag = $root->addChild('tag', null);
$tag->addChild('name', repamp($row['tag']));
$tag->addChild('count', repamp($row['freq']));
......@@ -189,11 +191,11 @@ class UserXML {
}
public static function getLovedTracks($u, $limit=50) {
try {
$user = new User($u);
$res = $user->getLovedTracks($limit);
} catch (exception $ex) {
} catch (Exception $e) {
return XML::error('error', '7', 'Invalid resource specified');
}
......@@ -201,7 +203,7 @@ class UserXML {
$root = $xml->addChild('lovedtracks');
$root->addAttribute('user', $user->name);
foreach($res as &$row) {
foreach ($res as &$row) {
$track_node = $root->addChild('track', null);
UserXML::_addLBTrackDetails($track_node, $row);
}
......@@ -210,11 +212,11 @@ class UserXML {
}
public static function getBannedTracks($u, $limit=50) {
try {
$user = new User($u);
$res = $user->getBannedTracks($limit);
} catch (exception $ex) {
} catch (Exception $e) {
return XML::error('error', '7', 'Invalid resource specified');
}
......@@ -222,7 +224,7 @@ class UserXML {
$root = $xml->addChild('bannedtracks');
$root->addAttribute('user', $user->name);
foreach($res as &$row) {
foreach ($res as &$row) {
$track_node = $root->addChild('track', null);
UserXML::_addLBTrackDetails($track_node, $row);
}
......@@ -235,7 +237,7 @@ class UserXML {
$track_node->addChild('name', repamp($track->name));
$track_node->addChild('mbid', $track->mbid);
$track_node->addChild('url', $track->getURL());
$date = $track_node->addChild('date', gmdate("d M Y H:i",$row['time']) . " GMT");
$date = $track_node->addChild('date', gmdate("d M Y H:i", $row['time']) . " GMT");
$date->addAttribute('uts', $row['time']);
try {
$artist = new Artist($row['artist']);
......
......@@ -26,18 +26,19 @@ require_once('../../database.php');
<body>
<?php if (isset($_POST['username'], $_POST['api_key'], $_POST['token'])) {
<?php
if (isset($_POST['username'], $_POST['api_key'], $_POST['token'])) {
// Authenticate the user using the submitted password
try {
$result = $adodb->GetOne('SELECT username FROM Users WHERE '
. 'lower(username) = ' . $adodb->qstr(strtolower($_POST['username'])) . ' AND '
. 'password = ' . $adodb->qstr(md5($_POST['password'])));
}
catch (exception $e) {
} catch (Exception $e) {
die('Database error');
}
if (!$result)
if (!$result) {
die('Authentication failed');
}
// Bind the user to the token and cancel the expiration rule
try {
......@@ -46,8 +47,7 @@ require_once('../../database.php');
. 'expires = 0 '
. 'WHERE '
. 'token = ' . $adodb->qstr($_POST['token']));
}
catch (exception $e) {
} catch (Exception $e) {
die('Database error');
}
?>
......@@ -56,25 +56,25 @@ require_once('../../database.php');
<p>You may now close the browser.</p>
<?php } elseif (!isset($_GET['api_key'], $_GET['token'])) { ?>
<?php } else if (!isset($_GET['api_key'], $_GET['token'])) { ?>
<p>Must submit an api_key and token to proceed.</p>
<?php
} else {
// Ensures the token exists and is not already bound to a user
try {
$result = $adodb->GetRow('SELECT * FROM Auth WHERE '
. 'token = ' . $adodb->qstr($_GET['token']) . ' AND '
. 'username IS NULL');
}
catch (exception $e) {
die('Database error');
}
if (!$result)
die('Invalid token');
?>
<?php
} else {
// Ensures the token exists and is not already bound to a user
try {
$result = $adodb->GetRow('SELECT * FROM Auth WHERE '
. 'token = ' . $adodb->qstr($_GET['token']) . ' AND '
. 'username IS NULL');
} catch (Exception $e) {
die('Database error');
}
if (!$result) {
die('Invalid token');
}
?>
<form method="post" action="">
......@@ -90,7 +90,7 @@ require_once('../../database.php');
</form>
<?php } ?>
<?php } ?>
</body>
......
......@@ -25,7 +25,7 @@ class XML {
$dom->preserveWhitespace = false;
$dom->loadXML($xml->asXML());
$dom->formatOutput = true;
return($dom->saveXML());
return $dom->saveXML();
}
public static function error($status, $errcode, $errtext) {
......@@ -33,11 +33,11 @@ class XML {
$xml->addAttribute('status', $status);
$error = $xml->addChild('error', $errtext);
$error->addAttribute('code', $errcode);
return($xml);
return $xml;
}
}
function repamp($input) {
$input = str_replace('&', '&amp;', $input);
return($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