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

Commit d519e327 authored by elleo's avatar elleo

Create a Server class for general server details

Change welcome page to use the Server class for getting Now Playing and Scrobbles
Make the Now Playing and Scrobble links use the new getUserURL() function for friendly URLs
Add username to recent scrobbles
parent f0a6a7cd
<?php
/* Libre.fm -- a free network service for sharing your music listening habits
Copyright (C) 2009 Libre.fm Project
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/>.
*/
require_once($install_path . '/database.php');
require_once($install_path . '/data/Artist.php');
require_once($install_path . '/data/Track.php');
require_once($install_path . '/data/User.php');
require_once($install_path . "/data/sanitize.php");
/**
* Provides access to server-wide data
*
* All methods are statically accessible
*/
class Server {
/**
* Retrieves a list of recent scrobbles
*
* @param int $number The number of scrobbles to return
* @return An array of scrobbles or a PEAR_Error in case of failure
*/
static function getRecentScrobbles($number) {
global $mdb2;
$res = $mdb2->query('SELECT username, artist, track, time FROM Scrobbles ORDER BY time DESC LIMIT 10');
if(PEAR::isError($res)) {
return $res;
}
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as $i) {
$i = sanitize($i);
$i["userurl"] = Server::getUserURL($i["username"]);
}
return $data;
}
/**
* Retrieves a list of the currently playing tracks
*
* @param int $number The maximum number of tracks to return
* @return An array of now playing data or a PEAR_Error in case of failure
*/
static function getNowPlaying($number) {
global $mdb2;
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url from Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code ORDER BY Now_Playing.expires DESC LIMIT ' . $mdb2->quote($number, "integer"));
if(PEAR::isError($res)) {
return $res;
}
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$i = sanitize($i);
if($i["name"] == "") {
$clientstr = strip_tags(stripslashes($i["client"])) . " (unknown, please tell us what this is)";
} else {
$clientstr = "<a href=\"" . strip_tags(stripslashes($i["url"])) . "\">" . strip_tags(stripslashes($i["name"])) . "</a>";
}
$i["clientstr"] = $clientstr;
$i["userurl"] = Server::getUserURL($i["username"]);
}
}
/**
* The get*URL functions are implemented here rather than in their respective
* objects so that we can produce URLs without needing to build whole objects.
*
* @param string $username The username we want a URL for
* @return A string containing URL to the user's profile
*/
static function getUserURL($username) {
global $friendly_urls, $base_url;
if($friendly_urls) {
return $base_url . "/user/" . urlencode(stripslashes($username));
} else {
return $base_url . "/profile.php?user=" . urlencode(stripslashes($username));
}
}
}
......@@ -21,35 +21,11 @@
require_once('database.php');
require_once('templating.php');
require_once($install_path . "/data/sanitize.php");
require_once("data/sanitize.php");
require_once("data/Server.php");
$res = $mdb2->query('SELECT username, artist, track, time FROM Scrobbles ORDER BY time DESC LIMIT 10');
if(PEAR::isError($res)) {
die($res->getMessage());
}
$smarty->assign('recenttracks', $res->fetchAll(MDB2_FETCHMODE_ASSOC));
$res = $mdb2->query('SELECT username, artist, track, client, ClientCodes.name, ClientCodes.url from Now_Playing LEFT OUTER JOIN Scrobble_Sessions ON Now_Playing.sessionid=Scrobble_Sessions.sessionid LEFT OUTER JOIN ClientCodes ON Scrobble_Sessions.client=ClientCodes.code ORDER BY Now_Playing.expires DESC');
if(PEAR::isError($res)) {
die($res->getMessage());
}
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$i = sanitize($i);
if($i["name"] == "") {
$clientstr = strip_tags(stripslashes($i["client"])) . "(unknown, please tell us what this is)";
} else {
$clientstr = "<a href=\"" . strip_tags(stripslashes($i["url"])) . "\">" . strip_tags(stripslashes($i["name"])) . "</a>";
}
$i["clientstr"] = $clientstr;
}
$smarty->assign('nowplaying', $data);
$smarty->assign('recenttracks', Server::getRecentScrobbles(10));
$smarty->assign('nowplaying', Server::getNowPlaying(10));
$smarty->display('welcome.tpl');
?>
......@@ -158,6 +158,7 @@ ul#topartists.listcloud dd { font-size: 80%; }
ul#nowplaying.listcloud li { float: left; margin-right: 10px; margin-bottom: 10px; list-style-type: none; padding: 10px; border: 1px solid #000; background-color: #eee; -moz-border-radius: 5px; height: 4.4em; }
ul#recenttracks.listcloud li { height: 3.4em; }
/* Users */
.user {
font-size: .8em;
......
......@@ -13,7 +13,7 @@ musical tastes with other people.</p>
<dt><a href="artist.php?artist={$nowplaying[np].artist|stripslashes|urlencode}">
{$nowplaying[np].artist|stripslashes|htmlspecialchars}</a></dt>
<dd>{$nowplaying[np].track|stripslashes|htmlspecialchars}</dd>
<dd><a href="http://alpha.libre.fm/profile.php?user={$nowplaying[np].username|stripslashes|urlencode}">{$nowplaying[np].username|stripslashes|htmlspecialchars}</a></dd>
<dd><a href="{$nowplaying[np].userurl}">{$nowplaying[np].username|stripslashes|htmlspecialchars}</a></dd>
<dd>{$nowplaying[np].clientstr}</dd>
</dl>
</li>
......@@ -29,6 +29,7 @@ musical tastes with other people.</p>
<dt><a href="artist.php?artist={$recenttracks[recent].artist|stripslashes|urlencode}">
{$recenttracks[recent].artist|stripslashes|htmlspecialchars}</a></dt>
<dd>{$recenttracks[recent].track|stripslashes|htmlspecialchars}</dd>
<dd><a href="{$recenttracks[recent].userurl}">{$recenttracks[recent].username|stripslashes|htmlspecialchars}</a></dd>
</dl>
</li>
{/section}
......
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