battles/chat.php

82 lines
2.6 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
*/
use Battles\Database\DBPDO;
use Battles\Template;
session_start();
require_once "config.php";
$msg = $_POST['msg'] ?? null;
$uid = $_SESSION['uid'] ?? null;
if ($msg) {
$db = new DBPDO();
$db->execute('INSERT INTO chat (user_id,msg) VALUES (?,?)', [$uid, $msg]);
}
function show_messages()
{
$db = new DBPDO();
$chat = $db->ofetchALL('SELECT msg,msgdate,type,s.login AS sender, r.login AS receiver, s.id AS sid, r.id AS rid FROM chat
LEFT JOIN users s on s.id = chat.user_id
LEFT JOIN users r on r.id = chat.receiver_id
WHERE r.id = ? OR r.id IS NULL OR s.id = ? ORDER BY chat.id', [$_SESSION['uid'], $_SESSION['uid']]);
$i = 0;
while ($i < count($chat)) {
$d = new DateTime($chat[$i]->msgdate);
$m = htmlspecialchars($chat[$i]->msg);
if ($chat[$i]->type == 'sys') { /* Системка */
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
} elseif ($chat[$i]->rid == $_SESSION['uid']) { /* С указанным получателем */
if ($chat[$i]->type == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">%s Телеграмма от [%s]: %s</span><br>', $d->format('d.m.Y H:i'), $chat[$i]->sender, $m);
} elseif ($chat[$i]->type == 'private') { /* Приват */
echo sprintf('<span style="background:#efe;">%s [%s] → [%s]: %s</span><br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
} else { /* Общак */
echo sprintf('%s [%s] → [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
}
} else { /* Без указанного получателя */
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $m);
}
$i++;
}
unset($i, $chat, $db);
}
Template::header('chat');
show_messages();
?>
<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>