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

Commit 2659bca7 authored by elleo's avatar elleo

Web front end

Support for registration, login and invitations
Rough librefm theme for the web front end (someone else can make it pretty)
Web installer script (just sets up configuration, shares gobbler's database)
Update gobbler install script to create invitations table
parent 1b7aab0c
......@@ -94,6 +94,12 @@ if (isset($_POST['install'])) {
mbid VARCHAR(36),
expires int)");
$res = $mdb2->query("CREATE TABLE Invitations(
inviter VARCHAR(64) REFERENCES Users(username),
invitee VARCHAR(64) REFERENCES Users(username),
code VARCHAR(32),
PRIMARY KEY(inviter, invitee, code))");
// Test user configuration
$res = $mdb2->query("INSERT INTO Users
(username, password, created)
......
<?php
require_once('database.php');
session_start();
if(isset($_SESSION['session_id'])) {
$res = $mdb2->query("SELECT username FROM Scrobble_Sessions WHERE "
. "sessionid = " . $mdb2->quote($_SESSION['session_id'], "text")
. " AND expires > " . time());
if(!$res->numRows()) {
// Session is invalid
unset($_SESSION['session_id']);
} else {
$logged_in = true;
$username = $res->fetchOne(0);
}
}
?>
<?php $config_version = 0.1;
$connect_string = 'mysql://librefm:librefmpass@localhost:3306/librefm';
$default_theme = 'librefm';
$base_url = 'http://localhost/~mike/librefm/';
$install_path = '/home/mike/src/librefm/trunk/web/'; ?>
<?
if(!file_exists(dirname(__FILE__) . "/config.php")) {
die("Please run the <a href='install.php'>Install</a> script to configure your installation");
}
require_once('config.php');
require_once('MDB2.php');
$mdb2 =& MDB2::connect($connect_string);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
?>
<?php
require_once("database.php");
require_once("templating.php");
$smarty->display("welcome.tpl");
?>
<?php
require_once('MDB2.php');
require_once('version.php');
if(file_exists("config.php")) {
die("A configuration file already exists. Please delete <i>config.php</i> if you wish to reinstall.");
}
if (isset($_POST['install'])) {
//Get the database connection string
$dbms = $_POST['dbms'];
if($dbms == "sqlite") {
$filename = $_POST['filename'];
$connect_string = "sqlite:///" . $filename;
} else {
$connect_string = $dbms . "://" . $_POST['username'] . ":" . $_POST['password'] . "@" . $_POST['hostname'] . ":" . $_POST['port'] . "/" . $_POST['dbname'];
}
// Check the connection
$mdb2 =& MDB2::connect($connect_string);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
$mdb2->disconnect();
$install_path = dirname(__FILE__) . "/";
$default_theme = $_POST['default_theme'];
$base_url = $_POST['base_url'];
//Write out the configuration
$config = "<?php \$config_version = " . $version .";\n \$connect_string = '" . $connect_string . "';\n \$default_theme = '" . $default_theme . "';\n \$base_url = '" . $base_url . "';\n \$install_path = '" . $install_path . "'; ?>";
$conf_file = fopen("config.php", "w");
$result = fwrite($conf_file, $config);
fclose($conf_file);
if(!$result) {
$print_config = str_replace("<", "&lt;", $config);
die("Unable to write to file '<i>config.php</i>'. Please create this file and copy the following in to it: <br /><pre>" . $print_config . "</pre>");
}
die("Configuration completed successfully!");
}
?>
<html>
<head>
<title>Libre.fm Website Installer</title>
<script type='text/javascript'>
function showSqlite() {
document.getElementById("sqlite").style.visibility = "visible";
document.getElementById("networkdbms").style.visibility = "hidden";
}
function showNetworkDBMS() {
document.getElementById("sqlite").style.visibility = "hidden";
document.getElementById("networkdbms").style.visibility = "visible";
}
</script>
</head>
<body onload="showSqlite()">
<h1>Libre.fm Website Installer</h1>
<p>Before installing the libre.fm website please be sure that you've installed and configured gobbler, as this creates all the database tables.</p>
<form method="post">
<h2>Database</h2>
Database Management System: <br />
<input type="radio" name="dbms" value="sqlite" onclick='showSqlite()' checked>SQLite (use an absolute path)</input><br />
<input type="radio" name="dbms" value="mysql" onclick='showNetworkDBMS()'>MySQL</input><br />
<input type="radio" name="dbms" value="pgsql" onclick='showNetworkDBMS()'>PostgreSQL</input><br />
<br />
<div id="sqlite">
Filename: <input type="text" name="filename" /><br />
</div>
<div id="networkdbms">
Hostname: <input type="text" name="hostname" /><br />
Port: <input type="text" name="port" /><br />
Database: <input type="text" name="dbname" /><br />
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
</div>
<br />
<h2>General</h2>
Default Theme: <select name="default_theme">
<?php
$dir = opendir("themes");
while($theme = readdir($dir)) {
if(is_dir("themes/" . $theme) && $theme[0] != ".") {
echo "<option>" . $theme . "</option>";
}
}
?>
</select><br />
Base URL: <input type="text" name="base_url" /><br />
<br /><br />
<input type="submit" value="Install" name="install" />
</form>
</body>
</html>
<?php
require_once("database.php");
require_once("templating.php");
if(!$logged_in) {
$smarty->display("login.tpl");
die();
}
if(isset($_POST['invite'])) {
$errors = "";
$email = $_POST['email'];
if(empty($email)) {
$errors .= "You must enter an e-mail address.<br />";
}
if(empty($errors)) {
$code = md5(md5($username) . time());
$mdb2->query("INSERT INTO Invitations (inviter, code) VALUES ("
. $mdb2->quote($username, "text") . ", "
. $mdb2->quote($code, "text") . ")");
$url = $base_url . "/register.php?authcode=" . $code;
$headers = "From: Libre.fm Invitations <invitations@libre.fm>";
mail($email, "Libre.fm Invitation", "Hi!\n\nClearly " . $username . " really likes you, because they've sent you an inivitation to join http://libre.fm\n Just visit " . $url . " to sign up, all the cool kids are doing it.\n", $headers);
$smarty->assign("sent", true);
} else {
$smarty->assign("errors", $errors);
}
}
$smarty->display("invite.tpl");
?>
<?php
require_once("database.php");
require_once("templating.php");
if(isset($_POST['login'])) {
$errors = "";
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username)) {
$errors .= "You must enter a username.<br />";
}
if(empty($password)) {
$errors .= "You must enter a password.<br />";
}
if(empty($errors)) {
$res = $mdb2->query("SELECT username FROM Users WHERE "
. " username = " . $mdb2->quote($username, "text")
. " AND password = " . $mdb2->quote(md5($password), "text"));
if(!$res->numRows()) {
$errors .= "Invalid username or password.";
} else {
// Give the user a session id, like any other client
$session_id = md5(md5($password) . time());
$mdb2->query("INSERT INTO Scrobble_Sessions (username, sessionid, expires) VALUES ("
. $mdb2->quote($username, "text") . ", "
. $mdb2->quote($session_id, "text") . ", "
. $mdb2->quote(time() + 604800) . ")"); // Web sessions last a week
$_SESSION['session_id'] = $session_id;
$logged_in = true;
$smarty->assign("logged_in", true);
}
}
}
if($logged_in) {
// Send the user to the welcome page when they've logged in
$smarty->display("welcome.tpl");
} else {
$smarty->display("login.tpl");
}
?>
<?php
require_once("database.php");
require_once("templating.php");
$authcode = $_GET["authcode"];
$res = $mdb2->query("SELECT inviter FROM Invitations WHERE code = " . $mdb2->quote($authcode, "text"));
if(!$res->numRows()) {
$invalid_authcode = true;
} else {
$invalid_authcode = false;
}
if(isset($_POST['register'])) {
$errors = "";
$username = $_POST['username'];
$password = $_POST['password'];
$passwordrepeat = $_POST['password-repeat'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$location = $_POST['location'];
$bio = $_POST['bio'];
//Basic validation
if(empty($username)) {
$errors .= "You must enter a username.<br />";
}
if(empty($password)) {
$errors .= "You must enter a password.<br />";
}
if($password != $passwordrepeat) {
$errors .= "Your passwords do not match.<br />";
}
if(empty($email)) {
$errors .= "You must enter an e-mail address.<br />";
}
//Check this username is available
$res = $mdb2->query("SELECT username FROM Users WHERE username = " . $mdb2->quote($username));
if($res->numRows()) {
$errors .= "Sorry, that username is already registered.<br />";
}
if(empty($errors) && !$invalid_authcode) {
// Create the user
$mdb2->query("INSERT INTO Users (username, password, email, fullname, bio, location, created) VALUES ("
. $mdb2->quote($username, "text") . ", "
. $mdb2->quote(md5($password), "text") . ", "
. $mdb2->quote($email, "text") . ", "
. $mdb2->quote($fullname, "text") . ", "
. $mdb2->quote($bio, "text") . ", "
. $mdb2->quote($location, "text") . ", "
. time() . ")");
// Remove auth code and set their username as the invitee
$mdb2->query("UPDATE Invitations SET code = NULL, invitee = " . $mdb2->quote($username, "text") . " WHERE code = " . $mdb2->quote($authcode, "text"));
$smarty->assign("registered", true);
} else {
$smarty->assign("username", $username);
$smarty->assign("fullname", $fullname);
$smarty->assign("email", $email);
$smarty->assign("location", $location);
$smarty->assign("bio", $bio);
$smarty->assign("errors", $errors);
}
}
$smarty->assign("invalid_authcode", $invalid_authcode);
$smarty->assign("authcode", $authcode);
$smarty->display("register.tpl");
?>
<?php
require_once("config.php");
require_once("auth.php");
require_once("smarty/Smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = $install_path . "/themes/". $default_theme . "/templates/";
$smarty->compile_dir = $install_path. "/themes/" . $default_theme . '/templates_c/';
$smarty->assign("base_url", $base_url);
if($logged_in) {
$smarty->assign("logged_in", true);
$smarty->assign("username", $username);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Libre.fm</title>
<link rel="stylesheet" href="{$base_url}/themes/librefm/base.css" type="text/css">
<link rel="stylesheet" href="{$base_url}/themes/librefm/librefm.css" type="text/css">
<!--
<rdf:RDF xmlns="http://web.resource.org/cc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<license rdf:resource="http://creativecommons.org/licenses/by-sa/3.0/us/" />
</Work>
<License rdf:about="http://creativecommons.org/licenses/by-sa/3.0/us/">
</License>
</rdf:RDF>
-->
</head>
<body>
<div id="doc3" class="yui-t7">
<div id="hd" style="banner"><h1><a href="http://libre.fm/">Libre.fm</a></h1></div>
{include file='menu.tpl'}
<div id="main">
{include file='header.tpl'}
<h2>Invite</h2>
{if isset($errors)}
<p id='errors'>{$errors}</p>
{/if}
{if isset($sent)}
<p>Your invitation has been sent, pretty soon your friend should be thanking you profusely.</p>
{else}
<div id="invite">
<p>Do you have an awesome friend you'd like to share libre.fm with? Just enter their email address and we'll sort them out with an invitation code.</p>
<form action='' method='post'>
<fieldset>
<label for='email'>Invitee's E-mail:</label>
<input id='email' name='email' type='text' value='' /><br />
<input type='submit' name='invite' value="Let them in!" />
</fieldset>
</form>
</div>
{/if}
{include file='footer.tpl'}
{include file='header.tpl'}
<h2>Login</h2>
{if isset($errors)}
<p id='errors'>{$errors}</p>
{/if}
<div id="login">
<form action='' method='post'>
<fieldset>
<label for='username'>Username:</label>
<input id='username' name='username' type='text' value='{$username}' maxlength='64' />
<label for='password'>Your password:</label>
<input id='password' name='password' type='password' value=''/>
<input type='submit' name='login' value="Let me in!" />
</fieldset>
</form>
</div>
{include file='footer.tpl'}
<div id="menu">
{if isset($logged_in)}
<a href="{$base_url}/invite.php">Invite a friend</a><br />
{else}
<a href="{$base_url}/login.php">Login</a><br />
<a href="{$base_url}/register.php">Register</a><br />
{/if}
</div>
{include file='header.tpl'}
<h2>Register</h2>
{if !isset($authcode) }
<p>Sorry! You need to have an invite to be able to register.</p>
{elseif $invalid_authcode }
<p>Sorry! That invitation code is either invalid or has already been used.</p>
{elseif isset($registered) }
<p>You're now registered with libre.fm! Yay!</p>
<p><small>(p.s. we love you)</small></p>
{else}
{if isset($errors) }
<p id='errors'>{$errors}</p>
{/if}
<br />
<div id='register'>
<form action='' method='post'>
<fieldset>
<label for='fullname'>You:<span>(that's your real name.)</span></label>
<input id='fullname' name='fullname' type='text' value='{$fullname}' maxlength='255'/>
<label for='username'>Your nickname:<span>(no more than 64 chars.)</span></label>
<input id='username' name='username' type='text' value='{$username}' maxlength='64' />
<label for='email'>Your e-mail:<span>(must be valid!)</span></label>
<input id='email' name='email' type='text' value='{$email}' maxlength='64' />
<label for='location'>Location:<span>(like 'CABA, Buenos Aires, Argentina')</span></label>
<input id='location' name='location' type='text' value='{$location}' maxlength='255' />
<label for='password'>Your password:<span>(make it hard to guess)</span></label>
<input id='password' name='password' type='password' value=''/>
<label for='password-repeat'>Your password again<span>(you should repeat it.)</span></label>
<input id='password-repeat' name='password-repeat' type='password' value=''/>
<label for='bio'>About yourself:<span>(we want to know you! in 140 chars.)</span></label>
<input id='bio' name='bio' type='text' value='{$bio}' maxlength='140'/>
<input type='submit' name='register' value="OK, I'm in" />
</fieldset>
</form>
</div>
{/if}
{include file='footer.tpl'}
{include file='header.tpl'}
<h2>Welcome</h2>
{include file='footer.tpl'}
<?
$version = 0.1;
?>
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