This commit is contained in:
William 2023-01-22 18:18:12 +01:00
parent 00814c6fb7
commit e1e9c9e99b
5 changed files with 34 additions and 57 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

View File

@ -27,34 +27,39 @@ Route::match('get|post','/willychat/', function() {
$default_nick = 'Willy'; $default_nick = 'Willy';
$nick = $default_nick; $nick = $default_nick;
$just_sent_message = false; $just_sent_message = false;
$errmsg = false;
if (!empty($_POST)) { if (!empty($_POST)) {
$nick = filter_input(INPUT_POST, 'nick'); $nick = filter_input(INPUT_POST, 'nick');
$text = filter_input(INPUT_POST, 'text'); $text = filter_input(INPUT_POST, 'text');
if (empty(trim($nick, ' '))) { $errmsg = (function() use (&$text, &$nick): string|false {
$nick = $default_nick;
}
if (strlen($nick) > 128) { if (empty(trim($nick, ' '))) {
view('pages/willychat/error', [ return 'You must choose a nickname.';
'error' => 'Your nickname is too long.' }
]);
die();
}
if (strlen($text) > 4096) { $nick_max_chars = 128;
view('pages/willychat/error', [ if (strlen($nick) > $nick_max_chars) {
'error' => 'Do not send EXTREMELY long messages.' return 'Your nickname is too long.';
]); }
die();
}
if (count(WillyChat::$messages) > 10) { $text_max_chars = 4096;
array_pop(WillyChat::$messages); if (strlen($text) > $text_max_chars) {
} return 'Your message is too long. ' . strlen($text) . ' out of ' . $text_max_chars . ' characters.';
}
if (!empty(trim($text, ' '))) { if (empty(trim($text, ' '))) {
return 'Message body cannot be empty.';
}
return false;
})();
if (!$errmsg) {
if (count(WillyChat::$messages) > 10) {
array_pop(WillyChat::$messages);
}
array_unshift(WillyChat::$messages, [ array_unshift(WillyChat::$messages, [
'nick' => $nick, 'nick' => $nick,
@ -68,13 +73,13 @@ Route::match('get|post','/willychat/', function() {
) )
); );
} }
$just_sent_message = true; $just_sent_message = true;
} }
view('pages/willychat/index', [ view('pages/willychat/index', [
'nick' => $nick, 'nick' => $nick,
'just_sent_message' => $just_sent_message, 'just_sent_message' => $just_sent_message,
'errmsg' => $errmsg
]); ]);
}); });

View File

@ -1,33 +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">
<title>What are you doing?</title>
<link rel="stylesheet" href="<?=url('/static/style/fonts.css')?>">
</head>
<body>
<style>
#errordialog {
background: tomato;
color: white;
background: rgba(0, 0, 0, 0.75);
padding: .75rem;
border: 1px solid #aaa;
}
a {
color: #0f0;
}
</style>
<div id="errordialog">
<h2><?=$error?></h2>
<a href="">I understand.</a>
</div>
</body>
</html>

View File

@ -37,6 +37,7 @@
padding-left: 0; padding-left: 0;
padding-right: 0; padding-right: 0;
border: 0; border: 0;
text-indent: .5rem;
} }
table { table {
border-collapse: collapse; border-collapse: collapse;
@ -44,7 +45,7 @@
width: 100%; width: 100%;
} }
td { td {
border: 1px solid #aaa; border: 1px solid silver;
} }
</style> </style>
@ -52,16 +53,20 @@
<table> <table>
<tr> <tr>
<td style="width: 15%;"> <td style="width: 15%;">
<input style="text-indent: .5rem" type="text" name="nick" id="nick" placeholder="Nickname" value="<?=htmlspecialchars($nick)?>" autocomplete="off"> <input type="text" name="nick" id="nick" placeholder="Nickname" value="<?=htmlspecialchars($nick)?>" autocomplete="off">
</td> </td>
<td style="width: 75%"> <td style="width: 75%">
<input style="text-indent: .5rem" type="textarea" name="text" id="text" placeholder="Message" autocomplete="off"> <input type="textarea" name="text" id="text" placeholder="Message" autocomplete="off">
</td> </td>
</tr> </tr>
</table> </table>
<input style="display: none;" type="submit" value="Send"> <input style="display: none;" type="submit" value="Send">
</form> </form>
<?php if ($errmsg): ?>
<p style="color: red">Error: <?=$errmsg?></p>
<?php endif; ?>
<div class="messages"> <div class="messages">
<iframe src="<?=url('/willychat/messages')?>" marginwidth="0" marginheight="0" scrolling="yes" frameborder="0"></iframe> <iframe src="<?=url('/willychat/messages')?>" marginwidth="0" marginheight="0" scrolling="yes" frameborder="0"></iframe>
</div> </div>

View File

@ -27,7 +27,7 @@
<?php foreach($messages as $message): ?> <?php foreach($messages as $message): ?>
<div> <div>
<small><?=gmdate("D M j G:i:s Y", $message['date']);?> <span style="color: yellow"><?=htmlspecialchars($message['nick'])?></span> says:</small> <small><span style="color: yellow"><?=htmlspecialchars($message['nick'])?></span> <?=gmdate("D M j G:i:s Y", $message['date']);?></small>
<p><?=htmlspecialchars($message['text'])?></p> <p><?=htmlspecialchars($message['text'])?></p>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>