battles/chat.php

78 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Copyright (c) 2018.
* Author: Igor Barkov <lopar.4ever@gmail.com>
* Project name: Battles-Game
*/
session_start();
require_once "config.php";
$msg = $_POST['msg'] ?? null;
$uid = $_SESSION['uid'] ?? null;
if ($msg) {
try {
db::c()->query('INSERT INTO `chat` (`user_id`, `msg`) VALUES (?i, "?s")', $uid, $msg);
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
}
}
function show_messages()
{
try {
$chat = db::c()->query('
SELECT
`msg`,
`msgdate`,
(SELECT `login` FROM `users` WHERE `users`.`id` = `user_id`) AS `from`,
`type`
FROM `chat` ORDER BY `id` LIMIT 50');
while ($message = $chat->fetch_assoc()) {
$d = new DateTime($message['msgdate']);
$m = htmlspecialchars($message['msg']);
if ($message['type'] == 'sys') { /* Системка */
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
} elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span><br>', $d->format('H:i'), $m);
} else {
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $message['from'], $m);
}
}
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
}
}
show_messages();
\Battles\Template::header('chat');
?>
<style>
form {
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 80%;
margin-right: .5%;
}
form input[type="submit"] {
width: 15%;
border: none;
padding: 10px;
}
</style>
<div>
<?php ?>
</div>
<form action="chat.php" method="post">
<input id="msg" name="msg" size="100" placeholder="Введите сообщение...">
<input type="submit" value="Отправить">
</form>