willy.club/app/App/Controller/ChatController.php

69 lines
1.7 KiB
PHP
Raw Normal View History

2023-02-03 11:00:13 +00:00
<?php
namespace App\Controller;
use App\Model\ChatModel;
2023-02-03 11:00:13 +00:00
abstract class ChatController
{
static function index()
{
if (empty($_POST)) {
return view('pages/chat/index', [
'nick' => 'Willy',
'just_sent_message' => false,
'errmsg' => false
]);
}
$nick = filter_input(INPUT_POST, 'nick');
$text = filter_input(INPUT_POST, 'text');
$errmsg = (function() use (&$text, &$nick): string {
2023-02-03 11:00:13 +00:00
if (empty(trim($nick, ' '))) {
return 'You must choose a nickname.';
}
$nick_max_chars = 128;
if (strlen($nick) > $nick_max_chars) {
return 'Your nickname is TOO LONG! ' . strlen($nick) . ' out of ' . $nick_max_chars. ' characters.';
}
$text_max_chars = 4096;
if (strlen($text) > $text_max_chars) {
return 'Your message is TOO LONG!!!! ' . strlen($text) . ' out of ' . $text_max_chars . ' characters.';
}
if (empty(trim($text, ' '))) {
return 'Message body cannot be empty.';
}
return '';
2023-02-03 11:00:13 +00:00
})();
if (empty($errmsg)) {
ChatModel::save_message(
$nick,
$text
2023-02-03 11:00:13 +00:00
);
}
2023-02-03 11:00:13 +00:00
view('pages/chat/index', [
'nick' => $nick,
'just_sent_message' => true,
'errmsg' => $errmsg
]);
}
static function messages()
{
view('pages/chat/messages',
['messages' => ChatModel::get_messages()]);
2023-02-03 11:00:13 +00:00
}
static function sync()
{
json_response(hash('crc32', serialize(
ChatModel::get_messages()
)));
2023-02-03 11:00:13 +00:00
}
}