battles/chat.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2018-03-01 23:21:53 +00:00
<?php
/**
* Copyright (c) 2018.
* Author: Igor Barkov <lopar.4ever@gmail.com>
* Project name: Battles-Game
*/
session_start();
2020-06-23 12:22:32 +00:00
if (empty($_SESSION['uid'])) {
2018-03-01 23:21:53 +00:00
header("Location: index.php");
}
2020-06-23 12:22:32 +00:00
require_once "config.php";
2018-03-22 19:32:48 +00:00
//include_once "functions.php";
2018-03-01 23:21:53 +00:00
2020-06-23 12:22:32 +00:00
$msg = filter_input(INPUT_POST, 'msg');
2018-03-23 22:04:55 +00:00
$uid = $_SESSION['uid'];
2020-06-23 10:49:12 +00:00
if ($msg) {
2020-06-23 13:41:40 +00:00
try { db::c()->query('INSERT INTO `chat` (`user_id`, `msg`) VALUES (?i, "?s")', $uid, $msg); }
2020-06-23 12:22:32 +00:00
catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
2020-06-23 11:36:33 +00:00
}
2020-06-23 10:49:12 +00:00
}
2018-03-02 09:29:28 +00:00
2020-06-23 11:36:33 +00:00
function show_messages()
{
try {
$chat = db::c()->query('
SELECT
`msg`,
`msgdate`,
2020-06-23 13:37:25 +00:00
(SELECT `login` FROM `users` WHERE `users`.`id` = `user_id`) AS `from`,
2020-06-23 11:36:33 +00:00
`type`
FROM `chat` ORDER BY `id` LIMIT 50');
2020-06-23 12:22:32 +00:00
while ($message = $chat->fetch_assoc()) {
$d = new DateTime($message['msgdate']);
$m = htmlspecialchars($message['msg']);
if ($message['type'] == 'sys') { /* Системка */
2020-06-23 13:49:56 +00:00
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
2020-06-23 12:22:32 +00:00
} elseif ($message['type'] == 'sms') { /* Телеграмма */
2020-06-23 13:49:56 +00:00
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span><br>', $d->format('H:i'), $m);
2020-06-23 12:22:32 +00:00
} else {
2020-06-23 13:49:56 +00:00
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $message['from'], $m);
2020-06-23 12:22:32 +00:00
}
}
2020-06-23 11:36:33 +00:00
} catch (\Krugozor\Database\Mysql\Exception $e) {
2020-06-23 12:22:32 +00:00
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
2018-03-22 18:32:37 +00:00
}
2018-03-01 23:21:53 +00:00
}
2020-06-23 12:22:32 +00:00
2018-03-23 23:08:05 +00:00
show_messages();
2018-03-01 23:21:53 +00:00
?>
2020-06-23 13:39:22 +00:00
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css">
<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>
</head>
<body>
<div>
2020-06-23 12:22:32 +00:00
<?php ?>
2020-06-23 13:39:22 +00:00
</div>
<form action="chat.php" method="post">
<input id="msg" name="msg" size="100" placeholder="Введите сообщение...">
<input type="submit" value="Отправить">
</form>
</body>