88 lines
3.2 KiB
PHP
88 lines
3.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/lib/bootstrap.php';
|
|
$me = current_user();
|
|
|
|
$fid = (int)($_GET['f'] ?? 0);
|
|
|
|
// ---------- forum index ----------
|
|
if (!$fid) {
|
|
$rows = db()->query("SELECT f.*,
|
|
(SELECT COUNT(*) FROM topics t WHERE t.forum_id=f.id) tc,
|
|
(SELECT COUNT(*) FROM posts p JOIN topics t ON t.id=p.topic_id
|
|
WHERE t.forum_id=f.id) pc
|
|
FROM forums f ORDER BY f.sort_order, f.id")->fetchAll();
|
|
page_start('Forum', ['back' => '/index.php']);
|
|
if (!$rows) p_para('No forums yet.');
|
|
foreach ($rows as $r) {
|
|
p_link('/forum.php', $r['name'] . ' (' . $r['tc'] . 't/' . $r['pc'] . 'p)'
|
|
. ($r['is_locked'] ? ' [locked]' : ''), ['f' => $r['id']]);
|
|
if (!is_wml() && $r['descr'] !== '') p_para($r['descr'], 'small');
|
|
}
|
|
p_links([['/index.php', 'Home']]);
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
// ---------- topic list for one forum ----------
|
|
$s = db()->prepare("SELECT * FROM forums WHERE id=?");
|
|
$s->execute([$fid]);
|
|
$f = $s->fetch();
|
|
if (!$f) bail('Forum', 'No such forum.', '/forum.php');
|
|
|
|
// new topic
|
|
$err = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$me = require_login();
|
|
require_csrf();
|
|
if ($f['is_locked'] && !$me['is_admin']) bail('Forum', 'Forum is locked.', '/forum.php');
|
|
$title = mb_substr(trim((string)($_POST['title'] ?? '')), 0, 80);
|
|
$body = trim((string)($_POST['body'] ?? ''));
|
|
if ($title === '' || $body === '') {
|
|
$err = 'Title and message are both required.';
|
|
} else {
|
|
$now = time();
|
|
$d = db();
|
|
$d->prepare("INSERT INTO topics (forum_id,user_id,title,created_at,bumped_at)
|
|
VALUES (?,?,?,?,?)")->execute([$fid, $me['id'], $title, $now, $now]);
|
|
$tid = (int)$d->lastInsertId();
|
|
$d->prepare("INSERT INTO posts (topic_id,user_id,body,created_at) VALUES (?,?,?,?)")
|
|
->execute([$tid, $me['id'], mb_substr($body, 0, 4000), $now]);
|
|
redirect('/topic.php', ['t' => $tid]);
|
|
}
|
|
}
|
|
|
|
$page = max(1, (int)($_GET['p'] ?? 1));
|
|
$off = ($page - 1) * PER_PAGE;
|
|
$s = db()->prepare("SELECT t.*, u.username,
|
|
(SELECT COUNT(*) FROM posts p WHERE p.topic_id=t.id) pc
|
|
FROM topics t LEFT JOIN users u ON u.id=t.user_id
|
|
WHERE t.forum_id=? ORDER BY t.bumped_at DESC LIMIT ? OFFSET ?");
|
|
$s->execute([$fid, PER_PAGE + 1, $off]);
|
|
$rows = $s->fetchAll();
|
|
$more = count($rows) > PER_PAGE;
|
|
if ($more) array_pop($rows);
|
|
|
|
page_start($f['name'], ['back' => '/forum.php']);
|
|
if ($err) p_err($err);
|
|
if (!$rows) p_para('No topics yet. Start one!');
|
|
foreach ($rows as $r) {
|
|
p_link('/topic.php', ($r['is_locked'] ? '[L] ' : '') . $r['title']
|
|
. ' (' . $r['pc'] . ') by ' . ($r['username'] ?? '?'), ['t' => $r['id']]);
|
|
}
|
|
p_pager('/forum.php', $page, $more, ['f' => $fid]);
|
|
p_rule();
|
|
if ($me && (!$f['is_locked'] || $me['is_admin'])) {
|
|
p_para('New topic:');
|
|
p_form('/forum.php?f=' . $fid, [
|
|
['name' => 'title', 'label' => 'Title', 'maxlength' => 80],
|
|
['name' => 'body', 'label' => 'Message', 'type' => is_wml() ? 'text' : 'textarea',
|
|
'maxlength' => is_wml() ? 200 : 4000],
|
|
], 'Post');
|
|
} elseif (!$me) {
|
|
p_links([['/login.php', 'Log in to post']]);
|
|
} else {
|
|
p_para('This forum is locked.');
|
|
}
|
|
p_links([['/forum.php', 'All forums'], ['/index.php', 'Home']]);
|
|
page_end();
|