battles/chat.php
Igor Barkov (iwork) 0892729efb Handle exceptions.
2020-06-23 13:49:12 +03:00

70 lines
2.3 KiB
PHP

<?php
/**
* Copyright (c) 2018.
* Author: Igor Barkov <lopar.4ever@gmail.com>
* Project name: Battles-Game
*/
session_start();
if ($_SESSION['uid'] == null) {
header("Location: index.php");
}
include_once "config.php";
//include_once "functions.php";
$msg = filter_input(INPUT_POST,'msg');
$uid = $_SESSION['uid'];
if ($msg) {
try {
db::c()->query('INSERT INTO `chat` (`uid`, `msg`) VALUES (?i, "?s")', $uid, $msg);
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "DB Error:". $e->getMessage() ."On Line:". $e->getLine();
}
}
function show_messages()
{
try {
$chat = db::c()->query('
SELECT
`msg`,
`msgdate`,
(SELECT `login` FROM `users` WHERE `users`.`id` = `uid`) AS `from`,
`type`
FROM `chat` ORDER BY `id` LIMIT 50');
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "DB Error:". $e->getMessage() ."On Line:". $e->getLine();
}
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>', $d->format('H:i'),$m).PHP_EOL;
} elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span>', $d->format('H:i'),$m).PHP_EOL;
} else {echo sprintf('%s [%s]: %s', $d->format('H:i'),$message['from'],$m).PHP_EOL;}
}
}
show_messages();
?>
<!--<!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>-->
<?php ?>
<!--</div>-->
<!--<form action="chat.php" method="post">-->
<!-- <input id="msg" name="msg" size="100" placeholder="Введите сообщение...">-->
<!-- <input type="submit" value="Отправить">-->
<!--</form>-->
<!--</body>-->