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

Commit 3e48d637 authored by elleo's avatar elleo

User mod_rewrite urls for Users if friendly_urls option is set in config

Add getURL methods to User and Artist
Create a User object in Auth rather than just passing around the username and userlevel
Make tabs/spaces more consistent
parent 5300579d
......@@ -20,21 +20,21 @@
*/
require_once('database.php');
require_once('data/User.php');
session_start();
if(isset($_SESSION['session_id'])) {
$res = $mdb2->query('SELECT Users.username,userlevel FROM Scrobble_Sessions,Users WHERE '
$res = $mdb2->query('SELECT username FROM Scrobble_Sessions WHERE '
. 'sessionid = ' . $mdb2->quote($_SESSION['session_id'], 'text')
. ' AND expires > ' . time() . ' AND Users.username = Scrobble_Sessions.username');
. ' AND expires > ' . time());
if(!$res->numRows()) {
// Session is invalid
unset($_SESSION['session_id']);
} else {
$logged_in = true;
$row = $res->fetchRow(MDB2_FETCHMODE_ASSOC);
$username = $row['username'];
$userlevel = $row['userlevel'];
$u_user = new User($row['username']);
}
}
?>
......@@ -117,4 +117,18 @@ class Artist {
return $tracks;
}
/**
* Gives the URL for this artist
*
* @return A string containing the URL of this artist
*/
function getURL() {
global $friendly_urls, $base_url;
if($friendly_urls) {
return $base_url . "/artist/" . urlencode(stripslashes($this->name));
} else {
return $base_url . "/artist.php?artist=" . urlencode(stripslashes($this->name));
}
}
}
......@@ -121,4 +121,13 @@ class Track {
$this->_listenercount = $row['listeners'];
}
/**
* Retrieve the artist for this track.
*
* @return An artist object
*/
function getArtist() {
return new Artist($this->artist_name);
}
}
......@@ -33,45 +33,44 @@ require_once($install_path . '/utils/human-time.php');
class User {
public $name, $email, $fullname, $bio, $location, $homepage, $error,$userlevel;
/**
* User constructor
*
* @param string $name The name of the user to load
*/
function __construct($name) {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Users WHERE '
. 'username = ' . $mdb2->quote($name, 'text'));
if($res->numRows()) {
$row = sanitize($res->fetchRow(MDB2_FETCHMODE_ASSOC));
$this->name = $row['username'];
$this->email = $row['email'];
$this->fullname = $row['fullname'];
$this->homepage = $row['homepage'];
$this->bio = $row['bio'];
$this->location = $row['location'];
$this->userlevel = $row['userlevel'];
}
}
/**
* Get a user's scrobbles ordered by time
*
* @param int $number The number of scrobbles to return
* @return An array of scrobbles
*/
function getScrobbles($number) {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Scrobbles WHERE username = ' .$mdb2->quote($this->name, 'text') . ' ORDER BY time DESC LIMIT '.$mdb2->quote($number, 'integer'));
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$i = sanitize($i);
$i['timehuman'] = human_timestamp($i['time']);
}
return $data;
public $name, $email, $fullname, $bio, $location, $homepage, $error, $userlevel;
/**
* User constructor
*
* @param string $name The name of the user to load
*/
function __construct($name) {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Users WHERE '
. 'username = ' . $mdb2->quote($name, 'text'));
if($res->numRows()) {
$row = sanitize($res->fetchRow(MDB2_FETCHMODE_ASSOC));
$this->name = $row['username'];
$this->email = $row['email'];
$this->fullname = $row['fullname'];
$this->homepage = $row['homepage'];
$this->bio = $row['bio'];
$this->location = $row['location'];
$this->userlevel = $row['userlevel'];
}
}
/**
* Get a user's scrobbles ordered by time
*
* @param int $number The number of scrobbles to return
* @return An array of scrobbles
*/
function getScrobbles($number) {
global $mdb2;
$res = $mdb2->query('SELECT * FROM Scrobbles WHERE username = ' .$mdb2->quote($this->name, 'text') . ' ORDER BY time DESC LIMIT '.$mdb2->quote($number, 'integer'));
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
foreach($data as &$i) {
$i = sanitize($i);
$i['timehuman'] = human_timestamp($i['time']);
}
return $data;
}
/**
......@@ -84,29 +83,37 @@ class User {
return "http://www.gravatar.com/avatar/" . md5($this->email) . "?s=" . $size . "&d=monsterid";
}
/**
* Get a user's now-playing tracks
*
* @return An array of nowplaying data
*/
function getNP() {
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 WHERE username=' . $mdb2->quote($this->name, 'text'));
$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>";
function getURL() {
global $friendly_urls, $base_url;
if($friendly_urls) {
return $base_url . "/user/" . urlencode(stripslashes($this->name));
} else {
return $base_url . "/profile.php?user=" . urlencode(stripslashes($this->name));
}
}
$i["clientstr"] = $clientstr;
}
return $data;
/**
* Get a user's now-playing tracks
*
* @return An array of nowplaying data
*/
function getNP() {
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 WHERE username=' . $mdb2->quote($this->name, 'text'));
$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;
}
return $data;
}
}
......
......@@ -30,8 +30,7 @@ $smarty->assign('base_url', $base_url);
if(isset($logged_in)) {
$smarty->assign('logged_in', true);
// Pre-fix this user's details with u to avoid confusion with other users
$smarty->assign('u_username', $username);
$smarty->assign('u_userlevel', $userlevel);
$smarty->assign('u_user', $u_user);
}
?>
<div id="menu">
<ul id="navigation">
{if ($logged_in)}
<li><a href="{$base_url}/profile.php?user={$u_username|urlencode}">{$u_username}</a></li>
<li><a href="{$u_user->getURL()}">{$u_user->name}</a></li>
{/if}
{if ($logged_in)}
<li>Invites disabled</li>
{if $u_userlevel > 0}
{if $u_user->userlevel > 0}
<li><a href="/admin.php">Admin panel</a></li>
{/if}
{else}
......
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