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();