55 lines
2.1 KiB
PHP
55 lines
2.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/lib/bootstrap.php';
|
|
$me = current_user();
|
|
|
|
$id = (int)($_GET['id'] ?? 0);
|
|
if ($id === 0) { $me = require_login(); $id = (int)$me['id']; }
|
|
$u = user_by_id($id);
|
|
if (!$u) bail('Profile', 'No such user.');
|
|
$mine = $me && (int)$me['id'] === $id;
|
|
|
|
// ---- edit handling ----
|
|
$msg = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $mine) {
|
|
require_csrf();
|
|
if (isset($_POST['tagline'])) {
|
|
$tag = mb_substr(trim((string)$_POST['tagline']), 0, 80);
|
|
$loc = mb_substr(trim((string)($_POST['location'] ?? '')), 0, 40);
|
|
db()->prepare("UPDATE users SET tagline=?, location=? WHERE id=?")
|
|
->execute([$tag, $loc, $id]);
|
|
$u['tagline'] = $tag; $u['location'] = $loc;
|
|
$msg = 'Profile saved.';
|
|
}
|
|
}
|
|
|
|
$d = db();
|
|
$s = $d->prepare("SELECT COUNT(*) FROM posts WHERE user_id=?"); $s->execute([$id]);
|
|
$posts = (int)$s->fetchColumn();
|
|
$s = $d->prepare("SELECT game, MAX(score) sc FROM scores WHERE user_id=? GROUP BY game");
|
|
$s->execute([$id]);
|
|
$best = $s->fetchAll();
|
|
|
|
page_start('Profile: ' . $u['username'], ['back' => '/index.php']);
|
|
if ($msg) p_ok($msg);
|
|
p_para('User: ' . $u['username'] . ($u['is_admin'] ? ' [admin]' : ''));
|
|
if ($u['tagline'] !== '') p_para('"' . $u['tagline'] . '"');
|
|
if ($u['location'] !== '') p_para('From: ' . $u['location']);
|
|
p_para('Joined: ' . date('d/m/Y', (int)$u['created_at']));
|
|
p_para('Last seen: ' . ($u['last_seen'] ? ago((int)$u['last_seen']) . ' ago' : 'never'));
|
|
p_para('Forum posts: ' . $posts);
|
|
foreach ($best as $b) p_para('Best ' . $b['game'] . ': ' . $b['sc']);
|
|
|
|
p_rule();
|
|
if ($mine) {
|
|
p_para('Edit your details:');
|
|
p_form('/profile.php', [
|
|
['name' => 'tagline', 'label' => 'Tagline', 'value' => $u['tagline'], 'maxlength' => 80],
|
|
['name' => 'location', 'label' => 'Location', 'value' => $u['location'], 'maxlength' => 40],
|
|
], 'Save');
|
|
p_links([['/password.php', 'Change password'], ['/inbox.php', 'My inbox']]);
|
|
} elseif ($me) {
|
|
p_links([['/compose.php', 'Send message', ['to' => $u['id']]]]);
|
|
}
|
|
p_links([['/users.php', 'Member list'], ['/index.php', 'Home']]);
|
|
page_end();
|