Remove chat cuz y'all can't behave!

This commit is contained in:
William 2023-02-12 19:15:30 +01:00
parent 1a0a9f635e
commit ed9d7f1511
7 changed files with 1 additions and 296 deletions

View File

@ -1,69 +0,0 @@
<?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()
)));
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace App\Model;
use App\Config;
ChatModel::init();
abstract class ChatModel
{
private static array $messages;
static function init()
{
if (!file_exists(Config::get('path_to_chat_json_file'))) {
file_put_contents(
Config::get('path_to_chat_json_file'),
json_encode([])
);
}
self::$messages = json_decode(
file_get_contents(Config::get('path_to_chat_json_file')),
true
);
}
static function get_messages(): array
{
return self::$messages;
}
static function save_message(string $nick, string $text)
{
if (count(self::$messages) > 100) {
array_pop($messages);
}
array_unshift(self::$messages, [
'nick' => $nick,
'date' => time(),
'text' => $text
]);
file_put_contents(Config::get('path_to_chat_json_file'),
json_encode(
self::$messages
)
);
}
}

View File

@ -4,5 +4,5 @@
* Copy and name it config.php to override these defaults.
*/
return [
'path_to_chat_json_file' => '/dev/shm/database.json'
// nothing here yet!
];

View File

@ -24,16 +24,5 @@ App::get('/echo/$text?',
App::get('/error',
DefaultController::error(...));
use App\Controller\ChatController;
App::group('/chat', function() {
App::form('/',
ChatController::index(...));
App::get('/messages',
ChatController::messages(...));
App::get('/sync',
ChatController::sync(...));
});
http_response_code(404);
view('errors/404');

View File

@ -1,81 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="<?=url('/static/style/fonts.css')?>">
<title>WillyChat</title>
</head>
<body>
<style>
html, body {
height: 100%;
}
body {
margin: 0;
display: flex;
flex-flow: column;
color: white;
}
body .messages {
flex: 1 1 auto;
display: flex;
}
.messages {
border: 0;
}
iframe {
width: 100%;
height: 100%;
}
input {
color: #fff;
width: 100%;
background: rgba(0, 0, 0, 0.75);
padding: .75rem;
padding-left: 0;
padding-right: 0;
border: 0;
text-indent: .5rem;
}
table {
border-collapse: collapse;
margin-bottom: .75rem;;
width: 100%;
}
td {
border: 1px solid silver;
}
</style>
<form method="post">
<table>
<tr>
<td style="width: 15%;">
<input type="text" name="nick" id="nick" placeholder="Nickname" value="<?=htmlspecialchars($nick)?>" autocomplete="off">
</td>
<td style="width: 75%">
<input type="textarea" name="text" id="text" placeholder="Message" autocomplete="off">
</td>
</tr>
</table>
<input style="display: none;" type="submit" value="Send">
</form>
<?php if ($errmsg): ?>
<p style="color: red">Error: <?=$errmsg?></p>
<?php endif; ?>
<div class="messages">
<iframe src="<?=url('./messages')?>" marginwidth="0" marginheight="0" scrolling="yes" frameborder="0"></iframe>
</div>
<script>
if (<?=$just_sent_message ? 'true' : 'false'?>) {
text.focus();
}
</script>
</body>
</html>

View File

@ -1,68 +0,0 @@
<?php
function get_time_ago_human($time) {
$time = time() - $time;
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="<?=url('/static/style/fonts.css')?>">
</head>
<body>
<style>
div {
border: 1px solid silver;
margin-bottom: .5rem;
padding: .5rem;
background: rgba(0, 0, 0, 0.75);
color: white;
word-break: break-all;
}
div > small {
color: gray;
}
div > p {
margin: 0;
margin-top: .5rem;
}
</style>
<?php foreach($messages as $message): ?>
<div>
<small><span style="color: yellow"><?=htmlspecialchars($message['nick'])?></span> <?=get_time_ago_human($message['date']) . ' ago'//gmdate("D M j G:i:s Y", $message['date']);?></small>
<p><?=htmlspecialchars($message['text'])?></p>
</div>
<?php endforeach; ?>
<script>
let hash = '';
setInterval(async () => {
let new_hash = await fetch('sync').then((response) => response.json());
if (hash == '') {
hash = new_hash;
}
if (new_hash !== hash) {
document.location.reload();
}
}, 2000);
</script>
</body>
</html>

View File

@ -1,22 +1,6 @@
</main>
<hr>
<h1 id="message-board">Message board</h1>
<img src="<?=url('/static/img/tower.gif')?>" alt="">
<p>Remember, be kind to each other</p>
<style>
.willychat {
max-width: 30rem;
max-height: 40rem;
margin: auto;
width: 100%;
height: 100%;
}
</style>
<iframe class="willychat" src="<?=url('/chat/')?>" frameborder="0" scrolling="no"></iframe>
<hr>
<br>
<br>
<img src="<?=url('/static/img/3d-spinning-toilet-smiley-emoticon-small.gif')?>" alt="the Willy Club(WC)">
<br>