* Project name: Battles-Game
*/
session_start();
if (empty($_SESSION['uid'])) {
header("Location: index.php");
exit;
}
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 "
Ошибка: " . $e->getMessage() . "
В файле: " . $e->getFile() . " (" . $e->getLine() . ")
";
}
}
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('%s %s
', $d->format('H:i'), $m);
} elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf('[Телеграмма]: %s %s
', $d->format('H:i'), $m);
} else {
echo sprintf('%s [%s]: %s
', $d->format('H:i'), $message['from'], $m);
}
}
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "Ошибка: " . $e->getMessage() . "
В файле: " . $e->getFile() . " (" . $e->getLine() . ")
";
}
}
show_messages();
Template::header('chat');
?>