47 lines
1.7 KiB
PHP
47 lines
1.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/lib/bootstrap.php';
|
|
$me = require_login();
|
|
$box = ($_GET['box'] ?? 'in') === 'out' ? 'out' : 'in';
|
|
$page = max(1, (int)($_GET['p'] ?? 1));
|
|
$off = ($page - 1) * PER_PAGE;
|
|
|
|
// delete
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['act'] ?? '') === 'del') {
|
|
require_csrf();
|
|
$mid = (int)($_POST['id'] ?? 0);
|
|
db()->prepare("DELETE FROM messages WHERE id=? AND (to_id=? OR from_id=?)")
|
|
->execute([$mid, $me['id'], $me['id']]);
|
|
redirect('/inbox.php', ['box' => $box]);
|
|
}
|
|
|
|
if ($box === 'in') {
|
|
$sql = "SELECT m.*, u.username AS who FROM messages m
|
|
LEFT JOIN users u ON u.id=m.from_id
|
|
WHERE m.to_id=? ORDER BY m.id DESC LIMIT ? OFFSET ?";
|
|
} else {
|
|
$sql = "SELECT m.*, u.username AS who FROM messages m
|
|
LEFT JOIN users u ON u.id=m.to_id
|
|
WHERE m.from_id=? ORDER BY m.id DESC LIMIT ? OFFSET ?";
|
|
}
|
|
$s = db()->prepare($sql);
|
|
$s->execute([$me['id'], PER_PAGE + 1, $off]);
|
|
$rows = $s->fetchAll();
|
|
$more = count($rows) > PER_PAGE;
|
|
if ($more) array_pop($rows);
|
|
|
|
page_start($box === 'in' ? 'Inbox' : 'Sent', ['back' => '/index.php']);
|
|
if (!$rows) p_para($box === 'in' ? 'No messages.' : 'Nothing sent yet.');
|
|
foreach ($rows as $r) {
|
|
$flag = ($box === 'in' && !$r['is_read']) ? '* ' : '';
|
|
$lab = $flag . ($r['who'] ?? '[deleted]') . ': ' . $r['subject'] . ' (' . ago((int)$r['created_at']) . ')';
|
|
p_link('/message.php', $lab, ['id' => $r['id']]);
|
|
}
|
|
p_pager('/inbox.php', $page, $more, ['box' => $box]);
|
|
p_rule();
|
|
p_links([
|
|
['/compose.php', 'New message'],
|
|
['/inbox.php', $box === 'in' ? 'Sent items' : 'Inbox', ['box' => $box === 'in' ? 'out' : 'in']],
|
|
['/index.php', 'Home'],
|
|
]);
|
|
page_end();
|