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

Commit 56510409 authored by tobyink's avatar tobyink

Adding support for editable profiles. Don't know if this actually works or not!

This uses jQuery, which is available under an MIT license, so should be OK to use.
Otherwise, I'm happy if someone wants to rewrite it without the jQuery dep.
parent 18e31bdd
......@@ -117,6 +117,9 @@ class User {
* @return A URL to the user's avatar image
*/
function getAvatar($size=64) {
if (!empty($this->avatar_uri))
return $this->avatar_uri;
return "http://www.gravatar.com/avatar/" . md5($this->email) . "?s=" . $size . "&d=monsterid";
}
......
<?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('database.php');
require_once('templating.php');
require_once('data/User.php');
require_once('data/TagCloud.php');
# This function tends to be quite useful. Might move it somewhere else so
# that it can be included and reused in other files.
function ifsetor (&$value, $fallback)
{
if (isset($value))
return $value;
return $fallback;
}
if($logged_in == false)
{
$smarty->assign('error', 'Error!');
$smarty->assign('details', 'Not logged in! You shouldn\'t be here!');
$smarty->display('error.tpl');
die();
}
$user = $_SESSION['user'];
$errors = array();
if (!empty($_POST['id']))
{
# Need better URI validation, but this will do for now. I think
# PEAR has a suitable module to help out here.
if ( !preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['id']) )
$errors[] = "WebID must be a URI.";
if ( preg_match('/\s/', $_POST['id']) )
$errors[] = "WebID must be a URI. Valid URIs cannot contain whitespace.";
}
if (!empty($_POST['homepage']))
{
# Need better URI validation, but this will do for now. I think
# PEAR has a suitable module to help out here.
if ( !preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['homepage']) )
$errors[] = "Homepage must be a URI.";
if ( preg_match('/\s/', $_POST['homepage']) )
$errors[] = "Homepage must be a URI. Valid URIs cannot contain whitespace.";
}
if (!empty($_POST['avatar_uri']))
{
# Need better URI validation, but this will do for now. I think
# PEAR has a suitable module to help out here.
if ( !preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['avatar_uri']) )
$errors[] = "Avatar must be a URI.";
if ( preg_match('/\s/', $_POST['avatar_uri']) )
$errors[] = "Avatar must be a URI. Valid URIs cannot contain whitespace.";
}
if (!empty($_POST['location_uri']))
{
# Currently only allow geonames URIs, but there's no reason we can't accept
# others at some point in the future. (e.g. dbpedia)
if ( !preg_match('/^http:\/\/sws.geonames.org\/[0-9]+\/$/', $_POST['location_uri']) )
$errors[] = "This should be a geonames.org semantic web service URI.";
}
if (!isset($errors[0]))
{
# Currently we don't allow them to change e-mail as we probably should
# have some kind of confirmation login to do so.
$user->id = $_POST['id'];
$user->fullname = $_POST['fullname'];
$user->homepage = $_POST['homepage'];
$user->bio = $_POST['bio'];
$user->location = $_POST['location'];
$user->location_uri = $_POST['location_uri'];
$user->avatar_uri = $_POST['avatar_uri'];
}
if(isset($user->name))
{
# Stuff which cannot be changed.
$smarty->assign("acctid", $user->acctid);
$smarty->assign('avatar', $user->getAvatar());
$smarty->assign('user', $user->name);
# Stuff which cannot be changed *here*
$smarty->assign('userlevel', $user->userlevel);
# Stuff which cannot be changed *yet*
$smarty->assign('email', $user->email);
# This is what we're going to let them change.
$smarty->assign("id", ifsetor($_POST['id']), $user->id));
$smarty->assign('fullname', ifsetor($_POST['fullname']), $user->fullname));
$smarty->assign('bio', ifsetor($_POST['bio']), $user->bio));
$smarty->assign('homepage', ifsetor($_POST['homepage']), $user->homepage));
$smarty->assign('location', ifsetor($_POST['location']), $user->location));
$smarty->assign('location_uri', ifsetor($_POST['location_uri']), $user->location_uri));
$smarty->assign('avatar_uri', ifsetor($_POST['avatar_uri']), $user->avatar_uri));
# And display the page.
$smarty->assign('errors', $errors);
$smarty->display('edit_profile.tpl');
}
else
{
$smarty->assign('error', 'User not found');
$smarty->assign('details', 'Shall I call in a missing persons report? This shouldn\'t happen.');
$smarty->display('error.tpl');
}
/* 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/>.
*/
function unrecognised ( msg )
{
$('#location_uri')[0].value = '';
$('#location_uri_label').removeClass('ok');
$('#location_uri_label').addClass('no');
$('#location_uri_label').text('Unrecognised location');
window.alert( msg );
return 0;
}
function doit ()
{
if ( !$('#loc').val() )
{
return unrecognised("You must enter a location.");
}
if ($('#loading')[0])
{
$('#loading').show();
}
else
{
var loading = document.createElement('IMG');
loading.id = 'loading';
document.body.appendChild(loading);
loading.src = '/i/loading.gif';
loading.style.height = '32px';
loading.style.width = '32px';
loading.style.position = 'absolute';
loading.style.top = '50%';
loading.style.left = '50%';
loading.style.marginTop = '-16px';
loading.style.marginLeft = '-16px';
}
$.getJSON("location-ws.php",
{ 'q' : $('#loc').val() },
function (data, status)
{
$('#loading').hide();
if (! data.geonames[0])
{
return unrecognised("This location was unrecognisable.");
}
var list;
if ($('#chooser_list')[0])
{
$('#chooser_list').empty();
$('#chooser_list').show();
list = $('#chooser_list')[0];
}
else
{
list = document.createElement('UL');
list.id = 'chooser_list';
$('#chooser')[0].appendChild(list);
}
for (var g in data.geonames)
{
var G = data.geonames[g];
var fullName = G.name;
if (G.adminName3) fullName += ", " + G.adminName3;
if (G.adminName2) fullName += ", " + G.adminName2;
if (G.adminName1) fullName += ", " + G.adminName1;
if (G.countryName) fullName += ", " + G.countryName;
var coords = G.lat.toFixed(2) + ';' + G.lng.toFixed(2);
var shortName = G.name;
if (G.countryCode == 'US')
shortName += ", " + G.adminCode1 + ", USA";
else
shortName += ", " + G.countryCode;
var item = document.createElement('LI');
var label1 = document.createElement('B');
label1.appendChild(document.createTextNode(fullName));
item.appendChild(label1);
item.appendChild(document.createTextNode(' '));
var label2 = document.createElement('SMALL');
label2.appendChild(document.createTextNode('['+coords+']'));
item.appendChild(label2);
item.setAttribute('data-geoname', 'http://sws.geonames.org/' + G.geonameId + '/');
item.setAttribute('data-geoname-name', shortName);
item.setAttribute('data-geoname-coords', coords);
item.onclick = function (e) {
if (!e) var e = window.event;
var tg = (window.event) ? e.srcElement : e.target;
var geoname = $(tg).closest('li').attr('data-geoname');
$('#location_uri')[0].value = geoname;
$('#chooser_list').empty();
$('#chooser_list').hide();
$('#location_uri_label').text(
$(tg).closest('li').attr('data-geoname-name')
+ ' [' + $(tg).closest('li').attr('data-geoname-coords') + ']' );
$('#location_uri_label').addClass('ok');
$('#location_uri_label').removeClass('no');
}
$(item).hover(function ()
{
$(this).addClass('hover');
},
function ()
{
$(this).removeClass('hover');
}
);
list.appendChild(item);
}
});
}
\ No newline at end of file
This diff is collapsed.
<?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/>.
*/
# This file should probably be moved out of the web root. /js/edit_profile.js will
# need updating if you do.
// The place being searched for.
$query = $_GET['q'];
if (!$query)
{
header("Content-Type: text/plain");
die("Must supply a query argument.\r\n");
}
// The number of results requested
$num = $_GET['n'];
if (! $num)
$num = 10;
// When translated versions become available, should be able to calculate
// the language code from the subdomain.
$lang = 'en';
$uri = sprintf('http://ws.geonames.org/searchJSON?q=%s&maxRows=%d&lang=%s&style=full',
urlencode($query),
$num,
urlencode($lang));
# We'll try to use cURL if the extension is installed on this server.
if (function_exists('curl_init'))
{
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'libre.fm');
$output = curl_exec($ch);
curl_close($ch);
header("Content-Type: application/json");
echo $output;
}
# Otherwise, we'll fall back to direct socket calls. Ugly.
elseif (function_exists('parse_url'))
{
$_uri = parse_url($uri);
if (! $_uri['port'])
$_uri['port'] = 80;
if (! ($nh = fsockopen($_uri['host'], $_uri['port'], $errno, $errstr, 20)) )
{
header("Content-Type: text/plain");
die("Could not open network connection! ($errno - $errstr)\r\n");
}
fwrite($nh, "GET {$_uri[path]}?{$_uri[query]} HTTP/1.0\r\n"
. "Host: {$_uri['host']}\r\n"
. "User-Agent: libre.fm\r\n"
. "Connection: close\r\n\r\n"
);
header("Content-Type: application/json");
while (!feof($nh))
{
$output .= fgets($nh, 128);
}
fclose($nh);
// Remove HTTP header.
echo substr(strstr($output, "\r\n\r\n"), 4);
}
......@@ -42,6 +42,7 @@ if(isset($user->name)) {
$smarty->assign('bio', $user->bio);
$smarty->assign('homepage', $user->homepage);
$smarty->assign('location', $user->location);
$smarty->assign('location_uri', $user->location_uri);
$aUserScrobbles = $user->getScrobbles(20);
if (!PEAR::isError ($aUserScrobbles)) {
$smarty->assign('scrobbles', $aUserScrobbles);
......
{include file='header.tpl'}
<h2 property="dc:title">Edit your profile</h2>
<script type="text/javascript" src="{$base_url}/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="{$base_url}/js/edit_profile.js"></script>
<form action="{$base_url}/edit_profile.php" method="post">
<table>
<tr>
<th align="right"><label for="fullname">Full name:</label></th>
<td><input name="fullname" id="fullname" value="{$fullname|htmlentities}" /></td>
</tr>
<tr>
<th align="right" rowspan="2" valign="top"><label for="location">Location:</label></th>
<td><input name="location" id="location" value="{$location|htmlentities}" /></td>
</tr>
<tr>
<td id="chooser">
<input type="hidden" name="location_uri" id="location_uri" value="{$location_uri|htmlentities}" />
<input type="button" value="Check ..." onclick="LocationCheck();" />
<span id="location_uri_label"></span>
</td>
</tr>
<tr>
<th align="right"><label for="homepage">Homepage URL:</label></th>
<td><input name="homepage" id="homepage" value="{$homepage|htmlentities}" /></td>
</tr>
<tr>
<th align="right"><label for="avatar_uri">Avatar URL:</label></th>
<td><input name="avatar_uri" id="avatar_uri" value="{$avatar_uri|htmlentities}" /></td>
</tr>
<tr>
<th align="right"><label for="bio">Mini Biography:</label></th>
<td><input name="bio" id="bio" value="{$bio|htmlentities}" /></td>
</tr>
<tr>
<th align="right"><label for="id">WebID (FOAF):</label></th>
<td><input name="id" id="id" value="{$id|htmlentities}" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Change" />
</td>
</tr>
</table>
</form>
{include file='footer.tpl'}
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