2023-02-03 11:00:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2023-02-03 12:57:08 +00:00
|
|
|
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');
|
2023-02-03 12:57:08 +00:00
|
|
|
$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.';
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:57:08 +00:00
|
|
|
return '';
|
2023-02-03 11:00:13 +00:00
|
|
|
})();
|
|
|
|
|
2023-02-03 12:57:08 +00:00
|
|
|
if (empty($errmsg)) {
|
|
|
|
ChatModel::save_message(
|
|
|
|
$nick,
|
|
|
|
$text
|
2023-02-03 11:00:13 +00:00
|
|
|
);
|
|
|
|
}
|
2023-02-03 12:57:08 +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()
|
|
|
|
{
|
2023-02-03 12:57:08 +00:00
|
|
|
view('pages/chat/messages',
|
|
|
|
['messages' => ChatModel::get_messages()]);
|
2023-02-03 11:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function sync()
|
|
|
|
{
|
2023-02-03 12:57:08 +00:00
|
|
|
json_response(hash('crc32', serialize(
|
|
|
|
ChatModel::get_messages()
|
|
|
|
)));
|
2023-02-03 11:00:13 +00:00
|
|
|
}
|
|
|
|
}
|