69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Model\ChatModel;
|
|
|
|
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 {
|
|
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 '';
|
|
})();
|
|
|
|
if (empty($errmsg)) {
|
|
ChatModel::save_message(
|
|
$nick,
|
|
$text
|
|
);
|
|
}
|
|
|
|
view('pages/chat/index', [
|
|
'nick' => $nick,
|
|
'just_sent_message' => true,
|
|
'errmsg' => $errmsg
|
|
]);
|
|
}
|
|
|
|
static function messages()
|
|
{
|
|
view('pages/chat/messages',
|
|
['messages' => ChatModel::get_messages()]);
|
|
}
|
|
|
|
static function sync()
|
|
{
|
|
json_response(hash('crc32', serialize(
|
|
ChatModel::get_messages()
|
|
)));
|
|
}
|
|
} |