51 lines
2.0 KiB
PHP
51 lines
2.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/lib/bootstrap.php';
|
|
$me = require_login();
|
|
|
|
$toId = (int)($_GET['to'] ?? 0);
|
|
$reId = (int)($_GET['re'] ?? 0);
|
|
$toName = '';
|
|
$subj = '';
|
|
|
|
if ($toId) { $t = user_by_id($toId); if ($t) $toName = $t['username']; }
|
|
if ($reId) {
|
|
$s = db()->prepare("SELECT subject FROM messages WHERE id=? AND to_id=?");
|
|
$s->execute([$reId, $me['id']]);
|
|
$sub = $s->fetchColumn();
|
|
if ($sub) $subj = (strncasecmp($sub, 'Re:', 3) === 0) ? $sub : 'Re: ' . $sub;
|
|
}
|
|
|
|
$err = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
require_csrf();
|
|
$toName = trim((string)($_POST['to'] ?? ''));
|
|
$subj = mb_substr(trim((string)($_POST['subject'] ?? '')), 0, 60);
|
|
$body = trim((string)($_POST['body'] ?? ''));
|
|
$dest = user_by_name($toName);
|
|
if (!$dest) $err = 'No such user: ' . $toName;
|
|
elseif ((int)$dest['id'] === (int)$me['id']) $err = 'You cannot message yourself.';
|
|
elseif ($subj === '') $err = 'Subject required.';
|
|
elseif ($body === '') $err = 'Message body required.';
|
|
else {
|
|
db()->prepare("INSERT INTO messages (from_id,to_id,subject,body,created_at)
|
|
VALUES (?,?,?,?,?)")
|
|
->execute([$me['id'], $dest['id'], $subj, mb_substr($body, 0, 4000), time()]);
|
|
page_start('Sent', ['back' => '/inbox.php']);
|
|
p_ok('Message sent to ' . $dest['username'] . '.');
|
|
p_links([['/inbox.php', 'Inbox'], ['/index.php', 'Home']]);
|
|
page_end();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
page_start('New message', ['back' => '/inbox.php']);
|
|
if ($err) p_err($err);
|
|
p_form('/compose.php', [
|
|
['name' => 'to', 'label' => 'To (username)', 'value' => $toName, 'maxlength' => 16],
|
|
['name' => 'subject', 'label' => 'Subject', 'value' => $subj, 'maxlength' => 60],
|
|
['name' => 'body', 'label' => 'Message', 'type' => is_wml() ? 'text' : 'textarea',
|
|
'maxlength' => is_wml() ? 200 : 4000],
|
|
], 'Send');
|
|
p_links([['/inbox.php', 'Inbox'], ['/users.php', 'Member list']]);
|
|
page_end();
|